Skip to contentSkip to conversation

mm/swap, memcg: Introduce swap tiers for cgroup based swap control

RFC PATCH Superseded Youngjun Park 4 participants 34 messages Jan 26 – Mar 18

Subsystems (6)

v2 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Youngjun Park

author reviewer
#0 lore raw

v2 cover mm/swap, memcg: Introduce swap tiers for cgroup based swap control

This is the second version of the RFC for the "Swap Tiers" concept.
Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

This version incorporates feedback received during LPC 2025 and addresses comments from the previous review. We have also included experimental results based on usage scenarios intended for our internal platforms.

Motivation & Concept recap
==========================
Current Linux swap allocation is global, limiting the ability to assign faster devices to specific cgroups. Our initial attempt at per-cgroup priorities proved over-engineered and caused LRU inversion.

Following Chris Li's suggestion, we pivoted to "Swap Tiers." A tier is simply a user-named group of swap devices sharing the same priority range. This abstraction facilitates swap device selection based on speed, allowing users to configure specific tiers for cgroups.

For more details, please refer to the LPC 2025 presentation
https://lpc.events/event/19/contributions/2141/attachments/1857/3998/LPC2025Finalss.pdf
or v1 patch.

Changes in v2
=============
1. Respect cgroup hierarchy principle (LPC 2025 feedback)
- The logic now strictly follows standard cgroup hierarchy principles.

Previous: Children could select any tier using "+" regardless of the parent's configuration. "+" tier is referenced. (could not be silently disappeared)

Current: The explicit selection ("+") concept is removed. By default, all tiers are selected. Users now use "-" to exclude specific tiers. Excluded tier could disappeared silently.
A child cgroup is always a subset of its parent. Even if a child re-enables a tier with "+" that was excluded by the parent, the effective tier list is limited to the parent's allowed subset.

Example:
Global Tiers: SSD, HDD, NET
Parent: SSD, NET (HDD excluded)
Child: HDD, NET (SSD excluded)
-> Effective Child Tier: NET (Intersection of Parent and Child)

2. Simplified swap_tier structure (Chris Li)
- Replaced 'end prio' and priority lists with standard list_head.

3. Reference counting removed
- Removed refcount for swap_tiers. Liveness is now guaranteed by checking if the swap device is in use.
- Since the default selection is "ALL", holding references for disappearing tiers is unnecessary. only exclusions ("-") matter.

4. Support mixed operation (/sys/kernel/mm/swap/tiers) (Chris Li)
- Supports mixed use of "+" and "-" operations.

5. Add modify operation
- Introduced an operation to modify the priority of existing tiers.
Format: "tier_name:priority"

6. Restore swap device cluster allocation rule on same swap device priority (Kairui Song, Baoquan He)
- Preserve existing fastpath and slowpath swap allocation logic using percpu swap device cache.
- Therefore, the swap device cluster allocation rule is preserved on same swap device priority

7. Remove compile time selection (Chris Li)
- Removed CONFIG_SWAP_TIER. this is now a base kernel feature.

8. Cgroup tier calculation logic update
- The effective swap tier for a cgroup is now calculated at the time of configuration (writing to swap.tiers), rather than at the time of swap allocation.

9. Commit reorganization (Chris Li)
- Commit order reorganized for clarity.

10. Documentation (Chris Li)
- Added documentation for the Swap Tiers concept and usage, as explained in the RFC.

Apply and Benchmark
===================
1. Real-world Scenario: App Preloading
We applied this patchset to our embedded platform to enable application preloading for faster launch times. Since the platform uses flash storage as the default swap device, we aim to minimize swap usage on it to extend its lifespan.

To achieve this, we utilized an idle device (configured via NBD) as a separate swap tier specifically for these preloaded applications.

While it is self-evident that restoring from swap (warm launch) is faster than a cold launch, the data below demonstrates the latency reduction achieved in this environment.

Streaming App A:
Before (Cold Launch): 13.17s
After (Preloaded): 4.18s (68% reduction)

Streaming App B:
Before (Cold Launch): 5.60s
After (Preloaded): 1.12s (80% reduction)

E-commerce App C:
Before (Cold Launch): 10.25s
After (Preloaded): 2.00s (80% reduction)

We have a plan to solidify this usage and expand the usage.

2. Microbenchmarks
In response to feedback regarding potential regressions in the swap fastpath (specifically concerning the overhead of global percpu clusters vs. swap device percpu cache), we addressed this in RFC v2.

By preserving the existing fastpath and slowpath mechanisms via the per-cpu swap device cache, we ensured that the performance characteristics remain unchanged. The simple benchmark results below confirm there is no significant difference.

A. Build kernel test:
Test using 128GB swapfile on Simulated SSD, Qemu VM with 4 CPUs, 4GB RAM, avg of 5 test runs:

Before After
System time: 1584.20s 1590.74s (+0.41%)

Considering the deviation between max/min values, there seems to be no significant difference.

B. vm-scalability
usemem --init-time -O -y -x -n 32 256M (qemu, 16G memory, global pressure, simulated SDD as swap), avg of 5 test runs:

                           Before          After
System time:               588.48 s        592.15 s
Sum Throughput:            16.65 MB/s      15.95 MB/s
Single process Throughput: 0.52 MB/s       0.50 MB/s
Avg Free latency:          1098422.97 us   1106388.97 us

The results indicate that the performance remains stable with negligible variance.

Any feedback is welcome.

Thanks,
Youngjun Park

Youngjun Park (5):
mm: swap: introduce swap tier infrastructure
mm: swap: associate swap devices with tiers
mm: memcontrol: add interface for swap tier selection mm, swap: change back to use each swap device's percpu cluster mm, swap: introduce percpu swap device cache to avoid fragmentation

 Documentation/admin-guide/cgroup-v2.rst |  27 ++
 Documentation/mm/swap-tier.rst          | 109 ++++++
 MAINTAINERS                             |   2 +
 include/linux/memcontrol.h              |   3 +-
 include/linux/swap.h                    |  17 +-
 mm/Makefile                             |   2 +-
 mm/memcontrol.c                         |  80 +++++
 mm/swap.h                               |   4 +
 mm/swap_state.c                         |  70 ++++
 mm/swap_tier.c                          | 452 ++++++++++++++++++++++++
 mm/swap_tier.h                          |  70 ++++
 mm/swapfile.c                           | 132 +++----
 12 files changed, 900 insertions(+), 68 deletions(-)
 create mode 100644 Documentation/mm/swap-tier.rst
 create mode 100644 mm/swap_tier.c
 create mode 100644 mm/swap_tier.h

base-commit: 5a3704ed2dce0b54a7f038b765bb752b87ee8cc2

Signature · 1 line
2.34.1
author reviewer
#1 lore raw

RFC PATCH v2 1/5 mm: swap: introduce swap tier infrastructure

This patch introduces the "Swap tier" concept, which serves as an abstraction layer for managing swap devices based on their performance characteristics (e.g., NVMe, HDD, Network swap).

Swap tiers are user-named groups representing priority ranges. These tiers collectively cover the entire priority space from -1 (DEF_SWAP_PRIO) to SHRT_MAX.

To configure tiers, a new sysfs interface is exposed at /sys/kernel/mm/swap/tiers. The input parser evaluates commands from left to right and supports batch input, allowing users to add, remove or modify multiple tiers in a single write operation.

Tier management enforces continuous priority ranges anchored by start priorities. Operations trigger range splitting or merging, but overwriting start priorities is forbidden. Merging expands lower tiers upwards to preserve configured start priorities, except when removing DEF_SWAP_PRIO, which merges downwards.

---
 MAINTAINERS     |   2 +
 mm/Makefile     |   2 +-
 mm/swap.h       |   4 +
 mm/swap_state.c |  70 +++++++++++
 mm/swap_tier.c  | 304 ++++++++++++++++++++++++++++++++++++++++++++++++
 mm/swap_tier.h  |  38 ++++++
 mm/swapfile.c   |   7 +-
 7 files changed, 423 insertions(+), 4 deletions(-)
 create mode 100644 mm/swap_tier.c
 create mode 100644 mm/swap_tier.h
MAINTAINERS +2 -0
index 18d1ebf053db..501bf46adfb4 100644
@@ -16743,6 +16743,8 @@ F:	mm/swap.c
 F:	mm/swap.h
 F:	mm/swap_table.h
 F:	mm/swap_state.c
+F:	mm/swap_tier.c
+F:	mm/swap_tier.h
 F:	mm/swapfile.c
 
 MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)
mm/Makefile +1 -1
index 53ca5d4b1929..3b3de2de7285 100644
@@ -75,7 +75,7 @@ ifdef CONFIG_MMU
 	obj-$(CONFIG_ADVISE_SYSCALLS)	+= madvise.o
 endif
 
-obj-$(CONFIG_SWAP)	+= page_io.o swap_state.o swapfile.o
+obj-$(CONFIG_SWAP)	+= page_io.o swap_state.o swapfile.o swap_tier.o
 obj-$(CONFIG_ZSWAP)	+= zswap.o
 obj-$(CONFIG_HAS_DMA)	+= dmapool.o
 obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o hugetlb_sysfs.o hugetlb_sysctl.o
mm/swap.h +4 -0
index bfafa637c458..55f230cbe4e7 100644
@@ -16,6 +16,10 @@ extern int page_cluster;
 #define swap_entry_order(order)	0
 #endif
 
+#define DEF_SWAP_PRIO  -1
+
+extern spinlock_t swap_lock;
+extern struct plist_head swap_active_head;
 extern struct swap_info_struct *swap_info[];
 
 /*
mm/swap_state.c +70 -0

84 lines not on this page · raw message

mm/swap_tier.c new file +304 -0

304 lines not on this page · raw message

mm/swap_tier.h new file +38 -0
new file mode 100644
index 000000000000..4b1b0602d691
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _SWAP_TIER_H
+#define _SWAP_TIER_H
+
+#include <linux/types.h>
+#include <linux/spinlock.h>
+
+#define MAX_TIERNAME		16
+
+/* Ensure MAX_SWAPTIER does not exceed MAX_SWAPFILES */
+#if 8 > MAX_SWAPFILES
+#define MAX_SWAPTIER		MAX_SWAPFILES
+#else
+#define MAX_SWAPTIER		8
+#endif
+
+extern spinlock_t swap_tier_lock;
+
+struct swap_tier_save_ctx {
+	char name[MAX_TIERNAME];
+	short prio;
+};
+
+#define DEFINE_SWAP_TIER_SAVE_CTX(_name) \
+	struct swap_tier_save_ctx _name[MAX_SWAPTIER] = {0}
+
+/* Initialization and application */
+void swap_tiers_init(void);
+ssize_t swap_tiers_sysfs_show(char *buf);
+
+int swap_tiers_add(const char *name, int prio);
+int swap_tiers_remove(const char *name);
+int swap_tiers_modify(const char *name, int prio);
+
+void swap_tiers_save(struct swap_tier_save_ctx ctx[]);
+void swap_tiers_restore(struct swap_tier_save_ctx ctx[]);
+bool swap_tiers_validate(void);
+#endif /* _SWAP_TIER_H */
mm/swapfile.c +4 -3
index 7b055f15d705..c27952b41d4f 100644
@@ -50,6 +50,7 @@
 #include "internal.h"
 #include "swap_table.h"
 #include "swap.h"
+#include "swap_tier.h"
 
 static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
 				 unsigned char);
@@ -65,7 +66,7 @@ static void move_cluster(struct swap_info_struct *si,
 			 struct swap_cluster_info *ci, struct list_head *list,
 			 enum swap_cluster_flags new_flags);
 
-static DEFINE_SPINLOCK(swap_lock);
+DEFINE_SPINLOCK(swap_lock);
 static unsigned int nr_swapfiles;
 atomic_long_t nr_swap_pages;
 /*
@@ -76,7 +77,6 @@ atomic_long_t nr_swap_pages;
 EXPORT_SYMBOL_GPL(nr_swap_pages);
 /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
 long total_swap_pages;
-#define DEF_SWAP_PRIO  -1
 unsigned long swapfile_maximum_size;
 #ifdef CONFIG_MIGRATION
 bool swap_migration_ad_supported;
@@ -89,7 +89,7 @@ static const char Bad_offset[] = "Bad swap offset entry ";
  * all active swap_info_structs
  * protected with swap_lock, and ordered by priority.
  */
-static PLIST_HEAD(swap_active_head);
+PLIST_HEAD(swap_active_head);
 
 /*
  * all available (active, not full) swap_info_structs
@@ -3977,6 +3977,7 @@ static int __init swapfile_init(void)
 		swap_migration_ad_supported = true;
 #endif	/* CONFIG_MIGRATION */
 
+	swap_tiers_init();
 	return 0;
 }
 subsys_initcall(swapfile_init);
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [RFC PATCH v2 v2 1/5] mm: swap: introduce swap tier infrastructure

Hi Yongjun,

Quoted 129 lines · Youngjun Park

This patch introduces the "Swap tier" concept, which serves as an abstraction layer for managing swap devices based on their performance characteristics (e.g., NVMe, HDD, Network swap).

Swap tiers are user-named groups representing priority ranges. These tiers collectively cover the entire priority space from -1 (DEF_SWAP_PRIO) to SHRT_MAX.

To configure tiers, a new sysfs interface is exposed at /sys/kernel/mm/swap/tiers. The input parser evaluates commands from left to right and supports batch input, allowing users to add, remove or modify multiple tiers in a single write operation.

Tier management enforces continuous priority ranges anchored by start priorities. Operations trigger range splitting or merging, but overwriting start priorities is forbidden. Merging expands lower tiers upwards to preserve configured start priorities, except when removing DEF_SWAP_PRIO, which merges downwards.

---
 MAINTAINERS     |   2 +
 mm/Makefile     |   2 +-
 mm/swap.h       |   4 +
 mm/swap_state.c |  70 +++++++++++
 mm/swap_tier.c  | 304 ++++++++++++++++++++++++++++++++++++++++++++++++
 mm/swap_tier.h  |  38 ++++++
 mm/swapfile.c   |   7 +-
 7 files changed, 423 insertions(+), 4 deletions(-)
 create mode 100644 mm/swap_tier.c
 create mode 100644 mm/swap_tier.h
MAINTAINERS +2 -0
index 18d1ebf053db..501bf46adfb4 100644
@@ -16743,6 +16743,8 @@ F:      mm/swap.c
 F:     mm/swap.h
 F:     mm/swap_table.h
 F:     mm/swap_state.c
+F:     mm/swap_tier.c
+F:     mm/swap_tier.h
 F:     mm/swapfile.c
 
 MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)
mm/Makefile +1 -1
index 53ca5d4b1929..3b3de2de7285 100644
@@ -75,7 +75,7 @@ ifdef CONFIG_MMU
        obj-$(CONFIG_ADVISE_SYSCALLS)   += madvise.o
 endif
 
-obj-$(CONFIG_SWAP)     += page_io.o swap_state.o swapfile.o
+obj-$(CONFIG_SWAP)     += page_io.o swap_state.o swapfile.o swap_tier.o
 obj-$(CONFIG_ZSWAP)    += zswap.o
 obj-$(CONFIG_HAS_DMA)  += dmapool.o
 obj-$(CONFIG_HUGETLBFS)        += hugetlb.o hugetlb_sysfs.o hugetlb_sysctl.o
mm/swap.h +4 -0
index bfafa637c458..55f230cbe4e7 100644
@@ -16,6 +16,10 @@ extern int page_cluster;
 #define swap_entry_order(order)        0
 #endif
 
