Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which mutex lock variant should I use in Linux kernel developing?

AFAIK, the mutex API was introduced to the kernel after LDD3 (Linux device drivers 3rd edition) was written so it's not described in the book.

The book describes how to use the kernel's semaphore API for mutex functionality.

It suggest to use down_interruptable() instead of down():

You do not, as a general rule, want to use noninterruptible operations unless there truly is no alternative. Non-interruptible operations are a good way to create unkillable processes (the dreaded “D state” seen in ps), and annoy your users [Linux Device Drivers 3rd ed]

Now. here's my question:

The mutex API has two "similar" functions: mutex_lock_killable() an mutex_lock_interruptable(). Which one should I choose?

like image 622
stdcall Avatar asked Aug 18 '13 12:08

stdcall


1 Answers

Use mutex_lock_interruptible() function to allow your driver to be interrupted by any signal. This implies that your system call should be written so that it can be restarted. (Also see ERESTARTSYS.)

Use mutex_lock_killable() to allow your driver to be interrupted only by signals that actually kill the process, i.e., when the process has no opportunity to look at the results of your system call, or even to try it again.

Use mutex_lock() when you can guarantee that the mutex will not be held for a long time.

like image 174
CL. Avatar answered Mar 06 '23 01:03

CL.