mm/memory_hotplug: Fix try_offline_node()
Subsystems (3)
PATCH 1/1 mm/memory_hotplug: Fix try_offline_node()
try_offline_node() is pretty much broken right now:
- The node span is updated when onlining memory, not when adding it. We ignore memory that was mever onlined. Bad.
- We touch possible garbage memmaps. The pfn_to_nid(pfn) can easily trigger a kernel panic. Bad for memory that is offline but also bad for subsection hotadd with ZONE_DEVICE, whereby the memmap of the first PFN of a section might contain garbage.
- Sections belonging to mixed nodes are not properly considered.
As memory blocks might belong to multiple nodes, we would have to walk all pageblocks (or at least subsections) within present sections. However, we don't have a way to identify whether a memmap that is not online was initialized (relevant for ZONE_DEVICE). This makes things more complicated.
Luckily, we can piggy pack on the node span and the sysfs links between memory blocks and the node. Currently, the node span is grown when calling move_pfn_range_to_zone() - e.g., when onlining memory, and shrunk when removing memory, before calling try_offline_node(). Sysfs links are created via link_mem_sections(), e.g., during boot or when adding memory.
If the node still spans memory or if any memory block is linked to the node in sysfs, we don't set the node offline. Without CONFIG_NUMA, or without CONFIG_SYSFS, we will simply always detect the node as being linked to the memory block and not set the node offline.
Add a way to test if a sysfs link exists.
Note: We will soon stop shrinking the ZONE_DEVICE zone and the node span when removing ZONE_DEVICE memory to fix similar issues (acess of garbage memmaps) - until we have a reliable way to identify whether these memmaps were properly initialized. This implies later, that once a node had ZONE_DEVICE memory, we won't be able to set a node offline - which should be acceptable.
Since commit f1dd2cd13c4b ("mm, memory_hotplug: do not associate hotadded memory to zones until online") memory that is added is not assoziated with a zone/node (memmap not initialized). The introducing commit 60a5a19e7419 ("memory-hotplug: remove sysfs file of node") already missed that we could have multiple nodes for a section and that the zone/node span is updated when onlining pages, not when adding them.
I tested this by hotplugging two DIMMs to a memory-less and cpu-less NUMA node. The node is properly onlined when adding the DIMMs. When removing the DIMMs, the node is properly offlined.
---
I just realized that we are are missing a try_online_node() call in the memremap path. Bad when adding device memory to a memory-less and cpu-less node - the pgdat is not initialized. Will also have to be fixed.
We stop shrinking the ZONE_DEVICE zone after the following patch:
[PATCH v6 04/10] mm/memory_hotplug: Don't access uninitialized memmaps in shrink_zone_span()
This implies, the above note regarding ZONE_DEVICE on a node blocking a node from getting offlined until we sorted out how to properly shrink the ZONE_DEVICE zone.
This patch is especially important for:
[PATCH v6 05/10] mm/memory_hotplug: Shrink zones when offlining memory
As the BUG fixed with this patch becomes now easier to observe when memory is offlined (in contrast to when memory would never have been onlined before).
As both patches are stable fixes and in next/master for a long time, we should probably pull this patch in front of both and also backport this patch at least to
Cc: [email protected] # v4.13+
I have not checked yet if there are real blockers to do that. I guess not.
--- drivers/base/node.c | 9 +++++++++ fs/sysfs/symlink.c | 21 +++++++++++++++++++++ include/linux/node.h | 7 +++++++ include/linux/sysfs.h | 6 ++++++ mm/memory_hotplug.c | 42 ++++++++++++++++++++++++++---------------- 5 files changed, 69 insertions(+), 16 deletions(-)
drivers/base/node.c +9 -0
@@ -833,6 +833,15 @@ int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn) register_mem_sect_under_node); } +bool memory_block_registered_under_node (struct memory_block *mem,int nid) +{ +if (mem->nid == nid) +return true; +/* memory blocks can span multiple nodes. Check against the link. */ +return sysfs_link_exists (&mem->dev.kobj, +kobject_name (&node_devices[nid]->dev.kobj)); +} +#ifdef CONFIG_HUGETLBFS /* * Handle per node hstate attribute [un]registration on transistions
fs/sysfs/symlink.c +21 -0
@@ -153,6 +153,27 @@ void sysfs_remove_link(struct kobject *kobj, const char *name) }EXPORT_SYMBOL_GPL (sysfs_remove_link); +/** +* sysfs_link_exists - test if a symlink exists in object's directory. +* @kobj: object we're acting for. +* @name: name of the symlink to test. +*/ +bool sysfs_link_exists (struct kobject *kobj,const char *name) +{ +struct kernfs_node *parent =NULL , *kn; + +if (!kobj) + parent = sysfs_root_kn; +else + parent = kobj->sd; + + kn =kernfs_find_and_get (parent, name); +kernfs_put (kn); + +return kn !=NULL ; +} +EXPORT_SYMBOL_GPL (sysfs_link_exists); +/** * sysfs_rename_link_ns - rename symlink in object's directory. * @kobj: object we're acting for.
include/linux/node.h +7 -0
@@ -138,6 +138,8 @@ extern void unregister_one_node(int nid);extern int register_cpu_under_node (unsigned int cpu,unsigned int nid);extern int unregister_cpu_under_node (unsigned int cpu,unsigned int nid);extern void unregister_memory_block_under_nodes (struct memory_block *mem_blk); +extern bool memory_block_registered_under_node (struct memory_block *mem, +int nid); extern int register_memory_node_under_compute_node(unsigned int mem_nid, unsigned int cpu_nid,
@@ -171,6 +173,11 @@ static inline int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)static inline void unregister_memory_block_under_nodes (struct memory_block *mem_blk) { } +static inline bool memory_block_registered_under_node (struct memory_block *mem, +int nid) +{ +return true; +}static inline void register_hugetlbfs_with_node (node_registration_func_t reg,node_registration_func_t unreg)
include/linux/sysfs.h +6 -0
@@ -265,6 +265,7 @@ int __must_check sysfs_create_link_nowarn(struct kobject *kobj,struct kobject *target, const char *name);void sysfs_remove_link (struct kobject *kobj,const char *name); +bool sysfs_link_exists (struct kobject *kobj,const char *name); int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target, const char *old_name, const char *new_name,
@@ -420,6 +421,11 @@ static inline void sysfs_remove_link(struct kobject *kobj, const char *name) { } +static inline bool sysfs_link_exists (struct kobject *kobj,const char *name) +{ +return true; +} +static inline int sysfs_rename_link_ns (struct kobject *k,struct kobject *t,const char *old_name,const char *new_name,const void *ns)
mm/memory_hotplug.c +26 -16
58 lines not on this page · raw message
Signature · 1 line
2.21.0
Quoted 252 lines · David Hildenbrand
try_offline_node()is pretty much broken right now:
- The node span is updated when onlining memory, not when adding it. We ignore memory that was mever onlined. Bad.
- We touch possible garbage memmaps. Thepfn_to_nid(pfn)can easily trigger a kernel panic. Bad for memory that is offline but also bad for subsection hotadd withZONE_DEVICE, whereby the memmap of the first PFN of a section might contain garbage.
- Sections belonging to mixed nodes are not properly considered.As memory blocks might belong to multiple nodes, we would have to walk all pageblocks (or at least subsections) within present sections. However, we don't have a way to identify whether a memmap that is not online was initialized (relevant for
ZONE_DEVICE). This makes things more complicated.Luckily, we can piggy pack on the node span and the sysfs links between memory blocks and the node. Currently, the node span is grown when calling
move_pfn_range_to_zone()- e.g., when onlining memory, and shrunk when removing memory, before callingtry_offline_node(). Sysfs links are created vialink_mem_sections(), e.g., during boot or when adding memory.If the node still spans memory or if any memory block is linked to the node in sysfs, we don't set the node offline. Without
CONFIG_NUMA, or withoutCONFIG_SYSFS, we will simply always detect the node as being linked to the memory block and not set the node offline.Add a way to test if a sysfs link exists.
Note: We will soon stop shrinking the
ZONE_DEVICEzone and the node span when removingZONE_DEVICEmemory to fix similar issues (acess of garbage memmaps) - until we have a reliable way to identify whether these memmaps were properly initialized. This implies later, that once a node hadZONE_DEVICEmemory, we won't be able to set a node offline - which should be acceptable.Since commit
f1dd2cd13c4b("mm,memory_hotplug: do not associate hotadded memory to zones until online") memory that is added is not assoziated with a zone/node (memmap not initialized). The introducing commit60a5a19e7419("memory-hotplug: remove sysfs file of node") already missed that we could have multiple nodes for a section and that the zone/node span is updated when onlining pages, not when adding them.I tested this by hotplugging two DIMMs to a memory-less and cpu-less NUMA node. The node is properly onlined when adding the DIMMs. When removing the DIMMs, the node is properly offlined.
Fixes 60a5a19e7419 ("memory-hotplug: remove sysfs file of node")Fixes f1dd2cd13c4b ("mm, memory_hotplug: do not associate hotadded memory to zones until online") # visiable after d0dc12e86b319Cc Tang Chen <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>Cc Andrew Morton <[email protected]>Cc Keith Busch <[email protected]>Cc Jiri Olsa <[email protected]>Cc "Peter Zijlstra (Intel)" <[email protected]>Cc Jani Nikula <[email protected]>Cc Nayna Jain <[email protected]>Cc Michal Hocko <[email protected]>Cc Oscar Salvador <[email protected]>Cc Stephen Rothwell <[email protected]>Cc Dan Williams <[email protected]>Cc Pavel Tatashin <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
I just realized that we are are missing a
try_online_node()call in the memremap path. Bad when adding device memory to a memory-less and cpu-less node - the pgdat is not initialized. Will also have to be fixed.We stop shrinking the
ZONE_DEVICEzone after the following patch:
[PATCH v6 04/10]mm/memory_hotplug: Don't access uninitialized memmaps inshrink_zone_span()
This implies, the above note regardingZONE_DEVICEon a node blocking a node from getting offlined until we sorted out how to properly shrink theZONE_DEVICEzone.This patch is especially important for:
[PATCH v6 05/10]mm/memory_hotplug: Shrink zones when offlining memory
As the BUG fixed with this patch becomes now easier to observe when memory is offlined (in contrast to when memory would never have been onlined before).As both patches are stable fixes and in next/master for a long time, we should probably pull this patch in front of both and also backport this patch at least to
Cc: [email protected] # v4.13+I have not checked yet if there are real blockers to do that. I guess not.
--- drivers/base/node.c | 9 +++++++++ fs/sysfs/symlink.c | 21 +++++++++++++++++++++ include/linux/node.h | 7 +++++++ include/linux/sysfs.h | 6 ++++++ mm/memory_hotplug.c | 42 ++++++++++++++++++++++++++---------------- 5 files changed, 69 insertions(+), 16 deletions(-)drivers/base/node.c +9
-0@@ -833,6 +833,15 @@ int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn) register_mem_sect_under_node); } +bool memory_block_registered_under_node (struct memory_block *mem,int nid) +{ +if (mem->nid == nid) +return true; +/* memory blocks can span multiple nodes. Check against the link. */ +return sysfs_link_exists (&mem->dev.kobj, +kobject_name (&node_devices[nid]->dev.kobj)); +} +#ifdef CONFIG_HUGETLBFS /* * Handle per node hstate attribute [un]registration on transistions fs/sysfs/symlink.c +21
-0@@ -153,6 +153,27 @@ void sysfs_remove_link(struct kobject *kobj, const char *name) }EXPORT_SYMBOL_GPL (sysfs_remove_link); +/** +* sysfs_link_exists - test if a symlink exists in object's directory. +* @kobj: object we're acting for. +* @name: name of the symlink to test. +*/ +bool sysfs_link_exists (struct kobject *kobj,const char *name) +{ +struct kernfs_node *parent =NULL , *kn; + +if (!kobj) + parent = sysfs_root_kn; +else + parent = kobj->sd; + + kn =kernfs_find_and_get (parent, name); +kernfs_put (kn); + +return kn !=NULL ; +} +EXPORT_SYMBOL_GPL (sysfs_link_exists); +/** * sysfs_rename_link_ns - rename symlink in object's directory. * @kobj: object we're acting for. include/linux/node.h +7
-0@@ -138,6 +138,8 @@ extern void unregister_one_node(int nid);extern int register_cpu_under_node (unsigned int cpu,unsigned int nid);extern int unregister_cpu_under_node (unsigned int cpu,unsigned int nid);extern void unregister_memory_block_under_nodes (struct memory_block *mem_blk); +extern bool memory_block_registered_under_node (struct memory_block *mem, +int nid); extern int register_memory_node_under_compute_node(unsigned int mem_nid, unsigned int cpu_nid,@@ -171,6 +173,11 @@ static inline int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)static inline void unregister_memory_block_under_nodes (struct memory_block *mem_blk) { } +static inline bool memory_block_registered_under_node (struct memory_block *mem, +int nid) +{ +return true; +}static inline void register_hugetlbfs_with_node (node_registration_func_t reg,node_registration_func_t unreg)include/linux/sysfs.h +6
-0@@ -265,6 +265,7 @@ int __must_check sysfs_create_link_nowarn(struct kobject *kobj,struct kobject *target, const char *name);void sysfs_remove_link (struct kobject *kobj,const char *name); +bool sysfs_link_exists (struct kobject *kobj,const char *name); int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target, const char *old_name, const char *new_name,@@ -420,6 +421,11 @@ static inline void sysfs_remove_link(struct kobject *kobj, const char *name) { } +static inline bool sysfs_link_exists (struct kobject *kobj,const char *name) +{ +return true; +} +static inline int sysfs_rename_link_ns (struct kobject *k,struct kobject *t,const char *old_name,const char *new_name,const void *ns)mm/memory_hotplug.c +21
-15@@ -1634,6 +1634,15 @@ static int check_cpu_on_node(pg_data_t *pgdat)return 0 ; } +static int check_no_memblock_registered_under_node_cb (struct memory_block *mem, +void *arg) +{ + +const int nid = *(int *)arg; + +return memory_block_registered_under_node (mem, nid) ? -EEXIST :0 ; +} +/** * try_offline_node * @nid: the node ID @@ -1645,26 +1654,27 @@ static int check_cpu_on_node(pg_data_t *pgdat)*/ void try_offline_node (int nid) { +unsigned long end_pfn =section_nr_to_pfn (__highest_present_section_nr);pg_data_t *pgdat =NODE_DATA (nid); -unsigned long start_pfn = pgdat->node_start_pfn; -unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages; -unsigned long pfn; - -for (pfn = start_pfn; pfn < end_pfn; pfn +=PAGES_PER_SECTION ) { -unsigned long section_nr =pfn_to_section_nr (pfn); - -if (!present_section_nr (section_nr)) -continue ; +int rc; -if (pfn_to_nid (pfn) != nid) -continue ; +/* +* If the node still spans pages (especially ZONE_DEVICE), don't +* offline it. A node spans memory after move_pfn_range_to_zone(), +* e.g., after the memory block was onlined. +*/ +if (pgdat->node_spanned_pages) +return ; -/* -* some memory sections of this node are not removed, and we -* can't offline node now. -*/ +/* +* Especially offline memory blocks might not be spanned by the +* node. They will get spanned by the node once they get onlined.
mm/memory_hotplug.c static int check_cpu_on_node(pg_data_t *pgdat) · line 1680
Quoted 10 lines
mm/memory_hotplug.c +5
-1+* However, they link to the node in sysfs and can get onlined later. +*/ + rc =walk_memory_blocks (0 ,PFN_PHYS (end_pfn), &nid, + check_no_memblock_registered_under_node_cb); +if (rc)return ; - }if (check_cpu_on_node (pgdat))return ;
I just realized that we don't need the complicated sysfs handling. Memory blocks that belong to multiple nodes can't get offlined and are therefore always spanned by the node. For offline memory blocks, we can rely on the stored nid. Will do a quick test and send the simpler variant :)
Signature · 3 lines
Thanks, David / dhildenb