From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98DD63AEB5F for ; Tue, 28 Jul 2026 22:17:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785277074; cv=none; b=o6T0vslpVlQ0NzTpmbvcBzWuJOtH3L5SyhqcPncM1UHADpAG7Q06PDKAkEMKyWwQzlWu6O8Fu1i8HSM86As6Z+5sKFKv4LZ1fr/UkkbAXeMtAdZUy23rhJQjo21guDBeaW/XH2UgrQzpzCHZbeYELO2w02ZpP9dtweCIuE61M1s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785277074; c=relaxed/simple; bh=fAqojg44twXA7aWs+vrxwlfFja4qS1hqPy1TKYqUtXg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ds4D4TIgMyb2yqkSktadz2s9gwaePpLl1XpaRYsEqjLG0j5xc2OaXgp2tR/WHYT1S9Sd4UBhw52BgaJMXLWUYuPrQEa/Yp5lAcwrmM2ZpQ0BPVAeQZa8jI9lsCfvTKjG96Hm/hCj3Gl1461Q6cluAyovNB/UsZpRlqivxN8k07M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TWxVnmIT; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TWxVnmIT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E153E1F00A3D; Tue, 28 Jul 2026 22:17:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1785277073; bh=mCIPkb6U6o6FC/l4zcKHM4bLe7Mh3ZGVIr4p5omNBKg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TWxVnmITgxezuvH44uF9l6GYAuoIxKs3tgnic4O6svDK+Ggb3M8PdyLdDoY9sjeNX 1YguRL0o9CfDmtLypyLhZXv+Cr+bmYEo1yUgdzn2Savdw8Hq+IQC6Yr0rg6w+tL0NX HKx2GQq/hjGpGAByDkD8STA3SMIwgnO0ozyDZn/dObRyGMBAJ/BiRXwYxqUoXzw21E 4dybPdP2wKwxxxERBbNqpC2faf8cy44hLjg2WPNddUpmUCriPyexMHwUp4cCTsk2Se fAtSgp2GeOrPbI1E1r3go7MzV6skxMbb8ctiOQvxbq9e11VQOOdJZKuMFj6h4t3IoI 6XDakh+AnhqDQ== From: Sasha Levin To: stable@vger.kernel.org Cc: Breno Leitao , "Masami Hiramatsu (Google)" , Sasha Levin Subject: [PATCH 5.15.y 2/3] bootconfig: move xbc_snprint_cmdline() to lib/bootconfig.c Date: Tue, 28 Jul 2026 18:17:49 -0400 Message-ID: <20260728221750.745951-2-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260728221750.745951-1-sashal@kernel.org> References: <2026072113-skintight-barometer-812e@gregkh> <20260728221750.745951-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Breno Leitao [ Upstream commit 5a643e4623238e14b03d75ca0d4eda0645720cee ] Move xbc_snprint_cmdline() from init/main.c to lib/bootconfig.c so the function (and its xbc_namebuf scratch buffer) becomes part of the shared parser library. tools/bootconfig already compiles lib/bootconfig.c directly, which lets a follow-up patch reuse the same renderer in the userspace tool to convert a bootconfig file into a flat cmdline string at build time. No functional change. Link: https://lore.kernel.org/all/20260508-bootconfig_using_tools-v1-1-1132219aa773@debian.org/ Signed-off-by: Breno Leitao Signed-off-by: Masami Hiramatsu (Google) Stable-dep-of: dec4d8118c17 ("bootconfig: fix NULL-pointer arithmetic in xbc_snprint_cmdline()") Signed-off-by: Sasha Levin --- include/linux/bootconfig.h | 3 ++ init/main.c | 45 ------------------------------ lib/bootconfig.c | 56 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/include/linux/bootconfig.h b/include/linux/bootconfig.h index 5296fbb8408cae..2105a94294ca6a 100644 --- a/include/linux/bootconfig.h +++ b/include/linux/bootconfig.h @@ -254,6 +254,9 @@ static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *node int __init xbc_node_compose_key_after(struct xbc_node *root, struct xbc_node *node, char *buf, size_t size); +/* Render key/value pairs under @root as a flat cmdline string */ +int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root); + /** * xbc_node_compose_key() - Compose full key string of the XBC node * @node: An XBC node. diff --git a/init/main.c b/init/main.c index f9e98fb3a3e098..c86df2005204aa 100644 --- a/init/main.c +++ b/init/main.c @@ -319,51 +319,6 @@ static void * __init get_boot_config_from_initrd(u32 *_size, u32 *_csum) #ifdef CONFIG_BOOT_CONFIG -static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata; - -#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0) - -static int __init xbc_snprint_cmdline(char *buf, size_t size, - struct xbc_node *root) -{ - struct xbc_node *knode, *vnode; - char *end = buf + size; - const char *val, *q; - int ret; - - xbc_node_for_each_key_value(root, knode, val) { - ret = xbc_node_compose_key_after(root, knode, - xbc_namebuf, XBC_KEYLEN_MAX); - if (ret < 0) - return ret; - - vnode = xbc_node_get_child(knode); - if (!vnode) { - ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf); - if (ret < 0) - return ret; - buf += ret; - continue; - } - xbc_array_for_each_value(vnode, val) { - /* - * For prettier and more readable /proc/cmdline, only - * quote the value when necessary, i.e. when it contains - * whitespace. - */ - 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; - } - } - - return buf - (end - size); -} -#undef rest - /* Make an extra command line under given key word */ static char * __init xbc_make_cmdline(const char *key) { diff --git a/lib/bootconfig.c b/lib/bootconfig.c index 9873c6372adca6..f497087e6fb1c9 100644 --- a/lib/bootconfig.c +++ b/lib/bootconfig.c @@ -339,6 +339,62 @@ const char * __init xbc_node_find_next_key_value(struct xbc_node *root, return ""; /* No value key */ } +static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata; + +#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0) + +/** + * xbc_snprint_cmdline() - Render bootconfig keys under @root as a cmdline string + * @buf: Destination buffer (may be NULL when @size is 0 to query the length) + * @size: Size of @buf in bytes + * @root: Subtree root whose key=value pairs should be rendered + * + * Walk all key/value pairs under @root and emit them as a space-separated + * cmdline string into @buf. Values containing whitespace are quoted with + * double quotes. Returns the number of bytes that would be written if @buf + * were large enough (matching snprintf semantics), or a negative errno on + * failure. + */ +int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root) +{ + struct xbc_node *knode, *vnode; + char *end = buf + size; + const char *val, *q; + int ret; + + xbc_node_for_each_key_value(root, knode, val) { + ret = xbc_node_compose_key_after(root, knode, + xbc_namebuf, XBC_KEYLEN_MAX); + if (ret < 0) + return ret; + + vnode = xbc_node_get_child(knode); + if (!vnode) { + ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf); + if (ret < 0) + return ret; + buf += ret; + continue; + } + xbc_array_for_each_value(vnode, val) { + /* + * For prettier and more readable /proc/cmdline, only + * quote the value when necessary, i.e. when it contains + * whitespace. + */ + 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; + } + } + + return buf - (end - size); +} +#undef rest + /* XBC parse and tree build */ static int __init xbc_init_node(struct xbc_node *node, char *data, u32 flag) -- 2.53.0