Skip to contentSkip to conversation

fs/mbcache: cancel shrink work before destroying the cache

PATCH Applied Hyungjung Joo 3 participants 5 messages Mar 17 – Mar 18 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/mbcache: cancel shrink work before destroying the cache

From: HyungJung Joo <[email protected]>

mb_cache_destroy() calls shrinker_free() and then frees all cache entries and the cache itself, but it does not cancel the pending c_shrink_work work item first.

If mb_cache_entry_create() schedules c_shrink_work via schedule_work() and the work item is still pending or running when mb_cache_destroy() runs, mb_cache_shrink_worker() will access the cache after its memory has been freed, causing a use-after-free.

This is only reachable by a privileged user (root or CAP_SYS_ADMIN) who can trigger the last put of a mounted ext2/ext4/ocfs2 filesystem.

Cancel the work item with cancel_work_sync() before calling shrinker_free(), ensuring the worker has finished and will not be rescheduled before the cache is torn down.

---
 fs/mbcache.c | 1 +
 1 file changed, 1 insertion(+)
fs/mbcache.c +1 -0
index 480d02d6ebf0..2a6319b4072c 100644
@@ -406,6 +406,7 @@ void mb_cache_destroy(struct mb_cache *cache)
 {
 	struct mb_cache_entry *entry, *next;
 
+	cancel_work_sync(&cache->c_shrink_work);
 	shrinker_free(cache->c_shrink);
 
 	/*
Signature · 1 line
2.34.1
maintainer Applied
#1 lore raw
Quoted 10 lines · Hyungjung Joo

mb_cache_destroy() calls shrinker_free() and then frees all cache entries and the cache itself, but it does not cancel the pending c_shrink_work work item first.

If mb_cache_entry_create() schedules c_shrink_work via schedule_work() and the work item is still pending or running when mb_cache_destroy() runs, mb_cache_shrink_worker() will access the cache after its memory has been freed, causing a use-after-free.

[...]

Pretty sure this is AI generated and it misses a Fixes: tag but otherwise looks correct.

---

Applied to the vfs-7.1.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.1.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.1.misc

[1/1] fs/mbcache: cancel shrink work before destroying the cache

      https://git.kernel.org/vfs/vfs/c/d227786ab111
#2 lore raw

2026년 3월 17일 (화) PM 11:38, Christian Brauner <[email protected]>님이 작성:

Quoted 2 lines

Pretty sure this is AI generated and it misses a Fixes: tag but otherwise looks correct.

I'm sorry, forgot the fixes tag.
Below is the correct fixes: tag.

Fixes c2f3140fe2ec

Thanks.

#3 lore raw
Quoted 9 lines · Hyungjung Joo

2026년 3월 17일 (화) PM 11:38, Christian Brauner <[email protected]>님이 작성:

Quoted 2 lines

Pretty sure this is AI generated and it misses a Fixes: tag but otherwise looks correct.

I'm sorry, forgot the fixes tag.
Below is the correct fixes: tag.

Fixes c2f3140fe2ec

I had already added that.

reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Hyungjung Joo
Quoted 11 lines · Hyungjung Joo

From: HyungJung Joo <[email protected]>

mb_cache_destroy() calls shrinker_free() and then frees all cache entries and the cache itself, but it does not cancel the pending c_shrink_work work item first.

If mb_cache_entry_create() schedules c_shrink_work via schedule_work() and the work item is still pending or running when mb_cache_destroy() runs, mb_cache_shrink_worker() will access the cache after its memory has been freed, causing a use-after-free.

Quoted 8 lines

This is only reachable by a privileged user (root or CAP_SYS_ADMIN) who can trigger the last put of a mounted ext2/ext4/ocfs2 filesystem.

Cancel the work item with cancel_work_sync() before calling shrinker_free(), ensuring the worker has finished and will not be rescheduled before the cache is torn down.

Thanks! The patch looks good to me. Feel free to add:

Honza

Quoted 19 lines
---
 fs/mbcache.c | 1 +
 1 file changed, 1 insertion(+)
fs/mbcache.c +1 -0
index 480d02d6ebf0..2a6319b4072c 100644
@@ -406,6 +406,7 @@ void mb_cache_destroy(struct mb_cache *cache)
 {
 	struct mb_cache_entry *entry, *next;
 
+	cancel_work_sync(&cache->c_shrink_work);
 	shrinker_free(cache->c_shrink);
 
 	/*
Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR