clinux-kernellockingnetfiltersoftirq

Sharing data between softirq and process context


I am developing a kernel module that shares data structures between a softirq (netfilter pre-routing hook) and a user context (within an ioctl call).

After reading this link, I know I need to disable the software interrupts in the user context when modifying the data (using either spin_lock_bh or spin_lock_irqsave) and re-enable them once the operations on the share data are completed.

However, I am un-sure whether I need to grab the lock in the softirq handler function. Do I need to grab the lock here as well (with spin_lock) ?

It is my understanding that I only need to use a spin lock in a softirq handler if sharing data with another softirq or hardirq. Is my understanding correct?


Solution

  • You must obtain a lock if you sharing data with any code, which can be executed on the same time. Kernel might execute both your softirq and your ioctl handler, so you must obtain a lock.

    When you disable interrupts with either spin_lock_bh or spin_lock_irqsave, interrupts are disabled on current processor only. So, it's OK to handle interrupts on another one.