From: Qu Wenruo Date: Sat, 20 Nov 2021 08:34:11 +0000 (+0800) Subject: btrfs: fix the memory leak caused in lzo_compress_pages() X-Git-Tag: v5.16-rc3~26^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=daf87e953527b03c0bd4c0f41d704ba71186256d;p=thirdparty%2Fkernel%2Flinux.git btrfs: fix the memory leak caused in lzo_compress_pages() [BUG] Fstests generic/027 is pretty easy to trigger a slow but steady memory leak if run with "-o compress=lzo" mount option. Normally one single run of generic/027 is enough to eat up at least 4G ram. [CAUSE] In commit d4088803f511 ("btrfs: subpage: make lzo_compress_pages() compatible") we changed how @page_in is released. But that refactoring makes @page_in only released after all pages being compressed. This leaves error path not releasing @page_in. And by "error path" things like incompressible data will also be treated as an error (-E2BIG). Thus it can cause a memory leak if even nothing wrong happened. [FIX] Add check under @out label to release @page_in when needed, so when we hit any error, the input page is properly released. Reported-by: Josef Bacik Fixes: d4088803f511 ("btrfs: subpage: make lzo_compress_pages() compatible") Reviewed-and-tested-by: Josef Bacik Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba --- diff --git a/fs/btrfs/lzo.c b/fs/btrfs/lzo.c index f410ceabcdbd8..e61f00a192dbe 100644 --- a/fs/btrfs/lzo.c +++ b/fs/btrfs/lzo.c @@ -276,6 +276,8 @@ int lzo_compress_pages(struct list_head *ws, struct address_space *mapping, *total_out = cur_out; *total_in = cur_in - start; out: + if (page_in) + put_page(page_in); *out_pages = DIV_ROUND_UP(cur_out, PAGE_SIZE); return ret; }