net: Provide a PREEMPT_RT specific check for netdev_queue::_xmit_lock
Subsystems (3)
Sebastian Andrzej Siewior
maintainerPATCH 1/1 net: Provide a PREEMPT_RT specific check for netdev_queue::_xmit_lock
After acquiring netdev_queue::_xmit_lock the number of the CPU owning the lock is recorded in netdev_queue::xmit_lock_owner. This works as long as the BH context is not preemptible.
On PREEMPT_RT the softirq context is preemptible and without the softirq-lock it is possible to have multiple user in __dev_queue_xmit() submitting a skb on the same CPU. This is fine in general but this means also that the current CPU is recorded as netdev_queue::xmit_lock_owner. This in turn leads to the recursion alert and the skb is dropped.
Instead checking the for CPU number, that owns the lock, PREEMPT_RT can check if the lockowner matches the current task.
Add netif_tx_owned() which returns true if the current context owns the lock by comparing the provided CPU number with the recorded number. This resembles the current check by negating the condition (the current check returns true if the lock is not owned).
On PREEMPT_RT use rt_mutex_owner() to return the lock owner and compare the current task against it.
Use the new helper in __dev_queue_xmit() and netif_local_xmit_active() which provides a similar check.
Update comments regarding pairing READ_ONCE().
--- include/linux/netdevice.h | 27 ++++++++++++++++++++++----- net/core/dev.c | 5 +---- net/core/netpoll.c | 2 +- 3 files changed, 24 insertions(+), 10 deletions(-)
include/linux/netdevice.h +22 -5
@@ -4711,7 +4711,7 @@ static inline u32 netif_msg_init(int debug_value, int default_msg_enable_bits)static inline void __netif_tx_lock (struct netdev_queue *txq,int cpu) {spin_lock (&txq->_xmit_lock); -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner, cpu); }
@@ -4729,7 +4729,7 @@ static inline void __netif_tx_release(struct netdev_queue *txq)static inline void __netif_tx_lock_bh (struct netdev_queue *txq) {spin_lock_bh (&txq->_xmit_lock); -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner,smp_processor_id ()); }
@@ -4738,7 +4738,7 @@ static inline bool __netif_tx_trylock(struct netdev_queue *txq)bool ok =spin_trylock (&txq->_xmit_lock);if (likely (ok)) { -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner,smp_processor_id ()); }return ok;
@@ -4746,14 +4746,14 @@ static inline bool __netif_tx_trylock(struct netdev_queue *txq)static inline void __netif_tx_unlock (struct netdev_queue *txq) { -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner,-1 );spin_unlock (&txq->_xmit_lock); }static inline void __netif_tx_unlock_bh (struct netdev_queue *txq) { -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner,-1 );spin_unlock_bh (&txq->_xmit_lock); }
@@ -4846,6 +4846,23 @@ static inline void netif_tx_disable(struct net_device *dev)local_bh_enable (); } +#ifndef CONFIG_PREEMPT_RT +static inline bool netif_tx_owned (struct netdev_queue *txq,unsigned int cpu) +{ +/* Other cpus might concurrently change txq->xmit_lock_owner +* to -1 or to their cpu id, but not to our id. +*/ +return READ_ONCE (txq->xmit_lock_owner) == cpu; +} + +#else +static inline bool netif_tx_owned (struct netdev_queue *txq,unsigned int cpu) +{ +return rt_mutex_owner (&txq->_xmit_lock.lock) == current; +} + +#endif +static inline void netif_addr_lock (struct net_device *dev) {unsigned char nest_level =0 ;
net/core/dev.c +1 -4
@@ -4818,10 +4818,7 @@ int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)if (dev->flags &IFF_UP ) {int cpu =smp_processor_id ();/* ok because BHs are off */ -/* Other cpus might concurrently change txq->xmit_lock_owner -* to -1 or to their cpu id, but not to our id. -*/ -if (READ_ONCE (txq->xmit_lock_owner) != cpu) { +if (!netif_tx_owned (txq, cpu)) {bool is_list = false;if (dev_xmit_recursion ())
net/core/netpoll.c +1 -1
@@ -132,7 +132,7 @@ static int netif_local_xmit_active(struct net_device *dev)for (i =0 ; i < dev->num_tx_queues; i++) {struct netdev_queue *txq =netdev_get_tx_queue (dev, i); -if (READ_ONCE (txq->xmit_lock_owner) ==smp_processor_id ()) +if (netif_tx_owned (txq,smp_processor_id ()))return 1 ; }
Signature · 1 line
2.53.0
RE: -EXT-[PATCH net] net: Provide a PREEMPT_RT specific check for netdev_queue::_xmit_lock
Please remove from list
Thanks,
David
-----Original Message-----
From: Sebastian Andrzej Siewior <[email protected]>
Sent: Monday, March 2, 2026 9:27 AM
To: [email protected]; [email protected]
Cc: David S. Miller <[email protected]>; Andrew Lunn <[email protected]>; Bert Karwatzki <[email protected]>; Clark Williams <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Simon Horman <[email protected]>; Steven Rostedt <[email protected]>
Subject: -EXT-[PATCH net] net: Provide a PREEMPT_RT specific check for netdev_queue::_xmit_lock
WARNING: This message is from an external source. Evaluate the message carefully BEFORE clicking on links or opening attachments.
After acquiring netdev_queue::_xmit_lock the number of the CPU owning the lock is recorded in netdev_queue::xmit_lock_owner. This works as long as the BH context is not preemptible.
On PREEMPT_RT the softirq context is preemptible and without the softirq-lock it is possible to have multiple user in __dev_queue_xmit() submitting a skb on the same CPU. This is fine in general but this means also that the current CPU is recorded as netdev_queue::xmit_lock_owner. This in turn leads to the recursion alert and the skb is dropped.
Instead checking the for CPU number, that owns the lock, PREEMPT_RT can check if the lockowner matches the current task.
Add netif_tx_owned() which returns true if the current context owns the lock by comparing the provided CPU number with the recorded number. This resembles the current check by negating the condition (the current check returns true if the lock is not owned). On PREEMPT_RT use rt_mutex_owner() to return the lock owner and compare the current task against it.
Use the new helper in __dev_queue_xmit() and netif_local_xmit_active() which provides a similar check.
Update comments regarding pairing READ_ONCE().
--- include/linux/netdevice.h | 27 ++++++++++++++++++++++----- net/core/dev.c | 5 +---- net/core/netpoll.c | 2 +- 3 files changed, 24 insertions(+), 10 deletions(-)
include/linux/netdevice.h +5 -5
@@ -4711,7 +4711,7 @@ static inline u32 netif_msg_init(int debug_value, int default_msg_enable_bits) static inline void __netif_tx_lock(struct netdev_queue *txq, int cpu) {spin_lock (&txq->_xmit_lock); -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner, cpu); }
@@ -4729,7 +4729,7 @@ static inline void __netif_tx_release(struct netdev_queue *txq) static inline void __netif_tx_lock_bh(struct netdev_queue *txq) {spin_lock_bh (&txq->_xmit_lock); -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner,smp_processor_id ()); }
@@ -4738,7 +4738,7 @@ static inline bool __netif_tx_trylock(struct netdev_queue *txq)bool ok =spin_trylock (&txq->_xmit_lock);if (likely (ok)) { -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner,smp_processor_id ()); }return ok;
@@ -4746,14 +4746,14 @@ static inline bool __netif_tx_trylock(struct netdev_queue *txq)static inline void __netif_tx_unlock (struct netdev_queue *txq) { -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner,-1 );spin_unlock (&txq->_xmit_lock); }static inline void __netif_tx_unlock_bh (struct netdev_queue *txq) { -/* Pairs with READ_ONCE() in __dev_queue_xmit() */ +/* Pairs with READ_ONCE() in netif_tx_owned() */ WRITE_ONCE (txq->xmit_lock_owner,-1 ); spin_unlock_bh(&txq->_xmit_lock); } @@ -4846,6 +4846,23 @@ static inline void netif_tx_disable(struct net_device *dev) local_bh_enable(); }
+#ifndef CONFIG_PREEMPT_RT
+static inline bool netif_tx_owned(struct netdev_queue *txq, unsigned +int cpu) {
+ /* Other cpus might concurrently change txq->xmit_lock_owner
+ * to -1 or to their cpu id, but not to our id.
+ */
+ return READ_ONCE(txq->xmit_lock_owner) == cpu; }
+
+#else
+static inline bool netif_tx_owned(struct netdev_queue *txq, unsigned +int cpu) {
+ return rt_mutex_owner(&txq->_xmit_lock.lock) == current; }
+
+#endif
+
static inline void netif_addr_lock(struct net_device *dev) {
unsigned char nest_level = 0;net/core/dev.c +1 -4
@@ -4818,10 +4818,7 @@ int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)if (dev->flags &IFF_UP ) {int cpu =smp_processor_id ();/* ok because BHs are off */ -/* Other cpus might concurrently change txq->xmit_lock_owner -* to -1 or to their cpu id, but not to our id. -*/ -if (READ_ONCE (txq->xmit_lock_owner) != cpu) { +if (!netif_tx_owned (txq, cpu)) {bool is_list = false; if (dev_xmit_recursion()) diff --git a/net/core/netpoll.c b/net/core/netpoll.c index a8558a52884f9..cd74beffd209c 100644
net/core/netpoll.c +1 -1
@@ -132,7 +132,7 @@ static int netif_local_xmit_active(struct net_device *dev)for (i =0 ; i < dev->num_tx_queues; i++) {struct netdev_queue *txq =netdev_get_tx_queue (dev, i); -if (READ_ONCE (txq->xmit_lock_owner) ==smp_processor_id ()) +if (netif_tx_owned (txq,smp_processor_id ()))return 1 ; }
--
2.53.0
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 11 lines · On Mon, 2 Mar 2026 17:26:31 +0100 you wrote:
After acquiring
netdev_queue::_xmit_lockthe number of the CPU owning the lock is recorded innetdev_queue::xmit_lock_owner. This works as long as the BH context is not preemptible.On
PREEMPT_RTthe softirq context is preemptible and without the softirq-lock it is possible to have multiple user in__dev_queue_xmit()submitting a skb on the same CPU. This is fine in general but this means also that the current CPU is recorded asnetdev_queue::xmit_lock_owner. This in turn leads to the recursion alert and the skb is dropped.[...]
Here is the summary with links:
- [net] net: Provide a PREEMPT_RT specific check for netdev_queue::_xmit_lock
https://git.kernel.org/netdev/net/c/b824c3e16c19
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html