Skip to contentSkip to conversation

net: make dev_set_mtu() honor notification return code

PATCH Superseded Veaceslav Falico 3 participants 6 messages Jan 10, 2014 – Jan 15, 2014

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: make dev_set_mtu() honor notification return code

Currently, after changing the MTU for a device, dev_set_mtu() calls NETDEV_CHANGEMTU notification, however doesn't verify it's return code - which can be NOTIFY_BAD - i.e. some of the net notifier blocks refused this change, and continues nevertheless.

To fix this, verify the return code, and if it's an error - then revert the MTU to the original one, and pass the error code.

CC Jiri Pirko <[email protected]>
CC "David S. Miller" <[email protected]>
CC Eric Dumazet <[email protected]>
CC Alexander Duyck <[email protected]>
CC Nicolas Dichtel <[email protected]>
---
 net/core/dev.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
net/core/dev.c +20 -9
index ce01847..1c570ff 100644
@@ -5287,6 +5287,17 @@ int dev_change_flags(struct net_device *dev, unsigned int flags)
 }
 EXPORT_SYMBOL(dev_change_flags);
 
+static int __dev_set_mtu(struct net_device *dev, int new_mtu)
+{
+	const struct net_device_ops *ops = dev->netdev_ops;
+
+	if (ops->ndo_change_mtu)
+		return ops->ndo_change_mtu(dev, new_mtu);
+
+	dev->mtu = new_mtu;
+	return 0;
+}
+
 /**
  *	dev_set_mtu - Change maximum transfer unit
  *	@dev: device
@@ -5296,8 +5307,7 @@ EXPORT_SYMBOL(dev_change_flags);
  */
 int dev_set_mtu(struct net_device *dev, int new_mtu)
 {
-	const struct net_device_ops *ops = dev->netdev_ops;
-	int err;
+	int err, orig_mtu;
 
 	if (new_mtu == dev->mtu)
 		return 0;
@@ -5309,14 +5319,15 @@ int dev_set_mtu(struct net_device *dev, int new_mtu)
 	if (!netif_device_present(dev))
 		return -ENODEV;
 
-	err = 0;
-	if (ops->ndo_change_mtu)
-		err = ops->ndo_change_mtu(dev, new_mtu);
-	else
-		dev->mtu = new_mtu;
+	orig_mtu = dev->mtu;
+	err = __dev_set_mtu(dev, new_mtu);
 
-	if (!err)
-		call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
+	if (!err) {
+		err = call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
+		err = notifier_to_errno(err);
+		if (err)
+			__dev_set_mtu(dev, orig_mtu);
+	}
 	return err;
 }
 EXPORT_SYMBOL(dev_set_mtu);
Signature · 1 line
1.8.4
#1 lore raw
Quoted 63 lines · Veaceslav Falico

Currently, after changing the MTU for a device, dev_set_mtu() calls NETDEV_CHANGEMTU notification, however doesn't verify it's return code - which can be NOTIFY_BAD - i.e. some of the net notifier blocks refused this change, and continues nevertheless.

To fix this, verify the return code, and if it's an error - then revert the MTU to the original one, and pass the error code.

CC Jiri Pirko <[email protected]>
CC "David S. Miller" <[email protected]>
CC Eric Dumazet <[email protected]>
CC Alexander Duyck <[email protected]>
CC Nicolas Dichtel <[email protected]>
---
 net/core/dev.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
net/core/dev.c +14 -8
index ce01847..1c570ff 100644
@@ -5287,6 +5287,17 @@ int dev_change_flags(struct net_device *dev, unsigned int flags)
 }
 EXPORT_SYMBOL(dev_change_flags);
 
