Skip to contentSkip to conversation

vfio: Fix BAR->VFIODevice translation in vfio_bar_read/write

PATCH In review Jan Kiszka 2 participants 2 messages Oct 8, 2012

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 vfio: Fix BAR->VFIODevice translation in vfio_bar_read/write

DO_UPCAST is supposed to translate from the first member of a struct to that struct, not from arbitrary ones. And it (usually) breaks the build when neglecting this rule. Use container_of to fix the build breakage and likely also the runtime behavior.

---
 hw/vfio_pci.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
hw/vfio_pci.c +2 -2
index a1eeced..d36d50e 100644
@@ -601,7 +601,7 @@ static void vfio_bar_write(void *opaque, target_phys_addr_t addr,
      * which access will service the interrupt, so we're potentially
      * getting quite a few host interrupts per guest interrupt.
      */
-    vfio_eoi(DO_UPCAST(VFIODevice, bars[bar->nr], bar));
+    vfio_eoi(container_of(bar, VFIODevice, bars[bar->nr]));
 }
 
 static uint64_t vfio_bar_read(void *opaque,
@@ -641,7 +641,7 @@ static uint64_t vfio_bar_read(void *opaque,
             __func__, bar->nr, addr, size, data);
 
     /* Same as write above */
-    vfio_eoi(DO_UPCAST(VFIODevice, bars[bar->nr], bar));
+    vfio_eoi(container_of(bar, VFIODevice, bars[bar->nr]));
 
     return data;
 }
Signature · 1 line
1.7.3.4
#1 lore raw
Quoted 4 lines · Jan Kiszka

DO_UPCAST is supposed to translate from the first member of a struct to that struct, not from arbitrary ones. And it (usually) breaks the build when neglecting this rule. Use container_of to fix the build breakage and likely also the runtime behavior.

It should have warned, but afaict the runtime behavior should have been the same. It would have been miraculous if INTx ever got an EOI otherwise. I'll apply this to my tree. Thanks,

Alex

Quoted 27 lines
---
 hw/vfio_pci.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
hw/vfio_pci.c +2 -2
index a1eeced..d36d50e 100644
@@ -601,7 +601,7 @@ static void vfio_bar_write(void *opaque, target_phys_addr_t addr,
      * which access will service the interrupt, so we're potentially
      * getting quite a few host interrupts per guest interrupt.
      */
-    vfio_eoi(DO_UPCAST(VFIODevice, bars[bar->nr], bar));
+    vfio_eoi(container_of(bar, VFIODevice, bars[bar->nr]));
 }
 
 static uint64_t vfio_bar_read(void *opaque,
@@ -641,7 +641,7 @@ static uint64_t vfio_bar_read(void *opaque,
             __func__, bar->nr, addr, size, data);
 
     /* Same as write above */
-    vfio_eoi(DO_UPCAST(VFIODevice, bars[bar->nr], bar));
+    vfio_eoi(container_of(bar, VFIODevice, bars[bar->nr]));
 
     return data;
 }