qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/2] virtio-pci: Fix the use of an uninitialized irqfd
@ 2024-07-02  1:59 Cindy Lu
  2024-07-02  1:59 ` [PATCH v4 1/2] " Cindy Lu
  2024-07-02  1:59 ` [PATCH v4 2/2] virtio-pci: Add check the return of kvm_virtio_pci_vector_use_one Cindy Lu
  0 siblings, 2 replies; 6+ messages in thread
From: Cindy Lu @ 2024-07-02  1:59 UTC (permalink / raw)
  To: lulu, mst, jasowang, qemu-devel; +Cc: qemu-stable

The crash was reported in MAC OS and NixOS, here is the link for this bug
https://gitlab.com/qemu-project/qemu/-/issues/2334
https://gitlab.com/qemu-project/qemu/-/issues/2321

changelog v4 -> v3
 - Address the comments in V3
 - sperate the return check in kvm_virtio_pci_vector_use_one()
   in different patch
changelog v3 -> v2
 - Move the vector_irqfd check to virtio_pci_get_notifier().
   This function can also be used while vdev->status is not VIRTIO_CONFIG_S_DRIVER_OK.
   In that case, the vector_irqfd could be NULL, so also add the status check here.
 - Add the return value check for kvm_virtio_pci_vector_use_one().
   Since the return value of function virtio_pci_set_vector() is void,
   just add the error message here.

Cindy Lu (2):
  virtio-pci: Fix the use of an uninitialized irqfd
  virtio-pci: Add check the return of kvm_virtio_pci_vector_use_one

 hw/virtio/virtio-pci.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

-- 
2.45.0



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v4 1/2] virtio-pci: Fix the use of an uninitialized irqfd
  2024-07-02  1:59 [PATCH v4 0/2] virtio-pci: Fix the use of an uninitialized irqfd Cindy Lu
@ 2024-07-02  1:59 ` Cindy Lu
  2024-07-02  7:09   ` Jason Wang
  2024-07-02  1:59 ` [PATCH v4 2/2] virtio-pci: Add check the return of kvm_virtio_pci_vector_use_one Cindy Lu
  1 sibling, 1 reply; 6+ messages in thread
From: Cindy Lu @ 2024-07-02  1:59 UTC (permalink / raw)
  To: lulu, mst, jasowang, qemu-devel; +Cc: qemu-stable

The crash was reported in MAC OS and NixOS, here is the link for this bug
https://gitlab.com/qemu-project/qemu/-/issues/2334
https://gitlab.com/qemu-project/qemu/-/issues/2321

The root cause is the function virtio_pci_set_guest_notifiers() was not called
in the virtio_input device.So the vector_irqfd was not initialized

So the fix is add the check for vector_irqfd in virtio_pci_get_notifier()
The function virtio_pci_get_notifier() can also be used while vdev->status
is not VIRTIO_CONFIG_S_DRIVER_OK. In that case, the vector_irqfd could be NULL.
So also add the status check here.

This fix is verified in vyatta,MacOS,NixOS,fedora system.

The bt tree for this bug is:
Thread 6 "CPU 0/KVM" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7c817be006c0 (LWP 1269146)]
kvm_virtio_pci_vq_vector_use () at ../qemu-9.0.0/hw/virtio/virtio-pci.c:817
817         if (irqfd->users == 0) {
(gdb) thread apply all bt
...
Thread 6 (Thread 0x7c817be006c0 (LWP 1269146) "CPU 0/KVM"):
0  kvm_virtio_pci_vq_vector_use () at ../qemu-9.0.0/hw/virtio/virtio-pci.c:817
1  kvm_virtio_pci_vector_use_one () at ../qemu-9.0.0/hw/virtio/virtio-pci.c:893
2  0x00005983657045e2 in memory_region_write_accessor () at ../qemu-9.0.0/system/memory.c:497
3  0x0000598365704ba6 in access_with_adjusted_size () at ../qemu-9.0.0/system/memory.c:573
4  0x0000598365705059 in memory_region_dispatch_write () at ../qemu-9.0.0/system/memory.c:1528
5  0x00005983659b8e1f in flatview_write_continue_step.isra.0 () at ../qemu-9.0.0/system/physmem.c:2713
6  0x000059836570ba7d in flatview_write_continue () at ../qemu-9.0.0/system/physmem.c:2743
7  flatview_write () at ../qemu-9.0.0/system/physmem.c:2774
8  0x000059836570bb76 in address_space_write () at ../qemu-9.0.0/system/physmem.c:2894
9  0x0000598365763afe in address_space_rw () at ../qemu-9.0.0/system/physmem.c:2904
10 kvm_cpu_exec () at ../qemu-9.0.0/accel/kvm/kvm-all.c:2917
11 0x000059836576656e in kvm_vcpu_thread_fn () at ../qemu-9.0.0/accel/kvm/kvm-accel-ops.c:50
12 0x0000598365926ca8 in qemu_thread_start () at ../qemu-9.0.0/util/qemu-thread-posix.c:541
13 0x00007c8185bcd1cf in ??? () at /usr/lib/libc.so.6
14 0x00007c8185c4e504 in clone () at /usr/lib/libc.so.6

Fixes: 2ce6cff94d ("virtio-pci: fix use of a released vector")
Cc: qemu-stable@nongnu.org
Signed-off-by: Cindy Lu <lulu@redhat.com>
---
 hw/virtio/virtio-pci.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index b1d02f4b3d..6b62165ef6 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -860,6 +860,9 @@ static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
     VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
     VirtQueue *vq;
 
+    if (!proxy->vector_irqfd && vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)
+        return -1;
+
     if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
         *n = virtio_config_get_guest_notifier(vdev);
         *vector = vdev->config_vector;
-- 
2.45.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v4 2/2] virtio-pci: Add check the return of kvm_virtio_pci_vector_use_one
  2024-07-02  1:59 [PATCH v4 0/2] virtio-pci: Fix the use of an uninitialized irqfd Cindy Lu
  2024-07-02  1:59 ` [PATCH v4 1/2] " Cindy Lu
