Skip to contentSkip to conversation

page_io: zswap: do not crash the kernel on decompression failure

PATCH Superseded Nhat Pham 3 participants 8 messages Mar 3, 2025 – Mar 4, 2025

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Nhat Pham

author maintainer
#0 lore raw

PATCH v3 1/1 page_io: zswap: do not crash the kernel on decompression failure

Currently, we crash the kernel when a decompression failure occurs in zswap (either because of memory corruption, or a bug in the compression algorithm). This is overkill. We should only SIGBUS the unfortunate process asking for the zswap entry on zswap load, and skip the corrupted entry in zswap writeback.

See [1] for a recent upstream discussion about this.

The zswap writeback case is relatively straightforward to fix. For the zswap_load() case, we reorganize the swap read paths, having swap_read_folio_zeromap() and zswap_load() return specific error codes:

* 0 indicates the backend owns the swapped out content, which was successfully loaded into the page.
* -ENOENT indicates the backend does not own the swapped out content.
* -EINVAL and -EIO indicate the backend own the swapped out content, but the content was not successfully loaded into the page for some reasons. The folio will not be marked up-to-date, which will eventually cause the process requesting the page to SIGBUS (see the handling of not-up-to-date folio in do_swap_page() in mm/memory.c).

zswap decompression failures on the zswap load path are treated as an -EIO error, as described above, and will no longer crash the kernel.

As a side effect, we require one extra zswap tree traversal in the load and writeback paths. Quick benchmarking on a kernel build test shows no performance difference:

With the new scheme:
real: mean: 125.1s, stdev: 0.12s
user: mean: 3265.23s, stdev: 9.62s
sys: mean: 2156.41s, stdev: 13.98s

The old scheme:
real: mean: 125.78s, stdev: 0.45s
user: mean: 3287.18s, stdev: 5.95s
sys: mean: 2177.08s, stdev: 26.52s

[1]: https://lore.kernel.org/all/[email protected]/

---
 include/linux/zswap.h |   4 +-
 mm/page_io.c          |  35 ++++++++----
 mm/zswap.c            | 130 ++++++++++++++++++++++++++++++------------
 3 files changed, 120 insertions(+), 49 deletions(-)
include/linux/zswap.h +2 -2
index d961ead91bf1..9468cb3e0878 100644
@@ -26,7 +26,7 @@ struct zswap_lruvec_state {
 
 unsigned long zswap_total_pages(void);
 bool zswap_store(struct folio *folio);
-bool zswap_load(struct folio *folio);
+int zswap_load(struct folio *folio);
 void zswap_invalidate(swp_entry_t swp);
 int zswap_swapon(int type, unsigned long nr_pages);
 void zswap_swapoff(int type);
@@ -46,7 +46,7 @@ static inline bool zswap_store(struct folio *folio)
 
 static inline bool zswap_load(struct folio *folio)
 {
-	return false;
+	return -ENOENT;
 }
 
 static inline void zswap_invalidate(swp_entry_t swp) {}
mm/page_io.c +25 -10
index 9b983de351f9..8a44faec3f92 100644
@@ -511,7 +511,21 @@ static void sio_read_complete(struct kiocb *iocb, long ret)
 	mempool_free(sio, sio_pool);
 }
 
-static bool swap_read_folio_zeromap(struct folio *folio)
+/*
+ * Return: one of the following error codes:
+ *
+ *  0: the folio is zero-filled (and was populated as such and marked
+ *  up-to-date and unlocked).
+ *
+ *  -ENOENT: the folio was not zero-filled.
+ *
+ *  -EINVAL: some of the subpages in the folio are zero-filled, but not all of
+ *  them. This is an error because we don't currently support a large folio
+ *  that is partially in the zeromap. The folio is unlocked, but NOT marked
+ *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
+ *  sigbus).
+ */
+static int swap_read_folio_zeromap(struct folio *folio)
 {
 	int nr_pages = folio_nr_pages(folio);
 	struct obj_cgroup *objcg;
@@ -523,11 +537,13 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 	 * that an IO error is emitted (e.g. do_swap_page() will sigbus).
 	 */
 	if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages,
-			&is_zeromap) != nr_pages))
-		return true;
+			&is_zeromap) != nr_pages)) {
+		folio_unlock(folio);
+		return -EINVAL;
+	}
 
 	if (!is_zeromap)
-		return false;
+		return -ENOENT;
 
 	objcg = get_obj_cgroup_from_folio(folio);
 	count_vm_events(SWPIN_ZERO, nr_pages);
@@ -538,7 +554,8 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 
 	folio_zero_range(folio, 0, folio_size(folio));
 	folio_mark_uptodate(folio);
-	return true;
+	folio_unlock(folio);
+	return 0;
 }
 
 static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
@@ -635,13 +652,11 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 	}
 	delayacct_swapin_start();
 
-	if (swap_read_folio_zeromap(folio)) {
-		folio_unlock(folio);
+	if (swap_read_folio_zeromap(folio) != -ENOENT)
 		goto finish;
-	} else if (zswap_load(folio)) {
-		folio_unlock(folio);
+
+	if (zswap_load(folio) != -ENOENT)
 		goto finish;
-	}
 
 	/* We have to read from slower devices. Increase zswap protection. */
 	zswap_folio_swapin(folio);
mm/zswap.c +93 -37

217 lines not on this page · raw message

base-commit: 598d34afeca6bb10554846cf157a3ded8729516c

Signature · 1 line
2.43.5
#1 lore raw
Quoted 64 lines · Nhat Pham

Currently, we crash the kernel when a decompression failure occurs in zswap (either because of memory corruption, or a bug in the compression algorithm). This is overkill. We should only SIGBUS the unfortunate process asking for the zswap entry on zswap load, and skip the corrupted entry in zswap writeback.

See [1] for a recent upstream discussion about this.

The zswap writeback case is relatively straightforward to fix. For the zswap_load() case, we reorganize the swap read paths, having swap_read_folio_zeromap() and zswap_load() return specific error codes:

* 0 indicates the backend owns the swapped out content, which was successfully loaded into the page.
* -ENOENT indicates the backend does not own the swapped out content.
* -EINVAL and -EIO indicate the backend own the swapped out content, but the content was not successfully loaded into the page for some reasons. The folio will not be marked up-to-date, which will eventually cause the process requesting the page to SIGBUS (see the handling of not-up-to-date folio in do_swap_page() in mm/memory.c).

zswap decompression failures on the zswap load path are treated as an -EIO error, as described above, and will no longer crash the kernel.

As a side effect, we require one extra zswap tree traversal in the load and writeback paths. Quick benchmarking on a kernel build test shows no performance difference:

With the new scheme:
real: mean: 125.1s, stdev: 0.12s
user: mean: 3265.23s, stdev: 9.62s
sys: mean: 2156.41s, stdev: 13.98s

The old scheme:
real: mean: 125.78s, stdev: 0.45s
user: mean: 3287.18s, stdev: 5.95s
sys: mean: 2177.08s, stdev: 26.52s

[1]: https://lore.kernel.org/all/[email protected]/

---
 include/linux/zswap.h |   4 +-
 mm/page_io.c          |  35 ++++++++----
 mm/zswap.c            | 130 ++++++++++++++++++++++++++++++------------
 3 files changed, 120 insertions(+), 49 deletions(-)
include/linux/zswap.h +1 -1
index d961ead91bf1..9468cb3e0878 100644
@@ -26,7 +26,7 @@ struct zswap_lruvec_state {
 
 unsigned long zswap_total_pages(void);
 bool zswap_store(struct folio *folio);
-bool zswap_load(struct folio *folio);
+int zswap_load(struct folio *folio);
 void zswap_invalidate(swp_entry_t swp);
 int zswap_swapon(int type, unsigned long nr_pages);
 void zswap_swapoff(int type);

include/linux/zswap.h static inline bool zswap_store(struct folio *folio) · line 47

Quoted 2 lines
include/linux/zswap.h +0 -0
@@ -46,7 +46,7 @@ static inline bool zswap_store(struct folio *folio)
 
 static inline bool zswap_load(struct folio *folio)

int?

Quoted 11 lines
include/linux/zswap.h +1 -1
 {
-	return false;
+	return -ENOENT;
 }
 
 static inline void zswap_invalidate(swp_entry_t swp) {}
mm/page_io.c +0 -0
index 9b983de351f9..8a44faec3f92 100644

mm/page_io.c static void sio_read_complete(struct kiocb *iocb, long ret) · line 515

Quoted 6 lines
mm/page_io.c +2 -1
@@ -511,7 +511,21 @@ static void sio_read_complete(struct kiocb *iocb, long ret)
 	mempool_free(sio, sio_pool);
 }
 
-static bool swap_read_folio_zeromap(struct folio *folio)
+/*
+ * Return: one of the following error codes:

nit:
"Returns 0 on success, or one of the following errors on failure:"

Also might as well make this a full kerneldoc?

Quoted 17 lines
mm/page_io.c +13 -0
+ *
+ *  0: the folio is zero-filled (and was populated as such and marked
+ *  up-to-date and unlocked).
+ *
+ *  -ENOENT: the folio was not zero-filled.
+ *
+ *  -EINVAL: some of the subpages in the folio are zero-filled, but not all of
+ *  them. This is an error because we don't currently support a large folio
+ *  that is partially in the zeromap. The folio is unlocked, but NOT marked
+ *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
+ *  sigbus).
+ */
+static int swap_read_folio_zeromap(struct folio *folio)
 {
 	int nr_pages = folio_nr_pages(folio);
 	struct obj_cgroup *objcg;

mm/page_io.c static bool swap_read_folio_zeromap(struct folio *folio) · line 538

Quoted 2 lines
mm/page_io.c +0 -0
@@ -523,11 +537,13 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 	 * that an IO error is emitted (e.g. do_swap_page() will sigbus).
 	 */

This comment says "Return true", so it needs to be updated. We probably don't need to explain the return value anymore since it's documented above.

Quoted 25 lines
mm/page_io.c +7 -4
 	if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages,
-			&is_zeromap) != nr_pages))
-		return true;
+			&is_zeromap) != nr_pages)) {
+		folio_unlock(folio);
+		return -EINVAL;
+	}
 
 	if (!is_zeromap)
-		return false;
+		return -ENOENT;
 
 	objcg = get_obj_cgroup_from_folio(folio);
 	count_vm_events(SWPIN_ZERO, nr_pages);
@@ -538,7 +554,8 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 
 	folio_zero_range(folio, 0, folio_size(folio));
 	folio_mark_uptodate(folio);
-	return true;
+	folio_unlock(folio);
+	return 0;
 }
 
 static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)

mm/page_io.c void swap_read_folio(struct folio *folio, struct swap_iocb **plug) · line 656

Quoted 7 lines
mm/page_io.c +1 -2
@@ -635,13 +652,11 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 	}
 	delayacct_swapin_start();
 
-	if (swap_read_folio_zeromap(folio)) {
-		folio_unlock(folio);
+	if (swap_read_folio_zeromap(folio) != -ENOENT)
 		goto finish;

I would split the zeromap change into a separate patch, but it's probably fine either way.

Quoted 43 lines
mm/page_io.c +2 -3
-	} else if (zswap_load(folio)) {
-		folio_unlock(folio);
+
+	if (zswap_load(folio) != -ENOENT)
 		goto finish;
-	}
 
 	/* We have to read from slower devices. Increase zswap protection. */
 	zswap_folio_swapin(folio);
mm/zswap.c +5 -3
index 6dbf31bd2218..b67481defc6c 100644
@@ -62,6 +62,8 @@ static u64 zswap_reject_reclaim_fail;
 static u64 zswap_reject_compress_fail;
 /* Compressed page was too big for the allocator to (optimally) store */
 static u64 zswap_reject_compress_poor;
+/* Load or writeback failed due to decompression failure */
+static u64 zswap_decompress_fail;
 /* Store failed because underlying allocator could not get memory */
 static u64 zswap_reject_alloc_fail;
 /* Store failed because the entry metadata could not be allocated (rare) */
@@ -996,11 +998,12 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,
 	return comp_ret == 0 && alloc_ret == 0;
 }
 
-static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
+static bool zswap_decompress(struct zswap_entry *entry, struct folio *folio)
 {
 	struct zpool *zpool = entry->pool->zpool;
 	struct scatterlist input, output;
 	struct crypto_acomp_ctx *acomp_ctx;
+	int decomp_ret, dlen;
 	u8 *src;
 
 	acomp_ctx = acomp_ctx_get_cpu_lock(entry->pool);
@@ -1025,12 +1028,31 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
 	sg_init_table(&output, 1);
 	sg_set_folio(&output, folio, PAGE_SIZE, 0);
 	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);
+	decomp_ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) · line 1041

Quoted 10 lines
mm/zswap.c +6 -0
+	dlen = acomp_ctx->req->dlen;
 
 	if (src != acomp_ctx->buffer)
 		zpool_unmap_handle(zpool, entry->handle);
 	acomp_ctx_put_unlock(acomp_ctx);
+
+	if (decomp_ret || dlen != PAGE_SIZE) {
+		zswap_decompress_fail++;
+		pr_alert_ratelimited(
+			"decompression failed with returned value %d on zswap entry with "

nit: Decompression*

I am also wondering how this looks like in dmesg? Is the line too long to be read? Should we add some line breaks (e.g. like warn_sysctl_write()), we could probably also put this in a helper to keep this function visually easy to follow.

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) · line 1051

Quoted 10 lines
mm/zswap.c +10 -0
+			"swap entry value %08lx, swap type %d, and swap offset %lu. "
+			"compression algorithm is %s. compressed size is %u bytes, and "
+			"decompressed size is %u bytes.\n",
+			decomp_ret,
+			entry->swpentry.val,
+			swp_type(entry->swpentry),
+			swp_offset(entry->swpentry),
+			entry->pool->tfm_name,
+			entry->length,
+			acomp_ctx->req->dlen);

We should probably be using 'dlen' here since we're outside the lock.

Quoted 7 lines
mm/zswap.c +4 -0
+
+		return false;
+	}
+	return true;
 }
 
 /*********************************

[..]

mm/zswap.c bool zswap_store(struct folio *folio) · line 1658

Quoted 10 lines
mm/zswap.c +5 -1
@@ -1620,7 +1651,29 @@ bool zswap_store(struct folio *folio)
 	return ret;
 }
 
-bool zswap_load(struct folio *folio)
+/**
+ * zswap_load() - load a page from zswap
+ * @folio: folio to load
+ *
+ * Return: one of the following error codes:

nit: Returns 0 on success, or ..

Quoted 21 lines
mm/zswap.c +18 -0
+ *
+ *  0: if the swapped out content was in zswap and was successfully loaded.
+ *  The folio is unlocked and marked up-to-date.
+ *
+ *  -EIO: if the swapped out content was in zswap, but could not be loaded
+ *  into the page (for e.g, because there was a memory corruption, or a
+ *  decompression bug). The folio is unlocked, but NOT marked up-to-date,
+ *  so that an IO error is emitted (e.g. do_swap_page() will SIGBUS).
+ *
+ *  -EINVAL: if the swapped out content was in zswap, but the page belongs
+ *  to a large folio, which is not supported by zswap. The folio is unlocked,
+ *  but NOT marked up-to-date, so that an IO error is emitted (e.g.
+ *  do_swap_page() will SIGBUS).
+ *
+ *  -ENOENT: if the swapped out content was not in zswap. The folio remains
+ *  locked on return.
+ */
+int zswap_load(struct folio *folio)
 {
 	swp_entry_t swp = folio->swap;
 	pgoff_t offset = swp_offset(swp);

[..]

maintainer
#2 lore raw
Quoted 5 lines · Yosry Ahmed
Quoted 4 lines · Nhat Pham
mm/page_io.c +0 -0
@@ -635,13 +652,11 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 	}
 	delayacct_swapin_start();
 
Quoted 7 lines

mm/page_io.c void swap_read_folio(struct folio *folio, struct swap_iocb **plug) · line 656

Quoted 4 lines
mm/page_io.c +1 -2
-	if (swap_read_folio_zeromap(folio)) {
-		folio_unlock(folio);
+	if (swap_read_folio_zeromap(folio) != -ENOENT)
 		goto finish;

I would split the zeromap change into a separate patch, but it's probably fine either way.

+1

Quoted 20 lines
Quoted 7 lines
mm/zswap.c +1 -2
@@ -1025,12 +1028,31 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
 	sg_init_table(&output, 1);
 	sg_set_folio(&output, folio, PAGE_SIZE, 0);
 	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);
+	decomp_ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) · line 1041

Quoted 10 lines
mm/zswap.c +6 -0
+	dlen = acomp_ctx->req->dlen;
 
 	if (src != acomp_ctx->buffer)
 		zpool_unmap_handle(zpool, entry->handle);
 	acomp_ctx_put_unlock(acomp_ctx);
+
+	if (decomp_ret || dlen != PAGE_SIZE) {
+		zswap_decompress_fail++;
+		pr_alert_ratelimited(
+			"decompression failed with returned value %d on zswap entry with "

nit: Decompression*

Quoted 4 lines

I am also wondering how this looks like in dmesg? Is the line too long to be read? Should we add some line breaks (e.g. like warn_sysctl_write()), we could probably also put this in a helper to keep this function visually easy to follow.

If it were more interwoven, I would agree. But it's only followed by the return true, false. Moving it out of line would need another name in the zswap namespace and also take an awkward amount of parameters, so IMO more taxing on the reader.

But maybe do if (!decomp_ret && dlen == PAGE_SIZE) return true, and then save an indentation for the error part?

Quoted 3 lines

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) · line 1044

Quoted 3 lines
mm/zswap.c +3 -0
+			"swap entry value %08lx, swap type %d, and swap offset %lu. "
+			"compression algorithm is %s. compressed size is %u bytes, and "
+			"decompressed size is %u bytes.\n",

Any objections to shortening it and avoiding the line length issue? Even with \n's, this is still a lot of characters to dump 10x/5s. And it's not like the debug info is super useful to anyone but kernel developers, who in turn wouldn't have an issue interpreting this:

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

		     swptype, swpoffset, name, clen, dlen);
Quoted 7 lines
Quoted 7 lines
mm/zswap.c +7 -0
+			decomp_ret,
+			entry->swpentry.val,
+			swp_type(entry->swpentry),
+			swp_offset(entry->swpentry),
+			entry->pool->tfm_name,
+			entry->length,
+			acomp_ctx->req->dlen);
#3 lore raw
Quoted 41 lines · Johannes Weiner
Quoted 5 lines · Yosry Ahmed
Quoted 4 lines · Nhat Pham
mm/page_io.c +0 -0
@@ -635,13 +652,11 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 	}
 	delayacct_swapin_start();
 
Quoted 7 lines

mm/page_io.c void swap_read_folio(struct folio *folio, struct swap_iocb **plug) · line 656

Quoted 4 lines
mm/page_io.c +1 -2
-	if (swap_read_folio_zeromap(folio)) {
-		folio_unlock(folio);
+	if (swap_read_folio_zeromap(folio) != -ENOENT)
 		goto finish;

I would split the zeromap change into a separate patch, but it's probably fine either way.

+1

Quoted 20 lines
Quoted 7 lines
mm/zswap.c +1 -2
@@ -1025,12 +1028,31 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
 	sg_init_table(&output, 1);
 	sg_set_folio(&output, folio, PAGE_SIZE, 0);
 	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);
+	decomp_ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) · line 1041

Quoted 10 lines
mm/zswap.c +6 -0
+	dlen = acomp_ctx->req->dlen;
 
 	if (src != acomp_ctx->buffer)
 		zpool_unmap_handle(zpool, entry->handle);
 	acomp_ctx_put_unlock(acomp_ctx);
+
+	if (decomp_ret || dlen != PAGE_SIZE) {
+		zswap_decompress_fail++;
+		pr_alert_ratelimited(
+			"decompression failed with returned value %d on zswap entry with "

nit: Decompression*

Quoted 4 lines

I am also wondering how this looks like in dmesg? Is the line too long to be read? Should we add some line breaks (e.g. like warn_sysctl_write()), we could probably also put this in a helper to keep this function visually easy to follow.

Quoted 4 lines

If it were more interwoven, I would agree. But it's only followed by the return true, false. Moving it out of line would need another name in the zswap namespace and also take an awkward amount of parameters, so IMO more taxing on the reader.

My rationale was that no one reading zswap_decompress() will feel the need to read a function called zswap_warn_decompress_failure() in the error path, so it will save people parsing this huge thing.

FWIW it would only need to take 3 parameters: decomp_ret, dlen, entry.

Quoted 13 lines

But maybe do if (!decomp_ret && dlen == PAGE_SIZE) return true, and then save an indentation for the error part?

Quoted 3 lines

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) · line 1044

Quoted 3 lines
mm/zswap.c +3 -0
+			"swap entry value %08lx, swap type %d, and swap offset %lu. "
+			"compression algorithm is %s. compressed size is %u bytes, and "
+			"decompressed size is %u bytes.\n",

Any objections to shortening it and avoiding the line length issue? Even with \n's, this is still a lot of characters to dump 10x/5s. And it's not like the debug info is super useful to anyone but kernel developers, who in turn wouldn't have an issue interpreting this:

Quoted 2 lines

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

		     swptype, swpoffset, name, clen, dlen);

Yeah this looks much more concise. It's a bit harder to parser the dmesg as you have to cross check the code, but hopefully this is something that people rarely have to do.

I don't feel strongly about adding a helper in this case, unless we want to add local variables (like Johannes did above), in which case a helper would be a good way to hide them.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
mm/zswap.c +7 -0
+			decomp_ret,
+			entry->swpentry.val,
+			swp_type(entry->swpentry),
+			swp_offset(entry->swpentry),
+			entry->pool->tfm_name,
+			entry->length,
+			acomp_ctx->req->dlen);
maintainer
#4 lore raw
Quoted 11 lines · Yosry Ahmed
Quoted 2 lines · Johannes Weiner

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

		     swptype, swpoffset, name, clen, dlen);

Yeah this looks much more concise. It's a bit harder to parser the dmesg as you have to cross check the code, but hopefully this is something that people rarely have to do.

I don't feel strongly about adding a helper in this case, unless we want to add local variables (like Johannes did above), in which case a helper would be a good way to hide them.

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

		swp_type(entry->swpentry), swp_offset(entry->swpentry),
		entry->pool->tfm_name, entry->length, acomp_ctx->req->dlen);

Seriously, this does not warrant another function.

It's also valuable to keep warnings inside the problem context instead of socking them away somewhere. It makes it clear that decompression failure is a serious situation. We also expect this to trigger almost never and it won't be tested routinely, so the best chance to fight bitrot is to keep all those derefs close by. Imagine if this triggers and the data is misleading or it crashes the system because some rules around entry, acomp_ctx, the pool or whatever changed. Or if the work involved in decompression changed and this is incomplete/unhelpful.

author maintainer
#5 lore raw
Quoted 20 lines · Johannes Weiner
Quoted 11 lines · Yosry Ahmed
Quoted 2 lines · Johannes Weiner

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

                 swptype, swpoffset, name, clen, dlen);

Yeah this looks much more concise. It's a bit harder to parser the dmesg as you have to cross check the code, but hopefully this is something that people rarely have to do.

I don't feel strongly about adding a helper in this case, unless we want to add local variables (like Johannes did above), in which case a helper would be a good way to hide them.

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

                swp_type(entry->swpentry), swp_offset(entry->swpentry),
                entry->pool->tfm_name, entry->length, acomp_ctx->req->dlen);

Seriously, this does not warrant another function.

Quoted 8 lines

It's also valuable to keep warnings inside the problem context instead of socking them away somewhere. It makes it clear that decompression failure is a serious situation. We also expect this to trigger almost never and it won't be tested routinely, so the best chance to fight bitrot is to keep all those derefs close by. Imagine if this triggers and the data is misleading or it crashes the system because some rules around entry, acomp_ctx, the pool or whatever changed. Or if the work involved in decompression changed and this is incomplete/unhelpful.

I was actually thinking along the line of Yosry's, but you make some good points.

Anyway, let's just keep the printing in the OG function. Let's not overthink one print function call. :)

(sorry for the duplicate email, Johannes. I accidentally sent this email to you and not cc-ing the rest lol. Resending it)

#6 lore raw
In reply to Johannes Weiner
Quoted 13 lines · Johannes Weiner
Quoted 11 lines · Yosry Ahmed
Quoted 2 lines · Johannes Weiner

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

		     swptype, swpoffset, name, clen, dlen);

Yeah this looks much more concise. It's a bit harder to parser the dmesg as you have to cross check the code, but hopefully this is something that people rarely have to do.

I don't feel strongly about adding a helper in this case, unless we want to add local variables (like Johannes did above), in which case a helper would be a good way to hide them.

Quoted 5 lines

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

		swp_type(entry->swpentry), swp_offset(entry->swpentry),
		entry->pool->tfm_name, entry->length, acomp_ctx->req->dlen);

Seriously, this does not warrant another function.

I don't disagree, especially after this was shortened. I wanted a helper when this was a huge function call making zswap_decompress() more annoying to parse.

I thought you wanted local variables to make the meaning clear after shortening the message, so my stance was to add a helper iff we do that.

Otherwise what you have above LGTM.

Quoted 9 lines

It's also valuable to keep warnings inside the problem context instead of socking them away somewhere. It makes it clear that decompression failure is a serious situation. We also expect this to trigger almost never and it won't be tested routinely, so the best chance to fight bitrot is to keep all those derefs close by. Imagine if this triggers and the data is misleading or it crashes the system because some rules around entry, acomp_ctx, the pool or whatever changed. Or if the work involved in decompression changed and this is incomplete/unhelpful.

Good point about bitrotting if rules change.

author maintainer
#7 lore raw
In reply to Yosry Ahmed
Quoted 43 lines · Yosry Ahmed
Quoted 41 lines · Johannes Weiner
Quoted 5 lines · Yosry Ahmed
Quoted 4 lines · Nhat Pham
mm/page_io.c +0 -0
@@ -635,13 +652,11 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
  }
  delayacct_swapin_start();
 
Quoted 7 lines

mm/page_io.c void swap_read_folio(struct folio *folio, struct swap_iocb **plug) · line 656

Quoted 4 lines
mm/page_io.c +1 -2
- if (swap_read_folio_zeromap(folio)) {
-         folio_unlock(folio);
+ if (swap_read_folio_zeromap(folio) != -ENOENT)
          goto finish;

I would split the zeromap change into a separate patch, but it's probably fine either way.

+1

Quoted 24 lines
Quoted 7 lines
mm/zswap.c +1 -2
@@ -1025,12 +1028,31 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
  sg_init_table(&output, 1);
  sg_set_folio(&output, folio, PAGE_SIZE, 0);
  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);
+ decomp_ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) · line 1041

Quoted 10 lines
mm/zswap.c +6 -0
+ dlen = acomp_ctx->req->dlen;
 
  if (src != acomp_ctx->buffer)
          zpool_unmap_handle(zpool, entry->handle);
  acomp_ctx_put_unlock(acomp_ctx);
+
+ if (decomp_ret || dlen != PAGE_SIZE) {
+         zswap_decompress_fail++;
+         pr_alert_ratelimited(
+                 "decompression failed with returned value %d on zswap entry with "

nit: Decompression*

I am also wondering how this looks like in dmesg? Is the line too long to be read? Should we add some line breaks (e.g. like warn_sysctl_write()), we could probably also put this in a helper to keep this function visually easy to follow.

Quoted 8 lines
Quoted 4 lines

If it were more interwoven, I would agree. But it's only followed by the return true, false. Moving it out of line would need another name in the zswap namespace and also take an awkward amount of parameters, so IMO more taxing on the reader.

My rationale was that no one reading zswap_decompress() will feel the need to read a function called zswap_warn_decompress_failure() in the error path, so it will save people parsing this huge thing.

I think Johannes' suggestion accomplishes a similar effect (see below).

Quoted 6 lines

FWIW it would only need to take 3 parameters: decomp_ret, dlen, entry.

Quoted 3 lines

But maybe do if (!decomp_ret && dlen == PAGE_SIZE) return true, and then save an indentation for the error part?

I like this. It also moves the (much rarer) failure case to its own corner, which we can skip (most of the time). :)

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

mm/zswap.c static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) · line 1044

Quoted 4 lines
mm/zswap.c +3 -0
+                 "swap entry value %08lx, swap type %d, and swap offset %lu. "
+                 "compression algorithm is %s. compressed size is %u bytes, and "
+                 "decompressed size is %u bytes.\n",

Any objections to shortening it and avoiding the line length issue? Even with \n's, this is still a lot of characters to dump 10x/5s. And it's not like the debug info is super useful to anyone but kernel developers, who in turn wouldn't have an issue interpreting this:

No objection from my end.

Quoted 11 lines
Quoted 3 lines

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

                   swptype, swpoffset, name, clen, dlen);

Yeah this looks much more concise. It's a bit harder to parser the dmesg as you have to cross check the code, but hopefully this is something that people rarely have to do.

I don't feel strongly about adding a helper in this case, unless we want to add local variables (like Johannes did above), in which case a helper would be a good way to hide them.

That said, I'm not so sure about adding local variables here. We would be cluttering the code for a bunch of single-use variables, that are not even the "common" case. I mean, this seems fine to me?

pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",

                                 swp_type(entry->swpentry),
                                 swp_offset(entry->swpentry),
                                 entry->pool->tfm_name, entry->length, dlen);

(with proper indentation, but you get the idea).