@samitouri / QOSamiQemu / commits / 08324de90d

hw/pci/msix: fix error handling for msix_init callers

Check return value of msix_init() and return early on failure instead of continuing with invalid state. - Use ret < 0 to handle negative return value. - Use errp parameter to handle failure instead of NULL. - No functional changes. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/413 Signed-off-by: Trieu Huynh <vikingtc4@gmail.com> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Message-ID: <20260318141415.8538-5-vikingtc4@gmail.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>

Trieu Huynh committed Mar 18, 2026 at 23:14 UTC 08324de90d1036326fb562daa89e8af335132a85
5 files changed +25 -13
hw/net/igbvf.c
+1 -1
@@ -260,7 +260,7 @@ static void igbvf_pci_realize(PCIDevice *dev, Error **errp)
260
261 ret = msix_init(dev, IGBVF_MSIX_VEC_NUM, &s->msix, IGBVF_MSIX_BAR_IDX, 0,
262 &s->msix, IGBVF_MSIX_BAR_IDX, 0x2000, 0x70, errp);
263 - if (ret) {
263 + if (ret < 0) {
264 return;
265 }
266
hw/net/rocker/rocker.c
+1 -1
@@ -1228,7 +1228,7 @@ static int rocker_msix_init(Rocker *r, Error **errp)
1228 &r->msix_bar,
1229 ROCKER_PCI_MSIX_BAR_IDX, ROCKER_PCI_MSIX_PBA_OFFSET,
1230 0, errp);
1231 - if (err) {
1231 + if (err < 0) {
1232 return err;
1233 }
1234
hw/pci/msix.c
+1 -1
@@ -432,7 +432,7 @@ int msix_init_exclusive_bar(PCIDevice *dev, uint32_t nentries,
432 0, &dev->msix_exclusive_bar,
433 bar_nr, bar_pba_offset,
434 0, errp);
435 - if (ret) {
435 + if (ret < 0) {
436 return ret;
437 }
438
hw/scsi/megasas.c
+11 -5
@@ -2380,11 +2380,17 @@ static void megasas_scsi_realize(PCIDevice *dev, Error **errp)
2380 memory_region_init_io(&s->queue_io, OBJECT(s), &megasas_queue_ops, s,
2381 "megasas-queue", 0x40000);
2382
2383 - if (megasas_use_msix(s) &&
2384 - msix_init(dev, 15, &s->mmio_io, b->mmio_bar, 0x2000,
2385 - &s->mmio_io, b->mmio_bar, 0x3800, 0x68, NULL)) {
2386 - /* TODO: check msix_init's error, and should fail on msix=on */
2387 - s->msix = ON_OFF_AUTO_OFF;
2383 + if (megasas_use_msix(s)) {
2384 + ret = msix_init(dev, 15, &s->mmio_io, b->mmio_bar, 0x2000,
2385 + &s->mmio_io, b->mmio_bar, 0x3800, 0x68,
2386 + s->msix == ON_OFF_AUTO_ON ? errp : NULL);
2387 +
2388 + if (ret < 0) {
2389 + if (s->msix == ON_OFF_AUTO_ON) {
2390 + return;
2391 + }
2392 + s->msix = ON_OFF_AUTO_OFF;
2393 + }
2394 }
2395
2396 if (pci_is_express(dev)) {
hw/usb/hcd-xhci-pci.c
+11 -5
@@ -173,11 +173,17 @@ static void usb_xhci_pci_realize(struct PCIDevice *dev, Error **errp)
173 }
174
175 if (s->msix != ON_OFF_AUTO_OFF) {
176 - /* TODO check for errors, and should fail when msix=on */
177 - msix_init(dev, s->xhci.numintrs,
178 - &s->xhci.mem, 0, OFF_MSIX_TABLE,
179 - &s->xhci.mem, 0, OFF_MSIX_PBA,
180 - 0x90, NULL);
176 + ret = msix_init(dev, s->xhci.numintrs,
177 + &s->xhci.mem, 0, OFF_MSIX_TABLE,
178 + &s->xhci.mem, 0, OFF_MSIX_PBA,
179 + 0x90, s->msix == ON_OFF_AUTO_ON ? errp : NULL);
180 +
181 + if (ret < 0) {
182 + if (s->msix == ON_OFF_AUTO_ON) {
183 + return;
184 + }
185 + s->msix = ON_OFF_AUTO_OFF;
186 + }
187 }
188 s->xhci.as = pci_get_address_space(dev);
189 }