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 AD920606A33A for ; Thu, 3 Dec 2026 18:15:18 +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=PdIKBFmb Received: by kanga.kvack.org (Postfix) id D88D66B05D1; Thu, 3 Dec 2026 13:15:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D12826B05D2; Thu, 3 Dec 2026 13:15:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C013A6B05D3; Thu, 3 Dec 2026 13:15:17 -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 966FF6B05D1 for ; Thu, 3 Dec 2026 13:15:17 -0500 (EST) Received: from smtpin19.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 22376C0162 for ; Thu, 3 Dec 2026 18:15:17 +0000 (UTC) X-FDA: 85503473874.19.BE5CC71 Received: from mta1.migadu.com (out-122.mta1.migadu.com [95.215.58.122]) by imf14.hostedemail.com (Postfix) with ESMTP id 136A9100005 for ; Thu, 3 Dec 2026 18:15:14 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=PdIKBFmb; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.122 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=1796321715; b=haYUd/gOCxNQhOGYo5+aohhNwSpZ5jlKGnY9wT1XpYE67+dvCjDJ1wFErH7YVMZQbGhKvm kVuG6RTydjThMubN0L8RjwOqTqtFTMF22y6B/xcz5sTQ0ySU0ZM1TgDCjimQNOBbm1HiIR pWncKBqUvcV6vpqe982xcJTKesEQ8Gk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321715; 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=dWjpjCWo/m5St+WlGcaF5T+IB8TE8rJ24WUAAuSIxWc=; b=O05jrF/WIff8Is8R0QYgREf8wa1JfPKv8SJZerhwY3Osi/5AAGkMSclZKy4erAmSXjwVNF 2Lx6Kazxgl7H8u/jTAJK7yre9k6FgN1BDbw40HPURP3n4+4ytOSB9a/0kKCXxJahUmgLqM +HNZcSqXg0113xC8vXAmeAr8Xgn/5c8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=PdIKBFmb; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.122 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=yXom1ozHsyBCTlAbt/1hKMXx/1SZJYpYRoffleAJ9Co=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321713; v=1; x=1796926513; b=PdIKBFmb5BE3lOHtixvcMGRr8WNCtg6Yi4Gk1Yidtm8TuRnL+L67ssnYX2QvVUpNteqoiCgo VHWH688QYX79bTezpwzxTqSbGEf7FvQKP2nIC3EMyBN7zs7iXl+wov750rqr2e1ljFyKdhIxJup tlmdPUOAM60kxwYcfIE0kgCI= X-Envelope-To: linux-mm@kvack.org Received: by mta11.migadu.com with ESMTPS id 189938075613d2bc; Thu, 03 Dec 2026 18:15:13 +0000 X-Mizu-Trace-ID: 189938075613d2bc 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 17/32] mm, swap: enable THP swapin for xswap entries Date: Fri, 4 Dec 2026 02:13:04 +0800 Message-ID: <20261203181319.901844-18-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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 136A9100005 X-Stat-Signature: wock6qb59zu6o4ko7b59kgsz7k9ozrhf 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: 1796321714-573366 X-HE-Meta: U2FsdGVkX1+I8ymHHjYAjtrsfohd9n0ttjO6W5LJsuX16vGN1Hx1Xnulu9bppINs6fOUvE+cJZ/xqht6kb1EtFnzjnujUHgy0iJsoeHEWZeWY25fc0ZiSnujC3kdybw/8LDZaqQyduuIoV/BbvBsAUjYP8++pwLvFPSLkLs3c0xfNp2pivl3d8ZVvxPkZn2ghv9RfzaFvpztoS9KHKTm5J3X22x5SrfArM0pRprDU1P2cucH7f9y3hFxU0NNy8uYhxDJL5umBsqt2g3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F+lzwBjFnD/1P6MldWj7IJagLvAp2DPZWAyf3BCnR1Mm+dc/+E+tm4kV0iU/DNKlDtmqsA76yec8xOozYWiRr6eK92aCY6KE4H4rqox1nh2PKB5qdTJa4g== 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 Read a large anon folio back as one unit when its xswap entries are contiguous on one physical swap device; otherwise read it page by page and retry at a smaller order. The device is SWP_SYNCHRONOUS_IO, which THP swapin requires and which also skips readahead. xswap_check_backing() refuses a zswap-backed or mixed range under the cluster lock, before the folio is allocated, so zswap_load() takes the physical path. If an earlier writeout fell back to a physical range and zswap later accepts the folio, drop the stale range in swap_writeout() on the zswap and zero-fill paths; reuse the backend loop as xswap_backend_drop(). Co-developed-by: Nhat Pham Signed-off-by: Nhat Pham Signed-off-by: Baoquan He --- mm/memory.c | 25 ++++++++------- mm/page_io.c | 6 +++- mm/swap.h | 13 ++++++++ mm/swap_state.c | 6 ++++ mm/swapfile.c | 82 +++++++++++++++++++++++++++++++++++++++++-------- 5 files changed, 107 insertions(+), 25 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 94b0b1921f54..aa9caf576564 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4910,23 +4910,26 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si, if (unlikely(userfaultfd_armed(vma))) return 0; + entry = softleaf_from_pte(vmf->orig_pte); + /* * A large swapped out folio could be partially or fully in zswap. We * lack handling for such cases, so fallback to swapping in order-0 * folio. + * An xswap entry is deferred to __swap_cache_add_check() instead. */ - if (!zswap_never_enabled()) - return 0; - - /* - * Neighbouring slots of a swap file can lie in two extents, and a folio - * is read with one bio. The flag is set in swapon() and stays, so the - * race is benign. - */ - if (!data_race(si->flags & SWP_BLKDEV)) - return 0; + if (!(__swap_entry_to_info(entry)->flags & SWP_XSWAP)) { + if (!zswap_never_enabled()) + return 0; - entry = softleaf_from_pte(vmf->orig_pte); + /* + * Neighbouring slots of a swap file can lie in two extents, and + * a folio is read with one bio. The flag is set in swapon() and + * stays, so the race is benign. + */ + if (!data_race(si->flags & SWP_BLKDEV)) + return 0; + } /* * Get a list of all the (large) orders below PMD_ORDER that are enabled * and suitable for swapping THP. diff --git a/mm/page_io.c b/mm/page_io.c index 080255ac7314..a49287162538 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -232,10 +232,12 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio) /* * The folio may have been stored in zswap before and dirtied * again while it stayed in the swap cache. Drop the old copy, - * or zswap writeback would bring it back into the swap cache. + * or zswap writeback would bring it back into the swap cache; + * a physical slot range recorded for it is stale for the same reason. */ zswap_invalidate(swp_type(entry), swp_offset(entry), folio_nr_pages(folio)); + xswap_backend_drop(entry, folio_order(folio)); goto out_unlock; } @@ -248,6 +250,8 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio) if (zswap_store(folio, ctx->may_enter_fs)) { count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT); + /* A slot range recorded by an earlier writeout is stale now. */ + xswap_backend_drop(folio->swap, folio_order(folio)); goto out_unlock; } diff --git a/mm/swap.h b/mm/swap.h index 1260b0ac6f41..2cd87a26843a 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -515,6 +515,9 @@ swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot); 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); +void xswap_backend_drop(swp_entry_t entry, unsigned int order); +int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off, + unsigned int nr); #else static inline swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot) @@ -531,6 +534,16 @@ static inline swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int or static inline void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order) { } + +static inline void xswap_backend_drop(swp_entry_t entry, unsigned int order) +{ +} + +static inline int xswap_check_backing(struct swap_cluster_info *ci, + unsigned int off, unsigned int nr) +{ + return nr; +} #endif /* CONFIG_XSWAP */ #endif /* _MM_SWAP_H */ diff --git a/mm/swap_state.c b/mm/swap_state.c index 7298a9894737..044e4856b7d8 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -209,6 +209,12 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci, if (nr == 1) return 0; + /* A zswap-backed or mixed batch falls back to a smaller order. */ + if (__swap_entry_to_info(targ_entry)->flags & SWP_XSWAP) { + if (xswap_check_backing(ci, round_down(ci_off, nr), nr) != nr) + return -EBUSY; + } + is_zero = __swap_table_test_zero(ci, ci_off); ci_off = round_down(ci_off, nr); ci_end = ci_off + nr; diff --git a/mm/swapfile.c b/mm/swapfile.c index f1257ae98685..463f53286a05 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -4634,6 +4634,39 @@ swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot) return (swp_entry_t){ .val = xs[slot] }; } +/* + * A large xswap folio is read back as one IO only when the whole range is + * backed by one device, contiguously. Returns how many leading slots are. + */ +int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off, + unsigned int nr) +{ + swp_entry_t first; + unsigned int i; + + lockdep_assert_held(&ci->lock); + /* A folio's slots never straddle a cluster, or the walk runs off. */ + if (off + nr > SWAPFILE_CLUSTER) { + VM_WARN_ON_ONCE(1); + nr = SWAPFILE_CLUSTER - off; + } + + /* In zswap, which cannot load a large folio. */ + if (!ci->xs_table || !ci->xs_table[off]) + return 1; + + first.val = ci->xs_table[off]; + for (i = 1; i < nr; i++) { + swp_entry_t cur = { .val = ci->xs_table[off + i] }; + + if (swp_type(cur) != swp_type(first) || + swp_offset(cur) != swp_offset(first) + i) + break; + } + + return i; +} + /* Cluster lock held. The mapped prefix only grows while the device exists. */ static unsigned long *xswap_xs_table_locked(struct swap_cluster_info *ci, gfp_t gfp) @@ -4798,6 +4831,36 @@ static void xswap_install_rmap(swp_entry_t phys, swp_entry_t entry) } } +/* + * The data of the range of 1 << @order slots at @entry has been replaced, + * by zswap or by the zero mark. Drop the physical slot range recorded for it: + * otherwise its slots stay held until the xswap entry is freed, and a + * large folio can be read from a stale physical copy. + */ +void xswap_backend_drop(swp_entry_t entry, unsigned int order) +{ + struct swap_info_struct *si = __swap_entry_to_info(entry); + unsigned long nr = 1UL << order; + unsigned long off = swp_offset(entry); + unsigned int i; + + if (!(si->flags & SWP_XSWAP)) + return; + + for (i = 0; i < nr; i++) { + struct swap_cluster_info *ci; + swp_entry_t old; + + ci = swap_cluster_lock(si, off + i); + if (!ci) + continue; + old = xswap_slot_backend(ci, (off + i) % SWAPFILE_CLUSTER); + swap_cluster_unlock(ci); + if (old.val) + xswap_backend_free(swp_entry(si->type, off + i), old, 0); + } +} + /* 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) @@ -4814,18 +4877,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, * cache and is now written out again still records the old slot range. * Drop it before the new one replaces it. */ - 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(swp_entry(si->type, off + i), old, 0); - } + xswap_backend_drop(entry, order); /* * Get the xswap side ready before a physical slot exists, so the @@ -5281,7 +5333,11 @@ static int xswap_create(int prio) si->bdev = NULL; si->swap_file = NULL; - si->flags |= SWP_XSWAP | SWP_SOLIDSTATE; + /* + * Reads are served from zswap, so no readahead. Only a synchronous + * device can swap a large folio back in as a unit. + */ + si->flags |= SWP_XSWAP | SWP_SOLIDSTATE | SWP_SYNCHRONOUS_IO; si->max = maxpages; si->pages = maxpages - 1; /* -- 2.54.0