Skip to contentSkip to conversation

mm, hotplug: protect nr_zones with pgdat_resize_lock()

PATCH In review Wei Yang 4 participants 40 messages Nov 20, 2018 – Dec 3, 2018

Subsystems (5)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 mm, hotplug: protect nr_zones with pgdat_resize_lock()

After memory hot-added, users could online pages through sysfs, and this could be done in parallel.

In case two threads online pages in two different empty zones at the same time, there would be a contention to update the nr_zones.

The patch use pgdat_resize_lock() to protect this critical section.

---
 mm/page_alloc.c | 3 +++
 1 file changed, 3 insertions(+)
mm/page_alloc.c +3 -0
index e13987c2e1c4..525a5344a13b 100644
@@ -5796,9 +5796,12 @@ void __meminit init_currently_empty_zone(struct zone *zone,
 {
 	struct pglist_data *pgdat = zone->zone_pgdat;
 	int zone_idx = zone_idx(zone) + 1;
+	unsigned long flags;
 
+	pgdat_resize_lock(pgdat, &flags);
 	if (zone_idx > pgdat->nr_zones)
 		pgdat->nr_zones = zone_idx;
+	pgdat_resize_unlock(pgdat, &flags);
 
 	zone->zone_start_pfn = zone_start_pfn;
 
Signature · 1 line
2.15.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Wei Yang

After memory hot-added, users could online pages through sysfs, and this could be done in parallel.

In case two threads online pages in two different empty zones at the same time, there would be a contention to update the nr_zones.

No, this shouldn't be the case as I've explained in the original thread. We use memory hotplug lock over the online phase. So there shouldn't be any race possible.

On the other hand I would like to see the global lock to go away because it causes scalability issues and I would like to change it to a range lock. This would make this race possible.

That being said this is more of a preparatory work than a fix. One could argue that pgdat resize lock is abused here but I am not convinced a dedicated lock is much better. We do take this lock already and spanning its scope seems reasonable. An update to the documentation is due.

Quoted 3 lines

The patch use pgdat_resize_lock() to protect this critical section.

After the changelog is updated to reflect the above, feel free to add

Quoted 23 lines
---
 mm/page_alloc.c | 3 +++
 1 file changed, 3 insertions(+)
mm/page_alloc.c +3 -0
index e13987c2e1c4..525a5344a13b 100644
@@ -5796,9 +5796,12 @@ void __meminit init_currently_empty_zone(struct zone *zone,
 {
 	struct pglist_data *pgdat = zone->zone_pgdat;
 	int zone_idx = zone_idx(zone) + 1;
+	unsigned long flags;
 
+	pgdat_resize_lock(pgdat, &flags);
 	if (zone_idx > pgdat->nr_zones)
 		pgdat->nr_zones = zone_idx;
+	pgdat_resize_unlock(pgdat, &flags);
 
 	zone->zone_start_pfn = zone_start_pfn;
 
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 8 lines

On the other hand I would like to see the global lock to go away because it causes scalability issues and I would like to change it to a range lock. This would make this race possible.

That being said this is more of a preparatory work than a fix. One could argue that pgdat resize lock is abused here but I am not convinced a dedicated lock is much better. We do take this lock already and spanning its scope seems reasonable. An update to the documentation is due.

Would not make more sense to move it within the pgdat lock in move_pfn_range_to_zone?
The call from free_area_init_core is safe as we are single-thread there.

And if we want to move towards a range locking, I even think it would be more consistent if we move it within the zone's span lock (which is already wrapped with a pgdat lock).

reviewer
#3 lore raw
Quoted 11 lines · On Tue 20-11-18 08:58:11, [email protected] wrote:
Quoted 8 lines

On the other hand I would like to see the global lock to go away because it causes scalability issues and I would like to change it to a range lock. This would make this race possible.

That being said this is more of a preparatory work than a fix. One could argue that pgdat resize lock is abused here but I am not convinced a dedicated lock is much better. We do take this lock already and spanning its scope seems reasonable. An update to the documentation is due.

Would not make more sense to move it within the pgdat lock in move_pfn_range_to_zone?

yes, that was what I meant originally and I haven't really looked closer to the diff itself because I've stopped right at the description.

Quoted 6 lines

The call from free_area_init_core is safe as we are single-thread there.

And if we want to move towards a range locking, I even think it would be more
consistent if we move it within the zone's span lock (which is already wrapped with a pgdat lock).

Agreed!

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#17 lore raw
In reply to Michal Hocko
Quoted 10 lines · Michal Hocko
Quoted 5 lines · Wei Yang

After memory hot-added, users could online pages through sysfs, and this could be done in parallel.

In case two threads online pages in two different empty zones at the same time, there would be a contention to update the nr_zones.

No, this shouldn't be the case as I've explained in the original thread. We use memory hotplug lock over the online phase. So there shouldn't be any race possible.

Sorry for misunderstanding your point.

Quoted 4 lines

On the other hand I would like to see the global lock to go away because it causes scalability issues and I would like to change it to a range lock. This would make this race possible.

The global lock you want to remove is mem_hotplug_begin() ?

Hmm... my understanding may not correct. While mem_hotplug_begin() use percpu lock, which means if there are two threads running on two different cpus to online pages at the same time, they could get their own lock?

If this is the case, will we face the race condition here?

Quoted 5 lines

That being said this is more of a preparatory work than a fix. One could argue that pgdat resize lock is abused here but I am not convinced a dedicated lock is much better. We do take this lock already and spanning its scope seems reasonable. An update to the documentation is due.

Agree, I will try to update the documentation in next verstion.

Quoted 35 lines
Quoted 4 lines

The patch use pgdat_resize_lock() to protect this critical section.

After the changelog is updated to reflect the above, feel free to add

Quoted 23 lines
---
 mm/page_alloc.c | 3 +++
 1 file changed, 3 insertions(+)
mm/page_alloc.c +3 -0
index e13987c2e1c4..525a5344a13b 100644
@@ -5796,9 +5796,12 @@ void __meminit init_currently_empty_zone(struct zone *zone,
 {
 	struct pglist_data *pgdat = zone->zone_pgdat;
 	int zone_idx = zone_idx(zone) + 1;
+	unsigned long flags;
 
+	pgdat_resize_lock(pgdat, &flags);
 	if (zone_idx > pgdat->nr_zones)
 		pgdat->nr_zones = zone_idx;
+	pgdat_resize_unlock(pgdat, &flags);
 
 	zone->zone_start_pfn = zone_start_pfn;
 
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
author
#4 lore raw
In reply to [email protected]
Quoted 4 lines · On Tue, Nov 20, 2018 at 08:58:11AM +0100, [email protected] wrote:
Quoted 4 lines

On the other hand I would like to see the global lock to go away because it causes scalability issues and I would like to change it to a range lock. This would make this race possible.

Quoted 9 lines
Quoted 4 lines

That being said this is more of a preparatory work than a fix. One could argue that pgdat resize lock is abused here but I am not convinced a dedicated lock is much better. We do take this lock already and spanning its scope seems reasonable. An update to the documentation is due.

Would not make more sense to move it within the pgdat lock in move_pfn_range_to_zone?
The call from free_area_init_core is safe as we are single-thread there.

Agree. This would be better.

Quoted 4 lines

And if we want to move towards a range locking, I even think it would be more consistent if we move it within the zone's span lock (which is already wrapped with a pgdat lock).

I lost a little here, just want to confirm with you.

Instead of call pgdat_resize_lock() around init_currently_empty_zone() in move_pfn_range_to_zone(), we move init_currently_empty_zone() before resize_zone_range()?

This sounds reasonable.

Quoted 2 lines
Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#18 lore raw
In reply to Wei Yang
Quoted 12 lines · Wei Yang
Quoted 10 lines · Michal Hocko
Quoted 5 lines · Wei Yang

After memory hot-added, users could online pages through sysfs, and this could be done in parallel.

In case two threads online pages in two different empty zones at the same time, there would be a contention to update the nr_zones.

No, this shouldn't be the case as I've explained in the original thread. We use memory hotplug lock over the online phase. So there shouldn't be any race possible.

Quoted 8 lines

Sorry for misunderstanding your point.

Quoted 4 lines

On the other hand I would like to see the global lock to go away because it causes scalability issues and I would like to change it to a range lock. This would make this race possible.

The global lock you want to remove is mem_hotplug_begin() ?

Yes

Quoted 5 lines

Hmm... my understanding may not correct. While mem_hotplug_begin() use percpu lock, which means if there are two threads running on two different cpus to online pages at the same time, they could get their own lock?

No. The per-cpu is a mere implementation detail on how the synchronization is done. Only one path might aquire the exclusive lock.

Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer
#5 lore raw
In reply to Wei Yang
Quoted 22 lines · Wei Yang
Quoted 4 lines · On Tue, Nov 20, 2018 at 08:58:11AM +0100, [email protected] wrote:
Quoted 4 lines

On the other hand I would like to see the global lock to go away because it causes scalability issues and I would like to change it to a range lock. This would make this race possible.

Quoted 9 lines
Quoted 4 lines

That being said this is more of a preparatory work than a fix. One could argue that pgdat resize lock is abused here but I am not convinced a dedicated lock is much better. We do take this lock already and spanning its scope seems reasonable. An update to the documentation is due.

Would not make more sense to move it within the pgdat lock in move_pfn_range_to_zone?
The call from free_area_init_core is safe as we are single-thread there.

Agree. This would be better.

Quoted 4 lines

And if we want to move towards a range locking, I even think it would be more consistent if we move it within the zone's span lock (which is already wrapped with a pgdat lock).

Quoted 7 lines

I lost a little here, just want to confirm with you.

Instead of call pgdat_resize_lock() around init_currently_empty_zone() in move_pfn_range_to_zone(), we move init_currently_empty_zone() before resize_zone_range()?

This sounds reasonable.

Btw. resolving the existing TODO would be nice as well, now that you are looking that direction...

mm/memory_hotplug.c +2 -3
index c6c42a7425e5..c75fca900044 100644
@@ -743,13 +743,12 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 	int nid = pgdat->node_id;
 	unsigned long flags;
 
+	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
+	pgdat_resize_lock(pgdat, &flags);
 	if (zone_is_empty(zone))
 		init_currently_empty_zone(zone, start_pfn, nr_pages);
 
 	clear_zone_contiguous(zone);
-
-	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
-	pgdat_resize_lock(pgdat, &flags);
 	zone_span_writelock(zone);
 	resize_zone_range(zone, start_pfn, nr_pages);
 	zone_span_writeunlock(zone);
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#16 lore raw
In reply to Wei Yang
Quoted 33 lines · Wei Yang
Quoted 15 lines · On Tue, Nov 20, 2018 at 08:58:11AM +0100, [email protected] wrote:
Quoted 6 lines

On the other hand I would like to see the global lock to go away because
it causes scalability issues and I would like to change it to a range
lock. This would make this race possible.

Quoted 8 lines

That being said this is more of a preparatory work than a fix. One could
argue that pgdat resize lock is abused here but I am not convinced a
dedicated lock is much better. We do take this lock already and spanning
its scope seems reasonable. An update to the documentation is due.

Quoted 5 lines

Would not make more sense to move it within the pgdat lock in move_pfn_range_to_zone?
The call from free_area_init_core is safe as we are single-thread there.

Agree. This would be better.

Quoted 6 lines

And if we want to move towards a range locking, I even think it would be more
consistent if we move it within the zone's span lock (which is already
wrapped with a pgdat lock).

I lost a little here, just want to confirm with you.

Quoted 7 lines

Instead of call pgdat_resize_lock() around init_currently_empty_zone()
in move_pfn_range_to_zone(), we move init_currently_empty_zone() before
resize_zone_range()?

This sounds reasonable.

Yeah.
spanned pages are being touched in:

- shrink_pgdat_span
- resize_zone_range
- init_currently_emty_zone

The first two are already protected by the span lock.

In init_currently_empty_zone, we also touch zone_start_pfn, which is part of the spanned pages (beginning), so I think it makes sense to also protect it with the span lock.
We just call init_currently_empty_zone in case the zone is empty, so the race should be not existent to be honest.

But I just think it is more consistent, and since moving it under spanlock would imply to also have it under pgdat lock, which was the main point of this, I think we do not have anything to lose.

author
#6 lore raw
In reply to Michal Hocko
Quoted 25 lines · Michal Hocko
Quoted 24 lines · Wei Yang
Quoted 4 lines · On Tue, Nov 20, 2018 at 08:58:11AM +0100, [email protected] wrote:
Quoted 4 lines

On the other hand I would like to see the global lock to go away because it causes scalability issues and I would like to change it to a range lock. This would make this race possible.

Quoted 9 lines
Quoted 4 lines

That being said this is more of a preparatory work than a fix. One could argue that pgdat resize lock is abused here but I am not convinced a dedicated lock is much better. We do take this lock already and spanning its scope seems reasonable. An update to the documentation is due.

Would not make more sense to move it within the pgdat lock in move_pfn_range_to_zone?
The call from free_area_init_core is safe as we are single-thread there.

Agree. This would be better.

Quoted 4 lines

And if we want to move towards a range locking, I even think it would be more consistent if we move it within the zone's span lock (which is already wrapped with a pgdat lock).

I lost a little here, just want to confirm with you.

Quoted 8 lines
Quoted 5 lines

Instead of call pgdat_resize_lock() around init_currently_empty_zone() in move_pfn_range_to_zone(), we move init_currently_empty_zone() before resize_zone_range()?

This sounds reasonable.

Btw. resolving the existing TODO would be nice as well, now that you are looking that direction...

I took a look at that commit, seems I need some time to understand this TODO. :-)

Quoted 9 lines
mm/memory_hotplug.c +0 -0
index c6c42a7425e5..c75fca900044 100644
@@ -743,13 +743,12 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 	int nid = pgdat->node_id;
 	unsigned long flags;
 

mm/memory_hotplug.c void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, · line 752

Quoted 10 lines
mm/memory_hotplug.c +2 -3
+	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
+	pgdat_resize_lock(pgdat, &flags);
 	if (zone_is_empty(zone))
 		init_currently_empty_zone(zone, start_pfn, nr_pages);
 
 	clear_zone_contiguous(zone);
-
-	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
-	pgdat_resize_lock(pgdat, &flags);
 	zone_span_writelock(zone);

Just want to make sure, Oscar suggests to move the code here to protect this under zone_span_lock.

If this the correct, I would spin a v2 and try to address the TODO.

Quoted 5 lines
mm/memory_hotplug.c +0 -0
 	resize_zone_range(zone, start_pfn, nr_pages);
 	zone_span_writeunlock(zone);
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#7 lore raw
Quoted 1 line · Wei Yang

On Wed, Nov 21, 2018 at 08:15:49AM +0100, Michal Hocko wrote:

[...]

Quoted 13 lines
Quoted 13 lines
mm/memory_hotplug.c +2 -0
index c6c42a7425e5..c75fca900044 100644
@@ -743,13 +743,12 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 	int nid = pgdat->node_id;
 	unsigned long flags;
 
+	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
+	pgdat_resize_lock(pgdat, &flags);
 	if (zone_is_empty(zone))
 		init_currently_empty_zone(zone, start_pfn, nr_pages);
 
Quoted 8 lines

mm/memory_hotplug.c void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, · line 752

Quoted 5 lines
mm/memory_hotplug.c +0 -3
 	clear_zone_contiguous(zone);
-
-	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
-	pgdat_resize_lock(pgdat, &flags);
 	zone_span_writelock(zone);

Just want to make sure, Oscar suggests to move the code here to protect this under zone_span_lock.

Yes, both locks held is probably safer. Because there is both pgdat and zone state updated.

Sorry to confuse you

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#19 lore raw

PATCH v2 1/1 mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

In reply to Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

---
v2:
* commit log changes
* modify the code in move_pfn_range_to_zone() instead of in init_currently_empty_zone()
* documentation change

---
 include/linux/mmzone.h | 7 ++++---
 mm/memory_hotplug.c    | 5 ++---
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/mmzone.h +4 -3
index 68d7b558924b..1bb749bee284 100644
@@ -636,9 +636,10 @@ typedef struct pglist_data {
 #endif
 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
 	/*
-	 * Must be held any time you expect node_start_pfn, node_present_pages
-	 * or node_spanned_pages stay constant.  Holding this will also
-	 * guarantee that any pfn_valid() stays that way.
+	 * Must be held any time you expect node_start_pfn,
+	 * node_present_pages, node_spanned_pages or nr_zones stay constant.
+	 * Holding this will also guarantee that any pfn_valid() stays that
+	 * way.
 	 *
 	 * pgdat_resize_lock() and pgdat_resize_unlock() are provided to
 	 * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG
mm/memory_hotplug.c +2 -3
index 61972da38d93..f626e7e5f57b 100644
@@ -742,14 +742,13 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 	int nid = pgdat->node_id;
 	unsigned long flags;
 
-	if (zone_is_empty(zone))
-		init_currently_empty_zone(zone, start_pfn, nr_pages);
-
 	clear_zone_contiguous(zone);
 
 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
 	pgdat_resize_lock(pgdat, &flags);
 	zone_span_writelock(zone);
+	if (zone_is_empty(zone))
+		init_currently_empty_zone(zone, start_pfn, nr_pages);
 	resize_zone_range(zone, start_pfn, nr_pages);
 	zone_span_writeunlock(zone);
 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
Signature · 1 line
2.15.1
author Acked-by Michal Hocko on v2 1/1
#20 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 7 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

Quoted 5 lines

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

Missed this, if I am correct. :-)

Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#21 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 11 lines · Wei Yang
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I would just add that the patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn

Quoted 6 lines
Quoted 2 lines

Missed this, if I am correct. :-)

Yes, thank you.

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#22 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

In reply to Michal Hocko
Quoted 18 lines · Michal Hocko
Quoted 11 lines · Wei Yang
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I would just add that the patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn

Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Missed this, if I am correct. :-)

Yes, thank you.

My pleasure :-)

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
#25 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

In reply to Wei Yang
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I looked into locking recently, and there is more to it.

Please read:

commit dee6da22efac451d361f5224a60be2796d847b51

Author: David Hildenbrand <[email protected]>
Date:   Tue Oct 30 15:10:44 2018 -0700

memory-hotplug.rst: add some details about locking internals

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

Short summary: Onlining/offlining of memory requires the device_hotplug_lock as of now.

mem_hotplug_begin() is just an internal optimization. (we don't want
everybody to take the device lock)

Quoted 54 lines

---
v2:
* commit log changes
* modify the code in move_pfn_range_to_zone() instead of in init_currently_empty_zone()
* documentation change

---
 include/linux/mmzone.h | 7 ++++---
 mm/memory_hotplug.c    | 5 ++---
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/mmzone.h +4 -3
index 68d7b558924b..1bb749bee284 100644
@@ -636,9 +636,10 @@ typedef struct pglist_data {
 #endif
 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
 	/*
-	 * Must be held any time you expect node_start_pfn, node_present_pages
-	 * or node_spanned_pages stay constant.  Holding this will also
-	 * guarantee that any pfn_valid() stays that way.
+	 * Must be held any time you expect node_start_pfn,
+	 * node_present_pages, node_spanned_pages or nr_zones stay constant.
+	 * Holding this will also guarantee that any pfn_valid() stays that
+	 * way.
 	 *
 	 * pgdat_resize_lock() and pgdat_resize_unlock() are provided to
 	 * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG
mm/memory_hotplug.c +2 -3
index 61972da38d93..f626e7e5f57b 100644
@@ -742,14 +742,13 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 	int nid = pgdat->node_id;
 	unsigned long flags;
 
-	if (zone_is_empty(zone))
-		init_currently_empty_zone(zone, start_pfn, nr_pages);
-
 	clear_zone_contiguous(zone);
 
 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
 	pgdat_resize_lock(pgdat, &flags);
 	zone_span_writelock(zone);
+	if (zone_is_empty(zone))
+		init_currently_empty_zone(zone, start_pfn, nr_pages);
 	resize_zone_range(zone, start_pfn, nr_pages);
 	zone_span_writeunlock(zone);
 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
Signature · 3 lines
Thanks,

David / dhildenb
author
#26 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 26 lines · David Hildenbrand
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I looked into locking recently, and there is more to it.

Please read:

commit dee6da22efac451d361f5224a60be2796d847b51

Author: David Hildenbrand <[email protected]>
Date:   Tue Oct 30 15:10:44 2018 -0700

memory-hotplug.rst: add some details about locking internals

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

Quoted 6 lines

Short summary: Onlining/offlining of memory requires the device_hotplug_lock as of now.

mem_hotplug_begin() is just an internal optimization. (we don't want everybody to take the device lock)

Hi, David

Thanks for your comment.

Hmm... I didn't catch your point.

Related to memory hot-plug, there are (at least) three locks,

  * device_hotplug_lock    (global)
  * device lock            (device scope)
  * mem_hotplug_lock       (global)

But with two different hold sequence in two cases:

* device_online()

device_hotplug_lock
device_lock
mem_hotplug_lock

* add_memory_resource()

device_hotplug_lock
mem_hotplug_lock
device_lock
^
|
I don't find where this is hold in add_memory_resource(). Would you mind giving me a hint?

If my understanding is correct, what is your point?

I guess your point is : just remove mem_hotplug_lock is not enough to resolve the scalability issue?

Please correct me, if I am not. :-)

Signature · 2 lines
Wei Yang
Help you, Help me
#27 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 30 lines · Wei Yang
Quoted 29 lines · David Hildenbrand
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I looked into locking recently, and there is more to it.

Please read:

commit dee6da22efac451d361f5224a60be2796d847b51

Author: David Hildenbrand <[email protected]>
Date:   Tue Oct 30 15:10:44 2018 -0700

memory-hotplug.rst: add some details about locking internals

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

Short summary: Onlining/offlining of memory requires the device_hotplug_lock as of now.

Quoted 7 lines
Quoted 3 lines

mem_hotplug_begin() is just an internal optimization. (we don't want everybody to take the device lock)

Hi, David

Thanks for your comment.

My last sentence should have been "we don't want everybody to take the device hotplug lock" :) That caused confusion.

Quoted 27 lines

Hmm... I didn't catch your point.

Related to memory hot-plug, there are (at least) three locks,

  * device_hotplug_lock    (global)
  * device lock            (device scope)
  * mem_hotplug_lock       (global)

But with two different hold sequence in two cases:

* device_online()

device_hotplug_lock
device_lock
mem_hotplug_lock

* add_memory_resource()

device_hotplug_lock
mem_hotplug_lock
device_lock
^
|
I don't find where this is hold in add_memory_resource(). Would you mind giving me a hint?

Quoted 2 lines

If my understanding is correct, what is your point?

The point I was trying to make:

Right now all onlining/offlining/adding/removing is protected by the device_hotplug_lock (and that's a good thing, things are fragile enough already).

mem_hotplug_lock() is used in addition for get_online_mems().

"This patch is a preparation for removing the global lock during online_pages phase." - is more like "one global lock".

Quoted 2 lines

I guess your point is : just remove mem_hotplug_lock is not enough to resolve the scalability issue?

Depends on which scalability issue :)

Getting rid of / removing the impact of mem_hotplug_lock is certainly a very good idea. And improves scalability of all callers of get_online_mems(). If that is the intention, very good :)

If the intention is to make onlining/offlining more scalable (e.g. in parallel or such), then scalability is limited by device_hotplug_lock.

Quoted 3 lines

Please correct me, if I am not. :-)

Guess I was just wondering which scalability issue we are trying to solve :)

Signature · 3 lines
Thanks,

David / dhildenb
author
#28 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 74 lines · David Hildenbrand
Quoted 30 lines · Wei Yang
Quoted 29 lines · David Hildenbrand
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I looked into locking recently, and there is more to it.

Please read:

commit dee6da22efac451d361f5224a60be2796d847b51

Author: David Hildenbrand <[email protected]>
Date:   Tue Oct 30 15:10:44 2018 -0700

memory-hotplug.rst: add some details about locking internals

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

Short summary: Onlining/offlining of memory requires the device_hotplug_lock as of now.

Quoted 7 lines
Quoted 3 lines

mem_hotplug_begin() is just an internal optimization. (we don't want everybody to take the device lock)

Hi, David

Thanks for your comment.

My last sentence should have been "we don't want everybody to take the device hotplug lock" :) That caused confusion.

Quoted 27 lines

Hmm... I didn't catch your point.

Related to memory hot-plug, there are (at least) three locks,

  * device_hotplug_lock    (global)
  * device lock            (device scope)
  * mem_hotplug_lock       (global)

But with two different hold sequence in two cases:

* device_online()

device_hotplug_lock
device_lock
mem_hotplug_lock

* add_memory_resource()

device_hotplug_lock
mem_hotplug_lock
device_lock
^
|
I don't find where this is hold in add_memory_resource(). Would you mind giving me a hint?

Quoted 2 lines

If my understanding is correct, what is your point?

The point I was trying to make:

Quoted 9 lines

Right now all onlining/offlining/adding/removing is protected by the device_hotplug_lock (and that's a good thing, things are fragile enough already).

mem_hotplug_lock() is used in addition for get_online_mems().

"This patch is a preparation for removing the global lock during online_pages phase." - is more like "one global lock".

Thanks for reminding. You are right.

Quoted 9 lines
Quoted 2 lines

I guess your point is : just remove mem_hotplug_lock is not enough to resolve the scalability issue?

Depends on which scalability issue :)

Getting rid of / removing the impact of mem_hotplug_lock is certainly a very good idea. And improves scalability of all callers of get_online_mems(). If that is the intention, very good :)

Maybe not exact.

The intention is to get rid of mem_hotplug_begin/done, if I am correct.

Quoted 3 lines

If the intention is to make onlining/offlining more scalable (e.g. in parallel or such), then scalability is limited by device_hotplug_lock.

I didn't notice this lock.

While this is a step by step improvement.

Quoted 12 lines
Quoted 4 lines

Please correct me, if I am not. :-)

Guess I was just wondering which scalability issue we are trying to solve :)

Signature · 3 lines
Thanks,

David / dhildenb
Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#29 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

In reply to David Hildenbrand
Quoted 20 lines · David Hildenbrand
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I looked into locking recently, and there is more to it.

Please read:

commit dee6da22efac451d361f5224a60be2796d847b51

Author: David Hildenbrand <[email protected]>
Date:   Tue Oct 30 15:10:44 2018 -0700
Quoted 8 lines

memory-hotplug.rst: add some details about locking internals

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

Short summary: Onlining/offlining of memory requires the device_hotplug_lock as of now.

Well, I would tend to disagree here. You might be describing the current state of art but the device_hotplug_lock doesn't make much sense for the memory hotplug in principle. There is absolutely nothing in the core MM that would require this lock. The current state just uses a BKL in some sense and we really want to get rid of that longterm. This patch is a tiny step in that direction and I suspect many more will need to come on the way. We really want to end up with a clear scope of each lock being taken. A project for a brave soul...

Signature · 2 lines
Michal Hocko
SUSE Labs
#30 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 27 lines · Michal Hocko
Quoted 26 lines · David Hildenbrand
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I looked into locking recently, and there is more to it.

Please read:

commit dee6da22efac451d361f5224a60be2796d847b51

Author: David Hildenbrand <[email protected]>
Date:   Tue Oct 30 15:10:44 2018 -0700

memory-hotplug.rst: add some details about locking internals

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

Quoted 6 lines
Quoted 2 lines

Short summary: Onlining/offlining of memory requires the device_hotplug_lock as of now.

Well, I would tend to disagree here. You might be describing the current state of art but the device_hotplug_lock doesn't make much sense for the memory hotplug in principle. There is absolutely nothing in the core MM

There are collisions with CPU hotplug that require this lock (when nodes come and go as far as I remember). And there is the problematic lock inversion that can happen when adding/remving memory. This all has to be sorted out, we'll have to see if we really need it for onlining/offlining, though, however ...

Quoted 5 lines

that would require this lock. The current state just uses a BKL in some sense and we really want to get rid of that longterm. This patch is a tiny step in that direction and I suspect many more will need to come on the way. We really want to end up with a clear scope of each lock being taken. A project for a brave soul...

... for now I don't consider "optimize for parallel onlining/offlining/adding/removing of memory and cpus" really necessary. What is necessary indeed is to not slowdown the whole system just because some memory is coming/going. Therefore I agree, this patch is a step into the right direction.

Signature · 3 lines
Thanks,

David / dhildenb
author
#31 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 35 lines · David Hildenbrand
Quoted 27 lines · Michal Hocko
Quoted 26 lines · David Hildenbrand
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I looked into locking recently, and there is more to it.

Please read:

commit dee6da22efac451d361f5224a60be2796d847b51

Author: David Hildenbrand <[email protected]>
Date:   Tue Oct 30 15:10:44 2018 -0700

memory-hotplug.rst: add some details about locking internals

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

Quoted 6 lines
Quoted 2 lines

Short summary: Onlining/offlining of memory requires the device_hotplug_lock as of now.

Well, I would tend to disagree here. You might be describing the current state of art but the device_hotplug_lock doesn't make much sense for the memory hotplug in principle. There is absolutely nothing in the core MM

Quoted 6 lines

There are collisions with CPU hotplug that require this lock (when nodes come and go as far as I remember). And there is the problematic lock inversion that can happen when adding/remving memory. This all has to be sorted out, we'll have to see if we really need it for onlining/offlining, though, however ...

Seems I get a little understanding on this part.

There are two hotplug:
* CPU hotplug
* Memory hotplug.

There are two phase for Memory hotplug:
* physical add/remove
* logical online/offline

All of them are protected by device_hotplug_lock now, so we need to be careful to release this in any case. Is my understanding correct?

Quoted 6 lines
Quoted 5 lines

that would require this lock. The current state just uses a BKL in some sense and we really want to get rid of that longterm. This patch is a tiny step in that direction and I suspect many more will need to come on the way. We really want to end up with a clear scope of each lock being taken. A project for a brave soul...

Quoted 6 lines

... for now I don't consider "optimize for parallel onlining/offlining/adding/removing of memory and cpus" really necessary. What is necessary indeed is to not slowdown the whole system just because some memory is coming/going. Therefore I agree, this patch is a step into the right direction.

Agree.

The target is to accelerate the hot-plug without slow down the normal process.

Quoted 5 lines
Signature · 3 lines
Thanks,

David / dhildenb
Signature · 2 lines
Wei Yang
Help you, Help me
author
#8 lore raw
In reply to Michal Hocko
Quoted 26 lines · Michal Hocko
Quoted 24 lines · Wei Yang
Quoted 4 lines · On Tue, Nov 20, 2018 at 08:58:11AM +0100, [email protected] wrote:
Quoted 4 lines

On the other hand I would like to see the global lock to go away because it causes scalability issues and I would like to change it to a range lock. This would make this race possible.

Quoted 9 lines
Quoted 4 lines

That being said this is more of a preparatory work than a fix. One could argue that pgdat resize lock is abused here but I am not convinced a dedicated lock is much better. We do take this lock already and spanning its scope seems reasonable. An update to the documentation is due.

Would not make more sense to move it within the pgdat lock in move_pfn_range_to_zone?
The call from free_area_init_core is safe as we are single-thread there.

Agree. This would be better.

Quoted 4 lines

And if we want to move towards a range locking, I even think it would be more consistent if we move it within the zone's span lock (which is already wrapped with a pgdat lock).

I lost a little here, just want to confirm with you.

Quoted 9 lines
Quoted 5 lines

Instead of call pgdat_resize_lock() around init_currently_empty_zone() in move_pfn_range_to_zone(), we move init_currently_empty_zone() before resize_zone_range()?

This sounds reasonable.

Btw. resolving the existing TODO would be nice as well, now that you are looking that direction...

Michal,

I took a look at commit f1dd2cd13c4b ("mm, memory_hotplug: do not associate hotadded memory to zones until online"), and try to understand this TODO.

The reason to acquire these lock is before this commit, the memory is associated with zone at physical adding phase, while after this commit, this is delayed to logical online stage.

But I get some difficulty to understand this TODO. You want to get rid of these lock? While these locks seem necessary to protect those data of pgdat/zone. Would you mind sharing more on this statement?

reviewer
#9 lore raw

On Mon 26-11-18 10:28:40, Wei Yang wrote:
[...]

Quoted 3 lines

But I get some difficulty to understand this TODO. You want to get rid of these lock? While these locks seem necessary to protect those data of pgdat/zone. Would you mind sharing more on this statement?

Why do we need this lock to be irqsave? Is there any caller that uses the lock from the IRQ context?

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#10 lore raw
Quoted 8 lines · Michal Hocko

On Mon 26-11-18 10:28:40, Wei Yang wrote:
[...]

Quoted 3 lines

But I get some difficulty to understand this TODO. You want to get rid of these lock? While these locks seem necessary to protect those data of pgdat/zone. Would you mind sharing more on this statement?

Why do we need this lock to be irqsave? Is there any caller that uses the lock from the IRQ context?

I see you put the comment 'irqsave' in code, I thought this is the requirement bringing in by this commit. So this is copyed from somewhere else?

Quoted 1 line

From my understanding, we don't access pgdat from interrupt context.

BTW, one more confirmation. One irqsave lock means we can't do something during holding the lock, like sleep. Is my understanding correct?

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
#32 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

In reply to Wei Yang
Quoted 49 lines · Wei Yang
Quoted 35 lines · David Hildenbrand
Quoted 27 lines · Michal Hocko
Quoted 26 lines · David Hildenbrand
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I looked into locking recently, and there is more to it.

Please read:

commit dee6da22efac451d361f5224a60be2796d847b51

Author: David Hildenbrand <[email protected]>
Date:   Tue Oct 30 15:10:44 2018 -0700

memory-hotplug.rst: add some details about locking internals

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

Quoted 6 lines
Quoted 2 lines

Short summary: Onlining/offlining of memory requires the device_hotplug_lock as of now.

Well, I would tend to disagree here. You might be describing the current state of art but the device_hotplug_lock doesn't make much sense for the memory hotplug in principle. There is absolutely nothing in the core MM

Quoted 6 lines

There are collisions with CPU hotplug that require this lock (when nodes come and go as far as I remember). And there is the problematic lock inversion that can happen when adding/remving memory. This all has to be sorted out, we'll have to see if we really need it for onlining/offlining, though, however ...

Seems I get a little understanding on this part.

There are two hotplug:
* CPU hotplug
* Memory hotplug.

Quoted 6 lines

There are two phase for Memory hotplug:
* physical add/remove
* logical online/offline

All of them are protected by device_hotplug_lock now, so we need to be careful to release this in any case. Is my understanding correct?

Yes, e.g. the acpi driver always held the device_hotplug_lock (due to possible problems with concurrent cpu/memory hot(un)plug). Onlining offlining of devices (including cpu/memory) from user space always held the device_hotplug_lock. So this part was executed sequentially for a long time.

I recently made sure that any adding/removing/onlining/offlining correctly grabs the device_hotplug_lock AND the mem_hotplug_lock in any case (because it was inconsistent and broken), so it is all executed sequentially.

So when getting rid of mem_hotplug_lock we only have to care about all users that don't take the device_hotplug_lock.

Quoted 14 lines
Quoted 7 lines
Quoted 6 lines

that would require this lock. The current state just uses a BKL in some sense and we really want to get rid of that longterm. This patch is a tiny step in that direction and I suspect many more will need to come on the way. We really want to end up with a clear scope of each lock being taken. A project for a brave soul...

Quoted 6 lines

... for now I don't consider "optimize for parallel onlining/offlining/adding/removing of memory and cpus" really necessary. What is necessary indeed is to not slowdown the whole system just because some memory is coming/going. Therefore I agree, this patch is a step into the right direction.

Quoted 4 lines

Agree.

The target is to accelerate the hot-plug without slow down the normal process.

Indeed!

Signature · 3 lines
Thanks,

David / dhildenb
reviewer
#11 lore raw
In reply to Wei Yang
Quoted 7 lines · Wei Yang
Quoted 6 lines · Michal Hocko

On Mon 26-11-18 10:28:40, Wei Yang wrote:
[...]

Quoted 3 lines

But I get some difficulty to understand this TODO. You want to get rid of these lock? While these locks seem necessary to protect those data of pgdat/zone. Would you mind sharing more on this statement?

Quoted 6 lines
Quoted 2 lines

Why do we need this lock to be irqsave? Is there any caller that uses the lock from the IRQ context?

I see you put the comment 'irqsave' in code, I thought this is the requirement bringing in by this commit. So this is copyed from somewhere else?

No, the irqsave lock has been there for a long time but it was not clear to me whether it is still required. Maybe it never was. I just didn't have time to look into that and put a TODO there. The code wouldn't be less correct if I kept it.

Quoted 4 lines
Quoted 1 line

From my understanding, we don't access pgdat from interrupt context.

BTW, one more confirmation. One irqsave lock means we can't do something during holding the lock, like sleep. Is my understanding correct?

You cannot sleep in any atomic context. IRQ safe lock only means that IRQs are disabled along with the lock. The irqsave variant should be taken when an IRQ context itself can take the lock. There is a lot of documentation to clarify this e.g. Linux Device Drivers. I would recommend to read through that.

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#12 lore raw
Quoted 11 lines · Michal Hocko
Quoted 10 lines · Wei Yang
Quoted 8 lines · Michal Hocko

On Mon 26-11-18 10:28:40, Wei Yang wrote:
[...]

Quoted 3 lines

But I get some difficulty to understand this TODO. You want to get rid of these lock? While these locks seem necessary to protect those data of pgdat/zone. Would you mind sharing more on this statement?

Why do we need this lock to be irqsave? Is there any caller that uses the lock from the IRQ context?

Quoted 9 lines
Quoted 3 lines

I see you put the comment 'irqsave' in code, I thought this is the requirement bringing in by this commit. So this is copyed from somewhere else?

No, the irqsave lock has been there for a long time but it was not clear to me whether it is still required. Maybe it never was. I just didn't have time to look into that and put a TODO there. The code wouldn't be less correct if I kept it.

Let me summarize what you expect to do.

Go through all the users of pgdat_resize_lock, if none of them is called from IRQ context, we could do the following change:

include/linux/memory_hotplug.h +4 -4
index ffd9cd10fcf3..45a5affcab8a 100644
@@ -272,14 +272,14 @@ static inline bool movable_node_is_enabled(void)
  * pgdat resizing functions
  */
 static inline
-void pgdat_resize_lock(struct pglist_data *pgdat, unsigned long *flags)
+void pgdat_resize_lock(struct pglist_data *pgdat)
 {
-	spin_lock_irqsave(&pgdat->node_size_lock, *flags);
+	spin_lock(&pgdat->node_size_lock);
 }
 static inline
-void pgdat_resize_unlock(struct pglist_data *pgdat, unsigned long *flags)
+void pgdat_resize_unlock(struct pglist_data *pgdat)
 {
-	spin_unlock_irqrestore(&pgdat->node_size_lock, *flags);
+	spin_unlock(&pgdat->node_size_lock);
 }
 static inline
 void pgdat_resize_init(struct pglist_data *pgdat)
Quoted 11 lines
Quoted 4 lines
Quoted 1 line

From my understanding, we don't access pgdat from interrupt context.

BTW, one more confirmation. One irqsave lock means we can't do something during holding the lock, like sleep. Is my understanding correct?

You cannot sleep in any atomic context. IRQ safe lock only means that IRQs are disabled along with the lock. The irqsave variant should be taken when an IRQ context itself can take the lock. There is a lot of documentation to clarify this e.g. Linux Device Drivers. I would recommend to read through that.

Thanks.

I took a look at this one which seems to resolve my confusion.

https://www.kernel.org/doc/Documentation/locking/spinlocks.txt

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
author
#33 lore raw

Re: [PATCH v2] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

In reply to David Hildenbrand
Quoted 63 lines · David Hildenbrand
Quoted 49 lines · Wei Yang
Quoted 35 lines · David Hildenbrand
Quoted 27 lines · Michal Hocko
Quoted 26 lines · David Hildenbrand
Quoted 10 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

This patch is a preparation for removing the global lock during online_pages phase. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

I looked into locking recently, and there is more to it.

Please read:

commit dee6da22efac451d361f5224a60be2796d847b51

Author: David Hildenbrand <[email protected]>
Date:   Tue Oct 30 15:10:44 2018 -0700

memory-hotplug.rst: add some details about locking internals

Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space.

Quoted 6 lines
Quoted 2 lines

Short summary: Onlining/offlining of memory requires the device_hotplug_lock as of now.

Well, I would tend to disagree here. You might be describing the current state of art but the device_hotplug_lock doesn't make much sense for the memory hotplug in principle. There is absolutely nothing in the core MM

Quoted 6 lines

There are collisions with CPU hotplug that require this lock (when nodes come and go as far as I remember). And there is the problematic lock inversion that can happen when adding/remving memory. This all has to be sorted out, we'll have to see if we really need it for onlining/offlining, though, however ...

Seems I get a little understanding on this part.

There are two hotplug:
* CPU hotplug
* Memory hotplug.

Quoted 6 lines

There are two phase for Memory hotplug:
* physical add/remove
* logical online/offline

All of them are protected by device_hotplug_lock now, so we need to be careful to release this in any case. Is my understanding correct?

Yes, e.g. the acpi driver always held the device_hotplug_lock (due to possible problems with concurrent cpu/memory hot(un)plug). Onlining offlining of devices (including cpu/memory) from user space always held the device_hotplug_lock. So this part was executed sequentially for a long time.

Quoted 8 lines

I recently made sure that any adding/removing/onlining/offlining correctly grabs the device_hotplug_lock AND the mem_hotplug_lock in any case (because it was inconsistent and broken), so it is all executed sequentially.

So when getting rid of mem_hotplug_lock we only have to care about all users that don't take the device_hotplug_lock.

Thanks for your sharing.

So there are two global locks to protect the hotplug procedure.

* device_hotplug_lock
* mem_hotplug_lock

Seems even removing mem_hotplug_lock doesn't help much on scalability?

Maybe we need to move one by one.

Signature · 2 lines
Wei Yang
Help you, Help me
author
#13 lore raw
In reply to Michal Hocko
Quoted 8 lines · Michal Hocko

On Mon 26-11-18 10:28:40, Wei Yang wrote:
[...]

Quoted 3 lines

But I get some difficulty to understand this TODO. You want to get rid of these lock? While these locks seem necessary to protect those data of pgdat/zone. Would you mind sharing more on this statement?

Why do we need this lock to be irqsave? Is there any caller that uses the lock from the IRQ context?

Went through the code, we have totally 9 place acquire pgdat_resize_lock:

   lib/show_mem.c:         1    show_mem()
   mm/memory_hotplug.c:    4    online/offline_pages/__remove_zone()
   mm/page_alloc.c:        2    defer_init
   mm/sparse.c:            2    not necessary

Two places I am not sure:

* show_mem() would be called from __alloc_pages_slowpath()
* __remove_zone() is related to acpi_scan() on x86, may related to other method on different arch

I am not 100% for sure, while they looks like to be called in IRQ context.

My ugly idea is:

* drop pgdat_resize_lock in show_mem(), we don't change the value here. or replace this with a read/write lock?
* can we adjust pgdat's range in offline_pages()? This would be consistent since we adjust them in online_pages().

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#14 lore raw
Quoted 13 lines · Wei Yang
Quoted 8 lines · Michal Hocko

On Mon 26-11-18 10:28:40, Wei Yang wrote:
[...]

Quoted 3 lines

But I get some difficulty to understand this TODO. You want to get rid of these lock? While these locks seem necessary to protect those data of pgdat/zone. Would you mind sharing more on this statement?

Why do we need this lock to be irqsave? Is there any caller that uses the lock from the IRQ context?

Went through the code, we have totally 9 place acquire pgdat_resize_lock:

Quoted 8 lines
   lib/show_mem.c:         1    show_mem()
   mm/memory_hotplug.c:    4    online/offline_pages/__remove_zone()
   mm/page_alloc.c:        2    defer_init
   mm/sparse.c:            2    not necessary

Two places I am not sure:

* show_mem() would be called from __alloc_pages_slowpath()

This shouldn't really need the lock. It is a mostly debugging aid rather than something that cannot tolarate racing with hotplug. What is the worst case that can happen?

Quoted 2 lines

* __remove_zone() is related to acpi_scan() on x86, may related to other method on different arch

This one really needs a lock qwith a larger scope anyway.

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#15 lore raw
Quoted 19 lines · Michal Hocko
Quoted 18 lines · Wei Yang
Quoted 8 lines · Michal Hocko

On Mon 26-11-18 10:28:40, Wei Yang wrote:
[...]

Quoted 3 lines

But I get some difficulty to understand this TODO. You want to get rid of these lock? While these locks seem necessary to protect those data of pgdat/zone. Would you mind sharing more on this statement?

Why do we need this lock to be irqsave? Is there any caller that uses the lock from the IRQ context?

Went through the code, we have totally 9 place acquire pgdat_resize_lock:

   lib/show_mem.c:         1    show_mem()
   mm/memory_hotplug.c:    4    online/offline_pages/__remove_zone()
   mm/page_alloc.c:        2    defer_init
   mm/sparse.c:            2    not necessary
Quoted 8 lines
Quoted 3 lines

Two places I am not sure:

* show_mem() would be called from __alloc_pages_slowpath()

This shouldn't really need the lock. It is a mostly debugging aid rather than something that cannot tolarate racing with hotplug. What is the worst case that can happen?

Agree.

The worst case is debug information is not exact in case defer init or hotplug happens at the same time. While this is a rare case.

If you think it is ok, I would suggest to remove the lock here.

Quoted 4 lines
Quoted 2 lines

* __remove_zone() is related to acpi_scan() on x86, may related to other method on different arch

This one really needs a lock qwith a larger scope anyway.

Based on my understanding, __remove_zone() happens at physical memory remove phase. While for currently logic, we adjust zone information at logic memory online phase.

They looks not consistent?

If we could do this at logical memory offline phase, we are sure this is not in IRQ context.

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
author
#34 lore raw

PATCH v3 1/1 mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

In reply to Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

The patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

---
David, I may not catch you exact comment on the code or changelog. If I missed, just let me know.

---
v3:
* slightly modify the last paragraph of changelog based on Michal's comment
v2:
* commit log changes
* modify the code in move_pfn_range_to_zone() instead of in init_currently_empty_zone()
* pgdat_resize_lock documentation change

---
 include/linux/mmzone.h | 7 ++++---
 mm/memory_hotplug.c    | 5 ++---
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/mmzone.h +4 -3
index 3d0c472438d2..37d9c5c3faa6 100644
@@ -635,9 +635,10 @@ typedef struct pglist_data {
 #endif
 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
 	/*
-	 * Must be held any time you expect node_start_pfn, node_present_pages
-	 * or node_spanned_pages stay constant.  Holding this will also
-	 * guarantee that any pfn_valid() stays that way.
+	 * Must be held any time you expect node_start_pfn,
+	 * node_present_pages, node_spanned_pages or nr_zones stay constant.
+	 * Holding this will also guarantee that any pfn_valid() stays that
+	 * way.
 	 *
 	 * pgdat_resize_lock() and pgdat_resize_unlock() are provided to
 	 * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG
mm/memory_hotplug.c +2 -3
index 61972da38d93..f626e7e5f57b 100644
@@ -742,14 +742,13 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 	int nid = pgdat->node_id;
 	unsigned long flags;
 
-	if (zone_is_empty(zone))
-		init_currently_empty_zone(zone, start_pfn, nr_pages);
-
 	clear_zone_contiguous(zone);
 
 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
 	pgdat_resize_lock(pgdat, &flags);
 	zone_span_writelock(zone);
+	if (zone_is_empty(zone))
+		init_currently_empty_zone(zone, start_pfn, nr_pages);
 	resize_zone_range(zone, start_pfn, nr_pages);
 	zone_span_writeunlock(zone);
 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
Signature · 1 line
2.15.1
#35 lore raw

Re: [PATCH v3] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 11 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

The patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

s/protectioin/protection/

Quoted 9 lines

---
David, I may not catch you exact comment on the code or changelog. If I missed, just let me know.

I guess I would have rewritten it to something like the following

"
Currently the online_pages phase is protected by two global locks (device_device_hotplug_lock and mem_hotplug_lock). Especial the latter can result in scalability issues, as it will slow down code relying on get_online_mems(). Let's prepare code for not having to rely on get_online_mems() but instead some more fine grained locks.

During online_pages phase, pgdat->nr_zones will be updated in case the zone is empty. Right now mem_hotplug_lock ensures that there is no contention during the update of nr_zones.

The patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn. Also this patch changes the documentation of node_size_lock to include the protection of nr_zones.
"

Does that make sense?

Quoted 55 lines

---
v3:
* slightly modify the last paragraph of changelog based on Michal's comment
v2:
* commit log changes
* modify the code in move_pfn_range_to_zone() instead of in init_currently_empty_zone()
* pgdat_resize_lock documentation change

---
 include/linux/mmzone.h | 7 ++++---
 mm/memory_hotplug.c    | 5 ++---
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/mmzone.h +4 -3
index 3d0c472438d2..37d9c5c3faa6 100644
@@ -635,9 +635,10 @@ typedef struct pglist_data {
 #endif
 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
 	/*
-	 * Must be held any time you expect node_start_pfn, node_present_pages
-	 * or node_spanned_pages stay constant.  Holding this will also
-	 * guarantee that any pfn_valid() stays that way.
+	 * Must be held any time you expect node_start_pfn,
+	 * node_present_pages, node_spanned_pages or nr_zones stay constant.
+	 * Holding this will also guarantee that any pfn_valid() stays that
+	 * way.
 	 *
 	 * pgdat_resize_lock() and pgdat_resize_unlock() are provided to
 	 * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG
mm/memory_hotplug.c +2 -3
index 61972da38d93..f626e7e5f57b 100644
@@ -742,14 +742,13 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 	int nid = pgdat->node_id;
 	unsigned long flags;
 
-	if (zone_is_empty(zone))
-		init_currently_empty_zone(zone, start_pfn, nr_pages);
-
 	clear_zone_contiguous(zone);
 
 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
 	pgdat_resize_lock(pgdat, &flags);
 	zone_span_writelock(zone);
+	if (zone_is_empty(zone))
+		init_currently_empty_zone(zone, start_pfn, nr_pages);
 	resize_zone_range(zone, start_pfn, nr_pages);
 	zone_span_writeunlock(zone);
 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
Signature · 3 lines
Thanks,

David / dhildenb
author
#36 lore raw

Re: [PATCH v3] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 25 lines · David Hildenbrand
Quoted 11 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

The patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

s/protectioin/protection/

Quoted 9 lines

---
David, I may not catch you exact comment on the code or changelog. If I missed, just let me know.

Quoted 8 lines

I guess I would have rewritten it to something like the following

"
Currently the online_pages phase is protected by two global locks (device_device_hotplug_lock and mem_hotplug_lock). Especial the latter can result in scalability issues, as it will slow down code relying on get_online_mems(). Let's prepare code for not having to rely on get_online_mems() but instead some more fine grained locks.

I am not sure why we specify get_online_mems() here. mem_hotplug_lock is grabed in many places besides this one. In my mind, each place introduce scalability issue, not only this one.

Or you want to say, the mem_hotplug_lock will introduce scalability issue in two place:

* hotplug process itself
* slab allocation process

The second one is more critical. And this is what we try to address?

Quoted 75 lines

During online_pages phase, pgdat->nr_zones will be updated in case the zone is empty. Right now mem_hotplug_lock ensures that there is no contention during the update of nr_zones.

The patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn. Also this patch changes the documentation of node_size_lock to include the protection of nr_zones.
"

Does that make sense?

Quoted 55 lines

---
v3:
* slightly modify the last paragraph of changelog based on Michal's comment
v2:
* commit log changes
* modify the code in move_pfn_range_to_zone() instead of in init_currently_empty_zone()
* pgdat_resize_lock documentation change

---
 include/linux/mmzone.h | 7 ++++---
 mm/memory_hotplug.c    | 5 ++---
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/mmzone.h +4 -3
index 3d0c472438d2..37d9c5c3faa6 100644
@@ -635,9 +635,10 @@ typedef struct pglist_data {
 #endif
 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
 	/*
-	 * Must be held any time you expect node_start_pfn, node_present_pages
-	 * or node_spanned_pages stay constant.  Holding this will also
-	 * guarantee that any pfn_valid() stays that way.
+	 * Must be held any time you expect node_start_pfn,
+	 * node_present_pages, node_spanned_pages or nr_zones stay constant.
+	 * Holding this will also guarantee that any pfn_valid() stays that
+	 * way.
 	 *
 	 * pgdat_resize_lock() and pgdat_resize_unlock() are provided to
 	 * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG
mm/memory_hotplug.c +2 -3
index 61972da38d93..f626e7e5f57b 100644
@@ -742,14 +742,13 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 	int nid = pgdat->node_id;
 	unsigned long flags;
 
-	if (zone_is_empty(zone))
-		init_currently_empty_zone(zone, start_pfn, nr_pages);
-
 	clear_zone_contiguous(zone);
 
 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
 	pgdat_resize_lock(pgdat, &flags);
 	zone_span_writelock(zone);
+	if (zone_is_empty(zone))
+		init_currently_empty_zone(zone, start_pfn, nr_pages);
 	resize_zone_range(zone, start_pfn, nr_pages);
 	zone_span_writeunlock(zone);
 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
Signature · 3 lines
Thanks,

David / dhildenb
Signature · 2 lines
Wei Yang
Help you, Help me
#37 lore raw

Re: [PATCH v3] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 35 lines · Wei Yang
Quoted 25 lines · David Hildenbrand
Quoted 11 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

The patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

s/protectioin/protection/

Quoted 9 lines

---
David, I may not catch you exact comment on the code or changelog. If I missed, just let me know.

Quoted 8 lines

I guess I would have rewritten it to something like the following

"
Currently the online_pages phase is protected by two global locks (device_device_hotplug_lock and mem_hotplug_lock). Especial the latter can result in scalability issues, as it will slow down code relying on get_online_mems(). Let's prepare code for not having to rely on get_online_mems() but instead some more fine grained locks.

Quoted 3 lines

I am not sure why we specify get_online_mems() here. mem_hotplug_lock is grabed in many places besides this one. In my mind, each place introduce scalability issue, not only this one.

mem_hotplug_lock is grabbed in write only when adding/removing/onlining/offlining memory and when adding/removing device memory. The read locker are the critical part for now.

Quoted 8 lines

Or you want to say, the mem_hotplug_lock will introduce scalability issue in two place:

* hotplug process itself
* slab allocation process

The second one is more critical. And this is what we try to address?

Indeed, especially as the first usually (except device memory) also uses the device_hotplug_lock, I only consider the second one critical.

Feel free to change this description to whatever you like. As I already stated scalability of adding/removing/onlining/offlining is not really an issue as of now (prove me wrong :) ). So I would not care about including such information in this patch.

Signature · 3 lines
Thanks,

David / dhildenb
author
#38 lore raw

Re: [PATCH v3] mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

Quoted 53 lines · David Hildenbrand
Quoted 35 lines · Wei Yang
Quoted 25 lines · David Hildenbrand
Quoted 11 lines · Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global lock mem_hotplug_begin(), which ensures there is no contention during the update of nr_zones. But this global lock introduces scalability issues.

The patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn. Also this patch changes the documentation of node_size_lock to include the protectioin of nr_zones.

s/protectioin/protection/

Quoted 9 lines

---
David, I may not catch you exact comment on the code or changelog. If I missed, just let me know.

Quoted 8 lines

I guess I would have rewritten it to something like the following

"
Currently the online_pages phase is protected by two global locks (device_device_hotplug_lock and mem_hotplug_lock). Especial the latter can result in scalability issues, as it will slow down code relying on get_online_mems(). Let's prepare code for not having to rely on get_online_mems() but instead some more fine grained locks.

Quoted 3 lines

I am not sure why we specify get_online_mems() here. mem_hotplug_lock is grabed in many places besides this one. In my mind, each place introduce scalability issue, not only this one.

mem_hotplug_lock is grabbed in write only when adding/removing/onlining/offlining memory and when adding/removing device memory. The read locker are the critical part for now.

Quoted 8 lines

Or you want to say, the mem_hotplug_lock will introduce scalability issue in two place:

* hotplug process itself
* slab allocation process

The second one is more critical. And this is what we try to address?

Quoted 8 lines

Indeed, especially as the first usually (except device memory) also uses the device_hotplug_lock, I only consider the second one critical.

Feel free to change this description to whatever you like. As I already stated scalability of adding/removing/onlining/offlining is not really an issue as of now (prove me wrong :) ). So I would not care about including such information in this patch.

Thanks for your information.

Let me try to reword the changelog.

Quoted 5 lines
Signature · 3 lines
Thanks,

David / dhildenb
Signature · 2 lines
Wei Yang
Help you, Help me
author
#39 lore raw

PATCH v4 1/1 mm, hotplug: move init_currently_empty_zone() under zone_span_lock protection

In reply to Wei Yang

During online_pages phase, pgdat->nr_zones will be updated in case this zone is empty.

Currently the online_pages phase is protected by the global locks (device_device_hotplug_lock and mem_hotplug_lock), which ensures there is no contention during the update of nr_zones.

These global locks introduces scalability issues (especially the second one), which slow down code relying on get_online_mems(). This is also a preparation for not having to rely on get_online_mems() but instead some more fine grained locks.

The patch moves init_currently_empty_zone under both zone_span_writelock and pgdat_resize_lock because both the pgdat state is changed (nr_zones) and the zone's start_pfn. Also this patch changes the documentation of node_size_lock to include the protection of nr_zones.

---
v4:
* mention the preparation for improving scalability by David's comment
v3:
* slightly modify the last paragraph of changelog based on Michal's comment
v2:
* commit log changes
* modify the code in move_pfn_range_to_zone() instead of in init_currently_empty_zone()
* pgdat_resize_lock documentation change

---
 include/linux/mmzone.h | 7 ++++---
 mm/memory_hotplug.c    | 5 ++---
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/mmzone.h +4 -3
index 3d0c472438d2..37d9c5c3faa6 100644
@@ -635,9 +635,10 @@ typedef struct pglist_data {
 #endif
 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
 	/*
-	 * Must be held any time you expect node_start_pfn, node_present_pages
-	 * or node_spanned_pages stay constant.  Holding this will also
-	 * guarantee that any pfn_valid() stays that way.
+	 * Must be held any time you expect node_start_pfn,
+	 * node_present_pages, node_spanned_pages or nr_zones stay constant.
+	 * Holding this will also guarantee that any pfn_valid() stays that
+	 * way.
 	 *
 	 * pgdat_resize_lock() and pgdat_resize_unlock() are provided to
 	 * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG
mm/memory_hotplug.c +2 -3
index 61972da38d93..f626e7e5f57b 100644
@@ -742,14 +742,13 @@ void __ref move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 	int nid = pgdat->node_id;
 	unsigned long flags;
 
-	if (zone_is_empty(zone))
-		init_currently_empty_zone(zone, start_pfn, nr_pages);
-
 	clear_zone_contiguous(zone);
 
 	/* TODO Huh pgdat is irqsave while zone is not. It used to be like that before */
 	pgdat_resize_lock(pgdat, &flags);
 	zone_span_writelock(zone);
+	if (zone_is_empty(zone))
+		init_currently_empty_zone(zone, start_pfn, nr_pages);
 	resize_zone_range(zone, start_pfn, nr_pages);
 	zone_span_writeunlock(zone);
 	resize_pgdat_range(pgdat, start_pfn, nr_pages);
Signature · 1 line
2.15.1