All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: Parav Pandit <parav@nvidia.com>
Cc: <davem@davemloft.net>, <netdev@vger.kernel.org>, <jiri@nvidia.com>
Subject: Re: [PATCH net 1/2] devlink: Hold rtnl lock while reading netdev attributes
Date: Tue, 24 Nov 2020 14:29:10 -0800	[thread overview]
Message-ID: <20201124142910.14cadc35@kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com> (raw)
In-Reply-To: <20201122061257.60425-2-parav@nvidia.com>

On Sun, 22 Nov 2020 08:12:56 +0200 Parav Pandit wrote:
> 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 <parav@nvidia.com>
> ---
>  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);

The line wrapping was correct, please keep in under 80. Please tell
your colleges at Mellanox.

> +	if (err)
> +		goto done;

	return err;

> +	err = nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, netdev->name);

	return nla_put_...

> +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;

What's the point of this local variable?

> -		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)

just put the call in the if ()

>  			goto nla_put_failure_type_locked;
>  	}
>  	if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {


Honestly this patch is doing too much for a fix.

All you need is the RTNL lock and then add:

+               struct net *net = devlink_net(devlink_port->devlink);
                struct net_device *netdev = devlink_port->type_dev;
 
                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,


You can do refactoring later in net-next. Maybe even add a check that
drivers which support reload set namespace local on their netdevs.

  reply	other threads:[~2020-11-24 22:29 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-22  6:12 [PATCH net 0/2] devlink port attribute fixes Parav Pandit
2020-11-22  6:12 ` [PATCH net 1/2] devlink: Hold rtnl lock while reading netdev attributes Parav Pandit
2020-11-24 22:29   ` Jakub Kicinski [this message]
2020-11-25  7:13     ` Parav Pandit
2020-11-25 16:30       ` Jakub Kicinski
2020-11-25 17:21         ` Parav Pandit
2020-11-25 17:41           ` Jakub Kicinski
2020-11-25 18:17             ` Parav Pandit
2020-11-22  6:12 ` [PATCH net 2/2] devlink: Make sure devlink instance and port are in same net namespace Parav Pandit
2020-11-25  9:16   ` [PATCH net v2 0/2] devlink port attribute fixes Parav Pandit
2020-11-25  9:16     ` [PATCH net v2 1/2] devlink: Hold rtnl lock while reading netdev attributes Parav Pandit
2020-11-25  9:16     ` [PATCH net v2 2/2] devlink: Make sure devlink instance and port are in same net namespace Parav Pandit
2020-11-26  1:40     ` [PATCH net v2 0/2] devlink port attribute fixes patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201124142910.14cadc35@kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com \
    --to=kuba@kernel.org \
    --cc=davem@davemloft.net \
    --cc=jiri@nvidia.com \
    --cc=netdev@vger.kernel.org \
    --cc=parav@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.