1 From 79336504781e7fee5ddaf046dcc186c8dfdf60b1 Mon Sep 17 00:00:00 2001
2 From: Damien Le Moal <dlemoal@kernel.org>
3 Date: Fri, 12 Apr 2024 08:41:15 +0900
4 Subject: ata: libata-scsi: Fix ata_scsi_dev_rescan() error path
6 From: Damien Le Moal <dlemoal@kernel.org>
8 commit 79336504781e7fee5ddaf046dcc186c8dfdf60b1 upstream.
10 Commit 0c76106cb975 ("scsi: sd: Fix TCG OPAL unlock on system resume")
11 incorrectly handles failures of scsi_resume_device() in
12 ata_scsi_dev_rescan(), leading to a double call to
13 spin_unlock_irqrestore() to unlock a device port. Fix this by redefining
14 the goto labels used in case of errors and only unlock the port
15 scsi_scan_mutex when scsi_resume_device() fails.
17 Bug found with the Smatch static checker warning:
19 drivers/ata/libata-scsi.c:4774 ata_scsi_dev_rescan()
20 error: double unlocked 'ap->lock' (orig line 4757)
22 Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
23 Fixes: 0c76106cb975 ("scsi: sd: Fix TCG OPAL unlock on system resume")
24 Cc: stable@vger.kernel.org
25 Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
26 Reviewed-by: Niklas Cassel <cassel@kernel.org>
27 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
29 drivers/ata/libata-scsi.c | 9 +++++----
30 1 file changed, 5 insertions(+), 4 deletions(-)
32 --- a/drivers/ata/libata-scsi.c
33 +++ b/drivers/ata/libata-scsi.c
34 @@ -4667,7 +4667,7 @@ void ata_scsi_dev_rescan(struct work_str
37 if (ap->pflags & ATA_PFLAG_SUSPENDED)
43 @@ -4680,7 +4680,7 @@ void ata_scsi_dev_rescan(struct work_str
45 ret = scsi_resume_device(sdev);
46 if (ret == -EWOULDBLOCK)
49 dev->flags &= ~ATA_DFLAG_RESUMING;
51 ret = scsi_rescan_device(sdev);
52 @@ -4688,12 +4688,13 @@ void ata_scsi_dev_rescan(struct work_str
53 spin_lock_irqsave(ap->lock, flags);
63 spin_unlock_irqrestore(ap->lock, flags);
65 mutex_unlock(&ap->scsi_scan_mutex);
67 /* Reschedule with a delay if scsi_rescan_device() returned an error */