Skip to contentSkip to conversation

zsmalloc: zero-initialize zspage memory to prevent KMSAN uninit reads

PATCH In review Kartik Nair 5 participants 6 messages May 11 – May 13

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

PATCH 1/1 zsmalloc: zero-initialize zspage memory to prevent KMSAN uninit reads

Pages allocated via alloc_zpdesc() use alloc_pages_node() without __GFP_ZERO, leaving physical memory uninitialized. When a compressed object spans two physical pages in a zspage, zs_obj_read_sg_begin() sets up a scatterlist pointing directly at the raw second page. If the second page was freshly allocated and never written beyond the object boundary, KMSAN detects reads of uninitialized memory downstream in the decompressor (e.g. sw842_decompress reading the CRC trailer).

Fix this by passing __GFP_ZERO to alloc_zpdesc() in alloc_zspage() so all pages backing a zspage are zero-initialized at allocation time.

---
 mm/zsmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/zsmalloc.c +1 -1
index 63128ddb7..5bbd417d3 100644
@@ -951,7 +951,7 @@ static struct zspage *alloc_zspage(struct zs_pool *pool,
 	for (i = 0; i < class->pages_per_zspage; i++) {
 		struct zpdesc *zpdesc;
 
-		zpdesc = alloc_zpdesc(gfp, nid);
+		zpdesc = alloc_zpdesc(gfp | __GFP_ZERO, nid);
 		if (!zpdesc) {
 			while (--i >= 0) {
 				zpdesc_dec_zone_page_state(zpdescs[i]);
Signature · 1 line
2.39.5 (Apple Git-154)
maintainer
#1 lore raw
Quoted 8 lines · On Tue, 12 May 2026 03:06:58 +0530 Kartik Nair <[email protected]>…

Pages allocated via alloc_zpdesc() use alloc_pages_node() without __GFP_ZERO, leaving physical memory uninitialized. When a compressed object spans two physical pages in a zspage, zs_obj_read_sg_begin() sets up a scatterlist pointing directly at the raw second page. If the second page was freshly allocated and never written beyond the object boundary, KMSAN detects reads of uninitialized memory downstream in the decompressor (e.g. sw842_decompress reading the CRC trailer).

Quoted 6 lines

Fix this by passing __GFP_ZERO to alloc_zpdesc() in alloc_zspage() so all pages backing a zspage are zero-initialized at allocation time.

Thanks.

mm/zsmalloc.c static struct zspage *alloc_zspage(struct zs_pool *pool, · line 957

Quoted 11 lines
mm/zsmalloc.c +1 -1
@@ -951,7 +951,7 @@ static struct zspage *alloc_zspage(struct zs_pool *pool,
 	for (i = 0; i < class->pages_per_zspage; i++) {
 		struct zpdesc *zpdesc;
 
-		zpdesc = alloc_zpdesc(gfp, nid);
+		zpdesc = alloc_zpdesc(gfp | __GFP_ZERO, nid);
 		if (!zpdesc) {
 			while (--i >= 0) {
 				zpdesc_dec_zone_page_state(zpdescs[i]);

Decompressing uninitialized memory sounds rather bad, so I'll add a cc:stable to this.

I think the Fixes: target is 56e5a103a721 ("zsmalloc: prefer the the original page's node for compressed data"). Can people please check this when reviewing?

#2 lore raw

Adding Yosry and Herbert,

Quoted 7 lines · Andrew Morton
Quoted 7 lines

Pages allocated via alloc_zpdesc() use alloc_pages_node() without __GFP_ZERO, leaving physical memory uninitialized. When a compressed object spans two physical pages in a zspage, zs_obj_read_sg_begin() sets up a scatterlist pointing directly at the raw second page. If the second page was freshly allocated and never written beyond the object boundary, KMSAN detects reads of uninitialized memory downstream in the decompressor (e.g. sw842_decompress reading the CRC trailer).

I don't get this. How can sw842_decompress() read more bytes than it's told to decompress. We first compress and store the object, before we load and decompress, reading past the known compressed object size (which we pass to decompress function) should not happen. Yosry, Herbert, any ideas?

Quoted 23 lines
Quoted 6 lines

Fix this by passing __GFP_ZERO to alloc_zpdesc() in alloc_zspage() so all pages backing a zspage are zero-initialized at allocation time.

Thanks.

mm/zsmalloc.c static struct zspage *alloc_zspage(struct zs_pool *pool, · line 957

Quoted 11 lines
mm/zsmalloc.c +1 -1
@@ -951,7 +951,7 @@ static struct zspage *alloc_zspage(struct zs_pool *pool,
 	for (i = 0; i < class->pages_per_zspage; i++) {
 		struct zpdesc *zpdesc;
 
-		zpdesc = alloc_zpdesc(gfp, nid);
+		zpdesc = alloc_zpdesc(gfp | __GFP_ZERO, nid);
 		if (!zpdesc) {
 			while (--i >= 0) {
 				zpdesc_dec_zone_page_state(zpdescs[i]);

Decompressing uninitialized memory sounds rather bad, so I'll add a cc:stable to this.

[..]

Quoted 3 lines

I think the Fixes: target is 56e5a103a721 ("zsmalloc: prefer the the original page's node for compressed data"). Can people please check this when reviewing?

If we need to pick a zsmalloc commit, then this probably can be related to zs_obj_read_sg_begin() API (which is used only by zswap at the moemnt), so probably the Fixes: tag be dc2e4982cb018 (zsmalloc: introduce SG-list based object read API).

#3 lore raw
Quoted 11 lines · Sergey Senozhatsky

Adding Yosry and Herbert,

Quoted 7 lines · Andrew Morton
Quoted 7 lines

Pages allocated via alloc_zpdesc() use alloc_pages_node() without __GFP_ZERO, leaving physical memory uninitialized. When a compressed object spans two physical pages in a zspage, zs_obj_read_sg_begin() sets up a scatterlist pointing directly at the raw second page. If the second page was freshly allocated and never written beyond the object boundary, KMSAN detects reads of uninitialized memory downstream in the decompressor (e.g. sw842_decompress reading the CRC trailer).

Quoted 5 lines

I don't get this. How can sw842_decompress() read more bytes than it's told to decompress. We first compress and store the object, before we load and decompress, reading past the known compressed object size (which we pass to decompress function) should not happen. Yosry, Herbert, any ideas?

It sounds like a bug in 842. I'll look into it.

Thanks for pointer.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#4 lore raw
Quoted 12 lines · Herbert Xu
Quoted 11 lines · Sergey Senozhatsky

Adding Yosry and Herbert,

Quoted 7 lines · Andrew Morton
Quoted 7 lines

Pages allocated via alloc_zpdesc() use alloc_pages_node() without __GFP_ZERO, leaving physical memory uninitialized. When a compressed object spans two physical pages in a zspage, zs_obj_read_sg_begin() sets up a scatterlist pointing directly at the raw second page. If the second page was freshly allocated and never written beyond the object boundary, KMSAN detects reads of uninitialized memory downstream in the decompressor (e.g. sw842_decompress reading the CRC trailer).

Quoted 7 lines
Quoted 5 lines

I don't get this. How can sw842_decompress() read more bytes than it's told to decompress. We first compress and store the object, before we load and decompress, reading past the known compressed object size (which we pass to decompress function) should not happen. Yosry, Herbert, any ideas?

It sounds like a bug in 842. I'll look into it.

Thanks! The report doesn't have a reproducer, that is always alarming, a big red flag. Might be entirely unrelated to 842/zsmalloc/zswap.

#5 lore raw
In reply to Sergey Senozhatsky
Quoted 12 lines · Sergey Senozhatsky

Adding Yosry and Herbert,

Quoted 7 lines · Andrew Morton
Quoted 7 lines

Pages allocated via alloc_zpdesc() use alloc_pages_node() without __GFP_ZERO, leaving physical memory uninitialized. When a compressed object spans two physical pages in a zspage, zs_obj_read_sg_begin() sets up a scatterlist pointing directly at the raw second page. If the second page was freshly allocated and never written beyond the object boundary, KMSAN detects reads of uninitialized memory downstream in the decompressor (e.g. sw842_decompress reading the CRC trailer).

Quoted 5 lines

I don't get this. How can sw842_decompress() read more bytes than it's told to decompress. We first compress and store the object, before we load and decompress, reading past the known compressed object size (which we pass to decompress function) should not happen. Yosry, Herbert, any ideas?

I agree it's weird that sw842_decompress() is reading past the compressed length. I didn't look at the code but maybe it's ready in chunks of N bytes (e.g. 8 bytes) so it's reading a few bytes past the end of the compressed length?

Not sure if we should add __GFP_ZERO in zsmalloc just in case, or if that can hide other problems.

Quoted 25 lines
Quoted 24 lines
Quoted 7 lines

Fix this by passing __GFP_ZERO to alloc_zpdesc() in alloc_zspage() so all pages backing a zspage are zero-initialized at allocation time.

Thanks.

mm/zsmalloc.c static struct zspage *alloc_zspage(struct zs_pool *pool, · line 957

Quoted 11 lines
mm/zsmalloc.c +1 -1
@@ -951,7 +951,7 @@ static struct zspage *alloc_zspage(struct zs_pool *pool,
    for (i = 0; i < class->pages_per_zspage; i++) {
            struct zpdesc *zpdesc;
 
-           zpdesc = alloc_zpdesc(gfp, nid);
+           zpdesc = alloc_zpdesc(gfp | __GFP_ZERO, nid);
            if (!zpdesc) {
                    while (--i >= 0) {
                            zpdesc_dec_zone_page_state(zpdescs[i]);

Decompressing uninitialized memory sounds rather bad, so I'll add a cc:stable to this.

[..]

Quoted 8 lines
Quoted 3 lines

I think the Fixes: target is 56e5a103a721 ("zsmalloc: prefer the the original page's node for compressed data"). Can people please check this when reviewing?

If we need to pick a zsmalloc commit, then this probably can be related to zs_obj_read_sg_begin() API (which is used only by zswap at the moemnt), so probably the Fixes: tag be dc2e4982cb018 (zsmalloc: introduce SG-list based object read API).

Agreed.