Skip to contentSkip to conversation

x86/boot: Fix domain_cmdline_size()

PATCH In review Andrew Cooper 2 participants 2 messages Jun 3, 2025 – Jun 4, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/boot: Fix domain_cmdline_size()

The early exit from domain_cmdline_size() is buggy. Even if there's no bootloader cmdline and no kextra, there still might be Xen parameters to forward, and therefore a nonzero cmdline.

Explain what the function is doing, and rewrite it to be both more legible and more extendible.

Fixes 142f0a43a15a ("x86/boot: add cmdline to struct boot_domain")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

And just to demonstrate how bad ternary operators are when they're not used over trivial scalars, this "no effective change in logic" still saves most of a cacheline of logic:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-56 (-56)
Function old new delta
__start_xen 8946 8890 -56

The "if ( cmdline_size )" in create_dom0() could be dropped. It's impossible to be 0 now, which is correct behaviour. But, that should be deferred to a later change.

---
 xen/arch/x86/setup.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
xen/arch/x86/setup.c +11 -4
index 1f5cb67bd0ee..d47b99f1b2c9 100644
@@ -980,15 +980,22 @@ static unsigned int __init copy_bios_e820(struct e820entry *map, unsigned int li
     return n;
 }
 
+/*
+ * Calculate the maximum possible size of the dom0 cmdline.  Pieces of the
+ * dom0 cmdline optionally come from the bootloader directly, from Xen's
+ * cmdline (following " -- "), and individual Xen parameters are forwarded
+ * too.
+ */
 static size_t __init domain_cmdline_size(const struct boot_info *bi,
                                          const struct boot_domain *bd)
 {
-    size_t s = bi->kextra ? strlen(bi->kextra) : 0;
+    size_t s = 0;
 
-    s += bd->kernel->cmdline_pa ? strlen(__va(bd->kernel->cmdline_pa)) : 0;
+    if ( bd->kernel->cmdline_pa )
+        s += strlen(__va(bd->kernel->cmdline_pa));
 
-    if ( s == 0 )
-        return s;
+    if ( bi->kextra )
+        s += strlen(bi->kextra);
 
     /*
      * Certain parameters from the Xen command line may be added to the dom0

base-commit: 2c4a3d688943b2034756859844b8337a5a97ce07
prerequisite-patch-id: 32a8746877e6b92075be2f022dca25c6bfa6f31e

Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Andrew Cooper

The early exit from domain_cmdline_size() is buggy. Even if there's no bootloader cmdline and no kextra, there still might be Xen parameters to forward, and therefore a nonzero cmdline.

Explain what the function is doing, and rewrite it to be both more legible and more extendible.

Fixes 142f0a43a15a ("x86/boot: add cmdline to struct boot_domain")

I don't think this is correct. Iirc I even commented on this apparent anomaly, being (validly) told that it preserved original behavior, where all the code was inside "if ( bd->kernel->cmdline_pa || bi->kextra )". Since I had asked to fix this already back then, ...

Quoted 1 line

with the Fixes: tag corrected (or dropped, for simplicity).

Jan