From 4cbaa4d55c37e1e0687fd575febccae02ec6d5e8 Mon Sep 17 00:00:00 2001 From: roy Date: Sun, 4 Aug 2024 22:01:03 +0800 Subject: [PATCH] Fix cache leak in umount simplefs 1. When the cache be destoryed, we should call rcu_barrier() to prevent call_rcu() still works and this also prevent the cache be reused. 2. After simplefs_destroy_inode_cache() function, we need rcu_barrier() to make sure all memory have be freed. --- fs.c | 4 ++++ super.c | 3 +++ 2 files changed, 7 insertions(+) diff --git a/fs.c b/fs.c index da7e187..7bd6ac3 100644 --- a/fs.c +++ b/fs.c @@ -58,6 +58,8 @@ static int __init simplefs_init(void) err_inode: simplefs_destroy_inode_cache(); + /* Only after rcu_barrier() is the memory guaranteed to be freed. */ + rcu_barrier(); err: return ret; } @@ -69,6 +71,8 @@ static void __exit simplefs_exit(void) pr_err("Failed to unregister file system\n"); simplefs_destroy_inode_cache(); + /* Only after rcu_barrier() is the memory guaranteed to be freed. */ + rcu_barrier(); pr_info("module unloaded\n"); } diff --git a/super.c b/super.c index e966ec9..cad118c 100644 --- a/super.c +++ b/super.c @@ -32,6 +32,9 @@ int simplefs_init_inode_cache(void) /* De-allocate the inode cache */ void simplefs_destroy_inode_cache(void) { + /* wait for call_rcu() and prevent the free cache be used */ + rcu_barrier(); + kmem_cache_destroy(simplefs_inode_cache); }