@ 2024-07-02  1:59 ` Cindy Lu
  2024-07-02  7:10   ` Jason Wang
  1 sibling, 1 reply; 6+ messages in thread
From: Cindy Lu @ 2024-07-02  1:59 UTC (permalink / raw)
  To: lulu, mst, jasowang, qemu-devel; +Cc: qemu-stable

Add the return value check for kvm_virtio_pci_vector_use_one().
Since the return value of function virtio_pci_set_vector() is void,
we can do nothing here. So just add the error message here.

Signed-off-by: Cindy Lu <lulu@redhat.com>
---
 hw/virtio/virtio-pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index 6b62165ef6..dc31a37ec0 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -1455,7 +1455,9 @@ static void virtio_pci_set_vector(VirtIODevice *vdev,
     }
     /* If the new vector changed need to set it up. */
     if (kvm_irqfd && new_vector != VIRTIO_NO_VECTOR) {
-        kvm_virtio_pci_vector_use_one(proxy, queue_no);
+        if (kvm_virtio_pci_vector_use_one(proxy, queue_no)) {
+            virtio_error(vdev, "fail to set the vector %d", new_vector);
+        }
     }
 }
 
-- 
2.45.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 1/2] virtio-pci: Fix the use of an uninitialized irqfd
  2024-07-02  1:59 ` [PATCH v4 1/2] " Cindy Lu
@ 2024-07-02  7:09   ` Jason Wang
  2024-07-16  6:25     ` Cindy Lu
  0 siblings, 1 reply; 6+ messages in thread
From: Jason Wang @ 2024-07-02  7:09 UTC (permalink / raw)
  To: Cindy Lu; +Cc: mst, qemu-devel, qemu-stable

On Tue, Jul 2, 2024 at 10:00 AM Cindy Lu <lulu@redhat.com> wrote:
>
> The crash was reported in MAC OS and NixOS, here is the link for this bug
> https://gitlab.com/qemu-project/qemu/-/issues/2334
> https://gitlab.com/qemu-project/qemu/-/issues/2321
>
> The root cause is the function virtio_pci_set_guest_notifiers() was not called
> in the virtio_input device.

Note that the above two issues seem to be virtio-console instead of
virtio-input.

And we need to tweak the above a little bit for example what does "in
the virtio_input device" mean. For example does it mean irqfd is not
used etc.

> So the vector_irqfd was not initialized
>
> So the fix is add the check for vector_irqfd in virtio_pci_get_notifier()

Probably "fix is adding".

> The function virtio_pci_get_notifier() can also be used while vdev->status
> is not VIRTIO_CONFIG_S_DRIVER_OK. In that case, the vector_irqfd could be NULL.
> So also add the status check here.

Let's explain why this is safe, for example, does it mean the
notifiers initialization will be delayed to DRIVER_OK?

>
> This fix is verified in vyatta,MacOS,NixOS,fedora system.
>
> The bt tree for this bug is:
> Thread 6 "CPU 0/KVM" received signal SIGSEGV, Segmentation fault.
> [Switching to Thread 0x7c817be006c0 (LWP 1269146)]
> kvm_virtio_pci_vq_vector_use () at ../qemu-9.0.0/hw/virtio/virtio-pci.c:817
> 817         if (irqfd->users == 0) {
> (gdb) thread apply all bt
> ...
> Thread 6 (Thread 0x7c817be006c0 (LWP 1269146) "CPU 0/KVM"):
> 0  kvm_virtio_pci_vq_vector_use () at ../qemu-9.0.0/hw/virtio/virtio-pci.c:817
> 1  kvm_virtio_pci_vector_use_one () at ../qemu-9.0.0/hw/virtio/virtio-pci.c:893
> 2  0x00005983657045e2 in memory_region_write_accessor () at ../qemu-9.0.0/system/memory.c:497
> 3  0x0000598365704ba6 in access_with_adjusted_size () at ../qemu-9.0.0/system/memory.c:573
> 4  0x0000598365705059 in memory_region_dispatch_write () at ../qemu-9.0.0/system/memory.c:1528
> 5  0x00005983659b8e1f in flatview_write_continue_step.isra.0 () at ../qemu-9.0.0/system/physmem.c:2713
> 6  0x000059836570ba7d in flatview_write_continue () at ../qemu-9.0.0/system/physmem.c:2743
> 7  flatview_write () at ../qemu-9.0.0/system/physmem.c:2774
> 8  0x000059836570bb76 in address_space_write () at ../qemu-9.0.0/system/physmem.c:2894
> 9  0x0000598365763afe in address_space_rw () at ../qemu-9.0.0/system/physmem.c:2904
> 10 kvm_cpu_exec () at ../qemu-9.0.0/accel/kvm/kvm-all.c:2917
> 11 0x000059836576656e in kvm_vcpu_thread_fn () at ../qemu-9.0.0/accel/kvm/kvm-accel-ops.c:50
> 12 0x0000598365926ca8 in qemu_thread_start () at ../qemu-9.0.0/util/qemu-thread-posix.c:541
> 13 0x00007c8185bcd1cf in ??? () at /usr/lib/libc.so.6
> 14 0x00007c8185c4e504 in clone () at /usr/lib/libc.so.6
>
> Fixes: 2ce6cff94d ("virtio-pci: fix use of a released vector")
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Cindy Lu <lulu@redhat.com>
> ---
>  hw/virtio/virtio-pci.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
> index b1d02f4b3d..6b62165ef6 100644
> --- a/hw/virtio/virtio-pci.c
> +++ b/hw/virtio/virtio-pci.c
> @@ -860,6 +860,9 @@ static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
>      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
>      VirtQueue *vq;
>
> +    if (!proxy->vector_irqfd && vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)
> +        return -1;
> +
>      if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
>          *n = virtio_config_get_guest_notifier(vdev);
>          *vector = vdev->config_vector;
> --
> 2.45.0

Thanks



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 2/2] virtio-pci: Add check the return of kvm_virtio_pci_vector_use_one
  2024-07-02  1:59 ` [PATCH v4 2/2] virtio-pci: Add check the return of kvm_virtio_pci_vector_use_one Cindy Lu
@ 2024-07-02  7:10   ` Jason Wang
  0 siblings, 0 replies; 6+ messages in thread
From: Jason Wang @ 2024-07-02  7:10 UTC (permalink / raw)
  To: Cindy Lu; +Cc: mst, qemu-devel, qemu-stable

On Tue, Jul 2, 2024 at 10:00 AM Cindy Lu <lulu@redhat.com> wrote:
>
> Add the return value check for kvm_virtio_pci_vector_use_one().
> Since the return value of function virtio_pci_set_vector() is void,
> we can do nothing here. So just add the error message here.
>
> Signed-off-by: Cindy Lu <lulu@redhat.com>

Acked-by: Jason Wang <jasowang@redhat.com>

Thanks



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 1/2] virtio-pci: Fix the use of an uninitialized irqfd
  2024-07-02  7:09   ` Jason Wang
@ 2024-07-16  6:25     ` Cindy Lu
  0 siblings, 0 replies; 6+ messages in thread
From: Cindy Lu @ 2024-07-16  6:25 UTC (permalink / raw)
  To: Jason Wang; +Cc: mst, qemu-devel, qemu-stable

On Tue, 2 Jul 2024 at 15:09, Jason Wang <jasowang@redhat.com> wrote:
>
> On Tue, Jul 2, 2024 at 10:00 AM Cindy Lu <lulu@redhat.com> wrote:
> >
> > The crash was reported in MAC OS and NixOS, here is the link for this bug
> > https://gitlab.com/qemu-project/qemu/-/issues/2334
> > https://gitlab.com/qemu-project/qemu/-/issues/2321
> >
> > The root cause is the function virtio_pci_set_guest_notifiers() was not called
> > in the virtio_input device.
>
> Note that the above two issues seem to be virtio-console instead of
> virtio-input.
>
but As I debug, this device they are using is virtio-input, not virtio-console
> And we need to tweak the above a little bit for example what does "in
> the virtio_input device" mean. For example does it mean irqfd is not
> used etc.
>
sure will fix this
> > So the vector_irqfd was not initialized
> >
> > So the fix is add the check for vector_irqfd in virtio_pci_get_notifier()
>
> Probably "fix is adding".
>
sure will fix this
thanks
cindy
> > The function virtio_pci_get_notifier() can also be used while vdev->status
> > is not VIRTIO_CONFIG_S_DRIVER_OK. In that case, the vector_irqfd could be NULL.
> > So also add the status check here.
>
> Let's explain why this is safe, for example, does it mean the
> notifiers initialization will be delayed to DRIVER_OK?
>
sure ,will fix this
thanks
cindy
> >
> > This fix is verified in vyatta,MacOS,NixOS,fedora system.
> >
> > The bt tree for this bug is:
> > Thread 6 "CPU 0/KVM" received signal SIGSEGV, Segmentation fault.
> > [Switching to Thread 0x7c817be006c0 (LWP 1269146)]
> > kvm_virtio_pci_vq_vector_use () at ../qemu-9.0.0/hw/virtio/virtio-pci.c:817
> > 817         if (irqfd->users == 0) {
> > (gdb) thread apply all bt
> > ...
> > Thread 6 (Thread 0x7c817be006c0 (LWP 1269146) "CPU 0/KVM"):
> > 0  kvm_virtio_pci_vq_vector_use () at ../qemu-9.0.0/hw/virtio/virtio-pci.c:817
> > 1  kvm_virtio_pci_vector_use_one () at ../qemu-9.0.0/hw/virtio/virtio-pci.c:893
> > 2  0x00005983657045e2 in memory_region_write_accessor () at ../qemu-9.0.0/system/memory.c:497
> > 3  0x0000598365704ba6 in access_with_adjusted_size () at ../qemu-9.0.0/system/memory.c:573
> > 4  0x0000598365705059 in memory_region_dispatch_write () at ../qemu-9.0.0/system/memory.c:1528
> > 5  0x00005983659b8e1f in flatview_write_continue_step.isra.0 () at ../qemu-9.0.0/system/physmem.c:2713
> > 6  0x000059836570ba7d in flatview_write_continue () at ../qemu-9.0.0/system/physmem.c:2743
> > 7  flatview_write () at ../qemu-9.0.0/system/physmem.c:2774
> > 8  0x000059836570bb76 in address_space_write () at ../qemu-9.0.0/system/physmem.c:2894
> > 9  0x0000598365763afe in address_space_rw () at ../qemu-9.0.0/system/physmem.c:2904
> > 10 kvm_cpu_exec () at ../qemu-9.0.0/accel/kvm/kvm-all.c:2917
> > 11 0x000059836576656e in kvm_vcpu_thread_fn () at ../qemu-9.0.0/accel/kvm/kvm-accel-ops.c:50
> > 12 0x0000598365926ca8 in qemu_thread_start () at ../qemu-9.0.0/util/qemu-thread-posix.c:541
> > 13 0x00007c8185bcd1cf in ??? () at /usr/lib/libc.so.6
> > 14 0x00007c8185c4e504 in clone () at /usr/lib/libc.so.6
> >
> > Fixes: 2ce6cff94d ("virtio-pci: fix use of a released vector")
> > Cc: qemu-stable@nongnu.org
> > Signed-off-by: Cindy Lu <lulu@redhat.com>
> > ---
> >  hw/virtio/virtio-pci.c | 3 +++
> >  1 file changed, 3 insertions(+)
> >
> > diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
> > index b1d02f4b3d..6b62165ef6 100644
> > --- a/hw/virtio/virtio-pci.c
> > +++ b/hw/virtio/virtio-pci.c
> > @@ -860,6 +860,9 @@ static int virtio_pci_get_notifier(VirtIOPCIProxy *proxy, int queue_no,
> >      VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
> >      VirtQueue *vq;
> >
> > +    if (!proxy->vector_irqfd && vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)
> > +        return -1;
> > +
> >      if (queue_no == VIRTIO_CONFIG_IRQ_IDX) {
> >          *n = virtio_config_get_guest_notifier(vdev);
> >          *vector = vdev->config_vector;
> > --
> > 2.45.0
>
> Thanks
>



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-07-16  6:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-02  1:59 [PATCH v4 0/2] virtio-pci: Fix the use of an uninitialized irqfd Cindy Lu
2024-07-02  1:59 ` [PATCH v4 1/2] " Cindy Lu
2024-07-02  7:09   ` Jason Wang
2024-07-16  6:25     ` Cindy Lu
2024-07-02  1:59 ` [PATCH v4 2/2] virtio-pci: Add check the return of kvm_virtio_pci_vector_use_one Cindy Lu
2024-07-02  7:10   ` Jason Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).