All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
@ 2015-12-18 14:50 ` Vitaly Kuznetsov
  0 siblings, 0 replies; 12+ messages in thread
From: Vitaly Kuznetsov @ 2015-12-18 14:50 UTC (permalink / raw)
  To: linux-mm
  Cc: linux-kernel, Andrew Morton, Tang Chen, Naoya Horiguchi,
	Xishi Qiu, Sheng Yong, David Rientjes, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

Out of memory condition is not a bug and while we can't add new memory in
such case crashing the system seems wrong. Propagating the return value
from register_memory_resource() requires interface change.

Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Tang Chen <tangchen@cn.fujitsu.com>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Cc: Xishi Qiu <qiuxishi@huawei.com>
Cc: Sheng Yong <shengyong1@huawei.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Zhu Guihua <zhugh.fnst@cn.fujitsu.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: David Vrabel <david.vrabel@citrix.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 mm/memory_hotplug.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 67d488a..9392f01 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -127,11 +127,13 @@ void mem_hotplug_done(void)
 }
 
 /* add this memory to iomem resource */
-static struct resource *register_memory_resource(u64 start, u64 size)
+static int register_memory_resource(u64 start, u64 size,
+				    struct resource **resource)
 {
 	struct resource *res;
 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
-	BUG_ON(!res);
+	if (!res)
+		return -ENOMEM;
 
 	res->name = "System RAM";
 	res->start = start;
@@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
 	if (request_resource(&iomem_resource, res) < 0) {
 		pr_debug("System RAM resource %pR cannot be added\n", res);
 		kfree(res);
-		res = NULL;
+		return -EEXIST;
 	}
-	return res;
+	*resource = res;
+	return 0;
 }
 
 static void release_memory_resource(struct resource *res)
@@ -1311,9 +1314,9 @@ int __ref add_memory(int nid, u64 start, u64 size)
 	struct resource *res;
 	int ret;
 
-	res = register_memory_resource(start, size);
-	if (!res)
-		return -EEXIST;
+	ret = register_memory_resource(start, size, &res);
+	if (ret)
+		return ret;
 
 	ret = add_memory_resource(nid, res);
 	if (ret < 0)
-- 
2.4.3


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

* [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
@ 2015-12-18 14:50 ` Vitaly Kuznetsov
  0 siblings, 0 replies; 12+ messages in thread
From: Vitaly Kuznetsov @ 2015-12-18 14:50 UTC (permalink / raw)
  To: linux-mm
  Cc: linux-kernel, Andrew Morton, Tang Chen, Naoya Horiguchi,
	Xishi Qiu, Sheng Yong, David Rientjes, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

Out of memory condition is not a bug and while we can't add new memory in
such case crashing the system seems wrong. Propagating the return value
from register_memory_resource() requires interface change.

Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Tang Chen <tangchen@cn.fujitsu.com>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Cc: Xishi Qiu <qiuxishi@huawei.com>
Cc: Sheng Yong <shengyong1@huawei.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Zhu Guihua <zhugh.fnst@cn.fujitsu.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: David Vrabel <david.vrabel@citrix.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 mm/memory_hotplug.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 67d488a..9392f01 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -127,11 +127,13 @@ void mem_hotplug_done(void)
 }
 
 /* add this memory to iomem resource */
-static struct resource *register_memory_resource(u64 start, u64 size)
+static int register_memory_resource(u64 start, u64 size,
+				    struct resource **resource)
 {
 	struct resource *res;
 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
-	BUG_ON(!res);
+	if (!res)
+		return -ENOMEM;
 
 	res->name = "System RAM";
 	res->start = start;
@@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
 	if (request_resource(&iomem_resource, res) < 0) {
 		pr_debug("System RAM resource %pR cannot be added\n", res);
 		kfree(res);
-		res = NULL;
+		return -EEXIST;
 	}
-	return res;
+	*resource = res;
+	return 0;
 }
 
 static void release_memory_resource(struct resource *res)
@@ -1311,9 +1314,9 @@ int __ref add_memory(int nid, u64 start, u64 size)
 	struct resource *res;
 	int ret;
 
-	res = register_memory_resource(start, size);
-	if (!res)
-		return -EEXIST;
+	ret = register_memory_resource(start, size, &res);
+	if (ret)
+		return ret;
 
 	ret = add_memory_resource(nid, res);
 	if (ret < 0)
-- 
2.4.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
  2015-12-18 14:50 ` Vitaly Kuznetsov
@ 2015-12-18 16:33   ` Igor Mammedov
  -1 siblings, 0 replies; 12+ messages in thread
From: Igor Mammedov @ 2015-12-18 16:33 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: linux-mm, linux-kernel, Andrew Morton, Tang Chen,
	Naoya Horiguchi, Xishi Qiu, Sheng Yong, David Rientjes,
	Zhu Guihua, Dan Williams, David Vrabel

On Fri, 18 Dec 2015 15:50:24 +0100
Vitaly Kuznetsov <vkuznets@redhat.com> wrote:

> Out of memory condition is not a bug and while we can't add new memory in
> such case crashing the system seems wrong. Propagating the return value
> from register_memory_resource() requires interface change.
> 
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Tang Chen <tangchen@cn.fujitsu.com>
> Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> Cc: Xishi Qiu <qiuxishi@huawei.com>
> Cc: Sheng Yong <shengyong1@huawei.com>
> Cc: David Rientjes <rientjes@google.com>
> Cc: Zhu Guihua <zhugh.fnst@cn.fujitsu.com>
> Cc: Dan Williams <dan.j.williams@intel.com>
> Cc: David Vrabel <david.vrabel@citrix.com>
> Cc: Igor Mammedov <imammedo@redhat.com>
> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> ---
>  mm/memory_hotplug.c | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
> index 67d488a..9392f01 100644
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> @@ -127,11 +127,13 @@ void mem_hotplug_done(void)
>  }
>  
>  /* add this memory to iomem resource */
> -static struct resource *register_memory_resource(u64 start, u64 size)
> +static int register_memory_resource(u64 start, u64 size,
> +				    struct resource **resource)
>  {
>  	struct resource *res;
>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> -	BUG_ON(!res);
> +	if (!res)
> +		return -ENOMEM;
>  
>  	res->name = "System RAM";
>  	res->start = start;
> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
>  	if (request_resource(&iomem_resource, res) < 0) {
>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>  		kfree(res);
> -		res = NULL;
> +		return -EEXIST;
>  	}
> -	return res;
> +	*resource = res;
> +	return 0;
>  }
>  
>  static void release_memory_resource(struct resource *res)
> @@ -1311,9 +1314,9 @@ int __ref add_memory(int nid, u64 start, u64 size)
>  	struct resource *res;
>  	int ret;
>  
> -	res = register_memory_resource(start, size);
> -	if (!res)
> -		return -EEXIST;
> +	ret = register_memory_resource(start, size, &res);
> +	if (ret)
> +		return ret;
>  
>  	ret = add_memory_resource(nid, res);
>  	if (ret < 0)

Reviewed-by: Igor Mammedov <imammedo@redhat.com>

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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
@ 2015-12-18 16:33   ` Igor Mammedov
  0 siblings, 0 replies; 12+ messages in thread
From: Igor Mammedov @ 2015-12-18 16:33 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: linux-mm, linux-kernel, Andrew Morton, Tang Chen,
	Naoya Horiguchi, Xishi Qiu, Sheng Yong, David Rientjes,
	Zhu Guihua, Dan Williams, David Vrabel

On Fri, 18 Dec 2015 15:50:24 +0100
Vitaly Kuznetsov <vkuznets@redhat.com> wrote:

> Out of memory condition is not a bug and while we can't add new memory in
> such case crashing the system seems wrong. Propagating the return value
> from register_memory_resource() requires interface change.
> 
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Tang Chen <tangchen@cn.fujitsu.com>
> Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> Cc: Xishi Qiu <qiuxishi@huawei.com>
> Cc: Sheng Yong <shengyong1@huawei.com>
> Cc: David Rientjes <rientjes@google.com>
> Cc: Zhu Guihua <zhugh.fnst@cn.fujitsu.com>
> Cc: Dan Williams <dan.j.williams@intel.com>
> Cc: David Vrabel <david.vrabel@citrix.com>
> Cc: Igor Mammedov <imammedo@redhat.com>
> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> ---
>  mm/memory_hotplug.c | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
> index 67d488a..9392f01 100644
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> @@ -127,11 +127,13 @@ void mem_hotplug_done(void)
>  }
>  
>  /* add this memory to iomem resource */
> -static struct resource *register_memory_resource(u64 start, u64 size)
> +static int register_memory_resource(u64 start, u64 size,
> +				    struct resource **resource)
>  {
>  	struct resource *res;
>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> -	BUG_ON(!res);
> +	if (!res)
> +		return -ENOMEM;
>  
>  	res->name = "System RAM";
>  	res->start = start;
> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
>  	if (request_resource(&iomem_resource, res) < 0) {
>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>  		kfree(res);
> -		res = NULL;
> +		return -EEXIST;
>  	}
> -	return res;
> +	*resource = res;
> +	return 0;
>  }
>  
>  static void release_memory_resource(struct resource *res)
> @@ -1311,9 +1314,9 @@ int __ref add_memory(int nid, u64 start, u64 size)
>  	struct resource *res;
>  	int ret;
>  
> -	res = register_memory_resource(start, size);
> -	if (!res)
> -		return -EEXIST;
> +	ret = register_memory_resource(start, size, &res);
> +	if (ret)
> +		return ret;
>  
>  	ret = add_memory_resource(nid, res);
>  	if (ret < 0)

Reviewed-by: Igor Mammedov <imammedo@redhat.com>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
  2015-12-18 14:50 ` Vitaly Kuznetsov
@ 2015-12-18 22:50   ` Andrew Morton
  -1 siblings, 0 replies; 12+ messages in thread
From: Andrew Morton @ 2015-12-18 22:50 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: linux-mm, linux-kernel, Tang Chen, Naoya Horiguchi, Xishi Qiu,
	Sheng Yong, David Rientjes, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

On Fri, 18 Dec 2015 15:50:24 +0100 Vitaly Kuznetsov <vkuznets@redhat.com> wrote:

> Out of memory condition is not a bug and while we can't add new memory in
> such case crashing the system seems wrong. Propagating the return value
> from register_memory_resource() requires interface change.
> 
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> +static int register_memory_resource(u64 start, u64 size,
> +				    struct resource **resource)
>  {
>  	struct resource *res;
>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> -	BUG_ON(!res);
> +	if (!res)
> +		return -ENOMEM;
>  
>  	res->name = "System RAM";
>  	res->start = start;
> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
>  	if (request_resource(&iomem_resource, res) < 0) {
>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>  		kfree(res);
> -		res = NULL;
> +		return -EEXIST;
>  	}
> -	return res;
> +	*resource = res;
> +	return 0;
>  }

Was there a reason for overwriting the request_resource() return value?
Ordinarily it should be propagated back to callers.

Please review.

--- a/mm/memory_hotplug.c~memory-hotplug-dont-bug-in-register_memory_resource-fix
+++ a/mm/memory_hotplug.c
@@ -131,7 +131,9 @@ static int register_memory_resource(u64
 				    struct resource **resource)
 {
 	struct resource *res;
+	int ret = 0;
 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
+
 	if (!res)
 		return -ENOMEM;
 
@@ -139,13 +141,14 @@ static int register_memory_resource(u64
 	res->start = start;
 	res->end = start + size - 1;
 	res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
-	if (request_resource(&iomem_resource, res) < 0) {
+	ret = request_resource(&iomem_resource, res);
+	if (ret < 0) {
 		pr_debug("System RAM resource %pR cannot be added\n", res);
 		kfree(res);
-		return -EEXIST;
+	} else {
+		*resource = res;
 	}
-	*resource = res;
-	return 0;
+	return ret;
 }
 
 static void release_memory_resource(struct resource *res)
_


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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
@ 2015-12-18 22:50   ` Andrew Morton
  0 siblings, 0 replies; 12+ messages in thread
