From nobody Thu May 16 15:25:51 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1712903354; cv=none; d=zohomail.com; s=zohoarc; b=IwrSJQpBOVslo+apN3VOe+6ge3Y073XsX3dkonjKGFNuFH+y6J40drJ5HzV21ohOuPiqh/PH4O8+USAGF7La2VwomfK9c48YrC/ZH1gRL3HWXAT7l14CetLH1dUg42hnlyMOh6wURPIBttwbmqDOGzFhiiU1k6ndMxVLjCj4dP8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1712903354; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2d7ReGeZaAGrK6QeKjsa3T/domBjtFnuH/9lxJAcAhg=; b=EtRzg7yUEqpLtJr8FD8fy/pUnMeFOBwrAsLgFSiTl3w5Rng+458arxEY72Ix7j95yweyhKqN5z/MdLa0oAtGsSfFuRfvwMoEtoU9MODSSH76oiark4aQUFSynHGbl935ml04PJRDlBXru9HRxpbC1998UvDInVerNa/vMPlSidU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1712903354308990.2039812368795; Thu, 11 Apr 2024 23:29:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rvAOP-0002UA-3d; Fri, 12 Apr 2024 02:28:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rvAON-0002Ty-A1 for qemu-devel@nongnu.org; Fri, 12 Apr 2024 02:28:07 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rvAOK-000519-TN for qemu-devel@nongnu.org; Fri, 12 Apr 2024 02:28:07 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-3-fxVH2dcePFewjgAlI9QsRQ-1; Fri, 12 Apr 2024 02:28:00 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E806D28EC115; Fri, 12 Apr 2024 06:27:59 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 910332026D06; Fri, 12 Apr 2024 06:27:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712903284; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2d7ReGeZaAGrK6QeKjsa3T/domBjtFnuH/9lxJAcAhg=; b=g9ptJ3JujQA5IeaFh00e+4qQkCMaUNNr1zyzjQTpOya54bmANXHrG9iSY9G51O2ybo5yUm 61zXKBXQTzKflfiE7kjRtYjhgxgXCJI9djWCNm2UpZ/f68toYH9ZxLnWwO2k78QGbYBj7v G4MbGfvjEjyURI+AtP0ygKoiSrztPgE= X-MC-Unique: fxVH2dcePFewjgAlI9QsRQ-1 From: Cindy Lu To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org Subject: [PATCH v6] virtio-pci: Fix the crash that the vector was used after released. Date: Fri, 12 Apr 2024 14:26:55 +0800 Message-ID: <20240412062750.475180-1-lulu@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=lulu@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -45 X-Spam_score: -4.6 X-Spam_bar: ---- X-Spam_report: (-4.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.49, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1712903356366100003 During the booting process of the non-standard image, the behavior of the called function in qemu is as follows: 1. vhost_net_stop() was triggered by guest image. This will call the functi= on virtio_pci_set_guest_notifiers() with assgin=3D false, virtio_pci_set_guest_notifiers(=EF=BC=89 will release the irqfd for vector 0 2. virtio_reset() was triggered, this will set configure vector to VIRTIO_N= O_VECTOR 3.vhost_net_start() was called (at this time, the configure vector is still VIRTIO_NO_VECTOR) and then call virtio_pci_set_guest_notifiers() with assgin=3Dtrue, so the irqfd for vector 0 is still not "init" during this pr= ocess 4. The system continues to boot and sets the vector back to 0. After that msix_fire_vector_notifier() was triggered to unmask the vector 0 and meet = the crash To fix the issue, we need to support changing the vector after VIRTIO_CONFI= G_S_DRIVER_OK is set. (gdb) bt 0 __pthread_kill_implementation (threadid=3D, signo=3Dsigno= @entry=3D6, no_tid=3Dno_tid@entry=3D0) at pthread_kill.c:44 1 0x00007fc87148ec53 in __pthread_kill_internal (signo=3D6, threadid=3D) at pthread_kill.c:78 2 0x00007fc87143e956 in __GI_raise (sig=3Dsig@entry=3D6) at ../sysdeps/pos= ix/raise.c:26 3 0x00007fc8714287f4 in __GI_abort () at abort.c:79 4 0x00007fc87142871b in __assert_fail_base (fmt=3D0x7fc8715bbde0 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", asse= rtion=3D0x5606413efd53 "ret =3D=3D 0", file=3D0x5606413ef87d "../accel/kvm/= kvm-all.c", line=3D1837, function=3D) at assert.c:92 5 0x00007fc871437536 in __GI___assert_fail (assertion=3D0x5606413efd53 "ret =3D=3D 0", file=3D0x5606413ef87d "../a= ccel/kvm/kvm-all.c", line=3D1837, function=3D0x5606413f06f0 <__PRETTY_FUNCT= ION__.19> "kvm_irqchip_commit_routes") at assert.c:101 6 0x0000560640f884b5 in kvm_irqchip_commit_routes (s=3D0x560642cae1f0) at = ../accel/kvm/kvm-all.c:1837 7 0x0000560640c98f8e in virtio_pci_one_vector_unmask (proxy=3D0x560643c65f00, queue_no=3D4294967295, vector=3D0, msg=3D..., = n=3D0x560643c6e4c8) at ../hw/virtio/virtio-pci.c:1005 8 0x0000560640c99201 in virtio_pci_vector_unmask (dev=3D0x560643c65f00, ve= ctor=3D0, msg=3D...) at ../hw/virtio/virtio-pci.c:1070 9 0x0000560640bc402e in msix_fire_vector_notifier (dev=3D0x560643c65f00, v= ector=3D0, is_masked=3Dfalse) at ../hw/pci/msix.c:120 10 0x0000560640bc40f1 in msix_handle_mask_update (dev=3D0x560643c65f00, vec= tor=3D0, was_masked=3Dtrue) at ../hw/pci/msix.c:140 11 0x0000560640bc4503 in msix_table_mmio_write (opaque=3D0x560643c65f00, ad= dr=3D12, val=3D0, size=3D4) at ../hw/pci/msix.c:231 12 0x0000560640f26d83 in memory_region_write_accessor (mr=3D0x560643c66540, addr=3D12, value=3D0x7fc86b7bc628, size=3D4, shif= t=3D0, mask=3D4294967295, attrs=3D...) at ../system/memory.c:497 13 0x0000560640f270a6 in access_with_adjusted_size (addr=3D12, value=3D0x7fc86b7bc628, size=3D4, access_size_min=3D1, acc= ess_size_max=3D4, access_fn=3D0x560640f26c8d = , mr=3D0x560643c66540, attrs=3D...) at ../system/memory.c:573 14 0x0000560640f2a2b5 in memory_region_dispatch_write (mr=3D0x560643c66540,= addr=3D12, data=3D0, op=3DMO_32, attrs=3D...) at ../system/memory.c:1521 15 0x0000560640f37bac in flatview_write_continue (fv=3D0x7fc65805e0b0, addr=3D4273803276, attrs=3D..., ptr=3D0x7fc871e9c= 028, len=3D4, addr1=3D12, l=3D4, mr=3D0x560643c66540) at ../system/physmem.c:2714 16 0x0000560640f37d0f in flatview_write (fv=3D0x7fc65805e0b0, addr=3D4273803276, attrs=3D..., buf=3D0x7fc871e9c= 028, len=3D4) at ../system/physmem.c:2756 17 0x0000560640f380bf in address_space_write (as=3D0x560642161ae0 , addr=3D4273803276, attrs= =3D..., buf=3D0x7fc871e9c028, len=3D4) at ../system/physmem.c:2863 18 0x0000560640f3812c in address_space_rw (as=3D0x560642161ae0 , addr=3D4273803276, attrs= =3D..., buf=3D0x7fc871e9c028, len=3D4, is_write=3Dtrue) at ../system/physme= m.c:2873 --Type for more, q to quit, c to continue without paging-- 19 0x0000560640f8aa55 in kvm_cpu_exec (cpu=3D0x560642f205e0) at ../accel/kv= m/kvm-all.c:2915 20 0x0000560640f8d731 in kvm_vcpu_thread_fn (arg=3D0x560642f205e0) at ../ac= cel/kvm/kvm-accel-ops.c:51 21 0x00005606411949f4 in qemu_thread_start (args=3D0x560642f292b0) at ../ut= il/qemu-thread-posix.c:541 22 0x00007fc87148cdcd in start_thread (arg=3D) at pthread_cr= eate.c:442 23 0x00007fc871512630 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clo= ne3.S:81 (gdb) Fixes: f9a09ca3ea ("vhost: add support for configure interrupt") Cc: qemu-stable@nongnu.org Signed-off-by: Cindy Lu Acked-by: Jason Wang Reviewed-by: Michael S. Tsirkin Tested-by: Lei Yang --- hw/virtio/virtio-pci.c | 43 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index 1a7039fb0c..f83ec92990 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -1423,6 +1423,36 @@ static int virtio_pci_add_mem_cap(VirtIOPCIProxy *pr= oxy, =20 return offset; } +static void virtio_pci_set_and_change_vector(VirtIODevice *vdev, + VirtIOPCIProxy *proxy, + int queue_no, uint16_t old_ve= ctor, + uint16_t new_vector) +{ + /* + * If the device uses irqfd and the vector changes after DRIVER_OK is + * set, we need to release the old vector and set up the new one. + * others just need to set the new vector to device + */ + if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) && + (msix_enabled(&proxy->pci_dev) && kvm_msi_via_irqfd_enabled())) { + if (old_vector !=3D VIRTIO_NO_VECTOR) { + kvm_virtio_pci_vector_release_one(proxy, queue_no); + } + } + /*set the new vector to device*/ + if (queue_no =3D=3D VIRTIO_CONFIG_IRQ_IDX) { + vdev->config_vector =3D new_vector; + } else { + virtio_queue_set_vector(vdev, queue_no, new_vector); + } + /* if the new vector chanegd need to set it up */ + if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) && + (msix_enabled(&proxy->pci_dev) && kvm_msi_via_irqfd_enabled())) { + if (new_vector !=3D VIRTIO_NO_VECTOR) { + kvm_virtio_pci_vector_use_one(proxy, queue_no); + } + } +} =20 int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy, uint8_t bar, uint64_t offset, uint64_t length, @@ -1570,7 +1600,12 @@ static void virtio_pci_common_write(void *opaque, hw= addr addr, } else { val =3D VIRTIO_NO_VECTOR; } - vdev->config_vector =3D val; + vector =3D vdev->config_vector; + /*check if need to change the vector*/ + if (val !=3D vector) { + virtio_pci_set_and_change_vector(vdev, proxy, VIRTIO_CONFIG_IR= Q_IDX, + vector, val); + } break; case VIRTIO_PCI_COMMON_STATUS: if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { @@ -1610,7 +1645,11 @@ static void virtio_pci_common_write(void *opaque, hw= addr addr, } else { val =3D VIRTIO_NO_VECTOR; } - virtio_queue_set_vector(vdev, vdev->queue_sel, val); + /*check if need to change the vector*/ + if (val !=3D vector) { + virtio_pci_set_and_change_vector(vdev, proxy, vdev->queue_sel, + vector, val); + } break; case VIRTIO_PCI_COMMON_Q_ENABLE: if (val =3D=3D 1) { --=20 2.43.0