From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F249D606A321 for ; Sun, 6 Dec 2026 02:20:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=WROFuTf9 Received: by kanga.kvack.org (Postfix) id E658D6B05B1; Sat, 5 Dec 2026 21:20:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E3CBA6B05B2; Sat, 5 Dec 2026 21:20:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C41166B05B3; Sat, 5 Dec 2026 21:20:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 47CE56B05B1 for ; Sat, 5 Dec 2026 21:20:13 -0500 (EST) Received: from smtpin16.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 536BEA04A7 for ; Sun, 6 Dec 2026 02:20:12 +0000 (UTC) X-FDA: 85511953464.16.24DB184 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf21.hostedemail.com (Postfix) with ESMTP id 98C1E1C0004 for ; Sun, 6 Dec 2026 02:20:10 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=WROFuTf9; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf21.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796523610; b=MWUopcLDeZ6YjH7fsLei772Ucybq4kN3MeOtRdPAVravCpR0AbD+UKREpt4xgndTmjjFQw czFhVajzuKMqYSuliGoJTLF1jmQw3lcZEp6fKM/GkiL0ewk/gZB3CBe6L26i+gk64K1q21 3xo3864vN84/7CudiRfkmfd4pf0OTDU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796523610; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7luIxOVA1vM2mRINFz/aZ0AYfU05e5x5LBJcsoqHxBs=; b=Qo+eSXdhMtbS5McffqOytp7XIbw2bCzqaXDEL98QnUWI+eEd58VaOV7FnVXzHSWDSmTPic Ajezyqnr4+ubRYZ3e4jDUFwHA/nnnI8+R/hHFXofcJkoMx3Cr5k/YZecUPKzSw7/WvakEG eleYnQ7CmcT7xPwiXc+q+AkdjkFCbV4= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=WROFuTf9; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf21.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id E1CE04374D; Sun, 6 Dec 2026 02:20:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F28721F0093B; Sun, 6 Dec 2026 02:20:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796523609; bh=7luIxOVA1vM2mRINFz/aZ0AYfU05e5x5LBJcsoqHxBs=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=WROFuTf9XpsyCIV/9DvDXle7CCZZ1LnJZCnqM3mvQqq9QOS4fcPlSipWFLoD62eN0 tkfeo1rCW+W/5VAu2GbwkjvDkSqanRZHTLZ7UjQ4r93ypM74PBLY6xHBMoE+ks4/Yx EJdv5Zw2xZ5z7L3GZ4eB68es1OQ8B/K19OOiDRxUHPDwwWHFSwPscuoWMXvVAP2R/p 2aUq3rLaEX2kX7dKsd7QCS0RsEOMyvXq14jm+j7BhVJkLvJA7IbQoeaSMBXoJW5hvi //R0S2z6RCIFmw91h+tn9Tq6ceKaFshuegExX8jjN1OADt3bNLD+xM43uSKnOm8DqZ 4oVKdJ8/y15sQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 2/4] zram: support compression at the granularity of multi-pages Reply-To: sashiko-reviews@lists.linux.dev To: "Nanzhe Zhao" , "Andrew Morton" Cc: linux-mm@kvack.org, "Jens Axboe" , "Sergey Senozhatsky" , "Minchan Kim" , "Kairui Song" , "Johannes Weiner" , "Nhat Pham" , "Usama Arif" , "Yosry Ahmed" , "Chengming Zhou" , "Chris Li" , "David Hildenbrand" , "Ying Huang" , "Suren Baghdasaryan" , "Ryan Roberts" , "Kanchana P Sridhar" , "Wajdi K Feghali" , "Bala Seshasayee" , "Nick Terrell" , "Barry Song" , "Tangquan Zheng" , "Bo Zhang" In-Reply-To: <20261205021803.3704464-3-zhaonanzhe@xiaomi.com> References: <20261205021803.3704464-1-zhaonanzhe@xiaomi.com> <20261205021803.3704464-3-zhaonanzhe@xiaomi.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sun, 06 Dec 2026 02:20:08 +0000 X-Rspamd-Queue-Id: 98C1E1C0004 X-Stat-Signature: eb8nbqo5b5zb91ckoqswcu1is3zuniqq X-Rspam-User: X-Spam-Status: No, score=-11.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Server: rspam08 X-HE-Tag: 1796523610-784034 X-HE-Meta: U2FsdGVkX1/LF0HQyGBHZghBJ45kPxBzPr5aZOusUdNgSO5uEuAGsown3JlFSKap1q4yox5EzsDpe4AocYzC0LR8k361trrrvrCZu3gXMsX7csf7HPIJFopDL8R/H+k3POcdE8Ic+XasFjaB/2+QxW3NDR4muT6aNwwjJqhcpPrjvjbRfcrom0Rff59cUyTAX5QFINWTUeB0dui80nUdOWxi07IiweXwkWy96vQuY2p8iW+tt9aiRvQ/6peiGVrEPnNX0wx2xcbSVSmn6m5ztoORyIcQnsmbTyPI8oSUQnJNncri1QYq7Pa7MVmzZ2YMZv54s2k/1YavlvCv9Yp4ZAIjv7E1rVCMw2gZpwQPgB9V2oFDLxP87/NMRPtMC31250e6nb/Jt2CsglzolhNTUuHo7TVlUdZtR5PMeIevlSM+lptBwJTI4A/Cu2TKSG02e6CAEsAyQuPCXhu3pu1qBPlp4S2lOPznPrJ1RI428NddiwGbd/xypr1/WNeOl1rvp5XEahmH21xDeEsi76oSn+RJVmnD0pK8DlUn5DB3Bf7qC3UnhkywHj2fgyvOUnMWCyRqaJ9qM619LtH/6dHnbKb+BCEjguIvgTCenl0n/UqjbMbbWfDebNen5q1XCSILTWpOLFxYC3J7EyGG3drbiwax2W69LtmGPafW6uB5fYoIInIZog8aunW2q+P0xAygifD+cz80ylSbYit9S31yZE3UPAd373Bqnmy68wjCVK5UUpulwTQmmaR5UxTUk7jEmmRE3v3E4d05GELKoFDf4r6EcOcA9tqCHplf+vAAkoq8hjvZ6eS/SNDOnxmemqa8f+wGWrXYHBwxure8HY25pi21xrDyyi0Z3unPL++Vej6XNaG+8c12uxvCZh3iR0Bn5qdoUi8gojmepwiDBk70cyV69O22B35LLg3hRtHz5yGeZSgVleBumz753T1mMr7YBHg0Ky6P4wkvRUKIgKf KhK5ztto 19nLunslreQLUEME0ajZVhW+OVMcnwh5f/mPG3rRjUujzsGuYSTcEJ9wEYZPe/ixSFsvAPm4db4Nqh9uoLmxh0IeJGOSxfHF1vKso390CRC0KndXAY9EwNYsyFqBMo+T4sNi+BNh5z57HBa4QDVmQaAoP+px3OVAYiJRx2CO2hpLk8q8mSHKaj0kw3+XzqNvN30CUfnPYON9/2qmjrvSI2mEG4AGdCSzaWCMHl4LpdUFaxXiGpO6hAh6s1CVpsM7GgCNQq2BnpE1XUaXYdICnxyyPuoHsIAnFhYqolxkPjWglBfyD87qC1SR02TEExWMVFYiKzYp40vEEn+ZYsVBJSH62g8j3HpwDUAqBlxYAACcJd3uSnrAdJY9DzgLwwr9WKASomDZVQF3AcwldofabG3lBmssJSafxp29NLWiDosn0DImGvX6kdjnX+zAbLQWeaX8z8Af58k/4xcwcbgWpjDwAdJTymz5tioja38H+D0OC+2aipdp0q1QCdnjaGhFE7AgG+aUAaNvGgXnj/7ZSQPmxMgnuTF3/Mg/vDdj2GaaKu/ZokEPkAczbc7DsznUafF/LzOvetPJZ71U9eavwg0066nlL5MBXfwTeltzIksYGkPFYT5BG0ZBZBFcIVGjh7ldemGGStZ+FTqW0N/wbV34CLwWBCKF61cOnt+DiQqsJAe8ZYUunU9T8LHk+2Y7RP/qXyaRIYHAb5Wg1Z0CCL+UFsgSWJkJ/Sm96Ic5E1K87YWGCAZIjCms5F2bzjsKKaxkHULZhn5lgHF9KRAdCQBZDkmwH+fs5H/1zsMrv3YhPqttqDrujImDGeVTUVQSK806Q2VS1mwp7AMFJyMtSEEpOdtRd8SxdRHUB1QwN7jqIOzw7SZ/v/m/WrGkQbkzBte1pe/3m2AT6XohBhJmjeJh1bGR5VE6NB3fycPNtp+xYEBQ= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: 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 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 =3D zram_multi_pages_head_index(index); > + int nr_pages =3D 1; > + int i; > + > + /* Clear all slots' MULTI flags first. */ > + if (__test_multi_pages_comp(zram, head)) { > + index =3D head; > + nr_pages =3D ZCOMP_MULTI_PAGES_NR; > + for (i =3D 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 =3D 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_pag= es(), 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; > =20 > 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 =3D read_from_zspool(zram, page, index); > + ret =3D 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 =3D 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 =3D __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 =3D page_offset + ((index - head) << PAGE_SHIFT); > if (offset) { > - if (n <=3D (PAGE_SIZE - offset)) > + if (n <=3D page_size - offset) { > + slot_unlock(zram, head); > goto end_bio; > - > - n -=3D (PAGE_SIZE - offset); > - index++; > + } > + n -=3D page_size - offset; > + index =3D 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 =3D=3D PAGE_SIZE, zram_add() sets max_write_zeroes_sectors =3D UINT_MAX and zram_submit_bio() routes REQ_OP_WRITE_ZEROES to zram_bio_discard(). Skipping leading (offset !=3D 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 >=3D PAGE_SIZE) { > - slot_lock(zram, index); > + unsigned long head =3D zram_slot_head_index(zram, index); > + unsigned int page_size; > + > + slot_lock(zram, head); > + page_size =3D 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 -=3D PAGE_SIZE; > + index =3D head + (page_size >> PAGE_SHIFT); > + n -=3D 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 =3D 4 (multi-page 4..7) slot_free(4) (now single-page) slot_lock(zram, 4) page_size =3D PAGE_SIZE slot_free(zram, 6) <-- unlocked slot 6 (races with I/O) slot_unlock(zram, 4) index =3D 4 + 1 =3D 5 <-- moves backwards before start In the reverse single-to-multi race, head =3D=3D 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 =3D bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIF= T; > + > + /* > + * 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 !=3D 1); > + > + if (bio->bi_io_vec->bv_len >=3D ZCOMP_MULTI_PAGES_SIZE) > + return true; > + > + return !!__test_multi_pages_comp(zram, index); > +} [Severity: Medium] Will WARN_ON_ONCE(bio->bi_vcnt !=3D 1) fire on the first swap writeout? swap_bdev_submit_write() initializes the bio with bio_init() (which sets bio->bi_vcnt =3D 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 =3D 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 =3D get_slot_comp_priority(zram, index); > + zstrm =3D zcomp_dstrm_get(zram->comps[prio], > + ZCOMP_DSTRM_EXCLUSIVE); > + ret =3D zcomp_decompress(zram->comps[prio], zstrm, sg, size, > + zstrm->decomp_buf, page_size); > + if (!ret) { > + dst =3D 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 =3D=3D 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 =3D kmalloc(src_len, GFP_NOWAIT | __GFP_NOWARN); if (src) { ret =3D 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 =3D 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 =3D read_from_zspool(zram, page, index, ZCOMP_MULTI_PAGES_SIZE); > + if (!ret) { > + src =3D kmap_local_page(bvec->bv_page); > + dst =3D 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 =3D __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 >=3D 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 =3D bio_start_io_acct(bio); > + int ret =3D -EAGAIN; > + > + if (want_multi_pages_comp(zram, bio)) > + ret =3D zram_bio_write_multi_pages(zram, bio); > + > + /* > + * If the multi-page slot has been freed, fall back to the single-page > + * path. > + */ > + if (ret =3D=3D -EAGAIN) > + ret =3D 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 =3D=3D 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 =3D 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 !=3D 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 =3D 1) CPU1 (BLKDISCARD) zram_slot_head_index(zram, 1) -> head =3D 0 slot_lock(zram, 0) slot_free(zram, 0) slot_unlock(zram, 0) slot_trylock(zram, 0) -> ok __test_multi_pages_comp(0) =3D=3D 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 !=3D index and __test_multi_pages_comp(zram, head) =3D= =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205021803.3704= 464-1-zhaonanzhe@xiaomi.com?part=3D2