mm/demotion: Memory tiers and demotion
Subsystems (10)
v15 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 highest 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 implementation 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-backed memory-only node on a virtual machine) and that 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 nodes with shortest distance on the next lower tier as defined by the demotion path, not any other node from any lower tier. This strict, 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.
This patch series make the creation of memory tiers explicit under the control of device driver.
Memory Tier Initialization
==========================
Linux kernel presents memory devices as NUMA nodes and each memory device is of a specific type. The memory type of a device is represented by its abstract distance. A memory tier corresponds to a range of abstract distance. This allows for classifying memory devices with a specific performance range into a memory tier.
By default, all memory nodes are assigned to the default tier with abstract distance 512.
A device driver can move its memory nodes from the default tier. For example, PMEM can move its memory nodes below the default tier, whereas GPU can move 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.
Changes from v14
* Add Reviewed-by:
* Address review feedback w.r.t default adistance value
Changes from v13
* Address review feedback.
* Add path dropping memtier from struct memory_dev_type
Changes from v12
* Fix kernel crash on module unload
* Address review feedback.
* Add node_random patch to this series based on review feedback
Changes from v11:
* smaller abstract distance imply faster(higher) memory tier.
Changes from v10:
* rename performance level to abstract distance
* Thanks to all the good feedback from Huang, Ying <[email protected]>. Updated the patchset to cover most of the review feedback.
Changes from v9:
* Use performance level for initializing memory tiers.
Changes from v8:
* Drop the sysfs interface patches and related documentation changes.
Changes from v7:
* Fix kernel crash with demotion.
* Improve documentation.
Changes from v6:
* Drop the usage of rank.
* Address other review feedback.
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 (9):
mm/demotion: Add support for explicit memory tiers mm/demotion: Move memory demotion related code mm/demotion: Add hotplug callbacks to handle new numa node onlined mm/demotion/dax/kmem: Set node's abstract distance to
MEMTIER_DEFAULT_DAX_ADISTANCE
mm/demotion: Build demotion targets based on explicit memory tiers mm/demotion: Add pg_data_t member to track node memory tier details mm/demotion: Drop memtier from memtype
mm/demotion: Update node_is_toptier to work with memory tiers lib/nodemask: Optimize node_random for nodemask with single NUMA node
Jagdish Gediya (1):
mm/demotion: Demote pages according to allocation fallback order
drivers/dax/kmem.c | 42 ++- include/linux/memory-tiers.h | 102 ++++++ include/linux/migrate.h | 15 - include/linux/mmzone.h | 3 + include/linux/node.h | 5 - include/linux/nodemask.h | 15 +- mm/Makefile | 1 + mm/huge_memory.c | 1 + mm/memory-tiers.c | 645 +++++++++++++++++++++++++++++++++++ mm/migrate.c | 453 +----------------------- mm/mprotect.c | 1 + mm/vmscan.c | 59 +++- mm/vmstat.c | 4 - 13 files changed, 849 insertions(+), 497 deletions(-) create mode 100644 include/linux/memory-tiers.h create mode 100644 mm/memory-tiers.c
Signature · 1 line
2.37.2
PATCH v15 1/10 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 highest tier, and builds the tier hierarchy by establishing the per-node demotion targets based on the distances between nodes.
This current memory tier kernel implementation 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-backed memory-only node on a virtual machine) that 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 nodes with shortest distance on the next lower tier as defined by the demotion path, not any other node from any lower tier. This strict, 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.
This patch series address the above by defining memory tiers explicitly.
Linux kernel presents memory devices as NUMA nodes and each memory device is of a specific type. The memory type of a device is represented by its abstract distance. A memory tier corresponds to a range of abstract distance. This allows for classifying memory devices with a specific performance range into a memory tier.
This patch configures the range/chunk size to be 128. The default DRAM abstract distance is 512. We can have 4 memory tiers below the default DRAM with abstract distance range 0 - 127, 127 - 255, 256- 383, 384 - 511. Faster memory devices can be placed in these faster(higher) memory tiers. Slower memory devices like persistent memory will have abstract distance higher than the default DRAM level.
--- include/linux/memory-tiers.h | 18 +++++ mm/Makefile | 1 + mm/memory-tiers.c | 129 +++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 include/linux/memory-tiers.h create mode 100644 mm/memory-tiers.c
include/linux/memory-tiers.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MEMORY_TIERS_H +#define _LINUX_MEMORY_TIERS_H + +/* +* Each tier cover a abstrace distance chunk size of 128 +*/ +#define MEMTIER_CHUNK_BITS 7 +#define MEMTIER_CHUNK_SIZE (1 << MEMTIER_CHUNK_BITS) +/* +* Smaller abstract distance values imply faster(higher) memory tiers. Offset +* the DRAM adistance so that we can accommodate devices with a slightly lower +* adistance value (slightly slower) than default DRAM adistance to be part of +* the same memory tier. +*/ +#define MEMTIER_ADISTANCE_DRAM ((4 * MEMTIER_CHUNK_SIZE) + (MEMTIER_CHUNK_SIZE >> 1)) + +#endif /* _LINUX_MEMORY_TIERS_H */
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_NUMA) += 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 +129 -0
129 lines not on this page · raw message
Signature · 1 line
2.37.2
Re: [PATCH v15 01/10] mm/demotion: Add support for explicit memory tiers
include/linux/memory-tiers.h line 13
Quoted 7 lines · Aneesh Kumar K.V
+ */
include/linux/memory-tiers.h new file +6
-0+#define MEMTIER_CHUNK_BITS 7 +#define MEMTIER_CHUNK_SIZE (1 << MEMTIER_CHUNK_BITS) +/* +* Smaller abstract distance values imply faster(higher) memory tiers. Offset +* the DRAM adistance so that we can accommodate devices with a slightly lower +* adistance value (slightly slower) than default DRAM adistance to be part of
(^^^ slightly faster)
Quoted 3 lines
+ * the same memory tier.
+ */
PATCH v15 2/10 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 | 8 +++++ include/linux/migrate.h | 2 -- mm/memory-tiers.c | 64 ++++++++++++++++++++++++++++++++++++ mm/migrate.c | 60 +-------------------------------- mm/vmscan.c | 1 + 5 files changed, 74 insertions(+), 61 deletions(-)
include/linux/memory-tiers.h +8 -0
@@ -15,4 +15,12 @@*/ #define MEMTIER_ADISTANCE_DRAM ((4 * MEMTIER_CHUNK_SIZE) + (MEMTIER_CHUNK_SIZE >> 1)) +#ifdef CONFIG_NUMA +#include <linux/types.h> +extern bool numa_demotion_enabled; + +#else + +#define numa_demotion_enabled false +#endif /* CONFIG_NUMA */ #endif /* _LINUX_MEMORY_TIERS_H */
include/linux/migrate.h +0 -2
@@ -103,7 +103,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 ) {}
@@ -112,7 +111,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 +64 -0
73 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
@@ -49,6 +49,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.37.2
PATCH v15 3/10 mm/demotion: Add hotplug callbacks to handle new numa node onlined
In reply to Aneesh Kumar K.VIf the new NUMA node onlined doesn't have a abstract distance assigned, the kernel adds the NUMA node to default memory tier.
--- include/linux/memory-tiers.h | 1 + mm/memory-tiers.c | 68 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+)
include/linux/memory-tiers.h +1 -0
@@ -14,6 +14,7 @@* the same memory tier. */ #define MEMTIER_ADISTANCE_DRAM ((4 * MEMTIER_CHUNK_SIZE) + (MEMTIER_CHUNK_SIZE >> 1)) +#define MEMTIER_HOTPLUG_PRIO 100#ifdef CONFIG_NUMA #include <linux/types.h>
mm/memory-tiers.c +68 -0
86 lines not on this page · raw message
Signature · 1 line
2.37.2
PATCH v15 4/10 mm/demotion/dax/kmem: Set node's abstract distance to MEMTIER_DEFAULT_DAX_ADISTANCE
In reply to Aneesh Kumar K.VBy default, all nodes are assigned to the default memory tier which is the memory tier designated for nodes with DRAM
Set dax kmem device node's tier to slower memory tier by assigning abstract distance to MEMTIER_DEFAULT_DAX_ADISTANCE. Low-level drivers like papr_scm or ACPI NFIT can initialize memory device type to a more accurate value based on device tree details or HMAT. If the kernel doesn't find the memory type initialized, a default slower memory type is assigned by the kmem driver.
--- drivers/dax/kmem.c | 42 +++++++++++++++-- include/linux/memory-tiers.h | 42 ++++++++++++++++- mm/memory-tiers.c | 91 +++++++++++++++++++++++++++--------- 3 files changed, 149 insertions(+), 26 deletions(-)
drivers/dax/kmem.c +39 -3
@@ -11,9 +11,17 @@#include <linux/fs.h> #include <linux/mm.h> #include <linux/mman.h> +#include <linux/memory-tiers.h> #include "dax-private.h" #include "bus.h" +/* +* Default abstract distance assigned to the NUMA node onlined +* by DAX/kmem if the low level platform driver didn't initialize +* one for this NUMA node. +*/ +#define MEMTIER_DEFAULT_DAX_ADISTANCE (MEMTIER_ADISTANCE_DRAM * 5) +/* Memory resource name used for add_memory_driver_managed(). */ static const char *kmem_name;/* Set if any memory will remain added when the driver will be unloaded. */
@@ -41,6 +49,7 @@ struct dax_kmem_data {struct resource *res[]; }; +static struct memory_dev_type *dax_slowmem_type;static int dev_dax_kmem_probe (struct dev_dax *dev_dax) {struct device *dev = &dev_dax->dev;
@@ -79,11 +88,13 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)return -EINVAL ; } +init_node_memory_type (numa_node, dax_slowmem_type); + + rc = -ENOMEM ; data =kzalloc (struct_size (data, res, dev_dax->nr_range),GFP_KERNEL );if (!data) -return -ENOMEM ; + goto err_dax_kmem_data; - rc = -ENOMEM ; data->res_name =kstrdup (dev_name (dev),GFP_KERNEL );if (!data->res_name) goto err_res_name;
@@ -155,6 +166,8 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)kfree (data->res_name); err_res_name:kfree (data); +err_dax_kmem_data: +clear_node_memory_type (numa_node, dax_slowmem_type);return rc; }
@@ -162,6 +175,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)static void dev_dax_kmem_remove (struct dev_dax *dev_dax) {int i, success =0 ; +int node = dev_dax->target_node;struct device *dev = &dev_dax->dev;struct dax_kmem_data *data =dev_get_drvdata (dev);
@@ -198,6 +212,14 @@ static void dev_dax_kmem_remove(struct dev_dax *dev_dax)kfree (data->res_name);kfree (data);dev_set_drvdata (dev,NULL ); +/* +* Clear the memtype association on successful unplug. +* If not, we have memory blocks left which can be +* offlined/onlined later. We need to keep memory_dev_type +* for that. This implies this reference will be around +* till next reboot. +*/ + clear_node_memory_type(node, dax_slowmem_type); } }#else
@@ -228,9 +250,22 @@ static int __init dax_kmem_init(void)if (!kmem_name)return -ENOMEM ; + dax_slowmem_type =alloc_memory_type (MEMTIER_DEFAULT_DAX_ADISTANCE ); +if (IS_ERR (dax_slowmem_type)) { + rc =PTR_ERR (dax_slowmem_type); + goto err_dax_slowmem_type; + } + rc =dax_driver_register (&device_dax_kmem_driver);if (rc) -kfree_const (kmem_name); + goto error_dax_driver; + +return rc; + +error_dax_driver: +destroy_memory_type (dax_slowmem_type); +err_dax_slowmem_type: +kfree_const (kmem_name);return rc; }
@@ -239,6 +274,7 @@ static void __exit dax_kmem_exit(void)dax_driver_unregister (&device_dax_kmem_driver);if (!any_hotremove_failed)kfree_const (kmem_name); +destroy_memory_type (dax_slowmem_type); }MODULE_AUTHOR ("Intel Corporation" );
include/linux/memory-tiers.h +41 -1
59 lines not on this page · raw message
mm/memory-tiers.c +69 -22
128 lines not on this page · raw message
Signature · 1 line
2.37.2
PATCH v15 5/10 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 memory tier 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.
--- include/linux/memory-tiers.h | 13 ++ include/linux/migrate.h | 13 -- mm/memory-tiers.c | 238 +++++++++++++++++++-- mm/migrate.c | 394 ----------------------------------- mm/vmstat.c | 4 - 5 files changed, 239 insertions(+), 423 deletions(-)
include/linux/memory-tiers.h +13 -0
@@ -37,6 +37,14 @@ struct memory_dev_type *alloc_memory_type(int adistance);void destroy_memory_type (struct memory_dev_type *memtype);void init_node_memory_type (int node,struct memory_dev_type *default_type);void clear_node_memory_type (int node,struct memory_dev_type *memtype); +#ifdef CONFIG_MIGRATION +int next_demotion_node (int node); +#else +static inline int next_demotion_node (int node) +{ +return NUMA_NO_NODE ; +} +#endif #else
@@ -63,5 +71,10 @@ static inline void clear_node_memory_type(int node, struct memory_dev_type *memt { } + +static inline int next_demotion_node (int node) +{ +return NUMA_NO_NODE ; +}#endif /* CONFIG_NUMA */ #endif /* _LINUX_MEMORY_TIERS_H */
include/linux/migrate.h +0 -13
@@ -100,19 +100,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 bool PageMovable (struct page *page);void __SetPageMovable (struct page *page,const struct movable_operations *ops);
mm/memory-tiers.c +226 -12
294 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.37.2
PATCH v15 6/10 mm/demotion: Add pg_data_t member to track node memory tier details
In reply to Aneesh Kumar K.VAlso update different helpes to use NODE_DATA()->memtier. Since node specific memtier can change based on the reassignment of NUMA node to a different memory tiers, accessing NODE_DATA()->memtier needs to happen under an rcu read lock or memory_tier_lock.
--- include/linux/mmzone.h | 3 +++ mm/memory-tiers.c | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-)
include/linux/mmzone.h +3 -0
@@ -1012,6 +1012,9 @@ typedef struct pglist_data {/* Per-node vmstats */ struct per_cpu_nodestat __percpu *per_cpu_nodestats;atomic_long_t vm_stat[NR_VM_NODE_STAT_ITEMS ]; +#ifdef CONFIG_NUMA +struct memory_tier __rcu *memtier; +#endif } pg_data_t;#define node_present_pages (nid) (NODE_DATA(nid)->node_present_pages)
mm/memory-tiers.c +35 -5
@@ -4,6 +4,7 @@#include <linux/sysfs.h> #include <linux/kobject.h> #include <linux/memory.h> +#include <linux/mmzone.h> #include <linux/memory-tiers.h> #include "internal.h"
@@ -136,12 +137,18 @@ static struct memory_tier *find_create_memory_tier(struct memory_dev_type *memtystatic struct memory_tier *__node_get_memory_tier (int node) { -struct memory_dev_type *memtype; +pg_data_t *pgdat; - memtype = node_memory_types[node]; -if (memtype &&node_isset (node, memtype->nodes)) -return memtype->memtier; -return NULL ; + pgdat =NODE_DATA (node); +if (!pgdat) +return NULL ; +/* +* Since we hold memory_tier_lock, we can avoid +* RCU read locks when accessing the details. No +* parallel updates are possible here. +*/ +return rcu_dereference_check (pgdat->memtier, +lockdep_is_held (&memory_tier_lock)); }#ifdef CONFIG_MIGRATION
@@ -294,6 +301,8 @@ static struct memory_tier *set_node_memory_tier(int node) {struct memory_tier *memtier;struct memory_dev_type *memtype; +pg_data_t *pgdat =NODE_DATA (node); +lockdep_assert_held_once (&memory_tier_lock);
@@ -305,24 +314,45 @@ static struct memory_tier *set_node_memory_tier(int node) memtype = node_memory_types[node];node_set (node, memtype->nodes); memtier =find_create_memory_tier (memtype); +if (!IS_ERR (memtier)) +rcu_assign_pointer (pgdat->memtier, memtier);return memtier; }static void destroy_memory_tier (struct memory_tier *memtier) {list_del (&memtier->list); +/* +* synchronize_rcu in clear_node_memory_tier makes sure +* we don't have rcu access to this memory tier. +*/ kfree (memtier); }static bool clear_node_memory_tier (int node) {bool cleared = false; +pg_data_t *pgdat;struct memory_tier *memtier; + pgdat =NODE_DATA (node); +if (!pgdat) +return false; + +/* +* Make sure that anybody looking at NODE_DATA who finds +* a valid memtier finds memory_dev_types with nodes still +* linked to the memtier. We achieve this by waiting for +* rcu read section to finish using synchronize_rcu. +* This also enables us to free the destroyed memory tier +* with kfree instead of kfree_rcu +*/ memtier =__node_get_memory_tier (node);if (memtier) {struct memory_dev_type *memtype; +rcu_assign_pointer (pgdat->memtier,NULL ); +synchronize_rcu (); memtype = node_memory_types[node];node_clear (node, memtype->nodes);if (nodes_empty (memtype->nodes)) {
Signature · 1 line
2.37.2
PATCH v15 7/10 mm/demotion: Drop memtier from memtype
In reply to Aneesh Kumar K.VNow that we track node-specific memtier in pg_data_t, we can drop memtier from memtype.
--- include/linux/memory-tiers.h | 1 - mm/memory-tiers.c | 16 +++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-)
include/linux/memory-tiers.h +0 -1
@@ -28,7 +28,6 @@ struct memory_dev_type {/* Nodes of same abstract distance */ nodemask_t nodes;struct kref kref; -struct memory_tier *memtier; };#ifdef CONFIG_NUMA
mm/memory-tiers.c +9 -7
@@ -100,17 +100,22 @@ static struct memory_tier *find_create_memory_tier(struct memory_dev_type *memtylockdep_assert_held_once (&memory_tier_lock); + adistance =round_down (adistance, memtier_adistance_chunk_size);/* * If the memtype is already part of a memory tier, * just return that. */ -if (memtype->memtier) -return memtype->memtier; +if (!list_empty (&memtype->tier_sibiling)) { +list_for_each_entry (memtier, &memory_tiers, list) { +if (adistance == memtier->adistance_start) +return memtier; + } +WARN_ON (1 ); +return ERR_PTR (-EINVAL ); + } - adistance =round_down (adistance, memtier_adistance_chunk_size);list_for_each_entry (memtier, &memory_tiers, list) {if (adistance == memtier->adistance_start) { - memtype->memtier = memtier;list_add (&memtype->tier_sibiling, &memtier->memory_types);return memtier; }else if (adistance < memtier->adistance_start) {
@@ -130,7 +135,6 @@ static struct memory_tier *find_create_memory_tier(struct memory_dev_type *memtylist_add_tail (&new_memtier->list, &memtier->list); else list_add_tail(&new_memtier->list, &memory_tiers); - memtype->memtier = new_memtier;list_add (&memtype->tier_sibiling, &new_memtier->memory_types);return new_memtier; }
@@ -357,7 +361,6 @@ static bool clear_node_memory_tier(int node)node_clear (node, memtype->nodes);if (nodes_empty (memtype->nodes)) {list_del (&memtype->tier_sibiling); - memtype->memtier =NULL ;if (list_empty (&memtier->memory_types))destroy_memory_tier (memtier); }
@@ -385,7 +388,6 @@ struct memory_dev_type *alloc_memory_type(int adistance) memtype->adistance = adistance;INIT_LIST_HEAD (&memtype->tier_sibiling); memtype->nodes =NODE_MASK_NONE ; - memtype->memtier =NULL ;kref_init (&memtype->kref);return memtype; }
Signature · 1 line
2.37.2
PATCH v15 8/10 mm/demotion: Demote pages according to allocation fallback order
In reply to Aneesh Kumar K.VFrom: Jagdish Gediya <[email protected]>
Currently, a higher tier node can only be demoted to selected nodes on the next lower tier as defined by the demotion path. This strict 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 currently.
This patch adds support to get all the allowed demotion targets for a memory tier. demote_page_list() function is now modified to utilize this allowed node mask as the fallback allocation mask.
--- include/linux/memory-tiers.h | 12 ++++++++ mm/memory-tiers.c | 51 +++++++++++++++++++++++++++++-- mm/vmscan.c | 58 ++++++++++++++++++++++++++---------- 3 files changed, 103 insertions(+), 18 deletions(-)
include/linux/memory-tiers.h +12 -0
@@ -5,6 +5,7 @@#include <linux/types.h> #include <linux/nodemask.h> #include <linux/kref.h> +#include <linux/mmzone.h> /* * Each tier cover a abstrace distance chunk size of 128 */
@@ -38,11 +39,17 @@ void init_node_memory_type(int node, struct memory_dev_type *default_type);void clear_node_memory_type (int node,struct memory_dev_type *memtype);#ifdef CONFIG_MIGRATION int next_demotion_node (int node); +void node_get_allowed_targets (pg_data_t *pgdat,nodemask_t *targets);#else static inline int next_demotion_node (int node) {return NUMA_NO_NODE ; } + +static inline void node_get_allowed_targets (pg_data_t *pgdat,nodemask_t *targets) +{ + *targets =NODE_MASK_NONE ; +}#endif #else
@@ -75,5 +82,10 @@ static inline int next_demotion_node(int node) {return NUMA_NO_NODE ; } + +static inline void node_get_allowed_targets (pg_data_t *pgdat,nodemask_t *targets) +{ + *targets =NODE_MASK_NONE ; +}#endif /* CONFIG_NUMA */ #endif /* _LINUX_MEMORY_TIERS_H */
mm/memory-tiers.c +48 -3
90 lines not on this page · raw message
mm/vmscan.c +43 -15
80 lines not on this page · raw message
Signature · 1 line
2.37.2
PATCH v15 9/10 mm/demotion: Update node_is_toptier to work with memory tiers
In reply to Aneesh Kumar K.VWith memory tier support we can have memory only NUMA nodes in the top tier from which we want to avoid promotion tracking NUMA faults. Update node_is_toptier to work with memory tiers. All NUMA nodes are by default top tier nodes. With lower(slower) memory tiers added we consider all memory tiers above a memory tier having CPU NUMA nodes as a top memory tier
--- include/linux/memory-tiers.h | 11 +++++++++ include/linux/node.h | 5 ---- mm/huge_memory.c | 1 + mm/memory-tiers.c | 46 ++++++++++++++++++++++++++++++++++++ mm/migrate.c | 1 + mm/mprotect.c | 1 + 6 files changed, 60 insertions(+), 5 deletions(-)
include/linux/memory-tiers.h +11 -0
@@ -40,6 +40,7 @@ void clear_node_memory_type(int node, struct memory_dev_type *memtype);#ifdef CONFIG_MIGRATION int next_demotion_node (int node);void node_get_allowed_targets (pg_data_t *pgdat,nodemask_t *targets); +bool node_is_toptier (int node);#else static inline int next_demotion_node (int node) {
@@ -50,6 +51,11 @@ static inline void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *target { *targets =NODE_MASK_NONE ; } + +static inline bool node_is_toptier (int node) +{ +return true; +}#endif #else
@@ -87,5 +93,10 @@ static inline void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *target { *targets =NODE_MASK_NONE ; } + +static inline bool node_is_toptier (int node) +{ +return true; +}#endif /* CONFIG_NUMA */ #endif /* _LINUX_MEMORY_TIERS_H */
include/linux/node.h +0 -5
@@ -185,9 +185,4 @@ static inline void register_hugetlbfs_with_node(node_registration_func_t reg,#define to_node (device) container_of(device, struct node, dev) -static inline bool node_is_toptier (int node) -{ -return node_state (node,N_CPU ); -} -#endif /* _LINUX_NODE_H_ */
mm/huge_memory.c +1 -0
@@ -36,6 +36,7 @@#include <linux/numa.h> #include <linux/page_owner.h> #include <linux/sched/sysctl.h> +#include <linux/memory-tiers.h> #include <asm/tlb.h> #include <asm/pgalloc.h>
mm/memory-tiers.c +46 -0
64 lines not on this page · raw message
mm/migrate.c +1 -0
@@ -50,6 +50,7 @@#include <linux/memory.h> #include <linux/random.h> #include <linux/sched/sysctl.h> +#include <linux/memory-tiers.h> #include <asm/tlbflush.h>
mm/mprotect.c +1 -0
@@ -31,6 +31,7 @@#include <linux/pgtable.h> #include <linux/sched/sysctl.h> #include <linux/userfaultfd_k.h> +#include <linux/memory-tiers.h> #include <asm/cacheflush.h> #include <asm/mmu_context.h> #include <asm/tlbflush.h>
Signature · 1 line
2.37.2
PATCH v15 10/10 lib/nodemask: Optimize node_random for nodemask with single NUMA node
In reply to Aneesh Kumar K.VThe most common case for certain node_random usage (demotion nodemask) is with nodemask weight 1. We can avoid calling get_random_init() in that case and always return the only node set in the nodemask.
A simple test as below
before = rdtsc_ordered();
for (i= 0; i < 100; i++) {
rand = node_random(&nmask); }
after = rdtsc_ordered();
Without fix after - before : 16438
With fix after - before : 816
--- include/linux/nodemask.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
include/linux/nodemask.h +12 -3
@@ -504,12 +504,21 @@ static inline int num_node_state(enum node_states state)static inline int node_random (const nodemask_t *maskp) {#if defined(CONFIG_NUMA ) && (MAX_NUMNODES >1 ) -int w, bit =NUMA_NO_NODE ; +int w, bit; w =nodes_weight (*maskp); -if (w) +switch (w) { +case 0 : + bit =NUMA_NO_NODE ; +break ; +case 1 : + bit =first_node (*maskp); +break ; +default : bit =bitmap_ord_to_pos (maskp->bits, -get_random_int () % w,MAX_NUMNODES ); +get_random_int () % w,MAX_NUMNODES ); +break ; + }return bit;#else return 0 ;
Signature · 1 line
2.37.2
Yury Norov
maintainerRe: [PATCH v15 10/10] lib/nodemask: Optimize node_random for nodemask with single NUMA node
Hi Aneesh,
Please CC maintainers in your recipient list.
Quoted 3 lines · Aneesh Kumar K.V
The most common case for certain
node_randomusage (demotion nodemask) is with nodemask weight 1. We can avoid callingget_random_init()in that case and always return the only node set in the nodemask.
Can you move the comment about get_random_int() to the code?.
Quoted 23 lines
A simple test as below
before =rdtsc_ordered();for (i= 0; i < 100; i++) { rand = node_random(&nmask);}
after =rdtsc_ordered();Without fix after - before : 16438
With fix after - before : 816Reviewed-by "Huang, Ying" <[email protected]>Signed-off-by Aneesh Kumar K.V <[email protected]>--- include/linux/nodemask.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)include/linux/nodemask.h +0
-0@@ -504,12 +504,21 @@ static inline int num_node_state(enum node_states state)static inline int node_random (const nodemask_t *maskp) {
include/linux/nodemask.h static inline int num_node_state(enum node_states state) · line 513
Quoted 10 lines
include/linux/nodemask.h +5
-2#if defined(CONFIG_NUMA ) && (MAX_NUMNODES >1 ) -int w, bit =NUMA_NO_NODE ; +int w, bit; w =nodes_weight (*maskp); -if (w) +switch (w) { +case 0 : + bit =NUMA_NO_NODE ; +break ;
Why not 'return NUMA_NO_NODE' instead of break thing?
include/linux/nodemask.h static inline int num_node_state(enum node_states state) · line 519
Quoted 7 lines
include/linux/nodemask.h +5
-1+case 1 : + bit =first_node (*maskp); +break ; +default : bit =bitmap_ord_to_pos (maskp->bits, -get_random_int () % w,MAX_NUMNODES ); +get_random_int () % w,MAX_NUMNODES );
Don't fix tabs - it trashes the history.
Quoted 9 lines
include/linux/nodemask.h +2
-0+ break; + }return bit;#else return 0 ;Signature · 1 line
2.37.2
Quoted 60 lines · 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 highest 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 implementation 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-backed memory-only node on a virtual machine) and that 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 nodes with shortest distance on the next lower tier as defined by the demotion path, not any other node from any lower tier. This strict, 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.
This patch series make the creation of memory tiers explicit under the control of device driver.
Memory Tier Initialization
==========================Linux kernel presents memory devices as NUMA nodes and each memory device is of a specific type. The memory type of a device is represented by its abstract distance. A memory tier corresponds to a range of abstract distance. This allows for classifying memory devices with a specific performance range into a memory tier.
By default, all memory nodes are assigned to the default tier with abstract distance 512.
Quoted 7 lines
A device driver can move its memory nodes from the default tier. For example, PMEM can move its memory nodes below the default tier, whereas GPU can move 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.
I gave this patchset a quick try on two setups:
1. With QEMU, when an nvdimm device is bound to dax kmem driver, I can see the memory node with pmem getting into a lower tier than DRAM.
2. In an experimental CXL setup that has DRAM as part of CXL memory, I see that CXL memory node falls into the same tier as the regular DRAM tier. This is expected for now since there is no code (in low level ACPI driver?) yet to map the latency or bandwidth info (when available from firmware) into an abstract distance value, and register a memory type for the same. Guess these bits can be covered as part of future enhancements.
Regards,
Bharata.
Andrew Morton
maintainerQuoted 9 lines · On Fri, 19 Aug 2022 11:57:18 +0530 Bharata B Rao <[email protected]> wrote:
Quoted 3 lines
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.
I gave this patchset a quick try on two setups:
1. With QEMU, when an nvdimm device is bound to dax kmem driver, I can see the memory node with pmem getting into a lower tier than DRAM.
Quoted 6 lines
2. In an experimental CXL setup that has DRAM as part of CXL memory, I see that CXL memory node falls into the same tier as the regular DRAM tier. This is expected for now since there is no code (in low level ACPI driver?) yet to map the latency or bandwidth info (when available from firmware) into an abstract distance value, and register a memory type for the same. Guess these bits can be covered as part of future enhancements.
Should I add your Tested-by:?
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Fri, 19 Aug 2022 11:57:18 +0530 Bharata B Rao <[email protected]> wrote:
Quoted 3 lines
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.
I gave this patchset a quick try on two setups:
1. With QEMU, when an nvdimm device is bound to dax kmem driver, I can see the memory node with pmem getting into a lower tier than DRAM.
Quoted 8 lines
Quoted 6 lines
2. In an experimental CXL setup that has DRAM as part of CXL memory, I see that CXL memory node falls into the same tier as the regular DRAM tier. This is expected for now since there is no code (in low level ACPI driver?) yet to map the latency or bandwidth info (when available from firmware) into an abstract distance value, and register a memory type for the same. Guess these bits can be covered as part of future enhancements.
Should I add your Tested-by:?
May be not. I have done only a very minimal testing of specific scenarios as mentioned above. Thanks for checking.
Regards,
Bharata.
Quoted 178 lines · 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 highest 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 implementation 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-backed memory-only node on a virtual machine) and that 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 nodes with shortest distance on the next lower tier as defined by the demotion path, not any other node from any lower tier. This strict, 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.
This patch series make the creation of memory tiers explicit under the control of device driver.
Memory Tier Initialization
==========================Linux kernel presents memory devices as NUMA nodes and each memory device is of a specific type. The memory type of a device is represented by its abstract distance. A memory tier corresponds to a range of abstract distance. This allows for classifying memory devices with a specific performance range into a memory tier.
By default, all memory nodes are assigned to the default tier with abstract distance 512.
A device driver can move its memory nodes from the default tier. For example, PMEM can move its memory nodes below the default tier, whereas GPU can move 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.
Changes from v14
* Add Reviewed-by:
* Address review feedback w.r.t default adistance valueChanges from v13
* Address review feedback.
* Add path dropping memtier from structmemory_dev_typeChanges from v12
* Fix kernel crash on module unload
* Address review feedback.
* Addnode_randompatch to this series based on review feedbackChanges from v11:
* smaller abstract distance imply faster(higher) memory tier.Changes from v10:
* rename performance level to abstract distance
* Thanks to all the good feedback from Huang, Ying <[email protected]>. Updated the patchset to cover most of the review feedback.Changes from v9:
* Use performance level for initializing memory tiers.Changes from v8:
* Drop the sysfs interface patches and related documentation changes.Changes from v7:
* Fix kernel crash with demotion.
* Improve documentation.Changes from v6:
* Drop the usage of rank.
* Address other review feedback.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 (9):
mm/demotion: Add support for explicit memory tiers mm/demotion: Move memory demotion related code mm/demotion: Add hotplug callbacks to handle new numa node onlinedmm/demotion/dax/kmem: Set node's abstract distance to
MEMTIER_DEFAULT_DAX_ADISTANCE
mm/demotion: Build demotion targets based on explicit memory tiers mm/demotion: Addpg_data_tmember to track node memory tier details mm/demotion: Drop memtier from memtype
mm/demotion: Updatenode_is_toptierto work with memory tiers lib/nodemask: Optimizenode_randomfor nodemask with single NUMA nodeJagdish Gediya (1):
mm/demotion: Demote pages according to allocation fallback orderdrivers/dax/kmem.c | 42 ++- include/linux/memory-tiers.h | 102 ++++++ include/linux/migrate.h | 15 - include/linux/mmzone.h | 3 + include/linux/node.h | 5 - include/linux/nodemask.h | 15 +- mm/Makefile | 1 + mm/huge_memory.c | 1 + mm/memory-tiers.c | 645 +++++++++++++++++++++++++++++++++++ mm/migrate.c | 453 +----------------------- mm/mprotect.c | 1 + mm/vmscan.c | 59 +++- mm/vmstat.c | 4 - 13 files changed, 849 insertions(+), 497 deletions(-) create mode 100644 include/linux/memory-tiers.h create mode 100644 mm/memory-tiers.c--
2.37.2
Andrew Morton
maintainerQuoted 2 lines · On Thu, 18 Aug 2022 18:40:32 +0530 "Aneesh Kumar K.V"…
This patch series make the creation of memory tiers explicit under the control of device driver.
This series has been in mm-unstable for nearly four weeks and everything has died down, so I'm planning on moving it into mm-stable late this week unless someone stops me...