Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp7020328ybi; Thu, 13 Jun 2019 08:15:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqzVYJ9Ov8AkC3bkj+HAWDSeNOLD2VfhRZ0hKazW9NQ7Xn7IQ4d4QYuK6sqIVCDrCLA2g9vN X-Received: by 2002:a63:a34c:: with SMTP id v12mr30224789pgn.198.1560438925942; Thu, 13 Jun 2019 08:15:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560438925; cv=none; d=google.com; s=arc-20160816; b=ThxyZNEu0qOgxxNvwN/MtYe8CZBse5kzc2FGj8tMa9JFsPHmczg3VBumLlt4kt70zp jbIrT6CCW5kE1BpO1R+YWBYa4kRQl/plBv3aVtuvjQlK4BrRJf7sHFQVpjJdF8ttHIz4 dKSuZcIgrewWx3gpuU5NNkmjN1vc5iEjPBqAJe8yjz81KDHY9yMfAJzr4dKBjTHSeJlt y/HimXGbRSG31IXIujgbAo11oNTIlH8JL2+c+fSAas3B3ZPcsBlYYVzWZIfuGnTBD+To 3yKIQu6z37CSDd1H5UkYuStoqeXpIU9ZjgmhfJlItiMCI1fQT4VJFJ5y2ivnDuJD8uCb SGhg== 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=uz5AFFFPoKyMkFjmlbBtvraAuDO8dOKuJwiHzU7aXEM=; b=XW0a1jy/33DNWfzTNNxeOePeiN+JRdNHvSjmDbn1Q3b1KgFWRrolgpzeazlr6K+1wD ylsa1Zh9AlXm58aq2RLDUrrdMyiACyr1onPAgQDfJ8i7eh7LV/Fxu5hMehiy4UpZerGp TIZ+ANNxvi5sxmJ0mYlRTflIG5vffa5lTLehh7puCYM3P73P+vwvh2xlJU1w4UVSS5Rs 3AnT6DLmtC9+oecEtAM0M+AcGk+p84JZJ1pP0f5HS+fvH1N7QXpEl6LfauOKMyg7BmbR xue5D5iQTWbcKQP4bWD9FFd7FpcAqyJ1zuOBzZtShZKQdHpwuFoLrWuatmP7Ghp6SF1k VdOA== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 18si69150pgb.104.2019.06.13.08.15.10; Thu, 13 Jun 2019 08:15:25 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387975AbfFMPNN (ORCPT + 99 others); Thu, 13 Jun 2019 11:13:13 -0400 Received: from mx2.suse.de ([195.135.220.15]:46238 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1732270AbfFMNfR (ORCPT ); Thu, 13 Jun 2019 09:35:17 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 072DDAD08; Thu, 13 Jun 2019 13:35:16 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 3FE5ADA897; Thu, 13 Jun 2019 15:36:05 +0200 (CEST) From: David Sterba To: peterz@infradead.org Cc: linux-kernel@vger.kernel.org, mingo@redhat.com, David Sterba Subject: [PATCH] lockdep: introduce lockdep_assert_not_held() Date: Thu, 13 Jun 2019 15:36:04 +0200 Message-Id: <20190613133604.9889-1-dsterba@suse.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add an assertion that a lock is not held, suitable for the following (simplified) usecase in filesystems: - filesystem write - lock(&big_filesystem_lock) - kmalloc(GFP_KERNEL) - trigger dirty data write to get more memory - find dirty pages - call filesystem write - lock(&big_filesystem_lock) deadlock The cause here is the use of GFP_KERNEL that does not exclude poking filesystems to allow freeing some memory. Such scenario is a bug, so the use of GFP_NOFS is the right flag. The annotation can help catch such bugs during development because the actual deadlock could be hard to hit in practice. Signed-off-by: David Sterba --- Documentation/locking/lockdep-design.txt | 5 +++++ include/linux/lockdep.h | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/Documentation/locking/lockdep-design.txt b/Documentation/locking/lockdep-design.txt index 39fae143c9cb..8b3013aaf518 100644 --- a/Documentation/locking/lockdep-design.txt +++ b/Documentation/locking/lockdep-design.txt @@ -211,6 +211,11 @@ that nobody tampered with the lock, e.g. kernel/sched/sched.h lockdep_unpin_lock(&rq->lock, rf->cookie); } +In some contexts it is useful to assert that a given lock is not held. A +typical example are filesystems that must avoid recursion to their code when +a memory allocation triggers write of dirty data. When the allocation is done +with a lock taken, re-entering the code would cause a deadlock. + While comments about locking requirements might provide useful information, the runtime checks performed by annotations are invaluable when debugging locking problems and they carry the same level of details when inspecting diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h index 6e2377e6c1d6..a6682104bd95 100644 --- a/include/linux/lockdep.h +++ b/include/linux/lockdep.h @@ -397,6 +397,10 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie); WARN_ON_ONCE(debug_locks && !lockdep_is_held(l)); \ } while (0) +#define lockdep_assert_not_held(l) do { \ + WARN_ON(debug_locks && lockdep_is_held(l)); \ + } while (0) + #define lockdep_recursing(tsk) ((tsk)->lockdep_recursion) #define lockdep_pin_lock(l) lock_pin_lock(&(l)->dep_map) @@ -469,6 +473,7 @@ struct lockdep_map { }; #define lockdep_assert_held_exclusive(l) do { (void)(l); } while (0) #define lockdep_assert_held_read(l) do { (void)(l); } while (0) #define lockdep_assert_held_once(l) do { (void)(l); } while (0) +#define lockdep_assert_not_held(l) do { (void)(l); } while (0) #define lockdep_recursing(tsk) (0) -- 2.21.0