net-sysfs: check device is present when showing paths
Subsystems (2)
cover net-sysfs: check device is present when showing paths
A sysfs reader can race with a device reset or removal.
This was fixed for speed_show with commit 4224cfd7fb65 ("net-sysfs: add check for netdevice being present to speed_show") so add the same check to carrier, duplex, testing, and dormant paths.
Submitting as separate patches to make Fixes reporting accurate.
Jamie Bainbridge (4):
net-sysfs: check device is present when showing carrier
net-sysfs: check device is present when showing duplex
net-sysfs: check device is present when showing testing
net-sysfs: check device is present when showing dormant
net/core/net-sysfs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
--
2.39.2
PATCH 1/4 net-sysfs: check device is present when showing carrier
A sysfs reader can race with a device reset or removal.
This was fixed for speed_show with commit 4224cfd7fb65 ("net-sysfs: add check for netdevice being present to speed_show") so add the same check to carrier_show.
--- net/core/net-sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/net-sysfs.c +1 -1
@@ -206,7 +206,7 @@ static ssize_t carrier_show(struct device *dev,if (!rtnl_trylock ())return restart_syscall (); -if (netif_running (netdev)) { +if (netif_running (netdev) &&netif_device_present (netdev)) {/* Synchronize carrier state with link watch, * see also rtnl_getlink(). */
Signature · 1 line
2.39.2
Re: [PATCH net 1/4] net-sysfs: check device is present when showing carrier
Quoted 1 line · Jamie Bainbridge
A sysfs reader can race with a device reset or removal.
Kind of, yes, but please check what the race actually is.
Quoted 3 lines
This was fixed for
speed_showwith commit4224cfd7fb65("net-sysfs: add check for netdevice being present tospeed_show") so add the same check tocarrier_show.
You didn't say why it's needed here, so ... why is it?
FWIW, I don't think it actually _is_ needed, since the netdev struct itself is still around, linkwatch_sync_dev() will not do anything that's not still needed anyway (the removal from list must clearly either still happen or nothing happens in the function). This will not call into the driver (which would be the problematic part).
So while I don't think this is _wrong_ per se, I also don't think it's necessary, nor are you demonstrating that it is.
And for userspace it should be pretty much immaterial whether it gets a real value or -EINVAL in the race, or -ENOENT because the file disappeared anyway?
johannes
Re: [PATCH net 1/4] net-sysfs: check device is present when showing carrier
Quoted 17 lines · Johannes Berg
Quoted 1 line · Jamie Bainbridge
A sysfs reader can race with a device reset or removal.
Kind of, yes, but please check what the race actually is.
Quoted 3 lines
This was fixed for
speed_showwith commit4224cfd7fb65("net-sysfs: add check for netdevice being present tospeed_show") so add the same check tocarrier_show.You didn't say why it's needed here, so ... why is it?
FWIW, I don't think it actually _is_ needed, since the netdev struct itself is still around,
linkwatch_sync_dev()will not do anything that's not still needed anyway (the removal from list must clearly either still happen or nothing happens in the function). This will not call into the driver (which would be the problematic part).
Quoted 7 lines
So while I don't think this is _wrong_ per se, I also don't think it's necessary, nor are you demonstrating that it is.
And for userspace it should be pretty much immaterial whether it gets a real value or
-EINVALin the race, or-ENOENTbecause the file disappeared anyway?
All of which, btw, is also true for patches 3 and 4 in this set.
For patch 2 it seems applicable.
I do wonder if ethtool itself, at least ethtool netlink, doesn't have a similar problem though, since it just uses netdev_get_by_name() / netdev_get_by_index()?
johannes
Re: [PATCH net 1/4] net-sysfs: check device is present when showing carrier
Quoted 29 lines · Johannes Berg
Quoted 17 lines · Johannes Berg
Quoted 1 line · Jamie Bainbridge
A sysfs reader can race with a device reset or removal.
Kind of, yes, but please check what the race actually is.
Quoted 3 lines
This was fixed for
speed_showwith commit4224cfd7fb65("net-sysfs: add check for netdevice being present tospeed_show") so add the same check tocarrier_show.You didn't say why it's needed here, so ... why is it?
FWIW, I don't think it actually _is_ needed, since the netdev struct itself is still around,
linkwatch_sync_dev()will not do anything that's not still needed anyway (the removal from list must clearly either still happen or nothing happens in the function). This will not call into the driver (which would be the problematic part).Quoted 7 lines
So while I don't think this is _wrong_ per se, I also don't think it's necessary, nor are you demonstrating that it is.
And for userspace it should be pretty much immaterial whether it gets a real value or
-EINVALin the race, or-ENOENTbecause the file disappeared anyway?All of which, btw, is also true for patches 3 and 4 in this set.
Quoted 7 lines
For patch 2 it seems applicable.
I do wonder if ethtool itself, at least ethtool netlink, doesn't have a similar problem though, since it just uses
netdev_get_by_name()/netdev_get_by_index()?johannes
You are correct, patch 2 (duplex) is the one where we panicked during device reset. I thought to fix the other "show" functions in advance while I was there.
I will revise this and re-submit with only the necessary patch.
Thanks for the review, it is appreciated.
Jamie
Re: [PATCH net 1/4] net-sysfs: check device is present when showing carrier
In reply to Jamie BainbridgeHi Jamie,
Quoted 9 lines · Jamie Bainbridge
A sysfs reader can race with a device reset or removal.
This was fixed for
speed_showwith commit4224cfd7fb65("net-sysfs: add check for netdevice being present tospeed_show") so add the same check tocarrier_show.Fixes facd15dfd691 ("net: core: synchronize link-watch when carrier is queried")Signed-off-by Jamie Bainbridge <[email protected]>
nit: When resubmitting your fix, there is no need for a blank line
between the Fixes and SOB tags.
Thanks,
Shigeru
Quoted 21 lines
--- net/core/net-sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/core/net-sysfs.c +1
-1@@ -206,7 +206,7 @@ static ssize_t carrier_show(struct device *dev,if (!rtnl_trylock ())return restart_syscall (); -if (netif_running (netdev)) { +if (netif_running (netdev) &&netif_device_present (netdev)) {/* Synchronize carrier state with link watch, * see also rtnl_getlink(). */ Signature · 1 line
2.39.2
PATCH 2/4 net-sysfs: check device is present when showing duplex
In reply to Jamie BainbridgeA sysfs reader can race with a device reset or removal.
This was fixed for speed_show with commit 4224cfd7fb65 ("net-sysfs: add check for netdevice being present to speed_show") so add the same check to duplex_show.
--- net/core/net-sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/net-sysfs.c +1 -1
@@ -261,7 +261,7 @@ static ssize_t duplex_show(struct device *dev,if (!rtnl_trylock ())return restart_syscall (); -if (netif_running (netdev)) { +if (netif_running (netdev) &&netif_device_present (netdev)) {struct ethtool_link_ksettings cmd;if (!__ethtool_get_link_ksettings (netdev, &cmd)) {
Signature · 1 line
2.39.2
PATCH 3/4 net-sysfs: check device is present when showing testing
In reply to Jamie BainbridgeA sysfs reader can race with a device reset or removal.
This was fixed for speed_show with commit 4224cfd7fb65 ("net-sysfs: add check for netdevice being present to speed_show") so add the same check to testing_show.
--- net/core/net-sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/net-sysfs.c +1 -1
@@ -291,7 +291,7 @@ static ssize_t testing_show(struct device *dev, {struct net_device *netdev =to_net_dev (dev); -if (netif_running (netdev)) +if (netif_running (netdev) &&netif_device_present (netdev))return sysfs_emit (buf, fmt_dec, !!netif_testing (netdev));return -EINVAL ;
Signature · 1 line
2.39.2
Re: [PATCH net 3/4] net-sysfs: check device is present when showing testing
Quoted 18 lines · Jamie Bainbridge
A sysfs reader can race with a device reset or removal.
This was fixed for
speed_showwith commit4224cfd7fb65("net-sysfs: add check for netdevice being present tospeed_show") so add the same check totesting_show.Fixes db30a57779b1 ("net: Add testing sysfs attribute")Signed-off-by Jamie Bainbridge <[email protected]>--- net/core/net-sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/core/net-sysfs.c +0
-0
net/core/net-sysfs.c static ssize_t testing_show(struct device *dev, · line 294
Quoted 5 lines
net/core/net-sysfs.c +1
-1@@ -291,7 +291,7 @@ static ssize_t testing_show(struct device *dev, {struct net_device *netdev =to_net_dev (dev); -if (netif_running (netdev)) +if (netif_running (netdev) &&netif_device_present (netdev))
Maybe a dumb observation, but how can it be running if it is not present?
And if we are not holding any locks, it might be gone by the time you call netif_testing()?
net/core/net-sysfs.c static ssize_t testing_show(struct device *dev, · line 295
Quoted 1 line
net/core/net-sysfs.c +0
-0return sysfs_emit (buf, fmt_dec, !!netif_testing (netdev));
Andrew
PATCH 4/4 net-sysfs: check device is present when showing dormant
In reply to Jamie BainbridgeA sysfs reader can race with a device reset or removal.
This was fixed for speed_show with commit 4224cfd7fb65 ("net-sysfs: add check for netdevice being present to speed_show") so add the same check to dormant_show.
--- net/core/net-sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/net-sysfs.c +1 -1
@@ -303,7 +303,7 @@ static ssize_t dormant_show(struct device *dev, {struct net_device *netdev =to_net_dev (dev); -if (netif_running (netdev)) +if (netif_running (netdev) &&netif_device_present (netdev))return sysfs_emit (buf, fmt_dec, !!netif_dormant (netdev));return -EINVAL ;
Signature · 1 line
2.39.2