Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp2969390rdb; Mon, 4 Dec 2023 12:35:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IE9rEglMNmJopVIhxyKRtqRE6syBNKKN2h9+KunVCw8hNCmkfdTlgnihIuk4+wQ7OyzZf9o X-Received: by 2002:a17:903:1c5:b0:1d0:6ffd:9e0b with SMTP id e5-20020a17090301c500b001d06ffd9e0bmr5368859plh.93.1701722110851; Mon, 04 Dec 2023 12:35:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701722110; cv=none; d=google.com; s=arc-20160816; b=mDz6fw6WOZx34evU65OpFcmnov3QAuO2bCqw6J5N4DszzZbmk9Ql+daPCnVhrIpJTT 3rDJUYZEmVdMiEansGSAymC+NA/Ta7JiviitaIdeH85S/LVd1TU94wtTd6pCzKQhJ8yW HdhE2N/s5yky243T2V7lqJ0lccEBs7u3iAxXqrBV/acgSGK1sh+4JBitymkCSQbGgBNz K3AGviT5v+KQ0fG5TpxgzKHFL5zAkrvC14u0kcliBeDuhBdMURcF8sRq/YYUOCzokEi1 WUf9fncwY8BFFJeF+msLfAAXaoPOjxTl6RKfYvnpcdHdS8W14pvm62X4YW2cbMPtlEJ4 iq8Q== 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=O4lxlMRpgFITAHuA2dXNWC8Q7xFJXzqqCbgPIHAxUCU=; fh=O6f1As6qTdRDG4qMxG9MQ+/7HPm+rMmH0JY81zRf/Kw=; b=N+F3Ay0qhmO7IgEmsLOCdEFs8H25iArYUVRony6qyCsRRWqK52XsQkns1+6Oz9qcNv BePD2ZdUlMu3gNwttnITZk0YEPMKbOnJZyrBuLo44401et043PXjJiilphPxssXsYJoa VZfkwtJ0y7WwxEduupRgy64oCPM8tKrWPMByUrav1lG0OmP1zkqB6ZY0fR4PSv1xGJEG wdVQQC80GEwimxrdoYtowEKMNoB0IWIwm0L0hlzrYtO8kb/Oqa+qXRywfWq+KdJUwbPH RvOmw7tNRAoQ8jfmERqLfXV/GjVJdFKOe12EdFdwNDAppCo2Chm6lJfhzA0b58Pa54aC CeHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=aFcbtPt2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id bc4-20020a170902930400b001d05814624esi5726573plb.385.2023.12.04.12.35.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:35:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=aFcbtPt2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 00A5280CF51F; Mon, 4 Dec 2023 12:35:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346315AbjLDUed (ORCPT + 99 others); Mon, 4 Dec 2023 15:34:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346304AbjLDUeF (ORCPT ); Mon, 4 Dec 2023 15:34:05 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 785F3173E for ; Mon, 4 Dec 2023 12:33:55 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B74E1C433C9; Mon, 4 Dec 2023 20:33:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701722034; bh=qT22ZsEWOC48EvN1HHyJMi0wyTmmI5d9R9nP+L0thow=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aFcbtPt2zljluXLdHQYUWtZt9CR0MMxNI3nvpeJsozG3eQ3juSr/MRGk2fmIrQm7Z v5AO5ujWjJZKk3olxfoKb7K0LtyJQAsIXFT5otVfNoVsVUAF+9x+Rt1Z2SB7egWT16 rMOC8RW6unsR04Jy6uk0wbqdOsoSMmBhgbaBtAfIiROAvAefjEJdDSH4BIX0v/cLwq myUZ074aNHp4bneyV/5Jkd1iU0vNi+ws4/5loO3MxWg0EtmGHKuhaebi/wwbLhO40X FECt8JBuB0yJIYw/MnF50f4olOKYW9nhwxGUYLuaIIqxujKTluYcxObuStIZBwOuV6 RCWNmeGCQgf1Q== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Johannes Berg , Greg Kroah-Hartman , Sasha Levin Subject: [PATCH AUTOSEL 6.6 15/32] debugfs: annotate debugfs handlers vs. removal with lockdep Date: Mon, 4 Dec 2023 15:32:35 -0500 Message-ID: <20231204203317.2092321-15-sashal@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231204203317.2092321-1-sashal@kernel.org> References: <20231204203317.2092321-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.4 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 morse.vger.email 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 (morse.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:35:07 -0800 (PST) From: Johannes Berg [ Upstream commit f4acfcd4deb158b96595250cc332901b282d15b0 ] 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. Acked-by: Greg Kroah-Hartman Signed-off-by: Johannes Berg Signed-off-by: Sasha Levin --- 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 e40229c47fe58..bd9e6284d0a82 100644 --- a/fs/debugfs/file.c +++ b/fs/debugfs/file.c @@ -108,6 +108,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 } /* @@ -124,6 +130,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); @@ -141,6 +149,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 dcde4199a625d..80f4f000dcc13 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 a dir (no fsdata) or automount (no real_fops) */ + if (fsd && fsd->real_fops) { +#ifdef CONFIG_LOCKDEP + lockdep_unregister_key(&fsd->key); + kfree(fsd->lock_name); +#endif + } + kfree(fsd); } @@ -744,6 +752,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 f7c489b5a368c..c7d61cfc97d26 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 }; }; }; -- 2.42.0