After device is initialized via device_initialize(), or its name is set via dev_set_name(), the device has to be freed via put_device(), otherwise device name will be leaked because it is allocated dynamically in dev_set_name(). Fixes the issue by replacing kfree(shost) via put_device(&shost->shost_gendev) which can help to free dev_name(&shost->shost_dev) when host state is in SHOST_CREATED. Meantime needn't to remove IDA and stop the kthread of shost->ehandler in the error handling code. Cc: Bart Van Assche <bvanassche@xxxxxxx> Cc: John Garry <john.garry@xxxxxxxxxx> Cc: Hannes Reinecke <hare@xxxxxxx> Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx> --- drivers/scsi/hosts.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c index 624e2582c3df..25cf76e73595 100644 --- a/drivers/scsi/hosts.c +++ b/drivers/scsi/hosts.c @@ -391,8 +391,10 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) mutex_init(&shost->scan_mutex); index = ida_simple_get(&host_index_ida, 0, 0, GFP_KERNEL); - if (index < 0) - goto fail_kfree; + if (index < 0) { + kfree(shost); + return NULL; + } shost->host_no = index; shost->dma_channel = 0xff; @@ -484,7 +486,7 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) shost_printk(KERN_WARNING, shost, "error handler thread failed to spawn, error = %ld\n", PTR_ERR(shost->ehandler)); - goto fail_index_remove; + goto fail; } shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d", @@ -493,17 +495,18 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) if (!shost->tmf_work_q) { shost_printk(KERN_WARNING, shost, "failed to create tmf workq\n"); - goto fail_kthread; + goto fail; } scsi_proc_hostdir_add(shost->hostt); return shost; + fail: + /* + * host state is still SHOST_CREATED, and it is enough to release + * ->shost_gendev since scsi_host_dev_release() can help to free + * dev_name(&shost->shost_dev) + */ + put_device(&shost->shost_gendev); - fail_kthread: - kthread_stop(shost->ehandler); - fail_index_remove: - ida_simple_remove(&host_index_ida, shost->host_no); - fail_kfree: - kfree(shost); return NULL; } EXPORT_SYMBOL(scsi_host_alloc); -- 2.29.2