Skip to contentSkip to conversation

drivers/base/node.c: Simplify unregister_memory_block_under_nodes()

PATCH Superseded David Hildenbrand 2 participants 11 messages Jul 18, 2019 – Jul 19, 2019

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/base/node.c: Simplify unregister_memory_block_under_nodes()

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Stephen Rothwell <[email protected]>
Cc Pavel Tatashin <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oscar Salvador <[email protected]>
---
 drivers/base/memory.c  |  1 +
 drivers/base/node.c    | 40 ++++++++++++++++------------------------
 include/linux/memory.h |  4 +++-
 3 files changed, 20 insertions(+), 25 deletions(-)
drivers/base/memory.c +1 -0
index 20c39d1bcef8..154d5d4a0779 100644
@@ -674,6 +674,7 @@ static int init_memory_block(struct memory_block **memory,
 	mem->state = state;
 	start_pfn = section_nr_to_pfn(mem->start_section_nr);
 	mem->phys_device = arch_get_memory_phys_device(start_pfn);
+	mem->nid = NUMA_NO_NODE;
 
 	ret = register_memory(mem);
 
drivers/base/node.c +16 -24
index 75b7e6f6535b..29d27b8d5fda 100644
@@ -759,8 +759,6 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 	int ret, nid = *(int *)arg;
 	unsigned long pfn, sect_start_pfn, sect_end_pfn;
 
-	mem_blk->nid = nid;
-
 	sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
 	sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
 	sect_end_pfn += PAGES_PER_SECTION - 1;
@@ -789,6 +787,13 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 			if (page_nid != nid)
 				continue;
 		}
+
+		/* this memory block spans this node */
+		if (mem_blk->nid == NUMA_NO_NODE)
+			mem_blk->nid = nid;
+		else
+			mem_blk->nid = NUMA_NO_NODE - 1;
+
 		ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
 					&mem_blk->dev.kobj,
 					kobject_name(&mem_blk->dev.kobj));
@@ -804,32 +809,19 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 }
 
 /*
- * Unregister memory block device under all nodes that it spans.
- * Has to be called with mem_sysfs_mutex held (due to unlinked_nodes).
+ * Unregister a memory block device under the node it spans. Memory blocks
+ * with multiple nodes cannot be offlined and therefore also never be removed.
  */
 void unregister_memory_block_under_nodes(struct memory_block *mem_blk)
 {
-	unsigned long pfn, sect_start_pfn, sect_end_pfn;
-	static nodemask_t unlinked_nodes;
-
-	nodes_clear(unlinked_nodes);
-	sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
-	sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
-	for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
-		int nid;
+	if (mem_blk->nid == NUMA_NO_NODE ||
+	    WARN_ON_ONCE(mem_blk->nid == NUMA_NO_NODE - 1))
+		return;
 
-		nid = get_nid_for_pfn(pfn);
-		if (nid < 0)
-			continue;
-		if (!node_online(nid))
-			continue;
-		if (node_test_and_set(nid, unlinked_nodes))
-			continue;
-		sysfs_remove_link(&node_devices[nid]->dev.kobj,
-			 kobject_name(&mem_blk->dev.kobj));
-		sysfs_remove_link(&mem_blk->dev.kobj,
-			 kobject_name(&node_devices[nid]->dev.kobj));
-	}
+	sysfs_remove_link(&node_devices[mem_blk->nid]->dev.kobj,
+		 kobject_name(&mem_blk->dev.kobj));
+	sysfs_remove_link(&mem_blk->dev.kobj,
+		 kobject_name(&node_devices[mem_blk->nid]->dev.kobj));
 }
 
 int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn)
include/linux/memory.h +3 -1
index 02e633f3ede0..c91af10d5fb4 100644
@@ -33,7 +33,9 @@ struct memory_block {
 	void *hw;			/* optional pointer to fw/hw data */
 	int (*phys_callback)(struct memory_block *);
 	struct device dev;
-	int nid;			/* NID for this memory block */
+	int nid;			/* NID for this memory block.
+					   - NUMA_NO_NODE: uninitialized
+					   - NUMA_NO_NODE - 1: mixed nodes */
 };
 
 int arch_get_memory_phys_device(unsigned long start_pfn);
Signature · 1 line
2.21.0
#1 lore raw
Quoted 49 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Stephen Rothwell <[email protected]>
Cc Pavel Tatashin <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oscar Salvador <[email protected]>
---
 drivers/base/memory.c  |  1 +
 drivers/base/node.c    | 40 ++++++++++++++++------------------------
 include/linux/memory.h |  4 +++-
 3 files changed, 20 insertions(+), 25 deletions(-)
drivers/base/memory.c +1 -0
index 20c39d1bcef8..154d5d4a0779 100644
@@ -674,6 +674,7 @@ static int init_memory_block(struct memory_block **memory,
 	mem->state = state;
 	start_pfn = section_nr_to_pfn(mem->start_section_nr);
 	mem->phys_device = arch_get_memory_phys_device(start_pfn);
+	mem->nid = NUMA_NO_NODE;
 
 	ret = register_memory(mem);
 
drivers/base/node.c +0 -2
index 75b7e6f6535b..29d27b8d5fda 100644
@@ -759,8 +759,6 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 	int ret, nid = *(int *)arg;
 	unsigned long pfn, sect_start_pfn, sect_end_pfn;
 
-	mem_blk->nid = nid;
-
 	sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
 	sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
 	sect_end_pfn += PAGES_PER_SECTION - 1;

drivers/base/node.c static int register_mem_sect_under_node(struct memory_block *mem_blk, · line 796

Quoted 10 lines
drivers/base/node.c +7 -0
@@ -789,6 +787,13 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 			if (page_nid != nid)
 				continue;
 		}
+
+		/* this memory block spans this node */
+		if (mem_blk->nid == NUMA_NO_NODE)
+			mem_blk->nid = nid;
+		else
+			mem_blk->nid = NUMA_NO_NODE - 1;
+

Although I am not sure if it can happen, I think it is better to have

if (mem_blk->nid == NUMA_NO_NODE)

	mem_blk->nid = nid;
else if (mem_blk->nid != nid)
	mem_blk->nid = NUMA_NO_NODE - 1;
Signature · 3 lines
Thanks,

David / dhildenb
#2 lore raw
In reply to David Hildenbrand
Quoted 7 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

I do not really like NUMA_NO_NODE - 1 thing. This is yet another invalid node that is magic. Why should we even care? In other words why is this patch an improvement?

Quoted 113 lines
Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Stephen Rothwell <[email protected]>
Cc Pavel Tatashin <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oscar Salvador <[email protected]>
---
 drivers/base/memory.c  |  1 +
 drivers/base/node.c    | 40 ++++++++++++++++------------------------
 include/linux/memory.h |  4 +++-
 3 files changed, 20 insertions(+), 25 deletions(-)
drivers/base/memory.c +1 -0
index 20c39d1bcef8..154d5d4a0779 100644
@@ -674,6 +674,7 @@ static int init_memory_block(struct memory_block **memory,
 	mem->state = state;
 	start_pfn = section_nr_to_pfn(mem->start_section_nr);
 	mem->phys_device = arch_get_memory_phys_device(start_pfn);
+	mem->nid = NUMA_NO_NODE;
 
 	ret = register_memory(mem);
 
drivers/base/node.c +16 -24
index 75b7e6f6535b..29d27b8d5fda 100644
@@ -759,8 +759,6 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 	int ret, nid = *(int *)arg;
 	unsigned long pfn, sect_start_pfn, sect_end_pfn;
 
-	mem_blk->nid = nid;
-
 	sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
 	sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
 	sect_end_pfn += PAGES_PER_SECTION - 1;
@@ -789,6 +787,13 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 			if (page_nid != nid)
 				continue;
 		}
+
+		/* this memory block spans this node */
+		if (mem_blk->nid == NUMA_NO_NODE)
+			mem_blk->nid = nid;
+		else
+			mem_blk->nid = NUMA_NO_NODE - 1;
+
 		ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
 					&mem_blk->dev.kobj,
 					kobject_name(&mem_blk->dev.kobj));
@@ -804,32 +809,19 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 }
 
 /*
- * Unregister memory block device under all nodes that it spans.
- * Has to be called with mem_sysfs_mutex held (due to unlinked_nodes).
+ * Unregister a memory block device under the node it spans. Memory blocks
+ * with multiple nodes cannot be offlined and therefore also never be removed.
  */
 void unregister_memory_block_under_nodes(struct memory_block *mem_blk)
 {
-	unsigned long pfn, sect_start_pfn, sect_end_pfn;
-	static nodemask_t unlinked_nodes;
-
-	nodes_clear(unlinked_nodes);
-	sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
-	sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
-	for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
-		int nid;
+	if (mem_blk->nid == NUMA_NO_NODE ||
+	    WARN_ON_ONCE(mem_blk->nid == NUMA_NO_NODE - 1))
+		return;
 
-		nid = get_nid_for_pfn(pfn);
-		if (nid < 0)
-			continue;
-		if (!node_online(nid))
-			continue;
-		if (node_test_and_set(nid, unlinked_nodes))
-			continue;
-		sysfs_remove_link(&node_devices[nid]->dev.kobj,
-			 kobject_name(&mem_blk->dev.kobj));
-		sysfs_remove_link(&mem_blk->dev.kobj,
-			 kobject_name(&node_devices[nid]->dev.kobj));
-	}
+	sysfs_remove_link(&node_devices[mem_blk->nid]->dev.kobj,
+		 kobject_name(&mem_blk->dev.kobj));
+	sysfs_remove_link(&mem_blk->dev.kobj,
+		 kobject_name(&node_devices[mem_blk->nid]->dev.kobj));
 }
 
 int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn)
include/linux/memory.h +3 -1
index 02e633f3ede0..c91af10d5fb4 100644
@@ -33,7 +33,9 @@ struct memory_block {
 	void *hw;			/* optional pointer to fw/hw data */
 	int (*phys_callback)(struct memory_block *);
 	struct device dev;
-	int nid;			/* NID for this memory block */
+	int nid;			/* NID for this memory block.
+					   - NUMA_NO_NODE: uninitialized
+					   - NUMA_NO_NODE - 1: mixed nodes */
 };
 
 int arch_get_memory_phys_device(unsigned long start_pfn);
Signature · 1 line
2.21.0
Signature · 2 lines
Michal Hocko
SUSE Labs
#3 lore raw
Quoted 5 lines · Michal Hocko
Quoted 4 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Quoted 7 lines
Quoted 3 lines

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

I do not really like NUMA_NO_NODE - 1 thing. This is yet another invalid node that is magic. Why should we even care? In other words why is this patch an improvement?

I mean we can of course go ahead and drop the "NUMA_NO_NODE - 1" thingy from the patch. A memory block with multiple nodes would (as of now) only indicate one of the nodes.

Then there is simply no way to WARN_ON_ONCE() in case unexpected things would happen. (I mean it really shouldn't happen or we have a BUG somewhere else)

Alternative: Add "bool mixed_nids;" to "struct memory block".

Quoted 115 lines
Quoted 114 lines
Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Stephen Rothwell <[email protected]>
Cc Pavel Tatashin <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oscar Salvador <[email protected]>
---
 drivers/base/memory.c  |  1 +
 drivers/base/node.c    | 40 ++++++++++++++++------------------------
 include/linux/memory.h |  4 +++-
 3 files changed, 20 insertions(+), 25 deletions(-)
drivers/base/memory.c +1 -0
index 20c39d1bcef8..154d5d4a0779 100644
@@ -674,6 +674,7 @@ static int init_memory_block(struct memory_block **memory,
 	mem->state = state;
 	start_pfn = section_nr_to_pfn(mem->start_section_nr);
 	mem->phys_device = arch_get_memory_phys_device(start_pfn);
+	mem->nid = NUMA_NO_NODE;
 
 	ret = register_memory(mem);
 
drivers/base/node.c +16 -24
index 75b7e6f6535b..29d27b8d5fda 100644
@@ -759,8 +759,6 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 	int ret, nid = *(int *)arg;
 	unsigned long pfn, sect_start_pfn, sect_end_pfn;
 
-	mem_blk->nid = nid;
-
 	sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
 	sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
 	sect_end_pfn += PAGES_PER_SECTION - 1;
@@ -789,6 +787,13 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 			if (page_nid != nid)
 				continue;
 		}
+
+		/* this memory block spans this node */
+		if (mem_blk->nid == NUMA_NO_NODE)
+			mem_blk->nid = nid;
+		else
+			mem_blk->nid = NUMA_NO_NODE - 1;
+
 		ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
 					&mem_blk->dev.kobj,
 					kobject_name(&mem_blk->dev.kobj));
@@ -804,32 +809,19 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 }
 
 /*
- * Unregister memory block device under all nodes that it spans.
- * Has to be called with mem_sysfs_mutex held (due to unlinked_nodes).
+ * Unregister a memory block device under the node it spans. Memory blocks
+ * with multiple nodes cannot be offlined and therefore also never be removed.
  */
 void unregister_memory_block_under_nodes(struct memory_block *mem_blk)
 {
-	unsigned long pfn, sect_start_pfn, sect_end_pfn;
-	static nodemask_t unlinked_nodes;
-
-	nodes_clear(unlinked_nodes);
-	sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
-	sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
-	for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
-		int nid;
+	if (mem_blk->nid == NUMA_NO_NODE ||
+	    WARN_ON_ONCE(mem_blk->nid == NUMA_NO_NODE - 1))
+		return;
 
-		nid = get_nid_for_pfn(pfn);
-		if (nid < 0)
-			continue;
-		if (!node_online(nid))
-			continue;
-		if (node_test_and_set(nid, unlinked_nodes))
-			continue;
-		sysfs_remove_link(&node_devices[nid]->dev.kobj,
-			 kobject_name(&mem_blk->dev.kobj));
-		sysfs_remove_link(&mem_blk->dev.kobj,
-			 kobject_name(&node_devices[nid]->dev.kobj));
-	}
+	sysfs_remove_link(&node_devices[mem_blk->nid]->dev.kobj,
+		 kobject_name(&mem_blk->dev.kobj));
+	sysfs_remove_link(&mem_blk->dev.kobj,
+		 kobject_name(&node_devices[mem_blk->nid]->dev.kobj));
 }
 
 int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn)
include/linux/memory.h +3 -1
index 02e633f3ede0..c91af10d5fb4 100644
@@ -33,7 +33,9 @@ struct memory_block {
 	void *hw;			/* optional pointer to fw/hw data */
 	int (*phys_callback)(struct memory_block *);
 	struct device dev;
-	int nid;			/* NID for this memory block */
+	int nid;			/* NID for this memory block.
+					   - NUMA_NO_NODE: uninitialized
+					   - NUMA_NO_NODE - 1: mixed nodes */
 };
 
 int arch_get_memory_phys_device(unsigned long start_pfn);
Signature · 1 line
2.21.0
Signature · 3 lines
Thanks,

David / dhildenb
#6 lore raw
In reply to Michal Hocko
Quoted 5 lines · Michal Hocko
Quoted 4 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Quoted 7 lines
Quoted 3 lines

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

I do not really like NUMA_NO_NODE - 1 thing. This is yet another invalid node that is magic. Why should we even care? In other words why is this patch an improvement?

Oh, and to answer that part of the question:

We no longer have to iterate over each pfn of a memory block to be removed.

Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw
In reply to David Hildenbrand
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Michal Hocko
Quoted 7 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

Quoted 7 lines
Quoted 3 lines

I do not really like NUMA_NO_NODE - 1 thing. This is yet another invalid node that is magic. Why should we even care? In other words why is this patch an improvement?

I mean we can of course go ahead and drop the "NUMA_NO_NODE - 1" thingy from the patch. A memory block with multiple nodes would (as of now) only indicate one of the nodes.

Yes and that seemed to work reasonably well so far. Sure there is a potential confusion but platforms with interleaved nodes are rare enough to somebody to even notice so far.

Quoted 3 lines

Then there is simply no way to WARN_ON_ONCE() in case unexpected things would happen. (I mean it really shouldn't happen or we have a BUG somewhere else)

I do not really see much point to warn here. What can user potentially do?

Quoted 1 line

Alternative: Add "bool mixed_nids;" to "struct memory block".

That would be certainly possible but do we actually care?

Signature · 2 lines
Michal Hocko
SUSE Labs
#7 lore raw
In reply to David Hildenbrand
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Michal Hocko
Quoted 7 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

Quoted 7 lines
Quoted 3 lines

I do not really like NUMA_NO_NODE - 1 thing. This is yet another invalid node that is magic. Why should we even care? In other words why is this patch an improvement?

Oh, and to answer that part of the question:

We no longer have to iterate over each pfn of a memory block to be removed.

Is it possible that we are overzealous when unregistering syfs files and we should simply skip the pfn walk even without this change?

Signature · 2 lines
Michal Hocko
SUSE Labs
#5 lore raw
In reply to Michal Hocko
Quoted 15 lines · Michal Hocko
Quoted 14 lines · David Hildenbrand
Quoted 12 lines · Michal Hocko
Quoted 7 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

I do not really like NUMA_NO_NODE - 1 thing. This is yet another invalid node that is magic. Why should we even care? In other words why is this patch an improvement?

Quoted 7 lines
Quoted 3 lines

I mean we can of course go ahead and drop the "NUMA_NO_NODE - 1" thingy from the patch. A memory block with multiple nodes would (as of now) only indicate one of the nodes.

Yes and that seemed to work reasonably well so far. Sure there is a potential confusion but platforms with interleaved nodes are rare enough to somebody to even notice so far.

Let's hope there are no BUGs related to that and we just didn't catch them yet because it's barely used :)

Quoted 7 lines
Quoted 4 lines

Then there is simply no way to WARN_ON_ONCE() in case unexpected things would happen. (I mean it really shouldn't happen or we have a BUG somewhere else)

I do not really see much point to warn here. What can user potentially do?

We could detect this while testing and see that some other code seems to do unexpected things (remove such memory blocks although not allowed).

Quoted 4 lines
Quoted 2 lines

Alternative: Add "bool mixed_nids;" to "struct memory block".

That would be certainly possible but do we actually care?

Only if we want to warn. And I am fine with dropping this part.

Signature · 3 lines
Thanks,

David / dhildenb
#8 lore raw
In reply to Michal Hocko
Quoted 15 lines · Michal Hocko
Quoted 14 lines · David Hildenbrand
Quoted 12 lines · Michal Hocko
Quoted 7 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

I do not really like NUMA_NO_NODE - 1 thing. This is yet another invalid node that is magic. Why should we even care? In other words why is this patch an improvement?

Quoted 7 lines
Quoted 3 lines

Oh, and to answer that part of the question:

We no longer have to iterate over each pfn of a memory block to be removed.

Is it possible that we are overzealous when unregistering syfs files and we should simply skip the pfn walk even without this change?

I assume you mean something like v1 without the warning/"NUMA_NO_NODE -1"?

See what I have right now below.

From 27e9b02146e5fbe8edac49767693fa18c9b204dd Mon Sep 17 00:00:00 2001
From: David Hildenbrand <[email protected]>
Date: Thu, 18 Jul 2019 15:48:41 +0200
Subject: [PATCH v2] drivers/base/node.c: Simplify
unregister_memory_block_under_nodes()

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Stephen Rothwell <[email protected]>
Cc Pavel Tatashin <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oscar Salvador <[email protected]>
---
 drivers/base/memory.c |  1 +
 drivers/base/node.c   | 39 +++++++++++++++------------------------
 2 files changed, 16 insertions(+), 24 deletions(-)
drivers/base/memory.c +1 -0
index 20c39d1bcef8..154d5d4a0779 100644
@@ -674,6 +674,7 @@ static int init_memory_block(struct memory_block **memory,
 	mem->state = state;
 	start_pfn = section_nr_to_pfn(mem->start_section_nr);
 	mem->phys_device = arch_get_memory_phys_device(start_pfn);
+	mem->nid = NUMA_NO_NODE;
 
 	ret = register_memory(mem);
 
drivers/base/node.c +15 -24
index 75b7e6f6535b..840c95baa1d8 100644
@@ -759,8 +759,6 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 	int ret, nid = *(int *)arg;
 	unsigned long pfn, sect_start_pfn, sect_end_pfn;
 
-	mem_blk->nid = nid;
-
 	sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
 	sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
 	sect_end_pfn += PAGES_PER_SECTION - 1;
@@ -789,6 +787,13 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 			if (page_nid != nid)
 				continue;
 		}
+
+		/*
+		 * If this memory block spans multiple nodes, we only indicate
+		 * the last processed node.
+		 */
+		mem_blk->nid = nid;
+
 		ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
 					&mem_blk->dev.kobj,
 					kobject_name(&mem_blk->dev.kobj));
@@ -804,32 +809,18 @@ static int register_mem_sect_under_node(struct memory_block *mem_blk,
 }
 
 /*
- * Unregister memory block device under all nodes that it spans.
- * Has to be called with mem_sysfs_mutex held (due to unlinked_nodes).
+ * Unregister a memory block device under the node it spans. Memory blocks
+ * with multiple nodes cannot be offlined and therefore also never be removed.
  */
 void unregister_memory_block_under_nodes(struct memory_block *mem_blk)
 {
-	unsigned long pfn, sect_start_pfn, sect_end_pfn;
-	static nodemask_t unlinked_nodes;
-
-	nodes_clear(unlinked_nodes);
-	sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
-	sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
-	for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
-		int nid;
+	if (mem_blk->nid == NUMA_NO_NODE)
+		return;
 
-		nid = get_nid_for_pfn(pfn);
-		if (nid < 0)
-			continue;
-		if (!node_online(nid))
-			continue;
-		if (node_test_and_set(nid, unlinked_nodes))
-			continue;
-		sysfs_remove_link(&node_devices[nid]->dev.kobj,
-			 kobject_name(&mem_blk->dev.kobj));
-		sysfs_remove_link(&mem_blk->dev.kobj,
-			 kobject_name(&node_devices[nid]->dev.kobj));
-	}
+	sysfs_remove_link(&node_devices[mem_blk->nid]->dev.kobj,
+			  kobject_name(&mem_blk->dev.kobj));
+	sysfs_remove_link(&mem_blk->dev.kobj,
+			  kobject_name(&node_devices[mem_blk->nid]->dev.kobj));
 }
 
 int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn)
Signature · 8 lines
2.21.0


-- 

Thanks,

David / dhildenb
#9 lore raw
Quoted 20 lines · David Hildenbrand
Quoted 19 lines · Michal Hocko
Quoted 17 lines · David Hildenbrand
Quoted 5 lines · Michal Hocko
Quoted 4 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Quoted 7 lines
Quoted 3 lines

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

I do not really like NUMA_NO_NODE - 1 thing. This is yet another invalid node that is magic. Why should we even care? In other words why is this patch an improvement?

Oh, and to answer that part of the question:

We no longer have to iterate over each pfn of a memory block to be removed.

Quoted 7 lines
Quoted 3 lines

Is it possible that we are overzealous when unregistering syfs files and we should simply skip the pfn walk even without this change?

I assume you mean something like v1 without the warning/"NUMA_NO_NODE -1"?

See what I have right now below.

Yes. I didn'g get to look closely but you caught the idea. Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs
#10 lore raw
Quoted 25 lines · Michal Hocko
Quoted 24 lines · David Hildenbrand
Quoted 22 lines · Michal Hocko
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Michal Hocko
Quoted 7 lines · David Hildenbrand

We don't allow to offline memory block devices that belong to multiple numa nodes. Therefore, such devices can never get removed. It is sufficient to process a single node when removing the memory block.

Remember for each memory block if it belongs to no, a single, or mixed nodes, so we can use that information to skip unregistering or print a warning (essentially a safety net to catch BUGs).

Quoted 7 lines
Quoted 3 lines

I do not really like NUMA_NO_NODE - 1 thing. This is yet another invalid node that is magic. Why should we even care? In other words why is this patch an improvement?

Oh, and to answer that part of the question:

We no longer have to iterate over each pfn of a memory block to be removed.

Is it possible that we are overzealous when unregistering syfs files and we should simply skip the pfn walk even without this change?

Quoted 6 lines
Quoted 3 lines

I assume you mean something like v1 without the warning/"NUMA_NO_NODE -1"?

See what I have right now below.

Yes. I didn'g get to look closely but you caught the idea. Thanks!

Will do a quick test and resent later this day, thanks for having a look!

Signature · 3 lines
Thanks,

David / dhildenb