Skip to contentSkip to conversation

devlink port attribute fixes

PATCH Superseded Parav Pandit 3 participants 13 messages Nov 22, 2020 – Nov 26, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover devlink port attribute fixes

This patchset contains 2 small fixes for devlink port attributes.

Patch summary:
Patch-1 synchronize the devlink port attribute reader
with net namespace change operation
Patch-2 Ensure to return devlink port's netdevice attributes
when netdev and devlink instance belong to same net namespace

Parav Pandit (2):
devlink: Hold rtnl lock while reading netdev attributes
devlink: Make sure devlink instance and port are in same net namespace

 net/core/devlink.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 devlink: Hold rtnl lock while reading netdev attributes

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")
---
 net/core/devlink.c | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)
net/core/devlink.c +24 -6
index acc29d5157f4..6135ef5972ce 100644
@@ -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;
Signature · 1 line
2.26.2
maintainer
#2 lore raw

Re: [PATCH net 1/2] devlink: Hold rtnl lock while reading netdev attributes

Quoted 27 lines · 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")
---
 net/core/devlink.c | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)
net/core/devlink.c +0 -0
index acc29d5157f4..6135ef5972ce 100644
@@ -775,6 +775,23 @@ devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por
 	return err;
 }
 

net/core/devlink.c devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por · line 787

Quoted 10 lines
net/core/devlink.c +10 -0
+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.

net/core/devlink.c devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por · line 789

Quoted 2 lines
net/core/devlink.c +2 -0
+	if (err)
+		goto done;
	return err;

net/core/devlink.c devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por · line 790

Quoted 1 line
net/core/devlink.c +1 -0
+	err = nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, netdev->name);
	return nla_put_...
Quoted 17 lines
net/core/devlink.c +6 -0
+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;

net/core/devlink.c static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, · line 822

Quoted 5 lines
net/core/devlink.c +1 -1
@@ -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?

net/core/devlink.c static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, · line 825

Quoted 7 lines
net/core/devlink.c +2 -5
-		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 ()

net/core/devlink.c static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, · line 828

Quoted 3 lines
net/core/devlink.c +0 -0
 			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.

#3 lore raw

RE: [PATCH net 1/2] devlink: Hold rtnl lock while reading netdev attributes

Quoted 78 lines

From: Jakub Kicinski <[email protected]>
Sent: Wednesday, November 25, 2020 3:59 AM

net/core/devlink.c devlink_nl_port_function_attrs_put(struct sk_buff

Quoted 23 lines · 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")
---
 net/core/devlink.c | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)
net/core/devlink.c index renamed from net/core/devlink.c +0 -0

acc29d5157f4..6135ef5972ce 100644

net/core/devlink.c +0 -0
@@ -775,6 +775,23 @@ devlink_nl_port_function_attrs_put(struct sk_buff

*msg, struct devlink_port *por

Quoted 4 lines
net/core/devlink.c +1 -0
 	return err;
 }
 
+static int devlink_nl_port_netdev_fill(struct sk_buff *msg, struct

net/core/devlink.c devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por

Quoted 10 lines
net/core/devlink.c +10 -0
+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.

net/core/devlink.c devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por · line 789

Quoted 2 lines
net/core/devlink.c +2 -0
+	if (err)
+		goto done;
	return err;

net/core/devlink.c devlink_nl_port_function_attrs_put(struct sk_buff · line 792

Quoted 2 lines
net/core/devlink.c +2 -0
+	err = nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
+netdev->name);
	return nla_put_...

net/core/devlink.c devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por

Quoted 7 lines
net/core/devlink.c +4 -0
+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

net/core/devlink.c static int devlink_nl_port_fill(struct sk_buff *msg,

Quoted 11 lines

int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port-
index))

net/core/devlink.c +2 -0
 		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,

Quoted 8 lines

net/core/devlink.c static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, · line 822

Quoted 5 lines
net/core/devlink.c +1 -1
 			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?

I will avoid refactor for now, so above comment doesn't need to be addressed.

Quoted 9 lines

net/core/devlink.c static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, · line 825

Quoted 7 lines
net/core/devlink.c +2 -5
-		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 ()

Ok.

Quoted 10 lines

net/core/devlink.c static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, · line 828

Quoted 4 lines
net/core/devlink.c +0 -0
 			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:

Ok. I will differ the refactor to later point.

Quoted 10 lines
(quoted diff) +2 -0
+               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,
Quoted 2 lines

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

This will break the backward compatibility as orchestration for VFs are not using devlink reload, which is supported very recently.
But yes, for SF who doesn't have backward compatibility issue, as soon as initial series is merged, I will mark it as local, so that orchestration doesn't start on wrong foot.

maintainer
#4 lore raw

