Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / scsi_sysfs.c
index 931a7d9542038138537a5b76a98fa27217a95d63..9e2dd478dd15c06da38c062570aaf80ce44fd551 100644 (file)
@@ -332,17 +332,14 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 {
        struct scsi_device *sdev;
        struct device *parent;
-       struct scsi_target *starget;
        struct list_head *this, *tmp;
        unsigned long flags;
 
        sdev = container_of(work, struct scsi_device, ew.work);
 
        parent = sdev->sdev_gendev.parent;
-       starget = to_scsi_target(parent);
 
        spin_lock_irqsave(sdev->host->host_lock, flags);
-       starget->reap_ref++;
        list_del(&sdev->siblings);
        list_del(&sdev->same_target_siblings);
        list_del(&sdev->starved_entry);
@@ -362,8 +359,6 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
        /* NULL queue means the device can't be used */
        sdev->request_queue = NULL;
 
-       scsi_target_reap(scsi_target(sdev));
-
        kfree(sdev->inquiry);
        kfree(sdev);
 
@@ -978,6 +973,13 @@ void __scsi_remove_device(struct scsi_device *sdev)
                sdev->host->hostt->slave_destroy(sdev);
        transport_destroy_device(dev);
 
+       /*
+        * Paired with the kref_get() in scsi_sysfs_initialize().  We have
+        * remoed sysfs visibility from the device, so make the target
+        * invisible if this was the last device underneath it.
+        */
+       scsi_target_reap(scsi_target(sdev));
+
        put_device(dev);
 }
 
@@ -1040,7 +1042,7 @@ void scsi_remove_target(struct device *dev)
                        continue;
                if (starget->dev.parent == dev || &starget->dev == dev) {
                        /* assuming new targets arrive at the end */
-                       starget->reap_ref++;
+                       kref_get(&starget->reap_ref);
                        spin_unlock_irqrestore(shost->host_lock, flags);
                        if (last)
                                scsi_target_reap(last);
@@ -1124,6 +1126,12 @@ void scsi_sysfs_device_initialize(struct scsi_device *sdev)
        list_add_tail(&sdev->same_target_siblings, &starget->devices);
        list_add_tail(&sdev->siblings, &shost->__devices);
        spin_unlock_irqrestore(shost->host_lock, flags);
+       /*
+        * device can now only be removed via __scsi_remove_device() so hold
+        * the target.  Target will be held in CREATED state until something
+        * beneath it becomes visible (in which case it moves to RUNNING)
+        */
+       kref_get(&starget->reap_ref);
 }
 
 int scsi_is_sdev_device(const struct device *dev)