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 A7F17606A322 for ; Fri, 4 Dec 2026 22:10:02 +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=gJBrFOe+ Received: by kanga.kvack.org (Postfix) id C7E4B6B05C2; Fri, 4 Dec 2026 17:09:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C55C96B05C3; Fri, 4 Dec 2026 17:09:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B45726B05C4; Fri, 4 Dec 2026 17:09:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 938BC6B05C2 for ; Fri, 4 Dec 2026 17:09:31 -0500 (EST) Received: from smtpin02.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2AE181A03AC for ; Fri, 4 Dec 2026 22:09:31 +0000 (UTC) X-FDA: 85507692942.02.2F33587 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf25.hostedemail.com (Postfix) with ESMTP id 7EC39A0002 for ; Fri, 4 Dec 2026 22:09:29 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=gJBrFOe+; spf=pass (imf25.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796422169; b=ybMqxbqp+MlD3JmRqi7CRi7oUq2GEAWNb+1e/z6xNOAXmZYG0qFfGCzYeljkjPr2NYxR0L yA9FUQEIY5BSZPMSAtIRSFHPMBed82Vr+YwDuHjX+V8r+M34INvE6E8HfJpf4ePT1jKfdJ m18wy39XEIIWXnuvyEo5VWu92nntaJc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796422169; 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=7hXAlU6cftaCh6D4eoF+ntzb1ZZYmElX5BgpszQYUO4=; b=jE5QbkDRavWh1kwmOvCeW2VxN96sXr+DEQDDn1OcxvRrtPKq0gOJSPsXtQARJiBw9WjWkV H4900085H3mYe6l51RP1OgGuT4v5bX6f5Cu/GCs8Bmq3qPtk3obTn73tTLUuL3wgsl2IMQ KhdH5wWcLrs+V8DyCcpUM//mceLMEz4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=gJBrFOe+; spf=pass (imf25.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 1AF7D62294; Fri, 4 Dec 2026 22:09:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86F821F00936; Fri, 4 Dec 2026 22:09:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796422168; bh=7hXAlU6cftaCh6D4eoF+ntzb1ZZYmElX5BgpszQYUO4=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=gJBrFOe+M6bE0eXbpXgaM6P2x7/okfeQ2bNzV87n/Znh+f4RrL0ycvRvrBg8WDwLI Il0jz2dOXH4E34oHtONdehxNOdiXMWqpfcNMqsoUmg0p6tKexnWKpzFMwbuKjGXqan GiYR7AK/L+4wPAqaQYUPLbiuYJ/BHDZr4vxPSP10ncNT66wiAIPmnwuVW8uGn4N2wT R46T9wDVgVE8kFFeV8I4x6blOYh9I6ykFEUI4dLcZccCQcPzCi9hbH5ssIsz/Vdd2o sNEyzf8ISkmEhP0Z5NMRc/bxG2VS+4I+F2RloFrDkBBUwvuUJz9K0sFcb+3Mm7x7/k gi0NsnJTk386g== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries Reply-To: sashiko-reviews@lists.linux.dev To: "Nhat Pham" , linux-mm@kvack.org Cc: "Andrew Morton" , "Chris Li" , "Kairui Song" , "Kemeng Shi" , "Baoquan He" , "Barry Song" , "Youngjun Park" , "Johannes Weiner" , "Yosry Ahmed" , "Chengming Zhou" , "Qi Zheng" , "Shakeel Butt" , "Axel Rasmussen" , "Yuanchu Xie" , "Wei Xu" , "Baolin Wang" , "Michal Hocko" , "David Hildenbrand" , "Liam R. Howlett" , "Vlastimil Babka" , "Mike Rapoport" , "Hugh Dickins" , "Jonathan Corbet" , "Shuah Khan" , "Randy Dunlap" , "Roman Gushchin" , "Muchun Song" , "Arnd Bergmann" , "Karl Mehltretter" , "Dapeng Mi" , "Marco Elver" , "Ethan Nelson-Moore" , =?utf-8?b?Qm9yaXNsYXYgUGV0a292IChBTUQp?= , "Jeff Layton" , "Tejun Heo" , =?utf-8?b?TWljaGFsIEtvdXRuw70=?= In-Reply-To: <20261203220010.1302207-12-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> <20261203220010.1302207-12-nphamcs@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 22:09:27 +0000 X-Rspam-User: X-Rspamd-Queue-Id: 7EC39A0002 X-Rspamd-Server: rspam03 X-Stat-Signature: ugyskhpsdjc44xm9uiaccxdrs8hhcqmo X-Spam-Status: No, score=-11.2 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-HE-Tag: 1796422169-752069 X-HE-Meta: U2FsdGVkX1/VcGTTeOvk2ZNA6u5iUhFNogPtOagPu3Aize9bJAsN94jPmFKkXrmxQNG23CiXGo4P7STLRfQiKZfGiWr41y2utuMNHIUSGuOSKY7B4iVhVwbTu4Bd49WwbdY7q9GaWomOxCvjktWxSel+SkxeO6DmGHHLrc43oP5++VsGow2hv3UtG9ZHRtrIzq3Y/wZw5toFoGOjXcOqea8eh04aNCvkK+++FeZiyEiN3sP+kxnj0sDLQjwIhBz87tFMuRsDb6tweOJXczK6TtEcMdLtpcQKjYFG/XDM0IA8TvFWaLUAeMc1xwKjL6//G8RQkGGqc9dxeyBhygpOPL//9UZRiNOHjjldlD0zm3lz414BmkK9QYR86Rl8VE/L4UVJjsI+alkQ29F7tusuB12afNE1CfFDlrxQjs66kppO9we78BVRm8/ESyL63Gt/Q+gPKN30Z4oEGWi9W2xOxkaQCrMxYFksobccFi45rSi/2AFGgHqYVTUIobrX6O+YEpneEziF4+6fsRbF70GW4/F5//ReYf5FBsHNLMDyVtjcCPcB44/pw7+QHisZgNhnHAGw3qOsCl77qWn2P/4S8tUOAkRQTjo+/wLBCnoMKRjHFtCtKzWMslrBAoSn195SH0ehV/TECZ+qUai+SsfrxsZzUftgRh2rUdVvorV6dfgZeUYQrSmP72kDoa396dZS7gSySHGBqU4mwepqWtbch2Z6Z3dDM/m9PuBYMwoByjwx6viOuKheeKDXYL5xEwW68bgUdxiUfHkSKJxgDizcPdiL8hhCWjZxXnn8FolMCk8sCj3ujsBZNKk+8jJ13BEA1CbcMaKfOyrqvS3SJwCVNceZ9rW9B08BF5TRARf5YnSH52hZ77OJks+w0yx5Pyu9XxQ4yi9PohlSDEiufj63xNlHHVmbGnkb2q1CUcMWQBjozzILUERPuDyYskeKjwG9mJazYSHWCpfLQIa0/Gi /WurrooX vdBKJ/+EBHmJ48dN5WwDn8nUbc69r2XI3WQlhpFNIauiF6Itu8JhnNlZGl8/KHb8NxAjCuMPQizfR5Bi5pfpj90FxFrbOGEO8o/aSC8rKhdbJUbUsivrASdJv6Nrv5HEZnN1lEIL4x6phv+cT2dEritD0LP0jHOnxshC84Ke4NrlLV4woY5HhpfTX6YAvdvbZMvEFYhZKMxR8m4QVzQTIhk9jf9ZgSYWrbzJhU2I6YTOkZDihWHAYdqN4EwBe8eW7XAdsuhp5AZ7YD9ewqGJQSj4FrCxp0Y5G41fdYhZLjRSiPyjfnVYrCGBq4l5YDE5I6PeUgyo/PXhCM4qGsbmnYaFfdPOELNaS/k9tuxq81wPc0AkreMmVumVMoZsKCvgMZoY+zhC9mYKc43IOtgcF9I5WsxkSG/nEbbKUjE7eXlSGd8X3mNTICmdpbetN7ArPNSHn6RIZv5Um59/Q40f4SCIahtoru3yvAtVDRBNIiA2cqMerTziUHGffTs4RyKnvnAdRCyw184w9ic2vU0zBuB+SbJfBeDCKM6qrCwINWL6u5M9tm7nhiAxZ4vwPD3d70KpaaUAVDJFKueo7s8wjaKUcU8D4LsaRTkMfVViOyYigm/EgGUHAeCnKS9wlppf3ffTzr37LjmEr+st3jkqU85P4qUBPJdd451SQ/xnRVAwZLEj3k1aw5dFaHCiKHxJlcC6J4le560wz9yC9goXhfJnE/ZmwBDKl6Rsz+Kcea1Ai9NyV3biKef92SZ7Bx6H/ADekQQlXqMCQlza55JETzUESCXWCARnauN40X9+saND4x2PKJdrzKb39N+gDACsGqKA1PXMu/RRM/cnYE0WBkzipPA== 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 4 potential issues to consider: - [Medium] mm: folio_realloc_swap() omits SWP_RMAP_CACHE_ONLY on zero-count slots - [Medium] mm, swap: try_to_reclaim_vswap_backing() overshoots physical run when i > 0 - [Medium] mm: unconditional cross-cluster atomic RMWs in swap put/dup paths - [Medium] mm: try_to_reclaim_vswap_backing() returns 0 on folio_trylock() failure -- commit a8c95eb317dfaa1f58b41d171ec74555600e4670 Author: Nhat Pham mm, swap: reclaim physical slots backing cache-only vswap entries This commit adds a SWP_RMAP_CACHE_ONLY flag on physical Vswap-tagged swap table entries to mark cache-only vswap slots, and extends swap_reclaim_full_clusters(), cluster_scan_range(), and cluster_reclaim_range() to reclaim their physical backing when swap is more than half full. Link: https://lore.kernel.org/all/sashiko-outbox-181081@kernel.org/ > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 3dca9bd557361..ab65b84a51c7d 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c [ ... ] > @@ -936,6 +941,55 @@ static int swap_cluster_setup_bad_slot(struct swap_i= nfo_struct *si, > return ret; > } > =20 > +/* > + * Try to reclaim a Vswap-tagged physical slot backing a vswap entry. > + * The physical cluster lock must NOT be held. Returns the backing folio= 's > + * page count, negated if the slots could not be reclaimed, or 0 if the > + * folio could not be shown to own @offset (i.e. there is a race). > + */ > +static int try_to_reclaim_vswap_backing(struct swap_info_struct *si, > + unsigned long offset, > + swp_entry_t vswap_entry) > +{ > + swp_entry_t phys_base; > + struct folio *folio; > + unsigned int i; > + int ret; > + > + folio =3D swap_cache_get_folio(vswap_entry); > + if (!folio) > + return 0; > + > + if (!folio_trylock(folio)) { > + /* Unvalidated: the folio may not even own @offset. */ > + folio_put(folio); > + return 0; > + } [Severity: Medium] Should this return a negative value (such as -1) instead of 0 when folio_trylock() fails? Returning 0 conflates a contended locked folio with an already-freed slot. In cluster_reclaim_range(), the loop only breaks on < 0, so a locked folio at the start of an order > 0 range does not abort the scan; it retries every remaining subpage and calls folio_free_swap() on unrelated later folios (dirtying and evicting them from swap cache) even though the locked slot causes the final null-check loop to fail. In swap_reclaim_full_clusters(), returning 0 falls through to offset++, repeating the unlock, XArray lookup in swap_cache_get_folio(), failed folio_trylock(), and relock up to SWAPFILE_CLUSTER times on a locked large folio while holding local_lock(&percpu_swap_cluster.lock). > + if (!folio_matches_swap_entry(folio, vswap_entry)) { > + folio_unlock(folio); > + folio_put(folio); > + return 0; > + } > + > + i =3D vswap_entry.val - folio->swap.val; > + phys_base =3D vswap_to_phys(folio->swap); > + if (!phys_base.val || swp_type(phys_base) !=3D si->type || > + swp_offset(phys_base) + i !=3D offset) { > + folio_unlock(folio); > + folio_put(folio); > + return 0; > + } > + > + /* The run is ours: skip it all, whether or not the free succeeds. */ > + ret =3D folio_nr_pages(folio); > + if (!folio_free_swap(folio)) > + ret =3D -ret; > + folio_unlock(folio); > + folio_put(folio); > + return ret; > +} [Severity: Medium] Can returning folio_nr_pages(folio) here cause swap_reclaim_full_clusters() to overshoot past the end of the folio when i > 0? swap_reclaim_full_clusters() advances by offset +=3D abs(nr_reclaim), which jumps to swp_offset(phys_base) + i + folio_nr_pages(folio) instead of swp_offset(phys_base) + folio_nr_pages(folio) and skips i unrelated slots after the folio. This happens on a partially swapped large vswap folio where subpages [0, i) still have swap_count > 0 and only subpage i has SWP_RMAP_CACHE_ONLY set. Returning folio_nr_pages(folio) - i (negated when folio_free_swap() fails) would advance to the exact end of the folio's physical run. [ ... ] > @@ -2076,6 +2156,51 @@ int folio_alloc_swap(struct folio *folio) > return order ? -E2BIG : -ENOMEM; > } > =20 > +static void vswap_mark_cache_only(struct swap_cluster_info *ci, > + unsigned int ci_off) > +{ > + struct swap_cluster_info_dynamic *ci_dyn; > + struct swap_cluster_info *pci; > + swp_entry_t phys; > + unsigned long vt; > + > + ci_dyn =3D container_of(ci, struct swap_cluster_info_dynamic, ci); > + vt =3D __vtable_get(ci_dyn, ci_off); > + > + if (vtable_type(vt) =3D=3D VSWAP_SWAPFILE) { > + phys =3D vtable_to_phys(vt); > + pci =3D __swap_entry_to_cluster(phys); > + swap_rmap_mark_cache_only(pci, swp_cluster_offset(phys)); > + } > +} [Severity: Medium] Does folio_realloc_swap() leave SWP_RMAP_CACHE_ONLY unset when attaching physical backing to vswap slots that already have swap_count =3D=3D 0? __swap_cluster_alloc_entries() initializes new physical rmap entries with SWP_RMAP_CACHE_ONLY cleared, and vswap_mark_cache_only() only sets it on 1-to-0 transitions when virtual_table already holds VSWAP_SWAPFILE. folio_realloc_swap() installs VSWAP_SWAPFILE under ci->lock without checking whether any slot is already at count 0. This happens deterministically for a partially swapped large vswap folio entering swap_writeout() with some subpages already at swap_count =3D=3D 0, and also on order-0 folios when a concurrent swap_put_entries_cluster() drops swap_count from 1 to 0 before folio_realloc_swap() takes ci->lock: CPU0 CPU1 swap_writeout() folio_free_swap() -> false folio_realloc_swap() folio_alloc_phys_swap() swap_put_entries_cluster() lock(ci->lock) vswap_mark_cache_only() /* VSWAP_NONE = */ count: 1 -> 0 unlock(ci->lock) lock(ci->lock) __vtable_set(..., VSWAP_SWAPFILE) <-- SWP_RMAP_CACHE_ONLY left clear unlock(ci->lock) Because swap_count is already 0, swap_put_entries_cluster() will not run on those slots again, leaving their physical backing invisible to the reclaim scanner while the folio remains in swap cache. Checking for swap_count =3D=3D 0 inside the ci->lock loop in folio_realloc_swap() and calling swap_rmap_mark_cache_only() would close both paths. [ ... ] > diff --git a/mm/vswap.h b/mm/vswap.h > index 080e4bec82802..72b43897faaef 100644 > --- a/mm/vswap.h > +++ b/mm/vswap.h > @@ -43,6 +43,37 @@ static inline bool is_vswap_entry(swp_entry_t entry) > return swap_is_vswap(__swap_entry_to_info(entry)); > } > =20 > +/* > + * Rmap cache-only helpers for physical cluster Vswap-tagged entries. > + * SWP_RMAP_CACHE_ONLY records, inline on the physical swap_table entry, > + * that the backing vswap entry has swap_count =3D=3D 0 (swap-cache-only= , so > + * reclaimable). The physical reclaim scanner reads it directly instead = of > + * chasing the rmap into the vswap layer and paying the cluster-lookup > + * indirection. > + * > + * Callers hold the vswap cluster lock, not the physical one. The rmap is > + * only touched while the vtable holds the slot as SWAPFILE, and that > + * window is opened and closed under the vswap cluster lock, so the > + * allocator has finished writing the entry by then. > + */ > +static inline void swap_rmap_mark_cache_only(struct swap_cluster_info *c= i, > + unsigned int off) > +{ > + atomic_long_t *table; > + > + table =3D rcu_dereference_protected(ci->table, true); > + atomic_long_or(SWP_RMAP_CACHE_ONLY, &table[off]); > +} > + > +static inline void swap_rmap_clear_cache_only(struct swap_cluster_info *= ci, > + unsigned int off) > +{ > + atomic_long_t *table; > + > + table =3D rcu_dereference_protected(ci->table, true); > + atomic_long_and(~SWP_RMAP_CACHE_ONLY, &table[off]); > +} [Severity: Medium] Are unconditional locked atomic RMWs (atomic_long_or() and atomic_long_and(= )) on remote physical cluster tables needed here? As noted in the comment above, all writes to a Vswap-tagged slot during its VSWAP_SWAPFILE window are already serialized by the vswap cluster lock, so full locked RMWs dirty remote cachelines and extend ci->lock hold times unnecessarily compared to checking the bit and using plain stores. On swap-in, shmem_swapin_folio() and do_swap_page() (when should_try_to_free_swap() is true) call folio_put_swap() and immediately delete the folio from swap cache, executing up to 512 remote atomic_long_or= () operations per 2MB THP only to overwrite those slots with NULL right after. On swap-out, vswap_clear_cache_only() adds a second pass in swap_dup_entries_cluster() and runs atomic_long_and() even when SWP_RMAP_CACHE_ONLY is already clear. Once commit "mm/swap: make vswap mandatory" pre-allocates physical backing in folio_alloc_swap(), this hits every initial swapout page on non-zswap systems. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203220010.1302= 207-1-nphamcs@gmail.com?part=3D11