From: Baoquan He <baoquan.he@linux.dev>
To: linux-mm@kvack.org
Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org,
kasong@tencent.com, hannes@cmpxchg.org, corbet@lwn.net,
yosry@kernel.org, nphamcs@gmail.com, muchun.song@linux.dev,
baohua@kernel.org, youngjun.park@lge.com, hughd@google.com,
baolin.wang@linux.alibaba.com, baoquan.he@linux.dev,
Baoquan He <hebaoquan@kylinos.cn>
Subject: [PATCH v2 13/32] mm, swap: pass the folio order through the swap slot allocator
Date: Fri, 4 Dec 2026 02:13:00 +0800 [thread overview]
Message-ID: <20261203181319.901844-14-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
The allocation helpers derive the order from the folio. A physical
xswap slot has no folio of its own, so pass the order explicitly.
Callers that have a folio pass folio_order(); hibernation and the
order-0 xswap backend pass 0.
No functional change.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/swapfile.c | 60 ++++++++++++++++++++++++++-------------------------
1 file changed, 31 insertions(+), 29 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 7e0bdde39ab0..195fdc879311 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1170,10 +1170,9 @@ static bool cluster_scan_range(struct swap_info_struct *si,
static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
struct swap_cluster_info *ci,
struct folio *folio,
- unsigned int ci_off)
+ unsigned int ci_off, unsigned int order)
{
- unsigned int order;
- unsigned long nr_pages;
+ unsigned long nr_pages = 1UL << order;
lockdep_assert_held(&ci->lock);
@@ -1192,14 +1191,10 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
* freed by its owner.
*/
if (likely(folio)) {
- order = folio_order(folio);
- nr_pages = 1 << order;
swap_cluster_assert_empty(ci, ci_off, nr_pages, false);
__swap_cache_add_folio(ci, folio, swp_entry(si->type,
ci_off + cluster_offset(si, ci)));
} else {
- order = 0;
- nr_pages = 1;
swap_cluster_assert_empty(ci, ci_off, 1, false);
/* Owned by hibernation or the xswap backend; never cached. */
__swap_table_set(ci, ci_off, SWP_TB_HIB);
@@ -1232,13 +1227,13 @@ 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, bool no_cache)
+ unsigned long offset,
+ unsigned int order, bool no_cache)
{
unsigned long next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER);
- unsigned int order = likely(folio) ? folio_order(folio) : 0;
unsigned long end = start + SWAPFILE_CLUSTER;
- unsigned int nr_pages = 1 << order;
+ unsigned long nr_pages = 1UL << order;
bool need_reclaim, ret, usable;
lockdep_assert_held(&ci->lock);
@@ -1261,7 +1256,8 @@ static unsigned long alloc_swap_scan_cluster(struct swap_info_struct *si,
if (!ret)
continue;
}
- if (!__swap_cluster_alloc_entries(si, ci, folio, offset % SWAPFILE_CLUSTER))
+ if (!__swap_cluster_alloc_entries(si, ci, folio,
+ offset % SWAPFILE_CLUSTER, order))
break;
found = offset;
offset += nr_pages;
@@ -1288,7 +1284,8 @@ 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 no_cache)
+ unsigned int order, bool scan_all,
+ bool no_cache)
{
unsigned long found = SWAP_ENTRY_INVALID;
@@ -1299,7 +1296,7 @@ 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, order,
no_cache);
if (found)
break;
@@ -1386,11 +1383,10 @@ 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, unsigned int order,
bool no_cache)
{
struct swap_cluster_info *ci;
- unsigned int order = likely(folio) ? folio_order(folio) : 0;
unsigned long offset = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
#ifdef CONFIG_XSWAP
bool retried = false;
@@ -1422,7 +1418,7 @@ 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, order,
no_cache);
} else {
swap_cluster_unlock(ci);
@@ -1440,26 +1436,28 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
*/
if ((!folio || no_cache) && order < PMD_ORDER) {
found = alloc_swap_scan_list(si, &si->frag_clusters[order],
- folio, false, no_cache);
+ folio, order, false, no_cache);
if (found)
goto done;
}
if (swap_prefer_free_cluster(si)) {
- found = alloc_swap_scan_list(si, &si->free_clusters, folio, false, no_cache);
+ found = alloc_swap_scan_list(si, &si->free_clusters,
+ folio, order, false, no_cache);
if (found)
goto done;
}
if (order < PMD_ORDER) {
found = alloc_swap_scan_list(si, &si->nonfull_clusters[order],
- folio, true, no_cache);
+ folio, order, true, no_cache);
if (found)
goto done;
}
if (!swap_prefer_free_cluster(si)) {
- found = alloc_swap_scan_list(si, &si->free_clusters, folio, false, no_cache);
+ found = alloc_swap_scan_list(si, &si->free_clusters,
+ folio, order, false, no_cache);
if (found)
goto done;
}
@@ -1475,7 +1473,8 @@ 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, no_cache);
+ found = alloc_swap_scan_list(si, &si->frag_clusters[order],
+ folio, order, false, no_cache);
if (found)
goto done;
}
@@ -1489,11 +1488,13 @@ 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, no_cache);
+ found = alloc_swap_scan_list(si, &si->frag_clusters[o],
+ folio, order, true, no_cache);
if (found)
goto done;
- found = alloc_swap_scan_list(si, &si->nonfull_clusters[o], folio, true, no_cache);
+ found = alloc_swap_scan_list(si, &si->nonfull_clusters[o],
+ folio, order, true, no_cache);
if (found)
goto done;
}
@@ -1726,7 +1727,7 @@ static bool swap_alloc_fast(struct folio *folio, bool may_zswap)
if (cluster_is_usable(ci, order)) {
if (cluster_is_empty(ci))
offset = cluster_offset(si, ci);
- alloc_swap_scan_cluster(si, ci, folio, offset, false);
+ alloc_swap_scan_cluster(si, ci, folio, offset, order, false);
} else {
swap_cluster_unlock(ci);
}
@@ -1753,7 +1754,8 @@ static void swap_alloc_slow(struct folio *folio, bool may_zswap)
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, !may_zswap);
+ cluster_alloc_swap_entry(si, folio, folio_order(folio),
+ !may_zswap);
put_swap_device(si);
if (folio_test_swapcache(folio))
return;
@@ -2639,12 +2641,12 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
ci = swap_cluster_lock(si, pcp_offset);
if (cluster_is_usable(ci, 0))
offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset,
- false);
+ 0, false);
else
swap_cluster_unlock(ci);
}
if (!offset)
- offset = cluster_alloc_swap_entry(si, NULL, false);
+ offset = cluster_alloc_swap_entry(si, NULL, 0, false);
local_unlock(&percpu_swap_cluster.lock);
if (offset)
entry = swp_entry(si->type, offset);
@@ -4687,14 +4689,14 @@ static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si)
ci = swap_cluster_lock(si, pcp_offset);
if (cluster_is_usable(ci, 0))
offset = alloc_swap_scan_cluster(si, ci, NULL,
- pcp_offset,
+ pcp_offset, 0,
true);
else
swap_cluster_unlock(ci);
}
}
if (offset == SWAP_ENTRY_INVALID)
- offset = cluster_alloc_swap_entry(si, NULL, true);
+ offset = cluster_alloc_swap_entry(si, NULL, 0, true);
local_unlock(&percpu_swap_cluster.lock);
return offset;
--
2.54.0
next prev parent reply other threads:[~2026-12-03 18:14 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 18:12 [PATCH v2 00/32] mm, swap: extendable swap devices (xswap phases II-IV) Baoquan He
2026-12-03 18:12 ` [PATCH v2 01/32] mm, swap: prepare the swap IO path for xswap backends Baoquan He
2026-12-03 18:12 ` [PATCH v2 02/32] mm, swap: tag a swap table entry with its owning xswap entry Baoquan He
2026-12-03 18:12 ` [PATCH v2 03/32] mm, swap: prepare the folio-less allocation path for xswap Baoquan He
2026-12-03 18:12 ` [PATCH v2 04/32] mm, swap: add a physical backend for xswap slots Baoquan He
2026-12-03 18:12 ` [PATCH v2 05/32] mm, swap: support swapoff of an xswap physical backend Baoquan He
2026-12-03 18:12 ` [PATCH v2 06/32] mm, swap: use the " Baoquan He
2026-12-03 18:12 ` [PATCH v2 07/32] mm, swap: fall back to disk when zswap refuses an xswap page Baoquan He
2026-12-03 18:12 ` [PATCH v2 08/32] mm, swap: reclaim physical slots backing cache-only xswap entries Baoquan He
2026-12-03 18:12 ` [PATCH v2 09/32] mm, swap: drop swap_folio_sector() Baoquan He
2026-12-03 18:12 ` [PATCH v2 10/32] mm, swap: drop a stale xswap physical slot range before reallocating Baoquan He
2026-12-03 18:12 ` [PATCH v2 11/32] mm, swap: publish the xswap backend owner before its reverse map Baoquan He
2026-12-03 18:12 ` [PATCH v2 12/32] mm, swap: skip a NOFS backend for xswap physical writeback Baoquan He
2026-12-03 18:13 ` Baoquan He [this message]
2026-12-03 18:13 ` [PATCH v2 14/32] mm, swap: allocate a multi-slot range without a folio Baoquan He
2026-12-03 18:13 ` [PATCH v2 15/32] mm, swap: pass the folio order to the xswap physical backend Baoquan He
2026-12-03 18:13 ` [PATCH v2 16/32] mm, swap: enable large xswap folios with a contiguous physical slot range Baoquan He
2026-12-03 18:13 ` [PATCH v2 17/32] mm, swap: enable THP swapin for xswap entries Baoquan He
2026-12-03 18:13 ` [PATCH v2 18/32] mm, swap: split the swap memcg charge helpers Baoquan He
2026-12-03 18:13 ` [PATCH v2 19/32] mm, swap: do not charge zswap-backed xswap entries Baoquan He
2026-12-03 18:13 ` [PATCH v2 20/32] mm, swap: charge an xswap entry when it gets physical backing Baoquan He
2026-12-03 18:13 ` [PATCH v2 21/32] mm, swap: don't gate xswap on the physical swap free count Baoquan He
2026-12-03 18:13 ` [PATCH v2 22/32] mm, swap: do not retake the cluster lock when uncharging an xswap slot Baoquan He
2026-12-03 18:13 ` [PATCH v2 23/32] mm, swap: uncharge an xswap backend where its record is cleared Baoquan He
2026-12-03 18:13 ` [PATCH v2 24/32] mm, swap: account xswap backends for cgroup v1 Baoquan He
2026-12-03 18:13 ` [PATCH v2 25/32] mm, swap: drop a refused xswap backend slot range directly Baoquan He
2026-12-03 18:13 ` [PATCH v2 26/32] mm, swap: route xswap by the zswap eligibility without flushing Baoquan He
2026-12-03 18:13 ` [PATCH v2 27/32] mm, swap: keep a per-cluster backend record for every swap device Baoquan He
2026-12-03 18:13 ` [PATCH v2 28/32] mm, swap: encode the per-slot backend record Baoquan He
2026-12-03 18:13 ` [PATCH v2 29/32] mm, swap: add accessors for the " Baoquan He
2026-12-03 18:13 ` [PATCH v2 30/32] mm, zswap: index entries in the swap device's backend table Baoquan He
2026-12-03 18:13 ` [PATCH v2 31/32] mm, swap: release a range of backend handles at once Baoquan He
2026-12-03 18:13 ` [PATCH v2 32/32] mm, swap: only run the xswap backend hooks on xswap devices Baoquan He
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203181319.901844-14-baoquan.he@linux.dev \
--to=baoquan.he@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=chrisl@kernel.org \
--cc=corbet@lwn.net \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hebaoquan@kylinos.cn \
--cc=hughd@google.com \
--cc=kasong@tencent.com \
--cc=linux-mm@kvack.org \
--cc=muchun.song@linux.dev \
--cc=nphamcs@gmail.com \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox