From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.227.135 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796292162; b=qPDHehbuMupXOSYKJjDrcQMx3h9pIM4X72VSNTvcQ7QTl9yeHknNerHHm1bMxYTrdsXK 6nG2SELYdtfQO4ORcheg2f9OzDPB40F4FA/Fny5G5TyhS706QQPWH2l3VbkrR/Ed2Y4k4 hqPra4Waeijrk4aP0A8gSZ6NtvPuyVFWf2D/s4xF5UmFPvLO4yW2OL9qgwHPa2ajmCiEF h9B7J8VcOkEwfDRfu8Sd7Dp86NENEdtFUG840TngTWec/LVj7EI1VLI6dXndBrovtb2mY aj+nVmno+XHChtzfAZip9l26Xryv1FPmlQcR3dgcwWC1xE6Xr3DA/vlhiAjHo6eu8Pg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796292162; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Date:From:To:Cc: Subject:Message-ID:MIME-Version:Content-Type:Content-Disposition: X-Mailer; bh=SDDqF1/QqjY6TRhQKovS4MmSGeUEzJqP+H++7MIJT8k=; b=EZWnJd9nw1CxoQR2uKCisxpjbCXsLVXBg/4OaIc/rzqfEaTluNWFpd3ER/qXUpd3FHbb 07cb/0rJwOaQXU4jXtzNun81oNf2e+zio++vR3DS9TekC2JkCZGNHYJoEmaRPU86JOS2m YhMtWf67FoikMKpppbMb2flB0KonmiHvIgpV5358OX8HdNsn46q0BaSSjNuEnP+T5+90q D/SvsWD8Yl6rO/J37TnzZ5H5+y0u1T68sD1xXulFVRN6K+0kglgb8yCSb6zLw4DmMPh0J dUZYa1GZWqAtG/9uEHuLMIiCleBNGq+1DZn9OUNEMRgPDkiKaR9xQjiUnHofBHddAUQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SrjmgdPk; arc=none smtp.remote-ip=74.125.227.135 Received: from mail-pj2-f7.google.com (mail-pj2-f7.google.com [74.125.227.135]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1851B606A32F for ; Thu, 3 Dec 2026 10:02:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1851B606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SrjmgdPk Received: by mail-pj2-f7.google.com with SMTP id d9443c01a7336-2f1900fdf04so2956905ad.0 for ; Thu, 03 Dec 2026 02:02:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796292161; x=1796896961; darn=vger.kernel.org; h=content-disposition:content-type:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=SDDqF1/QqjY6TRhQKovS4MmSGeUEzJqP+H++7MIJT8k=; b=SrjmgdPkB7210+S/eCNiC58Rdf47bwsL6vkGR8epsnGY7TUBXdSUWfzOqMed8nCGFa Xf6Fyx4aEa0QAvanFWyrbx2menPaySK0pbiPP2ZxyXLM34xohdu9c4Zg7C2+zhMdKReD n2Fy4orgsUjxuG2cKA97h3Cdl0piB1oeVXBeTwJicRuYRVeEoCvUz10zoBPBEoueXDad BFKmkukc7X+A7wOGjMXfUp/jHtjJVMwGJb3KgMU56LmU8IVj5UWlw1zXlwlWcCtSGwRi fXwnUJKTIaAFv7YBOHFHZk+fXVL1jB2sRm5owBy7YwMQ8P418JgQjEAgk1avnF/SKs/r L+hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796292161; x=1796896961; h=content-disposition:content-type:mime-version:message-id:subject:cc :to:from:date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=SDDqF1/QqjY6TRhQKovS4MmSGeUEzJqP+H++7MIJT8k=; b=HZUuksdiAD8+Gu8hYwZHA3Fn/ZVJ6A2asSH4NKz0QW0of1nH+gvV6ymTP+jgNY7qzG QEx1HIFvW8kdkNQJ7rNpg7ISjrbub96h108kTODg5cS8gxaAnkeAT1jVO1qr6nQF8r56 mqiRxJ3ELPw8mmK3M6RP2P5mqKtgN5rRJA4s2R3j5LmoYSjLOSwFh81JXhSQ522MDGBG Ujzn0Znl3tVkayx5ssqgn6joqZRI7vXC0ASFsJIsrFXJL1lLDYLA6AiS/+DORON4Y4Ql BpyL50yguaOq+ZEY1LyLEdtPGmT9MBKpxvq8hGQmtwm9zptkYz7CPSh0j7CBsANVk+0/ o51g== X-Gm-Message-State: APuU4Z+zoMOeUa915Qc91NdYlyp1x7/P4Fj3krpcb86jzFsirsZbB5DE Lh5yDEvd+LYSL00FCYvUY6zHIi110uf9G4QuFxxi0BeE3bDJAs13S70fhjVlUKNbPJYBBPlr X-Gm-Gg: AZFdTZcYwOEymuNiCn+Lpk5u2mU4iuSV1dHJLlH5AXogVeQgR9c+xASG7EeD4DUAy1e 6NUZdUrmehvHbuYQKPp6PBTopIfnb9uhgp/rawKkbQfQZjT5o6lXpUTr8ZowvV60MemDe6OGi/f i5poUpSevx5SqylEJOYY3whcc2QOtOUiVjPQ+R8TpSGTjJnQVx3jLws+DLeVnB3L37lC27Ro2CB q6agJDDsrQ06DRfzyqBRr8oUfiPjB/WoDI2EBCzHthkiwCGMVbs0JuV4u6iF3j0oxA3uxZ/n4HM Kgi4N7RUnZEEwI2/V3nTXxkb/1g1CUL9bo/aer0hP/HMYzHw1UOfhw1Kgt6Uag8TEB5jnp5sLdt M297rJ1+5bAVIc4ou//fhv6uJRCX4aEBytCgs3lhahn75kI7n4in/zn9wkDGUl9J13ogJhWywQ5 pswpCzjBKBYmd1L8qz1BW4vXeT3Ld2fb33w7tc1k9qXAxdnaV5h6dLWBvbpp9L8bOrZc7l9lnKH fYSPTGQpnGW6uuFMLfUEXDSv9VF56z1MhNzvfRwtFhwjRlbgqfnzWtdi1v2cnxDaZjNR5FUBUmQ ZjnJfgFboq9BS9YGWrA= X-Received: by 2002:a05:6a20:d52f:b0:3fa:84a8:fad2 with SMTP id adf61e73a8af0-4014c94c9demr45164133637.71.1796292161289; Thu, 03 Dec 2026 02:02:41 -0800 (PST) Received: from hhost ([120.231.212.214]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cde6cfc80d4sm24081292a12.6.2026.12.03.02.02.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 02:02:40 -0800 (PST) Date: Thu, 3 Dec 2026 18:02:32 +0800 From: Xingyuan Mo To: Willem de Bruijn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman Cc: "open list:NETWORKING [GENERAL]" , Xingyuan Mo , stable@vger.kernel.org Subject: [PATCH net] af_packet: avoid divide by zero in packet_pick_tx_queue() Message-ID: <20261203100227.2266138-1-hdthky0@gmail.com> 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 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Mailer: git-send-email 2.43.0 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: 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 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 --- 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