From mboxrd@z Thu Jan 1 00:00:00 1970 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 92AEFC433FE for ; Mon, 21 Nov 2022 22:46:40 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oxFYb-0007i0-7v; Mon, 21 Nov 2022 17:46:29 -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 1oxFYW-0007fM-Rh for qemu-devel@nongnu.org; Mon, 21 Nov 2022 17:46:25 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oxFYU-0004zM-1U for qemu-devel@nongnu.org; Mon, 21 Nov 2022 17:46:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669070780; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OGcYMvlxXlDEAU+H6Z6jwQrdClGLLvEelOnkofbS1bY=; b=GvRe2Gv9aBaf89veEg+qFseFRhivYZKx2Z9SiEyeeM9ZxcKWKreY34URJYCRjAE/2NOMbY KkshvEJXuCJm5Ig/w6xYW/fMsrlr8odq8wwC+1wZptOpTtMNX8hxAg7i5dnXmM1z7O0cxa lpaYpNposa2TF9CzimMTNstauqlrkSw= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-675-BiSz7FQpOcm45PJRqERsog-1; Mon, 21 Nov 2022 17:46:19 -0500 X-MC-Unique: BiSz7FQpOcm45PJRqERsog-1 Received: by mail-wm1-f69.google.com with SMTP id h4-20020a1c2104000000b003d01b66fe65so2975516wmh.2 for ; Mon, 21 Nov 2022 14:46:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding: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=OGcYMvlxXlDEAU+H6Z6jwQrdClGLLvEelOnkofbS1bY=; b=Hqn+H7bjws3MRqLFDXN+u1wiMivCcPuLA4oxVMtOEL9vXkHJq1VYkXcQ+lEOrk+V0G hYl2ue3j7nuGhnRL4pl41TVrUKyzFNKBa8x05oG/ACXqn3xCJfHNjf3K2CMD59OLn3qy FrII9bF4j24K8npnk6Vb+3nwcSz+N88lmjzvAs5uPXkeC/5x8b6LlbB4azSvomn4E2cS hmSCfZgTPZA7e0sYbxuEkyX6zAwlbhY22MvhsbiVrQsyMFLmlA3Pd5xzircOwGR8hhdr xKbzfQXAhnrNmwABZOv4kfA/HlFrP0er/UsUNz6qbtjJMHQYfmpA+g9AwvrNLvj37G8q DE1w== X-Gm-Message-State: ANoB5pnHiiBfIwvc3GGO5MiPsqGr3tvIFPN9BXqZwWbYSSg2NArC50LB WTBhal1g6obVeOb8txDe2aStbokrLJUn4+JD4km8GHuljijktXRV1D95UD5bggIRqQscBJyPBWy tgp2Gf9MbJkQjcXI= X-Received: by 2002:a05:6000:181:b0:241:c6f9:3e5a with SMTP id p1-20020a056000018100b00241c6f93e5amr3073181wrx.157.1669070778267; Mon, 21 Nov 2022 14:46:18 -0800 (PST) X-Google-Smtp-Source: AA0mqf4+SaFMgPzIIxsww/YeBKjuiqa30LdWacfyqNsC6n4MYygPk9N95J5/EQ9wZz3xGBxbLNe8WA== X-Received: by 2002:a05:6000:181:b0:241:c6f9:3e5a with SMTP id p1-20020a056000018100b00241c6f93e5amr3073168wrx.157.1669070778017; Mon, 21 Nov 2022 14:46:18 -0800 (PST) Received: from redhat.com ([2.52.21.254]) by smtp.gmail.com with ESMTPSA id r4-20020a05600c35c400b003c6b874a0dfsm17732431wmq.14.2022.11.21.14.46.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Nov 2022 14:46:17 -0800 (PST) Date: Mon, 21 Nov 2022 17:46:14 -0500 From: "Michael S. Tsirkin" To: Stefan Hajnoczi Cc: Alex =?iso-8859-1?Q?Benn=E9e?= , qemu-devel@nongnu.org, Stefano Garzarella Subject: Re: [RFC PATCH] include/hw: attempt to document VirtIO feature variables (!DISCUSS!) Message-ID: <20221121173800-mutt-send-email-mst@kernel.org> References: <20221121144921.2830330-1-alex.bennee@linaro.org> <87a64kcdpk.fsf@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: 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, RCVD_IN_MSPIKE_H2=-0.001, 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 On Mon, Nov 21, 2022 at 04:51:40PM -0500, Stefan Hajnoczi wrote: > On Mon, 21 Nov 2022 at 14:25, Alex Bennée wrote: > > Stefan Hajnoczi writes: > > > > > On Mon, 21 Nov 2022 at 09:49, Alex Bennée wrote: > > >> > > >> We have a bunch of variables associated with the device and the vhost > > >> backend which are used inconsistently throughout the code base. Lets > > >> start trying to bring some order by agreeing what each variable is > > >> for. Some cases to address (vho/vio renames to avoid ambiguous results > > >> while grepping): > > >> > > >> virtio->guest_features is mostly the live status of the features field > > >> and read and written as such by the guest. It does get manipulated by > > >> the various load state via virtio_set_features_nocheck(vdev, val) for > > >> migration. > > >> > > >> virtio->host_features is the result of vcd->get_features() most of the > > >> time and for vhost-user devices eventually ends up down at the vhost > > >> get features message: > > >> > > >> ./hw/virtio/virtio-bus.c:66: vdev->host_features = vdc->get_features(vdev, vdev->host_features, > > >> > > >> However virtio-net does a lot of direct modification of it: > > >> > > >> ./hw/net/virtio-net.c:3517: n->host_features |= (1ULL << VIRTIO_NET_F_MTU); > > >> ./hw/net/virtio-net.c:3529: n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); > > >> ./hw/net/virtio-net.c:3539: n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); > > >> ./hw/net/virtio-net.c:3548: n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY); > > >> ./hw/virtio/virtio.c:3438: bool bad = (val & ~(vdev->host_features)) != 0; > > >> > > >> And we have this case which propagates the global QMP values for the > > >> device to the host features. This causes the resent regression of > > >> vhost-user-sock due to 69e1c14aa2 (virtio: core: vq reset feature > > >> negotation support) because the reset feature was rejected by the > > >> vhost-user backend causing it to freeze: > > >> > > >> ./hw/virtio/virtio.c:4667: status->host_features = qmp_decode_features(vdev->device_id, > > >> > > >> virtio->backend_features is only used by virtio-net to stash the > > >> vhost_net_get_features features for checking later: > > >> > > >> features = vhost_net_get_features(get_vhost_net(nc->peer), features); > > >> vdev->vio_backend_features = features; > > >> > > >> and: > > >> > > >> if (n->mtu_bypass_backend && > > >> !virtio_has_feature(vdev->vio_backend_features, VIRTIO_NET_F_MTU)) { > > >> features &= ~(1ULL << VIRTIO_NET_F_MTU); > > >> } > > >> > > >> vhost_dev->acked_features seems to mostly reflect > > >> virtio->guest_features (but where in the negotiation cycle?). Except > > >> for vhost_net where is becomes vhost_dev->backend_features > > >> > > >> ./backends/vhost-user.c:87: b->dev.vho_acked_features = b->vdev->guest_features; > > >> ./hw/block/vhost-user-blk.c:149: s->dev.vho_acked_features = vdev->guest_features; > > >> ./hw/net/vhost_net.c:132: net->dev.vho_acked_features = net->dev.vho_backend_features; > > >> ./hw/scsi/vhost-scsi-common.c:53: vsc->dev.vho_acked_features = vdev->guest_features; > > >> ./hw/virtio/vhost-user-fs.c:77: fs->vhost_dev.vho_acked_features = vdev->guest_features; > > >> ./hw/virtio/vhost-user-i2c.c:46: i2c->vhost_dev.vho_acked_features = vdev->guest_features; > > >> ./hw/virtio/vhost-user-rng.c:44: rng->vhost_dev.vho_acked_features = vdev->guest_features; > > >> ./hw/virtio/vhost-vsock-common.c:71: vvc->vhost_dev.vho_acked_features = vdev->guest_features; > > >> ./hw/virtio/vhost.c:1631: hdev->vho_acked_features |= bit_mask; > > >> > > >> vhost_dev->backend_features has become overloaded with two use cases: > > >> > > >> ./hw/block/vhost-user-blk.c:336: s->dev.vho_backend_features = 0; > > >> ./hw/net/vhost_net.c:180: net->dev.vho_backend_features = qemu_has_vnet_hdr(options->net_backend) > > >> ./hw/net/vhost_net.c:185: net->dev.vho_backend_features = 0; > > >> ./hw/scsi/vhost-scsi.c:220: vsc->dev.vho_backend_features = 0; > > >> ./hw/scsi/vhost-user-scsi.c:121: vsc->dev.vho_backend_features = 0; > > >> ./hw/virtio/vhost-user.c:2083: dev->vho_backend_features |= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES; > > >> One use for saving the availability of a vhost-net feature and another > > >> for ensuring we add the protocol feature negotiation bit when querying > > >> a vhost backend. Maybe the places where this is queried should really > > >> be bools that can be queried in the appropriate places? > > >> > > >> Signed-off-by: Alex Bennée > > >> Cc: Stefano Garzarella > > >> Cc: "Michael S. Tsirkin" > > >> Cc: Stefan Hajnoczi > > >> --- > > >> include/hw/virtio/vhost.h | 18 +++++++++++++++--- > > >> include/hw/virtio/virtio.h | 20 +++++++++++++++++++- > > >> 2 files changed, 34 insertions(+), 4 deletions(-) > > >> > > >> diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h > > >> index 353252ac3e..502aa5677a 100644 > > >> --- a/include/hw/virtio/vhost.h > > >> +++ b/include/hw/virtio/vhost.h > > >> @@ -88,13 +88,25 @@ struct vhost_dev { > > >> int vq_index_end; > > >> /* if non-zero, minimum required value for max_queues */ > > >> int num_queues; > > >> + /** > > >> + * vhost feature handling requires matching the feature set > > >> + * offered by a backend which may be a subset of the total > > >> + * features eventually offered to the guest. > > >> + * > > >> + * @features: available features provided by the backend > > >> + * @acked_features: final set of negotiated features with the > > >> + * front-end driver > > >> + * @backend_features: additional feature bits applied during negotiation > > > > > > What does this mean? > > > > Well practically it is currently either applying > > VHOST_USER_F_PROTOCOL_FEATURES to the vhost_user_set_features() or > > storing VHOST_NET_F_VIRTIO_NET_HDR which I think eventually gets applied > > to: > > > > net->dev.acked_features = net->dev.backend_features; > > > > I suspect both could be dropped and handled as flags and applied at the > > destination. > > I noticed there is also a VirtIODevice::backend_features field that is > barely used. virtio_net stashes vhost_net backend feature bits in > there, but I think the code would be clearer if it was rewritten > without the VirtIODevice::backend_features field. > > Having both VirtIODevice and struct vhost_dev fields with the same > name is confusing. :/ > > > > > > > > >> + * > > >> + * Finally the @protocol_features is the final protocal feature > > > > > > s/protocal/protocol/ > > > > > > All the other fields are VIRTIO feature bits and this field holds the > > > VHOST_USER_SET_FEATURES feature bits? > > > > No these are the protocol features so a totally separate set of feature > > bits for the vhost user protocol. I don't think these apply to kernel > > vhost stuff? > > Oh, I see. Only vhost-user has this concept. > > How about "the vhost-user VHOST_USER_SET_PROTOCOL_FEATURES bits"? > > > > > > > > >> + * set negotiated between QEMU and the backend (after > > >> + * VHOST_USER_F_PROTOCOL_FEATURES is negotiated) > > >> + */ > > >> uint64_t features; > > >> - /** @acked_features: final set of negotiated features */ > > >> uint64_t acked_features; > > >> - /** @backend_features: backend specific feature bits */ > > >> uint64_t backend_features; > > >> - /** @protocol_features: final negotiated protocol features */ > > >> uint64_t protocol_features; > > >> + > > >> uint64_t max_queues; > > >> uint64_t backend_cap; > > >> /* @started: is the vhost device started? */ > > >> diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h > > >> index a973811cbf..9939a0a632 100644 > > >> --- a/include/hw/virtio/virtio.h > > >> +++ b/include/hw/virtio/virtio.h > > >> @@ -93,6 +93,12 @@ enum virtio_device_endian { > > >> VIRTIO_DEVICE_ENDIAN_BIG, > > >> }; > > >> > > >> +/** > > >> + * struct VirtIODevice - common VirtIO structure > > >> + * @name: name of the device > > >> + * @status: VirtIO Device Status field > > >> + * > > >> + */ > > >> struct VirtIODevice > > >> { > > >> DeviceState parent_obj; > > >> @@ -100,9 +106,21 @@ struct VirtIODevice > > >> uint8_t status; > > >> uint8_t isr; > > >> uint16_t queue_sel; > > >> - uint64_t guest_features; > > >> + /** > > >> + * These fields represent a set of VirtIO features at various > > >> + * levels of the stack. @host_features indicates the complete > > >> + * feature set the VirtIO device can offer to the driver. > > >> + * @guest_features indicates which features the VirtIO driver can > > >> + * support. > > > > > > The device never knows the features that the driver can support, so > > > this sentence is ambiguous/incorrect. The device only knows the > > > features that the driver writes during negotiation, which the spec > > > says is a subset of host_features. > > > > > > Maybe "indicates the features that driver wrote"? > > > > > > I noticed that this field is assigned even when the guest writes > > > invalid feature bits. > > > > Should we fix that? The negotiation sequence should be guest read, mask > > and write back so the value should be validated against host_features? > > I think the negotiation/state machine is okay. Invalid features will > not be accepted (negotiation fails). > > It's just weird that an invalid input from the guest can be stored in > guest_features. Hopefully nothing trusts the guest_features value > without first checking that negotiation succeeded (I haven't checked > the code). > > Stefan We can add more validation in virtio_set_features but I don't think ultimately it matters much. -- MST