From: Baoquan He Freeing a range of swap slots calls swap_backend_erase() for every slot, which resolves the device and the cluster again each time: two percpu_ref_is_zero() calls and a division per slot. A slot range is a folio's slots or a cluster's free batch, so it never crosses a cluster. Resolve the cluster once and walk the slots. A tagged handle is only ever a zswap entry, so the walk releases those directly, which is why zswap_entry_free() is no longer static. The backend table is the only owner of a stored zswap entry, so the range erase is where it goes; collecting the handles first would only add an array or a callback to the free path. Release a tagged entry that is still in a cluster's table when the table goes as well, instead of only warning about it, so a broken invariant cannot leak it. Signed-off-by: Baoquan He --- include/linux/zswap.h | 4 ++++ mm/swap.h | 1 + mm/swapfile.c | 43 +++++++++++++++++++++++++++++++++++++++---- mm/zswap.c | 12 ++---------- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/include/linux/zswap.h b/include/linux/zswap.h index c7faad1d1b42..df9e0f29480e 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -6,6 +6,7 @@ #include struct lruvec; +struct zswap_entry; extern atomic_long_t zswap_stored_pages; @@ -25,6 +26,7 @@ struct zswap_lruvec_state { }; unsigned long zswap_total_pages(void); +void zswap_entry_free(struct zswap_entry *entry); bool zswap_store(struct folio *folio, bool may_enter_fs); int zswap_load(struct folio *folio); void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries); @@ -38,6 +40,8 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr); struct zswap_lruvec_state {}; +static inline void zswap_entry_free(struct zswap_entry *entry) {} + static inline bool zswap_store(struct folio *folio, bool may_enter_fs) { return false; diff --git a/mm/swap.h b/mm/swap.h index 9b2d8d556bd0..4f6e2c2de16f 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -229,6 +229,7 @@ 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); +void swap_backend_erase_range(swp_entry_t entry, unsigned long nr); bool swap_backend_present(swp_entry_t entry, unsigned int nr); /* diff --git a/mm/swapfile.c b/mm/swapfile.c index 66a7682e29f0..3098a4086cdb 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -4884,10 +4884,40 @@ void *swap_backend_erase(swp_entry_t swp) return swap_backend_decode_zswap(val); } +/* + * Drop the handles of a range of slots. A range is a folio's slots or a + * cluster's free batch, so it never crosses a cluster: the caller holds + * either the cluster lock (slot free) or the folio lock (store rollback). + * + * Clamp and warn if the one-cluster invariant ever breaks; the next + * cluster is not protected here and the remaining slots are not erased. + */ +void swap_backend_erase_range(swp_entry_t swp, unsigned long nr) +{ + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned int slot = swp_cluster_offset(swp); + unsigned long *tbl = READ_ONCE(ci->backend_table); + unsigned long n = min_t(unsigned long, nr, SWAPFILE_CLUSTER - slot); + + VM_WARN_ON_ONCE(nr > SWAPFILE_CLUSTER - slot); + if (!tbl) + return; + + for (unsigned int i = 0; i < n; i++) { + unsigned long val = READ_ONCE(tbl[slot + i]); + + if (!swap_backend_is_zswap(val)) + continue; + WRITE_ONCE(tbl[slot + i], 0); + zswap_entry_free(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. + * xswap physical backend that has to be given back; a tagged one should + * have been invalidated by the slot free path, but release it here too so a + * broken invariant cannot leak it. Caller holds ci->lock. */ static void swap_backend_table_free(struct swap_info_struct *si, struct swap_cluster_info *ci) @@ -4901,9 +4931,14 @@ static void swap_backend_table_free(struct swap_info_struct *si, for (slot = 0; slot < SWAPFILE_CLUSTER; slot++) { unsigned long val = READ_ONCE(tbl[slot]); - VM_WARN_ON_ONCE(swap_backend_is_zswap(val)); + if (swap_backend_is_zswap(val)) { + VM_WARN_ON_ONCE(1); + WRITE_ONCE(tbl[slot], 0); + zswap_entry_free(swap_backend_decode_zswap(val)); + continue; + } #ifdef CONFIG_XSWAP - if (val && !swap_backend_is_zswap(val)) + if (val) xswap_release_slot_backend(si, ci, slot); #else VM_WARN_ON_ONCE(val); diff --git a/mm/zswap.c b/mm/zswap.c index 1903367a9576..9df2eaf4a21c 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -766,7 +766,7 @@ static void zswap_entry_cache_free(struct zswap_entry *entry) * Carries out the common pattern of freeing an entry's zsmalloc allocation, * freeing the entry itself, and decrementing the number of stored pages. */ -static void zswap_entry_free(struct zswap_entry *entry) +void zswap_entry_free(struct zswap_entry *entry) { struct zswap_pool *pool = zswap_entry_pool(entry); @@ -1849,8 +1849,6 @@ int zswap_load(struct folio *folio) void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries) { - unsigned long i; - if (zswap_never_enabled()) return; @@ -1859,13 +1857,7 @@ void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries) * no longer allocatable, and the store rollback holds the folio * lock, so only one caller can see a tagged handle. */ - for (i = 0; i < nr_entries; i++) { - struct zswap_entry *entry; - - entry = swap_backend_erase(swp_entry(type, offset + i)); - if (entry) - zswap_entry_free(entry); - } + swap_backend_erase_range(swp_entry(type, offset), nr_entries); } /********************************* -- 2.54.0