Dear developers and maintainers, We encountered a deadlock in function ptp_clock_unregister(). We reproduced this bug on the latest upstream kernel(Linux 6.9-rc1). C repro and kernel config are attached to this email. The bug report is listed below. ``` ============================================ WARNING: possible recursive locking detected 6.7.0-rc7 #1 Not tainted -------------------------------------------- syz-executor367/8057 is trying to acquire lock: ffff88801cff4860 (&ptp->n_vclocks_mux){+.+.}-{3:3}, at: ptp_vclock_in_use drivers/ptp/ptp_private.h:103 [inline] ffff88801cff4860 (&ptp->n_vclocks_mux){+.+.}-{3:3}, at: ptp_clock_unregister+0x21/0x240 drivers/ptp/ptp_clock.c:386 but task is already holding lock: ffff88801ca28860 (&ptp->n_vclocks_mux){+.+.}-{3:3}, at: n_vclocks_store+0xd8/0x6e0 drivers/ptp/ptp_sysfs.c:216 other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock(&ptp->n_vclocks_mux); lock(&ptp->n_vclocks_mux); *** DEADLOCK *** May be due to missing lock nesting notation 4 locks held by syz-executor367/8057: #0: ffff888016a0e418 (sb_writers#5){.+.+}-{0:0}, at: ksys_write+0x122/0x250 fs/read_write.c:637 #1: ffff888016e5dc88 (&of->mutex){+.+.}-{3:3}, at: kernfs_fop_write_iter+0x27c/0x500 fs/kernfs/file.c:325 #2: ffff888049f3acf8 (kn->active#63){.+.+}-{0:0}, at: kernfs_fop_write_iter+0x2a0/0x500 fs/kernfs/file.c:326 #3: ffff88801ca28860 (&ptp->n_vclocks_mux){+.+.}-{3:3}, at: n_vclocks_store+0xd8/0x6e0 drivers/ptp/ptp_sysfs.c:216 stack backtrace: CPU: 0 PID: 8057 Comm: syz-executor367 Not tainted 6.7.0-rc7 #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xd9/0x150 lib/dump_stack.c:106 check_deadlock kernel/locking/lockdep.c:3062 [inline] validate_chain kernel/locking/lockdep.c:3856 [inline] __lock_acquire+0x2152/0x3bb0 kernel/locking/lockdep.c:5137 lock_acquire kernel/locking/lockdep.c:5754 [inline] lock_acquire+0x1b1/0x530 kernel/locking/lockdep.c:5719 __mutex_lock_common kernel/locking/mutex.c:603 [inline] __mutex_lock+0x147/0x940 kernel/locking/mutex.c:747 ptp_vclock_in_use drivers/ptp/ptp_private.h:103 [inline] ptp_clock_unregister+0x21/0x240 drivers/ptp/ptp_clock.c:386 ptp_vclock_unregister+0x119/0x160 drivers/ptp/ptp_vclock.c:228 unregister_vclock+0x108/0x1a0 drivers/ptp/ptp_sysfs.c:178 device_for_each_child_reverse+0x114/0x180 drivers/base/core.c:3984 n_vclocks_store+0x498/0x6e0 drivers/ptp/ptp_sysfs.c:242 dev_attr_store+0x54/0x80 drivers/base/core.c:2366 sysfs_kf_write+0x114/0x170 fs/sysfs/file.c:136 kernfs_fop_write_iter+0x337/0x500 fs/kernfs/file.c:334 call_write_iter include/linux/fs.h:2020 [inline] new_sync_write fs/read_write.c:491 [inline] vfs_write+0x96a/0xd80 fs/read_write.c:584 ksys_write+0x122/0x250 fs/read_write.c:637 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x40/0x110 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x63/0x6b RIP: 0033:0x7f86812e5b2d Code: 28 c3 e8 06 2a 00 00 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 c0 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007ffda42cf588 EFLAGS: 00000246 ORIG_RAX: 0000000000000001 RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f86812e5b2d RDX: 0000000000000015 RSI: 0000000020000000 RDI: 0000000000000004 RBP: 00007f868129f4e0 R08: 0000000000000014 R09: 0000000000000000 R10: 00007f868133201e R11: 0000000000000246 R12: 00007f868129f580 R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 ``` According to the report, ptp_clock_unregister() in ptp_clock.c takes the mutex which was taken before by n_vclocks_store ptp_sysfs.c. If you have any questions, please contact us. Reported by: Yue Sun Reported by: xingwei lee Best Regards, Yue