Skip to content

Commit 9171072

Browse files
KAGA-KOKOIngo Molnar
authored andcommitted
locking: Introduce local_lock()
preempt_disable() and local_irq_disable/save() are in principle per CPU big kernel locks. This has several downsides: - The protection scope is unknown - Violation of protection rules is hard to detect by instrumentation - For PREEMPT_RT such sections, unless in low level critical code, can violate the preemptability constraints. To address this PREEMPT_RT introduced the concept of local_locks which are strictly per CPU. The lock operations map to preempt_disable(), local_irq_disable/save() and the enabling counterparts on non RT enabled kernels. If lockdep is enabled local locks gain a lock map which tracks the usage context. This will catch cases where an area is protected by preempt_disable() but the access also happens from interrupt context. local locks have identified quite a few such issues over the years, the most recent example is: b7d5dc2 ("random: add a spinlock_t to struct batched_entropy") Aside of the lockdep coverage this also improves code readability as it precisely annotates the protection scope. PREEMPT_RT substitutes these local locks with 'sleeping' spinlocks to protect such sections while maintaining preemtability and CPU locality. local locks can replace: - preempt_enable()/disable() pairs - local_irq_disable/enable() pairs - local_irq_save/restore() pairs They are also used to replace code which implicitly disables preemption like: - get_cpu()/put_cpu() - get_cpu_var()/put_cpu_var() with PREEMPT_RT friendly constructs. Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4f470ff commit 9171072

File tree

3 files changed

+348
-11
lines changed

3 files changed

+348
-11
lines changed

Documentation/locking/locktypes.rst

Lines changed: 204 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The kernel provides a variety of locking primitives which can be divided
1313
into two categories:
1414

1515
- Sleeping locks
16+
- CPU local locks
1617
- Spinning locks
1718

1819
This document conceptually describes these lock types and provides rules
@@ -44,9 +45,23 @@ Sleeping lock types:
4445

4546
On PREEMPT_RT kernels, these lock types are converted to sleeping locks:
4647

48+
- local_lock
4749
- spinlock_t
4850
- rwlock_t
4951

52+
53+
CPU local locks
54+
---------------
55+
56+
- local_lock
57+
58+
On non-PREEMPT_RT kernels, local_lock functions are wrappers around
59+
preemption and interrupt disabling primitives. Contrary to other locking
60+
mechanisms, disabling preemption or interrupts are pure CPU local
61+
concurrency control mechanisms and not suited for inter-CPU concurrency
62+
control.
63+
64+
5065
Spinning locks
5166
--------------
5267

@@ -67,6 +82,7 @@ can have suffixes which apply further protections:
6782
_irqsave/restore() Save and disable / restore interrupt disabled state
6883
=================== ====================================================
6984

85+
7086
Owner semantics
7187
===============
7288

@@ -139,6 +155,56 @@ implementation, thus changing the fairness:
139155
writer from starving readers.
140156

141157

158+
local_lock
159+
==========
160+
161+
local_lock provides a named scope to critical sections which are protected
162+
by disabling preemption or interrupts.
163+
164+
On non-PREEMPT_RT kernels local_lock operations map to the preemption and
165+
interrupt disabling and enabling primitives:
166+
167+
=========================== ======================
168+
local_lock(&llock) preempt_disable()
169+
local_unlock(&llock) preempt_enable()
170+
local_lock_irq(&llock) local_irq_disable()
171+
local_unlock_irq(&llock) local_irq_enable()
172+
local_lock_save(&llock) local_irq_save()
173+
local_lock_restore(&llock) local_irq_save()
174+
=========================== ======================
175+
176+
The named scope of local_lock has two advantages over the regular
177+
primitives:
178+
179+
- The lock name allows static analysis and is also a clear documentation
180+
of the protection scope while the regular primitives are scopeless and
181+
opaque.
182+
183+
- If lockdep is enabled the local_lock gains a lockmap which allows to
184+
validate the correctness of the protection. This can detect cases where
185+
e.g. a function using preempt_disable() as protection mechanism is
186+
invoked from interrupt or soft-interrupt context. Aside of that
187+
lockdep_assert_held(&llock) works as with any other locking primitive.
188+
189+
local_lock and PREEMPT_RT
190+
-------------------------
191+
192+
PREEMPT_RT kernels map local_lock to a per-CPU spinlock_t, thus changing
193+
semantics:
194+
195+
- All spinlock_t changes also apply to local_lock.
196+
197+
local_lock usage
198+
----------------
199+
200+
local_lock should be used in situations where disabling preemption or
201+
interrupts is the appropriate form of concurrency control to protect
202+
per-CPU data structures on a non PREEMPT_RT kernel.
203+
204+
local_lock is not suitable to protect against preemption or interrupts on a
205+
PREEMPT_RT kernel due to the PREEMPT_RT specific spinlock_t semantics.
206+
207+
142208
raw_spinlock_t and spinlock_t
143209
=============================
144210

