Skip to contentSkip to conversation

mm/zswap: reduce the size of the compression buffer to a single page

PATCH In review Nhat Pham 3 participants 5 messages Aug 20, 2025 – Aug 21, 2025

Subsystems (2)
lore
Share & tools
lore

Nhat Pham

author maintainer
#0 lore raw

PATCH 1/1 mm/zswap: reduce the size of the compression buffer to a single page

Reduce the compression buffer size from 2 * PAGE_SIZE to only one page, as the compression output (in the success case) should not exceed the length of the input.

In the past, Chengming tried to reduce the compression buffer size, but ran into issues with the LZO algorithm (see [2]). Herbert Xu reported that the issue has been fixed (see [3]). Now we should have the guarantee that compressors' output should not exceed one page in the success case, and the algorithm will just report failure otherwise.

With this patch, we save one page per cpu (per compression algorithm).

[1]: https://lore.kernel.org/linux-mm/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/zswap.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
mm/zswap.c +2 -7
index 1f1ac043a2d9..5dd282c5b626 100644
@@ -833,7 +833,7 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 	u8 *buffer = NULL;
 	int ret;
 
-	buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
+	buffer = kmalloc_node(PAGE_SIZE, GFP_KERNEL, cpu_to_node(cpu));
 	if (!buffer) {
 		ret = -ENOMEM;
 		goto fail;
@@ -960,12 +960,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,
 	sg_init_table(&input, 1);
 	sg_set_page(&input, page, PAGE_SIZE, 0);
 
-	/*
-	 * We need PAGE_SIZE * 2 here since there maybe over-compression case,
-	 * and hardware-accelerators may won't check the dst buffer size, so
-	 * giving the dst buffer with enough length to avoid buffer overflow.
-	 */
-	sg_init_one(&output, dst, PAGE_SIZE * 2);
+	sg_init_one(&output, dst, PAGE_SIZE);
 	acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen);
 
 	/*

base-commit: c0e3b3f33ba7b767368de4afabaf7c1ddfdc3872

Signature · 1 line
2.47.3
author maintainer
#1 lore raw
Quoted 11 lines · Nhat Pham

Reduce the compression buffer size from 2 * PAGE_SIZE to only one page, as the compression output (in the success case) should not exceed the length of the input.

In the past, Chengming tried to reduce the compression buffer size, but ran into issues with the LZO algorithm (see [2]). Herbert Xu reported that the issue has been fixed (see [3]). Now we should have the guarantee that compressors' output should not exceed one page in the success case, and the algorithm will just report failure otherwise.

Quoted 8 lines

With this patch, we save one page per cpu (per compression algorithm).

[1]: https://lore.kernel.org/linux-mm/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/linux-mm/[email protected]/

The syntax changes very slightly, but this is functionally the same patch as Chengming's. So I'm keeping all of his credit - please let me know if I miss any proper accreditations. :)

Acked-by on v1 1/1
#2 lore raw
In reply to Nhat Pham
Quoted 10 lines · On Wed, 20 Aug 2025 11:15:47 -0700 Nhat Pham <[email protected]> wrote:

Reduce the compression buffer size from 2 * PAGE_SIZE to only one page, as the compression output (in the success case) should not exceed the length of the input.

In the past, Chengming tried to reduce the compression buffer size, but ran into issues with the LZO algorithm (see [2]). Herbert Xu reported that the issue has been fixed (see [3]). Now we should have the guarantee that compressors' output should not exceed one page in the success case, and the algorithm will just report failure otherwise.

Quoted 3 lines

With this patch, we save one page per cpu (per compression algorithm).

[1]: https://lore.kernel.org/linux-mm/[email protected]/

This is not mentioned anywhere. Probably you added this as a reference for the past work from Chengming?

Quoted 6 lines

[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/linux-mm/[email protected]/

[...]

Thanks,
SJ

author maintainer
#3 lore raw
Quoted 8 lines · SeongJae Park
Quoted 5 lines · On Wed, 20 Aug 2025 11:15:47 -0700 Nhat Pham <[email protected]> wrote:

Reduce the compression buffer size from 2 * PAGE_SIZE to only one page, as the compression output (in the success case) should not exceed the length of the input.

In the past, Chengming tried to reduce the compression buffer size, but

Yeah, this should include the reference below. My bad. So it should be:

In the past, Chengming tried to reduce the compression buffer size (see [1]), but

Andrew, would it be OK if you fix it for me when you put this patch in your tree :) Or I can also send a new version if that's better. Thank you!

Quoted 11 lines
Quoted 8 lines

ran into issues with the LZO algorithm (see [2]). Herbert Xu reported that the issue has been fixed (see [3]). Now we should have the guarantee that compressors' output should not exceed one page in the success case, and the algorithm will just report failure otherwise.

With this patch, we save one page per cpu (per compression algorithm).

[1]: https://lore.kernel.org/linux-mm/[email protected]/

This is not mentioned anywhere. Probably you added this as a reference for the past work from Chengming?

Thanks for taking a look, SJ!

Quoted 9 lines
Quoted 7 lines

[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/linux-mm/[email protected]/

reviewer Reviewed-by
#4 lore raw
Quoted 10 lines · Nhat Pham
Quoted 8 lines · SeongJae Park
Quoted 5 lines · On Wed, 20 Aug 2025 11:15:47 -0700 Nhat Pham <[email protected]> wrote:

Reduce the compression buffer size from 2 * PAGE_SIZE to only one page, as the compression output (in the success case) should not exceed the length of the input.

In the past, Chengming tried to reduce the compression buffer size, but

Quoted 13 lines

Yeah, this should include the reference below. My bad. So it should be:

In the past, Chengming tried to reduce the compression buffer size (see [1]), but

Andrew, would it be OK if you fix it for me when you put this patch in your tree :) Or I can also send a new version if that's better. Thank you!

Quoted 4 lines
Quoted 4 lines

ran into issues with the LZO algorithm (see [2]). Herbert Xu reported that the issue has been fixed (see [3]). Now we should have the guarantee that compressors' output should not exceed one page in the success case, and the algorithm will just report failure otherwise.

Great!

Thank you!

Quoted 19 lines
Quoted 7 lines
Quoted 4 lines

With this patch, we save one page per cpu (per compression algorithm).

[1]: https://lore.kernel.org/linux-mm/[email protected]/

This is not mentioned anywhere. Probably you added this as a reference for the past work from Chengming?

Thanks for taking a look, SJ!

Quoted 9 lines
Quoted 7 lines

[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/linux-mm/[email protected]/