Skip to content

Commit 23870f1

Browse files
peterz@infradead.orgingomolnar
authored andcommitted
locking/lockdep: Fix "USED" <- "IN-NMI" inversions
During the LPC RCU BoF Paul asked how come the "USED" <- "IN-NMI" detector doesn't trip over rcu_read_lock()'s lockdep annotation. Looking into this I found a very embarrasing typo in verify_lock_unused(): - if (!(class->usage_mask & LOCK_USED)) + if (!(class->usage_mask & LOCKF_USED)) fixing that will indeed cause rcu_read_lock() to insta-splat :/ The above typo means that instead of testing for: 0x100 (1 << LOCK_USED), we test for 8 (LOCK_USED), which corresponds to (1 << LOCK_ENABLED_HARDIRQ). So instead of testing for _any_ used lock, it will only match any lock used with interrupts enabled. The rcu_read_lock() annotation uses .check=0, which means it will not set any of the interrupt bits and will thus never match. In order to properly fix the situation and allow rcu_read_lock() to correctly work, split LOCK_USED into LOCK_USED and LOCK_USED_READ and by having .read users set USED_READ and test USED, pure read-recursive locks are permitted. Fixes: f6f48e1 ("lockdep: Teach lockdep about "USED" <- "IN-NMI" inversions") Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Tested-by: Masami Hiramatsu <mhiramat@kernel.org> Acked-by: Paul E. McKenney <paulmck@kernel.org> Link: https://lore.kernel.org/r/20200902160323.GK1362448@hirez.programming.kicks-ass.net
1 parent fc3abb5 commit 23870f1

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

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 |

0 commit comments

Comments
 (0)