From patchwork Mon Apr 22 12:52:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13638505 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E5DB9C4345F for ; Mon, 22 Apr 2024 12:53:01 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rytAE-0001gJ-U8; Mon, 22 Apr 2024 08:52:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rytA6-0001Ys-2s for qemu-devel@nongnu.org; Mon, 22 Apr 2024 08:52:47 -0400 Received: from mail-lj1-x233.google.com ([2a00:1450:4864:20::233]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rytA4-0006Ol-EQ for qemu-devel@nongnu.org; Mon, 22 Apr 2024 08:52:45 -0400 Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-2dd615b6c44so14036941fa.0 for ; Mon, 22 Apr 2024 05:52:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713790362; x=1714395162; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=016dPan8iD0jaVU8GbAMdou82zyqcKjvvzowQZ1c5wk=; b=VreUoioiBEbZvE/gNOfb1M3pCEcXJXOS+bYCaQOh4OqEdVdG7bjrXbgWZyvGaIhrpG DeG57aSXF4irN2rdGERL4KaCCeYwebbUd/0rlIbHHWJ8XLRhpBN865iBpGZEgXpcR7Ls Le1Kq5cWm76sY/S1Q4T+QmPpanVfmRoMfVOyQTccdYFe54FQGn/79y4C3EFfceIMtb7a l8CQcJjFRxYnagu1H6WM7NPH3aU70exfi6GpfrHtB9lLDL4yTC+oZ2fQexXg+sUFQHd0 h77NvW6v0MTQ7Ed3+O2JFhmbNTUcGnq5wypAIW1voK2qCnU5K94bvgHQphkcccAYyjiN mU/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713790362; x=1714395162; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=016dPan8iD0jaVU8GbAMdou82zyqcKjvvzowQZ1c5wk=; b=Ry7T9D1ccMjcvO171B0g7V5hZR9iC0XZ+cfsOXP917vC7tEJtgJo3ppMFssHKIouq4 wNqX2E/S4F8G/eE3xyf4rIkr/Bm+gCflJ4mKxLY8eQbBfiQpofsR3WsmHt7mRa4cIevD eqkiXjy/kesXVnSIIp+WzcO23PjacTV0TUc9V6KBHzfl1OG0raJ6iF9AMUsmpLOzHyeC tESlU9v9zUnJtWIMKUyYg5a0dvGx4t+TKSHUuLEBW54MBWpvfuP0L31kS+KSBYuS6Ps5 bMlypLIsuDWo8KIyM6Mf+EY5GyYLliH/RTwj85S75+z+Qo+W48J2pbmCRYjnOh7O7elq NELw== X-Gm-Message-State: AOJu0YwqFIfoWJWkstKVkJNp3D+vamI7BBPZ04oCzSo6RZtFIPNm3B5v fmdY+FQNIf3JMLkhzaRDhHKnGkAw95WJgw7vIy9TxyNtlsy79LHZnQlh4W4INqYIwI6GE8p/2qH v X-Google-Smtp-Source: AGHT+IFN99FDses3uFukVhJUkq6/1Lb3OUcMf2Qh6An4MVy2EryRsR68TDkdYvRHy5nAfX10LSjwbA== X-Received: by 2002:ac2:4bc3:0:b0:518:91fd:b343 with SMTP id o3-20020ac24bc3000000b0051891fdb343mr7364183lfq.68.1713790362127; Mon, 22 Apr 2024 05:52:42 -0700 (PDT) Received: from localhost.localdomain (adsl-53.37.6.0.tellas.gr. [37.6.0.53]) by smtp.gmail.com with ESMTPSA id l10-20020a1709066b8a00b00a55bfb4e3b6sm593574ejr.217.2024.04.22.05.52.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 05:52:41 -0700 (PDT) From: Manos Pitsidianakis To: qemu-devel@nongnu.org, qemu-stable@nongnu.org Cc: Gerd Hoffmann , "Michael S. Tsirkin" , Zheyu Ma , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v1 1/4] virtio-snd: add virtio_snd_is_config_valid() Date: Mon, 22 Apr 2024 15:52:11 +0300 Message-ID: <491c651b075ac51f1c54f561bebaaac6dfc7f8de.1713789200.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::233; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-lj1-x233.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Factor out virtio_snd_config value validation in a separate function, in order to re-use it in follow up commits. Signed-off-by: Manos Pitsidianakis --- hw/audio/virtio-snd.c | 47 ++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index c80b58bf5d..7ca9ed251c 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -1045,6 +1045,34 @@ virtio_snd_vm_state_change(void *opaque, bool running, } } +static bool +virtio_snd_is_config_valid(virtio_snd_config snd_conf, Error **errp) +{ + if (snd_conf.jacks > 8) { + error_setg(errp, + "Invalid number of jacks: %"PRIu32 + ": maximum value is 8", snd_conf.jacks); + return false; + } + if (snd_conf.streams < 1 || snd_conf.streams > 64) { + error_setg(errp, + "Invalid number of streams: %"PRIu32 + ": minimum value is 1, maximum value is 64", + snd_conf.streams); + return false; + } + + if (snd_conf.chmaps > VIRTIO_SND_CHMAP_MAX_SIZE) { + error_setg(errp, + "Invalid number of channel maps: %"PRIu32 + ": VIRTIO v1.2 sets the maximum value at %"PRIu32, + snd_conf.chmaps, VIRTIO_SND_CHMAP_MAX_SIZE); + return false; + } + + return true; +} + static void virtio_snd_realize(DeviceState *dev, Error **errp) { ERRP_GUARD(); @@ -1055,24 +1083,7 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) trace_virtio_snd_realize(vsnd); - /* check number of jacks and streams */ - if (vsnd->snd_conf.jacks > 8) { - error_setg(errp, - "Invalid number of jacks: %"PRIu32, - vsnd->snd_conf.jacks); - return; - } - if (vsnd->snd_conf.streams < 1 || vsnd->snd_conf.streams > 10) { - error_setg(errp, - "Invalid number of streams: %"PRIu32, - vsnd->snd_conf.streams); - return; - } - - if (vsnd->snd_conf.chmaps > VIRTIO_SND_CHMAP_MAX_SIZE) { - error_setg(errp, - "Invalid number of channel maps: %"PRIu32, - vsnd->snd_conf.chmaps); + if (!virtio_snd_is_config_valid(vsnd->snd_conf, errp)) { return; } From patchwork Mon Apr 22 12:52:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13638508 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F0DDBC07E8E for ; Mon, 22 Apr 2024 12:53:44 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rytAI-0001pj-H6; Mon, 22 Apr 2024 08:52:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rytAA-0001du-3A for qemu-devel@nongnu.org; Mon, 22 Apr 2024 08:52:50 -0400 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rytA8-0006Pw-CA for qemu-devel@nongnu.org; Mon, 22 Apr 2024 08:52:49 -0400 Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-56e69a51a33so3971687a12.1 for ; Mon, 22 Apr 2024 05:52:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713790366; x=1714395166; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wtIOp9fwmceyQgZZl5DZ5tv3mHejfW4oGwssHFQ9/1A=; b=GiuqhOOKYGFyjWJz5kJf8rdUhM3A3dP15lftwyZqeYDYuWIRd5O5XlCF5p+YOtvfd9 P+9Ca7kcBMRhf1VdNfs4CW6fESpZ0dslkexcYnTV7kAWMrO7zWDoatpBAG2adjKsMfu9 +EOu/Ke2o5R0tMtGW6bfjTfLTO+Ni8ZKTMqTLkw8hukKUyvDCrTH0C9CreERMSv+Hj7+ WijlH5mFrepxwjkU4Ns0p0M4tMmeMf7DBxeYd1usKMVLU1+fgDgD6hnSqQgVSVxmrgZ3 OfdHLb0m2ZMDVqIOesmDYbPVLPnqdRScgstjSeqeFAB9ksNLRuvFsa4eIjS8jkTXE6CJ Skfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713790366; x=1714395166; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wtIOp9fwmceyQgZZl5DZ5tv3mHejfW4oGwssHFQ9/1A=; b=HrvXn+zK3miFt4CBhSPC71pMf6h/Wd7XmEqbtxl2ludsCfOz3eoH1IrqUXE5Xb66q6 L+Zy+qKRCV6oJlS/FZDMlBG+G+ax0bVu40WwO20+Na5McAiIGS/3CUo0vVSpQbdwyrox JUVyp2udKBHUnMyNs5d72pIte9yOXtdj5ObPHy8atibf4UXyyFYJt5YU3Y99VIZEyntu 8v1SQnxZeOsJQm5trv3zxPBHl5ARj4X6r16FKvJYlFXzjywHKyvBLeqVStr8TpEvfpFJ JMdBJuPV4qRFsEwlkR3uC2YT2ZA/tfs84ygUZIi3od/uRquB5xTEskV/+M1VLH6aMDlI BT4w== X-Gm-Message-State: AOJu0YxhamQmNybDwkApEDhrUqFNgLVOsSHV8xDBkl6AL/yl55PPmP7v y1IyiC7PY0qCBbplhPivHp+8L8mnTyf5lblqn5ZW77UTpGK228RKhhZM2jG4t+IUz1IWzWqinIG m X-Google-Smtp-Source: AGHT+IHbg2QxQMal/J5vECQD3Nyxycu/iAMpYrikqRSLeFjGXcG1XDwvlf0gdZx2hXB6CVFno2zP4Q== X-Received: by 2002:a17:906:b117:b0:a52:671a:aaf2 with SMTP id u23-20020a170906b11700b00a52671aaaf2mr7136278ejy.72.1713790365901; Mon, 22 Apr 2024 05:52:45 -0700 (PDT) Received: from localhost.localdomain (adsl-53.37.6.0.tellas.gr. [37.6.0.53]) by smtp.gmail.com with ESMTPSA id l10-20020a1709066b8a00b00a55bfb4e3b6sm593574ejr.217.2024.04.22.05.52.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 05:52:45 -0700 (PDT) From: Manos Pitsidianakis To: qemu-devel@nongnu.org, qemu-stable@nongnu.org Cc: Gerd Hoffmann , "Michael S. Tsirkin" , Zheyu Ma , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v1 2/4] virtio-snd: factor card setup out of realize func Date: Mon, 22 Apr 2024 15:52:12 +0300 Message-ID: <62de737e3320e647454a306bb87c6eb20a2c0cfb.1713789200.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::52b; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-ed1-x52b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Extract audio card setup logic out of the device realize callback so that it can be re-used in follow up commits. Signed-off-by: Manos Pitsidianakis --- hw/audio/virtio-snd.c | 72 ++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 7ca9ed251c..82dd320ebe 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -1073,27 +1073,21 @@ virtio_snd_is_config_valid(virtio_snd_config snd_conf, Error **errp) return true; } -static void virtio_snd_realize(DeviceState *dev, Error **errp) +/* Registers card and sets up streams according to configuration. */ +static bool virtio_snd_setup(VirtIOSound *vsnd, Error **errp) { ERRP_GUARD(); - VirtIOSound *vsnd = VIRTIO_SND(dev); - VirtIODevice *vdev = VIRTIO_DEVICE(dev); virtio_snd_pcm_set_params default_params = { 0 }; uint32_t status; - trace_virtio_snd_realize(vsnd); - if (!virtio_snd_is_config_valid(vsnd->snd_conf, errp)) { - return; + return false; } if (!AUD_register_card("virtio-sound", &vsnd->card, errp)) { - return; + return false; } - vsnd->vmstate = - qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd); - vsnd->pcm = g_new0(VirtIOSoundPCM, 1); vsnd->pcm->snd = vsnd; vsnd->pcm->streams = @@ -1101,9 +1095,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) vsnd->pcm->pcm_params = g_new0(virtio_snd_pcm_set_params, vsnd->snd_conf.streams); - virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config)); - virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1); - /* set default params for all streams */ default_params.features = 0; default_params.buffer_bytes = cpu_to_le32(8192); @@ -1111,6 +1102,41 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) default_params.channels = 2; default_params.format = VIRTIO_SND_PCM_FMT_S16; default_params.rate = VIRTIO_SND_PCM_RATE_48000; + + for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) { + status = virtio_snd_set_pcm_params(vsnd, i, &default_params); + if (status != cpu_to_le32(VIRTIO_SND_S_OK)) { + error_setg(errp, + "Can't initialize stream params, device responded with %s.", + print_code(status)); + return false; + } + status = virtio_snd_pcm_prepare(vsnd, i); + if (status != cpu_to_le32(VIRTIO_SND_S_OK)) { + error_setg(errp, + "Can't prepare streams, device responded with %s.", + print_code(status)); + return false; + } + } + + return true; +} + +static void virtio_snd_realize(DeviceState *dev, Error **errp) +{ + ERRP_GUARD(); + VirtIOSound *vsnd = VIRTIO_SND(dev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + + trace_virtio_snd_realize(vsnd); + + vsnd->vmstate = + qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd); + + virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config)); + virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1); + vsnd->queues[VIRTIO_SND_VQ_CONTROL] = virtio_add_queue(vdev, 64, virtio_snd_handle_ctrl); vsnd->queues[VIRTIO_SND_VQ_EVENT] = @@ -1123,26 +1149,10 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) QTAILQ_INIT(&vsnd->cmdq); QSIMPLEQ_INIT(&vsnd->invalid); - for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) { - status = virtio_snd_set_pcm_params(vsnd, i, &default_params); - if (status != cpu_to_le32(VIRTIO_SND_S_OK)) { - error_setg(errp, - "Can't initialize stream params, device responded with %s.", - print_code(status)); - goto error_cleanup; - } - status = virtio_snd_pcm_prepare(vsnd, i); - if (status != cpu_to_le32(VIRTIO_SND_S_OK)) { - error_setg(errp, - "Can't prepare streams, device responded with %s.", - print_code(status)); - goto error_cleanup; - } + if (virtio_snd_setup(vsnd, errp)) { + return; } - return; - -error_cleanup: virtio_snd_unrealize(dev); } From patchwork Mon Apr 22 12:52:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13638506 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 93CF3C4345F for ; Mon, 22 Apr 2024 12:53:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rytAJ-0001tU-Gk; Mon, 22 Apr 2024 08:52:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rytAC-0001gK-60 for qemu-devel@nongnu.org; Mon, 22 Apr 2024 08:52:53 -0400 Received: from mail-ej1-x62a.google.com ([2a00:1450:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rytAA-0006Rg-Jh for qemu-devel@nongnu.org; Mon, 22 Apr 2024 08:52:51 -0400 Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-a52582ecde4so397775366b.0 for ; Mon, 22 Apr 2024 05:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713790368; x=1714395168; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c44BPLf1Z01NnuVhiMEI0qHANuUe6ukUP9XCsdX0lnk=; b=Qu01Zx1Y6xmnvTLH5mrX4ESSJEDVWpni7DDJgj8Ab2rNBKMA3XvAfQrZe0ClsLUJfW Mz6eIev7DKcZZB16qz6pfmQfRjaVKUboIXWe+EmrQwQVYVRV9qM82xowzUHTmxpcl92j aMW6yEcX9FHqGoj55zEUqCd+/TR30rfu6RhreCKHjpUpQ9bsuSlZcHnTwrTPKIZleM+a +ltGEGCzzxCy3078Aa5a0vZuM6HGHTrPCIfslThNhYGZUlKzs6iHYi/DK/5w1WR1g+UJ fF27ANMPZdJ68NlQsU9VpFqkMdEcuJjMzK4yQ3vTqzoI7EFEphOv5TVUvhX2N3wwUc0b +xDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713790368; x=1714395168; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c44BPLf1Z01NnuVhiMEI0qHANuUe6ukUP9XCsdX0lnk=; b=EaFtoU/mOTNlcpCp5vsYtH1WvuYo5Gogc1I/F3DpVy+fjunVN/7l9vEA0tcMDdpqMa jAiAG7Czjb6XWb/UMjQ6n5JR7wOPJMMLTFGzfUsgnOzVqQT7a5WNeOZkrOgpwCtd3UnI tsfEz1uibAPFyc6L+ssJih2jw2UdTFkH53ACuYf6TJg7h9osEBHQaINz/D/rWG0exzUK MDhFi5n6TO2KzybwRwp3Cryx+hw/82s889MZBSWvhiAhKxbUT+nKbke1U5BM0dkJsbrw CXkrrKoocF5AORwXTn00/drx8NTRtRucHXHh4cVGvEkJ1qFWwEGXE2unLXceC0Tew2lB S52w== X-Gm-Message-State: AOJu0YwOcNVg7iwx6VB0lv407lmNHx/1jp/V/pvslWVQsC1KzOcecToF +uyWXPWSqfCC/q9151dXi9BW4x+jBc0L+Thzwbxh+IEGITXXrNZjn30l2Bp9gt0RJkS+a+uSn20 6 X-Google-Smtp-Source: AGHT+IHmc571ZQcgoOY4LtDDjS88Bv5wTliE2gTONxVrPXOUIHum4ymmxpJkXlUnq+5Fu/DEybIk2w== X-Received: by 2002:a17:906:5f89:b0:a55:5ba6:ace9 with SMTP id a9-20020a1709065f8900b00a555ba6ace9mr6214121eju.9.1713790368772; Mon, 22 Apr 2024 05:52:48 -0700 (PDT) Received: from localhost.localdomain (adsl-53.37.6.0.tellas.gr. [37.6.0.53]) by smtp.gmail.com with ESMTPSA id l10-20020a1709066b8a00b00a55bfb4e3b6sm593574ejr.217.2024.04.22.05.52.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 05:52:48 -0700 (PDT) From: Manos Pitsidianakis To: qemu-devel@nongnu.org, qemu-stable@nongnu.org Cc: Gerd Hoffmann , "Michael S. Tsirkin" , Zheyu Ma , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v1 3/4] virtio-snd: factor card removal out of unrealize() Date: Mon, 22 Apr 2024 15:52:13 +0300 Message-ID: <2231ee9229ba7259763c9814e6ec119d8954b81c.1713789200.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::62a; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-ej1-x62a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Extract audio card removal logic out of the device unrealize callback so that it can be re-used in follow up commits. Signed-off-by: Manos Pitsidianakis --- hw/audio/virtio-snd.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 82dd320ebe..a9cfaea046 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -1343,15 +1343,11 @@ static inline void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream) } } -static void virtio_snd_unrealize(DeviceState *dev) +/* Remove audio card and cleanup streams. */ +static void virtio_snd_unsetup(VirtIOSound *vsnd) { - VirtIODevice *vdev = VIRTIO_DEVICE(dev); - VirtIOSound *vsnd = VIRTIO_SND(dev); VirtIOSoundPCMStream *stream; - qemu_del_vm_change_state_handler(vsnd->vmstate); - trace_virtio_snd_unrealize(vsnd); - if (vsnd->pcm) { if (vsnd->pcm->streams) { for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) { @@ -1370,6 +1366,18 @@ static void virtio_snd_unrealize(DeviceState *dev) vsnd->pcm = NULL; } AUD_remove_card(&vsnd->card); +} + +static void virtio_snd_unrealize(DeviceState *dev) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOSound *vsnd = VIRTIO_SND(dev); + + qemu_del_vm_change_state_handler(vsnd->vmstate); + trace_virtio_snd_unrealize(vsnd); + + virtio_snd_unsetup(vsnd); + qemu_mutex_destroy(&vsnd->cmdq_mutex); virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_CONTROL]); virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_EVENT]); From patchwork Mon Apr 22 12:52:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13638507 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C126BC4345F for ; Mon, 22 Apr 2024 12:53:42 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rytAK-0001vc-03; Mon, 22 Apr 2024 08:53:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rytAH-0001ly-Lh for qemu-devel@nongnu.org; Mon, 22 Apr 2024 08:52:57 -0400 Received: from mail-ed1-x52c.google.com ([2a00:1450:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rytAF-0006Um-Mz for qemu-devel@nongnu.org; Mon, 22 Apr 2024 08:52:57 -0400 Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-57217644ba1so206492a12.2 for ; Mon, 22 Apr 2024 05:52:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713790373; x=1714395173; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8hCHqhMxNEfKL+VNLDziQLRb3vVkMKAX1ZPlFQlO+/E=; b=e9LfsEo/hjd+LRGVFfjvxFIIVx5wMHtxT3vbTn4kXWCyuU+JbyrIkubsu3ZIe52qK2 vNe0yK70DgbTROqoGxg6yIbnT1OyYQDiBITvhKk0nV0or3vQ7oTqXwSg9MTcEaLKROz9 EWLkXYRrSYXttKcipZS7WPcmCSJf2M7G9lgh6Q2GRqNsMyCUjHRgF95dXaGnKysUx+QO oxwE98i1V1QzhBK0sx6TVTmgqBkp4UBJwNmxSzNeQwcMcjGJazW3lyx3Mrsh5QHv7RXS sbpRDDNmkzTXAbLNBaVpZtkrermYMzP/93VR3w+bTzuFEo2JPL2tPD3XRxlpKkxWaEkL nIRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713790373; x=1714395173; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8hCHqhMxNEfKL+VNLDziQLRb3vVkMKAX1ZPlFQlO+/E=; b=d2mjHjqlwl69WMi2HHcOrhFOZBEqb9mqQVIzi5bapEQnp59jcx8QcUsQqBgNU4bS/5 7OWcAeW0GiMHmRmWwhix1EuVPx4MgdX4oKMsVw2o6q+wHsi/0gnVaOxmK29FyrW2N1BQ 9MRHqonWvyR6WPc15z15iHblF2dJg2RecxIDuOiwWqmG7knsfmxFJwgcMHYppq7ykMnn rsI8lK8pkZAOANjQL1YJiEzn7RAxVKNB5a8ljJt1Mu1pfV2Jrpkdti4S5Y3sei0i/Pnx A3fzm1+igs3ZxaYk9h8zpH3s7Y90adN7qqvU4aZ6/J/k8XvVb6m9XCldp3zAUqLYKxbG hZUg== X-Gm-Message-State: AOJu0YyKBprK6/LqM3qdOJXNlOB3JbX29SKIf4EgHQwddDjKVJyXKmBk 5PLxtuZC40sf3CZN8qujyli3817nn5xWwfxLEMSh9hjuLWYLDYfJAaKa99u5cpFXRD155f2+tHq 9 X-Google-Smtp-Source: AGHT+IFbCiXVIL1jL8GFULyYTFqB1od2FCqbxp4ISwMFew6KZuoSf3RKU9OJWwYD61uDny4DPpkG7w== X-Received: by 2002:a17:906:1949:b0:a52:5925:1f76 with SMTP id b9-20020a170906194900b00a5259251f76mr5863700eje.47.1713790373264; Mon, 22 Apr 2024 05:52:53 -0700 (PDT) Received: from localhost.localdomain (adsl-53.37.6.0.tellas.gr. [37.6.0.53]) by smtp.gmail.com with ESMTPSA id l10-20020a1709066b8a00b00a55bfb4e3b6sm593574ejr.217.2024.04.22.05.52.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 05:52:53 -0700 (PDT) From: Manos Pitsidianakis To: qemu-devel@nongnu.org, qemu-stable@nongnu.org Cc: Gerd Hoffmann , "Michael S. Tsirkin" , Zheyu Ma , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v1 4/4] virtio_snd_set_config: validate and re-setup card Date: Mon, 22 Apr 2024 15:52:14 +0300 Message-ID: <4556f526055b140d949189d78e7c46b261302373.1713789200.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::52c; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-ed1-x52c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Validate new configuration values and re-setup audio card. Changing the number of streams via virtio_snd_set_config() did not re-configure the audio card, leaving it in an invalid state. This can be demonstrated by this heap buffer overflow: ```shell cat << EOF | qemu-system-x86_64 -display none \ -machine accel=qtest -m 512M -machine q35 -device \ virtio-sound,audiodev=my_audiodev,streams=2 -audiodev \ alsa,id=my_audiodev -qtest stdio outl 0xcf8 0x80001804 outw 0xcfc 0x06 outl 0xcf8 0x80001820 outl 0xcfc 0xe0008000 write 0xe0008016 0x1 0x03 write 0xe0008020 0x4 0x00901000 write 0xe0008028 0x4 0x00a01000 write 0xe000801c 0x1 0x01 write 0xe000a004 0x1 0x40 write 0x10c000 0x1 0x02 write 0x109001 0x1 0xc0 write 0x109002 0x1 0x10 write 0x109008 0x1 0x04 write 0x10a002 0x1 0x01 write 0xe000b00d 0x1 0x00 EOF ``` When built with `--enable-sanitizers`, QEMU prints this error: ERROR: AddressSanitizer: heap-buffer-overflow [..snip..] in virtio_snd_handle_rx_xfer(). Closes #2296. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2296 Reported-by: Zheyu Ma Suggested-by: Zheyu Ma Signed-off-by: Manos Pitsidianakis --- hw/audio/virtio-snd.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index a9cfaea046..d47af2ed69 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -36,7 +36,11 @@ static void virtio_snd_pcm_out_cb(void *data, int available); static void virtio_snd_process_cmdq(VirtIOSound *s); static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream); static void virtio_snd_pcm_in_cb(void *data, int available); +static bool virtio_snd_setup(VirtIOSound *vsnd, Error **errp); +static void virtio_snd_unsetup(VirtIOSound *vsnd); static void virtio_snd_unrealize(DeviceState *dev); +static bool virtio_snd_is_config_valid(virtio_snd_config snd_conf, + Error **errp); static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8) | BIT(VIRTIO_SND_PCM_FMT_U8) @@ -111,23 +115,26 @@ static void virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config) { VirtIOSound *s = VIRTIO_SND(vdev); - const virtio_snd_config *sndconfig = - (const virtio_snd_config *)config; + virtio_snd_config new_value = + *(const virtio_snd_config *)config; + le32_to_cpus(&new_value.jacks); + le32_to_cpus(&new_value.streams); + le32_to_cpus(&new_value.chmaps); - trace_virtio_snd_set_config(vdev, - s->snd_conf.jacks, - sndconfig->jacks, - s->snd_conf.streams, - sndconfig->streams, - s->snd_conf.chmaps, - sndconfig->chmaps); - - memcpy(&s->snd_conf, sndconfig, sizeof(virtio_snd_config)); - le32_to_cpus(&s->snd_conf.jacks); - le32_to_cpus(&s->snd_conf.streams); - le32_to_cpus(&s->snd_conf.chmaps); + trace_virtio_snd_set_config(vdev, + s->snd_conf.jacks, + new_value.jacks, + s->snd_conf.streams, + new_value.streams, + s->snd_conf.chmaps, + new_value.chmaps); + if (virtio_snd_is_config_valid(new_value, &error_warn)) { + virtio_snd_unsetup(s); + s->snd_conf = new_value; + virtio_snd_setup(s, &error_fatal); + } } static void