Skip to content

Commit 3d49167

Browse files
committed
Merge tag 'locking_urgent_for_v5.9_rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull locking fixes from Borislav Petkov: "Two fixes from the locking/urgent pile: - Fix lockdep's detection of "USED" <- "IN-NMI" inversions (Peter Zijlstra) - Make percpu-rwsem operations on the semaphore's ->read_count IRQ-safe because it can be used in an IRQ context (Hou Tao)" * tag 'locking_urgent_for_v5.9_rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: locking/percpu-rwsem: Use this_cpu_{inc,dec}() for read_count locking/lockdep: Fix "USED" <- "IN-NMI" inversions
2 parents 5674d81 + e6b1a44 commit 3d49167

4 files changed

Lines changed: 37 additions & 12 deletions

File tree

include/linux/percpu-rwsem.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
6060
* anything we did within this RCU-sched read-size critical section.
6161
*/
6262
if (likely(rcu_sync_is_idle(&sem->rss)))
63-
__this_cpu_inc(*sem->read_count);
63+
this_cpu_inc(*sem->read_count);
6464
else
6565
__percpu_down_read(sem, false); /* Unconditional memory barrier */
6666
/*
@@ -79,7 +79,7 @@ static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
7979
* Same as in percpu_down_read().
8080
*/
8181
if (likely(rcu_sync_is_idle(&sem->rss)))
82-
__this_cpu_inc(*sem->read_count);
82+
this_cpu_inc(*sem->read_count);
8383
else
8484
ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */
8585
preempt_enable();
@@ -103,7 +103,7 @@ static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
103103
* Same as in percpu_down_read().
104104
*/
105105
if (likely(rcu_sync_is_idle(&sem->rss))) {
106-
__this_cpu_dec(*sem->read_count);
106+
this_cpu_dec(*sem->read_count);
107107
} else {
108108
/*
109109
* slowpath; reader will only ever wake a single blocked
@@ -115,7 +115,7 @@ static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
115115
* aggregate zero, as that is the only time it matters) they
116116
* will also see our critical section.
117117
*/
118-
__this_cpu_dec(*sem->read_count);
118+
this_cpu_dec(*sem->read_count);
119119
rcuwait_wake_up(&sem->writer);
120120
}
121121
preempt_enable();

kernel/locking/lockdep.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3969,13 +3969,18 @@ static int separate_irq_context(struct task_struct *curr,
39693969
static int mark_lock(struct task_struct *curr, struct held_lock *this,
39703970
enum lock_usage_bit new_bit)
39713971
{
3972-
unsigned int new_mask = 1 << new_bit, ret = 1;
3972+
unsigned int old_mask, new_mask, ret = 1;
39733973

39743974
if (new_bit >= LOCK_USAGE_STATES) {
39753975
DEBUG_LOCKS_WARN_ON(1);
39763976
return 0;
39773977
}
39783978

3979+
if (new_bit == LOCK_USED && this->read)
3980+
new_bit = LOCK_USED_READ;
3981+
3982+
new_mask = 1 << new_bit;
3983+
39793984
/*
39803985
* If already set then do not dirty the cacheline,
39813986
* nor do any checks:
@@ -3988,13 +3993,22 @@ static int mark_lock(struct task_struct *curr, struct held_lock *this,
39883993
/*
39893994
* Make sure we didn't race:
39903995
*/
3991-
if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
3992-
graph_unlock();
3993-
return 1;
3994-
}
3996+
if (unlikely(hlock_class(this)->usage_mask & new_mask))
3997+
goto unlock;
39953998

3999+
old_mask = hlock_class(this)->usage_mask;
39964000
hlock_class(this)->usage_mask |= new_mask;
39974001

4002+
/*
4003+
* Save one usage_traces[] entry and map both LOCK_USED and
4004+
* LOCK_USED_READ onto the same entry.
4005+
*/
4006+
if (new_bit == LOCK_USED || new_bit == LOCK_USED_READ) {
4007+
if (old_mask & (LOCKF_USED | LOCKF_USED_READ))
4008+
goto unlock;
4009+
new_bit = LOCK_USED;
4010+
}
4011+
39984012
if (!(hlock_class(this)->usage_traces[new_bit] = save_trace()))
39994013
return 0;
40004014

@@ -4008,6 +4022,7 @@ static int mark_lock(struct task_struct *curr, struct held_lock *this,
40084022
return 0;
40094023
}
40104024

4025+
unlock:
40114026
graph_unlock();
40124027

40134028
/*
@@ -4942,12 +4957,20 @@ static void verify_lock_unused(struct lockdep_map *lock, struct held_lock *hlock
49424957
{
49434958
#ifdef CONFIG_PROVE_LOCKING
49444959
struct lock_class *class = look_up_lock_class(lock, subclass);
4960+
unsigned long mask = LOCKF_USED;
49454961

49464962
/* if it doesn't have a class (yet), it certainly hasn't been used yet */
49474963
if (!class)
49484964
return;
49494965

4950-
if (!(class->usage_mask & LOCK_USED))
4966+
/*
4967+
* READ locks only conflict with USED, such that if we only ever use
4968+
* READ locks, there is no deadlock possible -- RCU.
4969+
*/
4970+
if (!hlock->read)
4971+
mask |= LOCKF_USED_READ;
4972+
4973+
if (!(class->usage_mask & mask))
49514974
return;
49524975

49534976
hlock->class_idx = class - lock_classes;

kernel/locking/lockdep_internals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ enum lock_usage_bit {
1919
#include "lockdep_states.h"
2020
#undef LOCKDEP_STATE
2121
LOCK_USED,
22+
LOCK_USED_READ,
2223
LOCK_USAGE_STATES
2324
};
2425

@@ -40,6 +41,7 @@ enum {
4041
#include "lockdep_states.h"
4142
#undef LOCKDEP_STATE
4243
__LOCKF(USED)
44+
__LOCKF(USED_READ)
4345
};
4446

4547
#define LOCKDEP_STATE(__STATE) LOCKF_ENABLED_##__STATE |

kernel/locking/percpu-rwsem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ EXPORT_SYMBOL_GPL(percpu_free_rwsem);
4545

4646
static bool __percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
4747
{
48-
__this_cpu_inc(*sem->read_count);
48+
this_cpu_inc(*sem->read_count);
4949

5050
/*
5151
* Due to having preemption disabled the decrement happens on
@@ -71,7 +71,7 @@ static bool __percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
7171
if (likely(!atomic_read_acquire(&sem->block)))
7272
return true;
7373

74-
__this_cpu_dec(*sem->read_count);
74+
this_cpu_dec(*sem->read_count);
7575

7676
/* Prod writer to re-evaluate readers_active_check() */
7777
rcuwait_wake_up(&sem->writer);

0 commit comments

Comments
 (0)