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 6E629606A330 for ; Thu, 3 Dec 2026 18:15:03 +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=Zl2BJk7D Received: by kanga.kvack.org (Postfix) id 96A036B05CD; Thu, 3 Dec 2026 13:15:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 91AD96B05CE; Thu, 3 Dec 2026 13:15:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7BB686B05CF; Thu, 3 Dec 2026 13:15:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 5B7A76B05CD for ; Thu, 3 Dec 2026 13:15:02 -0500 (EST) Received: from smtpin03.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EFA8F806D0 for ; Thu, 3 Dec 2026 18:15:01 +0000 (UTC) X-FDA: 85503473202.03.96222DC Received: from mta0.migadu.com (out-55.mta0.migadu.com [91.218.175.55]) by imf30.hostedemail.com (Postfix) with ESMTP id 000E78000D for ; Thu, 3 Dec 2026 18:14:59 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Zl2BJk7D; spf=pass (imf30.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.55 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796321700; b=A3Lzw1gwfH1wBy/Arf350CXV7p6xESJbsifTdqx+GcaVvED3UfAkeaWjblKHjv7KH97JxV lSlT4cK/4+qfWOw/KC4EkD99GsO5p5VSLPNQHcKUoLyxRCE1dPF4Dl6smW4GWQXH8oPsQ8 YngG0uBIo09mniRK0sEng7bHYNernZM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321700; 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=YRwtFt5F6lJimPA1aq9yVxC0eCPUpAwvydD0snenymg=; b=pVevtbdrvTVM13XBCxjy39A693RXUh93q/tiD/pt/eSMHuvP1T/asFMcztmAGadeNTOMCg Ue/0km3UjNgzjaAt3BScDmDG2PGD0f1FYfr4V23Y9tb34B7CLWctmXkSaMHtZhPNr+J3pj Mm0D3bCCIvRUQ2vRJkdSBUOVcUN0ULs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Zl2BJk7D; spf=pass (imf30.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.55 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=+26+mB+6ESNr3qhTFu0Z9yVqaaCTZqMj60A7lahNADU=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321698; v=1; x=1796926498; b=Zl2BJk7DiFGKm5KeMn3gDY3g502RhyZBE61YGcg8yR5GqAmrXdY4JbBUZf27aEbJd+pXEcWR 7z0fb1j45fDscYbFAPtga/LdGRBdOeXPOMQB2Es4rQnLr2yi21ZR+fxq6Osb4rFXB6AXN0CYCuq 06TS8sOQfo0S8j7eflfLMbOM= X-Envelope-To: linux-mm@kvack.org Received: by mta12.migadu.com with ESMTPS id a2ff359e37e51a06; Thu, 03 Dec 2026 18:14:58 +0000 X-Mizu-Trace-ID: a2ff359e37e51a06 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, 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 Subject: [PATCH v2 15/32] mm, swap: pass the folio order to the xswap physical backend Date: Fri, 4 Dec 2026 02:13:02 +0800 Message-ID: <20261203181319.901844-16-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev> References: <20261203181319.901844-1-baoquan.he@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit 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-Stat-Signature: y4ncrjfrxj51bb7zaob5mjb53nt8ynsf X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 000E78000D X-Rspam-User: X-HE-Tag: 1796321699-103240 X-HE-Meta: U2FsdGVkX19aHPmF1PrVQISXQ4WQOrjkbnwLoD+bNDOVCnooGCCjQoDuNR3zSmrB1pYb6nPAAK6Xj/eeWIgRIDa6qVVa339ltQnRDMDUT69w13IpAnv903INUoAVKaHK7vHZN4KmFH+/Z8jMsuYz6YdhwCiBT7ZtIZYZJml6AdxRMRk6Mb0dYVmd3IRzQZRCDCKnFG63UEDprObVNNxIJ2Emyqo3kcLhxeMMxO2zBw21gDFpugQBQHUeQw7X61s1BmcBux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TjIuLjTPnxySoRoGaww7QOlzbsxieYERY/ypoEUgISNbeIVkPmUaz8nS/R9OA/14dgjh2qIqCimwd+PRdCYazqsU2PPV5wtFGAuVFQ 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 An xswap folio can need more than one physical swap slot. A large folio needs 1 << order contiguous slots on the real device, so the physical backend must know the order when it allocates. Pass the order to the backend functions, set up every slot in the range, and free the whole range together. Callers still pass order 0 for now. Large xswap folios are not enabled yet. Signed-off-by: Baoquan He --- mm/page_io.c | 3 +- mm/swap.h | 10 +++-- mm/swapfile.c | 120 +++++++++++++++++++++++++++++--------------------- mm/zswap.c | 4 +- 4 files changed, 80 insertions(+), 57 deletions(-) diff --git a/mm/page_io.c b/mm/page_io.c index 5aa3717b3d70..71e442a8f744 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -265,7 +265,8 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio) swp_entry_t phys; /* The pool refused it: fall back to a real swap device. */ - phys = xswap_backend_alloc(entry, ctx->may_enter_fs); + phys = xswap_backend_alloc(entry, folio_order(folio), + ctx->may_enter_fs); if (!phys.val) { folio_mark_dirty(folio); return AOP_WRITEPAGE_ACTIVATE; diff --git a/mm/swap.h b/mm/swap.h index 6e3128c54b4f..9739f9d9e9db 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -510,8 +510,9 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, #ifdef CONFIG_XSWAP swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot); -swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs); -void xswap_backend_free(swp_entry_t entry, swp_entry_t phys); +swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, + bool may_enter_fs); +void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order); #else static inline swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot) @@ -519,12 +520,13 @@ static inline swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, return (swp_entry_t){}; } -static inline swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs) +static inline swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, + bool may_enter_fs) { return (swp_entry_t){}; } -static inline void xswap_backend_free(swp_entry_t entry, swp_entry_t phys) +static inline void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order) { } #endif /* CONFIG_XSWAP */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 0e4b468ad87b..6bc14f5d1618 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -4663,8 +4663,9 @@ static void xswap_free_xs_table(struct swap_info_struct *si, ci->xs_table = NULL; } -/* Order-0 slot from @si, no folio attached; goes through the per-CPU cache. */ -static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si) +/* A range of 1 << @order slots, no folio attached. */ +static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si, + unsigned int order) { struct swap_cluster_info *ci; unsigned long pcp_offset, offset = SWAP_ENTRY_INVALID; @@ -4677,26 +4678,26 @@ static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si) * again. Start from the cached cursor if it already points at this * device, but never install this device for the next swapout. */ - if (this_cpu_read(percpu_swap_cluster.si[0]) == si) { - pcp_offset = this_cpu_read(percpu_swap_cluster.offset[0]); + if (this_cpu_read(percpu_swap_cluster.si[order]) == si) { + pcp_offset = this_cpu_read(percpu_swap_cluster.offset[order]); if (pcp_offset) { ci = swap_cluster_lock(si, pcp_offset); - if (cluster_is_usable(ci, 0)) + if (cluster_is_usable(ci, order)) offset = alloc_swap_scan_cluster(si, ci, NULL, - pcp_offset, 0, + pcp_offset, order, true); else swap_cluster_unlock(ci); } } if (offset == SWAP_ENTRY_INVALID) - offset = cluster_alloc_swap_entry(si, NULL, 0, true); + offset = cluster_alloc_swap_entry(si, NULL, order, true); local_unlock(&percpu_swap_cluster.lock); return offset; } -static swp_entry_t xswap_alloc_phys_slot(bool may_enter_fs) +static swp_entry_t xswap_alloc_phys_slot(unsigned int order, bool may_enter_fs) { struct swap_info_struct *devs[MAX_SWAPFILES], *si; int nr = 0, i; @@ -4734,7 +4735,7 @@ static swp_entry_t xswap_alloc_phys_slot(bool may_enter_fs) put_swap_device(devs[i]); continue; } - offset = xswap_alloc_slot_local(devs[i]); + offset = xswap_alloc_slot_local(devs[i], order); put_swap_device(devs[i]); if (offset && offset != SWAP_ENTRY_INVALID) return swp_entry(devs[i]->type, offset); @@ -4760,25 +4761,33 @@ static void xswap_install_rmap(swp_entry_t phys, swp_entry_t entry) } } -swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs) +/* Back the range of 1 << @order slots at @entry with contiguous physical slots. */ +swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, + bool may_enter_fs) { struct swap_info_struct *si = __swap_entry_to_info(entry); struct swap_cluster_info *ci; + unsigned long nr = 1UL << order; unsigned long off = swp_offset(entry); - unsigned long swp_tb; - swp_entry_t phys, old; + swp_entry_t phys; + unsigned int i; /* * A folio that was written back, redirtied while still in the swap * cache and is now written out again still records the old slot range. * Drop it before the new one replaces it. */ - ci = swap_cluster_lock(si, off); - if (ci) { - old = xswap_slot_backend(ci, off % SWAPFILE_CLUSTER); - swap_cluster_unlock(ci); + for (i = 0; i < nr; i++) { + struct swap_cluster_info *oci; + swp_entry_t old; + + oci = swap_cluster_lock(si, off + i); + if (!oci) + continue; + old = xswap_slot_backend(oci, (off + i) % SWAPFILE_CLUSTER); + swap_cluster_unlock(oci); if (old.val) - xswap_backend_free(entry, old); + xswap_backend_free(swp_entry(si->type, off + i), old, 0); } /* @@ -4794,7 +4803,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs) } swap_cluster_unlock(ci); - phys = xswap_alloc_phys_slot(may_enter_fs); + phys = xswap_alloc_phys_slot(order, may_enter_fs); if (!phys.val) return phys; @@ -4802,53 +4811,64 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs) * The slot is a shadow until the reverse map is installed below. * Physical swapoff can see it and retry; the window is short and * try_to_unuse() reschedules. - * - * Owner first, then the reverse map, under one hold of the lock. */ + /* One IO reads a large folio, so its range has to be contiguous. */ + VM_WARN_ON_ONCE(swp_offset(phys) % nr); + VM_WARN_ON_ONCE(off % SWAPFILE_CLUSTER + nr > SWAPFILE_CLUSTER); + + /* Owner first, then the reverse map, under one hold of the lock. */ ci = swap_cluster_lock(si, off); - if (!ci) { - xswap_backend_free(entry, phys); - return (swp_entry_t){}; - } - /* The table is preallocated above, so this cannot fail. */ - if (!xswap_slot_set_backend(ci, off % SWAPFILE_CLUSTER, phys)) - VM_WARN_ON_ONCE(1); - xswap_install_rmap(phys, entry); + for (i = 0; i < nr; i++) { + unsigned int slot = (off + i) % SWAPFILE_CLUSTER; + swp_entry_t cur = swp_entry(swp_type(phys), swp_offset(phys) + i); + unsigned long swp_tb; - /* - * The folio can lose its last reference while the owner is not - * recorded yet; that path cannot mark the slot for us. - */ - swp_tb = __swap_table_get(ci, off % SWAPFILE_CLUSTER); - if (swp_tb_is_folio(swp_tb) && !swp_tb_get_count(swp_tb)) - xswap_mark_cache_only(si, ci, off % SWAPFILE_CLUSTER); + /* The table is preallocated above, so this cannot fail. */ + if (!xswap_slot_set_backend(ci, slot, cur)) + VM_WARN_ON_ONCE(1); + xswap_install_rmap(cur, swp_entry(si->type, off + i)); + + /* + * The folio can lose its last reference while the owner is + * not recorded yet; that path cannot mark the slot for us. + */ + swp_tb = __swap_table_get(ci, slot); + if (swp_tb_is_folio(swp_tb) && !swp_tb_get_count(swp_tb)) + xswap_mark_cache_only(si, ci, slot); + } swap_cluster_unlock(ci); return phys; } /* Undo xswap_backend_alloc(): the zswap copy is still in place. */ -void xswap_backend_free(swp_entry_t entry, swp_entry_t phys) +void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order) { struct swap_info_struct *si = __swap_entry_to_info(entry); - struct swap_info_struct *psi; - struct swap_cluster_info *ci; + struct swap_info_struct *psi = swap_type_to_info(swp_type(phys)); + unsigned long nr = 1UL << order; unsigned long off = swp_offset(entry); + unsigned long poff = swp_offset(phys); + unsigned int i; - ci = swap_cluster_lock(si, off); - if (ci) { - unsigned int slot = off % SWAPFILE_CLUSTER; - unsigned long *xs = ci->xs_table; + for (i = 0; i < nr; i++) { + struct swap_cluster_info *ci; - if (xs && xs[slot] == phys.val) - WRITE_ONCE(xs[slot], 0); - swap_cluster_unlock(ci); - } + ci = swap_cluster_lock(si, off + i); + if (ci) { + unsigned int slot = (off + i) % SWAPFILE_CLUSTER; + unsigned long *xs = ci->xs_table; - /* xswap_free_phys_slot() clears the reverse mapping. */ - psi = swap_type_to_info(swp_type(phys)); - if (psi) - xswap_free_phys_slot(psi, entry, phys); + if (xs && xs[slot] == phys.val + i) + WRITE_ONCE(xs[slot], 0); + swap_cluster_unlock(ci); + } + + /* xswap_free_phys_slot() clears the reverse mapping. */ + if (psi) + xswap_free_phys_slot(psi, swp_entry(si->type, off + i), + swp_entry(swp_type(phys), poff + i)); + } } static int xswap_map_clusters(struct swap_info_struct *si, diff --git a/mm/zswap.c b/mm/zswap.c index c367fd04551d..dfb7ea058d66 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1161,7 +1161,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, /* Reserve the destination before dropping the zswap copy. */ if (is_xswap) { - phys = xswap_backend_alloc(swpentry, may_enter_fs); + phys = xswap_backend_alloc(swpentry, 0, may_enter_fs); if (!phys.val) { ret = -ENOMEM; goto err; @@ -1206,7 +1206,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, err: if (is_xswap && phys.val) - xswap_backend_free(swpentry, phys); + xswap_backend_free(swpentry, phys, 0); swap_cache_del_folio(folio); folio_unlock(folio); folio_put(folio); -- 2.54.0