From nobody Thu Apr 2 15:36:28 2026 Received: from lgeamrelo03.lge.com (lgeamrelo03.lge.com [156.147.51.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52A2940B6E6 for ; Wed, 25 Mar 2026 17:55:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=156.147.51.102 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774461307; cv=none; b=tXMDKU7KqUKLeQCk/PueV0Nj1MvmEf+HmjM5ph3yFP1CbQp2+Tv4HpLCqG3O9ZyWZE2KDQiDau+hzmw+fJYMPkw/9cDWyWpfJ/J4HeJePtqe3kaTPoRrSxOYP0cf0/U1PWJtzojTja7xgL8fjhmZrF9MCIMPbVGU700fPaiM+4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774461307; c=relaxed/simple; bh=YtMoix/vqzVtjDXttB/xs+lLcH+ZY/0S4LfH1vfwDLU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dXAIxFH/5iG2+RMOuRv7xYCb7So7wg6TX4zPIWFkRVbAarPB6gNmBNMAKAc4V/lvwauWdHS+cmY8s6x9wX57g+NVmsvD9ACjhbLi8j1xg2OTf16/eqY2VlorGLMd7Y3mwDy9sAwYBdBWZltb+hjKvylD6RL0hq57CI+AqqaRyK0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com; spf=pass smtp.mailfrom=lge.com; arc=none smtp.client-ip=156.147.51.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lge.com Received: from unknown (HELO yjaykim-PowerEdge-T330.lge.net) (10.177.112.156) by 156.147.51.102 with ESMTP; 26 Mar 2026 02:54:56 +0900 X-Original-SENDERIP: 10.177.112.156 X-Original-MAILFROM: youngjun.park@lge.com From: Youngjun Park To: Andrew Morton Cc: Chris Li , Youngjun Park , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kasong@tencent.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, shikemeng@huaweicloud.com, nphamcs@gmail.com, bhe@redhat.com, baohua@kernel.org, gunho.lee@lge.com, taejoon.song@lge.com, hyungjun.cho@lge.com, mkoutny@suse.com Subject: [PATCH v5 1/4] mm: swap: introduce swap tier infrastructure Date: Thu, 26 Mar 2026 02:54:50 +0900 Message-Id: <20260325175453.2523280-2-youngjun.park@lge.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260325175453.2523280-1-youngjun.park@lge.com> References: <20260325175453.2523280-1-youngjun.park@lge.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Suggested-by: Chris Li Signed-off-by: Youngjun Park --- MAINTAINERS | 2 + mm/Kconfig | 12 ++ mm/Makefile | 2 +- mm/swap.h | 4 + mm/swap_state.c | 74 +++++++++++++ mm/swap_tier.c | 285 ++++++++++++++++++++++++++++++++++++++++++++++++ mm/swap_tier.h | 20 ++++ mm/swapfile.c | 8 +- 8 files changed, 403 insertions(+), 4 deletions(-) create mode 100644 mm/swap_tier.c create mode 100644 mm/swap_tier.h diff --git a/MAINTAINERS b/MAINTAINERS index 76431aa5efbe..f3b07f1fa38a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16916,6 +16916,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 =20 MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE) diff --git a/mm/Kconfig b/mm/Kconfig index bd283958d675..b645e9430af5 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -19,6 +19,18 @@ menuconfig SWAP used to provide more virtual memory than the actual RAM present in your computer. If unsure say Y. =20 +config NR_SWAP_TIERS + int "Number of swap device tiers" + depends on SWAP + default 4 + range 1 32 + 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 diff --git a/mm/Makefile b/mm/Makefile index 8ad2ab08244e..db6449f84991 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -75,7 +75,7 @@ ifdef CONFIG_MMU obj-$(CONFIG_ADVISE_SYSCALLS) +=3D madvise.o endif =20 -obj-$(CONFIG_SWAP) +=3D page_io.o swap_state.o swapfile.o +obj-$(CONFIG_SWAP) +=3D page_io.o swap_state.o swapfile.o swap_tier.o obj-$(CONFIG_ZSWAP) +=3D zswap.o obj-$(CONFIG_HAS_DMA) +=3D dmapool.o obj-$(CONFIG_HUGETLBFS) +=3D hugetlb.o hugetlb_sysfs.o hugetlb_sysctl.o diff --git a/mm/swap.h b/mm/swap.h index a77016f2423b..fda8363bee73 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -16,6 +16,10 @@ extern int page_cluster; #define swap_entry_order(order) 0 #endif =20 +#define DEF_SWAP_PRIO -1 + +extern spinlock_t swap_lock; +extern struct plist_head swap_active_head; extern struct swap_info_struct *swap_info[]; =20 /* diff --git a/mm/swap_state.c b/mm/swap_state.c index 1415a5c54a43..bfdc0208e081 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -25,6 +25,7 @@ #include "internal.h" #include "swap_table.h" #include "swap.h" +#include "swap_tier.h" =20 /* * swapper_space is a fiction, retained to simplify the path through @@ -924,8 +925,81 @@ static ssize_t vma_ra_enabled_store(struct kobject *ko= bj, } static struct kobj_attribute vma_ra_enabled_attr =3D __ATTR_RW(vma_ra_enab= led); =20 +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 =3D 0; + short prio; + + tmp =3D kstrdup(buf, GFP_KERNEL); + if (!tmp) + return -ENOMEM; + + spin_lock(&swap_lock); + spin_lock(&swap_tier_lock); + swap_tiers_snapshot(); + + p =3D tmp; + while ((token =3D strsep(&p, ", \t\n")) !=3D NULL) { + if (!*token) + continue; + + switch (token[0]) { + case '+': + name =3D token + 1; + token =3D strchr(name, ':'); + if (!token) { + ret =3D -EINVAL; + goto out; + } + *token++ =3D '\0'; + if (kstrtos16(token, 10, &prio)) { + ret =3D -EINVAL; + goto out; + } + ret =3D swap_tiers_add(name, prio); + if (ret) + goto restore; + break; + case '-': + ret =3D swap_tiers_remove(token + 1); + if (ret) + goto restore; + break; + default: + ret =3D -EINVAL; + goto out; + } + } + + if (!swap_tiers_validate()) { + ret =3D -EINVAL; + goto restore; + } + goto out; + +restore: + swap_tiers_snapshot_restore(); +out: + spin_unlock(&swap_tier_lock); + spin_unlock(&swap_lock); + kfree(tmp); + return ret ? ret : count; +} + +static struct kobj_attribute tier_attr =3D __ATTR_RW(tiers); + static struct attribute *swap_attrs[] =3D { &vma_ra_enabled_attr.attr, + &tier_attr.attr, NULL, }; =20 diff --git a/mm/swap_tier.c b/mm/swap_tier.c new file mode 100644 index 000000000000..62b60fa8d3b7 --- /dev/null +++ b/mm/swap_tier.c @@ -0,0 +1,285 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include "memcontrol-v1.h" +#include +#include + +#include "swap.h" +#include "swap_tier.h" + +#define MAX_SWAPTIER CONFIG_NR_SWAP_TIERS +#define MAX_TIERNAME 16 + +/* + * struct swap_tier - structure representing a swap tier. + * + * @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]; + +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_INACTIVE_PRIO (DEF_SWAP_PRIO - 1) +#define TIER_IS_ACTIVE(tier) ((tier->prio) !=3D TIER_INACTIVE_PRIO) +#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 =3D 0, tier =3D &swap_tiers[0]; idx < MAX_SWAPTIER; \ + idx++, tier =3D &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; +} + +/* Insert new tier into the active list sorted by priority. */ +static void swap_tier_activate(struct swap_tier *new) +{ + struct swap_tier *tier; + + for_each_active_tier(tier) { + if (tier->prio <=3D new->prio) + break; + } + + list_add_tail(&new->list, &tier->list); +} + +static void swap_tier_inactivate(struct swap_tier *tier) +{ + list_move(&tier->list, &swap_tier_inactive_list); + tier->prio =3D TIER_INACTIVE_PRIO; +} + +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); + swap_tier_inactivate(tier); + } +} + +ssize_t swap_tiers_sysfs_show(char *buf) +{ + struct swap_tier *tier; + ssize_t len =3D 0; + + len +=3D 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 +=3D sysfs_emit_at(buf, len, "%-16s %-5ld %-11d %-11d\n", + tier->name, + TIER_IDX(tier), + tier->prio, + TIER_END_PRIO(tier)); + } + spin_unlock(&swap_tier_lock); + + return len; +} + +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 ERR_PTR(-EINVAL); + + if (list_empty(&swap_tier_inactive_list)) + return ERR_PTR(-ENOSPC); + + tier =3D list_first_entry(&swap_tier_inactive_list, + struct swap_tier, list); + + list_del_init(&tier->list); + strscpy(tier->name, name, MAX_TIERNAME); + tier->prio =3D 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); + + for_each_active_tier(tier) { + /* No overwrite */ + if (tier->prio =3D=3D prio) + return -EINVAL; + } + + return 0; +} + +static bool swap_tier_validate_name(const char *name) +{ + if (!name || !*name) + return false; + + while (*name) { + if (!isalnum(*name) && *name !=3D '_') + return false; + name++; + } + return true; +} + +int swap_tiers_add(const char *name, int prio) +{ + 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 -EEXIST; + + if (!swap_tier_validate_name(name)) + return -EINVAL; + + ret =3D swap_tier_check_range(prio); + if (ret) + return ret; + + tier =3D swap_tier_prepare(name, prio); + if (IS_ERR(tier)) { + ret =3D PTR_ERR(tier); + return ret; + } + + swap_tier_activate(tier); + + return ret; +} + +int swap_tiers_remove(const char *name) +{ + int ret =3D 0; + struct swap_tier *tier; + + lockdep_assert_held(&swap_lock); + lockdep_assert_held(&swap_tier_lock); + + tier =3D swap_tier_lookup(name); + if (!tier) + return -EINVAL; + + /* Removing DEF_SWAP_PRIO merges into the higher tier. */ + if (!list_is_singular(&swap_tier_active_list) + && tier->prio =3D=3D DEF_SWAP_PRIO) + list_prev_entry(tier, list)->prio =3D DEF_SWAP_PRIO; + + swap_tier_inactivate(tier); + + return ret; +} + +static struct swap_tier swap_tiers_snap[MAX_SWAPTIER]; +/* + * XXX: When multiple operations (adds and removes) are submitted in a + * single write, reverting each individually on failure is complex and + * error-prone. Instead, snapshot the entire state beforehand and + * restore it wholesale if any operation fails. + */ +void swap_tiers_snapshot(void) +{ + BUILD_BUG_ON(sizeof(swap_tiers_snap) !=3D sizeof(swap_tiers)); + + lockdep_assert_held(&swap_lock); + lockdep_assert_held(&swap_tier_lock); + + memcpy(swap_tiers_snap, swap_tiers, sizeof(swap_tiers)); +} + +void swap_tiers_snapshot_restore(void) +{ + struct swap_tier *tier; + int idx; + + lockdep_assert_held(&swap_lock); + lockdep_assert_held(&swap_tier_lock); + + memcpy(swap_tiers, swap_tiers_snap, sizeof(swap_tiers)); + + INIT_LIST_HEAD(&swap_tier_active_list); + INIT_LIST_HEAD(&swap_tier_inactive_list); + + for_each_tier(tier, idx) { + if (TIER_IS_ACTIVE(tier)) + swap_tier_activate(tier); + else + swap_tier_inactivate(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). + */ + if (swap_tier_is_active()) { + tier =3D list_last_entry(&swap_tier_active_list, + struct swap_tier, list); + + if (tier->prio !=3D DEF_SWAP_PRIO) + return false; + } + + return true; +} diff --git a/mm/swap_tier.h b/mm/swap_tier.h new file mode 100644 index 000000000000..a1395ec02c24 --- /dev/null +++ b/mm/swap_tier.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _SWAP_TIER_H +#define _SWAP_TIER_H + +#include +#include + +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 */ diff --git a/mm/swapfile.c b/mm/swapfile.c index ff315b752afd..03bf2a0a42ac 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -49,6 +49,7 @@ #include "swap_table.h" #include "internal.h" #include "swap.h" +#include "swap_tier.h" =20 static void swap_range_alloc(struct swap_info_struct *si, unsigned int nr_entries); @@ -64,7 +65,8 @@ static void move_cluster(struct swap_info_struct *si, * * Also protects swap_active_head total_swap_pages, and the SWP_WRITEOK fl= ag. */ -static DEFINE_SPINLOCK(swap_lock); +DEFINE_SPINLOCK(swap_lock); + static unsigned int nr_swapfiles; atomic_long_t nr_swap_pages; /* @@ -75,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; @@ -88,7 +89,7 @@ static const char Bad_offset[] =3D "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); =20 /* * all available (active, not full) swap_info_structs @@ -3890,6 +3891,7 @@ static int __init swapfile_init(void) swap_migration_ad_supported =3D true; #endif /* CONFIG_MIGRATION */ =20 + swap_tiers_init(); return 0; } subsys_initcall(swapfile_init); --=20 2.34.1 From nobody Thu Apr 2 15:36:28 2026 Received: from lgeamrelo03.lge.com (lgeamrelo03.lge.com [156.147.51.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C1FC40B6E0 for ; Wed, 25 Mar 2026 17:55:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=156.147.51.102 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774461310; cv=none; b=JwHQ0FOw12Xo+ljb9czxavnBML59f5imgjiYE1B5FOocMtg4HvvJTG5rAJG8aE5GPNBPetx46a+K4C85XVETJdhb7Xye7JLhnhEuYwPTpL7uQtVPBNjcbrygBEy7DB+Zaqucw0IwtytBq3ZiMKcZ3npmL4jAjcrjfKwu7qbzobY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774461310; c=relaxed/simple; bh=WLgT96/04aFN1RznkIejz9PlZfNuOom7Ts7lPn/UNf8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=mkh/+hWyBCdevuHsV5yiteKBV2f+37QtoA7/5PnjTWV9lg9I5i01YirVRpLz8rboNLZiKPdVxDKnGum9YXTDInQDmQ5ZYfWHK6pLhKIGpfsqzMGAGxpa1IaS4lsHq5SFvTldRg3s2Q2EyAiJLEHXHFOC8AcxA7U2BLdwXZ5fJbs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com; spf=pass smtp.mailfrom=lge.com; arc=none smtp.client-ip=156.147.51.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lge.com Received: from unknown (HELO yjaykim-PowerEdge-T330.lge.net) (10.177.112.156) by 156.147.51.102 with ESMTP; 26 Mar 2026 02:54:58 +0900 X-Original-SENDERIP: 10.177.112.156 X-Original-MAILFROM: youngjun.park@lge.com From: Youngjun Park To: Andrew Morton Cc: Chris Li , Youngjun Park , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kasong@tencent.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, shikemeng@huaweicloud.com, nphamcs@gmail.com, bhe@redhat.com, baohua@kernel.org, gunho.lee@lge.com, taejoon.song@lge.com, hyungjun.cho@lge.com, mkoutny@suse.com Subject: [PATCH v5 2/4] mm: swap: associate swap devices with tiers Date: Thu, 26 Mar 2026 02:54:51 +0900 Message-Id: <20260325175453.2523280-3-youngjun.park@lge.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260325175453.2523280-1-youngjun.park@lge.com> References: <20260325175453.2523280-1-youngjun.park@lge.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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. Signed-off-by: Youngjun Park --- Documentation/mm/swap-tier.rst | 159 +++++++++++++++++++++++++++++++++ MAINTAINERS | 1 + include/linux/swap.h | 1 + mm/swap_state.c | 2 +- mm/swap_tier.c | 101 ++++++++++++++++++--- mm/swap_tier.h | 12 ++- mm/swapfile.c | 2 + 7 files changed, 264 insertions(+), 14 deletions(-) create mode 100644 Documentation/mm/swap-tier.rst diff --git a/Documentation/mm/swap-tier.rst b/Documentation/mm/swap-tier.rst new file mode 100644 index 000000000000..7b29b0e4e414 --- /dev/null +++ b/Documentation/mm/swap-tier.rst @@ -0,0 +1,159 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Chris Li Youngjun Park + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Swap Tier +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Swap tier is a collection of user-named groups classified by priority rang= es. +It acts as a facilitation layer, allowing users to manage swap devices bas= ed +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 acco= rding +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. Whe= n a +swap device is activated within a tier range, the tier covering that devic= e's +priority is guaranteed not to disappear or change while the device remains +active. Adding a new tier may split the range of an existing tier, but the +active device's tier assignment remains unchanged. + +However, specifying a tier in a cgroup does not guarantee the tier's exist= ence. +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: ``+"":""`` +* Remove: ``-""`` + +Tier names must consist of alphanumeric characters and underscores. Multip= le +operations can be provided in a single write, separated by commas (",") or +whitespace (spaces, tabs, newlines). + +When configuring tiers, the specified value represents the **start priorit= y** +of that tier. The end priority is automatically determined by the start +priority of the next higher tier. Consequently, adding a tier +automatically adjusts 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 remain= ing +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. Adding a New Tier (split)** + +A new tier 'SSD' is added at priority 100, splitting the existing 'HDD' ti= er. +The ranges are automatically recalculated: + +* 'SSD' takes the top range (100 to SHRT_MAX). +* 'HDD' is adjusted to the range between 'NET' and 'SSD' (50 to 99). +* 'NET' remains unchanged (-1 to 49). + +:: + + # echo "+SSD:100" > /sys/kernel/mm/swap/tiers + # cat /sys/kernel/mm/swap/tiers + Name Idx PrioStart PrioEnd + SSD 2 100 32767 + HDD 0 50 99 + NET 1 -1 49 + +**3. Removal (merge)** + +Tiers can be removed using the '-' prefix. +:: + + # echo "-SSD" > /sys/kernel/mm/swap/tiers + +When a tier is removed, its priority range is merged into the adjacent +tier. The merge direction is always upward (the tier below expands), +except when the lowest tier is removed =E2=80=94 in that case the tier abo= ve +shifts its starting priority down to -1 to maintain full range coverage. + +:: + + Initial state: + Name Idx PrioStart PrioEnd + SSD 2 100 32767 + HDD 1 50 99 + NET 0 -1 49 + + # echo "-SSD" > /sys/kernel/mm/swap/tiers + + Name Idx PrioStart PrioEnd + HDD 1 50 32767 <- merged with SSD's ra= nge + NET 0 -1 49 + + # echo "-NET" > /sys/kernel/mm/swap/tiers + + Name Idx PrioStart PrioEnd + HDD 1 -1 32767 <- shifted down to -1 + +**4. Interaction with Active Swap Devices** + +If a swap device is active (swapon), the tier covering that device's +priority cannot be removed. Splitting the active tier's range is only +allowed above the device's priority. + +Assume a swap device is active at priority 60 (inside 'HDD' tier). + +:: + + # swapon -p 60 /dev/zram0 + + Name Idx PrioStart PrioEnd + HDD 0 50 32767 + NET 1 -1 49 + + # echo "-HDD" > /sys/kernel/mm/swap/tiers + -bash: echo: write error: Device or resource busy + + # echo "+SSD:60" > /sys/kernel/mm/swap/tiers + -bash: echo: write error: Device or resource busy + + # echo "+SSD:100" > /sys/kernel/mm/swap/tiers + + Name Idx PrioStart PrioEnd + SSD 2 100 32767 + HDD 0 50 99 <- device (prio 60) sta= ys here + NET 1 -1 49 diff --git a/MAINTAINERS b/MAINTAINERS index f3b07f1fa38a..62a177983799 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16908,6 +16908,7 @@ R: Youngjun Park L: linux-mm@kvack.org S: Maintained 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 diff --git a/include/linux/swap.h b/include/linux/swap.h index 1930f81e6be4..3bc06a1a4a17 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -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 */ diff --git a/mm/swap_state.c b/mm/swap_state.c index bfdc0208e081..847096e2f3e5 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -980,7 +980,7 @@ static ssize_t tiers_store(struct kobject *kobj, } } =20 - if (!swap_tiers_validate()) { + if (!swap_tiers_update()) { ret =3D -EINVAL; goto restore; } diff --git a/mm/swap_tier.c b/mm/swap_tier.c index 62b60fa8d3b7..91aac55d3a8b 100644 --- a/mm/swap_tier.c +++ b/mm/swap_tier.c @@ -38,6 +38,8 @@ static LIST_HEAD(swap_tier_inactive_list); (!list_is_first(&(tier)->list, &swap_tier_active_list) ? \ list_prev_entry((tier), list)->prio - 1 : SHRT_MAX) =20 +#define MASK_TO_TIER(mask) (&swap_tiers[__ffs((mask))]) + #define for_each_tier(tier, idx) \ for (idx =3D 0, tier =3D &swap_tiers[0]; idx < MAX_SWAPTIER; \ idx++, tier =3D &swap_tiers[idx]) @@ -59,6 +61,26 @@ static bool swap_tier_is_active(void) return !list_empty(&swap_tier_active_list) ? true : false; } =20 +static bool swap_tier_prio_in_range(struct swap_tier *tier, short prio) +{ + if (tier->prio <=3D prio && TIER_END_PRIO(tier) >=3D prio) + return true; + + return false; +} + +static bool swap_tier_prio_is_used(short prio) +{ + struct swap_tier *tier; + + for_each_active_tier(tier) { + if (tier->prio =3D=3D prio) + return true; + } + + return false; +} + static struct swap_tier *swap_tier_lookup(const char *name) { struct swap_tier *tier; @@ -96,6 +118,7 @@ void swap_tiers_init(void) int idx; =20 BUILD_BUG_ON(BITS_PER_TYPE(int) < MAX_SWAPTIER); + BUILD_BUG_ON(MAX_SWAPTIER > TIER_DEFAULT_IDX); =20 for_each_tier(tier, idx) { INIT_LIST_HEAD(&tier->list); @@ -146,17 +169,29 @@ static struct swap_tier *swap_tier_prepare(const char= *name, short prio) return tier; } =20 -static int swap_tier_check_range(short prio) +static int swap_tier_can_split_range(short new_prio) { + struct swap_info_struct *p; struct swap_tier *tier; =20 lockdep_assert_held(&swap_lock); lockdep_assert_held(&swap_tier_lock); =20 - for_each_active_tier(tier) { - /* No overwrite */ - if (tier->prio =3D=3D prio) - return -EINVAL; + plist_for_each_entry(p, &swap_active_head, list) { + if (p->tier_mask =3D=3D TIER_DEFAULT_MASK) + continue; + + tier =3D MASK_TO_TIER(p->tier_mask); + if (!swap_tier_prio_in_range(tier, new_prio)) + continue; + + /* + * Device sits in a tier that spans new_prio; + * splitting here would reassign it to a + * different tier. + */ + if (p->prio >=3D new_prio) + return -EBUSY; } =20 return 0; @@ -190,7 +225,11 @@ int swap_tiers_add(const char *name, int prio) if (!swap_tier_validate_name(name)) return -EINVAL; =20 - ret =3D swap_tier_check_range(prio); + /* No overwrite */ + if (swap_tier_prio_is_used(prio)) + return -EBUSY; + + ret =3D swap_tier_can_split_range(prio); if (ret) return ret; =20 @@ -217,6 +256,11 @@ int swap_tiers_remove(const char *name) if (!tier) return -EINVAL; =20 + /* Simulate adding a tier to check for conflicts */ + ret =3D swap_tier_can_split_range(tier->prio); + if (ret) + return ret; + /* Removing DEF_SWAP_PRIO merges into the higher tier. */ if (!list_is_singular(&swap_tier_active_list) && tier->prio =3D=3D DEF_SWAP_PRIO) @@ -227,13 +271,15 @@ int swap_tiers_remove(const char *name) return ret; } =20 -static struct swap_tier swap_tiers_snap[MAX_SWAPTIER]; /* - * XXX: When multiple operations (adds and removes) are submitted in a - * single write, reverting each individually on failure is complex and - * error-prone. Instead, snapshot the entire state beforehand and - * restore it wholesale if any operation fails. + * XXX: Static global snapshot buffer for batch operations. Small + * and used once per write, so a static global is not bad. + * When multiple adds/removes are submitted in a single write, + * reverting each individually on failure is error-prone. Instead, + * snapshot beforehand and restore wholesale if any operation fails. */ +static struct swap_tier swap_tiers_snap[MAX_SWAPTIER]; + void swap_tiers_snapshot(void) { BUILD_BUG_ON(sizeof(swap_tiers_snap) !=3D sizeof(swap_tiers)); @@ -265,9 +311,29 @@ void swap_tiers_snapshot_restore(void) } } =20 -bool swap_tiers_validate(void) +void swap_tiers_assign_dev(struct swap_info_struct *swp) +{ + struct swap_tier *tier; + + lockdep_assert_held(&swap_lock); + + for_each_active_tier(tier) { + if (swap_tier_prio_in_range(tier, swp->prio)) { + swp->tier_mask =3D TIER_MASK(tier); + return; + } + } + + swp->tier_mask =3D TIER_DEFAULT_MASK; +} + +bool swap_tiers_update(void) { struct swap_tier *tier; + struct swap_info_struct *swp; + + lockdep_assert_held(&swap_lock); + lockdep_assert_held(&swap_tier_lock); =20 /* * Initial setting might not cover DEF_SWAP_PRIO. @@ -281,5 +347,16 @@ bool swap_tiers_validate(void) return false; } =20 + /* + * If applied initially, the swap tier_mask may change + * from the default value. + */ + plist_for_each_entry(swp, &swap_active_head, list) { + /* Tier is already configured */ + if (swp->tier_mask !=3D TIER_DEFAULT_MASK) + break; + swap_tiers_assign_dev(swp); + } + return true; } diff --git a/mm/swap_tier.h b/mm/swap_tier.h index a1395ec02c24..6f281e95ed81 100644 --- a/mm/swap_tier.h +++ b/mm/swap_tier.h @@ -5,8 +5,15 @@ #include #include =20 +/* Forward declarations */ +struct swap_info_struct; + extern spinlock_t swap_tier_lock; =20 +#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); @@ -16,5 +23,8 @@ int swap_tiers_remove(const char *name); =20 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 */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 03bf2a0a42ac..645e10c3af28 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2914,6 +2914,8 @@ static void _enable_swap_info(struct swap_info_struct= *si) =20 /* Add back to available list */ add_to_avail_list(si, true); + + swap_tiers_assign_dev(si); } =20 /* --=20 2.34.1 From nobody Thu Apr 2 15:36:28 2026 Received: from lgeamrelo03.lge.com (lgeamrelo03.lge.com [156.147.51.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDD163FB072 for ; Wed, 25 Mar 2026 17:55:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=156.147.51.102 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774461311; cv=none; b=UR8Xgmj3iCnOlCfIhnu7E6R6YplV7QW3ufiI/v5sLcYCuVYqS/vgkAdooFlRL7vAkSvJJoors8rRME21Dfy1OhSo7nJFQNv3wpCdxlknzAsp/elRbWsi7lB+tcxaIM+Hlnj2h2p0A/5V/E+ZPMkUmX81weCRVbdHo2nS0Q/zScU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774461311; c=relaxed/simple; bh=iKiBB8/3ib88iWIauxXoaKiDnn+Eg7L0aIUL+2QL1Hw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RsUTxSxFS5Tkn6vrRUXVdgIqV/ISu9oMF+xn/4eMgla7yCdeUetBata8Y/2E1Z3JMBO09Ovijv6gl2jIec6p8KeiRF4G6AIGXJzyxf7BfOMWzRFAPtdPcsVPvmiAv/otFwoN7Nekxku3a66So0jhb353IhHr1pVQqM5uZP6zfJM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com; spf=pass smtp.mailfrom=lge.com; arc=none smtp.client-ip=156.147.51.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lge.com Received: from unknown (HELO yjaykim-PowerEdge-T330.lge.net) (10.177.112.156) by 156.147.51.102 with ESMTP; 26 Mar 2026 02:55:00 +0900 X-Original-SENDERIP: 10.177.112.156 X-Original-MAILFROM: youngjun.park@lge.com From: Youngjun Park To: Andrew Morton Cc: Chris Li , Youngjun Park , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kasong@tencent.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, shikemeng@huaweicloud.com, nphamcs@gmail.com, bhe@redhat.com, baohua@kernel.org, gunho.lee@lge.com, taejoon.song@lge.com, hyungjun.cho@lge.com, mkoutny@suse.com Subject: [PATCH v5 3/4] mm: memcontrol: add interfaces for swap tier selection Date: Thu, 26 Mar 2026 02:54:52 +0900 Message-Id: <20260325175453.2523280-4-youngjun.park@lge.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260325175453.2523280-1-youngjun.park@lge.com> References: <20260325175453.2523280-1-youngjun.park@lge.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Integrate swap tier infrastructure with cgroup to allow selecting specific swap devices per cgroup. Introduce `memory.swap.tiers` for configuring allowed tiers, and `memory.swap.tiers.effective` for exposing the effective tiers. The effective tiers are the intersection of the configured tiers and the parent's effective tiers. Note that cgroups do not pin swap tiers, similar to `cpuset` and CPU hotplug, allowing configuration changes regardless of usage. Signed-off-by: Youngjun Park --- Documentation/admin-guide/cgroup-v2.rst | 27 +++++++ include/linux/memcontrol.h | 3 +- mm/memcontrol.c | 95 +++++++++++++++++++++++++ mm/swap_state.c | 5 +- mm/swap_tier.c | 93 +++++++++++++++++++++++- mm/swap_tier.h | 56 +++++++++++++-- 6 files changed, 268 insertions(+), 11 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-= guide/cgroup-v2.rst index 8ad0b2781317..6effe1bfe74d 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1850,6 +1850,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. =20 + memory.swap.tiers + A read-write file which exists on non-root cgroups. + Format is similar to cgroup.subtree_control. + + Controls which swap tiers this cgroup is allowed to swap + out to. All tiers are enabled by default. + + (-|+)TIER [(-|+)TIER ...] + + "-" disables a tier, "+" re-enables it. + Entries are whitespace-delimited. + + Changes here are combined with parent restrictions to + compute memory.swap.tiers.effective. + + If a tier is removed from /sys/kernel/mm/swap/tiers, + any prior disable for that tier is invalidated. + + memory.swap.tiers.effective + A read-only file which exists on non-root cgroups. + + Shows the tiers this cgroup can actually swap out to. + This is the intersection of the parent's effective tiers + and this cgroup's own memory.swap.tiers configuration. + A child cannot enable a tier that is disabled in its + parent. + memory.swap.events A read-only flat-keyed file which exists on non-root cgroups. The following entries are defined. Unless specified diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 0782c72a1997..5603d6ce905f 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -281,7 +281,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 */ diff --git a/mm/memcontrol.c b/mm/memcontrol.c index ac7b46c4d67e..5d7036b3926f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -68,6 +68,7 @@ #include #include "slab.h" #include "memcontrol-v1.h" +#include "swap_tier.h" =20 #include =20 @@ -4086,6 +4087,9 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *pare= nt_css) WRITE_ONCE(memcg->zswap_writeback, true); #endif page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); + memcg->tier_mask =3D TIER_ALL_MASK; + swap_tiers_memcg_inherit_mask(memcg, parent); + if (parent) { WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent)); =20 @@ -5694,6 +5698,86 @@ static int swap_events_show(struct seq_file *m, void= *v) return 0; } =20 +static int swap_tier_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg =3D mem_cgroup_from_seq(m); + + swap_tiers_mask_show(m, memcg->tier_mask); + return 0; +} + +static ssize_t swap_tier_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off) +{ + struct mem_cgroup *memcg =3D mem_cgroup_from_css(of_css(of)); + char *pos, *token; + int ret =3D 0; + int original_mask; + + pos =3D strstrip(buf); + + spin_lock(&swap_tier_lock); + if (!*pos) { + memcg->tier_mask =3D TIER_ALL_MASK; + goto sync; + } + + original_mask =3D memcg->tier_mask; + + while ((token =3D strsep(&pos, " \t\n")) !=3D NULL) { + int mask; + + if (!*token) + continue; + + if (token[0] !=3D '-' && token[0] !=3D '+') { + ret =3D -EINVAL; + goto err; + } + + mask =3D swap_tiers_mask_lookup(token+1); + if (!mask) { + ret =3D -EINVAL; + goto err; + } + + /* + * if child already set, cannot add that tiers for hierarch mismatching. + * parent compatible, child must respect parent selected swap device. + */ + switch (token[0]) { + case '-': + memcg->tier_mask &=3D ~mask; + break; + case '+': + memcg->tier_mask |=3D mask; + break; + default: + ret =3D -EINVAL; + break; + } + + if (ret) + goto err; + } + +sync: + swap_tiers_memcg_sync_mask(memcg); +err: + if (ret) + memcg->tier_mask =3D original_mask; + spin_unlock(&swap_tier_lock); + return ret ? ret : nbytes; +} + +static int swap_tier_effective_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg =3D mem_cgroup_from_seq(m); + + swap_tiers_mask_show(m, memcg->tier_effective_mask); + return 0; +} + static struct cftype swap_files[] =3D { { .name =3D "swap.current", @@ -5726,6 +5810,17 @@ static struct cftype swap_files[] =3D { .file_offset =3D offsetof(struct mem_cgroup, swap_events_file), .seq_show =3D swap_events_show, }, + { + .name =3D "swap.tiers", + .flags =3D CFTYPE_NOT_ON_ROOT, + .seq_show =3D swap_tier_show, + .write =3D swap_tier_write, + }, + { + .name =3D "swap.tiers.effective", + .flags =3D CFTYPE_NOT_ON_ROOT, + .seq_show =3D swap_tier_effective_show, + }, { } /* terminate */ }; =20 diff --git a/mm/swap_state.c b/mm/swap_state.c index 847096e2f3e5..2d1bc6bc09d3 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -938,6 +938,7 @@ static ssize_t tiers_store(struct kobject *kobj, char *p, *token, *name, *tmp; int ret =3D 0; short prio; + int mask =3D 0; =20 tmp =3D kstrdup(buf, GFP_KERNEL); if (!tmp) @@ -970,7 +971,7 @@ static ssize_t tiers_store(struct kobject *kobj, goto restore; break; case '-': - ret =3D swap_tiers_remove(token + 1); + ret =3D swap_tiers_remove(token + 1, &mask); if (ret) goto restore; break; @@ -980,7 +981,7 @@ static ssize_t tiers_store(struct kobject *kobj, } } =20 - if (!swap_tiers_update()) { + if (!swap_tiers_update(mask)) { ret =3D -EINVAL; goto restore; } diff --git a/mm/swap_tier.c b/mm/swap_tier.c index 91aac55d3a8b..64365569b970 100644 --- a/mm/swap_tier.c +++ b/mm/swap_tier.c @@ -244,7 +244,7 @@ int swap_tiers_add(const char *name, int prio) return ret; } =20 -int swap_tiers_remove(const char *name) +int swap_tiers_remove(const char *name, int *mask) { int ret =3D 0; struct swap_tier *tier; @@ -267,6 +267,7 @@ int swap_tiers_remove(const char *name) list_prev_entry(tier, list)->prio =3D DEF_SWAP_PRIO; =20 swap_tier_inactivate(tier); + *mask |=3D TIER_MASK(tier); =20 return ret; } @@ -327,7 +328,24 @@ void swap_tiers_assign_dev(struct swap_info_struct *sw= p) swp->tier_mask =3D TIER_DEFAULT_MASK; } =20 -bool swap_tiers_update(void) +/* + * When a tier is removed, set its bit in every memcg's tier_mask and + * tier_effective_mask. This prevents stale tier indices from being + * silently filtered out if the same index is reused later. + */ +static void swap_tier_memcg_propagate(int mask) +{ + struct mem_cgroup *child; + + rcu_read_lock(); + for_each_mem_cgroup_tree(child, root_mem_cgroup) { + child->tier_mask |=3D mask; + child->tier_effective_mask |=3D mask; + } + rcu_read_unlock(); +} + +bool swap_tiers_update(int mask) { struct swap_tier *tier; struct swap_info_struct *swp; @@ -357,6 +375,77 @@ bool swap_tiers_update(void) break; swap_tiers_assign_dev(swp); } + /* + * XXX: Unused tiers default to ON, disabled after next tier added. + * Use removed tier mask to clear settings for removed/re-added tiers. + * (Could hold tier refs, but better to keep cgroup config independent) + */ + if (mask) + swap_tier_memcg_propagate(mask); =20 return true; } + +void swap_tiers_mask_show(struct seq_file *m, int mask) +{ + struct swap_tier *tier; + + spin_lock(&swap_tier_lock); + for_each_active_tier(tier) { + if (mask & TIER_MASK(tier)) + seq_printf(m, "%s ", tier->name); + } + spin_unlock(&swap_tier_lock); + seq_puts(m, "\n"); +} + +int swap_tiers_mask_lookup(const char *name) +{ + struct swap_tier *tier; + + lockdep_assert_held(&swap_tier_lock); + + for_each_active_tier(tier) { + if (!strcmp(name, tier->name)) + return TIER_MASK(tier); + } + + return 0; +} + +static void __swap_tier_memcg_inherit_mask(struct mem_cgroup *memcg, + struct mem_cgroup *parent) +{ + int effective_mask + =3D parent ? parent->tier_effective_mask : TIER_ALL_MASK; + + memcg->tier_effective_mask + =3D effective_mask & memcg->tier_mask; +} + +/* Computes the initial effective mask from the parent's effective mask. */ +void swap_tiers_memcg_inherit_mask(struct mem_cgroup *memcg, + struct mem_cgroup *parent) +{ + spin_lock(&swap_tier_lock); + rcu_read_lock(); + __swap_tier_memcg_inherit_mask(memcg, parent); + rcu_read_unlock(); + spin_unlock(&swap_tier_lock); +} + +/* + * Called when a memcg's tier_mask is modified. Walks the subtree + * and recomputes each descendant's effective mask against its parent. + */ +void swap_tiers_memcg_sync_mask(struct mem_cgroup *memcg) +{ + struct mem_cgroup *child; + + lockdep_assert_held(&swap_tier_lock); + + rcu_read_lock(); + for_each_mem_cgroup_tree(child, memcg) + __swap_tier_memcg_inherit_mask(child, parent_mem_cgroup(child)); + rcu_read_unlock(); +} diff --git a/mm/swap_tier.h b/mm/swap_tier.h index 6f281e95ed81..329c6a4f375f 100644 --- a/mm/swap_tier.h +++ b/mm/swap_tier.h @@ -10,21 +10,65 @@ struct swap_info_struct; =20 extern spinlock_t swap_tier_lock; =20 -#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); =20 int swap_tiers_add(const char *name, int prio); -int swap_tiers_remove(const char *name); +int swap_tiers_remove(const char *name, int *mask); =20 void swap_tiers_snapshot(void); void swap_tiers_snapshot_restore(void); -bool swap_tiers_update(void); +bool swap_tiers_update(int mask); =20 /* Tier assignment */ void swap_tiers_assign_dev(struct swap_info_struct *swp); + +#ifdef CONFIG_SWAP +/* 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); +#else +static inline void swap_tiers_mask_show(struct seq_file *m, int mask) {} +static inline void swap_tiers_memcg_inherit_mask(struct mem_cgroup *memcg, + struct mem_cgroup *parent) {} +static inline void swap_tiers_memcg_sync_mask(struct mem_cgroup *memcg) {} +static inline void __swap_tiers_memcg_sync_mask(struct mem_cgroup *memcg) = {} +#endif + +/* 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; +} + +#define TIER_ALL_MASK (~0) +#define TIER_DEFAULT_IDX (31) +#define TIER_DEFAULT_MASK (1 << TIER_DEFAULT_IDX) + +#ifdef CONFIG_MEMCG +static inline int folio_tier_effective_mask(struct folio *folio) +{ + struct mem_cgroup *memcg =3D folio_memcg(folio); + + return memcg ? memcg->tier_effective_mask : TIER_ALL_MASK; +} +#else +static inline int folio_tier_effective_mask(struct folio *folio) +{ + return TIER_ALL_MASK; +} +#endif + #endif /* _SWAP_TIER_H */ --=20 2.34.1 From nobody Thu Apr 2 15:36:28 2026 Received: from lgeamrelo03.lge.com (lgeamrelo03.lge.com [156.147.51.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C87A340F8E5 for ; Wed, 25 Mar 2026 17:55:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=156.147.51.102 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774461307; cv=none; b=o3zBAYkG+MiYhp6Yn8UPSLH9kPtqOHS6SgeaiMbYZLFzu1zw5S5hZJ6kr5sp0XsYoJ1+7WZOIYVFB09Pm0dT158Hj+90lmmay2y5fzemHAjbHbhKpO44G1JcqwkxGIuJFIRuTeDIcsNgApuggYIGDHi6HhNDwWWGYXjW+b9ZIqg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774461307; c=relaxed/simple; bh=3jReesnbo+6fxWSQiMSlmEbBtfmn95VxBkJ86UYJpXk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bWyxMYcxJdTX2bWh5rzRM7/XsPAOVVoSPmSXN1UKQCXNaQ54U6hpODMILzQA2xU34eStNKDK21loHHoKUJBo/oFGyZZNoXHy3bLGZ39wxXyCeUIa3GvgZadixiFsvN0a4ihMBJTRVE+03pVvin2Lg3sYBiGyDsKPEEWlBSHNny4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com; spf=pass smtp.mailfrom=lge.com; arc=none smtp.client-ip=156.147.51.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lge.com Received: from unknown (HELO yjaykim-PowerEdge-T330.lge.net) (10.177.112.156) by 156.147.51.102 with ESMTP; 26 Mar 2026 02:55:01 +0900 X-Original-SENDERIP: 10.177.112.156 X-Original-MAILFROM: youngjun.park@lge.com From: Youngjun Park To: Andrew Morton Cc: Chris Li , Youngjun Park , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kasong@tencent.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, shikemeng@huaweicloud.com, nphamcs@gmail.com, bhe@redhat.com, baohua@kernel.org, gunho.lee@lge.com, taejoon.song@lge.com, hyungjun.cho@lge.com, mkoutny@suse.com Subject: [PATCH v5 4/4] mm: swap: filter swap allocation by memcg tier mask Date: Thu, 26 Mar 2026 02:54:53 +0900 Message-Id: <20260325175453.2523280-5-youngjun.park@lge.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260325175453.2523280-1-youngjun.park@lge.com> References: <20260325175453.2523280-1-youngjun.park@lge.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Apply memcg tier effective mask during swap slot allocation to enforce per-cgroup swap tier restrictions. 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. 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 multiple non-rotational devices exist, percpu swap caches from different memcg contexts may reference mismatched tiers, causing unnecessary fast path misses. - When multiple non-rotational devices are assigned to different tiers and same-priority devices exist among them, cluster-based rotation may not work correctly. These edge cases do not affect the primary use case of directing swap traffic per cgroup. Further optimization is planned for future work. Signed-off-by: Youngjun Park --- mm/swapfile.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 645e10c3af28..627b09e57c1d 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1352,15 +1352,22 @@ static bool swap_alloc_fast(struct folio *folio) struct swap_cluster_info *ci; struct swap_info_struct *si; unsigned int offset; + int mask =3D folio_tier_effective_mask(folio); =20 /* * Once allocated, swap_info_struct will never be completely freed, * so checking it's liveness by get_swap_device_info is enough. */ si =3D this_cpu_read(percpu_swap_cluster.si[order]); + if (!si || !swap_tiers_mask_test(si->tier_mask, mask) || + !get_swap_device_info(si)) + return false; + offset =3D this_cpu_read(percpu_swap_cluster.offset[order]); - if (!si || !offset || !get_swap_device_info(si)) + if (!offset) { + put_swap_device(si); return false; + } =20 ci =3D swap_cluster_lock(si, offset); if (cluster_is_usable(ci, order)) { @@ -1379,10 +1386,14 @@ static bool swap_alloc_fast(struct folio *folio) static void swap_alloc_slow(struct folio *folio) { struct swap_info_struct *si, *next; + int mask =3D folio_tier_effective_mask(folio); =20 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); --=20 2.34.1