Re: [PATCH] ocfs2: fix local_alloc_bh leak in ocfs2_shutdown_local_alloc()
From: Joseph Qi
Date: Wed Nov 18 2026 - 01:24:10 EST
On 11/17/26 10:58 PM, Zhiyuan Zhang wrote:
> From: Zhiyuan Zhang <zhiyuanzhang912@xxxxxxxxx>
>
> ocfs2_shutdown_local_alloc() owns the buffer_head reference stored in
> osb->local_alloc_bh, but only its success path drops it: after clearing
> and journalling the local alloc inode it calls brelse() and clears the
> pointer. Every error exit after the local allocator has been enabled
> returns with the reference still held:
>
> - failure to look up the local alloc or global bitmap system inode
> - ocfs2_inode_lock() failure on the global bitmap
> - ocfs2_start_trans() failure, e.g. on an aborted journal
> - kmemdup() of the local alloc dinode returning NULL
> - ocfs2_journal_access_di() failure
>
> Nothing else releases it, so the buffer_head and its page stay pinned
> after the unmount.
>
> Drop the reference at the common out: label and clear the pointer
> there. The success path has already set osb->local_alloc_bh to NULL
> before it reaches out:, so nothing is released twice. The reservation
> path is not affected: it takes its own reference with get_bh() and
> drops it in ocfs2_free_ac_resource(). Also mark the local allocator
> unused when the buffer is dropped, so that a NULL local_alloc_bh is
> never left behind with the allocator still marked as in use.
>
> Fixes: ccd979bdbce9 ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem")
> Assisted-by: Claude:claude-fable-5-2
> Signed-off-by: Zhiyuan Zhang <zhiyuanzhang912@xxxxxxxxx>
Looks fine.
Reviewed-by: Joseph Qi <joseph.qi@xxxxxxxxxxxxxxxxx>
> ---
>
> Notes:
> Testing: QEMU (x86_64), local mount on a loop device, kernel built with
> KASAN, PROVE_LOCKING, DEBUG_ATOMIC_SLEEP, DEBUG_VM and DEBUG_LIST. A
> test-only pr_info() printed the pfn and b_count of local_alloc_bh on
> entry and exit of ocfs2_shutdown_local_alloc(), and the page was looked
> up in /proc/kpageflags after umount, losetup -d and drop_caches.
>
> All six error exits were exercised, twice per boot, two boots per
> kernel. The ocfs2_start_trans() failure is the real one: the loop
> device is shrunk under the mounted filesystem so that jbd2 aborts the
> journal. The other five were forced with a test-only knob.
>
> - Unpatched, 24 of 24: the function returns with b_count still 1, and
> the page is still allocated at the end of the run.
> - Patched, 24 of 24: the reference is dropped and the page is free.
> - A clean unmount frees the page on both kernels (8 of 8).
> - No KASAN, lockdep or other warnings on either kernel.
>
> The leak is one page per failed shutdown. kmemleak does not report it,
> and neither Buffers in /proc/meminfo nor the buffer_head slab count
> shows it.
>
> AI assistants were used to find the leak, to write the patch and the
> changelog, and to run the tests above.
>
> fs/ocfs2/localalloc.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/fs/ocfs2/localalloc.c b/fs/ocfs2/localalloc.c
> index 82d290cf4432..d8d81cef92fc 100644
> --- a/fs/ocfs2/localalloc.c
> +++ b/fs/ocfs2/localalloc.c
> @@ -462,6 +462,17 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
> iput(main_bm_inode);
>
> out:
> + /*
> + * The success path has already dropped the reference and cleared
> + * the pointer, so this is a no-op there. Every error exit after
> + * the local allocator was enabled used to leak local_alloc_bh.
> + */
> + if (osb->local_alloc_bh) {
> + brelse(osb->local_alloc_bh);
> + osb->local_alloc_bh = NULL;
> + osb->local_alloc_state = OCFS2_LA_UNUSED;
> + }
> +
> iput(local_alloc_inode);
>
> kfree(alloc_copy);
>
> base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4