scsi_dh: get module reference outside of device handler
authorChristoph Hellwig <hch@lst.de>
Sun, 14 Sep 2014 02:41:16 +0000 (19:41 -0700)
committerChristoph Hellwig <hch@lst.de>
Wed, 12 Nov 2014 10:19:22 +0000 (11:19 +0100)
We need to grab a reference to the module before calling the attach
routines to avoid a small race vs module removal.  It also cleans up
the code significantly as a side effect.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Mike Christie <michaelc@cs.wisc.edu>
Reviewed-by: Hannes Reinecke <hare@suse.de>
drivers/scsi/device_handler/scsi_dh.c
drivers/scsi/device_handler/scsi_dh_alua.c
drivers/scsi/device_handler/scsi_dh_emc.c
drivers/scsi/device_handler/scsi_dh_hp_sw.c
drivers/scsi/device_handler/scsi_dh_rdac.c

index 33e422e7583500001acc1fbe272c950ede8aff92..1a8dbf33f2acf1bcc14b56f0c0e07d1018017770 100644 (file)
@@ -102,23 +102,36 @@ static int scsi_dh_handler_attach(struct scsi_device *sdev,
 
        if (sdev->scsi_dh_data) {
                if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
-                       err = -EBUSY;
-               else
-                       kref_get(&sdev->scsi_dh_data->kref);
-       } else if (scsi_dh->attach) {
+                       return -EBUSY;
+
+               kref_get(&sdev->scsi_dh_data->kref);
+               return 0;
+       }
+
+       if (scsi_dh->attach) {
+               if (!try_module_get(scsi_dh->module))
+                       return -EINVAL;
+
                err = scsi_dh->attach(sdev);
-               if (!err) {
-                       kref_init(&sdev->scsi_dh_data->kref);
-                       sdev->scsi_dh_data->sdev = sdev;
+               if (err) {
+                       module_put(scsi_dh->module);
+                       return err;
                }
+
+               kref_init(&sdev->scsi_dh_data->kref);
+               sdev->scsi_dh_data->sdev = sdev;
        }
        return err;
 }
 
 static void __detach_handler (struct kref *kref)
 {
-       struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
-       scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
+       struct scsi_dh_data *scsi_dh_data =
+               container_of(kref, struct scsi_dh_data, kref);
+       struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh;
+
+       scsi_dh->detach(scsi_dh_data->sdev);
+       module_put(scsi_dh->module);
 }
 
 /*
index fd78bdc535280a0eebcefc527221ebf091300e8f..9115c31f26e9e447ebdb67d08d473fc806d2f890 100644 (file)
@@ -873,9 +873,6 @@ static int alua_bus_attach(struct scsi_device *sdev)
        if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
                goto failed;
 
-       if (!try_module_get(THIS_MODULE))
-               goto failed;
-
        spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
        sdev->scsi_dh_data = scsi_dh_data;
        spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
@@ -908,7 +905,6 @@ static void alua_bus_detach(struct scsi_device *sdev)
        if (h->buff && h->inq != h->buff)
                kfree(h->buff);
        kfree(scsi_dh_data);
-       module_put(THIS_MODULE);
        sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
 }
 
index 84765384c47ca4486caf3a7e4366521af193be3f..153b4c3547a2c3687806110a08ef00ca43900285 100644 (file)
@@ -692,9 +692,6 @@ static int clariion_bus_attach(struct scsi_device *sdev)
        if (err != SCSI_DH_OK)
                goto failed;
 
-       if (!try_module_get(THIS_MODULE))
-               goto failed;
-
        spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
        sdev->scsi_dh_data = scsi_dh_data;
        spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
@@ -728,7 +725,6 @@ static void clariion_bus_detach(struct scsi_device *sdev)
                    CLARIION_NAME);
 
        kfree(scsi_dh_data);
-       module_put(THIS_MODULE);
 }
 
 static int __init clariion_init(void)
index 4824bfbe3051b6e48c4dd59315d867bffa63075a..cf36557a1d4daa915842434e401286e88e64d326 100644 (file)
@@ -377,9 +377,6 @@ static int hp_sw_bus_attach(struct scsi_device *sdev)
        if (ret != SCSI_DH_OK || h->path_state == HP_SW_PATH_UNINITIALIZED)
                goto failed;
 
-       if (!try_module_get(THIS_MODULE))
-               goto failed;
-
        spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
        sdev->scsi_dh_data = scsi_dh_data;
        spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
@@ -406,7 +403,6 @@ static void hp_sw_bus_detach( struct scsi_device *sdev )
        scsi_dh_data = sdev->scsi_dh_data;
        sdev->scsi_dh_data = NULL;
        spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
-       module_put(THIS_MODULE);
 
        sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", HP_SW_NAME);
 
index 1b5bc9293e37d416769974055dbe122d38ef0c8c..b850954c4e22aed273a34b7ba23d129f07f5dce0 100644 (file)
@@ -878,9 +878,6 @@ static int rdac_bus_attach(struct scsi_device *sdev)
        if (err != SCSI_DH_OK)
                goto clean_ctlr;
 
-       if (!try_module_get(THIS_MODULE))
-               goto clean_ctlr;
-
        spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
        sdev->scsi_dh_data = scsi_dh_data;
        spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
@@ -924,7 +921,6 @@ static void rdac_bus_detach( struct scsi_device *sdev )
                kref_put(&h->ctlr->kref, release_controller);
        spin_unlock(&list_lock);
        kfree(scsi_dh_data);
-       module_put(THIS_MODULE);
        sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", RDAC_NAME);
 }