Re: [PATCH net 1/2] devlink: Hold rtnl lock while reading netdev attributes

Quoted 7 lines · On Wed, 25 Nov 2020 07:13:40 +0000 Parav Pandit wrote:
Quoted 2 lines

Maybe even add a check that drivers
which support reload set namespace local on their netdevs.

This will break the backward compatibility as orchestration for VFs are not using devlink reload, which is supported very recently. But yes, for SF who doesn't have backward compatibility issue, as soon as initial series is merged, I will mark it as local, so that orchestration doesn't start on wrong foot.

Ah, right, that will not work because of the shenanigans you guys play with the uplink port. If all reprs are NETNS_LOCAL it'd not be an issue.

#5 lore raw

RE: [PATCH net 1/2] devlink: Hold rtnl lock while reading netdev attributes

Quoted 8 lines

From: Jakub Kicinski <[email protected]>
Sent: Wednesday, November 25, 2020 10:00 PM

Quoted 2 lines · On Wed, 25 Nov 2020 07:13:40 +0000 Parav Pandit wrote:
Quoted 2 lines

Maybe even add a check that drivers
which support reload set namespace local on their netdevs.

Quoted 8 lines
Quoted 5 lines

This will break the backward compatibility as orchestration for VFs are not using devlink reload, which is supported very recently. But yes, for SF who doesn't have backward compatibility issue, as soon as initial series is merged, I will mark it as local, so that orchestration doesn't start on wrong foot.

Ah, right, that will not work because of the shenanigans you guys play with the uplink port. If all reprs are NETNS_LOCAL it'd not be an issue.

I am not sure what secret are you talking about with uplink.
I am taking about the SF netdevice to have the NETNS_LOCAL not the SF rep. SF rep anyway has NETNS_LOCAL set.

I do not follow your comment - 'that will not work'. Can you please explain?
Do you mean I should take care for SF's netdevice to have NETNS_LOCAL in first patchset or you mean setting NETNS_LOCAL for VF's Netdev will not work? If its later, sure it will break the backward compatibility, so will not do as default.
But yes, SF I want to subsequently.

maintainer
#6 lore raw

Re: [PATCH net 1/2] devlink: Hold rtnl lock while reading netdev attributes

Quoted 12 lines · On Wed, 25 Nov 2020 17:21:41 +0000 Parav Pandit wrote:
Quoted 12 lines

From: Jakub Kicinski <[email protected]>
Sent: Wednesday, November 25, 2020 10:00 PM

Quoted 7 lines · On Wed, 25 Nov 2020 07:13:40 +0000 Parav Pandit wrote:
Quoted 2 lines

Maybe even add a check that drivers
which support reload set namespace local on their netdevs.

This will break the backward compatibility as orchestration for VFs are not using devlink reload, which is supported very recently. But yes, for SF who doesn't have backward compatibility issue, as soon as initial series is merged, I will mark it as local, so that orchestration doesn't start on wrong foot.

Quoted 3 lines
Quoted 2 lines

Ah, right, that will not work because of the shenanigans you guys play with the uplink port. If all reprs are NETNS_LOCAL it'd not be an issue.

I am not sure what secret are you talking about with uplink.

I'm referring to Mellanox conflating PF with uplink. It's not a secret, we argued about it in the past.

Quoted 2 lines

I am taking about the SF netdevice to have the NETNS_LOCAL not the SF rep. SF rep anyway has NETNS_LOCAL set.

All reps build by mlx5e_build_rep_netdev() have NETNS_LOCAL.

Quoted 1 line

I do not follow your comment - 'that will not work'. Can you please explain?

My half-baked suggestion was to basically add a:

	WARN_ON(ops->reload_down && ops->reload_up &&
		!(netdev->priv & NETIF_F_NETNS_LOCAL));

to devlink_port_type_netdev_checks(). Given if device has a reload callback devlink is the way to change netns. But yeah, we can't break existing behavior so your uplink has to be movable and can't have NETNS_LOCAL. IOW adding the WARN_ON() won't work.

Hope this is crystal clear now.

Quoted 2 lines

Do you mean I should take care for SF's netdevice to have NETNS_LOCAL in first patchset or you mean setting NETNS_LOCAL for VF's Netdev will not work? If its later, sure it will break the backward compatibility, so will not do as default.

#7 lore raw

RE: [PATCH net 1/2] devlink: Hold rtnl lock while reading netdev attributes

Quoted 18 lines

From: Jakub Kicinski <[email protected]>
Sent: Wednesday, November 25, 2020 11:12 PM

Quoted 12 lines · On Wed, 25 Nov 2020 17:21:41 +0000 Parav Pandit wrote:
Quoted 12 lines

From: Jakub Kicinski <[email protected]>
Sent: Wednesday, November 25, 2020 10:00 PM

