Skip to contentSkip to conversation

x86/PVH: account for module command line length

PATCH Superseded Jan Beulich 3 participants 5 messages Mar 19, 2025

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/PVH: account for module command line length

As per observation in practice, initrd->cmdline_pa is not normally zero. Hence so far we always appended at least one byte. That alone may already render insufficient the "allocation" made by find_memory(). Things would be worse when there's actually a (perhaps long) command line.

Skip setup when the command line is empty. Amend the "allocation" size by padding and actual size of module command line.

Fixes 0ecb8eb09f9f ("x86/pvh: pass module command line to dom0")
xen/arch/x86/hvm/dom0_build.c +19 -8
@@ -712,7 +712,15 @@ static int __init pvh_load_kernel(
      * simplify it.
      */
     last_addr = find_memory(d, &elf, sizeof(start_info) +
-                            (initrd ? ROUNDUP(initrd_len, PAGE_SIZE) +
+                            (initrd ? ROUNDUP(ROUNDUP(initrd_len,
+                                                      elf_64bit(&elf) ? 8 : 4) +
+                                              (initrd->cmdline_pa &&
+                                               strlen(__va(initrd->
+                                                           cmdline_pa))
+                                               ? strlen(__va(initrd->
+                                                             cmdline_pa)) + 1
+                                               : 0),
+                                              PAGE_SIZE) +
                                       sizeof(mod)
                                     : 0) +
                             (cmdline ? ROUNDUP(strlen(cmdline) + 1,
@@ -740,16 +748,19 @@ static int __init pvh_load_kernel(
         if ( initrd->cmdline_pa )
         {
             char *str = __va(initrd->cmdline_pa);
-            size_t len = strlen(str) + 1;
+            size_t len = strlen(str);
 
-            rc = hvm_copy_to_guest_phys(last_addr, str, len, v);
-            if ( rc )
+            if ( len++ )
             {
-                printk("Unable to copy module command line\n");
-                return rc;
+                rc = hvm_copy_to_guest_phys(last_addr, str, len, v);
+                if ( rc )
+                {
+                    printk("Unable to copy module command line\n");
+                    return rc;
+                }
+                mod.cmdline_paddr = last_addr;
+                last_addr += len;
             }
-            mod.cmdline_paddr = last_addr;
-            last_addr += len;
         }
         last_addr = ROUNDUP(last_addr, PAGE_SIZE);
     }
#1 lore raw
Quoted 21 lines · Jan Beulich

As per observation in practice, initrd->cmdline_pa is not normally zero. Hence so far we always appended at least one byte. That alone may already render insufficient the "allocation" made by find_memory(). Things would be worse when there's actually a (perhaps long) command line.

Skip setup when the command line is empty. Amend the "allocation" size by padding and actual size of module command line.

Fixes 0ecb8eb09f9f ("x86/pvh: pass module command line to dom0")
xen/arch/x86/hvm/dom0_build.c +2 -1
@@ -712,7 +712,15 @@ static int __init pvh_load_kernel(
      * simplify it.
      */
     last_addr = find_memory(d, &elf, sizeof(start_info) +
-                            (initrd ? ROUNDUP(initrd_len, PAGE_SIZE) +
+                            (initrd ? ROUNDUP(ROUNDUP(initrd_len,
+                                                      elf_64bit(&elf) ? 8 : 4) +

xen/arch/x86/hvm/dom0_build.c static int __init pvh_load_kernel( · line 726

Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +7 -0
+                                              (initrd->cmdline_pa &&
+                                               strlen(__va(initrd->
+                                                           cmdline_pa))
+                                               ? strlen(__va(initrd->
+                                                             cmdline_pa)) + 1
+                                               : 0),
+                                              PAGE_SIZE) +
                                       sizeof(mod)
                                     : 0) +
                             (cmdline ? ROUNDUP(strlen(cmdline) + 1,

This piece of logic was already bad, but this is rather worse.

One patch I made while doing the boot module work is:

xen/arch/x86/hvm/dom0_build.c +0 -0
index 6a4453103a9a..7292ddd07276 100644
@@ -654,6 +654,7 @@ static int __init pvh_load_kernel(

     const char *cmdline = image->cmdline_pa ? __va(image->cmdline_pa) :
NULL;
     struct elf_binary elf;
     struct elf_dom_parms parms;
+    size_t metadata_len;
     paddr_t last_addr;
     struct hvm_start_info start_info = { 0 };
     struct hvm_modlist_entry mod = { 0 };
@@ -711,13 +712,16 @@ static int __init pvh_load_kernel(
      * split into smaller allocations, done as a single region in order to
      * simplify it.
      */
-    last_addr = find_memory(d, &elf, sizeof(start_info) +
-                            (initrd ? ROUNDUP(initrd_len, PAGE_SIZE) + -                                      sizeof(mod)
-                                    : 0) +
-                            (cmdline ? ROUNDUP(strlen(cmdline) + 1, -                                               elf_64bit(&elf) ? 8 : 4) -                                     : 0));
+    metadata_len = sizeof(start_info);
+
+    if ( initrd )
+        metadata_len += sizeof(mod) + ROUNDUP(initrd_len, PAGE_SIZE); +
+    if ( cmdline )
+        metadata_len += ROUNDUP(strlen(cmdline) + 1,
+                                elf_64bit(&elf) ? 8 : 4);
+
+    last_addr = find_memory(d, &elf, metadata_len);
     if ( last_addr == INVALID_PADDR )
     {
         printk("Unable to find a memory region to load initrd and metadata\n");

which I think I ought to submit as a prerequisite to this, after which your logic squashed on the RHS now becomes an expansion of the if ( initrd ).

Thoughts?

~Andrew

#2 lore raw
Quoted 77 lines · Andrew Cooper
Quoted 21 lines · Jan Beulich

As per observation in practice, initrd->cmdline_pa is not normally zero. Hence so far we always appended at least one byte. That alone may already render insufficient the "allocation" made by find_memory(). Things would be worse when there's actually a (perhaps long) command line.

Skip setup when the command line is empty. Amend the "allocation" size by padding and actual size of module command line.

Fixes 0ecb8eb09f9f ("x86/pvh: pass module command line to dom0")
xen/arch/x86/hvm/dom0_build.c +2 -1
@@ -712,7 +712,15 @@ static int __init pvh_load_kernel(
      * simplify it.
      */
     last_addr = find_memory(d, &elf, sizeof(start_info) +
-                            (initrd ? ROUNDUP(initrd_len, PAGE_SIZE) +
+                            (initrd ? ROUNDUP(ROUNDUP(initrd_len,
+                                                      elf_64bit(&elf) ? 8 : 4) +

xen/arch/x86/hvm/dom0_build.c static int __init pvh_load_kernel( · line 726

Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +7 -0
+                                              (initrd->cmdline_pa &&
+                                               strlen(__va(initrd->
+                                                           cmdline_pa))
+                                               ? strlen(__va(initrd->
+                                                             cmdline_pa)) + 1
+                                               : 0),
+                                              PAGE_SIZE) +
                                       sizeof(mod)
                                     : 0) +
                             (cmdline ? ROUNDUP(strlen(cmdline) + 1,

This piece of logic was already bad, but this is rather worse.

One patch I made while doing the boot module work is:

xen/arch/x86/hvm/dom0_build.c +0 -0
index 6a4453103a9a..7292ddd07276 100644
@@ -654,6 +654,7 @@ static int __init pvh_load_kernel(

     const char *cmdline = image->cmdline_pa ? __va(image->cmdline_pa) :
NULL;
     struct elf_binary elf;
     struct elf_dom_parms parms;
+    size_t metadata_len;
     paddr_t last_addr;
     struct hvm_start_info start_info = { 0 };
     struct hvm_modlist_entry mod = { 0 };

xen/arch/x86/hvm/dom0_build.c +10 -7
@@ -711,13 +712,16 @@ static int __init pvh_load_kernel(
       * split into smaller allocations, done as a single region in order to
       * simplify it.
       */
-    last_addr = find_memory(d, &elf, sizeof(start_info) +
-                            (initrd ? ROUNDUP(initrd_len, PAGE_SIZE) +
-                                      sizeof(mod)
-                                    : 0) +
-                            (cmdline ? ROUNDUP(strlen(cmdline) + 1,
-                                               elf_64bit(&elf) ? 8 : 4)
-                                     : 0));
+    metadata_len = sizeof(start_info);
+
+    if ( initrd )
+        metadata_len += sizeof(mod) + ROUNDUP(initrd_len, PAGE_SIZE);
+
+    if ( cmdline )
+        metadata_len += ROUNDUP(strlen(cmdline) + 1,
+                                elf_64bit(&elf) ? 8 : 4);
+
+    last_addr = find_memory(d, &elf, metadata_len);
      if ( last_addr == INVALID_PADDR )
      {

         printk("Unable to find a memory region to load initrd and metadata\n");

Quoted 5 lines

which I think I ought to submit as a prerequisite to this, after which your logic squashed on the RHS now becomes an expansion of the if ( initrd ).

Thoughts?

Yes please, I was going to ask Jan to break that chain of ?: operators as it's impossible to read.

Thanks, Roger.

#3 lore raw
In reply to Andrew Cooper
Quoted 77 lines · Andrew Cooper
Quoted 21 lines · Jan Beulich

As per observation in practice, initrd->cmdline_pa is not normally zero. Hence so far we always appended at least one byte. That alone may already render insufficient the "allocation" made by find_memory(). Things would be worse when there's actually a (perhaps long) command line.

Skip setup when the command line is empty. Amend the "allocation" size by padding and actual size of module command line.

Fixes 0ecb8eb09f9f ("x86/pvh: pass module command line to dom0")
xen/arch/x86/hvm/dom0_build.c +2 -1
@@ -712,7 +712,15 @@ static int __init pvh_load_kernel(
      * simplify it.
      */
     last_addr = find_memory(d, &elf, sizeof(start_info) +
-                            (initrd ? ROUNDUP(initrd_len, PAGE_SIZE) +
+                            (initrd ? ROUNDUP(ROUNDUP(initrd_len,
+                                                      elf_64bit(&elf) ? 8 : 4) +

xen/arch/x86/hvm/dom0_build.c static int __init pvh_load_kernel( · line 726

Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +7 -0
+                                              (initrd->cmdline_pa &&
+                                               strlen(__va(initrd->
+                                                           cmdline_pa))
+                                               ? strlen(__va(initrd->
+                                                             cmdline_pa)) + 1
+                                               : 0),
+                                              PAGE_SIZE) +
                                       sizeof(mod)
                                     : 0) +
                             (cmdline ? ROUNDUP(strlen(cmdline) + 1,

This piece of logic was already bad, but this is rather worse.

One patch I made while doing the boot module work is:

xen/arch/x86/hvm/dom0_build.c +0 -0
index 6a4453103a9a..7292ddd07276 100644
@@ -654,6 +654,7 @@ static int __init pvh_load_kernel(

     const char *cmdline = image->cmdline_pa ? __va(image->cmdline_pa) :
NULL;
     struct elf_binary elf;
     struct elf_dom_parms parms;
+    size_t metadata_len;
     paddr_t last_addr;
     struct hvm_start_info start_info = { 0 };
     struct hvm_modlist_entry mod = { 0 };

xen/arch/x86/hvm/dom0_build.c +10 -7
@@ -711,13 +712,16 @@ static int __init pvh_load_kernel(
       * split into smaller allocations, done as a single region in order to
       * simplify it.
       */
-    last_addr = find_memory(d, &elf, sizeof(start_info) +
-                            (initrd ? ROUNDUP(initrd_len, PAGE_SIZE) +
-                                      sizeof(mod)
-                                    : 0) +
-                            (cmdline ? ROUNDUP(strlen(cmdline) + 1,
-                                               elf_64bit(&elf) ? 8 : 4)
-                                     : 0));
+    metadata_len = sizeof(start_info);
+
+    if ( initrd )
+        metadata_len += sizeof(mod) + ROUNDUP(initrd_len, PAGE_SIZE);
+
+    if ( cmdline )
+        metadata_len += ROUNDUP(strlen(cmdline) + 1,
+                                elf_64bit(&elf) ? 8 : 4);
+
+    last_addr = find_memory(d, &elf, metadata_len);
      if ( last_addr == INVALID_PADDR )
      {

         printk("Unable to find a memory region to load initrd and metadata\n");

Quoted 3 lines

which I think I ought to submit as a prerequisite to this, after which your logic squashed on the RHS now becomes an expansion of the if ( initrd ).

Yes please.

Another thing I was considering to put in a local variable is the repeated "elf_64bit(&elf) ? 8 : 4". If that sounds okay, I can make that a(nother) pre-patch to mine.

Jan

#4 lore raw
Quoted 77 lines · Jan Beulich
Quoted 76 lines · Andrew Cooper
Quoted 21 lines · Jan Beulich

As per observation in practice, initrd->cmdline_pa is not normally zero. Hence so far we always appended at least one byte. That alone may already render insufficient the "allocation" made by find_memory(). Things would be worse when there's actually a (perhaps long) command line.

Skip setup when the command line is empty. Amend the "allocation" size by padding and actual size of module command line.

Fixes 0ecb8eb09f9f ("x86/pvh: pass module command line to dom0")
xen/arch/x86/hvm/dom0_build.c +2 -1
@@ -712,7 +712,15 @@ static int __init pvh_load_kernel(
      * simplify it.
      */
     last_addr = find_memory(d, &elf, sizeof(start_info) +
-                            (initrd ? ROUNDUP(initrd_len, PAGE_SIZE) +
+                            (initrd ? ROUNDUP(ROUNDUP(initrd_len,
+                                                      elf_64bit(&elf) ? 8 : 4) +

xen/arch/x86/hvm/dom0_build.c static int __init pvh_load_kernel( · line 726

Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +7 -0
+                                              (initrd->cmdline_pa &&
+                                               strlen(__va(initrd->
+                                                           cmdline_pa))
+                                               ? strlen(__va(initrd->
+                                                             cmdline_pa)) + 1
+                                               : 0),
+                                              PAGE_SIZE) +
                                       sizeof(mod)
                                     : 0) +
                             (cmdline ? ROUNDUP(strlen(cmdline) + 1,

This piece of logic was already bad, but this is rather worse.

One patch I made while doing the boot module work is:

xen/arch/x86/hvm/dom0_build.c +0 -0
index 6a4453103a9a..7292ddd07276 100644
@@ -654,6 +654,7 @@ static int __init pvh_load_kernel(

     const char *cmdline = image->cmdline_pa ? __va(image->cmdline_pa) :
NULL;
     struct elf_binary elf;
     struct elf_dom_parms parms;
+    size_t metadata_len;
     paddr_t last_addr;
     struct hvm_start_info start_info = { 0 };
     struct hvm_modlist_entry mod = { 0 };

xen/arch/x86/hvm/dom0_build.c +10 -7
@@ -711,13 +712,16 @@ static int __init pvh_load_kernel(
       * split into smaller allocations, done as a single region in order to
       * simplify it.
       */
-    last_addr = find_memory(d, &elf, sizeof(start_info) +
-                            (initrd ? ROUNDUP(initrd_len, PAGE_SIZE) +
-                                      sizeof(mod)
-                                    : 0) +
-                            (cmdline ? ROUNDUP(strlen(cmdline) + 1,
-                                               elf_64bit(&elf) ? 8 : 4)
-                                     : 0));
+    metadata_len = sizeof(start_info);
+
+    if ( initrd )
+        metadata_len += sizeof(mod) + ROUNDUP(initrd_len, PAGE_SIZE);
+
+    if ( cmdline )
+        metadata_len += ROUNDUP(strlen(cmdline) + 1,
+                                elf_64bit(&elf) ? 8 : 4);
+
+    last_addr = find_memory(d, &elf, metadata_len);
      if ( last_addr == INVALID_PADDR )
      {

         printk("Unable to find a memory region to load initrd and metadata\n");

Quoted 4 lines
Quoted 3 lines

which I think I ought to submit as a prerequisite to this, after which your logic squashed on the RHS now becomes an expansion of the if ( initrd ).

Yes please.

https://lore.kernel.org/xen-devel/[email protected]/T/#u

Quoted 3 lines

Another thing I was considering to put in a local variable is the repeated "elf_64bit(&elf) ? 8 : 4". If that sounds okay, I can make that a(nother) pre-patch to mine.

Yeah, I was thinking the same.  There's a related expression in elf_round_up(), so it might be worth having a macro in include/xen/libelf.h for elf_ptr_size() or so.

~Andrew