From nobody Thu Nov 14 17:41:38 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 1708245296717237.88687587759318; Sun, 18 Feb 2024 00:34:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rbcci-00037b-LB; Sun, 18 Feb 2024 03:34:08 -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 1rbccf-00036y-1H for qemu-devel@nongnu.org; Sun, 18 Feb 2024 03:34:05 -0500 Received: from mailout01.t-online.de ([194.25.134.80]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rbccc-0006K2-R5 for qemu-devel@nongnu.org; Sun, 18 Feb 2024 03:34:04 -0500 Received: from fwd85.aul.t-online.de (fwd85.aul.t-online.de [10.223.144.111]) by mailout01.t-online.de (Postfix) with SMTP id B4614659E; Sun, 18 Feb 2024 09:34:00 +0100 (CET) Received: from linpower.localnet ([79.208.24.6]) by fwd85.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rbccW-0XOtJx0; Sun, 18 Feb 2024 09:33:56 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 66C2C200378; Sun, 18 Feb 2024 09:33:51 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" Cc: qemu-devel@nongnu.org Subject: [PATCH v2 03/11] hw/audio/virtio-sound: remove command and stream mutexes Date: Sun, 18 Feb 2024 09:33:43 +0100 Message-Id: <20240218083351.8524-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::1708245236-8CB04383-EFE88149/0/0 CLEAN NORMAL X-TOI-MSGID: bde513c9-f605-49a3-a391-8863c1e11567 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.80; envelope-from=volker.ruemelin@t-online.de; helo=mailout01.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: 1708245298485100007 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 | 215 +++++++++++++++------------------- include/hw/audio/virtio-snd.h | 3 - 2 files changed, 93 insertions(+), 125 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index b87653daf4..7ed5f3de3e 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" @@ -454,7 +453,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); =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,10 +602,8 @@ static size_t virtio_snd_pcm_get_io_msgs_count(VirtIOS= oundPCMStream *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->queue, entry, next) { + count +=3D 1; } return count; } @@ -760,23 +754,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 @@ -891,18 +877,16 @@ 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: @@ -967,16 +951,14 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *v= dev, 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: @@ -1083,7 +1065,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++) { @@ -1143,50 +1124,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 @@ -1237,41 +1216,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 @@ -1288,11 +1265,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 @@ -1312,7 +1287,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); } } @@ -1323,7 +1297,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]); @@ -1337,12 +1310,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 1209b122b9..a2868067fb 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; }; @@ -219,9 +218,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