From: Gavin Shan <gshan@redhat.com>
To: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, david@redhat.com,
osalvador@suse.de, gregkh@linuxfoundation.org, rafael@kernel.org,
shan.gavin@gmail.com
Subject: [PATCH v2] drivers/base/memory: Use array to show memory block state
Date: Sat, 21 Jan 2023 07:38:14 +0800 [thread overview]
Message-ID: <20230120233814.368803-1-gshan@redhat.com> (raw)
Use an array to show memory block state from '/sys/devices/system/
memory/memoryX/state', to simplify the code. Besides, WARN_ON()
is removed since the warning can be caught by the return value,
which is "ERROR-UNKNOWN-%ld\n". A system reboot caused by WARN_ON()
is definitely unexpected as Greg mentioned.
No functional change intended.
Signed-off-by: Gavin Shan <gshan@redhat.com>
---
v2: Drop WARN_ON() (Greg)
---
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..0fdacdc79806 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 (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
next reply other threads:[~2023-01-20 23:39 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-20 23:38 Gavin Shan [this message]
2023-01-23 10:33 ` [PATCH v2] drivers/base/memory: Use array to show memory block state David Hildenbrand
2023-01-31 14:54 ` Greg KH
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230120233814.368803-1-gshan@redhat.com \
--to=gshan@redhat.com \
--cc=david@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=osalvador@suse.de \
--cc=rafael@kernel.org \
--cc=shan.gavin@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.