Skip to contentSkip to conversation

zram: use copy_page for full page copy

PATCH In review Jisheng Zhang 3 participants 5 messages Jun 13, 2024 – Jun 14, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 zram: use copy_page for full page copy

commit 42e99bd975fd ("zram: optimize memory operations with clear_page()/copy_page()") optimize page copy/clean operations, but then commit d72e9a7a93e4 ("zram: do not use copy_page with non-page aligned address") removes the optimization because there's memory corruption at that time, the reason was well explained. But after commit 1f7319c74275 ("zram: partial IO refactoring"), partial IO uses alloc_page() instead of kmalloc to allocate a page, so we can bring back the optimization.

commit 80ba4caf8ba9 ("zram: use copy_page for full page copy") brings back partial optimization, missed one point in zram_write_page(). optimize the full page copying in zram_write_page() with copy_page()

---
 drivers/block/zram/zram_drv.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/block/zram/zram_drv.c +5 -3
index 3acd7006ad2c..4b2b5098062f 100644
@@ -1478,11 +1478,13 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index)
 	dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
 
 	src = zstrm->buffer;
-	if (comp_len == PAGE_SIZE)
+	if (comp_len == PAGE_SIZE) {
 		src = kmap_local_page(page);
-	memcpy(dst, src, comp_len);
-	if (comp_len == PAGE_SIZE)
+		copy_page(dst, src);
 		kunmap_local(src);
+	} else {
+		memcpy(dst, src, comp_len);
+	}
 
 	zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
 	zs_unmap_object(zram->mem_pool, handle);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · Jisheng Zhang

commit 42e99bd975fd ("zram: optimize memory operations with clear_page()/copy_page()") optimize page copy/clean operations, but then commit d72e9a7a93e4 ("zram: do not use copy_page with non-page aligned address") removes the optimization because there's memory corruption at that time, the reason was well explained. But after commit 1f7319c74275 ("zram: partial IO refactoring"), partial IO uses alloc_page() instead of kmalloc to allocate a page, so we can bring back the optimization.

Quoted 3 lines

commit 80ba4caf8ba9 ("zram: use copy_page for full page copy") brings back partial optimization, missed one point in zram_write_page(). optimize the full page copying in zram_write_page() with copy_page()

Is copy_page() really more optimal than memcpy(PAGE_SIZE)?

#2 lore raw
Quoted 10 lines · Sergey Senozhatsky
Quoted 9 lines · Jisheng Zhang

commit 42e99bd975fd ("zram: optimize memory operations with clear_page()/copy_page()") optimize page copy/clean operations, but then commit d72e9a7a93e4 ("zram: do not use copy_page with non-page aligned address") removes the optimization because there's memory corruption at that time, the reason was well explained. But after commit 1f7319c74275 ("zram: partial IO refactoring"), partial IO uses alloc_page() instead of kmalloc to allocate a page, so we can bring back the optimization.

Quoted 5 lines
Quoted 3 lines

commit 80ba4caf8ba9 ("zram: use copy_page for full page copy") brings back partial optimization, missed one point in zram_write_page(). optimize the full page copying in zram_write_page() with copy_page()

Is copy_page() really more optimal than memcpy(PAGE_SIZE)?

I think yes copy_page performs better than memcpy(PAGE_SIZE) commit afb2d666d025 ("zsmalloc: use copy_page for full page copy") also shows the result.

#3 lore raw
In reply to Jisheng Zhang
Quoted 26 lines · Jisheng Zhang

commit 42e99bd975fd ("zram: optimize memory operations with clear_page()/copy_page()") optimize page copy/clean operations, but then commit d72e9a7a93e4 ("zram: do not use copy_page with non-page aligned address") removes the optimization because there's memory corruption at that time, the reason was well explained. But after commit 1f7319c74275 ("zram: partial IO refactoring"), partial IO uses alloc_page() instead of kmalloc to allocate a page, so we can bring back the optimization.

commit 80ba4caf8ba9 ("zram: use copy_page for full page copy") brings back partial optimization, missed one point in zram_write_page(). optimize the full page copying in zram_write_page() with copy_page()

---
 drivers/block/zram/zram_drv.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/block/zram/zram_drv.c +0 -0
index 3acd7006ad2c..4b2b5098062f 100644
@@ -1478,11 +1478,13 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index)
 	dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
 
 	src = zstrm->buffer;

drivers/block/zram/zram_drv.c static int zram_write_page(struct zram *zram, struct page *page, u32 index) · line 1487

Quoted 10 lines
drivers/block/zram/zram_drv.c +5 -3
-	if (comp_len == PAGE_SIZE)
+	if (comp_len == PAGE_SIZE) {
 		src = kmap_local_page(page);
-	memcpy(dst, src, comp_len);
-	if (comp_len == PAGE_SIZE)
+		copy_page(dst, src);
 		kunmap_local(src);
+	} else {
+		memcpy(dst, src, comp_len);
+	}

I know this is pre-existing code, but why do we need to kmap for comp_len == PAGE_SIZE and not for the other cases? Something feels really obsfucated here.

#4 lore raw
Quoted 26 lines · Christoph Hellwig
Quoted 25 lines · Jisheng Zhang

commit 42e99bd975fd ("zram: optimize memory operations with clear_page()/copy_page()") optimize page copy/clean operations, but then commit d72e9a7a93e4 ("zram: do not use copy_page with non-page aligned address") removes the optimization because there's memory corruption at that time, the reason was well explained. But after commit 1f7319c74275 ("zram: partial IO refactoring"), partial IO uses alloc_page() instead of kmalloc to allocate a page, so we can bring back the optimization.

commit 80ba4caf8ba9 ("zram: use copy_page for full page copy") brings back partial optimization, missed one point in zram_write_page(). optimize the full page copying in zram_write_page() with copy_page()

---
 drivers/block/zram/zram_drv.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/block/zram/zram_drv.c +0 -0
index 3acd7006ad2c..4b2b5098062f 100644
@@ -1478,11 +1478,13 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index)
 	dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
 
Quoted 15 lines

drivers/block/zram/zram_drv.c static int zram_write_page(struct zram *zram, struct page *page, u32 index) · line 1487

Quoted 11 lines
drivers/block/zram/zram_drv.c +5 -3
 	src = zstrm->buffer;
-	if (comp_len == PAGE_SIZE)
+	if (comp_len == PAGE_SIZE) {
 		src = kmap_local_page(page);
-	memcpy(dst, src, comp_len);
-	if (comp_len == PAGE_SIZE)
+		copy_page(dst, src);
 		kunmap_local(src);
+	} else {
+		memcpy(dst, src, comp_len);
+	}

I know this is pre-existing code, but why do we need to kmap for comp_len == PAGE_SIZE and not for the other cases? Something feels really obsfucated here.

It is tricky a little.

If we managed to compress page (size < zsmalloc uncompressible watermark) then src is per-CPU buffer with compressed data. Otherwise src is original page (with uncompressed data).