From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.167 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795964966; b=goOi87XpOdanqxdBwROxgxtcYztUsaXS8yohF1BkeKz9+VhCh+2EJnuvd+9ayy4iet7Q VrSCWXH4aygzWG/Pog2JgDCp79bL0sgXUj6SlnZkjzwGMHa7hKT48Dr9QL0uORex2RUbz Nw7Slwtc8FlFikLIHbYG4xRZow4FSfr/XYZKPVR/MbylOnHdbmhYUAZxpWU9k7bXyat8b X+NgiKeYphMl8Yklkq6EBuJ+8VJlQIjvTF5VaqRoNMBV8ch4DylxQRq5Ni07LGYihMIlI byQfyXYW5hs0o1g/ViDp6xlsWN4aAR0X8jANziYgMs1MXapzFH72fJ2RaDywSYe/mxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795964966; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version:Content-type: Content-Transfer-Encoding; bh=35GC0YsfDFasyyYlVepzmz1yvZqjnN4NuJbuuT4+fD8=; b=05qzMPEWQjzXdHX2QhnRAMtxs0a0WbUz46G2A3plkGceBwFsWFA75XQxPdSVaHl8hgiX 1O3c1h9yrHO92qKhDnaJbyplOuD+xZstY8LayQKcL5oU6AHtOEAw+ycUcQBxSu7xLLaOC yDY6ngMJyoqTG37PEq5i/gMaXz0/B9po09Z2G/Luo0UtDjj/PRAfRl4cMfx2osQmqlVfS FS5l57nbTui+/oZQdIWP7poOMrSg4IQSt5BHS7jTUY9uQgNi5KV8oMepchOOLOITcw+Y0 4y7pueKmjyFj/NG6OUDZKml5KutAephG+B2FNdm38u6ozsWNYQ5dKbkGhxI+jMwgacQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=cn0ZSq8k; arc=none smtp.remote-ip=91.218.175.167 Received: from mta0.migadu.com (out-167.mta0.migadu.com [91.218.175.167]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C92F606A31C for ; Sun, 29 Nov 2026 15:09:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4C92F606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=cn0ZSq8k X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=9cQsMHmyWE/nvynW3zC5tR4AIm01a1WjEuIMfBBJGDI=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795964963; v=1; x=1796569763; b=cn0ZSq8kOeyVTaUGV/bkc+CwBvuaFXAhP1Rwe8X6yY6MHiT/VE//YihK9FSkNzMlPKv5zHOq 3ObRHvTzhEkSSVH0ffuhs6oiJdB7HPQxPvxe+SOo/KYQ7hBzm2pdyrdoUi2bX9Gpi23Xt2AwDYG 5b1btJkNhbBeomLhINxmUCHs= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta12.migadu.com with ESMTPS id 52a4d604bfe54a11; Sun, 29 Nov 2026 15:09:23 +0000 X-Mizu-Trace-ID: 52a4d604bfe54a11 X-Migadu-Flow: FLOW_OUT From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org, kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org, corbet@lwn.net, nphamcs@gmail.com, baohua@kernel.org, youngjun.park@lge.com, shikemeng@huaweicloud.com, yosry@kernel.org, chengming.zhou@linux.dev, kunwu.chan@gmail.com, klarasmodin@gmail.com, linux-kernel@vger.kernel.org, baoquan.he@linux.dev, Baoquan He Subject: [PATCH v7 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Date: Sun, 29 Nov 2026 23:07:54 +0800 Message-ID: <20261129150755.191662-14-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261129150755.191662-1-baoquan.he@linux.dev> References: <20261129150755.191662-1-baoquan.he@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit 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 | 119 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 97 insertions(+), 22 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 8b2082bdac08..3a84dbd7a54c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -226,6 +226,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 @@ -1197,7 +1199,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); @@ -1238,6 +1240,9 @@ 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) + return found; this_cpu_write(percpu_swap_cluster.offset[order], next); this_cpu_write(percpu_swap_cluster.si[order], si); } else { @@ -1249,7 +1254,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; @@ -1260,7 +1265,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); @@ -1330,7 +1336,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; @@ -1362,7 +1369,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); } @@ -1372,19 +1380,20 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, new_cluster: 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; } @@ -1400,7 +1409,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; } @@ -1414,11 +1423,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; } @@ -1433,7 +1442,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, READ_ONCE(si->nr_clusters_max)) xswap_grow(si, true); found = alloc_swap_scan_list(si, &si->free_clusters, - folio, false); + folio, false, no_cache); } #endif done: @@ -1460,7 +1469,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 { @@ -1496,7 +1507,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)) @@ -1620,7 +1633,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; @@ -1636,11 +1649,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); } @@ -1650,18 +1668,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; @@ -1979,6 +2003,17 @@ 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; +} + /** * folio_alloc_swap - allocate swap space for a folio * @folio: folio we want to move to swap @@ -1995,10 +2030,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 @@ -2017,10 +2063,17 @@ int folio_alloc_swap(struct folio *folio) } } + /* + * Only xswap devices exist and this folio cannot use them; there is + * no slot to find. + */ + if (!may_zswap && !atomic_read(&nr_real_swapfiles)) + goto failed; + again: local_lock(&percpu_swap_cluster.lock); - if (!swap_alloc_fast(folio)) - swap_alloc_slow(folio); + 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))) { @@ -2028,6 +2081,20 @@ int folio_alloc_swap(struct folio *folio) 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); @@ -2045,6 +2112,13 @@ int folio_alloc_swap(struct folio *folio) if (mem_cgroup_get_folio_swap_margin(folio) <= 0) return -ENOMEM; + /* + * Only xswap devices exist and the folio cannot use them; splitting + * cannot help. + */ + if (!may_zswap && !atomic_read(&nr_real_swapfiles)) + return -ENOMEM; + return order ? -E2BIG : -ENOMEM; } @@ -2459,12 +2533,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