From: Baoquan He An xswap entry that zswap evicts had nowhere to go. Send it to the physical backend, and reserve the backend before decompressing so a failed allocation wastes no work. Now that an xswap entry can be written back, put it on the zswap writeback LRU again. With no real swap device it stays on the LRU and the shrinker rotates it until one appears, instead of staying pinned until swapin or destroy. An incompressible xswap page is stored uncompressed only while a real swap device exists to hold its backend. The zswap shrinker copies a swpentry and drops the LRU lock before verifying it, while xswap maps cluster_info on demand. Check the range under xswap_lock and reject a swpentry outside nr_clusters_mapped. trylock avoids a deadlock: the allocation can trigger reclaim that calls this path again. A slot that was written out has no zswap copy, so swap_read_folio() forwards its read to the physical entry. Signed-off-by: Baoquan He --- mm/page_io.c | 32 ++++++++++++++++++---- mm/zswap.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 91 insertions(+), 16 deletions(-) diff --git a/mm/page_io.c b/mm/page_io.c index 2bc3b46e5529..76e8d08961bb 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -481,9 +481,27 @@ static bool swap_read_folio_zeromap(struct folio *folio) void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) { struct swap_info_struct *sis = __swap_entry_to_info(folio->swap); - bool synchronous = sis->flags & SWP_SYNCHRONOUS_IO; + swp_entry_t entry = folio->swap; + swp_entry_t phys = {}; + bool synchronous; bool workingset = folio_test_workingset(folio); + /* Resolved here: the batch and submit mode precede zswap_load(). */ + if (unlikely(sis->flags & SWP_XSWAP)) { + struct swap_cluster_info *ci; + unsigned long offset = swp_offset(folio->swap); + + ci = __swap_offset_to_cluster(sis, offset); + if (ci) { + spin_lock(&ci->lock); + phys = xswap_slot_backend(ci, offset % SWAPFILE_CLUSTER); + spin_unlock(&ci->lock); + } + if (phys.val) + entry = phys; + } + synchronous = __swap_entry_to_info(entry)->flags & SWP_SYNCHRONOUS_IO; + VM_BUG_ON_FOLIO(!folio_test_swapcache(folio) && !synchronous, folio); VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio); @@ -493,7 +511,7 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) * window opens, so that each batch is billed for its own read * rather than for the next folio's lookup and decompression. */ - if (ctx->sio && !swap_can_merge(ctx, folio, folio->swap, READ)) + if (ctx->sio && !swap_can_merge(ctx, folio, entry, READ)) swap_read_submit(ctx); /* @@ -522,8 +540,12 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) if (zswap_load(folio) != -ENOENT) return; - if (unlikely(sis->flags & SWP_XSWAP)) { - /* An xswap entry only lives in zswap: zswap_load() must hit. */ + if (unlikely(sis->flags & SWP_XSWAP) && !phys.val) { + /* + * Neither zswap nor a physical slot has it. No + * folio_mark_uptodate(), so do_swap_page() sees this as a + * failed read and SIGBUSes silently. + */ WARN_ON_ONCE(1); folio_unlock(folio); return; @@ -531,7 +553,7 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) /* We have to read from slower devices. Increase zswap protection. */ zswap_folio_swapin(folio); - swap_add_folio(ctx, folio, folio->swap, READ); + swap_add_folio(ctx, folio, entry, READ); /* * Synchronous devices do the whole read at submission time, submit diff --git a/mm/zswap.c b/mm/zswap.c index b6a328ab787a..188f763a8af9 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -935,9 +935,10 @@ static bool zswap_compress(struct folio *folio, long index, * save the content as is without a compression, to keep the LRU order * of writebacks. If writeback is disabled, reject the page since it * only adds metadata overhead. swap_writeout() will put the page back - * to the active LRU list in the case. An xswap entry in this phase - * is never put on the LRU, so an incompressible page stored for it - * could never be reclaimed: reject it the same way. + * to the active LRU list in the case. An xswap entry can be written + * back only while a real swap device exists to hold its backend; + * with none, an incompressible page stored for it could never be + * reclaimed, so reject it the same way. */ if (comp_ret) { if (!can_writeback) @@ -1091,6 +1092,8 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, struct folio *folio; struct mempolicy *mpol; struct swap_info_struct *si; + swp_entry_t phys = {}; + bool is_xswap; int ret = 0; /* try to allocate swap cache folio */ @@ -1098,14 +1101,33 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, if (IS_ERR_OR_NULL(si)) return -ENOENT; - if (si->flags & SWP_XSWAP) { - put_swap_device(si); - return -EINVAL; + is_xswap = !!(si->flags & SWP_XSWAP); + + /* + * The allocation can enter reclaim and grow this device, which takes + * the lock again, so do not hold it across. Check that the entry's + * range is mapped before the allocation. + */ +#ifdef CONFIG_XSWAP + if (is_xswap) { + if (!mutex_trylock(&si->xswap_lock)) { + put_swap_device(si); + return -EAGAIN; + } + if (swp_offset(swpentry) / SWAPFILE_CLUSTER >= + READ_ONCE(si->nr_clusters_mapped)) { + mutex_unlock(&si->xswap_lock); + put_swap_device(si); + return -ENOENT; + } + mutex_unlock(&si->xswap_lock); } +#endif mpol = get_task_policy(current); folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol, NO_INTERLEAVE_INDEX); + put_swap_device(si); /* @@ -1135,6 +1157,15 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, goto err; } + /* Reserve the destination before dropping the zswap copy. */ + if (is_xswap) { + phys = xswap_backend_alloc(swpentry); + if (!phys.val) { + ret = -ENOMEM; + goto err; + } + } + if (!zswap_decompress(entry, folio)) { ret = -EIO; goto err; @@ -1167,11 +1198,13 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, folio_put(folio); /* start writeback */ - __swap_writeout(ctx, folio, folio->swap); + __swap_writeout(ctx, folio, is_xswap ? phys : folio->swap); return 0; err: + if (is_xswap && phys.val) + xswap_backend_free(swpentry, phys); swap_cache_del_folio(folio); folio_unlock(folio); folio_put(folio); @@ -1207,10 +1240,12 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o void *arg) { struct zswap_entry *entry = container_of(item, struct zswap_entry, lru); + struct swap_info_struct *si; struct zswap_shrink_ctl *ctl = arg; swp_entry_t swpentry; enum lru_status ret = LRU_REMOVED_RETRY; int writeback_result; + bool no_backend; /* * Second chance algorithm: if the entry has its referenced bit set, give it @@ -1256,6 +1291,21 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o */ swpentry = entry->swpentry; + /* + * An xswap entry with no real swap device has nowhere to be + * written back to, so keep it on the LRU for when one appears. + * LRU_ROTATE needs the lru lock and the get/put_swap_device() + * pair does not sleep, so this stays ahead of the unlock. + */ + si = get_swap_device(swpentry); + if (IS_ERR_OR_NULL(si)) + return LRU_ROTATE; + no_backend = (si->flags & SWP_XSWAP) && + !atomic_read(&nr_real_swapfiles); + put_swap_device(si); + if (no_backend) + return LRU_ROTATE; + /* * It's safe to drop the lock here because we return either * LRU_REMOVED_RETRY, LRU_RETRY or LRU_STOP. @@ -1555,8 +1605,12 @@ static bool zswap_store_page(struct folio *folio, long index, return false; } - /* In this phase xswap has no device to write the page back to. */ - can_writeback = !(__swap_entry_to_info(page_swpentry)->flags & SWP_XSWAP); + /* + * An xswap entry can be written back only while a real swap device + * exists to hold its physical backend. + */ + can_writeback = !(__swap_entry_to_info(page_swpentry)->flags & SWP_XSWAP) || + atomic_read(&nr_real_swapfiles); if (!zswap_compress(folio, index, entry, pool, can_writeback)) goto compress_failed; @@ -1617,8 +1671,7 @@ static bool zswap_store_page(struct folio *folio, long index, entry->objcg = objcg; entry->referenced = true; INIT_LIST_HEAD(&entry->lru); - if (can_writeback) - zswap_lru_add(entry); + zswap_lru_add(entry); return true; -- 2.54.0