From nobody Thu Dec 18 08:06:53 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1665425472; cv=none; d=zohomail.com; s=zohoarc; b=eOb8KF6mt4ykZFLbtIV+VjPBAOvv+EiaBZ8kJBJESJwO0wHf9idWOKwCUTS0+MZ8VMp1VJV6iWlY6lPio7gpJZUHpPj0FZlw19tRXfxyLMeFchpr5bBN787pSBEclw3Cz/LmH9KJMenS2lbfhIY3v8AdEEdoeuCX/GIfeFYL1ns= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1665425472; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=a6Zgc/f/oExp1Oq3yCEWAHTzhom8dFVVHPot0BXkafs=; b=QCOIEDe+1SRZVDWWNVFfmUPFxT/vMRlZU/ola/mtaUnw+z/XOHa451xeDcgqBP+O4iNsH+WFSemD65IksQZSCZ5Xd/rTOlvJ6XhgMxGkuS8EFW2LOfJO74dxtGkTvJKgCJ2T5YlwKZqvvPeLllY6Et9z49/b9cYiMKBzArA1dyE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1665425472880136.98842379655548; Mon, 10 Oct 2022 11:11:12 -0700 (PDT) Received: from localhost ([::1]:55324 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ohxF9-0005IX-J4 for importer@patchew.org; Mon, 10 Oct 2022 14:11:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60208) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ohwbY-0002Dm-Dn for qemu-devel@nongnu.org; Mon, 10 Oct 2022 13:30:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:33469) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ohwbV-0006vQ-Bb for qemu-devel@nongnu.org; Mon, 10 Oct 2022 13:30:16 -0400 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-605-sttR7A0-NFGkqxHti4Myhg-1; Mon, 10 Oct 2022 13:30:11 -0400 Received: by mail-wr1-f70.google.com with SMTP id s4-20020adfbc04000000b0022e03fc10a9so2955539wrg.15 for ; Mon, 10 Oct 2022 10:30:11 -0700 (PDT) Received: from redhat.com ([2.55.183.131]) by smtp.gmail.com with ESMTPSA id g11-20020a5d488b000000b00228d7078c4esm9349924wrq.4.2022.10.10.10.30.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Oct 2022 10:30:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665423012; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=a6Zgc/f/oExp1Oq3yCEWAHTzhom8dFVVHPot0BXkafs=; b=JI01/RVT6liu2BoRNkNsTlOGjR2yXiHcyR2iCi2yoL9VyIpbsLU/gzLcrOFBKfoL+ZZV65 VpAxjN7jcfS9WS8fQC0/LKOEPnxxEcXc0DfI0deFdYAm6O4ZoQHvCTh7nq9tpU+lrJ+4lS jEOZnGXW1XRnhojPrOD3uiHV96G3+zQ= X-MC-Unique: sttR7A0-NFGkqxHti4Myhg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=a6Zgc/f/oExp1Oq3yCEWAHTzhom8dFVVHPot0BXkafs=; b=YplNnP4HzQl2Samgm6pPGlKgGZWbvOK6FDnGUTpa7WQFzDyNqBGNAsSOLSGhTWZPCM /2Qce8a8yqh0vGh55ZkCeMLPlt/puqnPkAhKcJPhGSZOPuSAil8x4RXKLxmDs1VmRf2O l5FwociFS3Vk4n74kzHFwVuDQ9SW3oi5LeETgtctwNxEVvbZch1keHqgziyL8y04VUdc kP1DAOitYDvmIF3WMJuM6JrT5RQjQe6maNPKzAO2wrpqHdxiME2CJ9j7TIAt1+y9cYBr /gTA7wurjMUXuK/PZIAERhp0mV9WIRQ+t8Xm6JThM1MI68325J+Qp1wyDHz74mcoaDsK T+ow== X-Gm-Message-State: ACrzQf2udxr4Y4OyLXM/xREfkvnyMf4Oy/Zwu2XkOczE8lgZ+h9fBhds 1MCPib4y1FDnZETmE4RPDWJSgJajynWQAMJ69RBL+J3SGsk0oT71s+e6aUeqnTn1nRM4srlHy7w ObZDDRRLVqu1mSfy2vM2b6hgwCwTrtvtV22vJYNFKWIguB5xbZ7cZ5f3S8jAk X-Received: by 2002:adf:d215:0:b0:22e:479e:8cc1 with SMTP id j21-20020adfd215000000b0022e479e8cc1mr13028651wrh.39.1665423010018; Mon, 10 Oct 2022 10:30:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6bfRSsNOgYba3EOI4xorECx4f7WukBzalel3I6UMI9a2VU6inI8ZKA2pvihrNADgT5VGVkVQ== X-Received: by 2002:adf:d215:0:b0:22e:479e:8cc1 with SMTP id j21-20020adfd215000000b0022e479e8cc1mr13028625wrh.39.1665423009643; Mon, 10 Oct 2022 10:30:09 -0700 (PDT) Date: Mon, 10 Oct 2022 13:30:05 -0400 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Cc: Peter Maydell , Daniil Tatianin , Raphael Norwitz , Stefan Hajnoczi , Kevin Wolf , Hanna Reitz , Jason Wang , qemu-block@nongnu.org Subject: [PULL 20/55] virtio: introduce VirtIOConfigSizeParams & virtio_get_config_size Message-ID: <20221010172813.204597-21-mst@redhat.com> References: <20221010172813.204597-1-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20221010172813.204597-1-mst@redhat.com> X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.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, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1665425473690100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Daniil Tatianin This is the first step towards moving all device config size calculation logic into the virtio core code. In particular, this adds a struct that contains all the necessary information for common virtio code to be able to calculate the final config size for a device. This is expected to be used with the new virtio_get_config_size helper, which calculates the final length based on the provided host features. This builds on top of already existing code like VirtIOFeature and virtio_feature_get_config_size(), but adds additional fields, as well as sanity checking so that device-specifc code doesn't have to duplicate it. An example usage would be: static const VirtIOFeature dev_features[] =3D { {.flags =3D 1ULL << FEATURE_1_BIT, .end =3D endof(struct virtio_dev_config, feature_1)}, {.flags =3D 1ULL << FEATURE_2_BIT, .end =3D endof(struct virtio_dev_config, feature_2)}, {} }; static const VirtIOConfigSizeParams dev_cfg_size_params =3D { .min_size =3D DEV_BASE_CONFIG_SIZE, .max_size =3D sizeof(struct virtio_dev_config), .feature_sizes =3D dev_features }; // code inside my_dev_device_realize() size_t config_size =3D virtio_get_config_size(&dev_cfg_size_params, host_features); virtio_init(vdev, VIRTIO_ID_MYDEV, config_size); Currently every device is expected to write its own boilerplate from the example above in device_realize(), however, the next step of this transition is moving VirtIOConfigSizeParams into VirtioDeviceClass, so that it can be done automatically by the virtio initialization code. All of the users of virtio_feature_get_config_size have been converted to use virtio_get_config_size so it's no longer needed and is removed with this commit. Signed-off-by: Daniil Tatianin Message-Id: <20220906073111.353245-2-d-tatianin@yandex-team.ru> Reviewed-by: Raphael Norwitz Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- include/hw/virtio/virtio.h | 10 ++++++++-- hw/block/virtio-blk.c | 16 +++++++--------- hw/net/virtio-net.c | 9 +++++++-- hw/virtio/virtio.c | 10 ++++++---- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 74e7ad5a92..cecfb7c552 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -49,8 +49,14 @@ typedef struct VirtIOFeature { size_t end; } VirtIOFeature; =20 -size_t virtio_feature_get_config_size(const VirtIOFeature *features, - uint64_t host_features); +typedef struct VirtIOConfigSizeParams { + size_t min_size; + size_t max_size; + const VirtIOFeature *feature_sizes; +} VirtIOConfigSizeParams; + +size_t virtio_get_config_size(const VirtIOConfigSizeParams *params, + uint64_t host_features); =20 typedef struct VirtQueue VirtQueue; =20 diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index e9ba752f6b..10c47c2934 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -49,13 +49,11 @@ static const VirtIOFeature feature_sizes[] =3D { {} }; =20 -static void virtio_blk_set_config_size(VirtIOBlock *s, uint64_t host_featu= res) -{ - s->config_size =3D MAX(VIRTIO_BLK_CFG_SIZE, - virtio_feature_get_config_size(feature_sizes, host_features)); - - assert(s->config_size <=3D sizeof(struct virtio_blk_config)); -} +static const VirtIOConfigSizeParams cfg_size_params =3D { + .min_size =3D VIRTIO_BLK_CFG_SIZE, + .max_size =3D sizeof(struct virtio_blk_config), + .feature_sizes =3D feature_sizes +}; =20 static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq, VirtIOBlockReq *req) @@ -1204,8 +1202,8 @@ static void virtio_blk_device_realize(DeviceState *de= v, Error **errp) return; } =20 - virtio_blk_set_config_size(s, s->host_features); - + s->config_size =3D virtio_get_config_size(&cfg_size_params, + s->host_features); virtio_init(vdev, VIRTIO_ID_BLOCK, s->config_size); =20 s->blk =3D conf->conf.blk; diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 63a8332cd0..e9f696b4cf 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -106,6 +106,12 @@ static const VirtIOFeature feature_sizes[] =3D { {} }; =20 +static const VirtIOConfigSizeParams cfg_size_params =3D { + .min_size =3D endof(struct virtio_net_config, mac), + .max_size =3D sizeof(struct virtio_net_config), + .feature_sizes =3D feature_sizes +}; + static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) { VirtIONet *n =3D qemu_get_nic_opaque(nc); @@ -3241,8 +3247,7 @@ static void virtio_net_set_config_size(VirtIONet *n, = uint64_t host_features) { virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); =20 - n->config_size =3D virtio_feature_get_config_size(feature_sizes, - host_features); + n->config_size =3D virtio_get_config_size(&cfg_size_params, host_featu= res); } =20 void virtio_net_set_netclient_name(VirtIONet *n, const char *name, diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 97a6307c0f..2cc1d7d24a 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -3006,11 +3006,12 @@ int virtio_set_features(VirtIODevice *vdev, uint64_= t val) return ret; } =20 -size_t virtio_feature_get_config_size(const VirtIOFeature *feature_sizes, - uint64_t host_features) +size_t virtio_get_config_size(const VirtIOConfigSizeParams *params, + uint64_t host_features) { - size_t config_size =3D 0; - int i; + size_t config_size =3D params->min_size; + const VirtIOFeature *feature_sizes =3D params->feature_sizes; + size_t i; =20 for (i =3D 0; feature_sizes[i].flags !=3D 0; i++) { if (host_features & feature_sizes[i].flags) { @@ -3018,6 +3019,7 @@ size_t virtio_feature_get_config_size(const VirtIOFea= ture *feature_sizes, } } =20 + assert(config_size <=3D params->max_size); return config_size; } =20 --=20 MST