The physical allocator reports whether the folio entered swapcache. A folio that already occupies a virtual entry needs the newly allocated physical entry, so its swapcache state cannot report the allocation result. Return the allocated entry from the fast and scanning allocation paths. Share their existing allocation and discard retry in folio_alloc_phys_swap(), which the native folio_alloc_swap() path calls immediately. Keep cache installation inside __swap_cluster_alloc_entries(). The folio metadata and cache pointer retain their existing publication and locking. Signed-off-by: Nhat Pham --- mm/swapfile.c | 59 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index aa08111bea62..fa315389399b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1545,12 +1545,14 @@ 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) +static swp_entry_t swap_alloc_fast(struct folio *folio) { unsigned int order = folio_order(folio); struct swap_cluster_info *ci; struct swap_info_struct *si; - unsigned int offset; + unsigned long offset, found = 0; + + lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock); /* * Once allocated, swap_info_struct will never be completely freed, @@ -1559,25 +1561,28 @@ static bool swap_alloc_fast(struct folio *folio) si = this_cpu_read(percpu_swap_cluster.si[order]); offset = this_cpu_read(percpu_swap_cluster.offset[order]); if (!si || !offset || !get_swap_device_info(si)) - return false; + return (swp_entry_t){}; ci = swap_cluster_lock(si, offset); if (ci && cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset = cluster_offset(si, ci); - alloc_swap_scan_cluster(si, ci, folio, offset); + found = alloc_swap_scan_cluster(si, ci, folio, offset); } else if (ci) { swap_cluster_unlock(ci); } put_swap_device(si); - return folio_test_swapcache(folio); + if (found) + return swp_entry(si->type, found); + return (swp_entry_t){}; } /* Rotate the device and switch to a new cluster */ -static void swap_alloc_slow(struct folio *folio) +static swp_entry_t swap_alloc_slow(struct folio *folio) { struct swap_info_struct *si, *next; + unsigned long found; spin_lock(&swap_avail_lock); start_over: @@ -1586,12 +1591,12 @@ static void swap_alloc_slow(struct folio *folio) plist_requeue(&si->avail_list, &swap_avail_head); spin_unlock(&swap_avail_lock); if (get_swap_device_info(si)) { - cluster_alloc_swap_entry(si, folio); + found = cluster_alloc_swap_entry(si, folio); put_swap_device(si); - if (folio_test_swapcache(folio)) - return; + if (found) + return swp_entry(si->type, found); if (folio_test_large(folio)) - return; + return (swp_entry_t){}; } spin_lock(&swap_avail_lock); @@ -1609,6 +1614,7 @@ static void swap_alloc_slow(struct folio *folio) goto start_over; } spin_unlock(&swap_avail_lock); + return (swp_entry_t){}; } /* @@ -1959,6 +1965,23 @@ static bool vswap_alloc(struct folio *folio) return false; } +static swp_entry_t folio_alloc_phys_swap(struct folio *folio) +{ + swp_entry_t entry; + +again: + local_lock(&percpu_swap_cluster.lock); + entry = swap_alloc_fast(folio); + if (!entry.val) + entry = swap_alloc_slow(folio); + local_unlock(&percpu_swap_cluster.lock); + + if (!entry.val && !folio_order(folio) && swap_sync_discard()) + goto again; + + return entry; +} + /** * folio_alloc_swap - allocate swap space for a folio * @folio: folio we want to move to swap @@ -2000,20 +2023,8 @@ int folio_alloc_swap(struct folio *folio) } } - if (may_vswap && vswap_alloc(folio)) - goto done; - -again: - local_lock(&percpu_swap_cluster.lock); - if (!swap_alloc_fast(folio)) - swap_alloc_slow(folio); - local_unlock(&percpu_swap_cluster.lock); - -done: - if (!order && unlikely(!folio_test_swapcache(folio))) { - if (swap_sync_discard()) - goto again; - } + if (!may_vswap || !vswap_alloc(folio)) + folio_alloc_phys_swap(folio); /* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */ if (unlikely(mem_cgroup_try_charge_swap(folio))) { -- 2.53.1-Meta