Skip to contentSkip to conversation

drivers/base/memory: Two small cleanups

PATCH In review Gavin Shan 3 participants 6 messages Jan 20, 2023 – Jan 23, 2023

Subsystems (3)
lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

cover drivers/base/memory: Two small cleanups

PATCH[1] Fixes comments for phys_index_show() since it's showing
the memory block ID instead of the section ID.
PATCH[2] Use an array to show memory block state in state_show()

Gavin Shan (2):
drivers/base/memory: Fix comments for phys_index_show() drivers/base/memory: Use array to show memory block state

 drivers/base/memory.c | 34 ++++++++--------------------------
 1 file changed, 8 insertions(+), 26 deletions(-)
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 1/2 drivers/base/memory: Fix comments for phys_index_show()

According to 'admin-guide/mm/memory-hotplug.rst', the memory block ID, instead of the section index, is shown by '/sys/devices/system/memory/ memoryX/phys_index'.

Fix the comments to match with 'admin-guide/mm/memory-hotplug.rst'. Besides, use the existing helper memory_block_id() to convert the section index to the memory block index.

No functional change intended.

---
 drivers/base/memory.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/base/memory.c +2 -7
index fe98fb8d94e5..b456ac213610 100644
@@ -115,18 +115,13 @@ unsigned long __weak memory_block_size_bytes(void)
 }
 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
 
-/*
- * Show the first physical section index (number) of this memory block.
- */
+/* Show the memory block ID, relative to the memory block size */
 static ssize_t phys_index_show(struct device *dev,
 			       struct device_attribute *attr, char *buf)
 {
 	struct memory_block *mem = to_memory_block(dev);
-	unsigned long phys_index;
-
-	phys_index = mem->start_section_nr / sections_per_block;
 
-	return sysfs_emit(buf, "%08lx\n", phys_index);
+	return sysfs_emit(buf, "%08lx\n", memory_block_id(mem->start_section_nr));
 }
 
 /*
Signature · 1 line
2.23.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] drivers/base/memory: Fix comments for phys_index_show()

Quoted 4 lines · Gavin Shan

According to 'admin-guide/mm/memory-hotplug.rst', the memory block ID, instead of the section index, is shown by '/sys/devices/system/memory/ memoryX/phys_index'.

Quoted 8 lines

Fix the comments to match with 'admin-guide/mm/memory-hotplug.rst'. Besides, use the existing helper memory_block_id() to convert the section index to the memory block index.

No functional change intended.

---

Signature · 3 lines
Thanks,

David / dhildenb
author
#3 lore raw

PATCH 2/2 drivers/base/memory: Use array to show memory block state

In reply to Gavin Shan

Use an array to show memory block state from '/sys/devices/system/ memory/memoryX/state', to simplify the code.

No functional change intended.

---
 drivers/base/memory.c | 25 ++++++-------------------
 1 file changed, 6 insertions(+), 19 deletions(-)
drivers/base/memory.c +6 -19
index b456ac213610..9474f25c452c 100644
@@ -141,28 +141,15 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,
 			  char *buf)
 {
 	struct memory_block *mem = to_memory_block(dev);
-	const char *output;
+	static const char *const mem_state_str[] = {
+		NULL, "online", "going-offline", NULL, "offline",
+	};
 
-	/*
-	 * We can probably put these states in a nice little array
-	 * so that they're not open-coded
-	 */
-	switch (mem->state) {
-	case MEM_ONLINE:
-		output = "online";
-		break;
-	case MEM_OFFLINE:
-		output = "offline";
-		break;
-	case MEM_GOING_OFFLINE:
-		output = "going-offline";
-		break;
-	default:
-		WARN_ON(1);
+	if (WARN_ON(mem->state >= ARRAY_SIZE(mem_state_str) ||
+		    !mem_state_str[mem->state]))
 		return sysfs_emit(buf, "ERROR-UNKNOWN-%ld\n", mem->state);
-	}
 
-	return sysfs_emit(buf, "%s\n", output);
+	return sysfs_emit(buf, "%s\n", mem_state_str[mem->state]);
 }
 
 int memory_notify(unsigned long val, void *v)
Signature · 1 line
2.23.0
maintainer
#4 lore raw

Re: [PATCH 2/2] drivers/base/memory: Use array to show memory block state

Quoted 31 lines · Gavin Shan

Use an array to show memory block state from '/sys/devices/system/ memory/memoryX/state', to simplify the code.

No functional change intended.

---
 drivers/base/memory.c | 25 ++++++-------------------
 1 file changed, 6 insertions(+), 19 deletions(-)
drivers/base/memory.c +3 -9
index b456ac213610..9474f25c452c 100644
@@ -141,28 +141,15 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,
 			  char *buf)
 {
 	struct memory_block *mem = to_memory_block(dev);
-	const char *output;
+	static const char *const mem_state_str[] = {
+		NULL, "online", "going-offline", NULL, "offline",
+	};
 
-	/*
-	 * We can probably put these states in a nice little array
-	 * so that they're not open-coded
-	 */
-	switch (mem->state) {
-	case MEM_ONLINE:
-		output = "online";
-		break;

drivers/base/memory.c static ssize_t state_show(struct device *dev, struct device_attribute *attr, · line 149

Quoted 10 lines
drivers/base/memory.c +2 -8
-	case MEM_OFFLINE:
-		output = "offline";
-		break;
-	case MEM_GOING_OFFLINE:
-		output = "going-offline";
-		break;
-	default:
-		WARN_ON(1);
+	if (WARN_ON(mem->state >= ARRAY_SIZE(mem_state_str) ||
+		    !mem_state_str[mem->state]))

Ick, the whole WARN_ON() should just be removed please. We don't want to reboot any systems if this changed incorrectly.

Please fix this up to properly handle this and keep going on, don't mess with WARN_ON() anymore in code that can recover properly.

thanks,

greg k-h

author
#5 lore raw

Re: [PATCH 2/2] drivers/base/memory: Use array to show memory block state

Quoted 46 lines · Greg KH
Quoted 31 lines · Gavin Shan

Use an array to show memory block state from '/sys/devices/system/ memory/memoryX/state', to simplify the code.

No functional change intended.

---
  drivers/base/memory.c | 25 ++++++-------------------
  1 file changed, 6 insertions(+), 19 deletions(-)
drivers/base/memory.c +3 -9
index b456ac213610..9474f25c452c 100644
@@ -141,28 +141,15 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  			  char *buf)
  {
  	struct memory_block *mem = to_memory_block(dev);
-	const char *output;
+	static const char *const mem_state_str[] = {
+		NULL, "online", "going-offline", NULL, "offline",
+	};
 
-	/*
-	 * We can probably put these states in a nice little array
-	 * so that they're not open-coded
-	 */
-	switch (mem->state) {
-	case MEM_ONLINE:
-		output = "online";
-		break;

drivers/base/memory.c static ssize_t state_show(struct device *dev, struct device_attribute *attr, · line 149

Quoted 10 lines
drivers/base/memory.c +2 -8
-	case MEM_OFFLINE:
-		output = "offline";
-		break;
-	case MEM_GOING_OFFLINE:
-		output = "going-offline";
-		break;
-	default:
-		WARN_ON(1);
+	if (WARN_ON(mem->state >= ARRAY_SIZE(mem_state_str) ||
+		    !mem_state_str[mem->state]))

Ick, the whole WARN_ON() should just be removed please. We don't want to reboot any systems if this changed incorrectly.

Quoted 3 lines

Please fix this up to properly handle this and keep going on, don't mess with WARN_ON() anymore in code that can recover properly.

Thanks for your review, Greg. Indeed, the WARN_ON() here is no sense because the warning can be caught from the return value. "ERROR-UNKNOWN-%ld\n" is returned for unknown or invalid state.

I will drop WARN_ON() in v2. PATCH[1/2] won't be reposted since it has been merged to driver.core git tree.

Thanks,
Gavin