Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
netdevsim: fix race conditions in netdevsim operations
This patch fixes a several locking problem. 1. netdevsim basic operations(new_device, del_device, new_port, and del_port) are called with sysfs. These operations use the same resource so they should acquire a lock for the whole resource not only for a list. 2. devices are managed by nsim_bus_dev_list. and all devices are deleted in the __exit() routine. After delete routine, new_device() and del_device() should be disallowed. So, the global flag variable 'enable' is added. 3. new_port() and del_port() would be called before resources are allocated or initialized. If so, panic will occur. In order to avoid this scenario, variable 'nsim_bus_dev->init' is added. Test commands: #SHELL1 modprobe netdevsim while : do echo "1 1" > /sys/bus/netdevsim/new_device echo "1 1" > /sys/bus/netdevsim/del_device done #SHELL2 while : do echo 1 > /sys/devices/netdevsim1/new_port echo 1 > /sys/devices/netdevsim1/del_port done Splat looks like: [ 66.648015][ T1095] kasan: CONFIG_KASAN_INLINE enabled [ 66.660685][ T1095] kasan: GPF could be caused by NULL-ptr deref or user memory access [ 66.662106][ T1095] general protection fault: 0000 [#1] SMP DEBUG_PAGEALLOC KASAN PTI [ 66.663151][ T1095] CPU: 0 PID: 1095 Comm: bash Not tainted 5.5.0-rc6+ torvalds#318 [ 66.664046][ T1095] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006 [ 66.665308][ T1095] RIP: 0010:__mutex_lock+0x10a/0x14b0 [ 66.666056][ T1095] Code: 08 84 d2 0f 85 7f 12 00 00 44 8b 0d 70 c4 66 02 45 85 c9 75 29 49 8d 7f 68 48 b8 00 f [ 66.670158][ T1095] RSP: 0018:ffff8880d36efbb0 EFLAGS: 00010206 [ 66.672254][ T1095] RAX: dffffc0000000000 RBX: 0000000000000000 RCX: 0000000000000000 [ 66.673392][ T1095] RDX: 0000000000000021 RSI: ffffffffbb922ac0 RDI: 0000000000000108 [ 66.674563][ T1095] RBP: ffff8880d36efd30 R08: ffffffffc033ead0 R09: 0000000000000000 [ 66.675731][ T1095] R10: ffff8880d36efd50 R11: ffff8880ca1f8040 R12: 0000000000000000 [ 66.676897][ T1095] R13: dffffc0000000000 R14: ffffffffbd17a7c0 R15: 00000000000000a0 [ 66.678005][ T1095] FS: 00007fe4a170f740(0000) GS:ffff8880d9c00000(0000) knlGS:0000000000000000 [ 66.679101][ T1095] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 66.679906][ T1095] CR2: 000055fa392f7ca0 CR3: 00000000b136a003 CR4: 00000000000606f0 [ 66.681467][ T1095] Call Trace: [ 66.681899][ T1095] ? nsim_dev_port_add+0x50/0x150 [netdevsim] [ 66.682681][ T1095] ? mutex_lock_io_nested+0x1380/0x1380 [ 66.683371][ T1095] ? _kstrtoull+0x76/0x160 [ 66.683819][ T1095] ? _parse_integer+0xf0/0xf0 [ 66.684324][ T1095] ? kernfs_fop_write+0x1cf/0x410 [ 66.684861][ T1095] ? sysfs_file_ops+0x160/0x160 [ 66.687441][ T1095] ? kstrtouint+0x86/0x110 [ 66.687961][ T1095] ? nsim_dev_port_add+0x50/0x150 [netdevsim] [ 66.688646][ T1095] nsim_dev_port_add+0x50/0x150 [netdevsim] [ 66.689269][ T1095] ? sysfs_file_ops+0x160/0x160 [ 66.690547][ T1095] new_port_store+0x99/0xb0 [netdevsim] [ 66.691114][ T1095] ? del_port_store+0xb0/0xb0 [netdevsim] [ 66.691699][ T1095] ? sysfs_file_ops+0x112/0x160 [ 66.692193][ T1095] ? sysfs_kf_write+0x3b/0x180 [ 66.692677][ T1095] kernfs_fop_write+0x276/0x410 [ 66.693176][ T1095] ? __sb_start_write+0x215/0x2e0 [ 66.693695][ T1095] vfs_write+0x197/0x4a0 [ 66.694136][ T1095] ksys_write+0x141/0x1d0 [ ... ] Fixes: f9d9db4 ("netdevsim: add bus attributes to add new and delete devices") Fixes: 794b2c0 ("netdevsim: extend device attrs to support port addition and deletion") Signed-off-by: Taehee Yoo <[email protected]>
- Loading branch information