[PATCH] ext4: free HTree fname rb-tree at EOF in ext4_dx_readdir()
From: Chris Wiseman
Date: Sun Nov 15 2026 - 21:25:50 EST
When ext4_dx_readdir() finishes scanning the last HTree leaf block of
an indexed directory, ext4_htree_fill_tree() sets info->next_hash to
~0. On the final iteration, once rb_next(info->curr_node) returns NULL,
ext4_dx_readdir() sets ctx->pos to ext4_get_htree_eof(file) and breaks
out of the loop without calling free_rb_tree_fname(&info->root).
While ext4_dx_readdir() frees info->root when advancing across leaf
blocks or resetting on seek, reaching EOF leaves the final leaf block's
red-black tree of struct fname nodes pinned in file->private_data until
the directory fd is closed in ext4_release_dir(). Processes and daemons
that keep directory descriptors open after reading to EOF retain those
slab allocations for the lifetime of the open fd.
Free info->root before returning EOF when info->next_hash == ~0. If the
caller later seeks back, the existing !info->curr_node check at the top
of the loop repopulates the tree on demand.
Benchmark results (/proc/allocinfo, 256 open HTree dirs at EOF):
Metric Before After Delta
---------------------------------- ----------- ---------- --------
ext4_htree_store_dirent live slab 1,007,616 B 0 B -100.0%
ext4_htree_store_dirent live objs 10,496 0 -100.0%
/proc/meminfo SUnreclaim delta +1,246 kB +133 kB -89.3%
Signed-off-by: Chris Wiseman <chriswiseman@xxxxxxxxxx>
---
fs/ext4/dir.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
index 8d7b81e6948e..435300cd2c61 100644
--- a/fs/ext4/dir.c
+++ b/fs/ext4/dir.c
@@ -640,6 +640,7 @@ static int ext4_dx_readdir(struct file *file, struct dir_context *ctx)
info->curr_minor_hash = fname->minor_hash;
} else {
if (info->next_hash == ~0) {
+ free_rb_tree_fname(&info->root);
ctx->pos = ext4_get_htree_eof(file);
break;
}
--
2.56.0