The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Bradley Morgan <include@grrlz.net>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, kees@kernel.org, include@grrlz.net
Subject: [PATCH] reboot: use a lookup table for hw_protection_action strings
Date: Tue,  7 Jul 2026 18:09:44 +0000	[thread overview]
Message-ID: <20260707180945.7781-1-include@grrlz.net> (raw)

Replace the switch and if else chains with one table.

Signed-off-by: Bradley Morgan <include@grrlz.net>
---
 kernel/reboot.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/kernel/reboot.c b/kernel/reboot.c
index 41a2519755aa..bed6967bfa96 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -938,16 +938,18 @@ void orderly_reboot(void)
 }
 EXPORT_SYMBOL_GPL(orderly_reboot);
 
+/* DEFAULT is NULL: a sentinel, not a real action. */
+static const char *const hw_protection_action_strs[] = {
+	[HWPROT_ACT_SHUTDOWN] = "shutdown",
+	[HWPROT_ACT_REBOOT]   = "reboot",
+};
+
 static const char *hw_protection_action_str(enum hw_protection_action action)
 {
-	switch (action) {
-	case HWPROT_ACT_SHUTDOWN:
-		return "shutdown";
-	case HWPROT_ACT_REBOOT:
-		return "reboot";
-	default:
+	if (action >= ARRAY_SIZE(hw_protection_action_strs) ||
+	    !hw_protection_action_strs[action])
 		return "undefined";
-	}
+	return hw_protection_action_strs[action];
 }
 
 static enum hw_protection_action hw_failure_emergency_action;
@@ -1055,14 +1057,17 @@ EXPORT_SYMBOL_GPL(__hw_protection_trigger);
 static bool hw_protection_action_parse(const char *str,
 				       enum hw_protection_action *action)
 {
-	if (sysfs_streq(str, "shutdown"))
-		*action = HWPROT_ACT_SHUTDOWN;
-	else if (sysfs_streq(str, "reboot"))
-		*action = HWPROT_ACT_REBOOT;
-	else
-		return false;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(hw_protection_action_strs); i++) {
+		if (hw_protection_action_strs[i] &&
+		    sysfs_streq(str, hw_protection_action_strs[i])) {
+			*action = i;
+			return true;
+		}
+	}
 
-	return true;
+	return false;
 }
 
 static int __init hw_protection_setup(char *str)
-- 
2.53.0


                 reply	other threads:[~2026-07-07 18:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260707180945.7781-1-include@grrlz.net \
    --to=include@grrlz.net \
    --cc=akpm@linux-foundation.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox