From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1704400585486937.4196368069848; Thu, 4 Jan 2024 12:36:25 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQD-0003CJ-SE; Thu, 04 Jan 2024 15:34:33 -0500 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 1rLUQB-0003B3-Ro for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:31 -0500 Received: from mailout08.t-online.de ([194.25.134.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQ8-0004JF-Tc for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:31 -0500 Received: from fwd81.aul.t-online.de (fwd81.aul.t-online.de [10.223.144.107]) by mailout08.t-online.de (Postfix) with SMTP id 9C93E2EAC; Thu, 4 Jan 2024 21:34:25 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd81.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQ2-1IPQgr0; Thu, 4 Jan 2024 21:34:22 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 44B1720024B; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 01/10] hw/audio/virtio-sound: remove command and stream mutexes Date: Thu, 4 Jan 2024 21:34:13 +0100 Message-Id: <20240104203422.12308-1-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400462-FCAAA990-FCE96F53/0/0 CLEAN NORMAL X-TOI-MSGID: 08c55425-e090-4b0c-8026-f7b759b07a05 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=194.25.134.20; envelope-from=volker.ruemelin@t-online.de; helo=mailout08.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400586225100001 All code in virtio-snd.c runs with the BQL held. Remove the command queue mutex and the stream queue mutexes. The qatomic functions are also not needed. Signed-off-by: Volker R=C3=BCmelin --- hw/audio/virtio-snd.c | 294 +++++++++++++++------------------- include/hw/audio/virtio-snd.h | 3 - 2 files changed, 130 insertions(+), 167 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index ea2aeaef14..8344f61c64 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -19,7 +19,6 @@ #include "qemu/iov.h" #include "qemu/log.h" #include "qemu/error-report.h" -#include "include/qemu/lockable.h" #include "sysemu/runstate.h" #include "trace.h" #include "qapi/error.h" @@ -453,7 +452,6 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, = uint32_t stream_id) stream->id =3D stream_id; stream->pcm =3D s->pcm; stream->s =3D s; - qemu_mutex_init(&stream->queue_mutex); QSIMPLEQ_INIT(&stream->queue); QSIMPLEQ_INIT(&stream->invalid); =20 @@ -580,9 +578,7 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSoun= d *s, =20 stream =3D virtio_snd_pcm_get_stream(s, stream_id); if (stream) { - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - stream->active =3D start; - } + stream->active =3D start; if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { AUD_set_active_out(stream->voice.out, start); } else { @@ -606,13 +602,11 @@ static size_t virtio_snd_pcm_get_io_msgs_count(VirtIO= SoundPCMStream *stream) VirtIOSoundPCMBuffer *buffer, *next; size_t count =3D 0; =20 - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) { - count +=3D 1; - } - QSIMPLEQ_FOREACH_SAFE(buffer, &stream->invalid, entry, next) { - count +=3D 1; - } + QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) { + count +=3D 1; + } + QSIMPLEQ_FOREACH_SAFE(buffer, &stream->invalid, entry, next) { + count +=3D 1; } return count; } @@ -762,23 +756,15 @@ static void virtio_snd_process_cmdq(VirtIOSound *s) { virtio_snd_ctrl_command *cmd; =20 - if (unlikely(qatomic_read(&s->processing_cmdq))) { - return; - } - - WITH_QEMU_LOCK_GUARD(&s->cmdq_mutex) { - qatomic_set(&s->processing_cmdq, true); - while (!QTAILQ_EMPTY(&s->cmdq)) { - cmd =3D QTAILQ_FIRST(&s->cmdq); + while (!QTAILQ_EMPTY(&s->cmdq)) { + cmd =3D QTAILQ_FIRST(&s->cmdq); =20 - /* process command */ - process_cmd(s, cmd); + /* process command */ + process_cmd(s, cmd); =20 - QTAILQ_REMOVE(&s->cmdq, cmd, next); + QTAILQ_REMOVE(&s->cmdq, cmd, next); =20 - virtio_snd_ctrl_cmd_free(cmd); - } - qatomic_set(&s->processing_cmdq, false); + virtio_snd_ctrl_cmd_free(cmd); } } =20 @@ -840,32 +826,30 @@ static inline void empty_invalid_queue(VirtIODevice *= vdev, VirtQueue *vq) stream =3D vsnd->pcm->streams[i]; if (stream) { any =3D false; - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - while (!QSIMPLEQ_EMPTY(&stream->invalid)) { - buffer =3D QSIMPLEQ_FIRST(&stream->invalid); - if (buffer->vq !=3D vq) { - break; - } - any =3D true; - resp.status =3D cpu_to_le32(VIRTIO_SND_S_BAD_MSG); - iov_from_buf(buffer->elem->in_sg, - buffer->elem->in_num, - 0, - &resp, - sizeof(virtio_snd_pcm_status)); - virtqueue_push(vq, - buffer->elem, - sizeof(virtio_snd_pcm_status)); - QSIMPLEQ_REMOVE_HEAD(&stream->invalid, entry); - virtio_snd_pcm_buffer_free(buffer); - } - if (any) { - /* - * Notify vq about virtio_snd_pcm_status responses. - * Buffer responses must be notified separately later. - */ - virtio_notify(vdev, vq); + while (!QSIMPLEQ_EMPTY(&stream->invalid)) { + buffer =3D QSIMPLEQ_FIRST(&stream->invalid); + if (buffer->vq !=3D vq) { + break; } + any =3D true; + resp.status =3D cpu_to_le32(VIRTIO_SND_S_BAD_MSG); + iov_from_buf(buffer->elem->in_sg, + buffer->elem->in_num, + 0, + &resp, + sizeof(virtio_snd_pcm_status)); + virtqueue_push(vq, + buffer->elem, + sizeof(virtio_snd_pcm_status)); + QSIMPLEQ_REMOVE_HEAD(&stream->invalid, entry); + virtio_snd_pcm_buffer_free(buffer); + } + if (any) { + /* + * Notify vq about virtio_snd_pcm_status responses. + * Buffer responses must be notified separately later. + */ + virtio_notify(vdev, vq); } } } @@ -924,28 +908,24 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *v= dev, VirtQueue *vq) goto tx_err; } =20 - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - size =3D iov_size(elem->out_sg, elem->out_num) - msg_sz; + size =3D iov_size(elem->out_sg, elem->out_num) - msg_sz; =20 - buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size); - buffer->elem =3D elem; - buffer->populated =3D false; - buffer->vq =3D vq; - buffer->size =3D size; - buffer->offset =3D 0; + buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size); + buffer->elem =3D elem; + buffer->populated =3D false; + buffer->vq =3D vq; + buffer->size =3D size; + buffer->offset =3D 0; =20 - QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry); - } + QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry); continue; =20 tx_err: - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - must_empty_invalid_queue =3D true; - buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer)); - buffer->elem =3D elem; - buffer->vq =3D vq; - QSIMPLEQ_INSERT_TAIL(&stream->invalid, buffer, entry); - } + must_empty_invalid_queue =3D true; + buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer)); + buffer->elem =3D elem; + buffer->vq =3D vq; + QSIMPLEQ_INSERT_TAIL(&stream->invalid, buffer, entry); } =20 if (must_empty_invalid_queue) { @@ -1005,26 +985,23 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *= vdev, VirtQueue *vq) if (stream =3D=3D NULL || stream->info.direction !=3D VIRTIO_SND_D= _INPUT) { goto rx_err; } - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - size =3D iov_size(elem->in_sg, elem->in_num) - - sizeof(virtio_snd_pcm_status); - buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size); - buffer->elem =3D elem; - buffer->vq =3D vq; - buffer->size =3D 0; - buffer->offset =3D 0; - QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry); - } + size =3D iov_size(elem->in_sg, elem->in_num) - + sizeof(virtio_snd_pcm_status); + buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size); + buffer->elem =3D elem; + buffer->vq =3D vq; + buffer->size =3D 0; + buffer->offset =3D 0; + QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry); continue; =20 rx_err: - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - must_empty_invalid_queue =3D true; - buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer)); - buffer->elem =3D elem; - buffer->vq =3D vq; - QSIMPLEQ_INSERT_TAIL(&stream->invalid, buffer, entry); - } + must_empty_invalid_queue =3D true; + buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer)); + buffer->elem =3D elem; + buffer->vq =3D vq; + QSIMPLEQ_INSERT_TAIL(&stream->invalid, buffer, entry); + } =20 if (must_empty_invalid_queue) { @@ -1122,7 +1099,6 @@ static void virtio_snd_realize(DeviceState *dev, Erro= r **errp) virtio_add_queue(vdev, 64, virtio_snd_handle_tx_xfer); vsnd->queues[VIRTIO_SND_VQ_RX] =3D virtio_add_queue(vdev, 64, virtio_snd_handle_rx_xfer); - qemu_mutex_init(&vsnd->cmdq_mutex); QTAILQ_INIT(&vsnd->cmdq); =20 for (uint32_t i =3D 0; i < vsnd->snd_conf.streams; i++) { @@ -1182,50 +1158,48 @@ static void virtio_snd_pcm_out_cb(void *data, int a= vailable) VirtIOSoundPCMBuffer *buffer; size_t size; =20 - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - while (!QSIMPLEQ_EMPTY(&stream->queue)) { - buffer =3D QSIMPLEQ_FIRST(&stream->queue); - if (!virtio_queue_ready(buffer->vq)) { - return; + while (!QSIMPLEQ_EMPTY(&stream->queue)) { + buffer =3D QSIMPLEQ_FIRST(&stream->queue); + if (!virtio_queue_ready(buffer->vq)) { + return; + } + if (!stream->active) { + /* Stream has stopped, so do not perform AUD_write. */ + return_tx_buffer(stream, buffer); + continue; + } + if (!buffer->populated) { + iov_to_buf(buffer->elem->out_sg, + buffer->elem->out_num, + sizeof(virtio_snd_pcm_xfer), + buffer->data, + buffer->size); + buffer->populated =3D true; + } + for (;;) { + size =3D AUD_write(stream->voice.out, + buffer->data + buffer->offset, + MIN(buffer->size, available)); + assert(size <=3D MIN(buffer->size, available)); + if (size =3D=3D 0) { + /* break out of both loops */ + available =3D 0; + break; } - if (!stream->active) { - /* Stream has stopped, so do not perform AUD_write. */ + buffer->size -=3D size; + buffer->offset +=3D size; + available -=3D size; + if (buffer->size < 1) { return_tx_buffer(stream, buffer); - continue; - } - if (!buffer->populated) { - iov_to_buf(buffer->elem->out_sg, - buffer->elem->out_num, - sizeof(virtio_snd_pcm_xfer), - buffer->data, - buffer->size); - buffer->populated =3D true; - } - for (;;) { - size =3D AUD_write(stream->voice.out, - buffer->data + buffer->offset, - MIN(buffer->size, available)); - assert(size <=3D MIN(buffer->size, available)); - if (size =3D=3D 0) { - /* break out of both loops */ - available =3D 0; - break; - } - buffer->size -=3D size; - buffer->offset +=3D size; - available -=3D size; - if (buffer->size < 1) { - return_tx_buffer(stream, buffer); - break; - } - if (!available) { - break; - } + break; } if (!available) { break; } } + if (!available) { + break; + } } } =20 @@ -1276,41 +1250,39 @@ static void virtio_snd_pcm_in_cb(void *data, int av= ailable) VirtIOSoundPCMBuffer *buffer; size_t size; =20 - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - while (!QSIMPLEQ_EMPTY(&stream->queue)) { - buffer =3D QSIMPLEQ_FIRST(&stream->queue); - if (!virtio_queue_ready(buffer->vq)) { - return; + while (!QSIMPLEQ_EMPTY(&stream->queue)) { + buffer =3D QSIMPLEQ_FIRST(&stream->queue); + if (!virtio_queue_ready(buffer->vq)) { + return; + } + if (!stream->active) { + /* Stream has stopped, so do not perform AUD_read. */ + return_rx_buffer(stream, buffer); + continue; + } + + for (;;) { + size =3D AUD_read(stream->voice.in, + buffer->data + buffer->size, + MIN(available, (stream->params.period_bytes - + buffer->size))); + if (!size) { + available =3D 0; + break; } - if (!stream->active) { - /* Stream has stopped, so do not perform AUD_read. */ + buffer->size +=3D size; + available -=3D size; + if (buffer->size >=3D stream->params.period_bytes) { return_rx_buffer(stream, buffer); - continue; - } - - for (;;) { - size =3D AUD_read(stream->voice.in, - buffer->data + buffer->size, - MIN(available, (stream->params.period_bytes - - buffer->size))); - if (!size) { - available =3D 0; - break; - } - buffer->size +=3D size; - available -=3D size; - if (buffer->size >=3D stream->params.period_bytes) { - return_rx_buffer(stream, buffer); - break; - } - if (!available) { - break; - } + break; } if (!available) { break; } } + if (!available) { + break; + } } } =20 @@ -1327,11 +1299,9 @@ static inline void virtio_snd_pcm_flush(VirtIOSoundP= CMStream *stream) (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) ? return_tx_bu= ffer : return_rx_buffer; =20 - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - while (!QSIMPLEQ_EMPTY(&stream->queue)) { - buffer =3D QSIMPLEQ_FIRST(&stream->queue); - cb(stream, buffer); - } + while (!QSIMPLEQ_EMPTY(&stream->queue)) { + buffer =3D QSIMPLEQ_FIRST(&stream->queue); + cb(stream, buffer); } } =20 @@ -1351,7 +1321,6 @@ static void virtio_snd_unrealize(DeviceState *dev) if (stream) { virtio_snd_process_cmdq(stream->s); virtio_snd_pcm_close(stream); - qemu_mutex_destroy(&stream->queue_mutex); g_free(stream); } } @@ -1362,7 +1331,6 @@ static void virtio_snd_unrealize(DeviceState *dev) vsnd->pcm =3D NULL; } AUD_remove_card(&vsnd->card); - qemu_mutex_destroy(&vsnd->cmdq_mutex); virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_CONTROL]); virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_EVENT]); virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_TX]); @@ -1376,12 +1344,10 @@ static void virtio_snd_reset(VirtIODevice *vdev) VirtIOSound *s =3D VIRTIO_SND(vdev); virtio_snd_ctrl_command *cmd; =20 - WITH_QEMU_LOCK_GUARD(&s->cmdq_mutex) { - while (!QTAILQ_EMPTY(&s->cmdq)) { - cmd =3D QTAILQ_FIRST(&s->cmdq); - QTAILQ_REMOVE(&s->cmdq, cmd, next); - virtio_snd_ctrl_cmd_free(cmd); - } + while (!QTAILQ_EMPTY(&s->cmdq)) { + cmd =3D QTAILQ_FIRST(&s->cmdq); + QTAILQ_REMOVE(&s->cmdq, cmd, next); + virtio_snd_ctrl_cmd_free(cmd); } } =20 diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h index c3767f442b..ea6315f59b 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -148,7 +148,6 @@ struct VirtIOSoundPCMStream { SWVoiceIn *in; SWVoiceOut *out; } voice; - QemuMutex queue_mutex; bool active; QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue; QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) invalid; @@ -220,9 +219,7 @@ struct VirtIOSound { QEMUSoundCard card; VMChangeStateEntry *vmstate; virtio_snd_config snd_conf; - QemuMutex cmdq_mutex; QTAILQ_HEAD(, virtio_snd_ctrl_command) cmdq; - bool processing_cmdq; }; =20 struct virtio_snd_ctrl_command { --=20 2.35.3 From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1704400502403303.8859862228737; Thu, 4 Jan 2024 12:35:02 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQF-0003Cu-VI; Thu, 04 Jan 2024 15:34:35 -0500 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 1rLUQD-0003Bz-AZ for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:33 -0500 Received: from mailout06.t-online.de ([194.25.134.19]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQA-0004Jd-Fg for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:33 -0500 Received: from fwd76.aul.t-online.de (fwd76.aul.t-online.de [10.223.144.102]) by mailout06.t-online.de (Postfix) with SMTP id 9FC4740A4; Thu, 4 Jan 2024 21:34:28 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd76.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQ4-38jvQP0; Thu, 4 Jan 2024 21:34:25 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 46F6420024C; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 02/10] hw/audio/virtio-sound: allocate all streams in advance Date: Thu, 4 Jan 2024 21:34:14 +0100 Message-Id: <20240104203422.12308-2-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400465-FA7FB94C-94D56E5B/0/0 CLEAN NORMAL X-TOI-MSGID: 2315c63e-0cad-4633-816e-443161c0b59f 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=194.25.134.19; envelope-from=volker.ruemelin@t-online.de; helo=mailout06.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400503980100003 It is much easier to migrate an array of structs than individual structs that are accessed via a pointer to a pointer to an array of pointers to struct, where some pointers can also be NULL. For this reason, the audio streams are already allocated during the realization phase and all stream variables that are constant at runtime are initialised immediately after allocation. This is a step towards being able to migrate the audio streams of the virtio sound device after the next few patches. Signed-off-by: Volker R=C3=BCmelin --- hw/audio/virtio-snd.c | 35 ++++++++++++++++++++++------------- include/hw/audio/virtio-snd.h | 1 + 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 8344f61c64..36b1bb502c 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -447,11 +447,9 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s,= uint32_t stream_id) =20 stream =3D virtio_snd_pcm_get_stream(s, stream_id); if (stream =3D=3D NULL) { - stream =3D g_new0(VirtIOSoundPCMStream, 1); + stream =3D &s->streams[stream_id]; stream->active =3D false; - stream->id =3D stream_id; stream->pcm =3D s->pcm; - stream->s =3D s; QSIMPLEQ_INIT(&stream->queue); QSIMPLEQ_INIT(&stream->invalid); =20 @@ -463,14 +461,6 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s,= uint32_t stream_id) } =20 virtio_snd_get_qemu_audsettings(&as, params); - stream->info.direction =3D stream_id < s->snd_conf.streams / 2 + - (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INP= UT; - stream->info.hdr.hda_fn_nid =3D VIRTIO_SOUND_HDA_FN_NID; - stream->info.features =3D 0; - stream->info.channels_min =3D 1; - stream->info.channels_max =3D as.nchannels; - stream->info.formats =3D supported_formats; - stream->info.rates =3D supported_rates; stream->params =3D *params; =20 stream->positions[0] =3D VIRTIO_SND_CHMAP_FL; @@ -1074,6 +1064,24 @@ static void virtio_snd_realize(DeviceState *dev, Err= or **errp) vsnd->vmstate =3D qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd); =20 + vsnd->streams =3D g_new0(VirtIOSoundPCMStream, vsnd->snd_conf.streams); + + for (uint32_t i =3D 0; i < vsnd->snd_conf.streams; i++) { + VirtIOSoundPCMStream *stream =3D &vsnd->streams[i]; + + stream->id =3D i; + stream->s =3D vsnd; + stream->info.hdr.hda_fn_nid =3D VIRTIO_SOUND_HDA_FN_NID; + stream->info.features =3D 0; + stream->info.formats =3D supported_formats; + stream->info.rates =3D supported_rates; + stream->info.direction =3D + i < vsnd->snd_conf.streams / 2 + (vsnd->snd_conf.streams & 1) + ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT; + stream->info.channels_min =3D 1; + stream->info.channels_max =3D 2; + } + vsnd->pcm =3D g_new0(VirtIOSoundPCM, 1); vsnd->pcm->snd =3D vsnd; vsnd->pcm->streams =3D @@ -1314,14 +1322,13 @@ static void virtio_snd_unrealize(DeviceState *dev) qemu_del_vm_change_state_handler(vsnd->vmstate); trace_virtio_snd_unrealize(vsnd); =20 - if (vsnd->pcm) { + if (vsnd->streams) { if (vsnd->pcm->streams) { for (uint32_t i =3D 0; i < vsnd->snd_conf.streams; i++) { stream =3D vsnd->pcm->streams[i]; if (stream) { virtio_snd_process_cmdq(stream->s); virtio_snd_pcm_close(stream); - g_free(stream); } } g_free(vsnd->pcm->streams); @@ -1329,6 +1336,8 @@ static void virtio_snd_unrealize(DeviceState *dev) g_free(vsnd->pcm->pcm_params); g_free(vsnd->pcm); vsnd->pcm =3D NULL; + g_free(vsnd->streams); + vsnd->streams =3D NULL; } AUD_remove_card(&vsnd->card); virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_CONTROL]); diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h index ea6315f59b..05b4490488 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -216,6 +216,7 @@ struct VirtIOSound { VirtQueue *queues[VIRTIO_SND_VQ_MAX]; uint64_t features; VirtIOSoundPCM *pcm; + VirtIOSoundPCMStream *streams; QEMUSoundCard card; VMChangeStateEntry *vmstate; virtio_snd_config snd_conf; --=20 2.35.3 From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1704400513213243.76982600026133; Thu, 4 Jan 2024 12:35:13 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQF-0003Cs-B9; Thu, 04 Jan 2024 15:34:35 -0500 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 1rLUQD-0003Bp-4L for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:33 -0500 Received: from mailout04.t-online.de ([194.25.134.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQA-0004JY-7U for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:32 -0500 Received: from fwd78.aul.t-online.de (fwd78.aul.t-online.de [10.223.144.104]) by mailout04.t-online.de (Postfix) with SMTP id 098A0E204; Thu, 4 Jan 2024 21:34:28 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd78.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQ7-1miNsH0; Thu, 4 Jan 2024 21:34:27 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 49A9720024D; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 03/10] hw/audio/virtio-sound: split out virtio_snd_pcm_start_stop() Date: Thu, 4 Jan 2024 21:34:15 +0100 Message-Id: <20240104203422.12308-3-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400467-5CF5D979-18D812F4/0/0 CLEAN NORMAL X-TOI-MSGID: 8a4e1b48-eb5e-4ae9-8f28-a2ef18b5e391 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=194.25.134.18; envelope-from=volker.ruemelin@t-online.de; helo=mailout04.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400513968100001 Split out virtio_snd_pcm_start_stop(). This is a preparation for the next patch so that it doesn't become too big. Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- hw/audio/trace-events | 3 ++- hw/audio/virtio-snd.c | 57 ++++++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/hw/audio/trace-events b/hw/audio/trace-events index b1870ff224..7554bfcc60 100644 --- a/hw/audio/trace-events +++ b/hw/audio/trace-events @@ -50,7 +50,8 @@ virtio_snd_unrealize(void *snd) "snd %p: unrealize" virtio_snd_handle_pcm_set_params(uint32_t stream) "VIRTIO_SND_PCM_SET_PARA= MS called for stream %"PRIu32 virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event fo= r queue %p" virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called = for stream %"PRIu32 -virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s ca= lled for stream %"PRIu32 +virtio_snd_handle_pcm_start(uint32_t stream) "VIRTIO_SND_R_PCM_START calle= d for stream %"PRIu32 +virtio_snd_handle_pcm_stop(uint32_t stream) "VIRTIO_SND_R_PCM_STOP called = for stream %"PRIu32 virtio_snd_handle_pcm_release(uint32_t stream) "VIRTIO_SND_PCM_RELEASE cal= led for stream %"PRIu32 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = =3D %"PRIu32" =3D=3D %s" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 36b1bb502c..040bc32ebe 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -534,7 +534,42 @@ static void virtio_snd_handle_pcm_prepare(VirtIOSound = *s, } =20 /* - * Handles VIRTIO_SND_R_PCM_START. + * Starts/Stops a VirtIOSound card stream. + * Returns the response status code. (VIRTIO_SND_S_*). + * + * @s: VirtIOSound device + * @stream_id: stream id + * @start: whether to start or stop the stream + */ +static uint32_t virtio_snd_pcm_start_stop(VirtIOSound *s, + uint32_t stream_id, + bool start) +{ + VirtIOSoundPCMStream *stream; + + stream =3D virtio_snd_pcm_get_stream(s, stream_id); + if (!stream) { + return cpu_to_le32(VIRTIO_SND_S_BAD_MSG); + } + + if (start) { + trace_virtio_snd_handle_pcm_start(stream_id); + } else { + trace_virtio_snd_handle_pcm_stop(stream_id); + } + + stream->active =3D start; + if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { + AUD_set_active_out(stream->voice.out, start); + } else { + AUD_set_active_in(stream->voice.in, start); + } + + return cpu_to_le32(VIRTIO_SND_S_OK); +} + +/* + * Handles VIRTIO_SND_R_PCM_START and VIRTIO_SND_R_PCM_STOP. * * @s: VirtIOSound device * @cmd: The request command queue element from VirtIOSound cmdq field @@ -544,7 +579,6 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSoun= d *s, virtio_snd_ctrl_command *cmd, bool start) { - VirtIOSoundPCMStream *stream; virtio_snd_pcm_hdr req; uint32_t stream_id; size_t msg_sz =3D iov_to_buf(cmd->elem->out_sg, @@ -562,24 +596,7 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSou= nd *s, } =20 stream_id =3D le32_to_cpu(req.stream_id); - cmd->resp.code =3D cpu_to_le32(VIRTIO_SND_S_OK); - trace_virtio_snd_handle_pcm_start_stop(start ? "VIRTIO_SND_R_PCM_START= " : - "VIRTIO_SND_R_PCM_STOP", stream_id); - - stream =3D virtio_snd_pcm_get_stream(s, stream_id); - if (stream) { - stream->active =3D start; - if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { - AUD_set_active_out(stream->voice.out, start); - } else { - AUD_set_active_in(stream->voice.in, start); - } - } else { - error_report("Invalid stream id: %"PRIu32, stream_id); - cmd->resp.code =3D cpu_to_le32(VIRTIO_SND_S_BAD_MSG); - return; - } - stream->active =3D start; + cmd->resp.code =3D virtio_snd_pcm_start_stop(s, stream_id, start); } =20 /* --=20 2.35.3 From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 170440053829461.05343325293518; Thu, 4 Jan 2024 12:35:38 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQM-0003El-JM; Thu, 04 Jan 2024 15:34:42 -0500 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 1rLUQK-0003E1-7d for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:40 -0500 Received: from mailout12.t-online.de ([194.25.134.22]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQH-0004KH-Cs for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:39 -0500 Received: from fwd71.aul.t-online.de (fwd71.aul.t-online.de [10.223.144.97]) by mailout12.t-online.de (Postfix) with SMTP id 7D31C2134B; Thu, 4 Jan 2024 21:34:34 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd71.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQ9-2Rt2sj0; Thu, 4 Jan 2024 21:34:29 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 4D32F2002CF; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 04/10] hw/audio/virtio-sound: add stream state variable Date: Thu, 4 Jan 2024 21:34:16 +0100 Message-Id: <20240104203422.12308-4-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400469-E626F957-BE448007/0/0 CLEAN NORMAL X-TOI-MSGID: 458ec8b6-00f8-4969-9965-edc2282b6491 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=194.25.134.22; envelope-from=volker.ruemelin@t-online.de; helo=mailout12.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400540096100003 So far, only rudimentary checks have been made to ensure that the guest only performs state transitions permitted in virtio-v1.2-csd01 5.14.6.6.1 PCM Command Lifecycle. Add a state variable per audio stream and check all state transitions. Because only permitted state transitions are possible, only one copy of the audio stream parameters is required and these do not need to be initialised with default values. The state variable will also make it easier to restore the audio stream after migration. Signed-off-by: Volker R=C3=BCmelin --- hw/audio/virtio-snd.c | 219 ++++++++++++++++++---------------- include/hw/audio/virtio-snd.h | 20 +--- 2 files changed, 116 insertions(+), 123 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 040bc32ebe..a2817a64b5 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -31,11 +31,31 @@ #define VIRTIO_SOUND_CHMAP_DEFAULT 0 #define VIRTIO_SOUND_HDA_FN_NID 0 =20 +#define VSND_PCMSTREAM_STATE_F_PARAMS_SET 0x10000 +#define VSND_PCMSTREAM_STATE_F_PREPARED 0x20000 +#define VSND_PCMSTREAM_STATE_F_ACTIVE 0x40000 + +#define VSND_PCMSTREAM_STATE_UNINITIALIZED 0 +#define VSND_PCMSTREAM_STATE_PARAMS_SET (1 \ + | VSND_PCMSTREAM_STATE_F_PARAMS= _SET) +#define VSND_PCMSTREAM_STATE_PREPARED (2 \ + | VSND_PCMSTREAM_STATE_F_PARAMS= _SET \ + | VSND_PCMSTREAM_STATE_F_PREPAR= ED) +#define VSND_PCMSTREAM_STATE_STARTED (4 \ + | VSND_PCMSTREAM_STATE_F_PARAMS= _SET \ + | VSND_PCMSTREAM_STATE_F_PREPAR= ED \ + | VSND_PCMSTREAM_STATE_F_ACTIVE) +#define VSND_PCMSTREAM_STATE_STOPPED (6 \ + | VSND_PCMSTREAM_STATE_F_PARAMS= _SET \ + | VSND_PCMSTREAM_STATE_F_PREPAR= ED) +#define VSND_PCMSTREAM_STATE_RELEASED (7 \ + | VSND_PCMSTREAM_STATE_F_PARAMS= _SET) + static void virtio_snd_pcm_out_cb(void *data, int available); static void virtio_snd_process_cmdq(VirtIOSound *s); +static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream); 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); =20 static uint32_t supported_formats =3D BIT(VIRTIO_SND_PCM_FMT_S8) | BIT(VIRTIO_SND_PCM_FMT_U8) @@ -153,8 +173,8 @@ virtio_snd_ctrl_cmd_free(virtio_snd_ctrl_command *cmd) static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s, uint32_t stream_id) { - return stream_id >=3D s->snd_conf.streams ? NULL : - s->pcm->streams[stream_id]; + return stream_id >=3D s->snd_conf.streams ? NULL + : &s->streams[stream_id]; } =20 /* @@ -167,7 +187,7 @@ static virtio_snd_pcm_set_params *virtio_snd_pcm_get_pa= rams(VirtIOSound *s, uint32_t strea= m_id) { return stream_id >=3D s->snd_conf.streams ? NULL - : &s->pcm->pcm_params[stream_id]; + : &s->streams[stream_id].params; } =20 /* @@ -252,11 +272,10 @@ static void virtio_snd_handle_pcm_info(VirtIOSound *s, =20 /* * Set the given stream params. - * Called by both virtio_snd_handle_pcm_set_params and during device - * initialization. * Returns the response status code. (VIRTIO_SND_S_*). * * @s: VirtIOSound device + * @stream_id: stream id * @params: The PCM params as defined in the virtio specification */ static @@ -264,9 +283,10 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s, uint32_t stream_id, virtio_snd_pcm_set_params *params) { + VirtIOSoundPCMStream *stream; virtio_snd_pcm_set_params *st_params; =20 - if (stream_id >=3D s->snd_conf.streams || s->pcm->pcm_params =3D=3D NU= LL) { + if (stream_id >=3D s->snd_conf.streams) { /* * TODO: do we need to set DEVICE_NEEDS_RESET? */ @@ -274,7 +294,17 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s, return cpu_to_le32(VIRTIO_SND_S_BAD_MSG); } =20 - st_params =3D virtio_snd_pcm_get_params(s, stream_id); + stream =3D virtio_snd_pcm_get_stream(s, stream_id); + + switch (stream->state) { + case VSND_PCMSTREAM_STATE_UNINITIALIZED: + case VSND_PCMSTREAM_STATE_PARAMS_SET: + case VSND_PCMSTREAM_STATE_PREPARED: + case VSND_PCMSTREAM_STATE_RELEASED: + break; + default: + return cpu_to_le32(VIRTIO_SND_S_BAD_MSG); + } =20 if (params->channels < 1 || params->channels > AUDIO_MAX_CHANNELS) { error_report("Number of channels is not supported."); @@ -289,6 +319,8 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s, return cpu_to_le32(VIRTIO_SND_S_NOT_SUPP); } =20 + st_params =3D virtio_snd_pcm_get_params(s, stream_id); + st_params->buffer_bytes =3D le32_to_cpu(params->buffer_bytes); st_params->period_bytes =3D le32_to_cpu(params->period_bytes); st_params->features =3D le32_to_cpu(params->features); @@ -297,6 +329,14 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s, st_params->format =3D params->format; st_params->rate =3D params->rate; =20 + if (stream->state & VSND_PCMSTREAM_STATE_F_PREPARED) { + /* implicit VIRTIO_SND_R_PCM_RELEASE */ + virtio_snd_pcm_flush(stream); + virtio_snd_pcm_close(stream); + } + + stream->state =3D VSND_PCMSTREAM_STATE_PARAMS_SET; + return cpu_to_le32(VIRTIO_SND_S_OK); } =20 @@ -409,15 +449,12 @@ static void virtio_snd_get_qemu_audsettings(audsettin= gs *as, */ static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream) { - if (stream) { - virtio_snd_pcm_flush(stream); - if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { - AUD_close_out(&stream->pcm->snd->card, stream->voice.out); - stream->voice.out =3D NULL; - } else if (stream->info.direction =3D=3D VIRTIO_SND_D_INPUT) { - AUD_close_in(&stream->pcm->snd->card, stream->voice.in); - stream->voice.in =3D NULL; - } + if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { + AUD_close_out(&stream->s->card, stream->voice.out); + stream->voice.out =3D NULL; + } else { + AUD_close_in(&stream->s->card, stream->voice.in); + stream->voice.in =3D NULL; } } =20 @@ -434,35 +471,28 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s= , uint32_t stream_id) virtio_snd_pcm_set_params *params; VirtIOSoundPCMStream *stream; =20 - if (s->pcm->streams =3D=3D NULL || - s->pcm->pcm_params =3D=3D NULL || - stream_id >=3D s->snd_conf.streams) { + stream =3D virtio_snd_pcm_get_stream(s, stream_id); + if (!stream) { return cpu_to_le32(VIRTIO_SND_S_BAD_MSG); } =20 - params =3D virtio_snd_pcm_get_params(s, stream_id); - if (params =3D=3D NULL) { + switch (stream->state) { + case VSND_PCMSTREAM_STATE_PARAMS_SET: + case VSND_PCMSTREAM_STATE_PREPARED: + case VSND_PCMSTREAM_STATE_RELEASED: + break; + default: return cpu_to_le32(VIRTIO_SND_S_BAD_MSG); } =20 - stream =3D virtio_snd_pcm_get_stream(s, stream_id); - if (stream =3D=3D NULL) { - stream =3D &s->streams[stream_id]; - stream->active =3D false; - stream->pcm =3D s->pcm; + if (!(stream->state & VSND_PCMSTREAM_STATE_F_PREPARED)) { QSIMPLEQ_INIT(&stream->queue); QSIMPLEQ_INIT(&stream->invalid); - - /* - * stream_id >=3D s->snd_conf.streams was checked before so this is - * in-bounds - */ - s->pcm->streams[stream_id] =3D stream; } =20 - virtio_snd_get_qemu_audsettings(&as, params); - stream->params =3D *params; + params =3D virtio_snd_pcm_get_params(s, stream_id); =20 + virtio_snd_get_qemu_audsettings(&as, params); stream->positions[0] =3D VIRTIO_SND_CHMAP_FL; stream->positions[1] =3D VIRTIO_SND_CHMAP_FR; stream->as =3D as; @@ -485,6 +515,8 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, = uint32_t stream_id) AUD_set_volume_in(stream->voice.in, 0, 255, 255); } =20 + stream->state =3D VSND_PCMSTREAM_STATE_PREPARED; + return cpu_to_le32(VIRTIO_SND_S_OK); } =20 @@ -553,12 +585,28 @@ static uint32_t virtio_snd_pcm_start_stop(VirtIOSound= *s, } =20 if (start) { + switch (stream->state) { + case VSND_PCMSTREAM_STATE_PREPARED: + case VSND_PCMSTREAM_STATE_STOPPED: + break; + default: + return cpu_to_le32(VIRTIO_SND_S_BAD_MSG); + } + trace_virtio_snd_handle_pcm_start(stream_id); + stream->state =3D VSND_PCMSTREAM_STATE_STARTED; } else { + switch (stream->state) { + case VSND_PCMSTREAM_STATE_STARTED: + break; + default: + return cpu_to_le32(VIRTIO_SND_S_BAD_MSG); + } + trace_virtio_snd_handle_pcm_stop(stream_id); + stream->state =3D VSND_PCMSTREAM_STATE_STOPPED; } =20 - stream->active =3D start; if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { AUD_set_active_out(stream->voice.out, start); } else { @@ -647,20 +695,23 @@ static void virtio_snd_handle_pcm_release(VirtIOSound= *s, } =20 stream_id =3D le32_to_cpu(stream_id); - trace_virtio_snd_handle_pcm_release(stream_id); stream =3D virtio_snd_pcm_get_stream(s, stream_id); - if (stream =3D=3D NULL) { - /* - * TODO: do we need to set DEVICE_NEEDS_RESET? - */ - error_report("already released stream %"PRIu32, stream_id); - virtio_error(VIRTIO_DEVICE(s), - "already released stream %"PRIu32, - stream_id); + if (!stream) { cmd->resp.code =3D cpu_to_le32(VIRTIO_SND_S_BAD_MSG); return; } =20 + switch (stream->state) { + case VSND_PCMSTREAM_STATE_PREPARED: + case VSND_PCMSTREAM_STATE_STOPPED: + break; + default: + cmd->resp.code =3D cpu_to_le32(VIRTIO_SND_S_BAD_MSG); + return; + } + + trace_virtio_snd_handle_pcm_release(stream_id); + if (virtio_snd_pcm_get_io_msgs_count(stream)) { /* * virtio-v1.2-csd01, 5.14.6.6.5.1, @@ -675,6 +726,10 @@ static void virtio_snd_handle_pcm_release(VirtIOSound = *s, virtio_snd_pcm_flush(stream); } =20 + virtio_snd_pcm_close(stream); + + stream->state =3D VSND_PCMSTREAM_STATE_RELEASED; + cmd->resp.code =3D cpu_to_le32(VIRTIO_SND_S_OK); } =20 @@ -830,7 +885,7 @@ static inline void empty_invalid_queue(VirtIODevice *vd= ev, VirtQueue *vq) bool any =3D false; =20 for (uint32_t i =3D 0; i < vsnd->snd_conf.streams; i++) { - stream =3D vsnd->pcm->streams[i]; + stream =3D &vsnd->streams[i]; if (stream) { any =3D false; while (!QSIMPLEQ_EMPTY(&stream->invalid)) { @@ -905,12 +960,11 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *v= dev, VirtQueue *vq) } stream_id =3D le32_to_cpu(hdr.stream_id); =20 - if (stream_id >=3D s->snd_conf.streams - || s->pcm->streams[stream_id] =3D=3D NULL) { + if (stream_id >=3D s->snd_conf.streams) { goto tx_err; } =20 - stream =3D s->pcm->streams[stream_id]; + stream =3D &s->streams[stream_id]; if (stream->info.direction !=3D VIRTIO_SND_D_OUTPUT) { goto tx_err; } @@ -983,13 +1037,12 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *= vdev, VirtQueue *vq) } stream_id =3D le32_to_cpu(hdr.stream_id); =20 - if (stream_id >=3D s->snd_conf.streams - || !s->pcm->streams[stream_id]) { + if (stream_id >=3D s->snd_conf.streams) { goto rx_err; } =20 - stream =3D s->pcm->streams[stream_id]; - if (stream =3D=3D NULL || stream->info.direction !=3D VIRTIO_SND_D= _INPUT) { + stream =3D &s->streams[stream_id]; + if (stream->info.direction !=3D VIRTIO_SND_D_INPUT) { goto rx_err; } size =3D iov_size(elem->in_sg, elem->in_num) - @@ -1048,8 +1101,6 @@ static void virtio_snd_realize(DeviceState *dev, Erro= r **errp) ERRP_GUARD(); VirtIOSound *vsnd =3D VIRTIO_SND(dev); VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); - virtio_snd_pcm_set_params default_params =3D { 0 }; - uint32_t status; =20 trace_virtio_snd_realize(vsnd); =20 @@ -1087,6 +1138,7 @@ static void virtio_snd_realize(DeviceState *dev, Erro= r **errp) VirtIOSoundPCMStream *stream =3D &vsnd->streams[i]; =20 stream->id =3D i; + stream->state =3D VSND_PCMSTREAM_STATE_UNINITIALIZED; stream->s =3D vsnd; stream->info.hdr.hda_fn_nid =3D VIRTIO_SOUND_HDA_FN_NID; stream->info.features =3D 0; @@ -1099,23 +1151,9 @@ static void virtio_snd_realize(DeviceState *dev, Err= or **errp) stream->info.channels_max =3D 2; } =20 - vsnd->pcm =3D g_new0(VirtIOSoundPCM, 1); - vsnd->pcm->snd =3D vsnd; - vsnd->pcm->streams =3D - g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams); - vsnd->pcm->pcm_params =3D - 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); =20 - /* set default params for all streams */ - default_params.features =3D 0; - default_params.buffer_bytes =3D cpu_to_le32(8192); - default_params.period_bytes =3D cpu_to_le32(2048); - default_params.channels =3D 2; - default_params.format =3D VIRTIO_SND_PCM_FMT_S16; - default_params.rate =3D VIRTIO_SND_PCM_RATE_48000; vsnd->queues[VIRTIO_SND_VQ_CONTROL] =3D virtio_add_queue(vdev, 64, virtio_snd_handle_ctrl); vsnd->queues[VIRTIO_SND_VQ_EVENT] =3D @@ -1125,28 +1163,6 @@ static void virtio_snd_realize(DeviceState *dev, Err= or **errp) vsnd->queues[VIRTIO_SND_VQ_RX] =3D virtio_add_queue(vdev, 64, virtio_snd_handle_rx_xfer); QTAILQ_INIT(&vsnd->cmdq); - - for (uint32_t i =3D 0; i < vsnd->snd_conf.streams; i++) { - status =3D virtio_snd_set_pcm_params(vsnd, i, &default_params); - if (status !=3D cpu_to_le32(VIRTIO_SND_S_OK)) { - error_setg(errp, - "Can't initialize stream params, device responded w= ith %s.", - print_code(status)); - goto error_cleanup; - } - status =3D virtio_snd_pcm_prepare(vsnd, i); - if (status !=3D cpu_to_le32(VIRTIO_SND_S_OK)) { - error_setg(errp, - "Can't prepare streams, device responded with %s.", - print_code(status)); - goto error_cleanup; - } - } - - return; - -error_cleanup: - virtio_snd_unrealize(dev); } =20 static inline void return_tx_buffer(VirtIOSoundPCMStream *stream, @@ -1188,7 +1204,7 @@ static void virtio_snd_pcm_out_cb(void *data, int ava= ilable) if (!virtio_queue_ready(buffer->vq)) { return; } - if (!stream->active) { + if (!(stream->state & VSND_PCMSTREAM_STATE_F_ACTIVE)) { /* Stream has stopped, so do not perform AUD_write. */ return_tx_buffer(stream, buffer); continue; @@ -1280,7 +1296,7 @@ static void virtio_snd_pcm_in_cb(void *data, int avai= lable) if (!virtio_queue_ready(buffer->vq)) { return; } - if (!stream->active) { + if (!(stream->state & VSND_PCMSTREAM_STATE_F_ACTIVE)) { /* Stream has stopped, so do not perform AUD_read. */ return_rx_buffer(stream, buffer); continue; @@ -1340,19 +1356,14 @@ static void virtio_snd_unrealize(DeviceState *dev) trace_virtio_snd_unrealize(vsnd); =20 if (vsnd->streams) { - if (vsnd->pcm->streams) { - for (uint32_t i =3D 0; i < vsnd->snd_conf.streams; i++) { - stream =3D vsnd->pcm->streams[i]; - if (stream) { - virtio_snd_process_cmdq(stream->s); - virtio_snd_pcm_close(stream); - } + virtio_snd_process_cmdq(vsnd); + for (uint32_t i =3D 0; i < vsnd->snd_conf.streams; i++) { + stream =3D &vsnd->streams[i]; + if (stream->state & VSND_PCMSTREAM_STATE_F_PREPARED) { + virtio_snd_pcm_flush(stream); + virtio_snd_pcm_close(stream); } - g_free(vsnd->pcm->streams); } - g_free(vsnd->pcm->pcm_params); - g_free(vsnd->pcm); - vsnd->pcm =3D NULL; g_free(vsnd->streams); vsnd->streams =3D NULL; } diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h index 05b4490488..d1a68444d0 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -75,8 +75,6 @@ typedef struct VirtIOSoundPCMStream VirtIOSoundPCMStream; =20 typedef struct virtio_snd_ctrl_command virtio_snd_ctrl_command; =20 -typedef struct VirtIOSoundPCM VirtIOSoundPCM; - typedef struct VirtIOSoundPCMBuffer VirtIOSoundPCMBuffer; =20 /* @@ -121,34 +119,19 @@ struct VirtIOSoundPCMBuffer { uint8_t data[]; }; =20 -struct VirtIOSoundPCM { - VirtIOSound *snd; - /* - * PCM parameters are a separate field instead of a VirtIOSoundPCMStre= am - * field, because the operation of PCM control requests is first - * VIRTIO_SND_R_PCM_SET_PARAMS and then VIRTIO_SND_R_PCM_PREPARE; this - * means that some times we get parameters without having an allocated - * stream yet. - */ - virtio_snd_pcm_set_params *pcm_params; - VirtIOSoundPCMStream **streams; -}; - struct VirtIOSoundPCMStream { - VirtIOSoundPCM *pcm; virtio_snd_pcm_info info; virtio_snd_pcm_set_params params; uint32_t id; + uint32_t state; /* channel position values (VIRTIO_SND_CHMAP_XXX) */ uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE]; VirtIOSound *s; - bool flushing; audsettings as; union { SWVoiceIn *in; SWVoiceOut *out; } voice; - bool active; QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue; QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) invalid; }; @@ -215,7 +198,6 @@ struct VirtIOSound { =20 VirtQueue *queues[VIRTIO_SND_VQ_MAX]; uint64_t features; - VirtIOSoundPCM *pcm; VirtIOSoundPCMStream *streams; QEMUSoundCard card; VMChangeStateEntry *vmstate; --=20 2.35.3 From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1704400555629813.1497743536909; Thu, 4 Jan 2024 12:35:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQK-0003E9-V3; Thu, 04 Jan 2024 15:34:40 -0500 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 1rLUQJ-0003Dr-4e for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:39 -0500 Received: from mailout12.t-online.de ([194.25.134.22]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQH-0004KK-DC for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:38 -0500 Received: from fwd71.aul.t-online.de (fwd71.aul.t-online.de [10.223.144.97]) by mailout12.t-online.de (Postfix) with SMTP id 93C4A2134C; Thu, 4 Jan 2024 21:34:34 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd71.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQC-2fhLBB0; Thu, 4 Jan 2024 21:34:32 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 5051E2001C7; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 05/10] hw/audio/virtio-sound: return correct command response size Date: Thu, 4 Jan 2024 21:34:17 +0100 Message-Id: <20240104203422.12308-5-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400472-E626F957-4DBA4646/0/0 CLEAN NORMAL X-TOI-MSGID: 3cec086b-da06-42a1-be0f-d300390acd78 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=194.25.134.22; envelope-from=volker.ruemelin@t-online.de; helo=mailout12.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400556207100001 The payload size returned by command VIRTIO_SND_R_PCM_INFO is wrong. The code in process_cmd() assumes that all commands return only a virtio_snd_hdr payload, but some commands like VIRTIO_SND_R_PCM_INFO may return an additional payload. Add a zero initialized payload_size variable to struct virtio_snd_ctrl_command to allow for additional payloads. Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- hw/audio/virtio-snd.c | 7 +++++-- include/hw/audio/virtio-snd.h | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index a2817a64b5..9f3269d72b 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -262,12 +262,13 @@ static void virtio_snd_handle_pcm_info(VirtIOSound *s, memset(&pcm_info[i].padding, 0, 5); } =20 + cmd->payload_size =3D sizeof(virtio_snd_pcm_info) * count; cmd->resp.code =3D cpu_to_le32(VIRTIO_SND_S_OK); iov_from_buf(cmd->elem->in_sg, cmd->elem->in_num, sizeof(virtio_snd_hdr), pcm_info, - sizeof(virtio_snd_pcm_info) * count); + cmd->payload_size); } =20 /* @@ -805,7 +806,8 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cm= d) 0, &cmd->resp, sizeof(virtio_snd_hdr)); - virtqueue_push(cmd->vq, cmd->elem, sizeof(virtio_snd_hdr)); + virtqueue_push(cmd->vq, cmd->elem, + sizeof(virtio_snd_hdr) + cmd->payload_size); virtio_notify(VIRTIO_DEVICE(s), cmd->vq); } =20 @@ -856,6 +858,7 @@ static void virtio_snd_handle_ctrl(VirtIODevice *vdev, = VirtQueue *vq) cmd->elem =3D elem; cmd->vq =3D vq; cmd->resp.code =3D cpu_to_le32(VIRTIO_SND_S_OK); + /* implicit cmd->payload_size =3D 0; */ QTAILQ_INSERT_TAIL(&s->cmdq, cmd, next); elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); } diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h index d1a68444d0..d6e08bd30d 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -210,6 +210,7 @@ struct virtio_snd_ctrl_command { VirtQueue *vq; virtio_snd_hdr ctrl; virtio_snd_hdr resp; + size_t payload_size; QTAILQ_ENTRY(virtio_snd_ctrl_command) next; }; #endif --=20 2.35.3 From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1704400567839846.3799644620815; Thu, 4 Jan 2024 12:36:07 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQJ-0003Dt-88; Thu, 04 Jan 2024 15:34:39 -0500 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 1rLUQH-0003DU-Ox for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:38 -0500 Received: from mailout08.t-online.de ([194.25.134.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQG-0004KI-5r for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:37 -0500 Received: from fwd82.aul.t-online.de (fwd82.aul.t-online.de [10.223.144.108]) by mailout08.t-online.de (Postfix) with SMTP id BA5162EAC; Thu, 4 Jan 2024 21:34:34 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd82.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQE-3xWTPl0; Thu, 4 Jan 2024 21:34:34 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 5347B2001CA; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 06/10] hw/audio/virtio-sound: introduce virtio_snd_pcm_open() Date: Thu, 4 Jan 2024 21:34:18 +0100 Message-Id: <20240104203422.12308-6-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400474-F7FFC954-B2337271/0/0 CLEAN NORMAL X-TOI-MSGID: e3898d1c-5d65-448f-8ecf-408d37b6a749 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=194.25.134.20; envelope-from=volker.ruemelin@t-online.de; helo=mailout08.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400568149100001 Split out the function virtio_snd_pcm_open() from virtio_snd_pcm_prepare(). A later patch also needs the new function. There is no functional change. Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- hw/audio/virtio-snd.c | 57 +++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 9f3269d72b..a1d2b3367e 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -443,6 +443,36 @@ static void virtio_snd_get_qemu_audsettings(audsetting= s *as, as->endianness =3D target_words_bigendian() ? 1 : 0; } =20 +/* + * Open a stream. + * + * @stream: VirtIOSoundPCMStream *stream + */ +static void virtio_snd_pcm_open(VirtIOSoundPCMStream *stream) +{ + virtio_snd_get_qemu_audsettings(&stream->as, &stream->params); + stream->positions[0] =3D VIRTIO_SND_CHMAP_FL; + stream->positions[1] =3D VIRTIO_SND_CHMAP_FR; + + if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { + stream->voice.out =3D AUD_open_out(&stream->s->card, + stream->voice.out, + "virtio-sound.out", + stream, + virtio_snd_pcm_out_cb, + &stream->as); + AUD_set_volume_out(stream->voice.out, 0, 255, 255); + } else { + stream->voice.in =3D AUD_open_in(&stream->s->card, + stream->voice.in, + "virtio-sound.in", + stream, + virtio_snd_pcm_in_cb, + &stream->as); + AUD_set_volume_in(stream->voice.in, 0, 255, 255); + } +} + /* * Close a stream and free all its resources. * @@ -468,8 +498,6 @@ static void virtio_snd_pcm_close(VirtIOSoundPCMStream *= stream) */ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id) { - audsettings as; - virtio_snd_pcm_set_params *params; VirtIOSoundPCMStream *stream; =20 stream =3D virtio_snd_pcm_get_stream(s, stream_id); @@ -491,30 +519,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s,= uint32_t stream_id) QSIMPLEQ_INIT(&stream->invalid); } =20 - params =3D virtio_snd_pcm_get_params(s, stream_id); - - virtio_snd_get_qemu_audsettings(&as, params); - stream->positions[0] =3D VIRTIO_SND_CHMAP_FL; - stream->positions[1] =3D VIRTIO_SND_CHMAP_FR; - stream->as =3D as; - - if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { - stream->voice.out =3D AUD_open_out(&s->card, - stream->voice.out, - "virtio-sound.out", - stream, - virtio_snd_pcm_out_cb, - &as); - AUD_set_volume_out(stream->voice.out, 0, 255, 255); - } else { - stream->voice.in =3D AUD_open_in(&s->card, - stream->voice.in, - "virtio-sound.in", - stream, - virtio_snd_pcm_in_cb, - &as); - AUD_set_volume_in(stream->voice.in, 0, 255, 255); - } + virtio_snd_pcm_open(stream); =20 stream->state =3D VSND_PCMSTREAM_STATE_PREPARED; =20 --=20 2.35.3 From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 170440051644536.41069072573305; Thu, 4 Jan 2024 12:35:16 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQN-0003Eu-Vv; Thu, 04 Jan 2024 15:34:44 -0500 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 1rLUQM-0003Em-Mp for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:42 -0500 Received: from mailout08.t-online.de ([194.25.134.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQL-0004Ki-4A for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:42 -0500 Received: from fwd78.aul.t-online.de (fwd78.aul.t-online.de [10.223.144.104]) by mailout08.t-online.de (Postfix) with SMTP id 60FDE2EA6; Thu, 4 Jan 2024 21:34:39 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd78.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQG-2osYPx0; Thu, 4 Jan 2024 21:34:37 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 566632001CF; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 07/10] hw/audio/virtio-sound: introduce virtio_snd_set_active() Date: Thu, 4 Jan 2024 21:34:19 +0100 Message-Id: <20240104203422.12308-7-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400477-5E75E979-9B3EEB01/0/0 CLEAN NORMAL X-TOI-MSGID: f90e3ea0-3577-4449-b562-9993c9f8cdba 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=194.25.134.20; envelope-from=volker.ruemelin@t-online.de; helo=mailout08.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400517957100003 Split out the function virtio_snd_pcm_set_active() from virtio_snd_pcm_start_stop(). A later patch also needs this new funcion. There is no functional change. Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- hw/audio/virtio-snd.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index a1d2b3367e..16e8c49655 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -473,6 +473,21 @@ static void virtio_snd_pcm_open(VirtIOSoundPCMStream *= stream) } } =20 +/* + * Activate/deactivate a stream. + * + * @stream: VirtIOSoundPCMStream *stream + * @active: whether to activate or deactivate the stream + */ +static void virtio_snd_pcm_set_active(VirtIOSoundPCMStream *stream, bool a= ctive) +{ + if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { + AUD_set_active_out(stream->voice.out, active); + } else { + AUD_set_active_in(stream->voice.in, active); + } +} + /* * Close a stream and free all its resources. * @@ -613,11 +628,7 @@ static uint32_t virtio_snd_pcm_start_stop(VirtIOSound = *s, stream->state =3D VSND_PCMSTREAM_STATE_STOPPED; } =20 - if (stream->info.direction =3D=3D VIRTIO_SND_D_OUTPUT) { - AUD_set_active_out(stream->voice.out, start); - } else { - AUD_set_active_in(stream->voice.in, start); - } + virtio_snd_pcm_set_active(stream, start); =20 return cpu_to_le32(VIRTIO_SND_S_OK); } --=20 2.35.3 From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1704400550920573.0153407114735; Thu, 4 Jan 2024 12:35:50 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQS-0003Rv-8O; Thu, 04 Jan 2024 15:34:48 -0500 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 1rLUQO-0003F9-Jt for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:45 -0500 Received: from mailout03.t-online.de ([194.25.134.81]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQM-0004L4-Hz for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:44 -0500 Received: from fwd85.aul.t-online.de (fwd85.aul.t-online.de [10.223.144.111]) by mailout03.t-online.de (Postfix) with SMTP id 69077401B2; Thu, 4 Jan 2024 21:34:40 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd85.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQJ-1C4Ckj0; Thu, 4 Jan 2024 21:34:39 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 595B52001D0; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 08/10] hw/audio/virtio-sound: fix segmentation fault in tx/rx xfer handler Date: Thu, 4 Jan 2024 21:34:20 +0100 Message-Id: <20240104203422.12308-8-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400479-C1AEC9F9-713543D7/0/0 CLEAN NORMAL X-TOI-MSGID: 495cb1ee-0d85-4428-b627-cbdef59e1bf2 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=194.25.134.81; envelope-from=volker.ruemelin@t-online.de; helo=mailout03.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400552160100003 With the involvement of a malicious guest, it's possible to reach the error paths in the virtio_snd_handle_tx/rx_xfer handlers with stream =3D=3D NULL. This triggers a segmentation fault. Move the queues for invalid messages from the stream structs to the device struct and initialize the queues quite early so they are always available to avoid this kind of issue. Signed-off-by: Volker R=C3=BCmelin --- hw/audio/virtio-snd.c | 84 ++++++++++++++++------------------- include/hw/audio/virtio-snd.h | 8 +++- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 16e8c49655..92b10287ad 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -531,7 +531,6 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, = uint32_t stream_id) =20 if (!(stream->state & VSND_PCMSTREAM_STATE_F_PREPARED)) { QSIMPLEQ_INIT(&stream->queue); - QSIMPLEQ_INIT(&stream->invalid); } =20 virtio_snd_pcm_open(stream); @@ -677,9 +676,6 @@ static size_t virtio_snd_pcm_get_io_msgs_count(VirtIOSo= undPCMStream *stream) QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) { count +=3D 1; } - QSIMPLEQ_FOREACH_SAFE(buffer, &stream->invalid, entry, next) { - count +=3D 1; - } return count; } =20 @@ -895,44 +891,38 @@ static void virtio_snd_handle_event(VirtIODevice *vde= v, VirtQueue *vq) trace_virtio_snd_handle_event(); } =20 -static inline void empty_invalid_queue(VirtIODevice *vdev, VirtQueue *vq) +static void empty_invalid_queue(VirtIODevice *vdev, uint32_t queue_index) { + VirtIOSound *s =3D VIRTIO_SND(vdev); + VirtQueue *vq; VirtIOSoundPCMBuffer *buffer =3D NULL; - VirtIOSoundPCMStream *stream =3D NULL; + VirtIOSoundPCMBufferSimpleQueue *invalidq; virtio_snd_pcm_status resp =3D { 0 }; - VirtIOSound *vsnd =3D VIRTIO_SND(vdev); bool any =3D false; =20 - for (uint32_t i =3D 0; i < vsnd->snd_conf.streams; i++) { - stream =3D &vsnd->streams[i]; - if (stream) { - any =3D false; - while (!QSIMPLEQ_EMPTY(&stream->invalid)) { - buffer =3D QSIMPLEQ_FIRST(&stream->invalid); - if (buffer->vq !=3D vq) { - break; - } - any =3D true; - resp.status =3D cpu_to_le32(VIRTIO_SND_S_BAD_MSG); - iov_from_buf(buffer->elem->in_sg, - buffer->elem->in_num, - 0, - &resp, - sizeof(virtio_snd_pcm_status)); - virtqueue_push(vq, - buffer->elem, - sizeof(virtio_snd_pcm_status)); - QSIMPLEQ_REMOVE_HEAD(&stream->invalid, entry); - virtio_snd_pcm_buffer_free(buffer); - } - if (any) { - /* - * Notify vq about virtio_snd_pcm_status responses. - * Buffer responses must be notified separately later. - */ - virtio_notify(vdev, vq); - } - } + vq =3D s->queues[queue_index]; + invalidq =3D s->invalidq[queue_index]; + while (!QSIMPLEQ_EMPTY(invalidq)) { + any =3D true; + buffer =3D QSIMPLEQ_FIRST(invalidq); + resp.status =3D cpu_to_le32(VIRTIO_SND_S_BAD_MSG); + iov_from_buf(buffer->elem->in_sg, + buffer->elem->in_num, + 0, + &resp, + sizeof(virtio_snd_pcm_status)); + virtqueue_push(vq, + buffer->elem, + sizeof(virtio_snd_pcm_status)); + QSIMPLEQ_REMOVE_HEAD(invalidq, entry); + virtio_snd_pcm_buffer_free(buffer); + } + if (any) { + /* + * Notify vq about virtio_snd_pcm_status responses. + * Buffer responses must be notified separately later. + */ + virtio_notify(vdev, vq); } } =20 @@ -953,8 +943,8 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vde= v, VirtQueue *vq) virtio_snd_pcm_xfer hdr; uint32_t stream_id; /* - * If any of the I/O messages are invalid, put them in stream->invalid= and - * return them after the for loop. + * If any of the I/O messages are invalid, put them in + * s->invalidq[VIRTIO_SND_VQ_TX] and return them after the for loop. */ bool must_empty_invalid_queue =3D false; =20 @@ -1005,11 +995,11 @@ tx_err: buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer)); buffer->elem =3D elem; buffer->vq =3D vq; - QSIMPLEQ_INSERT_TAIL(&stream->invalid, buffer, entry); + QSIMPLEQ_INSERT_TAIL(s->invalidq[VIRTIO_SND_VQ_TX], buffer, entry); } =20 if (must_empty_invalid_queue) { - empty_invalid_queue(vdev, vq); + empty_invalid_queue(vdev, VIRTIO_SND_VQ_TX); } } =20 @@ -1030,8 +1020,8 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *v= dev, VirtQueue *vq) virtio_snd_pcm_xfer hdr; uint32_t stream_id; /* - * if any of the I/O messages are invalid, put them in stream->invalid= and - * return them after the for loop. + * if any of the I/O messages are invalid, put them in + * s->invalidq[VIRTIO_SND_VQ_RX] and return them after the for loop. */ bool must_empty_invalid_queue =3D false; =20 @@ -1079,12 +1069,12 @@ rx_err: buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer)); buffer->elem =3D elem; buffer->vq =3D vq; - QSIMPLEQ_INSERT_TAIL(&stream->invalid, buffer, entry); + QSIMPLEQ_INSERT_TAIL(s->invalidq[VIRTIO_SND_VQ_RX], buffer, entry); =20 } =20 if (must_empty_invalid_queue) { - empty_invalid_queue(vdev, vq); + empty_invalid_queue(vdev, VIRTIO_SND_VQ_RX); } } =20 @@ -1182,6 +1172,10 @@ static void virtio_snd_realize(DeviceState *dev, Err= or **errp) vsnd->queues[VIRTIO_SND_VQ_RX] =3D virtio_add_queue(vdev, 64, virtio_snd_handle_rx_xfer); QTAILQ_INIT(&vsnd->cmdq); + QSIMPLEQ_INIT(&vsnd->invalidq_tx); + vsnd->invalidq[VIRTIO_SND_VQ_TX] =3D &vsnd->invalidq_tx; + QSIMPLEQ_INIT(&vsnd->invalidq_rx); + vsnd->invalidq[VIRTIO_SND_VQ_RX] =3D &vsnd->invalidq_rx; } =20 static inline void return_tx_buffer(VirtIOSoundPCMStream *stream, diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h index d6e08bd30d..9b81f66b05 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -77,6 +77,9 @@ typedef struct virtio_snd_ctrl_command virtio_snd_ctrl_co= mmand; =20 typedef struct VirtIOSoundPCMBuffer VirtIOSoundPCMBuffer; =20 +typedef QSIMPLEQ_HEAD(VirtIOSoundPCMBufferSimpleQueue, VirtIOSoundPCMBuffe= r) + VirtIOSoundPCMBufferSimpleQueue; + /* * The VirtIO sound spec reuses layouts and values from the High Definition * Audio spec (virtio/v1.2: 5.14 Sound Device). This struct handles each I= /O @@ -132,8 +135,7 @@ struct VirtIOSoundPCMStream { SWVoiceIn *in; SWVoiceOut *out; } voice; - QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue; - QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) invalid; + VirtIOSoundPCMBufferSimpleQueue queue; }; =20 /* @@ -197,12 +199,14 @@ struct VirtIOSound { VirtIODevice parent_obj; =20 VirtQueue *queues[VIRTIO_SND_VQ_MAX]; + VirtIOSoundPCMBufferSimpleQueue *invalidq[VIRTIO_SND_VQ_MAX]; uint64_t features; VirtIOSoundPCMStream *streams; QEMUSoundCard card; VMChangeStateEntry *vmstate; virtio_snd_config snd_conf; QTAILQ_HEAD(, virtio_snd_ctrl_command) cmdq; + VirtIOSoundPCMBufferSimpleQueue invalidq_tx, invalidq_rx; }; =20 struct virtio_snd_ctrl_command { --=20 2.35.3 From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1704400558410359.92921918043544; Thu, 4 Jan 2024 12:35:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQc-0003Zd-79; Thu, 04 Jan 2024 15:34:58 -0500 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 1rLUQZ-0003YO-6V for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:55 -0500 Received: from mailout04.t-online.de ([194.25.134.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQW-0004N0-Fq for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:54 -0500 Received: from fwd72.aul.t-online.de (fwd72.aul.t-online.de [10.223.144.98]) by mailout04.t-online.de (Postfix) with SMTP id AF78D3BBF; Thu, 4 Jan 2024 21:34:50 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd72.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQL-3uNmmv0; Thu, 4 Jan 2024 21:34:41 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 5C83E2001D1; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 09/10] hw/audio/virtio-sound: add missing vmstate fields Date: Thu, 4 Jan 2024 21:34:21 +0100 Message-Id: <20240104203422.12308-9-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400481-5181C95D-F891D282/0/0 CLEAN NORMAL X-TOI-MSGID: 19aac2dd-b5b8-4c83-8744-8f765b1dcaf1 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=194.25.134.18; envelope-from=volker.ruemelin@t-online.de; helo=mailout04.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400560117100003 The virtio-sound device is currently not migratable. Add the missing VMSTATE fields, enable migration and reconnect the audio streams after migration. The queue_inuse[] array variables mimic the inuse variable in struct VirtQueue which is private. They are needed to restart the virtio queues after migration. Signed-off-by: Volker R=C3=BCmelin --- hw/audio/virtio-snd.c | 81 +++++++++++++++++++++++++++++++---- include/hw/audio/virtio-snd.h | 1 + 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 92b10287ad..328ee54808 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -25,7 +25,6 @@ #include "hw/audio/virtio-snd.h" #include "hw/core/cpu.h" =20 -#define VIRTIO_SOUND_VM_VERSION 1 #define VIRTIO_SOUND_JACK_DEFAULT 0 #define VIRTIO_SOUND_STREAM_DEFAULT 2 #define VIRTIO_SOUND_CHMAP_DEFAULT 0 @@ -80,17 +79,40 @@ static uint32_t supported_rates =3D BIT(VIRTIO_SND_PCM_= RATE_5512) | BIT(VIRTIO_SND_PCM_RATE_192000) | BIT(VIRTIO_SND_PCM_RATE_384000); =20 +static const VMStateDescription vmstate_virtio_snd_stream =3D { + .name =3D "virtio-sound-stream", + .fields =3D (const VMStateField[]) { + VMSTATE_UINT32(state, VirtIOSoundPCMStream), + VMSTATE_UINT32(info.hdr.hda_fn_nid, VirtIOSoundPCMStream), + VMSTATE_UINT32(info.features, VirtIOSoundPCMStream), + VMSTATE_UINT64(info.formats, VirtIOSoundPCMStream), + VMSTATE_UINT64(info.rates, VirtIOSoundPCMStream), + VMSTATE_UINT8(info.direction, VirtIOSoundPCMStream), + VMSTATE_UINT8(info.channels_min, VirtIOSoundPCMStream), + VMSTATE_UINT8(info.channels_max, VirtIOSoundPCMStream), + VMSTATE_UINT32(params.buffer_bytes, VirtIOSoundPCMStream), + VMSTATE_UINT32(params.period_bytes, VirtIOSoundPCMStream), + VMSTATE_UINT32(params.features, VirtIOSoundPCMStream), + VMSTATE_UINT8(params.channels, VirtIOSoundPCMStream), + VMSTATE_UINT8(params.format, VirtIOSoundPCMStream), + VMSTATE_UINT8(params.rate, VirtIOSoundPCMStream), + VMSTATE_END_OF_LIST() + }, +}; + static const VMStateDescription vmstate_virtio_snd_device =3D { - .name =3D TYPE_VIRTIO_SND, - .version_id =3D VIRTIO_SOUND_VM_VERSION, - .minimum_version_id =3D VIRTIO_SOUND_VM_VERSION, + .name =3D "virtio-sound-device", + .fields =3D (const VMStateField[]) { + VMSTATE_UINT32_ARRAY(queue_inuse, VirtIOSound, VIRTIO_SND_VQ_MAX), + VMSTATE_STRUCT_VARRAY_POINTER_UINT32(streams, VirtIOSound, + snd_conf.streams, + vmstate_virtio_snd_stream, VirtIOSoundPCMStream), + VMSTATE_END_OF_LIST() + }, }; =20 static const VMStateDescription vmstate_virtio_snd =3D { - .name =3D TYPE_VIRTIO_SND, - .unmigratable =3D 1, - .minimum_version_id =3D VIRTIO_SOUND_VM_VERSION, - .version_id =3D VIRTIO_SOUND_VM_VERSION, + .name =3D "virtio-sound", .fields =3D (const VMStateField[]) { VMSTATE_VIRTIO_DEVICE, VMSTATE_END_OF_LIST() @@ -820,6 +842,7 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cm= d) sizeof(virtio_snd_hdr)); virtqueue_push(cmd->vq, cmd->elem, sizeof(virtio_snd_hdr) + cmd->payload_size); + s->queue_inuse[VIRTIO_SND_VQ_CONTROL] -=3D 1; virtio_notify(VIRTIO_DEVICE(s), cmd->vq); } =20 @@ -866,6 +889,7 @@ static void virtio_snd_handle_ctrl(VirtIODevice *vdev, = VirtQueue *vq) =20 elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); while (elem) { + s->queue_inuse[VIRTIO_SND_VQ_CONTROL] +=3D 1; cmd =3D g_new0(virtio_snd_ctrl_command, 1); cmd->elem =3D elem; cmd->vq =3D vq; @@ -914,6 +938,7 @@ static void empty_invalid_queue(VirtIODevice *vdev, uin= t32_t queue_index) virtqueue_push(vq, buffer->elem, sizeof(virtio_snd_pcm_status)); + s->queue_inuse[queue_index] -=3D 1; QSIMPLEQ_REMOVE_HEAD(invalidq, entry); virtio_snd_pcm_buffer_free(buffer); } @@ -958,6 +983,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vde= v, VirtQueue *vq) if (!elem) { break; } + s->queue_inuse[VIRTIO_SND_VQ_TX] +=3D 1; /* get the message hdr object */ msg_sz =3D iov_to_buf(elem->out_sg, elem->out_num, @@ -1035,6 +1061,7 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *v= dev, VirtQueue *vq) if (!elem) { break; } + s->queue_inuse[VIRTIO_SND_VQ_RX] +=3D 1; /* get the message hdr object */ msg_sz =3D iov_to_buf(elem->out_sg, elem->out_num, @@ -1192,6 +1219,7 @@ static inline void return_tx_buffer(VirtIOSoundPCMStr= eam *stream, virtqueue_push(buffer->vq, buffer->elem, sizeof(virtio_snd_pcm_status)); + stream->s->queue_inuse[VIRTIO_SND_VQ_TX] -=3D 1; virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq); QSIMPLEQ_REMOVE(&stream->queue, buffer, @@ -1283,6 +1311,7 @@ static inline void return_rx_buffer(VirtIOSoundPCMStr= eam *stream, virtqueue_push(buffer->vq, buffer->elem, sizeof(virtio_snd_pcm_status) + buffer->size); + stream->s->queue_inuse[VIRTIO_SND_VQ_RX] -=3D 1; virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq); QSIMPLEQ_REMOVE(&stream->queue, buffer, @@ -1388,6 +1417,41 @@ static void virtio_snd_unrealize(DeviceState *dev) virtio_cleanup(vdev); } =20 +static int virtio_snd_post_load(VirtIODevice *vdev) +{ + VirtIOSound *s =3D VIRTIO_SND(vdev); + uint32_t i; + + for (i =3D 0; i < s->snd_conf.streams; i++) { + struct VirtIOSoundPCMStream *stream; + + stream =3D virtio_snd_pcm_get_stream(s, i); + if (stream->state & VSND_PCMSTREAM_STATE_F_PREPARED) { + QSIMPLEQ_INIT(&stream->queue); + virtio_snd_pcm_open(stream); + + if (stream->state & VSND_PCMSTREAM_STATE_F_ACTIVE) { + virtio_snd_pcm_set_active(stream, true); + } + } + } + + for (i =3D 0; i < VIRTIO_SND_VQ_MAX; i++) { + if (s->queue_inuse[i]) { + bool rc; + + rc =3D virtqueue_rewind(s->queues[i], s->queue_inuse[i]); + if (!rc) { + error_report( + "virtio-sound: could not rewind %u elements in queue %= u", + s->queue_inuse[i], i); + } + s->queue_inuse[i] =3D 0; + } + } + + return 0; +} =20 static void virtio_snd_reset(VirtIODevice *vdev) { @@ -1412,6 +1476,7 @@ static void virtio_snd_class_init(ObjectClass *klass,= void *data) =20 dc->vmsd =3D &vmstate_virtio_snd; vdc->vmsd =3D &vmstate_virtio_snd_device; + vdc->post_load =3D virtio_snd_post_load; vdc->realize =3D virtio_snd_realize; vdc->unrealize =3D virtio_snd_unrealize; vdc->get_config =3D virtio_snd_get_config; diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h index 9b81f66b05..141e60e23c 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -200,6 +200,7 @@ struct VirtIOSound { =20 VirtQueue *queues[VIRTIO_SND_VQ_MAX]; VirtIOSoundPCMBufferSimpleQueue *invalidq[VIRTIO_SND_VQ_MAX]; + uint32_t queue_inuse[VIRTIO_SND_VQ_MAX]; uint64_t features; VirtIOSoundPCMStream *streams; QEMUSoundCard card; --=20 2.35.3 From nobody Tue Nov 26 20:25:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1704400560876803.5206697984355; Thu, 4 Jan 2024 12:36:00 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQW-0003Wl-Dc; Thu, 04 Jan 2024 15:34:52 -0500 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 1rLUQS-0003SO-BA for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:48 -0500 Received: from mailout08.t-online.de ([194.25.134.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQQ-0004LQ-PU for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:48 -0500 Received: from fwd78.aul.t-online.de (fwd78.aul.t-online.de [10.223.144.104]) by mailout08.t-online.de (Postfix) with SMTP id 4DFC42EA7; Thu, 4 Jan 2024 21:34:45 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd78.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQO-3Xzwx70; Thu, 4 Jan 2024 21:34:44 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 5F9842001D2; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 10/10] hw/audio/virtio-sound: add placeholder for buffer write position Date: Thu, 4 Jan 2024 21:34:22 +0100 Message-Id: <20240104203422.12308-10-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1704400484-5E75E979-2A84A121/0/0 CLEAN NORMAL X-TOI-MSGID: 4808f50d-700a-4bc4-a9ec-325450c8143d 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=194.25.134.20; envelope-from=volker.ruemelin@t-online.de; helo=mailout08.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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-ZM-MESSAGEID: 1704400562136100007 When a running audio stream is migrated, on average half of a recording stream buffer is lost or half of a playback stream buffer is played twice. Add a placeholder for the write position of the current stream buffer to the migrated data. Additional program code is required to resolve the above issues. However, the placeholder makes it possible to add code in a backwards and forwards compatible way. Signed-off-by: Volker R=C3=BCmelin --- hw/audio/virtio-snd.c | 2 ++ include/hw/audio/virtio-snd.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 328ee54808..71281967bf 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -83,6 +83,7 @@ static const VMStateDescription vmstate_virtio_snd_stream= =3D { .name =3D "virtio-sound-stream", .fields =3D (const VMStateField[]) { VMSTATE_UINT32(state, VirtIOSoundPCMStream), + VMSTATE_UINT32(buf_wpos, VirtIOSoundPCMStream), VMSTATE_UINT32(info.hdr.hda_fn_nid, VirtIOSoundPCMStream), VMSTATE_UINT32(info.features, VirtIOSoundPCMStream), VMSTATE_UINT64(info.formats, VirtIOSoundPCMStream), @@ -1434,6 +1435,7 @@ static int virtio_snd_post_load(VirtIODevice *vdev) virtio_snd_pcm_set_active(stream, true); } } + stream->buf_wpos =3D 0; } =20 for (i =3D 0; i < VIRTIO_SND_VQ_MAX; i++) { diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h index 141e60e23c..d46204967a 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -127,6 +127,8 @@ struct VirtIOSoundPCMStream { virtio_snd_pcm_set_params params; uint32_t id; uint32_t state; + /* placeholder: write position in current VirtIOSoundPCMBuffer */ + uint32_t buf_wpos; /* channel position values (VIRTIO_SND_CHMAP_XXX) */ uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE]; VirtIOSound *s; --=20 2.35.3