Netdev List
 help / color / mirror / Atom feed
From: Xingyuan Mo <hdthky0@gmail.com>
To: Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>
Cc: "open list:NETWORKING [GENERAL]" <netdev@vger.kernel.org>,
	Xingyuan Mo <hdthky0@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH net] af_packet: avoid divide by zero in packet_pick_tx_queue()
Date: Thu, 3 Dec 2026 18:02:32 +0800	[thread overview]
Message-ID: <20261203100227.2266138-1-hdthky0@gmail.com> (raw)

packet_pick_tx_queue() records cpu % dev->real_num_tx_queues as the
receive queue of the skb so that the queue selection done by
netdev_pick_tx() for PACKET_QDISC_BYPASS sends lands on the tx queue
of the current cpu. real_num_tx_queues is zero while the device is
being unregistered: remove_queue_kobjects() clears it before
netdev_run_todo() waits for the last references to go away, and a
packet socket bound to the device holds one of them.

packet_snd() only checks IFF_UP before copying the payload from user
space, so a sender that is descheduled inside that copy, for example
on a userfaultfd page, resumes after the unregistering task has
already cleared the flag, unlisted the device and zeroed the queue
counts. packet_xmit() then calls packet_pick_tx_queue(), which divides
by the zeroed count before __dev_direct_xmit() gets a chance to reject
the packet with its netif_running() check. The oopsing task also
skips the dev_put() in packet_snd(), so the unregistering task spins
in netdev_wait_allrefs_any() forever.

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.

Oops:
Oops: divide error: 0000 [#2] SMP KASAN PTI
CPU: 1 UID: 65534 PID: 769 Comm: poc-c11 Tainted: G      D W           7.4.0-rc4-00571-g9437ee8b6cf6 #1 PREEMPT(lazy)
Tainted: [D]=DIE, [W]=WARN
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
RIP: 0010:packet_xmit+0x9c/0x1a0
Code: 24 9c 00 00 00 4d 8d 7d 28 e8 30 f8 86 fe 8d 43 01 4c 89 ff 41 89 84 24 9c 00 00 00 e8 6d f7 86 fe 89 d8 31 d2 49 8d 7c 24 7c <41> f7 75 28 8d 5a 01 e8 98 f6 86 fe 66 41 89 5c 24 7c 49 8d 7e 30
RSP: 0018:ffff8880153dfae0 EFLAGS: 00010246
RAX: 0000000000000001 RBX: 0000000000000001 RCX: dffffc0000000000
RDX: 0000000000000000 RSI: 0000000000000008 RDI: ffff88800b15143c
RBP: ffff8880153dfb10 R08: ffffffff82f073d3 R09: ffffed1002b3e512
R10: ffff8880159f2897 R11: 000000000000000c R12: ffff88800b1513c0
R13: ffff888008bf0000 R14: ffffffff83919ce0 R15: ffff888008bf0028
FS:  00007f01e31176c0(0000) GS:ffff8880e7a30000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fe828507200 CR3: 00000000097a4000 CR4: 00000000000006f0
Call Trace:
 <TASK>
 packet_sendmsg+0x139c/0x2510
 ? percpu_counter_add_batch+0xb4/0x190
 ? rcu_is_watching+0x25/0x60
 ? __pfx_packet_sendmsg+0x10/0x10
 ? lock_release+0x1dc/0x280
 ? __rcu_read_unlock+0x7a/0x320
 ? selinux_socket_sendmsg+0x54/0x110
 __sys_sendto+0x384/0x3a0
 ? __pfx___sys_sendto+0x10/0x10
 ? sigprocmask+0x186/0x1e0
 ? __pfx_sigprocmask+0x10/0x10
 ? __kasan_check_write+0x18/0x20
 ? __x64_sys_rt_sigprocmask+0x108/0x190
 ? __pfx___x64_sys_rt_sigprocmask+0x10/0x10
 ? __kasan_check_write+0x18/0x20
 __x64_sys_sendto+0x84/0xa0
 x64_sys_call+0xf18/0x21d0
 do_syscall_64+0x11a/0x620
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f01e3225ba4
Code: 89 4c 24 1c e8 9d ac f7 ff 44 8b 54 24 1c 8b 3c 24 45 31 c9 89 c5 48 8b 54 24 10 48 8b 74 24 08 45 31 c0 b8 2c 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 34 89 ef 48 89 04 24 e8 e9 ac f7 ff 48 8b 04
RSP: 002b:00007f01e3116e90 EFLAGS: 00000246 ORIG_RAX: 000000000000002c
RAX: ffffffffffffffda RBX: 00007f01e31176c0 RCX: 00007f01e3225ba4
RDX: 000000000000003c RSI: 00007f01e3300000 RDI: 0000000000000003
RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: ffffffffffffff80
R13: 0000000000000000 R14: 00007ffc7610d810 R15: 00007f01e2917000
 </TASK>
Modules linked in:
---[ end trace 0000000000000000 ]---
RIP: 0010:packet_xmit+0x9c/0x1a0
Code: 24 9c 00 00 00 4d 8d 7d 28 e8 30 f8 86 fe 8d 43 01 4c 89 ff 41 89 84 24 9c 00 00 00 e8 6d f7 86 fe 89 d8 31 d2 49 8d 7c 24 7c <41> f7 75 28 8d 5a 01 e8 98 f6 86 fe 66 41 89 5c 24 7c 49 8d 7e 30
RSP: 0018:ffff88800cf17ae0 EFLAGS: 00010246
RAX: 0000000000000001 RBX: 0000000000000001 RCX: dffffc0000000000
RDX: 0000000000000000 RSI: 0000000000000008 RDI: ffff88800a80c43c
RBP: ffff88800cf17b10 R08: ffffffff82f073d3 R09: ffffed1003a38912
R10: ffff88801d1c4897 R11: 0000000000000001 R12: ffff88800a80c3c0
R13: ffff88800d61c000 R14: ffffffff83919ce0 R15: ffff88800d61c028
FS:  00007f01e31176c0(0000) GS:ffff8880e7a30000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fe828507200 CR3: 00000000097a4000 CR4: 00000000000006f0
unregister_netdevice: waiting for v91a to become free. Usage count = 2
unregister_netdevice: waiting for v91a to become free. Usage count = 2

Fixes: d7dac083414e ("net-sysfs: update the queue counts in the unregistration path")
Cc: stable@vger.kernel.org
Signed-off-by: Xingyuan Mo <hdthky0@gmail.com>
---
 net/packet/af_packet.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

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;
 	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);
 	if (ops->ndo_select_queue) {
 		queue_index = ops->ndo_select_queue(dev, skb, NULL);
 		queue_index = netdev_cap_txqueue(dev, queue_index);
-- 
2.43.0


             reply	other threads:[~2026-12-03 10:02 UTC|newest]

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

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=20261203100227.2266138-1-hdthky0@gmail.com \
    --to=hdthky0@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --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