Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Nanzhe Zhao" <zhaonanzhe@xiaomi.com>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, "Jens Axboe" <axboe@kernel.dk>,
	"Sergey Senozhatsky" <senozhatsky@chromium.org>,
	"Minchan Kim" <minchan@kernel.org>,
	"Kairui Song" <kasong@tencent.com>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Nhat Pham" <nphamcs@gmail.com>,
	"Usama Arif" <usama.arif@linux.dev>,
	"Yosry Ahmed" <yosry@kernel.org>,
	"Chengming Zhou" <chengming.zhou@linux.dev>,
	"Chris Li" <chrisl@kernel.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Ying Huang" <ying.huang@intel.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Ryan Roberts" <ryan.roberts@arm.com>,
	"Kanchana P Sridhar" <kanchana.p.sridhar@intel.com>,
	"Wajdi K Feghali" <wajdi.k.feghali@intel.com>,
	"Bala Seshasayee" <bala.seshasayee@linux.intel.com>,
	"Nick Terrell" <terrelln@fb.com>,
	"Barry Song" <baohua@kernel.org>,
	"Tangquan Zheng" <zhengtangquan@oppo.com>,
	"Bo Zhang" <zhangbo56@xiaomi.com>
Subject: Re: [PATCH 2/4] zram: support compression at the granularity of multi-pages
Date: Sun, 06 Dec 2026 02:20:08 +0000	[thread overview]
Message-ID: <sashiko-outbox-252054@kernel.org> (raw)
In-Reply-To: <20261205021803.3704464-3-zhaonanzhe@xiaomi.com>

Thank you for your contribution! Sashiko AI review found 18 potential
issues to consider:
- [Critical] zram: lockless first-sector dispatch and missing recheck in
  zram_bio_read()
- [Critical] zram: stale ZRAM_COMP_MULTI_PAGES flags on fallback and
  single-page writes
- [Critical] zram: premature __slot_free() in partial multi-page write
  loses sibling data
- [Critical] zram: zram_slot_free_notify() frees live multi-page slot on
  cleared index
- [Critical] zram: self-deadlock on excl_lock in
  zram_read_partial_from_zspool()
- [High] zram: multi-page zram_write_page() leaks tail slots and stale
  flags on error
- [High] zram: non-atomic tail flag updates under head lock corrupt
  bit-lock
- [High] zram: TOCTOU wrong-slot lock and out-of-range frees in
  zram_bio_discard()
- [High] zram: REQ_OP_WRITE_ZEROES silently skips partial multi-page
  slots
- [High] zram: missing slot_lock() mode recheck causes TOCTOU races in
  read/write
- [Medium] zram: broken bi_vcnt check and bi_io_vec read in
  want_multi_pages_comp()
- [Medium] zram: sysfs io_stat and mm_stat miscounted for multi-page
  slots
- [Medium] zram: zram_write_page() holds slot_lock() across compression
- [Medium] zram: zram_read_partial_from_zspool() forces exclusive dstrm
  mode
- [Medium] zram: global atomic64_inc() stats in struct zram cause
  cacheline bouncing
- [Medium] zram: redundant second slot_lock() in multi-page bio read and
  write loops
- [Medium] zram: high-order GFP_NOIO allocation under slot_lock() in
  partial write
- [Medium] zram: missing head recheck after slot_trylock() in
  zram_slot_free_notify()
--

commit 640f4f78298a7b9bcecc7f76d3900db49be4d15d
Author: Tangquan Zheng <zhengtangquan@oppo.com>

zram: support compression at the granularity of multi-pages

This commit adds CONFIG_ZRAM_MULTI_PAGES to compress and decompress
multi-page chunks at ZSMALLOC_MULTI_PAGES_ORDER granularity, serializing
each multi-page window on the bit-lock of its aligned head slot.

> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index 95cec8b34239a..f2476ce6afa8d 100644
> --- a/drivers/block/zram/zram_drv.c
> +++ b/drivers/block/zram/zram_drv.c

[ ... ]

