usbnet: Fix using smp_processor_id() in preemptible code warnings
PATCH 1/1 usbnet: Fix using smp_processor_id() in preemptible code warnings
Syzbot reported the following warning:
BUG: using smp_processor_id() in preemptible [00000000] code: dhcpcd/2879 caller is usbnet_skb_return+0x74/0x490 drivers/net/usb/usbnet.c:331 CPU: 1 UID: 0 PID: 2879 Comm: dhcpcd Not tainted 6.15.0-rc4-syzkaller-00098-g615dca38c2ea #0 PREEMPT(voluntary) Call Trace: <TASK> __dump_stack lib/dump_stack.c:94 [inline] dump_stack_lvl+0x16c/0x1f0 lib/dump_stack.c:120 check_preemption_disabled+0xd0/0xe0 lib/smp_processor_id.c:49 usbnet_skb_return+0x74/0x490 drivers/net/usb/usbnet.c:331 usbnet_resume_rx+0x4b/0x170 drivers/net/usb/usbnet.c:708 usbnet_change_mtu+0x1be/0x220 drivers/net/usb/usbnet.c:417 __dev_set_mtu net/core/dev.c:9443 [inline] netif_set_mtu_ext+0x369/0x5c0 net/core/dev.c:9496 netif_set_mtu+0xb0/0x160 net/core/dev.c:9520 dev_set_mtu+0xae/0x170 net/core/dev_api.c:247 dev_ifsioc+0xa31/0x18d0 net/core/dev_ioctl.c:572 dev_ioctl+0x223/0x10e0 net/core/dev_ioctl.c:821 sock_do_ioctl+0x19d/0x280 net/socket.c:1204 sock_ioctl+0x42f/0x6a0 net/socket.c:1311 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:906 [inline] __se_sys_ioctl fs/ioctl.c:892 [inline] __x64_sys_ioctl+0x190/0x200 fs/ioctl.c:892 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xcd/0x260 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f
The usbnet_skb_return() can be invoked in preemptible task-context, this commit therefore use get_cpu_ptr/put_cpu_ptr() instead of this_cpu_ptr() to get stats64 pointer.
--- drivers/net/usb/usbnet.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/usb/usbnet.c +3 -1
@@ -328,7 +328,7 @@ static void __usbnet_status_stop_force(struct usbnet *dev)*/ void usbnet_skb_return (struct usbnet *dev,struct sk_buff *skb) { -struct pcpu_sw_netstats *stats64 =this_cpu_ptr (dev->net->tstats); +struct pcpu_sw_netstats *stats64;unsigned long flags;int status;
@@ -341,10 +341,12 @@ void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)if (skb->protocol ==0 ) skb->protocol =eth_type_trans (skb, dev->net); + stats64 =get_cpu_ptr (dev->net->tstats); flags =u64_stats_update_begin_irqsave (&stats64->syncp);u64_stats_inc (&stats64->rx_packets);u64_stats_add (&stats64->rx_bytes, skb->len);u64_stats_update_end_irqrestore (&stats64->syncp, flags); +put_cpu_ptr (stats64);netif_dbg (dev, rx_status, dev->net,"< rx, len %zu, type 0x%x\n" , skb->len +sizeof (struct ethhdr ), skb->protocol);
Signature · 1 line
2.17.1
Jakub Kicinski
maintainerQuoted 3 lines · On Tue, 30 Sep 2025 16:46:36 +0800 Zqiang wrote:
The
usbnet_skb_return()can be invoked in preemptible task-context, this commit therefore use get_cpu_ptr/put_cpu_ptr() instead ofthis_cpu_ptr()to get stats64 pointer.
We also call netif_rx() which historically wanted to be in bh context. I think that disabling bh in usbnet_resume_rx() may be a better idea.
Signature · 1 line
pw-bot: cr
Quoted 10 lines · On Tue, 30 Sep 2025 16:46:36 +0800 Zqiang wrote:
Quoted 6 lines
The
usbnet_skb_return()can be invoked in preemptible task-context,
this commit therefore use get_cpu_ptr/put_cpu_ptr() instead ofthis_cpu_ptr()to get stats64 pointer.We also call
netif_rx()which historically wanted to be in bh context. I think that disabling bh inusbnet_resume_rx()may be a better idea.
Actually, the netif_rx() will disable bh internally when call from process context.
but the skb_defer_rx_timestamp() is usually called in the bh or interrupt context.
Anyway, I will disable bh in usbnet_resume_rx() and resend.
Thanks
Zqiang
Quoted 3 lines
Signature · 1 line
pw-bot: cr
Jakub Kicinski
maintainerQuoted 5 lines · On Wed, 01 Oct 2025 00:39:18 +0000 Zqiang wrote:
Quoted 2 lines
We also call
netif_rx()which historically wanted to be in bh context. I think that disabling bh inusbnet_resume_rx()may be a better idea.Actually, the
netif_rx()will disable bh internally when call from process context.
Sigh. I said "historically". Commit under Fixes is very old so the fix needs to be backported. Look thru the git history you'll see that netif_rx() did not disable BH when the bad commit was written.
Quoted 4 lines
but the
skb_defer_rx_timestamp()is usually called in the bh or interrupt context.Anyway, I will disable bh in
usbnet_resume_rx()and resend.
If you think you have better solution, please do not hesitate to propose.
Also make sure you read
https://www.kernel.org/doc/html/next/process/maintainer-netdev.html
if you haven't already.
Quoted 16 lines · On Wed, 01 Oct 2025 00:39:18 +0000 Zqiang wrote:
Quoted 8 lines
We also call
netif_rx()which historically wanted to be in bh context.
I think that disabling bh inusbnet_resume_rx()may be a better idea.Actually, the
netif_rx()will disable bh internally when call from process context.Sigh. I said "historically". Commit under Fixes is very old so the fix needs to be backported. Look thru the git history you'll see that
netif_rx()did not disable BH when the bad commit was written.Quoted 1 line
Quoted 7 lines
Quoted 5 lines
but the
skb_defer_rx_timestamp()is usually called in the bh or
interrupt context.Anyway, I will disable bh in
usbnet_resume_rx()and resend.If you think you have better solution, please do not hesitate to propose.
Maybe we can use netif_receive_skb() instead of skb_defer_rx_timestamp() and netif_rcv() in the usbnet_skb_return() and disbale bh in usbnet_resume_rx().
Any thoughts ?
Thanks
Zqiang
Quoted 5 lines
Also make sure you read
https://www.kernel.org/doc/html/next/process/maintainer-netdev.html
if you haven't already.