ref_tracker: add ability to register a debugfs file for a ref_tracker_dir
Subsystems (3)
v2 cover ref_tracker: add ability to register a debugfs file for a ref_tracker_dir
I had previously sent some patches to add debugfs files for the net namespace refcount trackers, but Andrew convinced me to make this more generic and better-integrated into the ref_tracker infrastructure.
This adds a new ref_tracker_dir_debugfs() call that subsystems can call to finalize the name of their dir and register a debugfs file for it. The last two patches add these calls for the netns and netdev ref_trackers.
---
Changes in v2:
- Add patch to do %pK -> %p conversion in ref_tracker.c
- Pass in output function to pr_ostream() instead of if statement
- Widen ref_tracker_dir.name to 64 bytes to accomodate unique names
- Eliminate error handling with debugfs manipulation
- Incorporate pointer value into netdev name
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Jeff Layton (8):
ref_tracker: don't use %pK in pr_ostream() output
ref_tracker: add a top level debugfs directory for ref_tracker
ref_tracker: have callers pass output function to pr_ostream()
ref_tracker: allow pr_ostream() to print directly to a seq_file
ref_tracker: add ability to register a file in debugfs for a ref_tracker_dir
net: add ref_tracker_dir_debugfs() calls for netns refcount tracking
ref_tracker: widen the ref_tracker_dir.name field
net: register debugfs file for net_device refcnt tracker
include/linux/ref_tracker.h | 15 ++++- lib/ref_tracker.c | 153 +++++++++++++++++++++++++++++++++++++++----- net/core/dev.c | 6 +- net/core/net_namespace.c | 34 +++++++++- 4 files changed, 190 insertions(+), 18 deletions(-)
---
base-commit: 695caca9345a160ecd9645abab8e70cfe849e9ff
change-id: 20250413-reftrack-dbgfs-3767b303e2fa
Best regards,
Signature · 1 line
Jeff Layton <[email protected]>
PATCH v2 1/8 ref_tracker: don't use %pK in pr_ostream() output
As Thomas Weißschuh points out [1], it is now preferable to use %p instead of hashed pointers with printk(), since raw pointers should no longer be leaked into the kernel log. Change the ref_tracker infrastructure to use %p instead of %pK in its formats.
[1]: https://lore.kernel.org/netdev/[email protected]/
--- lib/ref_tracker.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
lib/ref_tracker.c +3 -3
@@ -96,7 +96,7 @@ __ref_tracker_dir_pr_ostream(struct ref_tracker_dir *dir, stats =ref_tracker_get_stats (dir, display_limit);if (IS_ERR (stats)) { -pr_ostream (s,"%s@%pK: couldn't get stats, error %pe\n" , +pr_ostream (s,"%s@%p: couldn't get stats, error %pe\n" , dir->name, dir, stats);return ; }
@@ -107,13 +107,13 @@ __ref_tracker_dir_pr_ostream(struct ref_tracker_dir *dir, stack = stats->stacks[i].stack_handle;if (sbuf && !stack_depot_snprint (stack, sbuf,STACK_BUF_SIZE ,4 )) sbuf[0 ] =0 ; -pr_ostream (s,"%s@%pK has %d/%d users at\n%s\n" , dir->name, dir, +pr_ostream (s,"%s@%p has %d/%d users at\n%s\n" , dir->name, dir, stats->stacks[i].count, stats->total, sbuf); skipped -= stats->stacks[i].count; }if (skipped) -pr_ostream (s,"%s@%pK skipped reports about %d/%d users.\n" , +pr_ostream (s,"%s@%p skipped reports about %d/%d users.\n" , dir->name, dir, skipped, stats->total);kfree (sbuf);
Signature · 1 line
2.49.0
PATCH v2 2/8 ref_tracker: add a top level debugfs directory for ref_tracker
In reply to Jeff LaytonAdd a new "ref_tracker" directory in debugfs. Each individual refcount tracker can register files under there to display info about currently-held references.
--- lib/ref_tracker.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/ref_tracker.c +16 -0
@@ -12,6 +12,8 @@#define REF_TRACKER_STACK_ENTRIES 16#define STACK_BUF_SIZE 1024 +static struct dentry *ref_tracker_debug_dir = (struct dentry *)-ENOENT ; +struct ref_tracker {struct list_head head;/* anchor into dir->list or dir->quarantine */ bool dead;
@@ -273,3 +275,17 @@ int ref_tracker_free(struct ref_tracker_dir *dir,return 0 ; }EXPORT_SYMBOL_GPL (ref_tracker_free); + +#ifdef CONFIG_DEBUG_FS +#include <linux/debugfs.h> + +static int __init ref_tracker_debugfs_init (void ) +{ + ref_tracker_debug_dir =debugfs_create_dir ("ref_tracker" ,NULL ); +if (IS_ERR (ref_tracker_debug_dir)) +pr_warn ("ref_tracker: unable to create debugfs ref_tracker directory: %pe\n" , + ref_tracker_debug_dir); +return 0 ; +} +late_initcall (ref_tracker_debugfs_init); +#endif /* CONFIG_DEBUG_FS */
Signature · 1 line
2.49.0
PATCH v2 3/8 ref_tracker: have callers pass output function to pr_ostream()
In reply to Jeff LaytonIn a later patch, we'll be adding a 3rd mechanism for outputting ref_tracker info via seq_file. Instead of a conditional, have the caller set a pointer to an output function in struct ostream. As part of this, the log prefix must be explicitly passed in, as it's too late for the pr_fmt macro.
--- lib/ref_tracker.c | 53 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 15 deletions(-)
lib/ref_tracker.c +38 -15
@@ -64,22 +64,40 @@ ref_tracker_get_stats(struct ref_tracker_dir *dir, unsigned int limit)return stats; } +#define __ostream_printf __printf(2, 3) +struct ostream { + void __ostream_printf (*func)(struct ostream *stream, char *fmt, ...); +char *prefix;char *buf;int size, used; }; +static void __ostream_printf pr_ostream_log(struct ostream *stream, char *fmt, ...) +{ +va_list args; + +va_start (args, fmt); +vprintk (fmt, args); +va_end (args); +} + +static void __ostream_printf pr_ostream_buf(struct ostream *stream, char *fmt, ...) +{ +int ret, len = stream->size - stream->used; +va_list args; + +va_start (args, fmt); + ret =vsnprintf (stream->buf + stream->used, len, fmt, args); +va_end (args); + stream->used +=min (ret, len); +} + #define pr_ostream(stream, fmt, args...) \ ({ \ struct ostream *_s = (stream); \ \ - if (!_s->buf) { \ - pr_err(fmt, ##args); \ - } else { \ - int ret, len = _s->size - _s->used; \ - ret = snprintf(_s->buf + _s->used, len, pr_fmt(fmt), ##args); \ - _s->used += min(ret, len); \ - } \ + _s->func(_s, fmt, ##args); \ }) static void
@@ -98,8 +116,8 @@ __ref_tracker_dir_pr_ostream(struct ref_tracker_dir *dir, stats =ref_tracker_get_stats (dir, display_limit);if (IS_ERR (stats)) { -pr_ostream (s,"%s@%p: couldn't get stats, error %pe\n" , - dir->name, dir, stats); +pr_ostream (s,"%s%s@%p: couldn't get stats, error %pe\n" , + s->prefix, dir->name, dir, stats);return ; }
@@ -109,14 +127,15 @@ __ref_tracker_dir_pr_ostream(struct ref_tracker_dir *dir, stack = stats->stacks[i].stack_handle;if (sbuf && !stack_depot_snprint (stack, sbuf,STACK_BUF_SIZE ,4 )) sbuf[0 ] =0 ; -pr_ostream (s,"%s@%p has %d/%d users at\n%s\n" , dir->name, dir, - stats->stacks[i].count, stats->total, sbuf); +pr_ostream (s,"%s%s@%p has %d/%d users at\n%s\n" , s->prefix, + dir->name, dir, stats->stacks[i].count, + stats->total, sbuf); skipped -= stats->stacks[i].count; }if (skipped) -pr_ostream (s,"%s@%p skipped reports about %d/%d users.\n" , - dir->name, dir, skipped, stats->total); +pr_ostream (s,"%s%s@%p skipped reports about %d/%d users.\n" , + s->prefix, dir->name, dir, skipped, stats->total);kfree (sbuf);
@@ -126,7 +145,8 @@ __ref_tracker_dir_pr_ostream(struct ref_tracker_dir *dir,void ref_tracker_dir_print_locked (struct ref_tracker_dir *dir,unsigned int display_limit) { -struct ostream os = {}; +struct ostream os = { .func = pr_ostream_log, + .prefix ="ref_tracker: " };__ref_tracker_dir_pr_ostream (dir, display_limit, &os); }
@@ -145,7 +165,10 @@ EXPORT_SYMBOL(ref_tracker_dir_print);int ref_tracker_dir_snprint (struct ref_tracker_dir *dir,char *buf,size_t size) { -struct ostream os = { .buf = buf, .size = size }; +struct ostream os = { .func = pr_ostream_buf, + .prefix ="ref_tracker: " , + .buf = buf, + .size = size };unsigned long flags;spin_lock_irqsave (&dir->lock, flags);
Signature · 1 line
2.49.0
PATCH v2 4/8 ref_tracker: allow pr_ostream() to print directly to a seq_file
In reply to Jeff LaytonAllow pr_ostream to also output directly to a seq_file without an intermediate buffer.
--- lib/ref_tracker.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
lib/ref_tracker.c +25 -0
@@ -8,6 +8,7 @@#include <linux/slab.h> #include <linux/stacktrace.h> #include <linux/stackdepot.h> +#include <linux/seq_file.h> #define REF_TRACKER_STACK_ENTRIES 16#define STACK_BUF_SIZE 1024
@@ -70,6 +71,7 @@ struct ostream { void __ostream_printf (*func)(struct ostream *stream, char *fmt, ...);char *prefix;char *buf; +struct seq_file *seq;int size, used; };
@@ -93,6 +95,15 @@ static void __ostream_printf pr_ostream_buf(struct ostream *stream, char *fmt, . stream->used +=min (ret, len); } +static void __ostream_printf pr_ostream_seq(struct ostream *stream, char *fmt, ...) +{ +va_list args; + +va_start (args, fmt); +seq_vprintf (stream->seq, fmt, args); +va_end (args); +} + #define pr_ostream(stream, fmt, args...) \ ({ \ struct ostream *_s = (stream); \
@@ -302,6 +313,20 @@ EXPORT_SYMBOL_GPL(ref_tracker_free);#ifdef CONFIG_DEBUG_FS #include <linux/debugfs.h> +static int ref_tracker_dir_seq_print (struct ref_tracker_dir *dir,struct seq_file *seq) +{ +struct ostream os = { .func = pr_ostream_seq, + .prefix ="" , + .seq = seq }; +unsigned long flags; + +spin_lock_irqsave (&dir->lock, flags); +__ref_tracker_dir_pr_ostream (dir,16 , &os); +spin_unlock_irqrestore (&dir->lock, flags); + +return os.used; +} +static int __init ref_tracker_debugfs_init (void ) { ref_tracker_debug_dir =debugfs_create_dir ("ref_tracker" ,NULL );
Signature · 1 line
2.49.0
PATCH v2 5/8 ref_tracker: add ability to register a file in debugfs for a ref_tracker_dir
In reply to Jeff LaytonCurrently, there is no convenient way to see the info that the ref_tracking infrastructure collects. Add a new function that other subsystems can optionally call to update the name field in the ref_tracker_dir and register a corresponding seq_file for it in the top-level ref_tracker directory.
--- include/linux/ref_tracker.h | 13 ++++++++++ lib/ref_tracker.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+)
include/linux/ref_tracker.h +13 -0
@@ -5,6 +5,7 @@#include <linux/types.h> #include <linux/spinlock.h> #include <linux/stackdepot.h> +#include <linux/seq_file.h> struct ref_tracker ;
@@ -17,6 +18,9 @@ struct ref_tracker_dir {bool dead;struct list_head list;/* List of active trackers */ struct list_head quarantine;/* List of dead trackers */ +#ifdef CONFIG_DEBUG_FS +struct dentry *dentry; +#endif char name[32 ];#endif };
@@ -34,10 +38,15 @@ static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, dir->dead = false;refcount_set (&dir->untracked,1 );refcount_set (&dir->no_tracker,1 ); +#ifdef CONFIG_DEBUG_FS + dir->dentry =NULL ; +#endif strscpy (dir->name, name,sizeof (dir->name));stack_depot_init (); } +void ref_tracker_dir_debugfs (struct ref_tracker_dir *dir,const char *name); +void ref_tracker_dir_exit (struct ref_tracker_dir *dir); void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir,
@@ -62,6 +71,10 @@ static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, { } +static inline void ref_tracker_dir_debugfs (struct ref_tracker_dir *dir,const char *name) +{ +} +static inline void ref_tracker_dir_exit (struct ref_tracker_dir *dir) { }
lib/ref_tracker.c +59 -0
77 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v2 6/8 net: add ref_tracker_dir_debugfs() calls for netns refcount tracking
In reply to Jeff LaytonAfter assigning the inode number to the namespace, use it to create a unique name for each netns refcount tracker and register the debugfs files for them.
The init_net is registered early in the boot process before the ref_tracker dir is created, so add a late_initcall() to register its files.
--- net/core/net_namespace.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
net/core/net_namespace.c +33 -1
@@ -761,12 +761,44 @@ struct net *get_net_ns_by_pid(pid_t pid) }EXPORT_SYMBOL_GPL (get_net_ns_by_pid); +#ifdef CONFIG_NET_NS_REFCNT_TRACKER +static void net_ns_net_debugfs (struct net *net) +{ +char name[32 ]; +size_t len; + + len =snprintf (name,sizeof (name),"netns-%u-refcnt" , net->ns.inum); +if (len <sizeof (name)) +ref_tracker_dir_debugfs (&net->refcnt_tracker, name); + + len =snprintf (name,sizeof (name),"netns-%u-notrefcnt" , net->ns.inum); +if (len <sizeof (name)) +ref_tracker_dir_debugfs (&net->notrefcnt_tracker, name); +} + +static int __init init_net_debugfs (void ) +{ +net_ns_net_debugfs (&init_net); +return 0 ; +} +late_initcall (init_net_debugfs); +#else +static void net_ns_net_debugfs (struct net *net) +{ +} +#endif +static __net_init int net_ns_net_init (struct net *net) { +int ret; +#ifdef CONFIG_NET_NS net->ns.ops = &netns_operations;#endif -return ns_alloc_inum (&net->ns); + ret =ns_alloc_inum (&net->ns); +if (!ret) +net_ns_net_debugfs (net); +return ret; } static __net_exit void net_ns_net_exit(struct net *net)
Signature · 1 line
2.49.0
Re: [PATCH v2 6/8] net: add ref_tracker_dir_debugfs() calls for netns refcount tracking
From: Jeff Layton <[email protected]>
Date: Tue, 15 Apr 2025 14:49:44 -0400
Quoted 18 lines
After assigning the inode number to the namespace, use it to create a unique name for each netns refcount tracker and register the debugfs files for them.
The
init_netis registered early in the boot process before theref_trackerdir is created, so add alate_initcall()to register its files.Signed-off-by Jeff Layton <[email protected]>--- net/core/net_namespace.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)net/core/net_namespace.c +0
-0
net/core/net_namespace.c struct net *get_net_ns_by_pid(pid_t pid) · line 767
Quoted 7 lines
net/core/net_namespace.c +4
-0@@ -761,12 +761,44 @@ struct net *get_net_ns_by_pid(pid_t pid) }EXPORT_SYMBOL_GPL (get_net_ns_by_pid); +#ifdef CONFIG_NET_NS_REFCNT_TRACKER +static void net_ns_net_debugfs (struct net *net) +{ +char name[32 ];
Perhaps define REF_TRACKER_NAMESZ (I'm really bad at naming) and reuse it in the next two patches ?
Otherwise looks good to me.
PATCH v2 7/8 ref_tracker: widen the ref_tracker_dir.name field
In reply to Jeff LaytonCurrently it's 32 bytes, but with the need to move to unique names for debugfs files, that's inadequate. Move to a 64 byte name field.
--- include/linux/ref_tracker.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/ref_tracker.h +1 -1
@@ -21,7 +21,7 @@ struct ref_tracker_dir {#ifdef CONFIG_DEBUG_FS struct dentry *dentry;#endif -char name[32 ]; +char name[64 ];#endif };
Signature · 1 line
2.49.0
PATCH v2 8/8 net: register debugfs file for net_device refcnt tracker
In reply to Jeff LaytonAs a nearly-final step in register_netdevice(), finalize the name in the refcount tracker, and register a debugfs file for it.
--- net/core/dev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
net/core/dev.c +5 -1
@@ -10834,8 +10834,9 @@ static void netdev_free_phy_link_topology(struct net_device *dev)*/ int register_netdevice (struct net_device *dev) { -int ret;struct net *net =dev_net (dev); +char name[64 ]; +int ret;BUILD_BUG_ON (sizeof (netdev_features_t) *BITS_PER_BYTE <NETDEV_FEATURE_COUNT );
@@ -10994,6 +10995,9 @@ int register_netdevice(struct net_device *dev) dev->rtnl_link_state == RTNL_LINK_INITIALIZED)rtmsg_ifinfo (RTM_NEWLINK , dev, ~0U ,GFP_KERNEL ,0 ,NULL ); +/* Register debugfs file for the refcount tracker */ +if (snprintf (name,sizeof (name),"netdev-%s@%p" , dev->name, dev) <sizeof (name)) +ref_tracker_dir_debugfs (&dev->refcnt_tracker, name); out:return ret;
Signature · 1 line
2.49.0
Re: [PATCH v2 8/8] net: register debugfs file for net_device refcnt tracker
From: Jeff Layton <[email protected]>
Date: Tue, 15 Apr 2025 14:49:46 -0400
Quoted 24 lines
As a nearly-final step in
register_netdevice(), finalize the name in the refcount tracker, and register a debugfs file for it.Signed-off-by Jeff Layton <[email protected]>--- net/core/dev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)net/core/dev.c +2
-1@@ -10834,8 +10834,9 @@ static void netdev_free_phy_link_topology(struct net_device *dev)*/ int register_netdevice (struct net_device *dev) { -int ret;struct net *net =dev_net (dev); +char name[64 ]; +int ret;BUILD_BUG_ON (sizeof (netdev_features_t) *BITS_PER_BYTE <NETDEV_FEATURE_COUNT );
net/core/dev.c int register_netdevice(struct net_device *dev) · line 10999
Quoted 5 lines
net/core/dev.c +2
-0@@ -10994,6 +10995,9 @@ int register_netdevice(struct net_device *dev) dev->rtnl_link_state == RTNL_LINK_INITIALIZED)rtmsg_ifinfo (RTM_NEWLINK , dev, ~0U ,GFP_KERNEL ,0 ,NULL ); +/* Register debugfs file for the refcount tracker */ +if (snprintf (name,sizeof (name),"netdev-%s@%p" , dev->name, dev) <sizeof (name))
Given IFNAMSIZ is 16, using WARN_ON_ONCE() and calling ref_tracker_dir_debugfs() unconditionally would be better to catch future regression with syzbot.
Also, this hunk conflicts with
commit 097f171f98289cf737437599c40b0d1e81266e9e
Author: Jakub Kicinski <[email protected]> Date: Wed Apr 9 18:42:46 2025 -0700
net: convert dev->rtnl_link_state to a bool
Quoted 7 lines
net/core/dev.c +1
-0+ref_tracker_dir_debugfs (&dev->refcnt_tracker, name); out:return ret;Signature · 2 lines
-- 2.49.0