+static int __dev_set_mtu(struct net_device *dev, int new_mtu)
+{
+	const struct net_device_ops *ops = dev->netdev_ops;
+
+	if (ops->ndo_change_mtu)
+		return ops->ndo_change_mtu(dev, new_mtu);
+
+	dev->mtu = new_mtu;
+	return 0;
+}
+
 /**
  *	dev_set_mtu - Change maximum transfer unit
  *	@dev: device
@@ -5296,8 +5307,7 @@ EXPORT_SYMBOL(dev_change_flags);
  */
 int dev_set_mtu(struct net_device *dev, int new_mtu)
 {
-	const struct net_device_ops *ops = dev->netdev_ops;
-	int err;
+	int err, orig_mtu;
 
 	if (new_mtu == dev->mtu)
 		return 0;
@@ -5309,14 +5319,15 @@ int dev_set_mtu(struct net_device *dev, int new_mtu)
 	if (!netif_device_present(dev))
 		return -ENODEV;
 
-	err = 0;
-	if (ops->ndo_change_mtu)
-		err = ops->ndo_change_mtu(dev, new_mtu);
-	else
-		dev->mtu = new_mtu;
+	orig_mtu = dev->mtu;
+	err = __dev_set_mtu(dev, new_mtu);
 
-	if (!err)

net/core/dev.c int dev_set_mtu(struct net_device *dev, int new_mtu) · line 5333

Quoted 10 lines
net/core/dev.c +6 -1
-		call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
+	if (!err) {
+		err = call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
+		err = notifier_to_errno(err);
+		if (err)
+			__dev_set_mtu(dev, orig_mtu);
+	}
 	return err;
 }
 EXPORT_SYMBOL(dev_set_mtu);

So what about the netdevices that succeeded in changing the MTU based on the notifiers? It seems like you still have an inconsistent state after the failure unless you issue a second call with a notification that you reverted to the old MTU.

Thanks,

Alex

#2 lore raw
Quoted 1 line · Alexander Duyck

On 01/10/2014 04:48 AM, Veaceslav Falico wrote:

...snip...

Quoted 8 lines
Quoted 8 lines
net/core/dev.c +2 -5
-	err = 0;
-	if (ops->ndo_change_mtu)
-		err = ops->ndo_change_mtu(dev, new_mtu);
-	else
-		dev->mtu = new_mtu;
+	orig_mtu = dev->mtu;
+	err = __dev_set_mtu(dev, new_mtu);
Quoted 16 lines

net/core/dev.c int dev_set_mtu(struct net_device *dev, int new_mtu) · line 5333

Quoted 11 lines
net/core/dev.c +6 -2
-	if (!err)
-		call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
+	if (!err) {
+		err = call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
+		err = notifier_to_errno(err);
+		if (err)
+			__dev_set_mtu(dev, orig_mtu);
+	}
 	return err;
 }
 EXPORT_SYMBOL(dev_set_mtu);

So what about the netdevices that succeeded in changing the MTU based on the notifiers? It seems like you still have an inconsistent state after the failure unless you issue a second call with a notification that you reverted to the old MTU.

Good point, thank you. I'll add a second call_netdevice_notifiers() after setting the original MTU and resend.

Thank you!

Quoted 4 lines

Thanks,

Alex

maintainer
#3 lore raw
In reply to Veaceslav Falico

From: Veaceslav Falico <[email protected]>
Date: Fri, 10 Jan 2014 13:48:17 +0100

Quoted 9 lines

Currently, after changing the MTU for a device, dev_set_mtu() calls NETDEV_CHANGEMTU notification, however doesn't verify it's return code - which can be NOTIFY_BAD - i.e. some of the net notifier blocks refused this change, and continues nevertheless.

To fix this, verify the return code, and if it's an error - then revert the MTU to the original one, and pass the error code.

This is really a precariously designed code path.

If one of the notifiers says NOTIFY_BAD, well we've already changed dev->mtu, therefore what if a packet got sent during this time?

Whoever the NOTIFY_BAD signaller is, obviously cannot handle an MTU setting which we've already set in the netdev. So allowing it's a terribly idea to allow visibility of the new MTU value until we can be sure everyone can handle it.

The problem is that we really need a transaction of some sort to fix this properly. First, we'd need to ask all the notifiers if they can handle the new MTU, then we somehow atomically set netdev->mtu and have the notifiers commit their own state changes.

Then we'll have the stick issue of what to do if a notifier is unregistered between the check and the commit. :-)

