From patchwork Thu Nov 9 11:29:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 13450943 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 7613DC4332F for ; Thu, 9 Nov 2023 11:30:25 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r13Dz-00076r-Jw; Thu, 09 Nov 2023 06:29:27 -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 1r13Dx-00076a-6Y for qemu-devel@nongnu.org; Thu, 09 Nov 2023 06:29:25 -0500 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r13Du-0003MV-PB for qemu-devel@nongnu.org; Thu, 09 Nov 2023 06:29:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=MIME-Version:Content-Type:Date:Cc:To: From:Subject:Message-ID:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=vXHB3lMo5sCs/McFbt92Zb5GowAnLqKNuqKpXrhFEfk=; b=MaUE6spuxdFqVg64cDHHj0r1dK yxS681KGnoXhcp0kc94pTYw6hf983blEpGqSbDU2ywLdmIOYSCeK79+FB5Q6yytC0n9FR0JBXEIGy PgsbwZPMhuTL7uxXhpfDxJV+LuHPNux8MJ/DzfaJN5oFR9H8pU8aQVRO/4NojmJYlUseQagwxShvH wpC52F+CfIm/bZiTL7kA9VL5UJLO3F+dfGEUhLx2gd25Mdu2xF4AqbU9+cgaZrx8N0Tnsr098bsoC xyezevLm+nagxuZDQW9q+nFrZEWnHnA00CC7RBf3XIbmotHVx5sLg72heTjct454vmBRpAFDIVoLa q/6Ln3SA==; Received: from [2001:8b0:10b:5:e60c:fbc4:6b73:8eff] (helo=u3832b3a9db3152.ant.amazon.com) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1r13Dp-006yIf-Cf; Thu, 09 Nov 2023 11:29:18 +0000 Message-ID: <61ea91785772a8138ad12b305cbd5aac4aa1e86a.camel@infradead.org> Subject: [PATCH 8.2 bugfix] net: do not delete nics in net_cleanup() From: David Woodhouse To: Jason Wang Cc: qemu-devel Date: Thu, 09 Nov 2023 11:29:17 +0000 User-Agent: Evolution 3.44.4-0ubuntu2 MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Received-SPF: none client-ip=2001:8b0:10b:1236::1; envelope-from=BATV+c182118e23cc89d32fc1+7382+infradead.org+dwmw2@casper.srs.infradead.org; helo=casper.infradead.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 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 From: David Woodhouse In net_cleanup() we only need to delete the netdevs, as those may have state which outlives QEMU when it exits, and thus may actually need to be cleaned up on exit. The nics, on the other hand, are owned by the device which created them. Most devices don't bother to clean up on exit because they don't have any state which will outlive QEMU... but XenBus devices do need to clean up their nodes in XenStore, and do have an exit handler to delete them. When the XenBus exit handler destroys the xen-net-device, it attempts to delete its nic after net_cleanup() had already done so. And crashes. Fix this by only deleting netdevs as we walk the list in net_cleanup(). As the comment notes, we can't use QTAILQ_FOREACH_SAFE() as each deletion may remove *multiple* entries, including the "safely" saved 'next' pointer. But we can store the *previous* entry, since nics are safe. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant ---  net/net.c | 28 ++++++++++++++++++++++------  1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/net/net.c b/net/net.c index c0c0cbe99e..bbe33da176 100644 --- a/net/net.c +++ b/net/net.c @@ -1499,18 +1499,34 @@ static void net_vm_change_state_handler(void *opaque, bool running,    void net_cleanup(void)  { -    NetClientState *nc; +    NetClientState *nc, **p = &QTAILQ_FIRST(&net_clients);        /*cleanup colo compare module for COLO*/      colo_compare_cleanup();   -    /* We may del multiple entries during qemu_del_net_client(), -     * so QTAILQ_FOREACH_SAFE() is also not safe here. +    /* +     * Walk the net_clients list and remove the netdevs but *not* any +     * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device +     * model which created them, and in some cases (e.g. xen-net-device) +     * the device itself may do cleanup at exit and will be upset if we +     * just delete its NIC from underneath it. +     * +     * Since qemu_del_net_client() may delete multiple entries, using +     * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer +     * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep +     * 'p' pointing to either the head of the list, or the 'next' field +     * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk +     * the list. +     * +     * The 'nc' variable isn't part of the list traversal; it's purely +     * for convenience as too much '(*p)->' has a tendency to make the +     * readers' eyes bleed.       */ -    while (!QTAILQ_EMPTY(&net_clients)) { -        nc = QTAILQ_FIRST(&net_clients); +    while (*p) { +        nc = *p;          if (nc->info->type == NET_CLIENT_DRIVER_NIC) { -            qemu_del_nic(qemu_get_nic(nc)); +            /* Skip NET_CLIENT_DRIVER_NIC entries */ +            p = &QTAILQ_NEXT(nc, next);          } else {              qemu_del_net_client(nc);          }