From: Andrew Morton @ 2015-12-18 22:50 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: linux-mm, linux-kernel, Tang Chen, Naoya Horiguchi, Xishi Qiu,
	Sheng Yong, David Rientjes, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

On Fri, 18 Dec 2015 15:50:24 +0100 Vitaly Kuznetsov <vkuznets@redhat.com> wrote:

> Out of memory condition is not a bug and while we can't add new memory in
> such case crashing the system seems wrong. Propagating the return value
> from register_memory_resource() requires interface change.
> 
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> +static int register_memory_resource(u64 start, u64 size,
> +				    struct resource **resource)
>  {
>  	struct resource *res;
>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> -	BUG_ON(!res);
> +	if (!res)
> +		return -ENOMEM;
>  
>  	res->name = "System RAM";
>  	res->start = start;
> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
>  	if (request_resource(&iomem_resource, res) < 0) {
>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>  		kfree(res);
> -		res = NULL;
> +		return -EEXIST;
>  	}
> -	return res;
> +	*resource = res;
> +	return 0;
>  }

Was there a reason for overwriting the request_resource() return value?
Ordinarily it should be propagated back to callers.

Please review.

--- a/mm/memory_hotplug.c~memory-hotplug-dont-bug-in-register_memory_resource-fix
+++ a/mm/memory_hotplug.c
@@ -131,7 +131,9 @@ static int register_memory_resource(u64
 				    struct resource **resource)
 {
 	struct resource *res;
+	int ret = 0;
 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
+
 	if (!res)
 		return -ENOMEM;
 
@@ -139,13 +141,14 @@ static int register_memory_resource(u64
 	res->start = start;
 	res->end = start + size - 1;
 	res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
-	if (request_resource(&iomem_resource, res) < 0) {
+	ret = request_resource(&iomem_resource, res);
+	if (ret < 0) {
 		pr_debug("System RAM resource %pR cannot be added\n", res);
 		kfree(res);
-		return -EEXIST;
+	} else {
+		*resource = res;
 	}
-	*resource = res;
-	return 0;
+	return ret;
 }
 
 static void release_memory_resource(struct resource *res)
_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
  2015-12-18 22:50   ` Andrew Morton
@ 2015-12-21 10:13     ` Vitaly Kuznetsov
  -1 siblings, 0 replies; 12+ messages in thread
From: Vitaly Kuznetsov @ 2015-12-21 10:13 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-mm, linux-kernel, Tang Chen, Naoya Horiguchi, Xishi Qiu,
	Sheng Yong, David Rientjes, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

Andrew Morton <akpm@linux-foundation.org> writes:

> On Fri, 18 Dec 2015 15:50:24 +0100 Vitaly Kuznetsov <vkuznets@redhat.com> wrote:
>
>> Out of memory condition is not a bug and while we can't add new memory in
>> such case crashing the system seems wrong. Propagating the return value
>> from register_memory_resource() requires interface change.
>> 
>> --- a/mm/memory_hotplug.c
>> +++ b/mm/memory_hotplug.c
>> +static int register_memory_resource(u64 start, u64 size,
>> +				    struct resource **resource)
>>  {
>>  	struct resource *res;
>>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
>> -	BUG_ON(!res);
>> +	if (!res)
>> +		return -ENOMEM;
>>  
>>  	res->name = "System RAM";
>>  	res->start = start;
>> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
>>  	if (request_resource(&iomem_resource, res) < 0) {
>>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>>  		kfree(res);
>> -		res = NULL;
>> +		return -EEXIST;
>>  	}
>> -	return res;
>> +	*resource = res;
>> +	return 0;
>>  }
>
> Was there a reason for overwriting the request_resource() return
> value?
> Ordinarily it should be propagated back to callers.
>
> Please review.
>

This is a nice-to-have addition but it will break at least ACPI
memhotplug: request_resource() has the following:

conflict = request_resource_conflict(root, new);
return conflict ? -EBUSY : 0;

so we'll end up returning -EBUSY from register_memory_resource() and
add_memory(), at the same time acpi_memory_enable_device() counts on
-EEXIST:

result = add_memory(node, info->start_addr, info->length);

/*
* If the memory block has been used by the kernel, add_memory()
* returns -EEXIST. If add_memory() returns the other error, it
* means that this memory block is not used by the kernel.
*/
if (result && result != -EEXIST)
continue;

So I see 3 options here:
1) Keep the overwrite
2) Change the request_resource() return value to -EEXIST
3) Adapt all add_memory() call sites to -EBUSY.

