From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
andrew+netdev@lunn.ch, horms@kernel.org, jdamato@fastly.com,
Jakub Kicinski <kuba@kernel.org>,
leitao@debian.org
Subject: [PATCH net-next 08/11] net: protect threaded status of NAPI with netdev_lock()
Date: Mon, 13 Jan 2025 19:51:14 -0800 [thread overview]
Message-ID: <20250114035118.110297-9-kuba@kernel.org> (raw)
In-Reply-To: <20250114035118.110297-1-kuba@kernel.org>
Now that NAPI instances can't come and go without holding
netdev->lock we can trivially switch from rtnl_lock() to
netdev_lock() for setting netdev->threaded via sysfs.
Note that since we do not lock netdev_lock around sysfs
calls in the core we don't have to "trylock" like we do
with rtnl_lock.
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: leitao@debian.org
---
include/linux/netdevice.h | 13 +++++++++++--
net/core/dev.c | 2 ++
net/core/net-sysfs.c | 32 +++++++++++++++++++++++++++++++-
3 files changed, 44 insertions(+), 3 deletions(-)
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index d3108a12e562..75c30404657b 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -384,7 +384,7 @@ struct napi_struct {
int rx_count; /* length of rx_list */
unsigned int napi_id; /* protected by netdev_lock */
struct hrtimer timer;
- struct task_struct *thread;
+ struct task_struct *thread; /* protected by netdev_lock */
unsigned long gro_flush_timeout;
unsigned long irq_suspend_timeout;
u32 defer_hard_irqs;
@@ -2451,10 +2451,12 @@ struct net_device {
* Drivers are free to use it for other protection.
*
* Protects:
- * @napi_list, @net_shaper_hierarchy, @reg_state
+ * @napi_list, @net_shaper_hierarchy, @reg_state, @threaded
* Partially protects (readers hold either @lock or rtnl_lock,
* writers must hold both for registered devices):
* @up
+ * Also protects some fields in struct napi_struct.
+ *
* Ordering: take after rtnl_lock.
*/
struct mutex lock;
@@ -2696,6 +2698,13 @@ static inline void netdev_assert_locked(struct net_device *dev)
lockdep_assert_held(&dev->lock);
}
+static inline void netdev_assert_locked_or_invisible(struct net_device *dev)
+{
+ if (dev->reg_state == NETREG_REGISTERED ||
+ dev->reg_state == NETREG_UNREGISTERING)
+ netdev_assert_locked(dev);
+}
+
static inline void netif_napi_set_irq(struct napi_struct *napi, int irq)
{
napi->irq = irq;
diff --git a/net/core/dev.c b/net/core/dev.c
index 1151baaedf4d..5872f0797cc3 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -6784,6 +6784,8 @@ int dev_set_threaded(struct net_device *dev, bool threaded)
struct napi_struct *napi;
int err = 0;
+ netdev_assert_locked_or_invisible(dev);
+
if (dev->threaded == threaded)
return 0;
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index 2d9afc6e2161..5602a3c12e9a 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -108,6 +108,36 @@ static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
return ret;
}
+/* Same as netdev_store() but takes netdev_lock() instead of rtnl_lock() */
+static ssize_t
+netdev_lock_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t len,
+ int (*set)(struct net_device *, unsigned long))
+{
+ struct net_device *netdev = to_net_dev(dev);
+ struct net *net = dev_net(netdev);
+ unsigned long new;
+ int ret;
+
+ if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
+ return -EPERM;
+
+ ret = kstrtoul(buf, 0, &new);
+ if (ret)
+ return ret;
+
+ netdev_lock(netdev);
+
+ if (dev_isalive(netdev)) {
+ ret = (*set)(netdev, new);
+ if (ret == 0)
+ ret = len;
+ }
+ netdev_unlock(netdev);
+
+ return ret;
+}
+
NETDEVICE_SHOW_RO(dev_id, fmt_hex);
NETDEVICE_SHOW_RO(dev_port, fmt_dec);
NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
@@ -638,7 +668,7 @@ static ssize_t threaded_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
{
- return netdev_store(dev, attr, buf, len, modify_napi_threaded);
+ return netdev_lock_store(dev, attr, buf, len, modify_napi_threaded);
}
static DEVICE_ATTR_RW(threaded);
--
2.47.1
next prev parent reply other threads:[~2025-01-14 3:51 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-14 3:51 [PATCH net-next 00/11] net: use netdev->lock to protect NAPI Jakub Kicinski
2025-01-14 3:51 ` [PATCH net-next 01/11] net: add netdev_lock() / netdev_unlock() helpers Jakub Kicinski
2025-01-14 12:50 ` Eric Dumazet
2025-01-14 22:45 ` Joe Damato
2025-01-14 3:51 ` [PATCH net-next 02/11] net: add helpers for lookup and walking netdevs under netdev_lock() Jakub Kicinski
2025-01-14 13:03 ` Eric Dumazet
2025-01-14 14:45 ` Jakub Kicinski
2025-01-14 22:53 ` Joe Damato
2025-01-14 23:51 ` Jakub Kicinski
2025-01-14 3:51 ` [PATCH net-next 03/11] net: make netdev_lock() protect netdev->reg_state Jakub Kicinski
2025-01-14 13:05 ` Eric Dumazet
2025-01-14 22:57 ` Joe Damato
2025-01-14 3:51 ` [PATCH net-next 04/11] net: add netdev->up protected by netdev_lock() Jakub Kicinski
2025-01-14 13:08 ` Eric Dumazet
2025-01-14 23:09 ` Joe Damato
2025-01-14 3:51 ` [PATCH net-next 05/11] net: protect netdev->napi_list with netdev_lock() Jakub Kicinski
2025-01-14 13:09 ` Eric Dumazet
2025-01-14 23:10 ` Joe Damato
2025-01-14 3:51 ` [PATCH net-next 06/11] net: protect NAPI enablement " Jakub Kicinski
2025-01-14 13:14 ` Eric Dumazet
2025-01-14 23:00 ` Francois Romieu
2025-01-14 3:51 ` [PATCH net-next 07/11] net: make netdev netlink ops hold netdev_lock() Jakub Kicinski
2025-01-14 13:16 ` Eric Dumazet
2025-01-14 3:51 ` Jakub Kicinski [this message]
2025-01-14 13:19 ` [PATCH net-next 08/11] net: protect threaded status of NAPI with netdev_lock() Eric Dumazet
2025-01-14 3:51 ` [PATCH net-next 09/11] net: protect napi->irq " Jakub Kicinski
2025-01-14 13:20 ` Eric Dumazet
2025-01-14 23:19 ` Joe Damato
2025-01-14 3:51 ` [PATCH net-next 10/11] net: protect NAPI config fields " Jakub Kicinski
2025-01-14 13:23 ` Eric Dumazet
2025-01-14 23:20 ` Joe Damato
2025-01-14 3:51 ` [PATCH net-next 11/11] netdev-genl: remove rtnl_lock protection from NAPI ops Jakub Kicinski
2025-01-14 13:25 ` Eric Dumazet
2025-01-14 23:21 ` Joe Damato
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250114035118.110297-9-kuba@kernel.org \
--to=kuba@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=jdamato@fastly.com \
--cc=leitao@debian.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).