linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scsi: sg: fix memory leak in sg_build_indirect
@ 2020-04-13 11:32 Li Bin
  2020-04-13 22:15 ` Douglas Gilbert
  0 siblings, 1 reply; 4+ messages in thread
From: Li Bin @ 2020-04-13 11:32 UTC (permalink / raw)
  To: dgilbert, jejb, martin.petersen
  Cc: linux-scsi, linux-kernel, huawei.libin, xiexiuqi

Fix a memory leak when there have failed, that we should free the pages
under the condition rem_sz > 0.

Signed-off-by: Li Bin <huawei.libin@huawei.com>
---
 drivers/scsi/sg.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 4e6af592..8441ac5 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -1959,8 +1959,12 @@ static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned lon
 			 k, rem_sz));
 
 	schp->bufflen = blk_size;
-	if (rem_sz > 0)	/* must have failed */
+	if (rem_sz > 0)	{ /* must have failed */
+		for (i = 0; i < k; i++)
+			__free_pages(schp->pages[i], order);
+
 		return -ENOMEM;
+	}
 	return 0;
 out:
 	for (i = 0; i < k; i++)
-- 
1.7.12.4


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

* Re: [PATCH] scsi: sg: fix memory leak in sg_build_indirect
  2020-04-13 11:32 [PATCH] scsi: sg: fix memory leak in sg_build_indirect Li Bin
@ 2020-04-13 22:15 ` Douglas Gilbert
  0 siblings, 0 replies; 4+ messages in thread
From: Douglas Gilbert @ 2020-04-13 22:15 UTC (permalink / raw)
  To: Li Bin, jejb, martin.petersen; +Cc: linux-scsi, linux-kernel, xiexiuqi

On 2020-04-13 7:32 a.m., Li Bin wrote:
> Fix a memory leak when there have failed, that we should free the pages
> under the condition rem_sz > 0.

May I paraphrase the above:
"Fix a memory leak that occurs when alloc_pages() succeeds several
  times before failing. This condition is noticed when rem_sz > 0."

> 
> Signed-off-by: Li Bin <huawei.libin@huawei.com>
> ---
>   drivers/scsi/sg.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
> index 4e6af592..8441ac5 100644
> --- a/drivers/scsi/sg.c
> +++ b/drivers/scsi/sg.c
> @@ -1959,8 +1959,12 @@ static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned lon

It is the sg_build_indirect() function not sg_compat_ioctl() as suggested
above by git. Can be get a replacement for git :-)

>   			 k, rem_sz));
>   
>   	schp->bufflen = blk_size;
> -	if (rem_sz > 0)	/* must have failed */
> +	if (rem_sz > 0)	{ /* must have failed */
> +		for (i = 0; i < k; i++)
> +			__free_pages(schp->pages[i], order);
> +
>   		return -ENOMEM;

It is easier, and less code, to replace 'return -ENOMEM'; with
'goto out'. Or even simpler:

     if (likely(rem_sz == 0))
	return 0;
out:
      ........

Doug Gilbert


BTW I spotted this one during the sg driver rewrite and fixed it.
Note that this bug and several others like it won't be fixed by
me while the sg driver rewrite is pending.

> +	}
>   	return 0;
>   out:
>   	for (i = 0; i < k; i++)
> 


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

* Re: [PATCH] scsi: sg: fix memory leak in sg_build_indirect
  2020-04-13 19:35 Markus Elfring
@ 2020-04-14  1:39 ` Douglas Gilbert
  0 siblings, 0 replies; 4+ messages in thread
From: Douglas Gilbert @ 2020-04-14  1:39 UTC (permalink / raw)
  To: Markus Elfring, Li Bin, linux-scsi
  Cc: linux-kernel, James E. J. Bottomley, Martin K. Petersen, Xie XiuQi

On 2020-04-13 3:35 p.m., Markus Elfring wrote:
>> Fix a memory leak when there have failed, that we should free the pages
>> under the condition rem_sz > 0.
> 
> I suggest to improve the change description.
> 
> * Please use an imperative wording.

nominative, vocative, accusative, genitive ...

I have made some comment suggestions in response to the original posts
from the author.

> * Will the tag “Fixes” become relevant?

Yes, both patches from the author are fixes. These leaks would typically
appear on a system under resource pressure (specifically low memory) and
would tend to make the situation worse.

Doug Gilbert



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

* Re: [PATCH] scsi: sg: fix memory leak in sg_build_indirect
@ 2020-04-13 19:35 Markus Elfring
  2020-04-14  1:39 ` Douglas Gilbert
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Elfring @ 2020-04-13 19:35 UTC (permalink / raw)
  To: Li Bin, linux-scsi
  Cc: linux-kernel, Doug Gilbert, James E. J. Bottomley,
	Martin K. Petersen, Xie XiuQi

> Fix a memory leak when there have failed, that we should free the pages
> under the condition rem_sz > 0.

I suggest to improve the change description.

* Please use an imperative wording.

* Will the tag “Fixes” become relevant?

Regards,
Markus

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

end of thread, other threads:[~2020-04-14  1:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-13 11:32 [PATCH] scsi: sg: fix memory leak in sg_build_indirect Li Bin
2020-04-13 22:15 ` Douglas Gilbert
2020-04-13 19:35 Markus Elfring
2020-04-14  1:39 ` Douglas Gilbert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).