@@ -258,10 +324,82 @@ implementation, thus changing semantics:
258324
PREEMPT_RT caveats
259325
==================
260326

327+
local_lock on RT
328+
----------------
329+
330+
The mapping of local_lock to spinlock_t on PREEMPT_RT kernels has a few
331+
implications. For example, on a non-PREEMPT_RT kernel the following code
332+
sequence works as expected::
333+
334+
local_lock_irq(&local_lock);
335+
raw_spin_lock(&lock);
336+
337+
and is fully equivalent to::
338+
339+
raw_spin_lock_irq(&lock);
340+
341+
On a PREEMPT_RT kernel this code sequence breaks because local_lock_irq()
342+
is mapped to a per-CPU spinlock_t which neither disables interrupts nor
343+
preemption. The following code sequence works perfectly correct on both
344+
PREEMPT_RT and non-PREEMPT_RT kernels::
345+
346+
local_lock_irq(&local_lock);
347+
spin_lock(&lock);
348+
349+
Another caveat with local locks is that each local_lock has a specific
350+
protection scope. So the following substitution is wrong::
351+
352+
func1()
353+
{
354+
local_irq_save(flags); -> local_lock_irqsave(&local_lock_1, flags);
355+
func3();
356+
local_irq_restore(flags); -> local_lock_irqrestore(&local_lock_1, flags);
357+
}
358+
359+
func2()
360+
{
361+
local_irq_save(flags); -> local_lock_irqsave(&local_lock_2, flags);
362+
func3();
363+
local_irq_restore(flags); -> local_lock_irqrestore(&local_lock_2, flags);
364+
}
365+
366+
func3()
367+
{
368+
lockdep_assert_irqs_disabled();
369+
access_protected_data();
370+
}
371+
372+
On a non-PREEMPT_RT kernel this works correctly, but on a PREEMPT_RT kernel
373+
local_lock_1 and local_lock_2 are distinct and cannot serialize the callers
374+
of func3(). Also the lockdep assert will trigger on a PREEMPT_RT kernel
375+
because local_lock_irqsave() does not disable interrupts due to the
376+
PREEMPT_RT-specific semantics of spinlock_t. The correct substitution is::
377+
378+
func1()
379+
{
380+
local_irq_save(flags); -> local_lock_irqsave(&local_lock, flags);
381+
func3();
382+
local_irq_restore(flags); -> local_lock_irqrestore(&local_lock, flags);
383+
}
384+
385+
func2()
386+
{
387+
local_irq_save(flags); -> local_lock_irqsave(&local_lock, flags);
388+
func3();
389+
local_irq_restore(flags); -> local_lock_irqrestore(&local_lock, flags);
390+
}
391+
392+
func3()
393+
{
394+
lockdep_assert_held(&local_lock);
395+
access_protected_data();
396+
}
397+
398+
261399
spinlock_t and rwlock_t
262400
-----------------------
263401

264-
These changes in spinlock_t and rwlock_t semantics on PREEMPT_RT kernels
402+
The changes in spinlock_t and rwlock_t semantics on PREEMPT_RT kernels
265403
have a few implications. For example, on a non-PREEMPT_RT kernel the
266404
following code sequence works as expected::
267405

@@ -282,9 +420,61 @@ local_lock mechanism. Acquiring the local_lock pins the task to a CPU,
282420
allowing things like per-CPU interrupt disabled locks to be acquired.
283421
However, this approach should be used only where absolutely necessary.
284422

423+
A typical scenario is protection of per-CPU variables in thread context::
285424