> +static void slot_free(struct zram *zram, unsigned long index)
> +{
> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +	unsigned long head = zram_multi_pages_head_index(index);
> +	int nr_pages = 1;
> +	int i;
> +
> +	/* Clear all slots' MULTI flags first. */
> +	if (__test_multi_pages_comp(zram, head)) {
> +		index = head;
> +		nr_pages = ZCOMP_MULTI_PAGES_NR;
> +		for (i = 0; i < nr_pages; i++)
> +			clear_slot_flag(zram, head + i, ZRAM_COMP_MULTI_PAGES);
> +	}
> +
> +	/*
> +	 * Then release the whole multi-page slot (by releasing the head
> +	 * slot's handle).
> +	 */
> +	__slot_free(zram, index, nr_pages);
> +
> +	/*
> +	 * Drop single-page objects left behind by a failed
> +	 * zram_slot_free_notify trylock.
> +	 */
> +	for (i = 1; i < nr_pages; i++)
> +		__slot_free(zram, index + i, 1);
> +#else
> +	__slot_free(zram, index, 1);
> +#endif
> +}

[Severity: High]
Can modifying tail slots' attr.flags and calling __slot_free() on tail
slots under only slot_lock(zram, head) corrupt tail bit-locks and race
with zs_free()?

In struct zram_table_entry, u32 attr.flags aliases the same word as
unsigned long __lock, where ZRAM_ENTRY_LOCK_BIT lives.  set_slots_multi_pages(),
slot_free(), zram_bvec_write_multi_pages_partial(), and
zram_slot_free_notify() all perform non-atomic read-modify-writes on
tail slots' attr.flags while holding only slot_lock(zram, head).

Meanwhile, mark_slots_idle() and zram_block_state_show() iterate every
index and acquire slot_lock(zram, tail) directly.  In addition,
slot_free() clears ZRAM_COMP_MULTI_PAGES on all slots before calling
__slot_free(zram, index + i, 1), allowing concurrent single-page I/O to
lock and modify tail slots at the same time:

CPU0                              CPU1
slot_lock(zram, head)
slot_free(zram, head)
  clear_slot_flag(tail, MULTI)
                                  slot_lock(zram, tail)
  __slot_free(zram, tail, 1)
    clear_slot_flag(tail, ...)
    zs_free(table[tail].handle)   <-- races on attr.flags / __lock & handle
                                  slot_unlock(zram, tail)

An interleaved non-atomic write to attr.flags can restore
ZRAM_ENTRY_LOCK_BIT to 1 after slot_unlock() (permanent deadlock) or
clear it while held, and unlocked __slot_free() can double-free or
use-after-free the zsmalloc handle.

[ ... ]

> -static int zram_read_page(struct zram *zram, struct page *page,
> -			  unsigned long index, struct bio *parent)
> +static int zram_read_page_size(struct zram *zram, struct page *page,
> +			       unsigned long index, struct bio *parent,
> +			       unsigned long page_size)
>  {
>  	int ret;
>  
>  	slot_lock(zram, index);
>  	mark_slot_accessed(zram, index);
>  	if (!test_slot_flag(zram, index, ZRAM_WB)) {
>  		/* Slot should be locked through out the function call */
> -		ret = read_from_zspool(zram, page, index);
> +		ret = read_from_zspool(zram, page, index, page_size);
>  		slot_unlock(zram, index);

[Severity: High]
Should zram_read_page_size(), zram_bvec_read_multi_pages_partial(), and
zram_write_page() recheck __test_multi_pages_comp() after acquiring
slot_lock()?

Unlike zram_bvec_write_multi_pages_partial(), none of these paths verify
under slot_lock() that the slot's single-page vs multi-page mode still
matches the lockless hint from zram_bio_read() or zram_bio_write():

CPU0                              CPU1
zram_bio_read()
  test_multi_pages_comp() -> true
                                  zram_bio_discard() -> slot_free(head)
                                  zram_write_page(head, PAGE_SIZE)
zram_read_page_size(head,
    ZCOMP_MULTI_PAGES_SIZE)
  slot_lock(zram, head)
  read_incompressible_page()
    zs_obj_read_sg_begin(...,     <-- NULL deref if 4 KiB ZRAM_HUGE
        ZCOMP_MULTI_PAGES_SIZE)

If table[head] was replaced by a single-page ZRAM_HUGE object,
zs_obj_read_sg_begin() takes the spanning branch where get_next_zpdesc()
returns NULL, causing a NULL pointer dereference in sg_copy_to_buffer().
In the reverse single-to-multi race, single-page reads on tail slots
return zeroes and single-page writes leave 4 KiB objects tagged with
ZRAM_COMP_MULTI_PAGES or race on tail slots' __lock.

[ ... ]

> -	zram_stat_add(zram, ZRAM_STAT_COMPR_DATA_SIZE, PAGE_SIZE);
> +	zram_stat_add(zram, ZRAM_STAT_COMPR_DATA_SIZE, page_size);
>  	zram_stat_add(zram, ZRAM_STAT_HUGE_PAGES, 1);
>  	zram_stat_add(zram, ZRAM_STAT_HUGE_PAGES_SINCE, 1);
> -	zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, 1);
> +	zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, nr_pages);

