Is the following Linux device driver code safe, or do I need to protect access to interrupt_flag
with a spinlock?
static DECLARE_WAIT_QUEUE_HEAD(wq_head);
static int interrupt_flag = 0;
static ssize_t my_write(struct file* filp, const char* __user buffer, size_t length, loff_t* offset)
{
interrupt_flag = 0;
wait_event_interruptible(wq_head, interrupt_flag != 0);
}
static irqreturn_t handler(int irq, void* dev_id)
{
interrupt_flag = 1;
wake_up_interruptible(&wq_head);
return IRQ_HANDLED;
}
Basically, I kick off some event in my_write()
and wait for the interrupt to indicate that it completes.
If so, which form of spin_lock()
do I need to use? I thought spin_lock_irq()
was appropriate, but when I tried that I got a warning about the IRQ handler enabling interrupts.
Doesn't wait_event_interruptible
evaluate the interrupt_flag != 0
condition? That would imply that the lock should be held while it reads the flag, right?
spin_lock_irqsave()
– Milind Dumbare