Search code examples
clinuxtimerlinux-kernelspinlock

Synchronisation between process context and timer function


I want to update a data structure atomically in both process context (in queuecommand function to be specific) and timer function. In process context, should I use spin_lock_bh or spin_lock_irq or just spin_lock?

As per my understanding, we should use spin_lock_bh in queuecommand (process context) and just spin_lock in timer function. Am I correct?


Solution

  • If I understand correctly, it is about timer_list (bottom half context). Then your assumption is correct: yes, it would be sufficient to use spin_lock_bh in process context and spin_lock in timer handler (function of timer_list). But this is true only if that lock (and the corresponding data structure) is used only in the mentioned contexts is not used it interrupt handler. If so, you need e.g. spin_lock_irq.

    Also keep in mind that you shouldn't use sleep-able functions inside spin_lock*-spin_unlock*.

    There are a lot of examples in Linux kernel sources, e.g.: