Skip to contentSkip to conversation

mm: proactive reclaim and memory tiering topics

RFC PATCH In review Davidlohr Bueso 10 participants 25 messages Apr 16, 2022 – Jun 1, 2022

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover mm: proactive reclaim and memory tiering topics

Hello,

With the increasing popularity of memory tiering, the idea of this series is to trigger some discussion around David's[1] system-wide proactive reclaim beyond memcg[2] as well as sysfs as the interface for exporting system-wide tiering information[2]. I am hoping this can be discussed at LSFMM, and while I know many are interested in tiering subjects in general, I have not seen anyone bring this up in the list.

There has been some initial discussion towards the need to expose system-wide tiering information to userspace. I thought I'd start with two sysfs files as a node attribute that exports the demotion_node as well as whether or not the node is fast memory. This was considered (and I agree) better than a new /sys/devices/system/tier/tierN/ interface. So, are we going to go this route? If so, what further information is useful for users? Does having instead a /sys/devices/system/node/nodeN/reclaim/ make sense?

Applies against Linus' current tree and has only been _gently_ tested.

Thanks!

Davidlohr Bueso (6):
drivers/base/node: cleanup register_node() mm/vmscan: use node_is_toptier helper in node_reclaim
mm: make __node_reclaim() more flexible
mm: introduce per-node proactive reclaim interface mm/migration: export demotion_path of a node via sysfs mm/migrate: export whether or not tier is toptier in sysfs

 Documentation/ABI/stable/sysfs-devices-node |  22 ++++
 drivers/base/node.c                         |  68 ++++++++++--
 include/linux/migrate.h                     |  15 +++
 include/linux/swap.h                        |  16 +++
 include/trace/events/vmscan.h               |  12 +--
 mm/migrate.c                                |  15 +--
 mm/vmscan.c                                 | 108 +++++++++++++++-----
 7 files changed, 206 insertions(+), 50 deletions(-)

--
2.26.2

#1 lore raw

PATCH 1/6 drivers/base/node: cleanup register_node()

Trivially get rid of some unnecessary indentation.

---
 drivers/base/node.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/base/node.c +7 -7
index ec8bb24a5a22..6cdf25fd26c3 100644
@@ -661,16 +661,16 @@ static int register_node(struct node *node, int num)
 	node->dev.bus = &node_subsys;
 	node->dev.release = node_device_release;
 	node->dev.groups = node_dev_groups;
-	error = device_register(&node->dev);
 
-	if (error)
+	error = device_register(&node->dev);
+	if (error) {
 		put_device(&node->dev);
-	else {
-		hugetlb_register_node(node);
-
-		compaction_register_node(node);
+		return error;
 	}
-	return error;
+
+	hugetlb_register_node(node);
+	compaction_register_node(node);
+	return 0;
 }
 
 /**
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 1/6] drivers/base/node: cleanup register_node()

Quoted 38 lines · Davidlohr Bueso

Trivially get rid of some unnecessary indentation.

---
 drivers/base/node.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/base/node.c +7 -7
index ec8bb24a5a22..6cdf25fd26c3 100644
@@ -661,16 +661,16 @@ static int register_node(struct node *node, int num)
 	node->dev.bus = &node_subsys;
 	node->dev.release = node_device_release;
 	node->dev.groups = node_dev_groups;
-	error = device_register(&node->dev);
 
-	if (error)
+	error = device_register(&node->dev);
+	if (error) {
 		put_device(&node->dev);
-	else {
-		hugetlb_register_node(node);
-
-		compaction_register_node(node);
+		return error;
 	}
-	return error;
+
+	hugetlb_register_node(node);
+	compaction_register_node(node);
+	return 0;
 }
 
 /**
Signature · 1 line
2.26.2

Looks good.

Reviewed by: Adam Manzanares <[email protected]>

Reviewed-by on v1 1/6
#3 lore raw

Re: [PATCH 1/6] drivers/base/node: cleanup register_node()

In reply to Davidlohr Bueso
Quoted 25 lines · Davidlohr Bueso

Trivially get rid of some unnecessary indentation.

---
 drivers/base/node.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/base/node.c +2 -6
index ec8bb24a5a22..6cdf25fd26c3 100644
@@ -661,16 +661,16 @@ static int register_node(struct node *node, int num)
 	node->dev.bus = &node_subsys;
 	node->dev.release = node_device_release;
 	node->dev.groups = node_dev_groups;
-	error = device_register(&node->dev);
 
-	if (error)
+	error = device_register(&node->dev);
+	if (error) {
 		put_device(&node->dev);
-	else {
-		hugetlb_register_node(node);
-
-		compaction_register_node(node);

drivers/base/node.c static int register_node(struct node *node, int num) · line 676

Quoted 10 lines
drivers/base/node.c +5 -1
+		return error;
 	}
-	return error;
+
+	hugetlb_register_node(node);
+	compaction_register_node(node);
+	return 0;
 }
 
 /**
Signature · 3 lines
Thanks,

David / dhildenb
#5 lore raw

PATCH 2/6 mm/vmscan: use node_is_toptier helper in node_reclaim

In reply to Davidlohr Bueso

We have helpers for a reason.

---
 mm/vmscan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c +1 -1
index 1678802e03e7..cb583fcbf5bf 100644
@@ -4750,7 +4750,7 @@ int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
 	 * over remote processors and spread off node memory allocations
 	 * as wide as possible.
 	 */
-	if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
+	if (node_is_toptier(pgdat->node_id) && pgdat->node_id != numa_node_id())
 		return NODE_RECLAIM_NOSCAN;
 
 	if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
Signature · 1 line
2.26.2
#6 lore raw

Re: [PATCH 2/6] mm/vmscan: use node_is_toptier helper in node_reclaim

Quoted 23 lines · Davidlohr Bueso

We have helpers for a reason.

---
 mm/vmscan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c +1 -1
index 1678802e03e7..cb583fcbf5bf 100644
@@ -4750,7 +4750,7 @@ int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
 	 * over remote processors and spread off node memory allocations
 	 * as wide as possible.
 	 */
-	if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
+	if (node_is_toptier(pgdat->node_id) && pgdat->node_id != numa_node_id())
 		return NODE_RECLAIM_NOSCAN;
 
 	if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
Signature · 1 line
2.26.2

Looks good.

Reviewed by: Adam Manzanares <[email protected]>

#8 lore raw

Re: [PATCH 2/6] mm/vmscan: use node_is_toptier helper in node_reclaim

In reply to Davidlohr Bueso
Quoted 12 lines · Davidlohr Bueso

We have helpers for a reason.

---
 mm/vmscan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c +0 -0
index 1678802e03e7..cb583fcbf5bf 100644

mm/vmscan.c int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) · line 4753

Quoted 5 lines
mm/vmscan.c +1 -1
@@ -4750,7 +4750,7 @@ int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
 	 * over remote processors and spread off node memory allocations
 	 * as wide as possible.
 	 */
-	if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
+	if (node_is_toptier(pgdat->node_id) && pgdat->node_id != numa_node_id())

Currently node_is_toptier returns all N_CPU node as toptier but N_CPU node will not stay necessarily top tier as per discussions on below thread.

https://lore.kernel.org/linux-mm/CAAPL-u9sVx94ACSuCVN8V0tKp+AMxiY89cro0japtyB=xNfNBw@mail.gmail.com/

node_is_toptier() definition can change based on the discussion in above thread.

Quoted 7 lines
mm/vmscan.c +0 -0
 		return NODE_RECLAIM_NOSCAN;
 
 	if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
Signature · 1 line
2.26.2
#9 lore raw

Re: [PATCH 2/6] mm/vmscan: use node_is_toptier helper in node_reclaim

In reply to Davidlohr Bueso
Quoted 12 lines · Davidlohr Bueso

We have helpers for a reason.

---
 mm/vmscan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c +0 -0
index 1678802e03e7..cb583fcbf5bf 100644

mm/vmscan.c int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) · line 4756

Quoted 8 lines
mm/vmscan.c +1 -1
@@ -4750,7 +4750,7 @@ int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
 	 * over remote processors and spread off node memory allocations
 	 * as wide as possible.
 	 */
-	if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
+	if (node_is_toptier(pgdat->node_id) && pgdat->node_id != numa_node_id())
 		return NODE_RECLAIM_NOSCAN;
 
 	if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))

Are we really looking at the top tier in a tiered memory hierarchy here? The comment seems to suggest we are looking at local NUMA node?

-aneesh

#10 lore raw

Re: [PATCH 2/6] mm/vmscan: use node_is_toptier helper in node_reclaim

Quoted 22 lines · Aneesh Kumar K.V
Quoted 20 lines · Davidlohr Bueso

We have helpers for a reason.

---
 mm/vmscan.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c +1 -1
index 1678802e03e7..cb583fcbf5bf 100644
@@ -4750,7 +4750,7 @@ int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
  	 * over remote processors and spread off node memory allocations
  	 * as wide as possible.
  	 */
-	if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
+	if (node_is_toptier(pgdat->node_id) && pgdat->node_id != numa_node_id())
  		return NODE_RECLAIM_NOSCAN;

Quoted 5 lines
Quoted 1 line

  if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))

Are we really looking at the top tier in a tiered memory hierarchy here? The comment seems to suggest we are looking at local NUMA node?

The code change itself is correct. But it is an implementation details that node_is_toptier() == node_state(, N_CPU). And after we supporting more memory tiers (like GPU, HBM), we will change the implementation of
node_is_toptier() soon. So I think that it's better to keep the
original code.

Best Regards,
Huang, Ying

#11 lore raw

Re: [PATCH 2/6] mm/vmscan: use node_is_toptier helper in node_reclaim

Quoted 29 lines · Ying Huang
Quoted 22 lines · Aneesh Kumar K.V
Quoted 20 lines · Davidlohr Bueso

We have helpers for a reason.

---
 mm/vmscan.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c +1 -1
index 1678802e03e7..cb583fcbf5bf 100644
@@ -4750,7 +4750,7 @@ int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
  	 * over remote processors and spread off node memory allocations
  	 * as wide as possible.
  	 */
-	if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
+	if (node_is_toptier(pgdat->node_id) && pgdat->node_id != numa_node_id())
  		return NODE_RECLAIM_NOSCAN;
 
 
Quoted 5 lines
Quoted 1 line

  if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))

Are we really looking at the top tier in a tiered memory hierarchy here? The comment seems to suggest we are looking at local NUMA node?

Quoted 5 lines

The code change itself is correct. But it is an implementation details that node_is_toptier() == node_state(, N_CPU). And after we supporting more memory tiers (like GPU, HBM), we will change the implementation of
node_is_toptier() soon. So I think that it's better to keep the
original code.

Agreed.

Thanks,
Davidlohr

#12 lore raw

PATCH 3/6 mm: make __node_reclaim() more flexible

In reply to Davidlohr Bueso

Currently __node_reclaim() is tailored to the allocator paths. With proactive per-node reclaim it requires a bit more flexibly:

- Deal in terms of nr_pages instead of order. Similarly this also applies to the respective tracing.
- Make the caller pass an already armed scan control.
- Return number of reclaimed pages. The caller can trivially check against this explicitly instead.

The current node_reclaim() interface remains the same.

---
 include/trace/events/vmscan.h | 12 ++++-----
 mm/vmscan.c                   | 47 +++++++++++++++++++----------------
 2 files changed, 31 insertions(+), 28 deletions(-)
include/trace/events/vmscan.h +6 -6
index de136dbd623a..ab6ce8d8770b 100644
@@ -439,25 +439,25 @@ TRACE_EVENT(mm_vmscan_lru_shrink_active,
 
 TRACE_EVENT(mm_vmscan_node_reclaim_begin,
 
-	TP_PROTO(int nid, int order, gfp_t gfp_flags),
+	TP_PROTO(int nid, unsigned long nr_pages, gfp_t gfp_flags),
 
-	TP_ARGS(nid, order, gfp_flags),
+	TP_ARGS(nid, nr_pages, gfp_flags),
 
 	TP_STRUCT__entry(
 		__field(int, nid)
-		__field(int, order)
+		__field(int, nr_pages)
 		__field(gfp_t, gfp_flags)
 	),
 
 	TP_fast_assign(
 		__entry->nid = nid;
-		__entry->order = order;
+		__entry->nr_pages = nr_pages;
 		__entry->gfp_flags = gfp_flags;
 	),
 
-	TP_printk("nid=%d order=%d gfp_flags=%s",
+	TP_printk("nid=%d nr_pages=%d gfp_flags=%s",
 		__entry->nid,
-		__entry->order,
+		__entry->nr_pages,
 		show_gfp_flags(__entry->gfp_flags))
 );
 
mm/vmscan.c +25 -22

91 lines not on this page · raw message

Signature · 1 line
2.26.2
#13 lore raw

PATCH 4/6 mm: introduce per-node proactive reclaim interface

In reply to Davidlohr Bueso

This patch introduces a mechanism to trigger memory reclaim as a per-node sysfs interface, inspired by compaction's equivalent; ie:

echo 1G > /sys/devices/system/node/nodeX/reclaim

It is based on the discussions from David's thread[1] as well as the current upstreaming of the memcg[2] interface (which has nice explanations for the benefits of userspace reclaim overall). In both cases conclusions were that either way of inducing proactive reclaim should be KISS, and can be later extended. So this patch does not allow the user much fine tuning beyond the size of the reclaim, such as anon/file or whether or semantics of demotion.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

---
 Documentation/ABI/stable/sysfs-devices-node | 10 ++++
 drivers/base/node.c                         |  2 +
 include/linux/swap.h                        | 16 ++++++
 mm/vmscan.c                                 | 59 +++++++++++++++++++++
 4 files changed, 87 insertions(+)
Documentation/ABI/stable/sysfs-devices-node +10 -0
index 8db67aa472f1..3c935e1334f7 100644
@@ -182,3 +182,13 @@ Date:		November 2021
 Contact:	Jarkko Sakkinen <[email protected]>
 Description:
 		The total amount of SGX physical memory in bytes.
+
+What:		/sys/devices/system/node/nodeX/reclaim
+Date:		April 2022
+Contact:	Davidlohr Bueso <[email protected]>
+Description:
+		Write the amount of bytes to induce memory reclaim in this node.
+		This file accepts a single key, the number of bytes to reclaim.
+		When it completes successfully, the specified amount or more memory
+		will have been reclaimed, and -EAGAIN if less bytes are reclaimed
+		than the specified amount.
drivers/base/node.c +2 -0
index 6cdf25fd26c3..d80c478e2a6e 100644
@@ -670,6 +670,7 @@ static int register_node(struct node *node, int num)
 
 	hugetlb_register_node(node);
 	compaction_register_node(node);
+	reclaim_register_node(node);
 	return 0;
 }
 
@@ -685,6 +686,7 @@ void unregister_node(struct node *node)
 	hugetlb_unregister_node(node);		/* no-op, if memoryless node */
 	node_remove_accesses(node);
 	node_remove_caches(node);
+	reclaim_unregister_node(node);
 	device_unregister(&node->dev);
 }
 
include/linux/swap.h +16 -0
index 27093b477c5f..cca43ae6d770 100644
@@ -398,6 +398,22 @@ extern unsigned long shrink_all_memory(unsigned long nr_pages);
 extern int vm_swappiness;
 long remove_mapping(struct address_space *mapping, struct folio *folio);
 
+#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
+extern int reclaim_register_node(struct node *node);
+extern void reclaim_unregister_node(struct node *node);
+
+#else
+
+static inline int reclaim_register_node(struct node *node)
+{
+	return 0;
+}
+
+static inline void reclaim_unregister_node(struct node *node)
+{
+}
+#endif /* CONFIG_SYSFS && CONFIG_NUMA */
+
 extern unsigned long reclaim_pages(struct list_head *page_list);
 #ifdef CONFIG_NUMA
 extern int node_reclaim_mode;
mm/vmscan.c +59 -0

62 lines not on this page · raw message

Signature · 1 line
2.26.2
#14 lore raw

Re: [PATCH 4/6] mm: introduce per-node proactive reclaim interface

Quoted 6 lines · Davidlohr Bueso

This patch introduces a mechanism to trigger memory reclaim as a per-node sysfs interface, inspired by compaction's equivalent; ie:

echo 1G > /sys/devices/system/node/nodeX/reclaim

I think it will be more flexible to specify a node mask as a parameter along with amount of memory with the memory.reclaim memcg interface proposed by Yosry. Doing it node by node is more cumbersome. It is just a special case of reclaiming from root cgroup for a specific node.

Wei Gu, YIng and I have some discssions on this
https://lore.kernel.org/all/[email protected]/

Tim

Quoted 152 lines

It is based on the discussions from David's thread[1] as well as the current upstreaming of the memcg[2] interface (which has nice explanations for the benefits of userspace reclaim overall). In both cases conclusions were that either way of inducing proactive reclaim should be KISS, and can be later extended. So this patch does not allow the user much fine tuning beyond the size of the reclaim, such as anon/file or whether or semantics of demotion.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

---
 Documentation/ABI/stable/sysfs-devices-node | 10 ++++
 drivers/base/node.c                         |  2 +
 include/linux/swap.h                        | 16 ++++++
 mm/vmscan.c                                 | 59 +++++++++++++++++++++
 4 files changed, 87 insertions(+)
Documentation/ABI/stable/sysfs-devices-node +10 -0
index 8db67aa472f1..3c935e1334f7 100644
@@ -182,3 +182,13 @@ Date:		November 2021
 Contact:	Jarkko Sakkinen <[email protected]>
 Description:
 		The total amount of SGX physical memory in bytes.
+
+What:		/sys/devices/system/node/nodeX/reclaim
+Date:		April 2022
+Contact:	Davidlohr Bueso <[email protected]>
+Description:
+		Write the amount of bytes to induce memory reclaim in this node.
+		This file accepts a single key, the number of bytes to reclaim.
+		When it completes successfully, the specified amount or more memory
+		will have been reclaimed, and -EAGAIN if less bytes are reclaimed
+		than the specified amount.
drivers/base/node.c +2 -0
index 6cdf25fd26c3..d80c478e2a6e 100644
@@ -670,6 +670,7 @@ static int register_node(struct node *node, int num)
 
 	hugetlb_register_node(node);
 	compaction_register_node(node);
+	reclaim_register_node(node);
 	return 0;
 }
 
@@ -685,6 +686,7 @@ void unregister_node(struct node *node)
 	hugetlb_unregister_node(node);		/* no-op, if memoryless node */
 	node_remove_accesses(node);
 	node_remove_caches(node);
+	reclaim_unregister_node(node);
 	device_unregister(&node->dev);
 }
 
include/linux/swap.h +16 -0
index 27093b477c5f..cca43ae6d770 100644
@@ -398,6 +398,22 @@ extern unsigned long shrink_all_memory(unsigned long nr_pages);
 extern int vm_swappiness;
 long remove_mapping(struct address_space *mapping, struct folio *folio);
 
+#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
+extern int reclaim_register_node(struct node *node);
+extern void reclaim_unregister_node(struct node *node);
+
+#else
+
+static inline int reclaim_register_node(struct node *node)
+{
+	return 0;
+}
+
+static inline void reclaim_unregister_node(struct node *node)
+{
+}
+#endif /* CONFIG_SYSFS && CONFIG_NUMA */
+
 extern unsigned long reclaim_pages(struct list_head *page_list);
 #ifdef CONFIG_NUMA
 extern int node_reclaim_mode;
mm/vmscan.c +59 -0
index 1735c302831c..3539f8a0f0ea 100644
@@ -4819,3 +4819,62 @@ void check_move_unevictable_pages(struct pagevec *pvec)
 	}
 }
 EXPORT_SYMBOL_GPL(check_move_unevictable_pages);
+
+#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
+static ssize_t reclaim_store(struct device *dev,
+			     struct device_attribute *attr,
+			     const char *buf, size_t count)
+{
+	int err, nid = dev->id;
+	gfp_t gfp_mask = GFP_KERNEL;
+	struct pglist_data *pgdat = NODE_DATA(nid);
+	unsigned long nr_to_reclaim, nr_reclaimed = 0;
+	unsigned int nr_retries = MAX_RECLAIM_RETRIES;
+	struct scan_control sc = {
+		.gfp_mask = current_gfp_context(gfp_mask),
+		.reclaim_idx = gfp_zone(gfp_mask),
+		.priority = NODE_RECLAIM_PRIORITY,
+		.may_writepage = !laptop_mode,
+		.may_unmap = 1,
+		.may_swap = 1,
+	};
+
+	buf = strstrip((char *)buf);
+	err = page_counter_memparse(buf, "", &nr_to_reclaim);
+	if (err)
+		return err;
+
+	sc.nr_to_reclaim = max(nr_to_reclaim, SWAP_CLUSTER_MAX);
+
+	while (nr_reclaimed < nr_to_reclaim) {
+		unsigned long reclaimed;
+
+		if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
+			return -EAGAIN;
+
+		/* does cond_resched() */
+		reclaimed = __node_reclaim(pgdat, gfp_mask,
+					   nr_to_reclaim - nr_reclaimed, &sc);
+
+		clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
+
+		if (!reclaimed && !nr_retries--)
+			break;
+
+		nr_reclaimed += reclaimed;
+	}
+
+	return nr_reclaimed < nr_to_reclaim ? -EAGAIN : count;
+}
+
+static DEVICE_ATTR_WO(reclaim);
+int reclaim_register_node(struct node *node)
+{
+	return device_create_file(&node->dev, &dev_attr_reclaim);
+}
+
+void reclaim_unregister_node(struct node *node)
+{
+	return device_remove_file(&node->dev, &dev_attr_reclaim);
+}
+#endif
#15 lore raw

PATCH 5/6 mm/migration: export demotion_path of a node via sysfs

In reply to Davidlohr Bueso

Add a /sys/devices/system/node/nodeX/demotion_path file to export the possible target(s) in node_demotion[node].

---
 Documentation/ABI/stable/sysfs-devices-node |  6 ++++
 drivers/base/node.c                         | 39 +++++++++++++++++++++
 include/linux/migrate.h                     | 15 ++++++++
 mm/migrate.c                                | 15 +-------
 4 files changed, 61 insertions(+), 14 deletions(-)
Documentation/ABI/stable/sysfs-devices-node +6 -0
index 3c935e1334f7..f620c6ae013c 100644
@@ -192,3 +192,9 @@ Description:
 		When it completes successfully, the specified amount or more memory
 		will have been reclaimed, and -EAGAIN if less bytes are reclaimed
 		than the specified amount.
+
+What:		/sys/devices/system/node/nodeX/demotion_path
+Date:		April 2022
+Contact:	Davidlohr Bueso <[email protected]>
+Description:
+		Shows nodes within the next tier of slower memory below this node.
drivers/base/node.c +39 -0
index d80c478e2a6e..ab4bae777535 100644
@@ -17,6 +17,7 @@
 #include <linux/nodemask.h>
 #include <linux/cpu.h>
 #include <linux/device.h>
+#include <linux/migrate.h>
 #include <linux/pm_runtime.h>
 #include <linux/swap.h>
 #include <linux/slab.h>
@@ -560,11 +561,49 @@ static ssize_t node_read_distance(struct device *dev,
 }
 static DEVICE_ATTR(distance, 0444, node_read_distance, NULL);
 
+static ssize_t node_read_demotion_path(struct device *dev,
+				       struct device_attribute *attr, char *buf)
+{
+	int nid = dev->id;
+	int len = 0;
+	int i;
+	struct demotion_nodes *nd;
+
+	/*
+	 * buf is currently PAGE_SIZE in length and each node needs 4 chars
+	 * at the most (target + space or newline).
+	 */
+	BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
+
+	if (!node_demotion) {
+		len += sysfs_emit_at(buf, len, "%d", NUMA_NO_NODE);
+		goto done;
+	}
+
+	nd = &node_demotion[nid];
+
+	rcu_read_lock();
+	if (nd->nr == 0)
+		len += sysfs_emit_at(buf, len, "%d", NUMA_NO_NODE);
+	else {
+		for (i = 0; i < nd->nr; i++) {
+			len += sysfs_emit_at(buf, len, "%s%d",
+					     i ? " " : "", nd->nodes[i]);
+		}
+	}
+	rcu_read_unlock();
+done:
+	len += sysfs_emit_at(buf, len, "\n");
+	return len;
+}
+static DEVICE_ATTR(demotion_path, 0444, node_read_demotion_path, NULL);
+
 static struct attribute *node_dev_attrs[] = {
 	&dev_attr_meminfo.attr,
 	&dev_attr_numastat.attr,
 	&dev_attr_distance.attr,
 	&dev_attr_vmstat.attr,
+	&dev_attr_demotion_path.attr,
 	NULL
 };
 
include/linux/migrate.h +15 -0
index 90e75d5a54d6..b0ac6a717e44 100644
@@ -111,6 +111,21 @@ static inline int migrate_misplaced_page(struct page *page,
 }
 #endif /* CONFIG_NUMA_BALANCING */
 
+#define DEFAULT_DEMOTION_TARGET_NODES 15
+
+#if MAX_NUMNODES < DEFAULT_DEMOTION_TARGET_NODES
+#define DEMOTION_TARGET_NODES	(MAX_NUMNODES - 1)
+#else
+#define DEMOTION_TARGET_NODES	DEFAULT_DEMOTION_TARGET_NODES
+#endif
+
+struct demotion_nodes {
+	unsigned short nr;
+	short nodes[DEMOTION_TARGET_NODES];
+};
+
+extern struct demotion_nodes *node_demotion __read_mostly;
+
 #ifdef CONFIG_MIGRATION
 
 /*
mm/migrate.c +1 -14
index 6c31ee1e1c9b..e47ea25fcfe8 100644
@@ -2172,20 +2172,7 @@ int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
  * must be held over all reads to ensure that no cycles are
  * observed.
  */
-#define DEFAULT_DEMOTION_TARGET_NODES 15
-
-#if MAX_NUMNODES < DEFAULT_DEMOTION_TARGET_NODES
-#define DEMOTION_TARGET_NODES	(MAX_NUMNODES - 1)
-#else
-#define DEMOTION_TARGET_NODES	DEFAULT_DEMOTION_TARGET_NODES
-#endif
-
-struct demotion_nodes {
-	unsigned short nr;
-	short nodes[DEMOTION_TARGET_NODES];
-};
-
-static struct demotion_nodes *node_demotion __read_mostly;
+struct demotion_nodes *node_demotion __read_mostly;
 
 /**
  * next_demotion_node() - Get the next node in the demotion path
Signature · 1 line
2.26.2
#16 lore raw

Re: [PATCH 5/6] mm/migration: export demotion_path of a node via sysfs

Quoted 3 lines · Davidlohr Bueso

Add a /sys/devices/system/node/nodeX/demotion_path file to export the possible target(s) in node_demotion[node].

I'm not sure if you noticed that Jagdish Gediya is working on the similar patch, please see
https://lore.kernel.org/linux-mm/[email protected]/

It would be better to combine the two to avoid duplicate effort.

Quoted 141 lines
---
 Documentation/ABI/stable/sysfs-devices-node |  6 ++++
 drivers/base/node.c                         | 39 +++++++++++++++++++++
 include/linux/migrate.h                     | 15 ++++++++
 mm/migrate.c                                | 15 +-------
 4 files changed, 61 insertions(+), 14 deletions(-)
Documentation/ABI/stable/sysfs-devices-node +6 -0
index 3c935e1334f7..f620c6ae013c 100644
@@ -192,3 +192,9 @@ Description:
                When it completes successfully, the specified amount or more memory
                will have been reclaimed, and -EAGAIN if less bytes are reclaimed
                than the specified amount.
+
+What:          /sys/devices/system/node/nodeX/demotion_path
+Date:          April 2022
+Contact:       Davidlohr Bueso <[email protected]>
+Description:
+               Shows nodes within the next tier of slower memory below this node.
drivers/base/node.c +39 -0
index d80c478e2a6e..ab4bae777535 100644
@@ -17,6 +17,7 @@
 #include <linux/nodemask.h>
 #include <linux/cpu.h>
 #include <linux/device.h>
+#include <linux/migrate.h>
 #include <linux/pm_runtime.h>
 #include <linux/swap.h>
 #include <linux/slab.h>
@@ -560,11 +561,49 @@ static ssize_t node_read_distance(struct device *dev,
 }
 static DEVICE_ATTR(distance, 0444, node_read_distance, NULL);
 
+static ssize_t node_read_demotion_path(struct device *dev,
+                                      struct device_attribute *attr, char *buf)
+{
+       int nid = dev->id;
+       int len = 0;
+       int i;
+       struct demotion_nodes *nd;
+
+       /*
+        * buf is currently PAGE_SIZE in length and each node needs 4 chars
+        * at the most (target + space or newline).
+        */
+       BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
+
+       if (!node_demotion) {
+               len += sysfs_emit_at(buf, len, "%d", NUMA_NO_NODE);
+               goto done;
+       }
+
+       nd = &node_demotion[nid];
+
+       rcu_read_lock();
+       if (nd->nr == 0)
+               len += sysfs_emit_at(buf, len, "%d", NUMA_NO_NODE);
+       else {
+               for (i = 0; i < nd->nr; i++) {
+                       len += sysfs_emit_at(buf, len, "%s%d",
+                                            i ? " " : "", nd->nodes[i]);
+               }
+       }
+       rcu_read_unlock();
+done:
+       len += sysfs_emit_at(buf, len, "\n");
+       return len;
+}
+static DEVICE_ATTR(demotion_path, 0444, node_read_demotion_path, NULL);
+
 static struct attribute *node_dev_attrs[] = {
        &dev_attr_meminfo.attr,
        &dev_attr_numastat.attr,
        &dev_attr_distance.attr,
        &dev_attr_vmstat.attr,
+       &dev_attr_demotion_path.attr,
        NULL
 };
 
include/linux/migrate.h +15 -0
index 90e75d5a54d6..b0ac6a717e44 100644
@@ -111,6 +111,21 @@ static inline int migrate_misplaced_page(struct page *page,
 }
 #endif /* CONFIG_NUMA_BALANCING */
 
+#define DEFAULT_DEMOTION_TARGET_NODES 15
+
+#if MAX_NUMNODES < DEFAULT_DEMOTION_TARGET_NODES
+#define DEMOTION_TARGET_NODES  (MAX_NUMNODES - 1)
+#else
+#define DEMOTION_TARGET_NODES  DEFAULT_DEMOTION_TARGET_NODES
+#endif
+
+struct demotion_nodes {
+       unsigned short nr;
+       short nodes[DEMOTION_TARGET_NODES];
+};
+
+extern struct demotion_nodes *node_demotion __read_mostly;
+
 #ifdef CONFIG_MIGRATION
 
 /*
mm/migrate.c +1 -15
index 6c31ee1e1c9b..e47ea25fcfe8 100644
@@ -2172,20 +2172,7 @@ int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
  * must be held over all reads to ensure that no cycles are
  * observed.
  */
-#define DEFAULT_DEMOTION_TARGET_NODES 15
-
-#if MAX_NUMNODES < DEFAULT_DEMOTION_TARGET_NODES
-#define DEMOTION_TARGET_NODES  (MAX_NUMNODES - 1)
-#else
-#define DEMOTION_TARGET_NODES  DEFAULT_DEMOTION_TARGET_NODES
-#endif
-
-struct demotion_nodes {
-       unsigned short nr;
-       short nodes[DEMOTION_TARGET_NODES];
-};
-
-static struct demotion_nodes *node_demotion __read_mostly;
+struct demotion_nodes *node_demotion __read_mostly;
 
 /**
  * next_demotion_node() - Get the next node in the demotion path
--

2.26.2

#17 lore raw

Re: [PATCH 5/6] mm/migration: export demotion_path of a node via sysfs

Quoted 9 lines · Yang Shi
Quoted 3 lines · Davidlohr Bueso

Add a /sys/devices/system/node/nodeX/demotion_path file to export the possible target(s) in node_demotion[node].

I'm not sure if you noticed that Jagdish Gediya is working on the similar patch, please see
https://lore.kernel.org/linux-mm/[email protected]/

Loop in Jagdish Gediya, Ying Huang and Wei Xu.

Quoted 144 lines

It would be better to combine the two to avoid duplicate effort.

Quoted 141 lines
---
 Documentation/ABI/stable/sysfs-devices-node |  6 ++++
 drivers/base/node.c                         | 39 +++++++++++++++++++++
 include/linux/migrate.h                     | 15 ++++++++
 mm/migrate.c                                | 15 +-------
 4 files changed, 61 insertions(+), 14 deletions(-)
Documentation/ABI/stable/sysfs-devices-node +6 -0
index 3c935e1334f7..f620c6ae013c 100644
@@ -192,3 +192,9 @@ Description:
                When it completes successfully, the specified amount or more memory
                will have been reclaimed, and -EAGAIN if less bytes are reclaimed
                than the specified amount.
+
+What:          /sys/devices/system/node/nodeX/demotion_path
+Date:          April 2022
+Contact:       Davidlohr Bueso <[email protected]>
+Description:
+               Shows nodes within the next tier of slower memory below this node.
drivers/base/node.c +39 -0
index d80c478e2a6e..ab4bae777535 100644
@@ -17,6 +17,7 @@
 #include <linux/nodemask.h>
 #include <linux/cpu.h>
 #include <linux/device.h>
+#include <linux/migrate.h>
 #include <linux/pm_runtime.h>
 #include <linux/swap.h>
 #include <linux/slab.h>
@@ -560,11 +561,49 @@ static ssize_t node_read_distance(struct device *dev,
 }
 static DEVICE_ATTR(distance, 0444, node_read_distance, NULL);
 
+static ssize_t node_read_demotion_path(struct device *dev,
+                                      struct device_attribute *attr, char *buf)
+{
+       int nid = dev->id;
+       int len = 0;
+       int i;
+       struct demotion_nodes *nd;
+
+       /*
+        * buf is currently PAGE_SIZE in length and each node needs 4 chars
+        * at the most (target + space or newline).
+        */
+       BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
+
+       if (!node_demotion) {
+               len += sysfs_emit_at(buf, len, "%d", NUMA_NO_NODE);
+               goto done;
+       }
+
+       nd = &node_demotion[nid];
+
+       rcu_read_lock();
+       if (nd->nr == 0)
+               len += sysfs_emit_at(buf, len, "%d", NUMA_NO_NODE);
+       else {
+               for (i = 0; i < nd->nr; i++) {
+                       len += sysfs_emit_at(buf, len, "%s%d",
+                                            i ? " " : "", nd->nodes[i]);
+               }
+       }
+       rcu_read_unlock();
+done:
+       len += sysfs_emit_at(buf, len, "\n");
+       return len;
+}
+static DEVICE_ATTR(demotion_path, 0444, node_read_demotion_path, NULL);
+
 static struct attribute *node_dev_attrs[] = {
        &dev_attr_meminfo.attr,
        &dev_attr_numastat.attr,
        &dev_attr_distance.attr,
        &dev_attr_vmstat.attr,
+       &dev_attr_demotion_path.attr,
        NULL
 };
 
include/linux/migrate.h +15 -0
index 90e75d5a54d6..b0ac6a717e44 100644
@@ -111,6 +111,21 @@ static inline int migrate_misplaced_page(struct page *page,
 }
 #endif /* CONFIG_NUMA_BALANCING */
 
+#define DEFAULT_DEMOTION_TARGET_NODES 15
+
+#if MAX_NUMNODES < DEFAULT_DEMOTION_TARGET_NODES
+#define DEMOTION_TARGET_NODES  (MAX_NUMNODES - 1)
+#else
+#define DEMOTION_TARGET_NODES  DEFAULT_DEMOTION_TARGET_NODES
+#endif
+
+struct demotion_nodes {
+       unsigned short nr;
+       short nodes[DEMOTION_TARGET_NODES];
+};
+
+extern struct demotion_nodes *node_demotion __read_mostly;
+
 #ifdef CONFIG_MIGRATION
 
 /*
mm/migrate.c +1 -15
index 6c31ee1e1c9b..e47ea25fcfe8 100644
@@ -2172,20 +2172,7 @@ int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
  * must be held over all reads to ensure that no cycles are
  * observed.
  */
-#define DEFAULT_DEMOTION_TARGET_NODES 15
-
-#if MAX_NUMNODES < DEFAULT_DEMOTION_TARGET_NODES
-#define DEMOTION_TARGET_NODES  (MAX_NUMNODES - 1)
-#else
-#define DEMOTION_TARGET_NODES  DEFAULT_DEMOTION_TARGET_NODES
-#endif
-
-struct demotion_nodes {
-       unsigned short nr;
-       short nodes[DEMOTION_TARGET_NODES];
-};
-
-static struct demotion_nodes *node_demotion __read_mostly;
+struct demotion_nodes *node_demotion __read_mostly;
 
 /**
  * next_demotion_node() - Get the next node in the demotion path
--

2.26.2

#18 lore raw

Re: [PATCH 5/6] mm/migration: export demotion_path of a node via sysfs

Quoted 4 lines · Yang Shi
Quoted 3 lines · Yang Shi
Quoted 1 line · Davidlohr Bueso
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

Add a /sys/devices/system/node/nodeX/demotion_path file to export the possible target(s) in node_demotion[node].

I'm not sure if you noticed that Jagdish Gediya is working on the similar patch, please see
https://lore.kernel.org/linux-mm/[email protected]/

Loop in Jagdish Gediya, Ying Huang and Wei Xu.

Hmm I had missed this thread, I'll go have a look.

Quoted 2 lines
Quoted 2 lines

It would be better to combine the two to avoid duplicate effort.

Indeed - and even more reason for lsfmm discussions defining the future ABI for tiering.

Thanks,
Davidlohr

#19 lore raw

PATCH 6/6 mm/migrate: export whether or not node is toptier in sysf

In reply to Davidlohr Bueso

This allows userspace to know if the node is considered fast memory (with CPUs attached to it). While this can be already derived without a new file, this helps further encapsulate the concept.

---
Resending, just noticed this oatch was never posted.

 Documentation/ABI/stable/sysfs-devices-node |  6 ++++++
 drivers/base/node.c                         | 13 +++++++++++++
 2 files changed, 19 insertions(+)
Documentation/ABI/stable/sysfs-devices-node +0 -0
index f620c6ae013c..1c21c3985535 100644
@@ -198,3 +198,9 @@ Date:		April 2022
 Contact:	Davidlohr Bueso <[email protected]>
 Description:

Shows nodes within the next tier of slower memory below this node.
+
+What: /sys/devices/system/node/nodeX/memory_toptier
+Date: April 2022
+Contact: Davidlohr Bueso <[email protected]>
+Description:
+ Node is attached to fast memory or not.

drivers/base/node.c +12 -0
index ab4bae777535..b9de5b0360f2 100644
@@ -598,12 +598,25 @@ static ssize_t node_read_demotion_path(struct device *dev,
 }
 static DEVICE_ATTR(demotion_path, 0444, node_read_demotion_path, NULL);
 
+static ssize_t node_read_memory_toptier(struct device *dev,
+				     struct device_attribute *attr, char *buf)
+{
+	int nid = dev->id;
+	int len = 0;
+
+	len += sysfs_emit_at(buf, len, "%d\n", !!node_is_toptier(nid));
+
+	return len;
+}
+static DEVICE_ATTR(memory_toptier, 0444, node_read_memory_toptier, NULL);
+
 static struct attribute *node_dev_attrs[] = {

&dev_attr_meminfo.attr,
&dev_attr_numastat.attr,
&dev_attr_distance.attr,
&dev_attr_vmstat.attr,
&dev_attr_demotion_path.attr,
+ &dev_attr_memory_toptier.attr,
NULL
};

--
2.26.2

#20 lore raw

Re: [PATCH 6/6] mm/migrate: export whether or not node is toptier in sysf

Quoted 4 lines · Davidlohr Bueso

This allows userspace to know if the node is considered fast memory (with CPUs attached to it). While this can be already derived without a new file, this helps further encapsulate the concept.

What is userspace supposed to *do* with this, though?

What does "attached" mean?

Isn't it just asking for trouble to add (known) redundancy to the ABI? It seems like a recipe for future inconsistency.

#21 lore raw

Re: [PATCH 6/6] mm/migrate: export whether or not node is toptier in sysf

Quoted 7 lines · Dave Hansen
Quoted 4 lines · Davidlohr Bueso

This allows userspace to know if the node is considered fast memory (with CPUs attached to it). While this can be already derived without a new file, this helps further encapsulate the concept.

What is userspace supposed to *do* with this, though?

This came as a scratch to my own itch. I wanted to start testing more tiering patches overall that I see pop up, and wanted a way to differentiate the slow vs the fast memories in order to better configure workload(s) working set sizes beyond what is your typical grep MemTotal /proc/meminfo. If there is a better way I'm all for it.

Quoted 2 lines

What does "attached" mean?

I'll rephrase.

Quoted 2 lines

Isn't it just asking for trouble to add (known) redundancy to the ABI? It seems like a recipe for future inconsistency.

Perhaps. It was mostly about the fact that the notion of top tier could also change as technology evolves.

Thanks,
Davidlohr

#22 lore raw

Re: [PATCH 6/6] mm/migrate: export whether or not node is toptier in sysf

Quoted 7 lines · Davidlohr Bueso
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Davidlohr Bueso

This allows userspace to know if the node is considered fast memory (with CPUs attached to it). While this can be already derived without a new file, this helps further encapsulate the concept.

Quoted 8 lines
Quoted 1 line

What is userspace supposed to *do* with this, though?

This came as a scratch to my own itch. I wanted to start testing more tiering patches overall that I see pop up, and wanted a way to differentiate the slow vs the fast memories in order to better configure workload(s) working set sizes beyond what is your typical grep MemTotal /proc/meminfo. If there is a better way I'm all for it.

But how does this help you? Does it save you a few lines in a shell script to find the nodes that have memory and CPUs?

Quoted 5 lines
Quoted 2 lines

Isn't it just asking for trouble to add (known) redundancy to the ABI? It seems like a recipe for future inconsistency.

Perhaps. It was mostly about the fact that the notion of top tier could also change as technology evolves.

It seems like something arbitrary that everyone will just disagree on. I think we should try to stick to cold, hard facts as must as possible rather than trying to have the *kernel* dictate as a policy what is fast versus slow.

#23 lore raw

Re: [PATCH 6/6] mm/migrate: export whether or not node is toptier in sysf

Quoted 3 lines · Dave Hansen

I think we should try to stick to cold, hard facts as must as possible rather than trying to have the *kernel* dictate as a policy what is fast versus slow.

That's a very good point and I agree.

Thanks,
Davidlohr

#24 lore raw

Re: [PATCH 6/6] mm/migrate: export whether or not node is toptier in sysf

In reply to Davidlohr Bueso
Quoted 35 lines · Davidlohr Bueso

This allows userspace to know if the node is considered fast memory (with CPUs attached to it). While this can be already derived without a new file, this helps further encapsulate the concept.

---
Resending, just noticed this oatch was never posted.

  Documentation/ABI/stable/sysfs-devices-node |  6 ++++++
  drivers/base/node.c                         | 13 +++++++++++++
  2 files changed, 19 insertions(+)
Documentation/ABI/stable/sysfs-devices-node +6 -0
index f620c6ae013c..1c21c3985535 100644
@@ -198,3 +198,9 @@ Date:               April 2022
  Contact:      Davidlohr Bueso <[email protected]>
  Description:
                Shows nodes within the next tier of slower memory below this node.
+
+What:          /sys/devices/system/node/nodeX/memory_toptier
+Date:          April 2022
+Contact:       Davidlohr Bueso <[email protected]>
+Description:
+               Node is attached to fast memory or not.
drivers/base/node.c +0 -0
index ab4bae777535..b9de5b0360f2 100644

drivers/base/node.c static ssize_t node_read_demotion_path(struct device *dev, · line 607

Quoted 10 lines
drivers/base/node.c +7 -0
@@ -598,12 +598,25 @@ static ssize_t node_read_demotion_path(struct device *dev,
  }
  static DEVICE_ATTR(demotion_path, 0444, node_read_demotion_path, NULL);
 
+static ssize_t node_read_memory_toptier(struct device *dev,
+                                    struct device_attribute *attr, char *buf)
+{
+       int nid = dev->id;
+       int len = 0;
+
+       len += sysfs_emit_at(buf, len, "%d\n", !!node_is_toptier(nid));

It is not guaranteed. Some hardware configurations have cpuless DRAM nodes, but they should be treated as top tier nodes IMHO. Please see
https://lore.kernel.org/linux-mm/[email protected]/

Quoted 18 lines
drivers/base/node.c +6 -1
+
+       return len;
+}
+static DEVICE_ATTR(memory_toptier, 0444, node_read_memory_toptier, NULL);
+
  static struct attribute *node_dev_attrs[] = {
        &dev_attr_meminfo.attr,
        &dev_attr_numastat.attr,
        &dev_attr_distance.attr,
        &dev_attr_vmstat.attr,
        &dev_attr_demotion_path.attr,
+       &dev_attr_memory_toptier.attr,
        NULL
  };
 
--

2.26.2