Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1157360pxb; Fri, 21 Jan 2022 11:04:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJwZP0+I7iWD97/j3ri334kmtzTVNqnVm1FUg3RSXddnv9UjeyKm+Vy5dzkexR6WBzNIhy8F X-Received: by 2002:a17:902:d487:b0:14b:1bd7:fe69 with SMTP id c7-20020a170902d48700b0014b1bd7fe69mr4645465plg.148.1642791866112; Fri, 21 Jan 2022 11:04:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642791866; cv=none; d=google.com; s=arc-20160816; b=UvJEcO9XbP/Csz0BaT3kCQPRYjd4Dcc0/vZ091+dUXvux7DCIGthJSQFSdpe0uOTT4 v60ltH0cIWTP4AjWBkdiiPVP4DeG6eaJLXkG+xpaGFpVqlyvCVeeSXYR0MG8wSsRAg+c fZSvzAySuXOedZNqX0Sh2H2UDsHU0cK/1PQ8r4m88llg4J5uoRHqVPjjyh+RjpPQy8tv ejsCmcfIWn+hVsaOFU8KvW4smvfGlfk04WUtvEKC528t36T3Juvi3ar7gB1wchjv7VOf HKynVdSkqcSdwFMm8kBzdrNSiNy7pPDLtu+QSWCuZoUIF2ekKbQTQ1zl3/TW5fd9zsXi 4ckw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=PtP9Iz6xHgK8mpeAVkx8NjBKePAszJ/UHQqLGQj4py8=; b=NOdxwq5bBrYiJ8uRkgrVIov+Qp1WscNvJxf84OfqRtph+IxWw5qMsK/UBgae8pb3OH BIxTGygKRa7xaZnxcnV8e8Lw4tPjNSwkno3QYWMf2pVeS+ckmCgzIgCLmnuoV4aj+yLG DPxdBWzfkpmagnzMgLu+dziqIwU1uvQllQapQll/AmmOkQ9AodzomiMa3S3bwzOUGO7w FLoBJI5KNT9o/icOAC6V4uQcwRlpPRBYD9Sjn+rWOcdFV2aJ+OtrJ15wmZ9I0DHE52S1 jM5951hF/lkKziVKDKS7jUmFdQ+9gvf/NAqk5zsHavQ+J7fnUomRWe3/pztFkBQJzP62 +tEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="SC4s/M4e"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v2si6719662plp.620.2022.01.21.11.04.13; Fri, 21 Jan 2022 11:04:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="SC4s/M4e"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352961AbiASJSL (ORCPT + 99 others); Wed, 19 Jan 2022 04:18:11 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:30794 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352953AbiASJSL (ORCPT ); Wed, 19 Jan 2022 04:18:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642583890; 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=PtP9Iz6xHgK8mpeAVkx8NjBKePAszJ/UHQqLGQj4py8=; b=SC4s/M4ehcr11Fw7FNdGd4dDFca8632FRjuzKd1u/Xi3LZW2eDSYATCBKPPzhIh6O6thmz YHol5ZC9irbpp39MceDQQz3DIE8QCiN4ttWTR+rkERFmm8Pg+886qzI9bKOmXXpB5X3DSx KhwVDbMpQ3lo7GmQ/GV9UqxFJtzlzAU= 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-664-wQ8RjWjuN0eYT9NNmY7wWA-1; Wed, 19 Jan 2022 04:18:09 -0500 X-MC-Unique: wQ8RjWjuN0eYT9NNmY7wWA-1 Received: by mail-wm1-f69.google.com with SMTP id c62-20020a1c9a41000000b0034da9d62199so708605wme.7 for ; Wed, 19 Jan 2022 01:18:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=PtP9Iz6xHgK8mpeAVkx8NjBKePAszJ/UHQqLGQj4py8=; b=c0GvL0TYKdddI3rsmkdH7NwGvah+fi6UQvVUiLHz+H73XBcNLV1k0JXahC+/ynqUnY 1VFgIzw4EEL7T9wx/dOrLCEqCGiBYrqhnrhUGU8MKJF/X7UG+/uRxASeVJ6p2r4FDTDZ sLEfZ3TbUoMpVql1Dmd/XnH2fOjBe6xWsg4qkW7ETfIDuyb35WRdFrUxLMbvfew2Hxhw go9ESMSq4oLu/0GWhpubjyC+qQpwwnly9+9s6zSE7Ik1tZdEqxLj5GMiFUe5p7yO4gTA YSNT0N8pe+x7ZKAUCpENoqHeu4vxXZs1FqqfaZZC3DhIfDjR1k+EJhzSBVBH6p+YCFiK vTdQ== X-Gm-Message-State: AOAM533KOi+9CpeSalMpRFdlUtUfMDfu+iXGGdpreKScLRRo7zESJa4Q 7Bax0iJ/avs4Qi5eDz+zbvIkWGgRrbqqLWOhC0x8PUo9VZOgPr8NZOYfPN68XzRYjVd7oIF9YVG 1x74f84yCdx1TfGHx/F8EdEm0 X-Received: by 2002:a1c:a5d4:: with SMTP id o203mr2424983wme.43.1642583887810; Wed, 19 Jan 2022 01:18:07 -0800 (PST) X-Received: by 2002:a1c:a5d4:: with SMTP id o203mr2424959wme.43.1642583887489; Wed, 19 Jan 2022 01:18:07 -0800 (PST) Received: from redhat.com ([2a10:8008:4454:0:bb37:a05d:7459:8682]) by smtp.gmail.com with ESMTPSA id n32sm4860459wms.8.2022.01.19.01.18.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jan 2022 01:18:06 -0800 (PST) Date: Wed, 19 Jan 2022 04:18:04 -0500 From: "Michael S. Tsirkin" To: Jason Wang Cc: Halil Pasic , Cornelia Huck , linux-kernel , stable@vger.kernel.org, virtualization Subject: Re: [PATCH v2 2/2] virtio: acknowledge all features before access Message-ID: <20220119041343-mutt-send-email-mst@kernel.org> References: <20220118170225.30620-1-mst@redhat.com> <20220118170225.30620-2-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 19, 2022 at 10:52:34AM +0800, Jason Wang wrote: > On Wed, Jan 19, 2022 at 1:04 AM Michael S. Tsirkin wrote: > > > > The feature negotiation was designed in a way that > > makes it possible for devices to know which config > > fields will be accessed by drivers. > > > > This is broken since commit 404123c2db79 ("virtio: allow drivers to > > validate features") with fallout in at least block and net. We have a > > partial work-around in commit 2f9a174f918e ("virtio: write back > > F_VERSION_1 before validate") which at least lets devices find out which > > format should config space have, but this is a partial fix: guests > > should not access config space without acknowledging features since > > otherwise we'll never be able to change the config space format. > > So I guess this is for this part of the spec 3.1.1: > > """ > 4. Read device feature bits, and write the subset of feature bits > understood by the OS and driver to the device. During this step the > driver MAY read (but MUST NOT write) the device-specific configuration > fields to check that it can support the device before accepting it. > """ > > If it is, is this better to quote in the change log? I don't think this spec actually clarifies anything. Sent some spec patches to improve the situation. > Other than this, > > Acked-by: Jason Wang > > > > > To fix, split finalize_features from virtio_finalize_features and > > call finalize_features with all feature bits before validation, > > and then - if validation changed any bits - once again after. > > > > Since virtio_finalize_features no longer writes out features > > rename it to virtio_features_ok - since that is what it does: > > checks that features are ok with the device. > > > > As a side effect, this also reduces the amount of hypervisor accesses - > > we now only acknowledge features once unless we are clearing any > > features when validating (which is uncommon). > > > > Cc: stable@vger.kernel.org > > Fixes: 404123c2db79 ("virtio: allow drivers to validate features") > > Fixes: 2f9a174f918e ("virtio: write back F_VERSION_1 before validate") > > Cc: "Halil Pasic" > > Signed-off-by: Michael S. Tsirkin > > > > fixup! virtio: acknowledge all features before access > > --- > > drivers/virtio/virtio.c | 39 ++++++++++++++++++++--------------- > > include/linux/virtio_config.h | 3 ++- > > 2 files changed, 24 insertions(+), 18 deletions(-) > > > > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c > > index d891b0a354b0..d6396be0ea83 100644 > > --- a/drivers/virtio/virtio.c > > +++ b/drivers/virtio/virtio.c > > @@ -166,14 +166,13 @@ void virtio_add_status(struct virtio_device *dev, unsigned int status) > > } > > EXPORT_SYMBOL_GPL(virtio_add_status); > > > > -static int virtio_finalize_features(struct virtio_device *dev) > > +/* Do some validation, then set FEATURES_OK */ > > +static int virtio_features_ok(struct virtio_device *dev) > > { > > - int ret = dev->config->finalize_features(dev); > > unsigned status; > > + int ret; > > > > might_sleep(); > > - if (ret) > > - return ret; > > > > ret = arch_has_restricted_virtio_memory_access(); > > if (ret) { > > @@ -244,17 +243,6 @@ static int virtio_dev_probe(struct device *_d) > > driver_features_legacy = driver_features; > > } > > > > - /* > > - * Some devices detect legacy solely via F_VERSION_1. Write > > - * F_VERSION_1 to force LE config space accesses before FEATURES_OK for > > - * these when needed. > > - */ > > - if (drv->validate && !virtio_legacy_is_little_endian() > > - && device_features & BIT_ULL(VIRTIO_F_VERSION_1)) { > > - dev->features = BIT_ULL(VIRTIO_F_VERSION_1); > > - dev->config->finalize_features(dev); > > - } > > - > > if (device_features & (1ULL << VIRTIO_F_VERSION_1)) > > dev->features = driver_features & device_features; > > else > > @@ -265,13 +253,26 @@ static int virtio_dev_probe(struct device *_d) > > if (device_features & (1ULL << i)) > > __virtio_set_bit(dev, i); > > > > + err = dev->config->finalize_features(dev); > > + if (err) > > + goto err; > > + > > if (drv->validate) { > > + u64 features = dev->features; > > + > > err = drv->validate(dev); > > if (err) > > goto err; > > + > > + /* Did validation change any features? Then write them again. */ > > + if (features != dev->features) { > > + err = dev->config->finalize_features(dev); > > + if (err) > > + goto err; > > + } > > } > > > > - err = virtio_finalize_features(dev); > > + err = virtio_features_ok(dev); > > if (err) > > goto err; > > > > @@ -495,7 +496,11 @@ int virtio_device_restore(struct virtio_device *dev) > > /* We have a driver! */ > > virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER); > > > > - ret = virtio_finalize_features(dev); > > + ret = dev->config->finalize_features(dev); > > + if (ret) > > + goto err; > > + > > + ret = virtio_features_ok(dev); > > if (ret) > > goto err; > > > > diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h > > index 4d107ad31149..dafdc7f48c01 100644 > > --- a/include/linux/virtio_config.h > > +++ b/include/linux/virtio_config.h > > @@ -64,8 +64,9 @@ struct virtio_shm_region { > > * Returns the first 64 feature bits (all we currently need). > > * @finalize_features: confirm what device features we'll be using. > > * vdev: the virtio_device > > - * This gives the final feature bits for the device: it can change > > + * This sends the driver feature bits to the device: it can change > > * the dev->feature bits if it wants. > > + * Note: despite the name this can be called any number of times. > > * Returns 0 on success or error status > > * @bus_name: return the bus name associated with the device (optional) > > * vdev: the virtio_device > > -- > > MST > > > > _______________________________________________ > Virtualization mailing list > Virtualization@lists.linux-foundation.org > https://lists.linuxfoundation.org/mailman/listinfo/virtualization >