Please let me know your preference.

> --- a/mm/memory_hotplug.c~memory-hotplug-dont-bug-in-register_memory_resource-fix
> +++ a/mm/memory_hotplug.c
> @@ -131,7 +131,9 @@ static int register_memory_resource(u64
>  				    struct resource **resource)
>  {
>  	struct resource *res;
> +	int ret = 0;
>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> +
>  	if (!res)
>  		return -ENOMEM;
>
> @@ -139,13 +141,14 @@ static int register_memory_resource(u64
>  	res->start = start;
>  	res->end = start + size - 1;
>  	res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
> -	if (request_resource(&iomem_resource, res) < 0) {
> +	ret = request_resource(&iomem_resource, res);
> +	if (ret < 0) {
>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>  		kfree(res);
> -		return -EEXIST;
> +	} else {
> +		*resource = res;
>  	}
> -	*resource = res;
> -	return 0;
> +	return ret;
>  }
>
>  static void release_memory_resource(struct resource *res)
> _

-- 
  Vitaly

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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
@ 2015-12-21 10:13     ` Vitaly Kuznetsov
  0 siblings, 0 replies; 12+ messages in thread
From: Vitaly Kuznetsov @ 2015-12-21 10:13 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-mm, linux-kernel, Tang Chen, Naoya Horiguchi, Xishi Qiu,
	Sheng Yong, David Rientjes, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

Andrew Morton <akpm@linux-foundation.org> writes:

> On Fri, 18 Dec 2015 15:50:24 +0100 Vitaly Kuznetsov <vkuznets@redhat.com> wrote:
>
>> Out of memory condition is not a bug and while we can't add new memory in
>> such case crashing the system seems wrong. Propagating the return value
>> from register_memory_resource() requires interface change.
>> 
>> --- a/mm/memory_hotplug.c
>> +++ b/mm/memory_hotplug.c
>> +static int register_memory_resource(u64 start, u64 size,
>> +				    struct resource **resource)
>>  {
>>  	struct resource *res;
>>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
>> -	BUG_ON(!res);
>> +	if (!res)
>> +		return -ENOMEM;
>>  
>>  	res->name = "System RAM";
>>  	res->start = start;
>> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
>>  	if (request_resource(&iomem_resource, res) < 0) {
>>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>>  		kfree(res);
>> -		res = NULL;
>> +		return -EEXIST;
>>  	}
>> -	return res;
>> +	*resource = res;
>> +	return 0;
>>  }
>
> Was there a reason for overwriting the request_resource() return
> value?
> Ordinarily it should be propagated back to callers.
>
> Please review.
>

This is a nice-to-have addition but it will break at least ACPI
memhotplug: request_resource() has the following:

conflict = request_resource_conflict(root, new);
return conflict ? -EBUSY : 0;

so we'll end up returning -EBUSY from register_memory_resource() and
add_memory(), at the same time acpi_memory_enable_device() counts on
-EEXIST:

result = add_memory(node, info->start_addr, info->length);

/*
* If the memory block has been used by the kernel, add_memory()
* returns -EEXIST. If add_memory() returns the other error, it
* means that this memory block is not used by the kernel.
*/
if (result && result != -EEXIST)
continue;

So I see 3 options here:
1) Keep the overwrite
2) Change the request_resource() return value to -EEXIST
3) Adapt all add_memory() call sites to -EBUSY.

Please let me know your preference.

> --- a/mm/memory_hotplug.c~memory-hotplug-dont-bug-in-register_memory_resource-fix
> +++ a/mm/memory_hotplug.c
> @@ -131,7 +131,9 @@ static int register_memory_resource(u64
>  				    struct resource **resource)
>  {
>  	struct resource *res;
> +	int ret = 0;
>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> +
>  	if (!res)
>  		return -ENOMEM;
>
> @@ -139,13 +141,14 @@ static int register_memory_resource(u64
>  	res->start = start;
>  	res->end = start + size - 1;
>  	res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
> -	if (request_resource(&iomem_resource, res) < 0) {
> +	ret = request_resource(&iomem_resource, res);
> +	if (ret < 0) {
>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>  		kfree(res);
> -		return -EEXIST;
> +	} else {
> +		*resource = res;
>  	}
> -	*resource = res;
> -	return 0;
> +	return ret;
>  }
>
>  static void release_memory_resource(struct resource *res)
> _

-- 
  Vitaly

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
  2015-12-21 10:13     ` Vitaly Kuznetsov
@ 2015-12-21 23:06       ` Andrew Morton
  -1 siblings, 0 replies; 12+ messages in thread
From: Andrew Morton @ 2015-12-21 23:06 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: linux-mm, linux-kernel, Tang Chen, Naoya Horiguchi, Xishi Qiu,
	Sheng Yong, David Rientjes, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

On Mon, 21 Dec 2015 11:13:15 +0100 Vitaly Kuznetsov <vkuznets@redhat.com> wrote:

> Andrew Morton <akpm@linux-foundation.org> writes:
> 
> > On Fri, 18 Dec 2015 15:50:24 +0100 Vitaly Kuznetsov <vkuznets@redhat.com> wrote:
> >
> >> Out of memory condition is not a bug and while we can't add new memory in
> >> such case crashing the system seems wrong. Propagating the return value
> >> from register_memory_resource() requires interface change.
> >> 
> >> --- a/mm/memory_hotplug.c
> >> +++ b/mm/memory_hotplug.c
> >> +static int register_memory_resource(u64 start, u64 size,
> >> +				    struct resource **resource)
> >>  {
> >>  	struct resource *res;
> >>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> >> -	BUG_ON(!res);
> >> +	if (!res)
> >> +		return -ENOMEM;
> >>  
> >>  	res->name = "System RAM";
> >>  	res->start = start;
> >> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
> >>  	if (request_resource(&iomem_resource, res) < 0) {
> >>  		pr_debug("System RAM resource %pR cannot be added\n", res);
> >>  		kfree(res);
> >> -		res = NULL;
> >> +		return -EEXIST;
> >>  	}
> >> -	return res;
> >> +	*resource = res;
> >> +	return 0;
> >>  }
> >
> > Was there a reason for overwriting the request_resource() return
> > value?
> > Ordinarily it should be propagated back to callers.
> >
> > Please review.
> >
> 
> This is a nice-to-have addition but it will break at least ACPI
> memhotplug: request_resource() has the following:
> 
> conflict = request_resource_conflict(root, new);
> return conflict ? -EBUSY : 0;
> 
> so we'll end up returning -EBUSY from register_memory_resource() and
> add_memory(), at the same time acpi_memory_enable_device() counts on
> -EEXIST:
> 
> result = add_memory(node, info->start_addr, info->length);
> 
> /*
> * If the memory block has been used by the kernel, add_memory()
> * returns -EEXIST. If add_memory() returns the other error, it
> * means that this memory block is not used by the kernel.
> */
> if (result && result != -EEXIST)
> continue;
> 
> So I see 3 options here:
> 1) Keep the overwrite
> 2) Change the request_resource() return value to -EEXIST
> 3) Adapt all add_memory() call sites to -EBUSY.
> 
> Please let me know your preference.

urgh, what a mess.  We should standardize on EBUSY or EEXIST, I don't
see that it matter much which is chosen.  And for robustness the
callers should be checking for (err < 0) unless there's a very good
reason otherwise.

But it doesn't seem terribly important.

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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
@ 2015-12-21 23:06       ` Andrew Morton
  0 siblings, 0 replies; 12+ messages in thread
From: Andrew Morton @ 2015-12-21 23:06 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: linux-mm, linux-kernel, Tang Chen, Naoya Horiguchi, Xishi Qiu,
	Sheng Yong, David Rientjes, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

On Mon, 21 Dec 2015 11:13:15 +0100 Vitaly Kuznetsov <vkuznets@redhat.com> wrote:

> Andrew Morton <akpm@linux-foundation.org> writes:
> 
> > On Fri, 18 Dec 2015 15:50:24 +0100 Vitaly Kuznetsov <vkuznets@redhat.com> wrote:
> >
> >> Out of memory condition is not a bug and while we can't add new memory in
> >> such case crashing the system seems wrong. Propagating the return value
> >> from register_memory_resource() requires interface change.
> >> 
> >> --- a/mm/memory_hotplug.c
> >> +++ b/mm/memory_hotplug.c
> >> +static int register_memory_resource(u64 start, u64 size,
> >> +				    struct resource **resource)
> >>  {
> >>  	struct resource *res;
> >>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> >> -	BUG_ON(!res);
> >> +	if (!res)
> >> +		return -ENOMEM;
> >>  
> >>  	res->name = "System RAM";
> >>  	res->start = start;
> >> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
> >>  	if (request_resource(&iomem_resource, res) < 0) {
> >>  		pr_debug("System RAM resource %pR cannot be added\n", res);
> >>  		kfree(res);
> >> -		res = NULL;
> >> +		return -EEXIST;
> >>  	}
> >> -	return res;
> >> +	*resource = res;
> >> +	return 0;
> >>  }
> >
> > Was there a reason for overwriting the request_resource() return
> > value?
> > Ordinarily it should be propagated back to callers.
> >
> > Please review.
> >
> 
> This is a nice-to-have addition but it will break at least ACPI
> memhotplug: request_resource() has the following:
> 
> conflict = request_resource_conflict(root, new);
> return conflict ? -EBUSY : 0;
> 
> so we'll end up returning -EBUSY from register_memory_resource() and
> add_memory(), at the same time acpi_memory_enable_device() counts on
> -EEXIST:
> 
> result = add_memory(node, info->start_addr, info->length);
> 
> /*
> * If the memory block has been used by the kernel, add_memory()
> * returns -EEXIST. If add_memory() returns the other error, it
> * means that this memory block is not used by the kernel.
> */
> if (result && result != -EEXIST)
> continue;
> 
> So I see 3 options here:
> 1) Keep the overwrite
> 2) Change the request_resource() return value to -EEXIST
> 3) Adapt all add_memory() call sites to -EBUSY.
> 
> Please let me know your preference.

