Hi Evenryone,

I am facing an issue (buffer overflow) when I create kernel thread which actually blinks led for every 500ms.

My implementation of kernel thread is as below

Code:
static int led_control_thread()
{
    int led_on = 0;

 
    lock_kernel();

    daemonize("LED_Thread");
    allow_signal(SIGTERM);
    siginitsetinv(&current->blocked, sigmask(SIGKILL)|sigmask(SIGINT)|\
    unlock_kernel();

    while(!PDC->led.thread_should_stop && !signal_pending(current)) 
    {
        /* Sleep for 500 mili seconds */
        set_current_state(TASK_UNINTERRUPTIBLE); 
        schedule_timeout(HZ/2);
         
        ... 
    
        ... 
        ... 
        ... 

        LED made on and off (alternatively)
        ..
        ..
        ..
    }
     
    set_led_on(1);
    atomic_set(&(PDC->led.thread_created), 0);
    complete_and_exit(&(PDC->led.thread_exit), 0);

    return 0;
}


DWORD led_blink(Bool bOn)
{
    int err=0;

    if(bOn) 
    {
        // start LED flashing thread
        if( atomic_add_unless(&(PDC->led.thread_created), 1, 1) ) 
	{
            PDC->led.last_jiffies = 0;
            PDC->led.thread_should_stop = 0;
            init_completion(&(PDC->led.thread_exit));
            kernel_thread(led_control_thread, 0, 0);
        }
    }
    else 
	{
        // stop LED flashing thread
        if( atomic_add_unless(&(PDC->led.thread_created), -1, 0) ) 
		{
            PDC->led.thread_should_stop = 1;
            wait_for_completion(&(PDC->led.thread_exit));           
        }
    }
    return(err);
}
From above code,PDC is the global structure, led_blink function will be called with input 0 or 1. Value 1 is passed to make led blink and 0 to put ON led constantly (no blinking).

This code is working fine for short period, but in long run, causing issues with other kernel threads. Say if there is any background recording going on in my embedded device in which this code is used, in long run, this kernel thread is interrupting the recording which shouldn't happend.

When investigated this thread is hanging on for more than 2 seconds and leading to issue in my system. Why does it hang for more than 2 seconds ?

I suspect following lines may be causing the thread to hang on for more than 2 seconds.

Code:
     set_current_state(TASK_UNINTERRUPTIBLE); 
        schedule_timeout(HZ/2);
Could you please let me know whats wrong with my code ?

Let me know if you need any additional information for better understanding of the problem.

Thanks in advance,
MMK