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: "Tangquan Zheng" <zhengtangquan@oppo.com>,
	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>,
	"Bo Zhang" <zhangbo56@xiaomi.com>
Subject: Re: [RFC PATCH v4 2/4] zram: support compression at the granularity of multi-pages
Date: Mon, 16 Nov 2026 02:46:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-216089@kernel.org> (raw)
In-Reply-To: <20261115024455.1465726-3-zhaonanzhe@xiaomi.com>

Thank you for your contribution! Sashiko AI review found 14 potential
issues to consider:
- [Critical] zram: partial read in zram_read_partial_from_zspool()
  ignores bv_len/offset
- [Critical] zram: OOB access and data loss in zram_write_page() using
  folio_size()
- [Critical] zram: bio-wide routing in want_multi_pages_comp() and
  test_multi_pages_comp()
- [Critical] zram: multi_pages_bvec_iter_bvec() and partial read corrupt
  multi-page I/O
- [High] zram: unlocked flag updates and leaked handles in
  zram_write_page()
- [High] zram: sibling race and premature flag clear in
  zram_slot_free_notify()
- [High] zram: unlocked RMW in zram_bvec_write_multi_pages_partial()
  loses writes
- [High] zram: use-after-free in zram_bvec_write_multi_pages_partial()
  on ZRAM_WB
- [High] zram: out-of-bounds access in place_pp_slot() and
  post-processing overflow
- [High] mm: zsmalloc: VM_BUG_ON in obj_write() and bad offset in
  zs_rebind_handle()
- [Medium] zram: out-of-bounds zram->table access when disksize is not
  multi-page aligned
- [Medium] zram: ZRAM_FLAG_SHIFT overflows 32-bit attr.flags on 16K/64K
  page configs
- [Medium] zram: inverted lock order between excl_lock and buf_lock in
  zcomp
- [Low] zram: misplaced zsmalloc build fix, missing commit data, and
  stat format
--

commit d614cb6134b7b12651ec475514fd15b492598ab0
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 store large
folios in ZCOMP_MULTI_PAGES_SIZE chunks instead of individual PAGE_SIZE
pages.

> diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
> index ad8c7dc739223..8fa2a44adaa30 100644
> --- a/drivers/block/zram/zcomp.c
> +++ b/drivers/block/zram/zcomp.c

[ ... ]

> @@ -101,11 +105,23 @@ static int zcomp_dstrm_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
>  		}
>  	}
>  
> -	zstrm->local_copy = vzalloc(PAGE_SIZE);
> +	zstrm->local_copy = vzalloc(ZCOMP_MULTI_PAGES_SIZE);
>  	if (!zstrm->local_copy) {
>  		zcomp_dstrm_free(comp, zstrm);
>  		return -ENOMEM;
>  	}
> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +	/*
> +	 * Preallocate a decompression buffer for partial multi-page reads to
> +	 * avoid memory allocation in the swapin path.
> +	 */
> +	zstrm->decomp_buf = vzalloc(ZCOMP_MULTI_PAGES_SIZE);
> +	if (!zstrm->decomp_buf) {
> +		zcomp_dstrm_free(comp, zstrm);
> +		return -ENOMEM;
> +	}
> +	mutex_init(&zstrm->buf_lock);
> +#endif
>  	return 0;
>  }

[Severity: Medium]
Can zstrm->excl_lock and zstrm->buf_lock be acquired in opposite orders?

In zram_read_partial_from_zspool(), an exclusive decompression backend
(ZCOMP_DSTRM_EXCLUSIVE) takes zstrm->excl_lock inside zcomp_dstrm_get()
and then locks zstrm->buf_lock.  A shared decompression backend
(ZCOMP_DSTRM_SHARED) locks zstrm->buf_lock first and then calls
zcomp_decompress(), which locks zstrm->excl_lock when a two-page object
falls back to zstrm->local_copy:

CPU0                              CPU1
----                              ----
lock(&zstrm->excl_lock)
                                  lock(&zstrm->buf_lock)
lock(&zstrm->buf_lock)
                                  lock(&zstrm->excl_lock)

 *** DEADLOCK ***

