scsi: core: Correct wrong kfree() usage for `kobj->name`
Subsystems (2)
PATCH 1/1 scsi: core: Correct wrong kfree() usage for `kobj->name`
kobj->name should be freed by kfree_const()[1][2]. Correct it.
[1] https://elixir.bootlin.com/linux/v6.18/source/lib/kasprintf.c#L41
[2] https://elixir.bootlin.com/linux/v6.18/source/lib/kobject.c#L695
--- drivers/scsi/hosts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/hosts.c +1 -1
@@ -373,7 +373,7 @@ static void scsi_host_dev_release(struct device *dev)* name as well as the proc dir structure are leaked. */ scsi_proc_hostdir_rm (shost->hostt); -kfree (dev_name (&shost->shost_dev)); +kfree_const (dev_name (&shost->shost_dev)); }kfree (shost->shost_data);
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Greg KH
maintainerQuoted 17 lines · Tzung-Bi Shih
kobj->nameshould be freed by kfree_const()[1][2]. Correct it.[1] https://elixir.bootlin.com/linux/v6.18/source/lib/kasprintf.c#L41
[2] https://elixir.bootlin.com/linux/v6.18/source/lib/kobject.c#L695Fixes b49493f99690 ("Fix a memory leak in scsi_host_dev_release()")Signed-off-by Tzung-Bi Shih <[email protected]>--- drivers/scsi/hosts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/scsi/hosts.c +0
-0
drivers/scsi/hosts.c static void scsi_host_dev_release(struct device *dev) · line 376
Quoted 5 lines
drivers/scsi/hosts.c +1
-1@@ -373,7 +373,7 @@ static void scsi_host_dev_release(struct device *dev)* name as well as the proc dir structure are leaked. */ scsi_proc_hostdir_rm (shost->hostt); -kfree (dev_name (&shost->shost_dev)); +kfree_const (dev_name (&shost->shost_dev));
Shouldn't the struct device name be freed by the driver core for this device when it goes out of scope? Why is it being manually freed here at all?
thanks,
greg k-h
Quoted 13 lines · Greg KH
Quoted 12 lines · Tzung-Bi Shih
kobj->nameshould be freed by kfree_const()[1][2]. Correct it.[1] https://elixir.bootlin.com/linux/v6.18/source/lib/kasprintf.c#L41
[2] https://elixir.bootlin.com/linux/v6.18/source/lib/kobject.c#L695Fixes b49493f99690 ("Fix a memory leak in scsi_host_dev_release()")Signed-off-by Tzung-Bi Shih <[email protected]>--- drivers/scsi/hosts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
drivers/scsi/hosts.c static void scsi_host_dev_release(struct device *dev) · line 376
Quoted 10 lines
drivers/scsi/hosts.c +1
-1@@ -373,7 +373,7 @@ static void scsi_host_dev_release(struct device *dev)* name as well as the proc dir structure are leaked. */ scsi_proc_hostdir_rm (shost->hostt); -kfree (dev_name (&shost->shost_dev)); +kfree_const (dev_name (&shost->shost_dev));Shouldn't the struct device name be freed by the driver core for this device when it goes out of scope? Why is it being manually freed here at all?
Ah, correct. I think the following patch is what it really needs:
drivers/scsi/hosts.c +1 -6
@@ -373,7 +373,6 @@ static void scsi_host_dev_release(struct device *dev)* name as well as the proc dir structure are leaked. */ scsi_proc_hostdir_rm (shost->hostt); -kfree (dev_name (&shost->shost_dev)); }kfree (shost->shost_data);
@@ -548,11 +547,7 @@ struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv goto fail;return shost; fail: -/* -* Host state is still SHOST_CREATED and that is enough to release -* ->shost_gendev. scsi_host_dev_release() will free -* dev_name(&shost->shost_dev). -*/ +put_device (&shost->shost_dev);put_device (&shost->shost_gendev);return NULL ;
Greg KH
maintainerQuoted 46 lines · Tzung-Bi Shih
Quoted 13 lines · Greg KH
Quoted 12 lines · Tzung-Bi Shih
kobj->nameshould be freed by kfree_const()[1][2]. Correct it.[1] https://elixir.bootlin.com/linux/v6.18/source/lib/kasprintf.c#L41
[2] https://elixir.bootlin.com/linux/v6.18/source/lib/kobject.c#L695Fixes b49493f99690 ("Fix a memory leak in scsi_host_dev_release()")Signed-off-by Tzung-Bi Shih <[email protected]>--- drivers/scsi/hosts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 14 lines
drivers/scsi/hosts.c static void scsi_host_dev_release(struct device *dev) · line 376
Quoted 10 lines
drivers/scsi/hosts.c +1
-1@@ -373,7 +373,7 @@ static void scsi_host_dev_release(struct device *dev)* name as well as the proc dir structure are leaked. */ scsi_proc_hostdir_rm (shost->hostt); -kfree (dev_name (&shost->shost_dev)); +kfree_const (dev_name (&shost->shost_dev));Shouldn't the struct device name be freed by the driver core for this device when it goes out of scope? Why is it being manually freed here at all?
Ah, correct. I think the following patch is what it really needs:
drivers/scsi/hosts.c +0
-1@@ -373,7 +373,6 @@ static void scsi_host_dev_release(struct device *dev)* name as well as the proc dir structure are leaked. */ scsi_proc_hostdir_rm (shost->hostt); -kfree (dev_name (&shost->shost_dev)); }kfree (shost->shost_data);@@ -548,11 +547,7 @@ struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv goto fail;return shost;
drivers/scsi/hosts.c struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv · line 553
Quoted 10 lines
drivers/scsi/hosts.c +1
-5fail: -/* -* Host state is still SHOST_CREATED and that is enough to release -* ->shost_gendev. scsi_host_dev_release() will free -* dev_name(&shost->shost_dev). -*/ +put_device (&shost->shost_dev);put_device (&shost->shost_gendev);return NULL ;
Can you test this to verify that the leak you were seeing is actually now handled?
thanks,
greg k-h
Quoted 56 lines · Greg KH
Quoted 55 lines · Tzung-Bi Shih
Quoted 13 lines · Greg KH
Quoted 12 lines · Tzung-Bi Shih
kobj->nameshould be freed by kfree_const()[1][2]. Correct it.[1] https://elixir.bootlin.com/linux/v6.18/source/lib/kasprintf.c#L41
[2] https://elixir.bootlin.com/linux/v6.18/source/lib/kobject.c#L695Fixes b49493f99690 ("Fix a memory leak in scsi_host_dev_release()")Signed-off-by Tzung-Bi Shih <[email protected]>--- drivers/scsi/hosts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 14 lines
drivers/scsi/hosts.c static void scsi_host_dev_release(struct device *dev) · line 376
Quoted 10 lines
drivers/scsi/hosts.c +1
-1@@ -373,7 +373,7 @@ static void scsi_host_dev_release(struct device *dev)* name as well as the proc dir structure are leaked. */ scsi_proc_hostdir_rm (shost->hostt); -kfree (dev_name (&shost->shost_dev)); +kfree_const (dev_name (&shost->shost_dev));Shouldn't the struct device name be freed by the driver core for this device when it goes out of scope? Why is it being manually freed here at all?
Ah, correct. I think the following patch is what it really needs:
drivers/scsi/hosts.c +1
-6@@ -373,7 +373,6 @@ static void scsi_host_dev_release(struct device *dev)* name as well as the proc dir structure are leaked. */ scsi_proc_hostdir_rm (shost->hostt); -kfree (dev_name (&shost->shost_dev)); }kfree (shost->shost_data);@@ -548,11 +547,7 @@ struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv goto fail;return shost; fail: -/* -* Host state is still SHOST_CREATED and that is enough to release -* ->shost_gendev. scsi_host_dev_release() will free -* dev_name(&shost->shost_dev). -*/ +put_device (&shost->shost_dev);put_device (&shost->shost_gendev);
Quoted 1 line
drivers/scsi/hosts.c struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv · line 553
Quoted 1 line
drivers/scsi/hosts.c +0
-0return NULL ;
The patch doesn't work well. It can cause an underflow on the reference count of &shost->shost_gendev. [3] is a more appropriate fix.
Quoted 2 lines
Can you test this to verify that the leak you were seeing is actually now handled?
To clarify, the patch wasn't motivated by the leak. But I can reproduce the leak by reverting b49493f99690, manual fault injection, rebinding the driver, and kmemleak. [3] is tested by the scenario.