diff mbox series

scsi: smartpqi: Add lun_reset_key to initialize ctrl_info->lun_reset_mutex

Message ID 20250214192324.2471148-1-longman@redhat.com
State New
Headers show
Series scsi: smartpqi: Add lun_reset_key to initialize ctrl_info->lun_reset_mutex | expand

Commit Message

Waiman Long Feb. 14, 2025, 7:23 p.m. UTC
A lockdep recursive locking splat happens when shutting down a debug kernel
on some systems.

 ============================================
 WARNING: possible recursive locking detected
 --------------------------------------------
 reboot/15103 is trying to acquire lock:
 ffff8881435af8c8 (&ctrl_info->lun_reset_mutex){+.+.}-{3:3}, at: pqi_shutdown+0x112/0x4b0 [smartpqi]

 but task is already holding lock:
 ffff8888929278c8 (&ctrl_info->lun_reset_mutex){+.+.}-{3:3}, at: pqi_shutdown+0x112/0x4b0 [smartpqi]

  other info that might help us debug this:
   Possible unsafe locking scenario:

         CPU0
         ----
    lock(&ctrl_info->lun_reset_mutex);
    lock(&ctrl_info->lun_reset_mutex);

   *** DEADLOCK ***

   May be due to missing lock nesting notation

  4 locks held by reboot/15103:
   #0: ffffffff8db76f40 (system_transition_mutex){+.+.}-{3:3}, at: __do_sys_reboot+0x12b/0x2f0
   #1: ffff8888929278c8 (&ctrl_info->lun_reset_mutex){+.+.}-{3:3}, at: pqi_shutdown+0x112/0x4b0 [smartpqi]
   #2: ffff88810a8921a8 (&dev->mutex){....}-{3:3}, at: device_shutdown+0x1de/0x540
   #3: ffff88810a9141a8 (&dev->mutex){....}-{3:3}, at: device_shutdown+0x1ec/0x540
  Call Trace:

   dump_stack_lvl+0x6f/0xb0
   print_deadlock_bug.cold+0xbd/0xca
   validate_chain+0x37b/0x570
   __lock_acquire+0x55b/0xac0
   lock_acquire.part.0+0xf5/0x2b0
   mutex_lock_nested+0x4b/0x190
   pqi_shutdown+0x112/0x4b0 [smartpqi]
   pci_device_shutdown+0x76/0x110
   device_shutdown+0x2ea/0x540
   kernel_restart+0x64/0xa0
   __do_sys_reboot+0x1d8/0x2f0
   do_syscall_64+0x92/0x180

The fact that there are two dev->mutex'es acquired in device_shutdown()
means both a parent and a child devices are being worked on and
likely that the lun_reset_mutex'es of these two devices are being
acquired here too. However, the way lun_reset_mutex is initialized in
pqi_alloc_ctrl_info() will casue all the lun_reset_mutex'es to be treated
as in the same class leading to this false positive warning. Fix that
by initializing each instance of lun_reset_mutex with its own unique
key so that they will be treated as different by lockdep.

Also call mutex_destroy() and lockdep_unregister_key() in
pqi_free_ctrl_info() before ctrl_info is freed.

With this patch applied, the lockdep splat no longer happens.

Signed-off-by: Waiman Long <longman@redhat.com>
---
 drivers/scsi/smartpqi/smartpqi.h      | 1 +
 drivers/scsi/smartpqi/smartpqi_init.c | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/drivers/scsi/smartpqi/smartpqi.h b/drivers/scsi/smartpqi/smartpqi.h
index fae6db20a6e9..fb75de53d401 100644
--- a/drivers/scsi/smartpqi/smartpqi.h
+++ b/drivers/scsi/smartpqi/smartpqi.h
@@ -1359,6 +1359,7 @@  struct pqi_ctrl_info {
 
 	struct mutex	scan_mutex;
 	struct mutex	lun_reset_mutex;
+	struct lock_class_key lun_reset_key;
 	bool		controller_online;
 	bool		block_requests;
 	bool		scan_blocked;
diff --git a/drivers/scsi/smartpqi/smartpqi_init.c b/drivers/scsi/smartpqi/smartpqi_init.c
index 0da7be40c925..5c54dee67e54 100644
--- a/drivers/scsi/smartpqi/smartpqi_init.c
+++ b/drivers/scsi/smartpqi/smartpqi_init.c
@@ -8789,9 +8789,11 @@  static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
 		return NULL;
 
 	mutex_init(&ctrl_info->scan_mutex);
-	mutex_init(&ctrl_info->lun_reset_mutex);
 	mutex_init(&ctrl_info->ofa_mutex);
 
+	lockdep_register_key(&ctrl_info->lun_reset_key);
+	mutex_init_with_key(&ctrl_info->lun_reset_mutex, &ctrl_info->lun_reset_key);
+
 	INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
 	spin_lock_init(&ctrl_info->scsi_device_list_lock);
 
@@ -8830,6 +8832,10 @@  static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
 
 static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
 {
+	mutex_destroy(&ctrl_info->scan_mutex);
+	mutex_destroy(&ctrl_info->ofa_mutex);
+	mutex_destroy(&ctrl_info->lun_reset_mutex);
+	lockdep_unregister_key(&ctrl_info->lun_reset_key);
 	kfree(ctrl_info);
 }