mm: zswap: increase reject_compress_poor but not reject_compress_fail if compression returns ENOSPC
Subsystems (2)
PATCH 1/1 mm: zswap: increase reject_compress_poor but not reject_compress_fail if compression returns ENOSPC
From: Barry Song <[email protected]>
My commit fc8580edbaa6 ("mm: zsmalloc: return -ENOSPC rather than -EINVAL in zs_malloc while size is too large") wanted to depend on zs_malloc's returned ENOSPC to distinguish the case that compressed data is larger than the original data from normal compression cases. The commit, for sure, was correct and worked as expected but the code wouldn't run to there after commit 744e1885922a ("crypto: scomp - fix req->dst buffer overflow") as Chengming's this patch makes zswap_store() goto out immediately after the special compression case happens. So there is no chance to execute zs_malloc() now. We need to fix the count right after compressions return ENOSPC.
--- mm/zswap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
mm/zswap.c +4 -1
@@ -1627,7 +1627,10 @@ bool zswap_store(struct folio *folio) dlen = acomp_ctx->req->dlen;if (ret) { - zswap_reject_compress_fail++; +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else + zswap_reject_compress_fail++; goto put_dstmem; }
Signature · 1 line
2.34.1
Quoted 13 lines · Barry Song
From: Barry Song <[email protected]>
My commit
fc8580edbaa6("mm: zsmalloc: return-ENOSPCrather than-EINVALinzs_mallocwhile size is too large") wanted to depend on zs_malloc's returned ENOSPC to distinguish the case that compressed data is larger than the original data from normal compression cases. The commit, for sure, was correct and worked as expected but the code wouldn't run to there after commit744e1885922a("crypto: scomp - fixreq->dstbuffer overflow") as Chengming's this patch makeszswap_store()goto out immediately after the special compression case happens. So there is no chance to executezs_malloc()now. We need to fix the count right after compressions return ENOSPC.
Quoted 1 line
Fixes fc8580edbaa6 ("mm: zsmalloc: return -ENOSPC rather than -EINVAL in zs_malloc while size is too large")
I don't see how this is a fix for that commit. Commit fc8580edbaa6 made sure zsmalloc returns a correct errno when the compressed size is too large. The fact that zswap stores were failing before calling into zsmalloc and not reporting the error correctly in debug counters is not that commits fault.
I think the proper fixes should be 744e1885922a if it introduced the first scenario where -ENOSPC can be returned from scomp without handling it properly in zswap. If -ENOSPC was a possible return value before that, then it should be cb61dad80fdc ("zswap: export compression failure stats"), where the counter was introduced.
Quoted 13 lines
Cc Chengming Zhou <[email protected]>Cc Nhat Pham <[email protected]>Cc Sergey Senozhatsky <[email protected]>Signed-off-by Barry Song <[email protected]>--- mm/zswap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)mm/zswap.c +0
-0
mm/zswap.c bool zswap_store(struct folio *folio) · line 1633
Quoted 8 lines
mm/zswap.c +4
-1@@ -1627,7 +1627,10 @@ bool zswap_store(struct folio *folio) dlen = acomp_ctx->req->dlen;if (ret) { - zswap_reject_compress_fail++; +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else + zswap_reject_compress_fail++;
With this diff, we have four locations in zswap_store() where we increment zswap_reject_compress_{poor/fail}.
How about the following instead?A
mm/zswap.c +6 -10
@@ -1059,24 +1059,16 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry)*/ ret =crypto_wait_req (crypto_acomp_compress (acomp_ctx->req), &acomp_ctx->wait); dlen = acomp_ctx->req->dlen; -if (ret) { - zswap_reject_compress_fail++; +if (ret) goto unlock; - } zpool =zswap_find_zpool (entry); gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;if (zpool_malloc_support_movable (zpool)) gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; ret =zpool_malloc (zpool, dlen, gfp, &handle); -if (ret == -ENOSPC ) { - zswap_reject_compress_poor++; - goto unlock; - } -if (ret) { - zswap_reject_alloc_fail++; +if (ret) goto unlock; - } buf =zpool_map_handle (zpool, handle,ZPOOL_MM_WO );memcpy (buf, dst, dlen);
@@ -1086,6 +1078,10 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) entry->length = dlen; unlock: +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else if (ret) + zswap_reject_alloc_fail++;mutex_unlock (&acomp_ctx->mutex);return ret ==0 ; }
Quoted 6 lines
mm/zswap.c +0
-0goto put_dstmem; }Signature · 1 line
2.34.1
Quoted 22 lines · Yosry Ahmed
Quoted 13 lines · Barry Song
From: Barry Song <[email protected]>
My commit
fc8580edbaa6("mm: zsmalloc: return-ENOSPCrather than-EINVALinzs_mallocwhile size is too large") wanted to depend on zs_malloc's returned ENOSPC to distinguish the case that compressed data is larger than the original data from normal compression cases. The commit, for sure, was correct and worked as expected but the code wouldn't run to there after commit744e1885922a("crypto: scomp - fixreq->dstbuffer overflow") as Chengming's this patch makeszswap_store()goto out immediately after the special compression case happens. So there is no chance to executezs_malloc()now. We need to fix the count right after compressions return ENOSPC.Quoted 1 line
Fixes fc8580edbaa6 ("mm: zsmalloc: return -ENOSPC rather than -EINVAL in zs_malloc while size is too large")I don't see how this is a fix for that commit. Commit
fc8580edbaa6made sure zsmalloc returns a correct errno when the compressed size is too large. The fact that zswap stores were failing before calling into zsmalloc and not reporting the error correctly in debug counters is not that commits fault.
Quoted 5 lines
I think the proper fixes should be
744e1885922aif it introduced the first scenario where-ENOSPCcan be returned from scomp without handling it properly in zswap. If-ENOSPCwas a possible return value before that, then it should becb61dad80fdc("zswap: export compression failure stats"), where the counter was introduced.
Right, 744e1885922a maybe a better fixes target.
Quoted 60 lines
Quoted 14 lines
Cc Chengming Zhou <[email protected]>Cc Nhat Pham <[email protected]>Cc Sergey Senozhatsky <[email protected]>Signed-off-by Barry Song <[email protected]>--- mm/zswap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)mm/zswap.c +0
-0mm/zswap.c bool zswap_store(struct folio *folio) · line 1633
Quoted 8 lines
mm/zswap.c +4
-1@@ -1627,7 +1627,10 @@ bool zswap_store(struct folio *folio) dlen = acomp_ctx->req->dlen;if (ret) { - zswap_reject_compress_fail++; +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else + zswap_reject_compress_fail++;With this diff, we have four locations in
zswap_store()where we increment zswap_reject_compress_{poor/fail}.How about the following instead?A
mm/zswap.c +2
-10@@ -1059,24 +1059,16 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry)*/ ret =crypto_wait_req (crypto_acomp_compress (acomp_ctx->req), &acomp_ctx->wait); dlen = acomp_ctx->req->dlen; -if (ret) { - zswap_reject_compress_fail++; +if (ret) goto unlock; - } zpool =zswap_find_zpool (entry); gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;if (zpool_malloc_support_movable (zpool)) gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; ret =zpool_malloc (zpool, dlen, gfp, &handle); -if (ret == -ENOSPC ) { - zswap_reject_compress_poor++; - goto unlock; - } -if (ret) { - zswap_reject_alloc_fail++; +if (ret) goto unlock; - } buf =zpool_map_handle (zpool, handle,ZPOOL_MM_WO );memcpy (buf, dst, dlen);
mm/zswap.c static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) · line 1084
Quoted 7 lines
mm/zswap.c +4
-0@@ -1086,6 +1078,10 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) entry->length = dlen; unlock: +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else if (ret) + zswap_reject_alloc_fail++;
Here have two cases: zswap_reject_compress_fail, zswap_reject_alloc_fail.
Quoted 10 lines
mm/zswap.c +0
-0mutex_unlock (&acomp_ctx->mutex);return ret ==0 ; }Quoted 7 lines
mm/zswap.c +0
-0goto put_dstmem; }Signature · 1 line
2.34.1
Quoted 16 lines · Chengming Zhou
Quoted 14 lines · Yosry Ahmed
Quoted 13 lines · Barry Song
From: Barry Song <[email protected]>
My commit
fc8580edbaa6("mm: zsmalloc: return-ENOSPCrather than-EINVALinzs_mallocwhile size is too large") wanted to depend on zs_malloc's returned ENOSPC to distinguish the case that compressed data is larger than the original data from normal compression cases. The commit, for sure, was correct and worked as expected but the code wouldn't run to there after commit744e1885922a("crypto: scomp - fixreq->dstbuffer overflow") as Chengming's this patch makeszswap_store()goto out immediately after the special compression case happens. So there is no chance to executezs_malloc()now. We need to fix the count right after compressions return ENOSPC.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Fixes fc8580edbaa6 ("mm: zsmalloc: return -ENOSPC rather than -EINVAL in zs_malloc while size is too large")I don't see how this is a fix for that commit. Commit
fc8580edbaa6made sure zsmalloc returns a correct errno when the compressed size is too large. The fact that zswap stores were failing before calling into zsmalloc and not reporting the error correctly in debug counters is not that commits fault.
Hi Yosry, Chengming,
Thanks for your quick responses.
Quoted 7 lines
Quoted 5 lines
I think the proper fixes should be
744e1885922aif it introduced the first scenario where-ENOSPCcan be returned from scomp without handling it properly in zswap. If-ENOSPCwas a possible return value before that, then it should becb61dad80fdc("zswap: export compression failure stats"), where the counter was introduced.Right,
744e1885922amaybe a better fixes target.
I agree 744e1885922a is a better fixes target.
Quoted 63 lines
Quoted 63 lines
Quoted 15 lines
Cc Chengming Zhou <[email protected]>Cc Nhat Pham <[email protected]>Cc Sergey Senozhatsky <[email protected]>Signed-off-by Barry Song <[email protected]>--- mm/zswap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)mm/zswap.c +0
-0mm/zswap.c bool zswap_store(struct folio *folio) · line 1633
Quoted 8 lines
mm/zswap.c +4
-1@@ -1627,7 +1627,10 @@ bool zswap_store(struct folio *folio) dlen = acomp_ctx->req->dlen;if (ret) { - zswap_reject_compress_fail++; +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else + zswap_reject_compress_fail++;With this diff, we have four locations in
zswap_store()where we increment zswap_reject_compress_{poor/fail}.How about the following instead?A
mm/zswap.c +2
-10@@ -1059,24 +1059,16 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry)*/ ret =crypto_wait_req (crypto_acomp_compress (acomp_ctx->req), &acomp_ctx->wait); dlen = acomp_ctx->req->dlen; -if (ret) { - zswap_reject_compress_fail++; +if (ret) goto unlock; - } zpool =zswap_find_zpool (entry); gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;if (zpool_malloc_support_movable (zpool)) gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; ret =zpool_malloc (zpool, dlen, gfp, &handle); -if (ret == -ENOSPC ) { - zswap_reject_compress_poor++; - goto unlock; - } -if (ret) { - zswap_reject_alloc_fail++; +if (ret) goto unlock; - } buf =zpool_map_handle (zpool, handle,ZPOOL_MM_WO );memcpy (buf, dst, dlen);@@ -1086,6 +1078,10 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) entry->length = dlen;
Quoted 7 lines
mm/zswap.c static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) · line 1084
Quoted 5 lines
mm/zswap.c +4
-0unlock: +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else if (ret) + zswap_reject_alloc_fail++;Here have two cases:
zswap_reject_compress_fail,zswap_reject_alloc_fail.
Is it safe to differentiate these two cases by checking ret == -ENOMEM ? otherwise, it seems the original patch still makes more sense?
Quoted 11 lines
Quoted 11 lines
mm/zswap.c +0
-0mutex_unlock (&acomp_ctx->mutex);return ret ==0 ; }Quoted 7 lines
mm/zswap.c +0
-1goto put_dstmem; } --2.34.1
Thanks
Barry
Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
mm/zswap.c bool zswap_store(struct folio *folio) · line 1633
Quoted 13 lines
mm/zswap.c +4
-1@@ -1627,7 +1627,10 @@ bool zswap_store(struct folio *folio) dlen = acomp_ctx->req->dlen;if (ret) { - zswap_reject_compress_fail++; +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else + zswap_reject_compress_fail++;With this diff, we have four locations in
zswap_store()where we increment zswap_reject_compress_{poor/fail}.How about the following instead?A
mm/zswap.c +2
-10@@ -1059,24 +1059,16 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry)*/ ret =crypto_wait_req (crypto_acomp_compress (acomp_ctx->req), &acomp_ctx->wait); dlen = acomp_ctx->req->dlen; -if (ret) { - zswap_reject_compress_fail++; +if (ret) goto unlock; - } zpool =zswap_find_zpool (entry); gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;if (zpool_malloc_support_movable (zpool)) gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; ret =zpool_malloc (zpool, dlen, gfp, &handle); -if (ret == -ENOSPC ) { - zswap_reject_compress_poor++; - goto unlock; - } -if (ret) { - zswap_reject_alloc_fail++; +if (ret) goto unlock; - } buf =zpool_map_handle (zpool, handle,ZPOOL_MM_WO );memcpy (buf, dst, dlen);@@ -1086,6 +1078,10 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) entry->length = dlen;
Quoted 7 lines
Quoted 7 lines
mm/zswap.c static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) · line 1084
Quoted 5 lines
mm/zswap.c +4
-0unlock: +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else if (ret) + zswap_reject_alloc_fail++;Here have two cases:
zswap_reject_compress_fail,zswap_reject_alloc_fail.
Ah brain fart, sorry.
Quoted 3 lines
Is it safe to differentiate these two cases by checking ret ==
-ENOMEM? otherwise, it seems the original patch still makes more sense?
I don't think it is in all cases, some allocators return other error codes. It seems unlikely that we'll get any of them, but it can be missed in the future. How about we use different return codes to differentiate failures, and still centralize the counters handling.
Something like the following (ideally that one is not a brain fart):
mm/zswap.c +12 -13
@@ -1021,12 +1021,12 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) {struct crypto_acomp_ctx *acomp_ctx;struct scatterlist input, output; +int comp_ret =0 , alloc_ret =0 ;unsigned int dlen =PAGE_SIZE ;unsigned long handle;struct zpool *zpool;char *buf;gfp_t gfp; -int ret;u8 *dst; acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx);
@@ -1057,26 +1057,18 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry)* but in different threads running on different cpu, we have different * acomp instance, so multiple threads can do (de)compression in parallel. */ - ret =crypto_wait_req (crypto_acomp_compress (acomp_ctx->req), &acomp_ctx->wait); + comp_ret =crypto_wait_req (crypto_acomp_compress (acomp_ctx->req), &acomp_ctx->wait); dlen = acomp_ctx->req->dlen; -if (ret) { - zswap_reject_compress_fail++; +if (comp_ret) goto unlock; - } zpool =zswap_find_zpool (entry); gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;if (zpool_malloc_support_movable (zpool)) gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; - ret =zpool_malloc (zpool, dlen, gfp, &handle); -if (ret == -ENOSPC ) { - zswap_reject_compress_poor++; - goto unlock; - } -if (ret) { - zswap_reject_alloc_fail++; + alloc_ret =zpool_malloc (zpool, dlen, gfp, &handle); +if (alloc_ret) goto unlock; - } buf =zpool_map_handle (zpool, handle,ZPOOL_MM_WO );memcpy (buf, dst, dlen);
@@ -1086,6 +1078,13 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) entry->length = dlen; unlock: +if (comp_ret == -ENOSPC || alloc_ret == -ENOSPC ) + zswap_reject_compress_poor++; +else if (comp_ret) + zswap_reject_compress_fail++; +else if (alloc_ret) + zswap_reject_alloc_fail++; +mutex_unlock (&acomp_ctx->mutex);return ret ==0 ; }
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
mm/zswap.c +0
-0mutex_unlock (&acomp_ctx->mutex);return ret ==0 ; }Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
mm/zswap.c +0
-1goto put_dstmem; } --2.34.1
Thanks
Barry
Nhat Pham
maintainerQuoted 23 lines · Yosry Ahmed
Quoted 13 lines · Barry Song
From: Barry Song <[email protected]>
My commit
fc8580edbaa6("mm: zsmalloc: return-ENOSPCrather than-EINVALinzs_mallocwhile size is too large") wanted to depend on zs_malloc's returned ENOSPC to distinguish the case that compressed data is larger than the original data from normal compression cases. The commit, for sure, was correct and worked as expected but the code wouldn't run to there after commit744e1885922a("crypto: scomp - fixreq->dstbuffer overflow") as Chengming's this patch makeszswap_store()goto out immediately after the special compression case happens. So there is no chance to executezs_malloc()now. We need to fix the count right after compressions return ENOSPC.Quoted 1 line
Fixes fc8580edbaa6 ("mm: zsmalloc: return -ENOSPC rather than -EINVAL in zs_malloc while size is too large")I don't see how this is a fix for that commit. Commit
fc8580edbaa6made sure zsmalloc returns a correct errno when the compressed size is too large. The fact that zswap stores were failing before calling into zsmalloc and not reporting the error correctly in debug counters is not that commits fault.
Quoted 5 lines
I think the proper fixes should be
744e1885922aif it introduced the first scenario where-ENOSPCcan be returned from scomp without handling it properly in zswap. If-ENOSPCwas a possible return value before that, then it should becb61dad80fdc("zswap: export compression failure stats"), where the counter was introduced.
IIRC, the counter was introduced before the zsmalloc patch that allowed for returning -ENOSPC, as well as the patch that allowed crypto API to return -ENOSPC.
I think "Fixes: 744e1885922a" would be the closest, as it introduces the -ENOSPC return value, without handling it in zswap_store().
Quoted 60 lines
Quoted 14 lines
Cc Chengming Zhou <[email protected]>Cc Nhat Pham <[email protected]>Cc Sergey Senozhatsky <[email protected]>Signed-off-by Barry Song <[email protected]>--- mm/zswap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)mm/zswap.c +0
-0mm/zswap.c bool zswap_store(struct folio *folio) · line 1633
Quoted 8 lines
mm/zswap.c +4
-1@@ -1627,7 +1627,10 @@ bool zswap_store(struct folio *folio) dlen = acomp_ctx->req->dlen;if (ret) { - zswap_reject_compress_fail++; +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else + zswap_reject_compress_fail++;With this diff, we have four locations in
zswap_store()where we increment zswap_reject_compress_{poor/fail}.How about the following instead?A
mm/zswap.c +2
-10@@ -1059,24 +1059,16 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry)*/ ret =crypto_wait_req (crypto_acomp_compress (acomp_ctx->req), &acomp_ctx->wait); dlen = acomp_ctx->req->dlen; -if (ret) { - zswap_reject_compress_fail++; +if (ret) goto unlock; - } zpool =zswap_find_zpool (entry); gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;if (zpool_malloc_support_movable (zpool)) gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; ret =zpool_malloc (zpool, dlen, gfp, &handle); -if (ret == -ENOSPC ) { - zswap_reject_compress_poor++; - goto unlock; - } -if (ret) { - zswap_reject_alloc_fail++; +if (ret) goto unlock; - } buf =zpool_map_handle (zpool, handle,ZPOOL_MM_WO );memcpy (buf, dst, dlen);
mm/zswap.c static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) · line 1084
Quoted 7 lines
mm/zswap.c +4
-0@@ -1086,6 +1078,10 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) entry->length = dlen; unlock: +if (ret == -ENOSPC ) + zswap_reject_compress_poor++; +else if (ret) + zswap_reject_alloc_fail++;
I'm eyeballing this, but we have 3 debug counters possible right? zswap_reject_compress_poor, zswap_reject_compress_fail, zswap_reject_alloc_fail. I think you remove 3 incrementations (is that a word lol), and add only 2 cases here.
Quoted 10 lines
mm/zswap.c +0
-0mutex_unlock (&acomp_ctx->mutex);return ret ==0 ; }Quoted 7 lines
mm/zswap.c +0
-1goto put_dstmem; } --2.34.1