mm/swap, memcg: Introduce swap tiers for cgroup based swap control
Subsystems (8)
v9 cover mm/swap, memcg: Introduce swap tiers for cgroup based swap control
This is the v9 series of the swap tier patchset.
The main change in this version is the addition of selftests for the tier interfaces, requested by Nhat; see the changelog below for the other changes. I designed the test cases and wrote the selftests with some AI assistance.
For context, the bulk of the series is unchanged since v8, with great thanks to Shakeel Butt and Yosry for the reviews and discussions [1] that shaped it. The main change in v8 was the interface change to use memory.swap.tiers.max with '0' (disable) and 'max' (enable) values. This mechanism was suggested by Shakeel and Yosry.
This change allows for future extensions to control swap between tiers and aligns better with existing memcg interfaces. It is confined to patch #3's user-facing interface; internally, patch #3 still uses the existing mask processing method, which is implementation-efficient.
We also discussed tier extensions. Thanks to Yosry, Nhat and Shakeel for their valuable feedback.
Here is a brief summary of our tentative conclusions. Please correct me if anything is misrepresented (details in references):
* Zswap tiering [2]:
Tiering applies only to the vswap + zswap combo. Zswap itself will not be tiered, as the current architecture requires a physical device for zswap allocation.
* Vswap tiering [3]:
Vswap should be handled transparently to the user. Vswap itself will not be tiered. But, someday supported if there is strong and real usecase.
* Relationship with zswap.writeback [4]:
If zswap tiering is introduced, it could replace the zswap-only tier. However, since zswap cannot be tiered independently, it is still needed for non-vswap cases. Separately, the internal logic could potentially be integrated into the tiering logic.
* Tier demotion [5]:
A separate interface like memory.swap.tiers.demotion might be needed. For now, we only support 0/max to enable/disable tiers. In the future, we could introduce an "auto" mode to automatically scale the limit based on swapfile size and memory.swap.max, similar to the direction memory tiering is heading in.
I plan to apply the swap tier infrastructure and the first use case (cgroup-based swap control) first, and continue following up on the discussions above.
Overview
========
Swap Tiers group swap devices into performance classes (e.g. NVMe, HDD, Network) and allow per-memcg selection of which tiers to use. This mechanism was suggested by Chris Li.
Design Rationale
================
Swap tier selection is attached to memcg. A child cgroup may select a subset of the parent's allowed tiers.
This
- Preserves cgroup inheritance semantics (boundary at parent, refinement at child).
- Reuses memcg, which already groups processes and enforces hierarchical memory limits.
- Aligns with existing memcg swap controls (e.g. swap.max, zswap.writeback)
- Avoids introducing a parallel swap control hierarchy.
Placing tier control outside memcg (e.g., via BPF, syscalls, or madvise) would allow swap preference to diverge from the memcg hierarchy. Integrating it into memcg keeps the swap policy consistent with existing memory ownership semantics. There are also real use cases built around memcg.
In the future, this can be extended to other interfaces to cover additional use cases.
I believe a memcg-based swap control is a good starting point before such extensions.
Use Cases
=========
#1: Latency separation (our primary deployment scenario)
[ / ]
|
+-- latency-sensitive workload (fast tier)
+-- background workload (slow tier)
The parent defines the memory boundary.
Each workload selects a swap tier via memory.swap.tiers.max according to latency requirements.
This prevents latency-sensitive workloads from being swapped to slow devices used by background workloads.
#2: Per-VM swap selection (Chris Li's deployment scenario)
[ / ]
|
+-- [ Job on VM ] (tiers: zswap, SSD)
|
+-- [ VMM guest memory ] (tiers: SSD)
The parent (job) has access to both zswap and SSD tiers. The child (VMM guest memory) selects SSD as its swap tier via memory.swap.tiers.max. In this deployment, swap device selection happens at the child level from the parent's available set.
#3: Tier isolation for reduced contention (hypothetical)
[ / ] (tiers: A, B)
|
+-- workload X (tiers: A)
+-- workload Y (tiers: B)
Each child uses a different tier. Since swap paths are separated per tier, synchronization overhead between the two workloads is reduced.
Future extension (Follow up)
============================
#1: Intra-tier distribution policy:
Currently, swap devices with the same priority are allocated in a round-robin fashion. Per-tier policy files under /sys/kernel/mm/swap/tiers/ can control how devices within a tier are selected (e.g. round-robin, weighted).
#2: Inter-tier promotion and demotion:
Promotion and demotion apply between tiers, not within a single tier. The current interface defines only tier assignment; it does not yet define when or how pages move between tiers. Two triggering models are possible:
(a) User-triggered: userspace explicitly initiates migration between tiers (e.g. via a new interface or existing move_pages semantics).
(b) Kernel-triggered: the kernel moves pages between tiers at appropriate points such as reclaim or refault.
#3: Per-VMA, per-process swap and BPF:
Not just for memcg based swap, possible to extend Per-VMA or per-process swap. Or we can use it as BPF program.
#4: Zswap and vswap tiering:
Tiering applies to the vswap + zswap combination.
#5: Vswap on/off control:
Currently not supported. If a strong use case arises where vswap needs to be controlled by memcg, the tier interface could be used for it.
#6: Per-CPU swap allocation caching:
Per-si/per-tier per-CPU caching of allocations to reduce contention in the tier-filtered allocation path.
Experimentation
===============
Tested on our internal platform using NBD as a separate swap tier. Our first production's simple usecase.
Without tiers:
- No selective control over flash wear
- Cannot selectively assign NBD to specific applications
Cold launch improvement (preloaded vs. baseline):
- App A: 13.17s -> 4.18s (68%)
- App B: 5.60s -> 1.12s (80%)
- App C: 10.25s -> 2.00s (80%)
Performance impact with no tiers configured:
<1% regression in kernel build and vm-scalability benchmarks
Change log
===========
v9
- Added selftests (per Nhat's request):
- selftests/mm: swap tier configuration test for /sys/kernel/mm/swap/tiers.(#5 patch)
- selftests/cgroup: swap tier routing test for memory.swap.tiers.max. (#6 patch)
- Removed the redundant rcu_read_lock() around the memcg tier-mask tree walk; for_each_mem_cgroup_tree() already takes RCU internally and returns each memcg with a reference held. (#3 patch)
- Sashiko review: swap_sync_discard() now honors the memcg tier mask, so the discard fallback no longer drains clusters on disallowed tiers. Left as-is:
the cgroup tree walk under spinlock (bounded by cgroup.max.descendants, an admin-controlled limit, and triggered only by infrequent tier writes) and the pre-existing swap_avail_lock drop in swap_alloc_slow(). (#4 patch)
- Dropped patch #4's Reviewed-by tags (Nhat, Kairui, Baoquan): the swap_sync_discard() change above modifies that patch (the tier mask is now passed as a parameter into the alloc and discard paths), so the earlier tags no longer apply. Re-review would be welcome.
- v8 link: https://lore.kernel.org/linux-mm/[email protected]/
v8
- Changed the memcg interface to memory.swap.tiers.max.
Values are '0' (disable) and 'max' (enable). Default is 'max'.
- Addressed Sashiko's review: Update the mask value atomically at once and read the mask value while grabbing lock.
- Collected review tags from Kairui and Nhat.
- Rebase on recent mm-new
- v7 link: https://lore.kernel.org/linux-mm/[email protected]/
v7
- Collect Baoquan's review tag
- Baoquan's feedback on fixing improper comment
- Minor code adjustments per Baoquan's feedback.
- Rebase on recent mm-new
- v6 link: https://lore.kernel.org/linux-mm/[email protected]/
v6
- Sashiko AI review fixes
- Fix batch parsing error path to restore snapshot before exit
- Reject overlong tier names to prevent truncated duplicates
- Avoid restoring raw list_head via memcpy (stale pointer risk)
- Ensure early parse errors do not skip DEF_SWAP_PRIO validation
- Use (1U << TIER_DEFAULT_IDX) to avoid signed shift UB
- Defer tier mask inheritance to css_online() to close race window
- Add READ_ONCE()/WRITE_ONCE() for tier mask accesses
- Other fixes
- Fix build error reintroduced due to missing v5 change
- Fix WARNING in folio_tier_effective_mask by adding rcu_read_lock()
- default number of swap tier max (change to 32->31, for reserving last bit)
- commit message refinement.
- rebased on recently mm-new
- v5 link: https://lore.kernel.org/linux-mm/[email protected]/
v5
- Fixed build errors reported in v4
- rebased on up to date mm-new
- Minor cleanups
- Design docs with validation (by Shakeel Butt discussion)
- v4 link : https://lore.kernel.org/linux-mm/[email protected]/
v4
- Simplified control flow and indentation
- Added CONFIG option for MAX_SWAPTIER (default: 4)
- Added memory.swap.tiers.effective interface
- Reworked save/restore logic into snapshot/rollback model
- Removed tier priority modification support (deferred)
- Improved validation and fixed edge cases
- Rebased onto latest mm-new
- RFC v3 link: https://lore.kernel.org/linux-mm/[email protected]/
RFC v1 ~ v3
- Change the direction after discussion with Chris-Li
- apply some LPC feedback.
- RFC v2 - https://lore.kernel.org/linux-mm/[email protected]/
- RFC v1 - https://lore.kernel.org/linux-mm/[email protected]/
Earlier Approach (per cgroup swap priority)
- v1: https://lore.kernel.org/linux-mm/[email protected]/
- RFC: https://lore.kernel.org/linux-mm/aEvLjEInMQC7hEyh@yjaykim-PowerEdge-T330/T/#mbbb6a5e9e30843097e1f5f65fb98f31d582b973d
Reference
=========
[1] https://lore.kernel.org/linux-doc/[email protected]/
[2] https://lore.kernel.org/linux-mm/CAKEwX=Nz9SWcEVQGQjHN8P8OANJY4BG0w+iQOzoNOWuteoVjAg@mail.gmail.com/
[3] https://lore.kernel.org/cgroups/CAKEwX=O23a4iWBZoewKVb8QqODte6r3Xijckw3_oCJNoiO9M5A@mail.gmail.com/
[4] https://lore.kernel.org/linux-mm/CAO9r8zOg0OP1Ak1v7CRzSfQq0D8b4Dw+_T0Jui6YTM_KwQQNOA@mail.gmail.com/
[5] https://lore.kernel.org/linux-mm/CAO9r8zNi4-QC4sUi=xXWHt9WMeG39mbyoSf8kON9vLOZ=cbCmw@mail.gmail.com/
Youngjun Park (6):
mm: swap: introduce swap tier infrastructure
mm: swap: associate swap devices with tiers
mm: memcontrol: add interface for swap tier selection
mm: swap: filter swap allocation by memcg tier mask selftests/mm: add a swap tier configuration test selftests/cgroup: add a swap tier routing test
Documentation/admin-guide/cgroup-v2.rst | 20 + Documentation/mm/index.rst | 1 + Documentation/mm/swap-tier.rst | 159 ++++++ MAINTAINERS | 3 + include/linux/memcontrol.h | 5 + include/linux/swap.h | 1 + mm/Kconfig | 12 + mm/Makefile | 2 +- mm/memcontrol.c | 67 +++ mm/swap.h | 4 + mm/swap_state.c | 75 +++ mm/swap_tier.c | 477 +++++++++++++++++ mm/swap_tier.h | 76 +++ mm/swapfile.c | 34 +- tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + tools/testing/selftests/cgroup/config | 2 + .../selftests/cgroup/test_swap_tiers.c | 500 ++++++++++++++++++ tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/config | 2 + tools/testing/selftests/mm/run_vmtests.sh | 5 + tools/testing/selftests/mm/swap_tier.c | 323 +++++++++++ 23 files changed, 1762 insertions(+), 11 deletions(-) create mode 100644 Documentation/mm/swap-tier.rst create mode 100644 mm/swap_tier.c create mode 100644 mm/swap_tier.h create mode 100644 tools/testing/selftests/cgroup/test_swap_tiers.c create mode 100644 tools/testing/selftests/mm/swap_tier.c
base-commit: cdad4d4e4fc2e5acb9a8b2cac9af6ce87c92656f
Signature · 1 line
2.48.1
PATCH v9 1/6 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. Tier names must consist of alphanumeric characters and underscores. 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 or remove 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/Kconfig | 12 ++ mm/Makefile | 2 +- mm/swap.h | 4 + mm/swap_state.c | 74 ++++++++++++ mm/swap_tier.c | 302 ++++++++++++++++++++++++++++++++++++++++++++++++ mm/swap_tier.h | 20 ++++ mm/swapfile.c | 8 +- 8 files changed, 420 insertions(+), 4 deletions(-) create mode 100644 mm/swap_tier.c create mode 100644 mm/swap_tier.h
MAINTAINERS +2 -0
@@ -17060,6 +17060,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/Kconfig +12 -0
@@ -19,6 +19,18 @@ menuconfig SWAP used to provide more virtual memory than the actual RAM present in your computer. If unsure say Y. +config NR_SWAP_TIERS + int "Number of swap device tiers" + depends on SWAP + default 4 + range 1 31 + help + Sets the number of swap device tiers. Swap devices are + grouped into tiers based on their priority, allowing the + system to prefer faster devices over slower ones. + + If unsure, say 4. + config ZSWAP bool "Compressed cache for swap pages" depends on SWAP
mm/Makefile +1 -1
@@ -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
@@ -34,6 +34,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 +74 -0
88 lines not on this page · raw message
mm/swap_tier.c new file +302 -0
302 lines not on this page · raw message
mm/swap_tier.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _SWAP_TIER_H +#define _SWAP_TIER_H + +#include <linux/types.h> +#include <linux/spinlock.h> + +extern spinlock_t swap_tier_lock; + +/* 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); + +void swap_tiers_snapshot (void ); +void swap_tiers_snapshot_restore (void ); +bool swap_tiers_validate (void ); +#endif /* _SWAP_TIER_H */
mm/swapfile.c +5 -3
@@ -48,6 +48,7 @@#include "swap_table.h" #include "internal.h" #include "swap.h" +#include "swap_tier.h" static void swap_range_alloc (struct swap_info_struct *si,unsigned int nr_entries);
@@ -63,7 +64,8 @@ static void move_cluster(struct swap_info_struct *si,* * Also protects swap_active_head total_swap_pages, and the SWP_WRITEOK flag. */ -static DEFINE_SPINLOCK (swap_lock); +DEFINE_SPINLOCK (swap_lock); +static unsigned int nr_swapfiles;atomic_long_t nr_swap_pages;/*
@@ -74,7 +76,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 -1unsigned long swapfile_maximum_size;#ifdef CONFIG_MIGRATION bool swap_migration_ad_supported;
@@ -87,7 +88,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
@@ -3988,6 +3989,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.48.1
PATCH v9 2/6 mm: swap: associate swap devices with tiers
In reply to Youngjun ParkThis 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/index.rst | 1 + Documentation/mm/swap-tier.rst | 150 +++++++++++++++++++++++++++++++++ MAINTAINERS | 1 + include/linux/swap.h | 1 + mm/swap_state.c | 2 +- mm/swap_tier.c | 101 +++++++++++++++++++--- mm/swap_tier.h | 13 ++- mm/swapfile.c | 2 + 8 files changed, 257 insertions(+), 14 deletions(-) create mode 100644 Documentation/mm/swap-tier.rst
Documentation/mm/index.rst +1 -0
@@ -21,6 +21,7 @@ see the :doc:`admin guide <../admin-guide/mm/index>`. page_reclaim swap swap-table + swap-tier page_cache shmfs oom
Documentation/mm/swap-tier.rst new file +150 -0
150 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -17052,6 +17052,7 @@ L: [email protected] S: Maintained F: Documentation/ABI/testing/sysfs-kernel-mm-swap F: Documentation/mm/swap-table.rst +F: Documentation/mm/swap-tier.rst F: include/linux/swap.h F: include/linux/swapfile.h F: include/linux/swapops.h
include/linux/swap.h +1 -0
@@ -250,6 +250,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;/* size of this swap device */
mm/swap_state.c +1 -1
@@ -1063,7 +1063,7 @@ static ssize_t tiers_store(struct kobject *kobj, } } -if (!swap_tiers_validate ()) { +if (!swap_tiers_update ()) { ret = -EINVAL ; goto restore; }
mm/swap_tier.c +89 -12
165 lines not on this page · raw message
mm/swap_tier.h +12 -1
@@ -5,8 +5,15 @@#include <linux/types.h> #include <linux/spinlock.h> +/* Forward declarations */ +struct swap_info_struct ; +extern spinlock_t swap_tier_lock; +#define TIER_ALL_MASK (~0) +#define TIER_DEFAULT_IDX (31) +#define TIER_DEFAULT_MASK (1U << TIER_DEFAULT_IDX) +/* Initialization and application */ void swap_tiers_init (void );ssize_t swap_tiers_sysfs_show (char *buf);
@@ -16,5 +23,9 @@ int swap_tiers_remove(const char *name);void swap_tiers_snapshot (void );void swap_tiers_snapshot_restore (void ); -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
@@ -3036,6 +3036,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); }/*
Signature · 1 line
2.48.1
PATCH v9 3/6 mm: memcontrol: add interface for swap tier selection
In reply to Youngjun ParkIntroduce memory.swap.tiers.max, a flat-keyed file listing each tier defined in /sys/kernel/mm/swap/tiers with its state, "max" (allowed, the default) or "0" (disabled). A tier is one bit in the cgroup's tier mask, so writing "<tier> max" or "<tier> 0" sets or clears that bit.
Since the current use case lacks amount control, it only supports "max" (on) and "0" (off). Therefore, it does not track per-tier swap usage, relying instead on a fast runtime bitmask check.
We maintain both mask and effective_mask. The effective_mask is strictly bounded by the parent (e.g., if a parent is "0", the child's effective state is "0" even if its mask is "max"). Maintaining this separately avoids costly cgroup tree traversals to check ancestors at runtime.
--- Documentation/admin-guide/cgroup-v2.rst | 20 +++++ Documentation/mm/swap-tier.rst | 9 +++ include/linux/memcontrol.h | 5 ++ mm/memcontrol.c | 67 ++++++++++++++++ mm/swap_state.c | 5 +- mm/swap_tier.c | 102 +++++++++++++++++++++++- mm/swap_tier.h | 57 +++++++++++-- 7 files changed, 255 insertions(+), 10 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +20 -0
@@ -1850,6 +1850,26 @@ 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.max + A read-write flat-keyed file which exists on non-root + cgroups. The default is "max" for every tier. + + Limits the swap tiers this cgroup may swap to. Tiers are + defined globally in /sys/kernel/mm/swap/tiers and listed here, + one per line. When read, the values are displayed in descending + order of the tiers (highest tier first):: + + <tier_1> max + <tier_2> 0 + ... + + Currently, only "max" and "0" are supported. "max" allows the + tier, "0" disables it. Each write sets a single "<tier> max" + or "<tier> 0" pair. + + A child may only narrow what its parent allows. A tier an + ancestor disabled stays disabled regardless of the value here. + memory.swap.events A read-only flat-keyed file which exists on non-root cgroups. The following entries are defined. Unless specified
Documentation/mm/swap-tier.rst +9 -0
@@ -15,6 +15,15 @@ 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 --------------
include/linux/memcontrol.h +5 -0
@@ -283,6 +283,11 @@ struct mem_cgroup {struct lru_gen_mm_list mm_list;#endif +#ifdef CONFIG_SWAP +int tier_mask; +int tier_effective_mask; +#endif +#ifdef CONFIG_MEMCG_V1 /* Legacy consumer-oriented counters */ struct page_counter kmem;/* v1 only */
mm/memcontrol.c +67 -0
91 lines not on this page · raw message
mm/swap_state.c +3 -2
@@ -1021,6 +1021,7 @@ static ssize_t tiers_store(struct kobject *kobj,char *p, *token, *name, *tmp;int ret =0 ;short prio; +int mask =0 ; tmp =kstrdup (buf,GFP_KERNEL );if (!tmp)
@@ -1053,7 +1054,7 @@ static ssize_t tiers_store(struct kobject *kobj, goto restore;break ;case '-' : - ret =swap_tiers_remove (token +1 ); + ret =swap_tiers_remove (token +1 , &mask);if (ret) goto restore;break ;
@@ -1063,7 +1064,7 @@ static ssize_t tiers_store(struct kobject *kobj, } } -if (!swap_tiers_update ()) { +if (!swap_tiers_update (mask)) { ret = -EINVAL ; goto restore; }
mm/swap_tier.c +100 -2
125 lines not on this page · raw message
mm/swap_tier.h +51 -6
73 lines not on this page · raw message
Signature · 1 line
2.48.1
Youngjun Park
reviewerRe: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
https://sashiko.dev/#/patchset/[email protected]?part=3
Regarding the Sashiko review comment, I don't think this needs to be fixed. It is acceptable for the effective_mask to be stale on a zombie memcg.
The zombie memcg LRU is already reparented by the following patchset.
https://lore.kernel.org/all/[email protected]/
the effective_mask is only needed on the swap path. Since swap I/O rarely (if ever) happens on a zombie memcg, leaving the stale mask should not cause issues.
(As I mentioned above, there is no need to fix as I think. But somehow if we needed to fix this, there would be two alternatives.
1. Fix it at offline time by setting it to SWAP_TIER_ALL_MASK. However, this approach would break the parent tier relationship.
2. Change the memcg iteration routine to use css_for_each_descendant_pre to explicitly handle the zombie memcg case. This approach would safely preserve the parent tier relationship.
)
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
In reply to Youngjun ParkQuoted 35 lines · Youngjun Park
Introduce memory.swap.tiers.max, a flat-keyed file listing each tier defined in
/sys/kernel/mm/swap/tierswith its state, "max" (allowed, the default) or "0" (disabled). A tier is one bit in the cgroup's tier mask, so writing "<tier> max" or "<tier> 0" sets or clears that bit.Since the current use case lacks amount control, it only supports "max" (on) and "0" (off). Therefore, it does not track per-tier swap usage, relying instead on a fast runtime bitmask check.
We maintain both
maskandeffective_mask. Theeffective_maskis strictly bounded by the parent (e.g., if a parent is "0", the child's effective state is "0" even if itsmaskis "max"). Maintaining this separately avoids costly cgroup tree traversals to check ancestors at runtime.Suggested-by Shakeel Butt <[email protected]>Suggested-by Yosry Ahmed <[email protected]>Signed-off-by Youngjun Park <[email protected]>--- Documentation/admin-guide/cgroup-v2.rst | 20 +++++ Documentation/mm/swap-tier.rst | 9 +++ include/linux/memcontrol.h | 5 ++ mm/memcontrol.c | 67 ++++++++++++++++ mm/swap_state.c | 5 +- mm/swap_tier.c | 102 +++++++++++++++++++++++- mm/swap_tier.h | 57 +++++++++++-- 7 files changed, 255 insertions(+), 10 deletions(-)Documentation/admin-guide/cgroup-v2.rst +0
-0
Documentation/admin-guide/cgroup-v2.rst The following nested keys are defined. · line 1855
Quoted 6 lines
Documentation/admin-guide/cgroup-v2.rst +3
-0@@ -1850,6 +1850,26 @@ 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.max + A read-write flat-keyed file which exists on non-root + cgroups. The default is "max" for every tier.
I wonder what should the default behavior be if memory.swap.max is set to a value other than "max". Should the limits in memory.swap.tiers.max auto-scale or remain as "max"? We probably want to keep the behavior consistent with memory tiering.
Shakeel/Joshua, WDYT?
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
Quoted 39 lines · On Mon, 22 Jun 2026 14:21:30 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 38 lines · Youngjun Park
Introduce memory.swap.tiers.max, a flat-keyed file listing each tier defined in
/sys/kernel/mm/swap/tierswith its state, "max" (allowed, the default) or "0" (disabled). A tier is one bit in the cgroup's tier mask, so writing "<tier> max" or "<tier> 0" sets or clears that bit.Since the current use case lacks amount control, it only supports "max" (on) and "0" (off). Therefore, it does not track per-tier swap usage, relying instead on a fast runtime bitmask check.
We maintain both
maskandeffective_mask. Theeffective_maskis strictly bounded by the parent (e.g., if a parent is "0", the child's effective state is "0" even if itsmaskis "max"). Maintaining this separately avoids costly cgroup tree traversals to check ancestors at runtime.Suggested-by Shakeel Butt <[email protected]>Suggested-by Yosry Ahmed <[email protected]>Signed-off-by Youngjun Park <[email protected]>--- Documentation/admin-guide/cgroup-v2.rst | 20 +++++ Documentation/mm/swap-tier.rst | 9 +++ include/linux/memcontrol.h | 5 ++ mm/memcontrol.c | 67 ++++++++++++++++ mm/swap_state.c | 5 +- mm/swap_tier.c | 102 +++++++++++++++++++++++- mm/swap_tier.h | 57 +++++++++++-- 7 files changed, 255 insertions(+), 10 deletions(-)Documentation/admin-guide/cgroup-v2.rst +0
-0@@ -1850,6 +1850,26 @@ 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.
Quoted 3 lines
Documentation/admin-guide/cgroup-v2.rst The following nested keys are defined. · line 1855
Quoted 3 lines
Documentation/admin-guide/cgroup-v2.rst +3
-0+ memory.swap.tiers.max + A read-write flat-keyed file which exists on non-root + cgroups. The default is "max" for every tier.
Hi Yosry,
Sorry, I feel like I'm joining the party late. Apologies if I'm missing some context or repeating a discussion that's already been had. Please let me know if that is the case.
One quick tangent:
I was chatting with Nhat last week about swap tiers and its relation to memory tiering. Nhat brought up a good point, which is that while both swap tiers and memory tiers provide a clear hierarchy of performance, only memory tiering allows for movement between the tiers. AFAICT, swap tiering does not allow for direct migration from a higher tier swap backend to a lower tier swap backend if the higher tier backend runs out of memory.
In that sense, I'm not entirely sure if we need to enforce similar semantics across swap tiering and memory tiering; it seems like there are some fundamental differences anyways to how we treat these tiers.
Quoted 6 lines
I wonder what should the default behavior be if memory.swap.max is set to a value other than "max". Should the limits in memory.swap.tiers.max auto-scale or remain as "max"? We probably want to keep the behavior consistent with memory tiering.
Shakeel/Joshua, WDYT?
I think that the motivation behind these tiers is different for swap and memory. Tiered memory limits is motivated by preventing one workload from conusming all of a valuable resource, while swap tiers seems more to do with excluding certain workloads from using performant tiers and ensuring other workloads stay on those performant tiers.
IOW memory tiers exist for fairness, but it seems like swap tiers exist for workload performance tiering. But maybe there's a usecase out there that would want fairness to apply in the swap tiers as well that I am not seeing.
If that is the case, I think auto-scaling makes sense but can be a bit tricky, since there is no universal tiered ratio; each workload will have different tiers it can swap to, so they will all have to calculate their own ratios. Tiered memory limits escapes this difficulty since we assume all memory can be placed on all tiers, so we have a system-wide ratio : -)
Let me know what you think! Have a great day :D
Joshua
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
Quoted 78 lines · Joshua Hahn
Quoted 39 lines · On Mon, 22 Jun 2026 14:21:30 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 38 lines · Youngjun Park
Introduce memory.swap.tiers.max, a flat-keyed file listing each tier defined in
/sys/kernel/mm/swap/tierswith its state, "max" (allowed, the default) or "0" (disabled). A tier is one bit in the cgroup's tier mask, so writing "<tier> max" or "<tier> 0" sets or clears that bit.Since the current use case lacks amount control, it only supports "max" (on) and "0" (off). Therefore, it does not track per-tier swap usage, relying instead on a fast runtime bitmask check.
We maintain both
maskandeffective_mask. Theeffective_maskis strictly bounded by the parent (e.g., if a parent is "0", the child's effective state is "0" even if itsmaskis "max"). Maintaining this separately avoids costly cgroup tree traversals to check ancestors at runtime.Suggested-by Shakeel Butt <[email protected]>Suggested-by Yosry Ahmed <[email protected]>Signed-off-by Youngjun Park <[email protected]>--- Documentation/admin-guide/cgroup-v2.rst | 20 +++++ Documentation/mm/swap-tier.rst | 9 +++ include/linux/memcontrol.h | 5 ++ mm/memcontrol.c | 67 ++++++++++++++++ mm/swap_state.c | 5 +- mm/swap_tier.c | 102 +++++++++++++++++++++++- mm/swap_tier.h | 57 +++++++++++-- 7 files changed, 255 insertions(+), 10 deletions(-)Documentation/admin-guide/cgroup-v2.rst +0
-0@@ -1850,6 +1850,26 @@ 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.Quoted 3 lines
Documentation/admin-guide/cgroup-v2.rst The following nested keys are defined. · line 1855
Quoted 3 lines
Documentation/admin-guide/cgroup-v2.rst +3
-0+ memory.swap.tiers.max + A read-write flat-keyed file which exists on non-root + cgroups. The default is "max" for every tier.Hi Yosry,
Sorry, I feel like I'm joining the party late. Apologies if I'm missing some context or repeating a discussion that's already been had. Please let me know if that is the case.
One quick tangent:
I was chatting with Nhat last week about swap tiers and its relation to memory tiering. Nhat brought up a good point, which is that while both swap tiers and memory tiers provide a clear hierarchy of performance, only memory tiering allows for movement between the tiers. AFAICT, swap tiering does not allow for direct migration from a higher tier swap backend to a lower tier swap backend if the higher tier backend runs out of memory.In that sense, I'm not entirely sure if we need to enforce similar semantics across swap tiering and memory tiering; it seems like there are some fundamental differences anyways to how we treat these tiers.
Quoted 6 lines
I wonder what should the default behavior be if memory.swap.max is set to a value other than "max". Should the limits in memory.swap.tiers.max auto-scale or remain as "max"? We probably want to keep the behavior consistent with memory tiering.
Shakeel/Joshua, WDYT?
I think that the motivation behind these tiers is different for swap and memory. Tiered memory limits is motivated by preventing one workload from conusming all of a valuable resource, while swap tiers seems more to do with excluding certain workloads from using performant tiers and ensuring other workloads stay on those performant tiers.
Quoted 4 lines
IOW memory tiers exist for fairness, but it seems like swap tiers exist for workload performance tiering. But maybe there's a usecase out there that would want fairness to apply in the swap tiers as well that I am not seeing.
I am not sure what use cases exist, but I think it's possible we end up wanting to enforce fairness for swap tiers as well. Maybe not as aggressively as memory (e.g. to avoid wearing out SSDs), but maybe at least proactively through userspace?
At the end of the day, faster swap tiers are also valuable resources that we probably don't want a few workloads to hog. I also think the interfaces being consistent makes everyone's lives easier, even if it's a bit of an overkill for swap tiers.
Quoted 7 lines
If that is the case, I think auto-scaling makes sense but can be a bit tricky, since there is no universal tiered ratio; each workload will have different tiers it can swap to, so they will all have to calculate their own ratios. Tiered memory limits escapes this difficulty since we assume all memory can be placed on all tiers, so we have a system-wide ratio : -)
Hmm I don't follow. It's also possible (maybe not initially) that a memcg cannot use specific memory tiers, right? I am not sure what the difference is.
Quoted 3 lines
Let me know what you think! Have a great day :D
Joshua
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
Quoted 89 lines · On Mon, 22 Jun 2026 15:26:17 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 78 lines · Joshua Hahn
Quoted 39 lines · On Mon, 22 Jun 2026 14:21:30 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 38 lines · Youngjun Park
Introduce memory.swap.tiers.max, a flat-keyed file listing each tier defined in
/sys/kernel/mm/swap/tierswith its state, "max" (allowed, the default) or "0" (disabled). A tier is one bit in the cgroup's tier mask, so writing "<tier> max" or "<tier> 0" sets or clears that bit.Since the current use case lacks amount control, it only supports "max" (on) and "0" (off). Therefore, it does not track per-tier swap usage, relying instead on a fast runtime bitmask check.
We maintain both
maskandeffective_mask. Theeffective_maskis strictly bounded by the parent (e.g., if a parent is "0", the child's effective state is "0" even if itsmaskis "max"). Maintaining this separately avoids costly cgroup tree traversals to check ancestors at runtime.Suggested-by Shakeel Butt <[email protected]>Suggested-by Yosry Ahmed <[email protected]>Signed-off-by Youngjun Park <[email protected]>--- Documentation/admin-guide/cgroup-v2.rst | 20 +++++ Documentation/mm/swap-tier.rst | 9 +++ include/linux/memcontrol.h | 5 ++ mm/memcontrol.c | 67 ++++++++++++++++ mm/swap_state.c | 5 +- mm/swap_tier.c | 102 +++++++++++++++++++++++- mm/swap_tier.h | 57 +++++++++++-- 7 files changed, 255 insertions(+), 10 deletions(-)Documentation/admin-guide/cgroup-v2.rst +0
-0@@ -1850,6 +1850,26 @@ 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.Quoted 3 lines
Documentation/admin-guide/cgroup-v2.rst The following nested keys are defined. · line 1855
Quoted 3 lines
Documentation/admin-guide/cgroup-v2.rst +3
-0+ memory.swap.tiers.max + A read-write flat-keyed file which exists on non-root + cgroups. The default is "max" for every tier.Hi Yosry,
Sorry, I feel like I'm joining the party late. Apologies if I'm missing some context or repeating a discussion that's already been had. Please let me know if that is the case.
One quick tangent:
I was chatting with Nhat last week about swap tiers and its relation to memory tiering. Nhat brought up a good point, which is that while both swap tiers and memory tiers provide a clear hierarchy of performance, only memory tiering allows for movement between the tiers. AFAICT, swap tiering does not allow for direct migration from a higher tier swap backend to a lower tier swap backend if the higher tier backend runs out of memory.In that sense, I'm not entirely sure if we need to enforce similar semantics across swap tiering and memory tiering; it seems like there are some fundamental differences anyways to how we treat these tiers.
Quoted 6 lines
I wonder what should the default behavior be if memory.swap.max is set to a value other than "max". Should the limits in memory.swap.tiers.max auto-scale or remain as "max"? We probably want to keep the behavior consistent with memory tiering.
Shakeel/Joshua, WDYT?
I think that the motivation behind these tiers is different for swap and memory. Tiered memory limits is motivated by preventing one workload from conusming all of a valuable resource, while swap tiers seems more to do with excluding certain workloads from using performant tiers and ensuring other workloads stay on those performant tiers.
Quoted 4 lines
IOW memory tiers exist for fairness, but it seems like swap tiers exist for workload performance tiering. But maybe there's a usecase out there that would want fairness to apply in the swap tiers as well that I am not seeing.
I am not sure what use cases exist, but I think it's possible we end up wanting to enforce fairness for swap tiers as well. Maybe not as aggressively as memory (e.g. to avoid wearing out SSDs), but maybe at least proactively through userspace?
Quoted 4 lines
At the end of the day, faster swap tiers are also valuable resources that we probably don't want a few workloads to hog. I also think the interfaces being consistent makes everyone's lives easier, even if it's a bit of an overkill for swap tiers.
I see, thank you for the explanation. That makes sense to me.
Quoted 10 lines
Quoted 6 lines
If that is the case, I think auto-scaling makes sense but can be a bit tricky, since there is no universal tiered ratio; each workload will have different tiers it can swap to, so they will all have to calculate their own ratios. Tiered memory limits escapes this difficulty since we assume all memory can be placed on all tiers, so we have a system-wide ratio : -)
Hmm I don't follow. It's also possible (maybe not initially) that a memcg cannot use specific memory tiers, right? I am not sure what the difference is.
You're right, I was speaking more to the current state of memory tiers. The majority of the feedack I received was that we already have too many memcg knobs, so I just opted to make tiered memcg limits a cgroup mount, with no ability for individual memcgs to tune their limits or opt-in/out.
What do you think Yosry? Would it make sense for us to be able to tune these values? Personally I think it makes sense but just wanted to make the basic features merged before I went to push for making those knobs tunable.
If we want to make the tuning the same across swap & memory we should probably align on the file names and how we interact with them.
Thanks,
Joshua
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
If that is the case, I think auto-scaling makes sense but can be a bit tricky, since there is no universal tiered ratio; each workload will have different tiers it can swap to, so they will all have to calculate their own ratios. Tiered memory limits escapes this difficulty since we assume all memory can be placed on all tiers, so we have a system-wide ratio : -)
Hmm I don't follow. It's also possible (maybe not initially) that a memcg cannot use specific memory tiers, right? I am not sure what the difference is.
Quoted 5 lines
You're right, I was speaking more to the current state of memory tiers. The majority of the feedack I received was that we already have too many memcg knobs, so I just opted to make tiered memcg limits a cgroup mount, with no ability for individual memcgs to tune their limits or opt-in/out.
Right, I think this is similar to the approach taken here. We have a single interface for per-tier limits. The main difference is that we're allowing 0/max values to disable/enable different swap tiers per-memcg, as there's a use case for that.
Seems like for memory tiering there's no use case for that yet.
Quoted 4 lines
What do you think Yosry? Would it make sense for us to be able to tune these values? Personally I think it makes sense but just wanted to make the basic features merged before I went to push for making those knobs tunable.
Right now we're not proposing to allow tuning swap tier limits either, just enable or disable a tier. My main question is about the default values.
IIUC, for memory tiering, if you set memory.max, then the limits for tiers are auto-scaled. I think it makes sense to do the same for swap tiers for cosnsitency. Or am I wrong about the memory tiering limits behavior?
Quoted 2 lines
If we want to make the tuning the same across swap & memory we should probably align on the file names and how we interact with them.
Yeah I think we should make the interfaces as consistent as possible, within reason.
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
Quoted 17 lines · On Mon, 22 Jun 2026 23:46:31 +0000 Yosry Ahmed <[email protected]> wrote:
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
If that is the case, I think auto-scaling makes sense but can be a bit tricky, since there is no universal tiered ratio; each workload will have different tiers it can swap to, so they will all have to calculate their own ratios. Tiered memory limits escapes this difficulty since we assume all memory can be placed on all tiers, so we have a system-wide ratio : -)
Hmm I don't follow. It's also possible (maybe not initially) that a memcg cannot use specific memory tiers, right? I am not sure what the difference is.
Quoted 5 lines
You're right, I was speaking more to the current state of memory tiers. The majority of the feedack I received was that we already have too many memcg knobs, so I just opted to make tiered memcg limits a cgroup mount, with no ability for individual memcgs to tune their limits or opt-in/out.
Quoted 6 lines
Right, I think this is similar to the approach taken here. We have a single interface for per-tier limits. The main difference is that we're allowing 0/max values to disable/enable different swap tiers per-memcg, as there's a use case for that.
Seems like for memory tiering there's no use case for that yet.
Yes, I would agree with that.
Quoted 5 lines
Quoted 4 lines
What do you think Yosry? Would it make sense for us to be able to tune these values? Personally I think it makes sense but just wanted to make the basic features merged before I went to push for making those knobs tunable.
Quoted 8 lines
Right now we're not proposing to allow tuning swap tier limits either, just enable or disable a tier. My main question is about the default values.
IIUC, for memory tiering, if you set memory.max, then the limits for tiers are auto-scaled. I think it makes sense to do the same for swap tiers for cosnsitency. Or am I wrong about the memory tiering limits behavior?
No, you're right about that. Sorry for steering the thread to my series ; -)
To get back to the question of how the auto-tuning should work, the main question is to which ratio we scale the swap limits to. Do we set the swap limits proportional to how much swap is present in the system, or how much swap is available to the cgroup?
So if we have 3 swap tiers A, B, C, with 50G, 30G, and 20G capacity respectively, how much should a cgroup with swap.max = 10G have if it is limited to tiers A and B?
This is what I was getting at earlier when I said we have to calculate different ratios for different cgroups, based on what tiers they have access to.
Sorry if that doesn't make sense, please let me know how I can elaborate!
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
Quoted 51 lines · Joshua Hahn
Quoted 17 lines · On Mon, 22 Jun 2026 23:46:31 +0000 Yosry Ahmed <[email protected]> wrote:
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
If that is the case, I think auto-scaling makes sense but can be a bit tricky, since there is no universal tiered ratio; each workload will have different tiers it can swap to, so they will all have to calculate their own ratios. Tiered memory limits escapes this difficulty since we assume all memory can be placed on all tiers, so we have a system-wide ratio : -)
Hmm I don't follow. It's also possible (maybe not initially) that a memcg cannot use specific memory tiers, right? I am not sure what the difference is.
Quoted 5 lines
You're right, I was speaking more to the current state of memory tiers. The majority of the feedack I received was that we already have too many memcg knobs, so I just opted to make tiered memcg limits a cgroup mount, with no ability for individual memcgs to tune their limits or opt-in/out.
Quoted 6 lines
Right, I think this is similar to the approach taken here. We have a single interface for per-tier limits. The main difference is that we're allowing 0/max values to disable/enable different swap tiers per-memcg, as there's a use case for that.
Seems like for memory tiering there's no use case for that yet.
Yes, I would agree with that.
Quoted 5 lines
Quoted 4 lines
What do you think Yosry? Would it make sense for us to be able to tune these values? Personally I think it makes sense but just wanted to make the basic features merged before I went to push for making those knobs tunable.
Quoted 8 lines
Right now we're not proposing to allow tuning swap tier limits either, just enable or disable a tier. My main question is about the default values.
IIUC, for memory tiering, if you set memory.max, then the limits for tiers are auto-scaled. I think it makes sense to do the same for swap tiers for cosnsitency. Or am I wrong about the memory tiering limits behavior?
No, you're right about that. Sorry for steering the thread to my series ; -)
To get back to the question of how the auto-tuning should work, the main question is to which ratio we scale the swap limits to. Do we set the swap limits proportional to how much swap is present in the system, or how much swap is available to the cgroup?
Quoted 7 lines
So if we have 3 swap tiers A, B, C, with 50G, 30G, and 20G capacity respectively, how much should a cgroup with swap.max = 10G have if it is limited to tiers A and B?
This is what I was getting at earlier when I said we have to calculate different ratios for different cgroups, based on what tiers they have access to.
That's a good question. I think the case that is particularly interesting is whether or not the limits of other tiers should change when another tier is disabled/enabled.
So basically in your example, assuming everything starts as "max", when swap.max is set to 10G, the autoscaled limits would be: (tier A, 5G), (tier B, 3G), (tier C, 2G). Now the question becomes, if userspace sets the limit of tier C to 0, should the limits for tiers A and B change?
On one hand, it's simpler to just keep the autoscaled limits unchanged in this case. However, this means that the effective swap limit is now 8G, which is not great :/
The alternative is to recalculate all the limits when one of them changes, in which case the limits of A and B would change to 6.25G and 3.75G. But I don't know if this will work well if we allow custom limits. What happens if the limit of tier C is written as 1 (or 4096) instead of 0? It's effectively the same scenario, but the tier is technically allowed.
The more I think about it, the more I realize it may be best to drop the autoscaling thing. I imagine memory tiering might run into similar issues too :/
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
Quoted 27 lines · On Tue, 23 Jun 2026 11:10:32 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 5 lines
To get back to the question of how the auto-tuning should work, the main question is to which ratio we scale the swap limits to. Do we set the swap limits proportional to how much swap is present in the system, or how much swap is available to the cgroup?
Quoted 7 lines
So if we have 3 swap tiers A, B, C, with 50G, 30G, and 20G capacity respectively, how much should a cgroup with swap.max = 10G have if it is limited to tiers A and B?
This is what I was getting at earlier when I said we have to calculate different ratios for different cgroups, based on what tiers they have access to.
That's a good question. I think the case that is particularly interesting is whether or not the limits of other tiers should change when another tier is disabled/enabled.
So basically in your example, assuming everything starts as "max", when swap.max is set to 10G, the autoscaled limits would be: (tier A, 5G), (tier B, 3G), (tier C, 2G). Now the question becomes, if userspace sets the limit of tier C to 0, should the limits for tiers A and B change?
On one hand, it's simpler to just keep the autoscaled limits unchanged in this case. However, this means that the effective swap limit is now 8G, which is not great :/
Quoted 6 lines
The alternative is to recalculate all the limits when one of them changes, in which case the limits of A and B would change to 6.25G and 3.75G. But I don't know if this will work well if we allow custom limits. What happens if the limit of tier C is written as 1 (or 4096) instead of 0? It's effectively the same scenario, but the tier is technically allowed.
I think the one problem with this is that it becomes quite easy to accidentally overcommit. As a toy example, if you have 10 workloads and 100G swap (as in the example I gave above), intuitively setting swap.max = 10G for all 10 workloads shouldn't ever cause any contention on capacity. But if you start excluding some tiers from some workloads, you actually get overcommitting on the tiers that can service the most workloads.
I am not sure how concerning swap overcommit was, but at least in the memory tiering scenario accidental overcommitting of toptier memory seemed bad enough that I wanted to avoid the problem entirely.
Quoted 3 lines
The more I think about it, the more I realize it may be best to drop the autoscaling thing. I imagine memory tiering might run into similar issues too :/
And that's why I didn't include opt-in/opt-out for any of the tiers; if you have system-wide ratios, there's no need to change the ratios at all, and as long as the sum of your memory.limit for each workload is under the total capacity, all tiers will also not be overcommitted.
Now, all of these complications aside, I think we might be overthinking a bit here : -) The auto-scaling should just provide some sort of "reasonable" default, the users can always override the per-tier limits if they are unhappy with the autoscaled values.
In fact, maybe it even makes sense to have sum of swap tier limits > swap.max.
(I actually recall having a really similar discussion when I was working on weighted interleave auto-tuning a year ago, on how weights should be set when switching between manually-set limits and relying on auto-scaled defaults [1]. I don't think there's a need to follow this convention, but we should think about what the expected behavior should be if a user manually sets a limit, but later wants to go back to auto-scaling limits).
Anyways, I think these are important questions. Youngjun, Nhat, Shakeel, any thoughts from you all? : -)
[1] https://lore.kernel.org/all/8734hbiq7j.fsf@DESKTOP-5N7EMDA/
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
Quoted 49 lines · Joshua Hahn
Quoted 27 lines · On Tue, 23 Jun 2026 11:10:32 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 5 lines
To get back to the question of how the auto-tuning should work, the main question is to which ratio we scale the swap limits to. Do we set the swap limits proportional to how much swap is present in the system, or how much swap is available to the cgroup?
Quoted 7 lines
So if we have 3 swap tiers A, B, C, with 50G, 30G, and 20G capacity respectively, how much should a cgroup with swap.max = 10G have if it is limited to tiers A and B?
This is what I was getting at earlier when I said we have to calculate different ratios for different cgroups, based on what tiers they have access to.
That's a good question. I think the case that is particularly interesting is whether or not the limits of other tiers should change when another tier is disabled/enabled.
So basically in your example, assuming everything starts as "max", when swap.max is set to 10G, the autoscaled limits would be: (tier A, 5G), (tier B, 3G), (tier C, 2G). Now the question becomes, if userspace sets the limit of tier C to 0, should the limits for tiers A and B change?
On one hand, it's simpler to just keep the autoscaled limits unchanged in this case. However, this means that the effective swap limit is now 8G, which is not great :/
Quoted 6 lines
The alternative is to recalculate all the limits when one of them changes, in which case the limits of A and B would change to 6.25G and 3.75G. But I don't know if this will work well if we allow custom limits. What happens if the limit of tier C is written as 1 (or 4096) instead of 0? It's effectively the same scenario, but the tier is technically allowed.
I think the one problem with this is that it becomes quite easy to accidentally overcommit. As a toy example, if you have 10 workloads and 100G swap (as in the example I gave above), intuitively setting swap.max = 10G for all 10 workloads shouldn't ever cause any contention on capacity. But if you start excluding some tiers from some workloads, you actually get overcommitting on the tiers that can service the most workloads.
I am not sure how concerning swap overcommit was, but at least in the memory tiering scenario accidental overcommitting of toptier memory seemed bad enough that I wanted to avoid the problem entirely.
Quoted 8 lines
Quoted 3 lines
The more I think about it, the more I realize it may be best to drop the autoscaling thing. I imagine memory tiering might run into similar issues too :/
And that's why I didn't include opt-in/opt-out for any of the tiers; if you have system-wide ratios, there's no need to change the ratios at all, and as long as the sum of your memory.limit for each workload is under the total capacity, all tiers will also not be overcommitted.
I think eventually there may be use cases to opt some memcgs out for some memory tiers. For example, limit sensitive workloads to the top tier (or vice versa).
Quoted 5 lines
Now, all of these complications aside, I think we might be overthinking a bit here : -) The auto-scaling should just provide some sort of "reasonable" default, the users can always override the per-tier limits if they are unhappy with the autoscaled values.
I agree, but it seems like both options are not ideal here. I think it might make more sense to not present a default value at all, have "max" be the default for all the tiers, even if memory.max or swap.max isn't. Userspace can set the limits if they need to. Autoscaling the limits in userspace should be easy.
Quoted 16 lines
In fact, maybe it even makes sense to have sum of swap tier limits > swap.max.
(I actually recall having a really similar discussion when I was working on weighted interleave auto-tuning a year ago, on how weights should be set when switching between manually-set limits and relying on auto-scaled defaults [1]. I don't think there's a need to follow this convention, but we should think about what the expected behavior should be if a user manually sets a limit, but later wants to go back to auto-scaling limits).
Anyways, I think these are important questions. Youngjun, Nhat, Shakeel, any thoughts from you all? : -)
[1] https://lore.kernel.org/all/8734hbiq7j.fsf@DESKTOP-5N7EMDA/
Re: [PATCH v9 3/6] mm: memcontrol: add interface for swap tier selection
Quoted 54 lines · On Tue, 23 Jun 2026 13:06:10 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 53 lines · Joshua Hahn
Quoted 27 lines · On Tue, 23 Jun 2026 11:10:32 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 5 lines
To get back to the question of how the auto-tuning should work, the main question is to which ratio we scale the swap limits to. Do we set the swap limits proportional to how much swap is present in the system, or how much swap is available to the cgroup?
Quoted 7 lines
So if we have 3 swap tiers A, B, C, with 50G, 30G, and 20G capacity respectively, how much should a cgroup with swap.max = 10G have if it is limited to tiers A and B?
This is what I was getting at earlier when I said we have to calculate different ratios for different cgroups, based on what tiers they have access to.
That's a good question. I think the case that is particularly interesting is whether or not the limits of other tiers should change when another tier is disabled/enabled.
So basically in your example, assuming everything starts as "max", when swap.max is set to 10G, the autoscaled limits would be: (tier A, 5G), (tier B, 3G), (tier C, 2G). Now the question becomes, if userspace sets the limit of tier C to 0, should the limits for tiers A and B change?
On one hand, it's simpler to just keep the autoscaled limits unchanged in this case. However, this means that the effective swap limit is now 8G, which is not great :/
Quoted 6 lines
The alternative is to recalculate all the limits when one of them changes, in which case the limits of A and B would change to 6.25G and 3.75G. But I don't know if this will work well if we allow custom limits. What happens if the limit of tier C is written as 1 (or 4096) instead of 0? It's effectively the same scenario, but the tier is technically allowed.
I think the one problem with this is that it becomes quite easy to accidentally overcommit. As a toy example, if you have 10 workloads and 100G swap (as in the example I gave above), intuitively setting swap.max = 10G for all 10 workloads shouldn't ever cause any contention on capacity. But if you start excluding some tiers from some workloads, you actually get overcommitting on the tiers that can service the most workloads.
I am not sure how concerning swap overcommit was, but at least in the memory tiering scenario accidental overcommitting of toptier memory seemed bad enough that I wanted to avoid the problem entirely.
Quoted 3 lines
The more I think about it, the more I realize it may be best to drop the autoscaling thing. I imagine memory tiering might run into similar issues too :/
Quoted 8 lines
Quoted 4 lines
And that's why I didn't include opt-in/opt-out for any of the tiers; if you have system-wide ratios, there's no need to change the ratios at all, and as long as the sum of your memory.limit for each workload is under the total capacity, all tiers will also not be overcommitted.
I think eventually there may be use cases to opt some memcgs out for some memory tiers. For example, limit sensitive workloads to the top tier (or vice versa).
Yup, that makes sense to me too.
One of the things that did concern me a bit with my model for tiered memcg limit was that system-critical processes would also be susceptible to being demoted and churned, when we would much rather make sure those are kept protected at the toptier.
Quoted 10 lines
Quoted 4 lines
Now, all of these complications aside, I think we might be overthinking a bit here : -) The auto-scaling should just provide some sort of "reasonable" default, the users can always override the per-tier limits if they are unhappy with the autoscaled values.
I agree, but it seems like both options are not ideal here. I think it might make more sense to not present a default value at all, have "max" be the default for all the tiers, even if memory.max or swap.max isn't. Userspace can set the limits if they need to. Autoscaling the limits in userspace should be easy.
I like this idea a lot. That would basically make swap tiers a no-op unless you opt-into setting the limits yourself, so we don't run the risk of accidentally enabling tiers.
On that note, maybe it makes sense for me to change my memory tiering series to also just not present a default setting for tiered limits, and instead just set them as max until the user comes and configures them?
I think this is a better question for the memcg maintainers, who might have more to say on this. Johannes, Michal, Roman, and Shakeel, what do you guys think? Could an approach to just make the memory tier limits writable from the get-go and not expose any defaults make sense to you?
I think that would simplify the code quite a bit and also help mitigate the possible side effects on system-critical workloads.
Thanks!
Joshua
PATCH v9 4/6 mm: swap: filter swap allocation by memcg tier mask
In reply to Youngjun ParkApply memcg tier effective mask during swap slot allocation to enforce per-cgroup swap tier restrictions.
The folio's effective mask is computed once and passed to the fast, slow and discard paths as a parameter, so all of them act on the same mask even if the memcg's mask changes concurrently.
In the fast path, check the percpu cached swap_info's tier_mask against the folio's effective mask. If it does not match, fall through to the slow path. In the slow path, skip swap devices whose tier_mask is not covered by the folio's effective mask. The discard fallback honors the mask too: otherwise it would drain the discard clusters of a device outside the folio's tiers and then loop back to allocate from a tier the memcg is not allowed to use.
This works correctly when there is only one non-rotational device in the system and no devices share the same priority. However, there are known limitations:
- When non-rotational devices are distributed across multiple tiers, and different memcgs are configured to use those distinct tiers, they may constantly overwrite the shared percpu swap cache. This cache thrashing leads to frequent fast path misses.
- Combined with the above issue, if same-priority devices exist among them, a percpu cache miss (overwritten by another memcg) forces the allocator to round-robin to the next device prematurely, even if the current cluster is not fully exhausted.
These edge cases do not affect the primary use case of directing swap traffic per cgroup. Further optimization is planned for future work.
--- mm/swapfile.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
mm/swapfile.c +17 -7
@@ -1359,7 +1359,7 @@ static bool get_swap_device_info(struct swap_info_struct *si)* Fast path try to get swap entries with specified order from current * CPU's swap entry pool (a cluster). */ -static bool swap_alloc_fast (struct folio *folio) +static bool swap_alloc_fast (struct folio *folio,int mask) {unsigned int order =folio_order (folio);struct swap_cluster_info *ci;
@@ -1371,8 +1371,11 @@ static bool swap_alloc_fast(struct folio *folio)* so checking it's liveness by get_swap_device_info is enough. */ si =this_cpu_read (percpu_swap_cluster.si[order]); +if (!si || !swap_tiers_mask_test (si->tier_mask, mask)) +return false; + offset =this_cpu_read (percpu_swap_cluster.offset[order]); -if (!si || !offset || !get_swap_device_info (si)) +if (!offset || !get_swap_device_info (si))return false; ci =swap_cluster_lock (si, offset);
@@ -1389,13 +1392,16 @@ static bool swap_alloc_fast(struct folio *folio) }/* Rotate the device and switch to a new cluster */ -static void swap_alloc_slow (struct folio *folio) +static void swap_alloc_slow (struct folio *folio,int mask) {struct swap_info_struct *si, *next;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);
@@ -1429,7 +1435,7 @@ static void swap_alloc_slow(struct folio *folio)* Discard pending clusters in a synchronized way when under high pressure. * Return: true if any cluster is discarded. */ -static bool swap_sync_discard (void ) +static bool swap_sync_discard (int mask) {bool ret = false;struct swap_info_struct *si, *next;
@@ -1437,6 +1443,8 @@ static bool swap_sync_discard(void)spin_lock (&swap_lock); start_over:plist_for_each_entry_safe (si, next, &swap_active_head, list) { +if (!swap_tiers_mask_test (si->tier_mask, mask)) +continue ;spin_unlock (&swap_lock);if (get_swap_device_info (si)) {if (si->flags &SWP_PAGE_DISCARD )
@@ -1736,6 +1744,7 @@ int folio_alloc_swap(struct folio *folio) {unsigned int order =folio_order (folio);unsigned int size =1 << order; +int mask;VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio);VM_BUG_ON_FOLIO (!folio_test_uptodate (folio), folio);
@@ -1759,13 +1768,14 @@ int folio_alloc_swap(struct folio *folio) } again: + mask =folio_tier_effective_mask (folio);local_lock (&percpu_swap_cluster.lock); -if (!swap_alloc_fast (folio)) -swap_alloc_slow (folio); +if (!swap_alloc_fast (folio, mask)) +swap_alloc_slow (folio, mask);local_unlock (&percpu_swap_cluster.lock);if (!order &&unlikely (!folio_test_swapcache (folio))) { -if (swap_sync_discard ()) +if (swap_sync_discard (mask)) goto again; }
Signature · 1 line
2.48.1
PATCH v9 5/6 selftests/mm: add a swap tier configuration test
In reply to Youngjun ParkThis commit adds a test program for the global swap tier interface at /sys/kernel/mm/swap/tiers. It checks the add, split and remove operations and the documented error and batch atomicity rules. It also checks that a tier with an active swap device cannot be removed until the device is swapped off. That device is a zram device, and the check is skipped when zram is not available.
--- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/config | 2 + tools/testing/selftests/mm/run_vmtests.sh | 5 + tools/testing/selftests/mm/swap_tier.c | 323 ++++++++++++++++++++++ 5 files changed, 332 insertions(+) create mode 100644 tools/testing/selftests/mm/swap_tier.c
tools/testing/selftests/mm/.gitignore +1 -0
@@ -46,6 +46,7 @@ hmm-tests memfd_secret soft-dirty split_huge_page_test +swap_tier ksm_tests local_config.h local_config.mk
tools/testing/selftests/mm/Makefile +1 -0
@@ -105,6 +105,7 @@ TEST_GEN_FILES += guard-regions TEST_GEN_FILES += merge TEST_GEN_FILES += rmap TEST_GEN_FILES += folio_split_race_test +TEST_GEN_FILES += swap_tier ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty
tools/testing/selftests/mm/config +2 -0
@@ -14,3 +14,5 @@ CONFIG_UPROBES=y CONFIG_MEMORY_FAILURE=y CONFIG_HWPOISON_INJECT=m CONFIG_PROC_MEM_ALWAYS_FORCE=y +CONFIG_SWAP=y +CONFIG_ZRAM=y
tools/testing/selftests/mm/run_vmtests.sh +5 -0
@@ -71,6 +71,8 @@ separated by spaces: tests for VM_PFNMAP handling - process_madv test for process_madv +- swap_tier + test the /sys/kernel/mm/swap/tiers configuration interface - cow test copy-on-write semantics - thp
@@ -353,6 +355,9 @@ CATEGORY="process_madv" run_test ./process_madv CATEGORY="vma_merge" run_test ./merge +# swap tier configuration interface (/sys/kernel/mm/swap/tiers) +CATEGORY="swap_tier" run_test ./swap_tier + if [ -x ./memfd_secret ] then if [ -f /proc/sys/kernel/yama/ptrace_scope ]; then
tools/testing/selftests/mm/swap_tier.c new file +323 -0
323 lines not on this page · raw message
Signature · 1 line
2.48.1
PATCH v9 6/6 selftests/cgroup: add a swap tier routing test
In reply to Youngjun ParkThis commit adds a test program for the per-cgroup swap tier control memory.swap.tiers.max. It checks the default mask, toggling a tier, rejection of invalid input, and that recreating a tier resets the mask. It also checks that a cgroup's pages swap only to an allowed tier, including across the parent and child hierarchy. The routing check uses two zram devices placed in different tiers.
--- tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + tools/testing/selftests/cgroup/config | 2 + .../selftests/cgroup/test_swap_tiers.c | 500 ++++++++++++++++++ 4 files changed, 505 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_swap_tiers.c
tools/testing/selftests/cgroup/.gitignore +1 -0
@@ -8,5 +8,6 @@ test_kill test_kmem test_memcontrol test_pids +test_swap_tiers test_zswap wait_inotify
tools/testing/selftests/cgroup/Makefile +2 -0
@@ -16,6 +16,7 @@ TEST_GEN_PROGS += test_kill TEST_GEN_PROGS += test_kmem TEST_GEN_PROGS += test_memcontrol TEST_GEN_PROGS += test_pids +TEST_GEN_PROGS += test_swap_tiers TEST_GEN_PROGS += test_zswap LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
@@ -32,4 +33,5 @@ $(OUTPUT)/test_kill: $(LIBCGROUP_O) $(OUTPUT)/test_kmem: $(LIBCGROUP_O) $(OUTPUT)/test_memcontrol: $(LIBCGROUP_O) $(OUTPUT)/test_pids: $(LIBCGROUP_O) +$(OUTPUT)/test_swap_tiers: $(LIBCGROUP_O) $(OUTPUT)/test_zswap: $(LIBCGROUP_O)
tools/testing/selftests/cgroup/config +2 -0
@@ -4,3 +4,5 @@ CONFIG_CGROUP_FREEZER=y CONFIG_CGROUP_SCHED=y CONFIG_MEMCG=y CONFIG_PAGE_COUNTER=y +CONFIG_SWAP=y +CONFIG_ZRAM=y
tools/testing/selftests/cgroup/test_swap_tiers.c new file +500 -0
500 lines not on this page · raw message
Signature · 1 line
2.48.1
Quoted 21 lines · Youngjun Park
This is the v9 series of the swap tier patchset.
The main change in this version is the addition of selftests for the tier interfaces, requested by Nhat; see the changelog below for the other changes. I designed the test cases and wrote the selftests with some AI assistance.
For context, the bulk of the series is unchanged since v8, with great thanks to Shakeel Butt and Yosry for the reviews and discussions [1] that shaped it. The main change in v8 was the interface change to use memory.swap.tiers.max with '0' (disable) and 'max' (enable) values. This mechanism was suggested by Shakeel and Yosry.
This change allows for future extensions to control swap between tiers and aligns better with existing memcg interfaces. It is confined to patch #3's user-facing interface; internally, patch #3 still uses the existing mask processing method, which is implementation-efficient.
We also discussed tier extensions. Thanks to Yosry, Nhat and Shakeel for their valuable feedback.
Quoted 7 lines
Here is a brief summary of our tentative conclusions. Please correct me if anything is misrepresented (details in references):
* Zswap tiering [2]:
Tiering applies only to the vswap + zswap combo. Zswap itself will not be tiered, as the current architecture requires a physical device for zswap allocation.
I thought we agreed that zswap should be a tier, so that proactive zswap writeback can be implemented as proactive swap demotion?
The only restriction we talked about is that zswap cannot be the only allowed tier as long as vswap isn't supported. We can lift the restriction when vswap support is added.
Youngjun Park
reviewerQuoted 30 lines · Yosry Ahmed
Quoted 21 lines · Youngjun Park
This is the v9 series of the swap tier patchset.
The main change in this version is the addition of selftests for the tier interfaces, requested by Nhat; see the changelog below for the other changes. I designed the test cases and wrote the selftests with some AI assistance.
For context, the bulk of the series is unchanged since v8, with great thanks to Shakeel Butt and Yosry for the reviews and discussions [1] that shaped it. The main change in v8 was the interface change to use memory.swap.tiers.max with '0' (disable) and 'max' (enable) values. This mechanism was suggested by Shakeel and Yosry.
This change allows for future extensions to control swap between tiers and aligns better with existing memcg interfaces. It is confined to patch #3's user-facing interface; internally, patch #3 still uses the existing mask processing method, which is implementation-efficient.
We also discussed tier extensions. Thanks to Yosry, Nhat and Shakeel for their valuable feedback.
Quoted 7 lines
Here is a brief summary of our tentative conclusions. Please correct me if anything is misrepresented (details in references):
* Zswap tiering [2]:
Tiering applies only to the vswap + zswap combo. Zswap itself will not be tiered, as the current architecture requires a physical device for zswap allocation.
Quoted 6 lines
I thought we agreed that zswap should be a tier, so that proactive zswap writeback can be implemented as proactive swap demotion?
The only restriction we talked about is that zswap cannot be the only allowed tier as long as vswap isn't supported. We can lift the restriction when vswap support is added.
Okay, I misunderstood that part. Thanks for the clarification.
To summarize our agreement.
zswap can be the first tier regardless of vswap support.
- With vswap: zswap can be the only allowed tier, as it can operate independently.
- Without vswap: zswap cannot be the only allowed tier, as it cannot operate without a physical backing device.
I will proceed with this understanding.
Youngjun Park
Youngjun Park
reviewerHi Shakeel,
Gentle ping on this review when you have a chance.
I submitted the patch using the memcg interface approach you suggested.
Thanks,
Youngjun