From: Youngjun Park <youngjun.park@lge.com>
To: akpm@linux-foundation.org
Cc: chrisl@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
kasong@tencent.com, hannes@cmpxchg.org, mhocko@kernel.org,
roman.gushchin@linux.dev, shakeel.butt@linux.dev,
muchun.song@linux.dev, shikemeng@huaweicloud.com,
baoquan.he@linux.dev, baohua@kernel.org, yosry@kernel.org,
joshua.hahnjy@gmail.com, taejoon.song@lge.com,
her0gyugyu@gmail.com, lianux.mm@gmail.com
Subject: [RFC PATCH v11 4/4] mm: swap: filter swap allocation by memcg tier mask
Date: Thu, 17 Sep 2026 03:34:37 +0900 [thread overview]
Message-ID: <20260916183437.2946306-5-youngjun.park@lge.com> (raw)
In-Reply-To: <20260916183437.2946306-1-youngjun.park@lge.com>
Apply the cgroup tier mask during swap slot allocation to enforce
per-cgroup swap tier restrictions.
The folio's mask is looked up once and passed to the fast, slow and
discard paths as a parameter, so all of them act on the same mask even
if the cgroup's mask changes concurrently.
The device tier_mask is read with READ_ONCE() in the two paths that
do not hold swap_lock, matching how si->flags is read in the same
allocator.
In the fast path, check the percpu cached swap_info's tier_mask
against the folio's mask. If it does not match, fall through to the
slow path. In the slow path, skip swap devices whose tier_mask is not
covered by the folio's mask. The discard fallback honors the mask too.
Without it, a discard on a device outside the folio's tiers still
returns true and drives the retry, so the allocation spins through the
loop consuming another device's discard queue while it cannot succeed.
This works correctly when there is only one non-rotational
device in the system and no devices share the same priority.
However, there are known limitations.
- When non-rotational devices are distributed across multiple
tiers, and different memcgs are configured to use those
distinct tiers, they may constantly overwrite the shared
percpu swap cache. This cache thrashing leads to frequent
fast path misses.
- Combined with the above issue, if same-priority devices exist
among them, a percpu cache miss (overwritten by another memcg)
forces the allocator to round-robin to the next device
prematurely, even if the current cluster is not fully
exhausted.
These edge cases do not affect the primary use case of
directing swap traffic per cgroup. Further optimization is
planned for future work.
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swapfile.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index bb953dd33ca0..b246eff25c96 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1364,7 +1364,7 @@ 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 bool swap_alloc_fast(struct folio *folio, unsigned int mask)
{
unsigned int order = folio_order(folio);
struct swap_cluster_info *ci;
@@ -1376,8 +1376,11 @@ static bool swap_alloc_fast(struct folio *folio)
* so checking it's liveness by get_swap_device_info is enough.
*/
si = this_cpu_read(percpu_swap_cluster.si[order]);
+ if (!si || !swap_tiers_mask_test(READ_ONCE(si->tier_mask), mask))
+ return false;
+
offset = this_cpu_read(percpu_swap_cluster.offset[order]);
- if (!si || !offset || !get_swap_device_info(si))
+ if (!offset || !get_swap_device_info(si))
return false;
ci = swap_cluster_lock(si, offset);
@@ -1394,7 +1397,7 @@ static bool swap_alloc_fast(struct folio *folio)
}
/* Rotate the device and switch to a new cluster */
-static void swap_alloc_slow(struct folio *folio)
+static void swap_alloc_slow(struct folio *folio, unsigned int mask)
{
struct swap_info_struct *si, *next;
struct swap_tier *tier;
@@ -1405,6 +1408,9 @@ static void swap_alloc_slow(struct folio *folio)
for_each_active_tier(tier) {
prio = tier->prio;
plist_for_each_entry_safe(si, next, &tier->avail_head, avail_list) {
+ if (!swap_tiers_mask_test(READ_ONCE(si->tier_mask), mask))
+ continue;
+
/* Rotate the device and switch to a new cluster */
plist_requeue(&si->avail_list, &tier->avail_head);
spin_unlock(&swap_avail_lock);
@@ -1439,7 +1445,7 @@ static void swap_alloc_slow(struct folio *folio)
* Discard pending clusters in a synchronized way when under high pressure.
* Return: true if any cluster is discarded.
*/
-static bool swap_sync_discard(void)
+static bool swap_sync_discard(unsigned int mask)
{
bool ret = false;
struct swap_info_struct *si, *next;
@@ -1451,6 +1457,8 @@ static bool swap_sync_discard(void)
for_each_active_tier(tier) {
prio = tier->prio;
plist_for_each_entry_safe(si, next, &tier->active_head, list) {
+ if (!swap_tiers_mask_test(si->tier_mask, mask))
+ continue;
spin_unlock(&swap_lock);
if (get_swap_device_info(si)) {
if (si->flags & SWP_PAGE_DISCARD)
@@ -1749,6 +1757,7 @@ int folio_alloc_swap(struct folio *folio)
{
unsigned int order = folio_order(folio);
unsigned int size = 1 << order;
+ unsigned int mask;
VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio);
@@ -1772,13 +1781,14 @@ int folio_alloc_swap(struct folio *folio)
}
again:
+ mask = folio_tier_mask(folio);
local_lock(&percpu_swap_cluster.lock);
- if (!swap_alloc_fast(folio))
- swap_alloc_slow(folio);
+ if (!swap_alloc_fast(folio, mask))
+ swap_alloc_slow(folio, mask);
local_unlock(&percpu_swap_cluster.lock);
if (!order && unlikely(!folio_test_swapcache(folio))) {
- if (swap_sync_discard())
+ if (swap_sync_discard(mask))
goto again;
}
--
2.48.1
next prev parent reply other threads:[~2026-09-16 18:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-16 18:34 [RFC PATCH v11 0/4] mm/swap: priority-based swap tiers with per-cgroup selection Youngjun Park
2026-09-16 18:34 ` [RFC PATCH v11 1/4] mm: swap: introduce swap tier infrastructure Youngjun Park
2026-09-16 18:34 ` [RFC PATCH v11 2/4] mm: swap: allocate swap slots from swap tiers Youngjun Park
2026-09-16 18:34 ` [RFC PATCH v11 3/4] mm: swap: add a debugfs interface for memcg tier selection Youngjun Park
2026-09-16 18:34 ` Youngjun Park [this message]
2026-09-16 20:04 ` [RFC PATCH v11 0/4] mm/swap: priority-based swap tiers with per-cgroup selection Johannes Weiner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260916183437.2946306-5-youngjun.park@lge.com \
--to=youngjun.park@lge.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baoquan.he@linux.dev \
--cc=cgroups@vger.kernel.org \
--cc=chrisl@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=her0gyugyu@gmail.com \
--cc=joshua.hahnjy@gmail.com \
--cc=kasong@tencent.com \
--cc=lianux.mm@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=shikemeng@huaweicloud.com \
--cc=taejoon.song@lge.com \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®