From nobody Mon Nov 23 04:34:25 2026 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=quarantine dis=none) header.from=openvz.org ARC-Seal: i=1; a=rsa-sha256; t=1793995426; cv=none; d=zohomail.com; s=zohoarc; b=El5PhGmXQOwJp4XqLGIxyrfu2fMwPKm5DrxtMR08taBA/UH2rQAOd5lM+Y/O4bQ61d4kxaIPR2Y29v/vzSUJ2O222oi5MxlmjqrZUTTACSt0yi7InS563CzlleYmXzujq0W9/9Kbu5OeriysczdVUmgEcZCdp+nA1Cf8hhAksBg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793995426; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0A4sVlENlNU1c//EuQKD+X3QRpd1z9onjZ/ZxRfdywM=; b=jByK/t3stR1iBP6WmWXtoVg1Ii13nLx4ueFMLwdmUoHiJTpR9nT7Inh2tidXkcGqrrtBhrtMh1TiGr4PlootWw5F8bg6Xw9Jd9NqMrcJz0Z8OQjNG6MfaSm8n3cVR7dAKmOs9DPaKdEXxKdjboIXXqFbTMlMN3S+Nl8Cx+mJ++I= 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=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793995426401688.0759768308443; Fri, 6 Nov 2026 12:03:46 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xPQ9g-0003Be-SU; Fri, 06 Nov 2026 15:03:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xPQ9f-0003Aj-2i for qemu-devel@nongnu.org; Fri, 06 Nov 2026 15:03:19 -0500 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xPQ9c-0004mi-Ne for qemu-devel@nongnu.org; Fri, 06 Nov 2026 15:03:18 -0500 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-49d05d51553so7026765e9.2 for ; Fri, 06 Nov 2026 12:03:16 -0800 (PST) Received: from athena.sw.ru ([2a06:5b06:b600:300:1983:68c3:80e8:4ae2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4b7e3271867sm4660995e9.6.2026.11.06.12.03.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 12:03:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=google; t=1793995395; x=1794600195; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=0A4sVlENlNU1c//EuQKD+X3QRpd1z9onjZ/ZxRfdywM=; b=tM6zZVupBeZTuYnhd6hleje9OPVVhBO6OuhWPsS8Cr8AoEP5S9vfJhmyki6KDCkAsq F7FeES3aSfoVhmAfgq06MJXuE34+OHwETdDkz4KAE3YUNi2nqHfjXHusQSNmDmr4Zp2F LADq/EJ9N/3flj/RcgY1Pf/7iSRVM7sdHIA44HhnE46XO2HxjlriWJ8+LCKg29z1GQSf YI9SiGcahns3TFcRdFykABQdMuZjXeSHfwyWW4+mkOvAl38v4W55NwxLV7JyHqrlS53Q fbZt1PhYoIDmGChkTJ4pSy3+haJT1I5wGmSww1RGlrPF48dp5A0M5qUA9iBLpkHwlKEt /e/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793995395; x=1794600195; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=0A4sVlENlNU1c//EuQKD+X3QRpd1z9onjZ/ZxRfdywM=; b=YXgIKH7sKf8JosGISaqDx8pNsZSVzF7yRjiXxx7oKmhaw6DYgIeEMnf4AxrRK3D7Nf dMioKsu5u6xT8K2Lxcsy5iyOb6UHdznZ/XaPioUJN8Cso9yhx8kpNAQPSUxmubuZtoeq 15s6z+WHf6PNZPBBoDJOgX7jkfoqaskO+44LJOkHLxBhE/ZYcc3n0DdQNRQBVXCzuOhE yCKIwt03umpmFNbgeuRDbL32AZnMDsfz/llnOdK99nV0z6vGfdcuaj5xRPu285ZGc0Nc Vmx5oLn9XNZbbW0XxRM2Qxa3RMAoPbkig+XTF3sWQlWPkP2v1q2E4GSl+B9LKWX0FpnB U8SQ== X-Gm-Message-State: APuU4Z81AsStXrPu35WB83EiBwIWGxsaiKapj8/yNea5mJOMl8N4pktt ektw7ipf6an6aJ3iQyVtMGx2GhOrfDw7NlPC3RpUE7+fojq6De1z+J7RvjH0jlgk1aG5saoZF2Z zXcU5 X-Gm-Gg: AYBFou00NTGccLxUbxUJNroPp2kDRGHgiGCYzUZ4omveIiG4UGCoPaudZZWH6MrQc4U LJi/0F7fzqjBI8DT2XGksKxEBZDRivzemyrv8BS/wZpyfjNSgRTMQMOeV0exINx6REDQz7Yp6Bc iq7FxfLzIk9+Dy/tUVXTCZJjuGGuqo2npOHL7QR8pNrhYzZKx1S0vVsSzp21+5RhKLZqsxp0j6u kqENhQiR4oo2+69wlcZrhu4qZOCvm4t/Pp8IS7kaXaIb+Ud2gbD1fsZBJsRUWdypcTrWBzh2IZ1 sj6KiDUcj56JshCxjEwifKvkgFxZmEIjleyaukG1sqfSq+LTGjjMz1zqK6K0tvR4hChE6z9Gk/6 1zuUnTJOfvFHBsqxtk3vHymF9MCvWAPbPpUSk/qtT1yh+5Uao8rMZSsTsGIMEIkznTeWmGxK6cB 7ZecSrpnyUCKZIOGmPqu2d5MOzq4DD0W31HvtBYxnSFyjaSQtMjR9EZEkQW0m5x1cF5IwlA0NnG Atjp3YkbE8S48q6I1m6JKEznMPxxktITugrRugiD02HwmNMcK805p8y96MSMVLoGDtIt0x4tZy2 ZGNo6KVgW3iVW8J+KHuBVvgh9Q== X-Received: by 2002:a05:600c:4704:b0:4a1:faa1:3813 with SMTP id 5b1f17b1804b1-4b6e035edbamr72047315e9.27.1793995394905; Fri, 06 Nov 2026 12:03:14 -0800 (PST) From: "Denis V. Lunev" To: qemu-devel@nongnu.org Cc: den@openvz.org, "Michael S. Tsirkin" , Jason Wang , Vladimir Sementsov-Ogievskiy , Fabiano Rosas , Laurent Vivier , Paolo Bonzini Subject: [PATCH 1/2] virtio-net: fix abort in virtio_net_set_queue_pairs() on a deleted tap Date: Fri, 6 Nov 2026 21:03:08 +0100 Message-ID: <20261106200309.122830-2-den@openvz.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261106200309.122830-1-den@openvz.org> References: <20261106200309.122830-1-den@openvz.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=den@openvz.org; helo=mail-wm1-x334.google.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, 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: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @openvz.org) X-ZM-MESSAGEID: 1793995428932158501 Content-Type: text/plain; charset="utf-8" From: Denis V. Lunev Once the tap device behind a multiqueue netdev is deleted on the host, the queue fds QEMU holds are no longer bound to any device and TUNSETQUEUE fails with EINVAL, for attach and detach alike. Management can delete the device under a running VM, e.g. OpenStack unplugs the VIF of an instance it is deleting. The next VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET from the guest then kills QEMU: could not enable queue virtio_net_set_queue_pairs: Assertion `!r' failed. A guest reset aborts the same way through set_features(): clearing the features detaches the extra queues and the detach fails too. Commit a1708203b583 ("net/tap-linux.c: avoid abort when setting invalid fd") removed the earlier abort on the same path in tap_set_vnet_hdr_len(); this is the next one. tap_fd_enable() and tap_fd_disable() already report the error. Walk the remaining queues anyway, return the failure and let the callers decide: - the control command restores the previous number of queue pairs and returns VIRTIO_NET_ERR, so the device matches the guest, which keeps its old setting on error. The rollback cannot fail in turn: tap tracks which queues are attached and skips the ioctl for a queue whose attach or detach already failed; - post-load fails the migration, so the source keeps running instead of the destination aborting; - set_features() has no way to report it and carries on with a dead link, as a single queue NIC on a deleted tap already does. The test attaches its own copy of the second queue fd before the guest asks for two queue pairs, so the attach from QEMU fails with EINVAL on a live device. Creating the tap needs CAP_NET_ADMIN, so the test is skipped without it. Fixes: fed699f9ca6a ("virtio-net: multiqueue support") Signed-off-by: Denis V. Lunev Cc: "Michael S. Tsirkin" Cc: Jason Wang Cc: Vladimir Sementsov-Ogievskiy Cc: Fabiano Rosas Cc: Laurent Vivier Cc: Paolo Bonzini --- hw/net/virtio-net.c | 25 +++++--- tests/qtest/virtio-net-test.c | 104 ++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 7 deletions(-) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 54e42cfec9..82e3c22ca3 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -766,24 +766,27 @@ static int peer_detach(VirtIONet *n, int index) return tap_disable(nc->peer); } =20 -static void virtio_net_set_queue_pairs(VirtIONet *n) +static int virtio_net_set_queue_pairs(VirtIONet *n) { int i; int r; + int ret =3D 0; =20 if (n->nic->peer_deleted) { - return; + return 0; } =20 for (i =3D 0; i < n->max_queue_pairs; i++) { if (i < n->curr_queue_pairs) { r =3D peer_attach(n, i); - assert(!r); } else { r =3D peer_detach(n, i); - assert(!r); + } + if (r < 0) { + ret =3D r; } } + return ret; } =20 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); @@ -1499,7 +1502,7 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t= cmd, struct iovec *iov, unsigned int iov_cnt) { VirtIODevice *vdev =3D VIRTIO_DEVICE(n); - uint16_t queue_pairs; + uint16_t queue_pairs, old_queue_pairs; NetClientState *nc =3D qemu_get_queue(n->nic); =20 virtio_net_disable_rss(n); @@ -1532,6 +1535,7 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t= cmd, return VIRTIO_NET_ERR; } =20 + old_queue_pairs =3D n->curr_queue_pairs; n->curr_queue_pairs =3D queue_pairs; if (nc->peer && nc->peer->info->type =3D=3D NET_CLIENT_DRIVER_VHOST_VD= PA) { /* @@ -1543,7 +1547,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_= t cmd, /* stop the backend before changing the number of queue_pairs to avoid= handling a * disabled queue */ virtio_net_set_status(vdev, vdev->status); - virtio_net_set_queue_pairs(n); + if (virtio_net_set_queue_pairs(n) < 0) { + n->curr_queue_pairs =3D old_queue_pairs; + virtio_net_set_status(vdev, vdev->status); + virtio_net_set_queue_pairs(n); + return VIRTIO_NET_ERR; + } =20 return VIRTIO_NET_OK; } @@ -3298,7 +3307,9 @@ static int virtio_net_post_load_device(void *opaque, = int version_id) */ n->saved_guest_offloads =3D n->curr_guest_offloads; =20 - virtio_net_set_queue_pairs(n); + if (virtio_net_set_queue_pairs(n) < 0) { + return -EINVAL; + } =20 /* Find the first multicast entry in the saved MAC filter */ for (i =3D 0; i < n->mac_table.in_use; i++) { diff --git a/tests/qtest/virtio-net-test.c b/tests/qtest/virtio-net-test.c index 60e5229a3d..9224ee05d9 100644 --- a/tests/qtest/virtio-net-test.c +++ b/tests/qtest/virtio-net-test.c @@ -16,6 +16,12 @@ #include "libqos/qgraph.h" #include "libqos/virtio-net.h" =20 +#ifdef CONFIG_LINUX +#include +#include +#include +#endif + #ifndef ETH_P_RARP #define ETH_P_RARP 0x8035 #endif @@ -285,6 +291,97 @@ static void *virtio_net_test_setup(GString *cmd_line, = void *arg) return sv; } =20 +#ifdef CONFIG_LINUX +static void virtio_net_test_cleanup_tap(void *opaque) +{ + int *fds =3D opaque; + int i; + + qos_invalidate_command_line(); + for (i =3D 0; i < 2; i++) { + if (fds[i] >=3D 0) { + close(fds[i]); + } + } + g_free(fds); +} + +/* arg is the number of tap queues, 1 or 2 */ +static void *virtio_net_test_setup_tap(GString *cmd_line, void *arg) +{ + struct ifreq ifr =3D { + .ifr_flags =3D IFF_TAP | IFF_NO_PI | IFF_VNET_HDR | IFF_MULTI_QUEU= E, + }; + int queues =3D GPOINTER_TO_INT(arg); + int *fds =3D g_new(int, 2); + int i; + + fds[0] =3D fds[1] =3D -1; + /* TUNSETIFF returns the device name in ifr, the next call attaches to= it */ + for (i =3D 0; i < queues; i++) { + fds[i] =3D open("/dev/net/tun", O_RDWR); + if (fds[i] < 0 || ioctl(fds[i], TUNSETIFF, &ifr) < 0) { + break; + } + } + + if (i < queues) { + for (i =3D 0; i < 2; i++) { + if (fds[i] >=3D 0) { + close(fds[i]); + fds[i] =3D -1; + } + } + g_string_append(cmd_line, " -netdev hubport,hubid=3D0,id=3Dhs0 "); + } else if (queues =3D=3D 1) { + g_string_append_printf(cmd_line, " -netdev tap,fd=3D%d,id=3Dhs0 ", + fds[0]); + } else { + g_string_append_printf(cmd_line, " -netdev tap,fds=3D%d:%d,id=3Dhs= 0 ", + fds[0], fds[1]); + } + + g_test_queue_destroy(virtio_net_test_cleanup_tap, fds); + return fds; +} + +static void mq_tap_attach_fail(void *obj, void *data, QGuestAllocator *t_a= lloc) +{ + QVirtioNet *net =3D obj; + int *fds =3D data; + QTestState *qts =3D global_qtest; + QVirtQueue *vq =3D net->queues[net->n_queues - 1]; + struct ifreq ifr =3D { .ifr_flags =3D IFF_ATTACH_QUEUE }; + uint8_t cmd[4] =3D { VIRTIO_NET_CTRL_MQ, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_S= ET }; + uint64_t addr; + uint32_t head; + + if (fds[0] < 0) { + g_test_skip("cannot create a multiqueue tap device"); + return; + } + + if (qvirtio_is_big_endian(net->vdev)) { + stw_be_p(cmd + 2, 2); + } else { + stw_le_p(cmd + 2, 2); + } + + /* Grab queue 1 so that QEMU fails to attach it */ + g_assert_cmpint(ioctl(fds[1], TUNSETQUEUE, &ifr), =3D=3D, 0); + + addr =3D guest_alloc(t_alloc, sizeof(cmd) + 1); + memwrite(addr, cmd, sizeof(cmd)); + head =3D qvirtqueue_add(qts, vq, addr, sizeof(cmd), false, true); + qvirtqueue_add(qts, vq, addr + sizeof(cmd), 1, true, false); + qvirtqueue_kick(qts, net->vdev, vq, head); + qvirtio_wait_used_elem(qts, net->vdev, vq, head, NULL, + QVIRTIO_NET_TIMEOUT_US); + g_assert_cmpint(readb(addr + sizeof(cmd)), =3D=3D, VIRTIO_NET_ERR); + guest_free(t_alloc, addr); +} +#endif /* CONFIG_LINUX */ + #endif /* _WIN32 */ =20 static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc) @@ -328,6 +425,13 @@ static void register_virtio_net_test(void) qos_add_test("basic", "virtio-net", send_recv_test, &opts); qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts); qos_add_test("announce-self", "virtio-net", announce_self, &opts); +#ifdef CONFIG_LINUX + opts.before =3D virtio_net_test_setup_tap; + opts.arg =3D GINT_TO_POINTER(2); + opts.edge.extra_device_opts =3D "mq=3Don"; + qos_add_test("mq-tap-attach-fail", "virtio-net", mq_tap_attach_fail, &= opts); + opts.edge.extra_device_opts =3D NULL; +#endif #endif =20 /* These tests do not need a loopback backend. */ base-commit: 76fdb3373c9a27b3f50ecc9e012cbdeb90af7151 --=20 2.53.0 From nobody Mon Nov 23 04:34:25 2026 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=quarantine dis=none) header.from=openvz.org ARC-Seal: i=1; a=rsa-sha256; t=1793995426; cv=none; d=zohomail.com; s=zohoarc; b=TMpK6R22GbIn1TGePT8ktWyUEIC9mTaZ2hjB0VA/XS0sDLhGo4F7cHZiAPHK/rVIYKf3RdaP6VhkFIFIjJoHuRSipseRejAgfkwIpC3roUZBb3q/Uf2GG7+W7mk55AVHnHy92SPE86UNFcRgyOOtk992q3LWTLIabTbknSn1O0w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793995426; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=hFUTERdPrSqmP8xKhWv9s8cMoPJQwZU0Cgpn/MagI5U=; b=gMsLOxjKyGagvmPay1ytFwWhNGGQhW0AZS9qaVO4cbF+6eTLr+Fp4+u5ZRuRijAdirAO/1Cwzr0xpoqbYeramBb+GW/ZR2vXtol2DEeJ5xgj1g08ku8HmVz4mHr7EIg3OwMZqXdUjmpBQmQpQVLvfK/4t1kcS8CgO197RO5rps8= 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=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793995426088747.3164684251636; Fri, 6 Nov 2026 12:03:46 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xPQ9i-0003Bj-0G; Fri, 06 Nov 2026 15:03:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xPQ9g-0003B8-1r for qemu-devel@nongnu.org; Fri, 06 Nov 2026 15:03:20 -0500 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xPQ9e-0004mp-4k for qemu-devel@nongnu.org; Fri, 06 Nov 2026 15:03:19 -0500 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-4a2072b9832so48545e9.1 for ; Fri, 06 Nov 2026 12:03:17 -0800 (PST) Received: from athena.sw.ru ([2a06:5b06:b600:300:1983:68c3:80e8:4ae2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4b7e3271867sm4660995e9.6.2026.11.06.12.03.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 12:03:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=google; t=1793995396; x=1794600196; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=hFUTERdPrSqmP8xKhWv9s8cMoPJQwZU0Cgpn/MagI5U=; b=KfgLYx63PWKuDaLZ8BGEYDLnMmQs2dZCmfTI8WkfEqPmP1RMW0NnBTHOmTfa36De+u jsjkQKeI31UQG9OgJ2+70M4UZB5gwya+81mPLnr6/eoBG8BuP+Yxc4Ocbl+geoa4fKdU gLqrJGbhkIFT3rrTAYOK6tJNl931RshwtbusJQrOHfocy5LH3CfcfdOjCx9fWxmfh0H8 xmPosAfT4i8Exj4euKKd0pbd/mJJEe6C2fZWL1oXRkpMIHcAB/2xizGDI+dA0fC0lGts lZGZAR+psJ45xqldr6skoTX8iTYCbJ5Z/pXkHU54Tkj/9V8H4uvDL7ZnnRkgaXUQQ7qC WQdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793995396; x=1794600196; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=hFUTERdPrSqmP8xKhWv9s8cMoPJQwZU0Cgpn/MagI5U=; b=Gt3al1Y7wkkC38707dQ72RLFF8UNQxalY43FigunY9X+8n+asIF975scZ1K4yII2js 3x74eGAaq4/H/jBbIxhjGSuNdN305zdwCj2kpN/rqHSRjiULteDOpvaRInt03lKso3X5 2cDrRV4GCYQs6K77SB5qmIn7NnBtf6DVk9iHPHkWdkQpueLL75GgjZHicKis5eDHNccJ /h0Y2jcMAlf2GES/ST/qd0kuCHPoDD294HWEtzFqeZ4oaXb92vKD+DW80IA8li6zzCou 8+arqdR9kOem4KKp/AlnlyxMrAn+HpuOZndO3Fz9Q1A8GkuDecHCvaQ8OVzaik5tQ8B1 od0w== X-Gm-Message-State: APuU4Z9vsgGRqiGP6jqKOPXlR7jRL7u3yUWxkYoZV+HJDRgoaUptC4xU gD18KFYcGB3IZHNdUO75jEZGPnXAbtGJxfPtl+XRQAB+oK0Z559UjEEa+XnRFCBwHlBqWJ36J1x 0UfWu X-Gm-Gg: AYBFou3LY1ne3KJoFjRRwOSdPBKx8ajw0d0UsQFesV1SMKu1zdslHo7f3lN3+tIqyOX bGrV/N5ytZNw/YEgekawMgVNLi6dIou01Wc7kj65oF14l/VySnczRpYD4hCege0hgH+Ou1lHhGg 3WdDgFY/flB2NEubBBWBYIr1k4eRJxVj/X0hr3rj7bFBwibvT5iXIDkXPYCxfkMoJruXhyCt7s3 oohV/fsTvptCO7IOe5gniyPUY3V3n3qOapXost3PD304QCES1s6lzHNlzn8wl0UTlnDNrAsw3fE 5kOVG8d2BR8J1g3dIsq3iLBE2CwssezBIn5Fji0PALmMSRwauKlSkM3rbJYwf3/Zx9G9wrWCC+Y nvFqXXJjPMFq4/EOZGVp6LMOCLdY0cY0bCTQBr/UNpUxyoVNyFwMQBt2Zz1qVvPq2MNFC4+hyoL 3QXkNqwpUcONdVEV/cTrlzfSVE4di5eNQ1wTDmrCZDTkDWa915AOw5AlmReiifMmHIOSDPaqsx+ VtKgF0M1OSvDw8gDyXzrd27aLFaQ4iuD0eJrTSvW0KGWcNGUe2AiVz9evLdSfv/HFXDe19kCNMC OYB/bY0kwmFg12OM4qvEYi27Fg== X-Received: by 2002:a05:600c:8115:b0:4a2:10f9:37db with SMTP id 5b1f17b1804b1-4b8190f468amr881655e9.7.1793995396420; Fri, 06 Nov 2026 12:03:16 -0800 (PST) From: "Denis V. Lunev" To: qemu-devel@nongnu.org Cc: den@openvz.org, Jason Wang , Vladimir Sementsov-Ogievskiy , Fabiano Rosas , Laurent Vivier , Paolo Bonzini Subject: [PATCH 2/2] net/tap: fix busy loop in tap_send() after the tap device is deleted Date: Fri, 6 Nov 2026 21:03:09 +0100 Message-ID: <20261106200309.122830-3-den@openvz.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261106200309.122830-1-den@openvz.org> References: <20261106200309.122830-1-den@openvz.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::32e; envelope-from=den@openvz.org; helo=mail-wm1-x32e.google.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, 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: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @openvz.org) X-ZM-MESSAGEID: 1793995428884158500 Content-Type: text/plain; charset="utf-8" From: Denis V. Lunev When the tap device is deleted on the host while QEMU still holds its fd, poll() reports EPOLLERR on the fd every time and read() fails with EBADFD. tap_send() just breaks out of its loop with the handler still registered, the main loop wakes up again at once and QEMU spins at full CPU until the VM is destroyed. Every tap netdev is affected, single queue or multiqueue. With vhost-net the spin starts once vhost stops, e.g. on guest reboot, since vhost removes the handler while it runs. Stop read polling on EBADFD and report it. Other read errors are left alone: tun fails a read with EINVAL for a single packet it cannot convert, e.g. an unsupported GSO type, and the next read succeeds. EBADFD only exists on Linux and Solaris, hence the #ifdef. Anything that turns polling back on later, a vhost stop or the delivery of a queued packet, hits EBADFD once and stops it again. The test deletes a single queue tap through netlink under a running guest and checks that the CPU time of QEMU stays low afterwards. Deleting the device needs CAP_NET_ADMIN, so the test is skipped without it. Signed-off-by: Denis V. Lunev Cc: Jason Wang Cc: Vladimir Sementsov-Ogievskiy Cc: Fabiano Rosas Cc: Laurent Vivier Cc: Paolo Bonzini --- net/tap.c | 8 ++++ tests/qtest/virtio-net-test.c | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/net/tap.c b/net/tap.c index 9bce0d088f..8274aebda6 100644 --- a/net/tap.c +++ b/net/tap.c @@ -265,6 +265,14 @@ static void tap_send(void *opaque) size_t min_pktsz =3D sizeof(min_pkt); =20 size =3D tap_read_packet(s->fd, s->buf, sizeof(s->buf)); +#ifdef EBADFD + if (size < 0 && errno =3D=3D EBADFD) { + error_report("tap: device is gone, stop polling: %s", + strerror(errno)); + tap_read_poll(s, false); + break; + } +#endif if (size <=3D 0) { break; } diff --git a/tests/qtest/virtio-net-test.c b/tests/qtest/virtio-net-test.c index 9224ee05d9..027fa6210b 100644 --- a/tests/qtest/virtio-net-test.c +++ b/tests/qtest/virtio-net-test.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #endif =20 #ifndef ETH_P_RARP @@ -380,6 +382,72 @@ static void mq_tap_attach_fail(void *obj, void *data, = QGuestAllocator *t_alloc) g_assert_cmpint(readb(addr + sizeof(cmd)), =3D=3D, VIRTIO_NET_ERR); guest_free(t_alloc, addr); } + +static unsigned long qemu_cpu_ticks(void) +{ + g_autofree char *path =3D g_strdup_printf("/proc/%d/stat", + qtest_pid(global_qtest)); + g_autofree char *stat =3D NULL; + g_auto(GStrv) f =3D NULL; + char *p; + + g_assert(g_file_get_contents(path, &stat, NULL, NULL)); + p =3D strrchr(stat, ')'); + g_assert(p); + /* utime and stime follow the 11 fields after the command name */ + f =3D g_strsplit(p + 2, " ", 14); + g_assert_cmpint(g_strv_length(f), =3D=3D, 14); + return g_ascii_strtoull(f[11], NULL, 10) + + g_ascii_strtoull(f[12], NULL, 10); +} + +static void tap_delete(int fd) +{ + struct ifreq ifr =3D { }; + struct { + struct nlmsghdr nh; + struct ifinfomsg ifi; + } req =3D { + .nh.nlmsg_len =3D sizeof(req), + .nh.nlmsg_type =3D RTM_DELLINK, + .nh.nlmsg_flags =3D NLM_F_REQUEST | NLM_F_ACK, + .ifi.ifi_family =3D AF_UNSPEC, + }; + struct { + struct nlmsghdr nh; + struct nlmsgerr err; + } ack; + int sock; + + g_assert_cmpint(ioctl(fd, TUNGETIFF, &ifr), =3D=3D, 0); + req.ifi.ifi_index =3D if_nametoindex(ifr.ifr_name); + g_assert_cmpint(req.ifi.ifi_index, >, 0); + + sock =3D socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + g_assert_cmpint(sock, >=3D, 0); + g_assert_cmpint(send(sock, &req, sizeof(req), 0), =3D=3D, sizeof(req)); + g_assert_cmpint(recv(sock, &ack, sizeof(ack), 0), =3D=3D, sizeof(ack)); + g_assert_cmpint(ack.nh.nlmsg_type, =3D=3D, NLMSG_ERROR); + g_assert_cmpint(ack.err.error, =3D=3D, 0); + close(sock); +} + +static void tap_deleted(void *obj, void *data, QGuestAllocator *t_alloc) +{ + int *fds =3D data; + unsigned long ticks; + + if (fds[0] < 0) { + g_test_skip("cannot create a tap device"); + return; + } + + tap_delete(fds[0]); + + ticks =3D qemu_cpu_ticks(); + g_usleep(G_USEC_PER_SEC); + g_assert_cmpuint(qemu_cpu_ticks() - ticks, <, sysconf(_SC_CLK_TCK) / 2= ); +} #endif /* CONFIG_LINUX */ =20 #endif /* _WIN32 */ @@ -431,6 +499,8 @@ static void register_virtio_net_test(void) opts.edge.extra_device_opts =3D "mq=3Don"; qos_add_test("mq-tap-attach-fail", "virtio-net", mq_tap_attach_fail, &= opts); opts.edge.extra_device_opts =3D NULL; + opts.arg =3D GINT_TO_POINTER(1); + qos_add_test("tap-deleted", "virtio-net", tap_deleted, &opts); #endif #endif =20 --=20 2.53.0