From: Baoquan He Allow a large order on an xswap device once a block device can back it. The physical slot range has to be contiguous because the folio is read back with a single IO from its first physical slot. If the backend refuses a contiguous slot range, return a split request to reclaim instead of activating the large folio over and over. shmem cannot repair its page cache state on such a return, so it keeps can_split clear. When reclaim cannot enter the fs, a REQUIRE_NOFS block device cannot back the large slot range either. Carry may_enter_fs into the large-backend check and into folio_alloc_swap() so the folio is split instead of allocating a slot range that writeback cannot use. Signed-off-by: Baoquan He --- include/linux/swap_ops.h | 1 + mm/page_io.c | 7 ++++ mm/shmem.c | 2 +- mm/swap.h | 6 ++-- mm/swapfile.c | 77 +++++++++++++++++++++++++++++----------- mm/vmscan.c | 44 ++++++++++++++++++++--- 6 files changed, 110 insertions(+), 27 deletions(-) diff --git a/include/linux/swap_ops.h b/include/linux/swap_ops.h index 3e3f14e3b98f..3962482fad9d 100644 --- a/include/linux/swap_ops.h +++ b/include/linux/swap_ops.h @@ -29,6 +29,7 @@ struct swap_io_ctx { bool in_thrashing; unsigned long in_memstall; bool may_enter_fs; /* this context may enter the fs */ + bool can_split; /* caller can handle a split folio */ }; /* diff --git a/mm/page_io.c b/mm/page_io.c index 71e442a8f744..080255ac7314 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -269,6 +269,13 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio) ctx->may_enter_fs); if (!phys.val) { folio_mark_dirty(folio); + /* + * No contiguous slot range: split instead of activating a + * large folio over and over. Only reclaim can take + * the pieces back. + */ + if (folio_test_large(folio) && ctx->can_split) + return SWAP_WRITEOUT_SPLIT; return AOP_WRITEPAGE_ACTIVATE; } diff --git a/mm/shmem.c b/mm/shmem.c index 344f7edf5e15..f1c4b116c003 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1954,7 +1954,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, folio_mark_uptodate(folio); } - ret = folio_alloc_swap(folio); + ret = folio_alloc_swap(folio, ctx->may_enter_fs); if (!ret) { bool first_swapped = shmem_recalc_inode(inode, 0, nr_pages); int error; diff --git a/mm/swap.h b/mm/swap.h index 9739f9d9e9db..1260b0ac6f41 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -247,7 +247,7 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp); * * folio_put_swap(): does the opposite thing of folio_dup_swap(). */ -int folio_alloc_swap(struct folio *folio); +int folio_alloc_swap(struct folio *folio, bool may_enter_fs); int folio_dup_swap(struct folio *folio, struct page *page); void folio_put_swap(struct folio *folio, struct page *page); @@ -258,6 +258,8 @@ extern void __swap_cluster_free_entries(struct swap_info_struct *si, /* linux/mm/page_io.c */ int sio_pool_init(void); +/* swap_writeout() wants a large folio split; reclaim only. */ +#define SWAP_WRITEOUT_SPLIT 0x80002 void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio); void swap_read_submit(struct swap_io_ctx *ctx); void swap_write_submit(struct swap_io_ctx *ctx); @@ -386,7 +388,7 @@ static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry) return NULL; } -static inline int folio_alloc_swap(struct folio *folio) +static inline int folio_alloc_swap(struct folio *folio, bool may_enter_fs) { return -EINVAL; } diff --git a/mm/swapfile.c b/mm/swapfile.c index 6bc14f5d1618..f1257ae98685 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1372,13 +1372,35 @@ static void swap_reclaim_work(struct work_struct *work) swap_reclaim_full_clusters(si, true); } +#ifdef CONFIG_XSWAP +/* Whether a block device that can back a large xswap folio is available. */ +static bool xswap_has_large_backend(bool may_enter_fs) +{ + struct swap_info_struct *si; + bool found = false; + + spin_lock(&swap_avail_lock); + plist_for_each_entry(si, &swap_avail_head, avail_list) { + if ((si->flags & SWP_XSWAP) || !(si->flags & SWP_BLKDEV)) + continue; + if (!may_enter_fs && (si->ops->flags & SWAP_OPS_F_REQUIRE_NOFS)) + continue; + found = true; + break; + } + spin_unlock(&swap_avail_lock); + + return found; +} +#endif /* CONFIG_XSWAP */ + /* * Try to allocate swap entries with specified order and try set a new * cluster for current CPU too. */ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, struct folio *folio, unsigned int order, - bool no_cache) + bool may_enter_fs, bool no_cache) { struct swap_cluster_info *ci; unsigned long offset = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID; @@ -1388,13 +1410,24 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, /* * Swapfile is not block device so unable - * to allocate large entries. + * to allocate large entries. xswap has no backing file, so it can. */ - if (order && !(si->flags & SWP_BLKDEV)) + if (order && !(si->flags & SWP_BLKDEV) && !(si->flags & SWP_XSWAP)) return 0; #ifdef CONFIG_XSWAP - /* Grow before allocating: mapping pages can sleep. */ + /* A large xswap folio needs a contiguous block device slot range behind it. */ + if (order && (si->flags & SWP_XSWAP) && + atomic_read(&nr_real_swapfiles) && + !xswap_has_large_backend(may_enter_fs)) + return 0; + + /* + * Top the range up early. Every path below leaves through `done`, + * so this has to come first; mapping pages can sleep, and doing it + * from the allocation that would otherwise fail leaves nothing to + * fall back on. + */ if ((si->flags & SWP_XSWAP) && (si->flags & SWP_WRITEOK) && xswap_should_grow(si)) xswap_grow(si, false); @@ -1696,7 +1729,8 @@ static bool get_swap_device_info(struct swap_info_struct *si) * Fast path try to get swap entries with specified order from current * CPU's swap entry pool (a cluster). */ -static bool swap_alloc_fast(struct folio *folio, bool may_zswap) +static bool swap_alloc_fast(struct folio *folio, bool may_zswap, + bool may_enter_fs) { unsigned int order = folio_order(folio); struct swap_cluster_info *ci; @@ -1731,7 +1765,8 @@ static bool swap_alloc_fast(struct folio *folio, bool may_zswap) } /* Rotate the device and switch to a new cluster */ -static void swap_alloc_slow(struct folio *folio, bool may_zswap) +static void swap_alloc_slow(struct folio *folio, bool may_zswap, + bool may_enter_fs) { struct swap_info_struct *si, *next; @@ -1749,7 +1784,7 @@ static void swap_alloc_slow(struct folio *folio, bool may_zswap) spin_unlock(&swap_avail_lock); if (get_swap_device_info(si)) { cluster_alloc_swap_entry(si, folio, folio_order(folio), - !may_zswap); + may_enter_fs, !may_zswap); put_swap_device(si); if (folio_test_swapcache(folio)) return; @@ -2094,18 +2129,20 @@ static bool folio_may_zswap(struct folio *folio) return may_zswap; } -/* Can a non-xswap device still take a slot? */ -static bool real_swap_has_room(void) +/* Can a non-xswap device take a slot in this context? */ +static bool real_swap_has_room(bool may_enter_fs) { struct swap_info_struct *si; bool room = false; spin_lock(&swap_avail_lock); plist_for_each_entry(si, &swap_avail_head, avail_list) { - if (!(si->flags & SWP_XSWAP)) { - room = true; - break; - } + if ((si->flags & SWP_XSWAP) || + (!may_enter_fs && + (si->ops->flags & SWAP_OPS_F_REQUIRE_NOFS))) + continue; + room = true; + break; } spin_unlock(&swap_avail_lock); return room; @@ -2123,7 +2160,7 @@ static bool real_swap_has_room(void) * %-ENOSPC if no global swap space is available, or %-ENOMEM if splitting * would not help. */ -int folio_alloc_swap(struct folio *folio) +int folio_alloc_swap(struct folio *folio, bool may_enter_fs) { unsigned int order = folio_order(folio); unsigned int size = 1 << order; @@ -2165,10 +2202,10 @@ int folio_alloc_swap(struct folio *folio) * A folio that cannot zswap can only use a real device; skip the * scan when none has room. The failure is still reported below. */ - if (may_zswap || real_swap_has_room()) { + if (may_zswap || real_swap_has_room(may_enter_fs)) { local_lock(&percpu_swap_cluster.lock); - if (!swap_alloc_fast(folio, may_zswap)) - swap_alloc_slow(folio, may_zswap); + if (!swap_alloc_fast(folio, may_zswap, may_enter_fs)) + swap_alloc_slow(folio, may_zswap, may_enter_fs); local_unlock(&percpu_swap_cluster.lock); } @@ -2212,7 +2249,7 @@ int folio_alloc_swap(struct folio *folio) * The folio can only use a real device and none has room; splitting * cannot help. */ - if (!may_zswap && !real_swap_has_room()) + if (!may_zswap && !real_swap_has_room(may_enter_fs)) return -ENOMEM; return order ? -E2BIG : -ENOMEM; @@ -2640,7 +2677,7 @@ swp_entry_t swap_alloc_hibernation_slot(int type) swap_cluster_unlock(ci); } if (!offset) - offset = cluster_alloc_swap_entry(si, NULL, 0, false); + offset = cluster_alloc_swap_entry(si, NULL, 0, true, false); local_unlock(&percpu_swap_cluster.lock); if (offset) entry = swp_entry(si->type, offset); @@ -4691,7 +4728,7 @@ static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si, } } if (offset == SWAP_ENTRY_INVALID) - offset = cluster_alloc_swap_entry(si, NULL, order, true); + offset = cluster_alloc_swap_entry(si, NULL, order, true, true); local_unlock(&percpu_swap_cluster.lock); return offset; diff --git a/mm/vmscan.c b/mm/vmscan.c index c3ba8050ed89..74920609a2b9 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -613,10 +613,46 @@ static pageout_t pageout(struct swap_io_ctx *ctx, struct address_space *mapping, * or we failed to allocate contiguous swap entries, in which case * the split out folios get added back to folio_list. */ - if (shmem_mapping(mapping)) + if (shmem_mapping(mapping)) { + /* + * shmem_writeout() cannot repair its page cache state on a + * split return. ctx is reused across folios, so reset it + * every call. + */ + ctx->can_split = false; res = shmem_writeout(ctx, folio, folio_list); - else + } else { + ctx->can_split = true; res = swap_writeout(ctx, folio); + } + + /* + * The backend refused a contiguous range. Split here; nothing else + * will, and an activated large folio just comes back. + */ + if (res == SWAP_WRITEOUT_SPLIT) { + int order = folio_order(folio); + + /* + * The slots exist but no backend is recorded yet, so the + * split only has to re-point each piece at its own slot. + */ + if (!split_folio_to_list(folio, folio_list)) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (order >= HPAGE_PMD_ORDER) { + count_memcg_folio_events(folio, + THP_SWPOUT_FALLBACK, 1); + count_vm_event(THP_SWPOUT_FALLBACK); + } +#endif + count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK); + /* Pieces are queued; the folio is still locked. */ + folio_clear_reclaim(folio); + return PAGE_ACTIVATE; + } + folio_mark_dirty(folio); + res = AOP_WRITEPAGE_ACTIVATE; + } if (res < 0) handle_write_error(mapping, folio, res); @@ -1587,7 +1623,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, split_folio_to_list(folio, folio_list)) goto activate_locked; } - ret = folio_alloc_swap(folio); + ret = folio_alloc_swap(folio, ctx.may_enter_fs); if (ret) { int __maybe_unused order = folio_order(folio); @@ -1606,7 +1642,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, } #endif count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK); - if (folio_alloc_swap(folio)) + if (folio_alloc_swap(folio, ctx.may_enter_fs)) goto activate_locked_split; } /* -- 2.54.0