2023-01-20 06:23:29

by Gavin Shan

[permalink] [raw]
Subject: [PATCH 0/2] 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(-)

--
2.23.0


2023-01-20 06:41:46

by Gavin Shan

[permalink] [raw]
Subject: [PATCH 2/2] drivers/base/memory: Use array to show memory block state

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

No functional change intended.

Signed-off-by: Gavin Shan <[email protected]>
---
drivers/base/memory.c | 25 ++++++-------------------
1 file changed, 6 insertions(+), 19 deletions(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index b456ac213610..9474f25c452c 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -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)
--
2.23.0

2023-01-20 13:33:39

by Greg Kroah-Hartman

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

On Fri, Jan 20, 2023 at 01:57:27PM +0800, Gavin Shan wrote:
> Use an array to show memory block state from '/sys/devices/system/
> memory/memoryX/state', to simplify the code.
>
> No functional change intended.
>
> Signed-off-by: Gavin Shan <[email protected]>
> ---
> drivers/base/memory.c | 25 ++++++-------------------
> 1 file changed, 6 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index b456ac213610..9474f25c452c 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -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]))

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

2023-01-20 23:17:26

by Gavin Shan

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

On 1/21/23 12:14 AM, Greg KH wrote:
> On Fri, Jan 20, 2023 at 01:57:27PM +0800, Gavin Shan wrote:
>> Use an array to show memory block state from '/sys/devices/system/
>> memory/memoryX/state', to simplify the code.
>>
>> No functional change intended.
>>
>> Signed-off-by: Gavin Shan <[email protected]>
>> ---
>> drivers/base/memory.c | 25 ++++++-------------------
>> 1 file changed, 6 insertions(+), 19 deletions(-)
>>
>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
>> index b456ac213610..9474f25c452c 100644
>> --- a/drivers/base/memory.c
>> +++ b/drivers/base/memory.c
>> @@ -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]))
>
> 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 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