net: depend on instance lock for queue related netlink ops
Subsystems (6)
Jakub Kicinski
maintainercover net: depend on instance lock for queue related netlink ops
netdev-genl used to be protected by rtnl_lock. In previous release we already switched the queue management ops (for Rx zero-copy) to the instance lock. This series converts other ops to depend on the instance lock when possible.
Unfortunately queue related state is hard to lock (unlike NAPI) as the process of switching the number of queues usually involves a large reconfiguration of the driver. The reconfig process has historically been under rtnl_lock, but for drivers which opt into ops locking it is also under the instance lock. Leverage that and conditionally take rtnl_lock or instance lock depending on the device capabilities.
Jakub Kicinski (8):
net: avoid potential race between netdev_get_by_index_lock() and netns
switch
net: designate XSK pool pointers in queues as "ops protected"
netdev: add "ops compat locking" helpers
netdev: don't hold rtnl_lock over nl queue info get when possible
xdp: double protect netdev->xdp_flags with netdev->lock
netdev: depend on netdev->lock for xdp features
docs: netdev: break down the instance locking info per ops struct
netdev: depend on netdev->lock for qstats in ops locked drivers
Documentation/networking/netdevices.rst | 61 +++++++++++++---- include/linux/netdevice.h | 7 +- include/net/netdev_lock.h | 16 +++++ include/net/netdev_queues.h | 4 +- include/net/netdev_rx_queue.h | 6 +- include/net/xdp.h | 1 + net/core/dev.h | 17 ++++- drivers/net/ethernet/google/gve/gve_main.c | 2 +- net/core/dev.c | 76 ++++++++++++++++++++-- net/core/lock_debug.c | 2 +- net/core/netdev-genl.c | 73 ++++++++++----------- net/core/xdp.c | 12 +++- net/xdp/xsk.c | 2 + net/xdp/xsk_buff_pool.c | 7 +- 14 files changed, 220 insertions(+), 66 deletions(-)
Signature · 1 line
2.49.0
Jakub Kicinski
maintainerPATCH 1/8 net: avoid potential race between netdev_get_by_index_lock() and netns switch
netdev_get_by_index_lock() performs following steps:
rcu_lock(); dev = lookup(netns, ifindex); dev_get(dev); rcu_unlock(); [... lock & validate the dev ...] return dev
Validation right now only checks if the device is registered but since the lookup is netns-aware we must also protect against the device switching netns right after we dropped the RCU lock. Otherwise the caller in netns1 may get a pointer to a device which has just switched to netns2.
We can't hold the lock for the entire netns change process (because of the NETDEV_UNREGISTER notifier), and there's no existing marking to indicate that the netns is unlisted because of netns move, so add one.
AFAIU none of the existing netdev_get_by_index_lock() callers can suffer from this problem (NAPI code double checks the netns membership and other callers are either under rtnl_lock or not ns-sensitive), so this patch does not have to be treated as a fix.
--- include/linux/netdevice.h | 6 +++++- net/core/dev.h | 2 +- net/core/dev.c | 25 ++++++++++++++++++------- 3 files changed, 24 insertions(+), 9 deletions(-)
include/linux/netdevice.h +5 -1
@@ -1952,6 +1952,7 @@ enum netdev_reg_state {* @priv_destructor: Called from unregister * @npinfo: XXX: need comments on this one * @nd_net: Network namespace this network device is inside +* protected by @lock * * @ml_priv: Mid-layer private * @ml_priv_type: Mid-layer private type
@@ -2359,6 +2360,9 @@ struct net_device {bool dismantle; +/** @moving_ns: device is changing netns, protected by @lock */ +bool moving_ns; +enum {RTNL_LINK_INITIALIZED ,RTNL_LINK_INITIALIZING ,
@@ -2521,7 +2525,7 @@ struct net_device {* @net_shaper_hierarchy, @reg_state, @threaded * * Double protects: -* @up +* @up, @moving_ns, @nd_net * * Double ops protects: * @real_num_rx_queues, @real_num_tx_queues
net/core/dev.h +1 -1
@@ -29,7 +29,7 @@ netdev_napi_by_id_lock(struct net *net, unsigned int napi_id);struct net_device *dev_get_by_napi_id (unsigned int napi_id);struct net_device *netdev_get_by_index_lock (struct net *net,int ifindex); -struct net_device *__netdev_put_lock (struct net_device *dev); +struct net_device *__netdev_put_lock (struct net_device *dev,struct net *net);struct net_device *netdev_xa_find_lock (struct net *net,struct net_device *dev,unsigned long *index);
net/core/dev.c +18 -7
@@ -828,7 +828,7 @@ netdev_napi_by_id_lock(struct net *net, unsigned int napi_id)dev_hold (dev);rcu_read_unlock (); - dev =__netdev_put_lock (dev); + dev =__netdev_put_lock (dev, net);if (!dev)return NULL ;
@@ -1039,10 +1039,11 @@ struct net_device *dev_get_by_napi_id(unsigned int napi_id)* This helper is intended for locking net_device after it has been looked up * using a lockless lookup helper. Lock prevents the instance from going away. */ -struct net_device *__netdev_put_lock (struct net_device *dev) +struct net_device *__netdev_put_lock (struct net_device *dev,struct net *net) {netdev_lock (dev); -if (dev->reg_state >NETREG_REGISTERED ) { +if (dev->reg_state >NETREG_REGISTERED || + dev->moving_ns || !net_eq (dev_net (dev), net)) {netdev_unlock (dev);dev_put (dev);return NULL ;
@@ -1070,7 +1071,7 @@ struct net_device *netdev_get_by_index_lock(struct net *net, int ifindex)if (!dev)return NULL ; -return __netdev_put_lock (dev); +return __netdev_put_lock (dev, net); } struct net_device *
@@ -1090,7 +1091,7 @@ netdev_xa_find_lock(struct net *net, struct net_device *dev,dev_hold (dev);rcu_read_unlock (); - dev =__netdev_put_lock (dev); + dev =__netdev_put_lock (dev, net);if (dev)return dev;
@@ -12154,7 +12155,11 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net,netif_close (dev);/* And unlink it from device chain */ unlist_netdevice (dev); -netdev_unlock_ops (dev); + +if (!netdev_need_ops_lock (dev)) +netdev_lock (dev); + dev->moving_ns = true; +netdev_unlock (dev);synchronize_net ();
@@ -12190,7 +12195,9 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net,move_netdevice_notifiers_dev_net (dev, net);/* Actually switch the network namespace */ +netdev_lock (dev);dev_net_set (dev, net); +netdev_unlock (dev); dev->ifindex = new_ifindex;if (new_name[0 ]) {
@@ -12216,7 +12223,11 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net, err =netdev_change_owner (dev, net_old, net);WARN_ON (err); -netdev_lock_ops (dev); +netdev_lock (dev); + dev->moving_ns = false; +if (!netdev_need_ops_lock (dev)) +netdev_unlock (dev); +/* Add the device back in the hashes */ list_netdevice (dev);/* Notify protocols, that a new device appeared. */
Signature · 1 line
2.49.0
Re: [PATCH net-next 1/8] net: avoid potential race between netdev_get_by_index_lock() and netns switch
Quoted 24 lines · Jakub Kicinski
netdev_get_by_index_lock()performs following steps:rcu_lock(); dev = lookup(netns, ifindex); dev_get(dev); rcu_unlock(); [... lock & validate the dev ...] return devValidation right now only checks if the device is registered but since the lookup is netns-aware we must also protect against the device switching netns right after we dropped the RCU lock. Otherwise the caller in netns1 may get a pointer to a device which has just switched to netns2.
We can't hold the lock for the entire netns change process (because of the
NETDEV_UNREGISTERnotifier), and there's no existing marking to indicate that the netns is unlisted because of netns move, so add one.AFAIU none of the existing
netdev_get_by_index_lock()callers can suffer from this problem (NAPI code double checks the netns membership and other callers are either underrtnl_lockor not ns-sensitive), so this patch does not have to be treated as a fix.
Quoted 6 lines
Signed-off-by Jakub Kicinski <[email protected]>--- include/linux/netdevice.h | 6 +++++- net/core/dev.h | 2 +- net/core/dev.c | 25 ++++++++++++++++++------- 3 files changed, 24 insertions(+), 9 deletions(-)
Jakub Kicinski
maintainerPATCH 2/8 net: designate XSK pool pointers in queues as "ops protected"
In reply to Jakub KicinskiRead accesses go via xsk_get_pool_from_qid(), the call coming from the core and gve look safe (other "ops locked" drivers don't support XSK).
Write accesses go via xsk_reg_pool_at_qid() and xsk_clear_pool_at_qid(). Former is already under the ops lock, latter needs to be locked when coming from the workqueue via xp_clear_dev().
--- include/linux/netdevice.h | 1 + include/net/netdev_rx_queue.h | 6 +++--- net/xdp/xsk.c | 2 ++ net/xdp/xsk_buff_pool.c | 7 ++++++- 4 files changed, 12 insertions(+), 4 deletions(-)
include/linux/netdevice.h +1 -0
@@ -688,6 +688,7 @@ struct netdev_queue {/* Subordinate device that the queue has been assigned to */ struct net_device *sb_dev;#ifdef CONFIG_XDP_SOCKETS +/* "ops protected", see comment about net_device::lock */ struct xsk_buff_pool *pool;#endif
include/net/netdev_rx_queue.h +3 -3
@@ -20,12 +20,12 @@ struct netdev_rx_queue {struct net_device *dev;netdevice_tracker dev_tracker; +/* All fields below are "ops protected", +* see comment about net_device::lock +*/ #ifdef CONFIG_XDP_SOCKETS struct xsk_buff_pool *pool;#endif -/* NAPI instance for the queue -* "ops protected", see comment about net_device::lock -*/ struct napi_struct *napi;struct pp_memory_provider_params mp_params; }____cacheline_aligned_in_smp ;
net/xdp/xsk.c +2 -0
@@ -1654,7 +1654,9 @@ static int xsk_notifier(struct notifier_block *this,xsk_unbind_dev (xs);/* Clear device references. */ +netdev_lock_ops (dev);xp_clear_dev (xs->pool); +netdev_unlock_ops (dev); }mutex_unlock (&xs->mutex); }
net/xdp/xsk_buff_pool.c +6 -1
@@ -279,9 +279,14 @@ static void xp_release_deferred(struct work_struct *work) { struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool, work); +struct net_device *netdev = pool->netdev;rtnl_lock (); -xp_clear_dev (pool); +if (netdev) { +netdev_lock_ops (netdev); +xp_clear_dev (pool); +netdev_unlock_ops (netdev); + }rtnl_unlock ();if (pool->fq) {
Signature · 1 line
2.49.0
Re: [PATCH net-next 2/8] net: designate XSK pool pointers in queues as "ops protected"
Quoted 17 lines · Jakub Kicinski
Read accesses go via
xsk_get_pool_from_qid(), the call coming from the core and gve look safe (other "ops locked" drivers don't support XSK).Write accesses go via
xsk_reg_pool_at_qid()andxsk_clear_pool_at_qid(). Former is already under the ops lock, latter needs to be locked when coming from the workqueue viaxp_clear_dev().Signed-off-by Jakub Kicinski <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- include/linux/netdevice.h | 1 + include/net/netdev_rx_queue.h | 6 +++--- net/xdp/xsk.c | 2 ++ net/xdp/xsk_buff_pool.c | 7 ++++++- 4 files changed, 12 insertions(+), 4 deletions(-)
[...]
Quoted 8 lines
net/xdp/xsk_buff_pool.c +0
-0@@ -279,9 +279,14 @@ static void xp_release_deferred(struct work_struct *work) { struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool, work);
net/xdp/xsk_buff_pool.c static void xp_release_deferred(struct work_struct *work) · line 290
Quoted 10 lines
net/xdp/xsk_buff_pool.c +6
-1+struct net_device *netdev = pool->netdev;rtnl_lock (); -xp_clear_dev (pool); +if (netdev) { +netdev_lock_ops (netdev); +xp_clear_dev (pool); +netdev_unlock_ops (netdev); + }rtnl_unlock ();
Is it actually possible for netdev to be NULL here?
I feel like it probably isn't, but if it were possible we'd need an else case here to xp_clear_dev(pool) without the netdev_lock_ops?
Jakub Kicinski
maintainerRe: [PATCH net-next 2/8] net: designate XSK pool pointers in queues as "ops protected"
Quoted 9 lines · On Mon, 7 Apr 2025 19:17:36 -0700 Joe Damato wrote:
net/xdp/xsk_buff_pool.c static void xp_release_deferred(struct work_struct *work) · line 290
Quoted 7 lines
net/xdp/xsk_buff_pool.c +5
-1-xp_clear_dev (pool); +if (netdev) { +netdev_lock_ops (netdev); +xp_clear_dev (pool); +netdev_unlock_ops (netdev); + }rtnl_unlock ();Is it actually possible for netdev to be NULL here?
So I've been told in v1 review :) I should have probably linked to previous postings, these patches had been a part of various series before.
The code is indeed buggy, tho, we need to move the netdev = pool->netdev assignment under rtnl_lock..
Quoted 2 lines
I feel like it probably isn't, but if it were possible we'd need an else case here to
xp_clear_dev(pool)without thenetdev_lock_ops?
It does nothing when netdev is null. Actually, due to "other changes" all callers of xp_clear_dev() are now wrapped in the lock. We can move the locking inside that helper.
Jakub Kicinski
maintainerPATCH 3/8 netdev: add "ops compat locking" helpers
In reply to Jakub KicinskiAdd helpers to "lock a netdev in a backward-compatible way", which for ops-locked netdevs will mean take the instance lock. For drivers which haven't opted into the ops locking we'll take rtnl_lock.
The scoped foreach is dropping and re-taking the lock for each device, even if prev and next are both under rtnl_lock. I hope that's fine since we expect that netdev nl to be mostly supported by modern drivers, and modern drivers should also opt into the instance locking.
Note that these helpers are mostly needed for queue related state, because drivers modify queue config in their ops in a non-atomic way. Or differently put, queue changes don't have a clear-cut API like NAPI configuration. Any state that can should just use the instance lock directly, not the "compat" hacks.
--- include/net/netdev_lock.h | 16 ++++++++++++ net/core/dev.h | 15 ++++++++++++ net/core/dev.c | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+)
include/net/netdev_lock.h +16 -0
@@ -64,6 +64,22 @@ netdev_ops_assert_locked_or_invisible(const struct net_device *dev)netdev_ops_assert_locked (dev); } +static inline void netdev_lock_ops_compat (struct net_device *dev) +{ +if (netdev_need_ops_lock (dev)) +netdev_lock (dev); +else +rtnl_lock (); +} + +static inline void netdev_unlock_ops_compat (struct net_device *dev) +{ +if (netdev_need_ops_lock (dev)) +netdev_unlock (dev); +else +rtnl_unlock (); +} +static inline int netdev_lock_cmp_fn (const struct lockdep_map *a,const struct lockdep_map *b) {
net/core/dev.h +15 -0
@@ -41,6 +41,21 @@ DEFINE_FREE(netdev_unlock, struct net_device *, if (_T) netdev_unlock(_T)); (var_name =netdev_xa_find_lock (net, var_name, &ifindex)); \ ifindex++) +struct net_device * +netdev_get_by_index_lock_ops_compat (struct net *net,int ifindex); +struct net_device * +netdev_xa_find_lock_ops_compat (struct net *net,struct net_device *dev, +unsigned long *index); + +DEFINE_FREE(netdev_unlock_ops_compat, struct net_device *, + if (_T) netdev_unlock_ops_compat(_T)); + +#define for_each_netdev_lock_ops_compat_scoped (net, var_name, ifindex) \ + for (struct net_device *var_name __free(netdev_unlock_ops_compat) = NULL; \ + (var_name = netdev_xa_find_lock_ops_compat(net, var_name, \ + &ifindex)); \ + ifindex++) +#ifdef CONFIG_PROC_FS int __init dev_proc_init (void );#else
net/core/dev.c +51 -0
69 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH net-next 3/8] netdev: add "ops compat locking" helpers
Quoted 17 lines · Jakub Kicinski
Add helpers to "lock a netdev in a backward-compatible way", which for ops-locked netdevs will mean take the instance lock. For drivers which haven't opted into the ops locking we'll take
rtnl_lock.The scoped foreach is dropping and re-taking the lock for each device, even if prev and next are both under
rtnl_lock. I hope that's fine since we expect that netdev nl to be mostly supported by modern drivers, and modern drivers should also opt into the instance locking.Note that these helpers are mostly needed for queue related state, because drivers modify queue config in their ops in a non-atomic way. Or differently put, queue changes don't have a clear-cut API like NAPI configuration. Any state that can should just use the instance lock directly, not the "compat" hacks.
Quoted 7 lines
Signed-off-by Jakub Kicinski <[email protected]>--- include/net/netdev_lock.h | 16 ++++++++++++ net/core/dev.h | 15 ++++++++++++ net/core/dev.c | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+)
Jakub Kicinski
maintainerPATCH 4/8 netdev: don't hold rtnl_lock over nl queue info get when possible
In reply to Jakub KicinskiNetdev queue dump accesses: NAPI, memory providers, XSk pointers. All three are "ops protected" now, switch to the op compat locking. rtnl lock does not have to be taken for "ops locked" devices.
--- net/core/netdev-genl.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
net/core/netdev-genl.c +7 -11
@@ -481,18 +481,15 @@ int netdev_nl_queue_get_doit(struct sk_buff *skb, struct genl_info *info)if (!rsp)return -ENOMEM ; -rtnl_lock (); - - netdev =netdev_get_by_index_lock (genl_info_net (info), ifindex); + netdev =netdev_get_by_index_lock_ops_compat (genl_info_net (info), + ifindex);if (netdev) { err =netdev_nl_queue_fill (rsp, netdev, q_id, q_type, info); -netdev_unlock (netdev); +netdev_unlock_ops_compat (netdev); }else { err = -ENODEV ; } -rtnl_unlock (); -if (err) goto err_free_msg;
@@ -541,17 +538,17 @@ int netdev_nl_queue_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)if (info->attrs[NETDEV_A_QUEUE_IFINDEX ]) ifindex =nla_get_u32 (info->attrs[NETDEV_A_QUEUE_IFINDEX ]); -rtnl_lock ();if (ifindex) { - netdev =netdev_get_by_index_lock (net, ifindex); + netdev =netdev_get_by_index_lock_ops_compat (net, ifindex);if (netdev) { err =netdev_nl_queue_dump_one (netdev, skb, info, ctx); -netdev_unlock (netdev); +netdev_unlock_ops_compat (netdev); }else { err = -ENODEV ; } }else { -for_each_netdev_lock_scoped (net, netdev, ctx->ifindex) { +for_each_netdev_lock_ops_compat_scoped (net, netdev, + ctx->ifindex) { err =netdev_nl_queue_dump_one (netdev, skb, info, ctx);if (err <0 )break ;
@@ -559,7 +556,6 @@ int netdev_nl_queue_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) ctx->txq_idx =0 ; } } -rtnl_unlock ();return err; }
Signature · 1 line
2.49.0
Re: [PATCH net-next 4/8] netdev: don't hold rtnl_lock over nl queue info get when possible
Quoted 8 lines · Jakub Kicinski
Netdev queue dump accesses: NAPI, memory providers, XSk pointers. All three are "ops protected" now, switch to the op compat locking. rtnl lock does not have to be taken for "ops locked" devices.
Signed-off-by Jakub Kicinski <[email protected]>--- net/core/netdev-genl.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
Jakub Kicinski
maintainerPATCH 5/8 xdp: double protect netdev->xdp_flags with netdev->lock
In reply to Jakub KicinskiProtect xdp_features with netdev->lock. This way pure readers no longer have to take rtnl_lock to access the field.
This includes calling NETDEV_XDP_FEAT_CHANGE under the lock. Looks like that's fine for bonding, the only "real" listener, it's the same as ethtool feature change.
In terms of normal drivers - only GVE need special consideration (other drivers don't use instance lock or don't support XDP). It calls xdp_set_features_flag() helper from gve_init_priv() which in turn is called from gve_reset_recovery() (locked), or prior to netdev registration. So switch to _locked.
---
CC: [email protected]
--- Documentation/networking/netdevices.rst | 1 + include/linux/netdevice.h | 2 +- include/net/xdp.h | 1 + drivers/net/ethernet/google/gve/gve_main.c | 2 +- net/core/lock_debug.c | 2 +- net/core/xdp.c | 12 +++++++++++- 6 files changed, 16 insertions(+), 4 deletions(-)
Documentation/networking/netdevices.rst +1 -0
@@ -354,6 +354,7 @@ For devices with locked ops, currently only the following notifiers are running under the lock: * ``NETDEV_REGISTER`` * ``NETDEV_UP`` +* ``NETDEV_XDP_FEAT_CHANGE`` The following notifiers are running without the lock: * ``NETDEV_UNREGISTER``
include/linux/netdevice.h +1 -1
@@ -2526,7 +2526,7 @@ struct net_device {* @net_shaper_hierarchy, @reg_state, @threaded * * Double protects: -* @up, @moving_ns, @nd_net +* @up, @moving_ns, @nd_net, @xdp_flags * * Double ops protects: * @real_num_rx_queues, @real_num_tx_queues
include/net/xdp.h +1 -0
@@ -616,6 +616,7 @@ struct xdp_metadata_ops {u32 bpf_xdp_metadata_kfunc_id (int id);bool bpf_dev_bound_kfunc_id (u32 btf_id);void xdp_set_features_flag (struct net_device *dev,xdp_features_t val); +void xdp_set_features_flag_locked (struct net_device *dev,xdp_features_t val);void xdp_features_set_redirect_target (struct net_device *dev,bool support_sg);void xdp_features_clear_redirect_target (struct net_device *dev);#else
drivers/net/ethernet/google/gve/gve_main.c +1 -1
@@ -2185,7 +2185,7 @@ static void gve_set_netdev_xdp_features(struct gve_priv *priv) xdp_features =0 ; } -xdp_set_features_flag (priv->dev, xdp_features); +xdp_set_features_flag_locked (priv->dev, xdp_features); }static int gve_init_priv (struct gve_priv *priv,bool skip_describe_device)
net/core/lock_debug.c +1 -1
@@ -20,6 +20,7 @@ int netdev_debug_event(struct notifier_block *nb, unsigned long event,switch (cmd) {case NETDEV_REGISTER :case NETDEV_UP : +case NETDEV_XDP_FEAT_CHANGE :netdev_ops_assert_locked (dev); fallthrough;case NETDEV_DOWN :
@@ -58,7 +59,6 @@ int netdev_debug_event(struct notifier_block *nb, unsigned long event,case NETDEV_OFFLOAD_XSTATS_DISABLE :case NETDEV_OFFLOAD_XSTATS_REPORT_USED :case NETDEV_OFFLOAD_XSTATS_REPORT_DELTA : -case NETDEV_XDP_FEAT_CHANGE :ASSERT_RTNL ();break ;
net/core/xdp.c +11 -1
@@ -17,6 +17,7 @@#include <net/page_pool/helpers.h> #include <net/hotdata.h> +#include <net/netdev_lock.h> #include <net/xdp.h> #include <net/xdp_priv.h> /* struct xdp_mem_allocator */ #include <trace/events/xdp.h>
@@ -991,17 +992,26 @@ static int __init xdp_metadata_init(void) }late_initcall (xdp_metadata_init); -void xdp_set_features_flag (struct net_device *dev,xdp_features_t val) +void xdp_set_features_flag_locked (struct net_device *dev,xdp_features_t val) { val &=NETDEV_XDP_ACT_MASK ;if (dev->xdp_features == val)return ; +netdev_assert_locked_or_invisible (dev); dev->xdp_features = val;if (dev->reg_state ==NETREG_REGISTERED )call_netdevice_notifiers (NETDEV_XDP_FEAT_CHANGE , dev); } +EXPORT_SYMBOL_GPL (xdp_set_features_flag_locked); + +void xdp_set_features_flag (struct net_device *dev,xdp_features_t val) +{ +netdev_lock (dev); +xdp_set_features_flag_locked (dev, val); +netdev_unlock (dev); +}EXPORT_SYMBOL_GPL (xdp_set_features_flag);void xdp_features_set_redirect_target (struct net_device *dev,bool support_sg)
Signature · 1 line
2.49.0
Re: [PATCH net-next 5/8] xdp: double protect netdev->xdp_flags with netdev->lock
Quoted 16 lines · Jakub Kicinski
Protect
xdp_featureswithnetdev->lock. This way pure readers no longer have to takertnl_lockto access the field.This includes calling
NETDEV_XDP_FEAT_CHANGEunder the lock. Looks like that's fine for bonding, the only "real" listener, it's the same as ethtool feature change.In terms of normal drivers - only GVE need special consideration (other drivers don't use instance lock or don't support XDP). It calls
xdp_set_features_flag()helper fromgve_init_priv()which in turn is called fromgve_reset_recovery()(locked), or prior to netdev registration. So switch to_locked.Signed-off-by Jakub Kicinski <[email protected]>---
CC: [email protected]
Quoted 8 lines
--- Documentation/networking/netdevices.rst | 1 + include/linux/netdevice.h | 2 +- include/net/xdp.h | 1 + drivers/net/ethernet/google/gve/gve_main.c | 2 +- net/core/lock_debug.c | 2 +- net/core/xdp.c | 12 +++++++++++- 6 files changed, 16 insertions(+), 4 deletions(-)
Jakub Kicinski
maintainerPATCH 6/8 netdev: depend on netdev->lock for xdp features
In reply to Jakub KicinskiWrites to XDP features are now protected by netdev->lock. Other things we report are based on ops which don't change once device has been registered. It is safe to stop taking rtnl_lock, and depend on netdev->lock instead.
--- net/core/netdev-genl.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)
net/core/netdev-genl.c +12 -14
@@ -38,6 +38,8 @@ netdev_nl_dev_fill(struct net_device *netdev, struct sk_buff *rsp,u64 xdp_rx_meta =0 ;void *hdr; +netdev_assert_locked (netdev);/* note: rtnl_lock may not be held! */ + hdr =genlmsg_iput (rsp, info);if (!hdr)return -EMSGSIZE ;
@@ -122,15 +124,14 @@ int netdev_nl_dev_get_doit(struct sk_buff *skb, struct genl_info *info)if (!rsp)return -ENOMEM ; -rtnl_lock (); - - netdev =__dev_get_by_index (genl_info_net (info), ifindex); -if (netdev) - err =netdev_nl_dev_fill (netdev, rsp, info); -else + netdev =netdev_get_by_index_lock (genl_info_net (info), ifindex); +if (!netdev) { err = -ENODEV ; + goto err_free_msg; + } -rtnl_unlock (); + err =netdev_nl_dev_fill (netdev, rsp, info); +netdev_unlock (netdev);if (err) goto err_free_msg;
@@ -146,18 +147,15 @@ int netdev_nl_dev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) {struct netdev_nl_dump_ctx *ctx =netdev_dump_ctx (cb);struct net *net =sock_net (skb->sk); -struct net_device *netdev; -int err =0 ; +int err; -rtnl_lock (); -for_each_netdev_dump (net, netdev, ctx->ifindex) { +for_each_netdev_lock_scoped (net, netdev, ctx->ifindex) { err =netdev_nl_dev_fill (netdev, skb,genl_info_dump (cb));if (err <0 ) -break ; +return err; } -rtnl_unlock (); -return err; +return 0 ; } static int
Signature · 1 line
2.49.0
Re: [PATCH net-next 6/8] netdev: depend on netdev->lock for xdp features
Quoted 9 lines · Jakub Kicinski
Writes to XDP features are now protected by
netdev->lock. Other things we report are based on ops which don't change once device has been registered. It is safe to stop takingrtnl_lock, and depend onnetdev->lockinstead.Signed-off-by Jakub Kicinski <[email protected]>--- net/core/netdev-genl.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)
Jakub Kicinski
maintainerPATCH 7/8 docs: netdev: break down the instance locking info per ops struct
In reply to Jakub KicinskiExplicitly list all the ops structs and what locking they provide. Use "ops locked" as a term for drivers which have ops called under the instance lock.
--- Documentation/networking/netdevices.rst | 54 +++++++++++++++++++------ 1 file changed, 42 insertions(+), 12 deletions(-)
Documentation/networking/netdevices.rst +42 -12
@@ -314,13 +314,8 @@ struct napi_struct synchronization rules softirq will be called with interrupts disabled by netconsole. -struct netdev_queue_mgmt_ops synchronization rules -================================================== - -All queue management ndo callbacks are holding netdev instance lock. - -RTNL and netdev instance lock -============================= +netdev instance lock +==================== Historically, all networking control operations were protected by a single global lock known as ``rtnl_lock``. There is an ongoing effort to replace this
@@ -328,10 +323,13 @@ global lock with separate locks for each network namespace. Additionally, properties of individual netdev are increasingly protected by per-netdev locks. For device drivers that implement shaping or queue management APIs, all control -operations will be performed under the netdev instance lock. Currently, this -instance lock is acquired within the context of ``rtnl_lock``. The drivers -can also explicitly request instance lock to be acquired via -``request_ops_lock``. In the future, there will be an option for individual +operations will be performed under the netdev instance lock. +Drivers can also explicitly request instance lock to be held during ops +by setting ``request_ops_lock`` to true. Code comments and docs refer +to drivers which have ops called under the instance lock as "ops locked". +See also the documentation of the ``lock`` member of struct net_device. + +In the future, there will be an option for individual drivers to opt out of using ``rtnl_lock`` and instead perform their control operations directly under the netdev instance lock.
@@ -343,8 +341,40 @@ there are two sets of interfaces: ``dev_xxx`` and ``netif_xxx`` (e.g., acquiring the instance lock themselves, while the ``netif_xxx`` functions assume that the driver has already acquired the instance lock. +struct net_device_ops +--------------------- + +``ndos`` are called without holding the instance lock for most drivers. + +"Ops locked" drivers will have most of the ``ndos`` invoked under +the instance lock. + +struct ethtool_ops +------------------ + +Similarly to ``ndos`` the instance lock is only held for select drivers. +For "ops locked" drivers all ethtool ops without an exception should +be called under the instance lock. + +struct net_shaper_ops +--------------------- + +All net shaper callbacks are invoked while holding the netdev instance +lock. ``rtnl_lock`` may or may not be held. + +Note that supporting net shapers automatically enables "ops locking". + +struct netdev_queue_mgmt_ops +---------------------------- + +All queue management callbacks are invoked while holding the netdev instance +lock. ``rtnl_lock`` may or may not be held. + +Note that supporting struct netdev_queue_mgmt_ops automatically enables +"ops locking". + Notifiers and netdev instance lock -================================== +---------------------------------- For device drivers that implement shaping or queue management APIs, some of the notifiers (``enum netdev_cmd``) are running under the netdev
Signature · 1 line
2.49.0
Re: [PATCH net-next 7/8] docs: netdev: break down the instance locking info per ops struct
Quoted 14 lines · Jakub Kicinski
Explicitly list all the ops structs and what locking they provide. Use "ops locked" as a term for drivers which have ops called under the instance lock.
Signed-off-by Jakub Kicinski <[email protected]>--- Documentation/networking/netdevices.rst | 54 +++++++++++++++++++------ 1 file changed, 42 insertions(+), 12 deletions(-)Documentation/networking/netdevices.rst +0
-0@@ -314,13 +314,8 @@ struct napi_struct synchronization rules
[...]
Documentation/networking/netdevices.rst there are two sets of interfaces: ``dev_xxx`` and ``netif_xxx`` (e.g., · line 357
Quoted 6 lines
Documentation/networking/netdevices.rst +6
-0+struct ethtool_ops +------------------ + +Similarly to ``ndos`` the instance lock is only held for select drivers. +For "ops locked" drivers all ethtool ops without an exception should +be called under the instance lock.
Extreme nit (which you can ignore): "without an exception" read oddly to me. Did you mean "without exception" ?
At any rate:
Jakub Kicinski
maintainerRe: [PATCH net-next 7/8] docs: netdev: break down the instance locking info per ops struct
Quoted 6 lines · On Mon, 7 Apr 2025 19:34:34 -0700 Joe Damato wrote:
Documentation/networking/netdevices.rst there are two sets of interfaces: ``dev_xxx`` and ``netif_xxx`` (e.g., · line 357
Quoted 3 lines
Documentation/networking/netdevices.rst +3
-0+Similarly to ``ndos`` the instance lock is only held for select drivers. +For "ops locked" drivers all ethtool ops without an exception should +be called under the instance lock.Extreme nit (which you can ignore): "without an exception" read oddly to me. Did you mean "without exception" ?
I guess I'll never understand indefinite articles. Let me change to "without exceptions".
AFAIU plural forms are a get out of jail free card..
Jakub Kicinski
maintainerPATCH 8/8 netdev: depend on netdev->lock for qstats in ops locked drivers
In reply to Jakub KicinskiWe mostly needed rtnl_lock in qstat to make sure the queue count is stable while we work. For "ops locked" drivers the instance lock protects the queue count, so we don't have to take rtnl_lock.
For currently ops-locked drivers: netdevsim and bnxt need the protection from netdev going down while we dump, which instance lock provides. gve doesn't care.
--- Documentation/networking/netdevices.rst | 6 +++++ include/net/netdev_queues.h | 4 +++- net/core/netdev-genl.c | 29 +++++++++++++++---------- 3 files changed, 26 insertions(+), 13 deletions(-)
Documentation/networking/netdevices.rst +6 -0
@@ -356,6 +356,12 @@ Similarly to ``ndos`` the instance lock is only held for select drivers. For "ops locked" drivers all ethtool ops without an exception should be called under the instance lock. +struct netdev_stat_ops +---------------------- + +"qstat" ops are invoked under the instance lock for "ops locked" drivers, +and under rtnl_lock for all other drivers. + struct net_shaper_ops ---------------------
include/net/netdev_queues.h +3 -1
@@ -85,9 +85,11 @@ struct netdev_queue_stats_tx {* for some of the events is not maintained, and reliable "total" cannot * be provided). * +* Ops are called under the instance lock if netdev_need_ops_lock() +* returns true, otherwise under rtnl_lock. * Device drivers can assume that when collecting total device stats, * the @get_base_stats and subsequent per-queue calls are performed -* "atomically" (without releasing the rtnl_lock). +* "atomically" (without releasing the relevant lock). * * Device drivers are encouraged to reset the per-queue statistics when * number of queues change. This is because the primary use case for
net/core/netdev-genl.c +17 -12
@@ -795,26 +795,31 @@ int netdev_nl_qstats_get_dumpit(struct sk_buff *skb,if (info->attrs[NETDEV_A_QSTATS_IFINDEX ]) ifindex =nla_get_u32 (info->attrs[NETDEV_A_QSTATS_IFINDEX ]); -rtnl_lock ();if (ifindex) { - netdev =__dev_get_by_index (net, ifindex); -if (netdev && netdev->stat_ops) { + netdev =netdev_get_by_index_lock_ops_compat (net, ifindex); +if (!netdev) { +NL_SET_BAD_ATTR (info->extack, + info->attrs[NETDEV_A_QSTATS_IFINDEX ]); +return -ENODEV ; + } +if (netdev->stat_ops) { err =netdev_nl_qstats_get_dump_one (netdev, scope, skb, info, ctx); }else {NL_SET_BAD_ATTR (info->extack, info->attrs[NETDEV_A_QSTATS_IFINDEX ]); - err = netdev ? -EOPNOTSUPP : -ENODEV ; - } - }else { -for_each_netdev_dump (net, netdev, ctx->ifindex) { - err =netdev_nl_qstats_get_dump_one (netdev, scope, skb, - info, ctx); -if (err <0 ) -break ; + err = -EOPNOTSUPP ; } +netdev_unlock_ops_compat (netdev); +return err; + } + +for_each_netdev_lock_ops_compat_scoped (net, netdev, ctx->ifindex) { + err =netdev_nl_qstats_get_dump_one (netdev, scope, skb, + info, ctx); +if (err <0 ) +break ; } -rtnl_unlock ();return err; }
Signature · 1 line
2.49.0
Re: [PATCH net-next 8/8] netdev: depend on netdev->lock for qstats in ops locked drivers
Quoted 8 lines · Jakub Kicinski
We mostly needed
rtnl_lockin qstat to make sure the queue count is stable while we work. For "ops locked" drivers the instance lock protects the queue count, so we don't have to takertnl_lock.For currently ops-locked drivers: netdevsim and bnxt need the protection from netdev going down while we dump, which instance lock provides. gve doesn't care.
Quoted 6 lines
Signed-off-by Jakub Kicinski <[email protected]>--- Documentation/networking/netdevices.rst | 6 +++++ include/net/netdev_queues.h | 4 +++- net/core/netdev-genl.c | 29 +++++++++++++++---------- 3 files changed, 26 insertions(+), 13 deletions(-)
Quoted 13 lines · Jakub Kicinski
netdev-genl used to be protected by
rtnl_lock. In previous release we already switched the queue management ops (for Rx zero-copy) to the instance lock. This series converts other ops to depend on the instance lock when possible.Unfortunately queue related state is hard to lock (unlike NAPI) as the process of switching the number of queues usually involves a large reconfiguration of the driver. The reconfig process has historically been under
rtnl_lock, but for drivers which opt into ops locking it is also under the instance lock. Leverage that and conditionally takertnl_lockor instance lock depending on the device capabilities.
Quoted 10 lines
Jakub Kicinski (8):
net: avoid potential race betweennetdev_get_by_index_lock()and netns
switch
net: designate XSK pool pointers in queues as "ops protected"
netdev: add "ops compat locking" helpers
netdev: don't holdrtnl_lockover nl queue info get when possible
xdp: double protectnetdev->xdp_flagswithnetdev->lock
netdev: depend onnetdev->lockfor xdp features
docs: netdev: break down the instance locking info per ops struct
netdev: depend onnetdev->lockfor qstats in ops locked drivers