Path: csiph.com!goblin3!goblin2!goblin.stu.neva.ru!gothmog.csi.it!bofh.it!news.nic.it!robomod From: Dmitry Vyukov Newsgroups: linux.kernel Subject: [PATCH] lockdep: fix stack trace caching logic Date: Thu, 04 Feb 2016 14:50:03 +0100 Message-ID: Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=8MC+wx8TaxWLvGN/wAzRF4RvPmbXq6z01SqKSRmUoMY=; b=p4jO1r/k/LTz488YIhikgl1myAkI8f27kunjuxi6aZsluNnWqSdaTNBQjxbsT1hW0M OvMqB2kIScHPuUAdnGeUrBNFmxgL9+aac59F7DelmVoD6zBjHIdVOq30ig/LRfUwUgzq lJwnAY6fgURnlViN8u4IXJcQHukO+29ve897eAZ8OhRE2yUE7vKrscIvFwbIrMOt7o/f r+vgW9vyvvWqLqW+o/xqxCuapVrDyyRenFQHACGhy2GzootX6xnHHjO706x82C1hen3E LvdokuD4s1nGHx2sp7j/j7c2ep3KzJ+nTOV8EWD/rYrHNCdliQ5+GWHgMGVfCKWPanBj N4Dw== X-Google-Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=8MC+wx8TaxWLvGN/wAzRF4RvPmbXq6z01SqKSRmUoMY=; b=HoXYZ+vOu5lDn5DOW77OJ6quZV0TJIwdZWFkVcJePMDMM+6XjLnXJDgMRC/u992M1h d0rzB1y9wUCpZJQr0JfK4kTat8HBKrK3yzxTLHtXtl536uxyRiA/eyhxZ7oHlcwJM0v9 +Wo/GV9KEe1htqMrOWCWj+4aANS7WiEP+Je7M8RteG54wQr89wNQQ5vRU5KGDswTb5mY deAXTK56CiC1Q6Wyx5FPPJ2YIQikvVuHAxUyFlv/zXAsilEA61FEvWJw0m0qcLB17Lm/ PyTNZKrwDJQIatrGEhNZQHAu96CbZ7lDkLHA5ciT+V6zqByBJCB+u91e7CJl7LKMPzxj fXeg== X-Gm-Message-State: AG10YOQS6StGtLfHFEjakHbK2MHO4lcK1wrQwU41Xg5yO0wyUMj2jq27SoJR4z5LVmcSVvZV X-Received: by 10.195.11.100 with SMTP id eh4mr8849959wjd.83.1454593244649; Thu, 04 Feb 2016 05:40:44 -0800 (PST) X-Mailer: git-send-email 2.7.0.rc3.207.g0ac5344 Sender: robomod@news.nic.it List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Approved: robomod@news.nic.it Lines: 83 Organization: linux.* mail to news gateway X-Original-Cc: linux-kernel@vger.kernel.org, peter@hurleysoftware.com, kcc@google.com, glider@google.com, sasha.levin@oracle.com, Dmitry Vyukov X-Original-Date: Thu, 4 Feb 2016 14:40:40 +0100 X-Original-Message-ID: <1454593240-121647-1-git-send-email-dvyukov@google.com> X-Original-Sender: linux-kernel-owner@vger.kernel.org Xref: csiph.com linux.kernel:1326824 check_prev_add() caches saved stack trace in static trace variable to avoid duplicate save_trace() calls in dependencies involving trylocks. But that caching logic contains a bug. We may not save trace on first iteration due to early return from check_prev_add(). Then on the second iteration when we actually need the trace we don't save it because we think that we've already saved it. Let check_prev_add() itself control when stack is saved. There is another bug. Trace variable is protected by graph lock. But we can temporary release graph lock during printing. Fix this by invalidating cached stack trace when we release graph lock. Signed-off-by: Dmitry Vyukov --- kernel/locking/lockdep.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index 60ace56..c7710e4 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -1822,7 +1822,7 @@ check_deadlock(struct task_struct *curr, struct held_lock *next, */ static int check_prev_add(struct task_struct *curr, struct held_lock *prev, - struct held_lock *next, int distance, int trylock_loop) + struct held_lock *next, int distance, int *stack_saved) { struct lock_list *entry; int ret; @@ -1883,8 +1883,11 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev, } } - if (!trylock_loop && !save_trace(&trace)) - return 0; + if (!*stack_saved) { + if (!save_trace(&trace)) + return 0; + *stack_saved = 1; + } /* * Ok, all validations passed, add the new lock @@ -1907,6 +1910,8 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev, * Debugging printouts: */ if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) { + /* We drop graph lock, so another thread can overwrite trace. */ + *stack_saved = 0; graph_unlock(); printk("\n new dependency: "); print_lock_name(hlock_class(prev)); @@ -1929,7 +1934,7 @@ static int check_prevs_add(struct task_struct *curr, struct held_lock *next) { int depth = curr->lockdep_depth; - int trylock_loop = 0; + int stack_saved = 0; struct held_lock *hlock; /* @@ -1956,7 +1961,7 @@ check_prevs_add(struct task_struct *curr, struct held_lock *next) */ if (hlock->read != 2 && hlock->check) { if (!check_prev_add(curr, hlock, next, - distance, trylock_loop)) + distance, &stack_saved)) return 0; /* * Stop after the first non-trylock entry, @@ -1979,7 +1984,6 @@ check_prevs_add(struct task_struct *curr, struct held_lock *next) if (curr->held_locks[depth].irq_context != curr->held_locks[depth-1].irq_context) break; - trylock_loop = 1; } return 1; out_bug: -- 2.7.0.rc3.207.g0ac5344