Skip to content

Commit 6005606

Browse files
committed
Merge tag 'locking-core-2020-06-01' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull locking updates from Ingo Molnar: "The biggest change to core locking facilities in this cycle is the introduction of local_lock_t - this primitive comes from the -rt project and identifies CPU-local locking dependencies normally handled opaquely beind preempt_disable() or local_irq_save/disable() critical sections. The generated code on mainline kernels doesn't change as a result, but still there are benefits: improved debugging and better documentation of data structure accesses. The new local_lock_t primitives are introduced and then utilized in a couple of kernel subsystems. No change in functionality is intended. There's also other smaller changes and cleanups" * tag 'locking-core-2020-06-01' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: zram: Use local lock to protect per-CPU data zram: Allocate struct zcomp_strm as per-CPU memory connector/cn_proc: Protect send_msg() with a local lock squashfs: Make use of local lock in multi_cpu decompressor mm/swap: Use local_lock for protection radix-tree: Use local_lock for protection locking: Introduce local_lock() locking/lockdep: Replace zero-length array with flexible-array locking/rtmutex: Remove unused rt_mutex_cmpxchg_relaxed()
2 parents 2227e5b + 19f545b commit 6005606

File tree

15 files changed

+502
-110
lines changed

15 files changed

+502
-110
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

drivers/block/zram/zcomp.c

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,16 @@ static void zcomp_strm_free(struct zcomp_strm *zstrm)
3737
if (!IS_ERR_OR_NULL(zstrm->tfm))
3838
crypto_free_comp(zstrm->tfm);
3939
free_pages((unsigned long)zstrm->buffer, 1);
40-
kfree(zstrm);
40+
zstrm->tfm = NULL;
41+
zstrm->buffer = NULL;
4142
}
4243

4344
/*
44-
* allocate new zcomp_strm structure with ->tfm initialized by
45-
* backend, return NULL on error
45+
* Initialize zcomp_strm structure with ->tfm initialized by backend, and
46+
* ->buffer. Return a negative value on error.
4647
*/
47-
static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
48+
static int zcomp_strm_init(struct zcomp_strm *zstrm, struct zcomp *comp)
4849
{
49-
struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
50-
if (!zstrm)
51-
return NULL;
52-
5350
zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
5451
/*
5552
* allocate 2 pages. 1 for compressed data, plus 1 extra for the
@@ -58,9 +55,9 @@ static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
5855
zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
5956
if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) {
6057
zcomp_strm_free(zstrm);
61-
zstrm = NULL;
58+
return -ENOMEM;
6259
}
63-
return zstrm;
60+
return 0;
6461
}
6562

6663
bool zcomp_available_algorithm(const char *comp)
@@ -113,12 +110,13 @@ ssize_t zcomp_available_show(const char *comp, char *buf)
113110

114111
struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
115112
{
116-
return *get_cpu_ptr(comp->stream);
113+
local_lock(&comp->stream->lock);
114+
return this_cpu_ptr(comp->stream);
117115
}
118116

119117
void zcomp_stream_put(struct zcomp *comp)
120118
{
121-
put_cpu_ptr(comp->stream);
119+
local_unlock(&comp->stream->lock);
122120
}
123121

124122
int zcomp_compress(struct zcomp_strm *zstrm,
@@ -159,36 +157,32 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
159157
{
160158
struct zcomp *comp = hlist_entry(node, struct zcomp, node);
161159
struct zcomp_strm *zstrm;
160+
int ret;
162161

163-
if (WARN_ON(*per_cpu_ptr(comp->stream, cpu)))
164-
return 0;
162+
zstrm = per_cpu_ptr(comp->stream, cpu);
163+
local_lock_init(&zstrm->lock);
165164

166-
zstrm = zcomp_strm_alloc(comp);
167-
if (IS_ERR_OR_NULL(zstrm)) {
165+
ret = zcomp_strm_init(zstrm, comp);
166+
if (ret)
168167
pr_err("Can't allocate a compression stream\n");
169-
return -ENOMEM;
170-
}
171-
*per_cpu_ptr(comp->stream, cpu) = zstrm;
172-
return 0;
168+
return ret;
173169
}
174170

175171
int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
176172
{
177173
struct zcomp *comp = hlist_entry(node, struct zcomp, node);
178174
struct zcomp_strm *zstrm;
179175

180-
zstrm = *per_cpu_ptr(comp->stream, cpu);
181-
if (!IS_ERR_OR_NULL(zstrm))
182-
zcomp_strm_free(zstrm);
183-
*per_cpu_ptr(comp->stream, cpu) = NULL;
176+
zstrm = per_cpu_ptr(comp->stream, cpu);
177+
zcomp_strm_free(zstrm);
184178
return 0;
185179
}
186180

187181
static int zcomp_init(struct zcomp *comp)
188182
{
189183
int ret;
190184

191-
comp->stream = alloc_percpu(struct zcomp_strm *);
185+
comp->stream = alloc_percpu(struct zcomp_strm);
192186
if (!comp->stream)
193187
return -ENOMEM;
194188

drivers/block/zram/zcomp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55

66
#ifndef _ZCOMP_H_
77
#define _ZCOMP_H_
8+
#include <linux/local_lock.h>
89

910
struct zcomp_strm {
11+
/* The members ->buffer and ->tfm are protected by ->lock. */
12+
local_lock_t lock;
1013
/* compression/decompression buffer */
1114
void *buffer;
1215
struct crypto_comp *tfm;
1316
};
1417

1518
/* dynamic per-device compression frontend */
1619
struct zcomp {
17-
struct zcomp_strm * __percpu *stream;
20+
struct zcomp_strm __percpu *stream;
1821
const char *name;
1922
struct hlist_node node;
2023
};

0 commit comments

Comments
 (0)