To allow per-cgroup swap device selection, make the allocation paths skip swap tiers above a per-memcg maximum. The per-CPU cached cluster is shared by all memcgs, so the fast path is skipped for a memcg with a limit. No limit can be set yet, so there is no functional change. The next patch adds the interface to set it. Signed-off-by: Youngjun Park --- mm/swapfile.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 95928d9f85da..d30ee41f8217 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1439,6 +1439,15 @@ static inline short swap_tier(struct swap_info_struct *si) return si->prio; } +/* + * Highest swap tier the memcg of @folio may allocate from. No limit + * can be set yet. + */ +static inline short swap_memcg_max_tier(struct folio *folio) +{ + return SWAP_TIER_MAX; +} + /* * Fast path try to get swap entries with specified order from current * CPU's swap entry pool (a cluster). @@ -1456,7 +1465,12 @@ 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)) + if (!si || !offset) + return false; + /* The per-CPU cluster is shared by all memcgs, so skip it if limited. */ + if (swap_memcg_max_tier(folio) != SWAP_TIER_MAX) + return false; + if (!get_swap_device_info(si)) return false; ci = swap_cluster_lock(si, offset); @@ -1476,10 +1490,14 @@ static bool swap_alloc_fast(struct folio *folio) static void swap_alloc_slow(struct folio *folio) { struct swap_info_struct *si, *next; + short max = swap_memcg_max_tier(folio); spin_lock(&swap_avail_lock); start_over: plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) { + /* Skip tiers above the memcg limit. */ + if (swap_tier(si) > max) + continue; /* Rotate the device and switch to a new cluster */ plist_requeue(&si->avail_list, &swap_avail_head); spin_unlock(&swap_avail_lock); -- 2.34.1