Skip to content

Commit fe44260

Browse files
bvanasschemartinkpetersen
authored andcommitted
scsi: core: Make sure that targets outlive devices
This commit prevents that the following sequence triggers a kernel crash: - Deletion of a SCSI device is requested via sysfs. Device removal takes some time because blk_cleanup_queue() is waiting for the SCSI error handler. - The SCSI target associated with that SCSI device is removed. - scsi_remove_target() returns and its caller frees the resources associated with the SCSI target. - The error handler makes progress and invokes an LLD callback that dereferences the SCSI target pointer. Link: https://lore.kernel.org/r/[email protected] Cc: Christoph Hellwig <[email protected]> Cc: Mike Christie <[email protected]> Cc: Hannes Reinecke <[email protected]> Cc: John Garry <[email protected]> Cc: Li Zhijian <[email protected]> Reported-by: Mike Christie <[email protected]> Reviewed-by: Ming Lei <[email protected]> Reviewed-by: Mike Christie <[email protected]> Signed-off-by: Bart Van Assche <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent a190667 commit fe44260

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

drivers/scsi/scsi_scan.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
521521
starget->state = STARGET_CREATED;
522522
starget->scsi_level = SCSI_2;
523523
starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED;
524+
init_waitqueue_head(&starget->sdev_wq);
525+
524526
retry:
525527
spin_lock_irqsave(shost->host_lock, flags);
526528

drivers/scsi/scsi_sysfs.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,9 @@ static void scsi_device_cls_release(struct device *class_dev)
443443

444444
static void scsi_device_dev_release_usercontext(struct work_struct *work)
445445
{
446-
struct scsi_device *sdev;
446+
struct scsi_device *sdev = container_of(work, struct scsi_device,
447+
ew.work);
448+
struct scsi_target *starget = sdev->sdev_target;
447449
struct device *parent;
448450
struct list_head *this, *tmp;
449451
struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
@@ -452,8 +454,6 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
452454
unsigned long flags;
453455
struct module *mod;
454456

455-
sdev = container_of(work, struct scsi_device, ew.work);
456-
457457
mod = sdev->host->hostt->module;
458458

459459
scsi_dh_release_device(sdev);
@@ -516,6 +516,9 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
516516
kfree(sdev->inquiry);
517517
kfree(sdev);
518518

519+
if (starget && atomic_dec_return(&starget->sdev_count) == 0)
520+
wake_up(&starget->sdev_wq);
521+
519522
if (parent)
520523
put_device(parent);
521524
module_put(mod);
@@ -1535,6 +1538,14 @@ static void __scsi_remove_target(struct scsi_target *starget)
15351538
goto restart;
15361539
}
15371540
spin_unlock_irqrestore(shost->host_lock, flags);
1541+
1542+
/*
1543+
* After scsi_remove_target() returns its caller can remove resources
1544+
* associated with @starget, e.g. an rport or session. Wait until all
1545+
* devices associated with @starget have been removed to prevent that
1546+
* a SCSI error handling callback function triggers a use-after-free.
1547+
*/
1548+
wait_event(starget->sdev_wq, atomic_read(&starget->sdev_count) == 0);
15381549
}
15391550

15401551
/**
@@ -1645,6 +1656,9 @@ void scsi_sysfs_device_initialize(struct scsi_device *sdev)
16451656
list_add_tail(&sdev->same_target_siblings, &starget->devices);
16461657
list_add_tail(&sdev->siblings, &shost->__devices);
16471658
spin_unlock_irqrestore(shost->host_lock, flags);
1659+
1660+
atomic_inc(&starget->sdev_count);
1661+
16481662
/*
16491663
* device can now only be removed via __scsi_remove_device() so hold
16501664
* the target. Target will be held in CREATED state until something

include/scsi/scsi_device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ struct scsi_target {
309309
struct list_head devices;
310310
struct device dev;
311311
struct kref reap_ref; /* last put renders target invisible */
312+
atomic_t sdev_count;
313+
wait_queue_head_t sdev_wq;
312314
unsigned int channel;
313315
unsigned int id; /* target id ... replace
314316
* scsi_device.id eventually */

0 commit comments

Comments
 (0)