* [PATCH] sg.c: fix a memory leak in devices seq_file implementation
@ 2005-08-19 12:27 Jan Blunck
[not found] ` <200508232057.50334.ioe-lkml@rameria.de>
0 siblings, 1 reply; 2+ messages in thread
From: Jan Blunck @ 2005-08-19 12:27 UTC (permalink / raw)
To: linux-scsi; +Cc: Andrew Morton, Linux-Kernel Mailing List
[-- Attachment #1: Type: text/plain, Size: 686 bytes --]
I know that scsi procfs is legacy code but this is a fix for a memory leak.
While reading through sg.c I realized that the implementation of
/proc/scsi/sg/devices with seq_file is leaking memory due to freeing the
pointer returned by the next() iterator method. Since next() might
return NULL or an error this is wrong. This patch fixes it through using
the seq_files private field for holding the reference to the iterator
object.
Here is a small bash script to trigger the leak. Use slabtop to watch
the size-32 usage grow and grow.
[--snipp--]
#!/bin/sh
while true; do
cat /proc/scsi/sg/devices > /dev/null
done
[--snipp--]
Signed-off-by: Jan Blunck <j.blunck@tu-harburg.de>
[-- Attachment #2: scsi_sg.c-dev_seq_file-fix.diff --]
[-- Type: text/x-patch, Size: 1211 bytes --]
drivers/scsi/sg.c | 16 +++++++++-------
1 files changed, 9 insertions(+), 7 deletions(-)
Index: experimental-jb/drivers/scsi/sg.c
===================================================================
--- experimental-jb.orig/drivers/scsi/sg.c
+++ experimental-jb/drivers/scsi/sg.c
@@ -2971,23 +2971,22 @@ static void * dev_seq_start(struct seq_f
{
struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
+ s->private = it;
if (! it)
return NULL;
+
if (NULL == sg_dev_arr)
- goto err1;
+ return NULL;
it->index = *pos;
it->max = sg_last_dev();
if (it->index >= it->max)
- goto err1;
+ return NULL;
return it;
-err1:
- kfree(it);
- return NULL;
}
static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
- struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
+ struct sg_proc_deviter * it = s->private;
*pos = ++it->index;
return (it->index < it->max) ? it : NULL;
@@ -2995,7 +2994,10 @@ static void * dev_seq_next(struct seq_fi
static void dev_seq_stop(struct seq_file *s, void *v)
{
- kfree (v);
+ struct sg_proc_deviter *it = s->private;
+
+ if (it)
+ kfree (it);
}
static int sg_proc_open_dev(struct inode *inode, struct file *file)
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] sg.c: fix a memory leak in devices seq_file implementation (2nd)
[not found] ` <200508232057.50334.ioe-lkml@rameria.de>
@ 2005-08-23 20:25 ` Jan Blunck
0 siblings, 0 replies; 2+ messages in thread
From: Jan Blunck @ 2005-08-23 20:25 UTC (permalink / raw)
To: Ingo Oeser; +Cc: linux-scsi, Andrew Morton, Linux-Kernel Mailing List
[-- Attachment #1: Type: text/plain, Size: 250 bytes --]
Ingo Oeser schrieb:
>
> kfree() checks its argument, so instead of
>
> if (pointer)
> kfree(pointer);
>
> just do
>
> kfree(pointer);
>
This is an updated patch. Removed the unnecessary if.
Signed-off-by: Jan Blunck <j.blunck@tu-harburg.de>
[-- Attachment #2: scsi_sg.c-dev_seq_file-fix.diff --]
[-- Type: text/x-patch, Size: 1181 bytes --]
drivers/scsi/sg.c | 15 ++++++++-------
1 files changed, 8 insertions(+), 7 deletions(-)
Index: linux-2.6/drivers/scsi/sg.c
===================================================================
--- linux-2.6.orig/drivers/scsi/sg.c
+++ linux-2.6/drivers/scsi/sg.c
@@ -2971,23 +2971,22 @@ static void * dev_seq_start(struct seq_f
{
struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
+ s->private = it;
if (! it)
return NULL;
+
if (NULL == sg_dev_arr)
- goto err1;
+ return NULL;
it->index = *pos;
it->max = sg_last_dev();
if (it->index >= it->max)
- goto err1;
+ return NULL;
return it;
-err1:
- kfree(it);
- return NULL;
}
static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
- struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
+ struct sg_proc_deviter * it = s->private;
*pos = ++it->index;
return (it->index < it->max) ? it : NULL;
@@ -2995,7 +2994,9 @@ static void * dev_seq_next(struct seq_fi
static void dev_seq_stop(struct seq_file *s, void *v)
{
- kfree (v);
+ struct sg_proc_deviter * it = s->private;
+
+ kfree (it);
}
static int sg_proc_open_dev(struct inode *inode, struct file *file)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-08-23 20:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-08-19 12:27 [PATCH] sg.c: fix a memory leak in devices seq_file implementation Jan Blunck
[not found] ` <200508232057.50334.ioe-lkml@rameria.de>
2005-08-23 20:25 ` [PATCH] sg.c: fix a memory leak in devices seq_file implementation (2nd) Jan Blunck
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).