Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6095998rwr; Tue, 9 May 2023 10:07:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4OAoMYoMnjtl3I6V71lonbmd3PfaXsH/0jDUH/ZaEPq6l8k8flf7pNd1zEL7hLJAvy7Zph X-Received: by 2002:a05:6a20:4328:b0:101:1d1d:4396 with SMTP id h40-20020a056a20432800b001011d1d4396mr4824280pzk.2.1683652067754; Tue, 09 May 2023 10:07:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683652067; cv=none; d=google.com; s=arc-20160816; b=F9neB9v3p9HpXRrVfKOvZC9yCVhK7WCggj1Y0fAQ0gFB/vB1+qbYCit5gqugXl+xyK kOc6OJc0HgowfDsdn30k19FzkaJLKhUyMw/JWOOrYscdG6mF2tTlnFr0pB2m4Yh3E4It DvTUBAl7vBtXoVGuOkkMHXVBIis+Mfjt3B6IsgOyZFJJjT7etTsCenXRrSUU3gjtJTGL M2+W8NaIj84mdyJTkJoOgXDJxwOKfYsAebVGeibl/6i71ggF2lwdfcM7sVRGx8DKu82W phUdRU5eWuI7Br0D32WjMVOEC6m0crlDEJMqS64z9pl5cLgCGSfe6OYqVzgaLkCttSMi lwWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZW9aTms42T46/dSvQYESAhkOAQjGvYQLSdyAX7h+W6s=; b=I5WCK1xoLYsShKDSjbEjGT7U7bafeKjoPrcRgS4NUx45zD2KkJylL8edmG1bAl8Vz5 C/WVGDPum7AAn6rq5jEDbaoxhlNoFX9/SB+kGbu7y6Mn5f7/ZTXDgVaFBayB6K18+bqt lbEYJMNU/FBxMRTeT8lXcdw+0pdewMhCBFs5C4eRrwG8j+CpjzZyxMZZAm/cBBgmgmVQ V6pWciaOiqPbyHZ/Zg3g2RIhIbCn0pVr1BNCr+lztT8efoj0IjIEZIKPm9MF8NgWb90V 7qu0BCtPpjF9crZ1uMv8pietX2DMDsoIUUWvH5XRdPpsdj28xbTgswJH8rNEX3Jiyukf Y+3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oPneXlUL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z7-20020a633307000000b0052c73483bbfsi1800962pgz.830.2023.05.09.10.07.28; Tue, 09 May 2023 10:07:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oPneXlUL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234768AbjEIQ5Y (ORCPT + 99 others); Tue, 9 May 2023 12:57:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234588AbjEIQ5R (ORCPT ); Tue, 9 May 2023 12:57:17 -0400 Received: from out-46.mta1.migadu.com (out-46.mta1.migadu.com [95.215.58.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31E7B2126 for ; Tue, 9 May 2023 09:57:12 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1683651431; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZW9aTms42T46/dSvQYESAhkOAQjGvYQLSdyAX7h+W6s=; b=oPneXlUL5Qng7vxH10AO/hWFlZlWqRuLgPD4Xj67JKkDqbHoCKWj8RiBBZ2u0qdoMotROK 8MIYFSyqk4psf12fOel+CldcoNyYMIAcEhhPk6a/CXVe/Z2fCA404A36ou5D++qDPkcHzF AkOhN+4hig+zKd5aX1NbEdZ3+BlhFSY= From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-bcachefs@vger.kernel.org Cc: Kent Overstreet , Kent Overstreet , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng Subject: [PATCH 02/32] locking/lockdep: lock_class_is_held() Date: Tue, 9 May 2023 12:56:27 -0400 Message-Id: <20230509165657.1735798-3-kent.overstreet@linux.dev> In-Reply-To: <20230509165657.1735798-1-kent.overstreet@linux.dev> References: <20230509165657.1735798-1-kent.overstreet@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kent Overstreet This patch adds lock_class_is_held(), which can be used to assert that a particular type of lock is not held. Signed-off-by: Kent Overstreet Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Will Deacon Cc: Waiman Long Cc: Boqun Feng --- include/linux/lockdep.h | 4 ++++ kernel/locking/lockdep.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h index 1023f349af..e858c288c7 100644 --- a/include/linux/lockdep.h +++ b/include/linux/lockdep.h @@ -339,6 +339,8 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie); #define lockdep_repin_lock(l,c) lock_repin_lock(&(l)->dep_map, (c)) #define lockdep_unpin_lock(l,c) lock_unpin_lock(&(l)->dep_map, (c)) +int lock_class_is_held(struct lock_class_key *key); + #else /* !CONFIG_LOCKDEP */ static inline void lockdep_init_task(struct task_struct *task) @@ -427,6 +429,8 @@ extern int lockdep_is_held(const void *); #define lockdep_repin_lock(l, c) do { (void)(l); (void)(c); } while (0) #define lockdep_unpin_lock(l, c) do { (void)(l); (void)(c); } while (0) +static inline int lock_class_is_held(struct lock_class_key *key) { return 0; } + #endif /* !LOCKDEP */ enum xhlock_context_t { diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index 50d4863974..e631464070 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -6487,6 +6487,26 @@ void debug_check_no_locks_held(void) } EXPORT_SYMBOL_GPL(debug_check_no_locks_held); +#ifdef CONFIG_LOCKDEP +int lock_class_is_held(struct lock_class_key *key) +{ + struct task_struct *curr = current; + struct held_lock *hlock; + + if (unlikely(!debug_locks)) + return 0; + + for (hlock = curr->held_locks; + hlock < curr->held_locks + curr->lockdep_depth; + hlock++) + if (hlock->instance->key == key) + return 1; + + return 0; +} +EXPORT_SYMBOL_GPL(lock_class_is_held); +#endif + #ifdef __KERNEL__ void debug_show_all_locks(void) { -- 2.40.1