Sleeping Spinlocks

Spinlocks are used in the kernel to guarantee only one thread at a time can be executing within a protected section of code. Their advantages over mutex locks are simplicity and speed of execution. However, if the lock is held too long or the odds of contention for it from other threads of execution becomes significant, spinlocks can become a performance bottleneck and a source of latency. Of course latency is a particularly important issue in real-time systems.

In order to reduce latency the PREEMPT_RT patch converts the majority of kernel spinlocks into rt_mutex locks. The underlying changes necessary to support this have mostly been upstreamed already. These include changes to reduce the scope of code executed with a lock held, and the addition of a new type to distinguish true spinlocks from the rt_mutex used inside the modified spinlock_t type. In order to minimize the changes to the kernel source the existing spinlock_t datatype and the functions which operate on it retain their old names but, when PREEMPT_RT is enabled, now refer to an rt_mutex lock. Unfortunately this is somewhat confusing when reading the code, since a spinlock_t is not really a spinlock when PREEMPT_RT is enabled.

Since sleeping spinlocks obviously need to allow preemption they cannot be locked while preemption or interrupts are disabled. If this is necessary a traditional spinlock must be used.

When an rt_mutex lock cannot be used for some reason the code must be changed to use the new raw_spinlock_t, which forces traditional spinlock behavior. It is also necessary to use the corresponding raw_* variants of the spinlock functions when operating on this newer datatype. Some older documentation on PREEMPT_RT states that the old function names are used with the new datatype and the actual lock type is detected automatically, but this is not currently the case. The datatype and functions used to operate on it must match.

See also:

McKenney, Paul E. (8/2005). A Realtime Preemption Overview. LWN.net, Articles. Retrieved from https://lwn.net/Articles/146861

Sven-Thorsten Dietrich, Daniel Walker (2005). The Evolution of Real-Time Linux. Open Source Automation Development Lab. Retrieved from https://www.osadl.org/fileadmin/events/rtlws-2005/SvenThorstenDietrich.pdf

Rostedt, Steven (2015). The RT Patch. Retrieved from http://events.linuxfoundation.org/sites/events/files/slides/linux-con-rt-into-mainline-2015.pdf