Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Linux kernel, why a mutex cannot be acquired in bottom half?

I am reading Linux Kernel Development and get confused by the differences between mutex and semaphore.

The author says:

A mutex cannot be acquired by an interrupt handler or bottom half

I know a mutex may lead to sleep, and interrupt handler is not running in any specific process context so mutex or semaphore is not allowed. But bottom half can be implemented with work queues and it can sleep.

So, why a mutex can't be acquired in bottom half? Is simplicity and efficiency concerned here or something else?

like image 743
TrueWill Avatar asked Nov 20 '12 05:11

TrueWill


1 Answers

Mutex/semaphore locking can sleep, but BHs are designed not to sleep. Softirqs are asynchronously checked for execution in many places. For example they can execute every time you are restoring BH (like spin_unlock_bh). Causing such code to sleep on a mutex is a very bad idea. If you sleep while holding a BH spinlock you can cause other BH code to sleep and perhaps even deadlock the entire system.

From this point of view workqueues are not considered BH, they run in the context of kernel threads which are free to sleep. So a mutex is OK for workqueues but not for tasklets.

BH is a vague term, I find it helpful to think of the linux kernel as having three execution contexts: user (including kernel threads), softirq and hardirq. Preempting by each of these can be controlled with a set of bits in preempt_count.

like image 192
cdleonard Avatar answered Oct 23 '22 03:10

cdleonard