| From 1123d21bc161ec1b21d8cd7f95b8a4750909d414 Mon Sep 17 00:00:00 2001 |
| From: Boqun Feng <boqun.feng@gmail.com> |
| Date: Thu, 12 Mar 2020 23:12:55 +0800 |
| Subject: [PATCH] locking/lockdep: Avoid recursion in |
| lockdep_count_{for,back}ward_deps() |
| |
| commit 25016bd7f4caf5fc983bbab7403d08e64cba3004 upstream. |
| |
| Qian Cai reported a bug when PROVE_RCU_LIST=y, and read on /proc/lockdep |
| triggered a warning: |
| |
| [ ] DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled) |
| ... |
| [ ] Call Trace: |
| [ ] lock_is_held_type+0x5d/0x150 |
| [ ] ? rcu_lockdep_current_cpu_online+0x64/0x80 |
| [ ] rcu_read_lock_any_held+0xac/0x100 |
| [ ] ? rcu_read_lock_held+0xc0/0xc0 |
| [ ] ? __slab_free+0x421/0x540 |
| [ ] ? kasan_kmalloc+0x9/0x10 |
| [ ] ? __kmalloc_node+0x1d7/0x320 |
| [ ] ? kvmalloc_node+0x6f/0x80 |
| [ ] __bfs+0x28a/0x3c0 |
| [ ] ? class_equal+0x30/0x30 |
| [ ] lockdep_count_forward_deps+0x11a/0x1a0 |
| |
| The warning got triggered because lockdep_count_forward_deps() call |
| __bfs() without current->lockdep_recursion being set, as a result |
| a lockdep internal function (__bfs()) is checked by lockdep, which is |
| unexpected, and the inconsistency between the irq-off state and the |
| state traced by lockdep caused the warning. |
| |
| Apart from this warning, lockdep internal functions like __bfs() should |
| always be protected by current->lockdep_recursion to avoid potential |
| deadlocks and data inconsistency, therefore add the |
| current->lockdep_recursion on-and-off section to protect __bfs() in both |
| lockdep_count_forward_deps() and lockdep_count_backward_deps() |
| |
| Reported-by: Qian Cai <cai@lca.pw> |
| Signed-off-by: Boqun Feng <boqun.feng@gmail.com> |
| Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> |
| Link: https://lkml.kernel.org/r/20200312151258.128036-1-boqun.feng@gmail.com |
| Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com> |
| |
| diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c |
| index dbc936ccf149..c56da2843ccc 100644 |
| --- a/kernel/locking/lockdep.c |
| +++ b/kernel/locking/lockdep.c |
| @@ -1604,9 +1604,11 @@ unsigned long lockdep_count_forward_deps(struct lock_class *class) |
| this.class = class; |
| |
| raw_local_irq_save(flags); |
| + current->lockdep_recursion = 1; |
| arch_spin_lock(&lockdep_lock); |
| ret = __lockdep_count_forward_deps(&this); |
| arch_spin_unlock(&lockdep_lock); |
| + current->lockdep_recursion = 0; |
| raw_local_irq_restore(flags); |
| |
| return ret; |
| @@ -1631,9 +1633,11 @@ unsigned long lockdep_count_backward_deps(struct lock_class *class) |
| this.class = class; |
| |
| raw_local_irq_save(flags); |
| + current->lockdep_recursion = 1; |
| arch_spin_lock(&lockdep_lock); |
| ret = __lockdep_count_backward_deps(&this); |
| arch_spin_unlock(&lockdep_lock); |
| + current->lockdep_recursion = 0; |
| raw_local_irq_restore(flags); |
| |
| return ret; |
| -- |
| 2.7.4 |
| |