qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
	"Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>,
	"Volker Rümelin" <vr_qemu@t-online.de>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Gerd Hoffmann" <kraxel@redhat.com>
Subject: [PULL 08/15] virtio-sound: add realize() error cleanup path
Date: Fri, 1 Dec 2023 12:15:28 -0500	[thread overview]
Message-ID: <8cb7b5b8ac46e49c86ada1738e10690e64aeaea9.1701450838.git.mst@redhat.com> (raw)
In-Reply-To: <cover.1701450838.git.mst@redhat.com>

From: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>

QEMU crashes on exit when a virtio-sound device has failed to
realise. Its vmstate field was not cleaned up properly with
qemu_del_vm_change_state_handler().

This patch changes the realize() order as

1. Validate the given configuration values (no resources allocated
   by us either on success or failure)
2. Try AUD_register_card() and return on failure (no resources allocated
   by us on failure)
3. Initialize vmstate, virtio device, heap allocations and stream
   parameters at once.
   If error occurs, goto error_cleanup label which calls
   virtio_snd_unrealize(). This cleans up all resources made in steps
   1-3.

Reported-by: Volker Rümelin <vr_qemu@t-online.de>
Fixes: 2880e676c000 ("Add virtio-sound device stub")
Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Message-Id: <20231116072046.4002957-1-manos.pitsidianakis@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/audio/virtio-snd.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c
index 83e97858e0..3c9f94e94a 100644
--- a/hw/audio/virtio-snd.c
+++ b/hw/audio/virtio-snd.c
@@ -36,6 +36,7 @@ static void virtio_snd_pcm_out_cb(void *data, int available);
 static void virtio_snd_process_cmdq(VirtIOSound *s);
 static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream);
 static void virtio_snd_pcm_in_cb(void *data, int available);
+static void virtio_snd_unrealize(DeviceState *dev);
 
 static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8)
                                   | BIT(VIRTIO_SND_PCM_FMT_U8)
@@ -1065,23 +1066,9 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)
     virtio_snd_pcm_set_params default_params = { 0 };
     uint32_t status;
 
-    vsnd->pcm = NULL;
-    vsnd->vmstate =
-        qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd);
-
     trace_virtio_snd_realize(vsnd);
 
-    vsnd->pcm = g_new0(VirtIOSoundPCM, 1);
-    vsnd->pcm->snd = vsnd;
-    vsnd->pcm->streams =
-        g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams);
-    vsnd->pcm->pcm_params =
-        g_new0(virtio_snd_pcm_set_params, vsnd->snd_conf.streams);
-
-    virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config));
-    virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1);
-
-    /* set number of jacks and streams */
+    /* check number of jacks and streams */
     if (vsnd->snd_conf.jacks > 8) {
         error_setg(errp,
                    "Invalid number of jacks: %"PRIu32,
@@ -1106,6 +1093,19 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)
         return;
     }
 
+    vsnd->vmstate =
+        qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd);
+
+    vsnd->pcm = g_new0(VirtIOSoundPCM, 1);
+    vsnd->pcm->snd = vsnd;
+    vsnd->pcm->streams =
+        g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams);
+    vsnd->pcm->pcm_params =
+        g_new0(virtio_snd_pcm_set_params, vsnd->snd_conf.streams);
+
+    virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config));
+    virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1);
+
     /* set default params for all streams */
     default_params.features = 0;
     default_params.buffer_bytes = cpu_to_le32(8192);
@@ -1130,16 +1130,21 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)
             error_setg(errp,
                        "Can't initialize stream params, device responded with %s.",
                        print_code(status));
-            return;
+            goto error_cleanup;
         }
         status = virtio_snd_pcm_prepare(vsnd, i);
         if (status != cpu_to_le32(VIRTIO_SND_S_OK)) {
             error_setg(errp,
                        "Can't prepare streams, device responded with %s.",
                        print_code(status));
-            return;
+            goto error_cleanup;
         }
     }
+
+    return;
+
+error_cleanup:
+    virtio_snd_unrealize(dev);
 }
 
 static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,
-- 
MST



  parent reply	other threads:[~2023-12-01 17:18 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-01 17:15 [PULL 00/15] virtio,pc,pci: fixes Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 01/15] osdep: add getloadavg Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 02/15] netdev: set timeout depending on loadavg Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 03/15] tests/acpi/bios-tables-test: do not write new blobs unless there are changes Michael S. Tsirkin
2023-12-01 17:27   ` Ani Sinha
2023-12-02 20:59     ` Michael S. Tsirkin
2024-01-24  8:58   ` Igor Mammedov
2024-01-24  9:47     ` Ani Sinha
2023-12-01 17:15 ` [PULL 04/15] hw/audio/virtio-snd-pci: fix the PCI class code Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 05/15] hw/audio/hda-codec: fix multiplication overflow Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 06/15] hw/audio/hda-codec: reenable the audio mixer Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 07/15] virtio-snd: check AUD_register_card return value Michael S. Tsirkin
2023-12-01 17:15 ` Michael S. Tsirkin [this message]
2023-12-01 17:15 ` [PULL 09/15] pcie_sriov: Remove g_new assertion Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 10/15] hw/acpi/erst: Do not ignore Error* in realize handler Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 11/15] hw/i386: fix short-circuit logic with non-optimizing builds Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 12/15] virtio-iommu: Remove useless !sdev check in virtio_iommu_probe() Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 13/15] msix: unset PCIDevice::msix_vector_poll_notifier in rollback Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 14/15] vhost-user: fix the reconnect error Michael S. Tsirkin
2023-12-01 17:15 ` [PULL 15/15] vhost-user-scsi: free the inflight area when reset Michael S. Tsirkin
2023-12-02 20:58 ` [PULL 00/15] virtio,pc,pci: fixes Michael S. Tsirkin
2023-12-04 14:45 ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8cb7b5b8ac46e49c86ada1738e10690e64aeaea9.1701450838.git.mst@redhat.com \
    --to=mst@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=manos.pitsidianakis@linaro.org \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vr_qemu@t-online.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).