Skip to content

Commit 90b05e4

Browse files
ideakgregkh
authored andcommitted
locking/lockdep: Fix OOO unlock when hlocks need merging
[ Upstream commit 8c8889d ] The sequence static DEFINE_WW_CLASS(test_ww_class); struct ww_acquire_ctx ww_ctx; struct ww_mutex ww_lock_a; struct ww_mutex ww_lock_b; struct mutex lock_c; struct mutex lock_d; ww_acquire_init(&ww_ctx, &test_ww_class); ww_mutex_init(&ww_lock_a, &test_ww_class); ww_mutex_init(&ww_lock_b, &test_ww_class); mutex_init(&lock_c); ww_mutex_lock(&ww_lock_a, &ww_ctx); mutex_lock(&lock_c); ww_mutex_lock(&ww_lock_b, &ww_ctx); mutex_unlock(&lock_c); (*) ww_mutex_unlock(&ww_lock_b); ww_mutex_unlock(&ww_lock_a); ww_acquire_fini(&ww_ctx); triggers the following WARN in __lock_release() when doing the unlock at *: DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1); The problem is that the WARN check doesn't take into account the merging of ww_lock_a and ww_lock_b which results in decreasing curr->lockdep_depth by 2 not only 1. Note that the following sequence doesn't trigger the WARN, since there won't be any hlock merging. ww_acquire_init(&ww_ctx, &test_ww_class); ww_mutex_init(&ww_lock_a, &test_ww_class); ww_mutex_init(&ww_lock_b, &test_ww_class); mutex_init(&lock_c); mutex_init(&lock_d); ww_mutex_lock(&ww_lock_a, &ww_ctx); mutex_lock(&lock_c); mutex_lock(&lock_d); ww_mutex_lock(&ww_lock_b, &ww_ctx); mutex_unlock(&lock_d); ww_mutex_unlock(&ww_lock_b); ww_mutex_unlock(&ww_lock_a); mutex_unlock(&lock_c); ww_acquire_fini(&ww_ctx); In general both of the above two sequences are valid and shouldn't trigger any lockdep warning. Fix this by taking the decrement due to the hlock merging into account during lock release and hlock class re-setting. Merging can't happen during lock downgrading since there won't be a new possibility to merge hlocks in that case, so add a WARN if merging still happens then. Signed-off-by: Imre Deak <imre.deak@intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Will Deacon <will.deacon@arm.com> Cc: ville.syrjala@linux.intel.com Link: https://lkml.kernel.org/r/20190524201509.9199-1-imre.deak@intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a2b46e7 commit 90b05e4

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

‎kernel/locking/lockdep.c‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3623,7 +3623,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
36233623
hlock->references = 2;
36243624
}
36253625

3626-
return 1;
3626+
return 2;
36273627
}
36283628
}
36293629

@@ -3829,22 +3829,33 @@ static struct held_lock *find_held_lock(struct task_struct *curr,
38293829
}
38303830

38313831
static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3832-
int idx)
3832+
int idx, unsigned int *merged)
38333833
{
38343834
struct held_lock *hlock;
3835+
int first_idx = idx;
38353836

38363837
if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
38373838
return 0;
38383839

38393840
for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3840-
if (!__lock_acquire(hlock->instance,
3841+
switch (__lock_acquire(hlock->instance,
38413842
hlock_class(hlock)->subclass,
38423843
hlock->trylock,
38433844
hlock->read, hlock->check,
38443845
hlock->hardirqs_off,
38453846
hlock->nest_lock, hlock->acquire_ip,
3846-
hlock->references, hlock->pin_count))
3847+
hlock->references, hlock->pin_count)) {
3848+
case 0:
38473849
return 1;
3850+
case 1:
3851+
break;
3852+
case 2:
3853+
*merged += (idx == first_idx);
3854+
break;
3855+
default:
3856+
WARN_ON(1);
3857+
return 0;
3858+
}
38483859
}
38493860
return 0;
38503861
}
@@ -3855,9 +3866,9 @@ __lock_set_class(struct lockdep_map *lock, const char *name,
38553866
unsigned long ip)
38563867
{
38573868
struct task_struct *curr = current;
3869+
unsigned int depth, merged = 0;
38583870
struct held_lock *hlock;
38593871
struct lock_class *class;
3860-
unsigned int depth;
38613872
int i;
38623873

38633874
if (unlikely(!debug_locks))
@@ -3882,23 +3893,23 @@ __lock_set_class(struct lockdep_map *lock, const char *name,
38823893
curr->lockdep_depth = i;
38833894
curr->curr_chain_key = hlock->prev_chain_key;
38843895

3885-
if (reacquire_held_locks(curr, depth, i))
3896+
if (reacquire_held_locks(curr, depth, i, &merged))
38863897
return 0;
38873898

38883899
/*
38893900
* I took it apart and put it back together again, except now I have
38903901
* these 'spare' parts.. where shall I put them.
38913902
*/
3892-
if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3903+
if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged))
38933904
return 0;
38943905
return 1;
38953906
}
38963907

38973908
static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
38983909
{
38993910
struct task_struct *curr = current;
3911+
unsigned int depth, merged = 0;
39003912
struct held_lock *hlock;
3901-
unsigned int depth;
39023913
int i;
39033914

39043915
if (unlikely(!debug_locks))
@@ -3923,7 +3934,11 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
39233934
hlock->read = 1;
39243935
hlock->acquire_ip = ip;
39253936

3926-
if (reacquire_held_locks(curr, depth, i))
3937+
if (reacquire_held_locks(curr, depth, i, &merged))
3938+
return 0;
3939+
3940+
/* Merging can't happen with unchanged classes.. */
3941+
if (DEBUG_LOCKS_WARN_ON(merged))
39273942
return 0;
39283943

39293944
/*
@@ -3932,6 +3947,7 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
39323947
*/
39333948
if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
39343949
return 0;
3950+
39353951
return 1;
39363952
}
39373953

@@ -3946,8 +3962,8 @@ static int
39463962
__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
39473963
{
39483964
struct task_struct *curr = current;
3965+
unsigned int depth, merged = 1;
39493966
struct held_lock *hlock;
3950-
unsigned int depth;
39513967
int i;
39523968

39533969
if (unlikely(!debug_locks))
@@ -4002,14 +4018,15 @@ __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
40024018
if (i == depth-1)
40034019
return 1;
40044020

4005-
if (reacquire_held_locks(curr, depth, i + 1))
4021+
if (reacquire_held_locks(curr, depth, i + 1, &merged))
40064022
return 0;
40074023

40084024
/*
40094025
* We had N bottles of beer on the wall, we drank one, but now
40104026
* there's not N-1 bottles of beer left on the wall...
4027+
* Pouring two of the bottles together is acceptable.
40114028
*/
4012-
DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth-1);
4029+
DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged);
40134030

40144031
/*
40154032
* Since reacquire_held_locks() would have called check_chain_key()

0 commit comments

Comments
 (0)