All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix a memory leak in scsi_host_dev_release()
@ 2015-11-18 22:56 Bart Van Assche
  2015-11-20 11:52 ` Christoph Hellwig
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Bart Van Assche @ 2015-11-18 22:56 UTC (permalink / raw)
  To: James Bottomley
  Cc: Martin K. Petersen, Christoph Hellwig, Hannes Reinecke, linux-scsi

Avoid that kmemleak reports the following memory leak if a
SCSI LLD calls scsi_host_alloc() and scsi_host_put() but neither
scsi_host_add() nor scsi_host_remove(). The following shell
command triggers that scenario:

for ((i=0; i<2; i++)); do
  srp_daemon -oac |
  while read line; do
    echo $line >/sys/class/infiniband_srp/srp-mlx4_0-1/add_target
  done
done

unreferenced object 0xffff88021b24a220 (size 8):
  comm "srp_daemon", pid 56421, jiffies 4295006762 (age 4240.750s)
  hex dump (first 8 bytes):
    68 6f 73 74 35 38 00 a5                          host58..
  backtrace:
    [<ffffffff8151014a>] kmemleak_alloc+0x7a/0xc0
    [<ffffffff81165c1e>] __kmalloc_track_caller+0xfe/0x160
    [<ffffffff81260d2b>] kvasprintf+0x5b/0x90
    [<ffffffff81260e2d>] kvasprintf_const+0x8d/0xb0
    [<ffffffff81254b0c>] kobject_set_name_vargs+0x3c/0xa0
    [<ffffffff81337e3c>] dev_set_name+0x3c/0x40
    [<ffffffff81355757>] scsi_host_alloc+0x327/0x4b0
    [<ffffffffa03edc8e>] srp_create_target+0x4e/0x8a0 [ib_srp]
    [<ffffffff8133778b>] dev_attr_store+0x1b/0x20
    [<ffffffff811f27fa>] sysfs_kf_write+0x4a/0x60
    [<ffffffff811f1e8e>] kernfs_fop_write+0x14e/0x180
    [<ffffffff81176eef>] __vfs_write+0x2f/0xf0
    [<ffffffff811771e4>] vfs_write+0xa4/0x100
    [<ffffffff81177c64>] SyS_write+0x54/0xc0
    [<ffffffff8151b257>] entry_SYSCALL_64_fastpath+0x12/0x6f

Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Hannes Reinecke <hare@suse.de>
Cc: stable <stable@vger.kernel.org>
---
 drivers/scsi/hosts.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c
index 323982f..82ac1cd 100644
--- a/drivers/scsi/hosts.c
+++ b/drivers/scsi/hosts.c
@@ -333,6 +333,17 @@ static void scsi_host_dev_release(struct device *dev)
 		kfree(queuedata);
 	}
 
+	if (shost->shost_state == SHOST_CREATED) {
+		/*
+		 * Free the shost_dev device name here if scsi_host_alloc()
+		 * and scsi_host_put() have been called but neither
+		 * scsi_host_add() nor scsi_host_remove() has been called.
+		 * This avoids that the memory allocated for the shost_dev
+		 * name is leaked.
+		 */
+		kfree(dev_name(&shost->shost_dev));
+	}
+
 	scsi_destroy_command_freelist(shost);
 	if (shost_use_blk_mq(shost)) {
 		if (shost->tag_set.tags)
-- 
2.1.4


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] Fix a memory leak in scsi_host_dev_release()
  2015-11-18 22:56 [PATCH] Fix a memory leak in scsi_host_dev_release() Bart Van Assche
@ 2015-11-20 11:52 ` Christoph Hellwig
  2015-11-20 17:49     ` Bart Van Assche
  2015-11-24 12:55 ` Johannes Thumshirn
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Christoph Hellwig @ 2015-11-20 11:52 UTC (permalink / raw)
  To: Bart Van Assche
  Cc: James Bottomley, Martin K. Petersen, Christoph Hellwig,
	Hannes Reinecke, linux-scsi, linux-kernel, Greg Kroah-Hartman

Hi Bart,

the memory leak looks real, and your fix looks corret, but I still
don't like it.

I think it's reasonable for SCSI to assume that the final put_device
fully frees the struct device including the name pointer that is
assigned entirely behind the back of the caller.

So I think the fix for this probably should be in the driver core.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Fix a memory leak in scsi_host_dev_release()
  2015-11-20 11:52 ` Christoph Hellwig
@ 2015-11-20 17:49     ` Bart Van Assche
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2015-11-20 17:49 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: James Bottomley, Martin K. Petersen, Hannes Reinecke, linux-scsi,
	linux-kernel, Greg Kroah-Hartman

