1 From: Wei Wang <weiwan@google.com>
2 Date: Mon, 8 Feb 2021 11:34:10 -0800
3 Subject: [PATCH] net: add sysfs attribute to control napi threaded mode
5 This patch adds a new sysfs attribute to the network device class.
6 Said attribute provides a per-device control to enable/disable the
7 threaded mode for all the napi instances of the given network device,
8 without the need for a device up/down.
9 User sets it to 1 or 0 to enable or disable threaded mode.
10 Note: when switching between threaded and the current softirq based mode
11 for a napi instance, it will not immediately take effect if the napi is
12 currently being polled. The mode switch will happen for the next time
13 napi_schedule() is called.
15 Co-developed-by: Paolo Abeni <pabeni@redhat.com>
16 Signed-off-by: Paolo Abeni <pabeni@redhat.com>
17 Co-developed-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
18 Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
19 Co-developed-by: Felix Fietkau <nbd@nbd.name>
20 Signed-off-by: Felix Fietkau <nbd@nbd.name>
21 Signed-off-by: Wei Wang <weiwan@google.com>
22 Reviewed-by: Alexander Duyck <alexanderduyck@fb.com>
23 Signed-off-by: David S. Miller <davem@davemloft.net>
26 --- a/Documentation/ABI/testing/sysfs-class-net
27 +++ b/Documentation/ABI/testing/sysfs-class-net
28 @@ -337,3 +337,18 @@ Contact: netdev@vger.kernel.org
30 32-bit unsigned integer counting the number of times the link has
33 +What: /sys/class/net/<iface>/threaded
36 +Contact: netdev@vger.kernel.org
38 + Boolean value to control the threaded mode per device. User could
39 + set this value to enable/disable threaded mode for all napi
40 + belonging to this device, without the need to do device up/down.
43 + == ==================================
44 + 0 threaded mode disabled for this dev
45 + 1 threaded mode enabled for this dev
46 + == ==================================
47 --- a/include/linux/netdevice.h
48 +++ b/include/linux/netdevice.h
49 @@ -491,6 +491,8 @@ static inline bool napi_complete(struct
50 return napi_complete_done(n, 0);
53 +int dev_set_threaded(struct net_device *dev, bool threaded);
56 * napi_disable - prevent NAPI from scheduling
60 @@ -4281,8 +4281,9 @@ static inline void ____napi_schedule(str
62 if (test_bit(NAPI_STATE_THREADED, &napi->state)) {
63 /* Paired with smp_mb__before_atomic() in
64 - * napi_enable(). Use READ_ONCE() to guarantee
65 - * a complete read on napi->thread. Only call
66 + * napi_enable()/dev_set_threaded().
67 + * Use READ_ONCE() to guarantee a complete
68 + * read on napi->thread. Only call
69 * wake_up_process() when it's not NULL.
71 thread = READ_ONCE(napi->thread);
72 @@ -6756,6 +6757,49 @@ static void init_gro_hash(struct napi_st
73 napi->gro_bitmask = 0;
76 +int dev_set_threaded(struct net_device *dev, bool threaded)
78 + struct napi_struct *napi;
81 + if (dev->threaded == threaded)
85 + list_for_each_entry(napi, &dev->napi_list, dev_list) {
86 + if (!napi->thread) {
87 + err = napi_kthread_create(napi);
96 + dev->threaded = threaded;
98 + /* Make sure kthread is created before THREADED bit
101 + smp_mb__before_atomic();
103 + /* Setting/unsetting threaded mode on a napi might not immediately
104 + * take effect, if the current napi instance is actively being
105 + * polled. In this case, the switch between threaded mode and
106 + * softirq mode will happen in the next round of napi_schedule().
107 + * This should not cause hiccups/stalls to the live traffic.
109 + list_for_each_entry(napi, &dev->napi_list, dev_list) {
111 + set_bit(NAPI_STATE_THREADED, &napi->state);
113 + clear_bit(NAPI_STATE_THREADED, &napi->state);
119 void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
120 int (*poll)(struct napi_struct *, int), int weight)
122 --- a/net/core/net-sysfs.c
123 +++ b/net/core/net-sysfs.c
124 @@ -538,6 +538,45 @@ static ssize_t phys_switch_id_show(struc
126 static DEVICE_ATTR_RO(phys_switch_id);
128 +static ssize_t threaded_show(struct device *dev,
129 + struct device_attribute *attr, char *buf)
131 + struct net_device *netdev = to_net_dev(dev);
132 + ssize_t ret = -EINVAL;
134 + if (!rtnl_trylock())
135 + return restart_syscall();
137 + if (dev_isalive(netdev))
138 + ret = sprintf(buf, fmt_dec, netdev->threaded);
144 +static int modify_napi_threaded(struct net_device *dev, unsigned long val)
148 + if (list_empty(&dev->napi_list))
149 + return -EOPNOTSUPP;
151 + if (val != 0 && val != 1)
152 + return -EOPNOTSUPP;
154 + ret = dev_set_threaded(dev, val);
159 +static ssize_t threaded_store(struct device *dev,
160 + struct device_attribute *attr,
161 + const char *buf, size_t len)
163 + return netdev_store(dev, attr, buf, len, modify_napi_threaded);
165 +static DEVICE_ATTR_RW(threaded);
167 static struct attribute *net_class_attrs[] __ro_after_init = {
168 &dev_attr_netdev_group.attr,
170 @@ -570,6 +609,7 @@ static struct attribute *net_class_attrs
171 &dev_attr_proto_down.attr,
172 &dev_attr_carrier_up_count.attr,
173 &dev_attr_carrier_down_count.attr,
174 + &dev_attr_threaded.attr,
177 ATTRIBUTE_GROUPS(net_class);