From efee03a50c2844d78f6fb5e98be1ffd17605dc5b Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Sat, 13 Apr 2024 01:02:08 +0900 Subject: bootconfig: do not put quotes on cmdline items unless necessary When trying to migrate to using bootconfig to embed the kernel's and PID1's command line with the kernel image itself, and so allowing changing that without modifying the bootloader, I noticed that /proc/cmdline changed from e.g. console=ttymxc0,115200n8 cma=128M quiet -- --log-level=notice to console="ttymxc0,115200n8" cma="128M" quiet -- --log-level="notice" The kernel parameters are parsed just fine, and the quotes are indeed stripped from the actual argv[] given to PID1. However, the quoting doesn't really serve any purpose and looks excessive, and might confuse some (naive) userspace tool trying to parse /proc/cmdline. So do not quote the value unless it contains whitespace. Link: https://lore.kernel.org/all/20240308124401.1702046-1-linux@rasmusvillemoes.dk/ Signed-off-by: Rasmus Villemoes Acked-by: Masami Hiramatsu (Google) Signed-off-by: Masami Hiramatsu (Google) --- init/main.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/init/main.c b/init/main.c index 5dcf5274c09c..96e59e5761ff 100644 --- a/init/main.c +++ b/init/main.c @@ -327,7 +327,7 @@ static int __init xbc_snprint_cmdline(char *buf, size_t size, { struct xbc_node *knode, *vnode; char *end = buf + size; - const char *val; + const char *val, *q; int ret; xbc_node_for_each_key_value(root, knode, val) { @@ -345,8 +345,9 @@ static int __init xbc_snprint_cmdline(char *buf, size_t size, continue; } xbc_array_for_each_value(vnode, val) { - ret = snprintf(buf, rest(buf, end), "%s=\"%s\" ", - xbc_namebuf, val); + q = strpbrk(val, " \t\r\n") ? "\"" : ""; + ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ", + xbc_namebuf, q, val, q); if (ret < 0) return ret; buf += ret; -- cgit v1.2.3 From cd24bdb06820d12dc84aeb0a47a10ad7ecf4b027 Mon Sep 17 00:00:00 2001 From: Yuntao Wang Date: Sat, 13 Apr 2024 01:02:08 +0900 Subject: init/main.c: Remove redundant space from saved_command_line There is a space at the end of extra_init_args. In the current logic, copying extra_init_args to saved_command_line will cause extra spaces in saved_command_line here or there. Remove the trailing space from extra_init_args to make the string in saved_command_line look more perfect. Link: https://lore.kernel.org/all/20240412032950.12687-1-ytcoode@gmail.com/ Signed-off-by: Yuntao Wang Acked-by: Masami Hiramatsu (Google) Signed-off-by: Masami Hiramatsu (Google) --- init/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/init/main.c b/init/main.c index 96e59e5761ff..02156c3c5879 100644 --- a/init/main.c +++ b/init/main.c @@ -628,8 +628,10 @@ static void __init setup_command_line(char *command_line) if (extra_command_line) xlen = strlen(extra_command_line); - if (extra_init_args) + if (extra_init_args) { + extra_init_args = strim(extra_init_args); /* remove trailing space */ ilen = strlen(extra_init_args) + 4; /* for " -- " */ + } len = xlen + strlen(boot_command_line) + 1; -- cgit v1.2.3 From ddd53363f875eb23b6754362ce0a43f2214f0a83 Mon Sep 17 00:00:00 2001 From: Yuntao Wang Date: Fri, 12 Apr 2024 16:17:33 +0800 Subject: init/main.c: Minor cleanup for the setup_command_line() function This is just a minor cleanup to make the code look a bit cleaner. Link: https://lore.kernel.org/all/20240412081733.35925-3-ytcoode@gmail.com/ Signed-off-by: Yuntao Wang Signed-off-by: Masami Hiramatsu (Google) --- init/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/init/main.c b/init/main.c index 02156c3c5879..383796015113 100644 --- a/init/main.c +++ b/init/main.c @@ -633,11 +633,11 @@ static void __init setup_command_line(char *command_line) ilen = strlen(extra_init_args) + 4; /* for " -- " */ } - len = xlen + strlen(boot_command_line) + 1; + len = xlen + strlen(boot_command_line) + ilen + 1; - saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES); + saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES); if (!saved_command_line) - panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen); + panic("%s: Failed to allocate %zu bytes\n", __func__, len); len = xlen + strlen(command_line) + 1; -- cgit v1.2.3