[PATCH net v3 1/1] netdevsim: fix the inverted queue count check in flow control

From: Kyuwon Cho

Date: Mon Nov 30 2026 - 00:57:07 EST


nsim_start_xmit() maps the TX queues of the sending device onto the RX
queues of its peer, and flow control relies on one TX queue per RX
queue: nsim_stop_tx_queue() stops TX queue N when RX queue N is full,
and nsim_start_peer_tx_queue() wakes TX queue N after polling RX queue
N. Both skip this unless the queue counts match, but they compare the
wrong pair, the RX device's real_num_tx_queues with the TX device's
num_rx_queues.

A device with 8 queues whose channels were reduced to 1 with ethtool -L
passes this check once it is relinked to a peer with a single queue. If
one of its upper RX queues still holds traffic from the old peer,
nsim_start_peer_tx_queue() wakes a TX queue that the peer does not have:

BUG: KASAN: slab-out-of-bounds in nsim_poll (include/linux/netdevice.h:3834 drivers/net/netdevsim/netdev.c:60)
Read of size 8
Call Trace:
<IRQ>
nsim_poll (drivers/net/netdevsim/netdev.c:409)
__napi_poll.constprop.0 (net/core/dev.c:7997)
net_rx_action (net/core/dev.c:8217)

Compare the TX device's real_num_tx_queues with the RX device's
num_rx_queues in both places. The polled RX queue index is below the RX
device's num_rx_queues, so the queue woken is then always inside the
peer's TX queue array. nsim_set_channels() can change
real_num_tx_queues of the other device at the same time, so read it
with READ_ONCE().

Fixes: ff2ac4df58ad ("netdevsim: implement peer queue flow control")
Suggested-by: Willem de Bruijn <willemb@xxxxxxxxxx>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Kyuwon Cho <kyuwoncho18@xxxxxxxxx>
---
drivers/net/netdevsim/netdev.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c
index eebc02ccc4a9..b0c4c00577e9 100644
--- a/drivers/net/netdevsim/netdev.c
+++ b/drivers/net/netdevsim/netdev.c
@@ -53,7 +53,7 @@ static void nsim_start_peer_tx_queue(struct net_device *dev, struct nsim_rq *rq)

/* TX device */
peer_dev = peer_ns->netdev;
- if (dev->real_num_tx_queues != peer_dev->num_rx_queues)
+ if (READ_ONCE(peer_dev->real_num_tx_queues) != dev->num_rx_queues)
goto out;

txq = netdev_get_tx_queue(peer_dev, idx);
@@ -73,7 +73,7 @@ static void nsim_stop_tx_queue(struct net_device *tx_dev,
/* If different queues size, do not stop, since it is not
* easy to find which TX queue is mapped here
*/
- if (rx_dev->real_num_tx_queues != tx_dev->num_rx_queues)
+ if (READ_ONCE(tx_dev->real_num_tx_queues) != rx_dev->num_rx_queues)
return;

/* rq is the queue on the receive side */
--
2.43.0