Quoted 7 lines · On Wed, 25 Nov 2020 07:13:40 +0000 Parav Pandit wrote:
Quoted 2 lines

Maybe even add a check that drivers which support reload set namespace local on their netdevs.

This will break the backward compatibility as orchestration for VFs are not using devlink reload, which is supported very recently. But yes, for SF who doesn't have backward compatibility issue, as soon as initial series is merged, I will mark it as local, so that orchestration doesn't start on wrong foot.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

Ah, right, that will not work because of the shenanigans you guys play with the uplink port. If all reprs are NETNS_LOCAL it'd not be an

issue.

Quoted 1 line

I am not sure what secret are you talking about with uplink.

I'm referring to Mellanox conflating PF with uplink. It's not a secret,

Ok.

Quoted 8 lines

we argued about it in the past.

Quoted 1 line

I am taking about the SF netdevice to have the NETNS_LOCAL not the SF

rep.

Quoted 1 line

SF rep anyway has NETNS_LOCAL set.

All reps build by mlx5e_build_rep_netdev() have NETNS_LOCAL.

Yes. this is clear to me and we are good here. 😊

Quoted 4 lines
Quoted 1 line

I do not follow your comment - 'that will not work'. Can you please explain?

My half-baked suggestion was to basically add a:

Quoted 8 lines
	WARN_ON(ops->reload_down && ops->reload_up &&
		!(netdev->priv & NETIF_F_NETNS_LOCAL));

to devlink_port_type_netdev_checks(). Given if device has a reload callback devlink is the way to change netns. But yeah, we can't break existing behavior so your uplink has to be movable and can't have NETNS_LOCAL. IOW adding the WARN_ON() won't work.

Right.

Quoted 1 line

Hope this is crystal clear now.

Yes, its clear. Thanks.

I addressed your comments and cut down both the fixes to merely 7 lines change. Sent v2.

#8 lore raw

PATCH 2/2 devlink: Make sure devlink instance and port are in same net namespace

In reply to Parav Pandit

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")
---
 net/core/devlink.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/core/devlink.c +4 -1
index 6135ef5972ce..2e1b0e82a102 100644
@@ -778,12 +778,15 @@ devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *por
 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;
+	int err = 0;
 
 	ASSERT_RTNL();
 	if (!netdev)
 		return 0;
 
+	if (!net_eq(devlink_net(devlink_port->devlink), dev_net(netdev)))
+		goto done;
+
 	err = nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, netdev->ifindex);
 	if (err)
 		goto done;
Signature · 1 line
2.26.2
#9 lore raw

v2 cover devlink port attribute fixes

This patchset contains 2 small fixes for devlink port attributes.

Patch summary:
Patch-1 synchronize the devlink port attribute reader
with net namespace change operation
Patch-2 Ensure to return devlink port's netdevice attributes
when netdev and devlink instance belong to same net namespace

---
Changelog:
v1->v2:
- avoided refactoring the port fill attribute routine

Parav Pandit (2):
devlink: Hold rtnl lock while reading netdev attributes
devlink: Make sure devlink instance and port are in same net namespace

 net/core/devlink.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Signature · 1 line
2.26.2
#10 lore raw

PATCH v2 1/2 devlink: Hold rtnl lock while reading netdev attributes

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

---
Changelog:
v1->v2:
- avoided refactoring the code
- updated commit log

---
 net/core/devlink.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/devlink.c +4 -0
index acc29d5157f4..04ae67bbe19c 100644
@@ -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;
Signature · 1 line
2.26.2
#11 lore raw

PATCH v2 2/2 devlink: Make sure devlink instance and port are in same net namespace

In reply to Parav Pandit

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

---
Changelog:
v1->v2:
- avoided refactor code from previous patch

---
 net/core/devlink.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/devlink.c +2 -1
index 04ae67bbe19c..28c45be7f1bf 100644
@@ -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,
Signature · 1 line
2.26.2
#12 lore raw
In reply to Parav Pandit
This series was applied to netdev/net.git (refs/heads/master):

Hello:

This series was applied to netdev/net.git (refs/heads/master):

Quoted 9 lines · On Wed, 25 Nov 2020 11:16:18 +0200 you wrote:

This patchset contains 2 small fixes for devlink port attributes.

Patch summary:
Patch-1 synchronize the devlink port attribute reader
with net namespace change operation
Patch-2 Ensure to return devlink port's netdevice attributes
when netdev and devlink instance belong to same net namespace

[...]

Here is the summary with links:
- [net,v2,1/2] devlink: Hold rtnl lock while reading netdev attributes

    https://git.kernel.org/netdev/net/c/b187c9b4178b
  - [net,v2,2/2] devlink: Make sure devlink instance and port are in same net namespace
    https://git.kernel.org/netdev/net/c/a7b43649507d

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html