linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* memory_hotplug: zone_can_shift() returns boolean value
@ 2016-12-12 20:29 Yasuaki Ishimatsu
  2016-12-13 15:54 ` Reza Arbab
  0 siblings, 1 reply; 3+ messages in thread
From: Yasuaki Ishimatsu @ 2016-12-12 20:29 UTC (permalink / raw)
  To: linux-mm; +Cc: isimatu.yasuaki, arbab, linux-kernel

online_{kernel|movable} is used to change the memory zone to
ZONE_{NORMAL|MOVABLE} and online the memory.

To check that memory zone can be changed, zone_can_shift() is used.
Currently the function returns minus integer value, plus integer
value and 0. When the function returns minus or plus integer value,
it means that the memory zone can be changed to ZONE_{NORNAL|MOVABLE}.

But when the function returns 0, there is 2 meanings.

One of the meanings is that the memory zone does not need to be changed.
For example, when memory is in ZONE_NORMAL and onlined by online_kernel
the memory zone does not need to be changed.

Another meaning is that the memory zone cannot be changed. When memory
is in ZONE_NORMAL and onlined by online_movable, the memory zone may
not be changed to ZONE_MOVALBE due to memory online limitation(see
Documentation/memory-hotplug.txt). In this case, memory must not be
onlined.

The patch changes the return type of zone_can_shift() so that memory
is not onlined when memory zone cannot be changed.

Fixes: df429ac03936 ("memory-hotplug: more general validation of zone during online")
Signed-off-by: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com>
CC: Reza Arbab <arbab@linux.vnet.ibm.com>
---
  drivers/base/memory.c          |  6 ++----
  include/linux/memory_hotplug.h |  4 ++--
  mm/memory_hotplug.c            | 26 +++++++++++++++-----------
  3 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 62c63c0..5a94d5e 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -410,15 +410,13 @@ static ssize_t show_valid_zones(struct device *dev,
  	sprintf(buf, "%s", zone->name);

  	/* MMOP_ONLINE_KERNEL */
-	zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL);
-	if (zone_shift) {
+	if (zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL, &zone_shift)) {
  		strcat(buf, " ");
  		strcat(buf, (zone + zone_shift)->name);
  	}

  	/* MMOP_ONLINE_MOVABLE */
-	zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE);
-	if (zone_shift) {
+	if (zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE, &zone_shift)) {
  		strcat(buf, " ");
  		strcat(buf, (zone + zone_shift)->name);
  	}
diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h
index 01033fa..c1784c0 100644
--- a/include/linux/memory_hotplug.h
+++ b/include/linux/memory_hotplug.h
@@ -284,7 +284,7 @@ extern void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
  		unsigned long map_offset);
  extern struct page *sparse_decode_mem_map(unsigned long coded_mem_map,
  					  unsigned long pnum);
-extern int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
-			  enum zone_type target);
+extern bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
+			  enum zone_type target, int *zone_shift);

  #endif /* __LINUX_MEMORY_HOTPLUG_H */
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index cad4b91..96f05e9 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -1033,8 +1033,8 @@ static void node_states_set_node(int node, struct memory_notify *arg)
  	node_set_state(node, N_MEMORY);
  }

-int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
-		   enum zone_type target)
+bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
+		   enum zone_type target, int *zone_shift)
  {
  	struct zone *zone = page_zone(pfn_to_page(pfn));
  	enum zone_type idx = zone_idx(zone);
@@ -1043,26 +1043,27 @@ int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
  	if (idx < target) {
  		/* pages must be at end of current zone */
  		if (pfn + nr_pages != zone_end_pfn(zone))
-			return 0;
+			return false;

  		/* no zones in use between current zone and target */
  		for (i = idx + 1; i < target; i++)
  			if (zone_is_initialized(zone - idx + i))
-				return 0;
+				return false;
  	}

  	if (target < idx) {
  		/* pages must be at beginning of current zone */
  		if (pfn != zone->zone_start_pfn)
-			return 0;
+			return false;

  		/* no zones in use between current zone and target */
  		for (i = target + 1; i < idx; i++)
  			if (zone_is_initialized(zone - idx + i))
-				return 0;
+				return false;
  	}

-	return target - idx;
+	*zone_shift = target - idx;
+	return true;
  }

  /* Must be protected by mem_hotplug_begin() */
@@ -1089,10 +1090,13 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
  	    !can_online_high_movable(zone))
  		return -EINVAL;

-	if (online_type == MMOP_ONLINE_KERNEL)
-		zone_shift = zone_can_shift(pfn, nr_pages, ZONE_NORMAL);
-	else if (online_type == MMOP_ONLINE_MOVABLE)
-		zone_shift = zone_can_shift(pfn, nr_pages, ZONE_MOVABLE);
+	if (online_type == MMOP_ONLINE_KERNEL) {
+		if (!zone_can_shift(pfn, nr_pages, ZONE_NORMAL, &zone_shift))
+			return -EINVAL;
+	} else if (online_type == MMOP_ONLINE_MOVABLE) {
+		if (!zone_can_shift(pfn, nr_pages, ZONE_MOVABLE, &zone_shift))
+			return -EINVAL;
+	}

  	zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
  	if (!zone)
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: memory_hotplug: zone_can_shift() returns boolean value
  2016-12-12 20:29 memory_hotplug: zone_can_shift() returns boolean value Yasuaki Ishimatsu
@ 2016-12-13 15:54 ` Reza Arbab
  2016-12-13 16:47   ` Yasuaki Ishimatsu
  0 siblings, 1 reply; 3+ messages in thread
From: Reza Arbab @ 2016-12-13 15:54 UTC (permalink / raw)
  To: Yasuaki Ishimatsu; +Cc: linux-mm, isimatu.yasuaki, linux-kernel

On Mon, Dec 12, 2016 at 03:29:04PM -0500, Yasuaki Ishimatsu wrote:
>--- a/drivers/base/memory.c
>+++ b/drivers/base/memory.c
>@@ -410,15 +410,13 @@ static ssize_t show_valid_zones(struct device *dev,
> 	sprintf(buf, "%s", zone->name);
>
> 	/* MMOP_ONLINE_KERNEL */
>-	zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL);
>-	if (zone_shift) {
>+	if (zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL, &zone_shift)) {
> 		strcat(buf, " ");
> 		strcat(buf, (zone + zone_shift)->name);
> 	}
>
> 	/* MMOP_ONLINE_MOVABLE */
>-	zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE);
>-	if (zone_shift) {
>+	if (zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE, &zone_shift)) {
> 		strcat(buf, " ");
> 		strcat(buf, (zone + zone_shift)->name);
> 	}

You still need to check zone_shift != 0, otherwise you may get duplicate 
output:

$ cat /sys/devices/system/node/node1/memory256/valid_zones
Movable Normal Movable
$ cat /sys/devices/system/node/node1/memory257/valid_zones
Movable Movable

>--- a/mm/memory_hotplug.c
>+++ b/mm/memory_hotplug.c
>@@ -1033,8 +1033,8 @@ static void node_states_set_node(int node, struct memory_notify *arg)
> 	node_set_state(node, N_MEMORY);
> }
>
>-int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
>-		   enum zone_type target)
>+bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
>+		   enum zone_type target, int *zone_shift)
> {
> 	struct zone *zone = page_zone(pfn_to_page(pfn));
> 	enum zone_type idx = zone_idx(zone);

I think you should initialize zone_shift here. It should be 0 if the 
function returns false.

	*zone_shift = 0;

>@@ -1043,26 +1043,27 @@ int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
> 	if (idx < target) {
> 		/* pages must be at end of current zone */
> 		if (pfn + nr_pages != zone_end_pfn(zone))
>-			return 0;
>+			return false;
>
> 		/* no zones in use between current zone and target */
> 		for (i = idx + 1; i < target; i++)
> 			if (zone_is_initialized(zone - idx + i))
>-				return 0;
>+				return false;
> 	}
>
> 	if (target < idx) {
> 		/* pages must be at beginning of current zone */
> 		if (pfn != zone->zone_start_pfn)
>-			return 0;
>+			return false;
>
> 		/* no zones in use between current zone and target */
> 		for (i = target + 1; i < idx; i++)
> 			if (zone_is_initialized(zone - idx + i))
>-				return 0;
>+				return false;
> 	}
>
>-	return target - idx;
>+	*zone_shift = target - idx;
>+	return true;
> }
>
> /* Must be protected by mem_hotplug_begin() */
>@@ -1089,10 +1090,13 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
> 	    !can_online_high_movable(zone))
> 		return -EINVAL;
>
>-	if (online_type == MMOP_ONLINE_KERNEL)
>-		zone_shift = zone_can_shift(pfn, nr_pages, ZONE_NORMAL);
>-	else if (online_type == MMOP_ONLINE_MOVABLE)
>-		zone_shift = zone_can_shift(pfn, nr_pages, ZONE_MOVABLE);
>+	if (online_type == MMOP_ONLINE_KERNEL) {
>+		if (!zone_can_shift(pfn, nr_pages, ZONE_NORMAL, &zone_shift))
>+			return -EINVAL;
>+	} else if (online_type == MMOP_ONLINE_MOVABLE) {
>+		if (!zone_can_shift(pfn, nr_pages, ZONE_MOVABLE, &zone_shift))
>+			return -EINVAL;
>+	}
>
> 	zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
> 	if (!zone)
>-- 
>1.8.3.1
>

-- 
Reza Arbab

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: memory_hotplug: zone_can_shift() returns boolean value
  2016-12-13 15:54 ` Reza Arbab
@ 2016-12-13 16:47   ` Yasuaki Ishimatsu
  0 siblings, 0 replies; 3+ messages in thread
From: Yasuaki Ishimatsu @ 2016-12-13 16:47 UTC (permalink / raw)
  To: Reza Arbab; +Cc: linux-mm, isimatu.yasuaki, linux-kernel

Hi Reza,

Thank you for your review.

On 12/13/2016 10:54 AM, Reza Arbab wrote:
> On Mon, Dec 12, 2016 at 03:29:04PM -0500, Yasuaki Ishimatsu wrote:
>> --- a/drivers/base/memory.c
>> +++ b/drivers/base/memory.c
>> @@ -410,15 +410,13 @@ static ssize_t show_valid_zones(struct device *dev,
>>     sprintf(buf, "%s", zone->name);
>>
>>     /* MMOP_ONLINE_KERNEL */
>> -    zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL);
>> -    if (zone_shift) {
>> +    if (zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL, &zone_shift)) {
>>         strcat(buf, " ");
>>         strcat(buf, (zone + zone_shift)->name);
>>     }
>>
>>     /* MMOP_ONLINE_MOVABLE */
>> -    zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE);
>> -    if (zone_shift) {
>> +    if (zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE, &zone_shift)) {
>>         strcat(buf, " ");
>>         strcat(buf, (zone + zone_shift)->name);
>>     }
>
> You still need to check zone_shift != 0, otherwise you may get duplicate output:
>
> $ cat /sys/devices/system/node/node1/memory256/valid_zones
> Movable Normal Movable
> $ cat /sys/devices/system/node/node1/memory257/valid_zones
> Movable Movable

I'll update it.

>
>> --- a/mm/memory_hotplug.c
>> +++ b/mm/memory_hotplug.c
>> @@ -1033,8 +1033,8 @@ static void node_states_set_node(int node, struct memory_notify *arg)
>>     node_set_state(node, N_MEMORY);
>> }
>>
>> -int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
>> -           enum zone_type target)
>> +bool zone_can_shift(unsigned long pfn, unsigned long nr_pages,
>> +           enum zone_type target, int *zone_shift)
>> {
>>     struct zone *zone = page_zone(pfn_to_page(pfn));
>>     enum zone_type idx = zone_idx(zone);
>
> I think you should initialize zone_shift here. It should be 0 if the function returns false.
>
>     *zone_shift = 0;

I'll update it.

Thanks,
Yasuaki Ishimatsu

>> @@ -1043,26 +1043,27 @@ int zone_can_shift(unsigned long pfn, unsigned long nr_pages,
>>     if (idx < target) {
>>         /* pages must be at end of current zone */
>>         if (pfn + nr_pages != zone_end_pfn(zone))
>> -            return 0;
>> +            return false;
>>
>>         /* no zones in use between current zone and target */
>>         for (i = idx + 1; i < target; i++)
>>             if (zone_is_initialized(zone - idx + i))
>> -                return 0;
>> +                return false;
>>     }
>>
>>     if (target < idx) {
>>         /* pages must be at beginning of current zone */
>>         if (pfn != zone->zone_start_pfn)
>> -            return 0;
>> +            return false;
>>
>>         /* no zones in use between current zone and target */
>>         for (i = target + 1; i < idx; i++)
>>             if (zone_is_initialized(zone - idx + i))
>> -                return 0;
>> +                return false;
>>     }
>>
>> -    return target - idx;
>> +    *zone_shift = target - idx;
>> +    return true;
>> }
>>
>> /* Must be protected by mem_hotplug_begin() */
>> @@ -1089,10 +1090,13 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ
>>         !can_online_high_movable(zone))
>>         return -EINVAL;
>>
>> -    if (online_type == MMOP_ONLINE_KERNEL)
>> -        zone_shift = zone_can_shift(pfn, nr_pages, ZONE_NORMAL);
>> -    else if (online_type == MMOP_ONLINE_MOVABLE)
>> -        zone_shift = zone_can_shift(pfn, nr_pages, ZONE_MOVABLE);
>> +    if (online_type == MMOP_ONLINE_KERNEL) {
>> +        if (!zone_can_shift(pfn, nr_pages, ZONE_NORMAL, &zone_shift))
>> +            return -EINVAL;
>> +    } else if (online_type == MMOP_ONLINE_MOVABLE) {
>> +        if (!zone_can_shift(pfn, nr_pages, ZONE_MOVABLE, &zone_shift))
>> +            return -EINVAL;
>> +    }
>>
>>     zone = move_pfn_range(zone_shift, pfn, pfn + nr_pages);
>>     if (!zone)
>> --
>> 1.8.3.1
>>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-12-13 16:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-12 20:29 memory_hotplug: zone_can_shift() returns boolean value Yasuaki Ishimatsu
2016-12-13 15:54 ` Reza Arbab
2016-12-13 16:47   ` Yasuaki Ishimatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).