Skip to content

Commit 729d70f

Browse files
Jan BlunckLinus Torvalds
authored andcommitted
[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. #!/bin/sh while true; do cat /proc/scsi/sg/devices > /dev/null done Signed-off-by: Jan Blunck <j.blunck@tu-harburg.de> Acked-by: James Bottomley <James.Bottomley@steeleye.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 8126fdb commit 729d70f

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

drivers/scsi/sg.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,31 +2971,30 @@ static void * dev_seq_start(struct seq_file *s, loff_t *pos)
29712971
{
29722972
struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
29732973

2974+
s->private = it;
29742975
if (! it)
29752976
return NULL;
2977+
29762978
if (NULL == sg_dev_arr)
2977-
goto err1;
2979+
return NULL;
29782980
it->index = *pos;
29792981
it->max = sg_last_dev();
29802982
if (it->index >= it->max)
2981-
goto err1;
2983+
return NULL;
29822984
return it;
2983-
err1:
2984-
kfree(it);
2985-
return NULL;
29862985
}
29872986

29882987
static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
29892988
{
2990-
struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2989+
struct sg_proc_deviter * it = s->private;
29912990

29922991
*pos = ++it->index;
29932992
return (it->index < it->max) ? it : NULL;
29942993
}
29952994

29962995
static void dev_seq_stop(struct seq_file *s, void *v)
29972996
{
2998-
kfree (v);
2997+
kfree(s->private);
29992998
}
30002999

30013000
static int sg_proc_open_dev(struct inode *inode, struct file *file)

0 commit comments

Comments
 (0)