🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #26894

Re: [PATCH net] af_packet: avoid divide by zero in packet_pick_tx_queue()

#26894
From: netdev-bot+sashiko
Date: Fri, 04 Dec 2026 11:04
204 lines
15235 bytes
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
Technical Details
Message-ID: <179638227552.3146.4201211237560745618@kernel.org>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377277-linux-kernel-netdev=dewy.ru
References: <20261203100227.2266138-1-hdthky0@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377277-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net] af_packet: avoid divide by zero in packet_pick_tx_queue()
Date: Fri, 04 Dec 2026 11:04:35 +0000
Organization: Usenet Network
Message-ID: <179638227552.3146.4201211237560745618@kernel.org>
References: <20261203100227.2266138-1-hdthky0@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network; logging-data="2747041"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:giZ4yg7a8SawCwDulLFU7sNIRSA= sha256:7jyUbytAPfjRcEhXOd4r6TKf3HJuaFL6xJVna64CX6A= sha1:9HGyl5xTKSbGgLn8OqkS3KsQ4VA= sha256:5y+V31y/qCmqQzFuiKAsMMZTqwa6YGGDYWP8puWe+Lw=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 63DA0117BBD; Fri,  4 Dec 2026 14:05:02 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.541
X-Spam-Status: No, score=-3.541 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.142, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id G0aeW8Mo6L22; Fri,  4 Dec 2026 14:04:58 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-377277-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id C7EBF117BA8; Fri,  4 Dec 2026 14:04:49 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id B004427839; Fri,  4 Dec 2026 11:04:37 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796382277; b=AMxx75vP9f7iBrWk3F7l+BDxsKQav6N0rDFrYxxW+qA65FlmVSxZ+0Eq8LWDe9JSc3ll 67k22KQH9bVl/R/eTEhr5LLvrGKYhkH5EZzB7gt9wDuA0QyTj6XOH5Zokb383RnxjwAC0 bs63LBhyi+Rr69ie9cjVUV8vf0OZy8fr+i+ajOycdbsjaKomnKLCbIIqsBIlf4/kv0QHY Bmj/ELrd/kkIjF95wA8RgAyv1C3bz7kxq9Lvjo1gWkMnVluDGFfrkWnkmHYxOmAq2LJW1 FwWVpbHAmeaFXfnw5TQmt3Btu1qpLegK71UEAuYz8YvV40Spe44R99xCWAx9iKBkJGw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382277; h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc: Date:Message-ID:In-Reply-To:References:X-sashiko-severity: Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version; bh=U/nWJmPzxIEspPQysjBWFiml1iJyZJfoORcz8p9qDCM=; b=sHatkrphzpQ61T8aYr8JKw8Va0ngWyKeeOq2lhKNMfedZXxKbrSnhejJcoiqOHJ0JrTN sNauezAn8NMH3x5IlEmlxWjOOmU8xkglKm5/6s28lhU7iVJyq1KQnYVsiSrky+juMvSTT 36UXkSQuRTEpDPoAUkbSSoC8RQvLPIZ6nDBFtxXcTOuCdMClEjqlqKKwQCfa4bs/RPCzd VFJQkaMJYquoTLpLBsYB3887z6pd9exfkbQlgX92JJ42mIJqdcR7eQIqrtbaaG1/rAhYI yv6DchyhQabNX02dX//lEbGw06BuqnmkF3IkIYtohg3M3fR+21eJqeNC9IjCIUKMqwg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="oMysxQ/h"; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 88BE1606A333; Fri,  4 Dec 2026 11:04:37 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=oMysxQ/h
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382276; b=2nl4LErHZdMSnO57OyG6kzYsRloNaXEJSQLfqHOhWRQM94Hp90JbP2QHgbwxQmgUFEMR g13G2Af1LQpNvkEO9AmulxTpjDcihXH9cCje4PRLY68uHEeW66uwdPm+fD7cY9Ze2MiF8 D7ldqcA4pQu/t1+SG4N9d4oy1K08nCpIOoPjnBuLYL7jquto9NQB8+azZtlvt9UQCs+oT g0TNxq5+xmQefvNfqEXWxRLJJTV8xjWgqlkuucoWA16Y3QtFRxQhe1dcAEs4c4Cgfy+IV ri4NM59oMbCJ+KxFbPUztvukUyV1s9DFLoVvZJkumnZUlwy8hMfVTIkpeSHOWNggz8w==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382276; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=U/nWJmPzxIEspPQysjBWFiml1iJyZJfoORcz8p9qDCM=; b=wdZVEl7ezXS3UysxoqaioxOsEGUHwEC9ipUxgQyxZhjjB0BDEvVWjRFoklxcgGEfhs46 g53JMX7OIx4iZ5948OO86v5aDIktCIGLtTACu6oyCZVkWVY/Cf8ujKD+Sr7r1yfwFsOYt zp+64XC8BLy6jcsZgGV93JymAA2Br9WJX4C4E0XN7xOqdWeXoZWWIn0XaJyhv3PVPrCJN BJpVtXB9jL6hx2P9oMoHBXWiHyRAuufsEyTny6xGSMAHgVZ3zdSIbNn+pdr1WsIKOCt90 8l0fktk1WcUCtunIIQBc+tWpfYFa6NyXTvSujrOo79X5sMeG58NGWdnZCfkPP4wKSRw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="oMysxQ/h"; arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABC6D606A337; Fri,  4 Dec 2026 11:04:36 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF2F01F00931; Fri,  4 Dec 2026 11:04:35 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796382276; bh=U/nWJmPzxIEspPQysjBWFiml1iJyZJfoORcz8p9qDCM=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=oMysxQ/hlnNlC5gIujBXBFFIjY1Oo62X9J+XBd0U4tk6XCrKBpj2H+oQFdXcUqSW1 SlCnxpRlfncSQnaU5a4oE0lt+SUc1bSXIMKcYmI1sdzOncCrbmsK9DJJR4LvumPS52 F8jxeWVKmmDZg3VRhc+J/MlsvVhrV3ozsSc7SkB2SOsAlqe/zDZvx18k6SqH16kMlg 38jbQEuFZcGbHLAwmp5tjbYpaARiSOnMK7ViFb56rOyKqtdK8Qibd/sgjMv+/eK/rB CV9zP8nVPIq92B26ugj/SbACML10BrkKJwfq2ICexAOpA8RFwGvAN8ozbkOH3lQ5cf 23Jx0vaR7s0Ig==
X-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
X-In-Reply-To: <20261203100227.2266138-1-hdthky0@gmail.com>
X-sashiko-severity: High
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list