+#define DEF_SWAP_PRIO  -1
+
+extern spinlock_t swap_lock;
+extern struct plist_head swap_active_head;
 extern struct swap_info_struct *swap_info[];
 
 /*
mm/swap_state.c +39 -0
index 6d0eef7470be..f1a7d9cdc648 100644
@@ -25,6 +25,7 @@
 #include "internal.h"
 #include "swap_table.h"
 #include "swap.h"
+#include "swap_tier.h"
 
 /*
  * swapper_space is a fiction, retained to simplify the path through
@@ -947,8 +948,77 @@ static ssize_t vma_ra_enabled_store(struct kobject *kobj,
 }
 static struct kobj_attribute vma_ra_enabled_attr = __ATTR_RW(vma_ra_enabled);
 
+static ssize_t tiers_show(struct kobject *kobj,
+                                    struct kobj_attribute *attr, char *buf)
+{
+       return swap_tiers_sysfs_show(buf);
+}
+
+static ssize_t tiers_store(struct kobject *kobj,
+                       struct kobj_attribute *attr,
+                       const char *buf, size_t count)
+{
+       char *p, *token, *name, *tmp;
+       int ret = 0;
+       short prio;
+       DEFINE_SWAP_TIER_SAVE_CTX(ctx);
+
+       tmp = kstrdup(buf, GFP_KERNEL);
+       if (!tmp)
+               return -ENOMEM;
+
+       spin_lock(&swap_lock);
+       spin_lock(&swap_tier_lock);
+
+       p = tmp;
+       swap_tiers_save(ctx);
+
+       while (!ret && (token = strsep(&p, ", \t\n")) != NULL) {
+               if (!*token)
+                       continue;
+
+               if (token[0] == '-') {
+                       ret = swap_tiers_remove(token + 1);
+               } else {
+
+                       name = strsep(&token, ":");
+                       if (!token || kstrtos16(token, 10, &prio)) {
+                               ret = -EINVAL;
+                               goto out;
+                       }

mm/swap_state.c static ssize_t vma_ra_enabled_store(struct kobject *kobj, · line 998

Quoted 10 lines
mm/swap_state.c +10 -0
+
+                       if (name[0] == '+')
+                               ret = swap_tiers_add(name + 1, prio);
+                       else
+                               ret = swap_tiers_modify(name, prio);
+               }
+
+               if (ret)
+                       goto restore;
+       }

This function can use some simplification to make the indentation flater.

Quoted 44 lines
mm/swap_state.c +21 -0
+
+       if (!swap_tiers_validate()) {
+               ret = -EINVAL;
+               goto restore;
+       }
+
+out:
+       spin_unlock(&swap_tier_lock);
+       spin_unlock(&swap_lock);
+
+       kfree(tmp);
+       return ret ? ret : count;
+
+restore:
+       swap_tiers_restore(ctx);
+       goto out;
+}
+
+static struct kobj_attribute tier_attr = __ATTR_RW(tiers);
+
 static struct attribute *swap_attrs[] = {
        &vma_ra_enabled_attr.attr,
+       &tier_attr.attr,
        NULL,
 };
 
mm/swap_tier.c new file +12 -0
new file mode 100644
index 000000000000..87882272eec8
@@ -0,0 +1,304 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/swap.h>
+#include <linux/memcontrol.h>
+#include "memcontrol-v1.h"
+#include <linux/sysfs.h>
+#include <linux/plist.h>
+
+#include "swap.h"
+#include "swap_tier.h"
+
+/*
+ * struct swap_tier - structure representing a swap tier.

mm/swap_tier.c line 22

Quoted 10 lines
mm/swap_tier.c new file +10 -0
+ *
+ * @name: name of the swap_tier.
+ * @prio: starting value of priority.
+ * @list: linked list of tiers.
+*/
+static struct swap_tier {
+       char name[MAX_TIERNAME];
+       short prio;
+       struct list_head list;
+} swap_tiers[MAX_SWAPTIER];

We can have a CONFIG option for the MAX_SWAPTIER. I think the default should be a small number like 4.

Quoted 132 lines
mm/swap_tier.c new file +132 -0
+
+DEFINE_SPINLOCK(swap_tier_lock);
+/* active swap priority list, sorted in descending order */
+static LIST_HEAD(swap_tier_active_list);
+/* unused swap_tier object */
+static LIST_HEAD(swap_tier_inactive_list);
+
+#define TIER_IDX(tier) ((tier) - swap_tiers)
+#define TIER_MASK(tier)        (1 << TIER_IDX(tier))
+#define TIER_INVALID_PRIO (DEF_SWAP_PRIO - 1)
+#define TIER_END_PRIO(tier) \
+       (!list_is_first(&(tier)->list, &swap_tier_active_list) ? \
+       list_prev_entry((tier), list)->prio - 1 : SHRT_MAX)
+
+#define for_each_tier(tier, idx) \
+       for (idx = 0, tier = &swap_tiers[0]; idx < MAX_SWAPTIER; \
+               idx++, tier = &swap_tiers[idx])
+
+#define for_each_active_tier(tier) \
+       list_for_each_entry(tier, &swap_tier_active_list, list)
+
+#define for_each_inactive_tier(tier) \
+       list_for_each_entry(tier, &swap_tier_inactive_list, list)
+
+/*
+ * Naming Convention:
+ *   swap_tiers_*() - Public/exported functions
+ *   swap_tier_*()  - Private/internal functions
+ */
+
+static bool swap_tier_is_active(void)
+{
+       return !list_empty(&swap_tier_active_list) ? true : false;
+}
+
+static struct swap_tier *swap_tier_lookup(const char *name)
+{
+       struct swap_tier *tier;
+
+       for_each_active_tier(tier) {
+               if (!strcmp(tier->name, name))
+                       return tier;
+       }
+
+       return NULL;
+}
+
+void swap_tiers_init(void)
+{
+       struct swap_tier *tier;
+       int idx;
+
+       BUILD_BUG_ON(BITS_PER_TYPE(int) < MAX_SWAPTIER);
+
+       for_each_tier(tier, idx) {
+               INIT_LIST_HEAD(&tier->list);
+               list_add_tail(&tier->list, &swap_tier_inactive_list);
+       }
+}
+
+ssize_t swap_tiers_sysfs_show(char *buf)
+{
+       struct swap_tier *tier;
+       ssize_t len = 0;
+
+       len += sysfs_emit_at(buf, len, "%-16s %-5s %-11s %-11s\n",
+                        "Name", "Idx", "PrioStart", "PrioEnd");
+
+       spin_lock(&swap_tier_lock);
+       for_each_active_tier(tier) {
+               len += sysfs_emit_at(buf, len, "%-16s %-5ld %-11d %-11d\n",
+                                    tier->name,
+                                    TIER_IDX(tier),
+                                    tier->prio,
+                                    TIER_END_PRIO(tier));
+               if (len >= PAGE_SIZE)
+                       break;
+       }
+       spin_unlock(&swap_tier_lock);
+
+       return len;
+}
+
+static void swap_tier_insert_by_prio(struct swap_tier *new)
+{
+       struct swap_tier *tier;
+
+       for_each_active_tier(tier) {
+               if (tier->prio > new->prio)
+                       continue;
+
+               list_add_tail(&new->list, &tier->list);
+               return;
+       }
+       /* First addition, or becomes the first tier */
+       list_add_tail(&new->list, &swap_tier_active_list);
+}
+
+static void __swap_tier_prepare(struct swap_tier *tier, const char *name,
+       short prio)
+{
+       list_del_init(&tier->list);
+       strscpy(tier->name, name, MAX_TIERNAME);
+       tier->prio = prio;
+}
+
+static struct swap_tier *swap_tier_prepare(const char *name, short prio)
+{
+       struct swap_tier *tier;
+
+       lockdep_assert_held(&swap_tier_lock);
+
+       if (prio < DEF_SWAP_PRIO)
+               return NULL;
+
+       if (list_empty(&swap_tier_inactive_list))
+               return ERR_PTR(-EPERM);
+
+       tier = list_first_entry(&swap_tier_inactive_list,
+               struct swap_tier, list);
+
+       __swap_tier_prepare(tier, name, prio);
+       return tier;
+}
+
+static int swap_tier_check_range(short prio)
+{
+       struct swap_tier *tier;
+
+       lockdep_assert_held(&swap_lock);
+       lockdep_assert_held(&swap_tier_lock);
+

mm/swap_tier.c line 164

Quoted 10 lines
mm/swap_tier.c new file +10 -0
+       for_each_active_tier(tier) {
+               /* No overwrite */
+               if (tier->prio == prio)
+                       return -EINVAL;
+       }
+
+       return 0;
+}
+
+int swap_tiers_add(const char *name, int prio)

When we add, modify, remove a tier. The simple case is there is no swap file under any tiers.
But if the modification causes some swap files to jump to different tiers. That might be problematic.

Quoted 70 lines
mm/swap_tier.c new file +70 -0
+{
+       int ret;
+       struct swap_tier *tier;
+
+       lockdep_assert_held(&swap_lock);
+       lockdep_assert_held(&swap_tier_lock);
+
+       /* Duplicate check */
+       if (swap_tier_lookup(name))
+               return -EPERM;
+
+       ret = swap_tier_check_range(prio);
+       if (ret)
+               return ret;
+
+       tier = swap_tier_prepare(name, prio);
+       if (IS_ERR(tier)) {
+               ret = PTR_ERR(tier);
+               return ret;
+       }
+
+
+       swap_tier_insert_by_prio(tier);
+       return ret;
+}
+
+int swap_tiers_remove(const char *name)
+{
+       int ret = 0;
+       struct swap_tier *tier;
+
+       lockdep_assert_held(&swap_lock);
+       lockdep_assert_held(&swap_tier_lock);
+
+       tier = swap_tier_lookup(name);
+       if (!tier)
+               return -EINVAL;
+
+       list_move(&tier->list, &swap_tier_inactive_list);
+
+       /* Removing DEF_SWAP_PRIO merges into the higher tier. */
+       if (swap_tier_is_active() && tier->prio == DEF_SWAP_PRIO)
+               list_prev_entry(tier, list)->prio = DEF_SWAP_PRIO;
+
+       return ret;
+}
+
+int swap_tiers_modify(const char *name, int prio)
+{
+       int ret;
+       struct swap_tier *tier;
+
+       lockdep_assert_held(&swap_lock);
+       lockdep_assert_held(&swap_tier_lock);
+
+       tier = swap_tier_lookup(name);
+       if (!tier)
+               return -EINVAL;
+
+       /* No need to modify */
+       if (tier->prio == prio)
+               return 0;
+
+       ret = swap_tier_check_range(prio);
+       if (ret)
+               return ret;
+
+       list_del_init(&tier->list);
+       tier->prio = prio;
+       swap_tier_insert_by_prio(tier);

mm/swap_tier.c line 244

Quoted 10 lines
mm/swap_tier.c new file +10 -0
+
+       return ret;
+}
+
+/*
+ * XXX: Reverting individual operations becomes complex as the number of
+ * operations grows. Instead, we save the original state beforehand and
+ * fully restore it if any operation fails.
+ */
+void swap_tiers_save(struct swap_tier_save_ctx ctx[])

I really hope we don't have to do the save and restore thing. Is there another design we can simplify this?

Quoted 134 lines
mm/swap_tier.c new file +60 -0
+{
+       struct swap_tier *tier;
+       int idx;
+
+       lockdep_assert_held(&swap_lock);
+       lockdep_assert_held(&swap_tier_lock);
+
+       for_each_active_tier(tier) {
+               idx = TIER_IDX(tier);
+               strcpy(ctx[idx].name, tier->name);
+               ctx[idx].prio = tier->prio;
+       }
+
+       for_each_inactive_tier(tier) {
+               idx = TIER_IDX(tier);
+               /* Indicator of inactive */
+               ctx[idx].prio = TIER_INVALID_PRIO;
+       }
+}
+
+void swap_tiers_restore(struct swap_tier_save_ctx ctx[])
+{
+       struct swap_tier *tier;
+       int idx;
+
+       lockdep_assert_held(&swap_lock);
+       lockdep_assert_held(&swap_tier_lock);
+
+       /* Invalidate active list */
+       list_splice_tail_init(&swap_tier_active_list,
+                       &swap_tier_inactive_list);
+
+       for_each_tier(tier, idx) {
+               if (ctx[idx].prio != TIER_INVALID_PRIO) {
+                       /* Preserve idx(mask) */
+                       __swap_tier_prepare(tier, ctx[idx].name, ctx[idx].prio);
+                       swap_tier_insert_by_prio(tier);
+               }
+       }
+}
+
+bool swap_tiers_validate(void)
+{
+       struct swap_tier *tier;
+
+       /*
+        * Initial setting might not cover DEF_SWAP_PRIO.
+        * Swap tier must cover the full range (DEF_SWAP_PRIO to SHRT_MAX).
+        * Also, modify operation can change only one remaining priority.
+        */
+       if (swap_tier_is_active()) {
+               tier = list_last_entry(&swap_tier_active_list,
+                       struct swap_tier, list);
+
+               if (tier->prio != DEF_SWAP_PRIO)
+                       return false;
+       }
+
+       return true;
+}
mm/swap_tier.h new file +38 -0
new file mode 100644
index 000000000000..4b1b0602d691
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _SWAP_TIER_H
+#define _SWAP_TIER_H
+
+#include <linux/types.h>
+#include <linux/spinlock.h>
+
+#define MAX_TIERNAME           16
+
+/* Ensure MAX_SWAPTIER does not exceed MAX_SWAPFILES */
+#if 8 > MAX_SWAPFILES
+#define MAX_SWAPTIER           MAX_SWAPFILES
+#else
+#define MAX_SWAPTIER           8
+#endif
+
+extern spinlock_t swap_tier_lock;
+
+struct swap_tier_save_ctx {
+       char name[MAX_TIERNAME];
+       short prio;
+};
+
+#define DEFINE_SWAP_TIER_SAVE_CTX(_name) \
+       struct swap_tier_save_ctx _name[MAX_SWAPTIER] = {0}
+
+/* Initialization and application */
+void swap_tiers_init(void);
+ssize_t swap_tiers_sysfs_show(char *buf);
+
+int swap_tiers_add(const char *name, int prio);
+int swap_tiers_remove(const char *name);
+int swap_tiers_modify(const char *name, int prio);
+
+void swap_tiers_save(struct swap_tier_save_ctx ctx[]);
+void swap_tiers_restore(struct swap_tier_save_ctx ctx[]);
+bool swap_tiers_validate(void);
+#endif /* _SWAP_TIER_H */
mm/swapfile.c +2 -2
index 7b055f15d705..c27952b41d4f 100644
@@ -50,6 +50,7 @@
 #include "internal.h"
 #include "swap_table.h"
 #include "swap.h"
+#include "swap_tier.h"
 
 static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
                                 unsigned char);
@@ -65,7 +66,7 @@ static void move_cluster(struct swap_info_struct *si,
                         struct swap_cluster_info *ci, struct list_head *list,
                         enum swap_cluster_flags new_flags);
 
-static DEFINE_SPINLOCK(swap_lock);
+DEFINE_SPINLOCK(swap_lock);
 static unsigned int nr_swapfiles;
 atomic_long_t nr_swap_pages;
 /*
@@ -76,7 +77,6 @@ atomic_long_t nr_swap_pages;
 EXPORT_SYMBOL_GPL(nr_swap_pages);
 /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
 long total_swap_pages;
-#define DEF_SWAP_PRIO  -1
 unsigned long swapfile_maximum_size;
 #ifdef CONFIG_MIGRATION
 bool swap_migration_ad_supported;

mm/swapfile.c static const char Bad_offset[] = "Bad swap offset entry "; · line 92

Quoted 5 lines
mm/swapfile.c +1 -1
@@ -89,7 +89,7 @@ static const char Bad_offset[] = "Bad swap offset entry ";
  * all active swap_info_structs
  * protected with swap_lock, and ordered by priority.
  */
-static PLIST_HEAD(swap_active_head);
+PLIST_HEAD(swap_active_head);

One idea is to make each tier have swap_active_head. So different swap entry releases on different tiers don't need to be competing on the same swap_active_head.

That will require the swapfile don't jump to another tiers.

Chris

author reviewer
#3 lore raw

Re: [RFC PATCH v2 v2 1/5] mm: swap: introduce swap tier infrastructure

Quoted 24 lines · Chris Li
Quoted 24 lines
mm/swap_state.c +24 -0
+
+       spin_lock(&swap_lock);
+       spin_lock(&swap_tier_lock);
+
+       p = tmp;
+       swap_tiers_save(ctx);
+
+       while (!ret && (token = strsep(&p, ", \t\n")) != NULL) {
+               if (!*token)
+                       continue;
+
+               if (token[0] == '-') {
+                       ret = swap_tiers_remove(token + 1);
+               } else {
+
+                       name = strsep(&token, ":");
+                       if (!token || kstrtos16(token, 10, &prio)) {
+                               ret = -EINVAL;
+                               goto out;
+                       }
+
+                       if (name[0] == '+')
+                               ret = swap_tiers_add(name + 1, prio);
+                       else
Quoted 8 lines

mm/swap_state.c static ssize_t vma_ra_enabled_store(struct kobject *kobj, · line 998

Quoted 6 lines
mm/swap_state.c +6 -0
+                               ret = swap_tiers_modify(name, prio);
+               }
+
+               if (ret)
+                       goto restore;
+       }

This function can use some simplification to make the indentation flater.

Agreed. I will refactor this to flatten the indentation.

Quoted 7 lines
Quoted 7 lines
mm/swap_tier.c new file +7 -0
+/*
+ * struct swap_tier - structure representing a swap tier.
+ *
+ * @name: name of the swap_tier.
+ * @prio: starting value of priority.
+ * @list: linked list of tiers.
+*/
Quoted 8 lines

mm/swap_tier.c line 22

Quoted 5 lines
mm/swap_tier.c new file +5 -0
+static struct swap_tier {
+       char name[MAX_TIERNAME];
+       short prio;
+       struct list_head list;
+} swap_tiers[MAX_SWAPTIER];

We can have a CONFIG option for the MAX_SWAPTIER. I think the default should be a small number like 4.

Sounds good. I will add a CONFIG option for it and ensure it doesn't exceed MAX_SWAPFILE.

Quoted 10 lines

mm/swap_tier.c line 244

Quoted 7 lines
mm/swap_tier.c new file +7 -0
+
+/*
+ * XXX: Reverting individual operations becomes complex as the number of
+ * operations grows. Instead, we save the original state beforehand and
+ * fully restore it if any operation fails.
+ */
+void swap_tiers_save(struct swap_tier_save_ctx ctx[])

I really hope we don't have to do the save and restore thing. Is there another design we can simplify this?

I have given this a lot of thought.

Since the current interface allows mixing add (+), remove (-), and modify operations, we must either restore from a saved state or reverse the successful individual operations upon failure.

I implemented both approaches and concluded that reversing individual operations is error-prone. Also, it could be slow if there are many operations.

Another approach could be using a "global clone tier" strategy. (Because operation globally synchronized)

Therefore, I would like to propose restricting the interface to handle a single operation at a time. What do you think?

Quoted 19 lines
Quoted 9 lines
mm/swapfile.c +0 -1
@@ -76,7 +77,6 @@ atomic_long_t nr_swap_pages;
 EXPORT_SYMBOL_GPL(nr_swap_pages);
 /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
 long total_swap_pages;
-#define DEF_SWAP_PRIO  -1
 unsigned long swapfile_maximum_size;
 #ifdef CONFIG_MIGRATION
 bool swap_migration_ad_supported;

mm/swapfile.c static const char Bad_offset[] = "Bad swap offset entry "; · line 92

Quoted 5 lines
mm/swapfile.c +1 -1
@@ -89,7 +89,7 @@ static const char Bad_offset[] = "Bad swap offset entry ";
  * all active swap_info_structs
  * protected with swap_lock, and ordered by priority.
  */
-static PLIST_HEAD(swap_active_head);
+PLIST_HEAD(swap_active_head);

One idea is to make each tier have swap_active_head. So different swap entry releases on different tiers don't need to be competing on the same swap_active_head.

Quoted 2 lines

That will require the swapfile don't jump to another tiers.

I agree. With the tier structure, we can limit contention to objects within the same tier.

I also think swap_avail_list could be optimized in a similar way in the future.

Youngjun

author reviewer
#4 lore raw

Re: [RFC PATCH v2 v2 1/5] mm: swap: introduce swap tier infrastructure

In reply to Chris Li
Quoted 8 lines · Chris Li

mm/swap_tier.c line 164

Quoted 3 lines
mm/swap_tier.c new file +3 -0
+}
+
+int swap_tiers_add(const char *name, int prio)

When we add, modify, remove a tier. The simple case is there is no swap file under any tiers.
But if the modification causes some swap files to jump to different tiers. That might be problematic.

I missed one comment.

The tier of existing swapfiles is immutable once assigned at swapon. I removed tier reference.
Instead of reference counting, each operation validates the tier range at operation time to guarantee this invariant.

- add: Does not change existing swapfiles' tier. New tier may
split priority range, but existing assignments stay.
- remove: Rejected with -EBUSY if any swapfile is attached.
- modify: Rejected if the change would cause any swapfile to move to a different tier.

So swapfiles never jump between tiers at runtime.

Youngjun Park

author reviewer
#5 lore raw

RFC PATCH v2 2/5 mm: swap: associate swap devices with tiers

In reply to Youngjun Park

This patch connects swap devices to the swap tier infrastructure, ensuring that devices are correctly assigned to tiers based on their priority.

A tier_mask is added to identify the tier membership of swap devices. Although tier-based allocation logic is not yet implemented, this mapping is necessary to track which tier a device belongs to. Upon activation, the device is assigned to a tier by matching its priority against the configured tier ranges.

The infrastructure allows dynamic modification of tiers, such as splitting or merging ranges. These operations are permitted provided that the tier assignment of already configured swap devices remains unchanged.

This patch also adds the documentation for the swap tier feature, covering the core concepts, sysfs interface usage, and configuration details.

---
 Documentation/mm/swap-tier.rst | 109 +++++++++++++++++++++++++++++++++
 include/linux/swap.h           |   1 +
 mm/swap_state.c                |   2 +-
 mm/swap_tier.c                 | 106 ++++++++++++++++++++++++++++----
 mm/swap_tier.h                 |  13 +++-
 mm/swapfile.c                  |   2 +
 6 files changed, 219 insertions(+), 14 deletions(-)
 create mode 100644 Documentation/mm/swap-tier.rst
Documentation/mm/swap-tier.rst new file +109 -0
new file mode 100644
index 000000000000..3386161b9b18
@@ -0,0 +1,109 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+:Author: Chris Li <[email protected]> Youngjun Park <[email protected]>
+
+==========
+Swap Tier
+==========
+
+Swap tier is a collection of user-named groups classified by priority ranges.
+It acts as a facilitation layer, allowing users to manage swap devices based
+on their speeds.
+
+Users are encouraged to assign swap device priorities according to device
+speed to fully utilize this feature. While the current implementation is
+integrated with cgroups, the concept is designed to be extensible for other
+subsystems in the future.
+
+Use case
+-------
+
+Users can perform selective swapping by choosing a swap tier assigned according
+to speed within a cgroup.
+
+For more information on cgroup v2, please refer to
+``Documentation/admin-guide/cgroup-v2.rst``.
+
+Priority Range
+--------------
+
+The specified tiers must cover the entire priority range from -1
+(DEF_SWAP_PRIO) to SHRT_MAX.
+
+Consistency
+-----------
+
+Tier consistency is guaranteed with a focus on maximizing flexibility. When a
+swap device is activated within a tier range, a reference is held from the
+start of the tier to the priority of that swap device. This ensures that the
+tier of region containing the active swap device does not disappear.
+
+If a request to add a new tier with a priority higher than the current swap
+device is received, the existing tier can be split.
+
+However, specifying a tier in a cgroup does not hold a reference to the tier.
+Consequently, the corresponding tier can disappear at any time.
+
+Configuration Interface
+-----------------------
+
+The swap tiers can be configured via the following interface:
+
+/sys/kernel/mm/swap/tiers
+
+Operations can be performed using the following syntax:
+
+* Add:    ``+"<tiername>":"<start_priority>"``
+* Remove: ``-"<tiername>"``
+* Modify: ``"<tiername>":"<start_priority>"``
+
+Multiple operations can be provided in a single write, separated by spaces (" ")
+or commas (",").
+
+When configuring tiers, the specified value represents the **start priority**
+of that tier. The end priority is automatically determined by the start
+priority of the next higher tier. Consequently, adding or modifying a tier
+automatically adjusts (splits or merges) the ranges of adjacent tiers to
+ensure continuity.
+
+Examples
+--------
+
+**1. Initialization**
+
+A tier starting at -1 is mandatory to cover the entire priority range up to
+SHRT_MAX. In this example, 'HDD' starts at 50, and 'NET' covers the remaining
+lower range starting from -1.
+
+::
+
+    # echo "+HDD:50, +NET:-1" > /sys/kernel/mm/swap/tiers
+    # cat /sys/kernel/mm/swap/tiers
+    Name             Idx   PrioStart   PrioEnd
+    HDD              0     50          32767
+    NET              1     -1          49
+
+**2. Modification and Splitting**
+
+Here, 'HDD' is moved to start at 80, and a new tier 'SSD' is added at 100.
+Notice how the ranges are automatically recalculated:
+* 'SSD' takes the top range. Split HDD Tier's range. (100 to SHRT_MAX).
+* 'HDD' is adjusted to the range between 'NET' and 'SSD' (80 to 99).
+* 'NET' automatically extends to fill the gap below 'HDD' (-1 to 79).
+
+::
+
+    # echo "HDD:80, +SSD:100" > /sys/kernel/mm/swap/tiers
+    # cat /sys/kernel/mm/swap/tiers
+    Name             Idx   PrioStart   PrioEnd
+    SSD              2     100         32767
+    HDD              0     80          99
+    NET              1     -1          79
+
+**3. Removal**
+
+Tiers can be removed using the '-' prefix.
+
+::
+
+    # echo "-SSD,-HDD,-NET" > /sys/kernel/mm/swap/tiers
include/linux/swap.h +1 -0
index 62fc7499b408..1e68c220a0e7 100644
@@ -262,6 +262,7 @@ struct swap_info_struct {
 	struct percpu_ref users;	/* indicate and keep swap device valid. */
 	unsigned long	flags;		/* SWP_USED etc: see above */
 	signed short	prio;		/* swap priority of this type */
+	int tier_mask;			/* swap tier mask */
 	struct plist_node list;		/* entry in swap_active_head */
 	signed char	type;		/* strange name for an index */
 	unsigned int	max;		/* extent of the swap_map */
mm/swap_state.c +1 -1
index f1a7d9cdc648..d46ca61d2e42 100644
@@ -997,7 +997,7 @@ static ssize_t tiers_store(struct kobject *kobj,
 			goto restore;
 	}
 
-	if (!swap_tiers_validate()) {
+	if (!swap_tiers_update()) {
 		ret = -EINVAL;
 		goto restore;
 	}
mm/swap_tier.c +94 -12

189 lines not on this page · raw message

mm/swap_tier.h +12 -1
index 4b1b0602d691..de81d540e3b5 100644
@@ -14,6 +14,9 @@
 #define MAX_SWAPTIER		8
 #endif
 
+/* Forward declarations */
+struct swap_info_struct;
+
 extern spinlock_t swap_tier_lock;
 
 struct swap_tier_save_ctx {
@@ -24,6 +27,10 @@ struct swap_tier_save_ctx {
 #define DEFINE_SWAP_TIER_SAVE_CTX(_name) \
 	struct swap_tier_save_ctx _name[MAX_SWAPTIER] = {0}
 
+#define TIER_ALL_MASK		(~0)
+#define TIER_DEFAULT_IDX	(31)
+#define TIER_DEFAULT_MASK	(1 << TIER_DEFAULT_IDX)
+
 /* Initialization and application */
 void swap_tiers_init(void);
 ssize_t swap_tiers_sysfs_show(char *buf);
@@ -34,5 +41,9 @@ int swap_tiers_modify(const char *name, int prio);
 
 void swap_tiers_save(struct swap_tier_save_ctx ctx[]);
 void swap_tiers_restore(struct swap_tier_save_ctx ctx[]);
-bool swap_tiers_validate(void);
+bool swap_tiers_update(void);
+
+/* Tier assignment */
+void swap_tiers_assign_dev(struct swap_info_struct *swp);
+
 #endif /* _SWAP_TIER_H */
mm/swapfile.c +2 -0
index c27952b41d4f..4f8ce021c5bd 100644
@@ -2672,6 +2672,8 @@ static void _enable_swap_info(struct swap_info_struct *si)
 
 	/* Add back to available list */
 	add_to_avail_list(si, true);
+
+	swap_tiers_assign_dev(si);
 }
 
 static void enable_swap_info(struct swap_info_struct *si, int prio,
Signature · 1 line
2.34.1
author reviewer
#6 lore raw

RFC PATCH v2 3/5 mm: memcontrol: add interface for swap tier selection

In reply to Youngjun Park

This patch integrates the swap tier infrastructure with cgroup, enabling the selection of specific swap devices per cgroup by configuring allowed swap tiers.

The new memory.swap.tiers interface controls allowed swap tiers via a mask. By default, the mask is set to include all tiers, allowing specific tiers to be excluded or restored. Note that effective tiers are calculated separately using a dedicated mask to respect the cgroup hierarchy. Consequently, configured tiers may differ from effective ones, as they must be a subset of the parent's.

Note that cgroups do not pin swap tiers. This is similar to the cpuset controller, which does not prevent CPU hotplug. This approach ensures flexibility by allowing tier configuration changes regardless of cgroup usage.

---
 Documentation/admin-guide/cgroup-v2.rst | 27 +++++++++
 include/linux/memcontrol.h              |  3 +-
 mm/memcontrol.c                         | 80 +++++++++++++++++++++++++
 mm/swap_tier.c                          | 66 ++++++++++++++++++++
 mm/swap_tier.h                          | 21 +++++++
 mm/swapfile.c                           |  5 ++
 6 files changed, 201 insertions(+), 1 deletion(-)
Documentation/admin-guide/cgroup-v2.rst +27 -0
index 7f5b59d95fce..776a908ce1b9 100644
@@ -1848,6 +1848,33 @@ The following nested keys are defined.
 	Swap usage hard limit.  If a cgroup's swap usage reaches this
 	limit, anonymous memory of the cgroup will not be swapped out.
 
+  memory.swap.tiers
+        A read-write nested-keyed file which exists on non-root
+        cgroups. The default is to enable all tiers.
+
+        This interface allows selecting which swap tiers a cgroup can
+        use for swapping out memory.
+
+        The effective tiers are inherited from the parent. Only tiers
+        effective in the parent can be effective in the child. However,
+        the child can explicitly disable tiers allowed by the parent.
+
+        When read, the file shows two lines:
+          - The first line shows the operation string that was
+            written to this file.
+          - The second line shows the effective operation after
+            merging with parent settings.
+
+        When writing, the format is:
+          (+/-)(TIER_NAME) (+/-)(TIER_NAME) ...
+
+        Valid tier names are those configured in
+        /sys/kernel/mm/swap/tiers.
+
+        Each tier can be prefixed with:
+          +    Enable this tier
+          -    Disable this tier
+
   memory.swap.events
 	A read-only flat-keyed file which exists on non-root cgroups.
 	The following entries are defined.  Unless specified
include/linux/memcontrol.h +2 -1
index b6c82c8f73e1..542bee1b5f60 100644
@@ -283,7 +283,8 @@ struct mem_cgroup {
 	/* per-memcg mm_struct list */
 	struct lru_gen_mm_list mm_list;
 #endif
-
+	int tier_mask;
+	int tier_effective_mask;
 #ifdef CONFIG_MEMCG_V1
 	/* Legacy consumer-oriented counters */
 	struct page_counter kmem;		/* v1 only */
mm/memcontrol.c +80 -0

110 lines not on this page · raw message

mm/swap_tier.c +66 -0

69 lines not on this page · raw message

mm/swap_tier.h +21 -0
index de81d540e3b5..8652a7f993ab 100644
@@ -46,4 +46,25 @@ bool swap_tiers_update(void);
 /* Tier assignment */
 void swap_tiers_assign_dev(struct swap_info_struct *swp);
 
+/* Memcg related functions */
+void swap_tiers_mask_show(struct seq_file *m, int mask);
+void swap_tiers_memcg_inherit_mask(struct mem_cgroup *memcg,
+	struct mem_cgroup *parent);
+void swap_tiers_memcg_sync_mask(struct mem_cgroup *memcg);
+void __swap_tiers_memcg_sync_mask(struct mem_cgroup *memcg);
+
+/* Mask and tier lookup */
+int swap_tiers_mask_lookup(const char *name);
+
+/**
+ * swap_tiers_mask_test - Check if the tier mask is valid
+ * @tier_mask: The tier mask to check
+ * @mask: The mask to compare against
+ *
+ * Return: true if condition matches, false otherwise
+ */
+static inline bool swap_tiers_mask_test(int tier_mask, int mask)
+{
+	return tier_mask & mask;
+}
 #endif /* _SWAP_TIER_H */
mm/swapfile.c +5 -0
index 4f8ce021c5bd..dd97e850ea2c 100644
@@ -1348,10 +1348,15 @@ static bool swap_alloc_fast(struct folio *folio)
 static void swap_alloc_slow(struct folio *folio)
 {
 	struct swap_info_struct *si, *next;
+	int mask = folio_memcg(folio) ?
+		folio_memcg(folio)->tier_effective_mask : TIER_ALL_MASK;
 
 	spin_lock(&swap_avail_lock);
 start_over:
 	plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
+		if (!swap_tiers_mask_test(si->tier_mask, mask))
+			continue;
+
 		/* Rotate the device and switch to a new cluster */
 		plist_requeue(&si->avail_list, &swap_avail_head);
 		spin_unlock(&swap_avail_lock);
Signature · 1 line
2.34.1
author reviewer
#7 lore raw

RFC PATCH v2 4/5 mm, swap: change back to use each swap device's percpu cluster

In reply to Youngjun Park

This reverts commit 1b7e90020eb7 ("mm, swap: use percpu cluster as allocation fast path").

Because in the newly introduced swap tiers, the global percpu cluster will cause two issues:
1) it will cause caching oscillation in the same order of different si if two different memcg can only be allowed to access different si and both of them are swapping out.
2) It can cause priority inversion on swap devices. Imagine a case where there are two memcg, say memcg1 and memcg2. Memcg1 can access si A, B and A is higher priority device. While memcg2 can only access si B. Then memcg 2 could write the global percpu cluster with si B, then memcg1 take si B in fast path even though si A is not exhausted.

Hence in order to support swap tier, revert commit to use each swap device's percpu cluster.

---
 include/linux/swap.h |  17 ++++--
 mm/swapfile.c        | 142 ++++++++++++++-----------------------------
 2 files changed, 57 insertions(+), 102 deletions(-)
include/linux/swap.h +12 -5
index 1e68c220a0e7..6921e22b14d3 100644
@@ -247,11 +247,18 @@ enum {
 #define SWAP_NR_ORDERS		1
 #endif
 
-/*
- * We keep using same cluster for rotational device so IO will be sequential.
- * The purpose is to optimize SWAP throughput on these device.
- */
+ /*
+  * We assign a cluster to each CPU, so each CPU can allocate swap entry from
+  * its own cluster and swapout sequentially. The purpose is to optimize swapout
+  * throughput.
+  */
+struct percpu_cluster {
+	local_lock_t lock; /* Protect the percpu_cluster above */
+	unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */
+};
+
 struct swap_sequential_cluster {
+	spinlock_t lock; /* Serialize usage of global cluster */
 	unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */
 };
 
@@ -277,8 +284,8 @@ struct swap_info_struct {
 					/* list of cluster that are fragmented or contented */
 	unsigned int pages;		/* total of usable pages of swap */
 	atomic_long_t inuse_pages;	/* number of those currently in use */
+	struct percpu_cluster	__percpu *percpu_cluster; /* per cpu's swap location */
 	struct swap_sequential_cluster *global_cluster; /* Use one global cluster for rotating device */
-	spinlock_t global_cluster_lock;	/* Serialize usage of global cluster */
 	struct rb_root swap_extent_root;/* root of the swap extent rbtree */
 	struct block_device *bdev;	/* swap device or bdev of swap file */
 	struct file *swap_file;		/* seldom referenced */
mm/swapfile.c +45 -97

269 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#8 lore raw

Re: [RFC PATCH v2 v2 4/5] mm, swap: change back to use each swap device's percpu cluster

Quoted 4 lines · Youngjun Park

This reverts commit 1b7e90020eb7 ("mm, swap: use percpu cluster as allocation fast path").

Quoted 10 lines

Because in the newly introduced swap tiers, the global percpu cluster will cause two issues:
1) it will cause caching oscillation in the same order of different si if two different memcg can only be allowed to access different si and both of them are swapping out.
2) It can cause priority inversion on swap devices. Imagine a case where there are two memcg, say memcg1 and memcg2. Memcg1 can access si A, B and A is higher priority device. While memcg2 can only access si B. Then memcg 2 could write the global percpu cluster with si B, then memcg1 take si B in fast path even though si A is not exhausted.

One idea is that, instead of using percpu per swap device. You can make the global percpu cluster per tier. Because the max tier number is smaller than the max number of swap devices. That is likely a win.

Chris

Quoted 345 lines

Hence in order to support swap tier, revert commit to use each swap device's percpu cluster.

---
 include/linux/swap.h |  17 ++++--
 mm/swapfile.c        | 142 ++++++++++++++-----------------------------
 2 files changed, 57 insertions(+), 102 deletions(-)
include/linux/swap.h +12 -5
index 1e68c220a0e7..6921e22b14d3 100644
@@ -247,11 +247,18 @@ enum {
 #define SWAP_NR_ORDERS         1
 #endif
 
-/*
- * We keep using same cluster for rotational device so IO will be sequential.
- * The purpose is to optimize SWAP throughput on these device.
- */
+ /*
+  * We assign a cluster to each CPU, so each CPU can allocate swap entry from
+  * its own cluster and swapout sequentially. The purpose is to optimize swapout
+  * throughput.
+  */
+struct percpu_cluster {
+       local_lock_t lock; /* Protect the percpu_cluster above */
+       unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */
+};
+
 struct swap_sequential_cluster {
+       spinlock_t lock; /* Serialize usage of global cluster */
        unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */
 };
 
@@ -277,8 +284,8 @@ struct swap_info_struct {
                                        /* list of cluster that are fragmented or contented */
        unsigned int pages;             /* total of usable pages of swap */
        atomic_long_t inuse_pages;      /* number of those currently in use */
+       struct percpu_cluster   __percpu *percpu_cluster; /* per cpu's swap location */
        struct swap_sequential_cluster *global_cluster; /* Use one global cluster for rotating device */
-       spinlock_t global_cluster_lock; /* Serialize usage of global cluster */
        struct rb_root swap_extent_root;/* root of the swap extent rbtree */
        struct block_device *bdev;      /* swap device or bdev of swap file */
        struct file *swap_file;         /* seldom referenced */
mm/swapfile.c +45 -98
index dd97e850ea2c..5e3b87799440 100644
@@ -118,18 +118,6 @@ static atomic_t proc_poll_event = ATOMIC_INIT(0);
 
 atomic_t nr_rotate_swap = ATOMIC_INIT(0);
 
-struct percpu_swap_cluster {
-       struct swap_info_struct *si[SWAP_NR_ORDERS];
-       unsigned long offset[SWAP_NR_ORDERS];
-       local_lock_t lock;
-};
-
-static DEFINE_PER_CPU(struct percpu_swap_cluster, percpu_swap_cluster) = {
-       .si = { NULL },
-       .offset = { SWAP_ENTRY_INVALID },
-       .lock = INIT_LOCAL_LOCK(),
-};
-
 /* May return NULL on invalid type, caller must check for NULL return */
 static struct swap_info_struct *swap_type_to_info(int type)
 {
@@ -477,7 +465,7 @@ swap_cluster_alloc_table(struct swap_info_struct *si,
         * Swap allocator uses percpu clusters and holds the local lock.
         */
        lockdep_assert_held(&ci->lock);
-       lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock);
+       lockdep_assert_held(this_cpu_ptr(&si->percpu_cluster->lock));
 
        /* The cluster must be free and was just isolated from the free list. */
        VM_WARN_ON_ONCE(ci->flags || !cluster_is_empty(ci));
@@ -495,8 +483,8 @@ swap_cluster_alloc_table(struct swap_info_struct *si,
         */
        spin_unlock(&ci->lock);
        if (!(si->flags & SWP_SOLIDSTATE))
-               spin_unlock(&si->global_cluster_lock);
-       local_unlock(&percpu_swap_cluster.lock);
+               spin_unlock(&si->global_cluster->lock);
+       local_unlock(&si->percpu_cluster->lock);
 
        table = swap_table_alloc(__GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL);
 
@@ -508,9 +496,9 @@ swap_cluster_alloc_table(struct swap_info_struct *si,
         * could happen with ignoring the percpu cluster is fragmentation,
         * which is acceptable since this fallback and race is rare.
         */
-       local_lock(&percpu_swap_cluster.lock);
+       local_lock(&si->percpu_cluster->lock);
        if (!(si->flags & SWP_SOLIDSTATE))
-               spin_lock(&si->global_cluster_lock);
+               spin_lock(&si->global_cluster->lock);
        spin_lock(&ci->lock);
 
        /* Nothing except this helper should touch a dangling empty cluster. */
@@ -622,7 +610,7 @@ static bool swap_do_scheduled_discard(struct swap_info_struct *si)
                ci = list_first_entry(&si->discard_clusters, struct swap_cluster_info, list);
                /*
                 * Delete the cluster from list to prepare for discard, but keep
-                * the CLUSTER_FLAG_DISCARD flag, percpu_swap_cluster could be
+                * the CLUSTER_FLAG_DISCARD flag, there could be percpu_cluster
                 * pointing to it, or ran into by relocate_cluster.
                 */
                list_del(&ci->list);
@@ -953,12 +941,11 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
 out:
        relocate_cluster(si, ci);
        swap_cluster_unlock(ci);
-       if (si->flags & SWP_SOLIDSTATE) {
-               this_cpu_write(percpu_swap_cluster.offset[order], next);
-               this_cpu_write(percpu_swap_cluster.si[order], si);
-       } else {
+       if (si->flags & SWP_SOLIDSTATE)
+               this_cpu_write(si->percpu_cluster->next[order], next);
+       else
                si->global_cluster->next[order] = next;
-       }
+
        return found;
 }
 
@@ -1052,13 +1039,17 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
        if (order && !(si->flags & SWP_BLKDEV))
                return 0;
 
-       if (!(si->flags & SWP_SOLIDSTATE)) {
+       if (si->flags & SWP_SOLIDSTATE) {
+               /* Fast path using per CPU cluster */
+               local_lock(&si->percpu_cluster->lock);
+               offset = __this_cpu_read(si->percpu_cluster->next[order]);
+       } else {
                /* Serialize HDD SWAP allocation for each device. */
-               spin_lock(&si->global_cluster_lock);
+               spin_lock(&si->global_cluster->lock);
                offset = si->global_cluster->next[order];
-               if (offset == SWAP_ENTRY_INVALID)
-                       goto new_cluster;
+       }
 
+       if (offset != SWAP_ENTRY_INVALID) {
                ci = swap_cluster_lock(si, offset);
                /* Cluster could have been used by another order */
                if (cluster_is_usable(ci, order)) {
@@ -1072,7 +1063,6 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
                        goto done;
        }
 
-new_cluster:
        /*
         * If the device need discard, prefer new cluster over nonfull
         * to spread out the writes.
@@ -1129,8 +1119,10 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
                        goto done;
        }
 done:
-       if (!(si->flags & SWP_SOLIDSTATE))
-               spin_unlock(&si->global_cluster_lock);
+       if (si->flags & SWP_SOLIDSTATE)
+               local_unlock(&si->percpu_cluster->lock);
+       else
+               spin_unlock(&si->global_cluster->lock);
 
        return found;
 }
@@ -1311,41 +1303,8 @@ static bool get_swap_device_info(struct swap_info_struct *si)
        return true;
 }
 
-/*
- * 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)
-{
-       unsigned int order = folio_order(folio);
-       struct swap_cluster_info *ci;
-       struct swap_info_struct *si;
-       unsigned int offset;
-
-       /*
-        * Once allocated, swap_info_struct will never be completely freed,
-        * so checking it's liveness by get_swap_device_info is enough.
-        */
-       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;
-
-       ci = swap_cluster_lock(si, offset);
-       if (cluster_is_usable(ci, order)) {
-               if (cluster_is_empty(ci))
-                       offset = cluster_offset(si, ci);
-               alloc_swap_scan_cluster(si, ci, folio, offset);
-       } else {
-               swap_cluster_unlock(ci);
-       }
-
-       put_swap_device(si);
-       return folio_test_swapcache(folio);
-}
-
 /* Rotate the device and switch to a new cluster */
-static void swap_alloc_slow(struct folio *folio)
+static void swap_alloc_entry(struct folio *folio)
 {
        struct swap_info_struct *si, *next;
        int mask = folio_memcg(folio) ?
@@ -1363,6 +1322,7 @@ static void swap_alloc_slow(struct folio *folio)
                if (get_swap_device_info(si)) {
                        cluster_alloc_swap_entry(si, folio);
                        put_swap_device(si);
+
                        if (folio_test_swapcache(folio))
                                return;
                        if (folio_test_large(folio))
@@ -1522,11 +1482,7 @@ int folio_alloc_swap(struct folio *folio)
        }
 
 again:
-       local_lock(&percpu_swap_cluster.lock);
-       if (!swap_alloc_fast(folio))
-               swap_alloc_slow(folio);
-       local_unlock(&percpu_swap_cluster.lock);
-
+       swap_alloc_entry(folio);
        if (!order && unlikely(!folio_test_swapcache(folio))) {
                if (swap_sync_discard())
                        goto again;
@@ -1945,9 +1901,7 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
                         * Grab the local lock to be compliant
                         * with swap table allocation.
                         */
-                       local_lock(&percpu_swap_cluster.lock);
                        offset = cluster_alloc_swap_entry(si, NULL);
-                       local_unlock(&percpu_swap_cluster.lock);
                        if (offset)
                                entry = swp_entry(si->type, offset);
                }
@@ -2751,28 +2705,6 @@ static void free_cluster_info(struct swap_cluster_info *cluster_info,
        kvfree(cluster_info);
 }
 
-/*
- * Called after swap device's reference count is dead, so
- * neither scan nor allocation will use it.
- */
-static void flush_percpu_swap_cluster(struct swap_info_struct *si)
-{
-       int cpu, i;
-       struct swap_info_struct **pcp_si;
-
-       for_each_possible_cpu(cpu) {
-               pcp_si = per_cpu_ptr(percpu_swap_cluster.si, cpu);
-               /*
-                * Invalidate the percpu swap cluster cache, si->users
-                * is dead, so no new user will point to it, just flush
-                * any existing user.
-                */
-               for (i = 0; i < SWAP_NR_ORDERS; i++)
-                       cmpxchg(&pcp_si[i], si, NULL);
-       }
-}
-
-
 SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
 {
        struct swap_info_struct *p = NULL;
@@ -2856,7 +2788,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
 
        flush_work(&p->discard_work);
        flush_work(&p->reclaim_work);
-       flush_percpu_swap_cluster(p);
 
        destroy_swap_extents(p);
        if (p->flags & SWP_CONTINUED)
@@ -2885,6 +2816,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        arch_swap_invalidate_area(p->type);
        zswap_swapoff(p->type);
        mutex_unlock(&swapon_mutex);
+       free_percpu(p->percpu_cluster);
+       p->percpu_cluster = NULL;
        kfree(p->global_cluster);
        p->global_cluster = NULL;
        vfree(swap_map);
@@ -3268,7 +3201,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
 {
        unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
        struct swap_cluster_info *cluster_info;
-       int err = -ENOMEM;
+       int cpu, err = -ENOMEM;
        unsigned long i;
 
        cluster_info = kvcalloc(nr_clusters, sizeof(*cluster_info), GFP_KERNEL);
@@ -3278,14 +3211,27 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
        for (i = 0; i < nr_clusters; i++)
                spin_lock_init(&cluster_info[i].lock);
 
-       if (!(si->flags & SWP_SOLIDSTATE)) {
+       if (si->flags & SWP_SOLIDSTATE) {
+               si->percpu_cluster = alloc_percpu(struct percpu_cluster);
+               if (!si->percpu_cluster)
+                       goto err;
+
+               for_each_possible_cpu(cpu) {
+                       struct percpu_cluster *cluster;
+
+                       cluster = per_cpu_ptr(si->percpu_cluster, cpu);
+                       for (i = 0; i < SWAP_NR_ORDERS; i++)
+                               cluster->next[i] = SWAP_ENTRY_INVALID;
+                       local_lock_init(&cluster->lock);
+               }
+       } else {
                si->global_cluster = kmalloc(sizeof(*si->global_cluster),
                                     GFP_KERNEL);
                if (!si->global_cluster)
                        goto err;
                for (i = 0; i < SWAP_NR_ORDERS; i++)
                        si->global_cluster->next[i] = SWAP_ENTRY_INVALID;
-               spin_lock_init(&si->global_cluster_lock);
+               spin_lock_init(&si->global_cluster->lock);
        }
 
        /*
@@ -3566,6 +3512,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 bad_swap_unlock_inode:
        inode_unlock(inode);
 bad_swap:
+       free_percpu(si->percpu_cluster);
+       si->percpu_cluster = NULL;
        kfree(si->global_cluster);
        si->global_cluster = NULL;
        inode = NULL;
--

2.34.1

author reviewer
#9 lore raw

RFC PATCH v2 5/5 mm, swap: introduce percpu swap device cache to avoid fragmentation

In reply to Youngjun Park

When using per-device percpu clusters (instead of a global one), a naive allocation logic triggers swap device rotation on every allocation. This behavior leads to severe fragmentation and performance regression.

To address this, this patch introduces a per-cpu cache for the swap device. The allocation logic is updated to prioritize the per-cpu cluster within the cached swap device, effectively restoring the traditional fastpath and slowpath flow. This approach minimizes side effects on the existing fastpath.

With this change, swap device rotation occurs only when the current cached device is unable to satisfy the allocation, rather than on every attempt.

---
 include/linux/swap.h |  1 -
 mm/swapfile.c        | 78 +++++++++++++++++++++++++++++++++++++-------
 2 files changed, 66 insertions(+), 13 deletions(-)
include/linux/swap.h +0 -1
index 6921e22b14d3..ac634a21683a 100644
@@ -253,7 +253,6 @@ enum {
   * throughput.
   */
 struct percpu_cluster {
-	local_lock_t lock; /* Protect the percpu_cluster above */
 	unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */
 };
 
mm/swapfile.c +66 -12

159 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#10 lore raw
In reply to Youngjun Park

Hi Youngjun,

Quoted 7 lines · Youngjun Park

This is the second version of the RFC for the "Swap Tiers" concept.
Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

This version incorporates feedback received during LPC 2025 and addresses comments from the previous review. We have also included experimental results based on usage scenarios intended for our internal platforms.

Thanks for the patches series.

Sorry for the late reply. I have been wanting to reply to it but get super busy at work.

Some high level feedback for the series. Now that you demonstrated the whole series, let's focus on making small mergiable baby steps. Just like the swap table has different phases. Make each step minimal, each step shows some value. Do the MVP, we can always add more features as a follow up step.

I suggest the first step is getting the tiers bits defined. Add only, no delete. Get that reviewed and merged, then the next step is to use those tiers.

Chris

maintainer
#11 lore raw
Quoted 23 lines · Chris Li

Hi Youngjun,

Quoted 7 lines · Youngjun Park

This is the second version of the RFC for the "Swap Tiers" concept.
Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

This version incorporates feedback received during LPC 2025 and addresses comments from the previous review. We have also included experimental results based on usage scenarios intended for our internal platforms.

Thanks for the patches series.

Sorry for the late reply. I have been wanting to reply to it but get super busy at work.

Some high level feedback for the series. Now that you demonstrated the whole series, let's focus on making small mergiable baby steps. Just like the swap table has different phases. Make each step minimal, each step shows some value. Do the MVP, we can always add more features as a follow up step.

Quoted 3 lines

I suggest the first step is getting the tiers bits defined. Add only, no delete. Get that reviewed and merged, then the next step is to use those tiers.

Just take a quick look at the series. I take that suggestion back. This series is actually not too long. Adding the tiers name alone does not add any real value. I actually need to look at the whole series rather than just the tier name alone.

Chris

author reviewer
#12 lore raw
Quoted 28 lines · Chris Li
Quoted 23 lines · Chris Li

Hi Youngjun,

Quoted 7 lines · Youngjun Park

This is the second version of the RFC for the "Swap Tiers" concept.
Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

This version incorporates feedback received during LPC 2025 and addresses comments from the previous review. We have also included experimental results based on usage scenarios intended for our internal platforms.

Thanks for the patches series.

Sorry for the late reply. I have been wanting to reply to it but get super busy at work.

Some high level feedback for the series. Now that you demonstrated the whole series, let's focus on making small mergiable baby steps. Just like the swap table has different phases. Make each step minimal, each step shows some value. Do the MVP, we can always add more features as a follow up step.

Quoted 3 lines

I suggest the first step is getting the tiers bits defined. Add only, no delete. Get that reviewed and merged, then the next step is to use those tiers.

Quoted 6 lines

Just take a quick look at the series. I take that suggestion back. This series is actually not too long. Adding the tiers name alone does not add any real value. I actually need to look at the whole series rather than just the tier name alone.

Chris

Oops, I replied to your previous email before seeing this one.

Stripping out the remove/modify parts is also feasible. Do you agree with that direction?

Youngjun

author reviewer
#13 lore raw
In reply to Chris Li
Quoted 22 lines · Chris Li

Hi Youngjun,

Quoted 7 lines · Youngjun Park

This is the second version of the RFC for the "Swap Tiers" concept.
Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

This version incorporates feedback received during LPC 2025 and addresses comments from the previous review. We have also included experimental results based on usage scenarios intended for our internal platforms.

Thanks for the patches series.

Sorry for the late reply. I have been wanting to reply to it but get super busy at work.

Some high level feedback for the series. Now that you demonstrated the whole series, let's focus on making small mergiable baby steps. Just like the swap table has different phases. Make each step minimal, each step shows some value. Do the MVP, we can always add more features as a follow up step.

Quoted 5 lines

I suggest the first step is getting the tiers bits defined. Add only, no delete. Get that reviewed and merged, then the next step is to use those tiers.

Chris

Hi Chris,

Thank you for the direction.

I agree that breaking the series into smaller, mergeable steps is the right approach. However, since introducing the definitions alone might lack immediate usage, I propose a slightly
modified roadmap to ensure Step 1 demonstrates some value.

Here is the plan I have in mind.

1. Swap Tier Definition & Addition
- Introduce the concept, grouping logic, and the 'add' interface.
- Value: Enables basic exception handling within the swap device itself using tiers.

2. Advanced Control (Delete/Modify)
- Implement logic to remove or update tiers.
- Value: Enhances the usability and management of the tiers established in Step 1.

3. External Integration (memcg, bpf etc ... )
- Apply swap tiers for broader swap control.
- Value: Connects swap tiers to other subsystems like memcg.

Does this roadmap look reasonable to you? I will proceed with preparing the real patch series based on this structure.

Best regards,
Youngjun

reviewer
#14 lore raw
In reply to Youngjun Park
Quoted 31 lines · Youngjun Park

This is the second version of the RFC for the "Swap Tiers" concept.
Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

This version incorporates feedback received during LPC 2025 and addresses comments from the previous review. We have also included experimental results based on usage scenarios intended for our internal platforms.

Motivation & Concept recap
==========================
Current Linux swap allocation is global, limiting the ability to assign faster devices to specific cgroups. Our initial attempt at per-cgroup priorities proved over-engineered and caused LRU inversion.

Following Chris Li's suggestion, we pivoted to "Swap Tiers." A tier is simply a user-named group of swap devices sharing the same priority range. This abstraction facilitates swap device selection based on speed, allowing users to configure specific tiers for cgroups.

For more details, please refer to the LPC 2025 presentation
https://lpc.events/event/19/contributions/2141/attachments/1857/3998/LPC2025Finalss.pdf
or v1 patch.

Changes in v2
=============
1. Respect cgroup hierarchy principle (LPC 2025 feedback)
- The logic now strictly follows standard cgroup hierarchy principles.

Previous: Children could select any tier using "+" regardless of the parent's configuration. "+" tier is referenced. (could not be silently disappeared)

Quoted 6 lines

Current: The explicit selection ("+") concept is removed. By default, all tiers are selected. Users now use "-" to exclude specific tiers. Excluded tier could disappeared silently.
A child cgroup is always a subset of its parent. Even if a child re-enables a tier with "+" that was excluded by the parent, the effective tier list is limited to the parent's allowed subset.

This comment seems a bit clunky to me. The "+" is removed, as noted above, but then why are we saying "even if a child re-enables a tier with "+"" here? Am I missing something?

Quoted 6 lines

Example:
Global Tiers: SSD, HDD, NET
Parent: SSD, NET (HDD excluded)
Child: HDD, NET (SSD excluded)
-> Effective Child Tier: NET (Intersection of Parent and Child)

But otherwise, I assume you mean to restrict child's allowed swap tiers to be a subset of children and its ancestors? That seems more straightforward to me than the last system :)

reviewer
#15 lore raw
Quoted 4 lines · Nhat Pham

But otherwise, I assume you mean to restrict child's allowed swap tiers to be a subset of children and its ancestors? That seems more

s/children/parent

Quoted 1 line

straightforward to me than the last system :)

author reviewer
#16 lore raw
In reply to Nhat Pham
Quoted 39 lines · Nhat Pham
Quoted 31 lines · Youngjun Park

This is the second version of the RFC for the "Swap Tiers" concept.
Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

This version incorporates feedback received during LPC 2025 and addresses comments from the previous review. We have also included experimental results based on usage scenarios intended for our internal platforms.

Motivation & Concept recap
==========================
Current Linux swap allocation is global, limiting the ability to assign faster devices to specific cgroups. Our initial attempt at per-cgroup priorities proved over-engineered and caused LRU inversion.

Following Chris Li's suggestion, we pivoted to "Swap Tiers." A tier is simply a user-named group of swap devices sharing the same priority range. This abstraction facilitates swap device selection based on speed, allowing users to configure specific tiers for cgroups.

For more details, please refer to the LPC 2025 presentation
https://lpc.events/event/19/contributions/2141/attachments/1857/3998/LPC2025Finalss.pdf
or v1 patch.

Changes in v2
=============
1. Respect cgroup hierarchy principle (LPC 2025 feedback)
- The logic now strictly follows standard cgroup hierarchy principles.

Previous: Children could select any tier using "+" regardless of the parent's configuration. "+" tier is referenced. (could not be silently disappeared)

Quoted 6 lines

Current: The explicit selection ("+") concept is removed. By default, all tiers are selected. Users now use "-" to exclude specific tiers. Excluded tier could disappeared silently.
A child cgroup is always a subset of its parent. Even if a child re-enables a tier with "+" that was excluded by the parent, the effective tier list is limited to the parent's allowed subset.

Quoted 3 lines

This comment seems a bit clunky to me. The "+" is removed, as noted above, but then why are we saying "even if a child re-enables a tier with "+"" here? Am I missing something?

To clarify, previously, the default state used all tiers. Using "+" switched to "an exclusive mode" where only that specific tier was used.

I am changing this to a subtraction-based model. By default, all tiers are selected, and users use "-" to exclude specific ones. (Then not "removed" but "changed" is more proper?)

In this context, I intended "+" to be used to restore a tier that was previously excluded by "-".

Quoted 9 lines
Quoted 6 lines

Example:
Global Tiers: SSD, HDD, NET
Parent: SSD, NET (HDD excluded)
Child: HDD, NET (SSD excluded)
-> Effective Child Tier: NET (Intersection of Parent and Child)

But otherwise, I assume you mean to restrict child's allowed swap tiers to be a subset of children and its ancestors?

Exactly.

Quoted 2 lines

That seems more
straightforward to me than the last system :)

Yes, that's right :)

Thanks
Youngjun Park.

maintainer
#17 lore raw
In reply to Youngjun Park

Hi Youngjun,

Quoted 13 lines · Youngjun Park

This is the second version of the RFC for the "Swap Tiers" concept.
Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

This version incorporates feedback received during LPC 2025 and addresses comments from the previous review. We have also included experimental results based on usage scenarios intended for our internal platforms.

Motivation & Concept recap
==========================
Current Linux swap allocation is global, limiting the ability to assign faster devices to specific cgroups. Our initial attempt at per-cgroup priorities proved over-engineered and caused LRU inversion.

Quoted 9 lines

Following Chris Li's suggestion, we pivoted to "Swap Tiers." A tier is simply a user-named group of swap devices sharing the same priority range. This abstraction facilitates swap device selection based on speed, allowing users to configure specific tiers for cgroups.

For more details, please refer to the LPC 2025 presentation
https://lpc.events/event/19/contributions/2141/attachments/1857/3998/LPC2025Finalss.pdf
or v1 patch.

One of the LPC feedback you missed is to not add memcg interface for this functionality and explore BPF way instead.

We are normally very conservative to add new interfaces to cgroup. However I am not even convinced that memcg interface is the right way to expose this functionality. Swap is currently global and the idea to limit or assign specific swap devices to specific cgroups makes sense but that is the decision for the job orchestator or node controller. Allowing workloads to pick and choose swap devices do not make sense to me.

Shakeel

author reviewer
#18 lore raw
Quoted 29 lines · Shakeel Butt

Hi Youngjun,

Quoted 13 lines · Youngjun Park

This is the second version of the RFC for the "Swap Tiers" concept.
Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

This version incorporates feedback received during LPC 2025 and addresses comments from the previous review. We have also included experimental results based on usage scenarios intended for our internal platforms.

Motivation & Concept recap
==========================
Current Linux swap allocation is global, limiting the ability to assign faster devices to specific cgroups. Our initial attempt at per-cgroup priorities proved over-engineered and caused LRU inversion.

Quoted 9 lines

Following Chris Li's suggestion, we pivoted to "Swap Tiers." A tier is simply a user-named group of swap devices sharing the same priority range. This abstraction facilitates swap device selection based on speed, allowing users to configure specific tiers for cgroups.

For more details, please refer to the LPC 2025 presentation
https://lpc.events/event/19/contributions/2141/attachments/1857/3998/LPC2025Finalss.pdf
or v1 patch.

One of the LPC feedback you missed is to not add memcg interface for this functionality and explore BPF way instead.

Quoted 7 lines

We are normally very conservative to add new interfaces to cgroup. However I am not even convinced that memcg interface is the right way to expose this functionality. Swap is currently global and the idea to limit or assign specific swap devices to specific cgroups makes sense but that is the decision for the job orchestator or node controller. Allowing workloads to pick and choose swap devices do not make sense to me.

Apologies for overlooking the feedback regarding the BPF approach. Thank you for the suggestion.

I agree that using BPF would provide greater flexibility, allowing control not just at the memcg level, but also per-process or for complex workloads. (As like orchestrator and node controller)

However, I am concerned that this level of freedom might introduce logical contradictions, particularly regarding cgroup hierarchy semantics.

For example, BPF might allow a topology that violates hierarchical constraints (a concern that was also touched upon during LPC)

- Group A (Parent): Assigned to SSD1
- Group B (Child of A): Assigned to SSD2

If Group A has a memory.swap.max limit, and Group B swaps out to SSD2, it creates a consistency issue. Group B consumes Group A's swap quota, but it is utilizing a device (SSD2) that is distinct from the Parent's assignment. This could lead to situations where the Parent's limit is exhausted by usage on a device it effectively doesn't "own" or shouldn't be using.

One might suggest restricting BPF to strictly adhere to these hierarchical constraints. However, doing so would effectively eliminate the primary advantage of using BPF—its flexibility. If we are to enforce standard cgroup semantics anyway, a native interface seems more appropriate than a constrained BPF hook.

Beyond this specific example, I suspect that delegating this logic to BPF might introduce other unforeseen edge cases regarding hierarchy enforcement. In my view, the BPF approach seems more like a "next step."

Since you acknowledged that the idea of assigning swap devices to cgroups "makes sense," I believe implementing this within the standard, strictly constrained "cgroup land" is preferable.

A strict cgroup interface ensures
that hierarchy and accounting rules are consistently enforced, avoiding the potential conflicts that the unrestricted freedom of BPF might create.

Ultimately, I hope this swap tier mechanism can serve as a foundation to be leveraged by other subsystems, such as BPF and DAMON. I view this proposal as the necessary first step toward that future.

Youngjun Park

maintainer
#19 lore raw

Please don't send a new version of the series before concluding the discussion on the previous one.

Quoted 12 lines · YoungJun Park
Quoted 11 lines

One of the LPC feedback you missed is to not add memcg interface for this functionality and explore BPF way instead.

We are normally very conservative to add new interfaces to cgroup. However I am not even convinced that memcg interface is the right way to expose this functionality. Swap is currently global and the idea to limit or assign specific swap devices to specific cgroups makes sense but that is the decision for the job orchestator or node controller. Allowing workloads to pick and choose swap devices do not make sense to me.

Quoted 2 lines

Apologies for overlooking the feedback regarding the BPF approach. Thank you for the suggestion.

No need for apologies. These things take time and multiple iterations.

Quoted 4 lines

I agree that using BPF would provide greater flexibility, allowing control not just at the memcg level, but also per-process or for complex workloads. (As like orchestrator and node controller)

Yes it provides the flexibility but that is not the main reason I am pushing for it. The reason I want you to first try the BPF approach without introducing any stable interfaces. Show how swap tiers will be used and configured in production environment and then we can talk if a stable interface is needed. I am still not convinced that swap tiers need to be controlled hierarchically and the non-root should be able to control it.

Quoted 6 lines

However, I am concerned that this level of freedom might introduce logical contradictions, particularly regarding cgroup hierarchy semantics.

For example, BPF might allow a topology that violates hierarchical constraints (a concern that was also touched upon during LPC)

Yes BPF provides more power but it is controlled by admin and admin can shoot their foot in multiple ways.

Quoted 4 lines

- Group A (Parent): Assigned to SSD1
- Group B (Child of A): Assigned to SSD2

Quoted 8 lines

If Group A has a memory.swap.max limit, and Group B swaps out to SSD2, it creates a consistency issue. Group B consumes Group A's swap quota, but it is utilizing a device (SSD2) that is distinct from the Parent's assignment. This could lead to situations where the Parent's limit is exhausted by usage on a device it effectively doesn't "own" or shouldn't be using.

One might suggest restricting BPF to strictly adhere to these hierarchical constraints.

No need to constraint anything.

Taking a step back, can you describe your use-case a bit more and share requirements?

You have multiple swap devices of different properties and you want to assign those swap devices to different workloads. Now couple of questions:

1. If more than one device is assign to a workload, do you want to have some kind of ordering between them for the worklod or do you want option to have round robin kind of policy?

2. What's the reason to use 'tiers' in the name? Is it similar to memory tiers and you want promotion/demotion among the tiers?

3. If a workload has multiple swap devices assigned, can you describe the scenario where such workloads need to partition/divide given devices to their sub-workloads?

Let's start with these questions. Please note that I want us to not just look at the current use-case but brainstorm more future use-cases and then come up with the solution which is more future proof.

thanks,
Shakeel

maintainer
#20 lore raw
Quoted 3 lines · Shakeel Butt

Please don't send a new version of the series before concluding the discussion on the previous one.

In this case I think it is fine. You haven't responded to YoungJun's last response in over a week. He might have mistaken that the discussion concluded.
Consider it is one of the iterations. It is hard enough to contribute to the kernel. Relax.
Plus, much of the discussion on the mailing list always has differing opinions. So, it's hard to determine what is truly concluded. Different people might have different interitations of the same text.

Quoted 20 lines · YoungJun Park
Quoted 12 lines
Quoted 11 lines

One of the LPC feedback you missed is to not add memcg interface for this functionality and explore BPF way instead.

We are normally very conservative to add new interfaces to cgroup. However I am not even convinced that memcg interface is the right way to expose this functionality. Swap is currently global and the idea to limit or assign specific swap devices to specific cgroups makes sense but that is the decision for the job orchestator or node controller. Allowing workloads to pick and choose swap devices do not make sense to me.

Quoted 2 lines

Apologies for overlooking the feedback regarding the BPF approach. Thank you for the suggestion.

No need for apologies. These things take time and multiple iterations.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

I agree that using BPF would provide greater flexibility, allowing control not just at the memcg level, but also per-process or for complex workloads. (As like orchestrator and node controller)

Yes it provides the flexibility but that is not the main reason I am pushing for it. The reason I want you to first try the BPF approach without introducing any stable interfaces. Show how swap tiers will be used and configured in production

Is that your biggest concern? Many different ways exist to solve that problem. e.g. We can put a config option protecting it and mark it as experimental. This will unblock the development allow experiment. We can have more people to try it out and give feedback.

Quoted 3 lines

environment and then we can talk if a stable interface is needed. I am still not convinced that swap tiers need to be controlled hierarchically and the non-root should be able to control it.

Yes, my company uses a different swap device at different cgroup level. I did ask my coworker to confirm that usage. Control at the non root level is a real need.

Quoted 10 lines
Quoted 7 lines

However, I am concerned that this level of freedom might introduce logical contradictions, particularly regarding cgroup hierarchy semantics.

For example, BPF might allow a topology that violates hierarchical constraints (a concern that was also touched upon during LPC)

Yes BPF provides more power but it is controlled by admin and admin can shoot their foot in multiple ways.

I think this swap device control is a very basic need. All your objections to swapping control in the group can equally apply to zswap.writeback. Unlike zswap.writeback, which only control from the zswap behavior. This is a more generic version control swap device other than zswap as well. BTW, I raised that concern about zswap.writeback was not generic enough as swap control was limited when zswap was proposed. We did hold back zswap.writeback. The consensers is interface can be improved as later iterations. So here we are.

Quoted 11 lines
Quoted 11 lines

- Group A (Parent): Assigned to SSD1
- Group B (Child of A): Assigned to SSD2

If Group A has a memory.swap.max limit, and Group B swaps out to SSD2, it creates a consistency issue. Group B consumes Group A's swap quota, but it is utilizing a device (SSD2) that is distinct from the Parent's assignment. This could lead to situations where the Parent's limit is exhausted by usage on a device it effectively doesn't "own" or shouldn't be using.

Quoted 7 lines
Quoted 2 lines

One might suggest restricting BPF to strictly adhere to these hierarchical constraints.

No need to constraint anything.

Taking a step back, can you describe your use-case a bit more and share requirements?

There is a very long thread on the linux-mm maillist. I'm too lazy to dig it up.

I can share our usage requirement to refresh your memory. We internally use a cgroup swapfile control interface that has not been upstreamed. With this we can remove the need of that internal interface and go upstream instead.

Quoted 7 lines

You have multiple swap devices of different properties and you want to assign those swap devices to different workloads. Now couple of questions:

1. If more than one device is assign to a workload, do you want to have some kind of ordering between them for the worklod or do you want option to have round robin kind of policy?

It depends on the number of devices in the tiers. Different tiers maintain an order. Within the same tier round robin.

Quoted 3 lines

2. What's the reason to use 'tiers' in the name? Is it similar to memory tiers and you want promotion/demotion among the tiers?

I propose the tier name. Guilty. Yes, in was inpired by memory tiers. It just different class of swap speeds. I am not fixed on the name. We can also call it swap.device_speed_classes. You can suggest alternatives.

Promotion / demotion is possible in the future. The current state, without promotion or demotion, already provides value. Our current deployment uses only one class of swap device at a time. However I do know other companies use more than one class of swap device.

Quoted 4 lines

3. If a workload has multiple swap devices assigned, can you describe the scenario where such workloads need to partition/divide given devices to their sub-workloads?

In our deployment, we always use more than one swap device to reduce swap device lock contention.
The job config can describe the swap speed it can tolerate. Some jobs can tolerate slower speeds, while others cannot.

Quoted 3 lines

Let's start with these questions. Please note that I want us to not just look at the current use-case but brainstorm more future use-cases and then come up with the solution which is more future proof.

Take zswap.writeback as example. We have a solution that worked for the requirement at that time. Incremental improvement is fine as well. Usually, incremental progress is better. At least currently there is a real need to allow different cgroups to select different swap speeds. There is a risk in being too future-proof: we might design things that people in the future don't use as we envisioned. I see that happen too often as well.

So starting from the current need is a solid starting point. It's just a different design philosophy. Each to their own.

That is the only usage case I know. YoungJun feel free to add yours usage as well.

Chris

maintainer
#21 lore raw
Quoted 1 line · Chris Li
Quoted 1 line

[...]

Quoted 10 lines
Quoted 8 lines
Quoted 4 lines

I agree that using BPF would provide greater flexibility, allowing control not just at the memcg level, but also per-process or for complex workloads. (As like orchestrator and node controller)

Yes it provides the flexibility but that is not the main reason I am pushing for it. The reason I want you to first try the BPF approach without introducing any stable interfaces. Show how swap tiers will be used and configured in production

Is that your biggest concern?

No, that is secondary because I am not seeing the real use-case of controlling/partitioning swap devices among sub-workloads. Until that is figured out, adding a stable API is not good.

Quoted 5 lines

Many different ways exist to solve that
problem. e.g. We can put a config option protecting it and mark it as experimental. This will unblock the development allow experiment. We can have more people to try it out and give feedback.

Quoted 7 lines
Quoted 3 lines

environment and then we can talk if a stable interface is needed. I am still not convinced that swap tiers need to be controlled hierarchically and the non-root should be able to control it.

Yes, my company uses a different swap device at different cgroup level. I did ask my coworker to confirm that usage. Control at the non root level is a real need.

I am assuming you meant Google and particularly Prodkernel team and not Android or ChromeOS. Google's prodkernel used to have per-cgroup swapfiles exposed through memory.swapfiles (if I remember correctly Suleiman implemented this along with ghost swapfiles). Later this was deprecated (by Yu Zhao) and global (ghost) swapfiles were being used. The memory.swapfiles interface instead of supporting real swapfiles started having select options among default, ghost/zswap and real (something like that). However such interface was used to just disable or enable zswap for a workload and never about hierarchically controlling the swap devices (Google prodkernel only have zswap). Has something changed?

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

However, I am concerned that this level of freedom might introduce logical contradictions, particularly regarding cgroup hierarchy semantics.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

For example, BPF might allow a topology that violates hierarchical constraints (a concern that was also touched upon during LPC)

Yes BPF provides more power but it is controlled by admin and admin can shoot their foot in multiple ways.

I think this swap device control is a very basic need.

Please explain that very basic need.

Quoted 9 lines

All your
objections to swapping control in the group can equally apply to zswap.writeback. Unlike zswap.writeback, which only control from the zswap behavior. This is a more generic version control swap device other than zswap as well. BTW, I raised that concern about zswap.writeback was not generic enough as swap control was limited when zswap was proposed. We did hold back zswap.writeback. The consensers is interface can be improved as later iterations. So here we are.

This just motivates me to pushback even harder on adding a new interface without a clear use-case.

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines

- Group A (Parent): Assigned to SSD1
- Group B (Child of A): Assigned to SSD2

If Group A has a memory.swap.max limit, and Group B swaps out to SSD2, it creates a consistency issue. Group B consumes Group A's swap quota, but it is utilizing a device (SSD2) that is distinct from the Parent's assignment. This could lead to situations where the Parent's limit is exhausted by usage on a device it effectively doesn't "own" or shouldn't be using.

Quoted 7 lines
Quoted 2 lines

One might suggest restricting BPF to strictly adhere to these hierarchical constraints.

No need to constraint anything.

Taking a step back, can you describe your use-case a bit more and share requirements?

Quoted 6 lines

There is a very long thread on the linux-mm maillist. I'm too lazy to dig it up.

I can share our usage requirement to refresh your memory. We internally use a cgroup swapfile control interface that has not been upstreamed. With this we can remove the need of that internal interface and go upstream instead.

I already asked above but let me say it again. What's the actual real world use-case to control/allow/disallow swap devices hierarchically?

Quoted 26 lines
Quoted 7 lines

You have multiple swap devices of different properties and you want to assign those swap devices to different workloads. Now couple of questions:

1. If more than one device is assign to a workload, do you want to have some kind of ordering between them for the worklod or do you want option to have round robin kind of policy?

It depends on the number of devices in the tiers. Different tiers maintain an order. Within the same tier round robin.

Quoted 3 lines

2. What's the reason to use 'tiers' in the name? Is it similar to memory tiers and you want promotion/demotion among the tiers?

I propose the tier name. Guilty. Yes, in was inpired by memory tiers. It just different class of swap speeds. I am not fixed on the name. We can also call it swap.device_speed_classes. You can suggest alternatives.

Promotion / demotion is possible in the future. The current state, without promotion or demotion, already provides value. Our current deployment uses only one class of swap device at a time. However I do know other companies use more than one class of swap device.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

3. If a workload has multiple swap devices assigned, can you describe the scenario where such workloads need to partition/divide given devices to their sub-workloads?

In our deployment, we always use more than one swap device to reduce swap device lock contention.

Having more than one swap devices to reduce lock contention is unrelated to hierarchically control swap devices among sub-workloads.

author reviewer
#22 lore raw
Quoted 69 lines · Shakeel Butt
Quoted 1 line · Chris Li
Quoted 1 line

[...]

Quoted 10 lines
Quoted 8 lines
Quoted 4 lines

I agree that using BPF would provide greater flexibility, allowing control not just at the memcg level, but also per-process or for complex workloads. (As like orchestrator and node controller)

Yes it provides the flexibility but that is not the main reason I am pushing for it. The reason I want you to first try the BPF approach without introducing any stable interfaces. Show how swap tiers will be used and configured in production

Is that your biggest concern?

No, that is secondary because I am not seeing the real use-case of controlling/partitioning swap devices among sub-workloads. Until that is figured out, adding a stable API is not good.

Quoted 5 lines

Many different ways exist to solve that
problem. e.g. We can put a config option protecting it and mark it as experimental. This will unblock the development allow experiment. We can have more people to try it out and give feedback.

Quoted 7 lines
Quoted 3 lines

environment and then we can talk if a stable interface is needed. I am still not convinced that swap tiers need to be controlled hierarchically and the non-root should be able to control it.

Yes, my company uses a different swap device at different cgroup level. I did ask my coworker to confirm that usage. Control at the non root level is a real need.

I am assuming you meant Google and particularly Prodkernel team and not Android or ChromeOS. Google's prodkernel used to have per-cgroup swapfiles exposed through memory.swapfiles (if I remember correctly Suleiman implemented this along with ghost swapfiles). Later this was deprecated (by Yu Zhao) and global (ghost) swapfiles were being used. The memory.swapfiles interface instead of supporting real swapfiles started having select options among default, ghost/zswap and real (something like that). However such interface was used to just disable or enable zswap for a workload and never about hierarchically controlling the swap devices (Google prodkernel only have zswap). Has something changed?

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

However, I am concerned that this level of freedom might introduce logical contradictions, particularly regarding cgroup hierarchy semantics.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

For example, BPF might allow a topology that violates hierarchical constraints (a concern that was also touched upon during LPC)

Yes BPF provides more power but it is controlled by admin and admin can shoot their foot in multiple ways.

I think this swap device control is a very basic need.

Please explain that very basic need.

Quoted 9 lines

All your
objections to swapping control in the group can equally apply to zswap.writeback. Unlike zswap.writeback, which only control from the zswap behavior. This is a more generic version control swap device other than zswap as well. BTW, I raised that concern about zswap.writeback was not generic enough as swap control was limited when zswap was proposed. We did hold back zswap.writeback. The consensers is interface can be improved as later iterations. So here we are.

Quoted 3 lines

This just motivates me to pushback even harder on adding a new interface without a clear use-case.

....

After reading the reply and re-think more of it.

I have a few questions regarding the BPF-first approach you suggested, if you don't mind. Some of them I am re-asking because I feel they have not been clearly addressed yet.

- We are in an embedded environment where enabling additional kernel compile options is costly. BPF is disabled by default in some of our production configurations. From a trade-off perspective, does it make sense to enable BPF just for swap device control?

- You suggest starting with BPF and discussing a stable interface later. I am genuinely curious, are there actual precedents where a BPF prototype graduated into a stable kernel interface?

- You raised that stable interfaces are hard to remove. Would gating it behind a CONFIG option or marking it experimental be an acceptable compromise?

- You already acknowledged the use-case for assigning different swap devices to different workloads. Your objection is specifically about hierarchical parent-child partitioning. If the interface enforced uniform policy within a subtree, would that be acceptable?

- We already run a modified kernel with internal swap control in production and have real feedback from it. Requiring BPF as a prerequisite to gather production experience seems unnecessary when we are already doing that.

To be honest, I am having trouble understanding the motivation behind the BPF-first validation approach. If the real point is that BPF enables more flexible swap-out policies than any fixed interface can, that would make much more sense to me. I would appreciate it if you could share more on this.

Thanks,
Youngjun Park

maintainer
#23 lore raw

Hi YoungJun,

Sorry for the late response.

On Sun, Feb 22, 2026 at 10:16:04AM +0900, YoungJun Park wrote:
[...]

Let me summarize our discussion first:

You have a use-case where they have systems running multiple workloads and have multiple swap devices. Those swap devices have different performance capabilities and they want to restrict/assign swap devices to the workloads. For example assigning a low latency SSD swap device to latency sensitive workload and slow disk swap to latency tolerant workload. (please correct me if I misunderstood something).

The use-case seems reasonable to me but I have concerns related to adding an interface to memory cgroups. Mainly I am not clear how hierarchical semantics on such interface would look like. In addition, I think it would be too rigid and will be very hard to evolve for future features. To me enabling this functionality through BPF would give much more flexibility and will be more future proof.

Quoted 7 lines

After reading the reply and re-think more of it.

I have a few questions regarding the BPF-first approach you suggested, if you don't mind. Some of them I am re-asking because I feel they have not been clearly addressed yet.

Quoted 5 lines

- We are in an embedded environment where enabling additional kernel compile options is costly. BPF is disabled by default in some of our production configurations. From a trade-off perspective, does it make sense to enable BPF just for swap device control?

To me, it is reasonable to enable BPF for environment running multiple workloads and having multiple swap devices.

Quoted 5 lines

- You suggest starting with BPF and discussing a stable interface later. I am genuinely curious, are there actual precedents where a BPF prototype graduated into a stable kernel interface?

After giving some thought, I think once we have BPF working, adding another interface for the same feature would not be an option. So, we have decide upfront which route to take.

Quoted 4 lines

- You raised that stable interfaces are hard to remove. Would gating it behind a CONFIG option or marking it experimental be an acceptable compromise?

I think hiding behind CONFIG options do not really protect against the usage and the rule of no API breakage usually apply.

Quoted 6 lines

- You already acknowledged the use-case for assigning different swap devices to different workloads. Your objection is specifically about hierarchical parent-child partitioning. If the interface enforced uniform policy within a subtree, would that be acceptable?

Let's start with that or maybe comeup with concrete examples on how that would look like.

Beside, give a bit more thought on potential future features e.g. demotion and reason about how you would incorporate those features.

author reviewer
#24 lore raw
Quoted 4 lines · Shakeel Butt

Hi YoungJun,

Sorry for the late response.

Hi Shakeel, No problem :)

Quoted 11 lines · YoungJun Park

[...]

Let me summarize our discussion first:

You have a use-case where they have systems running multiple workloads and have multiple swap devices. Those swap devices have different performance capabilities and they want to restrict/assign swap devices to the workloads. For example assigning a low latency SSD swap device to latency sensitive workload and slow disk swap to latency tolerant workload. (please correct me if I misunderstood something).

Thanks for the summary! Yes, that describes our use case accurately.

Quoted 14 lines

The use-case seems reasonable to me but I have concerns related to adding an interface to memory cgroups. Mainly I am not clear how hierarchical semantics on such interface would look like. In addition, I think it would be too rigid and will be very hard to evolve for future features. To me enabling this functionality through BPF would give much more flexibility and will be more future proof.

Quoted 7 lines

After reading the reply and re-think more of it.

I have a few questions regarding the BPF-first approach you suggested, if you don't mind. Some of them I am re-asking because I feel they have not been clearly addressed yet.

Quoted 8 lines
Quoted 5 lines

- We are in an embedded environment where enabling additional kernel compile options is costly. BPF is disabled by default in some of our production configurations. From a trade-off perspective, does it make sense to enable BPF just for swap device control?

To me, it is reasonable to enable BPF for environment running multiple workloads and having multiple swap devices.

I agree with the value and flexibility of BPF, but there is still some debate on whether it should be the mandatory prerequisite for this feature.

While I understand your point that BPF is reasonable for complex environments, requiring BPF solely for this feature in resource-constrained embedded systems (where it might be disabled by default) is a significant hurdle. This trade-off is a key reason why I am advocating for the memcg interface.

Quoted 9 lines
Quoted 5 lines

- You suggest starting with BPF and discussing a stable interface later. I am genuinely curious, are there actual precedents where a BPF prototype graduated into a stable kernel interface?

After giving some thought, I think once we have BPF working, adding another interface for the same feature would not be an option. So, we have decide upfront which route to take.

Yes, effectively, if BPF is applied, it opens up the possibility to control swap logic at various levels (process unit, cgroup unit, etc.), so it would likely become a superset of other mechanisms.

However, despite BPF's benefits, I would like to propose proceeding with a more restricted memcg approach, which I will detail below.

Quoted 6 lines
Quoted 4 lines

- You raised that stable interfaces are hard to remove. Would gating it behind a CONFIG option or marking it experimental be an acceptable compromise?

I think hiding behind CONFIG options do not really protect against the usage and the rule of no API breakage usually apply.

Understood. My suggestion regarding the CONFIG option was to mark it as 'experimental' to allow usage for specific cases like ours while reserving broader API stabilization for later, but I accept your point about API breakage rules.

Quoted 8 lines
Quoted 5 lines

- You already acknowledged the use-case for assigning different swap devices to different workloads. Your objection is specifically about hierarchical parent-child partitioning. If the interface enforced uniform policy within a subtree, would that be acceptable?

Let's start with that or maybe comeup with concrete examples on how that would look like.

So, just to clarify, are you open to discussing this restricted direction?

To reiterate, this would mean enforcing a uniform policy for all children within a memcg where the swap tier is configured.

For our use case, this is currently sufficient.

We deal with memcg's tree itself as one workload. This workload can use its specific swap device selectively. This is my view.

Chris, would you be okay with proceeding in this direction as a starting point?

Quoted 2 lines

Beside, give a bit more thought on potential future features e.g. demotion and reason about how you would incorporate those features.

Regarding demotion (assuming you refer to migration based on swap device tiers), I don't foresee issues if we apply tiered swap devices per memcg.

In fact, the 'tier' concept was proposed specifically as an abstraction layer to structure hierarchical swap devices. Since the current direction treats it as a unified tier view configured by the parent memcg, features like demotion should fit naturally.

Regarding future extensibility, I would like to add:

1. From the memcg perspective:
Applying memcg in this restricted manner minimizes complexity. While future expansions (such as complex tier inheritance rules or handling setting differences between parent and child) will require careful discussion, the restricted approach avoids immediate conflicts and side effects.

2. The swap tier abstraction itself:
The introduction of swap tiers primarily enables swap device assignment. However, this abstraction also opens the door for extended use cases such as inter-tier migration (demotion), round-robin policies between tiers, tier-based VMA swap, or even per-process swap controls in the future.

I believe this patch set is acceptable as it introduces this foundational concept and applies a specific use case that does not contradict core memcg principles.

Best regards,
Youngjun Park

maintainer
#25 lore raw

Hi YoungJun, sorry for the late response.

On Wed, Mar 04, 2026 at 04:27:37PM +0900, YoungJun Park wrote:
[...]

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines

- You already acknowledged the use-case for assigning different swap devices to different workloads. Your objection is specifically about hierarchical parent-child partitioning. If the interface enforced uniform policy within a subtree, would that be acceptable?

Let's start with that or maybe comeup with concrete examples on how that would look like.

So, just to clarify, are you open to discussing this restricted direction?

I am open to all options. The only thing I am looking for is we have thought through the pros and cons of all the options and have thought about extensibility of the interface.

Quoted 3 lines

To reiterate, this would mean enforcing a uniform policy for all children within a memcg where the swap tier is configured.

Give me an example on how this would look like.

Quoted 28 lines

For our use case, this is currently sufficient.

We deal with memcg's tree itself as one workload. This workload can use its specific swap device selectively. This is my view.

Chris, would you be okay with proceeding in this direction as a starting point?

Quoted 2 lines

Beside, give a bit more thought on potential future features e.g. demotion and reason about how you would incorporate those features.

Regarding demotion (assuming you refer to migration based on swap device tiers), I don't foresee issues if we apply tiered swap devices per memcg.

In fact, the 'tier' concept was proposed specifically as an abstraction layer to structure hierarchical swap devices. Since the current direction treats it as a unified tier view configured by the parent memcg, features like demotion should fit naturally.

Regarding future extensibility, I would like to add:

1. From the memcg perspective:
Applying memcg in this restricted manner minimizes complexity. While future expansions (such as complex tier inheritance rules or handling setting differences between parent and child) will require careful discussion, the restricted approach avoids immediate conflicts and side effects.

Quoted 5 lines

2. The swap tier abstraction itself:
The introduction of swap tiers primarily enables swap device assignment. However, this abstraction also opens the door for extended use cases such as inter-tier migration (demotion), round-robin policies between tiers, tier-based VMA swap, or even per-process swap controls in the future.

These are good examples. Just show how the proposed interface will be good for such features. No need to implement any such feature.

author reviewer
#26 lore raw
Quoted 54 lines · Shakeel Butt

Hi YoungJun, sorry for the late response.

On Wed, Mar 04, 2026 at 04:27:37PM +0900, YoungJun Park wrote:
[...]

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines

- You already acknowledged the use-case for assigning different swap devices to different workloads. Your objection is specifically about hierarchical parent-child partitioning. If the interface enforced uniform policy within a subtree, would that be acceptable?

Let's start with that or maybe comeup with concrete examples on how that would look like.

So, just to clarify, are you open to discussing this restricted direction?

I am open to all options. The only thing I am looking for is we have thought through the pros and cons of all the options and have thought about extensibility of the interface.

Quoted 3 lines

To reiterate, this would mean enforcing a uniform policy for all children within a memcg where the swap tier is configured.

Give me an example on how this would look like.

Quoted 28 lines

For our use case, this is currently sufficient.

We deal with memcg's tree itself as one workload. This workload can use its specific swap device selectively. This is my view.

Chris, would you be okay with proceeding in this direction as a starting point?

Quoted 2 lines

Beside, give a bit more thought on potential future features e.g. demotion and reason about how you would incorporate those features.

Regarding demotion (assuming you refer to migration based on swap device tiers), I don't foresee issues if we apply tiered swap devices per memcg.

In fact, the 'tier' concept was proposed specifically as an abstraction layer to structure hierarchical swap devices. Since the current direction treats it as a unified tier view configured by the parent memcg, features like demotion should fit naturally.

Regarding future extensibility, I would like to add:

1. From the memcg perspective:
Applying memcg in this restricted manner minimizes complexity. While future expansions (such as complex tier inheritance rules or handling setting differences between parent and child) will require careful discussion, the restricted approach avoids immediate conflicts and side effects.

Quoted 8 lines
Quoted 5 lines

2. The swap tier abstraction itself:
The introduction of swap tiers primarily enables swap device assignment. However, this abstraction also opens the door for extended use cases such as inter-tier migration (demotion), round-robin policies between tiers, tier-based VMA swap, or even per-process swap controls in the future.

These are good examples. Just show how the proposed interface will be good for such features. No need to implement any such feature.

Thank you for the feedback. I understand your point.
I have already replied to the other comments as well. Since this thread has become quite cluttered with various topics, I believe it would be better to address this in the upcoming v5 patchset.

I will decide on the implementation and design based on our discussions (including the ones with Chris). I will also provide an explanation of the reasoning and future extensibility in the v5 cover letter. It would be great to continue our discussion and make a decision in the new v5 thread.

Best regards,
Youngjun Park

author reviewer
#27 lore raw
In reply to Shakeel Butt
Quoted 65 lines · Shakeel Butt

Hi YoungJun,

Sorry for the late response.

On Sun, Feb 22, 2026 at 10:16:04AM +0900, YoungJun Park wrote:
[...]

Let me summarize our discussion first:

You have a use-case where they have systems running multiple workloads and have multiple swap devices. Those swap devices have different performance capabilities and they want to restrict/assign swap devices to the workloads. For example assigning a low latency SSD swap device to latency sensitive workload and slow disk swap to latency tolerant workload. (please correct me if I misunderstood something).

The use-case seems reasonable to me but I have concerns related to adding an interface to memory cgroups. Mainly I am not clear how hierarchical semantics on such interface would look like. In addition, I think it would be too rigid and will be very hard to evolve for future features. To me enabling this functionality through BPF would give much more flexibility and will be more future proof.

Quoted 7 lines

After reading the reply and re-think more of it.

I have a few questions regarding the BPF-first approach you suggested, if you don't mind. Some of them I am re-asking because I feel they have not been clearly addressed yet.

Quoted 5 lines

- We are in an embedded environment where enabling additional kernel compile options is costly. BPF is disabled by default in some of our production configurations. From a trade-off perspective, does it make sense to enable BPF just for swap device control?

To me, it is reasonable to enable BPF for environment running multiple workloads and having multiple swap devices.

Quoted 5 lines

- You suggest starting with BPF and discussing a stable interface later. I am genuinely curious, are there actual precedents where a BPF prototype graduated into a stable kernel interface?

After giving some thought, I think once we have BPF working, adding another interface for the same feature would not be an option. So, we have decide upfront which route to take.

Quoted 4 lines

- You raised that stable interfaces are hard to remove. Would gating it behind a CONFIG option or marking it experimental be an acceptable compromise?

I think hiding behind CONFIG options do not really protect against the usage and the rule of no API breakage usually apply.

Quoted 6 lines

- You already acknowledged the use-case for assigning different swap devices to different workloads. Your objection is specifically about hierarchical parent-child partitioning. If the interface enforced uniform policy within a subtree, would that be acceptable?

Quoted 5 lines

Let's start with that or maybe comeup with concrete examples on how that would look like.

Beside, give a bit more thought on potential future features e.g. demotion and reason about how you would incorporate those features.

Hello Shakeel, Chris Li,

Just sending a gentle ping on my previous reply. :D

To quickly summarize the main points:
(I might wrongly undestand your intentaion, then correct me please :) )

* Regarding Shakeel's BPF approach, stable interface movement would be difficult, so we need to choose a direction. I prefer adding it to memcg for immediate usage, and if it proves highly effective, we can consider transitioning entirely to BPF later.

* Shakeel seemed somewhat positive about matching all child tiers from the parent if tiers are applied to a specific cgroup use case, and I would like to start the discussion from here. Chris, I would appreciate your thoughts on whether you agree with this direction of unifying all swap tiers within the hierarchy as a first step.

Here are some additional thoughts I had after my last reply:
(Thanks for the insight and discussion. Hyungjun Cho)

* Cgroup distribution:
A direct use case where cgroup A distributes a portion to A' is hard to imagine, but the following scenario is possible:

swap: +SSD +HDD +NET
cgroup hierarchy:
/
A : +HDD +NET
A'(app 1) +HDD, A''(app 2) +NET

Cgroup A has two interdependent apps, and +SSD is excluded for more critical services. App1 (A') avoids reclaim with a large hot working set using fast +HDD, while App2 (A'') has a cold working set using slow/large +NET.

* Promotion / Demotion:
Unlike memory tiers, swap tiers are directly assigned by the user, providing flexibility beyond just speed. Since swap priority is already a user choice, this design makes perfect sense.

With this arbitrary assignment, we can support higher-to-slower tier allocation, similar to current memory tiers, if user properly bind the tier. (more flexible as I think)

Within the same tier (meaning we define it as equal speed(tier)), we could apply round-robin or other distribution policies via an additional tier layer interface. The current equal-priority round-robin policy could also be elevated to the tier layer.

Best regards,
Youngjun Park

maintainer
#28 lore raw

Hi YoungJun,

Quoted 67 lines · YoungJun Park
Quoted 65 lines · Shakeel Butt

Hi YoungJun,

Sorry for the late response.

On Sun, Feb 22, 2026 at 10:16:04AM +0900, YoungJun Park wrote:
[...]

Let me summarize our discussion first:

You have a use-case where they have systems running multiple workloads and have multiple swap devices. Those swap devices have different performance capabilities and they want to restrict/assign swap devices to the workloads. For example assigning a low latency SSD swap device to latency sensitive workload and slow disk swap to latency tolerant workload. (please correct me if I misunderstood something).

The use-case seems reasonable to me but I have concerns related to adding an interface to memory cgroups. Mainly I am not clear how hierarchical semantics on such interface would look like. In addition, I think it would be too rigid and will be very hard to evolve for future features. To me enabling this functionality through BPF would give much more flexibility and will be more future proof.

Quoted 7 lines

After reading the reply and re-think more of it.

I have a few questions regarding the BPF-first approach you suggested, if you don't mind. Some of them I am re-asking because I feel they have not been clearly addressed yet.

Quoted 5 lines

- We are in an embedded environment where enabling additional kernel compile options is costly. BPF is disabled by default in some of our production configurations. From a trade-off perspective, does it make sense to enable BPF just for swap device control?

To me, it is reasonable to enable BPF for environment running multiple workloads and having multiple swap devices.

Quoted 5 lines

- You suggest starting with BPF and discussing a stable interface later. I am genuinely curious, are there actual precedents where a BPF prototype graduated into a stable kernel interface?

After giving some thought, I think once we have BPF working, adding another interface for the same feature would not be an option. So, we have decide upfront which route to take.

Quoted 4 lines

- You raised that stable interfaces are hard to remove. Would gating it behind a CONFIG option or marking it experimental be an acceptable compromise?

I think hiding behind CONFIG options do not really protect against the usage and the rule of no API breakage usually apply.

Quoted 6 lines

- You already acknowledged the use-case for assigning different swap devices to different workloads. Your objection is specifically about hierarchical parent-child partitioning. If the interface enforced uniform policy within a subtree, would that be acceptable?

Quoted 8 lines
Quoted 5 lines

Let's start with that or maybe comeup with concrete examples on how that would look like.

Beside, give a bit more thought on potential future features e.g. demotion and reason about how you would incorporate those features.

Hello Shakeel, Chris Li,

Just sending a gentle ping on my previous reply. :D

Sorry for the late reply, busy days.

Quoted 8 lines

To quickly summarize the main points:
(I might wrongly undestand your intentaion, then correct me please :) )

* Regarding Shakeel's BPF approach, stable interface movement would be difficult, so we need to choose a direction. I prefer adding it to memcg for immediate usage, and if it proves highly effective, we can consider transitioning entirely to BPF later.

I am very concerned about locking down the kernel user interface just because things might change in the future. If we need to use BPF to get the stable user space API, I am fine with that. Completely blocking the new cgroup interface because of a worry about future change is not justifiable IMHO. There ought to be some intermediate staging we can do e.g. debugfs interface to test and play with the new API. We should focus on designing the interface as well as possible right now.

Quoted 5 lines

* Shakeel seemed somewhat positive about matching all child tiers from the parent if tiers are applied to a specific cgroup use case, and I would like to start the discussion from here. Chris, I would appreciate your thoughts on whether you agree with this direction of unifying all swap tiers within the hierarchy as a first step.

Does that mean all children will only use the parent cgroup setting? Wouldn't that be more restrictive and counteract the goal of making the API more future-proof?
For the record, the current Google deployment can uses a different swap device for the child cgroup, in the current delpyment. The typical setup is that the top-level cgroup is a job running on a VM. Then there is a second cgroup level for the VMM guest memory allocation; swap device selection occurs at this second level. There is also zswap vs SSD, the SSD is something new starting the deployment. So it's not just about enabling zswap or not. We also need to select the swap device.
If you get the current cgroup, it will need to walk the parent cgroup chain to find the toplevel cgroup any way. I just think having the hierarchy makes more sense.

Quoted 13 lines

Here are some additional thoughts I had after my last reply:
(Thanks for the insight and discussion. Hyungjun Cho)

* Cgroup distribution:
A direct use case where cgroup A distributes a portion to A' is hard to imagine, but the following scenario is possible:

swap: +SSD +HDD +NET
cgroup hierarchy:
/
A : +HDD +NET
A'(app 1) +HDD, A''(app 2) +NET

Quoted 3 lines

Cgroup A has two interdependent apps, and +SSD is excluded for more critical services. App1 (A') avoids reclaim with a large hot working set using fast +HDD, while App2 (A'') has a cold working set using slow/large +NET.

The app interface is a huge departure from the cgroup. The cgroup is a well defined interface.

Quoted 5 lines

* Promotion / Demotion:
Unlike memory tiers, swap tiers are directly assigned by the user, providing flexibility beyond just speed. Since swap priority is already a user choice, this design makes perfect sense.

We need to find customers willing to use this promotion/demotion. I hesitate to build something while hoping to find someone to use it later.
It would be good to identify someone who can immediately use and test this promotion/demotion feature.
We should focus the discussion on achieving a more flexible swap device selection approach and reach a conclusion on the API discussion before discussing promotion/demotion. If we can't even have a usable swap tier interface, there is nothing to promote.

Quoted 7 lines

With this arbitrary assignment, we can support higher-to-slower tier allocation, similar to current memory tiers, if user properly bind the tier. (more flexible as I think)

Within the same tier (meaning we define it as equal speed(tier)), we could apply round-robin or other distribution policies via an additional tier layer interface. The current equal-priority round-robin policy could also be elevated to the tier layer.

Chris

author reviewer
#29 lore raw
Quoted 1 line

Sorry for the late reply, busy days.

No worries at all. Hi Chris,

Quoted 9 lines
Quoted 8 lines

To quickly summarize the main points:
(I might wrongly undestand your intentaion, then correct me please :) )

* Regarding Shakeel's BPF approach, stable interface movement would be difficult, so we need to choose a direction. I prefer adding it to memcg for immediate usage, and if it proves highly effective, we can consider transitioning entirely to BPF later.

Quoted 8 lines

I am very concerned about locking down the kernel user interface just because things might change in the future. If we need to use BPF to get the stable user space API, I am fine with that. Completely blocking the new cgroup interface because of a worry about future change is not justifiable IMHO. There ought to be some intermediate staging we can do e.g. debugfs interface to test and play with the new API. We should focus on designing the interface as well as possible right now.

I completely agree with using the cgroup interface. We have clear use cases and have already shared many thoughts on this direction.

Quoted 6 lines
Quoted 5 lines

* Shakeel seemed somewhat positive about matching all child tiers from the parent if tiers are applied to a specific cgroup use case, and I would like to start the discussion from here. Chris, I would appreciate your thoughts on whether you agree with this direction of unifying all swap tiers within the hierarchy as a first step.

Quoted 14 lines

Does that mean all children will only use the parent cgroup setting? Wouldn't that be more restrictive and counteract the goal of making the API more future-proof?
For the record, the current Google deployment can uses a different swap device for the child cgroup, in the current delpyment. The typical setup is that the top-level cgroup is a job running on a VM. Then there is a second cgroup level for the VMM guest memory allocation; swap device selection occurs at this second level. There is also zswap vs SSD, the SSD is something new starting the deployment. So it's not just about enabling zswap or not. We also need to select the swap device.
If you get the current cgroup, it will need to walk the parent cgroup chain to find the toplevel cgroup any way. I just think having the hierarchy makes more sense.

Since you confirmed the use case for allowing child cgroups to have different swap devices from their parents, I would like to keep the current hierarchical design. It seems Shakeel was hesitant mainly due to the lack of a known use case for this. If Shakeel has further questions, I would appreciate it if you could help explain the Google deployment use case.

Quoted 10 lines

We need to find customers willing to use this promotion/demotion. I hesitate to build something while hoping to find someone to use it later.
It would be good to identify someone who can immediately use and test this promotion/demotion feature.
We should focus the discussion on achieving a more flexible swap device selection approach and reach a conclusion on the API discussion before discussing promotion/demotion. If we can't even have a usable swap tier interface, there is nothing to promote.

Since I have already sent RFCv3, Patch v4
(https://lore.kernel.org/linux-mm/[email protected]/)_
and we seem aligned on the core direction,
I will proceed with sending v5 based on the current design.

@Shakeel: I have been waiting for your feedback longtime, but I will move forward with v5 for now. Since Chris confirmed a solid use case for the child-parent hierarchy, please let us know if you have any remaining concerns when I post the v5 patch.

As a brief history, we have evolved this proposal from per-cgroup priority to swap tiers, presented it at LPC, and are actually using it in our production environment. I sincerely hope we can get this merged into mainline. and evolve this mechanism after applying it.

Thanks,
Youngjun Park

author reviewer
#30 lore raw
In reply to Shakeel Butt
Quoted 2 lines · Shakeel Butt

Please don't send a new version of the series before concluding the discussion on the previous one.

Understood. Let's continue the discussion. :D

Chris has already provided a thorough response, but I would like to add my perspective as well.

Quoted 4 lines

Yes it provides the flexibility but that is not the main reason I am pushing for it. The reason I want you to first try the BPF approach without introducing any stable interfaces. Show how swap tiers will be used and configured in production environment and then we can talk if a stable interface is needed.

I understand your concern about committing to a stable interface too early. As Chris suggested, we could reduce this concern by guarding the interface behind a build-time config option or marking it as experimental, which I will also touch on further below.

On that note, if BPF were to become the primary control mechanism, I am not sure a memcg interface would still be needed at all, since BPF already provides a high degree of freedom. However, that level of freedom is also what concerns me -- BPF-driven swap device assignments could subtly conflict with memcg hierarchy semantics in ways that are hard to predict or debug. A more constrained memcg-based approach might actually be safer in that regard.

Quoted 2 lines

I am still not convinced that swap tiers need to be controlled hierarchically and the non-root should be able to control it.

I think this concern is closely tied to your question #3 below about concrete use cases for partitioning devices across sub-workloads. I hope my answer there helps clarify this.

Quoted 2 lines

Yes BPF provides more power but it is controlled by admin and admin can shoot their foot in multiple ways.

As I mentioned above, I think guarding the feature behind a build-time config or runtime constraints could keep the usage well-defined and predictable, while still being useful.

Quoted 2 lines

Taking a step back, can you describe your use-case a bit more and share requirements?

Our use case is simple at now.
We have two swap devices with different performance characteristics and want to assign different swap devices to different workloads (cgroups).

For some background, when I initially proposed this, I suggested allowing per-cgroup swap device priorities so that it could also accommodate the broader scenarios you mentioned. However, since even our own use case does not require reversing swap priorities within a cgroup, we pivoted to the "swap tier" mechanism that Chris proposed.

Quoted 3 lines

1. If more than one device is assign to a workload, do you want to have some kind of ordering between them for the worklod or do you want option to have round robin kind of policy?

Both. If devices are in the same tier with the same priority, round robin. If they are in the same tier with different priorities, or in different tiers, ordering applies. The current tier structure should be able to satisfy either preference.

Quoted 2 lines

2. What's the reason to use 'tiers' in the name? Is it similar to memory tiers and you want promotion/demotion among the tiers?

This was originally Chris's idea. I think he explained the rationale well in his reply.

Quoted 3 lines

3. If a workload has multiple swap devices assigned, can you describe the scenario where such workloads need to partition/divide given devices to their sub-workloads?

One possible scenario is reducing lock contention by partitioning swap devices between parent and child cgroups.

Quoted 3 lines

Let's start with these questions. Please note that I want us to not just look at the current use-case but brainstorm more future use-cases and then come up with the solution which is more future proof.

We have clear production use cases from both us and Chris, and I also presented a deployment example in the cover letter.

I think it is hard to design concretely for future use cases at this point. When those needs become clearer, BPF with its flexibility would be a better fit then. I see BPF as a natural extension path rather than a starting point.

For now, guarding the memcg & tier behind a CONFIG option would let us move forward without committing to a stable interface, and we can always pivot to BPF later if needed

Thanks,
YoungJun Park

maintainer
#31 lore raw

Hi YoungJun,

I see you have sent a separate email on BPF specific questions to which I will respond separately, here I will respond to other questions/comments.

Quoted 1 line · YoungJun Park

On Fri, Feb 20, 2026 at 07:47:22PM -0800, Shakeel Butt wrote:

[...]

Quoted 8 lines
Quoted 3 lines

Taking a step back, can you describe your use-case a bit more and share requirements?

Our use case is simple at now.
We have two swap devices with different performance characteristics and want to assign different swap devices to different workloads (cgroups).

If you don't mind, can you share a bit more about the cgroup hierarchy structure of your deployment. Do you use cgroup v1 or v2 on your production environment?

Quoted 7 lines

For some background, when I initially proposed this, I suggested allowing per-cgroup swap device priorities so that it could also accommodate the broader scenarios you mentioned. However, since even our own use case does not require reversing swap priorities within a cgroup, we pivoted to the "swap tier" mechanism that Chris proposed.

Quoted 8 lines
Quoted 3 lines

1. If more than one device is assign to a workload, do you want to have some kind of ordering between them for the worklod or do you want option to have round robin kind of policy?

Both. If devices are in the same tier with the same priority, round robin. If they are in the same tier with different priorities, or in different tiers, ordering applies. The current tier structure should be able to satisfy either preference.

I assume this is the same swap priorities as of today, right? You want similar priority behavior within a tier.

Quoted 7 lines
Quoted 3 lines

2. What's the reason to use 'tiers' in the name? Is it similar to memory tiers and you want promotion/demotion among the tiers?

This was originally Chris's idea. I think he explained the rationale well in his reply.

Quoted 6 lines
Quoted 3 lines

3. If a workload has multiple swap devices assigned, can you describe the scenario where such workloads need to partition/divide given devices to their sub-workloads?

One possible scenario is reducing lock contention by partitioning swap devices between parent and child cgroups.

The lock contention is orthogonal (and distraction here).

Quoted 8 lines
Quoted 4 lines

Let's start with these questions. Please note that I want us to not just look at the current use-case but brainstorm more future use-cases and then come up with the solution which is more future proof.

We have clear production use cases from both us and Chris, and I also presented a deployment example in the cover letter.

Quoted 8 lines

I think it is hard to design concretely for future use cases at this point. When those needs become clearer, BPF with its flexibility would be a better fit then. I see BPF as a natural extension path rather than a starting point.

For now, guarding the memcg & tier behind a CONFIG option would let us move forward without committing to a stable interface, and we can always pivot to BPF later if needed

I think your use-case is very clear. Before committing to any options, I want us to brainstorm all options and gather pros/cons and then make an informed decision. Anyways I will respond to your other email (in a day or two).

Shakeel

author reviewer
#32 lore raw

On Sun, Feb 22, 2026 at 09:56:13PM -0800, Shakeel Butt wrote:

While I await your response on the other thread,
I thought I would answer these questions first :)

Quoted 12 lines

Hi YoungJun,

I see you have sent a separate email on BPF specific questions to which I will respond separately, here I will respond to other questions/comments.

Quoted 1 line · YoungJun Park

On Fri, Feb 20, 2026 at 07:47:22PM -0800, Shakeel Butt wrote:

[...]

Quoted 4 lines
Quoted 3 lines

Taking a step back, can you describe your use-case a bit more and share requirements?

Quoted 7 lines
Quoted 4 lines

Our use case is simple at now.
We have two swap devices with different performance characteristics and want to assign different swap devices to different workloads (cgroups).

If you don't mind, can you share a bit more about the cgroup hierarchy structure of your deployment. Do you use cgroup v1 or v2 on your production environment?

We are primarily targeting Cgroup v2 at now.

Quoted 11 lines
Quoted 11 lines
Quoted 1 line

For some background, when I initially proposed this, I suggested allowing per-cgroup swap device priorities so that it could also accommodate the broader scenarios you mentioned. However, since even our own use case does not require reversing swap priorities within a cgroup, we pivoted to the "swap tier" mechanism that Chris proposed.

Quoted 3 lines

1. If more than one device is assign to a workload, do you want to have some kind of ordering between them for the worklod or do you want option to have round robin kind of policy?

Quoted 7 lines
Quoted 4 lines

Both. If devices are in the same tier with the same priority, round robin. If they are in the same tier with different priorities, or in different tiers, ordering applies. The current tier structure should be able to satisfy either preference.

I assume this is the same swap priorities as of today, right? You want similar priority behavior within a tier.

That is correct; the swap priority behavior remains unchanged.

While this is slightly tangential, I see a potential use case for swap tiers to improve the current swap device selection logic, which is currently tightly coupled with priority.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

2. What's the reason to use 'tiers' in the name? Is it similar to memory tiers and you want promotion/demotion among the tiers?

This was originally Chris's idea. I think he explained the rationale well in his reply.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

3. If a workload has multiple swap devices assigned, can you describe the scenario where such workloads need to partition/divide given devices to their sub-workloads?

One possible scenario is reducing lock contention by partitioning swap devices between parent and child cgroups.

The lock contention is orthogonal (and distraction here).

Understood. It was just a hypothetical scenario where I thought there might be an additional benefit, but I agree we can set it aside for now.

Quoted 13 lines
Quoted 13 lines
Quoted 4 lines

Let's start with these questions. Please note that I want us to not just look at the current use-case but brainstorm more future use-cases and then come up with the solution which is more future proof.

We have clear production use cases from both us and Chris, and I also presented a deployment example in the cover letter.

I think it is hard to design concretely for future use cases at this point. When those needs become clearer, BPF with its flexibility would be a better fit then. I see BPF as a natural extension path rather than a starting point.

Quoted 6 lines
Quoted 3 lines

For now, guarding the memcg & tier behind a CONFIG option would let us move forward without committing to a stable interface, and we can always pivot to BPF later if needed

I think your use-case is very clear. Before committing to any options, I want us to brainstorm all options and gather pros/cons and then make an informed

This relates to my response in the other email thread, and I think it would be good to discuss it further there.

It seems the concern is that distributing swap devices using the memcg hierarchy might be seen as over-engineering (overspec) since there isn't a concrete use case for it yet. I have included a proposal to mitigate this concern in the other thread.

Quoted 1 line

decision. Anyways I will respond to your other email (in a day or two).

Sounds good. I look forward to your explanation.

Best regards,
Youngjun Park

author reviewer
#33 lore raw
In reply to Shakeel Butt
Quoted 72 lines · Shakeel Butt

Hi YoungJun,

I see you have sent a separate email on BPF specific questions to which I will respond separately, here I will respond to other questions/comments.

Quoted 1 line · YoungJun Park

On Fri, Feb 20, 2026 at 07:47:22PM -0800, Shakeel Butt wrote:

[...]

Quoted 8 lines
Quoted 3 lines

Taking a step back, can you describe your use-case a bit more and share requirements?

Our use case is simple at now.
We have two swap devices with different performance characteristics and want to assign different swap devices to different workloads (cgroups).

If you don't mind, can you share a bit more about the cgroup hierarchy structure of your deployment. Do you use cgroup v1 or v2 on your production environment?

Quoted 7 lines

For some background, when I initially proposed this, I suggested allowing per-cgroup swap device priorities so that it could also accommodate the broader scenarios you mentioned. However, since even our own use case does not require reversing swap priorities within a cgroup, we pivoted to the "swap tier" mechanism that Chris proposed.

Quoted 8 lines
Quoted 3 lines

1. If more than one device is assign to a workload, do you want to have some kind of ordering between them for the worklod or do you want option to have round robin kind of policy?

Both. If devices are in the same tier with the same priority, round robin. If they are in the same tier with different priorities, or in different tiers, ordering applies. The current tier structure should be able to satisfy either preference.

I assume this is the same swap priorities as of today, right? You want similar priority behavior within a tier.

Quoted 7 lines
Quoted 3 lines

2. What's the reason to use 'tiers' in the name? Is it similar to memory tiers and you want promotion/demotion among the tiers?

This was originally Chris's idea. I think he explained the rationale well in his reply.

Quoted 6 lines
Quoted 3 lines

3. If a workload has multiple swap devices assigned, can you describe the scenario where such workloads need to partition/divide given devices to their sub-workloads?

One possible scenario is reducing lock contention by partitioning swap devices between parent and child cgroups.

The lock contention is orthogonal (and distraction here).

Quoted 8 lines
Quoted 4 lines

Let's start with these questions. Please note that I want us to not just look at the current use-case but brainstorm more future use-cases and then come up with the solution which is more future proof.

We have clear production use cases from both us and Chris, and I also presented a deployment example in the cover letter.

Quoted 8 lines

I think it is hard to design concretely for future use cases at this point. When those needs become clearer, BPF with its flexibility would be a better fit then. I see BPF as a natural extension path rather than a starting point.

For now, guarding the memcg & tier behind a CONFIG option would let us move forward without committing to a stable interface, and we can always pivot to BPF later if needed

Quoted 5 lines

I think your use-case is very clear. Before committing to any options, I want us to brainstorm all options and gather pros/cons and then make an informed decision. Anyways I will respond to your other email (in a day or two).

Shakeel

Hi Shakeel,

Just a friendly ping on this thread :D. I understand you must be busy, so no rush at all.

I'm quite eager to move forward on this — even if it starts under an experimental CONFIG option, that would be perfectly fine from our side. We'd love to iterate on it incrementally.

Best regards,
Youngjun Park