[Severity: Medium]
Should ZRAM_STAT_HUGE_PAGES and ZRAM_STAT_HUGE_PAGES_SINCE be updated by
nr_pages here (and in __slot_free()) instead of 1?

Documentation/admin-guide/blockdev/zram.rst defines both counters as the
number of incompressible pages, matching ZRAM_STAT_PAGES_STORED and
ZRAM_STAT_SAME_PAGES, so counting 1 underreports by ZCOMP_MULTI_PAGES_NR.

Three other stats have similar mismatches: zram_bio_read_multi_pages()
and zram_bio_write_multi_pages() do not increment zram->stats.failed_reads
and failed_writes (leaving io_stat at 0 on multi-page errors),
zram_bio_discard() adds 1 instead of page_size >> PAGE_SHIFT to
ZRAM_STAT_NOTIFY_FREE, and zram_slot_free_notify() increments
multi_pages_miss_free even when the contended slot is a single-page slot.

[ ... ]

> -	slot_lock(zram, index);
> -	slot_free(zram, index);
> +	__slot_free(zram, index, zram_slot_nr_pages(zram, index));
>  	ret = zs_store(zram->mem_pool, &zram->table[index].handle,
>  		       zstrm->buffer, comp_len,
>  		       GFP_NOIO | __GFP_NOWARN |
>  		       __GFP_HIGHMEM | __GFP_MOVABLE, page_to_nid(page));
>  	zcomp_cstrm_put(zstrm);
> -	if (ret) {
> -		slot_unlock(zram, index);
> +	if (ret)
>  		return ret;
> -	}

[Severity: High]
Can a full multi-page write in zram_write_page() leak leftover
single-page objects in tail slots and leave stale ZRAM_COMP_MULTI_PAGES
flags on error?

As slot_free() notes, failed zram_slot_free_notify() trylocks can leave
live single-page objects in tail slots index + 1 .. index + nr_pages - 1.
Because __zram_write_page(), write_same_filled_page(), and
write_incompressible_page() only call __slot_free() on index, those tail
objects remain allocated and double-counted in ZRAM_STAT_PAGES_STORED
and ZRAM_STAT_COMPR_DATA_SIZE across multi-page overwrites.

Also, if zs_store() fails after __slot_free() when overwriting a
multi-page slot, ZRAM_COMP_MULTI_PAGES remains set on the empty window,
so a later single-page write can enter zram_bvec_write_multi_pages_partial()
and leak a multi-page object whose unused sibling flags are never cleared.

> +static int zram_write_page(struct zram *zram, struct page *page,
> +			   unsigned long index, unsigned int page_size)
> +{
> +	int ret;
> +
> +	slot_lock(zram, index);
> +	ret = __zram_write_page(zram, page, index, page_size);
> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +	if (!ret && page_size > PAGE_SIZE)
> +		set_slots_multi_pages(zram, index, page_size >> PAGE_SHIFT);
> +#endif
> +	slot_unlock(zram, index);
> +
> +	return ret;
> +}