urgh, what a mess.  We should standardize on EBUSY or EEXIST, I don't
see that it matter much which is chosen.  And for robustness the
callers should be checking for (err < 0) unless there's a very good
reason otherwise.

But it doesn't seem terribly important.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
  2015-12-18 14:50 ` Vitaly Kuznetsov
@ 2015-12-22 21:56   ` David Rientjes
  -1 siblings, 0 replies; 12+ messages in thread
From: David Rientjes @ 2015-12-22 21:56 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: linux-mm, linux-kernel, Andrew Morton, Tang Chen,
	Naoya Horiguchi, Xishi Qiu, Sheng Yong, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

On Fri, 18 Dec 2015, Vitaly Kuznetsov wrote:

> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
> index 67d488a..9392f01 100644
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> @@ -127,11 +127,13 @@ void mem_hotplug_done(void)
>  }
>  
>  /* add this memory to iomem resource */
> -static struct resource *register_memory_resource(u64 start, u64 size)
> +static int register_memory_resource(u64 start, u64 size,
> +				    struct resource **resource)
>  {
>  	struct resource *res;
>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> -	BUG_ON(!res);
> +	if (!res)
> +		return -ENOMEM;
>  
>  	res->name = "System RAM";
>  	res->start = start;
> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
>  	if (request_resource(&iomem_resource, res) < 0) {
>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>  		kfree(res);
> -		res = NULL;
> +		return -EEXIST;
>  	}
> -	return res;
> +	*resource = res;
> +	return 0;
>  }
>  
>  static void release_memory_resource(struct resource *res)
> @@ -1311,9 +1314,9 @@ int __ref add_memory(int nid, u64 start, u64 size)
>  	struct resource *res;
>  	int ret;
>  
> -	res = register_memory_resource(start, size);
> -	if (!res)
> -		return -EEXIST;
> +	ret = register_memory_resource(start, size, &res);
> +	if (ret)
> +		return ret;
>  
>  	ret = add_memory_resource(nid, res);
>  	if (ret < 0)

Wouldn't it be simpler and cleaner to keep the return type of 
register_memory_resource() the same and return ERR_PTR(-ENOMEM) or 
ERR_PTR(-EEXIST) on error?  add_memory() can check IS_ERR(res) and return 
PTR_ERR(res).

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

* Re: [PATCH] memory-hotplug: don't BUG() in register_memory_resource()
@ 2015-12-22 21:56   ` David Rientjes
  0 siblings, 0 replies; 12+ messages in thread
From: David Rientjes @ 2015-12-22 21:56 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: linux-mm, linux-kernel, Andrew Morton, Tang Chen,
	Naoya Horiguchi, Xishi Qiu, Sheng Yong, Zhu Guihua, Dan Williams,
	David Vrabel, Igor Mammedov

On Fri, 18 Dec 2015, Vitaly Kuznetsov wrote:

> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
> index 67d488a..9392f01 100644
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> @@ -127,11 +127,13 @@ void mem_hotplug_done(void)
>  }
>  
>  /* add this memory to iomem resource */
> -static struct resource *register_memory_resource(u64 start, u64 size)
> +static int register_memory_resource(u64 start, u64 size,
> +				    struct resource **resource)
>  {
>  	struct resource *res;
>  	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
> -	BUG_ON(!res);
> +	if (!res)
> +		return -ENOMEM;
>  
>  	res->name = "System RAM";
>  	res->start = start;
> @@ -140,9 +142,10 @@ static struct resource *register_memory_resource(u64 start, u64 size)
>  	if (request_resource(&iomem_resource, res) < 0) {
>  		pr_debug("System RAM resource %pR cannot be added\n", res);
>  		kfree(res);
> -		res = NULL;
> +		return -EEXIST;
>  	}
> -	return res;
> +	*resource = res;
> +	return 0;
>  }
>  
>  static void release_memory_resource(struct resource *res)
> @@ -1311,9 +1314,9 @@ int __ref add_memory(int nid, u64 start, u64 size)
>  	struct resource *res;
>  	int ret;
>  
> -	res = register_memory_resource(start, size);
> -	if (!res)
> -		return -EEXIST;
> +	ret = register_memory_resource(start, size, &res);
> +	if (ret)
> +		return ret;
>  
>  	ret = add_memory_resource(nid, res);
>  	if (ret < 0)

Wouldn't it be simpler and cleaner to keep the return type of 
register_memory_resource() the same and return ERR_PTR(-ENOMEM) or 
ERR_PTR(-EEXIST) on error?  add_memory() can check IS_ERR(res) and return 
PTR_ERR(res).

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

end of thread, other threads:[~2015-12-22 21:56 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-18 14:50 [PATCH] memory-hotplug: don't BUG() in register_memory_resource() Vitaly Kuznetsov
2015-12-18 14:50 ` Vitaly Kuznetsov
2015-12-18 16:33 ` Igor Mammedov
2015-12-18 16:33   ` Igor Mammedov
2015-12-18 22:50 ` Andrew Morton
2015-12-18 22:50   ` Andrew Morton
2015-12-21 10:13   ` Vitaly Kuznetsov
2015-12-21 10:13     ` Vitaly Kuznetsov
2015-12-21 23:06     ` Andrew Morton
2015-12-21 23:06       ` Andrew Morton
2015-12-22 21:56 ` David Rientjes
2015-12-22 21:56   ` David Rientjes

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.