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 7DCD5606A32D for ; Thu, 3 Dec 2026 16:45:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=RDNey35C Received: by kanga.kvack.org (Postfix) id 7E3826B00EF; Thu, 3 Dec 2026 11:44:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7941B6B05C7; Thu, 3 Dec 2026 11:44:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6AA2F6B05C8; Thu, 3 Dec 2026 11:44:50 -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 3001C6B00EF for ; Thu, 3 Dec 2026 11:44:50 -0500 (EST) Received: from smtpin22.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A98B5A0723 for ; Thu, 3 Dec 2026 16:44:49 +0000 (UTC) X-FDA: 85503245898.22.D43520D Received: from mta0.migadu.com (out-107.mta0.migadu.com [91.218.175.107]) by imf10.hostedemail.com (Postfix) with ESMTP id B15D9C000D for ; Thu, 3 Dec 2026 16:44:47 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=RDNey35C; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf10.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.107 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796316288; b=lbqJbZ3DLG77KO7bet8OU+9Hm+aKJcmtp4KBL83JEV5XT7pyEq6mcq/O7Xu12x+49y7zCL 1qSO8GQrW+cSXEgf+5Ku3jBiEWwGUXZA50QL0zK+eLikXP6bokQUrmmOLKFsdj/i1XR/nn T7VDAlnXVf1R5WArqvqfV/9Dx4/v7aI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796316288; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=u+qDCWOv9P3pH2+zLv8U07tsvedXxTQHaZLyObiJPmw=; b=uXtYbcyjWIlGLVI6OcSOMn0fHb7eJLbUPGav/G4M6pb2t0p01dQ3md2eEEPLFDNc8NrlNW tdrG3vbPm9eXaRsRl1r4hfrEYTd2KV14i7zWiLwoKc+v6z7StG/K8pdtXAAeUG+ricQnit 3EpLBNq3iJvaUQoNk1fprbGRMySGLsg= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=RDNey35C; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf10.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.107 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=K+BPDcRib3DgBzwsGqYXkkfyoEsiyZpuhDWxO4y35pY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796316286; v=1; x=1796921086; b=RDNey35C0nU4+7Ew1+ECPbc1XuMSFC5ODEiIj/sW10mjWzOnnF8btmqAufF8J8C/hmbNzbvQ gQvPEslMhn22oyJYz+hVrvuPRZUUFjviAEh+4Maz9XA5gTGnvRuCJv34y5CihcBGngHbDMfW5k2 1t1us/11ZCY6ozJ6uaOmZyFQ= X-Envelope-To: linux-mm@kvack.org Received: by mta11.migadu.com with ESMTPS id e9d29a5d742bf6cd; Thu, 03 Dec 2026 16:44:46 +0000 X-Mizu-Trace-ID: e9d29a5d742bf6cd X-Migadu-Flow: FLOW_OUT From: Baoquan He To: 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@linux.dev, Baoquan He Subject: [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Date: Fri, 4 Dec 2026 00:43:35 +0800 Message-ID: <20261203164336.864691-14-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261203164336.864691-1-baoquan.he@linux.dev> References: <20261203164336.864691-1-baoquan.he@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam05 X-Rspam-User: X-Rspamd-Queue-Id: B15D9C000D X-Stat-Signature: p3rme16hxefseazg44mikrkjw556xehz X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796316287-314746 X-HE-Meta: U2FsdGVkX18B0IeEy4DHuZaWibKkwEzMf1q9h/dMLesB+l3rlcbG6Bu7jYBIjDGGxFnSi1oU2X+jyjkRDssWL5gPsYnjGKr3UabpYFiAq5xQvpftaad4eGVJXILpBhi8WPoxdsgo21imk/7hWUQ9qCSKfeWDjI0H8cID5x1es4mwTd1vKj1XYgZD241mz39UfEcWTqLeGwtILiZ1ffUd9oeQ8ko3R4LESPCVrQDU56ZcCKWdsAoLx4174Ae24E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QTrjXlvstrHs9Ed/hjkEXMQoTkc1KiuFV+zGmDJ7Z5qkN+BThqIlwg3VgV+B1s8zW0Ya6KdpsEleSjOGLDgPbQ+0j+zhcbBd26Pye9yeYqLrZRBas2EtBDwQp97YAhhsVuhJP0qjYOao9Y+J6cbJeYuT5i/Ds6xcnjr7yNYIuHUXrxzCete+fczvmock13Gv/lHcq7M3MU= 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 From: Baoquan He 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 --- mm/swapfile.c | 150 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 127 insertions(+), 23 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index b2d24e1a24f0..6c1cfa0d693b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -228,6 +228,8 @@ static DEFINE_SPINLOCK(swap_lock); static unsigned int nr_swapfiles; atomic_long_t nr_swap_pages; atomic_t nr_real_swapfiles; +/* Active xswap devices, as in nr_real_swapfiles. */ +static atomic_t nr_xswap_files; /* * Some modules use swappable objects and may try to swap them out under * memory pressure (via the shrinker). Before doing so, they may wish to @@ -1199,7 +1201,7 @@ static bool swap_prefer_free_cluster(struct swap_info_struct *si) static unsigned long alloc_swap_scan_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci, struct folio *folio, - unsigned long offset) + unsigned long offset, bool no_cache) { unsigned long next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID; unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER); @@ -1240,6 +1242,10 @@ static unsigned long alloc_swap_scan_cluster(struct 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]) != 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 { @@ -1251,7 +1257,7 @@ static unsigned long alloc_swap_scan_cluster(struct swap_info_struct *si, static unsigned long alloc_swap_scan_list(struct swap_info_struct *si, struct list_head *list, struct folio *folio, - bool scan_all) + bool scan_all, bool no_cache) { unsigned long found = SWAP_ENTRY_INVALID; @@ -1262,7 +1268,8 @@ static unsigned long alloc_swap_scan_list(struct swap_info_struct *si, if (!ci) break; offset = cluster_offset(si, ci); - found = alloc_swap_scan_cluster(si, ci, folio, offset); + found = alloc_swap_scan_cluster(si, ci, folio, offset, + no_cache); if (found) break; } while (scan_all); @@ -1332,7 +1339,8 @@ static void swap_reclaim_work(struct work_struct *work) * cluster for current CPU too. */ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, - struct folio *folio) + struct folio *folio, + bool no_cache) { struct swap_cluster_info *ci; unsigned int order = likely(folio) ? folio_order(folio) : 0; @@ -1367,7 +1375,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, if (cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset = cluster_offset(si, ci); - found = alloc_swap_scan_cluster(si, ci, folio, offset); + found = alloc_swap_scan_cluster(si, ci, folio, offset, + no_cache); } else { swap_cluster_unlock(ci); } @@ -1376,20 +1385,33 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, } 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 = alloc_swap_scan_list(si, &si->frag_clusters[order], + folio, false, no_cache); + if (found) + goto done; + } + if (swap_prefer_free_cluster(si)) { - found = alloc_swap_scan_list(si, &si->free_clusters, folio, false); + found = alloc_swap_scan_list(si, &si->free_clusters, folio, false, no_cache); if (found) goto done; } if (order < PMD_ORDER) { - found = alloc_swap_scan_list(si, &si->nonfull_clusters[order], folio, true); + found = alloc_swap_scan_list(si, &si->nonfull_clusters[order], + folio, true, no_cache); if (found) goto done; } if (!swap_prefer_free_cluster(si)) { - found = alloc_swap_scan_list(si, &si->free_clusters, folio, false); + found = alloc_swap_scan_list(si, &si->free_clusters, folio, false, no_cache); if (found) goto done; } @@ -1405,7 +1427,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, * failure is not critical. Scanning one cluster still * keeps the list rotated and reclaimed (for clean swap cache). */ - found = alloc_swap_scan_list(si, &si->frag_clusters[order], folio, false); + found = alloc_swap_scan_list(si, &si->frag_clusters[order], folio, false, no_cache); if (found) goto done; } @@ -1419,11 +1441,11 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, * Clusters here have at least one usable slots and can't fail order 0 * allocation, but reclaim may drop si->lock and race with another user. */ - found = alloc_swap_scan_list(si, &si->frag_clusters[o], folio, true); + found = alloc_swap_scan_list(si, &si->frag_clusters[o], folio, true, no_cache); if (found) goto done; - found = alloc_swap_scan_list(si, &si->nonfull_clusters[o], folio, true); + found = alloc_swap_scan_list(si, &si->nonfull_clusters[o], folio, true, no_cache); if (found) goto done; } @@ -1467,7 +1489,9 @@ static void del_from_avail_list(struct swap_info_struct *si, bool swapoff) lockdep_assert_held(&si->lock); si->flags &= ~SWP_WRITEOK; /* Count active devices, not merely those on the avail list. */ - if (!(si->flags & SWP_XSWAP)) + if (si->flags & SWP_XSWAP) + atomic_sub(1, &nr_xswap_files); + else atomic_sub(1, &nr_real_swapfiles); atomic_long_or(SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages); } else { @@ -1503,7 +1527,9 @@ static void add_to_avail_list(struct swap_info_struct *si, bool swapon) lockdep_assert_held(&si->lock); si->flags |= SWP_WRITEOK; /* A full device is still active and skips the add below. */ - if (!(si->flags & SWP_XSWAP)) + if (si->flags & SWP_XSWAP) + atomic_add(1, &nr_xswap_files); + else atomic_add(1, &nr_real_swapfiles); } else { if (!(READ_ONCE(si->flags) & SWP_WRITEOK)) @@ -1627,7 +1653,7 @@ static bool get_swap_device_info(struct swap_info_struct *si) * Fast path try to get swap entries with specified order from current * CPU's swap entry pool (a cluster). */ -static bool swap_alloc_fast(struct folio *folio) +static bool swap_alloc_fast(struct folio *folio, bool may_zswap) { unsigned int order = folio_order(folio); struct swap_cluster_info *ci; @@ -1643,11 +1669,16 @@ static bool swap_alloc_fast(struct folio *folio) if (!si || !offset || !get_swap_device_info(si)) return false; + if (!may_zswap && (si->flags & SWP_XSWAP)) { + put_swap_device(si); + return false; + } + ci = swap_cluster_lock(si, offset); if (cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset = cluster_offset(si, ci); - alloc_swap_scan_cluster(si, ci, folio, offset); + alloc_swap_scan_cluster(si, ci, folio, offset, false); } else { swap_cluster_unlock(ci); } @@ -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; 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; @@ -1986,6 +2023,34 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si, return err; } +/* Can the folio's cgroup take another zswap page? */ +static bool folio_may_zswap(struct folio *folio) +{ + struct obj_cgroup *objcg = get_obj_cgroup_from_folio(folio); + bool may_zswap = !objcg || obj_cgroup_may_zswap(objcg); + + if (objcg) + obj_cgroup_put(objcg); + return may_zswap; +} + +/* Can a non-xswap device still take a slot? */ +static bool real_swap_has_room(void) +{ + struct swap_info_struct *si; + bool room = false; + + spin_lock(&swap_avail_lock); + plist_for_each_entry(si, &swap_avail_head, avail_list) { + if (!(si->flags & SWP_XSWAP)) { + room = true; + break; + } + } + spin_unlock(&swap_avail_lock); + return room; +} + /** * folio_alloc_swap - allocate swap space for a folio * @folio: folio we want to move to swap @@ -2002,10 +2067,21 @@ int folio_alloc_swap(struct folio *folio) { unsigned int order = folio_order(folio); unsigned int size = 1 << order; + bool may_zswap = true; + bool zswap_checked = false; VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio); + /* + * 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 = folio_may_zswap(folio); + zswap_checked = true; + } + if (order) { /* * Reject large allocation when THP_SWAP is disabled. Check below @@ -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); + } if (!order && unlikely(!folio_test_swapcache(folio))) { if (swap_sync_discard()) goto again; } + /* + * The gate was skipped when there was no device; one may have + * appeared since, so ask the slot itself. + */ + if (unlikely(!zswap_checked && folio_test_swapcache(folio) && + (__swap_entry_to_info(folio->swap)->flags & SWP_XSWAP))) { + zswap_checked = true; + may_zswap = folio_may_zswap(folio); + if (!may_zswap) { + swap_cache_del_folio(folio); + goto again; + } + } + /* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */ if (unlikely(mem_cgroup_try_charge_swap(folio))) { swap_cache_del_folio(folio); @@ -2052,6 +2148,13 @@ int folio_alloc_swap(struct folio *folio) if (mem_cgroup_get_folio_swap_margin(folio) <= 0) return -ENOMEM; + /* + * 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; } @@ -2466,12 +2569,13 @@ swp_entry_t swap_alloc_hibernation_slot(int type) if (pcp_si == si && pcp_offset) { ci = swap_cluster_lock(si, pcp_offset); if (cluster_is_usable(ci, 0)) - offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset); + offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset, + false); else swap_cluster_unlock(ci); } if (!offset) - offset = cluster_alloc_swap_entry(si, NULL); + offset = cluster_alloc_swap_entry(si, NULL, false); local_unlock(&percpu_swap_cluster.lock); if (offset) entry = swp_entry(si->type, offset); -- 2.54.0