summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey Dobriyan <adobriyan@sw.ru>2014-09-09 00:54:42 +0000
committerJohannes Weiner <hannes@cmpxchg.org>2014-09-09 00:54:42 +0000
commitea8f1c1784cb4f9089eb57c6d5201796b0110b4c (patch)
tree1b169ebbb5601c60f94a1d21759e13e9f3ac4518
parentc9126ecc7b0fc76d54476f678b75cdb1fddf2b46 (diff)
single_open/seq_release leak diagnosticsv3.17-rc2-mmotm-2014-08-29-15-15
"[PATCH] Fix leaks on /proc/{*/sched,sched_debug,timer_list,timer_stats}" and "[PATCH] Fix leak on /proc/lockdep_stats" fixed 5 leaks which happen if one uses single_open() as .open function and seq_release() as .release function. Let's add small amount of runtime checking. Sample output is: memory leak: 'timer_list' WARNING: at fs/seq_file.c:289 seq_release() [<c015e311>] seq_release+0x53/0x68 [<c0171bd8>] proc_reg_release+0x63/0x74 [<c0149877>] __fput+0x28/0xd3 [<c0147681>] filp_close+0x48/0x4f [<c014876a>] sys_close+0x74/0xbe [<c010248e>] sysenter_past_esp+0x5f/0x85 ======================= Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
-rw-r--r--fs/seq_file.c25
-rw-r--r--include/linux/seq_file.h1
2 files changed, 21 insertions, 5 deletions
diff --git a/fs/seq_file.c b/fs/seq_file.c
index 3857b720cb1b..9f3e1e7c7c91 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -362,6 +362,13 @@ EXPORT_SYMBOL(seq_lseek);
int seq_release(struct inode *inode, struct file *file)
{
struct seq_file *m = file->private_data;
+
+ if (m->seq_ops_allocated) {
+ struct dentry *dentry = file->f_dentry;
+ printk("memory leak: '%.*s'\n",
+ dentry->d_name.len, dentry->d_name.name);
+ WARN_ON(1);
+ }
kvfree(m->buf);
kfree(m);
return 0;
@@ -605,9 +612,12 @@ int single_open(struct file *file, int (*show)(struct seq_file *, void *),
op->stop = single_stop;
op->show = show;
res = seq_open(file, op);
- if (!res)
- ((struct seq_file *)file->private_data)->private = data;
- else
+ if (!res) {
+ struct seq_file *seq = file->private_data;
+
+ seq->private = data;
+ seq->seq_ops_allocated = 1;
+ } else
kfree(op);
}
return res;
@@ -634,8 +644,13 @@ EXPORT_SYMBOL(single_open_size);
int single_release(struct inode *inode, struct file *file)
{
- const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
- int res = seq_release(inode, file);
+ struct seq_file *seq = file->private_data;
+ const struct seq_operations *op = seq->op;
+ int res;
+
+ /* All roads lead to seq_release(), so... */
+ seq->seq_ops_allocated = 0;
+ res = seq_release(inode, file);
kfree(op);
return res;
}
diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index 52e0097f61f0..45ba376e84d1 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -31,6 +31,7 @@ struct seq_file {
struct user_namespace *user_ns;
#endif
void *private;
+ unsigned int seq_ops_allocated:1;
};
struct seq_operations {