diff options
author | Christoph Hellwig <hch@lst.de> | 2014-09-14 06:41:16 +0400 |
---|---|---|
committer | Christoph Hellwig <hch@lst.de> | 2014-11-12 13:19:22 +0300 |
commit | 27c888f0bb889693c6a3b6d39eba3265c16c072f (patch) | |
tree | 26513aa57507d3bac7e027eb8561d3d555d8f44d /drivers/scsi/device_handler/scsi_dh.c | |
parent | 0b9c08442c5d0991dd1632fed63221f5b6a35e83 (diff) | |
download | linux-27c888f0bb889693c6a3b6d39eba3265c16c072f.tar.xz |
scsi_dh: get module reference outside of device handler
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>
Diffstat (limited to 'drivers/scsi/device_handler/scsi_dh.c')
-rw-r--r-- | drivers/scsi/device_handler/scsi_dh.c | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/drivers/scsi/device_handler/scsi_dh.c b/drivers/scsi/device_handler/scsi_dh.c index 33e422e75835..1a8dbf33f2ac 100644 --- a/drivers/scsi/device_handler/scsi_dh.c +++ b/drivers/scsi/device_handler/scsi_dh.c @@ -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); } /* |