On Thu, Feb 17, 2022 at 6:05 AM Andrzej Hajda <andrzej.hajda@xxxxxxxxx> wrote: > > To improve readibility of ref_tracker printing following changes > have been performed: > - added display name for ref_tracker_dir, > - stack trace is printed indented, in the same printk call, > - total number of references is printed every time, > - print info about dropped references. > > Signed-off-by: Andrzej Hajda <andrzej.hajda@xxxxxxxxx> > Reviewed-by: Chris Wilson <chris.p.wilson@xxxxxxxxx> > --- > include/linux/ref_tracker.h | 15 ++++++++++++--- > lib/ref_tracker.c | 28 ++++++++++++++++++++++------ > 2 files changed, 34 insertions(+), 9 deletions(-) > > diff --git a/include/linux/ref_tracker.h b/include/linux/ref_tracker.h > index b9c968a716483..090230e5b485d 100644 > --- a/include/linux/ref_tracker.h > +++ b/include/linux/ref_tracker.h > @@ -15,18 +15,26 @@ struct ref_tracker_dir { > refcount_t untracked; > struct list_head list; /* List of active trackers */ > struct list_head quarantine; /* List of dead trackers */ > + char name[32]; > #endif > }; > > #ifdef CONFIG_REF_TRACKER > -static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, > - unsigned int quarantine_count) > + > +// Temporary allow two and three arguments, until consumers are converted > +#define ref_tracker_dir_init(_d, _q, args...) _ref_tracker_dir_init(_d, _q, ##args, #_d) > +#define _ref_tracker_dir_init(_d, _q, _n, ...) __ref_tracker_dir_init(_d, _q, _n) > + > +static inline void __ref_tracker_dir_init(struct ref_tracker_dir *dir, > + unsigned int quarantine_count, > + const char *name) > { > INIT_LIST_HEAD(&dir->list); > INIT_LIST_HEAD(&dir->quarantine); > spin_lock_init(&dir->lock); > dir->quarantine_avail = quarantine_count; > refcount_set(&dir->untracked, 1); > + strlcpy(dir->name, name, sizeof(dir->name)); > stack_depot_init(); > } > > @@ -47,7 +55,8 @@ int ref_tracker_free(struct ref_tracker_dir *dir, > #else /* CONFIG_REF_TRACKER */ > > static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, > - unsigned int quarantine_count) > + unsigned int quarantine_count, > + ...) > { > } > > diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c > index 0e9c7d2828ccb..943cff08110e3 100644 > --- a/lib/ref_tracker.c > +++ b/lib/ref_tracker.c > @@ -1,4 +1,7 @@ > // SPDX-License-Identifier: GPL-2.0-or-later > + > +#define pr_fmt(fmt) "ref_tracker: " fmt > + > #include <linux/export.h> > #include <linux/list_sort.h> > #include <linux/ref_tracker.h> > @@ -7,6 +10,7 @@ > #include <linux/stackdepot.h> > > #define REF_TRACKER_STACK_ENTRIES 16 > +#define STACK_BUF_SIZE 1024 > > struct ref_tracker { > struct list_head head; /* anchor into dir->list or dir->quarantine */ > @@ -26,31 +30,43 @@ static int ref_tracker_cmp(void *priv, const struct list_head *a, const struct l > void __ref_tracker_dir_print(struct ref_tracker_dir *dir, > unsigned int display_limit) > { > - unsigned int i = 0, count = 0; > + unsigned int i = 0, count = 0, total = 0; > struct ref_tracker *tracker; > depot_stack_handle_t stack; > + char *sbuf; > > lockdep_assert_held(&dir->lock); > > if (list_empty(&dir->list)) > return; > > + sbuf = kmalloc(STACK_BUF_SIZE, GFP_NOWAIT); > + > + list_for_each_entry(tracker, &dir->list, head) > + ++total; Another iteration over a potential long list. You can count the @skipped number in the following iteration just fine. int skipped = 0; > + > list_sort(NULL, &dir->list, ref_tracker_cmp); > > list_for_each_entry(tracker, &dir->list, head) { > - if (i++ >= display_limit) > - break; > if (!count++) > stack = tracker->alloc_stack_handle; > if (stack == tracker->alloc_stack_handle && > !list_is_last(&tracker->head, &dir->list)) > continue; > + if (i++ >= display_limit) skipped++; > + continue; > > - pr_err("leaked %d references.\n", count); > - if (stack) > - stack_depot_print(stack); > + if (sbuf && !stack_depot_snprint(stack, sbuf, STACK_BUF_SIZE, 4)) > + sbuf[0] = 0; > + pr_err("%s@%pK has %d/%d users at\n%s\n", > + dir->name, dir, count, total, sbuf); > count = 0; > } > + if (i > display_limit) > + pr_err("%s@%pK skipped %d/%d reports with %d unique stacks.\n", > + dir->name, dir, count, total, i - display_limit); > + > + kfree(sbuf); > } > EXPORT_SYMBOL(__ref_tracker_dir_print); > > -- > 2.25.1 >