[PATCH 6/8] lockdep: Free unused reservation slabs to buddy allocator at late boot

From: Jim Cromie

Date: Wed Aug 26 2026 - 23:59:01 EST


During early boot, lockdep reserves a generous slab pool (e.g. 64 slabs =
4 MB) from memblock to guarantee uninterrupted initialization. Once
the kernel reaches late_initcall, the boot locking storm is complete,
and lockdep's steady-state working set is known.

Add lockdep_post_init_trim() as a late_initcall_sync handler. Calculate
required runtime headroom (default 100% headroom over boot usage, with
a floor of 32 slabs, or custom lockdep_slabs=N / lockdep_headroom=M%),
and return all excess slabs to the buddy page allocator via
free_reserved_page().

Also register a reboot notifier to log total lifetime slab consumption
and remaining headroom on clean system shutdown.

Signed-off-by: Jim Cromie <jim.cromie@xxxxxxxxx>
---
kernel/locking/lockdep.c | 71 +++++++++++++++++++++++++++++++++++++++++++
kernel/locking/lockdep_proc.c | 23 +++++++-------
2 files changed, 83 insertions(+), 11 deletions(-)

diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index b2dc7619a5e3..3fb6c07cea36 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -6992,6 +6992,77 @@ void __init lockdep_init(void)
sizeof(((struct task_struct *)NULL)->held_locks));
}

+static int lockdep_shutdown_notify(struct notifier_block *nb,
+ unsigned long code, void *unused)
+{
+ unsigned int used = lockdep_slabs_used - lockdep_nr_free_slabs;
+ unsigned int total = lockdep_nr_slabs;
+ unsigned int free_slabs = total > used ? total - used : 0;
+ unsigned int headroom_pct = used ? (free_slabs * 100) / used : 0;
+
+ pr_info("lockdep: shutdown summary : %u/%u slabs (%u kB/%u kB, %u%% headroom left), %lu classes, %lu chains, %u hlocks\n",
+ used, total,
+ (used * LOCKDEP_SLAB_SIZE) / 1024,
+ (total * LOCKDEP_SLAB_SIZE) / 1024,
+ headroom_pct,
+ nr_lock_classes, lock_chain_count(), chain_hlocks_used());
+
+ return NOTIFY_OK;
+}
+
+static struct notifier_block lockdep_reboot_nb = {
+ .notifier_call = lockdep_shutdown_notify,
+};
+
+static int __init lockdep_post_init_trim(void)
+{
+ unsigned int used = lockdep_slabs_used - lockdep_nr_free_slabs;
+ unsigned int initial_slabs = lockdep_nr_slabs;
+ unsigned int target_slabs, kept_headroom_slabs;
+ unsigned int freed_slabs = 0;
+ unsigned int i;
+
+ if (!lockdep_nr_slabs)
+ return 0;
+
+ /* Compute headroom requirement (default 100% or custom percentage) with 32-slab floor */
+ kept_headroom_slabs = DIV_ROUND_UP(used * requested_lockdep_headroom_pct, 100);
+ if (kept_headroom_slabs < 32)
+ kept_headroom_slabs = 32;
+
+ target_slabs = used + kept_headroom_slabs;
+
+ /* Satisfy both: target_slabs >= requested_lockdep_slabs AND headroom >= M% */
+ if (requested_lockdep_slabs > target_slabs)
+ target_slabs = requested_lockdep_slabs;
+
+ target_slabs = clamp_t(unsigned int, target_slabs, used, lockdep_nr_slabs);
+
+ /* Release excess slabs to buddy allocator */
+ if (target_slabs < lockdep_nr_slabs) {
+ for (i = target_slabs; i < lockdep_nr_slabs; i++) {
+ struct page *page = virt_to_page(lockdep_slabs[i]);
+ unsigned long p;
+
+ for (p = 0; p < (LOCKDEP_SLAB_SIZE >> PAGE_SHIFT); p++)
+ free_reserved_page(page + p);
+
+ lockdep_slabs[i] = NULL;
+ }
+ freed_slabs = lockdep_nr_slabs - target_slabs;
+ lockdep_nr_slabs = target_slabs;
+ }
+
+ pr_info("lockdep: boot complete : %u/%u slabs used, %u kept (%u%% headroom), %u returned to buddy (%u kB freed)\n",
+ used, initial_slabs, lockdep_nr_slabs,
+ lockdep_nr_slabs > used ? ((lockdep_nr_slabs - used) * 100) / used : 0,
+ freed_slabs, (freed_slabs * LOCKDEP_SLAB_SIZE) / 1024);
+
+ register_reboot_notifier(&lockdep_reboot_nb);
+ return 0;
+}
+late_initcall_sync(lockdep_post_init_trim);
+
static void
print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
const void *mem_to, struct held_lock *hlock)
diff --git a/kernel/locking/lockdep_proc.c b/kernel/locking/lockdep_proc.c
index 1916db9aa46b..95b76047918d 100644
--- a/kernel/locking/lockdep_proc.c
+++ b/kernel/locking/lockdep_proc.c
@@ -32,16 +32,17 @@
* bitmap and max_lock_class_idx.
*/
#define iterate_lock_classes(idx, class) \
- for (idx = 0, class = lock_classes; idx <= max_lock_class_idx; \
- idx++, class++)
+ for (idx = 0, class = idx_to_lock_class(0); \
+ idx <= max_lock_class_idx; \
+ idx++, class = idx_to_lock_class(idx))

static void *l_next(struct seq_file *m, void *v, loff_t *pos)
{
- struct lock_class *class = v;
+ unsigned long idx = ++*pos;

- ++class;
- *pos = class - lock_classes;
- return (*pos > max_lock_class_idx) ? NULL : class;
+ if (idx > max_lock_class_idx)
+ return NULL;
+ return idx_to_lock_class(idx);
}

static void *l_start(struct seq_file *m, loff_t *pos)
@@ -50,7 +51,7 @@ static void *l_start(struct seq_file *m, loff_t *pos)

if (idx > max_lock_class_idx)
return NULL;
- return lock_classes + idx;
+ return idx_to_lock_class(idx);
}

static void l_stop(struct seq_file *m, void *v)
@@ -59,7 +60,7 @@ static void l_stop(struct seq_file *m, void *v)

static void print_name(struct seq_file *m, struct lock_class *class)
{
- char str[KSYM_NAME_LEN];
+ char str[128];
const char *name = class->name;

if (!name) {
@@ -79,9 +80,9 @@ static int l_show(struct seq_file *m, void *v)
struct lock_class *class = v;
struct lock_list *entry;
char usage[LOCK_USAGE_CHARS];
- int idx = class - lock_classes;
+ int idx = class->class_idx;

- if (v == lock_classes)
+ if (idx == 0)
seq_printf(m, "all lock classes:\n");

if (!test_bit(idx, lock_classes_in_use))
@@ -133,7 +134,7 @@ static void *lc_start(struct seq_file *m, loff_t *pos)
if (*pos == 0)
return SEQ_START_TOKEN;

- return lock_chains + (*pos - 1);
+ return idx_to_lock_chain(*pos - 1);
}

static void *lc_next(struct seq_file *m, void *v, loff_t *pos)

--
2.55.0