CPU0: zram_read_partial_from_zspool() -> zcomp_dstrm_get()
CPU1: zram_read_partial_from_zspool() -> zcomp_decompress()

Because all zstrm->excl_lock and zstrm->buf_lock instances share static
lockdep classes across devices and CONFIG_ZRAM_MULTI_COMP priorities,
this triggers a lockdep circular locking splat.

Also, zcomp_dstrm_init() runs from the CPUHP_ZCOMP_PREPARE hotplug
callback every time a CPU comes online, so mutex_init(&zstrm->buf_lock)
re-initializes an already initialized mutex instead of initializing it
once per possible CPU in zcomp_init().

> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index 8b7d6926da266..e258289679015 100644
> --- a/drivers/block/zram/zram_drv.c
> +++ b/drivers/block/zram/zram_drv.c

[ ... ]

> @@ -484,7 +486,7 @@ static ssize_t idle_store(struct device *dev, struct device_attribute *attr,
>  static int read_from_zspool_raw(struct zram *zram, struct page *page,
>  				unsigned long index);
>  static int read_from_zspool(struct zram *zram, struct page *page,
> -			    unsigned long index);
> +			    unsigned long index, unsigned long page_size);
>  
>  struct zram_wb_ctl {
>  	/* idle list is accessed only by the writeback task, no concurency */
> @@ -1092,7 +1094,7 @@ static int zram_writeback_slots(struct zram *zram,
>  		if (zram->compressed_wb)
>  			err = read_from_zspool_raw(zram, req->page, index);
>  		else
> -			err = read_from_zspool(zram, req->page, index);
> +			err = read_from_zspool(zram, req->page, index, PAGE_SIZE);
>  		if (err)
>  			goto next;
>  		slot_unlock(zram, index);

[Severity: High]
Can post-processing a multi-page slot overflow ctl->pp_buckets[] and
req->page?

Neither scan_slots_for_writeback() nor scan_slots_for_recompress() skips
ZRAM_COMP_MULTI_PAGES slots, where get_slot_size() can be up to
ZCOMP_MULTI_PAGES_SIZE.  In place_pp_slot(), NUM_PP_BUCKETS is sized for
PAGE_SIZE:

drivers/block/zram/zram_drv.c:place_pp_slot() {
	...
	bid = get_slot_size(zram, pps->index) / PP_BUCKET_SIZE_RANGE;
	list_add(&pps->entry, &ctl->pp_buckets[bid]);
}

When a multi-page slot's compressed size exceeds PAGE_SIZE, bid exceeds
NUM_PP_BUCKETS and corrupts memory past struct zram_pp_ctl.  In
zram_writeback_slots(), req->page is a single PAGE_SIZE page, so
read_from_zspool_raw() also overflows req->page and wraps PAGE_SIZE - size
in memzero_page(), while uncompressed writeback and recompress_slot() pass
PAGE_SIZE for a multi-page object.

[ ... ]

> @@ -2298,12 +2365,27 @@ static int zram_write_page(struct zram *zram, struct page *page,
>  	struct zcomp_cstrm *zstrm;
>  	unsigned long element;
>  	bool same_filled;
> +	unsigned long page_size = PAGE_SIZE;
> +	int huge_class_idx = ZSMALLOC_TYPE_BASEPAGE;
> +	int nr_pages = 1;
> +
> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +	/*
> +	 * Multi-page compression packs ZCOMP_MULTI_PAGES_NR pages into one
> +	 * compressed object.
> +	 */
> +	if (folio_size(page_folio(page)) >= ZCOMP_MULTI_PAGES_SIZE) {
> +		page_size = ZCOMP_MULTI_PAGES_SIZE;
> +		huge_class_idx = ZSMALLOC_TYPE_MULTI_PAGES;
> +		nr_pages = ZCOMP_MULTI_PAGES_NR;
> +	}
> +#endif
>  
>  	mem = kmap_local_page(page);
> -	same_filled = page_same_filled(mem, &element);
> +	same_filled = page_same_filled(mem, &element, page_size);
>  	kunmap_local(mem);
>  	if (same_filled)
> -		return write_same_filled_page(zram, element, index);
> +		return write_same_filled_page(zram, element, index, nr_pages);

[Severity: Critical]
Does sizing the write from folio_size(page_folio(page)) instead of the I/O
length cause out-of-bounds accesses and dropped pages?

When a single-page bio (bv_len == PAGE_SIZE) writes a subpage belonging to
a large folio, zram_bio_write() takes the single-page loop and calls
zram_write_page(), where folio_size(page_folio(page)) >=
ZCOMP_MULTI_PAGES_SIZE is still true.  If page is near the end of the
folio, reading ZCOMP_MULTI_PAGES_SIZE bytes starting at page reads past
the end of the folio, and setting ZRAM_COMP_MULTI_PAGES across index ..
index + nr_pages - 1 on an unaligned index corrupts adjacent slots or
writes past the end of zram->table.

Conversely, if bio_add_page() merges ZCOMP_MULTI_PAGES_NR physically
contiguous order-0 pages into one bvec of length ZCOMP_MULTI_PAGES_SIZE,
zram_bio_write_multi_pages() advances the iterator by
ZCOMP_MULTI_PAGES_SIZE while zram_write_page() sees folio_size() ==
PAGE_SIZE and stores only the first page.

> @@ -2320,9 +2402,10 @@ static int zram_write_page(struct zram *zram, struct page *page,
>  		return ret;
>  	}
>  
> -	if (comp_len >= huge_class_size) {
> +	if (comp_len >= huge_class_size[huge_class_idx]) {
>  		zcomp_cstrm_put(zstrm);
> -		return write_incompressible_page(zram, page, index);
> +		return write_incompressible_page(zram, page, index, page_size,
> +						 nr_pages);
>  	}
>  
>  	slot_lock(zram, index);
> @@ -2341,10 +2424,18 @@ static int zram_write_page(struct zram *zram, struct page *page,
>  
>  	set_slot_size(zram, index, comp_len);
>  	mark_slot_accessed(zram, index);
> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +	if (nr_pages > 1) {
> +		int i;
> +
> +		for (i = 0; i < nr_pages; i++)
> +			set_slot_flag(zram, index + i, ZRAM_COMP_MULTI_PAGES);
> +	}
> +#endif
>  	slot_unlock(zram, index);

[Severity: High]
Does setting ZRAM_COMP_MULTI_PAGES on index + 1 .. index + nr_pages - 1
without locking or freeing those tail slots corrupt the slot bit-lock and
leak existing objects?

zram_write_page(), write_same_filled_page(), and
write_incompressible_page() only lock and call slot_free() on the head
slot index:

1. Because table[index + i].attr.flags aliases table[index + i].__lock,
   the unlocked non-atomic |= in set_slot_flag(zram, index + i, ...) races
   with concurrent slot_lock() / slot_unlock() on tail slots and can
   corrupt ZRAM_ENTRY_LOCK_BIT, hanging waiters in wait_on_bit_lock().
2. Any single-page zsmalloc handles already stored in the tail slots are
   never freed before ZRAM_COMP_MULTI_PAGES is set on them, leaking those
   objects.
3. During a partial write after some sub-slots were already freed by
   zram_slot_free_notify(), re-setting ZRAM_COMP_MULTI_PAGES on all
   nr_pages sub-slots marks already-freed swap slots that will never
   receive another free notification, so the multi-page object can never
   be freed when the remaining active sub-slots are released.
4. If the head sub-slot was freed first via zram_slot_free_notify() (which
   clears ZRAM_COMP_MULTI_PAGES on index), a later slot_free(zram, index)
   sees ZRAM_COMP_MULTI_PAGES clear on index and subtracts 1 instead of
   ZCOMP_MULTI_PAGES_NR from pages_stored and same_pages, while
   slot_free_npages() on discard or zs_store() failure leaves
   ZRAM_COMP_MULTI_PAGES set on the tail slots.
5. On a zsmalloc multi-page size class, zs_store() -> obj_write() calls
   memcpy_to_page() with off + sizes[0] == ZSMALLOC_MULTI_PAGES_SIZE when
   an object spans two zpdescs, which triggers
   VM_BUG_ON(offset + len > PAGE_SIZE) in memcpy_to_page(); similarly,
   zs_rebind_handle() uses offset_in_page() instead of masking with
   page_size - 1, corrupting object headers at offsets >= PAGE_SIZE.

[ ... ]

> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +
> +static inline unsigned long zram_multi_pages_head_index(unsigned long index)
> +{
> +	return index & ~((unsigned long)ZCOMP_MULTI_PAGES_NR - 1);
> +}
> +
> +static inline int __test_multi_pages_comp(struct zram *zram,
> +					 unsigned long index)
> +{
> +	unsigned long head = zram_multi_pages_head_index(index);
> +	int i, count = 0;
> +
> +	for (i = 0; i < ZCOMP_MULTI_PAGES_NR; i++) {
> +		if (test_slot_flag(zram, head + i, ZRAM_COMP_MULTI_PAGES))
> +			count++;
> +	}
> +
> +	return count;
> +}

[Severity: Medium]
Can __test_multi_pages_comp() access past the end of zram->table when
disksize is not a multiple of ZCOMP_MULTI_PAGES_SIZE?

disksize_store() only aligns disksize with PAGE_ALIGN(disksize).  When
num_pages is not a multiple of ZCOMP_MULTI_PAGES_NR, an access to the
final slots has head + i >= num_pages, so __test_multi_pages_comp() and
the set_slot_flag() loops in the multi-page write path read and write past
the end of zram->table.

> +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;
> +
> +	if (bio->bi_io_vec->bv_len >= ZCOMP_MULTI_PAGES_SIZE)
> +		return true;
> +
> +	slot_lock(zram, index);
> +	if (__test_multi_pages_comp(zram, index)) {
> +		slot_unlock(zram, index);
> +		return true;
> +	}
> +	slot_unlock(zram, index);
> +
> +	return false;
> +}
> +
> +static inline bool test_multi_pages_comp(struct zram *zram, struct bio *bio)
> +{
> +	unsigned long index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
> +
> +	return !!__test_multi_pages_comp(zram, index);
> +}

[Severity: Critical]
Can routing the entire bio through the multi-page or single-page path
based only on the first sector crash or corrupt data?

want_multi_pages_comp() and test_multi_pages_comp() inspect only the bio's
initial sector and bio->bi_io_vec->bv_len before iterating the bio.  If an
unaligned write bio with bv_len >= ZCOMP_MULTI_PAGES_SIZE or a multi-bvec
bio enters the multi-page path and lands on a head slot that holds a
single-page (PAGE_SIZE) object, zram_bvec_write_multi_pages_partial() or
zram_bvec_read_multi_pages() calls zram_read_page_size(...,
ZCOMP_MULTI_PAGES_SIZE) on that single-page slot:

- If the single-page head slot has ZRAM_HUGE set (a 1-page ZsHugePage),
  read_incompressible_page() calls zs_obj_read_sg_begin() with
  ZCOMP_MULTI_PAGES_SIZE, which takes the two-page branch, gets NULL from
  get_next_zpdesc(zpdesc), and dereferences NULL in sg_copy_to_buffer().
- If the single-page head slot holds a compressed PAGE_SIZE object during
  zram_bvec_write_multi_pages_partial(), zcomp_decompress() only fills the
  first PAGE_SIZE bytes of the unzeroed order-ZCOMP_MULTI_PAGES_ORDER page,
  and zram_write_page() compresses and stores the remaining uninitialized
  kernel memory.

Conversely, if a read bio starts on a single-page slot and crosses into a
multi-page slot group, zram_bio_read() stays in the single-page loop,
failing decompression on the head slot and reading the unallocated tail
slots as zero-filled pages.

> +static inline bool is_multi_pages_partial_io(struct bio_vec *bvec)
> +{
> +	return bvec->bv_len != ZCOMP_MULTI_PAGES_SIZE;
> +}
> +
> +static int zram_read_partial_from_zspool(struct zram *zram, struct page *page,
> +					unsigned long index, int offset)
> +{
> +	struct zs_handle *handle = &zram->table[index].handle;
> +	struct zcomp_dstrm *zstrm;
> +	struct scatterlist sg[2];
> +	enum zcomp_dstrm_mode mode;
> +	unsigned int size;
> +	void *src, *dst;
> +	u32 prio;
> +	int ret;
> +	unsigned long page_size = ZCOMP_MULTI_PAGES_SIZE;
> +
> +	if (test_slot_flag(zram, index, ZRAM_SAME) ||
> +	    !zs_handle_is_allocated(handle)) {
> +		void *mem;
> +
> +		mem = kmap_local_page(page);
> +		zram_fill_page(mem, PAGE_SIZE, zram->table[index].element);
> +		kunmap_local(mem);
> +		return 0;
> +	}
> +
> +	size = get_slot_size(zram, index);
> +	zs_obj_read_sg_begin(zram->mem_pool, handle, sg, size);
> +
> +	if (size == page_size) {
> +		src = kmap_local_page(sg_page(sg));
> +		dst = kmap_local_page(page);
> +		memcpy(dst, src + offset, PAGE_SIZE);
> +		kunmap_local(dst);
> +		kunmap_local(src);
> +		ret = 0;
> +	} else {
> +		prio = get_slot_comp_priority(zram, index);
> +		mode = ZCOMP_DSTRM_EXCLUSIVE;
> +		if (zcomp_shared_decomp(zram->comps[prio]))
> +			mode = ZCOMP_DSTRM_SHARED;
> +
> +		zstrm = zcomp_dstrm_get(zram->comps[prio], mode);
> +		mutex_lock(&zstrm->buf_lock);
> +		ret = zcomp_decompress(zram->comps[prio], zstrm, sg, size,
> +				       zstrm->decomp_buf, page_size);
> +		if (!ret) {
> +			dst = kmap_local_page(page);
> +			memcpy(dst, zstrm->decomp_buf + offset, PAGE_SIZE);
> +			kunmap_local(dst);
> +		}
> +		mutex_unlock(&zstrm->buf_lock);
> +		zcomp_dstrm_put(zram->comps[prio], zstrm, mode);
> +	}
> +	zs_obj_read_sg_end(zram->mem_pool, handle);
> +
> +	return ret;
> +}

[Severity: Critical]
Does zram_read_partial_from_zspool() ignore bvec->bv_len and
bvec->bv_offset?

zram_bvec_read_multi_pages_partial() passes only bvec->bv_page and the
slot offset to zram_read_partial_from_zspool(), and all three branches
above always write PAGE_SIZE bytes to offset 0 of page.  When a partial
multi-page read has bv.bv_len > PAGE_SIZE (for example, 2 or 3 pages) or a
non-zero bvec->bv_offset, zram_bio_read_multi_pages() still advances the
bio iterator by bv.bv_len, writing to the wrong offset of bv_page or
leaving the remaining pages of the read uninitialized.

[ ... ]

> +static int zram_bvec_write_multi_pages_partial(struct zram *zram,
> +					       struct bio_vec *bvec,
> +					       unsigned long index, int offset,
> +					       struct bio *bio)
> +{
> +	struct page *page;
> +	void *src, *dst;
> +	int ret;
> +
> +	page = alloc_pages(GFP_NOIO | __GFP_COMP, ZCOMP_MULTI_PAGES_ORDER);
> +	if (!page)
> +		return -ENOMEM;
> +
> +	ret = zram_read_page_size(zram, page, index, bio,
> +				  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);
> +	}
> +
> +	__free_pages(page, ZCOMP_MULTI_PAGES_ORDER);
> +	return ret;
> +}

[Severity: High]
Can two concurrent partial writes to different sub-slots in the same
multi-page group overwrite each other?

zram_read_page_size() unlocks slot_lock(zram, index) after reading the
multi-page into the temporary page, and zram_write_page() only re-acquires
slot_lock(zram, index) after compressing the updated buffer.  If two CPUs
concurrently perform partial writes to different offsets of the same head
index, both read the old multi-page before either writes back, so the
second writer overwrites the first writer's sub-page with stale data.

[Severity: High]
Does passing bio as parent to zram_read_page_size() cause a use-after-free
of page when ZRAM_WB is set?

When test_slot_flag(zram, index, ZRAM_WB) is true, zram_read_page_size()
calls read_from_bdev(zram, page, index, blk_idx, parent) with a non-NULL
parent bio:

drivers/block/zram/zram_drv.c:read_from_bdev() {
	atomic64_inc(&zram->stats.bd_reads);
	if (!parent) {
		if (WARN_ON_ONCE(!IS_ENABLED(ZRAM_PARTIAL_IO)))
			return -EIO;
		return read_from_bdev_sync(zram, page, index, blk_idx);
	}
	return read_from_bdev_async(zram, page, index, blk_idx, parent);
}

read_from_bdev_async() submits an asynchronous child bio referencing page
(inheriting REQ_OP_WRITE from the write bio) and returns 0 immediately,
after which zram_bvec_write_multi_pages_partial() frees page via
__free_pages() while the backing-device I/O and completion work are still
using it.

[ ... ]

> @@ -2814,7 +3189,34 @@ static void zram_slot_free_notify(struct block_device *bdev,
>  		return;
>  	}
>  
> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +	{
> +		int comp_count = __test_multi_pages_comp(zram, index);
> +
> +		if (comp_count > 1) {
> +			clear_slot_flag(zram, index, ZRAM_COMP_MULTI_PAGES);
> +			slot_unlock(zram, index);
> +			return;
> +		}
> +
> +		if (comp_count == 1) {
> +			clear_slot_flag(zram, index, ZRAM_COMP_MULTI_PAGES);
> +			slot_unlock(zram, index);
> +			index = zram_multi_pages_head_index(index);
> +			nr_pages_to_free = ZCOMP_MULTI_PAGES_NR;
> +			if (!slot_trylock(zram, index)) {
> +				atomic64_inc(&zram->stats.multi_pages_miss_free);
> +				return;
> +			}
> +		}
> +	}
> +#endif

[Severity: High]
Can concurrent zram_slot_free_notify() calls on sibling sub-slots leak the
multi-page object?

__test_multi_pages_comp() counts ZRAM_COMP_MULTI_PAGES across the group
while only holding slot_trylock(zram, index) on the current sub-slot:

CPU0                                  CPU1
zram_slot_free_notify(head + 1)       zram_slot_free_notify(head + 2)
  slot_trylock(zram, head + 1)          slot_trylock(zram, head + 2)
  __test_multi_pages_comp() -> 2        __test_multi_pages_comp() -> 2
  clear_slot_flag(head + 1, ...)        clear_slot_flag(head + 2, ...)
  slot_unlock(zram, head + 1)           slot_unlock(zram, head + 2)
  return;                               return;  <-- head is never freed

In addition, when comp_count == 1, clearing ZRAM_COMP_MULTI_PAGES and
unlocking index before slot_trylock(zram, head) means that if
slot_trylock(zram, head) fails, the last ZRAM_COMP_MULTI_PAGES flag has
already been cleared, and another CPU can also reallocate and write to
head in the unlocked window before slot_trylock(zram, head) runs.
__test_multi_pages_comp() also does not check whether index itself has
ZRAM_COMP_MULTI_PAGES set before treating it as part of a multi-page
group.

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

[ ... ]

> @@ -37,7 +42,12 @@
>   *
>   * We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow.
>   */
> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +#define ZRAM_FLAG_SHIFT (PAGE_SHIFT + CONFIG_ZSMALLOC_MULTI_PAGES_ORDER + 1)
> +#else
>  #define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1)
> +#endif

[Severity: Medium]
Does ZRAM_FLAG_SHIFT overflow the 32-bit attr.flags field when
PAGE_SHIFT + CONFIG_ZSMALLOC_MULTI_PAGES_ORDER > 21?

CONFIG_ZSMALLOC_MULTI_PAGES_ORDER allows values up to 9, and enum
zram_pageflags defines 10 flag bits above ZRAM_FLAG_SHIFT.  On 16K or 64K
page configurations with larger orders (for example, PAGE_SHIFT == 16 with
order >= 6), __NR_ZRAM_PAGEFLAGS exceeds 32 and triggers
BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > sizeof(zram_te.attr.flags) * 8) in
zram_init().

[ ... ]

> @@ -167,4 +190,24 @@ struct zram {
>  	struct dentry *debugfs_dir;
>  #endif
>  };
> +
> +#ifdef CONFIG_ZRAM_MULTI_PAGES
> +#define multi_pages_bvec_iter_offset(bvec, iter)				\
> +	(mp_bvec_iter_offset((bvec), (iter)) % ZCOMP_MULTI_PAGES_SIZE)
> +
> +#define multi_pages_bvec_iter_len(bvec, iter)				\
> +	min_t(unsigned int, mp_bvec_iter_len((bvec), (iter)),		\
> +	      ZCOMP_MULTI_PAGES_SIZE -					\
> +	      multi_pages_bvec_iter_offset((bvec), (iter)))
> +
> +#define multi_pages_bvec_iter_bvec(bvec, iter)				\
> +((struct bio_vec) {							\
> +	.bv_page	= mp_bvec_iter_page((bvec), (iter)),		\
> +	.bv_len		= multi_pages_bvec_iter_len((bvec), (iter)),	\
> +	.bv_offset	= multi_pages_bvec_iter_offset((bvec), (iter)),	\
> +})
> +
> +#define multi_pages_bio_iter_iovec(bio, iter)				\
> +	multi_pages_bvec_iter_bvec((bio)->bi_io_vec, (iter))
> +#endif
>  #endif

[Severity: Critical]
Does using mp_bvec_iter_page() in multi_pages_bvec_iter_bvec() keep
.bv_page stuck at the first page of a large folio across iterations?

Unlike bvec_iter_page(), mp_bvec_iter_page(bvec, iter) always returns
bvec[iter.bi_idx].bv_page without adding mp_bvec_iter_page_idx(bvec,
iter), while multi_pages_bvec_iter_offset() wraps .bv_offset modulo
ZCOMP_MULTI_PAGES_SIZE.  When a folio larger than ZCOMP_MULTI_PAGES_SIZE
(such as a 64KiB or 128KiB folio in a single multi-page bvec) is read or
written, every iteration of zram_bio_write_multi_pages() and
zram_bio_read_multi_pages() accesses subpage 0 of the folio, repeatedly
compressing or overwriting the first ZCOMP_MULTI_PAGES_SIZE bytes and
leaving the rest of the folio unread or unwritten.

> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index 2d83fcc8610da..81d908d6d5a8d 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
> @@ -1697,7 +1697,8 @@ static struct zs_handle *find_alloced_obj(struct size_class *class,
>  	offset += class->size * index;
>  
>  	while (offset < page_size) {
> -		if (obj_allocated(zpdesc, addr + offset, &handle))
> +		handle = obj_allocated(zpdesc, addr + offset);
> +		if (handle)
>  			break;
>  
>  		offset += class->size;

[Severity: Low]
Should this find_alloced_obj() change be folded into the preceding commit
"mm: zsmalloc: support objects compressed based on multiple pages" so the
series stays bisectable?

Also, the commit message ends with "The following data illustrates the
time and compressed data for typical anonymous pages gathered from Android
phones." without including the data table, and multi_pages_debug_stat_show()
is missing a colon or space before "%8llu" on the failed and partial count
lines.

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


  reply	other threads:[~2026-11-16  2:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15  2:44 [RFC PATCH v4 0/4] mTHP-friendly compression in zsmalloc and zram based on multi-pages Nanzhe Zhao
2026-11-15  2:44 ` [RFC PATCH v4 1/4] mm: zsmalloc: support objects compressed based on multiple pages Nanzhe Zhao
2026-11-16  2:46   ` sashiko-bot
2026-11-15  2:44 ` [RFC PATCH v4 2/4] zram: support compression at the granularity of multi-pages Nanzhe Zhao
2026-11-16  2:46   ` sashiko-bot [this message]
2026-11-15  2:44 ` [RFC PATCH v4 3/4] zram: backend_zstd: use multi-page size for zstd parameter setup Nanzhe Zhao
2026-11-15  2:44 ` [RFC PATCH v4 4/4] mm: swap: allow direct reclaim for synchronous multi-page swapin Nanzhe Zhao
2026-11-16  2:46   ` 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-216089@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