netdevsim: check the driver is bound in new_port/del_port
Subsystems (3)
PATCH 1/1 netdevsim: check the driver is bound in new_port/del_port
new_port and del_port are attributes of the bus device, so they stay reachable after the driver is unbound. nsim_drv_remove() clears drvdata, yet both handlers pass it straight to priv_to_devlink() and devl_lock(), which faults on a small negative pointer:
BUG: unable to handle page fault for address: fffffffffffffeb0 RIP: 0010:__lock_acquire+0x41/0x2de0 kernel/locking/lockdep.c:5140 Call Trace: lock_acquire+0x115/0x350 mutex_lock_nested+0x55/0x1d0 nsim_drv_port_add+0x5a/0x140 drivers/net/netdevsim/dev.c:1760 new_port_store+0x1a5/0x280 drivers/net/netdevsim/bus.c:96 kernfs_fop_write_iter+0x3a5/0x540
The nsim_bus_dev->init check does not help: it is only cleared when the whole device is deleted, not on unbind.
Do what sriov_numvfs already does: take device_lock() so the driver cannot be unbound underneath us, and return -ENOENT if drvdata is NULL.
---
Reproduced in QEMU (x86_64, KASAN + lockdep) with the syzbot C repro:
the unpatched kernel oopses in nsim_drv_port_add() right after unbind. With the patch, the write to new_port returns -ENOENT, a 40s stress of bind/unbind + new_device/del_device racing with new_port/del_port/ sriov_numvfs runs clean (no splat, no lockdep report, no hung task), and new_port/del_port still work as before on a bound device.
drivers/net/netdevsim/bus.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/net/netdevsim/bus.c +14 -3
@@ -93,8 +93,13 @@ new_port_store(struct device *dev, struct device_attribute *attr,return -EINVAL ; } - ret =nsim_drv_port_add (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , port_index, - addr_set ? eth_addr :NULL ); +device_lock (dev); + ret = -ENOENT ; +if (dev_get_drvdata (dev)) + ret =nsim_drv_port_add (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , + port_index, addr_set ? eth_addr :NULL ); +device_unlock (dev); +return ret ? ret : count; }
@@ -115,7 +120,13 @@ del_port_store(struct device *dev, struct device_attribute *attr,if (ret)return ret; - ret =nsim_drv_port_del (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , port_index); +device_lock (dev); + ret = -ENOENT ; +if (dev_get_drvdata (dev)) + ret =nsim_drv_port_del (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , + port_index); +device_unlock (dev); +return ret ? ret : count; }
Signature · 1 line
2.43.0
Hi Nguyen,
Quoted 17 lines · Nguyen Ngoc Thang
new_portanddel_portare attributes of the bus device, so they stay reachable after the driver is unbound.nsim_drv_remove()clears drvdata, yet both handlers pass it straight topriv_to_devlink()anddevl_lock(), which faults on a small negative pointer:BUG: unable to handle page fault for address: fffffffffffffeb0 RIP: 0010:__lock_acquire+0x41/0x2de0 kernel/locking/lockdep.c:5140 Call Trace: lock_acquire+0x115/0x350 mutex_lock_nested+0x55/0x1d0 nsim_drv_port_add+0x5a/0x140 drivers/net/netdevsim/dev.c:1760 new_port_store+0x1a5/0x280 drivers/net/netdevsim/bus.c:96 kernfs_fop_write_iter+0x3a5/0x540The
nsim_bus_dev->initcheck does not help: it is only cleared when the whole device is deleted, not on unbind.
Quoted 8 lines
Do what
sriov_numvfsalready does: takedevice_lock()so the driver cannot be unbound underneath us, and return-ENOENTif drvdata is NULL.Fixes 794b2c05ca1c ("netdevsim: extend device attrs to support port addition and deletion")Reported-by [email protected]Signed-off-by Nguyen Ngoc Thang <[email protected]>
FYI, Jiacheng already posted a same fix for this issue.
https://lore.kernel.org/netdev/[email protected]/
Thanks
Hangbin
Quoted 48 lines
---
Reproduced in QEMU (x86_64, KASAN + lockdep) with the syzbot C repro:
the unpatched kernel oopses innsim_drv_port_add()right after unbind. With the patch, the write tonew_portreturns-ENOENT, a 40s stress of bind/unbind + new_device/del_device racing with new_port/del_port/sriov_numvfsruns clean (no splat, no lockdep report, no hung task), and new_port/del_port still work as before on a bound device.drivers/net/netdevsim/bus.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)drivers/net/netdevsim/bus.c +14
-3@@ -93,8 +93,13 @@ new_port_store(struct device *dev, struct device_attribute *attr,return -EINVAL ; } - ret =nsim_drv_port_add (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , port_index, - addr_set ? eth_addr :NULL ); +device_lock (dev); + ret = -ENOENT ; +if (dev_get_drvdata (dev)) + ret =nsim_drv_port_add (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , + port_index, addr_set ? eth_addr :NULL ); +device_unlock (dev); +return ret ? ret : count; }@@ -115,7 +120,13 @@ del_port_store(struct device *dev, struct device_attribute *attr,if (ret)return ret; - ret =nsim_drv_port_del (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , port_index); +device_lock (dev); + ret = -ENOENT ; +if (dev_get_drvdata (dev)) + ret =nsim_drv_port_del (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , + port_index); +device_unlock (dev); +return ret ? ret : count; }Signature · 1 line
2.43.0
Quoted 73 lines · Nguyen Ngoc Thang
new_portanddel_portare attributes of the bus device, so they stay reachable after the driver is unbound.nsim_drv_remove()clears drvdata, yet both handlers pass it straight topriv_to_devlink()anddevl_lock(), which faults on a small negative pointer:BUG: unable to handle page fault for address: fffffffffffffeb0 RIP: 0010:__lock_acquire+0x41/0x2de0 kernel/locking/lockdep.c:5140 Call Trace: lock_acquire+0x115/0x350 mutex_lock_nested+0x55/0x1d0 nsim_drv_port_add+0x5a/0x140 drivers/net/netdevsim/dev.c:1760 new_port_store+0x1a5/0x280 drivers/net/netdevsim/bus.c:96 kernfs_fop_write_iter+0x3a5/0x540The
nsim_bus_dev->initcheck does not help: it is only cleared when the whole device is deleted, not on unbind.Do what
sriov_numvfsalready does: takedevice_lock()so the driver cannot be unbound underneath us, and return-ENOENTif drvdata is NULL.Fixes 794b2c05ca1c ("netdevsim: extend device attrs to support port addition and deletion")Reported-by [email protected]Signed-off-by Nguyen Ngoc Thang <[email protected]>---
Reproduced in QEMU (x86_64, KASAN + lockdep) with the syzbot C repro:
the unpatched kernel oopses innsim_drv_port_add()right after unbind. With the patch, the write tonew_portreturns-ENOENT, a 40s stress of bind/unbind + new_device/del_device racing with new_port/del_port/sriov_numvfsruns clean (no splat, no lockdep report, no hung task), and new_port/del_port still work as before on a bound device.drivers/net/netdevsim/bus.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)drivers/net/netdevsim/bus.c +14
-3@@ -93,8 +93,13 @@ new_port_store(struct device *dev, struct device_attribute *attr,return -EINVAL ; } - ret =nsim_drv_port_add (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , port_index, - addr_set ? eth_addr :NULL ); +device_lock (dev); + ret = -ENOENT ; +if (dev_get_drvdata (dev)) + ret =nsim_drv_port_add (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , + port_index, addr_set ? eth_addr :NULL ); +device_unlock (dev); +return ret ? ret : count; }@@ -115,7 +120,13 @@ del_port_store(struct device *dev, struct device_attribute *attr,if (ret)return ret; - ret =nsim_drv_port_del (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , port_index); +device_lock (dev); + ret = -ENOENT ; +if (dev_get_drvdata (dev)) + ret =nsim_drv_port_del (nsim_bus_dev,NSIM_DEV_PORT_TYPE_PF , + port_index); +device_unlock (dev); +return ret ? ret : count; }Signature · 1 line
2.43.0
There are three identical fixes (author cc'ed) following Jiacheng’s patch [1]. I’m not sure whether there were others before that. I notified Jiacheng and Nguyen on Nov 2, but Jiacheng hasn’t reposted his patch. To prevent further duplicate fixes from landing on netdev, I’d prefer to ack Nguyen’s patch, since it is the earliest one that includes test information.
[1] https://lore.kernel.org/netdev/[email protected]/
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 14 lines · On Sun, 1 Nov 2026 20:58:24 +0700 you wrote:
new_portanddel_portare attributes of the bus device, so they stay reachable after the driver is unbound.nsim_drv_remove()clears drvdata, yet both handlers pass it straight topriv_to_devlink()anddevl_lock(), which faults on a small negative pointer:BUG: unable to handle page fault for address: fffffffffffffeb0 RIP: 0010:__lock_acquire+0x41/0x2de0 kernel/locking/lockdep.c:5140 Call Trace: lock_acquire+0x115/0x350 mutex_lock_nested+0x55/0x1d0 nsim_drv_port_add+0x5a/0x140 drivers/net/netdevsim/dev.c:1760 new_port_store+0x1a5/0x280 drivers/net/netdevsim/bus.c:96 kernfs_fop_write_iter+0x3a5/0x540
Quoted 1 line
[...]
Here is the summary with links:
- [net] netdevsim: check the driver is bound in new_port/del_port
https://git.kernel.org/netdev/net-next/c/98aaf4da98d2
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html