In my own kernel module, I am trying to init a kthread within an interrupt handler function.
in the global scope I have:
static struct task_struct *thread1;
the function handler of the irq is:
static irqreturn_t* func_irq_handler (int irq, void *dev_id)
{
printk("irq handler ... \n");
thread1 = kthread_create(thread_function,NULL,"my_thread");
if ((thread1)) {
printk(KERN_INFO "%s\n" , __FUNCTION__);
}
return IRQ_HANDLED;
}
and the thread function is:
static thread_function(void)
{
unsigned long j1=jiffies+20000;
int delay = 60*HZ;
printk("%s \n",__FUNCTION__);
while (time_before(jiffies,j1)) {
schedule();
printk(KERN_INFO "after schedule\n");
}
}
the request_irq looks like this:
request_irq(irq,func_irq_handler,IRQF_TRIGGER_HIGH | IRQF_TRIGGER_RISING ,"test_irq",(void*)&my_miscdev);
why do I get this error:
BUG: scheduling while atomic: swapper
I would imagine that creating a thread requires interaction with the thread scheduler, which is not allowed at interrupt/atomic context.
A better approach would be to create your kernel thread elsewhere, and queue interrupt request processing to it.