From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 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== 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== 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 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) 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 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF2F01F00931; Fri, 4 Dec 2026 11:04:35 +0000 (UTC) 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== Subject: Re: [PATCH net] af_packet: avoid divide by zero in packet_pick_tx_queue() 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 Date: Fri, 04 Dec 2026 11:04:35 +0000 Message-ID: <179638227552.3146.4201211237560745618@kernel.org> In-Reply-To: <20261203100227.2266138-1-hdthky0@gmail.com> References: <20261203100227.2266138-1-hdthky0@gmail.com> X-sashiko-severity: High Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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