On 11/20/2015 03:52 AM, Christoph Hellwig wrote:
> the memory leak looks real, and your fix looks corret, but I still
> don't like it.
>
> I think it's reasonable for SCSI to assume that the final put_device
> fully frees the struct device including the name pointer that is
> assigned entirely behind the back of the caller.
>
> So I think the fix for this probably should be in the driver core.

Hello Christoph,

Thanks for the feedback. However, I'm not sure this can be fixed by 
modifying the driver core. If scsi_host_remove() is not called the SCSI 
core doesn't call put_device(&shost->shost_dev). I will post a second 
version of this patch that ensures that the SCSI core always calls 
put_device(&shost->shost_dev).

Bart.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Fix a memory leak in scsi_host_dev_release()
@ 2015-11-20 17:49     ` Bart Van Assche
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2015-11-20 17:49 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: James Bottomley, Martin K. Petersen, Hannes Reinecke, linux-scsi,
	linux-kernel, Greg Kroah-Hartman

On 11/20/2015 03:52 AM, Christoph Hellwig wrote:
> the memory leak looks real, and your fix looks corret, but I still
> don't like it.
>
> I think it's reasonable for SCSI to assume that the final put_device
> fully frees the struct device including the name pointer that is
> assigned entirely behind the back of the caller.
>
> So I think the fix for this probably should be in the driver core.

Hello Christoph,

Thanks for the feedback. However, I'm not sure this can be fixed by 
modifying the driver core. If scsi_host_remove() is not called the SCSI 
core doesn't call put_device(&shost->shost_dev). I will post a second 
version of this patch that ensures that the SCSI core always calls 
put_device(&shost->shost_dev).

Bart.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Fix a memory leak in scsi_host_dev_release()
  2015-11-20 17:49     ` Bart Van Assche
  (?)
@ 2015-11-22 15:08     ` Christoph Hellwig
  -1 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2015-11-22 15:08 UTC (permalink / raw)
  To: Bart Van Assche
  Cc: Christoph Hellwig, James Bottomley, Martin K. Petersen,
	Hannes Reinecke, linux-scsi, linux-kernel, Greg Kroah-Hartman

On Fri, Nov 20, 2015 at 09:49:42AM -0800, Bart Van Assche wrote:
> On 11/20/2015 03:52 AM, Christoph Hellwig wrote:
> >the memory leak looks real, and your fix looks corret, but I still
> >don't like it.
> >
> >I think it's reasonable for SCSI to assume that the final put_device
> >fully frees the struct device including the name pointer that is
> >assigned entirely behind the back of the caller.
> >
> >So I think the fix for this probably should be in the driver core.
> 
> Hello Christoph,
> 
> Thanks for the feedback. However, I'm not sure this can be fixed by
> modifying the driver core. If scsi_host_remove() is not called the SCSI core
> doesn't call put_device(&shost->shost_dev). I will post a second version of
> this patch that ensures that the SCSI core always calls
> put_device(&shost->shost_dev).


Oh, I see.  The release method is called on shost_gendev, but the
name that needs to be freed is in shost_dev.  I take my comment on the
core back.

Let's get this patch in for now and see if we can do something about the
creative driver model (ab-)use for struct Scsi_Host in the long run.

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Fix a memory leak in scsi_host_dev_release()
  2015-11-18 22:56 [PATCH] Fix a memory leak in scsi_host_dev_release() Bart Van Assche
  2015-11-20 11:52 ` Christoph Hellwig
@ 2015-11-24 12:55 ` Johannes Thumshirn
  2015-11-24 14:50 ` Sagi Grimberg
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Johannes Thumshirn @ 2015-11-24 12:55 UTC (permalink / raw)
  To: Bart Van Assche, James Bottomley
  Cc: Martin K. Petersen, Christoph Hellwig, Hannes Reinecke, linux-scsi

On Wed, 2015-11-18 at 14:56 -0800, Bart Van Assche wrote:
> Avoid that kmemleak reports the following memory leak if a
> SCSI LLD calls scsi_host_alloc() and scsi_host_put() but neither
> scsi_host_add() nor scsi_host_remove(). The following shell
> command triggers that scenario:
> 
> for ((i=0; i<2; i++)); do
>   srp_daemon -oac |
>   while read line; do
>     echo $line >/sys/class/infiniband_srp/srp-mlx4_0-1/add_target
>   done
> done
> 
> unreferenced object 0xffff88021b24a220 (size 8):
>   comm "srp_daemon", pid 56421, jiffies 4295006762 (age 4240.750s)
>   hex dump (first 8 bytes):
>     68 6f 73 74 35 38 00 a5                          host58..
>   backtrace:
>     [<ffffffff8151014a>] kmemleak_alloc+0x7a/0xc0
>     [<ffffffff81165c1e>] __kmalloc_track_caller+0xfe/0x160
>     [<ffffffff81260d2b>] kvasprintf+0x5b/0x90
>     [<ffffffff81260e2d>] kvasprintf_const+0x8d/0xb0
>     [<ffffffff81254b0c>] kobject_set_name_vargs+0x3c/0xa0
>     [<ffffffff81337e3c>] dev_set_name+0x3c/0x40
>     [<ffffffff81355757>] scsi_host_alloc+0x327/0x4b0
>     [<ffffffffa03edc8e>] srp_create_target+0x4e/0x8a0 [ib_srp]
>     [<ffffffff8133778b>] dev_attr_store+0x1b/0x20
>     [<ffffffff811f27fa>] sysfs_kf_write+0x4a/0x60
>     [<ffffffff811f1e8e>] kernfs_fop_write+0x14e/0x180
>     [<ffffffff81176eef>] __vfs_write+0x2f/0xf0
>     [<ffffffff811771e4>] vfs_write+0xa4/0x100
>     [<ffffffff81177c64>] SyS_write+0x54/0xc0
>     [<ffffffff8151b257>] entry_SYSCALL_64_fastpath+0x12/0x6f
> 
> Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com>
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Hannes Reinecke <hare@suse.de>
> Cc: stable <stable@vger.kernel.org>
> ---
>  drivers/scsi/hosts.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c
> index 323982f..82ac1cd 100644
> --- a/drivers/scsi/hosts.c
> +++ b/drivers/scsi/hosts.c
> @@ -333,6 +333,17 @@ static void scsi_host_dev_release(struct device
> *dev)
>  		kfree(queuedata);
>  	}
>  
> +	if (shost->shost_state == SHOST_CREATED) {
> +		/*
> +		 * Free the shost_dev device name here if
> scsi_host_alloc()
> +		 * and scsi_host_put() have been called but neither
> +		 * scsi_host_add() nor scsi_host_remove() has been
> called.
> +		 * This avoids that the memory allocated for the
> shost_dev
> +		 * name is leaked.
> +		 */
> +		kfree(dev_name(&shost->shost_dev));
> +	}
> +
>  	scsi_destroy_command_freelist(shost);
>  	if (shost_use_blk_mq(shost)) {
>  		if (shost->tag_set.tags)

Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Fix a memory leak in scsi_host_dev_release()
  2015-11-18 22:56 [PATCH] Fix a memory leak in scsi_host_dev_release() Bart Van Assche
  2015-11-20 11:52 ` Christoph Hellwig
  2015-11-24 12:55 ` Johannes Thumshirn
@ 2015-11-24 14:50 ` Sagi Grimberg
  2015-11-24 23:10 ` Lee Duncan
  2015-12-01  1:46 ` Martin K. Petersen
  4 siblings, 0 replies; 9+ messages in thread
From: Sagi Grimberg @ 2015-11-24 14:50 UTC (permalink / raw)
  To: Bart Van Assche, James Bottomley
  Cc: Martin K. Petersen, Christoph Hellwig, Hannes Reinecke, linux-scsi



On 19/11/2015 00:56, Bart Van Assche wrote:
> Avoid that kmemleak reports the following memory leak if a
> SCSI LLD calls scsi_host_alloc() and scsi_host_put() but neither
> scsi_host_add() nor scsi_host_remove(). The following shell
> command triggers that scenario:

Looks good,

Reviewed-by: Sagi Grimberg <sagig@mellanox.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Fix a memory leak in scsi_host_dev_release()
  2015-11-18 22:56 [PATCH] Fix a memory leak in scsi_host_dev_release() Bart Van Assche
                   ` (2 preceding siblings ...)
  2015-11-24 14:50 ` Sagi Grimberg
@ 2015-11-24 23:10 ` Lee Duncan
  2015-12-01  1:46 ` Martin K. Petersen
  4 siblings, 0 replies; 9+ messages in thread
From: Lee Duncan @ 2015-11-24 23:10 UTC (permalink / raw)
  To: Bart Van Assche, James Bottomley
  Cc: Martin K. Petersen, Christoph Hellwig, Hannes Reinecke, linux-scsi

On 11/18/2015 02:56 PM, Bart Van Assche wrote:
> Avoid that kmemleak reports the following memory leak if a
> SCSI LLD calls scsi_host_alloc() and scsi_host_put() but neither
> scsi_host_add() nor scsi_host_remove(). The following shell
> command triggers that scenario:
> 
> for ((i=0; i<2; i++)); do
>   srp_daemon -oac |
>   while read line; do
>     echo $line >/sys/class/infiniband_srp/srp-mlx4_0-1/add_target
>   done
> done
> 
> unreferenced object 0xffff88021b24a220 (size 8):
>   comm "srp_daemon", pid 56421, jiffies 4295006762 (age 4240.750s)
>   hex dump (first 8 bytes):
>     68 6f 73 74 35 38 00 a5                          host58..
>   backtrace:
>     [<ffffffff8151014a>] kmemleak_alloc+0x7a/0xc0
>     [<ffffffff81165c1e>] __kmalloc_track_caller+0xfe/0x160
>     [<ffffffff81260d2b>] kvasprintf+0x5b/0x90
>     [<ffffffff81260e2d>] kvasprintf_const+0x8d/0xb0
>     [<ffffffff81254b0c>] kobject_set_name_vargs+0x3c/0xa0
>     [<ffffffff81337e3c>] dev_set_name+0x3c/0x40
>     [<ffffffff81355757>] scsi_host_alloc+0x327/0x4b0
>     [<ffffffffa03edc8e>] srp_create_target+0x4e/0x8a0 [ib_srp]
>     [<ffffffff8133778b>] dev_attr_store+0x1b/0x20
>     [<ffffffff811f27fa>] sysfs_kf_write+0x4a/0x60
>     [<ffffffff811f1e8e>] kernfs_fop_write+0x14e/0x180
>     [<ffffffff81176eef>] __vfs_write+0x2f/0xf0
>     [<ffffffff811771e4>] vfs_write+0xa4/0x100
>     [<ffffffff81177c64>] SyS_write+0x54/0xc0
>     [<ffffffff8151b257>] entry_SYSCALL_64_fastpath+0x12/0x6f
> 
> Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com>
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Hannes Reinecke <hare@suse.de>
> Cc: stable <stable@vger.kernel.org>
> ---
>  drivers/scsi/hosts.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c
> index 323982f..82ac1cd 100644
> --- a/drivers/scsi/hosts.c
> +++ b/drivers/scsi/hosts.c
> @@ -333,6 +333,17 @@ static void scsi_host_dev_release(struct device *dev)
>  		kfree(queuedata);
>  	}
>  
> +	if (shost->shost_state == SHOST_CREATED) {
> +		/*
> +		 * Free the shost_dev device name here if scsi_host_alloc()
> +		 * and scsi_host_put() have been called but neither
> +		 * scsi_host_add() nor scsi_host_remove() has been called.
> +		 * This avoids that the memory allocated for the shost_dev
> +		 * name is leaked.
> +		 */
> +		kfree(dev_name(&shost->shost_dev));
> +	}
> +
>  	scsi_destroy_command_freelist(shost);
>  	if (shost_use_blk_mq(shost)) {
>  		if (shost->tag_set.tags)
> 

Reviewed-by: Lee Duncan <lduncan@suse.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Fix a memory leak in scsi_host_dev_release()
  2015-11-18 22:56 [PATCH] Fix a memory leak in scsi_host_dev_release() Bart Van Assche
                   ` (3 preceding siblings ...)
  2015-11-24 23:10 ` Lee Duncan
@ 2015-12-01  1:46 ` Martin K. Petersen
  4 siblings, 0 replies; 9+ messages in thread
From: Martin K. Petersen @ 2015-12-01  1:46 UTC (permalink / raw)
  To: Bart Van Assche
  Cc: James Bottomley, Martin K. Petersen, Christoph Hellwig,
	Hannes Reinecke, linux-scsi

>>>>> "Bart" == Bart Van Assche <bart.vanassche@sandisk.com> writes:

Bart> Avoid that kmemleak reports the following memory leak if a SCSI
Bart> LLD calls scsi_host_alloc() and scsi_host_put() but neither
Bart> scsi_host_add() nor scsi_host_remove(). The following shell
Bart> command triggers that scenario:

Applied to 4.4/scsi-queue.

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-12-01  1:46 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-18 22:56 [PATCH] Fix a memory leak in scsi_host_dev_release() Bart Van Assche
2015-11-20 11:52 ` Christoph Hellwig
2015-11-20 17:49   ` Bart Van Assche
2015-11-20 17:49     ` Bart Van Assche
2015-11-22 15:08     ` Christoph Hellwig
2015-11-24 12:55 ` Johannes Thumshirn
2015-11-24 14:50 ` Sagi Grimberg
2015-11-24 23:10 ` Lee Duncan
2015-12-01  1:46 ` Martin K. Petersen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.