virtio-pci: Checks for virtio device presence on the bus.
cover virtio-pci: Checks for virtio device presence on the bus.
At some point, after unplugging virtio-pci the virtio device may be unrealised, but the memory regions may be present in flatview. So, it's a possible situation when memory region's callbacks are called for "unplugged" device.
In patches were added checks for virtio device presence on virtio-bus. Added checks in PCI config callbacks. Also changed return values in memory regions read if virtio not present.
BZ link https://bugzilla.redhat.com/show_bug.cgi?id=1938042
The issue similar to https://bugzilla.redhat.com/show_bug.cgi?id=1743098
Backtrace:
at ../hw/virtio/virtio-pci.c:1271
proxy = 0x560b535f8bf0
vdev = 0x0
(mr=<optimized out>, addr=<optimized out>, value=<optimized out>, size=<optimized out>, shift=<optimized out>, mask=<optimized out>, attrs=...) at ../softmmu/memory.c:491
tmp = <optimized out>
(addr=addr@entry=22, value=value@entry=0x7fd743dfe518, size=size@entry=2, access_size_min=<optimized out>, access_size_max=<optimized out>, access_fn=0x560b513fc500 <memory_region_write_accessor>, mr=0x560b535f95d0, attrs=...) at ../softmmu/memory.c:552
access_mask = 65535
access_size = 2
i = <optimized out>
r = 0
(mr=mr@entry=0x560b535f95d0, addr=22, data=<optimized out>, op=<optimized out>, attrs=attrs@entry=...) at ../softmmu/memory.c:1501
size = 2
(fv=fv@entry=0x7fd6f005d3b0, addr=addr@entry=4246716438, attrs=..., ptr=ptr@entry=0x7fe69b2b0028, len=len@entry=2, addr1=<optimized out>, l=<optimized out>, mr=0x560b535f95d0)
at /usr/src/debug/qemu-kvm-5.2.0-11.module+el8.4.0+10268+62bcbbed.x86_64/include/qemu/host-utils.h:164
ram_ptr = <optimized out>
val = <optimized out>
result = 0
release_lock = true
buf = 0x7fe69b2b0028 ""
at ../softmmu/physmem.c:2799
l = 2
addr1 = 22
mr = <optimized out>
result = 0
_rcu_read_auto = 0x1
result = 0
fv = <optimized out>
attrs = {unspecified = 0, secure = 0, user = 0, requester_id = 0, byte_swap = 0, target_tlb_bit0 = 0, target_tlb_bit1 = 0, target_tlb_bit2 = 0}
run = <optimized out>
ret = <optimized out>
run_ret = 0
cpu = 0x560b522738d0
r = <optimized out>
__clframe = {__cancel_routine = <optimized out>, __cancel_arg = 0x0, __do_it = 1, __cancel_type = <optimized out>}
qemu_thread_args = 0x560b5229a6f0
start_routine = 0x560b513faeb0 <kvm_vcpu_thread_fn>
arg = 0x560b522738d0
r = <optimized out>Andrew Melnychenko (3):
virtio-pci: Added check for virtio device presence in mm callbacks.
virtio-pci: Added check for virtio device in PCI config cbs.
virtio-pci: Changed return values for "notify", "device" and "isr"
read.
hw/virtio/virtio-pci.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/3 virtio-pci: Added check for virtio device presence in mm callbacks.
During unplug the virtio device is unplugged from virtio-bus on pci. In some cases, requests to virtio-pci mm may acquire during/after unplug. Added check that virtio device is on the bus, for "common" memory region.
--- hw/virtio/virtio-pci.c | 8 ++++++++ 1 file changed, 8 insertions(+)
hw/virtio/virtio-pci.c +8 -0
@@ -1146,6 +1146,10 @@ static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,uint32_t val =0 ;int i; +if (vdev ==NULL ) { +return UINT64_MAX ; + } +switch (addr) {case VIRTIO_PCI_COMMON_DFSELECT : val = proxy->dfselect;
@@ -1229,6 +1233,10 @@ static void virtio_pci_common_write(void *opaque, hwaddr addr,VirtIOPCIProxy *proxy = opaque;VirtIODevice *vdev =virtio_bus_get_device (&proxy->bus); +if (vdev ==NULL ) { +return ; + } +switch (addr) {case VIRTIO_PCI_COMMON_DFSELECT : proxy->dfselect = val;
Signature · 1 line
2.31.1
PATCH 2/3 virtio-pci: Added check for virtio device in PCI config cbs.
In reply to Andrew MelnychenkoNow, if virtio device is not present on virtio-bus - pci config callbacks will not lead to possible crush. The read will return "-1" which should be interpreted by a driver that pci device may be unplugged.
--- hw/virtio/virtio-pci.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
hw/virtio/virtio-pci.c +10 -0
@@ -423,6 +423,11 @@ static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,VirtIODevice *vdev =virtio_bus_get_device (&proxy->bus);uint32_t config =VIRTIO_PCI_CONFIG_SIZE (&proxy->pci_dev);uint64_t val =0 ; + +if (vdev ==NULL ) { +return UINT64_MAX ; + } +if (addr < config) {return virtio_ioport_read (proxy, addr); }
@@ -454,6 +459,11 @@ static void virtio_pci_config_write(void *opaque, hwaddr addr,VirtIOPCIProxy *proxy = opaque;uint32_t config =VIRTIO_PCI_CONFIG_SIZE (&proxy->pci_dev);VirtIODevice *vdev =virtio_bus_get_device (&proxy->bus); + +if (vdev ==NULL ) { +return ; + } +if (addr < config) {virtio_ioport_write (proxy, addr, val);return ;
Signature · 1 line
2.31.1
PATCH 3/3 virtio-pci: Changed return values for "notify", "device" and "isr" read.
In reply to Andrew MelnychenkoAdded check for "notify" memory region. Now reads will return "-1" if a virtio device is not present on a virtio bus.
--- hw/virtio/virtio-pci.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
hw/virtio/virtio-pci.c +7 -2
@@ -1348,6 +1348,11 @@ static void virtio_pci_common_write(void *opaque, hwaddr addr,static uint64_t virtio_pci_notify_read (void *opaque,hwaddr addr,unsigned size) { +VirtIOPCIProxy *proxy = opaque; +if (virtio_bus_get_device (&proxy->bus) ==NULL ) { +return UINT64_MAX ; + } +return 0 ; }
@@ -1385,7 +1390,7 @@ static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,uint64_t val;if (vdev ==NULL ) { -return 0 ; +return UINT64_MAX ; } val =qatomic_xchg (&vdev->isr,0 );
@@ -1406,7 +1411,7 @@ static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,uint64_t val;if (vdev ==NULL ) { -return 0 ; +return UINT64_MAX ; }switch (size) {
Signature · 1 line
2.31.1