Skip to content

Commit

Permalink
fs: use seq_open_private() for proc_mounts
Browse files Browse the repository at this point in the history
A patchset to remove support for passing pre-allocated struct seq_file to
seq_open().  Such feature is undocumented and prone to error.

In particular, if seq_release() is used in release handler, it will
kfree() a pointer which was not allocated by seq_open().

So this patchset drops support for pre-allocated struct seq_file: it's
only of use in proc_namespace.c and can be easily replaced by using
seq_open_private()/seq_release_private().

Additionally, it documents the use of file->private_data to hold pointer
to struct seq_file by seq_open().

This patch (of 3):

Since patch described below, from v2.6.15-rc1, seq_open() could use a
struct seq_file already allocated by the caller if the pointer to the
structure is stored in file->private_data before calling the function.

    Commit 1abe77b
    Author: Al Viro <[email protected]>
    Date:   Mon Nov 7 17:15:34 2005 -0500

        [PATCH] allow callers of seq_open do allocation themselves

        Allow caller of seq_open() to kmalloc() seq_file + whatever else they
        want and set ->private_data to it.  seq_open() will then abstain from
        doing allocation itself.

Such behavior is only used by mounts_open_common().

In order to drop support for such uncommon feature, proc_mounts is
converted to use seq_open_private(), which take care of allocating the
proc_mounts structure, making it available through ->private in struct
seq_file.

Conversely, proc_mounts is converted to use seq_release_private(), in
order to release the private structure allocated by seq_open_private().

Then, ->private is used directly instead of proc_mounts() macro to access
to the proc_mounts structure.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Yann Droneaud <[email protected]>
Cc: Al Viro <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
ydroneaud authored and torvalds committed Jul 1, 2015
1 parent 0e1cc95 commit ede1bf0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
3 changes: 0 additions & 3 deletions fs/mount.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ static inline void unlock_mount_hash(void)
}

struct proc_mounts {
struct seq_file m;
struct mnt_namespace *ns;
struct path root;
int (*show)(struct seq_file *, struct vfsmount *);
Expand All @@ -127,8 +126,6 @@ struct proc_mounts {
loff_t cached_index;
};

#define proc_mounts(p) (container_of((p), struct proc_mounts, m))

extern const struct seq_operations mounts_op;

extern bool __is_local_mountpoint(struct dentry *dentry);
Expand Down
6 changes: 3 additions & 3 deletions fs/namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ EXPORT_SYMBOL(replace_mount_options);
/* iterator; we want it to have access to namespace_sem, thus here... */
static void *m_start(struct seq_file *m, loff_t *pos)
{
struct proc_mounts *p = proc_mounts(m);
struct proc_mounts *p = m->private;

down_read(&namespace_sem);
if (p->cached_event == p->ns->event) {
Expand All @@ -1247,7 +1247,7 @@ static void *m_start(struct seq_file *m, loff_t *pos)

static void *m_next(struct seq_file *m, void *v, loff_t *pos)
{
struct proc_mounts *p = proc_mounts(m);
struct proc_mounts *p = m->private;

p->cached_mount = seq_list_next(v, &p->ns->list, pos);
p->cached_index = *pos;
Expand All @@ -1261,7 +1261,7 @@ static void m_stop(struct seq_file *m, void *v)

static int m_show(struct seq_file *m, void *v)
{
struct proc_mounts *p = proc_mounts(m);
struct proc_mounts *p = m->private;
struct mount *r = list_entry(v, struct mount, mnt_list);
return p->show(m, &r->mnt);
}
Expand Down
34 changes: 16 additions & 18 deletions fs/proc_namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

static unsigned mounts_poll(struct file *file, poll_table *wait)
{
struct proc_mounts *p = proc_mounts(file->private_data);
struct seq_file *m = file->private_data;
struct proc_mounts *p = m->private;
struct mnt_namespace *ns = p->ns;
unsigned res = POLLIN | POLLRDNORM;
int event;

poll_wait(file, &p->ns->poll, wait);

event = ACCESS_ONCE(ns->event);
if (p->m.poll_event != event) {
p->m.poll_event = event;
if (m->poll_event != event) {
m->poll_event = event;
res |= POLLERR | POLLPRI;
}

Expand Down Expand Up @@ -92,7 +93,7 @@ static void show_type(struct seq_file *m, struct super_block *sb)

static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt)
{
struct proc_mounts *p = proc_mounts(m);
struct proc_mounts *p = m->private;
struct mount *r = real_mount(mnt);
int err = 0;
struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
Expand Down Expand Up @@ -126,7 +127,7 @@ static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt)

static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt)
{
struct proc_mounts *p = proc_mounts(m);
struct proc_mounts *p = m->private;
struct mount *r = real_mount(mnt);
struct super_block *sb = mnt->mnt_sb;
struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
Expand Down Expand Up @@ -186,7 +187,7 @@ static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt)

static int show_vfsstat(struct seq_file *m, struct vfsmount *mnt)
{
struct proc_mounts *p = proc_mounts(m);
struct proc_mounts *p = m->private;
struct mount *r = real_mount(mnt);
struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
struct super_block *sb = mnt_path.dentry->d_sb;
Expand Down Expand Up @@ -236,6 +237,7 @@ static int mounts_open_common(struct inode *inode, struct file *file,
struct mnt_namespace *ns = NULL;
struct path root;
struct proc_mounts *p;
struct seq_file *m;
int ret = -EINVAL;

if (!task)
Expand All @@ -260,26 +262,21 @@ static int mounts_open_common(struct inode *inode, struct file *file,
task_unlock(task);
put_task_struct(task);

ret = -ENOMEM;
p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
if (!p)
ret = seq_open_private(file, &mounts_op, sizeof(struct proc_mounts));
if (ret)
goto err_put_path;

file->private_data = &p->m;
ret = seq_open(file, &mounts_op);
if (ret)
goto err_free;
m = file->private_data;
m->poll_event = ns->event;

p = m->private;
p->ns = ns;
p->root = root;
p->m.poll_event = ns->event;
p->show = show;
p->cached_event = ~0ULL;

return 0;

err_free:
kfree(p);
err_put_path:
path_put(&root);
err_put_ns:
Expand All @@ -290,10 +287,11 @@ static int mounts_open_common(struct inode *inode, struct file *file,

static int mounts_release(struct inode *inode, struct file *file)
{
struct proc_mounts *p = proc_mounts(file->private_data);
struct seq_file *m = file->private_data;
struct proc_mounts *p = m->private;
path_put(&p->root);
put_mnt_ns(p->ns);
return seq_release(inode, file);
return seq_release_private(inode, file);
}

static int mounts_open(struct inode *inode, struct file *file)
Expand Down

0 comments on commit ede1bf0

Please sign in to comment.