From patchwork Wed Nov 25 09:16:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Parav Pandit X-Patchwork-Id: 1405960 Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=nvidia.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=nvidia.com header.i=@nvidia.com header.a=rsa-sha256 header.s=n1 header.b=rbZgomLR; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4CgwJ83zJqz9sSn for ; Wed, 25 Nov 2020 20:16:56 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727374AbgKYJQy (ORCPT ); Wed, 25 Nov 2020 04:16:54 -0500 Received: from hqnvemgate25.nvidia.com ([216.228.121.64]:6703 "EHLO hqnvemgate25.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726928AbgKYJQx (ORCPT ); Wed, 25 Nov 2020 04:16:53 -0500 Received: from hqmail.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate25.nvidia.com (using TLS: TLSv1.2, AES256-SHA) id ; Wed, 25 Nov 2020 01:16:53 -0800 Received: from sw-mtx-036.mtx.labs.mlnx (172.20.13.39) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Wed, 25 Nov 2020 09:16:52 +0000 From: Parav Pandit To: , , , CC: Parav Pandit Subject: [PATCH net v2 1/2] devlink: Hold rtnl lock while reading netdev attributes Date: Wed, 25 Nov 2020 11:16:19 +0200 Message-ID: <20201125091620.6781-2-parav@nvidia.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201125091620.6781-1-parav@nvidia.com> References: <20201122061257.60425-3-parav@nvidia.com> <20201125091620.6781-1-parav@nvidia.com> MIME-Version: 1.0 X-Originating-IP: [172.20.13.39] X-ClientProxiedBy: HQMAIL107.nvidia.com (172.20.187.13) To HQMAIL107.nvidia.com (172.20.187.13) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1606295813; bh=xdvq/95jdyMMniYGD/4mEKAG8emco+ivwY+KlhCpUw0=; h=From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:Content-Type: X-Originating-IP:X-ClientProxiedBy; b=rbZgomLR5IskFiggbuU+KZquzNohCryHTSTIEu68Ah/n6duQPGYolkrMsB6OtquMt l2QX1QGnpP5lS0EEecyGjcE5s/7gcGlnIEPUl3Cm9Xf8IS9FZ7RgNVaYHQqLYzYiil yO9g1/3ZkOdnF1bcPj/FlvTtXZc+ZuG//HTCqVSt4PJ5Wnio5qEzpCQWxBNxadI9In 9xR4mB99PSr5lgJmLfAPfaCiaRDD1EBWX0U1nVGkxgh0Hve0yCO4SxKtTLC2ozgGe/ WJr9mI6YPoyGXywWegQiMdV00CMq8jeC+y2LaO+97yxHZnMK1LaCpm6PiGnB031sE8 1lghQRCJLSdaQ== Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org A netdevice of a devlink port can be moved to different net namespace than its parent devlink instance. This scenario occurs when devlink reload is not used. When netdevice is undergoing migration to net namespace, its ifindex and name may change. In such use case, devlink port query may read stale netdev attributes. Fix it by reading them under rtnl lock. Fixes: bfcd3a466172 ("Introduce devlink infrastructure") Signed-off-by: Parav Pandit --- Changelog: v1->v2: - avoided refactoring the code - updated commit log --- net/core/devlink.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/net/core/devlink.c b/net/core/devlink.c index acc29d5157f4..04ae67bbe19c 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -792,6 +792,8 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) goto nla_put_failure; + /* Hold rtnl lock while accessing port's netdev attributes. */ + rtnl_lock(); spin_lock_bh(&devlink_port->type_lock); if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type)) goto nla_put_failure_type_locked; @@ -818,6 +820,7 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, goto nla_put_failure_type_locked; } spin_unlock_bh(&devlink_port->type_lock); + rtnl_unlock(); if (devlink_nl_port_attrs_put(msg, devlink_port)) goto nla_put_failure; if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack)) @@ -828,6 +831,7 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, nla_put_failure_type_locked: spin_unlock_bh(&devlink_port->type_lock); + rtnl_unlock(); nla_put_failure: genlmsg_cancel(msg, hdr); return -EMSGSIZE; From patchwork Wed Nov 25 09:16:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Parav Pandit X-Patchwork-Id: 1405959 Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=nvidia.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=nvidia.com header.i=@nvidia.com header.a=rsa-sha256 header.s=n1 header.b=Egpx+tLU; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4CgwJ75RvYz9s0b for ; Wed, 25 Nov 2020 20:16:55 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727530AbgKYJQz (ORCPT ); Wed, 25 Nov 2020 04:16:55 -0500 Received: from hqnvemgate26.nvidia.com ([216.228.121.65]:13997 "EHLO hqnvemgate26.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727269AbgKYJQy (ORCPT ); Wed, 25 Nov 2020 04:16:54 -0500 Received: from hqmail.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate26.nvidia.com (using TLS: TLSv1.2, AES256-SHA) id ; Wed, 25 Nov 2020 01:16:56 -0800 Received: from sw-mtx-036.mtx.labs.mlnx (172.20.13.39) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Wed, 25 Nov 2020 09:16:53 +0000 From: Parav Pandit To: , , , CC: Parav Pandit Subject: [PATCH net v2 2/2] devlink: Make sure devlink instance and port are in same net namespace Date: Wed, 25 Nov 2020 11:16:20 +0200 Message-ID: <20201125091620.6781-3-parav@nvidia.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201125091620.6781-1-parav@nvidia.com> References: <20201122061257.60425-3-parav@nvidia.com> <20201125091620.6781-1-parav@nvidia.com> MIME-Version: 1.0 X-Originating-IP: [172.20.13.39] X-ClientProxiedBy: HQMAIL107.nvidia.com (172.20.187.13) To HQMAIL107.nvidia.com (172.20.187.13) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1606295816; bh=4w59AAqraJ4GdBe93WaSiu/9yugJKU4v1iUI+AkSXmc=; h=From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:Content-Type: X-Originating-IP:X-ClientProxiedBy; b=Egpx+tLUxPmxYOOUCXnCyoiI/s1D3uMGOKHQZ0vlXhcbbtGGn6yNdtBVEDJWV0nYK sntHn16iB5H0ZDS/MlXC6PKjNYWMdF1y8Wp2HeLO2sFbnnaHaG+DY3hXlLGTBTNc0F KrTb6lL/XFot+36fuZx16fQ1plSO8fV6VVTTzTTJ3NOYto/PwYY0y2okf1I3RWQzyb dEb4fBwrIKFw23j7vTQnHOiaXVI2wD1NUrHN3az/6hW7g03A/7S2NY9mKc/KacC12n MhFf9TdL1CFNfENI7l2FldB+yJwKnrlVnZVvXmI7RZIP3TY4XdNa7qah69u2nLSkCX MOGvGbdu+x3Yw== Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org When devlink reload operation is not used, netdev of an Ethernet port may be present in different net namespace than the net namespace of the devlink instance. Ensure that both the devlink instance and devlink port netdev are located in same net namespace. Fixes: 070c63f20f6c ("net: devlink: allow to change namespaces during reload") Signed-off-by: Parav Pandit --- Changelog: v1->v2: - avoided refactor code from previous patch --- net/core/devlink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/core/devlink.c b/net/core/devlink.c index 04ae67bbe19c..28c45be7f1bf 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -802,9 +802,10 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, devlink_port->desired_type)) goto nla_put_failure_type_locked; if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) { + struct net *net = devlink_net(devlink_port->devlink); struct net_device *netdev = devlink_port->type_dev; - if (netdev && + if (netdev && net_eq(net, dev_net(netdev)) && (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, netdev->ifindex) || nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,