[Severity: Medium]
Does holding slot_lock() across page_same_filled(), zcomp_cstrm_get(),
and zcomp_compress() regress write-path lock contention even when
CONFIG_ZRAM_MULTI_PAGES is disabled?

Previously, zram_write_page() only acquired slot_lock() after
compression completed.  Holding slot_lock() while waiting on cstrm->lock
and compressing widens contention on the shared head slot and increases
dropped frees (miss_free) in zram_slot_free_notify().

[ ... ]

> +	offset = page_offset + ((index - head) << PAGE_SHIFT);
>  	if (offset) {
> -		if (n <= (PAGE_SIZE - offset))
> +		if (n <= page_size - offset) {
> +			slot_unlock(zram, head);
>  			goto end_bio;
> -
> -		n -= (PAGE_SIZE - offset);
> -		index++;
> +		}
> +		n -= page_size - offset;
> +		index = head + (page_size >> PAGE_SHIFT);
>  	}
> +	slot_unlock(zram, head);

[Severity: High]
Does skipping partial multi-page slots in zram_bio_discard() break
REQ_OP_WRITE_ZEROES?

When ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE, zram_add() sets
max_write_zeroes_sectors = UINT_MAX and zram_submit_bio() routes
REQ_OP_WRITE_ZEROES to zram_bio_discard().  Skipping leading (offset != 0)
or trailing (n < page_size) portions of a multi-page slot completes
REQ_OP_WRITE_ZEROES with BLK_STS_OK while leaving old non-zero data in
those 4 KiB-aligned sectors.

>  	while (n >= PAGE_SIZE) {
> -		slot_lock(zram, index);
> +		unsigned long head = zram_slot_head_index(zram, index);
> +		unsigned int page_size;
> +
> +		slot_lock(zram, head);
> +		page_size = zram_slot_page_size(zram, head);
> +		if (n < page_size) {
> +			/* the request ends inside this slot, leave it alone */
> +			slot_unlock(zram, head);
> +			break;
> +		}
>  		slot_free(zram, index);
> -		slot_unlock(zram, index);
> +		slot_unlock(zram, head);
>  		zram_stat_add(zram, ZRAM_STAT_NOTIFY_FREE, 1);
> -		index++;
> -		n -= PAGE_SIZE;
> +		index = head + (page_size >> PAGE_SHIFT);
> +		n -= page_size;
>  	}

[Severity: High]
Can the lockless head lookup in zram_bio_discard() lead to an unlocked
slot_free() or freeing slots outside the requested range?

In the while loop, head is computed before slot_lock(zram, head) and is
never revalidated against index under the lock:

CPU0 (discard starting at 6)      CPU1
zram_slot_head_index(zram, 6)
  -> head = 4 (multi-page 4..7)
                                  slot_free(4) (now single-page)
slot_lock(zram, 4)
page_size = PAGE_SIZE
slot_free(zram, 6)                <-- unlocked slot 6 (races with I/O)
slot_unlock(zram, 4)
index = 4 + 1 = 5                 <-- moves backwards before start

In the reverse single-to-multi race, head == 6 is locked while
slot_free(zram, 6) frees table[4].handle without holding slot_lock(4),
destroying live slots 4 and 5 before the discard range and skipping
slots 8 and 9.  The prologue above also only rechecks the multi-to-single
transition, not single-to-multi.

> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +
> +static inline bool want_multi_pages_comp(struct zram *zram, struct bio *bio)
> +{
> +	unsigned long index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
> +
> +	/*
> +	 * For a synchronous device such as zram, swap submits one bio with a
> +	 * single bvec that carries one folio.  Bios with more than one bvec
> +	 * are not supported.
> +	 */
> +	WARN_ON_ONCE(bio->bi_vcnt != 1);
> +
> +	if (bio->bi_io_vec->bv_len >= ZCOMP_MULTI_PAGES_SIZE)
> +		return true;
> +
> +	return !!__test_multi_pages_comp(zram, index);
> +}

