Tuesday, February 22, 2022

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

Issue

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?


Solution

The main motive for creating a mutex is simplicity and efficiency. As synchronization in bottom halves can be complicated, it is suggested that mutex be avoided in bottom halves. The design of bottom halves is not suitable for mutex. Eg. Mutex should be locked/unlocked in the same context - this would be hard to follow in case of bottom halves.

In theory you can decide to implement the whole interrupt handling different in which use of mutex in justified. Like the "threaded" interrupt handlers. http://lwn.net/Articles/380931/



Answered By - Harman
Answer Checked By - Robin (WPSolving Admin)