tree f92ee98a709264984ef6e3e8d6c3ee4f3462797f
parent 8126fdbc76351bdf99c6737ef4fecf88a22fa538
author Jan Blunck <[EMAIL PROTECTED]> Sun, 28 Aug 2005 01:07:52 -0700
committer Linus Torvalds <[EMAIL PROTECTED]> Sun, 28 Aug 2005 01:22:27 -0700

[PATCH] sg.c: fix a memory leak in devices seq_file implementation

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.


while true; do
        cat /proc/scsi/sg/devices > /dev/null

Signed-off-by: Jan Blunck <[EMAIL PROTECTED]>
Acked-by: James Bottomley <[EMAIL PROTECTED]>
Signed-off-by: Andrew Morton <[EMAIL PROTECTED]>
Signed-off-by: Linus Torvalds <[EMAIL PROTECTED]>

 drivers/scsi/sg.c |   13 ++++++-------
 1 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
--- a/drivers/scsi/sg.c
+++ b/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;
-       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,7 @@ static void * dev_seq_next(struct seq_fi
 static void dev_seq_stop(struct seq_file *s, void *v)
-       kfree (v);
+       kfree(s->private);
 static int sg_proc_open_dev(struct inode *inode, struct file *file)
To unsubscribe from this list: send the line "unsubscribe git-commits-head" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at

Reply via email to