The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] reboot: use lookup tables for sysfs mode and type strings
@ 2026-07-06 12:28 Bradley Morgan
  0 siblings, 0 replies; only message in thread
From: Bradley Morgan @ 2026-07-06 12:28 UTC (permalink / raw)
  To: akpm; +Cc: kees, linux-kernel, Bradley Morgan

Replace switch and if else chains in the mode and type sysfs
handlers with lookup tables, removing duplicated string macros.

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

diff --git a/kernel/reboot.c b/kernel/reboot.c
index 695c33e75efd..41a2519755aa 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -1177,68 +1177,43 @@ __setup("reboot=", reboot_setup);
 
 #ifdef CONFIG_SYSFS
 
-#define REBOOT_COLD_STR		"cold"
-#define REBOOT_WARM_STR		"warm"
-#define REBOOT_HARD_STR		"hard"
-#define REBOOT_SOFT_STR		"soft"
-#define REBOOT_GPIO_STR		"gpio"
 #define REBOOT_UNDEFINED_STR	"undefined"
 
-#define BOOT_TRIPLE_STR		"triple"
-#define BOOT_KBD_STR		"kbd"
-#define BOOT_BIOS_STR		"bios"
-#define BOOT_ACPI_STR		"acpi"
-#define BOOT_EFI_STR		"efi"
-#define BOOT_PCI_STR		"pci"
+static const char *const reboot_mode_strs[] = {
+	[REBOOT_COLD]	= "cold",
+	[REBOOT_WARM]	= "warm",
+	[REBOOT_HARD]	= "hard",
+	[REBOOT_SOFT]	= "soft",
+	[REBOOT_GPIO]	= "gpio",
+};
 
 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
 {
-	const char *val;
+	const char *val = REBOOT_UNDEFINED_STR;
 
-	switch (reboot_mode) {
-	case REBOOT_COLD:
-		val = REBOOT_COLD_STR;
-		break;
-	case REBOOT_WARM:
-		val = REBOOT_WARM_STR;
-		break;
-	case REBOOT_HARD:
-		val = REBOOT_HARD_STR;
-		break;
-	case REBOOT_SOFT:
-		val = REBOOT_SOFT_STR;
-		break;
-	case REBOOT_GPIO:
-		val = REBOOT_GPIO_STR;
-		break;
-	default:
-		val = REBOOT_UNDEFINED_STR;
-	}
+	if ((unsigned int)reboot_mode < ARRAY_SIZE(reboot_mode_strs))
+		val = reboot_mode_strs[reboot_mode];
 
 	return sysfs_emit(buf, "%s\n", val);
 }
 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
 			  const char *buf, size_t count)
 {
+	unsigned int i;
+
 	if (!capable(CAP_SYS_BOOT))
 		return -EPERM;
 
-	if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
-		reboot_mode = REBOOT_COLD;
-	else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
-		reboot_mode = REBOOT_WARM;
-	else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
-		reboot_mode = REBOOT_HARD;
-	else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
-		reboot_mode = REBOOT_SOFT;
-	else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
-		reboot_mode = REBOOT_GPIO;
-	else
-		return -EINVAL;
-
-	reboot_default = 0;
+	for (i = 0; i < ARRAY_SIZE(reboot_mode_strs); i++) {
+		if (!strncmp(buf, reboot_mode_strs[i],
+			     strlen(reboot_mode_strs[i]))) {
+			reboot_mode = i;
+			reboot_default = 0;
+			return count;
+		}
+	}
 
-	return count;
+	return -EINVAL;
 }
 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
 
@@ -1265,31 +1240,28 @@ static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
 }
 static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
 
+static const struct {
+	enum reboot_type type;
+	const char *str;
+} reboot_type_strs[] = {
+	{ BOOT_TRIPLE,		"triple" },
+	{ BOOT_KBD,		"kbd" },
+	{ BOOT_BIOS,		"bios" },
+	{ BOOT_ACPI,		"acpi" },
+	{ BOOT_EFI,		"efi" },
+	{ BOOT_CF9_FORCE,	"pci" },
+};
+
 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
 {
-	const char *val;
+	const char *val = REBOOT_UNDEFINED_STR;
+	unsigned int i;
 
-	switch (reboot_type) {
-	case BOOT_TRIPLE:
-		val = BOOT_TRIPLE_STR;
-		break;
-	case BOOT_KBD:
-		val = BOOT_KBD_STR;
-		break;
-	case BOOT_BIOS:
-		val = BOOT_BIOS_STR;
-		break;
-	case BOOT_ACPI:
-		val = BOOT_ACPI_STR;
-		break;
-	case BOOT_EFI:
-		val = BOOT_EFI_STR;
-		break;
-	case BOOT_CF9_FORCE:
-		val = BOOT_PCI_STR;
-		break;
-	default:
-		val = REBOOT_UNDEFINED_STR;
+	for (i = 0; i < ARRAY_SIZE(reboot_type_strs); i++) {
+		if (reboot_type == reboot_type_strs[i].type) {
+			val = reboot_type_strs[i].str;
+			break;
+		}
 	}
 
 	return sysfs_emit(buf, "%s\n", val);
@@ -1297,27 +1269,21 @@ static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char
 static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
 			  const char *buf, size_t count)
 {
+	unsigned int i;
+
 	if (!capable(CAP_SYS_BOOT))
 		return -EPERM;
 
-	if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
-		reboot_type = BOOT_TRIPLE;
-	else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
-		reboot_type = BOOT_KBD;
-	else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
-		reboot_type = BOOT_BIOS;
-	else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
-		reboot_type = BOOT_ACPI;
-	else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
-		reboot_type = BOOT_EFI;
-	else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
-		reboot_type = BOOT_CF9_FORCE;
-	else
-		return -EINVAL;
-
-	reboot_default = 0;
+	for (i = 0; i < ARRAY_SIZE(reboot_type_strs); i++) {
+		if (!strncmp(buf, reboot_type_strs[i].str,
+			     strlen(reboot_type_strs[i].str))) {
+			reboot_type = reboot_type_strs[i].type;
+			reboot_default = 0;
+			return count;
+		}
+	}
 
-	return count;
+	return -EINVAL;
 }
 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
 #endif
-- 
2.53.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-07-06 12:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-06 12:28 [PATCH] reboot: use lookup tables for sysfs mode and type strings Bradley Morgan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox