From nobody Fri Jun 19 08:30:48 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 956D7C433EF for ; Wed, 6 Apr 2022 11:46:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230196AbiDFLr3 (ORCPT ); Wed, 6 Apr 2022 07:47:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229769AbiDFLrI (ORCPT ); Wed, 6 Apr 2022 07:47:08 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9996E597598 for ; Wed, 6 Apr 2022 01:36:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649234154; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xvfjr3XhmHkmPEznI6BD1smjCJrc58Cl2CSt883dCaY=; b=QgHvAxTiimoL+mMmY81upXbXS0O9AwI4mJ3QQJekpjDmBSFLrcwymyheyDkKV5qm7qFXPt mbYuZ8G5W2zH6f045pV6i0/gyH3TkllTOZnPArQKv4qwIgxkGcLncaZvRq7e1HqoRYXJkp EZ5hFiNpBRyQPoyOeLv6PtwIsE/FE98= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-304-YRfKIHFgNhu5nDR_Tjss4Q-1; Wed, 06 Apr 2022 04:35:51 -0400 X-MC-Unique: YRfKIHFgNhu5nDR_Tjss4Q-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C922680005D; Wed, 6 Apr 2022 08:35:50 +0000 (UTC) Received: from localhost.localdomain (ovpn-12-58.pek2.redhat.com [10.72.12.58]) by smtp.corp.redhat.com (Postfix) with ESMTP id 654D71121314; Wed, 6 Apr 2022 08:35:46 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, maz@kernel.org, tglx@linutronix.de, peterz@infradead.org, sgarzare@redhat.com, "Paul E. McKenney" Subject: [PATCH V2 1/5] virtio: use virtio_device_ready() in virtio_device_restore() Date: Wed, 6 Apr 2022 16:35:34 +0800 Message-Id: <20220406083538.16274-2-jasowang@redhat.com> In-Reply-To: <20220406083538.16274-1-jasowang@redhat.com> References: <20220406083538.16274-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Stefano Garzarella It will allows us to do extension on virtio_device_ready() without duplicating codes. Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: "Paul E. McKenney" Cc: Marc Zyngier Signed-off-by: Stefano Garzarella Signed-off-by: Jason Wang --- drivers/virtio/virtio.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 22f15f444f75..75c8d560bbd3 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -526,8 +526,9 @@ int virtio_device_restore(struct virtio_device *dev) goto err; } =20 - /* Finally, tell the device we're all set */ - virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK); + /* If restore didn't do it, mark device DRIVER_OK ourselves. */ + if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) + virtio_device_ready(dev); =20 virtio_config_enable(dev); =20 --=20 2.25.1 From nobody Fri Jun 19 08:30:49 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 73299C433EF for ; Wed, 6 Apr 2022 12:34:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231782AbiDFMgQ (ORCPT ); Wed, 6 Apr 2022 08:36:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234017AbiDFMdr (ORCPT ); Wed, 6 Apr 2022 08:33:47 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 40A5FE543A for ; Wed, 6 Apr 2022 01:36:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649234171; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Mo+37PUldM6ZumYASfl7EbwOu7jSED8t6QGqdYfIb4E=; b=dJtJjtnec2Hl/uGPz5u0UqpnmFzyNPK55PcU0D0t2hL9f59YR548JDn2ZhLS40sdOJk3U0 9At/IywvPcpdhXV8AcjCKXoBUBJr9fC1oG8bRDhEdgstdm4qA0h1KMVE0E8xNa5oq2/j77 mGvG5Om0nRLesKByahUu+mU09iCLv6w= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-355--b2tvIduNiulsavj4ZShjQ-1; Wed, 06 Apr 2022 04:36:05 -0400 X-MC-Unique: -b2tvIduNiulsavj4ZShjQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 73982800882; Wed, 6 Apr 2022 08:36:05 +0000 (UTC) Received: from localhost.localdomain (ovpn-12-58.pek2.redhat.com [10.72.12.58]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF36E1121314; Wed, 6 Apr 2022 08:35:51 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, maz@kernel.org, tglx@linutronix.de, peterz@infradead.org, sgarzare@redhat.com, "Paul E. McKenney" Subject: [PATCH V2 2/5] virtio: use virtio_reset_device() when possible Date: Wed, 6 Apr 2022 16:35:35 +0800 Message-Id: <20220406083538.16274-3-jasowang@redhat.com> In-Reply-To: <20220406083538.16274-1-jasowang@redhat.com> References: <20220406083538.16274-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This allows us to do common extension without duplicating codes. Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: "Paul E. McKenney" Cc: Marc Zyngier Signed-off-by: Jason Wang --- drivers/virtio/virtio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 75c8d560bbd3..8dde44ea044a 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -430,7 +430,7 @@ int register_virtio_device(struct virtio_device *dev) =20 /* We always start by resetting the device, in case a previous * driver messed it up. This also tests that code path a little. */ - dev->config->reset(dev); + virtio_reset_device(dev); =20 /* Acknowledge that we've seen the device. */ virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); @@ -496,7 +496,7 @@ int virtio_device_restore(struct virtio_device *dev) =20 /* We always start by resetting the device, in case a previous * driver messed it up. */ - dev->config->reset(dev); + virtio_reset_device(dev); =20 /* Acknowledge that we've seen the device. */ virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); --=20 2.25.1 From nobody Fri Jun 19 08:30:49 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9707DC433FE for ; Wed, 6 Apr 2022 12:34:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230126AbiDFMg3 (ORCPT ); Wed, 6 Apr 2022 08:36:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234101AbiDFMd5 (ORCPT ); Wed, 6 Apr 2022 08:33:57 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C0EB32E4BB8 for ; Wed, 6 Apr 2022 01:36:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649234174; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9fzws/TrjvfN5TEfT4l4EkTY22qMkOTX3Nn8UDj/95g=; b=U2jSjSPZZAGl10JXIb+4Hvb3bNyql2OIh1oR8HNVn8Of9dep/eHQQX/Xz32OiZfxO1NAEF KUgk7N25qgqRph5nwTxX8FqdUhpJnpADqhxOzXWbeprrdoxeaSvsVYwjqnX5Qp8YDt5SUr LUahedbw9xv0oLRhkdkMrARJaMk+jEU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-226-dyZ8qOuZMEmmn0cZcjH-8A-1; Wed, 06 Apr 2022 04:36:11 -0400 X-MC-Unique: dyZ8qOuZMEmmn0cZcjH-8A-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 901B780B71C; Wed, 6 Apr 2022 08:36:10 +0000 (UTC) Received: from localhost.localdomain (ovpn-12-58.pek2.redhat.com [10.72.12.58]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1A80A112132D; Wed, 6 Apr 2022 08:36:05 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, maz@kernel.org, tglx@linutronix.de, peterz@infradead.org, sgarzare@redhat.com, "Paul E. McKenney" Subject: [PATCH V2 3/5] virtio: introduce config op to synchronize vring callbacks Date: Wed, 6 Apr 2022 16:35:36 +0800 Message-Id: <20220406083538.16274-4-jasowang@redhat.com> In-Reply-To: <20220406083538.16274-1-jasowang@redhat.com> References: <20220406083538.16274-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch introduce a new virtio config ops to vring callbacks. Transport specific method is required to call synchornize_irq() on the IRQs. For the transport that doesn't provide synchronize_vqs(), use synchornize_rcu() as a fallback. Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: "Paul E. McKenney" Cc: Marc Zyngier Signed-off-by: Jason Wang --- include/linux/virtio_config.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index b341dd62aa4d..08b73d9bbff2 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h @@ -57,6 +57,8 @@ struct virtio_shm_region { * include a NULL entry for vqs unused by driver * Returns 0 on success or error status * @del_vqs: free virtqueues found by find_vqs(). + * @synchronize_vqs: synchronize with the virtqueue callbacks. + * vdev: the virtio_device * @get_features: get the array of feature bits for this device. * vdev: the virtio_device * Returns the first 64 feature bits (all we currently need). @@ -89,6 +91,7 @@ struct virtio_config_ops { const char * const names[], const bool *ctx, struct irq_affinity *desc); void (*del_vqs)(struct virtio_device *); + void (*synchronize_vqs)(struct virtio_device *); u64 (*get_features)(struct virtio_device *vdev); int (*finalize_features)(struct virtio_device *vdev); const char *(*bus_name)(struct virtio_device *vdev); @@ -217,6 +220,19 @@ int virtio_find_vqs_ctx(struct virtio_device *vdev, un= signed nvqs, desc); } =20 +/** + * virtio_synchronize_vqs - synchronize with virtqueue callbacks + * @vdev: the device + */ +static inline +void virtio_synchronize_vqs(struct virtio_device *dev) +{ + if (dev->config->synchronize_vqs) + dev->config->synchronize_vqs(dev); + else + synchronize_rcu(); +} + /** * virtio_device_ready - enable vq use in probe function * @vdev: the device --=20 2.25.1 From nobody Fri Jun 19 08:30:49 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC871C433F5 for ; Wed, 6 Apr 2022 12:34:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229909AbiDFMgm (ORCPT ); Wed, 6 Apr 2022 08:36:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234073AbiDFMd4 (ORCPT ); Wed, 6 Apr 2022 08:33:56 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A2494480C3F for ; Wed, 6 Apr 2022 01:36:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649234183; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/3YhGhX98tXHPwykWkG6BkL+HnhGiXnNU5siRoG2nw4=; b=Tq7Y3n2szdc5maCld7Qy1iqdvdGJE6hbGCPT57kjRZHisDT2ttnjknRNILLO7OunmDtLrk 3qYUidraaCA4TVV6evliiLZD3i0W7i6DdXxYVsyCKY/MoQXJIOJYWPJiq/ezlU2a5BhCHd LhOfpK0YJ9noxl5LOjiOHIrRdnq9UYo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-208-4tELYOGvP8ysDnXvHeBj-g-1; Wed, 06 Apr 2022 04:36:20 -0400 X-MC-Unique: 4tELYOGvP8ysDnXvHeBj-g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2F61B811E81; Wed, 6 Apr 2022 08:36:20 +0000 (UTC) Received: from localhost.localdomain (ovpn-12-58.pek2.redhat.com [10.72.12.58]) by smtp.corp.redhat.com (Postfix) with ESMTP id C1A6F112132D; Wed, 6 Apr 2022 08:36:10 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, maz@kernel.org, tglx@linutronix.de, peterz@infradead.org, sgarzare@redhat.com, "Paul E. McKenney" Subject: [PATCH V2 4/5] virtio-pci: implement synchronize_vqs() Date: Wed, 6 Apr 2022 16:35:37 +0800 Message-Id: <20220406083538.16274-5-jasowang@redhat.com> In-Reply-To: <20220406083538.16274-1-jasowang@redhat.com> References: <20220406083538.16274-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch implements PCI version of synchronize_vqs(). Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: "Paul E. McKenney" Cc: Marc Zyngier Signed-off-by: Jason Wang --- drivers/virtio/virtio_pci_common.c | 14 ++++++++++++++ drivers/virtio/virtio_pci_common.h | 2 ++ drivers/virtio/virtio_pci_legacy.c | 1 + drivers/virtio/virtio_pci_modern.c | 2 ++ 4 files changed, 19 insertions(+) diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci= _common.c index d724f676608b..b78c8bc93a97 100644 --- a/drivers/virtio/virtio_pci_common.c +++ b/drivers/virtio/virtio_pci_common.c @@ -37,6 +37,20 @@ void vp_synchronize_vectors(struct virtio_device *vdev) synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i)); } =20 +void vp_synchronize_vqs(struct virtio_device *vdev) +{ + struct virtio_pci_device *vp_dev =3D to_vp_device(vdev); + int i; + + if (vp_dev->intx_enabled) { + synchronize_irq(vp_dev->pci_dev->irq); + return; + } + + for (i =3D 0; i < vp_dev->msix_vectors; ++i) + synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i)); +} + /* the notify function used when creating a virt queue */ bool vp_notify(struct virtqueue *vq) { diff --git a/drivers/virtio/virtio_pci_common.h b/drivers/virtio/virtio_pci= _common.h index eb17a29fc7ef..2b84d5c1b5bc 100644 --- a/drivers/virtio/virtio_pci_common.h +++ b/drivers/virtio/virtio_pci_common.h @@ -105,6 +105,8 @@ static struct virtio_pci_device *to_vp_device(struct vi= rtio_device *vdev) void vp_synchronize_vectors(struct virtio_device *vdev); /* the notify function used when creating a virt queue */ bool vp_notify(struct virtqueue *vq); +/* synchronize with callbacks */ +void vp_synchronize_vqs(struct virtio_device *vdev); /* the config->del_vqs() implementation */ void vp_del_vqs(struct virtio_device *vdev); /* the config->find_vqs() implementation */ diff --git a/drivers/virtio/virtio_pci_legacy.c b/drivers/virtio/virtio_pci= _legacy.c index 6f4e34ce96b8..5a9e62320edc 100644 --- a/drivers/virtio/virtio_pci_legacy.c +++ b/drivers/virtio/virtio_pci_legacy.c @@ -192,6 +192,7 @@ static const struct virtio_config_ops virtio_pci_config= _ops =3D { .reset =3D vp_reset, .find_vqs =3D vp_find_vqs, .del_vqs =3D vp_del_vqs, + .synchronize_vqs =3D vp_synchronize_vqs, .get_features =3D vp_get_features, .finalize_features =3D vp_finalize_features, .bus_name =3D vp_bus_name, diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci= _modern.c index a2671a20ef77..584850389855 100644 --- a/drivers/virtio/virtio_pci_modern.c +++ b/drivers/virtio/virtio_pci_modern.c @@ -394,6 +394,7 @@ static const struct virtio_config_ops virtio_pci_config= _nodev_ops =3D { .reset =3D vp_reset, .find_vqs =3D vp_modern_find_vqs, .del_vqs =3D vp_del_vqs, + .synchronize_vqs =3D vp_synchronize_vqs, .get_features =3D vp_get_features, .finalize_features =3D vp_finalize_features, .bus_name =3D vp_bus_name, @@ -411,6 +412,7 @@ static const struct virtio_config_ops virtio_pci_config= _ops =3D { .reset =3D vp_reset, .find_vqs =3D vp_modern_find_vqs, .del_vqs =3D vp_del_vqs, + .synchronize_vqs =3D vp_synchronize_vqs, .get_features =3D vp_get_features, .finalize_features =3D vp_finalize_features, .bus_name =3D vp_bus_name, --=20 2.25.1 From nobody Fri Jun 19 08:30:49 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12CD1C433F5 for ; Wed, 6 Apr 2022 12:35:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230354AbiDFMg7 (ORCPT ); Wed, 6 Apr 2022 08:36:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231450AbiDFMeS (ORCPT ); Wed, 6 Apr 2022 08:34:18 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2C1641B29C3 for ; Wed, 6 Apr 2022 01:36:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649234201; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RXRl5yVraFjtAHNeuo8Tj/7VeEPbPvmJda7LXPVTN00=; b=RikE92o/kxvWj/75Bxg+5qCSXgMeey8Qg6d3qciE8i9a0yibFBXxNWiBSr6ZfJ/wqIoq/p mpHk5XDGV45RM3z5YI4lvPi+21VpuL8mjV6EIRu+PT7/TBsHe8kQg6Pt9h3FyTPKEiJpaJ dAYcwlMbYR3/6h5l4F0CwnBijmdw/6E= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-652-ipVoKGptOI24T9gwiW2-gA-1; Wed, 06 Apr 2022 04:36:38 -0400 X-MC-Unique: ipVoKGptOI24T9gwiW2-gA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EBC763822204; Wed, 6 Apr 2022 08:36:37 +0000 (UTC) Received: from localhost.localdomain (ovpn-12-58.pek2.redhat.com [10.72.12.58]) by smtp.corp.redhat.com (Postfix) with ESMTP id 97A9F1121319; Wed, 6 Apr 2022 08:36:20 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, maz@kernel.org, tglx@linutronix.de, peterz@infradead.org, sgarzare@redhat.com, "Paul E. McKenney" Subject: [PATCH V2 5/5] virtio: harden vring IRQ Date: Wed, 6 Apr 2022 16:35:38 +0800 Message-Id: <20220406083538.16274-6-jasowang@redhat.com> In-Reply-To: <20220406083538.16274-1-jasowang@redhat.com> References: <20220406083538.16274-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is a rework on the previous IRQ hardening that is done for virtio-pci where several drawbacks were found and were reverted: 1) try to use IRQF_NO_AUTOEN which is not friendly to affinity managed IRQ that is used by some device such as virtio-blk 2) done only for PCI transport In this patch, we tries to borrow the idea from the INTX IRQ hardening in the reverted commit 080cd7c3ac87 ("virtio-pci: harden INTX interrupts") by introducing a global device_ready variable for each virtio_device. Then we can to toggle it during virtio_reset_device()/virtio_device_ready(). A virtio_synchornize_vqs() is used in both virtio_device_ready() and virtio_reset_device() to synchronize with the vring callbacks. With this, vring_interrupt() can return check and early if driver_ready is false. Note that the hardening is only done for vring interrupt since the config interrupt hardening is already done in commit 22b7050a024d7 ("virtio: defer config changed notifications"). But the method that is used by config interrupt can't be reused by the vring interrupt handler because it uses spinlock to do the synchronization which is expensive. Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: "Paul E. McKenney" Cc: Marc Zyngier Signed-off-by: Jason Wang --- drivers/virtio/virtio.c | 11 +++++++++++ drivers/virtio/virtio_ring.c | 9 ++++++++- include/linux/virtio.h | 2 ++ include/linux/virtio_config.h | 8 ++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 8dde44ea044a..2f3a6f8e3d9c 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -220,6 +220,17 @@ static int virtio_features_ok(struct virtio_device *de= v) * */ void virtio_reset_device(struct virtio_device *dev) { + if (READ_ONCE(dev->driver_ready)) { + /* + * The below virtio_synchronize_vqs() guarantees that any + * interrupt for this line arriving after + * virtio_synchronize_vqs() has completed is guaranteed to see + * driver_ready =3D=3D false. + */ + WRITE_ONCE(dev->driver_ready, false); + virtio_synchronize_vqs(dev); + } + dev->config->reset(dev); } EXPORT_SYMBOL_GPL(virtio_reset_device); diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index cfb028ca238e..a4592e55c9f8 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -2127,10 +2127,17 @@ static inline bool more_used(const struct vring_vir= tqueue *vq) return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq); } =20 -irqreturn_t vring_interrupt(int irq, void *_vq) +irqreturn_t vring_interrupt(int irq, void *v) { + struct virtqueue *_vq =3D v; + struct virtio_device *vdev =3D _vq->vdev; struct vring_virtqueue *vq =3D to_vvq(_vq); =20 + if (!READ_ONCE(vdev->driver_ready)) { + dev_warn_once(&vdev->dev, "virtio vring IRQ raised before DRIVER_OK"); + return IRQ_NONE; + } + if (!more_used(vq)) { pr_debug("virtqueue interrupt with no work for %p\n", vq); return IRQ_NONE; diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 5464f398912a..dfa2638a293e 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -95,6 +95,7 @@ dma_addr_t virtqueue_get_used_addr(struct virtqueue *vq); * @failed: saved value for VIRTIO_CONFIG_S_FAILED bit (for restore) * @config_enabled: configuration change reporting enabled * @config_change_pending: configuration change reported while disabled + * @driver_ready: whehter the driver is ready (e.g for vring callbacks) * @config_lock: protects configuration change reporting * @dev: underlying device. * @id: the device type identification (used to match it with a driver). @@ -109,6 +110,7 @@ struct virtio_device { bool failed; bool config_enabled; bool config_change_pending; + bool driver_ready; spinlock_t config_lock; spinlock_t vqs_list_lock; /* Protects VQs list access */ struct device dev; diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index 08b73d9bbff2..c9e207bf2c9c 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h @@ -246,6 +246,14 @@ void virtio_device_ready(struct virtio_device *dev) { unsigned status =3D dev->config->get_status(dev); =20 + virtio_synchronize_vqs(dev); + /* + * The above virtio_synchronize_vqs() make sure + * vring_interrupt() will see the driver specific setup if it + * see driver_ready as true. + */ + WRITE_ONCE(dev->driver_ready, true); + BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK); dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK); } --=20 2.25.1