Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: hdthky0@gmail.com
Cc: willemdebruijn.kernel@gmail.com, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	horms@kernel.org, netdev@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH net] af_packet: avoid divide by zero in packet_pick_tx_queue()
Date: Fri, 04 Dec 2026 11:04:35 +0000	[thread overview]
Message-ID: <179638227552.3146.4201211237560745618@kernel.org> (raw)
In-Reply-To: <20261203100227.2266138-1-hdthky0@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 2 (1) · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The fix is incomplete.
- [High] The snapshot cannot close the race, because the functions called
  next read dev->real_num_tx_queues again.
- [Medium] The new snapshot `unsigned int real_num_tx_queues =
  dev->real_num_tx_queues;` (af_packet.c:316) is a plain, unannotated…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is a high-
  severity sibling of the race being fixed, and the patch leaves it…

--

af_packet: avoid divide by zero in packet_pick_tx_queue()

This commit avoids a divide by zero in packet_pick_tx_queue(). It happens
when a PACKET_QDISC_BYPASS sender races with device unregistration, because
remove_queue_kobjects() zeroes dev->real_num_tx_queues. When the count is
zero, the rx queue record is skipped and __dev_direct_xmit() is expected to
drop the packet.

> diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
> index e0ebd0627509f..d676a24bd192e 100644
> --- a/net/packet/af_packet.c
> +++ b/net/packet/af_packet.c
> @@ -313,13 +313,15 @@ static u16 packet_pick_tx_queue(struct sk_buff *skb)
>  {
>  	struct net_device *dev = skb->dev;
>  	const struct net_device_ops *ops = dev->netdev_ops;
> +	unsigned int real_num_tx_queues = dev->real_num_tx_queues;

[Severity: Medium]
Should this be READ_ONCE(dev->real_num_tx_queues)?

This is a lockless read. remove_queue_kobjects() writes the field at the
same time, under netdev_lock_ops(), which this path never takes:

net/core/net-sysfs.c:remove_queue_kobjects() {
    ...
	netdev_lock_ops(dev);
	dev->real_num_rx_queues = 0;
	dev->real_num_tx_queues = 0;
	netdev_unlock_ops(dev);
    ...
}

The fix needs the zero check and the modulo below to see the same value.
With a plain load, the compiler may drop the local copy and reload
dev->real_num_tx_queues for the divisor. KCSAN may also report this as a
data race.

Nearby TX selection code already annotates similar reads, for example
READ_ONCE(dev->num_tc) in skb_tx_hash(). A WRITE_ONCE() on the writer side
would pair with it.

>  	int cpu = raw_smp_processor_id();
>  	u16 queue_index;
>  
>  #ifdef CONFIG_XPS
>  	skb->sender_cpu = cpu + 1;
>  #endif
> -	skb_record_rx_queue(skb, cpu % dev->real_num_tx_queues);
> +	if (real_num_tx_queues)
> +		skb_record_rx_queue(skb, cpu % real_num_tx_queues);

[Severity: High]
Can the count still be zeroed after this snapshot is taken?

packet_pick_tx_queue() runs in preemptible context, without a lock or an
RCU read section. The functions it calls next read
dev->real_num_tx_queues again. Take a device without ndo_select_queue,
such as the veth device in the reported reproducer:

packet_xmit()
  packet_pick_tx_queue()
    real_num_tx_queues = dev->real_num_tx_queues;   /* non-zero */
    skb_record_rx_queue(skb, ...);
                                    remove_queue_kobjects()
                                      dev->real_num_tx_queues = 0;
    netdev_pick_tx()
      skb_tx_hash()

net/core/dev.c:skb_tx_hash() {
	u16 qcount = dev->real_num_tx_queues;
    ...
	if (skb_rx_queue_recorded(skb)) {
		DEBUG_NET_WARN_ON_ONCE(qcount == 0);
		hash = skb_get_rx_queue(skb);
		if (hash >= qoffset)
			hash -= qoffset;
		while (unlikely(hash >= qcount))
			hash -= qcount;
    ...
}

With an rx queue recorded and qcount == 0, does this loop ever end?

The subtraction loops in bond_select_queue(), team_select_queue(),
net_failover and cxgb_select_queue() have the same problem if the count
is zeroed after the record. The sender then never reaches dev_put() in
packet_snd() or tpacket_snd(), so unregister_netdevice() still waits
forever.

This window existed before the patch too. The snapshot narrows it and
turns the divide error into an endless loop. To close it, would a zero
count need to be handled where the value is re-read, or queue selection be
serialized against teardown?

>  	if (ops->ndo_select_queue) {
>  		queue_index = ops->ndo_select_queue(dev, skb, NULL);
>  		queue_index = netdev_cap_txqueue(dev, queue_index);

[Severity: High]
Is this fix complete for devices that implement ndo_select_queue()?

When real_num_tx_queues is zero, the rx queue record is now skipped.
However, the skb still goes to ops->ndo_select_queue(), and several
in-tree implementations divide by or loop on the same count. For example:

drivers/net/bonding/bond_main.c:bond_select_queue() {
	u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
    ...
	if (unlikely(txq >= dev->real_num_tx_queues)) {
		do {
			txq -= dev->real_num_tx_queues;
		} while (txq >= dev->real_num_tx_queues);
	}
    ...
}

With no rx queue recorded, txq is 0. The test is then 0 >= 0, so the loop
subtracts 0 forever. team_select_queue() has the same loop.

cxgb_select_queue() either spins in a similar while loop or does:

	return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues;

xenvif_select_queue() checks only vif->num_queues before doing:

		return netdev_pick_tx(dev, skb, NULL) %
		       dev->real_num_tx_queues;

Doesn't that give the same divide error?

Neither safeguard helps here. netdev_cap_txqueue() only clamps the value
that comes back. The netif_running() check in __dev_direct_xmit() runs
after queue selection. So the sender never reaches dev_put() in
packet_snd() or tpacket_snd(), and unregister_netdevice() still hangs in
netdev_wait_allrefs_any().

The commit message says:

    Skip the queue record when the count is zero. netdev_pick_tx() then
    falls back to queue 0 and __dev_direct_xmit() drops the packet because
    the device is no longer running.

That only holds for devices without ndo_select_queue(). Would it be better
to stop before any queue selection when the count is zero? For example,
this function could return early, or packet_xmit() could drop the packet.

[Severity: High]
This is a pre-existing issue and was not introduced by this patch. Does
the default path, without PACKET_QDISC_BYPASS, have the same problem?

packet_xmit()
  dev_queue_xmit()
    __dev_queue_xmit()      /* rcu_read_lock_bh() */
      netdev_core_pick_tx()

net/core/dev.c:netdev_core_pick_tx() {
    ...
	if (dev->real_num_tx_queues != 1) {
		const struct net_device_ops *ops = dev->netdev_ops;

		if (ops->ndo_select_queue)
			queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
    ...
}

A count of 0 passes the != 1 test. Suppose the same stalled copy race
happens in packet_snd() on a bond or team device that has already gone
through remove_queue_kobjects(). The sender then reaches
bond_select_queue() or team_select_queue() with txq == 0 and loops
forever.

On this path the loop runs with BH disabled, inside an RCU-bh read
section. Besides leaking the device reference and hanging the unregister,
would it also stall RCU grace periods?

The commit message and the fix only cover the bypass path.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203100227.2266138-1-hdthky0%40gmail.com

      parent reply	other threads:[~2026-12-04 11:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 10:02 [PATCH net] af_packet: avoid divide by zero in packet_pick_tx_queue() Xingyuan Mo
2026-12-03 10:03 ` netdev-bot+sinfo
2026-12-04 11:04 ` netdev-bot+sashiko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179638227552.3146.4201211237560745618@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=hdthky0@gmail.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=willemdebruijn.kernel@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox