From: Baoquan He Add swap_backend_get(), swap_backend_set(), swap_backend_erase() and swap_backend_present() to read and update a swap device's per-slot backend record, plus the zswap entry encoding. The next patch wires zswap to them. get, erase and present read without the cluster lock: the table is allocated before the first record is published and freed only when the cluster is empty, so an in-use slot can always reach it. Updates are serialized by the folio lock (store, load, writeback) or the cluster lock (slot free). Only set takes the cluster lock, only to allocate, and it avoids direct reclaim because the caller's folio is locked. present() walks a range within one cluster and returns early without a table; a range that would cross the cluster is refused after a warning. Setting the same handle is a no-op; only a different handle is reported as displaced. Signed-off-by: Baoquan He --- mm/swap.h | 5 ++ mm/swapfile.c | 208 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 170 insertions(+), 43 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index cdb82504269b..9b2d8d556bd0 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -226,6 +226,11 @@ static inline void swap_cluster_unlock_irq(struct swap_cluster_info *ci) extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp); +void *swap_backend_get(swp_entry_t entry); +void *swap_backend_set(swp_entry_t entry, void *handle); +void *swap_backend_erase(swp_entry_t entry); +bool swap_backend_present(swp_entry_t entry, unsigned int nr); + /* * Below are the core routines for doing swap for a folio. * All helpers requires the folio to be locked, and a locked folio diff --git a/mm/swapfile.c b/mm/swapfile.c index 96019307ce75..1862f0b93c92 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -90,6 +90,19 @@ static inline swp_entry_t swap_backend_decode_phys(unsigned long val) return (swp_entry_t){ .val = val >> 1 }; } +static inline unsigned long swap_backend_encode_zswap(void *handle) +{ + unsigned long val = (unsigned long)handle; + + VM_WARN_ON_ONCE(val & 1UL); + return val | 1UL; +} + +static inline void *swap_backend_decode_zswap(unsigned long val) +{ + return (void *)(val & ~1UL); +} + static inline bool swap_backend_is_zswap(unsigned long val) { return val & 1UL; @@ -4731,9 +4744,151 @@ static unsigned long read_swap_header(struct swap_info_struct *si, } /* - * Drop the table of a cluster that is going away. A recorded backend is - * given back first, or its physical slot and reverse mapping would leak. - * Caller holds ci->lock. + * Per-slot backend record of a cluster, allocated on demand and freed + * with the cluster. + */ +static unsigned long *swap_backend_table_locked(struct swap_cluster_info *ci, + gfp_t gfp) +{ + unsigned long *tbl = ci->backend_table; + + if (tbl) + return tbl; + + /* The owning entry is pinned, so the cluster survives the relock. */ + if (gfp & __GFP_DIRECT_RECLAIM) { + unsigned int noreclaim_flags; + + spin_unlock(&ci->lock); + /* + * The folio being stored is locked. Do not recurse into + * reclaim, which could wait for that folio. + */ + noreclaim_flags = memalloc_noreclaim_save(); + tbl = kcalloc(SWAPFILE_CLUSTER, sizeof(*tbl), gfp); + memalloc_noreclaim_restore(noreclaim_flags); + spin_lock(&ci->lock); + } else { + tbl = kcalloc(SWAPFILE_CLUSTER, sizeof(*tbl), gfp); + } + + if (!tbl) + return NULL; + if (cmpxchg(&ci->backend_table, NULL, tbl)) { + kfree(tbl); + tbl = ci->backend_table; + } + return tbl; +} + +/* + * The zswap side of the table. + * + * The table pointer and its slots are read without taking the cluster lock: + * the table is allocated before the first entry is published and freed only + * once the cluster has no in-use slot left, so a caller that holds an + * in-use slot can always reach it. Per-slot updates are serialized by the + * folio lock (store, load and writeback all run with the folio locked) or + * by the cluster lock (the slot free path runs under it). Only set takes + * the cluster lock, and only to allocate the table. + */ +void *swap_backend_get(swp_entry_t swp) +{ + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned long *tbl = READ_ONCE(ci->backend_table); + unsigned long val; + + if (!tbl) + return NULL; + + val = READ_ONCE(tbl[swp_cluster_offset(swp)]); + return swap_backend_is_zswap(val) ? swap_backend_decode_zswap(val) : NULL; +} + +/* Is any slot in [swp, swp + nr) in zswap? The range sits in one cluster. */ +bool swap_backend_present(swp_entry_t swp, unsigned int nr) +{ + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned long *tbl = READ_ONCE(ci->backend_table); + unsigned int slot = swp_cluster_offset(swp); + unsigned int i; + + /* The range is aligned to its own size and never straddles a cluster. */ + if (slot + nr > SWAPFILE_CLUSTER) { + VM_WARN_ON_ONCE(1); + return false; + } + + if (!tbl) + return false; + + for (i = 0; i < nr; i++) { + if (swap_backend_is_zswap(READ_ONCE(tbl[slot + i]))) + return true; + } + return false; +} + +void *swap_backend_set(swp_entry_t swp, void *handle) +{ + struct swap_info_struct *si = __swap_entry_to_info(swp); + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned int slot = swp_cluster_offset(swp); + unsigned long *tbl, old, encoded; + + tbl = READ_ONCE(ci->backend_table); + if (!tbl) { + ci = swap_cluster_lock(si, swp_offset(swp)); + tbl = swap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | + __GFP_NOMEMALLOC); + swap_cluster_unlock(ci); + if (!tbl) + return ERR_PTR(-ENOMEM); + } + + encoded = swap_backend_encode_zswap(handle); + old = READ_ONCE(tbl[slot]); + /* Re-setting the same handle is a no-op, not a displaced entry. */ + if (old == encoded) + return NULL; + + /* + * A folio written back and then redirtied while still in the swap + * cache is swapped out again into the same slot. The physical copy + * is stale then, and zswap takes the slot over. + */ +#ifdef CONFIG_XSWAP + if (old && !swap_backend_is_zswap(old)) + xswap_backend_free(swp, swap_backend_decode_phys(old), 0); +#endif + + WRITE_ONCE(tbl[slot], encoded); + + return swap_backend_is_zswap(old) ? swap_backend_decode_zswap(old) : NULL; +} + +void *swap_backend_erase(swp_entry_t swp) +{ + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned long *tbl = READ_ONCE(ci->backend_table); + unsigned int slot = swp_cluster_offset(swp); + unsigned long val; + + if (!tbl) + return NULL; + + val = READ_ONCE(tbl[slot]); + if (!swap_backend_is_zswap(val)) + return NULL; + + WRITE_ONCE(tbl[slot], 0); + return swap_backend_decode_zswap(val); +} + +/* + * Drop the table of a cluster that is going away. An untagged value is an + * xswap physical backend that has to be given back; a tagged one would be a + * leak, the slot free path invalidates those first. Caller holds ci->lock. */ static void swap_backend_table_free(struct swap_info_struct *si, struct swap_cluster_info *ci) @@ -4848,56 +5003,23 @@ int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off, return i; } -/* Cluster lock held. The mapped prefix only grows while the device exists. */ -static unsigned long *xswap_backend_table_locked(struct swap_cluster_info *ci, - gfp_t gfp) -{ - unsigned long *xs = ci->backend_table; - - if (xs) - return xs; - - /* The owning entry is pinned, so the cluster survives the relock. */ - if (gfp & __GFP_DIRECT_RECLAIM) { - unsigned int noreclaim_flags; - - spin_unlock(&ci->lock); - /* - * The folio being stored is locked. Do not recurse into - * reclaim, which could wait for that folio. - */ - noreclaim_flags = memalloc_noreclaim_save(); - xs = kcalloc(SWAPFILE_CLUSTER, sizeof(*xs), gfp); - memalloc_noreclaim_restore(noreclaim_flags); - spin_lock(&ci->lock); - } else { - xs = kcalloc(SWAPFILE_CLUSTER, sizeof(*xs), gfp); - } - - if (!xs) - return NULL; - if (cmpxchg(&ci->backend_table, NULL, xs)) { - kfree(xs); - xs = ci->backend_table; - } - return xs; -} - /* * 4K per cluster, and this runs in reclaim, so it has to be able to sleep. */ static bool xswap_slot_set_backend(struct swap_cluster_info *ci, unsigned int slot, swp_entry_t phys) { - unsigned long *xs = xswap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | - __GFP_NOMEMALLOC); + unsigned long *tbl = swap_backend_table_locked(ci, GFP_KERNEL | + __GFP_HIGH | + __GFP_NOMEMALLOC); - if (!xs) + if (!tbl) return false; - WRITE_ONCE(xs[slot], swap_backend_encode_phys(phys)); + WRITE_ONCE(tbl[slot], swap_backend_encode_phys(phys)); return true; } + /* A range of 1 << @order slots, no folio attached. */ static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si, unsigned int order) @@ -5052,7 +5174,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, ci = swap_cluster_lock(si, off); if (!ci) return (swp_entry_t){}; - if (!xswap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) { + if (!swap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) { swap_cluster_unlock(ci); return (swp_entry_t){}; } -- 2.54.0