Your patch is an improvement so I will apply it, this stuff really is full of holes still.

Thanks.

#4 lore raw
Quoted 23 lines · David Miller

From: Veaceslav Falico <[email protected]>
Date: Fri, 10 Jan 2014 13:48:17 +0100

Quoted 9 lines

Currently, after changing the MTU for a device, dev_set_mtu() calls NETDEV_CHANGEMTU notification, however doesn't verify it's return code - which can be NOTIFY_BAD - i.e. some of the net notifier blocks refused this change, and continues nevertheless.

To fix this, verify the return code, and if it's an error - then revert the MTU to the original one, and pass the error code.

This is really a precariously designed code path.

If one of the notifiers says NOTIFY_BAD, well we've already changed dev->mtu, therefore what if a packet got sent during this time?

Whoever the NOTIFY_BAD signaller is, obviously cannot handle an MTU setting which we've already set in the netdev. So allowing it's a terribly idea to allow visibility of the new MTU value until we can be sure everyone can handle it.

Quoted 4 lines

The problem is that we really need a transaction of some sort to fix this properly. First, we'd need to ask all the notifiers if they can handle the new MTU, then we somehow atomically set netdev->mtu and have the notifiers commit their own state changes.

Yeah, but I can't think of a method to atomically set it for both netdev and its notifiers... As in, for example, bridge (but not only) takes the lowest MTU of its ports.

Quoted 3 lines

Then we'll have the stick issue of what to do if a notifier is unregistered between the check and the commit. :-)

Maybe you've meant 'registered between ...' ? :-) Anyway, I guess dev_set_mtu() should always be called under RTNL, and this way we won't have these problems. Though I might be wrong, everyone seems playing with MTU the way they want.

Quoted 3 lines

Your patch is an improvement so I will apply it, this stuff really is full of holes still.

One (least intrusive) approach would be to add NETDEV_PRECHANGEMTU, which would be used to verify if the notifiers all agree with changing, and leave the NETDEV_CHANGEMTU fail only when something really bad happened. That's your idea, basically.

As, currently, only team can signal NOTIFY_BAD on mtu change, it's really easy to implement. What do you think?

drivers/net/team/team.c +1 -1
index 736050d..4e50e04 100644
@@ -2850,7 +2850,7 @@ static int team_device_event(struct notifier_block *unused,
 	case NETDEV_FEAT_CHANGE:
 		team_compute_features(port->team);
 		break;
-	case NETDEV_CHANGEMTU:
+	case NETDEV_PRECHANGEMTU:
 		/* Forbid to change mtu of underlaying device */
 		return NOTIFY_BAD;
 	case NETDEV_PRE_TYPE_CHANGE:
include/linux/netdevice.h +1 -0
index a2a70cc..7e023c4 100644
@@ -1731,6 +1731,7 @@ struct pcpu_sw_netstats {
 #define NETDEV_JOIN		0x0014
 #define NETDEV_CHANGEUPPER	0x0015
 #define NETDEV_RESEND_IGMP	0x0016
+#define NETDEV_PRECHANGEMTU	0x0017
 
 int register_netdevice_notifier(struct notifier_block *nb);
 int unregister_netdevice_notifier(struct notifier_block *nb);
net/core/dev.c +4 -0
index 87312dc..096d4dd 100644
@@ -5332,6 +5332,10 @@ int dev_set_mtu(struct net_device *dev, int new_mtu)
 	if (!netif_device_present(dev))
 		return -ENODEV;
 
+	err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
+	if (!err)
+		return notifier_to_errno(err);
+
 	orig_mtu = dev->mtu;
 	err = __dev_set_mtu(dev, new_mtu);
 
Quoted 3 lines

Thanks.

maintainer
#5 lore raw

From: Veaceslav Falico <[email protected]>
Date: Tue, 14 Jan 2014 13:13:54 +0100

Quoted 2 lines

As, currently, only team can signal NOTIFY_BAD on mtu change, it's really easy to implement. What do you think?

Looks great, and I agree that RTNL should solve all the other issues.