Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp895040ybi; Fri, 24 May 2019 13:17:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqx0V/AqcLffgmetOpDDmP2pe8dSFlFyr/QQ28EKfThCa7tKAQd/HquKwrE43jJ+yBCwr3/L X-Received: by 2002:a63:2ad2:: with SMTP id q201mr105060394pgq.94.1558729036662; Fri, 24 May 2019 13:17:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558729036; cv=none; d=google.com; s=arc-20160816; b=v2Q/wQULuhXcnBeOS7v7E25NDqSYnSLzafT7Ki4Xk/QzkO4IAKSOGFwU7YI0ypq3/t GLQQQXWeAh1zd8g6TQD9PnflH/sWEBr80WGetV3xtdGM6kyuZOfUFdlk4WK0YDBi/Mpi WQTu80aECwnyoK/HmekZPo1lsycEWoyaNFwOxp+Tz0NVinf58PmC2kqbFEJ9XBb8SIPM tXfvQpcldkXenuPyyUqV7eLjzSe3+MmE4Xk9NGt2pC+cmS5jtjag58UwMlTAU2y7vOUV oc5PFHaH6cLwCGRvutd0Ugk6TmUgnxucC7IG8P8nqHJKcMHU1Jr/2aD7fClj6AzPkuOB FlxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=8o89yekVbDnLFr6W/kaOiP//JWIrRYDTVVe3ZbOMta8=; b=n4/gk2EtMtRKTJtg1c6MY5p6Ng/6R6tJCDBryKR2lJbI2izBMdvlHHvHwj8IjOCH9a jrB8Ok6BT0Pci+u2S5T6q7qUVVABbcJB9w6QLSkAkKnd5M2N6ii30K769qS1+VMmZDbM 3EUNmHumsxa5+CK6VmxaGCtv1lIct1VeyhFjyTe42mYnQjPpMS1P8JfkjLbFMhd1rNaZ Mv1DXTilzknpp7JUnqXS8Emhkd7Ebtjdw3FrG4yQY5bz3Mlt4CTGgNpfO153lShTcLvI w/TGrr/pgx0YEPexLcCPEqQu6LuqztyXCb93mkPNOi+S6prtF2JnymS7rsdUDehCQbDc y+vg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w188si5970745pfd.204.2019.05.24.13.16.58; Fri, 24 May 2019 13:17:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391782AbfEXUPq (ORCPT + 99 others); Fri, 24 May 2019 16:15:46 -0400 Received: from mga04.intel.com ([192.55.52.120]:33088 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391756AbfEXUPp (ORCPT ); Fri, 24 May 2019 16:15:45 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 May 2019 13:15:45 -0700 X-ExtLoop1: 1 Received: from ideak-desk.fi.intel.com ([10.237.72.204]) by orsmga003.jf.intel.com with ESMTP; 24 May 2019 13:15:42 -0700 From: Imre Deak To: LKML Cc: =?UTF-8?q?Ville=20Syrj=C3=A4l=C3=A4?= , Peter Zijlstra , Ingo Molnar , Will Deacon Subject: [PATCH v2 1/2] lockdep: Fix OOO unlock when hlocks need merging Date: Fri, 24 May 2019 23:15:08 +0300 Message-Id: <20190524201509.9199-1-imre.deak@intel.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. v2: - Clarify the commit log and use mutex_lock() instead of mutex_trylock() in the example sequences for simplicity. Cc: Ville Syrjälä Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Will Deacon Signed-off-by: Imre Deak --- kernel/locking/lockdep.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index c40fba54e324..967352d32af1 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -3714,7 +3714,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, hlock->references = 2; } - return 1; + return 2; } } @@ -3920,22 +3920,33 @@ static struct held_lock *find_held_lock(struct task_struct *curr, } static int reacquire_held_locks(struct task_struct *curr, unsigned int depth, - int idx) + int idx, bool *first_merged) { struct held_lock *hlock; + int first_idx = idx; if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) return 0; for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) { - if (!__lock_acquire(hlock->instance, + switch (__lock_acquire(hlock->instance, hlock_class(hlock)->subclass, hlock->trylock, hlock->read, hlock->check, hlock->hardirqs_off, hlock->nest_lock, hlock->acquire_ip, - hlock->references, hlock->pin_count)) + hlock->references, hlock->pin_count)) { + case 0: return 1; + case 1: + break; + case 2: + *first_merged = idx == first_idx; + break; + default: + WARN_ON(1); + return 0; + } } return 0; } @@ -3948,6 +3959,7 @@ __lock_set_class(struct lockdep_map *lock, const char *name, struct task_struct *curr = current; struct held_lock *hlock; struct lock_class *class; + bool first_merged = false; unsigned int depth; int i; @@ -3973,14 +3985,14 @@ __lock_set_class(struct lockdep_map *lock, const char *name, curr->lockdep_depth = i; curr->curr_chain_key = hlock->prev_chain_key; - if (reacquire_held_locks(curr, depth, i)) + if (reacquire_held_locks(curr, depth, i, &first_merged)) return 0; /* * I took it apart and put it back together again, except now I have * these 'spare' parts.. where shall I put them. */ - if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) + if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - first_merged)) return 0; return 1; } @@ -3989,6 +4001,7 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) { struct task_struct *curr = current; struct held_lock *hlock; + bool first_merged = false; unsigned int depth; int i; @@ -4014,7 +4027,7 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) hlock->read = 1; hlock->acquire_ip = ip; - if (reacquire_held_locks(curr, depth, i)) + if (reacquire_held_locks(curr, depth, i, &first_merged)) return 0; /* @@ -4023,6 +4036,11 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) */ if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) return 0; + + /* Merging can't happen with unchanged classes.. */ + if (DEBUG_LOCKS_WARN_ON(first_merged)) + return 0; + return 1; } @@ -4038,6 +4056,7 @@ __lock_release(struct lockdep_map *lock, int nested, unsigned long ip) { struct task_struct *curr = current; struct held_lock *hlock; + bool first_merged = false; unsigned int depth; int i; @@ -4093,14 +4112,15 @@ __lock_release(struct lockdep_map *lock, int nested, unsigned long ip) if (i == depth-1) return 1; - if (reacquire_held_locks(curr, depth, i + 1)) + if (reacquire_held_locks(curr, depth, i + 1, &first_merged)) return 0; /* * We had N bottles of beer on the wall, we drank one, but now * there's not N-1 bottles of beer left on the wall... + * Pouring two of the bottles together is acceptable. */ - DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth-1); + DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1 - first_merged); /* * Since reacquire_held_locks() would have called check_chain_key() -- 2.17.1