Skip to contentSkip to conversation

memory-hotplug: fix store_mem_state() return value

PATCH Superseded Reza Arbab 2 participants 2 messages May 5, 2016

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Reza Arbab

author
#0 lore raw

PATCH 1/1 memory-hotplug: fix store_mem_state() return value

Attempting to online memory which is already online will cause this:

1. store_mem_state() called with buf="online"
2. device_online() returns 1 because device is already online
3. store_mem_state() returns 1
4. calling code interprets this as 1-byte buffer read
5. store_mem_state() called again with buf="nline"
6. store_mem_state() returns -EINVAL

Example:

$ cat /sys/devices/system/memory/memory0/state
online
$ echo online > /sys/devices/system/memory/memory0/state
-bash: echo: write error: Invalid argument

Fix the return value of store_mem_state() so this doesn't happen.

---
 drivers/base/memory.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/memory.c +1 -3
index 961e2cf..031950f 100644
@@ -359,9 +359,7 @@ store_mem_state(struct device *dev,
 err:
 	unlock_device_hotplug();
 
-	if (ret)
-		return ret;
-	return count;
+	return ret < 0 ? ret : count;
 }
 
 /*
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 28 lines · Reza Arbab

Attempting to online memory which is already online will cause this:

1. store_mem_state() called with buf="online"
2. device_online() returns 1 because device is already online
3. store_mem_state() returns 1
4. calling code interprets this as 1-byte buffer read
5. store_mem_state() called again with buf="nline"
6. store_mem_state() returns -EINVAL

Example:

$ cat /sys/devices/system/memory/memory0/state
online
$ echo online > /sys/devices/system/memory/memory0/state
-bash: echo: write error: Invalid argument

Fix the return value of store_mem_state() so this doesn't happen.

---
 drivers/base/memory.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/memory.c +0 -0
index 961e2cf..031950f 100644

drivers/base/memory.c store_mem_state(struct device *dev, · line 362

Quoted 7 lines
drivers/base/memory.c +1 -3
@@ -359,9 +359,7 @@ store_mem_state(struct device *dev,
 err:
 	unlock_device_hotplug();
 
-	if (ret)
-		return ret;
-	return count;
+	return ret < 0 ? ret : count;

Please spell this out as a real if () statement, it's very hard to now see if you really did change the logic here or not.

Hint, it's only a 1 line patch that way, right?

thanks,

greg k-h