286-
raw_spinlock_t
287-
--------------
425+
struct foo *p = get_cpu_ptr(&var1);
426+
427+
spin_lock(&p->lock);
428+
p->count += this_cpu_read(var2);
429+
430+
This is correct code on a non-PREEMPT_RT kernel, but on a PREEMPT_RT kernel
431+
this breaks. The PREEMPT_RT-specific change of spinlock_t semantics does
432+
not allow to acquire p->lock because get_cpu_ptr() implicitly disables
433+
preemption. The following substitution works on both kernels::
434+
435+
struct foo *p;
436+
437+
migrate_disable();
438+
p = this_cpu_ptr(&var1);
439+
spin_lock(&p->lock);
440+
p->count += this_cpu_read(var2);
441+
442+
On a non-PREEMPT_RT kernel migrate_disable() maps to preempt_disable()
443+
which makes the above code fully equivalent. On a PREEMPT_RT kernel
444+
migrate_disable() ensures that the task is pinned on the current CPU which
445+
in turn guarantees that the per-CPU access to var1 and var2 are staying on
446+
the same CPU.
447+
448+
The migrate_disable() substitution is not valid for the following
449+
scenario::
450+
451+
func()
452+
{
453+
struct foo *p;
454+
455+
migrate_disable();
456+
p = this_cpu_ptr(&var1);
457+
p->val = func2();
458+
459+
While correct on a non-PREEMPT_RT kernel, this breaks on PREEMPT_RT because
460+
here migrate_disable() does not protect against reentrancy from a
461+
preempting task. A correct substitution for this case is::
462+
463+
func()
464+
{
465+
struct foo *p;
466+
467+
local_lock(&foo_lock);
468+
p = this_cpu_ptr(&var1);
469+
p->val = func2();
470+
471+
On a non-PREEMPT_RT kernel this protects against reentrancy by disabling
472+
preemption. On a PREEMPT_RT kernel this is achieved by acquiring the
473+
underlying per-CPU spinlock.
474+
475+
476+
raw_spinlock_t on RT
477+
--------------------
288478

289479
Acquiring a raw_spinlock_t disables preemption and possibly also
290480
interrupts, so the critical section must avoid acquiring a regular
@@ -325,22 +515,25 @@ Lock type nesting rules
325515

326516
The most basic rules are:
327517

328-
- Lock types of the same lock category (sleeping, spinning) can nest
329-
arbitrarily as long as they respect the general lock ordering rules to
330-
prevent deadlocks.
518+
- Lock types of the same lock category (sleeping, CPU local, spinning)
519+
can nest arbitrarily as long as they respect the general lock ordering
520+
rules to prevent deadlocks.
521+
522+
- Sleeping lock types cannot nest inside CPU local and spinning lock types.
331523

332-
- Sleeping lock types cannot nest inside spinning lock types.
524+
- CPU local and spinning lock types can nest inside sleeping lock types.
333525

334-
- Spinning lock types can nest inside sleeping lock types.
526+
- Spinning lock types can nest inside all lock types
335527

336528
These constraints apply both in PREEMPT_RT and otherwise.
337529

338530
The fact that PREEMPT_RT changes the lock category of spinlock_t and
339-
rwlock_t from spinning to sleeping means that they cannot be acquired while
340-
holding a raw spinlock. This results in the following nesting ordering:
531+
rwlock_t from spinning to sleeping and substitutes local_lock with a
532+
per-CPU spinlock_t means that they cannot be acquired while holding a raw
533+
spinlock. This results in the following nesting ordering:
341534

342535
1) Sleeping locks
343-
2) spinlock_t and rwlock_t
536+
2) spinlock_t, rwlock_t, local_lock
344537
3) raw_spinlock_t and bit spinlocks
345538

346539
Lockdep will complain if these constraints are violated, both in

include/linux/local_lock.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _LINUX_LOCAL_LOCK_H
3+
#define _LINUX_LOCAL_LOCK_H
4+
5+
#include <linux/local_lock_internal.h>
6+
7+
/**
8+
* local_lock_init - Runtime initialize a lock instance
9+
*/
10+
#define local_lock_init(lock) __local_lock_init(lock)
11+
12+
/**
13+
* local_lock - Acquire a per CPU local lock
14+
* @lock: The lock variable
15+
*/
16+
#define local_lock(lock) __local_lock(lock)
17+
18+
/**
19+
* local_lock_irq - Acquire a per CPU local lock and disable interrupts
20+
* @lock: The lock variable
21+
*/
22+
#define local_lock_irq(lock) __local_lock_irq(lock)
23+
24+
/**
25+
* local_lock_irqsave - Acquire a per CPU local lock, save and disable
26+
* interrupts
27+
* @lock: The lock variable
28+
* @flags: Storage for interrupt flags
29+
*/
30+
#define local_lock_irqsave(lock, flags) \
31+
__local_lock_irqsave(lock, flags)
32+
33+
/**
34+
* local_unlock - Release a per CPU local lock
35+
* @lock: The lock variable
36+
*/
37+
#define local_unlock(lock) __local_unlock(lock)
38+
39+
/**
40+
* local_unlock_irq - Release a per CPU local lock and enable interrupts
41+
* @lock: The lock variable
42+
*/
43+
#define local_unlock_irq(lock) __local_unlock_irq(lock)
44+
45+
/**
46+
* local_unlock_irqrestore - Release a per CPU local lock and restore
47+
* interrupt flags
48+
* @lock: The lock variable
49+
* @flags: Interrupt flags to restore
50+
*/
51+
#define local_unlock_irqrestore(lock, flags) \
52+
__local_unlock_irqrestore(lock, flags)
53+
54+
#endif

0 commit comments

Comments
 (0)