[Severity: Medium]
Will WARN_ON_ONCE(bio->bi_vcnt != 1) fire on the first swap writeout?

swap_bdev_submit_write() initializes the bio with bio_init() (which sets
bio->bi_vcnt = 0) and populates bio->bi_iter.bi_size directly without
incrementing bi_vcnt:

mm/page_io.c:swap_bdev_submit_write() {
	...
	bio_init(bio, ctx->sis->bdev, sio->bvecs, ARRAY_SIZE(sio->bvecs),
			REQ_OP_WRITE | REQ_SWAP);
	bio->bi_iter.bi_size = sio->len;
	...
}

Reading bio->bi_io_vec->bv_len directly also ignores bio->bi_iter for
split, cloned, or multi-bvec bios, and not gating on REQ_SWAP or
alignment lets misaligned non-swap block writes enter the multi-page
write path and trigger the WARN_ON_ONCE() checks.

[ ... ]

> +		prio = get_slot_comp_priority(zram, index);
> +		zstrm = zcomp_dstrm_get(zram->comps[prio],
> +					ZCOMP_DSTRM_EXCLUSIVE);
> +		ret = zcomp_decompress(zram->comps[prio], zstrm, sg, size,
> +				       zstrm->decomp_buf, page_size);
> +		if (!ret) {
> +			dst = kmap_local_page(bvec->bv_page);
> +			memcpy(dst + bvec->bv_offset,
> +			       zstrm->decomp_buf + offset, bvec->bv_len);
> +			kunmap_local(dst);
> +		}

[Severity: Critical]
Can zram_read_partial_from_zspool() self-deadlock on zstrm->excl_lock
with stateless decompression backends such as lzo-rle or lz4?

zcomp_dstrm_get(..., ZCOMP_DSTRM_EXCLUSIVE) returns with zstrm->excl_lock
held.  When the compressed object spans two physical pages in zsmalloc,
zcomp_decompress() checks !zcomp_shared_decomp(comp), which is false for
stateless backends (create_dctx == NULL), so it falls through to the
shared path:

drivers/block/zram/zcomp.c:zcomp_decompress() {
	...
	/* shared mode: try a per-request bounce buffer first */
	src = kmalloc(src_len, GFP_NOWAIT | __GFP_NOWARN);
	if (src) {
		ret = zcomp_decompress_buf(comp, zstrm, sg, src, &req);
		kfree(src);
		return ret;
	}

	/* fall back to ->local_copy serialized by ->excl_lock */
	mutex_lock(&zstrm->excl_lock);
	...
}

Under swap-in memory pressure, if the GFP_NOWAIT allocation fails,
zcomp_decompress() calls mutex_lock(&zstrm->excl_lock) while the current
task already holds it, hanging permanently while also holding slot_lock()
and the zspage read lock.

> +		zcomp_dstrm_put(zram->comps[prio], zstrm,
> +				ZCOMP_DSTRM_EXCLUSIVE);

[Severity: Medium]
Does unconditionally using ZCOMP_DSTRM_EXCLUSIVE here defeat shared
decompression on stateless backends during concurrent partial swap-ins?

Unlike read_compressed_page(), which uses ZCOMP_DSTRM_SHARED when
zcomp_shared_decomp() is true, ZCOMP_DSTRM_EXCLUSIVE scans all online
CPUs in zcomp_dstrm_excl_tryget() and sleeps on the global
comp->dstrm_waitq under contention.  Trying a temporary bounce buffer
with ZCOMP_DSTRM_SHARED and falling back to exclusive mode on allocation
failure would preserve shared decompression scalability.

[ ... ]

