Skip to content

Commit c2a2925

Browse files
melverakpm00
authored andcommitted
stackdepot: add stats counters exported via debugfs
Add a few basic stats counters for stack depot that can be used to derive if stack depot is working as intended. This is a snapshot of the new stats after booting a system with a KASAN-enabled kernel: $ cat /sys/kernel/debug/stackdepot/stats pools: 838 allocations: 29861 frees: 6561 in_use: 23300 freelist_size: 1840 Generally, "pools" should be well below the max; once the system is booted, "in_use" should remain relatively steady. Link: https://lkml.kernel.org/r/20240118110216.2539519-1-elver@google.com Signed-off-by: Marco Elver <elver@google.com> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Alexander Potapenko <glider@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent f6564fc commit c2a2925

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

lib/stackdepot.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#define pr_fmt(fmt) "stackdepot: " fmt
1616

17+
#include <linux/debugfs.h>
1718
#include <linux/gfp.h>
1819
#include <linux/jhash.h>
1920
#include <linux/kernel.h>
@@ -115,6 +116,23 @@ static bool new_pool_required = true;
115116
/* Lock that protects the variables above. */
116117
static DEFINE_RWLOCK(pool_rwlock);
117118

119+
/* Statistics counters for debugfs. */
120+
enum depot_counter_id {
121+
DEPOT_COUNTER_ALLOCS,
122+
DEPOT_COUNTER_FREES,
123+
DEPOT_COUNTER_INUSE,
124+
DEPOT_COUNTER_FREELIST_SIZE,
125+
DEPOT_COUNTER_COUNT,
126+
};
127+
static long counters[DEPOT_COUNTER_COUNT];
128+
static const char *const counter_names[] = {
129+
[DEPOT_COUNTER_ALLOCS] = "allocations",
130+
[DEPOT_COUNTER_FREES] = "frees",
131+
[DEPOT_COUNTER_INUSE] = "in_use",
132+
[DEPOT_COUNTER_FREELIST_SIZE] = "freelist_size",
133+
};
134+
static_assert(ARRAY_SIZE(counter_names) == DEPOT_COUNTER_COUNT);
135+
118136
static int __init disable_stack_depot(char *str)
119137
{
120138
return kstrtobool(str, &stack_depot_disabled);
@@ -277,6 +295,7 @@ static void depot_init_pool(void *pool)
277295
stack->handle.extra = 0;
278296

279297
list_add(&stack->list, &free_stacks);
298+
counters[DEPOT_COUNTER_FREELIST_SIZE]++;
280299
}
281300

282301
/* Save reference to the pool to be used by depot_fetch_stack(). */
@@ -376,6 +395,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc)
376395
/* Get and unlink the first entry from the freelist. */
377396
stack = list_first_entry(&free_stacks, struct stack_record, list);
378397
list_del(&stack->list);
398+
counters[DEPOT_COUNTER_FREELIST_SIZE]--;
379399

380400
/* Limit number of saved frames to CONFIG_STACKDEPOT_MAX_FRAMES. */
381401
if (size > CONFIG_STACKDEPOT_MAX_FRAMES)
@@ -394,6 +414,8 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc)
394414
*/
395415
kmsan_unpoison_memory(stack, DEPOT_STACK_RECORD_SIZE);
396416

417+
counters[DEPOT_COUNTER_ALLOCS]++;
418+
counters[DEPOT_COUNTER_INUSE]++;
397419
return stack;
398420
}
399421

@@ -426,6 +448,10 @@ static void depot_free_stack(struct stack_record *stack)
426448
lockdep_assert_held_write(&pool_rwlock);
427449

428450
list_add(&stack->list, &free_stacks);
451+
452+
counters[DEPOT_COUNTER_FREELIST_SIZE]++;
453+
counters[DEPOT_COUNTER_FREES]++;
454+
counters[DEPOT_COUNTER_INUSE]--;
429455
}
430456

431457
/* Calculates the hash for a stack. */
@@ -690,3 +716,30 @@ unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle)
690716
return parts.extra;
691717
}
692718
EXPORT_SYMBOL(stack_depot_get_extra_bits);
719+
720+
static int stats_show(struct seq_file *seq, void *v)
721+
{
722+
/*
723+
* data race ok: These are just statistics counters, and approximate
724+
* statistics are ok for debugging.
725+
*/
726+
seq_printf(seq, "pools: %d\n", data_race(pools_num));
727+
for (int i = 0; i < DEPOT_COUNTER_COUNT; i++)
728+
seq_printf(seq, "%s: %ld\n", counter_names[i], data_race(counters[i]));
729+
730+
return 0;
731+
}
732+
DEFINE_SHOW_ATTRIBUTE(stats);
733+
734+
static int depot_debugfs_init(void)
735+
{
736+
struct dentry *dir;
737+
738+
if (stack_depot_disabled)
739+
return 0;
740+
741+
dir = debugfs_create_dir("stackdepot", NULL);
742+
debugfs_create_file("stats", 0444, dir, NULL, &stats_fops);
743+
return 0;
744+
}
745+
late_initcall(depot_debugfs_init);

0 commit comments

Comments
 (0)