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