From patchwork Sun Nov 22 06:12:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Parav Pandit X-Patchwork-Id: 330545 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 628CEC63697 for ; Sun, 22 Nov 2020 06:16:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2400C20885 for ; Sun, 22 Nov 2020 06:16:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nvidia.com header.i=@nvidia.com header.b="HwiQ8VO6" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727359AbgKVGNV (ORCPT ); Sun, 22 Nov 2020 01:13:21 -0500 Received: from hqnvemgate25.nvidia.com ([216.228.121.64]:18638 "EHLO hqnvemgate25.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727294AbgKVGNU (ORCPT ); Sun, 22 Nov 2020 01:13:20 -0500 Received: from hqmail.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate25.nvidia.com (using TLS: TLSv1.2, AES256-SHA) id ; Sat, 21 Nov 2020 22:13:21 -0800 Received: from sw-mtx-036.mtx.labs.mlnx (10.124.1.5) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Sun, 22 Nov 2020 06:13:19 +0000 From: Parav Pandit To: , , , CC: Parav Pandit Subject: [PATCH net 1/2] devlink: Hold rtnl lock while reading netdev attributes Date: Sun, 22 Nov 2020 08:12:56 +0200 Message-ID: <20201122061257.60425-2-parav@nvidia.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201122061257.60425-1-parav@nvidia.com> References: <20201122061257.60425-1-parav@nvidia.com> MIME-Version: 1.0 X-Originating-IP: [10.124.1.5] X-ClientProxiedBy: HQMAIL105.nvidia.com (172.20.187.12) To HQMAIL107.nvidia.com (172.20.187.13) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1606025601; bh=WK47DA5ZA2xv8ag88EpAjwArasehUfzCUddaNASKX0w=; 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=HwiQ8VO6UwBqL/cOo7OlFEIroNjUcTw+/UcgusnuhfFuEghCIadoSOM1uQFplxLLc Uy9H43FRXHN0XyczujOI7TkFaIs0vIlP71MFsDoUrnezogOxta1ZA6X5e3ifOAmDT2 ICBAy55SC0+QWEntcQw8ff9QD4hEc3SA56tH+qUQeebTSMVUURZPfBF2SOfJkGDwfo rRxr0yqscz25ujySsQNIXmfZCG+XLEKBx5lmJIZ9JsdEURVphkbrVveR3P+iBAhmnU 0VX7hlWMKvP1cyzgqgjhbwqaZeWVP9UsZnLaoFoG2fK5PqppINs42P8FU3v3syvfF4 f9VkvujVYV3qA== 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 for maintaining backward compatibility. 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 --- net/core/devlink.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/net/core/devlink.c b/net/core/devlink.c index acc29d5157f4..6135ef5972ce 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -775,6 +775,23 @@ devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por return err; } +static int devlink_nl_port_netdev_fill(struct sk_buff *msg, struct devlink_port *devlink_port) +{ + struct net_device *netdev = devlink_port->type_dev; + int err; + + ASSERT_RTNL(); + if (!netdev) + return 0; + + err = nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, netdev->ifindex); + if (err) + goto done; + err = nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, netdev->name); +done: + return err; +} + static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, struct devlink_port *devlink_port, enum devlink_command cmd, u32 portid, @@ -792,6 +809,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; @@ -800,13 +819,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_device *netdev = devlink_port->type_dev; + int err; - if (netdev && - (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, - netdev->ifindex) || - nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, - netdev->name))) + err = devlink_nl_port_netdev_fill(msg, devlink_port); + if (err) goto nla_put_failure_type_locked; } if (devlink_port->type == DEVLINK_PORT_TYPE_IB) { @@ -818,6 +834,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 +845,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;