Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2475243imm; Mon, 28 May 2018 08:47:44 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpZ6UR1Rq1Z5ow12racCCrqH/JNf+NpGxbETmDpp13OCWGqjOJGF+KkJ4KZpplTktLqr+sJ X-Received: by 2002:a17:902:24a5:: with SMTP id w34-v6mr14083977pla.52.1527522464559; Mon, 28 May 2018 08:47:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527522464; cv=none; d=google.com; s=arc-20160816; b=Ky4ZtTeBH7Xy9/daJYJOrWBJq1aTpuaaJcd62uYFCTEOORn9SdhZaQnmGMpGKKyxbJ iSpMdpK6TNfD84DYSAXswO3oLsn9HEI21NOlxCYlggMlLrWBg5ItY/UIVo/55Mlu6oGj eDYu3Z7RESxbv1sQnu1Esece23xQToixmBLzpDUsP2zJsZinn7+TYTB12XFLCVTXQMIA 5bZ+N+U3DXGMObH2wQ9FZpoKHAcOwt3e3qFeER9sd+wcYGjdpvwQYrNkAXKizMbq7tVB sJKVktnf6jSGOmdxNeSYm/jw9MbaI+rS8UfYrQnVqLvjUM2eUp6eNJNLZZ+cenU0D9Ld CZMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=m/qb1/I6UlGqSfTfRvArSnCTx2MkBGUPrvo9Fqy/PgQ=; b=RBNRBEEFMEjb8ZUEn1/idlMZD1OBeDgNNu4TD9lLAVWCowqMt1eyFhoPK7i9w38miC 2u4jxJ+i3lacs5lQOAa+ZWcKemtFNsvTQ3I9QCov65Ts6YRi/CRitkgnUKQp0kqy1JOb eGXRQ3I7aYcE0s01aixTRGuUwBN/RYKpPBLQOzYl2ZGZRtPAcKSHxu+s9gqEU3qBWybv cA0vG51elhvnT6Ch7sLDW3Vu8lJsXcMLzYC37yDLrK7jWfbljy9WYLYzt8L4oZ/Okz4S bJ6scVWWaFOplcbtvcH7uqEPb/7yUoxbvU8nKVWEbc+ZOUk8iBNxKiD76vPPT++NygsU vjtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=k/oj89Kf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ay2-v6si30212040plb.210.2018.05.28.08.47.28; Mon, 28 May 2018 08:47:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=k/oj89Kf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1034822AbeE1PrA (ORCPT + 99 others); Mon, 28 May 2018 11:47:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:36758 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968517AbeE1KQg (ORCPT ); Mon, 28 May 2018 06:16:36 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 528A62086D; Mon, 28 May 2018 10:16:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527502595; bh=xsnke6DhTkwhycdFSGXVFalCKyOQeDDonnWp4DF5lfo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k/oj89KfccdB6dKe2o0ZDnoEunBjLo8bEYKI6m/A4/xo8LkgQCXD0vYH8EyqagGVZ Ye60a4CGZWp5KRkg2YXvf3mbCJckLF8fudV58ohSYvmpirljbJlTTAvpekB8gZkoeV JOp7+KETEJYrXSHOvR8uLc4B8NgKUPa+FpFVTBgc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ross Lagerwall , Boris Ostrovsky , Juergen Gross , Sasha Levin Subject: [PATCH 4.4 059/268] xen-netfront: Fix race between device setup and open Date: Mon, 28 May 2018 12:00:33 +0200 Message-Id: <20180528100208.647996715@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100202.045206534@linuxfoundation.org> References: <20180528100202.045206534@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ross Lagerwall [ Upstream commit f599c64fdf7d9c108e8717fb04bc41c680120da4 ] When a netfront device is set up it registers a netdev fairly early on, before it has set up the queues and is actually usable. A userspace tool like NetworkManager will immediately try to open it and access its state as soon as it appears. The bug can be reproduced by hotplugging VIFs until the VM runs out of grant refs. It registers the netdev but fails to set up any queues (since there are no more grant refs). In the meantime, NetworkManager opens the device and the kernel crashes trying to access the queues (of which there are none). Fix this in two ways: * For initial setup, register the netdev much later, after the queues are setup. This avoids the race entirely. * During a suspend/resume cycle, the frontend reconnects to the backend and the queues are recreated. It is possible (though highly unlikely) to race with something opening the device and accessing the queues after they have been destroyed but before they have been recreated. Extend the region covered by the rtnl semaphore to protect against this race. There is a possibility that we fail to recreate the queues so check for this in the open function. Signed-off-by: Ross Lagerwall Reviewed-by: Boris Ostrovsky Signed-off-by: Juergen Gross Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/net/xen-netfront.c | 46 +++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) --- a/drivers/net/xen-netfront.c +++ b/drivers/net/xen-netfront.c @@ -342,6 +342,9 @@ static int xennet_open(struct net_device unsigned int i = 0; struct netfront_queue *queue = NULL; + if (!np->queues) + return -ENODEV; + for (i = 0; i < num_queues; ++i) { queue = &np->queues[i]; napi_enable(&queue->napi); @@ -1363,18 +1366,8 @@ static int netfront_probe(struct xenbus_ #ifdef CONFIG_SYSFS info->netdev->sysfs_groups[0] = &xennet_dev_group; #endif - err = register_netdev(info->netdev); - if (err) { - pr_warn("%s: register_netdev err=%d\n", __func__, err); - goto fail; - } return 0; - - fail: - xennet_free_netdev(netdev); - dev_set_drvdata(&dev->dev, NULL); - return err; } static void xennet_end_access(int ref, void *page) @@ -1743,8 +1736,6 @@ static void xennet_destroy_queues(struct { unsigned int i; - rtnl_lock(); - for (i = 0; i < info->netdev->real_num_tx_queues; i++) { struct netfront_queue *queue = &info->queues[i]; @@ -1753,8 +1744,6 @@ static void xennet_destroy_queues(struct netif_napi_del(&queue->napi); } - rtnl_unlock(); - kfree(info->queues); info->queues = NULL; } @@ -1770,8 +1759,6 @@ static int xennet_create_queues(struct n if (!info->queues) return -ENOMEM; - rtnl_lock(); - for (i = 0; i < *num_queues; i++) { struct netfront_queue *queue = &info->queues[i]; @@ -1780,7 +1767,7 @@ static int xennet_create_queues(struct n ret = xennet_init_queue(queue); if (ret < 0) { - dev_warn(&info->netdev->dev, + dev_warn(&info->xbdev->dev, "only created %d queues\n", i); *num_queues = i; break; @@ -1794,10 +1781,8 @@ static int xennet_create_queues(struct n netif_set_real_num_tx_queues(info->netdev, *num_queues); - rtnl_unlock(); - if (*num_queues == 0) { - dev_err(&info->netdev->dev, "no queues\n"); + dev_err(&info->xbdev->dev, "no queues\n"); return -EINVAL; } return 0; @@ -1839,6 +1824,7 @@ static int talk_to_netback(struct xenbus goto out; } + rtnl_lock(); if (info->queues) xennet_destroy_queues(info); @@ -1849,6 +1835,7 @@ static int talk_to_netback(struct xenbus info->queues = NULL; goto out; } + rtnl_unlock(); /* Create shared ring, alloc event channel -- for each queue */ for (i = 0; i < num_queues; ++i) { @@ -1945,8 +1932,10 @@ abort_transaction_no_dev_fatal: xenbus_transaction_end(xbt, 1); destroy_ring: xennet_disconnect_backend(info); + rtnl_lock(); xennet_destroy_queues(info); out: + rtnl_unlock(); device_unregister(&dev->dev); return err; } @@ -1982,6 +1971,15 @@ static int xennet_connect(struct net_dev netdev_update_features(dev); rtnl_unlock(); + if (dev->reg_state == NETREG_UNINITIALIZED) { + err = register_netdev(dev); + if (err) { + pr_warn("%s: register_netdev err=%d\n", __func__, err); + device_unregister(&np->xbdev->dev); + return err; + } + } + /* * All public and private state should now be sane. Get * ready to start sending and receiving packets and give the driver @@ -2172,10 +2170,14 @@ static int xennet_remove(struct xenbus_d xennet_disconnect_backend(info); - unregister_netdev(info->netdev); + if (info->netdev->reg_state == NETREG_REGISTERED) + unregister_netdev(info->netdev); - if (info->queues) + if (info->queues) { + rtnl_lock(); xennet_destroy_queues(info); + rtnl_unlock(); + } xennet_free_netdev(info->netdev); return 0;