scsi: two fixes in scsi_add_host_with_dma
v3 cover scsi: two fixes in scsi_add_host_with_dma
Hello Martin,
Fix two memory leaks and one double free issue in alloc/add host code path.
V3:
- fix memory leak caused in scsi_host_alloc
- comment typo suggested by John
V2:
- add patch 2 for addressing shost leak in case of adding host failure, reported by John Garry.
Ming Lei (3):
scsi: core: use put_device() to release host
scsi: core: fix failure handling of scsi_add_host_with_dma
scsi: core: put ->shost_gendev.parent in failure handling path
drivers/scsi/hosts.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
Signature · 1 line
2.29.2
PATCH v3 1/3 scsi: core: use put_device() to release host
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 two put_device() since both .shost_dev and .shost_gendev share same lifetime. Meantime move get_device(shost->shost_gendev) from scsi_add_host_with_dma to scsi_host_alloc(), so that we can grab parent's refcnt explicitly when assigning .shost_dev->parent. With this way code becomes more readable.
Also call put_device(dev->parent) in scsi_host_cls_release() so that code readability can be improved.
--- drivers/scsi/hosts.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/scsi/hosts.c +5 -5
@@ -55,7 +55,7 @@ static DEFINE_IDA(host_index_ida);static void scsi_host_cls_release (struct device *dev) { -put_device (&class_to_shost (dev)->shost_gendev); +put_device (dev->parent); }static struct class shost_class = {
@@ -261,8 +261,6 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,if (error) goto out_del_gendev; -get_device (&shost->shost_gendev); -if (shost->transportt->host_size) { shost->shost_data =kzalloc (shost->transportt->host_size,GFP_KERNEL );
@@ -473,7 +471,7 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) shost->shost_gendev.type = &scsi_host_type;device_initialize (&shost->shost_dev); - shost->shost_dev.parent = &shost->shost_gendev; + shost->shost_dev.parent =get_device (&shost->shost_gendev); shost->shost_dev.class = &shost_class;dev_set_name (&shost->shost_dev,"host%d" , shost->host_no); shost->shost_dev.groups = scsi_sysfs_shost_attr_groups;
@@ -503,7 +501,9 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) fail_index_remove:ida_simple_remove (&host_index_ida, shost->host_no); fail_kfree: -kfree (shost); +put_device (&shost->shost_dev); +put_device (&shost->shost_gendev); +return NULL ; }EXPORT_SYMBOL (scsi_host_alloc);
Signature · 1 line
2.29.2
PATCH v4 1/3 scsi: core: use put_device() to release host
From 2a70cffeca09156faf68d933157586b5bf9278fa Mon Sep 17 00:00:00 2001
From: Ming Lei <[email protected]>
Date: Mon, 31 May 2021 11:02:01 +0800
Subject: [PATCH V4 1/3] scsi: core: use put_device() to release host
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 two put_device() since both .shost_dev and .shost_gendev share same lifetime. Meantime move get_device(shost->shost_gendev) from scsi_add_host_with_dma to scsi_host_alloc(), so that we can grab parent's refcnt explicitly when assigning .shost_dev->parent. With this way code becomes more readable.
Also call put_device(dev->parent) in scsi_host_cls_release() so that code readability can be improved.
---
V4:
- avoid to touch un-initialized device instance
drivers/scsi/hosts.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/scsi/hosts.c +9 -8
@@ -55,7 +55,7 @@ static DEFINE_IDA(host_index_ida);static void scsi_host_cls_release (struct device *dev) { -put_device (&class_to_shost (dev)->shost_gendev); +put_device (dev->parent); }static struct class shost_class = {
@@ -261,8 +261,6 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,if (error) goto out_del_gendev; -get_device (&shost->shost_gendev); -if (shost->transportt->host_size) { shost->shost_data =kzalloc (shost->transportt->host_size,GFP_KERNEL );
@@ -391,8 +389,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 ;
@@ -473,7 +473,7 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) shost->shost_gendev.type = &scsi_host_type;device_initialize (&shost->shost_dev); - shost->shost_dev.parent = &shost->shost_gendev; + shost->shost_dev.parent =get_device (&shost->shost_gendev); shost->shost_dev.class = &shost_class;dev_set_name (&shost->shost_dev,"host%d" , shost->host_no); shost->shost_dev.groups = scsi_sysfs_shost_attr_groups;
@@ -502,8 +502,9 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)kthread_stop (shost->ehandler); fail_index_remove:ida_simple_remove (&host_index_ida, shost->host_no); - fail_kfree: -kfree (shost); +/* free host instance */ +put_device (&shost->shost_dev); +put_device (&shost->shost_gendev);return NULL ; }EXPORT_SYMBOL (scsi_host_alloc);
Signature · 1 line
2.29.2
PATCH v3 2/3 scsi: core: fix failure handling of scsi_add_host_with_dma
In reply to Ming LeiWhen scsi_add_host_with_dma() return failure, the caller will call scsi_host_put(shost) to release everything allocated for this host instance. So we can't free allocated stuff in scsi_add_host_with_dma(), otherwise double free will be caused.
Strictly speaking, these host resources allocation should have been moved to scsi_host_alloc(), but the allocation may need driver's info which can be built between calling scsi_host_alloc() and scsi_add_host(), so just keep the allocations in scsi_add_host_with_dma().
Fixes the problem by relying on host device's release handler to release everything.
--- drivers/scsi/hosts.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/scsi/hosts.c +6 -8
@@ -279,23 +279,22 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,if (!shost->work_q) { error = -EINVAL ; - goto out_free_shost_data; + goto out_del_dev; } } error =scsi_sysfs_add_host (shost);if (error) - goto out_destroy_host; + goto out_del_dev;scsi_proc_host_add (shost);scsi_autopm_put_host (shost);return error; - out_destroy_host: -if (shost->work_q) -destroy_workqueue (shost->work_q); - out_free_shost_data: -kfree (shost->shost_data); +/* +* any host allocation in this function will be freed in +* scsi_host_dev_release, so don't free them in the failure path +*/ out_del_dev:device_del (&shost->shost_dev); out_del_gendev:
@@ -305,7 +304,6 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,pm_runtime_disable (&shost->shost_gendev);pm_runtime_set_suspended (&shost->shost_gendev);pm_runtime_put_noidle (&shost->shost_gendev); -scsi_mq_destroy_tags (shost); fail:return error; }
Signature · 1 line
2.29.2
Re: [PATCH V3 2/3] scsi: core: fix failure handling of scsi_add_host_with_dma
Quoted 58 lines · Ming Lei
When
scsi_add_host_with_dma()return failure, the caller will callscsi_host_put(shost)to release everything allocated for this host instance. So we can't free allocated stuff inscsi_add_host_with_dma(), otherwise double free will be caused.Strictly speaking, these host resources allocation should have been moved to
scsi_host_alloc(), but the allocation may need driver's info which can be built between callingscsi_host_alloc()andscsi_add_host(), so just keep the allocations inscsi_add_host_with_dma().Fixes the problem by relying on host device's release handler to release everything.
Cc Bart Van Assche <[email protected]>Cc John Garry <[email protected]>Cc Hannes Reinecke <[email protected]>Signed-off-by Ming Lei <[email protected]>--- drivers/scsi/hosts.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)drivers/scsi/hosts.c +6
-7@@ -279,23 +279,22 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,if (!shost->work_q) { error = -EINVAL ; - goto out_free_shost_data; + goto out_del_dev; } } error =scsi_sysfs_add_host (shost);if (error) - goto out_destroy_host; + goto out_del_dev;scsi_proc_host_add (shost);scsi_autopm_put_host (shost);return error; - out_destroy_host: -if (shost->work_q) -destroy_workqueue (shost->work_q); - out_free_shost_data: -kfree (shost->shost_data); +/* +* any host allocation in this function will be freed in +* scsi_host_dev_release, so don't free them in the failure path +*/ out_del_dev:device_del (&shost->shost_dev); out_del_gendev:
drivers/scsi/hosts.c int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, · line 309
Quoted 7 lines
drivers/scsi/hosts.c +0
-1@@ -305,7 +304,6 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,pm_runtime_disable (&shost->shost_gendev);pm_runtime_set_suspended (&shost->shost_gendev);pm_runtime_put_noidle (&shost->shost_gendev); -scsi_mq_destroy_tags (shost); fail:return error; }
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
PATCH v3 3/3 scsi: core: put ->shost_gendev.parent in failure handling path
In reply to Ming Leiget_device(shost->shost_gendev.parent) is called in scsi_add_host_with_dma(), but its counter pair isn't called in the failure path, so fix it by calling put_device(shost->shost_gendev.parent) in its failure path.
--- drivers/scsi/hosts.c | 1 + 1 file changed, 1 insertion(+)
drivers/scsi/hosts.c +1 -0
@@ -298,6 +298,7 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, out_del_dev:device_del (&shost->shost_dev); out_del_gendev: +put_device (shost->shost_gendev.parent);device_del (&shost->shost_gendev); out_disable_runtime_pm:device_disable_async_suspend (&shost->shost_gendev);
Signature · 1 line
2.29.2
Re: [PATCH V3 3/3] scsi: core: put ->shost_gendev.parent in failure handling path
Quoted 19 lines · Ming Lei
get_device(shost->shost_gendev.parent)is called inscsi_add_host_with_dma(), but its counter pair isn't called in the failure path, so fix it by callingput_device(shost->shost_gendev.parent)in its failure path.Reported-by John Garry <[email protected]>Cc Bart Van Assche <[email protected]>Cc Hannes Reinecke <[email protected]>Signed-off-by Ming Lei <[email protected]>--- drivers/scsi/hosts.c | 1 + 1 file changed, 1 insertion(+)drivers/scsi/hosts.c +0
-0
drivers/scsi/hosts.c int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, · line 304
Quoted 7 lines
drivers/scsi/hosts.c +1
-0@@ -298,6 +298,7 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, out_del_dev:device_del (&shost->shost_dev); out_del_gendev: +put_device (shost->shost_gendev.parent);device_del (&shost->shost_gendev); out_disable_runtime_pm:device_disable_async_suspend (&shost->shost_gendev);
This really needs to be folded into the first patch as it's really a bugfix for that.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Re: [PATCH V3 3/3] scsi: core: put ->shost_gendev.parent in failure handling path
Quoted 14 lines · Hannes Reinecke
Quoted 13 lines · Ming Lei
get_device(shost->shost_gendev.parent)is called inscsi_add_host_with_dma(), but its counter pair isn't called in the failure path, so fix it by callingput_device(shost->shost_gendev.parent)in its failure path.Reported-by John Garry <[email protected]>Cc Bart Van Assche <[email protected]>Cc Hannes Reinecke <[email protected]>Signed-off-by Ming Lei <[email protected]>--- drivers/scsi/hosts.c | 1 + 1 file changed, 1 insertion(+)
Quoted 15 lines
drivers/scsi/hosts.c int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, · line 304
Quoted 13 lines
drivers/scsi/hosts.c +1
-0@@ -298,6 +298,7 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, out_del_dev:device_del (&shost->shost_dev); out_del_gendev: +put_device (shost->shost_gendev.parent);device_del (&shost->shost_gendev); out_disable_runtime_pm:device_disable_async_suspend (&shost->shost_gendev);This really needs to be folded into the first patch as it's really a bugfix for that.
All three are bug fixes, and this one may leak .shost_gendev's parent, but the 1st one leaks .shost_gendev->kobj.name, so we needn't to fold the 3rd into the 1st one.
Thanks,
Ming
Re: [PATCH V3 3/3] scsi: core: put ->shost_gendev.parent in failure handling path
Quoted 31 lines · Ming Lei
Quoted 14 lines · Hannes Reinecke
Quoted 13 lines · Ming Lei
get_device(shost->shost_gendev.parent)is called inscsi_add_host_with_dma(), but its counter pair isn't called in the failure path, so fix it by callingput_device(shost->shost_gendev.parent)in its failure path.Reported-by John Garry <[email protected]>Cc Bart Van Assche <[email protected]>Cc Hannes Reinecke <[email protected]>Signed-off-by Ming Lei <[email protected]>--- drivers/scsi/hosts.c | 1 + 1 file changed, 1 insertion(+)Quoted 15 lines
drivers/scsi/hosts.c int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, · line 304
Quoted 13 lines
drivers/scsi/hosts.c +1
-0@@ -298,6 +298,7 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, out_del_dev:device_del (&shost->shost_dev); out_del_gendev: +put_device (shost->shost_gendev.parent);device_del (&shost->shost_gendev); out_disable_runtime_pm:device_disable_async_suspend (&shost->shost_gendev);This really needs to be folded into the first patch as it's really a bugfix for that.
Quoted 4 lines
All three are bug fixes, and this one may leak .shost_gendev's parent, but the 1st one leaks .shost_gendev->kobj.name, so we needn't to fold the 3rd into the 1st one.
I beg to disagree.
The first patch removes the 'get_device()' fromscsi_add_host_with_dma(), but does not remove the corresponding 'put_device()' in the error path.
So the first patch introduces a reference imbalance which is fixed by the third patch. Hence my suggestion to merge those two patches.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
Re: [PATCH V3 3/3] scsi: core: put ->shost_gendev.parent in failure handling path
Quoted 32 lines · Hannes Reinecke
Quoted 31 lines · Ming Lei
Quoted 29 lines · Hannes Reinecke
Quoted 19 lines · Ming Lei
get_device(shost->shost_gendev.parent)is called inscsi_add_host_with_dma(), but its counter pair isn't called in the failure path, so fix it by callingput_device(shost->shost_gendev.parent)in its failure path.Reported-by John Garry <[email protected]>Cc Bart Van Assche <[email protected]>Cc Hannes Reinecke <[email protected]>Signed-off-by Ming Lei <[email protected]>--- drivers/scsi/hosts.c | 1 + 1 file changed, 1 insertion(+)drivers/scsi/hosts.c +0
-0drivers/scsi/hosts.c int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, · line 304
Quoted 7 lines
drivers/scsi/hosts.c +1
-0@@ -298,6 +298,7 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, out_del_dev:device_del (&shost->shost_dev); out_del_gendev: +put_device (shost->shost_gendev.parent);device_del (&shost->shost_gendev); out_disable_runtime_pm:device_disable_async_suspend (&shost->shost_gendev);This really needs to be folded into the first patch as it's really a bugfix for that.
Quoted 8 lines
Quoted 4 lines
All three are bug fixes, and this one may leak .shost_gendev's parent, but the 1st one leaks .shost_gendev->kobj.name, so we needn't to fold the 3rd into the 1st one.
I beg to disagree.
The first patch removes the 'get_device()' fromscsi_add_host_with_dma(), but does not remove the corresponding 'put_device()' in the error path.
There isn't such 'put_device' in the error path of scsi_add_host_with_dma(), is there?
Quoted 2 lines
So the first patch introduces a reference imbalance which is fixed by the third patch. Hence my suggestion to merge those two patches.
No, that isn't true, please look at current code of scsi_add_host_with_dma().
Thanks,
Ming
Quoted 20 lines · Ming Lei
Hello Martin,
Fix two memory leaks and one double free issue in alloc/add host code path.
V3:
- fix memory leak caused inscsi_host_alloc
- comment typo suggested by JohnV2:
- add patch 2 for addressing shost leak in case of adding host failure, reported by John Garry.Ming Lei (3):
scsi: core: useput_device()to release host
scsi: core: fix failure handling ofscsi_add_host_with_dma
scsi: core: put ->shost_gendev.parent in failure handling path
Quoted 6 lines
drivers/scsi/hosts.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)Cc Bart Van Assche <[email protected]>Cc John Garry <[email protected]>Cc Hannes Reinecke <[email protected]>
I tested this again with the same experiment as before to error inscsi_add_host_with_dma(), and we don't call scsi_host_dev_release() now. The shost_gendev dev refcount is 2 upon exit in scsi_add_host_with_dma().
We don't call scsi_host_cls_release() either, so I guess a ref count is leaked for shost_dev - I see its refcount is 1 at exit in scsi_add_host_with_dma(). We have the device_initialize(), device_add(), device_del() in the alloc and add host functions, but I don't know who is responsible for the final "device put".
Thanks,
John
Quoted 33 lines · John Garry
Quoted 20 lines · Ming Lei
Hello Martin,
Fix two memory leaks and one double free issue in alloc/add host code path.
V3:
- fix memory leak caused inscsi_host_alloc
- comment typo suggested by JohnV2:
- add patch 2 for addressing shost leak in case of adding host failure, reported by John Garry.Ming Lei (3):
scsi: core: useput_device()to release host
scsi: core: fix failure handling ofscsi_add_host_with_dma
scsi: core: put ->shost_gendev.parent in failure handling pathQuoted 6 lines
drivers/scsi/hosts.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)Cc Bart Van Assche <[email protected]>Cc John Garry <[email protected]>Cc Hannes Reinecke <[email protected]>I tested this again with the same experiment as before to error in
scsi_add_host_with_dma(), and we don't callscsi_host_dev_release()now. Theshost_gendevdev refcount is 2 upon exit inscsi_add_host_with_dma().
Quoted 5 lines
We don't call
scsi_host_cls_release()either, so I guess a ref count is leaked forshost_dev- I see its refcount is 1 at exit inscsi_add_host_with_dma(). We have thedevice_initialize(),device_add(),device_del()in the alloc and add host functions, but I don't know who is responsible for the final "device put".
Hammm, we still need to put ->shost_dev before returning the error, and the following delta patch can fix the issue, and it should have been wrapped into the 1st one.
drivers/scsi/hosts.c +2 -0
@@ -306,6 +306,8 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,pm_runtime_set_suspended (&shost->shost_gendev);pm_runtime_put_noidle (&shost->shost_gendev); fail: +/* drop ref of ->shost_dev so that caller can release this host */ +put_device (&shost->shost_dev);return error; }EXPORT_SYMBOL (scsi_add_host_with_dma);
Thanks,
Ming
Quoted 14 lines · Ming Lei
Quoted 5 lines
We don't call
scsi_host_cls_release()either, so I guess a ref count is leaked forshost_dev- I see its refcount is 1 at exit inscsi_add_host_with_dma(). We have thedevice_initialize(),device_add(),device_del()in the alloc and add host functions, but I don't know who is responsible for the final "device put".Hammm, we still need to put ->shost_dev before returning the error, and the following delta patch can fix the issue, and it should have been wrapped into the 1st one.
drivers/scsi/hosts.c +0
-0
drivers/scsi/hosts.c int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, · line 313
Quoted 8 lines
drivers/scsi/hosts.c +2
-0@@ -306,6 +306,8 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,pm_runtime_set_suspended (&shost->shost_gendev);pm_runtime_put_noidle (&shost->shost_gendev); fail: +/* drop ref of ->shost_dev so that caller can release this host */ +put_device (&shost->shost_dev);return error; }EXPORT_SYMBOL (scsi_add_host_with_dma);
That looks better now.
And we can see the equivalent on the normal removal path in scsi_remove_host() -> device_unregister(&shost->shost_dev), which does a device_del()+put_device().
So could we actually just have:out_del_dev:
unregister_dev(&shost->shost_dev)
I am not sure if we are required to keep that shost_dev reference all the way until the exit, as you do.
Thanks,
John
Quoted 26 lines · John Garry
Quoted 14 lines · Ming Lei
Quoted 5 lines
We don't call
scsi_host_cls_release()either, so I guess a ref count is leaked forshost_dev- I see its refcount is 1 at exit inscsi_add_host_with_dma(). We have thedevice_initialize(),device_add(),device_del()in the alloc and add host functions, but I don't know who is responsible for the final "device put".Hammm, we still need to put ->shost_dev before returning the error, and the following delta patch can fix the issue, and it should have been wrapped into the 1st one.
drivers/scsi/hosts.c +0
-0drivers/scsi/hosts.c int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, · line 313
Quoted 8 lines
drivers/scsi/hosts.c +2
-0@@ -306,6 +306,8 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,pm_runtime_set_suspended (&shost->shost_gendev);pm_runtime_put_noidle (&shost->shost_gendev); fail: +/* drop ref of ->shost_dev so that caller can release this host */ +put_device (&shost->shost_dev);return error; }EXPORT_SYMBOL (scsi_add_host_with_dma);That looks better now.
Quoted 8 lines
And we can see the equivalent on the normal removal path in
scsi_remove_host()->device_unregister(&shost->shost_dev), which does a device_del()+put_device().So could we actually just have:
out_del_dev:unregister_dev(&shost->shost_dev)
No, we still have to call put_device(&shost->shost_dev) only in case of failure before adding &shost->shost_dev.
Quoted 2 lines
I am not sure if we are required to keep that
shost_devreference all the way until the exit, as you do.
That has been done in this way, the problem is that both .shost_dev and .shost_gendev share same lifetime and memory(same struct Scsi_Host instance), this kind of pattern isn't one usual driver core use case, and we have to handle it carefully.
Thanks,
Ming