Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding sleep while holding a spinlock

I've recently read section 5.5.2 (Spinlocks and Atomic Context) of LDDv3 book:

Avoiding sleep while holding a lock can be more difficult; many kernel functions can sleep, and this behavior is not always well documented. Copying data to or from user space is an obvious example: the required user-space page may need to be swapped in from the disk before the copy can proceed, and that operation clearly requires a sleep. Just about any operation that must allocate memory can sleep; kmalloc can decide to give up the processor, and wait for more memory to become available unless it is explicitly told not to. Sleeps can happen in surprising places; writing code that will execute under a spinlock requires paying attention to every function that you call.

It's clear to me that spinlocks must always be held for the minimum time possible and I think that it's relatively easy to write correct spinlock-using code from scratch.

Suppose, however, that we have a big project where spinlocks are widely used. How can we make sure that functions called from critical sections protected by spinlocks will never sleep?

Thanks in advance!

like image 438
Dmitry Krivenok Avatar asked Oct 26 '22 07:10

Dmitry Krivenok


1 Answers

What about enabling "Sleep-inside-spinlock checking" for your kernel ? It is usually found under Kernel Debugging when you run make config. You might also try to duplicate its behavior in your code.

like image 107
Bandan Avatar answered Nov 03 '22 05:11

Bandan