linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress().
@ 2024-11-13  5:24 Kanchana P Sridhar
  2024-11-13  5:34 ` Yosry Ahmed
  0 siblings, 1 reply; 27+ messages in thread
From: Kanchana P Sridhar @ 2024-11-13  5:24 UTC (permalink / raw)
  To: linux-kernel, linux-mm, hannes, yosryahmed, nphamcs,
	chengming.zhou, usamaarif642, ryan.roberts, ying.huang, 21cnbao,
	akpm
  Cc: wajdi.k.feghali, vinodh.gopal, kanchana.p.sridhar

This is a hotfix for a potential zpool memory leak that could result in
the existing zswap_decompress():

        mutex_unlock(&acomp_ctx->mutex);

        if (src != acomp_ctx->buffer)
                zpool_unmap_handle(zpool, entry->handle);

Releasing the lock before the conditional does not protect the integrity of
"src", which is set earlier under the acomp_ctx mutex lock. This poses a
risk for the conditional behaving as intended, and consequently not
unmapping the zpool handle, which could cause a zswap zpool memory leak.

This patch moves the mutex_unlock() to occur after the conditional and
subsequent zpool_unmap_handle(). This ensures that the value of "src"
obtained earlier, with the mutex locked, does not change.

Even though an actual memory leak was not observed, this fix seems like a
cleaner implementation.

Signed-off-by: Kanchana P Sridhar <kanchana.p.sridhar@intel.com>
Fixes: 9c500835f279 ("mm: zswap: fix kernel BUG in sg_init_one")
---
 mm/zswap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/zswap.c b/mm/zswap.c
index f6316b66fb23..58810fa8ff23 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -986,10 +986,11 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
 	acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, PAGE_SIZE);
 	BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait));
 	BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
-	mutex_unlock(&acomp_ctx->mutex);
 
 	if (src != acomp_ctx->buffer)
 		zpool_unmap_handle(zpool, entry->handle);
+
+	mutex_unlock(&acomp_ctx->mutex);
 }
 
 /*********************************

base-commit: 0e5bdedb39ded767bff4c6184225578595cee98c
-- 
2.27.0



^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2024-11-20  2:32 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-13  5:24 [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress() Kanchana P Sridhar
2024-11-13  5:34 ` Yosry Ahmed
2024-11-13  5:58   ` Sridhar, Kanchana P
2024-11-13  6:21     ` Yosry Ahmed
2024-11-13 19:12       ` Sridhar, Kanchana P
2024-11-13 20:11         ` Yosry Ahmed
2024-11-13 20:59           ` Sridhar, Kanchana P
2024-11-13 20:59             ` Yosry Ahmed
2024-11-13 21:12               ` Sridhar, Kanchana P
2024-11-13 21:30         ` Johannes Weiner
2024-11-13 22:01           ` Yosry Ahmed
2024-11-13 22:13           ` Sridhar, Kanchana P
2024-11-14  0:28             ` Nhat Pham
2024-11-14  1:56               ` Sridhar, Kanchana P
2024-11-14  5:11                 ` Johannes Weiner
2024-11-14  6:37                   ` Sridhar, Kanchana P
2024-11-14  7:24                     ` Chengming Zhou
2024-11-15 21:12                       ` Sridhar, Kanchana P
2024-11-15 21:49                         ` Yosry Ahmed
2024-11-19 19:22                           ` Sridhar, Kanchana P
2024-11-19 19:27                             ` Yosry Ahmed
2024-11-19 19:41                               ` Sridhar, Kanchana P
2024-11-19 19:51                                 ` Yosry Ahmed
2024-11-19 22:35                                   ` Sridhar, Kanchana P
2024-11-19 23:44                                     ` Yosry Ahmed
2024-11-20  0:00                                       ` Sridhar, Kanchana P
2024-11-20  2:31                                       ` Chengming Zhou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox