Optimize zone->contiguous update and issue fix
Subsystems (5)
v8 cover Optimize zone->contiguous update and issue fix
This series contains 3 patches. The first one fix an issue when check the zone->contiguous during zone grows. The second one encapsulate the mhp_init_memmap_on_memory() and online_pages() into online_memory_block_pages(), the mhp_deinit_memmap_on_memory() and offline_pages() into offline_memory_block_pages(). The last one add a fast path to check the zone->contiguous. The issue fixed by the first patch can be found in the original code path.
Changes History
===============
v2 changes:
Add check_zone_contiguous_fast function to check zone contiguity for new memory PFN ranges.
v3 changes:
Add zone contiguity check for empty zones.
v4 changes:
1. Improve coding style.
2. Add fast path for zone contiguity check in memory unplugged cases, and update test results.
3. Refactor set_zone_contiguous: the new set_zone_contiguous updates zone contiguity based on the fast path results.
v5 changes:
1. Improve coding style.
2. Fix a issue in which zone->contiguous was always false when adding new memory, leveraging the fast path optimization.
v6 changes:
1. Improve coding style.
2. Add comments.
v7 changes:
1. Rebased to 6.19-rc1
2. Reorder the patches so that the fix will be the first in the series.
v8 changes:
1. Rebased to 6.19-rc6
2. Add online_memory_block_pages() and offline_memory_block_pages()
Tianyou Li (2):
mm/memory hotplug/unplug: Add online_memory_block_pages() and
offline_memory_block_pages()
mm/memory hotplug/unplug: Optimize zone->contiguous update when
changes pfn range
Yuan Liu (1):
mm/memory hotplug: Fix zone->contiguous always false when hotplug
drivers/base/memory.c | 53 +++---------- include/linux/memory_hotplug.h | 18 ++--- mm/internal.h | 8 +- mm/memory_hotplug.c | 136 +++++++++++++++++++++++++++++++-- mm/mm_init.c | 15 +++- 5 files changed, 167 insertions(+), 63 deletions(-)
Signature · 1 line
2.47.1
PATCH v8 1/3 mm/memory hotplug: Fix zone->contiguous always false when hotplug
From: Yuan Liu <[email protected]>
set_zone_contiguous() uses __pageblock_pfn_to_page() to detect pageblocks that either do not exist (hole) or that do not belong to the same zone.
__pageblock_pfn_to_page(), however, relies on pfn_to_online_page(), effectively always returning NULL for memory ranges that were not onlined yet. So when called on a range-to-be-onlined, it indicates a memory hole to set_zone_contiguous().
Consequently, the set_zone_contiguous() call in move_pfn_range_to_zone(), which happens early during memory onlining, will never detect a zone as being contiguous. Bad.
To fix the issue, move the set_zone_contiguous() call to a later stage in memory onlining, where pfn_to_online_page() will succeed:
after we mark the memory sections to be online.
--- mm/memory_hotplug.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
mm/memory_hotplug.c +7 -2
@@ -782,8 +782,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,memmap_init_range (nr_pages, nid,zone_idx (zone), start_pfn,0 ,MEMINIT_HOTPLUG , altmap, migratetype, isolate_pageblock); - -set_zone_contiguous (zone); }struct auto_movable_stats {
@@ -1205,6 +1203,13 @@ int online_pages(unsigned long pfn, unsigned long nr_pages, }online_pages_range (pfn, nr_pages); + +/* +* Now that the ranges are indicated as online, check whether the whole +* zone is contiguous. +*/ +set_zone_contiguous (zone); +adjust_present_page_count (pfn_to_page (pfn), group, nr_pages);if (node_arg.nid >=0 )
Signature · 1 line
2.47.1
Mike Rapoport
maintainerRe: [PATCH v8 1/3] mm/memory hotplug: Fix zone->contiguous always false when hotplug
Hi,
Quoted 15 lines · Tianyou Li
From: Yuan Liu <[email protected]>
set_zone_contiguous()uses__pageblock_pfn_to_page()to detect pageblocks that either do not exist (hole) or that do not belong to the same zone.
__pageblock_pfn_to_page(), however, relies onpfn_to_online_page(), effectively always returning NULL for memory ranges that were not onlined yet. So when called on a range-to-be-onlined, it indicates a memory hole toset_zone_contiguous().Consequently, the
set_zone_contiguous()call inmove_pfn_range_to_zone(), which happens early during memory onlining, will never detect a zone as being contiguous. Bad.
Quoted 5 lines
To fix the issue, move the
set_zone_contiguous()call to a later stage in memory onlining, wherepfn_to_online_page()will succeed:
after we mark the memory sections to be online.Fixes 2d070eab2e82 ("mm: consider zone which is not fully populated to have holes")
cc stable@ perhaps?
Quoted 13 lines
Cc Michal Hocko <[email protected]>Reviewed-by Nanhai Zou <[email protected]>Signed-off-by Yuan Liu <[email protected]>Signed-off-by Tianyou Li <[email protected]>--- mm/memory_hotplug.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)mm/memory_hotplug.c +0
-0
mm/memory_hotplug.c void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, · line 786
Quoted 5 lines
mm/memory_hotplug.c +0
-2@@ -782,8 +782,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,memmap_init_range (nr_pages, nid,zone_idx (zone), start_pfn,0 ,MEMINIT_HOTPLUG , altmap, migratetype, isolate_pageblock); - -set_zone_contiguous (zone);
move_pfn_range_to_zone() is also called from memremap::pagemap_range(). Shouldn't we add set_zone_contiguous() there as well?
Quoted 20 lines
mm/memory_hotplug.c +7
-0}struct auto_movable_stats {@@ -1205,6 +1203,13 @@ int online_pages(unsigned long pfn, unsigned long nr_pages, }online_pages_range (pfn, nr_pages); + +/* +* Now that the ranges are indicated as online, check whether the whole +* zone is contiguous. +*/ +set_zone_contiguous (zone); +adjust_present_page_count (pfn_to_page (pfn), group, nr_pages);if (node_arg.nid >=0 )Signature · 1 line
2.47.1
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v8 1/3] mm/memory hotplug: Fix zone->contiguous always false when hotplug
Appreciated for your review, Mike.
Quoted 18 lines · Mike Rapoport
Hi,
Quoted 15 lines · Tianyou Li
From: Yuan Liu <[email protected]>
set_zone_contiguous()uses__pageblock_pfn_to_page()to detect pageblocks that either do not exist (hole) or that do not belong to the same zone.
__pageblock_pfn_to_page(), however, relies onpfn_to_online_page(), effectively always returning NULL for memory ranges that were not onlined yet. So when called on a range-to-be-onlined, it indicates a memory hole toset_zone_contiguous().Consequently, the
set_zone_contiguous()call inmove_pfn_range_to_zone(), which happens early during memory onlining, will never detect a zone as being contiguous. Bad.
Quoted 6 lines
Quoted 5 lines
To fix the issue, move the
set_zone_contiguous()call to a later stage in memory onlining, wherepfn_to_online_page()will succeed:
after we mark the memory sections to be online.Fixes 2d070eab2e82 ("mm: consider zone which is not fully populated to have holes")cc stable@ perhaps?
Yes, will do. Thanks.
Quoted 9 lines
Quoted 9 lines
Cc Michal Hocko <[email protected]>Reviewed-by Nanhai Zou <[email protected]>Signed-off-by Yuan Liu <[email protected]>Signed-off-by Tianyou Li <[email protected]>--- mm/memory_hotplug.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
Quoted 12 lines
mm/memory_hotplug.c void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, · line 786
Quoted 10 lines
mm/memory_hotplug.c +0
-2@@ -782,8 +782,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,memmap_init_range (nr_pages, nid,zone_idx (zone), start_pfn,0 ,MEMINIT_HOTPLUG , altmap, migratetype, isolate_pageblock); - -set_zone_contiguous (zone);
move_pfn_range_to_zone()is also called frommemremap::pagemap_range(). Shouldn't we addset_zone_contiguous()there as well?
I did not find the place where the online_pages was invoked along path of the memremap:pagemap_range() function. Would there be other functions to online the pages remapped? Much appreciated for the guidance.
I leave the zone contiguous state remains the same as the optimization not took place in case any unexpected behavior.
Quoted 20 lines
Quoted 20 lines
mm/memory_hotplug.c +7
-0}struct auto_movable_stats {@@ -1205,6 +1203,13 @@ int online_pages(unsigned long pfn, unsigned long nr_pages, }online_pages_range (pfn, nr_pages); + +/* +* Now that the ranges are indicated as online, check whether the whole +* zone is contiguous. +*/ +set_zone_contiguous (zone); +adjust_present_page_count (pfn_to_page (pfn), group, nr_pages);if (node_arg.nid >=0 )Signature · 1 line
2.47.1
Andrew Morton
maintainerRe: [PATCH v8 1/3] mm/memory hotplug: Fix zone->contiguous always false when hotplug
Quoted 3 lines · Tianyou
Quoted 1 line
cc stable@ perhaps?
Yes, will do. Thanks.
Please separate backportable patches from those which target the next merge window. They have different timing and take different routes into mainline.
Thanks.
Re: [PATCH v8 1/3] mm/memory hotplug: Fix zone->contiguous always false when hotplug
Quoted 11 lines · Andrew Morton
Quoted 2 lines · Tianyou
Quoted 1 line
cc stable@ perhaps?
Yes, will do. Thanks.
Please separate backportable patches from those which target the next merge window. They have different timing and take different routes into mainline.
Thanks.
Appreciated Andrew for your kind explanation. My bad. Will separate it from the patch set in next version.
Again, thanks Mike for pointing it out.
Regards,
Tianyou
Mike Rapoport
maintainerRe: [PATCH v8 1/3] mm/memory hotplug: Fix zone->contiguous always false when hotplug
In reply to Li, TianyouHi,
Quoted 15 lines · Tianyou
Quoted 13 lines · Mike Rapoport
mm/memory_hotplug.c void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, · line 786
Quoted 10 lines · Tianyou Li
mm/memory_hotplug.c +0
-2@@ -782,8 +782,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,memmap_init_range (nr_pages, nid,zone_idx (zone), start_pfn,0 ,MEMINIT_HOTPLUG , altmap, migratetype, isolate_pageblock); - -set_zone_contiguous (zone);
move_pfn_range_to_zone()is also called frommemremap::pagemap_range(). Shouldn't we addset_zone_contiguous()there as well?
Quoted 3 lines
I did not find the place where the
online_pageswas invoked along path of the memremap:pagemap_range() function. Would there be other functions to online the pages remapped? Much appreciated for the guidance.
Currently when we do memremap_pages() we have
memremap_pages() -> pagemap_range() -> move_pfn_range_to_zone() -> set_zone_contiguous();
Once set_zone_contiguous() is moved out from move_pfn_range_to_zone(), memremap_pages() path never calls it.
I'm not sure if the pages added in memremap_pages() are online, but to keep it's current behaviour I think it should call set_zone_contiguous() explicitly.
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v8 1/3] mm/memory hotplug: Fix zone->contiguous always false when hotplug
Quoted 27 lines · Mike Rapoport
Hi,
Quoted 14 lines · Tianyou
Quoted 13 lines · Mike Rapoport
mm/memory_hotplug.c void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, · line 786
Quoted 10 lines · Tianyou Li
mm/memory_hotplug.c +0
-2@@ -782,8 +782,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,memmap_init_range (nr_pages, nid,zone_idx (zone), start_pfn,0 ,MEMINIT_HOTPLUG , altmap, migratetype, isolate_pageblock); - -set_zone_contiguous (zone);
move_pfn_range_to_zone()is also called frommemremap::pagemap_range(). Shouldn't we addset_zone_contiguous()there as well?Quoted 3 lines
I did not find the place where the
online_pageswas invoked along path of the memremap:pagemap_range() function. Would there be other functions to online the pages remapped? Much appreciated for the guidance.Currently when we do
memremap_pages()we havememremap_pages() -> pagemap_range() -> move_pfn_range_to_zone() -> set_zone_contiguous();
Quoted 5 lines
Once
set_zone_contiguous()is moved out frommove_pfn_range_to_zone(),memremap_pages()path never calls it.
I'm not sure if the pages added inmemremap_pages()are online, but to keep it's current behaviour I think it should callset_zone_contiguous()explicitly.
Thanks Mike. It's doable for me to add such a line. I am worried about place a set_zone_contiguous() will not add any meaningful value. Per my understanding, the zone contiguous will remain as false because the page is not online.
Regards,
Tianyou
PATCH v8 2/3 mm/memory hotplug/unplug: Add online_memory_block_pages() and offline_memory_block_pages()
In reply to Tianyou LiEncapsulate the mhp_init_memmap_on_memory() and online_pages() into online_memory_block_pages(). Thus we can further optimize the set_zone_contiguous() to check the whole memory block range, instead of check the zone contiguous in separate range.
Correspondingly, encapsulate the mhp_deinit_memmap_on_memory() and offline_pages() into offline_memory_block_pages().
--- drivers/base/memory.c | 53 ++++++--------------------- include/linux/memory_hotplug.h | 18 +++++----- mm/memory_hotplug.c | 65 +++++++++++++++++++++++++++++++--- 3 files changed, 80 insertions(+), 56 deletions(-)
drivers/base/memory.c +10 -43
@@ -246,31 +246,12 @@ static int memory_block_online(struct memory_block *mem) nr_vmemmap_pages = mem->altmap->free;mem_hotplug_begin (); -if (nr_vmemmap_pages) { - ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone); -if (ret) - goto out; - } - - ret =online_pages (start_pfn + nr_vmemmap_pages, - nr_pages - nr_vmemmap_pages, zone, mem->group); -if (ret) { -if (nr_vmemmap_pages) -mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); - goto out; - } - -/* -* Account once onlining succeeded. If the zone was unpopulated, it is -* now already properly populated. -*/ -if (nr_vmemmap_pages) -adjust_present_page_count (pfn_to_page (start_pfn), mem->group, - nr_vmemmap_pages); - - mem->zone = zone; -out: + ret =online_memory_block_pages (start_pfn, nr_pages, nr_vmemmap_pages, + zone, mem->group); +if (!ret) + mem->zone = zone;mem_hotplug_done (); +return ret; }
@@ -295,26 +276,12 @@ static int memory_block_offline(struct memory_block *mem) nr_vmemmap_pages = mem->altmap->free;mem_hotplug_begin (); -if (nr_vmemmap_pages) -adjust_present_page_count (pfn_to_page (start_pfn), mem->group, - -nr_vmemmap_pages); - - ret =offline_pages (start_pfn + nr_vmemmap_pages, - nr_pages - nr_vmemmap_pages, mem->zone, mem->group); -if (ret) { -/* offline_pages() failed. Account back. */ -if (nr_vmemmap_pages) -adjust_present_page_count (pfn_to_page (start_pfn), - mem->group, nr_vmemmap_pages); - goto out; - } - -if (nr_vmemmap_pages) -mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); - - mem->zone =NULL ; -out: + ret =offline_memory_block_pages (start_pfn, nr_pages, nr_vmemmap_pages, + mem->zone, mem->group); +if (!ret) + mem->zone =NULL ;mem_hotplug_done (); +return ret; }
include/linux/memory_hotplug.h +9 -9
@@ -106,11 +106,9 @@ extern void adjust_present_page_count(struct page *page,struct memory_group *group, long nr_pages);/* VM interface that may be used by firmware interface */ -extern int mhp_init_memmap_on_memory (unsigned long pfn,unsigned long nr_pages, -struct zone *zone); -extern void mhp_deinit_memmap_on_memory (unsigned long pfn,unsigned long nr_pages); -extern int online_pages (unsigned long pfn,unsigned long nr_pages, -struct zone *zone,struct memory_group *group); +extern int online_memory_block_pages (unsigned long start_pfn, +unsigned long nr_pages,unsigned long nr_vmemmap_pages, +struct zone *zone,struct memory_group *group);extern unsigned long __offline_isolated_pages (unsigned long start_pfn,unsigned long end_pfn);
@@ -261,8 +259,9 @@ static inline void pgdat_resize_init(struct pglist_data *pgdat) {}#ifdef CONFIG_MEMORY_HOTREMOVE extern void try_offline_node (int nid); -extern int offline_pages (unsigned long start_pfn,unsigned long nr_pages, -struct zone *zone,struct memory_group *group); +extern int offline_memory_block_pages (unsigned long start_pfn, +unsigned long nr_pages,unsigned long nr_vmemmap_pages, +struct zone *zone,struct memory_group *group);extern int remove_memory (u64 start,u64 size);extern void __remove_memory (u64 start,u64 size);extern int offline_and_remove_memory (u64 start,u64 size);
@@ -270,8 +269,9 @@ extern int offline_and_remove_memory(u64 start, u64 size);#else static inline void try_offline_node (int nid) {} -static inline int offline_pages (unsigned long start_pfn,unsigned long nr_pages, -struct zone *zone,struct memory_group *group) +static inline int offline_memory_block_pages (unsigned long start_pfn, +unsigned long nr_pages,unsigned long nr_vmemmap_pages, +struct zone *zone,struct memory_group *group) {return -EINVAL ; }
mm/memory_hotplug.c +61 -4
101 lines not on this page · raw message
Signature · 1 line
2.47.1
Mike Rapoport
maintainerRe: [PATCH v8 2/3] mm/memory hotplug/unplug: Add online_memory_block_pages() and offline_memory_block_pages()
Hi,
Quoted 43 lines · Tianyou Li
Encapsulate the
mhp_init_memmap_on_memory()andonline_pages()intoonline_memory_block_pages(). Thus we can further optimize theset_zone_contiguous()to check the whole memory block range, instead of check the zone contiguous in separate range.Correspondingly, encapsulate the
mhp_deinit_memmap_on_memory()andoffline_pages()intooffline_memory_block_pages().Tested-by Yuan Liu <[email protected]>Reviewed-by Yuan Liu <[email protected]>Signed-off-by Tianyou Li <[email protected]>--- drivers/base/memory.c | 53 ++++++--------------------- include/linux/memory_hotplug.h | 18 +++++----- mm/memory_hotplug.c | 65 +++++++++++++++++++++++++++++++--- 3 files changed, 80 insertions(+), 56 deletions(-)drivers/base/memory.c +0
-18@@ -246,31 +246,12 @@ static int memory_block_online(struct memory_block *mem) nr_vmemmap_pages = mem->altmap->free;mem_hotplug_begin (); -if (nr_vmemmap_pages) { - ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone); -if (ret) - goto out; - } - - ret =online_pages (start_pfn + nr_vmemmap_pages, - nr_pages - nr_vmemmap_pages, zone, mem->group); -if (ret) { -if (nr_vmemmap_pages) -mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); - goto out; - } - -/* -* Account once onlining succeeded. If the zone was unpopulated, it is -* now already properly populated. -*/
drivers/base/memory.c static int memory_block_online(struct memory_block *mem) · line 252
Quoted 10 lines
drivers/base/memory.c +4
-6-if (nr_vmemmap_pages) -adjust_present_page_count (pfn_to_page (start_pfn), mem->group, - nr_vmemmap_pages); - - mem->zone = zone; -out: + ret =online_memory_block_pages (start_pfn, nr_pages, nr_vmemmap_pages, + zone, mem->group); +if (!ret) + mem->zone = zone;
I think we can move most of memory_block_online() to the new function and pass struct memory_block to it.
I'd suggest
int mhp_block_online(struct memory_block *block)
and
int mhp_block_offline(struct memory_block *block)
Other than that LGTM.
Quoted 5 lines
drivers/base/memory.c +1
-0mem_hotplug_done (); +return ret; }
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v8 2/3] mm/memory hotplug/unplug: Add online_memory_block_pages() and offline_memory_block_pages()
Quoted 60 lines · Mike Rapoport
Hi,
Quoted 43 lines · Tianyou Li
Encapsulate the
mhp_init_memmap_on_memory()andonline_pages()intoonline_memory_block_pages(). Thus we can further optimize theset_zone_contiguous()to check the whole memory block range, instead of check the zone contiguous in separate range.Correspondingly, encapsulate the
mhp_deinit_memmap_on_memory()andoffline_pages()intooffline_memory_block_pages().Tested-by Yuan Liu <[email protected]>Reviewed-by Yuan Liu <[email protected]>Signed-off-by Tianyou Li <[email protected]>--- drivers/base/memory.c | 53 ++++++--------------------- include/linux/memory_hotplug.h | 18 +++++----- mm/memory_hotplug.c | 65 +++++++++++++++++++++++++++++++--- 3 files changed, 80 insertions(+), 56 deletions(-)drivers/base/memory.c +0
-18@@ -246,31 +246,12 @@ static int memory_block_online(struct memory_block *mem) nr_vmemmap_pages = mem->altmap->free;mem_hotplug_begin (); -if (nr_vmemmap_pages) { - ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone); -if (ret) - goto out; - } - - ret =online_pages (start_pfn + nr_vmemmap_pages, - nr_pages - nr_vmemmap_pages, zone, mem->group); -if (ret) { -if (nr_vmemmap_pages) -mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); - goto out; - } - -/* -* Account once onlining succeeded. If the zone was unpopulated, it is -* now already properly populated. -*/ drivers/base/memory.c static int memory_block_online(struct memory_block *mem) · line 252
Quoted 10 lines
drivers/base/memory.c +4
-6-if (nr_vmemmap_pages) -adjust_present_page_count (pfn_to_page (start_pfn), mem->group, - nr_vmemmap_pages); - - mem->zone = zone; -out: + ret =online_memory_block_pages (start_pfn, nr_pages, nr_vmemmap_pages, + zone, mem->group); +if (!ret) + mem->zone = zone;I think we can move most of
memory_block_online()to the new function and pass structmemory_blockto it.
I'd suggest
Quoted 7 lines
int
mhp_block_online(struct memory_block *block)and
int
mhp_block_offline(struct memory_block *block)Other than that LGTM.
It's doable, if not other comments I can change the code. Would it look like moving the functions to mm/memory_hotplug.c, change the name to mhp_block_online() and mhp_block_offline(), and change the references where the original function invoked in drivers/base/memory.c?
My prior thoughts on this was just break the code as small pieces as necessary to handle the pages online part together with zone contiguous state update.
Quoted 6 lines
Quoted 6 lines
drivers/base/memory.c +1
-0mem_hotplug_done (); +return ret; }
Mike Rapoport
maintainerRe: [PATCH v8 2/3] mm/memory hotplug/unplug: Add online_memory_block_pages() and offline_memory_block_pages()
Quoted 48 lines · Tianyou
Quoted 45 lines · Mike Rapoport
Quoted 25 lines
drivers/base/memory.c +0
-18@@ -246,31 +246,12 @@ static int memory_block_online(struct memory_block *mem) nr_vmemmap_pages = mem->altmap->free;mem_hotplug_begin (); -if (nr_vmemmap_pages) { - ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone); -if (ret) - goto out; - } - - ret =online_pages (start_pfn + nr_vmemmap_pages, - nr_pages - nr_vmemmap_pages, zone, mem->group); -if (ret) { -if (nr_vmemmap_pages) -mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); - goto out; - } - -/* -* Account once onlining succeeded. If the zone was unpopulated, it is -* now already properly populated. -*/ drivers/base/memory.c static int memory_block_online(struct memory_block *mem) · line 251
Quoted 10 lines
drivers/base/memory.c +4
-6-if (nr_vmemmap_pages) -adjust_present_page_count (pfn_to_page (start_pfn), mem->group, - nr_vmemmap_pages); - - mem->zone = zone; -out: + ret =online_memory_block_pages (start_pfn, nr_pages, nr_vmemmap_pages, + zone, mem->group); +if (!ret) + mem->zone = zone;I think we can move most of
memory_block_online()to the new function and pass structmemory_blockto it.
I'd suggestint
mhp_block_online(struct memory_block *block)and
int
mhp_block_offline(struct memory_block *block)
Quoted 7 lines
Quoted 1 line
Other than that LGTM.
It's doable, if not other comments I can change the code. Would it look like moving the functions to
mm/memory_hotplug.c, change the name tomhp_block_online()andmhp_block_offline(), and change the references where the original function invoked indrivers/base/memory.c?
Yeah, that's what I thought about.
Even more broadly, I think the functionality in drivers/base/memory.c belongs to mm/ much more than to drivers/ but that's surely out of scope for these patches.
Quoted 3 lines
My prior thoughts on this was just break the code as small pieces as necessary to handle the pages online part together with zone contiguous state update.
IMHO moving the entire function is cleaner, let's hear what David and Oscar think.
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v8 2/3] mm/memory hotplug/unplug: Add online_memory_block_pages() and offline_memory_block_pages()
Quoted 53 lines · Mike Rapoport
Quoted 44 lines · Tianyou
Quoted 43 lines · Mike Rapoport
Quoted 25 lines
drivers/base/memory.c +0
-18@@ -246,31 +246,12 @@ static int memory_block_online(struct memory_block *mem) nr_vmemmap_pages = mem->altmap->free;mem_hotplug_begin (); -if (nr_vmemmap_pages) { - ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone); -if (ret) - goto out; - } - - ret =online_pages (start_pfn + nr_vmemmap_pages, - nr_pages - nr_vmemmap_pages, zone, mem->group); -if (ret) { -if (nr_vmemmap_pages) -mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); - goto out; - } - -/* -* Account once onlining succeeded. If the zone was unpopulated, it is -* now already properly populated. -*/ drivers/base/memory.c static int memory_block_online(struct memory_block *mem) · line 251
Quoted 10 lines
drivers/base/memory.c +4
-6-if (nr_vmemmap_pages) -adjust_present_page_count (pfn_to_page (start_pfn), mem->group, - nr_vmemmap_pages); - - mem->zone = zone; -out: + ret =online_memory_block_pages (start_pfn, nr_pages, nr_vmemmap_pages, + zone, mem->group); +if (!ret) + mem->zone = zone;I think we can move most of
memory_block_online()to the new function and pass structmemory_blockto it.
I'd suggestint
mhp_block_online(struct memory_block *block)and
Quoted 8 lines
Quoted 3 lines
int
mhp_block_offline(struct memory_block *block)Other than that LGTM.
It's doable, if not other comments I can change the code. Would it look like moving the functions to
mm/memory_hotplug.c, change the name tomhp_block_online()andmhp_block_offline(), and change the references where the original function invoked indrivers/base/memory.c?
Quoted 6 lines
Yeah, that's what I thought about.
Even more broadly, I think the functionality in
drivers/base/memory.cbelongs tomm/much more than todrivers/but that's surely out of scope for these patches.
Thanks Mike for the confirmation.
Quoted 5 lines
Quoted 3 lines
My prior thoughts on this was just break the code as small pieces as necessary to handle the pages online part together with zone contiguous state update.
IMHO moving the entire function is cleaner, let's hear what David and Oscar think.
Sure. Actually the patch has been ready with the mhp_block_online() and mhp_block_offline() moved to memory_hotplug.c, the online_memory_block_pages() and offline_memory_block_pages() remains but as static function.
Once get feedback from David and Oscar, I can change them accordingly.
PATCH v8 3/3 mm/memory hotplug/unplug: Optimize zone->contiguous update when changes pfn range
In reply to Tianyou LiWhen invoke move_pfn_range_to_zone or remove_pfn_range_from_zone, it will update the zone->contiguous by checking the new zone's pfn range from the beginning to the end, regardless the previous state of the old zone. When the zone's pfn range is large, the cost of traversing the pfn range to update the zone->contiguous could be significant.
Add fast paths to quickly detect cases where zone is definitely not contiguous without scanning the new zone. The cases are: when the new range did not overlap with previous range, the contiguous should be false; if the new range adjacent with the previous range, just need to check the new range; if the new added pages could not fill the hole of previous zone, the contiguous should be false.
The following test cases of memory hotplug for a VM [1], tested in the environment [2], show that this optimization can significantly reduce the memory hotplug time [3].
+----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Plug Memory | 256G | 10s | 2s | 80% | | +------+---------------+--------------+----------------+ | | 512G | 33s | 6s | 81% | +----------------+------+---------------+--------------+----------------+
+----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Unplug Memory | 256G | 10s | 2s | 80% | | +------+---------------+--------------+----------------+ | | 512G | 34s | 6s | 82% | +----------------+------+---------------+--------------+----------------+
[1] Qemu commands to hotplug 256G/512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=256G/512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 256G/512G (Plug Memory) qom-set vmem1 requested-size 0G (Unplug Memory)
[2] Hardware : Intel Icelake server
Guest Kernel : v6.18-rc2
Qemu : v9.0.0
Launch VM :
qemu-system-x86_64 -accel kvm -cpu host \
-drive file=./Centos10_cloud.qcow2,format=qcow2,if=virtio \
-drive file=./seed.img,format=raw,if=virtio \
-smp 3,cores=3,threads=1,sockets=1,maxcpus=3 \
-m 2G,slots=10,maxmem=2052472M \
-device pcie-root-port,id=port1,bus=pcie.0,slot=1,multifunction=on \
-device pcie-root-port,id=port2,bus=pcie.0,slot=2 \
-nographic -machine q35 \
-nic user,hostfwd=tcp::3000-:22
Guest kernel auto-onlines newly added memory blocks:
echo online > /sys/devices/system/memory/auto_online_blocks[3] The time from typing the QEMU commands in [1] to when the output of 'grep MemTotal /proc/meminfo' on Guest reflects that all hotplugged memory is recognized.
--- mm/internal.h | 8 ++++- mm/memory_hotplug.c | 86 +++++++++++++++++++++++++++++++++++++-------- mm/mm_init.c | 15 ++++++-- 3 files changed, 92 insertions(+), 17 deletions(-)
mm/internal.h +7 -1
@@ -730,7 +730,13 @@ static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,return __pageblock_pfn_to_page (start_pfn, end_pfn, zone); } -void set_zone_contiguous (struct zone *zone); +enum zone_contig_state { +ZONE_CONTIG_YES , +ZONE_CONTIG_NO , +ZONE_CONTIG_MAYBE , +}; + +void set_zone_contiguous (struct zone *zone,enum zone_contig_state state);bool pfn_range_intersects_zones (int nid,unsigned long start_pfn,unsigned long nr_pages);
mm/memory_hotplug.c +72 -14
151 lines not on this page · raw message
mm/mm_init.c +13 -2
@@ -2263,11 +2263,22 @@ void __init init_cma_pageblock(struct page *page) }#endif -void set_zone_contiguous (struct zone *zone) +void set_zone_contiguous (struct zone *zone,enum zone_contig_state state) {unsigned long block_start_pfn = zone->zone_start_pfn;unsigned long block_end_pfn; +/* We expect an earlier call to clear_zone_contiguous(). */ +VM_WARN_ON_ONCE (zone->contiguous); + +if (state ==ZONE_CONTIG_YES ) { + zone->contiguous = true; +return ; + } + +if (state ==ZONE_CONTIG_NO ) +return ; + block_end_pfn =pageblock_end_pfn (block_start_pfn); for (; block_start_pfn < zone_end_pfn(zone); block_start_pfn = block_end_pfn,
@@ -2348,7 +2359,7 @@ void __init page_alloc_init_late(void)shuffle_free_memory (NODE_DATA (nid));for_each_populated_zone (zone) -set_zone_contiguous (zone); +set_zone_contiguous (zone,ZONE_CONTIG_MAYBE );/* Initialize page ext after all struct pages are initialized. */ if (deferred_struct_pages)
Signature · 1 line
2.47.1
Mike Rapoport
maintainerRe: [PATCH v8 3/3] mm/memory hotplug/unplug: Optimize zone->contiguous update when changes pfn range
Hi,
Quoted 72 lines · Tianyou Li
When invoke
move_pfn_range_to_zoneorremove_pfn_range_from_zone, it will update thezone->contiguousby checking the new zone's pfn range from the beginning to the end, regardless the previous state of the old zone. When the zone's pfn range is large, the cost of traversing the pfn range to update thezone->contiguouscould be significant.Add fast paths to quickly detect cases where zone is definitely not contiguous without scanning the new zone. The cases are: when the new range did not overlap with previous range, the contiguous should be false; if the new range adjacent with the previous range, just need to check the new range; if the new added pages could not fill the hole of previous zone, the contiguous should be false.
The following test cases of memory hotplug for a VM [1], tested in the environment [2], show that this optimization can significantly reduce the memory hotplug time [3].
+----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Plug Memory | 256G | 10s | 2s | 80% | | +------+---------------+--------------+----------------+ | | 512G | 33s | 6s | 81% | +----------------+------+---------------+--------------+----------------++----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Unplug Memory | 256G | 10s | 2s | 80% | | +------+---------------+--------------+----------------+ | | 512G | 34s | 6s | 82% | +----------------+------+---------------+--------------+----------------+[1] Qemu commands to hotplug 256G/512G memory for a VM:
object_addmemory-backend-ram,id=hotmem0,size=256G/512G,share=ondevice_addvirtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 256G/512G (Plug Memory) qom-set vmem1 requested-size 0G (Unplug Memory)[2] Hardware : Intel Icelake server
Guest Kernel : v6.18-rc2 Qemu : v9.0.0 Launch VM : qemu-system-x86_64 -accel kvm -cpu host \ -drive file=./Centos10_cloud.qcow2,format=qcow2,if=virtio \ -drive file=./seed.img,format=raw,if=virtio \ -smp 3,cores=3,threads=1,sockets=1,maxcpus=3 \ -m 2G,slots=10,maxmem=2052472M \ -device pcie-root-port,id=port1,bus=pcie.0,slot=1,multifunction=on \ -device pcie-root-port,id=port2,bus=pcie.0,slot=2 \ -nographic -machine q35 \ -nic user,hostfwd=tcp::3000-:22 Guest kernel auto-onlines newly added memory blocks: echo online > /sys/devices/system/memory/auto_online_blocks[3] The time from typing the QEMU commands in [1] to when the output of 'grep MemTotal
/proc/meminfo' on Guest reflects that all hotplugged memory is recognized.Reported-by Nanhai Zou <[email protected]>Reported-by Chen Zhang <[email protected]>Tested-by Yuan Liu <[email protected]>Reviewed-by Tim Chen <[email protected]>Reviewed-by Qiuxu Zhuo <[email protected]>Reviewed-by Yu C Chen <[email protected]>Reviewed-by Pan Deng <[email protected]>Reviewed-by Nanhai Zou <[email protected]>Reviewed-by Yuan Liu <[email protected]>Signed-off-by Tianyou Li <[email protected]>---
...
Quoted 10 lines
mm/memory_hotplug.c +7
-0+int online_memory_block_pages (unsigned long start_pfn,unsigned long nr_pages, +unsigned long nr_vmemmap_pages,struct zone *zone, +struct memory_group *group) { +const bool contiguous = zone->contiguous; +enum zone_contig_state new_contiguous_state;int ret; +/* +* Calculate the new zone contig state before move_pfn_range_to_zone()
mm/memory_hotplug.c static int online_pages(unsigned long pfn, unsigned long nr_pages, · line 1315
Quoted 10 lines
mm/memory_hotplug.c +6
-1+* sets the zone temporarily to non-contiguous. +*/ + new_contiguous_state =zone_contig_state_after_growing (zone, start_pfn, + nr_pages); +if (nr_vmemmap_pages) { ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone);if (ret) -return ret; + goto restore_zone_contig;
But zone_contig_state_after_growing() does not change zone->contiguous. Why do we need to save and restore it?
Quoted 27 lines
mm/memory_hotplug.c +10
-1} ret = online_pages(start_pfn + nr_vmemmap_pages,@@ -1271,7 +1320,7 @@ int online_memory_block_pages(unsigned long start_pfn,if (ret) {if (nr_vmemmap_pages)mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); -return ret; + goto restore_zone_contig; }/* @@ -1282,6 +1331,15 @@ int online_memory_block_pages(unsigned long start_pfn,adjust_present_page_count (pfn_to_page (start_pfn), group, nr_vmemmap_pages); +/* +* Now that the ranges are indicated as online, check whether the whole +* zone is contiguous. +*/ +set_zone_contiguous (zone, new_contiguous_state); +return 0 ; + +restore_zone_contig: + zone->contiguous = contiguous;return ret; }
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v8 3/3] mm/memory hotplug/unplug: Optimize zone->contiguous update when changes pfn range
Quoted 85 lines · Mike Rapoport
Hi,
Quoted 72 lines · Tianyou Li
When invoke
move_pfn_range_to_zoneorremove_pfn_range_from_zone, it will update thezone->contiguousby checking the new zone's pfn range from the beginning to the end, regardless the previous state of the old zone. When the zone's pfn range is large, the cost of traversing the pfn range to update thezone->contiguouscould be significant.Add fast paths to quickly detect cases where zone is definitely not contiguous without scanning the new zone. The cases are: when the new range did not overlap with previous range, the contiguous should be false; if the new range adjacent with the previous range, just need to check the new range; if the new added pages could not fill the hole of previous zone, the contiguous should be false.
The following test cases of memory hotplug for a VM [1], tested in the environment [2], show that this optimization can significantly reduce the memory hotplug time [3].
+----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Plug Memory | 256G | 10s | 2s | 80% | | +------+---------------+--------------+----------------+ | | 512G | 33s | 6s | 81% | +----------------+------+---------------+--------------+----------------++----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Unplug Memory | 256G | 10s | 2s | 80% | | +------+---------------+--------------+----------------+ | | 512G | 34s | 6s | 82% | +----------------+------+---------------+--------------+----------------+[1] Qemu commands to hotplug 256G/512G memory for a VM:
object_addmemory-backend-ram,id=hotmem0,size=256G/512G,share=ondevice_addvirtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 256G/512G (Plug Memory) qom-set vmem1 requested-size 0G (Unplug Memory)[2] Hardware : Intel Icelake server
Guest Kernel : v6.18-rc2 Qemu : v9.0.0 Launch VM : qemu-system-x86_64 -accel kvm -cpu host \ -drive file=./Centos10_cloud.qcow2,format=qcow2,if=virtio \ -drive file=./seed.img,format=raw,if=virtio \ -smp 3,cores=3,threads=1,sockets=1,maxcpus=3 \ -m 2G,slots=10,maxmem=2052472M \ -device pcie-root-port,id=port1,bus=pcie.0,slot=1,multifunction=on \ -device pcie-root-port,id=port2,bus=pcie.0,slot=2 \ -nographic -machine q35 \ -nic user,hostfwd=tcp::3000-:22 Guest kernel auto-onlines newly added memory blocks: echo online > /sys/devices/system/memory/auto_online_blocks[3] The time from typing the QEMU commands in [1] to when the output of 'grep MemTotal
/proc/meminfo' on Guest reflects that all hotplugged memory is recognized.Reported-by Nanhai Zou <[email protected]>Reported-by Chen Zhang <[email protected]>Tested-by Yuan Liu <[email protected]>Reviewed-by Tim Chen <[email protected]>Reviewed-by Qiuxu Zhuo <[email protected]>Reviewed-by Yu C Chen <[email protected]>Reviewed-by Pan Deng <[email protected]>Reviewed-by Nanhai Zou <[email protected]>Reviewed-by Yuan Liu <[email protected]>Signed-off-by Tianyou Li <[email protected]>---
...
Quoted 8 lines
mm/memory_hotplug.c +5
-0+int online_memory_block_pages (unsigned long start_pfn,unsigned long nr_pages, +unsigned long nr_vmemmap_pages,struct zone *zone, +struct memory_group *group) { +const bool contiguous = zone->contiguous; +enum zone_contig_state new_contiguous_state;int ret;
Quoted 14 lines
mm/memory_hotplug.c static int online_pages(unsigned long pfn, unsigned long nr_pages, · line 1315
Quoted 12 lines
mm/memory_hotplug.c +8
-1+/* +* Calculate the new zone contig state before move_pfn_range_to_zone() +* sets the zone temporarily to non-contiguous. +*/ + new_contiguous_state =zone_contig_state_after_growing (zone, start_pfn, + nr_pages); +if (nr_vmemmap_pages) { ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone);if (ret) -return ret; + goto restore_zone_contig;But
zone_contig_state_after_growing()does not changezone->contiguous. Why do we need to save and restore it?
Move_pfn_range_to_zone() will clear the zone contiguous state and it was invoked by online_pages(). If error occurs after move_pfn_range_to_zone() called like in online_pages(), I think we'd better to restore the original value if previous zone contiguous state is true.
Quoted 28 lines
Quoted 28 lines
}ret = online_pages(start_pfn +
nr_vmemmap_pages,mm/memory_hotplug.c +10
-1@@ -1271,7 +1320,7 @@ int online_memory_block_pages(unsigned long start_pfn,if (ret) {if (nr_vmemmap_pages)mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); -return ret; + goto restore_zone_contig; }/* @@ -1282,6 +1331,15 @@ int online_memory_block_pages(unsigned long start_pfn,adjust_present_page_count (pfn_to_page (start_pfn), group, nr_vmemmap_pages); +/* +* Now that the ranges are indicated as online, check whether the whole +* zone is contiguous. +*/ +set_zone_contiguous (zone, new_contiguous_state); +return 0 ; + +restore_zone_contig: + zone->contiguous = contiguous;return ret; }
Mike Rapoport
maintainerRe: [PATCH v8 3/3] mm/memory hotplug/unplug: Optimize zone->contiguous update when changes pfn range
Hi,
Quoted 24 lines · Tianyou
Quoted 13 lines · Mike Rapoport
Quoted 13 lines
mm/memory_hotplug.c +11
-0+int online_memory_block_pages (unsigned long start_pfn,unsigned long nr_pages, +unsigned long nr_vmemmap_pages,struct zone *zone, +struct memory_group *group) { +const bool contiguous = zone->contiguous; +enum zone_contig_state new_contiguous_state;int ret; +/* +* Calculate the new zone contig state before move_pfn_range_to_zone() +* sets the zone temporarily to non-contiguous. +*/ + new_contiguous_state =zone_contig_state_after_growing (zone, start_pfn, + nr_pages);Quoted 8 lines
mm/memory_hotplug.c static int online_pages(unsigned long pfn, unsigned long nr_pages, · line 1315
Quoted 6 lines
mm/memory_hotplug.c +2
-1+if (nr_vmemmap_pages) { ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone);if (ret) -return ret; + goto restore_zone_contig;But
zone_contig_state_after_growing()does not changezone->contiguous. Why do we need to save and restore it?
Quoted 4 lines
Move_pfn_range_to_zone()will clear the zone contiguous state and it was invoked byonline_pages(). If error occurs after move_pfn_range_to_zone() called like inonline_pages(), I think we'd better to restore the original value if previous zone contiguous state is true.
But after move_pfn_range_to_zone() the added pages are still offline, so I think the zone remains contiguous and the call to clear_zone_contiguous(zone) should not be there.
BTW, as we have set_zone_contiguous(ZONE_CONTIG_NO) I think we can use it instead if clear_zone_contiguous() and remove the latter.
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v8 3/3] mm/memory hotplug/unplug: Optimize zone->contiguous update when changes pfn range
Quoted 26 lines · Mike Rapoport
Hi,
Quoted 23 lines · Tianyou
Quoted 21 lines · Mike Rapoport
Quoted 9 lines
mm/memory_hotplug.c +7
-0+int online_memory_block_pages (unsigned long start_pfn,unsigned long nr_pages, +unsigned long nr_vmemmap_pages,struct zone *zone, +struct memory_group *group) { +const bool contiguous = zone->contiguous; +enum zone_contig_state new_contiguous_state;int ret; +/* +* Calculate the new zone contig state before move_pfn_range_to_zone() mm/memory_hotplug.c static int online_pages(unsigned long pfn, unsigned long nr_pages, · line 1315
Quoted 10 lines
mm/memory_hotplug.c +6
-1+* sets the zone temporarily to non-contiguous. +*/ + new_contiguous_state =zone_contig_state_after_growing (zone, start_pfn, + nr_pages); +if (nr_vmemmap_pages) { ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone);if (ret) -return ret; + goto restore_zone_contig;But
zone_contig_state_after_growing()does not changezone->contiguous. Why do we need to save and restore it?
Quoted 8 lines
Quoted 4 lines
Move_pfn_range_to_zone()will clear the zone contiguous state and it was invoked byonline_pages(). If error occurs after move_pfn_range_to_zone() called like inonline_pages(), I think we'd better to restore the original value if previous zone contiguous state is true.But after
move_pfn_range_to_zone()the added pages are still offline, so I think the zone remains contiguous and the call toclear_zone_contiguous(zone)should not be there.
Since move_pfn_range_to_zone() may change the zone contiguous state, clear_zone_contiguous() should be invoked. If we did not clear_zone_contiguous() properly, especially keep the zone contiguous state as true but actually the zone is non contiguous after resize the zone, the code path rely on the contiguous state potentially may fail?
I am not sure if we need to handle the clear_zone_contiguous() in or out of move_pfn_range_to_zone() in this patch series.
Quoted 3 lines
BTW, as we have
set_zone_contiguous(ZONE_CONTIG_NO)I think we can use it instead ifclear_zone_contiguous()and remove the latter.
Yes we can. I am hesitate to do so because clear_zone_contiguous() has a name that explain itself. The pair of clear/set seems a pattern should be preserved? I am OK to change the code, let's hear from other comments if feasible? Thanks.
Regards,
Tianyou