net: Hold netdev instance lock during ndo operations
Subsystems (19)
Stanislav Fomichev
reviewerv10 cover net: Hold netdev instance lock during ndo operations
As the gradual purging of rtnl continues, start grabbing netdev instance lock in more places so we can get to the state where most paths are working without rtnl. Start with requiring the drivers that use shaper api (and later queue mgmt api) to work with both rtnl and netdev instance lock. Eventually we might attempt to drop rtnl. This mostly affects iavf, gve, bnxt and netdev sim (as the drivers that implement shaper/queue mgmt) so those drivers are converted in the process.
call_netdevice_notifiers locking is very inconsistent and might need a separate follow up. Some notified events are covered by the instance lock, some are not, which might complicate the driver expectations.
Changes since v10:
- rebase on top of net-next
- redo 'tcm = nlmsg_data' during reinit in tc_modify_qdisc (Jamal)
Changes since v9:
- rework ndo_setup_tc locking (Saeed)
- net: hold netdev instance lock during ndo_setup_tc
- keep only nft parts (hopefully ok to keep Eric's RB)
- 2 new patches to grab the lock at sch_api netlink level
- net: sched: wrap doit/dumpit methods
- general refactoring to make it easier to grab instance lock
- net: hold netdev instance lock during qdisc ndo_setup_tc
- net: ethtool: try to protect all callback with netdev instance lock
- remove the lock around get_ts_info
Changes since v8:
- rebase on top of net-next
- redo 'tcm = nlmsg_data(n)' on reinit (Jamal)
Changes since v7:
- fix AA deadlock detection in netdev_lock_cmp_fn (Jakub)
Changes since v6:
- rebase on top of net-next
Changes since v5:
- fix comment in bnxt_lock_sp (Michael)
- add netdev_lock/unlock around GVE suspend/resume (Sabrina)
- grab netdev lock around ethtool_ops->reset in cmis_fw_update_reset (Sabrina)
Changes since v4:
- reword documentation about rtnl_lock and instance lock relation (Jakub)
- do s/RTNL/rtnl_lock/ in the documentation (Jakub)
- mention dev_xxx/netif_xxx distinction (Paolo)
- add new patch to add request_ops_lock opt-in (Jakub)
- drop patch that adds shaper API to dummy (Jakub)
- drop () around dev in netdev_need_ops_lock
Changes since v3:
- add instance lock to netdev_lockdep_set_classes, move lock_set_cmp_fn to happen after set_class (NIPA)
Changes since v2:
- new patch to replace dev_addr_sem with instance lock (forwarding tests)
- CONFIG_LOCKDEP around netdev_lock_cmp_fn (Jakub)
- remove netif_device_present check from dev_setup_tc (bpf_offload.py)
- reorder bpf_devs_locks and instance lock ordering in bpf map offload (bpf_offload.py)
Changes since v1:
- fix netdev_set_mtu_ext_locked in the wrong place ([email protected])
- add missing depend on CONFIG_NET_SHAPER for dummy device ([email protected])
- not sure we need to apply dummy device patch..
- need_netdev_ops_lock -> netdev_need_ops_lock (Jakub)
- remove netdev_assert_locked near napi_xxx_locked calls (Jakub)
- fix netdev_lock_cmp_fn comment and line length (Jakub)
- fix kdoc style of dev_api.c routines (Jakub)
- reflow dev_setup_tc to avoid indent (Jakub)
- keep tc_can_offload checks outside of dev_setup_tc (Jakub)
Changes since RFC:
- other control paths are protected
- bntx has been converted to mostly depend on netdev instance lock
Jakub Kicinski (1):
net: ethtool: try to protect all callback with netdev instance lock
Stanislav Fomichev (13):
net: hold netdev instance lock during ndo_open/ndo_stop
net: hold netdev instance lock during nft ndo_setup_tc
net: sched: wrap doit/dumpit methods
net: hold netdev instance lock during qdisc ndo_setup_tc
net: hold netdev instance lock during queue operations
net: hold netdev instance lock during rtnetlink operations
net: hold netdev instance lock during ioctl operations
net: hold netdev instance lock during sysfs operations
net: hold netdev instance lock during ndo_bpf
net: replace dev_addr_sem with netdev instance lock
net: add option to request netdev instance lock
docs: net: document new locking reality
eth: bnxt: remove most dependencies on RTNL
Documentation/networking/netdevices.rst | 65 +++- drivers/net/bonding/bond_main.c | 16 +- drivers/net/ethernet/broadcom/bnxt/bnxt.c | 133 ++++---- .../net/ethernet/broadcom/bnxt/bnxt_devlink.c | 9 + .../net/ethernet/broadcom/bnxt/bnxt_sriov.c | 6 + drivers/net/ethernet/broadcom/bnxt/bnxt_ulp.c | 16 +- drivers/net/ethernet/broadcom/bnxt/bnxt_vfr.c | 18 +- drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c | 3 +- drivers/net/ethernet/google/gve/gve_main.c | 12 +- drivers/net/ethernet/google/gve/gve_utils.c | 6 +- drivers/net/ethernet/intel/iavf/iavf_main.c | 16 +- drivers/net/netdevsim/ethtool.c | 2 - drivers/net/netdevsim/netdev.c | 39 ++- drivers/net/tap.c | 2 +- drivers/net/tun.c | 2 +- include/linux/netdevice.h | 90 ++++- kernel/bpf/offload.c | 6 +- net/8021q/vlan_dev.c | 4 +- net/core/Makefile | 2 +- net/core/dev.c | 285 ++++++---------- net/core/dev.h | 22 +- net/core/dev_api.c | 318 ++++++++++++++++++ net/core/dev_ioctl.c | 69 ++-- net/core/net-sysfs.c | 9 +- net/core/netdev_rx_queue.c | 5 + net/core/rtnetlink.c | 50 ++- net/dsa/conduit.c | 16 +- net/ethtool/cabletest.c | 20 +- net/ethtool/cmis_fw_update.c | 7 +- net/ethtool/features.c | 6 +- net/ethtool/ioctl.c | 6 + net/ethtool/module.c | 8 +- net/ethtool/netlink.c | 12 + net/ethtool/phy.c | 20 +- net/ethtool/rss.c | 2 + net/ethtool/tsinfo.c | 9 +- net/netfilter/nf_flow_table_offload.c | 2 +- net/netfilter/nf_tables_offload.c | 2 +- net/sched/sch_api.c | 215 ++++++++---- net/xdp/xsk.c | 3 + net/xdp/xsk_buff_pool.c | 2 + 41 files changed, 1046 insertions(+), 489 deletions(-) create mode 100644 net/core/dev_api.c
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 1/14 net: hold netdev instance lock during ndo_open/ndo_stop
For the drivers that use shaper API, switch to the mode where core stack holds the netdev lock. This affects two drivers:
* iavf - already grabs netdev lock in ndo_open/ndo_stop, so mostly remove these
* netdevsim - switch to _locked APIs to avoid deadlock
iavf_close diff is a bit confusing, the existing call looks like this:
iavf_close() {
netdev_lock()
..
netdev_unlock()
wait_event_timeout(down_waitqueue)}
I change it to the following:
netdev_lock()
iavf_close() {
..
netdev_unlock()
wait_event_timeout(down_waitqueue)
netdev_lock() // reusing this lock call
}
netdev_unlock()
Since I'm reusing existing netdev_lock call, so it looks like I only add netdev_unlock.
--- drivers/net/ethernet/intel/iavf/iavf_main.c | 14 ++++++------- drivers/net/netdevsim/netdev.c | 14 ++++++++----- include/linux/netdevice.h | 23 +++++++++++++++++++++ net/core/dev.c | 12 +++++++++++ net/core/dev.h | 6 ++++-- 5 files changed, 54 insertions(+), 15 deletions(-)
drivers/net/ethernet/intel/iavf/iavf_main.c +6 -8
@@ -4562,22 +4562,21 @@ static int iavf_open(struct net_device *netdev)struct iavf_adapter *adapter =netdev_priv (netdev);int err; +netdev_assert_locked (netdev); +if (adapter->flags &IAVF_FLAG_PF_COMMS_FAILED ) {dev_err (&adapter->pdev->dev,"Unable to open device due to PF driver failure.\n" );return -EIO ; } -netdev_lock (netdev);while (!mutex_trylock (&adapter->crit_lock)) {/* If we are in __IAVF_INIT_CONFIG_ADAPTER state the crit_lock * is already taken and iavf_open is called from an upper * device's notifier reacting on NETDEV_REGISTER event. * We have to leave here to avoid dead lock. */ -if (adapter->state == __IAVF_INIT_CONFIG_ADAPTER) { -netdev_unlock (netdev); +if (adapter->state == __IAVF_INIT_CONFIG_ADAPTER)return -EBUSY ; - }usleep_range (500 ,1000 ); }
@@ -4626,7 +4625,6 @@ static int iavf_open(struct net_device *netdev)iavf_irq_enable (adapter, true);mutex_unlock (&adapter->crit_lock); -netdev_unlock (netdev);return 0 ;
@@ -4639,7 +4637,6 @@ static int iavf_open(struct net_device *netdev)iavf_free_all_tx_resources (adapter); err_unlock:mutex_unlock (&adapter->crit_lock); -netdev_unlock (netdev);return err; }
@@ -4661,12 +4658,12 @@ static int iavf_close(struct net_device *netdev)u64 aq_to_restore;int status; -netdev_lock (netdev); +netdev_assert_locked (netdev); +mutex_lock (&adapter->crit_lock);if (adapter->state <= __IAVF_DOWN_PENDING) {mutex_unlock (&adapter->crit_lock); -netdev_unlock (netdev);return 0 ; }
@@ -4719,6 +4716,7 @@ static int iavf_close(struct net_device *netdev)if (!status)netdev_warn (netdev,"Device resources not yet released\n" ); +netdev_lock (netdev);mutex_lock (&adapter->crit_lock); adapter->aq_required |= aq_to_restore;mutex_unlock (&adapter->crit_lock);
drivers/net/netdevsim/netdev.c +9 -5
@@ -402,7 +402,7 @@ static int nsim_init_napi(struct netdevsim *ns)for (i =0 ; i < dev->num_rx_queues; i++) { rq = ns->rq[i]; -netif_napi_add_config (dev, &rq->napi, nsim_poll, i); +netif_napi_add_config_locked (dev, &rq->napi, nsim_poll, i); }for (i =0 ; i < dev->num_rx_queues; i++) {
@@ -422,7 +422,7 @@ static int nsim_init_napi(struct netdevsim *ns) }for (i =0 ; i < dev->num_rx_queues; i++) -__netif_napi_del (&ns->rq[i]->napi); +__netif_napi_del_locked (&ns->rq[i]->napi);return err; }
@@ -452,7 +452,7 @@ static void nsim_enable_napi(struct netdevsim *ns)struct nsim_rq *rq = ns->rq[i];netif_queue_set_napi (dev, i,NETDEV_QUEUE_TYPE_RX , &rq->napi); -napi_enable (&rq->napi); +napi_enable_locked (&rq->napi); } }
@@ -461,6 +461,8 @@ static int nsim_open(struct net_device *dev)struct netdevsim *ns =netdev_priv (dev);int err; +netdev_assert_locked (dev); + err =nsim_init_napi (ns);if (err)return err;
@@ -478,8 +480,8 @@ static void nsim_del_napi(struct netdevsim *ns)for (i =0 ; i < dev->num_rx_queues; i++) {struct nsim_rq *rq = ns->rq[i]; -napi_disable (&rq->napi); -__netif_napi_del (&rq->napi); +napi_disable_locked (&rq->napi); +__netif_napi_del_locked (&rq->napi); }synchronize_net ();
@@ -494,6 +496,8 @@ static int nsim_stop(struct net_device *dev)struct netdevsim *ns =netdev_priv (dev);struct netdevsim *peer; +netdev_assert_locked (dev); +netif_carrier_off (dev); peer =rtnl_dereference (ns->peer);if (peer)
include/linux/netdevice.h +23 -0
@@ -2753,6 +2753,29 @@ static inline void netdev_assert_locked_or_invisible(struct net_device *dev)netdev_assert_locked (dev); } +static inline bool netdev_need_ops_lock (struct net_device *dev) +{ +bool ret = false; + +#if IS_ENABLED (CONFIG_NET_SHAPER ) + ret |= !!dev->netdev_ops->net_shaper_ops; +#endif + +return ret; +} + +static inline void netdev_lock_ops (struct net_device *dev) +{ +if (netdev_need_ops_lock (dev)) +netdev_lock (dev); +} + +static inline void netdev_unlock_ops (struct net_device *dev) +{ +if (netdev_need_ops_lock (dev)) +netdev_unlock (dev); +} +void netif_napi_set_irq_locked (struct napi_struct *napi,int irq);static inline void netif_napi_set_irq (struct napi_struct *napi,int irq)
net/core/dev.c +12 -0
@@ -1627,6 +1627,8 @@ static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)if (ret)return ret; +netdev_lock_ops (dev); +set_bit (__LINK_STATE_START, &dev->state);if (ops->ndo_validate_addr)
@@ -1646,6 +1648,8 @@ static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)add_device_randomness (dev->dev_addr, dev->addr_len); } +netdev_unlock_ops (dev); +return ret; }
@@ -1716,11 +1720,19 @@ static void __dev_close_many(struct list_head *head)* We allow it to be called even after a DETACH hot-plug * event. */ + +/* TODO: move the lock up before clearing __LINK_STATE_START. +* Generates spurious lockdep warning. +*/ +netdev_lock_ops (dev); +if (ops->ndo_stop) ops->ndo_stop (dev);netif_set_up (dev, false);netpoll_poll_enable (dev); + +netdev_unlock_ops (dev); } }
net/core/dev.h +4 -2
@@ -134,9 +134,11 @@ static inline void netif_set_up(struct net_device *dev, bool value)else dev->flags &= ~IFF_UP ; -netdev_lock (dev); +if (!netdev_need_ops_lock (dev)) +netdev_lock (dev); dev->up = value; -netdev_unlock (dev); +if (!netdev_need_ops_lock (dev)) +netdev_unlock (dev); } static inline void netif_set_gso_max_size(struct net_device *dev,
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 2/14 net: hold netdev instance lock during nft ndo_setup_tc
In reply to Stanislav FomichevIntroduce new dev_setup_tc for nft ndo_setup_tc paths.
--- drivers/net/ethernet/intel/iavf/iavf_main.c | 2 -- include/linux/netdevice.h | 2 ++ net/core/dev.c | 18 ++++++++++++++++++ net/netfilter/nf_flow_table_offload.c | 2 +- net/netfilter/nf_tables_offload.c | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-)
drivers/net/ethernet/intel/iavf/iavf_main.c +0 -2
@@ -3894,10 +3894,8 @@ static int __iavf_setup_tc(struct net_device *netdev, void *type_data)if (test_bit (__IAVF_IN_REMOVE_TASK, &adapter->crit_section))return 0 ; -netdev_lock (netdev);netif_set_real_num_rx_queues (netdev, total_qps);netif_set_real_num_tx_queues (netdev, total_qps); -netdev_unlock (netdev);return ret; }
include/linux/netdevice.h +2 -0
@@ -3353,6 +3353,8 @@ int dev_alloc_name(struct net_device *dev, const char *name);int dev_open (struct net_device *dev,struct netlink_ext_ack *extack);void dev_close (struct net_device *dev);void dev_close_many (struct list_head *head,bool unlink); +int dev_setup_tc (struct net_device *dev,enum tc_setup_type type, +void *type_data);void dev_disable_lro (struct net_device *dev);int dev_loopback_xmit (struct net *net,struct sock *sk,struct sk_buff *newskb); u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
net/core/dev.c +18 -0
@@ -1786,6 +1786,24 @@ void dev_close(struct net_device *dev) }EXPORT_SYMBOL (dev_close); +int dev_setup_tc (struct net_device *dev,enum tc_setup_type type, +void *type_data) +{ +const struct net_device_ops *ops = dev->netdev_ops; +int ret; + +ASSERT_RTNL (); + +if (!ops->ndo_setup_tc) +return -EOPNOTSUPP ; + +netdev_lock_ops (dev); + ret = ops->ndo_setup_tc (dev, type, type_data); +netdev_unlock_ops (dev); + +return ret; +} +EXPORT_SYMBOL (dev_setup_tc);/** * dev_disable_lro - disable Large Receive Offload on a device
net/netfilter/nf_flow_table_offload.c +1 -1
@@ -1175,7 +1175,7 @@ static int nf_flow_table_offload_cmd(struct flow_block_offload *bo,nf_flow_table_block_offload_init (bo,dev_net (dev), cmd, flowtable, extack);down_write (&flowtable->flow_block_lock); - err = dev->netdev_ops->ndo_setup_tc (dev,TC_SETUP_FT , bo); + err =dev_setup_tc (dev,TC_SETUP_FT , bo);up_write (&flowtable->flow_block_lock);if (err <0 )return err;
net/netfilter/nf_tables_offload.c +1 -1
@@ -390,7 +390,7 @@ static int nft_block_offload_cmd(struct nft_base_chain *chain,nft_flow_block_offload_init (&bo,dev_net (dev), cmd, chain, &extack); - err = dev->netdev_ops->ndo_setup_tc (dev,TC_SETUP_BLOCK , &bo); + err =dev_setup_tc (dev,TC_SETUP_BLOCK , &bo);if (err <0 )return err;
Signature · 1 line
2.48.1
Re: [PATCH net-next v10 02/14] net: hold netdev instance lock during nft ndo_setup_tc
Quoted 89 lines · Stanislav Fomichev
Introduce new
dev_setup_tcfor nftndo_setup_tcpaths.Reviewed-by Eric Dumazet <[email protected]>Cc Saeed Mahameed <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- drivers/net/ethernet/intel/iavf/iavf_main.c | 2 -- include/linux/netdevice.h | 2 ++ net/core/dev.c | 18 ++++++++++++++++++ net/netfilter/nf_flow_table_offload.c | 2 +- net/netfilter/nf_tables_offload.c | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-)drivers/net/ethernet/intel/iavf/iavf_main.c +0
-2@@ -3894,10 +3894,8 @@ static int __iavf_setup_tc(struct net_device *netdev, void *type_data)if (test_bit (__IAVF_IN_REMOVE_TASK, &adapter->crit_section))return 0 ; -netdev_lock (netdev);netif_set_real_num_rx_queues (netdev, total_qps);netif_set_real_num_tx_queues (netdev, total_qps); -netdev_unlock (netdev);return ret; }include/linux/netdevice.h +2
-0@@ -3353,6 +3353,8 @@ int dev_alloc_name(struct net_device *dev, const char *name);int dev_open (struct net_device *dev,struct netlink_ext_ack *extack);void dev_close (struct net_device *dev);void dev_close_many (struct list_head *head,bool unlink); +int dev_setup_tc (struct net_device *dev,enum tc_setup_type type, +void *type_data);void dev_disable_lro (struct net_device *dev);int dev_loopback_xmit (struct net *net,struct sock *sk,struct sk_buff *newskb); u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,net/core/dev.c +18
-0@@ -1786,6 +1786,24 @@ void dev_close(struct net_device *dev) }EXPORT_SYMBOL (dev_close); +int dev_setup_tc (struct net_device *dev,enum tc_setup_type type, +void *type_data) +{ +const struct net_device_ops *ops = dev->netdev_ops; +int ret; + +ASSERT_RTNL (); + +if (!ops->ndo_setup_tc) +return -EOPNOTSUPP ; + +netdev_lock_ops (dev); + ret = ops->ndo_setup_tc (dev, type, type_data); +netdev_unlock_ops (dev); + +return ret; +} +EXPORT_SYMBOL (dev_setup_tc);/** * dev_disable_lro - disable Large Receive Offload on a device net/netfilter/nf_flow_table_offload.c +1
-1@@ -1175,7 +1175,7 @@ static int nf_flow_table_offload_cmd(struct flow_block_offload *bo,nf_flow_table_block_offload_init (bo,dev_net (dev), cmd, flowtable, extack);down_write (&flowtable->flow_block_lock); - err = dev->netdev_ops->ndo_setup_tc (dev,TC_SETUP_FT , bo); + err =dev_setup_tc (dev,TC_SETUP_FT , bo);up_write (&flowtable->flow_block_lock);if (err <0 )return err;net/netfilter/nf_tables_offload.c +0
-0
net/netfilter/nf_tables_offload.c static int nft_block_offload_cmd(struct nft_base_chain *chain, · line 396
Quoted 8 lines
net/netfilter/nf_tables_offload.c +1
-1@@ -390,7 +390,7 @@ static int nft_block_offload_cmd(struct nft_base_chain *chain,nft_flow_block_offload_init (&bo,dev_net (dev), cmd, chain, &extack); - err = dev->netdev_ops->ndo_setup_tc (dev,TC_SETUP_BLOCK , &bo); + err =dev_setup_tc (dev,TC_SETUP_BLOCK , &bo);if (err <0 )return err;
It seems RTNL was not taken in this path, can you take a look ?
syzbot reported :
RTNL: assertion failed at net/core/dev.c (1769)
WARNING: CPU: 1 PID: 9148 at net/core/dev.c:1769 dev_setup_tc+0x315/0x360 net/core/dev.c:1769 Modules linked in: CPU: 1 UID: 0 PID: 9148 Comm: syz.3.1494 Not tainted 6.14.0-rc5-syzkaller-01064-g2525e16a2bae #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 02/12/2025 RIP: 0010:dev_setup_tc+0x315/0x360 net/core/dev.c:1769 Code: cc 49 89 ee e8 dc da f7 f7 c6 05 c0 39 5d 06 01 90 48 c7 c7 a0
5e 2e 8d 48 c7 c6 80 5e 2e 8d ba e9 06 00 00 e8 3c 97 b7 f7 90 <0f> 0b 90 90 e9 66 fd ff ff 89 d1 80 e1 07 38 c1 0f 8c aa fd ff ff
RSP: 0018:ffffc9000be3eed0 EFLAGS: 00010246
RAX: eea924c6092c5700 RBX: 0000000000000000 RCX: 0000000000080000
RDX: ffffc9000c979000 RSI: 000000000000491b RDI: 000000000000491c
RBP: ffff88802a810008 R08: ffffffff81818e32 R09: fffffbfff1d3a67c
R10: dffffc0000000000 R11: fffffbfff1d3a67c R12: ffffc9000be3f070
R13: ffffffff8d4ab1e0 R14: ffff88802a810008 R15: ffff88802a810000
FS: 00007fbe7aece6c0(0000) GS:ffff8880b8700000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 000000110c2b5042 CR3: 0000000024cd0000 CR4: 00000000003526f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace: <TASK> nf_flow_table_offload_cmd net/netfilter/nf_flow_table_offload.c:1178 [inline] nf_flow_table_offload_setup+0x2ff/0x710 net/netfilter/nf_flow_table_offload.c:1198 nft_register_flowtable_net_hooks+0x24c/0x570 net/netfilter/nf_tables_api.c:8918 nf_tables_newflowtable+0x19f4/0x23d0 net/netfilter/nf_tables_api.c:9139 nfnetlink_rcv_batch net/netfilter/nfnetlink.c:524 [inline] nfnetlink_rcv_skb_batch net/netfilter/nfnetlink.c:647 [inline] nfnetlink_rcv+0x14e3/0x2ab0 net/netfilter/nfnetlink.c:665 netlink_unicast_kernel net/netlink/af_netlink.c:1313 [inline] netlink_unicast+0x7f6/0x990 net/netlink/af_netlink.c:1339 netlink_sendmsg+0x8de/0xcb0 net/netlink/af_netlink.c:1883 sock_sendmsg_nosec net/socket.c:709 [inline] __sock_sendmsg+0x221/0x270 net/socket.c:724 ____sys_sendmsg+0x53a/0x860 net/socket.c:2564 ___sys_sendmsg net/socket.c:2618 [inline] __sys_sendmsg+0x269/0x350 net/socket.c:2650 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fbe79f8d169
Re: [PATCH net-next v10 02/14] net: hold netdev instance lock during nft ndo_setup_tc
Quoted 140 lines · Eric Dumazet
Quoted 89 lines · Stanislav Fomichev
Introduce new
dev_setup_tcfor nftndo_setup_tcpaths.Reviewed-by Eric Dumazet <[email protected]>Cc Saeed Mahameed <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- drivers/net/ethernet/intel/iavf/iavf_main.c | 2 -- include/linux/netdevice.h | 2 ++ net/core/dev.c | 18 ++++++++++++++++++ net/netfilter/nf_flow_table_offload.c | 2 +- net/netfilter/nf_tables_offload.c | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-)drivers/net/ethernet/intel/iavf/iavf_main.c +0
-2@@ -3894,10 +3894,8 @@ static int __iavf_setup_tc(struct net_device *netdev, void *type_data)if (test_bit (__IAVF_IN_REMOVE_TASK, &adapter->crit_section))return 0 ; -netdev_lock (netdev);netif_set_real_num_rx_queues (netdev, total_qps);netif_set_real_num_tx_queues (netdev, total_qps); -netdev_unlock (netdev);return ret; }include/linux/netdevice.h +2
-0@@ -3353,6 +3353,8 @@ int dev_alloc_name(struct net_device *dev, const char *name);int dev_open (struct net_device *dev,struct netlink_ext_ack *extack);void dev_close (struct net_device *dev);void dev_close_many (struct list_head *head,bool unlink); +int dev_setup_tc (struct net_device *dev,enum tc_setup_type type, +void *type_data);void dev_disable_lro (struct net_device *dev);int dev_loopback_xmit (struct net *net,struct sock *sk,struct sk_buff *newskb); u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,net/core/dev.c +18
-0@@ -1786,6 +1786,24 @@ void dev_close(struct net_device *dev) }EXPORT_SYMBOL (dev_close); +int dev_setup_tc (struct net_device *dev,enum tc_setup_type type, +void *type_data) +{ +const struct net_device_ops *ops = dev->netdev_ops; +int ret; + +ASSERT_RTNL (); + +if (!ops->ndo_setup_tc) +return -EOPNOTSUPP ; + +netdev_lock_ops (dev); + ret = ops->ndo_setup_tc (dev, type, type_data); +netdev_unlock_ops (dev); + +return ret; +} +EXPORT_SYMBOL (dev_setup_tc);/** * dev_disable_lro - disable Large Receive Offload on a device net/netfilter/nf_flow_table_offload.c +1
-1@@ -1175,7 +1175,7 @@ static int nf_flow_table_offload_cmd(struct flow_block_offload *bo,nf_flow_table_block_offload_init (bo,dev_net (dev), cmd, flowtable, extack);down_write (&flowtable->flow_block_lock); - err = dev->netdev_ops->ndo_setup_tc (dev,TC_SETUP_FT , bo); + err =dev_setup_tc (dev,TC_SETUP_FT , bo);up_write (&flowtable->flow_block_lock);if (err <0 )return err;net/netfilter/nf_tables_offload.c +0
-0net/netfilter/nf_tables_offload.c static int nft_block_offload_cmd(struct nft_base_chain *chain, · line 396
Quoted 8 lines
net/netfilter/nf_tables_offload.c +1
-1@@ -390,7 +390,7 @@ static int nft_block_offload_cmd(struct nft_base_chain *chain,nft_flow_block_offload_init (&bo,dev_net (dev), cmd, chain, &extack); - err = dev->netdev_ops->ndo_setup_tc (dev,TC_SETUP_BLOCK , &bo); + err =dev_setup_tc (dev,TC_SETUP_BLOCK , &bo);if (err <0 )return err;It seems RTNL was not taken in this path, can you take a look ?
syzbot reported :
RTNL: assertion failed at
net/core/dev.c(1769)WARNING: CPU: 1 PID: 9148 at net/core/dev.c:1769 dev_setup_tc+0x315/0x360 net/core/dev.c:1769 Modules linked in: CPU: 1 UID: 0 PID: 9148 Comm: syz.3.1494 Not tainted 6.14.0-rc5-syzkaller-01064-g2525e16a2bae #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 02/12/2025 RIP: 0010:dev_setup_tc+0x315/0x360 net/core/dev.c:1769 Code: cc 49 89 ee e8 dc da f7 f7 c6 05 c0 39 5d 06 01 90 48 c7 c7 a05e 2e 8d 48 c7 c6 80 5e 2e 8d ba e9 06 00 00 e8 3c 97 b7 f7 90 <0f> 0b 90 90 e9 66 fd ff ff 89 d1 80 e1 07 38 c1 0f 8c aa fd ff ff
RSP: 0018:ffffc9000be3eed0 EFLAGS: 00010246
RAX:eea924c6092c5700RBX: 0000000000000000 RCX: 0000000000080000
RDX:ffffc9000c979000RSI:000000000000491bRDI:000000000000491c
RBP:ffff88802a810008R08:ffffffff81818e32R09:fffffbfff1d3a67c
R10:dffffc0000000000R11:fffffbfff1d3a67cR12:ffffc9000be3f070
R13:ffffffff8d4ab1e0R14:ffff88802a810008R15:ffff88802a810000
FS: 00007fbe7aece6c0(0000) GS:ffff8880b8700000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2:000000110c2b5042CR3:0000000024cd0000CR4:00000000003526f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6:00000000fffe0ff0DR7: 0000000000000400Call Trace: <TASK> nf_flow_table_offload_cmd net/netfilter/nf_flow_table_offload.c:1178 [inline] nf_flow_table_offload_setup+0x2ff/0x710 net/netfilter/nf_flow_table_offload.c:1198 nft_register_flowtable_net_hooks+0x24c/0x570 net/netfilter/nf_tables_api.c:8918 nf_tables_newflowtable+0x19f4/0x23d0 net/netfilter/nf_tables_api.c:9139 nfnetlink_rcv_batch net/netfilter/nfnetlink.c:524 [inline] nfnetlink_rcv_skb_batch net/netfilter/nfnetlink.c:647 [inline] nfnetlink_rcv+0x14e3/0x2ab0 net/netfilter/nfnetlink.c:665 netlink_unicast_kernel net/netlink/af_netlink.c:1313 [inline] netlink_unicast+0x7f6/0x990 net/netlink/af_netlink.c:1339 netlink_sendmsg+0x8de/0xcb0 net/netlink/af_netlink.c:1883 sock_sendmsg_nosec net/socket.c:709 [inline]
Quoted 8 lines
__sock_sendmsg+0x221/0x270 net/socket.c:724 ____sys_sendmsg+0x53a/0x860 net/socket.c:2564 ___sys_sendmsg net/socket.c:2618 [inline] __sys_sendmsg+0x269/0x350 net/socket.c:2650 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fbe79f8d169
Interesting. Looks like this path (and same for nft_block_offload_cmd?) was never grabbing grabbing rtnl_lock but calling device's ndo_setup_tc. Will take a look! CC'd Pablo in case it was by design.
Stanislav Fomichev
reviewerPATCH v10 3/14 net: sched: wrap doit/dumpit methods
In reply to Stanislav FomichevIn preparation for grabbing netdev instance lock around qdisc operations, introduce tc_xxx wrappers that lookup netdev and call respective __tc_xxx helper to do the actual work. No functional changes.
--- net/sched/sch_api.c | 191 ++++++++++++++++++++++++++++---------------- 1 file changed, 123 insertions(+), 68 deletions(-)
net/sched/sch_api.c +123 -68
@@ -1505,27 +1505,18 @@ const struct nla_policy rtm_tca_policy[TCA_MAX + 1] = {* Delete/get qdisc. */ -static int tc_get_qdisc (struct sk_buff *skb,struct nlmsghdr *n, -struct netlink_ext_ack *extack) +static int __tc_get_qdisc (struct sk_buff *skb,struct nlmsghdr *n, +struct netlink_ext_ack *extack, +struct net_device *dev, +struct nlattr *tca[TCA_MAX +1 ], +struct tcmsg *tcm) {struct net *net =sock_net (skb->sk); -struct tcmsg *tcm =nlmsg_data (n); -struct nlattr *tca[TCA_MAX +1 ]; -struct net_device *dev; -u32 clid;struct Qdisc *q =NULL ;struct Qdisc *p =NULL ; +u32 clid;int err; - err =nlmsg_parse_deprecated (n,sizeof (*tcm), tca,TCA_MAX , - rtm_tca_policy, extack); -if (err <0 ) -return err; - - dev =__dev_get_by_index (net, tcm->tcm_ifindex); -if (!dev) -return -ENODEV ; - clid = tcm->tcm_parent;if (clid) {if (clid !=TC_H_ROOT ) {
@@ -1582,6 +1573,27 @@ static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n,return 0 ; } +static int tc_get_qdisc (struct sk_buff *skb,struct nlmsghdr *n, +struct netlink_ext_ack *extack) +{ +struct net *net =sock_net (skb->sk); +struct tcmsg *tcm =nlmsg_data (n); +struct nlattr *tca[TCA_MAX +1 ]; +struct net_device *dev; +int err; + + err =nlmsg_parse_deprecated (n,sizeof (*tcm), tca,TCA_MAX , + rtm_tca_policy, extack); +if (err <0 ) +return err; + + dev =__dev_get_by_index (net, tcm->tcm_ifindex); +if (!dev) +return -ENODEV ; + +return __tc_get_qdisc (skb, n, extack, dev, tca, tcm); +} +static bool req_create_or_replace (struct nlmsghdr *n) { return (n->nlmsg_flags & NLM_F_CREATE &&
@@ -1601,35 +1613,19 @@ static bool req_change(struct nlmsghdr *n) !(n->nlmsg_flags & NLM_F_EXCL)); } -/* -* Create/change qdisc. -*/ -static int tc_modify_qdisc (struct sk_buff *skb,struct nlmsghdr *n, -struct netlink_ext_ack *extack) +static int __tc_modify_qdisc (struct sk_buff *skb,struct nlmsghdr *n, +struct netlink_ext_ack *extack, +struct net_device *dev, +struct nlattr *tca[TCA_MAX +1 ], +struct tcmsg *tcm, +bool *replay) { -struct net *net =sock_net (skb->sk); -struct tcmsg *tcm; -struct nlattr *tca[TCA_MAX +1 ]; -struct net_device *dev; +struct Qdisc *q =NULL ; +struct Qdisc *p =NULL ;u32 clid; -struct Qdisc *q, *p;int err; -replay: -/* Reinit, just in case something touches this. */ - err =nlmsg_parse_deprecated (n,sizeof (*tcm), tca,TCA_MAX , - rtm_tca_policy, extack); -if (err <0 ) -return err; - - tcm =nlmsg_data (n); clid = tcm->tcm_parent; - q = p =NULL ; - - dev =__dev_get_by_index (net, tcm->tcm_ifindex); -if (!dev) -return -ENODEV ; -if (clid) {if (clid !=TC_H_ROOT ) {
@@ -1755,7 +1751,7 @@ static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, } err =qdisc_change (q, tca, extack);if (err ==0 ) -qdisc_notify (net, skb, n, clid,NULL , q, extack); +qdisc_notify (sock_net (skb->sk), skb, n, clid,NULL , q, extack);return err; create_n_graft:
@@ -1788,8 +1784,10 @@ static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, tca, &err, extack); }if (q ==NULL ) { -if (err == -EAGAIN ) - goto replay; +if (err == -EAGAIN ) { + *replay = true; +return 0 ; + }return err; }
@@ -1804,6 +1802,39 @@ static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n,return 0 ; } +/* +* Create/change qdisc. +*/ +static int tc_modify_qdisc (struct sk_buff *skb,struct nlmsghdr *n, +struct netlink_ext_ack *extack) +{ +struct net *net =sock_net (skb->sk); +struct nlattr *tca[TCA_MAX +1 ]; +struct net_device *dev; +struct tcmsg *tcm; +bool replay; +int err; + +replay: +/* Reinit, just in case something touches this. */ + err =nlmsg_parse_deprecated (n,sizeof (*tcm), tca,TCA_MAX , + rtm_tca_policy, extack); +if (err <0 ) +return err; + + tcm =nlmsg_data (n); + dev =__dev_get_by_index (net, tcm->tcm_ifindex); +if (!dev) +return -ENODEV ; + + replay = false; + err =__tc_modify_qdisc (skb, n, extack, dev, tca, tcm, &replay); +if (replay) + goto replay; + +return err; +} + static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb, struct netlink_callback *cb, int *q_idx_p, int s_q_idx, bool recur,
@@ -2135,15 +2166,15 @@ static void tc_bind_tclass(struct Qdisc *q, u32 portid, u32 clid,#endif -static int tc_ctl_tclass (struct sk_buff *skb,struct nlmsghdr *n, -struct netlink_ext_ack *extack) +static int __tc_ctl_tclass (struct sk_buff *skb,struct nlmsghdr *n, +struct netlink_ext_ack *extack, +struct net_device *dev, +struct nlattr *tca[TCA_MAX +1 ], +struct tcmsg *tcm) {struct net *net =sock_net (skb->sk); -struct tcmsg *tcm =nlmsg_data (n); -struct nlattr *tca[TCA_MAX +1 ]; -struct net_device *dev; -struct Qdisc *q =NULL ;const struct Qdisc_class_ops *cops; +struct Qdisc *q =NULL ;unsigned long cl =0 ;unsigned long new_cl;u32 portid;
@@ -2151,15 +2182,6 @@ static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n,u32 qid;int err; - err =nlmsg_parse_deprecated (n,sizeof (*tcm), tca,TCA_MAX , - rtm_tca_policy, extack); -if (err <0 ) -return err; - - dev =__dev_get_by_index (net, tcm->tcm_ifindex); -if (!dev) -return -ENODEV ; -/* parent == TC_H_UNSPEC - unspecified parent. parent == TC_H_ROOT - class is root, which has no parent.
@@ -2268,6 +2290,27 @@ static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n,return err; } +static int tc_ctl_tclass (struct sk_buff *skb,struct nlmsghdr *n, +struct netlink_ext_ack *extack) +{ +struct net *net =sock_net (skb->sk); +struct tcmsg *tcm =nlmsg_data (n); +struct nlattr *tca[TCA_MAX +1 ]; +struct net_device *dev; +int err; + + err =nlmsg_parse_deprecated (n,sizeof (*tcm), tca,TCA_MAX , + rtm_tca_policy, extack); +if (err <0 ) +return err; + + dev =__dev_get_by_index (net, tcm->tcm_ifindex); +if (!dev) +return -ENODEV ; + +return __tc_ctl_tclass (skb, n, extack, dev, tca, tcm); +} +struct qdisc_dump_args {struct qdisc_walker w;struct sk_buff *skb;
@@ -2344,20 +2387,12 @@ static int tc_dump_tclass_root(struct Qdisc *root, struct sk_buff *skb,return 0 ; } -static int tc_dump_tclass (struct sk_buff *skb,struct netlink_callback *cb) +static int __tc_dump_tclass (struct sk_buff *skb,struct netlink_callback *cb, +struct tcmsg *tcm,struct net_device *dev) { -struct tcmsg *tcm =nlmsg_data (cb->nlh); -struct net *net =sock_net (skb->sk);struct netdev_queue *dev_queue; -struct net_device *dev;int t, s_t; -if (nlmsg_len (cb->nlh) <sizeof (*tcm)) -return 0 ; - dev =dev_get_by_index (net, tcm->tcm_ifindex); -if (!dev) -return 0 ; - s_t = cb->args[0 ]; t =0 ;
@@ -2374,10 +2409,30 @@ static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb) done: cb->args[0 ] = t; -dev_put (dev);return skb->len; } +static int tc_dump_tclass (struct sk_buff *skb,struct netlink_callback *cb) +{ +struct tcmsg *tcm =nlmsg_data (cb->nlh); +struct net *net =sock_net (skb->sk); +struct net_device *dev; +int err; + +if (nlmsg_len (cb->nlh) <sizeof (*tcm)) +return 0 ; + + dev =dev_get_by_index (net, tcm->tcm_ifindex); +if (!dev) +return 0 ; + + err =__tc_dump_tclass (skb, cb, tcm, dev); + +dev_put (dev); + +return err; +} +#ifdef CONFIG_PROC_FS static int psched_show (struct seq_file *seq,void *v) {
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 4/14 net: hold netdev instance lock during qdisc ndo_setup_tc
In reply to Stanislav FomichevQdisc operations that can lead to ndo_setup_tc might need to have an instance lock. Add netdev_lock_ops/netdev_unlock_ops invocations for all psched_rtnl_msg_handlers operations.
--- net/sched/sch_api.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)
net/sched/sch_api.c +24 -4
@@ -1279,9 +1279,11 @@ static struct Qdisc *qdisc_create(struct net_device *dev,* We replay the request because the device may * go away in the mean time. */ +netdev_unlock_ops (dev);rtnl_unlock ();request_module (NET_SCH_ALIAS_PREFIX "%s" , name);rtnl_lock (); +netdev_lock_ops (dev); ops =qdisc_lookup_ops (kind);if (ops !=NULL ) {/* We will try again qdisc_lookup_ops,
@@ -1591,7 +1593,11 @@ static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n,if (!dev)return -ENODEV ; -return __tc_get_qdisc (skb, n, extack, dev, tca, tcm); +netdev_lock_ops (dev); + err =__tc_get_qdisc (skb, n, extack, dev, tca, tcm); +netdev_unlock_ops (dev); + +return err; }static bool req_create_or_replace (struct nlmsghdr *n)
@@ -1828,7 +1834,9 @@ static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n,return -ENODEV ; replay = false; +netdev_lock_ops (dev); err =__tc_modify_qdisc (skb, n, extack, dev, tca, tcm, &replay); +netdev_unlock_ops (dev);if (replay) goto replay;
@@ -1919,17 +1927,23 @@ static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb) s_q_idx =0 ; q_idx =0 ; +netdev_lock_ops (dev);if (tc_dump_qdisc_root (rtnl_dereference (dev->qdisc), skb, cb, &q_idx, s_q_idx, - true, tca[TCA_DUMP_INVISIBLE ]) <0 ) + true, tca[TCA_DUMP_INVISIBLE ]) <0 ) { +netdev_unlock_ops (dev); goto done; + } dev_queue =dev_ingress_queue (dev);if (dev_queue &&tc_dump_qdisc_root (rtnl_dereference (dev_queue->qdisc_sleeping), skb, cb, &q_idx, s_q_idx, false, - tca[TCA_DUMP_INVISIBLE ]) <0 ) + tca[TCA_DUMP_INVISIBLE ]) <0 ) { +netdev_unlock_ops (dev); goto done; + } +netdev_unlock_ops (dev); cont: idx++;
@@ -2308,7 +2322,11 @@ static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n,if (!dev)return -ENODEV ; -return __tc_ctl_tclass (skb, n, extack, dev, tca, tcm); +netdev_lock_ops (dev); + err =__tc_ctl_tclass (skb, n, extack, dev, tca, tcm); +netdev_unlock_ops (dev); + +return err; }struct qdisc_dump_args {
@@ -2426,7 +2444,9 @@ static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)if (!dev)return 0 ; +netdev_lock_ops (dev); err =__tc_dump_tclass (skb, cb, tcm, dev); +netdev_unlock_ops (dev);dev_put (dev);
Signature · 1 line
2.48.1
Re: [PATCH net-next v10 04/14] net: hold netdev instance lock during qdisc ndo_setup_tc
Quoted 20 lines · Stanislav Fomichev
Qdisc operations that can lead to
ndo_setup_tcmight need to have an instance lock. Add netdev_lock_ops/netdev_unlock_ops invocations for allpsched_rtnl_msg_handlersoperations.Cc Jamal Hadi Salim <[email protected]>Cc Cong Wang <[email protected]>Cc Jiri Pirko <[email protected]>Cc Saeed Mahameed <[email protected]>Reviewed-by Jamal Hadi Salim <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- net/sched/sch_api.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)net/sched/sch_api.c +0
-0
net/sched/sch_api.c static struct Qdisc *qdisc_create(struct net_device *dev, · line 1285
Quoted 7 lines
net/sched/sch_api.c +1
-0@@ -1279,9 +1279,11 @@ static struct Qdisc *qdisc_create(struct net_device *dev,* We replay the request because the device may * go away in the mean time. */ +netdev_unlock_ops (dev);rtnl_unlock ();request_module (NET_SCH_ALIAS_PREFIX "%s" , name);rtnl_lock ();
Oops, dev might have disappeared.
As explained a few lines above in the comment :
/* We dropped the RTNL semaphore in order to
* perform the module load. So, even if we
* succeeded in loading the module we have to
* tell the caller to replay the request. We
* indicate this using -EAGAIN.
* We replay the request because the device may
* go away in the mean time.
*/
net/sched/sch_api.c static struct Qdisc *qdisc_create(struct net_device *dev, · line 1286
Quoted 1 line
net/sched/sch_api.c +1
-0+netdev_lock_ops (dev);
So this might trigger an UAF.
Quoted 78 lines
net/sched/sch_api.c +22
-5ops =qdisc_lookup_ops (kind);if (ops !=NULL ) {/* We will try again qdisc_lookup_ops, @@ -1591,7 +1593,11 @@ static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n,if (!dev)return -ENODEV ; -return __tc_get_qdisc (skb, n, extack, dev, tca, tcm); +netdev_lock_ops (dev); + err =__tc_get_qdisc (skb, n, extack, dev, tca, tcm); +netdev_unlock_ops (dev); + +return err; }static bool req_create_or_replace (struct nlmsghdr *n)@@ -1828,7 +1834,9 @@ static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n,return -ENODEV ; replay = false; +netdev_lock_ops (dev); err =__tc_modify_qdisc (skb, n, extack, dev, tca, tcm, &replay); +netdev_unlock_ops (dev);if (replay) goto replay;@@ -1919,17 +1927,23 @@ static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb) s_q_idx =0 ; q_idx =0 ; +netdev_lock_ops (dev);if (tc_dump_qdisc_root (rtnl_dereference (dev->qdisc), skb, cb, &q_idx, s_q_idx, - true, tca[TCA_DUMP_INVISIBLE ]) <0 ) + true, tca[TCA_DUMP_INVISIBLE ]) <0 ) { +netdev_unlock_ops (dev); goto done; + } dev_queue =dev_ingress_queue (dev);if (dev_queue &&tc_dump_qdisc_root (rtnl_dereference (dev_queue->qdisc_sleeping), skb, cb, &q_idx, s_q_idx, false, - tca[TCA_DUMP_INVISIBLE ]) <0 ) + tca[TCA_DUMP_INVISIBLE ]) <0 ) { +netdev_unlock_ops (dev); goto done; + } +netdev_unlock_ops (dev); cont: idx++;@@ -2308,7 +2322,11 @@ static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n,if (!dev)return -ENODEV ; -return __tc_ctl_tclass (skb, n, extack, dev, tca, tcm); +netdev_lock_ops (dev); + err =__tc_ctl_tclass (skb, n, extack, dev, tca, tcm); +netdev_unlock_ops (dev); + +return err; }struct qdisc_dump_args {@@ -2426,7 +2444,9 @@ static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)if (!dev)return 0 ; +netdev_lock_ops (dev); err =__tc_dump_tclass (skb, cb, tcm, dev); +netdev_unlock_ops (dev);dev_put (dev); --2.48.1
Re: [PATCH net-next v10 04/14] net: hold netdev instance lock during qdisc ndo_setup_tc
Quoted 44 lines · Eric Dumazet
Quoted 20 lines · Stanislav Fomichev
Qdisc operations that can lead to
ndo_setup_tcmight need to have an instance lock. Add netdev_lock_ops/netdev_unlock_ops invocations for allpsched_rtnl_msg_handlersoperations.Cc Jamal Hadi Salim <[email protected]>Cc Cong Wang <[email protected]>Cc Jiri Pirko <[email protected]>Cc Saeed Mahameed <[email protected]>Reviewed-by Jamal Hadi Salim <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- net/sched/sch_api.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)net/sched/sch_api.c +0
-0net/sched/sch_api.c static struct Qdisc *qdisc_create(struct net_device *dev, · line 1285
Quoted 7 lines
net/sched/sch_api.c +1
-0@@ -1279,9 +1279,11 @@ static struct Qdisc *qdisc_create(struct net_device *dev,* We replay the request because the device may * go away in the mean time. */ +netdev_unlock_ops (dev);rtnl_unlock ();request_module (NET_SCH_ALIAS_PREFIX "%s" , name);rtnl_lock ();Oops, dev might have disappeared.
As explained a few lines above in the comment :
/* We dropped the RTNL semaphore in order to
* perform the module load. So, even if we
* succeeded in loading the module we have to
* tell the caller to replay the request. We
* indicate this using-EAGAIN.
* We replay the request because the device may
* go away in the mean time.
*/
Quoted 3 lines
net/sched/sch_api.c static struct Qdisc *qdisc_create(struct net_device *dev, · line 1286
Quoted 1 line
net/sched/sch_api.c +1
-0+netdev_lock_ops (dev);So this might trigger an UAF.
Oh, good catch, let me try to add more logic here to be more careful on the replay path. We can make the caller not unlock the instance lock in this case I think..
Re: [PATCH net-next v10 04/14] net: hold netdev instance lock during qdisc ndo_setup_tc
In reply to Stanislav FomichevQuoted 3 lines · Stanislav Fomichev
Qdisc operations that can lead to
ndo_setup_tcmight need to have an instance lock. Add netdev_lock_ops/netdev_unlock_ops invocations for allpsched_rtnl_msg_handlersoperations.
Sorry for resurrecting this thread, but it seems like a good place to ask a related question.
If qdisc operations that lead to .ndo_setup_tc() need to hold an instance lock, shouldn't all such callers acquire it?
In my testing, I found out that e.g., when unloading a device, there's this call path which ends up calling .ndo_setup_tc() unlocked:
devlink_reload -...-> device_del -...-> unregister_netdev -...-> unregister_netdevice_many_notify -> dev_shutdown -> qdisc_put -> __qdisc_destroy -> mqprio_disable_offload -> .ndo_setup_tc
Many other qdiscs (other than mqprio) call .ndo_setup_tc() and would be in a similar situation.
Does it make sense to extend the netdev lock for dev_shutdown like in the patch below? After some basic testing, it seems safe but I haven't looked too deep into all possibilities.
Cosmin.
From e8b613dfd2b241a6c19bb89a829d598d6640b6f9 Mon Sep 17 00:00:00 2001
From: Cosmin Ratiu <[email protected]>
Date: Mon, 5 May 2025 15:34:53 +0300
Subject: [PATCH 01/20] net/sched: Lock netdevices during dev_shutdown
Various qdiscs can end up calling into .ndo_setup_tc() and as such, might need the netdev instance lock.
--- net/core/dev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/core/dev.c +0 -0
@@ -12020,9 +12020,9 @@ void unregister_netdevice_many_notify(struct
list_head *head,
struct sk_buff *skb = NULL;
/* Shutdown queueing discipline. */
+ netdev_lock_ops(dev);
dev_shutdown(dev);
dev_tcx_uninstall(dev);
- netdev_lock_ops(dev);
dev_xdp_uninstall(dev);
dev_memory_provider_uninstall(dev);
netdev_unlock_ops(dev);
@@ -12218,7 +12218,9 @@ int __dev_change_net_namespace(struct
net_device *dev, struct net *net,
synchronize_net();
/* Shutdown queueing discipline. */
+ netdev_lock_ops(dev);
dev_shutdown(dev);+ netdev_unlock_ops(dev);
/* Notify protocols, that we are about to destroy
* this device. They should clean all the things.
Signature · 1 line
2.45.0
Re: [PATCH net-next v10 04/14] net: hold netdev instance lock during qdisc ndo_setup_tc
Quoted 45 lines · Cosmin Ratiu
Quoted 3 lines · Stanislav Fomichev
Qdisc operations that can lead to
ndo_setup_tcmight need to have an instance lock. Add netdev_lock_ops/netdev_unlock_ops invocations for allpsched_rtnl_msg_handlersoperations.Sorry for resurrecting this thread, but it seems like a good place to ask a related question.
If qdisc operations that lead to .ndo_setup_tc() need to hold an instance lock, shouldn't all such callers acquire it?
In my testing, I found out that e.g., when unloading a device, there's this call path which ends up calling .ndo_setup_tc() unlocked:
devlink_reload-...->device_del-...->unregister_netdev-...->unregister_netdevice_many_notify->dev_shutdown->qdisc_put->__qdisc_destroy->mqprio_disable_offload-> .ndo_setup_tcMany other qdiscs (other than mqprio) call .ndo_setup_tc() and would be in a similar situation.
Does it make sense to extend the netdev lock for
dev_shutdownlike in the patch below? After some basic testing, it seems safe but I haven't looked too deep into all possibilities.Cosmin.
From
e8b613dfd2b241a6c19bb89a829d598d6640b6f9Mon Sep 17 00:00:00 2001
From: Cosmin Ratiu <[email protected]>
Date: Mon, 5 May 2025 15:34:53 +0300
Subject: [PATCH 01/20] net/sched: Lock netdevices duringdev_shutdownVarious qdiscs can end up calling into .ndo_setup_tc() and as such, might need the netdev instance lock.
Signed-off-by Cosmin Ratiu <[email protected]>--- net/core/dev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)net/core/dev.c +0
-0
net/core/dev.c void unregister_netdevice_many_notify(struct · line 12026
Quoted 7 lines
net/core/dev.c +1
-0@@ -12020,9 +12020,9 @@ void unregister_netdevice_many_notify(structlist_head *head, struct sk_buff *skb = NULL;/* Shutdown queueing discipline. */ +netdev_lock_ops (dev);dev_shutdown (dev);dev_tcx_uninstall (dev);
There is a synchronize_net hidden inside of dev_tcx_uninstall, so let's ops-lock only dev_shutdown here? Other than that, don't see anything wrong. Can you send this separately and target net tree?
Re: [PATCH net-next v10 04/14] net: hold netdev instance lock during qdisc ndo_setup_tc
Quoted 10 lines · Stanislav Fomichev
Quoted 9 lines · Cosmin Ratiu
net/core/dev.c +0
-0@@ -12020,9 +12020,9 @@ voidunregister_netdevice_many_notify(struct
list_head*head,
structsk_buff*skb = NULL;
Quoted 8 lines
Quoted 4 lines
/* Shutdown queueing discipline. */ +
netdev_lock_ops(dev);dev_shutdown(dev);dev_tcx_uninstall(dev);There is a
synchronize_nethidden inside ofdev_tcx_uninstall, so let's ops-lock onlydev_shutdownhere? Other than that, don't see anything wrong. Can you send this separately and target net tree?
Got it, so dev_tcx_uninstall can't be locked. I was trying to avoid repeated locking/unlocking for various steps, but I guess it's unavoidable. I think the ordering also can't change, qdiscs must be shutdown before dev_tcx_uninstall, no? To avoid injecting new packets.
I will send the patch targeted to net later today, yes.
Cosmin.
Jakub Kicinski
maintainerRe: [PATCH net-next v10 04/14] net: hold netdev instance lock during qdisc ndo_setup_tc
Quoted 11 lines · On Mon, 5 May 2025 15:12:39 +0000 Cosmin Ratiu wrote:
Quoted 10 lines · Stanislav Fomichev
Quoted 9 lines · Cosmin Ratiu
net/core/dev.c +0
-0@@ -12020,9 +12020,9 @@ voidunregister_netdevice_many_notify(struct
list_head*head,
structsk_buff*skb = NULL;
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
/* Shutdown queueing discipline. */ +
netdev_lock_ops(dev);dev_shutdown(dev);dev_tcx_uninstall(dev);There is a
synchronize_nethidden inside ofdev_tcx_uninstall, so let's ops-lock onlydev_shutdownhere? Other than that, don't see anything wrong. Can you send this separately and target net tree?
Avoiding synchronize_net() under the instance lock as an optimization? We're under rtnl_lock here, probably a premature optimization? But no strong preference..
BTW isn't the naming scheme now that dev_* takes the lock? So we should probably add netif_ versions or rename these existing calls?
Re: [PATCH net-next v10 04/14] net: hold netdev instance lock during qdisc ndo_setup_tc
Quoted 21 lines · Jakub Kicinski
Quoted 11 lines · On Mon, 5 May 2025 15:12:39 +0000 Cosmin Ratiu wrote:
Quoted 10 lines · Stanislav Fomichev
Quoted 9 lines · Cosmin Ratiu
net/core/dev.c +0
-0@@ -12020,9 +12020,9 @@ voidunregister_netdevice_many_notify(struct
list_head*head,
structsk_buff*skb = NULL;Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
/* Shutdown queueing discipline. */ +
netdev_lock_ops(dev);dev_shutdown(dev);dev_tcx_uninstall(dev);There is a
synchronize_nethidden inside ofdev_tcx_uninstall, so let's ops-lock onlydev_shutdownhere? Other than that, don't see anything wrong. Can you send this separately and target net tree?
Quoted 3 lines
Avoiding
synchronize_net()under the instance lock as an optimization? We're underrtnl_lockhere, probably a premature optimization? But no strong preference..
Good point, yes, let's not over complicate and just move the lock_ops up as Cosmin originally suggested.
Quoted 2 lines
BTW isn't the naming scheme now that dev_* takes the lock? So we should probably add
netif_versions or rename these existing calls?
Can I follow up on these separately? I was thinking about sending a bulk rename once we are closer to shipping 6.15. We have a lot of cases where dev_ is inconsistent.
Jakub Kicinski
maintainerRe: [PATCH net-next v10 04/14] net: hold netdev instance lock during qdisc ndo_setup_tc
Quoted 6 lines · On Mon, 5 May 2025 11:54:59 -0700 Stanislav Fomichev wrote:
Quoted 2 lines
BTW isn't the naming scheme now that dev_* takes the lock? So we should probably add
netif_versions or rename these existing calls?Can I follow up on these separately? I was thinking about sending a bulk rename once we are closer to shipping 6.15. We have a lot of cases where
dev_is inconsistent.
Ah, SG! There shouldn't be any driver callers of dev_shutdown so no risk of confusion.
Stanislav Fomichev
reviewerPATCH v10 5/14 net: hold netdev instance lock during queue operations
In reply to Stanislav FomichevFor the drivers that use queue management API, switch to the mode where core stack holds the netdev instance lock. This affects the following drivers:
- bnxt
- gve
- netdevsim
Originally I locked only start/stop, but switched to holding the lock over all iterations to make them look atomic to the device (feels like it should be easier to reason about).
--- drivers/net/ethernet/broadcom/bnxt/bnxt.c | 16 ++++++------- drivers/net/ethernet/google/gve/gve_main.c | 12 ++++++---- drivers/net/ethernet/google/gve/gve_utils.c | 6 ++--- drivers/net/netdevsim/netdev.c | 25 +++++++++++++-------- include/linux/netdevice.h | 2 +- net/core/netdev_rx_queue.c | 5 +++++ 6 files changed, 41 insertions(+), 25 deletions(-)
drivers/net/ethernet/broadcom/bnxt/bnxt.c +8 -8
@@ -11508,7 +11508,7 @@ static int bnxt_request_irq(struct bnxt *bp)if (rc)break ; -netif_napi_set_irq (&bp->bnapi[i]->napi, irq->vector); +netif_napi_set_irq_locked (&bp->bnapi[i]->napi, irq->vector); irq->requested =1 ;if (zalloc_cpumask_var (&irq->cpu_mask,GFP_KERNEL )) {
@@ -11557,9 +11557,9 @@ static void bnxt_del_napi(struct bnxt *bp)for (i =0 ; i < bp->cp_nr_rings; i++) {struct bnxt_napi *bnapi = bp->bnapi[i]; -__netif_napi_del (&bnapi->napi); +__netif_napi_del_locked (&bnapi->napi); } -/* We called __netif_napi_del(), we need +/* We called __netif_napi_del_locked(), we need * to respect an RCU grace period before freeing napi structures. */ synchronize_net ();
@@ -11578,12 +11578,12 @@ static void bnxt_init_napi(struct bnxt *bp) cp_nr_rings--;for (i =0 ; i < cp_nr_rings; i++) { bnapi = bp->bnapi[i]; -netif_napi_add_config (bp->dev, &bnapi->napi, poll_fn, - bnapi->index); +netif_napi_add_config_locked (bp->dev, &bnapi->napi, poll_fn, + bnapi->index); }if (BNXT_CHIP_TYPE_NITRO_A0 (bp)) { bnapi = bp->bnapi[cp_nr_rings]; -netif_napi_add (bp->dev, &bnapi->napi, bnxt_poll_nitroa0); +netif_napi_add_locked (bp->dev, &bnapi->napi, bnxt_poll_nitroa0); } }
@@ -11604,7 +11604,7 @@ static void bnxt_disable_napi(struct bnxt *bp) cpr->sw_stats->tx.tx_resets++;if (bnapi->in_reset) cpr->sw_stats->rx.rx_resets++; -napi_disable (&bnapi->napi); +napi_disable_locked (&bnapi->napi); } }
@@ -11626,7 +11626,7 @@ static void bnxt_enable_napi(struct bnxt *bp)INIT_WORK (&cpr->dim.work, bnxt_dim_work); cpr->dim.mode =DIM_CQ_PERIOD_MODE_START_FROM_EQE ; } -napi_enable (&bnapi->napi); +napi_enable_locked (&bnapi->napi); } }
drivers/net/ethernet/google/gve/gve_main.c +8 -4
@@ -1968,7 +1968,7 @@ static void gve_turndown(struct gve_priv *priv)netif_queue_set_napi (priv->dev, idx,NETDEV_QUEUE_TYPE_TX ,NULL ); -napi_disable (&block->napi); +napi_disable_locked (&block->napi); }for (idx =0 ; idx < priv->rx_cfg.num_queues; idx++) {int ntfy_idx =gve_rx_idx_to_ntfy (priv, idx);
@@ -1979,7 +1979,7 @@ static void gve_turndown(struct gve_priv *priv)netif_queue_set_napi (priv->dev, idx,NETDEV_QUEUE_TYPE_RX ,NULL ); -napi_disable (&block->napi); +napi_disable_locked (&block->napi); }/* Stop tx queues */
@@ -2009,7 +2009,7 @@ static void gve_turnup(struct gve_priv *priv)if (!gve_tx_was_added_to_block (priv, idx))continue ; -napi_enable (&block->napi); +napi_enable_locked (&block->napi);if (idx < priv->tx_cfg.num_queues) netif_queue_set_napi(priv->dev, idx,
@@ -2037,7 +2037,7 @@ static void gve_turnup(struct gve_priv *priv)if (!gve_rx_was_added_to_block (priv, idx))continue ; -napi_enable (&block->napi); +napi_enable_locked (&block->napi);netif_queue_set_napi (priv->dev, idx,NETDEV_QUEUE_TYPE_RX , &block->napi);
@@ -2887,6 +2887,7 @@ static int gve_suspend(struct pci_dev *pdev, pm_message_t state) priv->suspend_cnt++;rtnl_lock (); +netdev_lock (netdev);if (was_up &&gve_close (priv->dev)) {/* If the dev was up, attempt to close, if close fails, reset */ gve_reset_and_teardown (priv, was_up);
@@ -2895,6 +2896,7 @@ static int gve_suspend(struct pci_dev *pdev, pm_message_t state)gve_teardown_priv_resources (priv); } priv->up_before_suspend = was_up; +netdev_unlock (netdev);rtnl_unlock ();return 0 ; }
@@ -2907,7 +2909,9 @@ static int gve_resume(struct pci_dev *pdev) priv->resume_cnt++;rtnl_lock (); +netdev_lock (netdev); err =gve_reset_recovery (priv, priv->up_before_suspend); +netdev_unlock (netdev);rtnl_unlock ();return err; }
drivers/net/ethernet/google/gve/gve_utils.c +3 -3
@@ -110,13 +110,13 @@ void gve_add_napi(struct gve_priv *priv, int ntfy_idx, {struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx]; -netif_napi_add (priv->dev, &block->napi, gve_poll); -netif_napi_set_irq (&block->napi, block->irq); +netif_napi_add_locked (priv->dev, &block->napi, gve_poll); +netif_napi_set_irq_locked (&block->napi, block->irq); }void gve_remove_napi (struct gve_priv *priv,int ntfy_idx) {struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx]; -netif_napi_del (&block->napi); +netif_napi_del_locked (&block->napi); }
drivers/net/netdevsim/netdev.c +16 -9
@@ -683,7 +683,8 @@ nsim_queue_mem_alloc(struct net_device *dev, void *per_queue_mem, int idx) goto err_free;if (!ns->rq_reset_mode) -netif_napi_add_config (dev, &qmem->rq->napi, nsim_poll, idx); +netif_napi_add_config_locked (dev, &qmem->rq->napi, nsim_poll, + idx);return 0 ;
@@ -700,7 +701,7 @@ static void nsim_queue_mem_free(struct net_device *dev, void *per_queue_mem)page_pool_destroy (qmem->pp);if (qmem->rq) {if (!ns->rq_reset_mode) -netif_napi_del (&qmem->rq->napi); +netif_napi_del_locked (&qmem->rq->napi);page_pool_destroy (qmem->rq->page_pool);nsim_queue_free (qmem->rq); }
@@ -712,9 +713,11 @@ nsim_queue_start(struct net_device *dev, void *per_queue_mem, int idx)struct nsim_queue_mem *qmem = per_queue_mem;struct netdevsim *ns =netdev_priv (dev); +netdev_assert_locked (dev); +if (ns->rq_reset_mode ==1 ) { ns->rq[idx]->page_pool = qmem->pp; -napi_enable (&ns->rq[idx]->napi); +napi_enable_locked (&ns->rq[idx]->napi);return 0 ; }
@@ -722,15 +725,17 @@ nsim_queue_start(struct net_device *dev, void *per_queue_mem, int idx)* here we want to test various call orders. */ if (ns->rq_reset_mode ==2 ) { -netif_napi_del (&ns->rq[idx]->napi); -netif_napi_add_config (dev, &qmem->rq->napi, nsim_poll, idx); +netif_napi_del_locked (&ns->rq[idx]->napi); +netif_napi_add_config_locked (dev, &qmem->rq->napi, nsim_poll, + idx); }else if (ns->rq_reset_mode ==3 ) { -netif_napi_add_config (dev, &qmem->rq->napi, nsim_poll, idx); -netif_napi_del (&ns->rq[idx]->napi); +netif_napi_add_config_locked (dev, &qmem->rq->napi, nsim_poll, + idx); +netif_napi_del_locked (&ns->rq[idx]->napi); } ns->rq[idx] = qmem->rq; -napi_enable (&ns->rq[idx]->napi); +napi_enable_locked (&ns->rq[idx]->napi);return 0 ; }
@@ -740,7 +745,9 @@ static int nsim_queue_stop(struct net_device *dev, void *per_queue_mem, int idx)struct nsim_queue_mem *qmem = per_queue_mem;struct netdevsim *ns =netdev_priv (dev); -napi_disable (&ns->rq[idx]->napi); +netdev_assert_locked (dev); + +napi_disable_locked (&ns->rq[idx]->napi);if (ns->rq_reset_mode ==1 ) { qmem->pp = ns->rq[idx]->page_pool;
include/linux/netdevice.h +1 -1
@@ -2755,7 +2755,7 @@ static inline void netdev_assert_locked_or_invisible(struct net_device *dev)static inline bool netdev_need_ops_lock (struct net_device *dev) { -bool ret = false; +bool ret = !!dev->queue_mgmt_ops;#if IS_ENABLED (CONFIG_NET_SHAPER ) ret |= !!dev->netdev_ops->net_shaper_ops;
net/core/netdev_rx_queue.c +5 -0
@@ -30,6 +30,8 @@ int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq_idx) goto err_free_new_mem; } +netdev_lock (dev); + err = qops->ndo_queue_mem_alloc (dev, new_mem, rxq_idx);if (err) goto err_free_old_mem;
@@ -52,6 +54,8 @@ int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq_idx) qops->ndo_queue_mem_free (dev, old_mem); +netdev_unlock (dev); +kvfree (old_mem);kvfree (new_mem);
@@ -76,6 +80,7 @@ int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq_idx) qops->ndo_queue_mem_free (dev, new_mem); err_free_old_mem: +netdev_unlock (dev);kvfree (old_mem); err_free_new_mem:
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 6/14 net: hold netdev instance lock during rtnetlink operations
In reply to Stanislav FomichevTo preserve the atomicity, hold the lock while applying multiple attributes. The major issue with a full conversion to the instance lock are software nesting devices (bonding/team/vrf/etc). Those devices call into the core stack for their lower (potentially real hw) devices. To avoid explicitly wrapping all those places into instance lock/unlock, introduce new API boundaries:
- (some) existing dev_xxx calls are now considered "external" (to drivers) APIs and they transparently grab the instance lock if needed (dev_api.c)
- new netif_xxx calls are internal core stack API (naming is sketchy, I've tried netdev_xxx_locked per Jakub's suggestion, but it feels a bit verbose; but happy to get back to this naming scheme if this is the preference)
This avoids touching most of the existing ioctl/sysfs/drivers paths.
Note the special handling of ndo_xxx_slave operations: I exploit the fact that none of the drivers that call these functions need/use instance lock. At the same time, they use dev_xxx APIs, so the lower device has to be unlocked.
Changes in unregister_netdevice_many_notify (to protect dev->state with instance lock) trigger lockdep - the loop over close_list (mostly from cleanup_net) introduces spurious ordering issues. netdev_lock_cmp_fn has a justification on why it's ok to suppress for now.
--- include/linux/netdevice.h | 40 ++++++- net/core/Makefile | 2 +- net/core/dev.c | 154 +++++--------------------- net/core/dev.h | 13 ++- net/core/dev_api.c | 224 ++++++++++++++++++++++++++++++++++++++ net/core/rtnetlink.c | 46 +++++--- 6 files changed, 329 insertions(+), 150 deletions(-) create mode 100644 net/core/dev_api.c
include/linux/netdevice.h +34 -6
@@ -2620,16 +2620,35 @@ static inline void netdev_for_each_tx_queue(struct net_device *dev,f (dev, &dev->_tx[i], arg); } +static inline int netdev_lock_cmp_fn (const struct lockdep_map *a, +const struct lockdep_map *b) +{ +/* Only lower devices currently grab the instance lock, so no +* real ordering issues can occur. In the near future, only +* hardware devices will grab instance lock which also does not +* involve any ordering. Suppress lockdep ordering warnings +* until (if) we start grabbing instance lock on pure SW +* devices (bond/team/veth/etc). +*/ +if (a == b) +return 0 ; +return -1 ; +} +#define netdev_lockdep_set_classes (dev) \ { \ static struct lock_class_key qdisc_tx_busylock_key; \ static struct lock_class_key qdisc_xmit_lock_key; \ static struct lock_class_key dev_addr_list_lock_key; \ + static struct lock_class_key dev_instance_lock_key; \ unsigned int i; \ \ (dev)->qdisc_tx_busylock = &qdisc_tx_busylock_key; \ lockdep_set_class(&(dev)->addr_list_lock, \ &dev_addr_list_lock_key); \ + lockdep_set_class(&(dev)->lock, \ + &dev_instance_lock_key); \ + lock_set_cmp_fn(&dev->lock, netdev_lock_cmp_fn, NULL); \ for (i = 0; i < (dev)->num_tx_queues; i++) \ lockdep_set_class(&(dev)->_tx[i]._xmit_lock, \ &qdisc_xmit_lock_key); \
@@ -2776,6 +2795,12 @@ static inline void netdev_unlock_ops(struct net_device *dev)netdev_unlock (dev); } +static inline void netdev_ops_assert_locked (struct net_device *dev) +{ +if (netdev_need_ops_lock (dev)) +lockdep_assert_held (&dev->lock); +} +void netif_napi_set_irq_locked (struct napi_struct *napi,int irq);static inline void netif_napi_set_irq (struct napi_struct *napi,int irq)
@@ -3350,7 +3375,9 @@ struct net_device *dev_get_by_name_rcu(struct net *net, const char *name);struct net_device *__dev_get_by_name (struct net *net,const char *name);bool netdev_name_in_use (struct net *net,const char *name);int dev_alloc_name (struct net_device *dev,const char *name); +int netif_open (struct net_device *dev,struct netlink_ext_ack *extack);int dev_open (struct net_device *dev,struct netlink_ext_ack *extack); +void netif_close (struct net_device *dev);void dev_close (struct net_device *dev);void dev_close_many (struct list_head *head,bool unlink); int dev_setup_tc(struct net_device *dev, enum tc_setup_type type,
@@ -4211,25 +4238,26 @@ int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *userdata);unsigned int dev_get_flags (const struct net_device *);int __dev_change_flags (struct net_device *dev,unsigned int flags,struct netlink_ext_ack *extack); +int netif_change_flags (struct net_device *dev,unsigned int flags, +struct netlink_ext_ack *extack);int dev_change_flags (struct net_device *dev,unsigned int flags,struct netlink_ext_ack *extack); +int netif_set_alias (struct net_device *dev,const char *alias,size_t len);int dev_set_alias (struct net_device *,const char *,size_t );int dev_get_alias (const struct net_device *,char *,size_t ); -int __dev_change_net_namespace (struct net_device *dev,struct net *net, +int netif_change_net_namespace (struct net_device *dev,struct net *net,const char *pat,int new_ifindex,struct netlink_ext_ack *extack); -static inline int dev_change_net_namespace (struct net_device *dev,struct net *net, -const char *pat) -{ -return __dev_change_net_namespace (dev, net, pat,0 ,NULL ); -} +const char *pat);int __dev_set_mtu (struct net_device *,int );int dev_set_mtu (struct net_device *,int );int dev_pre_changeaddr_notify (struct net_device *dev,const char *addr,struct netlink_ext_ack *extack);int dev_set_mac_address (struct net_device *dev,struct sockaddr *sa,struct netlink_ext_ack *extack); +int netif_set_mac_address_user (struct net_device *dev,struct sockaddr *sa, +struct netlink_ext_ack *extack);int dev_set_mac_address_user (struct net_device *dev,struct sockaddr *sa,struct netlink_ext_ack *extack);int dev_get_mac_address (struct sockaddr *sa,struct net *net,char *dev_name);
net/core/Makefile +1 -1
@@ -9,7 +9,7 @@ obj-y := sock.o request_sock.o skbuff.o datagram.o stream.o scm.o \ obj-$(CONFIG_SYSCTL) += sysctl_net_core.o -obj-y += dev.o dev_addr_lists.o dst.o netevent.o \ +obj-y += dev.o dev_api.o dev_addr_lists.o dst.o netevent.o \ neighbour.o rtnetlink.o utils.o link_watch.o filter.o \ sock_diag.o dev_ioctl.o tso.o sock_reuseport.o \ fib_notifier.o xdp.o flow_offload.o gro.o \
net/core/dev.c +29 -125
330 lines not on this page · raw message
net/core/dev.h +9 -4
@@ -85,6 +85,7 @@ struct netdev_name_node { };int netdev_get_name (struct net *net,char *name,int ifindex); +int netif_change_name (struct net_device *dev,const char *newname);int dev_change_name (struct net_device *dev,const char *newname);#define netdev_for_each_altname (dev, namenode) \
@@ -98,24 +99,28 @@ int netdev_name_node_alt_destroy(struct net_device *dev, const char *name);int dev_validate_mtu (struct net_device *dev,int mtu,struct netlink_ext_ack *extack); -int dev_set_mtu_ext (struct net_device *dev,int mtu, -struct netlink_ext_ack *extack); +int netif_set_mtu_ext (struct net_device *dev,int new_mtu, +struct netlink_ext_ack *extack);int dev_get_phys_port_id (struct net_device *dev,struct netdev_phys_item_id *ppid);int dev_get_phys_port_name (struct net_device *dev,char *name,size_t len); +int netif_change_proto_down (struct net_device *dev,bool proto_down);int dev_change_proto_down (struct net_device *dev,bool proto_down); -void dev_change_proto_down_reason (struct net_device *dev,unsigned long mask, -u32 value); +void netdev_change_proto_down_reason_locked (struct net_device *dev, +unsigned long mask,u32 value);typedef int (*bpf_op_t )(struct net_device *dev,struct netdev_bpf *bpf);int dev_change_xdp_fd (struct net_device *dev,struct netlink_ext_ack *extack,int fd,int expected_fd,u32 flags); +int netif_change_tx_queue_len (struct net_device *dev,unsigned long new_len);int dev_change_tx_queue_len (struct net_device *dev,unsigned long new_len); +void netif_set_group (struct net_device *dev,int new_group);void dev_set_group (struct net_device *dev,int new_group); +int netif_change_carrier (struct net_device *dev,bool new_carrier);int dev_change_carrier (struct net_device *dev,bool new_carrier);void __dev_set_rx_mode (struct net_device *dev);
net/core/dev_api.c new file +224 -0
224 lines not on this page · raw message
net/core/rtnetlink.c +32 -14
158 lines not on this page · raw message
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 7/14 net: hold netdev instance lock during ioctl operations
In reply to Stanislav FomichevConvert all ndo_eth_ioctl invocations to dev_eth_ioctl which does the locking. Reflow some of the dev_siocxxx to drop else clause.
--- drivers/net/bonding/bond_main.c | 9 ++--- include/linux/netdevice.h | 3 ++ net/8021q/vlan_dev.c | 4 +- net/core/dev.c | 4 +- net/core/dev_api.c | 30 +++++++++++++++ net/core/dev_ioctl.c | 67 ++++++++++++++++++++------------- 6 files changed, 80 insertions(+), 37 deletions(-)
drivers/net/bonding/bond_main.c +4 -5
@@ -855,7 +855,6 @@ static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting) {const struct net_device_ops *slave_ops = slave_dev->netdev_ops; -int (*ioctl)(struct net_device *,struct ifreq *,int );struct ifreq ifr;struct mii_ioctl_data *mii;
@@ -871,8 +870,7 @@ static int bond_check_dev_link(struct bonding *bond, BMSR_LSTATUS :0 ;/* Ethtool can't be used, fallback to MII ioctls. */ - ioctl = slave_ops->ndo_eth_ioctl; -if (ioctl) { +if (slave_ops->ndo_eth_ioctl) {/* TODO: set pointer to correct ioctl on a per team member * bases to make this more efficient. that is, once * we determine the correct ioctl, we will always
@@ -888,9 +886,10 @@ static int bond_check_dev_link(struct bonding *bond,/* Yes, the mii is overlaid on the ifreq.ifr_ifru */ strscpy_pad (ifr.ifr_name, slave_dev->name,IFNAMSIZ ); mii =if_mii (&ifr); -if (ioctl (slave_dev, &ifr,SIOCGMIIPHY ) ==0 ) { + +if (dev_eth_ioctl (slave_dev, &ifr,SIOCGMIIPHY ) ==0 ) { mii->reg_num =MII_BMSR ; -if (ioctl (slave_dev, &ifr,SIOCGMIIREG ) ==0 ) +if (dev_eth_ioctl (slave_dev, &ifr,SIOCGMIIREG ) ==0 )return mii->val_out &BMSR_LSTATUS ; } }
include/linux/netdevice.h +3 -0
@@ -4229,6 +4229,8 @@ int put_user_ifreq(struct ifreq *ifr, void __user *arg);int dev_ioctl (struct net *net,unsigned int cmd,struct ifreq *ifr,void __user *data,bool *need_copyout);int dev_ifconf (struct net *net,struct ifconf __user *ifc); +int dev_eth_ioctl (struct net_device *dev, +struct ifreq *ifr,unsigned int cmd);int generic_hwtstamp_get_lower (struct net_device *dev,struct kernel_hwtstamp_config *kernel_cfg); int generic_hwtstamp_set_lower(struct net_device *dev,
@@ -4251,6 +4253,7 @@ int netif_change_net_namespace(struct net_device *dev, struct net *net,int dev_change_net_namespace (struct net_device *dev,struct net *net,const char *pat);int __dev_set_mtu (struct net_device *,int ); +int netif_set_mtu (struct net_device *dev,int new_mtu);int dev_set_mtu (struct net_device *,int );int dev_pre_changeaddr_notify (struct net_device *dev,const char *addr,struct netlink_ext_ack *extack);
net/8021q/vlan_dev.c +1 -3
@@ -377,7 +377,6 @@ static int vlan_hwtstamp_set(struct net_device *dev,static int vlan_dev_ioctl (struct net_device *dev,struct ifreq *ifr,int cmd) {struct net_device *real_dev =vlan_dev_priv (dev)->real_dev; -const struct net_device_ops *ops = real_dev->netdev_ops;struct ifreq ifrr;int err = -EOPNOTSUPP ;
@@ -388,8 +387,7 @@ static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)case SIOCGMIIPHY :case SIOCGMIIREG :case SIOCSMIIREG : -if (netif_device_present (real_dev) && ops->ndo_eth_ioctl) - err = ops->ndo_eth_ioctl (real_dev, &ifrr, cmd); + err =dev_eth_ioctl (real_dev, &ifrr, cmd);break ; }
net/core/dev.c +2 -2
@@ -9522,7 +9522,7 @@ int netif_set_mtu_ext(struct net_device *dev, int new_mtu,return err; } -int dev_set_mtu (struct net_device *dev,int new_mtu) +int netif_set_mtu (struct net_device *dev,int new_mtu) {struct netlink_ext_ack extack;int err;
@@ -9533,7 +9533,7 @@ int dev_set_mtu(struct net_device *dev, int new_mtu)net_err_ratelimited ("%s: %s\n" , dev->name, extack._msg);return err; } -EXPORT_SYMBOL (dev_set_mtu); +EXPORT_SYMBOL (netif_set_mtu);int netif_change_tx_queue_len (struct net_device *dev,unsigned long new_len) {
net/core/dev_api.c +30 -0
@@ -222,3 +222,33 @@ void dev_close(struct net_device *dev)netdev_unlock_ops (dev); }EXPORT_SYMBOL (dev_close); + +int dev_eth_ioctl (struct net_device *dev, +struct ifreq *ifr,unsigned int cmd) +{ +const struct net_device_ops *ops = dev->netdev_ops; +int ret = -ENODEV ; + +if (!ops->ndo_eth_ioctl) +return -EOPNOTSUPP ; + +netdev_lock_ops (dev); +if (netif_device_present (dev)) + ret = ops->ndo_eth_ioctl (dev, ifr, cmd); +netdev_unlock_ops (dev); + +return ret; +} +EXPORT_SYMBOL (dev_eth_ioctl); + +int dev_set_mtu (struct net_device *dev,int new_mtu) +{ +int ret; + +netdev_lock_ops (dev); + ret =netif_set_mtu (dev, new_mtu); +netdev_unlock_ops (dev); + +return ret; +} +EXPORT_SYMBOL (dev_set_mtu);
net/core/dev_ioctl.c +40 -27
136 lines not on this page · raw message
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 8/14 net: hold netdev instance lock during sysfs operations
In reply to Stanislav FomichevMost of them are already covered by the converted dev_xxx APIs. Add the locking wrappers for the remaining ones.
--- drivers/net/bonding/bond_main.c | 7 +++- include/linux/netdevice.h | 4 ++ net/core/dev.c | 58 ++++++----------------------- net/core/dev_api.c | 65 +++++++++++++++++++++++++++++++++ net/core/net-sysfs.c | 2 + 5 files changed, 88 insertions(+), 48 deletions(-)
drivers/net/bonding/bond_main.c +5 -2
@@ -2644,10 +2644,13 @@ static int __bond_release_one(struct net_device *bond_dev,dev_set_mac_address (slave_dev, (struct sockaddr *)&ss,NULL ); } -if (unregister) +if (unregister) { +netdev_lock_ops (slave_dev);__dev_set_mtu (slave_dev, slave->original_mtu); -else +netdev_unlock_ops (slave_dev); + }else {dev_set_mtu (slave_dev, slave->original_mtu); + }if (!netif_is_bond_master (slave_dev)) slave_dev->priv_flags &= ~IFF_BONDING ;
include/linux/netdevice.h +4 -0
@@ -3382,6 +3382,7 @@ void dev_close(struct net_device *dev);void dev_close_many (struct list_head *head,bool unlink);int dev_setup_tc (struct net_device *dev,enum tc_setup_type type,void *type_data); +void netif_disable_lro (struct net_device *dev);void dev_disable_lro (struct net_device *dev);int dev_loopback_xmit (struct net *net,struct sock *sk,struct sk_buff *newskb); u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
@@ -4257,6 +4258,8 @@ int netif_set_mtu(struct net_device *dev, int new_mtu);int dev_set_mtu (struct net_device *,int );int dev_pre_changeaddr_notify (struct net_device *dev,const char *addr,struct netlink_ext_ack *extack); +int netif_set_mac_address (struct net_device *dev,struct sockaddr *sa, +struct netlink_ext_ack *extack);int dev_set_mac_address (struct net_device *dev,struct sockaddr *sa,struct netlink_ext_ack *extack); int netif_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
@@ -5016,6 +5019,7 @@ static inline void __dev_mc_unsync(struct net_device *dev,/* Functions used for secondary unicast and multicast support */ void dev_set_rx_mode (struct net_device *dev);int dev_set_promiscuity (struct net_device *dev,int inc); +int netif_set_allmulti (struct net_device *dev,int inc,bool notify);int dev_set_allmulti (struct net_device *dev,int inc);void netdev_state_change (struct net_device *dev);void __netdev_notify_peers (struct net_device *dev);
net/core/dev.c +12 -46
113 lines not on this page · raw message
net/core/dev_api.c +65 -0
68 lines not on this page · raw message
net/core/net-sysfs.c +2 -0
@@ -1483,8 +1483,10 @@ static ssize_t tx_maxrate_store(struct kobject *kobj, struct attribute *attr,return err; err = -EOPNOTSUPP ; +netdev_lock_ops (dev);if (dev->netdev_ops->ndo_set_tx_maxrate) err = dev->netdev_ops->ndo_set_tx_maxrate (dev, index, rate); +netdev_unlock_ops (dev);if (!err) { queue->tx_maxrate = rate;
Signature · 1 line
2.48.1
Re: [PATCH net-next v10 08/14] net: hold netdev instance lock during sysfs operations
Quoted 9 lines · Stanislav Fomichev
net/core/dev_api.c +5
-0+ +/** +* dev_disable_lro() - disable Large Receive Offload on a device +* @dev: device +*
net/core/dev_api.c int dev_set_mtu(struct net_device *dev, int new_mtu) · line 269
Quoted 10 lines
net/core/dev_api.c +10
-0+* Disable Large Receive Offload (LRO) on a net device. Must be +* called under RTNL. This is needed if received packets may be +* forwarded to another interface. +*/ +void dev_disable_lro (struct net_device *dev) +{ +netdev_lock_ops (dev); +netif_disable_lro (dev); +netdev_unlock_ops (dev); +}
It seems this part plus the following part from patch 6 of this series result in a recursive deadlock when inet forwarding is not enabled:
Quoted 11 lines
net/core/dev_api.c +2
-0@@ -3013,6 +3021,8 @@ static int do_setlink(const struct sk_buff *skb, struct net_device *dev, char ifname[IFNAMSIZ];int err; +netdev_lock_ops (dev); + err =validate_linkmsg (dev, tb, extack);if (err <0 )goto errout;
Call Trace: dump_stack_lvl+0x62/0x90 print_deadlock_bug+0x274/0x3b0 __lock_acquire+0x1229/0x2470 lock_acquire+0xb7/0x2b0 __mutex_lock+0xa6/0xd20 dev_disable_lro+0x20/0x80 inetdev_init+0x12f/0x1f0 inetdev_event+0x48b/0x870 notifier_call_chain+0x38/0xf0 netif_change_net_namespace+0x72e/0x9f0 do_setlink.isra.0+0xd5/0x1220 rtnl_newlink+0x7ea/0xb50 rtnetlink_rcv_msg+0x459/0x5e0 netlink_rcv_skb+0x54/0x100 netlink_unicast+0x193/0x270 netlink_sendmsg+0x204/0x450
inetdev_init conditionally disables LRO if forwarding is on:
if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
dev_disable_lro(dev);What to do in this case (besides the silly workaround to disable forwarding)?
Cosmin.
Re: [PATCH net-next v10 08/14] net: hold netdev instance lock during sysfs operations
Quoted 3 lines · Cosmin Ratiu
It seems this part plus the following part from patch 6 of this series
result in a recursive deadlock when inet forwarding is not enabled:
Correction: This should read "when inet forwarding is enabled".
Re: [PATCH net-next v10 08/14] net: hold netdev instance lock during sysfs operations
In reply to Cosmin RatiuQuoted 54 lines · Cosmin Ratiu
Quoted 9 lines · Stanislav Fomichev
net/core/dev_api.c +5
-0+ +/** +* dev_disable_lro() - disable Large Receive Offload on a device +* @dev: device +* net/core/dev_api.c
Quoted 10 lines
net/core/dev_api.c +10
-0+* Disable Large Receive Offload (LRO) on a net device. Must be +* called under RTNL. This is needed if received packets may be +* forwarded to another interface. +*/ +void dev_disable_lro (struct net_device *dev) +{ +netdev_lock_ops (dev); +netif_disable_lro (dev); +netdev_unlock_ops (dev); +}It seems this part plus the following part from patch 6 of this series result in a recursive deadlock when inet forwarding is not enabled:
Quoted 11 lines
net/core/dev_api.c +2
-0@@ -3013,6 +3021,8 @@ static int do_setlink(const struct sk_buff *skb, struct net_device *dev, char ifname[IFNAMSIZ];int err; +netdev_lock_ops (dev); + err =validate_linkmsg (dev, tb, extack);if (err <0 )goto errout;
Call Trace: dump_stack_lvl+0x62/0x90 print_deadlock_bug+0x274/0x3b0 __lock_acquire+0x1229/0x2470 lock_acquire+0xb7/0x2b0 __mutex_lock+0xa6/0xd20 dev_disable_lro+0x20/0x80 inetdev_init+0x12f/0x1f0 inetdev_event+0x48b/0x870 notifier_call_chain+0x38/0xf0 netif_change_net_namespace+0x72e/0x9f0 do_setlink.isra.0+0xd5/0x1220 rtnl_newlink+0x7ea/0xb50 rtnetlink_rcv_msg+0x459/0x5e0 netlink_rcv_skb+0x54/0x100 netlink_unicast+0x193/0x270 netlink_sendmsg+0x204/0x450
Quoted 6 lines
inetdev_initconditionally disables LRO if forwarding is on:if (IPV4_DEVCONF(in_dev->cnf, FORWARDING)) dev_disable_lro(dev);What to do in this case (besides the silly workaround to disable forwarding)?
I think something like the patch below should fix it? inetdev_init is called for blackhole (sw device, we don't care about ops lock) and from REGISTER/UNREGISTER notifiers. We hold the lock during REGISTER, and will soon hold the lock during UNREGISTER:
https://lore.kernel.org/netdev/[email protected]/
(might also need to EXPORT_SYM netif_disable_lro)
net/ipv4/devinet.c +1 -1
@@ -281,7 +281,7 @@ static struct in_device *inetdev_init(struct net_device *dev)if (!in_dev->arp_parms) goto out_kfree;if (IPV4_DEVCONF (in_dev->cnf,FORWARDING )) -dev_disable_lro (dev); +netif_disable_lro (dev);/* Reference in_dev->dev */ netdev_hold (dev, &in_dev->dev_tracker,GFP_KERNEL );/* Account for reference dev->ip_ptr (below) */
Re: [PATCH net-next v10 08/14] net: hold netdev instance lock during sysfs operations
Quoted 41 lines · Stanislav Fomichev
Quoted 9 lines · Cosmin Ratiu
Call Trace: dump_stack_lvl+0x62/0x90 print_deadlock_bug+0x274/0x3b0 __lock_acquire+0x1229/0x2470 lock_acquire+0xb7/0x2b0 __mutex_lock+0xa6/0xd20 dev_disable_lro+0x20/0x80 inetdev_init+0x12f/0x1f0 inetdev_event+0x48b/0x870Quoted 8 lines
notifier_call_chain+0x38/0xf0 netif_change_net_namespace+0x72e/0x9f0 do_setlink.isra.0+0xd5/0x1220 rtnl_newlink+0x7ea/0xb50 rtnetlink_rcv_msg+0x459/0x5e0 netlink_rcv_skb+0x54/0x100 netlink_unicast+0x193/0x270 netlink_sendmsg+0x204/0x450I think something like the patch below should fix it?
inetdev_initis called for blackhole (sw device, we don't care about ops lock) and from
REGISTER/UNREGISTER notifiers. We hold the lock during REGISTER, and will soon hold the lock during UNREGISTER:
https://lore.kernel.org/netdev/[email protected]/(might also need to
EXPORT_SYMnetif_disable_lro)net/ipv4/devinet.c +1
-1@@ -281,7 +281,7 @@ static struct in_device *inetdev_init(struct net_device *dev) if (!in_dev->arp_parms) goto out_kfree; if (IPV4_DEVCONF(in_dev->cnf, FORWARDING)) -dev_disable_lro (dev); +netif_disable_lro (dev); /* Reference in_dev->dev */ netdev_hold(dev, &in_dev->dev_tracker, GFP_KERNEL);
Quoted 1 line
/* Account for reference
dev->ip_ptr(below) */
Unfortunately, this seems to result, on another code path, in:
WARNING: CPU: 10 PID: 1479 at ./include/net/netdev_lock.h:54 __netdev_update_features+0x65f/0xca0 __warn+0x81/0x180 __netdev_update_features+0x65f/0xca0 report_bug+0x156/0x180 handle_bug+0x4f/0x90 exc_invalid_op+0x13/0x60 asm_exc_invalid_op+0x16/0x20 __netdev_update_features+0x65f/0xca0 netif_disable_lro+0x30/0x1d0 inetdev_init+0x12f/0x1f0 inetdev_event+0x48b/0x870 notifier_call_chain+0x38/0xf0 register_netdevice+0x741/0x8b0 register_netdev+0x1f/0x40 mlx5e_probe+0x4e3/0x8e0 [mlx5_core] auxiliary_bus_probe+0x3f/0x90 really_probe+0xc3/0x3a0 __driver_probe_device+0x80/0x150 driver_probe_device+0x1f/0x90 __device_attach_driver+0x7d/0x100 bus_for_each_drv+0x80/0xd0 __device_attach+0xb4/0x1c0 bus_probe_device+0x91/0xa0 device_add+0x657/0x870
I see register_netdevice briefly acquires the netdev lock in two separate blocks and has a __netdev_update_features call in one of the blocks, but the lock is not held for call_netdevice_notifiers(NETDEV_REGISTER, dev).
Cosmin.
Re: [PATCH net-next v10 08/14] net: hold netdev instance lock during sysfs operations
Quoted 71 lines · Cosmin Ratiu
Quoted 41 lines · Stanislav Fomichev
Quoted 9 lines · Cosmin Ratiu
Call Trace: dump_stack_lvl+0x62/0x90 print_deadlock_bug+0x274/0x3b0 __lock_acquire+0x1229/0x2470 lock_acquire+0xb7/0x2b0 __mutex_lock+0xa6/0xd20 dev_disable_lro+0x20/0x80 inetdev_init+0x12f/0x1f0 inetdev_event+0x48b/0x870Quoted 8 lines
notifier_call_chain+0x38/0xf0 netif_change_net_namespace+0x72e/0x9f0 do_setlink.isra.0+0xd5/0x1220 rtnl_newlink+0x7ea/0xb50 rtnetlink_rcv_msg+0x459/0x5e0 netlink_rcv_skb+0x54/0x100 netlink_unicast+0x193/0x270 netlink_sendmsg+0x204/0x450I think something like the patch below should fix it?
inetdev_initis called for blackhole (sw device, we don't care about ops lock) and from
REGISTER/UNREGISTER notifiers. We hold the lock during REGISTER, and will soon hold the lock during UNREGISTER:
https://lore.kernel.org/netdev/[email protected]/(might also need to
EXPORT_SYMnetif_disable_lro)net/ipv4/devinet.c +1
-1@@ -281,7 +281,7 @@ static struct in_device *inetdev_init(struct net_device *dev) if (!in_dev->arp_parms) goto out_kfree; if (IPV4_DEVCONF(in_dev->cnf, FORWARDING)) -dev_disable_lro (dev); +netif_disable_lro (dev); /* Reference in_dev->dev */ netdev_hold(dev, &in_dev->dev_tracker, GFP_KERNEL);Quoted 1 line
/* Account for reference
dev->ip_ptr(below) */Unfortunately, this seems to result, on another code path, in:
WARNING: CPU: 10 PID: 1479 at ./include/net/netdev_lock.h:54 __netdev_update_features+0x65f/0xca0 __warn+0x81/0x180 __netdev_update_features+0x65f/0xca0 report_bug+0x156/0x180 handle_bug+0x4f/0x90 exc_invalid_op+0x13/0x60 asm_exc_invalid_op+0x16/0x20 __netdev_update_features+0x65f/0xca0 netif_disable_lro+0x30/0x1d0 inetdev_init+0x12f/0x1f0 inetdev_event+0x48b/0x870 notifier_call_chain+0x38/0xf0 register_netdevice+0x741/0x8b0 register_netdev+0x1f/0x40 mlx5e_probe+0x4e3/0x8e0 [mlx5_core] auxiliary_bus_probe+0x3f/0x90 really_probe+0xc3/0x3a0 __driver_probe_device+0x80/0x150 driver_probe_device+0x1f/0x90 __device_attach_driver+0x7d/0x100 bus_for_each_drv+0x80/0xd0 __device_attach+0xb4/0x1c0 bus_probe_device+0x91/0xa0 device_add+0x657/0x870
Quoted 4 lines
I see
register_netdevicebriefly acquires the netdev lock in two separate blocks and has a__netdev_update_featurescall in one of the blocks, but the lock is not held forcall_netdevice_notifiers(NETDEV_REGISTER, dev).
Ok, so we might need to also try to run NETDEV_REGISTER hooks consistently under the instance lock. This might bring more surprises, but I don't see any other easy option. Will test it out locally...
net/core/dev.c +5 -0
@@ -1815,7 +1815,9 @@ static int call_netdevice_register_notifiers(struct notifier_block *nb, {int err; +netdev_lock_ops (dev); err =call_netdevice_notifier (nb,NETDEV_REGISTER , dev); +netdev_unlock_ops (dev); err =notifier_to_errno (err);if (err)return err;
@@ -11014,7 +11016,9 @@ int register_netdevice(struct net_device *dev)memcpy (dev->perm_addr, dev->dev_addr, dev->addr_len);/* Notify protocols, that a new device appeared. */ +netdev_lock_ops (dev); ret =call_netdevice_notifiers (NETDEV_REGISTER , dev); +netdev_unlock_ops (dev); ret =notifier_to_errno (ret);if (ret) {/* Expect explicit free_netdev() on failure */
@@ -12036,6 +12040,7 @@ int netif_change_net_namespace(struct net_device *dev, struct net *net,int err, new_nsid;ASSERT_RTNL (); +netdev_ops_assert_locked (dev);/* Don't allow namespace local devices to be moved. */ err = -EINVAL ;
Stanislav Fomichev
reviewerPATCH v10 9/14 net: hold netdev instance lock during ndo_bpf
In reply to Stanislav FomichevCover the paths that come via bpf system call and XSK bind.
--- include/linux/netdevice.h | 1 + kernel/bpf/offload.c | 6 ++++-- net/core/dev.c | 13 +++++++++++-- net/core/dev_api.c | 12 ++++++++++++ net/xdp/xsk.c | 3 +++ net/xdp/xsk_buff_pool.c | 2 ++ 6 files changed, 33 insertions(+), 4 deletions(-)
include/linux/netdevice.h +1 -0
@@ -4277,6 +4277,7 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,int bpf_xdp_link_attach (const union bpf_attr *attr,struct bpf_prog *prog);u8 dev_xdp_prog_count (struct net_device *dev); +int netif_xdp_propagate (struct net_device *dev,struct netdev_bpf *bpf);int dev_xdp_propagate (struct net_device *dev,struct netdev_bpf *bpf);u8 dev_xdp_sb_prog_count (struct net_device *dev);u32 dev_xdp_prog_id (struct net_device *dev,enum bpf_xdp_mode mode);
kernel/bpf/offload.c +4 -2
@@ -528,10 +528,10 @@ struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)return ERR_PTR (-ENOMEM );bpf_map_init_from_attr (&offmap->map, attr); -rtnl_lock (); -down_write (&bpf_devs_lock); offmap->netdev =__dev_get_by_index (net, attr->map_ifindex); +netdev_lock_ops (offmap->netdev); +down_write (&bpf_devs_lock); err =bpf_dev_offload_check (offmap->netdev);if (err) goto err_unlock;
@@ -548,12 +548,14 @@ struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)list_add_tail (&offmap->offloads, &ondev->maps);up_write (&bpf_devs_lock); +netdev_unlock_ops (offmap->netdev);rtnl_unlock ();return &offmap->map; err_unlock:up_write (&bpf_devs_lock); +netdev_unlock_ops (offmap->netdev);rtnl_unlock ();bpf_map_area_free (offmap);return ERR_PTR (err);
net/core/dev.c +11 -2
@@ -9852,7 +9852,7 @@ u8 dev_xdp_sb_prog_count(struct net_device *dev)return count; } -int dev_xdp_propagate (struct net_device *dev,struct netdev_bpf *bpf) +int netif_xdp_propagate (struct net_device *dev,struct netdev_bpf *bpf) {if (!dev->netdev_ops->ndo_bpf)return -EOPNOTSUPP ;
@@ -9872,7 +9872,6 @@ int dev_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)return dev->netdev_ops->ndo_bpf (dev, bpf); } -EXPORT_SYMBOL_GPL (dev_xdp_propagate);u32 dev_xdp_prog_id (struct net_device *dev,enum bpf_xdp_mode mode) {
@@ -9902,6 +9901,8 @@ static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,struct netdev_bpf xdp;int err; +netdev_ops_assert_locked (dev); +if (dev->cfg->hds_config ==ETHTOOL_TCP_DATA_SPLIT_ENABLED && prog && !prog->aux->xdp_has_frags) {NL_SET_ERR_MSG (extack,"unable to install XDP to device using tcp-data-split" );
@@ -10134,7 +10135,9 @@ static void bpf_xdp_link_release(struct bpf_link *link)* already NULL, in which case link was already auto-detached */ if (xdp_link->dev) { +netdev_lock_ops (xdp_link->dev);WARN_ON (dev_xdp_detach_link (xdp_link->dev,NULL , xdp_link)); +netdev_unlock_ops (xdp_link->dev); xdp_link->dev =NULL ; }
@@ -10216,10 +10219,12 @@ static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog, goto out_unlock; } +netdev_lock_ops (xdp_link->dev); mode =dev_xdp_mode (xdp_link->dev, xdp_link->flags); bpf_op =dev_xdp_bpf_op (xdp_link->dev, mode); err =dev_xdp_install (xdp_link->dev, mode, bpf_op,NULL , xdp_link->flags, new_prog); +netdev_unlock_ops (xdp_link->dev);if (err) goto out_unlock;
@@ -11005,7 +11010,9 @@ int register_netdevice(struct net_device *dev)if (ret) goto err_uninit_notify; +netdev_lock_ops (dev);__netdev_update_features (dev); +netdev_unlock_ops (dev);/* * Default initial state at registry is that the
@@ -11945,7 +11952,9 @@ void unregister_netdevice_many_notify(struct list_head *head,/* Shutdown queueing discipline. */ dev_shutdown (dev);dev_tcx_uninstall (dev); +netdev_lock_ops (dev);dev_xdp_uninstall (dev); +netdev_unlock_ops (dev);bpf_dev_bound_netdev_unregister (dev);dev_memory_provider_uninstall (dev);
net/core/dev_api.c +12 -0
@@ -317,3 +317,15 @@ int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,return ret; }EXPORT_SYMBOL (dev_set_mac_address); + +int dev_xdp_propagate (struct net_device *dev,struct netdev_bpf *bpf) +{ +int ret; + +netdev_lock_ops (dev); + ret =netif_xdp_propagate (dev, bpf); +netdev_unlock_ops (dev); + +return ret; +} +EXPORT_SYMBOL_GPL (dev_xdp_propagate);
net/xdp/xsk.c +3 -0
@@ -1181,6 +1181,8 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) goto out_release; } +netdev_lock_ops (dev); +if (!xs->rx && !xs->tx) { err = -EINVAL ; goto out_unlock;
@@ -1315,6 +1317,7 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)smp_wmb ();WRITE_ONCE (xs->state,XSK_BOUND ); } +netdev_unlock_ops (dev); out_release:mutex_unlock (&xs->mutex);rtnl_unlock ();
net/xdp/xsk_buff_pool.c +2 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/netdevice.h> #include <net/xsk_buff_pool.h> #include <net/xdp_sock.h> #include <net/xdp_sock_drv.h>
@@ -219,6 +220,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool, bpf.xsk.pool = pool; bpf.xsk.queue_id = queue_id; +netdev_ops_assert_locked (netdev); err = netdev->netdev_ops->ndo_bpf (netdev, &bpf);if (err) goto err_unreg_pool;
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 10/14 net: ethtool: try to protect all callback with netdev instance lock
In reply to Stanislav FomichevFrom: Jakub Kicinski <[email protected]>
Protect all ethtool callbacks and PHY related state with the netdev instance lock, for drivers which want / need to have their ops instance-locked. Basically take the lock everywhere we take rtnl_lock. It was tempting to take the lock in ethnl_ops_begin(), but turns out we actually nest those calls (when generating notifications).
--- drivers/net/netdevsim/ethtool.c | 2 -- net/dsa/conduit.c | 16 +++++++++++++++- net/ethtool/cabletest.c | 20 ++++++++++++-------- net/ethtool/cmis_fw_update.c | 7 ++++++- net/ethtool/features.c | 6 ++++-- net/ethtool/ioctl.c | 6 ++++++ net/ethtool/module.c | 8 +++++--- net/ethtool/netlink.c | 12 ++++++++++++ net/ethtool/phy.c | 20 ++++++++++++++------ net/ethtool/rss.c | 2 ++ net/ethtool/tsinfo.c | 9 ++++++--- 11 files changed, 82 insertions(+), 26 deletions(-)
drivers/net/netdevsim/ethtool.c +0 -2
@@ -107,10 +107,8 @@ nsim_set_channels(struct net_device *dev, struct ethtool_channels *ch)struct netdevsim *ns =netdev_priv (dev);int err; -netdev_lock (dev); err =netif_set_real_num_queues (dev, ch->combined_count, ch->combined_count); -netdev_unlock (dev);if (err)return err;
net/dsa/conduit.c +15 -1
@@ -26,7 +26,9 @@ static int dsa_conduit_get_regs_len(struct net_device *dev)int len;if (ops->get_regs_len) { +netdev_lock_ops (dev); len = ops->get_regs_len (dev); +netdev_unlock_ops (dev);if (len <0 )return len; ret += len;
@@ -57,11 +59,15 @@ static void dsa_conduit_get_regs(struct net_device *dev,int len;if (ops->get_regs_len && ops->get_regs) { +netdev_lock_ops (dev); len = ops->get_regs_len (dev); -if (len <0 ) +if (len <0 ) { +netdev_unlock_ops (dev);return ; + } regs->len = len; ops->get_regs (dev, regs, data); +netdev_unlock_ops (dev); data += regs->len; }
@@ -91,8 +97,10 @@ static void dsa_conduit_get_ethtool_stats(struct net_device *dev,int count =0 ;if (ops->get_sset_count && ops->get_ethtool_stats) { +netdev_lock_ops (dev); count = ops->get_sset_count (dev,ETH_SS_STATS ); ops->get_ethtool_stats (dev, stats, data); +netdev_unlock_ops (dev); }if (ds->ops->get_ethtool_stats)
@@ -114,8 +122,10 @@ static void dsa_conduit_get_ethtool_phy_stats(struct net_device *dev,if (count >=0 )phy_ethtool_get_stats (dev->phydev, stats, data); }else if (ops->get_sset_count && ops->get_ethtool_phy_stats) { +netdev_lock_ops (dev); count = ops->get_sset_count (dev,ETH_SS_PHY_STATS ); ops->get_ethtool_phy_stats (dev, stats, data); +netdev_unlock_ops (dev); }if (count <0 )
@@ -132,11 +142,13 @@ static int dsa_conduit_get_sset_count(struct net_device *dev, int sset)struct dsa_switch *ds = cpu_dp->ds;int count =0 ; +netdev_lock_ops (dev);if (sset ==ETH_SS_PHY_STATS && dev->phydev && !ops->get_ethtool_phy_stats) count =phy_ethtool_get_sset_count (dev->phydev);else if (ops->get_sset_count) count = ops->get_sset_count (dev, sset); +netdev_unlock_ops (dev);if (count <0 ) count =0 ;
@@ -163,6 +175,7 @@ static void dsa_conduit_get_strings(struct net_device *dev, uint32_t stringset,/* We do not want to be NULL-terminated, since this is a prefix */ pfx[sizeof (pfx) -1 ] ='_' ; +netdev_lock_ops (dev);if (stringset ==ETH_SS_PHY_STATS && dev->phydev && !ops->get_ethtool_phy_stats) { mcount =phy_ethtool_get_sset_count (dev->phydev);
@@ -176,6 +189,7 @@ static void dsa_conduit_get_strings(struct net_device *dev, uint32_t stringset, mcount =0 ; ops->get_strings (dev, stringset, data); } +netdev_unlock_ops (dev);if (ds->ops->get_strings) { ndata = data + mcount * len;
net/ethtool/cabletest.c +12 -8
@@ -72,23 +72,24 @@ int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info) dev = req_info.dev;rtnl_lock (); +netdev_lock_ops (dev); phydev =ethnl_req_get_phydev (&req_info, tb[ETHTOOL_A_CABLE_TEST_HEADER ], info->extack);if (IS_ERR_OR_NULL (phydev)) { ret = -EOPNOTSUPP ; - goto out_rtnl; + goto out_unlock; } ops = ethtool_phy_ops;if (!ops || !ops->start_cable_test) { ret = -EOPNOTSUPP ; - goto out_rtnl; + goto out_unlock; } ret =ethnl_ops_begin (dev);if (ret <0 ) - goto out_rtnl; + goto out_unlock; ret = ops->start_cable_test (phydev, info->extack);
@@ -97,7 +98,8 @@ int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info)if (!ret)ethnl_cable_test_started (phydev,ETHTOOL_MSG_CABLE_TEST_NTF ); -out_rtnl: +out_unlock: +netdev_unlock_ops (dev);rtnl_unlock ();ethnl_parse_header_dev_put (&req_info);return ret;
@@ -339,23 +341,24 @@ int ethnl_act_cable_test_tdr(struct sk_buff *skb, struct genl_info *info) goto out_dev_put;rtnl_lock (); +netdev_lock_ops (dev); phydev =ethnl_req_get_phydev (&req_info, tb[ETHTOOL_A_CABLE_TEST_TDR_HEADER ], info->extack);if (IS_ERR_OR_NULL (phydev)) { ret = -EOPNOTSUPP ; - goto out_rtnl; + goto out_unlock; } ops = ethtool_phy_ops;if (!ops || !ops->start_cable_test_tdr) { ret = -EOPNOTSUPP ; - goto out_rtnl; + goto out_unlock; } ret =ethnl_ops_begin (dev);if (ret <0 ) - goto out_rtnl; + goto out_unlock; ret = ops->start_cable_test_tdr (phydev, info->extack, &cfg);
@@ -365,7 +368,8 @@ int ethnl_act_cable_test_tdr(struct sk_buff *skb, struct genl_info *info)ethnl_cable_test_started (phydev,ETHTOOL_MSG_CABLE_TEST_TDR_NTF ); -out_rtnl: +out_unlock: +netdev_unlock_ops (dev);rtnl_unlock (); out_dev_put:ethnl_parse_header_dev_put (&req_info);
net/ethtool/cmis_fw_update.c +6 -1
@@ -418,8 +418,13 @@ cmis_fw_update_commit_image(struct ethtool_cmis_cdb *cdb,static int cmis_fw_update_reset (struct net_device *dev) {__u32 reset_data =ETH_RESET_PHY ; +int ret; -return dev->ethtool_ops->reset (dev, &reset_data); +netdev_lock_ops (dev); + ret = dev->ethtool_ops->reset (dev, &reset_data); +netdev_unlock_ops (dev); + +return ret; }void
net/ethtool/features.c +4 -2
@@ -234,9 +234,10 @@ int ethnl_set_features(struct sk_buff *skb, struct genl_info *info) dev = req_info.dev;rtnl_lock (); +netdev_lock_ops (dev); ret =ethnl_ops_begin (dev);if (ret <0 ) - goto out_rtnl; + goto out_unlock;ethnl_features_to_bitmap (old_active, dev->features);ethnl_features_to_bitmap (old_wanted, dev->wanted_features); ret = ethnl_parse_bitset(req_wanted, req_mask, NETDEV_FEATURE_COUNT,
@@ -286,7 +287,8 @@ int ethnl_set_features(struct sk_buff *skb, struct genl_info *info) out_ops:ethnl_ops_complete (dev); -out_rtnl: +out_unlock: +netdev_unlock_ops (dev);rtnl_unlock ();ethnl_parse_header_dev_put (&req_info);return ret;
net/ethtool/ioctl.c +6 -0
@@ -2317,6 +2317,7 @@ static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)*/ busy = true;netdev_hold (dev, &dev_tracker,GFP_KERNEL ); +netdev_unlock_ops (dev);rtnl_unlock ();if (rc ==0 ) {
@@ -2331,8 +2332,10 @@ static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) do { rtnl_lock(); + netdev_lock_ops(dev); rc = ops->set_phys_id(dev, (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); + netdev_unlock_ops(dev); rtnl_unlock(); if (rc) break;
@@ -2341,6 +2344,7 @@ static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) }rtnl_lock (); +netdev_lock_ops (dev);netdev_put (dev, &dev_tracker); busy = false;
@@ -3140,6 +3144,7 @@ __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,return -EPERM ; } +netdev_lock_ops (dev);if (dev->dev.parent)pm_runtime_get_sync (dev->dev.parent);
@@ -3373,6 +3378,7 @@ __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr, out:if (dev->dev.parent)pm_runtime_put (dev->dev.parent); +netdev_unlock_ops (dev);return rc; }
net/ethtool/module.c +5 -3
@@ -419,19 +419,21 @@ int ethnl_act_module_fw_flash(struct sk_buff *skb, struct genl_info *info) dev = req_info.dev;rtnl_lock (); +netdev_lock_ops (dev); ret =ethnl_ops_begin (dev);if (ret <0 ) - goto out_rtnl; + goto out_unlock; ret =ethnl_module_fw_flash_validate (dev, info->extack);if (ret <0 ) - goto out_rtnl; + goto out_unlock; ret =module_flash_fw (dev, tb, skb, info);ethnl_ops_complete (dev); -out_rtnl: +out_unlock: +netdev_unlock_ops (dev);rtnl_unlock ();ethnl_parse_header_dev_put (&req_info);return ret;
net/ethtool/netlink.c +12 -0
@@ -90,6 +90,8 @@ int ethnl_ops_begin(struct net_device *dev)if (dev->dev.parent)pm_runtime_get_sync (dev->dev.parent); +netdev_ops_assert_locked (dev); +if (!netif_device_present (dev) || dev->reg_state >=NETREG_UNREGISTERING ) { ret = -ENODEV ;
@@ -490,7 +492,11 @@ static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)ethnl_init_reply_data (reply_data, ops, req_info->dev);rtnl_lock (); +if (req_info->dev) +netdev_lock_ops (req_info->dev); ret = ops->prepare_data (req_info, reply_data, info); +if (req_info->dev) +netdev_unlock_ops (req_info->dev);rtnl_unlock ();if (ret <0 ) goto err_cleanup;
@@ -548,7 +554,9 @@ static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,ethnl_init_reply_data (ctx->reply_data, ctx->ops, dev);rtnl_lock (); +netdev_lock_ops (ctx->req_info->dev); ret = ctx->ops->prepare_data (ctx->req_info, ctx->reply_data, info); +netdev_unlock_ops (ctx->req_info->dev);rtnl_unlock ();if (ret <0 ) goto out;
@@ -693,6 +701,7 @@ static int ethnl_default_set_doit(struct sk_buff *skb, struct genl_info *info) dev = req_info.dev;rtnl_lock (); +netdev_lock_ops (dev); dev->cfg_pending =kmemdup (dev->cfg,sizeof (*dev->cfg),GFP_KERNEL_ACCOUNT );if (!dev->cfg_pending) {
@@ -720,6 +729,7 @@ static int ethnl_default_set_doit(struct sk_buff *skb, struct genl_info *info)kfree (dev->cfg_pending); out_tie_cfg: dev->cfg_pending = dev->cfg; +netdev_unlock_ops (dev);rtnl_unlock (); out_dev:ethnl_parse_header_dev_put (&req_info);
@@ -777,6 +787,8 @@ static void ethnl_default_notify(struct net_device *dev, unsigned int cmd, req_info->dev = dev; req_info->flags |=ETHTOOL_FLAG_COMPACT_BITSETS ; +netdev_ops_assert_locked (dev); +ethnl_init_reply_data (reply_data, ops, dev); ret = ops->prepare_data (req_info, reply_data, &info);if (ret <0 )
net/ethtool/phy.c +14 -6
@@ -158,18 +158,19 @@ int ethnl_phy_doit(struct sk_buff *skb, struct genl_info *info)return ret;rtnl_lock (); +netdev_lock_ops (req_info.base.dev); ret =ethnl_phy_parse_request (&req_info.base, tb, info->extack);if (ret <0 ) - goto err_unlock_rtnl; + goto err_unlock;/* No PHY, return early */ if (!req_info.pdn) - goto err_unlock_rtnl; + goto err_unlock; ret =ethnl_phy_reply_size (&req_info.base, info->extack);if (ret <0 ) - goto err_unlock_rtnl; + goto err_unlock; reply_len = ret +ethnl_reply_header_size (); rskb = ethnl_reply_init(reply_len, req_info.base.dev,
@@ -178,13 +179,14 @@ int ethnl_phy_doit(struct sk_buff *skb, struct genl_info *info) info, &reply_payload);if (!rskb) { ret = -ENOMEM ; - goto err_unlock_rtnl; + goto err_unlock; } ret =ethnl_phy_fill_reply (&req_info.base, rskb);if (ret) goto err_free_msg; +netdev_unlock_ops (req_info.base.dev);rtnl_unlock ();ethnl_parse_header_dev_put (&req_info.base);genlmsg_end (rskb, reply_payload);
@@ -193,7 +195,8 @@ int ethnl_phy_doit(struct sk_buff *skb, struct genl_info *info) err_free_msg:nlmsg_free (rskb); -err_unlock_rtnl: +err_unlock: +netdev_unlock_ops (req_info.base.dev);rtnl_unlock ();ethnl_parse_header_dev_put (&req_info.base);return ret;
@@ -290,10 +293,15 @@ int ethnl_phy_dumpit(struct sk_buff *skb, struct netlink_callback *cb)rtnl_lock ();if (ctx->phy_req_info->base.dev) { - ret =ethnl_phy_dump_one_dev (skb, ctx->phy_req_info->base.dev, cb); + dev = ctx->phy_req_info->base.dev; +netdev_lock_ops (dev); + ret =ethnl_phy_dump_one_dev (skb, dev, cb); +netdev_unlock_ops (dev); }else {for_each_netdev_dump (net, dev, ctx->ifindex) { +netdev_lock_ops (dev); ret =ethnl_phy_dump_one_dev (skb, dev, cb); +netdev_unlock_ops (dev);if (ret)break ;
net/ethtool/rss.c +2 -0
@@ -345,7 +345,9 @@ int ethnl_rss_dumpit(struct sk_buff *skb, struct netlink_callback *cb)if (ctx->match_ifindex && ctx->match_ifindex != ctx->ifindex)break ; +netdev_lock_ops (dev); ret =rss_dump_one_dev (skb, cb, dev); +netdev_unlock_ops (dev);if (ret)break ; }
net/ethtool/tsinfo.c +6 -3
@@ -448,12 +448,15 @@ int ethnl_tsinfo_dumpit(struct sk_buff *skb, struct netlink_callback *cb)rtnl_lock ();if (ctx->req_info->base.dev) { - ret =ethnl_tsinfo_dump_one_net_topo (skb, - ctx->req_info->base.dev, - cb); + dev = ctx->req_info->base.dev; +netdev_lock_ops (dev); + ret =ethnl_tsinfo_dump_one_net_topo (skb, dev, cb); +netdev_unlock_ops (dev); }else {for_each_netdev_dump (net, dev, ctx->pos_ifindex) { +netdev_lock_ops (dev); ret =ethnl_tsinfo_dump_one_net_topo (skb, dev, cb); +netdev_unlock_ops (dev);if (ret <0 && ret != -EOPNOTSUPP )break ; ctx->pos_phyindex =0 ;
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 11/14 net: replace dev_addr_sem with netdev instance lock
In reply to Stanislav FomichevLockdep reports possible circular dependency in [0]. Instead of fixing the ordering, replace global dev_addr_sem with netdev instance lock. Most of the paths that set/get mac are RTNL protected. Two places where it's not, convert to explicit locking:
- sysfs address_show
- dev_get_mac_address via dev_ioctl
--- drivers/net/tap.c | 2 +- drivers/net/tun.c | 2 +- include/linux/netdevice.h | 6 +---- net/core/dev.c | 52 ++++++++++++++++++++------------------- net/core/dev.h | 3 +-- net/core/dev_api.c | 17 ++----------- net/core/dev_ioctl.c | 2 +- net/core/net-sysfs.c | 7 ++---- net/core/rtnetlink.c | 6 ++++- 9 files changed, 41 insertions(+), 56 deletions(-)
drivers/net/tap.c +1 -1
@@ -1017,7 +1017,7 @@ static long tap_ioctl(struct file *file, unsigned int cmd,rtnl_unlock ();return -ENOLINK ; } - ret =dev_set_mac_address_user (tap->dev, &sa,NULL ); + ret =dev_set_mac_address (tap->dev, &sa,NULL );tap_put_tap_dev (tap);rtnl_unlock ();return ret;
drivers/net/tun.c +1 -1
@@ -3175,7 +3175,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,case SIOCSIFHWADDR :/* Set hw address */ - ret =dev_set_mac_address_user (tun->dev, &ifr.ifr_hwaddr,NULL ); + ret =dev_set_mac_address (tun->dev, &ifr.ifr_hwaddr,NULL );break ;case TUNGETSNDBUF :
include/linux/netdevice.h +1 -5
@@ -2492,7 +2492,7 @@ struct net_device {* * Protects: * @gro_flush_timeout, @napi_defer_hard_irqs, @napi_list, -* @net_shaper_hierarchy, @reg_state, @threaded +* @net_shaper_hierarchy, @reg_state, @threaded, @dev_addr * * Partially protects (writers must hold both @lock and rtnl_lock): * @up
@@ -4262,10 +4262,6 @@ int netif_set_mac_address(struct net_device *dev, struct sockaddr *sa, struct netlink_ext_ack *extack);int dev_set_mac_address (struct net_device *dev,struct sockaddr *sa,struct netlink_ext_ack *extack); -int netif_set_mac_address_user (struct net_device *dev,struct sockaddr *sa, -struct netlink_ext_ack *extack); -int dev_set_mac_address_user (struct net_device *dev,struct sockaddr *sa, -struct netlink_ext_ack *extack);int dev_get_mac_address (struct sockaddr *sa,struct net *net,char *dev_name);int dev_get_port_parent_id (struct net_device *dev,struct netdev_phys_item_id *ppid,bool recurse);
net/core/dev.c +27 -25
77 lines not on this page · raw message
net/core/dev.h +1 -2
@@ -28,6 +28,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_get_by_name_lock (struct net *net,const char *name);struct net_device *__netdev_put_lock (struct net_device *dev);struct net_device * netdev_xa_find_lock(struct net *net, struct net_device *dev,
@@ -69,8 +70,6 @@ extern int weight_p;extern int dev_weight_rx_bias;extern int dev_weight_tx_bias; -extern struct rw_semaphore dev_addr_sem; -/* rtnl helpers */ extern struct list_head net_todo_list;void netdev_run_todo (void );
net/core/dev_api.c +2 -15
@@ -82,19 +82,6 @@ void dev_set_group(struct net_device *dev, int new_group)netdev_unlock_ops (dev); } -int dev_set_mac_address_user (struct net_device *dev,struct sockaddr *sa, -struct netlink_ext_ack *extack) -{ -int ret; - -netdev_lock_ops (dev); - ret =netif_set_mac_address_user (dev, sa, extack); -netdev_unlock_ops (dev); - -return ret; -} -EXPORT_SYMBOL (dev_set_mac_address_user); -/** * dev_change_net_namespace() - move device to different nethost namespace * @dev: device
@@ -310,9 +297,9 @@ int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa, {int ret; -netdev_lock_ops (dev); +netdev_lock (dev); ret =netif_set_mac_address (dev, sa, extack); -netdev_unlock_ops (dev); +netdev_unlock (dev);return ret; }
net/core/dev_ioctl.c +1 -1
@@ -574,7 +574,7 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,case SIOCSIFHWADDR :if (dev->addr_len >sizeof (struct sockaddr ))return -EINVAL ; -return dev_set_mac_address_user (dev, &ifr->ifr_hwaddr,NULL ); +return dev_set_mac_address (dev, &ifr->ifr_hwaddr,NULL );case SIOCSIFHWBROADCAST :if (ifr->ifr_hwaddr.sa_family != dev->type)
net/core/net-sysfs.c +2 -5
@@ -262,14 +262,11 @@ static ssize_t address_show(struct device *dev, struct device_attribute *attr,struct net_device *ndev =to_net_dev (dev);ssize_t ret = -EINVAL ; -down_read (&dev_addr_sem); - -rcu_read_lock (); +netdev_lock (ndev);if (dev_isalive (ndev)) ret =sysfs_format_mac (buf, ndev->dev_addr, ndev->addr_len); -rcu_read_unlock (); +netdev_unlock (ndev); -up_read (&dev_addr_sem);return ret; }static DEVICE_ATTR_RO (address);
net/core/rtnetlink.c +5 -1
@@ -3089,7 +3089,11 @@ static int do_setlink(const struct sk_buff *skb, struct net_device *dev, sa->sa_family = dev->type;memcpy (sa->sa_data,nla_data (tb[IFLA_ADDRESS ]), dev->addr_len); - err =netif_set_mac_address_user (dev, sa, extack); +if (!netdev_need_ops_lock (dev)) +netdev_lock (dev); + err =netif_set_mac_address (dev, sa, extack); +if (!netdev_need_ops_lock (dev)) +netdev_unlock (dev);kfree (sa);if (err) goto errout;
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 12/14 net: add option to request netdev instance lock
In reply to Stanislav FomichevCurrently only the drivers that implement shaper or queue APIs are grabbing instance lock. Add an explicit opt-in for the drivers that want to grab the lock without implementing the above APIs.
There is a 3-byte hole after @up, use it:
/* --- cacheline 47 boundary (3008 bytes) --- */
u32 napi_defer_hard_irqs; /* 3008 4 */
bool up; /* 3012 1 */
/* XXX 3 bytes hole, try to pack */
struct mutex lock; /* 3016 144 */
/* XXX last struct has 1 hole */
--- include/linux/netdevice.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/netdevice.h +7 -1
@@ -2485,6 +2485,12 @@ struct net_device {*/ bool up; +/** +* @request_ops_lock: request the core to run all @netdev_ops and +* @ethtool_ops under the @lock. +*/ +bool request_ops_lock; +/** * @lock: netdev-scope lock, protects a small selection of fields. * Should always be taken using netdev_lock() / netdev_unlock() helpers.
@@ -2774,7 +2780,7 @@ static inline void netdev_assert_locked_or_invisible(struct net_device *dev)static inline bool netdev_need_ops_lock (struct net_device *dev) { -bool ret = !!dev->queue_mgmt_ops; +bool ret = dev->request_ops_lock || !!dev->queue_mgmt_ops;#if IS_ENABLED (CONFIG_NET_SHAPER ) ret |= !!dev->netdev_ops->net_shaper_ops;
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 13/14 docs: net: document new locking reality
In reply to Stanislav FomichevAlso clarify ndo_get_stats (that read and write paths can run concurrently) and mention only RCU.
--- Documentation/networking/netdevices.rst | 65 ++++++++++++++++++++----- include/linux/netdevice.h | 4 ++ 2 files changed, 56 insertions(+), 13 deletions(-)
Documentation/networking/netdevices.rst +52 -13
@@ -210,49 +210,55 @@ packets is preferred. struct net_device synchronization rules ======================================= ndo_open: - Synchronization: rtnl_lock() semaphore. + Synchronization: rtnl_lock() semaphore. In addition, netdev instance + lock if the driver implements queue management or shaper API. Context: process ndo_stop: - Synchronization: rtnl_lock() semaphore. + Synchronization: rtnl_lock() semaphore. In addition, netdev instance + lock if the driver implements queue management or shaper API. Context: process Note: netif_running() is guaranteed false ndo_do_ioctl: Synchronization: rtnl_lock() semaphore. - Context: process - This is only called by network subsystems internally, - not by user space calling ioctl as it was in before - linux-5.14. + This is only called by network subsystems internally, + not by user space calling ioctl as it was in before + linux-5.14. ndo_siocbond: - Synchronization: rtnl_lock() semaphore. + Synchronization: rtnl_lock() semaphore. In addition, netdev instance + lock if the driver implements queue management or shaper API. Context: process - Used by the bonding driver for the SIOCBOND family of - ioctl commands. + Used by the bonding driver for the SIOCBOND family of + ioctl commands. ndo_siocwandev: - Synchronization: rtnl_lock() semaphore. + Synchronization: rtnl_lock() semaphore. In addition, netdev instance + lock if the driver implements queue management or shaper API. Context: process Used by the drivers/net/wan framework to handle the SIOCWANDEV ioctl with the if_settings structure. ndo_siocdevprivate: - Synchronization: rtnl_lock() semaphore. + Synchronization: rtnl_lock() semaphore. In addition, netdev instance + lock if the driver implements queue management or shaper API. Context: process This is used to implement SIOCDEVPRIVATE ioctl helpers. These should not be added to new drivers, so don't use. ndo_eth_ioctl: - Synchronization: rtnl_lock() semaphore. + Synchronization: rtnl_lock() semaphore. In addition, netdev instance + lock if the driver implements queue management or shaper API. Context: process ndo_get_stats: - Synchronization: rtnl_lock() semaphore, or RCU. + Synchronization: RCU (can be called concurrently with the stats + update path). Context: atomic (can't sleep under RCU) ndo_start_xmit:
@@ -284,6 +290,10 @@ struct net_device synchronization rules Synchronization: netif_addr_lock spinlock. Context: BHs disabled +Most ndo callbacks not specified in the list above are running +under ``rtnl_lock``. In addition, netdev instance lock is taken as well if +the driver implements queue management or shaper API. + struct napi_struct synchronization rules ======================================== napi->poll:
@@ -298,6 +308,35 @@ 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 +============================= + +Historically, all networking control operations were protected by a single +global lock known as ``rtnl_lock``. There is an ongoing effort to replace this +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 +drivers to opt out of using ``rtnl_lock`` and instead perform their control +operations directly under the netdev instance lock. + +Devices drivers are encouraged to rely on the instance lock where possible. + +For the (mostly software) drivers that need to interact with the core stack, +there are two sets of interfaces: ``dev_xxx`` and ``netif_xxx`` (e.g., +``dev_set_mtu`` and ``netif_set_mtu``). The ``dev_xxx`` functions handle +acquiring the instance lock themselves, while the ``netif_xxx`` functions +assume that the driver has already acquired the instance lock. + NETDEV_INTERNAL symbol namespace ================================
include/linux/netdevice.h +4 -0
@@ -2505,6 +2505,10 @@ struct net_device {* * Also protects some fields in struct napi_struct. * +* For the drivers that implement shaper or queue API, the scope +* of this lock is expanded to cover most ndo/queue/ethtool/sysfs +* operations. +* * Ordering: take after rtnl_lock. */ struct mutex lock;
Signature · 1 line
2.48.1
Stanislav Fomichev
reviewerPATCH v10 14/14 eth: bnxt: remove most dependencies on RTNL
In reply to Stanislav FomichevOnly devlink and sriov paths are grabbing rtnl explicitly. The rest is covered by netdev instance lock which the core now grabs, so there is no need to manage rtnl in most places anymore.
On the core side we can now try to drop rtnl in some places (do_setlink for example) for the drivers that signal non-rtnl mode (TBD).
Boot-tested and with ethtool -L eth1 combined 24 to trigger reset.
--- drivers/net/ethernet/broadcom/bnxt/bnxt.c | 117 +++++++++--------- .../net/ethernet/broadcom/bnxt/bnxt_devlink.c | 9 ++ .../net/ethernet/broadcom/bnxt/bnxt_sriov.c | 6 + drivers/net/ethernet/broadcom/bnxt/bnxt_ulp.c | 16 ++- drivers/net/ethernet/broadcom/bnxt/bnxt_vfr.c | 18 ++- drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c | 3 +- include/linux/netdevice.h | 5 + 7 files changed, 101 insertions(+), 73 deletions(-)
drivers/net/ethernet/broadcom/bnxt/bnxt.c +61 -56
@@ -5246,8 +5246,10 @@ static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all) {int i; -/* Under rtnl_lock and all our NAPIs have been disabled. It's -* safe to delete the hash table. +netdev_assert_locked (bp->dev); + +/* Under netdev instance lock and all our NAPIs have been disabled. +* It's safe to delete the hash table. */ for (i =0 ; i <BNXT_NTP_FLTR_HASH_SIZE ; i++) {struct hlist_head *head;
@@ -12789,7 +12791,6 @@ static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)return rc; } -/* rtnl_lock held */ int bnxt_open_nic (struct bnxt *bp,bool irq_re_init,bool link_re_init) {int rc =0 ;
@@ -12805,9 +12806,9 @@ int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)return rc; } -/* rtnl_lock held, open the NIC half way by allocating all resources, but -* NAPI, IRQ, and TX are not enabled. This is mainly used for offline -* self tests. +/* netdev instance lock held, open the NIC half way by allocating all +* resources, but NAPI, IRQ, and TX are not enabled. This is mainly used +* for offline self tests. */ int bnxt_half_open_nic (struct bnxt *bp) {
@@ -12842,8 +12843,8 @@ int bnxt_half_open_nic(struct bnxt *bp)return rc; } -/* rtnl_lock held, this call can only be made after a previous successful -* call to bnxt_half_open_nic(). +/* netdev instance lock held, this call can only be made after a previous +* successful call to bnxt_half_open_nic(). */ void bnxt_half_close_nic (struct bnxt *bp) {
@@ -12952,10 +12953,11 @@ void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)if (test_bit (BNXT_STATE_IN_FW_RESET , &bp->state)) {/* If we get here, it means firmware reset is in progress * while we are trying to close. We can safely proceed with -* the close because we are holding rtnl_lock(). Some firmware -* messages may fail as we proceed to close. We set the -* ABORT_ERR flag here so that the FW reset thread will later -* abort when it gets the rtnl_lock() and sees the flag. +* the close because we are holding netdev instance lock. +* Some firmware messages may fail as we proceed to close. +* We set the ABORT_ERR flag here so that the FW reset thread +* will later abort when it gets the netdev instance lock +* and sees the flag. */ netdev_warn (bp->dev,"FW reset in progress during close, FW reset will be aborted\n" );set_bit (BNXT_STATE_ABORT_ERR , &bp->state);
@@ -13046,7 +13048,7 @@ static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,return hwrm_req_send (bp, req); } -/* rtnl_lock held */ +/* netdev instance lock held */ static int bnxt_ioctl (struct net_device *dev,struct ifreq *ifr,int cmd) {struct mii_ioctl_data *mdio =if_mii (ifr);
@@ -13965,30 +13967,31 @@ static void bnxt_timer(struct timer_list *t)mod_timer (&bp->timer, jiffies + bp->current_interval); } -static void bnxt_rtnl_lock_sp (struct bnxt *bp) +static void bnxt_lock_sp (struct bnxt *bp) {/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK * set. If the device is being closed, bnxt_close() may be holding -* rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we -* must clear BNXT_STATE_IN_SP_TASK before holding rtnl(). +* netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear. +* So we must clear BNXT_STATE_IN_SP_TASK before holding netdev +* instance lock. */ clear_bit (BNXT_STATE_IN_SP_TASK , &bp->state); -rtnl_lock (); +netdev_lock (bp->dev); } -static void bnxt_rtnl_unlock_sp (struct bnxt *bp) +static void bnxt_unlock_sp (struct bnxt *bp) {set_bit (BNXT_STATE_IN_SP_TASK , &bp->state); -rtnl_unlock (); +netdev_unlock (bp->dev); }/* Only called from bnxt_sp_task() */ static void bnxt_reset (struct bnxt *bp,bool silent) { -bnxt_rtnl_lock_sp (bp); +bnxt_lock_sp (bp);if (test_bit (BNXT_STATE_OPEN , &bp->state))bnxt_reset_task (bp, silent); -bnxt_rtnl_unlock_sp (bp); +bnxt_unlock_sp (bp); }/* Only called from bnxt_sp_task() */
@@ -13996,9 +13999,9 @@ static void bnxt_rx_ring_reset(struct bnxt *bp) {int i; -bnxt_rtnl_lock_sp (bp); +bnxt_lock_sp (bp);if (!test_bit (BNXT_STATE_OPEN , &bp->state)) { -bnxt_rtnl_unlock_sp (bp); +bnxt_unlock_sp (bp);return ; }/* Disable and flush TPA before resetting the RX ring */
@@ -14037,7 +14040,7 @@ static void bnxt_rx_ring_reset(struct bnxt *bp) }if (bp->flags &BNXT_FLAG_TPA )bnxt_set_tpa (bp, true); -bnxt_rtnl_unlock_sp (bp); +bnxt_unlock_sp (bp); }static void bnxt_fw_fatal_close (struct bnxt *bp)
@@ -14093,7 +14096,7 @@ static bool is_bnxt_fw_ok(struct bnxt *bp)return false; } -/* rtnl_lock is acquired before calling this function */ +/* netdev instance lock is acquired before calling this function */ static void bnxt_force_fw_reset (struct bnxt *bp) {struct bnxt_fw_health *fw_health = bp->fw_health;
@@ -14136,9 +14139,9 @@ void bnxt_fw_exception(struct bnxt *bp)netdev_warn (bp->dev,"Detected firmware fatal condition, initiating reset\n" );set_bit (BNXT_STATE_FW_FATAL_COND , &bp->state);bnxt_ulp_stop (bp); -bnxt_rtnl_lock_sp (bp); +bnxt_lock_sp (bp);bnxt_force_fw_reset (bp); -bnxt_rtnl_unlock_sp (bp); +bnxt_unlock_sp (bp); }/* Returns the number of registered VFs, or 1 if VF configuration is pending, or
@@ -14168,7 +14171,7 @@ static int bnxt_get_registered_vfs(struct bnxt *bp)void bnxt_fw_reset (struct bnxt *bp) {bnxt_ulp_stop (bp); -bnxt_rtnl_lock_sp (bp); +bnxt_lock_sp (bp);if (test_bit (BNXT_STATE_OPEN , &bp->state) && !test_bit (BNXT_STATE_IN_FW_RESET , &bp->state)) {struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
@@ -14214,7 +14217,7 @@ void bnxt_fw_reset(struct bnxt *bp)bnxt_queue_fw_reset_work (bp, tmo); } fw_reset_exit: -bnxt_rtnl_unlock_sp (bp); +bnxt_unlock_sp (bp); }static void bnxt_chk_missed_irq (struct bnxt *bp)
@@ -14413,7 +14416,7 @@ static void bnxt_sp_task(struct work_struct *work)static void _bnxt_get_max_rings (struct bnxt *bp,int *max_rx,int *max_tx,int *max_cp); -/* Under rtnl_lock */ +/* Under netdev instance lock */ int bnxt_check_rings (struct bnxt *bp,int tx,int rx,bool sh,int tcs,int tx_xdp) {
@@ -14841,10 +14844,10 @@ static void bnxt_fw_reset_task(struct work_struct *work)return ; } bp->fw_reset_timestamp = jiffies; -rtnl_lock (); +netdev_lock (bp->dev);if (test_bit (BNXT_STATE_ABORT_ERR , &bp->state)) {bnxt_fw_reset_abort (bp, rc); -rtnl_unlock (); +netdev_unlock (bp->dev); goto ulp_start; }bnxt_fw_reset_close (bp);
@@ -14855,7 +14858,7 @@ static void bnxt_fw_reset_task(struct work_struct *work) bp->fw_reset_state =BNXT_FW_RESET_STATE_ENABLE_DEV ; tmo = bp->fw_reset_min_dsecs *HZ /10 ; } -rtnl_unlock (); +netdev_unlock (bp->dev);bnxt_queue_fw_reset_work (bp, tmo);return ; }
@@ -14929,7 +14932,7 @@ static void bnxt_fw_reset_task(struct work_struct *work) bp->fw_reset_state =BNXT_FW_RESET_STATE_OPENING ; fallthrough;case BNXT_FW_RESET_STATE_OPENING : -while (!rtnl_trylock ()) { +while (!netdev_trylock (bp->dev)) {bnxt_queue_fw_reset_work (bp,HZ /10 );return ; }
@@ -14937,7 +14940,7 @@ static void bnxt_fw_reset_task(struct work_struct *work)if (rc) {netdev_err (bp->dev,"bnxt_open() failed during FW reset\n" );bnxt_fw_reset_abort (bp, rc); -rtnl_unlock (); +netdev_unlock (bp->dev); goto ulp_start; }
@@ -14956,13 +14959,13 @@ static void bnxt_fw_reset_task(struct work_struct *work)bnxt_dl_health_fw_recovery_done (bp);bnxt_dl_health_fw_status_update (bp, true); } -rtnl_unlock (); +netdev_unlock (bp->dev);bnxt_ulp_start (bp,0 );bnxt_reenable_sriov (bp); -rtnl_lock (); +netdev_lock (bp->dev);bnxt_vf_reps_alloc (bp);bnxt_vf_reps_open (bp); -rtnl_unlock (); +netdev_unlock (bp->dev);break ; }return ;
@@ -14975,9 +14978,9 @@ static void bnxt_fw_reset_task(struct work_struct *work)netdev_err (bp->dev,"fw_health_status 0x%x\n" , sts); } fw_reset_abort: -rtnl_lock (); +netdev_lock (bp->dev);bnxt_fw_reset_abort (bp, rc); -rtnl_unlock (); +netdev_unlock (bp->dev); ulp_start:bnxt_ulp_start (bp, rc); }
@@ -15069,13 +15072,14 @@ static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)return rc; } -/* rtnl_lock held */ static int bnxt_change_mac_addr (struct net_device *dev,void *p) {struct sockaddr *addr = p;struct bnxt *bp =netdev_priv (dev);int rc =0 ; +netdev_assert_locked (dev); +if (!is_valid_ether_addr (addr->sa_data))return -EADDRNOTAVAIL ;
@@ -15096,11 +15100,12 @@ static int bnxt_change_mac_addr(struct net_device *dev, void *p)return rc; } -/* rtnl_lock held */ static int bnxt_change_mtu (struct net_device *dev,int new_mtu) {struct bnxt *bp =netdev_priv (dev); +netdev_assert_locked (dev); +if (netif_running (dev))bnxt_close_nic (bp, true, false);
@@ -16257,7 +16262,7 @@ int bnxt_restore_pf_fw_resources(struct bnxt *bp) {int rc; -ASSERT_RTNL (); +netdev_ops_assert_locked (bp->dev);bnxt_hwrm_func_qcaps (bp);if (netif_running (bp->dev))
@@ -16657,7 +16662,7 @@ static void bnxt_shutdown(struct pci_dev *pdev)if (!dev)return ; -rtnl_lock (); +netdev_lock (dev); bp =netdev_priv (dev);if (!bp) goto shutdown_exit;
@@ -16675,7 +16680,7 @@ static void bnxt_shutdown(struct pci_dev *pdev) } shutdown_exit: -rtnl_unlock (); +netdev_unlock (dev); }#ifdef CONFIG_PM_SLEEP
@@ -16687,7 +16692,7 @@ static int bnxt_suspend(struct device *device)bnxt_ulp_stop (bp); -rtnl_lock (); +netdev_lock (dev);if (netif_running (dev)) {netif_device_detach (dev); rc =bnxt_close (dev);
@@ -16696,7 +16701,7 @@ static int bnxt_suspend(struct device *device)bnxt_ptp_clear (bp);pci_disable_device (bp->pdev);bnxt_free_ctx_mem (bp, false); -rtnl_unlock (); +netdev_unlock (dev);return rc; }
@@ -16706,7 +16711,7 @@ static int bnxt_resume(struct device *device)struct bnxt *bp =netdev_priv (dev);int rc =0 ; -rtnl_lock (); +netdev_lock (dev); rc =pci_enable_device (bp->pdev);if (rc) { netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
@@ -16749,7 +16754,7 @@ static int bnxt_resume(struct device *device) } resume_exit: -rtnl_unlock (); +netdev_unlock (bp->dev);bnxt_ulp_start (bp, rc);if (!rc)bnxt_reenable_sriov (bp);
@@ -16784,7 +16789,7 @@ static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,bnxt_ulp_stop (bp); -rtnl_lock (); +netdev_lock (netdev);netif_device_detach (netdev);if (test_and_set_bit (BNXT_STATE_IN_FW_RESET , &bp->state)) {
@@ -16793,7 +16798,7 @@ static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, }if (abort || state == pci_channel_io_perm_failure) { -rtnl_unlock (); +netdev_unlock (netdev);return PCI_ERS_RESULT_DISCONNECT ; }
@@ -16812,7 +16817,7 @@ static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,if (pci_is_enabled (pdev))pci_disable_device (pdev);bnxt_free_ctx_mem (bp, false); -rtnl_unlock (); +netdev_unlock (netdev);/* Request a slot slot reset. */ return PCI_ERS_RESULT_NEED_RESET ;
@@ -16842,7 +16847,7 @@ static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))msleep (900 ); -rtnl_lock (); +netdev_lock (netdev);if (pci_enable_device (pdev)) { dev_err(&pdev->dev,
@@ -16897,7 +16902,7 @@ static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) reset_exit:clear_bit (BNXT_STATE_IN_FW_RESET , &bp->state);bnxt_clear_reservations (bp, true); -rtnl_unlock (); +netdev_unlock (netdev);return result; }
@@ -16916,7 +16921,7 @@ static void bnxt_io_resume(struct pci_dev *pdev)int err;netdev_info (bp->dev,"PCI Slot Resume\n" ); -rtnl_lock (); +netdev_lock (netdev); err =bnxt_hwrm_func_qcaps (bp);if (!err) {
@@ -16929,7 +16934,7 @@ static void bnxt_io_resume(struct pci_dev *pdev)if (!err)netif_device_attach (netdev); -rtnl_unlock (); +netdev_unlock (netdev);bnxt_ulp_start (bp, err);if (!err)bnxt_reenable_sriov (bp);
drivers/net/ethernet/broadcom/bnxt/bnxt_devlink.c +9 -0
@@ -439,14 +439,17 @@ static int bnxt_dl_reload_down(struct devlink *dl, bool netns_change,case DEVLINK_RELOAD_ACTION_DRIVER_REINIT : {bnxt_ulp_stop (bp);rtnl_lock (); +netdev_lock (bp->dev);if (bnxt_sriov_cfg (bp)) {NL_SET_ERR_MSG_MOD (extack,"reload is unsupported while VFs are allocated or being configured" ); +netdev_unlock (bp->dev);rtnl_unlock ();bnxt_ulp_start (bp,0 );return -EOPNOTSUPP ; }if (bp->dev->reg_state ==NETREG_UNREGISTERED ) { +netdev_unlock (bp->dev);rtnl_unlock ();bnxt_ulp_start (bp,0 );return -ENODEV ;
@@ -459,6 +462,7 @@ static int bnxt_dl_reload_down(struct devlink *dl, bool netns_change,NL_SET_ERR_MSG_MOD (extack,"Failed to deregister" );if (netif_running (bp->dev))dev_close (bp->dev); +netdev_unlock (bp->dev);rtnl_unlock ();break ; }
@@ -479,7 +483,9 @@ static int bnxt_dl_reload_down(struct devlink *dl, bool netns_change,return -EPERM ; }rtnl_lock (); +netdev_lock (bp->dev);if (bp->dev->reg_state ==NETREG_UNREGISTERED ) { +netdev_unlock (bp->dev);rtnl_unlock ();return -ENODEV ; }
@@ -493,6 +499,7 @@ static int bnxt_dl_reload_down(struct devlink *dl, bool netns_change,if (rc) {NL_SET_ERR_MSG_MOD (extack,"Failed to activate firmware" );clear_bit (BNXT_STATE_FW_ACTIVATE , &bp->state); +netdev_unlock (bp->dev);rtnl_unlock (); }break ;
@@ -568,7 +575,9 @@ static int bnxt_dl_reload_up(struct devlink *dl, enum devlink_reload_action acti } *actions_performed |=BIT (action); }else if (netif_running (bp->dev)) { +netdev_lock (bp->dev);dev_close (bp->dev); +netdev_unlock (bp->dev); }rtnl_unlock ();if (action ==DEVLINK_RELOAD_ACTION_DRIVER_REINIT )
drivers/net/ethernet/broadcom/bnxt/bnxt_sriov.c +6 -0
@@ -946,7 +946,9 @@ void bnxt_sriov_disable(struct bnxt *bp)/* Reclaim all resources for the PF. */ rtnl_lock (); +netdev_lock (bp->dev);bnxt_restore_pf_fw_resources (bp); +netdev_unlock (bp->dev);rtnl_unlock (); }
@@ -956,17 +958,21 @@ int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)struct bnxt *bp =netdev_priv (dev);rtnl_lock (); +netdev_lock (dev);if (!netif_running (dev)) {netdev_warn (dev,"Reject SRIOV config request since if is down!\n" ); +netdev_unlock (dev);rtnl_unlock ();return 0 ; }if (test_bit (BNXT_STATE_IN_FW_RESET , &bp->state)) {netdev_warn (dev,"Reject SRIOV config request when FW reset is in progress\n" ); +netdev_unlock (dev);rtnl_unlock ();return 0 ; } bp->sriov_cfg = true; +netdev_unlock (dev);rtnl_unlock ();if (pci_vfs_assigned (bp->pdev)) {
drivers/net/ethernet/broadcom/bnxt/bnxt_ulp.c +10 -6
@@ -112,7 +112,7 @@ int bnxt_register_dev(struct bnxt_en_dev *edev,struct bnxt_ulp *ulp;int rc =0 ; -rtnl_lock (); +netdev_lock (dev);mutex_lock (&edev->en_dev_lock);if (!bp->irq_tbl) { rc = -ENODEV ;
@@ -138,7 +138,7 @@ int bnxt_register_dev(struct bnxt_en_dev *edev, edev->flags |=BNXT_EN_FLAG_MSIX_REQUESTED ; exit:mutex_unlock (&edev->en_dev_lock); -rtnl_unlock (); +netdev_unlock (dev);return rc; }EXPORT_SYMBOL (bnxt_register_dev);
@@ -151,7 +151,7 @@ void bnxt_unregister_dev(struct bnxt_en_dev *edev)int i =0 ; ulp = edev->ulp_tbl; -rtnl_lock (); +netdev_lock (dev);mutex_lock (&edev->en_dev_lock);if (ulp->msix_requested) edev->flags &= ~BNXT_EN_FLAG_MSIX_REQUESTED ;
@@ -169,7 +169,7 @@ void bnxt_unregister_dev(struct bnxt_en_dev *edev) i++; }mutex_unlock (&edev->en_dev_lock); -rtnl_unlock (); +netdev_unlock (dev);return ; }EXPORT_SYMBOL (bnxt_unregister_dev);
@@ -309,12 +309,14 @@ void bnxt_ulp_irq_stop(struct bnxt *bp)if (!ulp->msix_requested)return ; - ops =rtnl_dereference (ulp->ulp_ops); +netdev_lock (bp->dev); + ops =rcu_dereference (ulp->ulp_ops);if (!ops || !ops->ulp_irq_stop)return ;if (test_bit (BNXT_STATE_FW_RESET_DET , &bp->state)) reset = true; ops->ulp_irq_stop (ulp->handle, reset); +netdev_unlock (bp->dev); } }
@@ -333,7 +335,8 @@ void bnxt_ulp_irq_restart(struct bnxt *bp, int err)if (!ulp->msix_requested)return ; - ops =rtnl_dereference (ulp->ulp_ops); +netdev_lock (bp->dev); + ops =rcu_dereference (ulp->ulp_ops);if (!ops || !ops->ulp_irq_restart)return ;
@@ -345,6 +348,7 @@ void bnxt_ulp_irq_restart(struct bnxt *bp, int err)bnxt_fill_msix_vecs (bp, ent); } ops->ulp_irq_restart (ulp->handle, ent); +netdev_unlock (bp->dev);kfree (ent); } }
drivers/net/ethernet/broadcom/bnxt/bnxt_vfr.c +8 -10
@@ -257,8 +257,7 @@ bool bnxt_dev_is_vf_rep(struct net_device *dev)/* Called when the parent PF interface is closed: * As the mode transition from SWITCHDEV to LEGACY -* happens under the rtnl_lock() this routine is safe -* under the rtnl_lock() +* happens under the netdev instance lock this routine is safe */ void bnxt_vf_reps_close (struct bnxt *bp) {
@@ -278,8 +277,7 @@ void bnxt_vf_reps_close(struct bnxt *bp)/* Called when the parent PF interface is opened (re-opened): * As the mode transition from SWITCHDEV to LEGACY -* happen under the rtnl_lock() this routine is safe -* under the rtnl_lock() +* happen under the netdev instance lock this routine is safe */ void bnxt_vf_reps_open (struct bnxt *bp) {
@@ -348,7 +346,7 @@ void bnxt_vf_reps_destroy(struct bnxt *bp)/* Ensure that parent PF's and VF-reps' RX/TX has been quiesced * before proceeding with VF-rep cleanup. */ -rtnl_lock (); +netdev_lock (bp->dev);if (netif_running (bp->dev)) {bnxt_close_nic (bp, false, false); closed = true;
@@ -365,10 +363,10 @@ void bnxt_vf_reps_destroy(struct bnxt *bp)bnxt_open_nic (bp, false, false); bp->eswitch_mode =DEVLINK_ESWITCH_MODE_SWITCHDEV ; } -rtnl_unlock (); +netdev_unlock (bp->dev); -/* Need to call vf_reps_destroy() outside of rntl_lock -* as unregister_netdev takes rtnl_lock +/* Need to call vf_reps_destroy() outside of netdev instance lock +* as unregister_netdev takes it */ __bnxt_vf_reps_destroy (bp); }
@@ -376,7 +374,7 @@ void bnxt_vf_reps_destroy(struct bnxt *bp)/* Free the VF-Reps in firmware, during firmware hot-reset processing. * Note that the VF-Rep netdevs are still active (not unregistered) during * this process. As the mode transition from SWITCHDEV to LEGACY happens -* under the rtnl_lock() this routine is safe under the rtnl_lock(). +* under the netdev instance lock this routine is safe. */ void bnxt_vf_reps_free (struct bnxt *bp) {
@@ -413,7 +411,7 @@ static int bnxt_alloc_vf_rep(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,/* Allocate the VF-Reps in firmware, during firmware hot-reset processing. * Note that the VF-Rep netdevs are still active (not unregistered) during * this process. As the mode transition from SWITCHDEV to LEGACY happens -* under the rtnl_lock() this routine is safe under the rtnl_lock(). +* under the netdev instance lock this routine is safe. */ int bnxt_vf_reps_alloc (struct bnxt *bp) {
drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c +2 -1
@@ -382,13 +382,14 @@ int bnxt_xdp_xmit(struct net_device *dev, int num_frames,return nxmit; } -/* Under rtnl_lock */ static int bnxt_xdp_set (struct bnxt *bp,struct bpf_prog *prog) {struct net_device *dev = bp->dev;int tx_xdp =0 , tx_cp, rc, tc;struct bpf_prog *old; +netdev_assert_locked (dev); +if (prog && !prog->aux->xdp_has_frags && bp->dev->mtu >BNXT_MAX_PAGE_MODE_MTU ) { netdev_warn(dev, "MTU %d larger than %d without XDP frag support.\n",
include/linux/netdevice.h +5 -0
@@ -2765,6 +2765,11 @@ static inline void netdev_lock(struct net_device *dev)mutex_lock (&dev->lock); } +static inline bool netdev_trylock (struct net_device *dev) +{ +return mutex_trylock (&dev->lock); +} +static inline void netdev_unlock (struct net_device *dev) {mutex_unlock (&dev->lock);
Signature · 1 line
2.48.1
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Wed, 5 Mar 2025 08:37:18 -0800 you wrote:
As the gradual purging of rtnl continues, start grabbing netdev instance lock in more places so we can get to the state where most paths are working without rtnl. Start with requiring the drivers that use shaper api (and later queue mgmt api) to work with both rtnl and netdev instance lock. Eventually we might attempt to drop rtnl. This mostly affects iavf, gve, bnxt and netdev sim (as the drivers that implement shaper/queue mgmt) so those drivers are converted in the process.
[...]
Here is the summary with links:
- [net-next,v10,01/14] net: hold netdev instance lock during ndo_open/ndo_stop
https://git.kernel.org/netdev/net-next/c/d4c22ec680c8 - [net-next,v10,02/14] net: hold netdev instance lock during nft ndo_setup_tc https://git.kernel.org/netdev/net-next/c/c4f0f30b424e - [net-next,v10,03/14] net: sched: wrap doit/dumpit methods https://git.kernel.org/netdev/net-next/c/7c79cff95535 - [net-next,v10,04/14] net: hold netdev instance lock during qdisc ndo_setup_tc https://git.kernel.org/netdev/net-next/c/a0527ee2df3f - [net-next,v10,05/14] net: hold netdev instance lock during queue operations https://git.kernel.org/netdev/net-next/c/cae03e5bdd9e - [net-next,v10,06/14] net: hold netdev instance lock during rtnetlink operations https://git.kernel.org/netdev/net-next/c/7e4d784f5810 - [net-next,v10,07/14] net: hold netdev instance lock during ioctl operations https://git.kernel.org/netdev/net-next/c/ffb7ed19ac0a - [net-next,v10,08/14] net: hold netdev instance lock during sysfs operations https://git.kernel.org/netdev/net-next/c/ad7c7b2172c3 - [net-next,v10,09/14] net: hold netdev instance lock during ndo_bpf https://git.kernel.org/netdev/net-next/c/97246d6d21c2 - [net-next,v10,10/14] net: ethtool: try to protect all callback with netdev instance lock https://git.kernel.org/netdev/net-next/c/2bcf4772e45a - [net-next,v10,11/14] net: replace dev_addr_sem with netdev instance lock https://git.kernel.org/netdev/net-next/c/df43d8bf1031 - [net-next,v10,12/14] net: add option to request netdev instance lock https://git.kernel.org/netdev/net-next/c/605ef7aec060 - [net-next,v10,13/14] docs: net: document new locking reality https://git.kernel.org/netdev/net-next/c/cc34acd577f1 - [net-next,v10,14/14] eth: bnxt: remove most dependencies on RTNL https://git.kernel.org/netdev/net-next/c/004b5008016a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html