From nobody Wed Dec 17 10:19:47 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3DB923C501; Fri, 9 May 2025 15:54:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746806078; cv=none; b=QULo+pmNG4Cz8NS4BP8iY0QA3HjxNwpVRTh7RAIwnTalbjideGNEq6nTCn8x2UrStnRGavz+PifO1uf90IrBpOdV926hkpFvLWV1JndepkmUHgCZ7/H8FTHytUZG2663xOsZmtXmM1hpuH6brK6fiVoQWio7w2ytp9shHLH6u/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746806078; c=relaxed/simple; bh=YUzGN4pDQEVUn13ezU+iyAv9BvSGVVgHmEotghHtWUM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Mr4jfzwZpGl4ORboHEOvB68PVG5yfPfni1vV9sjfC9jSFDw7r6fRd2EACvq2beOabHrFahdLRpqJlNVINz1/90tQFdzsYQt86GMY2KlfcgqBRC4DuRfSW0dkHj0mMcONcS2ohInxTzSYDpx49Z1LgTZf7nTNaVgF/n1Qayp5VSU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=m/+EDTfC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="m/+EDTfC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B75CAC4CEED; Fri, 9 May 2025 15:54:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746806078; bh=YUzGN4pDQEVUn13ezU+iyAv9BvSGVVgHmEotghHtWUM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=m/+EDTfCY1nsaLR1undiooc39pQe/V3hFU3bhaBdM9DaSUGQ+Z0La/Jj7EMAg8O8W VZYB0AFElEHLkYp+utwcfHkRzl4bHkzg2QIUyCnyYztoRsi2u1FIQe/qC7GJjDcUlM D1rHrllVwEmSojaVKnbAdPMPpCem94rxxVK6TrcBcZpF+teZ88y0XHiNvmFMYrso6w +34fkZOJ/vthrWCNL6bPmGfc6vdCndNkPj7vmMKKu1WARPsXu3xGoDSS2GAFkZ9bFs einzhrXdz2XIMJc8cL8cVlMA6hg74YL2/3LQ+Cvtk97efRs6jt/YyHSqgEmw7AiHJl p05E0cSnE4E0g== From: Jeff Layton Date: Fri, 09 May 2025 11:53:42 -0400 Subject: [PATCH v9 06/10] ref_tracker: automatically register a file in debugfs for a ref_tracker_dir Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250509-reftrack-dbgfs-v9-6-8ab888a4524d@kernel.org> References: <20250509-reftrack-dbgfs-v9-0-8ab888a4524d@kernel.org> In-Reply-To: <20250509-reftrack-dbgfs-v9-0-8ab888a4524d@kernel.org> To: Andrew Morton , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin Cc: Kuniyuki Iwashima , Qasim Ijaz , Nathan Chancellor , Andrew Lunn , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, Jeff Layton X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=5577; i=jlayton@kernel.org; h=from:subject:message-id; bh=YUzGN4pDQEVUn13ezU+iyAv9BvSGVVgHmEotghHtWUM=; b=owEBbQKS/ZANAwAKAQAOaEEZVoIVAcsmYgBoHiUvabZB90uKkmdA4sFkccVytpoiZvfWbFXD3 F+vQJrrCOiJAjMEAAEKAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCaB4lLwAKCRAADmhBGVaC FRhiD/9LBPhMfPBUCm8in2RIzspdtzQ+jN71wP52LV3CXR9s/NgRC2SOhkFXZu2IcVdjQz1h+JS bKBpn4olHIIqBqAq5hrGxvf1n/hjiDiALLukkHtMRCN7b9A71Hok2WPcjE4mjnt9tmtkE2JHe6J ytqoGbMhS4WRh1aWX6zb/SCcbLlQwYta5jaGiGeAjemc1oK7NUu9cOxtxXSu/xyGTsYliG7q3Wv 9zrW9t7R1sSNIjmpIrB9vrk2XzRqA3WN5Z28v/bjygjWPmC8nHgKAtP3Bpje2E+c0ipEcMTms8X VtzYophLaEHP9rZxIm8OXcnFpJFTCpNmMuuPmECkPf6ewjmLj13bF3unIUNqVDovF4cd85tCJG3 Gvw8FXZnDkMUi0H0y1MUuK8L0suSqVJrShpMxNrPLwqFF0ErsUp2xzvGCFekx06CSCxoRsXS14A fF61oOdcC9PR5N2zOzkMYxpgjTIc8bsn0filocDC2EbC+JBVOzSXYTDB/fw2jySplQTWPKoFYOC 8vy/fuY9FowG6Gcv7mm3a+k1S6gogOPAtsYyHnDy5JYKJkORSRlO1fcXxutwqp4bQSszxIdHWX6 1FTLBnRpz7hgc/V9eIzQO+omv/pSfp5wYH9DCH+lQHKfH7MXVn89+kDOWrvkAsHuBZLhliXK4kf Jn3pmmSg0Ku2abQ== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 Currently, there is no convenient way to see the info that the ref_tracking infrastructure collects. Attempt to create a file in debugfs when called from ref_tracker_dir_init(). The file is given the name "class@%px", as having the unmodified address is helpful for debugging. This should be safe since this directory is only accessible by root If debugfs file creation fails, a pr_warn will be isssued. Signed-off-by: Jeff Layton --- include/linux/ref_tracker.h | 23 ++++++++++++++ lib/ref_tracker.c | 73 +++++++++++++++++++++++++++++++++++++++++= ++-- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/include/linux/ref_tracker.h b/include/linux/ref_tracker.h index 3968f993db81e95c0d58c81454311841c1b9cd35..dd289fdda12b1a10197912f5796= f97002e785aaf 100644 --- a/include/linux/ref_tracker.h +++ b/include/linux/ref_tracker.h @@ -20,12 +20,27 @@ struct ref_tracker_dir { struct list_head list; /* List of active trackers */ struct list_head quarantine; /* List of dead trackers */ const char *class; /* object classname */ +#ifdef CONFIG_DEBUG_FS + struct dentry *dentry; +#endif char name[32]; #endif }; =20 #ifdef CONFIG_REF_TRACKER =20 +#ifdef CONFIG_DEBUG_FS + +void ref_tracker_dir_debugfs(struct ref_tracker_dir *dir); + +#else /* CONFIG_DEBUG_FS */ + +static inline void ref_tracker_dir_debugfs(struct ref_tracker_dir *dir) +{ +} + +#endif /* CONFIG_DEBUG_FS */ + static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, unsigned int quarantine_count, const char *class, @@ -39,7 +54,11 @@ static inline void ref_tracker_dir_init(struct ref_track= er_dir *dir, refcount_set(&dir->untracked, 1); refcount_set(&dir->no_tracker, 1); dir->class =3D class; +#ifdef CONFIG_DEBUG_FS + dir->dentry =3D NULL; +#endif strscpy(dir->name, name, sizeof(dir->name)); + ref_tracker_dir_debugfs(dir); stack_depot_init(); } =20 @@ -68,6 +87,10 @@ static inline void ref_tracker_dir_init(struct ref_track= er_dir *dir, { } =20 +static inline void ref_tracker_dir_debugfs(struct ref_tracker_dir *dir) +{ +} + static inline void ref_tracker_dir_exit(struct ref_tracker_dir *dir) { } diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c index 6e85e7eb347d86775ba38a72dad7159f9ac41ed9..1df12625d80cc7cff65d9f6be89= e1dd5c5ffb7f6 100644 --- a/lib/ref_tracker.c +++ b/lib/ref_tracker.c @@ -29,6 +29,14 @@ struct ref_tracker_dir_stats { } stacks[]; }; =20 +#ifdef CONFIG_DEBUG_FS +static void ref_tracker_debugfs_remove(struct ref_tracker_dir *dir); +#else +static inline void ref_tracker_debugfs_remove(struct ref_tracker_dir *dir) +{ +} +#endif + static struct ref_tracker_dir_stats * ref_tracker_get_stats(struct ref_tracker_dir *dir, unsigned int limit) { @@ -184,6 +192,7 @@ void ref_tracker_dir_exit(struct ref_tracker_dir *dir) bool leak =3D false; =20 dir->dead =3D true; + ref_tracker_debugfs_remove(dir); spin_lock_irqsave(&dir->lock, flags); list_for_each_entry_safe(tracker, n, &dir->quarantine, head) { list_del(&tracker->head); @@ -311,8 +320,7 @@ static void __ostream_printf pr_ostream_seq(struct ostr= eam *stream, char *fmt, . va_end(args); } =20 -static __maybe_unused int -ref_tracker_dir_seq_print(struct ref_tracker_dir *dir, struct seq_file *se= q) +static int ref_tracker_dir_seq_print(struct ref_tracker_dir *dir, struct s= eq_file *seq) { struct ostream os =3D { .func =3D pr_ostream_seq, .prefix =3D "", @@ -326,6 +334,67 @@ ref_tracker_dir_seq_print(struct ref_tracker_dir *dir,= struct seq_file *seq) return os.used; } =20 +static int ref_tracker_debugfs_show(struct seq_file *f, void *v) +{ + struct ref_tracker_dir *dir =3D f->private; + + return ref_tracker_dir_seq_print(dir, f); +} + +static int ref_tracker_debugfs_open(struct inode *inode, struct file *filp) +{ + struct ref_tracker_dir *dir =3D inode->i_private; + + return single_open(filp, ref_tracker_debugfs_show, dir); +} + +static const struct file_operations ref_tracker_debugfs_fops =3D { + .owner =3D THIS_MODULE, + .open =3D ref_tracker_debugfs_open, + .read =3D seq_read, + .llseek =3D seq_lseek, + .release =3D single_release, +}; + +/** + * ref_tracker_dir_debugfs - create debugfs file for ref_tracker_dir + * @dir: ref_tracker_dir to be associated with debugfs file + * + * In most cases, a debugfs file will be created automatically for every + * ref_tracker_dir. If the object was created before debugfs is brought up + * then that may fail. In those cases, it is safe to call this at a later + * time to create the file. + */ +void ref_tracker_dir_debugfs(struct ref_tracker_dir *dir) +{ + char name[NAME_MAX + 1]; + int ret; + + /* No-op if already created */ + if (!IS_ERR_OR_NULL(dir->dentry)) + return; + + ret =3D snprintf(name, sizeof(name), "%s@%px", dir->class, dir); + name[sizeof(name) - 1] =3D '\0'; + + if (ret < sizeof(name)) + dir->dentry =3D debugfs_create_file(name, S_IFREG | 0400, + ref_tracker_debug_dir, dir, + &ref_tracker_debugfs_fops); + else + dir->dentry =3D ERR_PTR(-ENAMETOOLONG); + + if (IS_ERR(dir->dentry)) + pr_warn("ref_tracker: unable to create debugfs file for %s: %pe\n", + name, dir->dentry); +} +EXPORT_SYMBOL(ref_tracker_dir_debugfs); + +static void ref_tracker_debugfs_remove(struct ref_tracker_dir *dir) +{ + debugfs_remove(dir->dentry); +} + static int __init ref_tracker_debugfs_init(void) { ref_tracker_debug_dir =3D debugfs_create_dir("ref_tracker", NULL); --=20 2.49.0