[RFC v3 07/15] mm, swap: add xswap grow trigger on cluster allocation
Baoquan He <[email protected]>
| Newsgroups | org.kvack.linux-mm,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
When cluster_alloc_swap_entry() fails to find a free cluster and the xswap device still has room to grow, expand the mapped range by XSWAP_GROW_CLUSTERS clusters. Since xswap is always SWP_SOLIDSTATE, no locks need to be dropped before calling xswap_map_clusters() — global_cluster_lock is never held on this path. The grow sequence: 1. Check nr_clusters_mapped < nr_clusters and free list empty 2. Call xswap_map_clusters() to allocate and map more physical pages 3. Add newly mapped clusters to si->free_clusters under si->lock 4. Retry allocation from the fresh free clusters This makes the xswap cluster space grow transparently as swap usage increases, without any userspace intervention. Signed-off-by: Baoquan He <[email protected]> --- mm/swapfile.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/mm/swapfile.c b/mm/swapfile.c index 006241558128..324d873b5f01 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1273,6 +1273,51 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, if (found) goto done; } + +#ifdef CONFIG_XSWAP + /* + * For xswap: if no free cluster was found and more clusters + * can be mapped, grow the cluster_info array and retry. + */ + if (!found && (si->flags & SWP_XSWAP) && + READ_ONCE(si->nr_clusters_mapped) < READ_ONCE(si->nr_clusters) && + list_empty(&si->free_clusters)) { + unsigned long nr_new = min(READ_ONCE(si->nr_clusters) - + READ_ONCE(si->nr_clusters_mapped), + XSWAP_GROW_CLUSTERS); + unsigned long start = READ_ONCE(si->nr_clusters_mapped); + unsigned long i; + + if (!xswap_map_clusters(si, start, nr_new)) { + unsigned long added = 0; + + for (i = start; i < start + nr_new; i++) { + struct swap_cluster_info *ci = &si->cluster_info[i]; + + /* + * A concurrent grower may have already added + * these clusters to the free list. Only add + * clusters that are still off-list (NONE). + * Lock ci->lock first: move_cluster() takes + * si->lock internally. + */ + spin_lock(&ci->lock); + if (ci->flags == CLUSTER_FLAG_NONE) { + move_cluster(si, ci, &si->free_clusters, + CLUSTER_FLAG_FREE); + added++; + } + spin_unlock(&ci->lock); + } + WRITE_ONCE(si->nr_free_tail, + READ_ONCE(si->nr_free_tail) + added); + + /* Retry allocation from the free list */ + found = alloc_swap_scan_list(si, &si->free_clusters, + folio, false); + } + } +#endif done: if (!(si->flags & SWP_SOLIDSTATE)) spin_unlock(&si->global_cluster_lock); -- 2.54.0