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 82373606A322 for ; Fri, 4 Dec 2026 16:49:17 +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=hGcAMsLU Received: by kanga.kvack.org (Postfix) id 7B5D36B05C0; Fri, 4 Dec 2026 11:48:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7662F6B05C1; Fri, 4 Dec 2026 11:48:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 654EA6B05C2; Fri, 4 Dec 2026 11:48:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3079B6B05C0 for ; Fri, 4 Dec 2026 11:48:33 -0500 (EST) Received: from smtpin04.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B25CAC051F for ; Fri, 4 Dec 2026 16:48:32 +0000 (UTC) X-FDA: 85506884064.04.21699E8 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf17.hostedemail.com (Postfix) with ESMTP id 0E1A04000A for ; Fri, 4 Dec 2026 16:48:30 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=hGcAMsLU; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf17.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=1796402911; b=NQAkOIeEGvLB0dvUHHOq69xi3BuqQntgcQI71qLQBUnfJJeVLqM/MkiH3naoXkjniemWzI jjyiz+nGwZU2msDUFQeiM7sMFj45KNNvVdMGSCmv0bYF3mX8euDMxmDK+Zjh3s62dPoXoj TeLyXDZ3WeAWt+uXIstYitt6MV/odAw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796402911; 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=2d3QdtuRKg6fx/BVhjZi2eIKNHhJCTgL3tSN8nvi/i0=; b=FBDGLPx7DxmrvQJD9KQGAPQ0JtsUTpvKvvNJuSUgKKbJKVQ+u1pN/0i6ghXKyRCsg5Unsd Q9qKluRZqQ0Sclvt3p9T5RfT5QYkfSY0uEzwqXqM+F0ARVm+7QYhZj0MhsXlAXuIC4FuFN c95Ge13ZVOnZj+0dxd+1GbVl+3ixg5w= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=hGcAMsLU; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf17.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 5D2AF43E96; Fri, 4 Dec 2026 16:48:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 663241F00937; Fri, 4 Dec 2026 16:48:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796402909; bh=2d3QdtuRKg6fx/BVhjZi2eIKNHhJCTgL3tSN8nvi/i0=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=hGcAMsLUbf+8PObOxHSweJXHafcmZOGpMjyfEKAgz9zf2SZdW6JyD993U0zvpO+54 wNNmeZKC+G9YqGh1R+5+osWRvUxx3YnNAnclPH4eFPbod99G1iLOMLClZqbv8dk0I6 Y27XOo4DFcE0na/GccPpj1JfO0cWVV5IYXP9cM0idkZzFPjdsvGDyA+8Fmf2x6KMj4 KwmZycgbfw6e0gDlpBsbEu8opI3UYDTaT66ZLWheKyP6wvorkHBAwXZqy8dFhZ95Mp jXA+Qcj5PNmhn92BKgWU7q9fTzwRnkmN7oxkPyLhfo9U8s2c5hWIQHglXMhfAWJIg1 NaH8jYCXH0EpQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Reply-To: sashiko-reviews@lists.linux.dev To: "Baoquan He" , linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@kernel.org, corbet@lwn.net, chrisl@kernel.org, kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org, yosry@kernel.org, nphamcs@gmail.com, baohua@kernel.org, youngjun.park@lge.com, kunwu.chan@gmail.com, klarasmodin@gmail.com, "Baoquan He" In-Reply-To: <20261203164336.864691-14-baoquan.he@linux.dev> References: <20261203164336.864691-1-baoquan.he@linux.dev> <20261203164336.864691-14-baoquan.he@linux.dev> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 16:48:29 +0000 X-Rspamd-Server: rspam11 X-Rspam-User: X-Stat-Signature: krx3t543kxgrb488z9wwr313wwb8whzb X-Spam-Status: No, score=-16.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Queue-Id: 0E1A04000A X-HE-Tag: 1796402910-124382 X-HE-Meta: U2FsdGVkX1/GrVQXAy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qDtxRqkyYgBn0tb+K7bb9vdbRbLRfr/I3KlxMn9brPg01Psvkve9CRqYtTFHlGwrz3OYn7j9fLxpxgzc1hb31kLgs6YAXeBjlVF9lqYssoxiAYj1HGU3HqlOdlGaMUjkSI5R9iZci66Pb3uWbcFWq8ULGN/9EQl7bxK1IvADKMLd8yWj7dDptsfLY9keVZR6B6yHM9GBOKDmar3DDSC+ecjFMPXkuwlGZPG92vEmOIO298OsQeIO2asmwwTQ2wg0Q1BXo0QqJnnsg8mb71P2bIk1yuObN7K/kYg7rE/x1k4zqQofn8Eu3yQxv4odq0cWIw7RaUST+Vgrfz3bRy+CDFHFxtOHXXqrRBjMmI5KsANcVhgSrQ5J8iNh2Ab0YUG/KEyEIIz9AIJXCpgWh+fmRmIc38a7xg6h+9nNnM20SLuNwIDx+iDa2BAHh1tnB1emf67Lu9/u/smk8SCIckTk/V3ijDsz1M9WDabE5fz/4rd/NkuXtSQ8ajhzaDLNDIK4FyiUISYb3PnYMEcb0gUccXLCRVue3BxzaLGZByhYpdF/592H8KHuEIlJXkWl1jpw11TxHv4G4TmUAgyNAqgvEKXR+ZLzsuQvLQU3cDw0nmuc/khS8rj8/tJGsfUx0HgTUWigAQJmAsSZRKGeQdmxsV+h9K6yxtK4Zhu+AHN2AoCGSaANlADUn3I41PruZWGmn7lCNiM97JMUDeA3kUOcYHZbEdjVdf4XP4ILip9EXxU1/lCNTKjbObofdsQ= 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 6 potential issues to consider: - [High] mm, swap: no_cache disables SSD per-CPU caching causing O(N^2) cluster scans - [Medium] mm, swap: no_cache guard still overwrites percpu_swap_cluster on stale offset - [Medium] mm, swap: no_cache list order fragments HDDs and skips nonfull_clusters - [Medium] mm: folio_alloc_swap() forces redundant global css_rstat_flush() per folio - [Medium] mm: failed order-0 folio_alloc_swap() contends on swap_lock and swap_avail_lock - [Low] mm, swap: commit message omits no_cache caching and frag_clusters changes -- commit 177a3ab0dbe1d28a173ce9251f7f626766cd240d Author: Baoquan He > mm, swap: do not give an xswap slot to a cgroup that cannot zswap > > At this point, an xswap slot holds only a zswap-backed page. It has no > storage of its own. If the folio's cgroup cannot take another zswap > page, giving it an xswap slot is wasted: zswap refuses the page and the > slot cannot hold it. > > Skip xswap devices when the cgroup cannot zswap, and try a physical swap > device instead. > > The check is obj_cgroup_may_zswap(): zswap_store() uses the same check > but first tries to shrink the memcg, while the allocation-time check does > not, so it is stricter. > > Signed-off-by: Baoquan He [Severity: Low] The commit message only describes skipping xswap devices via obj_cgroup_may_zswap(), omitting the no_cache plumbing, the percpu_swap_cluster bypass on SSDs, and the frag_clusters-first policy in cluster_alloc_swap_entry(). Could the changelog explain these allocator changes and the rationale behind them? > diff --git a/mm/swapfile.c b/mm/swapfile.c > index b2d24e1a24f08..6c1cfa0d693b5 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c [ ... ] > @@ -1240,6 +1242,10 @@ static unsigned long alloc_swap_scan_cluster(struc= t swap_info_struct *si, > relocate_cluster(si, ci); > swap_cluster_unlock(ci); > if (si->flags & SWP_SOLIDSTATE) { > + if (no_cache && > + (this_cpu_read(percpu_swap_cluster.si[order]) !=3D si || > + !this_cpu_read(percpu_swap_cluster.offset[order]))) > + return found; > this_cpu_write(percpu_swap_cluster.offset[order], next); > this_cpu_write(percpu_swap_cluster.si[order], si); > } else { [Severity: Medium] Can this no_cache guard still overwrite percpu_swap_cluster when si[order] already matches si and offset[order] is non-zero? If this CPU previously cached physical device si and that cluster later became unusable, swap_alloc_fast() sees !cluster_is_usable(ci, order) and returns false without clearing offset[order]. The following swap_alloc_slow() with no_cache set then passes this check and caches a fresh cluster on si, so subsequent may_zswap folios on this CPU bypass higher-priority xswap via swap_alloc_fast(). Also, swap_cluster_populate() drops percpu_swap_cluster.lock across a sleeping table allocation, so CPU migration can land on another CPU that already caches si with a non-zero offset and clobber its cached offset. Returning early whenever no_cache is true avoids both cases. [ ... ] > @@ -1376,20 +1385,33 @@ static unsigned long cluster_alloc_swap_entry(str= uct swap_info_struct *si, > } > =20 > new_cluster: > + /* > + * A no_cache allocation cannot keep a per-CPU cluster, so pack it > + * into a cluster already in use and leave the free ones to > + * allocations that can use a whole one. > + */ > + if (no_cache && order < PMD_ORDER) { > + found =3D alloc_swap_scan_list(si, &si->frag_clusters[order], > + folio, false, no_cache); > + if (found) > + goto done; > + } > + > if (swap_prefer_free_cluster(si)) { > - found =3D alloc_swap_scan_list(si, &si->free_clusters, folio, false); > + found =3D alloc_swap_scan_list(si, &si->free_clusters, folio, false, n= o_cache); > if (found) > goto done; > } > =20 > if (order < PMD_ORDER) { > - found =3D alloc_swap_scan_list(si, &si->nonfull_clusters[order], folio= , true); > + found =3D alloc_swap_scan_list(si, &si->nonfull_clusters[order], > + folio, true, no_cache); > if (found) > goto done; > } [Severity: Medium] Does this no_cache branch fragment rotational swap devices by scanning frag_clusters[order] ahead of free_clusters? For non-SWP_SOLIDSTATE devices, alloc_swap_scan_cluster() ignores no_cache and still caches next in si->global_cluster->next[order], pinning a fragmented cluster for HDDs. Also, when frag_clusters[order] is empty and swap_prefer_free_cluster(si) is true, this takes a fresh cluster from free_clusters before checking nonfull_clusters[order]. Checking SWP_SOLIDSTATE here and scanning nonfull_clusters before free_clusters under no_cache would fix both. [ ... ] > @@ -1657,18 +1688,24 @@ static bool swap_alloc_fast(struct folio *folio) > /* Rotate the device and switch to a new cluster */ > -static void swap_alloc_slow(struct folio *folio) > +static void swap_alloc_slow(struct folio *folio, bool may_zswap) > { > struct swap_info_struct *si, *next; > =20 > spin_lock(&swap_avail_lock); > start_over: > plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) { > + /* > + * Do not rotate a skipped device: the walk follows the > + * rotation and would spin forever. > + */ > + if (!may_zswap && (si->flags & SWP_XSWAP)) > + continue; > /* Rotate the device and switch to a new cluster */ > plist_requeue(&si->avail_list, &swap_avail_head); > spin_unlock(&swap_avail_lock); > if (get_swap_device_info(si)) { > - cluster_alloc_swap_entry(si, folio); > + cluster_alloc_swap_entry(si, folio, !may_zswap); > put_swap_device(si); > if (folio_test_swapcache(folio)) > return; [Severity: High] Does passing no_cache =3D !may_zswap unconditionally cause a severe swapout throughput regression on physical SSD swap devices for cgroups that cannot zswap? Once percpu_swap_cluster is empty, points to xswap, or reaches offset 0 on a physical SSD, swap_alloc_fast() misses and alloc_swap_scan_cluster() discards next under no_cache, permanently trapping later !may_zswap folios on the slow path even when the physical SSD has higher priority than xswap. Every folio then takes global swap_avail_lock twice (in real_swap_has_room() and swap_alloc_slow()), takes si->lock twice (in isolate_lock_cluster() and relocate_cluster()), and rescans frag_clusters[order] from slot 0 in alloc_swap_scan_list(). Filling a 512-slot cluster degrades to O(SWAPFILE_CLUSTER^2) slot checks under ci->lock, and tail insertion in move_cluster() rotates consecutive allocations across different fragmented clusters, breaking sequential swapout BIO merging. [ ... ] > @@ -2002,10 +2067,21 @@ int folio_alloc_swap(struct folio *folio) > { > unsigned int order =3D folio_order(folio); > unsigned int size =3D 1 << order; > + bool may_zswap =3D true; > + bool zswap_checked =3D false; > =20 > VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); > VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio); > =20 > + /* > + * A folio zswap will not take must not get an xswap slot; without > + * a device to route away from, skip the lookup. > + */ > + if (atomic_read(&nr_xswap_files)) { > + may_zswap =3D folio_may_zswap(folio); > + zswap_checked =3D true; > + } [Severity: Medium] Does calling folio_may_zswap() on every folio here double forced cross-CPU rstat flushes during swapout? When an xswap device is active and an ancestor memcg has a finite non-zero memory.zswap.max, obj_cgroup_may_zswap() forces css_rstat_flush(), taking the global memory cgroup rstat lock with IRQs disabled across all possible CPUs. Since zswap_store() still calls obj_cgroup_may_zswap() in swap_writeout(), every swapped folio triggers two forced global rstat flushes, plus extra flushes on zero-filled folios, large-folio split retries, and failed swap allocations. [ ... ] > @@ -2025,16 +2101,36 @@ int folio_alloc_swap(struct folio *folio) > again: > - local_lock(&percpu_swap_cluster.lock); > - if (!swap_alloc_fast(folio)) > - swap_alloc_slow(folio); > - local_unlock(&percpu_swap_cluster.lock); > + /* > + * A folio that cannot zswap can only use a real device; skip the > + * scan when none has room. The failure is still reported below. > + */ > + if (may_zswap || real_swap_has_room()) { > + local_lock(&percpu_swap_cluster.lock); > + if (!swap_alloc_fast(folio, may_zswap)) > + swap_alloc_slow(folio, may_zswap); > + local_unlock(&percpu_swap_cluster.lock); > + } > =20 > if (!order && unlikely(!folio_test_swapcache(folio))) { > if (swap_sync_discard()) > goto again; > } [ ... ] > @@ -2052,6 +2148,13 @@ int folio_alloc_swap(struct folio *folio) > if (mem_cgroup_get_folio_swap_margin(folio) <=3D 0) > return -ENOMEM; > =20 > + /* > + * The folio can only use a real device and none has room; splitting > + * cannot help. > + */ > + if (!may_zswap && !real_swap_has_room()) > + return -ENOMEM; > + > return order ? -E2BIG : -ENOMEM; > } [Severity: Medium] Can failed order-0 allocations for !may_zswap cgroups contend heavily on swap_lock and swap_avail_lock when no real swap device has room? Because get_nr_swap_pages() still counts free xswap space, vmscan keeps calling folio_alloc_swap() on every anonymous folio in a cgroup that cannot zswap even on an xswap-only host. Each failed order-0 call takes global swap_avail_lock in real_swap_has_room() without checking nr_real_swapfiles first, then unconditionally runs swap_sync_discard() under global swap_lock even though xswap never sets SWP_PAGE_DISCARD. At the failed label, real_swap_has_room() takes swap_avail_lock a second time even when order is 0, where the next line already returns -ENOMEM. Gating this check on order and fast-pathing nr_real_swapfiles =3D=3D 0 for = both real_swap_has_room() and swap_sync_discard() would avoid the contention. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203164336.8646= 91-1-baoquan.he@linux.dev?part=3D13