[RFC PATCH net-next 09/13] parisc/led: hold the netdev lists lock when retrieving device statistics

Vladimir Oltean
2020-12-07T00:02:18Z

In the effort of making .ndo_get_stats64 be able to sleep, we need to
ensure the callers of dev_get_stats do not use atomic context.

The LED driver for HP-PARISC workstations uses a workqueue to
periodically check for updates in network interface statistics, and
flicker when those have changed (i.e. there has been activity on the
line). Ignoring the fact that this driver is completely duplicating
drivers/leds/trigger/ledtrig-netdev.c, there is an even bigger problem.
Now, the dev_get_stats call can sleep, and iterating through the list of
network interfaces still needs to ensure the integrity of list of
network interfaces. So that leaves us only one locking option given the
current design of the network stack, and that is the netns mutex.

Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>
Cc: Helge Deller <deller@gmx.de>
Cc: linux-parisc@vger.kernel.org
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
drivers/parisc/led.c | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/drivers/parisc/led.c b/drivers/parisc/led.c
index b7005aaa782b..1289dd3ea6e4 100644
--- a/drivers/parisc/led.c
+++ b/drivers/parisc/led.c
@@ -38,7 +38,6 @@
#include <linux/ctype.h>
#include <linux/blkdev.h>
#include <linux/workqueue.h>
-#include <linux/rcupdate.h>
#include <asm/io.h>
#include <asm/processor.h>
#include <asm/hardware.h>
@@ -356,24 +355,28 @@ static __inline__ int led_get_net_activity(void)

rx_total = tx_total = 0;

- /* we are running as a workqueue task, so we can use an RCU lookup */
- rcu_read_lock();
- for_each_netdev_rcu(&init_net, dev) {
+ /* we are running as a workqueue task, so we can sleep */
+ mutex_lock(&init_net->netdev_lists_lock);
+
+ for_each_netdev(&init_net, dev) {
+ struct in_device *in_dev = in_dev_get(dev);
const struct rtnl_link_stats64 *stats;
struct rtnl_link_stats64 temp;
- struct in_device *in_dev = __in_dev_get_rcu(dev);

- if (!in_dev || !in_dev->ifa_list)
+ if (!in_dev || !in_dev->ifa_list ||
+ ipv4_is_loopback(in_dev->ifa_list->ifa_local)) {
+ in_dev_put(in_dev);
continue;
+ }

- if (ipv4_is_loopback(in_dev->ifa_list->ifa_local))
- continue;
+ in_dev_put(in_dev);

stats = dev_get_stats(dev, &temp);
rx_total += stats->rx_packets;
tx_total += stats->tx_packets;
}
- rcu_read_unlock();
+
+ mutex_unlock(&init_net->netdev_lists_lock);

retval = 0;

--
2.25.1