mm/demotion: Memory tiers and demotion
Subsystems (7)
v6 cover mm/demotion: Memory tiers and demotion
The current kernel has the basic memory tiering support: Inactive pages on a higher tier NUMA node can be migrated (demoted) to a lower tier NUMA node to make room for new allocations on the higher tier NUMA node. Frequently accessed pages on a lower tier NUMA node can be migrated (promoted) to a higher tier NUMA node to improve the performance.
In the current kernel, memory tiers are defined implicitly via a demotion path relationship between NUMA nodes, which is created during the kernel initialization and updated when a NUMA node is hot-added or hot-removed. The current implementation puts all nodes with CPU into the top tier, and builds the tier hierarchy tier-by-tier by establishing the per-node demotion targets based on the distances between nodes.
This current memory tier kernel interface needs to be improved for several important use cases:
* The current tier initialization code always initializes each memory-only NUMA node into a lower tier. But a memory-only NUMA node may have a high performance memory device (e.g. a DRAM device attached via CXL.mem or a DRAM-backed memory-only node on a virtual machine) and should be put into a higher tier.
* The current tier hierarchy always puts CPU nodes into the top tier. But on a system with HBM (e.g. GPU memory) devices, these memory-only HBM NUMA nodes should be in the top tier, and DRAM nodes with CPUs are better to be placed into the next lower tier.
* Also because the current tier hierarchy always puts CPU nodes into the top tier, when a CPU is hot-added (or hot-removed) and triggers a memory node from CPU-less into a CPU node (or vice versa), the memory tier hierarchy gets changed, even though no memory node is added or removed. This can make the tier hierarchy unstable and make it difficult to support tier-based memory accounting.
* A higher tier node can only be demoted to selected nodes on the next lower tier as defined by the demotion path, not any other node from any lower tier. This strict, hard-coded demotion order does not work in all use cases (e.g. some use cases may want to allow cross-socket demotion to another node in the same demotion tier as a fallback when the preferred demotion node is out of space), and has resulted in the feature request for an interface to override the system-wide, per-node demotion order from the userspace. This demotion order is also inconsistent with the page allocation fallback order when all the nodes in a higher tier are out of space: The page allocation can fall back to any node from any lower tier, whereas the demotion order doesn't allow that.
* There are no interfaces for the userspace to learn about the memory tier hierarchy in order to optimize its memory allocations.
This patch series make the creation of memory tiers explicit under the control of userspace or device driver.
Memory Tier Initialization
==========================
By default, all memory nodes are assigned to the default tier (1). The default tier device has a rank value (200).
A device driver can move up or down its memory nodes from the default tier. For example, PMEM can move down its memory nodes below the default tier, whereas GPU can move up its memory nodes above the default tier.
The kernel initialization code makes the decision on which exact tier a memory node should be assigned to based on the requests from the device drivers as well as the memory device hardware information provided by the firmware.
Hot-adding/removing CPUs doesn't affect memory tier hierarchy.
Memory Allocation for Demotion
==============================
This patch series keep the demotion target page allocation logic same. The demotion page allocation pick the closest NUMA node in the next lower tier to the current NUMA node allocating pages from.
This will be later improved to use the same page allocation strategy using fallback list.
Sysfs Interface:
=======================
Listing current list of memory tiers and rank details:
:/sys/devices/system/memtier$ ls default_tier max_tier memtier1 power uevent :/sys/devices/system/memtier$ cat default_tier memtier1 :/sys/devices/system/memtier$ cat max_tier 3 :/sys/devices/system/memtier$
Per node memory tier details:
For a cpu only NUMA node:
:/sys/devices/system/node# cat node0/memtier :/sys/devices/system/node# echo 1 > node0/memtier :/sys/devices/system/node# cat node0/memtier :/sys/devices/system/node#
For a NUMA node with memory:
:/sys/devices/system/node# cat node1/memtier 1 :/sys/devices/system/node# ls ../memtier/ default_tier max_tier memtier1 power uevent :/sys/devices/system/node# echo 2 > node1/memtier :/sys/devices/system/node# :/sys/devices/system/node# ls ../memtier/ default_tier max_tier memtier1 memtier2 power uevent :/sys/devices/system/node# cat node1/memtier 2 :/sys/devices/system/node# :/sys/devices/system/node# cat ../memtier/memtier2/rank 100 :/sys/devices/system/node# :/sys/devices/system/node# cat ../memtier/memtier1/rank 200 :/sys/devices/system/node#
Removing a NUMA node from demotion:
:/sys/devices/system/node# cat node1/memtier 2 :/sys/devices/system/node# echo none > node1/memtier :/sys/devices/system/node# :/sys/devices/system/node# cat node1/memtier :/sys/devices/system/node# :/sys/devices/system/node# ls ../memtier/ default_tier max_tier memtier1 power uevent :/sys/devices/system/node#
The above also resulted in removal of memtier2 which was created in the earlier step.
Changes from v5:
* Remove patch supporting N_MEMORY node removal from memory tiers. memory tiers are going to be used for features other than demotion. Hence keep all N_MEMORY nodes in memory tiers irrespective of whether they want to participate in promotion or demotion.
* Add NODE_DATA->memtier
* Rearrage patches to add sysfs files later.
* Add support to create memory tiers from userspace.
* Address other review feedback.
Changes from v4:
* Address review feedback.
* Reverse the meaning of "rank": higher rank value means higher tier.
* Add "/sys/devices/system/memtier/default_tier".
* Add node_is_toptier
v4:
Add support for explicit memory tiers and ranks.
v3:
- Modify patch 1 subject to make it more specific
- Remove /sys/kernel/mm/numa/demotion_targets interface, use /sys/devices/system/node/demotion_targets instead and make it writable to override node_states[N_DEMOTION_TARGETS].
- Add support to view per node demotion targets via sysfs
v2:
In v1, only 1st patch of this patch series was sent, which was implemented to avoid some of the limitations on the demotion target sharing, however for certain numa topology, the demotion targets found by that patch was not most optimal, so 1st patch in this series is modified according to suggestions from Huang and Baolin. Different examples of demotion list comparasion between existing implementation and changed implementation can be found in the commit message of 1st patch.
Aneesh Kumar K.V (11):
mm/demotion: Add support for explicit memory tiers mm/demotion: Move memory demotion related code mm/demotion: Return error on write to numa_demotion sysfs mm/demotion/dax/kmem: Set node's memory tier to MEMORY_TIER_PMEM mm/demotion: Build demotion targets based on explicit memory tiers mm/demotion: Expose memory tier details via sysfs mm/demotion: Add per node memory tier attribute to sysfs mm/demotion: Add support for memory tier creation from userspace mm/demotion: Add pg_data_t member to track node memory tier details mm/demotion: Update node_is_toptier to work with memory tiers mm/demotion: Add sysfs ABI documentation
Jagdish Gediya (2):
mm/demotion: Demote pages according to allocation fallback order mm/demotion: Add documentation for memory tiering
.../ABI/testing/sysfs-kernel-mm-memory-tiers | 87 ++ Documentation/admin-guide/mm/index.rst | 1 + .../admin-guide/mm/memory-tiering.rst | 181 ++++ drivers/base/node.c | 39 + drivers/dax/kmem.c | 4 + include/linux/memory-tiers.h | 63 ++ include/linux/migrate.h | 15 - include/linux/mmzone.h | 3 + include/linux/node.h | 5 - mm/Kconfig | 3 + mm/Makefile | 1 + mm/huge_memory.c | 1 + mm/memory-tiers.c | 888 ++++++++++++++++++ mm/migrate.c | 453 +-------- mm/mprotect.c | 1 + mm/vmscan.c | 57 +- mm/vmstat.c | 4 - 17 files changed, 1316 insertions(+), 490 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-kernel-mm-memory-tiers create mode 100644 Documentation/admin-guide/mm/memory-tiering.rst create mode 100644 include/linux/memory-tiers.h create mode 100644 mm/memory-tiers.c
Signature · 1 line
2.36.1
PATCH v6 1/13 mm/demotion: Add support for explicit memory tiers
In the current kernel, memory tiers are defined implicitly via a demotion path relationship between NUMA nodes, which is created during the kernel initialization and updated when a NUMA node is hot-added or hot-removed. The current implementation puts all nodes with CPU into the top tier, and builds the tier hierarchy tier-by-tier by establishing the per-node demotion targets based on the distances between nodes.
This current memory tier kernel interface needs to be improved for several important use cases,
The current tier initialization code always initializes each memory-only NUMA node into a lower tier. But a memory-only NUMA node may have a high performance memory device (e.g. a DRAM device attached via CXL.mem or a DRAM-backed memory-only node on a virtual machine) and should be put into a higher tier.
The current tier hierarchy always puts CPU nodes into the top tier. But on a system with HBM or GPU devices, the memory-only NUMA nodes mapping these devices should be in the top tier, and DRAM nodes with CPUs are better to be placed into the next lower tier.
With current kernel higher tier node can only be demoted to selected nodes on the next lower tier as defined by the demotion path, not any other
node from any lower tier. This strict, hard-coded demotion order does not work in all use cases (e.g. some use cases may want to allow cross-socket demotion to another node in the same demotion tier as a fallback when the preferred demotion node is out of space), This demotion order is also inconsistent with the page allocation fallback order when all the nodes in a higher tier are out of space: The page allocation can fall back to any node from
any lower tier, whereas the demotion order doesn't allow that.
The current kernel also don't provide any interfaces for the userspace to learn about the memory tier hierarchy in order to optimize its memory allocations.
This patch series address the above by defining memory tiers explicitly.
This patch introduce explicity memory tiers with ranks. The rank value of a memory tier is used to derive the demotion order between NUMA nodes. The memory tiers present in a system can be found at
"Rank" is an opaque value. Its absolute value doesn't have any special meaning. But the rank values of different memtiers can be compared with each other to determine the memory tier order.
For example, if we have 3 memtiers: memtier0, memtier1, memiter2, and their rank values are 300, 200, 100, then the memory tier order is:
memtier0 -> memtier1 -> memtier2, where memtier0 is the highest tier and memtier2 is the lowest tier.
The rank value of each memtier should be unique.
A higher rank memory tier will appear first in the demotion order than a lower rank memory tier. ie. while reclaim we choose a node in higher rank memory tier to demote pages to as compared to a node in a lower rank memory tier.
This patchset introduce 3 memory tiers (memtier0, memtier1 and memtier2) which are created by different kernel subsystems. The default memory tier created by the kernel is memtier1. Once created these memory tiers are not destroyed even if they don't have any NUMA nodes assigned to them.
This patch is based on the proposal sent by Wei Xu <[email protected]> at [1].
[1] https://lore.kernel.org/linux-mm/CAAPL-u9Wv+nH1VOZTj=9p9S70Y3Qz3+63EkqncRDdHfubsrjfw@mail.gmail.com
/sys/devices/system/memtier/memtierN/
The nodes which are part of a specific memory tier can be listed via/sys/devices/system/memtier/memtierN/nodelist
--- include/linux/memory-tiers.h | 20 ++++++++ mm/Kconfig | 3 ++ mm/Makefile | 1 + mm/memory-tiers.c | 89 ++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 include/linux/memory-tiers.h create mode 100644 mm/memory-tiers.c
include/linux/memory-tiers.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MEMORY_TIERS_H +#define _LINUX_MEMORY_TIERS_H + +#ifdef CONFIG_TIERED_MEMORY + +#define MEMORY_TIER_HBM_GPU 0 +#define MEMORY_TIER_DRAM 1 +#define MEMORY_TIER_PMEM 2 + +#define MEMORY_RANK_HBM_GPU 300 +#define MEMORY_RANK_DRAM 200 +#define MEMORY_RANK_PMEM 100 + +#define DEFAULT_MEMORY_TIER MEMORY_TIER_DRAM +#define MAX_MEMORY_TIERS 3 + +#endif /* CONFIG_TIERED_MEMORY */ + +#endif
mm/Kconfig +3 -0
@@ -614,6 +614,9 @@ config ARCH_ENABLE_HUGEPAGE_MIGRATION config ARCH_ENABLE_THP_MIGRATION bool +config TIERED_MEMORY + def_bool NUMA + config HUGETLB_PAGE_SIZE_VARIABLE def_bool n help
mm/Makefile +1 -0
@@ -92,6 +92,7 @@ obj-$(CONFIG_KFENCE) += kfence/ obj-$(CONFIG_FAILSLAB) += failslab.o obj-$(CONFIG_MEMTEST) += memtest.o obj-$(CONFIG_MIGRATION) += migrate.o +obj-$(CONFIG_TIERED_MEMORY) += memory-tiers.o obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
mm/memory-tiers.c new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
2.36.1
PATCH v6 2/13 mm/demotion: Move memory demotion related code
In reply to Aneesh Kumar K.VThis move memory demotion related code to mm/memory-tiers.c. No functional change in this patch.
--- include/linux/memory-tiers.h | 6 ++++ include/linux/migrate.h | 2 -- mm/memory-tiers.c | 61 ++++++++++++++++++++++++++++++++++++ mm/migrate.c | 60 +---------------------------------- mm/vmscan.c | 1 + 5 files changed, 69 insertions(+), 61 deletions(-)
include/linux/memory-tiers.h +6 -0
@@ -2,6 +2,8 @@#ifndef _LINUX_MEMORY_TIERS_H#define _LINUX_MEMORY_TIERS_H +#include <linux/types.h> +#ifdef CONFIG_TIERED_MEMORY #define MEMORY_TIER_HBM_GPU 0
@@ -15,6 +17,10 @@#define DEFAULT_MEMORY_TIER MEMORY_TIER_DRAM#define MAX_MEMORY_TIERS 3 +extern bool numa_demotion_enabled; +#else +#define numa_demotion_enabled false +#endif /* CONFIG_TIERED_MEMORY */ #endif
include/linux/migrate.h +0 -2
@@ -78,7 +78,6 @@ static inline int migrate_huge_page_move_mapping(struct address_space *mapping,#if defined(CONFIG_MIGRATION ) && defined(CONFIG_NUMA )extern void set_migration_target_nodes (void );extern void migrate_on_reclaim_init (void ); -extern bool numa_demotion_enabled;extern int next_demotion_node (int node);#else static inline void set_migration_target_nodes (void ) {}
@@ -87,7 +86,6 @@ static inline int next_demotion_node(int node) {return NUMA_NO_NODE ; } -#define numa_demotion_enabled false#endif #ifdef CONFIG_COMPACTION
mm/memory-tiers.c +61 -0
69 lines not on this page · raw message
mm/migrate.c +1 -59
65 lines not on this page · raw message
mm/vmscan.c +1 -0
@@ -50,6 +50,7 @@#include <linux/printk.h> #include <linux/dax.h> #include <linux/psi.h> +#include <linux/memory-tiers.h> #include <asm/tlbflush.h> #include <asm/div64.h>
Signature · 1 line
2.36.1
PATCH v6 3/13 mm/demotion: Return error on write to numa_demotion sysfs
In reply to Aneesh Kumar K.VWith CONFIG_MIGRATION disabled return EINVAL on write.
--- mm/memory-tiers.c | 3 +++ 1 file changed, 3 insertions(+)
mm/memory-tiers.c +3 -0
@@ -105,6 +105,9 @@ static ssize_t numa_demotion_enabled_store(struct kobject *kobj, {ssize_t ret; +if (!IS_ENABLED (CONFIG_MIGRATION )) +return -EINVAL ; + ret =kstrtobool (buf, &numa_demotion_enabled);if (ret)return ret;
Signature · 1 line
2.36.1
PATCH v6 4/13 mm/demotion/dax/kmem: Set node's memory tier to MEMORY_TIER_PMEM
In reply to Aneesh Kumar K.VBy default, all nodes are assigned to DEFAULT_MEMORY_TIER which is the memory tier designated for nodes with DRAM
Set dax kmem device node's tier to MEMORY_TIER_PMEM. MEMORY_TIER_PMEM is assigned a default rank value of 100 and appears below DEFAULT_MEMORY_TIER in demotion order.
--- drivers/dax/kmem.c | 4 ++ include/linux/memory-tiers.h | 1 + mm/memory-tiers.c | 78 ++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+)
drivers/dax/kmem.c +4 -0
@@ -11,6 +11,7 @@#include <linux/fs.h> #include <linux/mm.h> #include <linux/mman.h> +#include <linux/memory-tiers.h> #include "dax-private.h" #include "bus.h"
@@ -147,6 +148,9 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)dev_set_drvdata (dev, data); +#ifdef CONFIG_TIERED_MEMORY +node_create_and_set_memory_tier (numa_node,MEMORY_TIER_PMEM ); +#endif return 0 ; err_request_mem:
include/linux/memory-tiers.h +1 -0
@@ -18,6 +18,7 @@#define MAX_MEMORY_TIERS 3extern bool numa_demotion_enabled; +int node_create_and_set_memory_tier (int node,int tier);#else #define numa_demotion_enabled false
mm/memory-tiers.c +78 -0
84 lines not on this page · raw message
Signature · 1 line
2.36.1
PATCH v6 5/13 mm/demotion: Build demotion targets based on explicit memory tiers
In reply to Aneesh Kumar K.VThis patch switch the demotion target building logic to use memory tiers instead of NUMA distance. All N_MEMORY NUMA nodes will be placed in the default tier 1 and additional memory tiers will be added by drivers like dax kmem.
This patch builds the demotion target for a NUMA node by looking at all memory tiers below the tier to which the NUMA node belongs. The closest node in the immediately following memory tier is used as a demotion target.
Since we are now only building demotion target for N_MEMORY NUMA nodes the CPU hotplug calls are removed in this patch.
The rank approach allows us to keep memory tier device IDs stable even if there is a need to change the tier ordering among different memory tiers. e.g. DRAM nodes with CPUs will always be on memtier1, no matter how many tiers are higher or lower than these nodes. A new memory tier can be inserted into the tier hierarchy for a new set of nodes without affecting the node assignment of any existing memtier, provided that there is enough gap in the rank values for the new memtier.
The absolute value of "rank" of a memtier doesn't necessarily carry any meaning. Its value relative to other memtiers decides the level of this memtier in the tier hierarchy.
For now, This patch supports hardcoded rank values which are 300, 200, & 100 for memory tiers 0,1 & 2 respectively.
Below is the sysfs interface to read the rank values of memory tier, /sys/devices/system/memtier/memtierN/rank
This interface is read only for now. Write support can be added when there is a need of flexibility of more number of memory tiers(> 3) with flexibile ordering requirement among them.
--- include/linux/memory-tiers.h | 5 + include/linux/migrate.h | 13 -- mm/memory-tiers.c | 291 ++++++++++++++++++++++++++ mm/migrate.c | 394 ----------------------------------- mm/vmstat.c | 4 - 5 files changed, 296 insertions(+), 411 deletions(-)
include/linux/memory-tiers.h +5 -0
@@ -19,8 +19,13 @@extern bool numa_demotion_enabled;int node_create_and_set_memory_tier (int node,int tier); +int next_demotion_node (int node);#else #define numa_demotion_enabled false +static inline int next_demotion_node (int node) +{ +return NUMA_NO_NODE ; +}#endif /* CONFIG_TIERED_MEMORY */
include/linux/migrate.h +0 -13
@@ -75,19 +75,6 @@ static inline int migrate_huge_page_move_mapping(struct address_space *mapping,#endif /* CONFIG_MIGRATION */ -#if defined(CONFIG_MIGRATION ) && defined(CONFIG_NUMA ) -extern void set_migration_target_nodes (void ); -extern void migrate_on_reclaim_init (void ); -extern int next_demotion_node (int node); -#else -static inline void set_migration_target_nodes (void ) {} -static inline void migrate_on_reclaim_init (void ) {} -static inline int next_demotion_node (int node) -{ -return NUMA_NO_NODE ; -} -#endif -#ifdef CONFIG_COMPACTION extern int PageMovable (struct page *page);extern void __SetPageMovable (struct page *page,struct address_space *mapping);
mm/memory-tiers.c +291 -0
330 lines not on this page · raw message
mm/migrate.c +0 -394
398 lines not on this page · raw message
mm/vmstat.c +0 -4
@@ -28,7 +28,6 @@#include <linux/mm_inline.h> #include <linux/page_ext.h> #include <linux/page_owner.h> -#include <linux/migrate.h> #include "internal.h"
@@ -2060,7 +2059,6 @@ static int vmstat_cpu_online(unsigned int cpu)if (!node_state (cpu_to_node (cpu),N_CPU )) {node_set_state (cpu_to_node (cpu),N_CPU ); -set_migration_target_nodes (); }return 0 ;
@@ -2085,7 +2083,6 @@ static int vmstat_cpu_dead(unsigned int cpu)return 0 ;node_clear_state (node,N_CPU ); -set_migration_target_nodes ();return 0 ; }
@@ -2118,7 +2115,6 @@ void __init init_mm_internals(void)start_shepherd_timer ();#endif -migrate_on_reclaim_init ();#ifdef CONFIG_PROC_FS proc_create_seq ("buddyinfo" ,0444 ,NULL , &fragmentation_op);proc_create_seq ("pagetypeinfo" ,0400 ,NULL , &pagetypeinfo_op);
Signature · 1 line
2.36.1
PATCH v6 6/13 mm/demotion: Expose memory tier details via sysfs
In reply to Aneesh Kumar K.VThis patch adds /sys/devices/system/memtier/ where all memory tier related details can be found. All created memory tiers will be listed there as /sys/devices/system/memtier/memtierN/
The nodes which are part of a specific memory tier can be listed via /sys/devices/system/memtier/memtierN/nodelist
The rank value of a memory tier can be listed via
via /sys/devices/system/memtier/memtierN/rank
/sys/devices/system/memtier/max_tier shows the maximum number of memory tiers that can be created.
/sys/devices/system/memtier/default_tier shows the memory tier to which NUMA nodes get added by default if not assigned a specific memory tier.
--- mm/memory-tiers.c | 99 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 4 deletions(-)
mm/memory-tiers.c +95 -4
@@ -11,8 +11,8 @@struct memory_tier {struct list_head list; +struct device dev;nodemask_t nodelist; -int id;int rank; };
@@ -20,6 +20,7 @@ struct demotion_nodes {nodemask_t preferred; }; +#define to_memory_tier (device) container_of(device, struct memory_tier, dev)static void establish_migration_targets (void );static DEFINE_MUTEX (memory_tier_lock);static LIST_HEAD (memory_tiers);
@@ -86,6 +87,52 @@ static LIST_HEAD(memory_tiers);*/ static struct demotion_nodes *node_demotion__read_mostly ; +static struct bus_type memory_tier_subsys = { + .name ="memtier" , + .dev_name ="memtier" , +}; + +static ssize_t nodelist_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct memory_tier *memtier =to_memory_tier (dev); + +return sysfs_emit (buf,"%*pbl\n" , +nodemask_pr_args (&memtier->nodelist)); +} +static DEVICE_ATTR_RO (nodelist); + +static ssize_t rank_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct memory_tier *memtier =to_memory_tier (dev); + +return sysfs_emit (buf,"%d\n" , memtier->rank); +} +static DEVICE_ATTR_RO (rank); + +static struct attribute *memory_tier_dev_attrs[] = { + &dev_attr_nodelist.attr, + &dev_attr_rank.attr, +NULL +}; + +static const struct attribute_group memory_tier_dev_group = { + .attrs = memory_tier_dev_attrs, +}; + +static const struct attribute_group *memory_tier_dev_groups[] = { + &memory_tier_dev_group, +NULL +}; + +static void memory_tier_device_release (struct device *dev) +{ +struct memory_tier *tier =to_memory_tier (dev); + +kfree (tier); +} +/* * Keep it simple by having direct mapping between * tier index and rank value.
@@ -121,6 +168,7 @@ static void insert_memory_tier(struct memory_tier *memtier)static struct memory_tier *register_memory_tier (unsigned int tier,unsigned int rank) { +int error;struct memory_tier *memtier;if (tier >=MAX_MEMORY_TIERS )
@@ -130,11 +178,20 @@ static struct memory_tier *register_memory_tier(unsigned int tier,if (!memtier)return ERR_PTR (-ENOMEM ); - memtier->id = tier; + memtier->dev.id = tier; memtier->rank = rank; + memtier->dev.bus = &memory_tier_subsys; + memtier->dev.release = memory_tier_device_release; + memtier->dev.groups = memory_tier_dev_groups;insert_memory_tier (memtier); + error =device_register (&memtier->dev); +if (error) { +list_del (&memtier->list); +put_device (&memtier->dev); +return ERR_PTR (error); + }return memtier; }
@@ -154,7 +211,7 @@ static struct memory_tier *__get_memory_tier_from_id(int id)struct memory_tier *memtier;list_for_each_entry (memtier, &memory_tiers, list) { -if (memtier->id == id) +if (memtier->dev.id == id)return memtier; }return NULL ;
@@ -199,7 +256,7 @@ int node_create_and_set_memory_tier(int node, int tier) goto out; } -if (current_tier->id == tier) +if (current_tier->dev.id == tier) goto out;node_clear (node, current_tier->nodelist);
@@ -435,10 +492,44 @@ static void __init migrate_on_reclaim_init(void)hotplug_memory_notifier (migrate_on_reclaim_callback,100 ); } +static ssize_t +max_tier_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"%d\n" ,MAX_MEMORY_TIERS ); +} +static DEVICE_ATTR_RO (max_tier); + +static ssize_t +default_tier_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"memtier%d\n" ,DEFAULT_MEMORY_TIER ); +} +static DEVICE_ATTR_RO (default_tier); + +static struct attribute *memory_tier_attrs[] = { + &dev_attr_max_tier.attr, + &dev_attr_default_tier.attr, +NULL +}; + +static const struct attribute_group memory_tier_attr_group = { + .attrs = memory_tier_attrs, +}; + +static const struct attribute_group *memory_tier_attr_groups[] = { + &memory_tier_attr_group, +NULL , +}; +static int __init memory_tier_init (void ) { +int ret;struct memory_tier *memtier; + ret =subsys_system_register (&memory_tier_subsys, memory_tier_attr_groups); +if (ret) +pr_err ("%s() failed to register subsystem: %d\n" , __func__, ret); +/* * Register only default memory tier to hide all empty * memory tier from sysfs.
Signature · 1 line
2.36.1
Quoted 87 lines · On Fri, 10 Jun 2022 19:19:53 +0530 "Aneesh Kumar K.V"…
The current kernel has the basic memory tiering support: Inactive pages on a higher tier NUMA node can be migrated (demoted) to a lower tier NUMA node to make room for new allocations on the higher tier NUMA node. Frequently accessed pages on a lower tier NUMA node can be migrated (promoted) to a higher tier NUMA node to improve the performance.
In the current kernel, memory tiers are defined implicitly via a demotion path relationship between NUMA nodes, which is created during the kernel initialization and updated when a NUMA node is hot-added or hot-removed. The current implementation puts all nodes with CPU into the top tier, and builds the tier hierarchy tier-by-tier by establishing the per-node demotion targets based on the distances between nodes.
This current memory tier kernel interface needs to be improved for several important use cases:
* The current tier initialization code always initializes each memory-only NUMA node into a lower tier. But a memory-only NUMA node may have a high performance memory device (e.g. a DRAM device attached via CXL.mem or a DRAM-backed memory-only node on a virtual machine) and should be put into a higher tier.
* The current tier hierarchy always puts CPU nodes into the top tier. But on a system with HBM (e.g. GPU memory) devices, these memory-only HBM NUMA nodes should be in the top tier, and DRAM nodes with CPUs are better to be placed into the next lower tier.
* Also because the current tier hierarchy always puts CPU nodes into the top tier, when a CPU is hot-added (or hot-removed) and triggers a memory node from CPU-less into a CPU node (or vice versa), the memory tier hierarchy gets changed, even though no memory node is added or removed. This can make the tier hierarchy unstable and make it difficult to support tier-based memory accounting.
* A higher tier node can only be demoted to selected nodes on the next lower tier as defined by the demotion path, not any other node from any lower tier. This strict, hard-coded demotion order does not work in all use cases (e.g. some use cases may want to allow cross-socket demotion to another node in the same demotion tier as a fallback when the preferred demotion node is out of space), and has resulted in the feature request for an interface to override the system-wide, per-node demotion order from the userspace. This demotion order is also inconsistent with the page allocation fallback order when all the nodes in a higher tier are out of space: The page allocation can fall back to any node from any lower tier, whereas the demotion order doesn't allow that.
* There are no interfaces for the userspace to learn about the memory tier hierarchy in order to optimize its memory allocations.
This patch series make the creation of memory tiers explicit under the control of userspace or device driver.
Memory Tier Initialization
==========================By default, all memory nodes are assigned to the default tier (1). The default tier device has a rank value (200).
A device driver can move up or down its memory nodes from the default tier. For example, PMEM can move down its memory nodes below the default tier, whereas GPU can move up its memory nodes above the default tier.
The kernel initialization code makes the decision on which exact tier a memory node should be assigned to based on the requests from the device drivers as well as the memory device hardware information provided by the firmware.
Hot-adding/removing CPUs doesn't affect memory tier hierarchy.
Memory Allocation for Demotion
==============================
This patch series keep the demotion target page allocation logic same. The demotion page allocation pick the closest NUMA node in the next lower tier to the current NUMA node allocating pages from.This will be later improved to use the same page allocation strategy using fallback list.
Sysfs Interface:
=======================
Listing current list of memory tiers and rank details:
Quoted 6 lines
:/sys/devices/system/memtier$ ls default_tier max_tier memtier1 power uevent :/sys/devices/system/memtier$ cat default_tier memtier1 :/sys/devices/system/memtier$ cat max_tier 3
Should this be renamed to max_tiers or tiers_count? Otherwise one might confuse max_tier as the highest allowed tier ID.
Quoted 124 lines
:/sys/devices/system/memtier$
Per node memory tier details:
For a cpu only NUMA node:
:/sys/devices/system/node# cat node0/memtier :/sys/devices/system/node# echo 1 > node0/memtier :/sys/devices/system/node# cat node0/memtier :/sys/devices/system/node#For a NUMA node with memory:
:/sys/devices/system/node# cat node1/memtier 1 :/sys/devices/system/node# ls ../memtier/ default_tier max_tier memtier1 power uevent :/sys/devices/system/node# echo 2 > node1/memtier :/sys/devices/system/node# :/sys/devices/system/node# ls ../memtier/ default_tier max_tier memtier1 memtier2 power uevent :/sys/devices/system/node# cat node1/memtier 2 :/sys/devices/system/node# :/sys/devices/system/node# cat ../memtier/memtier2/rank 100 :/sys/devices/system/node# :/sys/devices/system/node# cat ../memtier/memtier1/rank 200 :/sys/devices/system/node#Removing a NUMA node from demotion:
:/sys/devices/system/node# cat node1/memtier 2 :/sys/devices/system/node# echo none > node1/memtier :/sys/devices/system/node# :/sys/devices/system/node# cat node1/memtier :/sys/devices/system/node# :/sys/devices/system/node# ls ../memtier/ default_tier max_tier memtier1 power uevent :/sys/devices/system/node#The above also resulted in removal of memtier2 which was created in the earlier step.
Changes from v5:
* Remove patch supportingN_MEMORYnode removal from memory tiers. memory tiers are going to be used for features other than demotion. Hence keep allN_MEMORYnodes in memory tiers irrespective of whether they want to participate in promotion or demotion.
* AddNODE_DATA->memtier
* Rearrage patches to add sysfs files later.
* Add support to create memory tiers from userspace.
* Address other review feedback.Changes from v4:
* Address review feedback.
* Reverse the meaning of "rank": higher rank value means higher tier.
* Add "/sys/devices/system/memtier/default_tier".
* Addnode_is_toptierv4:
Add support for explicit memory tiers and ranks.v3:
- Modify patch 1 subject to make it more specific
- Remove/sys/kernel/mm/numa/demotion_targetsinterface, use/sys/devices/system/node/demotion_targetsinstead and make it writable to override node_states[N_DEMOTION_TARGETS].
- Add support to view per node demotion targets via sysfsv2:
In v1, only 1st patch of this patch series was sent, which was implemented to avoid some of the limitations on the demotion target sharing, however for certain numa topology, the demotion targets found by that patch was not most optimal, so 1st patch in this series is modified according to suggestions from Huang and Baolin. Different examples of demotion list comparasion between existing implementation and changed implementation can be found in the commit message of 1st patch.Aneesh Kumar K.V (11):
mm/demotion: Add support for explicit memory tiers mm/demotion: Move memory demotion related code mm/demotion: Return error on write tonuma_demotionsysfsmm/demotion/dax/kmem: Set node's memory tier toMEMORY_TIER_PMEMmm/demotion: Build demotion targets based on explicit memory tiers mm/demotion: Expose memory tier details via sysfs mm/demotion: Add per node memory tier attribute to sysfs mm/demotion: Add support for memory tier creation from userspace mm/demotion: Addpg_data_tmember to track node memory tier details mm/demotion: Updatenode_is_toptierto work with memory tiers mm/demotion: Add sysfs ABI documentationJagdish Gediya (2):
mm/demotion: Demote pages according to allocation fallback order mm/demotion: Add documentation for memory tiering.../ABI/testing/sysfs-kernel-mm-memory-tiers | 87 ++ Documentation/admin-guide/mm/index.rst | 1 + .../admin-guide/mm/memory-tiering.rst | 181 ++++ drivers/base/node.c | 39 + drivers/dax/kmem.c | 4 + include/linux/memory-tiers.h | 63 ++ include/linux/migrate.h | 15 - include/linux/mmzone.h | 3 + include/linux/node.h | 5 - mm/Kconfig | 3 + mm/Makefile | 1 + mm/huge_memory.c | 1 + mm/memory-tiers.c | 888 ++++++++++++++++++ mm/migrate.c | 453 +-------- mm/mprotect.c | 1 + mm/vmscan.c | 57 +- mm/vmstat.c | 4 - 17 files changed, 1316 insertions(+), 490 deletions(-) create mode 100644
Documentation/ABI/testing/sysfs-kernel-mm-memory-tierscreate mode 100644Documentation/admin-guide/mm/memory-tiering.rstcreate mode 100644include/linux/memory-tiers.hcreate mode 100644mm/memory-tiers.c