From patchwork Thu Nov 9 21:22:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 163609 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp722199vqs; Thu, 9 Nov 2023 13:39:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IGN/3qgsv++uJ1RCdI8GkPBaOCquQ+exNCbF/HLua+74VMXQjhQTTXHEzDuWWX3X7nSps5I X-Received: by 2002:a05:6a20:4329:b0:16b:b4a0:e06b with SMTP id h41-20020a056a20432900b0016bb4a0e06bmr7531200pzk.43.1699565950948; Thu, 09 Nov 2023 13:39:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699565950; cv=none; d=google.com; s=arc-20160816; b=hakXuNVE5izsJYyQduQIDRitqjm89DeJcn9WdzExkRlxslSAsSvtvHVEkv8fEPQQk+ uwSCzGOwUKEYCV9arK2kqefCxUX8fVHg1ydfhl3DJgrfwaqlZ6Wottez+0KGBKrqP34k /H/TJ7g09Y6ZLiX/KHkdkBpHiF5pSGOa/xPzhO0070X4uW7+tb36IBhzfLlz0C+d50tk mdIOTIyue4d7lnD+JreD0ucwkM8+8b3gUqF0WeUZtobacQgYX4nC1JyZGjkep4ggiMig tCaT0PAi1YB8qc/KpQOVCKIPloyzyL4lLY1OMvEWlaOPI84ub1iCnjE/RK68jlsVkJn+ gkNQ== 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=FsLOXpYyzFGJvMDs+SWO8R+4g522I7OBFFoDx/3n4iY=; fh=fr6hIKaSb7E+0K08IHhjy1pQvdN8X98xxmR7pnwhgag=; b=y3dLwj2Z/rx5+I3fyGaBSIi/SD9WWDhH6B3KGE6yGy8h+A78YqzvCuWGg38WkG92jE MLUOnfRN9YLWePz1ij2ZO2UYrkXYEAoWQ2z3GYxNwyjnKVmY/1/OV60NOT2XynL25Ic6 z2BIayGr5Xx3Z1yRcnR7siSwmI43l6er7M8J6gHX9adIsL2ZtWFpj4dVw6ddtYK7mNEq tw4x7JY0OZjMoU9PXHtVLh+OFhIrp35rlViRlbTWIYWN5hjYMfGKUY4NrT+bqX9rQ0TT 4//QlS8Vn9HR79eMv8MXS1F0blzur2ciLF4nEiZmIAvxBG6iR0tVVt/eOcMdK/iNTc3C bLAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sipsolutions.net header.s=mail header.b=vZlLtyqI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=REJECT dis=NONE) header.from=sipsolutions.net Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id b13-20020a63d80d000000b005be2508cde6si186881pgh.593.2023.11.09.13.39.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 13:39:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@sipsolutions.net header.s=mail header.b=vZlLtyqI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=REJECT dis=NONE) header.from=sipsolutions.net Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 5DF6A822F74A; Thu, 9 Nov 2023 13:38:54 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345280AbjKIVi0 (ORCPT + 30 others); Thu, 9 Nov 2023 16:38:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345259AbjKIViX (ORCPT ); Thu, 9 Nov 2023 16:38:23 -0500 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:242:246e::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A2103C3D; Thu, 9 Nov 2023 13:38:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Content-Type:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-To: Resent-Cc:Resent-Message-ID; bh=FsLOXpYyzFGJvMDs+SWO8R+4g522I7OBFFoDx/3n4iY=; t=1699565901; x=1700775501; b=vZlLtyqIDj5duKA94ALT7y+Ns0paq19XWTcyqOijEAu8W2S y+JXON4phpgqgTruwZ8whIlgRs2i4eV7DzPlG1l68I9b2OYmAvnvXCLDY/4WVP4NpPAeLr0nhNea/ WSEKR/eia4v3U83RCinmH4q+zYeS8GQ7xgWxLakuPIG1eTSTpmjl1EBLQY35Szzeqz5EtbnIpsg/1 a4w4YSJybxmFRnoExexriqXMM3YeS6snGYQpWbxwT6jqeTOB4VMb3lfWX5fYb3DufVDsLRwGLbRn5 uXRr8PpuYI9jwLa3HMT0LD5E2BwrF38reACT+DGbzv6fqtkTlpF/2KjxZTf7tWwQ==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.97) (envelope-from ) id 1r1CjC-00000001znF-0g58; Thu, 09 Nov 2023 22:38:18 +0100 From: Johannes Berg To: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, Greg Kroah-Hartman , "Rafael J. Wysocki" , Nicolai Stange , Ben Greear , Johannes Berg Subject: [RFC PATCH 2/6] debugfs: annotate debugfs handlers vs. removal with lockdep Date: Thu, 9 Nov 2023 22:22:54 +0100 Message-ID: <20231109222251.a62811ebde9b.Ia70a49792c448867fd61b0234e1da507b0f75086@changeid> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231109212251.213873-7-johannes@sipsolutions.net> References: <20231109212251.213873-7-johannes@sipsolutions.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 09 Nov 2023 13:38:54 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782124066333305991 X-GMAIL-MSGID: 1782124066333305991 From: Johannes Berg When you take a lock in a debugfs handler but also try to remove the debugfs file under that lock, things can deadlock since the removal has to wait for all users to finish. Add lockdep annotations in debugfs_file_get()/_put() to catch such issues. Signed-off-by: Johannes Berg --- fs/debugfs/file.c | 10 ++++++++++ fs/debugfs/inode.c | 12 ++++++++++++ fs/debugfs/internal.h | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c index 23bdfc126b5e..e499d38b1077 100644 --- a/fs/debugfs/file.c +++ b/fs/debugfs/file.c @@ -103,6 +103,12 @@ int debugfs_file_get(struct dentry *dentry) kfree(fsd); fsd = READ_ONCE(dentry->d_fsdata); } +#ifdef CONFIG_LOCKDEP + fsd->lock_name = kasprintf(GFP_KERNEL, "debugfs:%pd", dentry); + lockdep_register_key(&fsd->key); + lockdep_init_map(&fsd->lockdep_map, fsd->lock_name ?: "debugfs", + &fsd->key, 0); +#endif } /* @@ -119,6 +125,8 @@ int debugfs_file_get(struct dentry *dentry) if (!refcount_inc_not_zero(&fsd->active_users)) return -EIO; + lock_map_acquire_read(&fsd->lockdep_map); + return 0; } EXPORT_SYMBOL_GPL(debugfs_file_get); @@ -136,6 +144,8 @@ void debugfs_file_put(struct dentry *dentry) { struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata); + lock_map_release(&fsd->lockdep_map); + if (refcount_dec_and_test(&fsd->active_users)) complete(&fsd->active_users_drained); } diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c index 269bad87d552..a4c77aafb77b 100644 --- a/fs/debugfs/inode.c +++ b/fs/debugfs/inode.c @@ -241,6 +241,14 @@ static void debugfs_release_dentry(struct dentry *dentry) if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) return; + /* check it wasn't an automount or dir */ + if (fsd && fsd->real_fops) { +#ifdef CONFIG_LOCKDEP + lockdep_unregister_key(&fsd->key); + kfree(fsd->lock_name); +#endif + } + kfree(fsd); } @@ -742,6 +750,10 @@ static void __debugfs_file_removed(struct dentry *dentry) fsd = READ_ONCE(dentry->d_fsdata); if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) return; + + lock_map_acquire(&fsd->lockdep_map); + lock_map_release(&fsd->lockdep_map); + if (!refcount_dec_and_test(&fsd->active_users)) wait_for_completion(&fsd->active_users_drained); } diff --git a/fs/debugfs/internal.h b/fs/debugfs/internal.h index f7c489b5a368..c7d61cfc97d2 100644 --- a/fs/debugfs/internal.h +++ b/fs/debugfs/internal.h @@ -7,6 +7,7 @@ #ifndef _DEBUGFS_INTERNAL_H_ #define _DEBUGFS_INTERNAL_H_ +#include struct file_operations; @@ -23,6 +24,11 @@ struct debugfs_fsdata { struct { refcount_t active_users; struct completion active_users_drained; +#ifdef CONFIG_LOCKDEP + struct lockdep_map lockdep_map; + struct lock_class_key key; + char *lock_name; +#endif }; }; };