Stopping a Kernel thread using a timer in linux -
i trying stop kernel thread calling kthread_stop() inside function called timer.when load module kernel, kthread starting , stopping after specified time mentioned in timer giving error message in log.
can me solve problem new kernel thread programming.this code
#include <linux/kernel.h> #include <linux/module.h> #include <linux/timer.h> #include <linux/kthread.h> #include <linux/sched.h> module_license("gpl"); struct task_struct *task; static struct timer_list my_timer; int thread_function(void *data) { printk(kern_alert"in thread function"); while(!kthread_should_stop()){ schedule(); } printk(kern_alert"after schedule\n"); return 1; } void my_timer_callback( unsigned long data ){ printk( "my_timer_callback called (%ld).\n", jiffies ); printk(kern_alert"thread stopped\n"); kthread_stop(task); } int init_module( void ){ int ret; printk("timer module installing\n"); //my_timer.function, my_timer.data setup_timer( &my_timer, my_timer_callback, 0 ); printk( "starting timer fire in 2000ms (%ld)\n", jiffies ); ret = mod_timer( &my_timer, jiffies + msecs_to_jiffies(2000) ); if (ret) printk("error in mod_timer\n"); printk(kern_info"-------------------- thread start------------------------"); task = kthread_run(thread_function,null,"kerneltthread"); printk(kern_alert"kernel thread name: %s\n",task->comm); return 0; } void cleanup_module( void ){ int ret; ret = del_timer( &my_timer ); if (ret) printk("the timer still in use...\n"); printk("timer module uninstalling\n"); return; } module_license("gpl");
this output message in log
aug 5 16:35:31 brao kernel: timer module installing aug 5 16:35:31 brao kernel: starting timer fire in 2000ms (4299209850) aug 5 16:35:31 brao kernel: -------------------- thread start------------------------ aug 5 16:35:31 brao kernel: kernel thread name: kerneltthread aug 5 16:35:31 brao kernel: in thread function aug 5 16:35:33 brao kernel: my_timer_callback called (4299211856). aug 5 16:35:33 brao kernel: thread stopped aug 5 16:35:33 brao kernel: bug: scheduling while atomic: swapper/3/0/0x10000100 aug 5 16:35:33 brao kernel: after schedule
timer's function executed in softirq context, because of shouldn't sleep. my_timer_callback
calls kthread_stop
function, waits until kthread finished, sleep.
so, scheduling while atomic
bug arised: function sleeps while shouldn't because of atomic context.
Comments
Post a Comment