From nobody Sat Nov 23 16:42:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1731657887; cv=none; d=zohomail.com; s=zohoarc; b=QiV7FY0C+8VbltIbiglAY0XcaIx+mV4ofqnTQsoulscX5aQaAfKL7mCqrxG16GAhyYX7Fye6JR7YkTIJMngaDWa1G6e8Pq3OGUQr7lJTQNHVinoXqjCRnEgL4FL6pLaCWIqzNv/uLmKkYqmzqrqDPAGaEQ0VCn9v7xWvHbRkjNc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731657887; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=3PcOFeomeP5U3GQrK0QeNScRlInjR6slveRgXC5liAM=; b=S85t1ySpaaqH8Dy+MKYtyN7myDkggxlafFlSP5A7DUwYwtnmzV7wWDMjWX6CbzC3fKX/K406Ob02v68BbSWXiJfGLk+zruLXEAzi1qxMzGRdGEMxTEj3dngUBf8iNPUNZ8DMAteLVsDh6tczst/h+8HhGzHjVXH6oXNNj+Qa2vc= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 1731657887741392.92502162276685; Fri, 15 Nov 2024 00:04:47 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tBrIy-0006cV-Jz; Fri, 15 Nov 2024 03:03:49 -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 1tBrIs-0006c7-VP for qemu-devel@nongnu.org; Fri, 15 Nov 2024 03:03:43 -0500 Received: from mx24.baidu.com ([111.206.215.185] helo=baidu.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tBrIn-0005ZE-IO for qemu-devel@nongnu.org; Fri, 15 Nov 2024 03:03:41 -0500 To: qemu-devel CC: "Michael S . Tsirkin" , Stefano Garzarella , Jason Wang , zuoboqun , Gao Shiyuan Subject: [PATCH v2] vhost_net: fix assertion triggered by batch of host notifiers processing Date: Fri, 15 Nov 2024 16:03:12 +0800 Message-ID: <20241115080312.3184-1-zuoboqun@baidu.com> X-Mailer: git-send-email 2.42.0.windows.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.127.72.17] X-ClientProxiedBy: BJHW-Mail-Ex06.internal.baidu.com (10.127.64.16) To BJHW-MAIL-EX26.internal.baidu.com (10.127.64.41) X-FEAS-Client-IP: 172.31.51.48 X-FE-Policy-ID: 52:10:53:SYSTEM 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=111.206.215.185; envelope-from=zuoboqun@baidu.com; helo=baidu.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-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: , Reply-to: Zuo boqun From: Zuo boqun via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1731657890008116600 Content-Type: text/plain; charset="utf-8" From: zuoboqun When the backend of vhost_net restarts during the vm is running, vhost_net is stopped and started. The virtio_device_grab_ioeventfd() fucntion in vhost_net_enable_notifiers() will result in a call to virtio_bus_set_host_notifier()(assign=3Dfalse). And now virtio_device_grab_ioeventfd() is batched in a single transaction with virtio_bus_set_host_notifier()(assign=3Dtrue). This triggers the following assertion: kvm_mem_ioeventfd_del: error deleting ioeventfd: Bad file descriptor This patch moves virtio_device_grab_ioeventfd() out of the batch to fix this problem. To be noted that the for loop to release ioeventfd should start from i+1, not i, because the i-th ioeventfd has already been released in vhost_dev_disable_notifiers_nvqs(). Fixes: 6166799f6 ("vhost_net: configure all host notifiers in a single MR t= ransaction") Signed-off-by: Zuo Boqun Reported-by: Gao Shiyuan Acked-by: Stefano Garzarella --- v1->v2: *To explain why the for loop to release ioeventfd starts from i+1: 1) add a comment in the code 2) describe it in the commit message --- hw/net/vhost_net.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c index 997aab0557..891f235a0a 100644 --- a/hw/net/vhost_net.c +++ b/hw/net/vhost_net.c @@ -229,9 +229,24 @@ static int vhost_net_enable_notifiers(VirtIODevice *de= v, int nvhosts =3D data_queue_pairs + cvq; struct vhost_net *net; struct vhost_dev *hdev; - int r, i, j; + int r, i, j, k; NetClientState *peer; =20 + /* + * We will pass the notifiers to the kernel, make sure that QEMU + * doesn't interfere. + */ + for (i =3D 0; i < nvhosts; i++) { + r =3D virtio_device_grab_ioeventfd(dev); + if (r < 0) { + error_report("vhost %d binding does not support host notifiers= ", i); + for (k =3D 0; k < i; k++) { + virtio_device_release_ioeventfd(dev); + } + return r; + } + } + /* * Batch all the host notifiers in a single transaction to avoid * quadratic time complexity in address_space_update_ioeventfds(). @@ -247,16 +262,6 @@ static int vhost_net_enable_notifiers(VirtIODevice *de= v, =20 net =3D get_vhost_net(peer); hdev =3D &net->dev; - /* - * We will pass the notifiers to the kernel, make sure that QEMU - * doesn't interfere. - */ - r =3D virtio_device_grab_ioeventfd(dev); - if (r < 0) { - error_report("binding does not support host notifiers"); - memory_region_transaction_commit(); - goto fail_nvhosts; - } =20 for (j =3D 0; j < hdev->nvqs; j++) { r =3D virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), @@ -277,6 +282,14 @@ static int vhost_net_enable_notifiers(VirtIODevice *de= v, return 0; fail_nvhosts: vhost_net_disable_notifiers_nvhosts(dev, ncs, data_queue_pairs, i); + /* + * This for loop starts from i+1, not i, because the i-th ioeventfd + * has already been released in vhost_dev_disable_notifiers_nvqs(). + */ + for (k =3D i + 1; k < nvhosts; k++) { + virtio_device_release_ioeventfd(dev); + } + return r; } =20 --=20 2.42.0.windows.2