> +	slot_lock(zram, index);
> +	if (!__test_multi_pages_comp(zram, index)) {
> +		slot_unlock(zram, index);
> +		return -EAGAIN;
> +	}
> +	mark_slot_accessed(zram, index);
> +
> +	page = alloc_pages(GFP_NOIO | __GFP_COMP, ZCOMP_MULTI_PAGES_ORDER);

[Severity: Medium]
Can allocating a high-order GFP_NOIO page while holding slot_lock(zram,
index) stall sibling slots during direct compaction or reclaim?

The temporary page does not depend on the slot contents and could be
allocated before taking slot_lock() (as zram_bvec_write_partial() does),
freeing it on the -EAGAIN path.

> +	ret = read_from_zspool(zram, page, index, ZCOMP_MULTI_PAGES_SIZE);
> +	if (!ret) {
> +		src = kmap_local_page(bvec->bv_page);
> +		dst = kmap_local_page(page);
> +		memcpy(dst + offset, src + bvec->bv_offset, bvec->bv_len);
> +		kunmap_local(dst);
> +		kunmap_local(src);
> +
> +		atomic64_inc(&zram->stats.zram_bio_write_multi_pages_partial_count);
> +		ret = __zram_write_page(zram, page, index,
> +					ZCOMP_MULTI_PAGES_SIZE);

[Severity: Critical]
Does __zram_write_page() destroy untouched live sibling pages if
zs_store() fails with -ENOMEM during a partial multi-page write?

Both __zram_write_page() and write_incompressible_page() call
__slot_free() on table[index] before calling zs_store().  If zs_store()
fails under memory pressure, the old multi-page handle is already freed
while ZRAM_COMP_MULTI_PAGES remains set on live sibling slots, so those
sibling pages silently read back as all zeroes on later swap-in.
Storing into a temporary zs_handle before freeing the old handle would
avoid losing sibling data on allocation failure.

[ ... ]

> +		slot_lock(zram, index);
> +		mark_slot_accessed(zram, index);
> +		slot_unlock(zram, index);

[Severity: Medium]
Is this second slot_lock()/slot_unlock() round-trip in
zram_bio_read_multi_pages() (and the identical block in
zram_bio_write_multi_pages()) redundant?

zram_read_page_size(), zram_write_page(), and
zram_bvec_write_multi_pages_partial() already call mark_slot_accessed()
under slot_lock(), and zram_bvec_read_multi_pages_partial() already holds
slot_lock() and can call mark_slot_accessed() there.  Removing both
blocks halves head-slot lock traffic on every multi-page I/O.

[ ... ]

>  static void zram_bio_read(struct zram *zram, struct bio *bio)
>  {
> +	if (test_multi_pages_comp(zram, bio)) {
> +		zram_bio_read_multi_pages(zram, bio);
> +		return;
> +	}

[Severity: Critical]
Can dispatching the entire read bio from a lockless check of only the
first sector cause a NULL pointer dereference or leak stale plaintext
across processes?

A multi-window read bio that starts in a multi-page window and crosses
into a single-page window (or hits a concurrent replacement before
slot_lock()) enters zram_bio_read_multi_pages() without per-window
rechecks in zram_read_page_size() or zram_bvec_read_multi_pages_partial().

If a single-page ZRAM_HUGE slot is read with ZCOMP_MULTI_PAGES_SIZE,
zs_obj_read_sg_begin() calls get_next_zpdesc(), which returns NULL on a
single-zpdesc huge zspage, crashing in sg_copy_to_buffer().  If a partial
read hits a single-page compressed slot at head with offset >= PAGE_SIZE,
zcomp_decompress() writes only PAGE_SIZE bytes and returns 0, so
memcpy() from zstrm->decomp_buf + offset copies stale data from prior
decompressions on that CPU into the caller's page.

[ ... ]

> +static void zram_bio_write(struct zram *zram, struct bio *bio)
> +{
> +	unsigned long start_time = bio_start_io_acct(bio);
> +	int ret = -EAGAIN;
> +
> +	if (want_multi_pages_comp(zram, bio))
> +		ret = zram_bio_write_multi_pages(zram, bio);
> +
> +	/*
> +	 * If the multi-page slot has been freed, fall back to the single-page
> +	 * path.
> +	 */
> +	if (ret == -EAGAIN)
> +		ret = zram_bio_write_page(zram, bio);

[Severity: Critical]
Can falling back to zram_bio_write_page() on -EAGAIN (or any single-page
write hitting a multi-page slot) leave 4 KiB objects tagged with
ZRAM_COMP_MULTI_PAGES?

If a write bio larger than ZCOMP_MULTI_PAGES_SIZE has a trailing partial
chunk on a non-multi-page slot (for example, a 20 KiB write covering
slots 0..4), iteration 1 of zram_bio_write_multi_pages() commits a
multi-page object to slots 0..3 and iteration 2 returns -EAGAIN.
zram_bio_write() then replays the whole bio from slot 0 via
zram_bio_write_page().

In zram_write_page() with page_size == PAGE_SIZE, overwriting slot 0
calls __slot_free() without clearing ZRAM_COMP_MULTI_PAGES on slots 0..3,
leaving a 4 KiB object tagged as multi-page (which corrupts accounting
and later triggers NULL dereference or stale decomp_buf reads).  For tail
slots, zram_write_page() locks index instead of head, leaking the tail
handle while subsequent reads still consult table[head].

[ ... ]

> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +	{
> +		unsigned long head = zram_slot_head_index(zram, index);
> +
> +		/*
> +		 * Try to lock the head slot to serialize with concurrent
> +		 * slot_free().  On failure, skip this free, same as the
> +		 * single-page path.
> +		 */
> +		if (!slot_trylock(zram, head)) {
> +			atomic64_inc(&zram->stats.miss_free);
> +			atomic64_inc(&zram->stats.multi_pages_miss_free);
> +			return;
> +		}

[Severity: Medium]
Can zram_slot_free_notify() run __slot_free(zram, index, 1) without
holding slot_lock(zram, index) if head != index and the multi-page slot
is freed before slot_trylock(zram, head)?

Because BLKDISCARD and BLKZEROOUT do not check IS_SWAPFILE() or hold the
swap cluster lock, a concurrent discard on the swap device can clear
ZRAM_COMP_MULTI_PAGES in the window before slot_trylock(zram, head):

CPU0 (notify index = 1)           CPU1 (BLKDISCARD)
zram_slot_head_index(zram, 1)
  -> head = 0
                                  slot_lock(zram, 0)
                                  slot_free(zram, 0)
                                  slot_unlock(zram, 0)
slot_trylock(zram, 0) -> ok
__test_multi_pages_comp(0) == 0
slot_free(zram, 1)
  __slot_free(zram, 1, 1)         <-- unlocked non-atomic RMW on table[1]

Since table[1].attr.flags aliases table[1].__lock, racing with a
concurrent slot_lock(zram, 1) can corrupt or permanently stick the slot 1
bit-lock.  When head != index and __test_multi_pages_comp(zram, head) ==
0, zram_slot_free_notify() should unlock head and retry slot_trylock() on
index.

> +		if (__test_multi_pages_comp(zram, head) > 1) {
> +			clear_slot_flag(zram, index, ZRAM_COMP_MULTI_PAGES);
> +			slot_unlock(zram, head);
> +			return;
> +		}
> +
> +		/*
> +		 * Free the whole multi-page slot if this is the last slot
> +		 * holding the MULTI_PAGE flag.
> +		 */
> +		slot_free(zram, index);
> +		slot_unlock(zram, head);

[Severity: Critical]
Can zram_slot_free_notify() destroy a live sibling page when called for
an index whose ZRAM_COMP_MULTI_PAGES flag is already clear?

Suppose slots 0..3 hold a multi-page object and slots 1..3 are freed,
leaving ZRAM_COMP_MULTI_PAGES set only on live slot 0.  If swap reuses
slot 1 for a zero-filled folio (recorded in swap zeromap without writing
to zram) and later frees slot 1, swap_range_free() calls
zram_slot_free_notify(bdev, 1).

Because __test_multi_pages_comp(zram, 0) is 1 (belonging to slot 0) and
test_slot_flag(zram, index, ZRAM_COMP_MULTI_PAGES) is never checked,
execution falls through to slot_free(zram, 1), freeing table[0].handle so
live slot 0 silently reads back as all zeroes.

> diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
> index 1fbb00dc2c298..5969619129924 100644
> --- a/drivers/block/zram/zram_drv.h
> +++ b/drivers/block/zram/zram_drv.h

[ ... ]

>  struct zram_stats {
>  	atomic64_t failed_reads;	/* can happen when memory is too low */
>  	atomic64_t failed_writes;	/* can happen when memory is too low */
>  	atomic_long_t max_used_pages;	/* no. of maximum pages stored */
>  	atomic64_t miss_free;		/* no. of missed free */
>  #ifdef	CONFIG_ZRAM_WRITEBACK
>  	atomic64_t bd_count;		/* no. of pages in backing device */
>  	atomic64_t bd_reads;		/* no. of reads from backing device */
>  	atomic64_t bd_writes;		/* no. of writes from backing device */
>  #endif
> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +	atomic64_t zram_bio_write_multi_pages_count;
> +	atomic64_t zram_bio_read_multi_pages_count;
> +	atomic64_t multi_pages_failed_writes;
> +	atomic64_t multi_pages_failed_reads;
> +	atomic64_t zram_bio_write_multi_pages_partial_count;
> +	atomic64_t zram_bio_read_multi_pages_partial_count;
> +	atomic64_t multi_pages_miss_free;
> +#endif
>  };

[Severity: Medium]
Should the hot per-I/O multi-page counters live in struct zram_pcpu_stats
as local64_t instead of global atomic64_t fields in struct zram_stats?

Incrementing these shared counters on every multi-page read and write
dirties cachelines inside struct zram adjacent to max_used_pages,
pcpu_stats, limit_pages, and disksize, reintroducing the cross-CPU
cacheline bouncing that commit 7691f2fb29d7 ("zram: keep the hot
statistics per CPU") eliminated.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205021803.3704464-1-zhaonanzhe@xiaomi.com?part=2


  reply	other threads:[~2026-12-06  2:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  2:17 [RFC PATCH v5 0/4] zram/zsmalloc: multi-page compression for large folios Nanzhe Zhao
2026-12-05  2:18 ` [PATCH 1/4] mm: zsmalloc: support objects compressed based on multiple pages Nanzhe Zhao
2026-12-06  2:20   ` sashiko-bot
2026-12-05  2:18 ` [PATCH 2/4] zram: support compression at the granularity of multi-pages Nanzhe Zhao
2026-12-06  2:20   ` sashiko-bot [this message]
2026-12-05  2:18 ` [PATCH 3/4] zram: backend_zstd: use multi-page size for zstd parameter setup Nanzhe Zhao
2026-12-05  2:18 ` [PATCH 4/4] mm: swap: allow direct reclaim for synchronous multi-page swapin Nanzhe Zhao
2026-12-06  2:20   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-252054@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=axboe@kernel.dk \
    --cc=bala.seshasayee@linux.intel.com \
    --cc=baohua@kernel.org \
    --cc=chengming.zhou@linux.dev \
    --cc=chrisl@kernel.org \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kanchana.p.sridhar@intel.com \
    --cc=kasong@tencent.com \
    --cc=linux-mm@kvack.org \
    --cc=minchan@kernel.org \
    --cc=nphamcs@gmail.com \
    --cc=ryan.roberts@arm.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=senozhatsky@chromium.org \
    --cc=surenb@google.com \
    --cc=terrelln@fb.com \
    --cc=usama.arif@linux.dev \
    --cc=wajdi.k.feghali@intel.com \
    --cc=ying.huang@intel.com \
    --cc=yosry@kernel.org \
    --cc=zhangbo56@xiaomi.com \
    --cc=zhaonanzhe@xiaomi.com \
    --cc=zhengtangquan@oppo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox