Skip to contentSkip to conversation

tools/libs/light: fix BAR memory address truncation

PATCH Superseded Jiqian Chen 4 participants 5 messages Oct 10, 2025 – Oct 13, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libs/light: fix BAR memory address truncation

64-bit BAR memory address is truncated when removing a passthrough pci device from guest since it uses "unsigned int".

So, change to use "unsigned long long" to fix this problem.

---
cc: Anthony PERARD <[email protected]>
cc: Juergen Gross <[email protected]>
cc: Oleksii Kurochko <[email protected]>

---
 tools/libs/light/libxl_pci.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/libs/light/libxl_pci.c +5 -4
index 2ea2caeb6624..88ffbbf08abe 100644
@@ -2001,7 +2001,8 @@ static void pci_remove_detached(libxl__egc *egc,
 {
     STATE_AO_GC(prs->aodev->ao);
     libxl_ctx *ctx = libxl__gc_owner(gc);
-    unsigned int start = 0, end = 0, flags = 0, size = 0, irq = 0;
+    unsigned long long start = 0, end = 0, flags = 0, size = 0;
+    unsigned int irq = 0;
     int i, stubdomid = 0;
     const char *sysfs_path;
     FILE *f;
@@ -2031,7 +2032,7 @@ static void pci_remove_detached(libxl__egc *egc,
     }
 
     for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
-        if (fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags) != 3)
+        if (fscanf(f, "0x%llx 0x%llx 0x%llx\n", &start, &end, &flags) != 3)
             continue;
         size = end - start + 1;
         if (start) {
@@ -2040,7 +2041,7 @@ static void pci_remove_detached(libxl__egc *egc,
                                                  size, 0);
                 if (rc < 0)
                     LOGED(ERROR, domid,
-                          "xc_domain_ioport_permission error 0x%x/0x%x",
+                          "xc_domain_ioport_permission error 0x%llx/0x%llx",
                           start,
                           size);
             } else {
@@ -2050,7 +2051,7 @@ static void pci_remove_detached(libxl__egc *egc,
                                                 0);
                 if (rc < 0)
                     LOGED(ERROR, domid,
-                          "xc_domain_iomem_permission error 0x%x/0x%x",
+                          "xc_domain_iomem_permission error 0x%llx/0x%llx",
                           start,
                           size);
             }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 2 lines · Jiqian Chen

64-bit BAR memory address is truncated when removing a passthrough pci device from guest since it uses "unsigned int".

You talking of address truncation only here, ...

tools/libs/light/libxl_pci.c static void pci_remove_detached(libxl__egc *egc, · line 2005

Quoted 9 lines
tools/libs/light/libxl_pci.c +2 -1
@@ -2001,7 +2001,8 @@ static void pci_remove_detached(libxl__egc *egc,
 {
     STATE_AO_GC(prs->aodev->ao);
     libxl_ctx *ctx = libxl__gc_owner(gc);
-    unsigned int start = 0, end = 0, flags = 0, size = 0, irq = 0;
+    unsigned long long start = 0, end = 0, flags = 0, size = 0;
+    unsigned int irq = 0;

... does "flags" really need widening, too?

tools/libs/light/libxl_pci.c static void pci_remove_detached(libxl__egc *egc, · line 2035

Quoted 6 lines
tools/libs/light/libxl_pci.c +1 -1
@@ -2031,7 +2032,7 @@ static void pci_remove_detached(libxl__egc *egc,
     }
 
     for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
-        if (fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags) != 3)
+        if (fscanf(f, "0x%llx 0x%llx 0x%llx\n", &start, &end, &flags) != 3)

While touching this, don't we want to drop the stray 0x in here? Their presence causes bogus input like 0x0x0 to be accepted, afaict.

Quoted 10 lines
tools/libs/light/libxl_pci.c +1 -1
@@ -2040,7 +2041,7 @@ static void pci_remove_detached(libxl__egc *egc,
                                                  size, 0);
                 if (rc < 0)
                     LOGED(ERROR, domid,
-                          "xc_domain_ioport_permission error 0x%x/0x%x",
+                          "xc_domain_ioport_permission error 0x%llx/0x%llx",
                           start,
                           size);
             } else {

tools/libs/light/libxl_pci.c static void pci_remove_detached(libxl__egc *egc, · line 2054

Quoted 5 lines
tools/libs/light/libxl_pci.c +1 -1
@@ -2050,7 +2051,7 @@ static void pci_remove_detached(libxl__egc *egc,
                                                 0);
                 if (rc < 0)
                     LOGED(ERROR, domid,
-                          "xc_domain_iomem_permission error 0x%x/0x%x",
+                          "xc_domain_iomem_permission error 0x%llx/0x%llx",

In the hypervisor I would request use of %#llx here; not sure what the toolstack's take on this is.

Jan

#2 lore raw
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Jiqian Chen

64-bit BAR memory address is truncated when removing a passthrough pci device from guest since it uses "unsigned int".

You talking of address truncation only here, ...

Quoted 11 lines

tools/libs/light/libxl_pci.c static void pci_remove_detached(libxl__egc *egc, · line 2005

Quoted 9 lines
tools/libs/light/libxl_pci.c +2 -1
@@ -2001,7 +2001,8 @@ static void pci_remove_detached(libxl__egc *egc,
  {
      STATE_AO_GC(prs->aodev->ao);
      libxl_ctx *ctx = libxl__gc_owner(gc);
-    unsigned int start = 0, end = 0, flags = 0, size = 0, irq = 0;
+    unsigned long long start = 0, end = 0, flags = 0, size = 0;
+    unsigned int irq = 0;

... does "flags" really need widening, too?

At least on the system I looked the value was printed as a 64-bit one:

# cat /sys/bus/pci/devices/0000:00:00.0/resource
0x0000000000000000 0x0000000000000000 0x0000000000000000
...

So not widening flags would rely on UB to preserve the evaluated PCI_BAR_IO flag in case the high 32 bits don't contain 0.

Quoted 10 lines

tools/libs/light/libxl_pci.c static void pci_remove_detached(libxl__egc *egc, · line 2035

Quoted 7 lines
tools/libs/light/libxl_pci.c +1 -1
@@ -2031,7 +2032,7 @@ static void pci_remove_detached(libxl__egc *egc,
      }
 
      for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
-        if (fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags) != 3)
+        if (fscanf(f, "0x%llx 0x%llx 0x%llx\n", &start, &end, &flags) != 3)

While touching this, don't we want to drop the stray 0x in here? Their presence causes bogus input like 0x0x0 to be accepted, afaict.

Hmm, do we really expect a sysfs file to produce bogus output?

Wouldn't it be better to keep the "0x" in order to detect a differing output format, which could result in silent misbehavior?

I'm not really feeling strong here, as both cases seem highly unlikely.

Quoted 11 lines
Quoted 11 lines
tools/libs/light/libxl_pci.c +1 -1
@@ -2040,7 +2041,7 @@ static void pci_remove_detached(libxl__egc *egc,
                                                   size, 0);
                  if (rc < 0)
                      LOGED(ERROR, domid,
-                          "xc_domain_ioport_permission error 0x%x/0x%x",
+                          "xc_domain_ioport_permission error 0x%llx/0x%llx",
                            start,
                            size);
              } else {
@@ -2050,7 +2051,7 @@ static void pci_remove_detached(libxl__egc *egc,
Quoted 8 lines

tools/libs/light/libxl_pci.c static void pci_remove_detached(libxl__egc *egc, · line 2054

Quoted 5 lines
tools/libs/light/libxl_pci.c +1 -1
                                                  0);
                  if (rc < 0)
                      LOGED(ERROR, domid,
-                          "xc_domain_iomem_permission error 0x%x/0x%x",
+                          "xc_domain_iomem_permission error 0x%llx/0x%llx",

In the hypervisor I would request use of %#llx here; not sure what the toolstack's take on this is.

I'd go a little bit further and request to use uint64_t instead of "unsigned long long" and then use "#"PRIx64 for the format.

Juergen

#3 lore raw
In reply to Jiqian Chen
Quoted 6 lines · Jiqian Chen

64-bit BAR memory address is truncated when removing a passthrough pci device from guest since it uses "unsigned int".

So, change to use "unsigned long long" to fix this problem.

Oh, and - please add a Fixes: tag when addressing bugs.

Jan

#4 lore raw
In reply to Jiqian Chen
Quoted 5 lines · Jiqian Chen

64-bit BAR memory address is truncated when removing a passthrough pci device from guest since it uses "unsigned int".

So, change to use "unsigned long long" to fix this problem.

Quoted 8 lines

---
cc: Anthony PERARD<[email protected]>
cc: Juergen Gross<[email protected]>
cc: Oleksii Kurochko<[email protected]>

---
  tools/libs/light/libxl_pci.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)

With getting proper Ack(s) from maintainers, the patch could be considered to be in 4.21:

Thanks.

~ Oleksii

Quoted 42 lines
tools/libs/light/libxl_pci.c +5 -4
index 2ea2caeb6624..88ffbbf08abe 100644
@@ -2001,7 +2001,8 @@ static void pci_remove_detached(libxl__egc *egc,
  {
      STATE_AO_GC(prs->aodev->ao);
      libxl_ctx *ctx = libxl__gc_owner(gc);
-    unsigned int start = 0, end = 0, flags = 0, size = 0, irq = 0;
+    unsigned long long start = 0, end = 0, flags = 0, size = 0;
+    unsigned int irq = 0;
      int i, stubdomid = 0;
      const char *sysfs_path;
      FILE *f;
@@ -2031,7 +2032,7 @@ static void pci_remove_detached(libxl__egc *egc,
      }
 
      for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
-        if (fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags) != 3)
+        if (fscanf(f, "0x%llx 0x%llx 0x%llx\n", &start, &end, &flags) != 3)
              continue;
          size = end - start + 1;
          if (start) {
@@ -2040,7 +2041,7 @@ static void pci_remove_detached(libxl__egc *egc,
                                                   size, 0);
                  if (rc < 0)
                      LOGED(ERROR, domid,
-                          "xc_domain_ioport_permission error 0x%x/0x%x",
+                          "xc_domain_ioport_permission error 0x%llx/0x%llx",
                            start,
                            size);
              } else {
@@ -2050,7 +2051,7 @@ static void pci_remove_detached(libxl__egc *egc,
                                                  0);
                  if (rc < 0)
                      LOGED(ERROR, domid,
-                          "xc_domain_iomem_permission error 0x%x/0x%x",
+                          "xc_domain_iomem_permission error 0x%llx/0x%llx",
                            start,
                            size);
              }