diff mbox series

btrfs: send: fix cache entry leak after failure to add it to the name cache

Message ID d012eb57320bc93ec45583a7ff86575060bcf51c.1676031742.git.fdmanana@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: send: fix cache entry leak after failure to add it to the name cache | expand

Commit Message

Filipe Manana Feb. 10, 2023, 12:27 p.m. UTC
From: Filipe Manana <fdmanana@suse.com>

At __get_cur_name_and_parent(), if we fail to insert a name cache entry to
the name cache, we end up returning without freeing the entry, resulting
in a memory leak. Fix this by freeing the entry if the insertion fails.

The issue was introduced by the following patch:

  "btrfs: send: use the lru cache to implement the name cache"

which is not yet in Linus' tree.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/send.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

David Sterba Feb. 15, 2023, 6:37 p.m. UTC | #1
On Fri, Feb 10, 2023 at 12:27:35PM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> At __get_cur_name_and_parent(), if we fail to insert a name cache entry to
> the name cache, we end up returning without freeing the entry, resulting
> in a memory leak. Fix this by freeing the entry if the insertion fails.
> 
> The issue was introduced by the following patch:
> 
>   "btrfs: send: use the lru cache to implement the name cache"

Folded to the patch, thanks.
diff mbox series

Patch

diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index f936c203f386..e5c963bb873d 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -2385,8 +2385,10 @@  static int __get_cur_name_and_parent(struct send_ctx *sctx,
 		nce->need_later_update = 1;
 
 	nce_ret = btrfs_lru_cache_store(&sctx->name_cache, &nce->entry, GFP_KERNEL);
-	if (nce_ret < 0)
+	if (nce_ret < 0) {
+		kfree(nce);
 		ret = nce_ret;
+	}
 
 out:
 	return ret;