procfs has both sets of file operations twice, with and without ->compat_ioctl(), and picks the one with it for entries that have ->proc_compat_ioctl(). That's from commit 778f3dd5a13c ("Fix procfs compat_ioctl regression") when a file without ->compat_ioctl() went through the translation tables in fs/compat_ioctl.c and one with it didn't. Those tables are gone since commit 77b9040195de ("compat_ioctl: simplify the implementation"). compat_sys_ioctl() answers -ENOTTY for a file without ->compat_ioctl() now, which is what proc_reg_compat_ioctl() answers for an entry without ->proc_compat_ioctl(). So always set ->compat_ioctl() and drop the second set of file operations and the flag that picked it. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 4 ---- fs/proc/inode.c | 45 +++------------------------------------------ fs/proc/internal.h | 9 --------- include/linux/proc_fs.h | 1 - 4 files changed, 3 insertions(+), 56 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 9a0a2fcac7c7..e7264f0cf5ab 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -446,10 +446,6 @@ static void pde_set_flags(struct proc_dir_entry *pde) pde->flags |= PROC_ENTRY_PERMANENT; if (proc_ops->proc_read_iter) pde->flags |= PROC_ENTRY_proc_read_iter; -#ifdef CONFIG_COMPAT - if (proc_ops->proc_compat_ioctl) - pde->flags |= PROC_ENTRY_proc_compat_ioctl; -#endif if (proc_ops->proc_lseek) pde->flags |= PROC_ENTRY_proc_lseek; } diff --git a/fs/proc/inode.c b/fs/proc/inode.c index 2d65f3c8ec03..18f6490bfe77 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -410,6 +410,8 @@ static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned } return rv; } +#else +#define proc_reg_compat_ioctl NULL #endif static int pde_mmap(struct proc_dir_entry *pde, struct file *file, struct vm_area_struct *vma) @@ -569,6 +571,7 @@ static const struct file_operations proc_reg_file_ops = { .write = proc_reg_write, .poll = proc_reg_poll, .unlocked_ioctl = proc_reg_unlocked_ioctl, + .compat_ioctl = proc_reg_compat_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, #ifndef CONFIG_MMU @@ -585,39 +588,6 @@ static const struct file_operations proc_iter_file_ops = { .splice_read = copy_splice_read, .poll = proc_reg_poll, .unlocked_ioctl = proc_reg_unlocked_ioctl, - .mmap = proc_reg_mmap, - .get_unmapped_area = proc_reg_get_unmapped_area, -#ifndef CONFIG_MMU - .mmap_capabilities = proc_reg_mmap_capabilities, -#endif - .open = proc_reg_open, - .release = proc_reg_release, -}; - -#ifdef CONFIG_COMPAT -static const struct file_operations proc_reg_file_ops_compat = { - .llseek = proc_reg_llseek, - .read = proc_reg_read, - .write = proc_reg_write, - .poll = proc_reg_poll, - .unlocked_ioctl = proc_reg_unlocked_ioctl, - .compat_ioctl = proc_reg_compat_ioctl, - .mmap = proc_reg_mmap, - .get_unmapped_area = proc_reg_get_unmapped_area, -#ifndef CONFIG_MMU - .mmap_capabilities = proc_reg_mmap_capabilities, -#endif - .open = proc_reg_open, - .release = proc_reg_release, -}; - -static const struct file_operations proc_iter_file_ops_compat = { - .llseek = proc_reg_llseek, - .read_iter = proc_reg_read_iter, - .splice_read = copy_splice_read, - .write = proc_reg_write, - .poll = proc_reg_poll, - .unlocked_ioctl = proc_reg_unlocked_ioctl, .compat_ioctl = proc_reg_compat_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, @@ -627,7 +597,6 @@ static const struct file_operations proc_iter_file_ops_compat = { .open = proc_reg_open, .release = proc_reg_release, }; -#endif static const char *proc_get_link(struct dentry *dentry, struct inode *inode, @@ -683,14 +652,6 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de) inode->i_fop = &proc_iter_file_ops; else inode->i_fop = &proc_reg_file_ops; -#ifdef CONFIG_COMPAT - if (pde_has_proc_compat_ioctl(de)) { - if (pde_has_proc_read_iter(de)) - inode->i_fop = &proc_iter_file_ops_compat; - else - inode->i_fop = &proc_reg_file_ops_compat; - } -#endif } else if (S_ISDIR(inode->i_mode)) { inode->i_op = de->proc_iops; inode->i_fop = de->proc_dir_ops; diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 04f7ce2b24ed..268a0523be12 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -92,15 +92,6 @@ static inline bool pde_has_proc_read_iter(const struct proc_dir_entry *pde) return pde->flags & PROC_ENTRY_proc_read_iter; } -static inline bool pde_has_proc_compat_ioctl(const struct proc_dir_entry *pde) -{ -#ifdef CONFIG_COMPAT - return pde->flags & PROC_ENTRY_proc_compat_ioctl; -#else - return false; -#endif -} - static inline bool pde_has_proc_lseek(const struct proc_dir_entry *pde) { return pde->flags & PROC_ENTRY_proc_lseek; diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 47d7deaeed8f..6ad5150020bf 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -26,7 +26,6 @@ enum { #endif PROC_ENTRY_proc_read_iter = 1U << 1, - PROC_ENTRY_proc_compat_ioctl = 1U << 2, PROC_ENTRY_proc_lseek = 1U << 3, PROC_ENTRY_FORCE_LOOKUP = 1U << 7, -- 2.53.0 Every call into an entry that can go away is wrapped in use_pde() and unuse_pde(), and a permanent entry is called straight away. The read, write, poll, ioctl, mmap and lseek methods all spell that out as the same if-else. The next patches add a few more callers. So put it into pde_pin() and pde_unpin() and use them everywhere. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/inode.c | 116 +++++++++++++++++++++++++++----------------------------- 1 file changed, 55 insertions(+), 61 deletions(-) diff --git a/fs/proc/inode.c b/fs/proc/inode.c index 18f6490bfe77..66a3688757dd 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -206,6 +206,18 @@ static void unuse_pde(struct proc_dir_entry *pde) complete(pde->pde_unload_completion); } +/* Pin an entry that can go away around a call into it, see use_pde(). */ +static bool pde_pin(struct proc_dir_entry *pde) +{ + return pde_is_permanent(pde) || use_pde(pde); +} + +static void pde_unpin(struct proc_dir_entry *pde) +{ + if (!pde_is_permanent(pde)) + unuse_pde(pde); +} + /* * At most 2 contexts can enter this function: the one doing the last * close on the descriptor and whoever is deleting PDE itself. @@ -275,14 +287,12 @@ void proc_entry_rundown(struct proc_dir_entry *de) static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence) { struct proc_dir_entry *pde = PDE(file_inode(file)); - loff_t rv = -EINVAL; + loff_t rv; - if (pde_is_permanent(pde)) { - return pde->proc_ops->proc_lseek(file, offset, whence); - } else if (use_pde(pde)) { - rv = pde->proc_ops->proc_lseek(file, offset, whence); - unuse_pde(pde); - } + if (!pde_pin(pde)) + return -EINVAL; + rv = pde->proc_ops->proc_lseek(file, offset, whence); + pde_unpin(pde); return rv; } @@ -291,13 +301,10 @@ static ssize_t proc_reg_read_iter(struct kiocb *iocb, struct iov_iter *iter) struct proc_dir_entry *pde = PDE(file_inode(iocb->ki_filp)); ssize_t ret; - if (pde_is_permanent(pde)) - return pde->proc_ops->proc_read_iter(iocb, iter); - - if (!use_pde(pde)) + if (!pde_pin(pde)) return -EIO; ret = pde->proc_ops->proc_read_iter(iocb, iter); - unuse_pde(pde); + pde_unpin(pde); return ret; } @@ -312,14 +319,12 @@ static ssize_t pde_read(struct proc_dir_entry *pde, struct file *file, char __us static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { struct proc_dir_entry *pde = PDE(file_inode(file)); - ssize_t rv = -EIO; + ssize_t rv; - if (pde_is_permanent(pde)) { - return pde_read(pde, file, buf, count, ppos); - } else if (use_pde(pde)) { - rv = pde_read(pde, file, buf, count, ppos); - unuse_pde(pde); - } + if (!pde_pin(pde)) + return -EIO; + rv = pde_read(pde, file, buf, count, ppos); + pde_unpin(pde); return rv; } @@ -334,14 +339,12 @@ static ssize_t pde_write(struct proc_dir_entry *pde, struct file *file, const ch static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct proc_dir_entry *pde = PDE(file_inode(file)); - ssize_t rv = -EIO; + ssize_t rv; - if (pde_is_permanent(pde)) { - return pde_write(pde, file, buf, count, ppos); - } else if (use_pde(pde)) { - rv = pde_write(pde, file, buf, count, ppos); - unuse_pde(pde); - } + if (!pde_pin(pde)) + return -EIO; + rv = pde_write(pde, file, buf, count, ppos); + pde_unpin(pde); return rv; } @@ -356,14 +359,12 @@ static __poll_t pde_poll(struct proc_dir_entry *pde, struct file *file, struct p static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts) { struct proc_dir_entry *pde = PDE(file_inode(file)); - __poll_t rv = DEFAULT_POLLMASK; + __poll_t rv; - if (pde_is_permanent(pde)) { - return pde_poll(pde, file, pts); - } else if (use_pde(pde)) { - rv = pde_poll(pde, file, pts); - unuse_pde(pde); - } + if (!pde_pin(pde)) + return DEFAULT_POLLMASK; + rv = pde_poll(pde, file, pts); + pde_unpin(pde); return rv; } @@ -378,14 +379,12 @@ static long pde_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned in static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct proc_dir_entry *pde = PDE(file_inode(file)); - long rv = -ENOTTY; + long rv; - if (pde_is_permanent(pde)) { - return pde_ioctl(pde, file, cmd, arg); - } else if (use_pde(pde)) { - rv = pde_ioctl(pde, file, cmd, arg); - unuse_pde(pde); - } + if (!pde_pin(pde)) + return -ENOTTY; + rv = pde_ioctl(pde, file, cmd, arg); + pde_unpin(pde); return rv; } @@ -401,13 +400,12 @@ static long pde_compat_ioctl(struct proc_dir_entry *pde, struct file *file, unsi static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct proc_dir_entry *pde = PDE(file_inode(file)); - long rv = -ENOTTY; - if (pde_is_permanent(pde)) { - return pde_compat_ioctl(pde, file, cmd, arg); - } else if (use_pde(pde)) { - rv = pde_compat_ioctl(pde, file, cmd, arg); - unuse_pde(pde); - } + long rv; + + if (!pde_pin(pde)) + return -ENOTTY; + rv = pde_compat_ioctl(pde, file, cmd, arg); + pde_unpin(pde); return rv; } #else @@ -425,14 +423,12 @@ static int pde_mmap(struct proc_dir_entry *pde, struct file *file, struct vm_are static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma) { struct proc_dir_entry *pde = PDE(file_inode(file)); - int rv = -EIO; + int rv; - if (pde_is_permanent(pde)) { - return pde_mmap(pde, file, vma); - } else if (use_pde(pde)) { - rv = pde_mmap(pde, file, vma); - unuse_pde(pde); - } + if (!pde_pin(pde)) + return -EIO; + rv = pde_mmap(pde, file, vma); + pde_unpin(pde); return rv; } @@ -457,14 +453,12 @@ proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr, unsigned long flags) { struct proc_dir_entry *pde = PDE(file_inode(file)); - unsigned long rv = -EIO; + unsigned long rv; - if (pde_is_permanent(pde)) { - return pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags); - } else if (use_pde(pde)) { - rv = pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags); - unuse_pde(pde); - } + if (!pde_pin(pde)) + return -EIO; + rv = pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags); + pde_unpin(pde); return rv; } -- 2.53.0 proc_readdir_de() and proc_sys_readdir() both resume a listing after the last entry it emitted and each remembers that entry in the open file with a cursor of its own: the same struct, the same way of setting it, the same release. Keep one in generic.c and let sysctl use it. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 29 ++++++++++++----------------- fs/proc/internal.h | 25 +++++++++++++++++++++++++ fs/proc/proc_sysctl.c | 45 +++++---------------------------------------- 3 files changed, 42 insertions(+), 57 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index e7264f0cf5ab..f6d620a5c3d2 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -301,22 +301,16 @@ struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry, } /* - * Where the next readdir resumes. The entries are counted, so one that is - * added or removed in front of the position would shift what the next call - * returns. The name of the last entry emitted doesn't move. + * Remember @name as the entry the listing is behind at @pos. If this can't + * the next call counts from the start. */ -struct proc_dir_cursor { - loff_t pos; /* the position behind the entry */ - unsigned int len; - char name[NAME_MAX + 1]; -}; - -/* If this can't remember the entry the next call counts from the start. */ -static void proc_dir_cursor_set(struct file *file, struct proc_dir_entry *de, - loff_t pos) +void proc_dir_cursor_set(struct file *file, const char *name, unsigned int len, + loff_t pos) { struct proc_dir_cursor *cursor = file->private_data; + if (len > NAME_MAX) + return; if (!cursor) { cursor = kmalloc(sizeof(*cursor), GFP_KERNEL); if (!cursor) @@ -324,8 +318,8 @@ static void proc_dir_cursor_set(struct file *file, struct proc_dir_entry *de, file->private_data = cursor; } cursor->pos = pos; - cursor->len = de->namelen; - memcpy(cursor->name, de->name, de->namelen); + cursor->len = len; + memcpy(cursor->name, name, len); } int proc_dir_release(struct inode *inode, struct file *file) @@ -346,7 +340,7 @@ int proc_dir_release(struct inode *inode, struct file *file) int proc_readdir_de(struct file *file, struct dir_context *ctx, struct proc_dir_entry *de) { - struct proc_dir_cursor *cursor = file->private_data; + const struct proc_dir_cursor *cursor; struct proc_dir_entry *dir = de; int i; @@ -354,7 +348,8 @@ int proc_readdir_de(struct file *file, struct dir_context *ctx, return 0; read_lock(&proc_subdir_lock); - if (cursor && cursor->pos == ctx->pos) { + cursor = proc_dir_cursor_at(file, ctx->pos); + if (cursor) { /* Resume after the last entry emitted, wherever it is now. */ de = pde_subdir_after(dir, cursor->name, cursor->len); } else { @@ -378,7 +373,7 @@ int proc_readdir_de(struct file *file, struct dir_context *ctx, return 0; } ctx->pos++; - proc_dir_cursor_set(file, de, ctx->pos); + proc_dir_cursor_set(file, de->name, de->namelen, ctx->pos); read_lock(&proc_subdir_lock); /* Removed in the meantime, go on after its name. */ if (RB_EMPTY_NODE(&de->subdir_node)) diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 268a0523be12..6b61d09ac5e5 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -278,6 +278,31 @@ extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int) struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *); extern int proc_readdir(struct file *, struct dir_context *); int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *); + +/* + * Where the next readdir resumes. The entries are counted, so one that is + * added or removed in front of the position would shift what the next call + * returns. The name of the last entry emitted doesn't move. The directory's + * file keeps it, see proc_dir_release(). + */ +struct proc_dir_cursor { + loff_t pos; /* the position behind the entry */ + unsigned int len; + char name[NAME_MAX + 1]; +}; + +/* The entry to resume after, if the position is still the one behind it. */ +static inline const struct proc_dir_cursor * +proc_dir_cursor_at(struct file *file, loff_t pos) +{ + const struct proc_dir_cursor *cursor = file->private_data; + + if (cursor && cursor->pos == pos) + return cursor; + return NULL; +} +void proc_dir_cursor_set(struct file *file, const char *name, unsigned int len, + loff_t pos); int proc_dir_release(struct inode *, struct file *); static inline void pde_get(struct proc_dir_entry *pde) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index 6a92c33db9b5..c1e41a3f8f6b 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -787,36 +787,6 @@ out: return ret; } -/* - * Where the next readdir resumes. The entries are counted, so one that is - * registered or unregistered in front of the position would shift what - * the next call returns. The name of the last entry emitted doesn't move. - */ -struct sysctl_cursor { - loff_t pos; /* the position behind the entry */ - int len; - char name[NAME_MAX + 1]; -}; - -/* If this can't remember the entry the next call counts from the start. */ -static void sysctl_cursor_set(struct file *file, const char *name, int len, - loff_t pos) -{ - struct sysctl_cursor *cursor = file->private_data; - - if (len > NAME_MAX) - return; - if (!cursor) { - cursor = kmalloc(sizeof(*cursor), GFP_KERNEL); - if (!cursor) - return; - file->private_data = cursor; - } - cursor->pos = pos; - cursor->len = len; - memcpy(cursor->name, name, len); -} - /* * The inode number and type of the entry @d holds, instantiating its dentry * if there is none. Needs the header pinned, proc_sys_make_inode() checks @@ -871,7 +841,7 @@ static int proc_sys_fill_dirent(struct file *file, struct sysctl_dirent *d) static int proc_sys_readdir(struct file *file, struct dir_context *ctx) { struct ctl_table_header *head = PROC_I(file_inode(file))->sysctl; - struct sysctl_cursor *cursor = file->private_data; + const struct proc_dir_cursor *cursor; struct sysctl_dirent d = {}; struct ctl_dir *ctl_dir; int err; @@ -888,7 +858,8 @@ static int proc_sys_readdir(struct file *file, struct dir_context *ctx) * that moved the position away from it counts from the start, like * seeking to an arbitrary position always did. */ - if (cursor && cursor->pos == ctx->pos) { + cursor = proc_dir_cursor_at(file, ctx->pos); + if (cursor) { err = sysctl_dir_next(ctl_dir, &d, cursor->name, cursor->len); } else { loff_t pos = 2; @@ -910,19 +881,13 @@ static int proc_sys_readdir(struct file *file, struct dir_context *ctx) break; } ctx->pos++; - sysctl_cursor_set(file, d.name, d.len, ctx->pos); + proc_dir_cursor_set(file, d.name, d.len, ctx->pos); err = sysctl_dir_next(ctl_dir, &d, d.name, d.len); } sysctl_dir_done(&d); return err; } -static int proc_sys_dir_release(struct inode *inode, struct file *file) -{ - kfree(file->private_data); - return 0; -} - static int proc_sys_permission(const struct mnt_idmap *idmap, struct inode *inode, int mask) { @@ -1002,7 +967,7 @@ static const struct file_operations proc_sys_dir_file_operations = { .read = generic_read_dir, .iterate_shared = proc_sys_readdir, .llseek = generic_file_llseek, - .release = proc_sys_dir_release, + .release = proc_dir_release, }; static const struct inode_operations proc_sys_inode_operations = { -- 2.53.0 proc_entry_rundown() waits for the last user of an entry with a completion on its stack that it points the entry at while it waits. That's a pointer in every struct proc_dir_entry for something that happens once in the life of an entry, if at all. Wait on the count itself with wait_var_event() instead and let unuse_pde() wake the waiter with wake_up_var() when the count hits the bias. The waiters sleep on the hashed wait queues behind that, so procfs doesn't need a wait queue of its own for this, neither in the entry nor a global one. kernfs keeps one per root for the same thing, deactivate_waitq. Note that atomic_dec_return() is fully ordered, so the count is visible before wake_up_var() looks for waiters. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/inode.c | 7 +++---- fs/proc/internal.h | 1 - 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/fs/proc/inode.c b/fs/proc/inode.c index 66a3688757dd..8795add1898d 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "internal.h" @@ -203,7 +204,7 @@ static inline int use_pde(struct proc_dir_entry *pde) static void unuse_pde(struct proc_dir_entry *pde) { if (unlikely(atomic_dec_return(&pde->in_use) == BIAS)) - complete(pde->pde_unload_completion); + wake_up_var(&pde->in_use); } /* Pin an entry that can go away around a call into it, see use_pde(). */ @@ -266,11 +267,9 @@ static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo) void proc_entry_rundown(struct proc_dir_entry *de) { - DECLARE_COMPLETION_ONSTACK(c); /* Wait until all existing callers into module are done. */ - de->pde_unload_completion = &c; if (atomic_add_return(BIAS, &de->in_use) != BIAS) - wait_for_completion(&c); + wait_var_event(&de->in_use, atomic_read(&de->in_use) == BIAS); /* ->pde_openers list can't grow from now on. */ diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 6b61d09ac5e5..ab4d7bd1fb75 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -38,7 +38,6 @@ struct proc_dir_entry { struct list_head pde_openers; /* who did ->open, but not ->release */ /* protects ->pde_openers and all struct pde_opener instances */ spinlock_t pde_unload_lock; - struct completion *pde_unload_completion; const struct inode_operations *proc_iops; union { const struct proc_ops *proc_ops; -- 2.53.0 procfs remembers the open files of an entry that has a ->proc_release(), so that it can call the release hook when the entry is removed while files are still open. The list head for that and the lock that protects it sit in every struct proc_dir_entry, for the few entries that need them, and the next patches want to keep more there, the vm_operations of an entry's mappings for a start. Move the list into a struct pde_open_node that only those entries get. It is allocated when the entry is registered, whether an entry needs one follows from its proc_ops, and freed with the entry. So ->open is constant for the life of an entry, nothing has to look it up or pin it, and the removal path stays what it is. kernfs allocates its kernfs_open_node on the first open instead, but there every attribute can be polled and so every one of them would need one. In procfs the entries that need the node are the exception. Permanent entries don't get one, they are never removed, so their release hook only ever runs when a file is closed. The spinlock stays in the entry and now protects what hangs off ->open. Moving it behind the gid fills a hole, so struct proc_dir_entry shrinks from 160 to 144 bytes on x86_64 here, and it ends up at 160 bytes at the end of this series instead of the 184 it would have grown to with the read and write limits and the vm_operations of its mappings in it. That keeps 32 bytes of inline name instead of 8. 34 of the 68 /proc/net names a network namespace gets are longer than 7 characters. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 20 +++++++++++++++++++- fs/proc/inode.c | 23 +++++++++++++---------- fs/proc/internal.h | 16 +++++++++++++--- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index f6d620a5c3d2..84f064a651e4 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -40,6 +40,7 @@ void pde_free(struct proc_dir_entry *pde) kfree(pde->data); if (pde->name != pde->inline_name) kfree(pde->name); + kfree(pde->open); kmem_cache_free(proc_dir_entry_cache, pde); } @@ -445,6 +446,18 @@ static void pde_set_flags(struct proc_dir_entry *pde) pde->flags |= PROC_ENTRY_proc_lseek; } +/* + * procfs has to know the open files of an entry whose ->proc_release() it + * calls at removal. A permanent entry is never removed and skips all that, + * see proc_reg_open(). + */ +static bool pde_needs_open_node(const struct proc_dir_entry *pde) +{ + if (!S_ISREG(pde->mode) || pde_is_permanent(pde)) + return false; + return pde->proc_ops->proc_release; +} + /* returns the registered entry, or frees dp and returns NULL on failure */ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, struct proc_dir_entry *dp) @@ -454,6 +467,12 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, if (!S_ISDIR(dp->mode)) pde_set_flags(dp); + if (pde_needs_open_node(dp)) { + dp->open = kzalloc(sizeof(*dp->open), GFP_KERNEL); + if (!dp->open) + goto out_free_inum; + INIT_LIST_HEAD(&dp->open->files); + } write_lock(&proc_subdir_lock); dp->parent = dir; @@ -533,7 +552,6 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent, ent->subdir = RB_ROOT; refcount_set(&ent->refcnt, 1); spin_lock_init(&ent->pde_unload_lock); - INIT_LIST_HEAD(&ent->pde_openers); proc_set_user(ent, (*parent)->uid, (*parent)->gid); /* Revalidate everything under /proc/${pid}/net */ diff --git a/fs/proc/inode.c b/fs/proc/inode.c index 8795add1898d..56eab69e1dcc 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -271,12 +271,13 @@ void proc_entry_rundown(struct proc_dir_entry *de) if (atomic_add_return(BIAS, &de->in_use) != BIAS) wait_var_event(&de->in_use, atomic_read(&de->in_use) == BIAS); - /* ->pde_openers list can't grow from now on. */ - + /* ->open->files can't grow from now on. */ + if (!de->open) + return; spin_lock(&de->pde_unload_lock); - while (!list_empty(&de->pde_openers)) { + while (!list_empty(&de->open->files)) { struct pde_opener *pdeo; - pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh); + pdeo = list_first_entry(&de->open->files, struct pde_opener, lh); close_pdeo(de, pdeo); spin_lock(&de->pde_unload_lock); } @@ -499,13 +500,13 @@ static int proc_reg_open(struct inode *inode, struct file *file) * 2) rmmod isn't blocked by opening file in /proc and sitting on * the descriptor (including "rmmod foo release hook. + * Save every "struct file" with custom ->release hook. Those entries + * have an open node, see proc_register(). */ if (!use_pde(pde)) return -ENOENT; - const auto release = pde->proc_ops->proc_release; - if (release) { + if (pde->open) { pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL); if (!pdeo) { rv = -ENOMEM; @@ -517,14 +518,14 @@ static int proc_reg_open(struct inode *inode, struct file *file) if (open) rv = open(inode, file); - if (release) { + if (pde->open) { if (rv == 0) { /* To know what to release. */ pdeo->file = file; pdeo->closing = false; pdeo->c = NULL; spin_lock(&pde->pde_unload_lock); - list_add(&pdeo->lh, &pde->pde_openers); + list_add(&pdeo->lh, &pde->open->files); spin_unlock(&pde->pde_unload_lock); } else kmem_cache_free(pde_opener_cache, pdeo); @@ -547,8 +548,10 @@ static int proc_reg_release(struct inode *inode, struct file *file) return 0; } + if (!pde->open) + return 0; spin_lock(&pde->pde_unload_lock); - list_for_each_entry(pdeo, &pde->pde_openers, lh) { + list_for_each_entry(pdeo, &pde->open->files, lh) { if (pdeo->file == file) { close_pdeo(pde, pdeo); return 0; diff --git a/fs/proc/internal.h b/fs/proc/internal.h index ab4d7bd1fb75..33776bc00ff0 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -35,9 +35,6 @@ struct proc_dir_entry { */ atomic_t in_use; refcount_t refcnt; - struct list_head pde_openers; /* who did ->open, but not ->release */ - /* protects ->pde_openers and all struct pde_opener instances */ - spinlock_t pde_unload_lock; const struct inode_operations *proc_iops; union { const struct proc_ops *proc_ops; @@ -47,6 +44,8 @@ struct proc_dir_entry { const struct seq_operations *seq_ops; int (*single_show)(struct seq_file *, void *); }; + /* the open files procfs has to know, see struct pde_open_node */ + struct pde_open_node *open; proc_write_t write; void *data; unsigned int state_size; @@ -54,6 +53,8 @@ struct proc_dir_entry { nlink_t nlink; kuid_t uid; kgid_t gid; + /* protects ->open->files and all struct pde_opener instances */ + spinlock_t pde_unload_lock; loff_t size; struct proc_dir_entry *parent; struct rb_root subdir; @@ -325,6 +326,15 @@ struct pde_opener { bool closing; struct completion *c; } __randomize_layout; + +/* + * The open files of an entry whose ->proc_release() procfs has to call when + * the entry is removed while they are still open. Allocated with the entry, + * see proc_register(). + */ +struct pde_open_node { + struct list_head files; /* who did ->open, but not ->release */ +}; extern const struct inode_operations proc_link_inode_operations; extern const struct inode_operations proc_pid_link_inode_operations; extern const struct super_operations proc_sops; -- 2.53.0 ->flags of struct proc_dir_entry is a u8 and the next patches need more flags than it has room for, so make it a u16. Bit 6 stays free, vfs-7.5.mount takes it for PROC_ENTRY_CANT_MOUNT. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 33776bc00ff0..7dea0237f08b 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -61,7 +61,7 @@ struct proc_dir_entry { struct rb_node subdir_node; char *name; umode_t mode; - u8 flags; + u16 flags; u8 namelen; char inline_name[]; } __randomize_layout; -- 2.53.0 remove_proc_entry() waits until every call into the entry has returned. For a seq_file entry that's all of seq_read_iter(), including the copy to userspace. A page fault in that copy can be made to take as long as the reader wants, e.g. with userfaultfd or by punching holes into a memfd over and over. And callers of remove_proc_entry() like to hold locks while they wait. xt_hashlimit holds hashlimit_mutex, which all network namespaces share, and addrconf holds RTNL. But the entry's code only runs between ->start() and ->stop() and the copies to userspace happen outside of that. So let procfs own the seq_file the way kernfs does. An entry declares its seq_operations, or just a show function, and the size of its private state in struct proc_ops. procfs then opens the seq_file with seq_operations it builds for the entry when it is registered: the entry's own ->next() and ->show() with a ->start() and ->stop() of procfs around them that pin the entry and unpin it again. Nothing of procfs runs between the records that way. procfs reads and seeks through seq_file directly. The copies to userspace happen with the entry unpinned. That changes who owns the seq_file. remove_proc_entry() can now finish while a reader is still copying out of the buffer, so the seq_file can't go away with the entry. procfs frees it once the file is released. ->proc_open() finds the seq_file in ->private_data and sets up whatever the entry needs and ->proc_release() releases just that. It still runs when the entry is removed, or when the file is closed before that. Since procfs reads these entries through seq_read_iter() itself they all have ->splice_read() now, so sendfile(), splice(), aio, the RWF flags and kernel_read() work on them, and readv() is one read instead of one per segment. Note that a removed entry still hands out what it had already formatted and then answers -EIO to reads, and to seeks that have to walk the records. Seeking it failed with -EINVAL right away before. For an entry that only has a show function the seq_operations do what single_open() does, like kernfs_seq_start(). If the entry can't be pinned ->start() fails with -EIO and switches the seq_file to seq_operations whose ->stop() doesn't unpin, so the entry's own ->start() and ->next() can return any error they like. An entry that declares a seq_file and also a read or seek method of its own, or one that releases the seq_file with seq_release() itself, doesn't make sense, procfs reads, seeks and releases the seq_file itself, so registering it fails. No users yet. Suggested-by: Jann Horn Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 65 ++++++++++++++++++--- fs/proc/inode.c | 146 +++++++++++++++++++++++++++++++++++++++++++----- fs/proc/internal.h | 21 +++++++ include/linux/proc_fs.h | 22 +++++++- 4 files changed, 231 insertions(+), 23 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 84f064a651e4..59b89e251ba3 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -41,6 +41,7 @@ void pde_free(struct proc_dir_entry *pde) if (pde->name != pde->inline_name) kfree(pde->name); kfree(pde->open); + kfree(pde->seq_direct); kmem_cache_free(proc_dir_entry_cache, pde); } @@ -431,19 +432,41 @@ static const struct inode_operations proc_dir_inode_operations = { .setattr = proc_setattr, }; -static void pde_set_flags(struct proc_dir_entry *pde) +static int pde_set_flags(struct proc_dir_entry *pde) { const struct proc_ops *proc_ops = pde->proc_ops; if (!proc_ops) - return; + return 0; if (proc_ops->proc_flags & PROC_ENTRY_PERMANENT) pde->flags |= PROC_ENTRY_PERMANENT; - if (proc_ops->proc_read_iter) - pde->flags |= PROC_ENTRY_proc_read_iter; - if (proc_ops->proc_lseek) + if (proc_ops->proc_seq_ops) { + pde->seq_ops = proc_ops->proc_seq_ops; + pde->state_size = proc_ops->proc_state_size; + pde->flags |= PROC_ENTRY_proc_seq_ops; + } else if (proc_ops->proc_show) { + pde->single_show = proc_ops->proc_show; + pde->flags |= PROC_ENTRY_proc_show; + } + if (pde_is_seq_file(pde)) { + /* procfs reads and seeks through the seq_file itself */ + if (WARN_ON_ONCE(proc_ops->proc_read || proc_ops->proc_read_iter || + proc_ops->proc_lseek)) + return -EINVAL; + /* and frees it, see pde_seq_release() */ + if (WARN_ON_ONCE(proc_ops->proc_release == seq_release || + proc_ops->proc_release == seq_release_private || + proc_ops->proc_release == single_release)) + return -EINVAL; pde->flags |= PROC_ENTRY_proc_lseek; + } else { + if (proc_ops->proc_read_iter) + pde->flags |= PROC_ENTRY_proc_read_iter; + if (proc_ops->proc_lseek) + pde->flags |= PROC_ENTRY_proc_lseek; + } + return 0; } /* @@ -458,6 +481,32 @@ static bool pde_needs_open_node(const struct proc_dir_entry *pde) return pde->proc_ops->proc_release; } +/* + * The seq_operations of a seq_file entry: procfs's start and stop around the + * entry's own next and show, see pde_seq_start(). + */ +static int pde_init_seq_direct(struct proc_dir_entry *pde) +{ + struct seq_operations *ops; + + if (!pde_is_seq_file(pde)) + return 0; + ops = kmalloc(sizeof(*ops), GFP_KERNEL); + if (!ops) + return -ENOMEM; + ops->start = pde_seq_start; + ops->stop = pde_seq_stop; + if (pde_has_proc_seq_ops(pde)) { + ops->next = pde->seq_ops->next; + ops->show = pde->seq_ops->show; + } else { + ops->next = pde_single_next; + ops->show = pde->single_show; + } + pde->seq_direct = ops; + return 0; +} + /* returns the registered entry, or frees dp and returns NULL on failure */ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, struct proc_dir_entry *dp) @@ -465,14 +514,16 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, if (proc_alloc_inum(&dp->low_ino)) goto out_free_entry; - if (!S_ISDIR(dp->mode)) - pde_set_flags(dp); + if (!S_ISDIR(dp->mode) && pde_set_flags(dp)) + goto out_free_inum; if (pde_needs_open_node(dp)) { dp->open = kzalloc(sizeof(*dp->open), GFP_KERNEL); if (!dp->open) goto out_free_inum; INIT_LIST_HEAD(&dp->open->files); } + if (pde_init_seq_direct(dp)) + goto out_free_inum; write_lock(&proc_subdir_lock); dp->parent = dir; diff --git a/fs/proc/inode.c b/fs/proc/inode.c index 56eab69e1dcc..d51ab7548b11 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -219,6 +219,87 @@ static void pde_unpin(struct proc_dir_entry *pde) unuse_pde(pde); } +/* + * procfs owns the seq_file of a seq_file entry and only calls into the entry + * with it pinned. A read copies to userspace without the pin, so a reader + * that faults on its buffer doesn't hold up remove_proc_entry(). + * + * The seq_operations the seq_file runs are the entry's own next and show + * with procfs's start and stop around them, see pde_init_seq_direct(): the + * pin is taken in start and dropped in stop, and nothing sits between the + * seq_file and the entry's per record code. An entry that only has a show + * function is iterated like single_open() does it. + * + * If the pin fails start switches the seq_file to seq_operations whose stop + * doesn't unpin, so that the entry's own start or next can return any error + * they like, ERR_PTR(-EIO) included. + */ +static struct proc_dir_entry *seq_pde(struct seq_file *m) +{ + return PDE(file_inode(m->file)); +} + +static void pde_seq_stop_unpinned(struct seq_file *m, void *v) +{ + m->op = seq_pde(m)->seq_direct; +} + +static const struct seq_operations pde_seq_ops_unpinned = { + .start = pde_seq_start, + .stop = pde_seq_stop_unpinned, +}; + +void *pde_seq_start(struct seq_file *m, loff_t *pos) +{ + struct proc_dir_entry *pde = seq_pde(m); + + if (!pde_pin(pde)) { + m->op = &pde_seq_ops_unpinned; + return ERR_PTR(-EIO); + } + m->op = pde->seq_direct; + if (!pde_has_proc_seq_ops(pde)) + return single_start(m, pos); + return pde->seq_ops->start(m, pos); +} + +void pde_seq_stop(struct seq_file *m, void *v) +{ + struct proc_dir_entry *pde = seq_pde(m); + + if (pde_has_proc_seq_ops(pde)) + pde->seq_ops->stop(m, v); + pde_unpin(pde); +} + +/* what single_open() iterates with, seq_file keeps its own private */ +void *pde_single_next(struct seq_file *m, void *v, loff_t *pos) +{ + ++*pos; + return NULL; +} + +static int pde_seq_open(struct proc_dir_entry *pde, struct file *file) +{ + int err; + + if (pde->state_size) + return seq_open_private(file, pde->seq_direct, pde->state_size); + err = seq_open(file, pde->seq_direct); + if (!err && pde_has_proc_show(pde)) + ((struct seq_file *)file->private_data)->private = pde->data; + return err; +} + +static void pde_seq_release(struct proc_dir_entry *pde, struct inode *inode, + struct file *file) +{ + if (pde->state_size) + seq_release_private(inode, file); + else + seq_release(inode, file); +} + /* * At most 2 contexts can enter this function: the one doing the last * close on the descriptor and whoever is deleting PDE itself. @@ -474,22 +555,35 @@ static unsigned int proc_reg_mmap_capabilities(struct file *file) } #endif +static int pde_open(struct proc_dir_entry *pde, struct inode *inode, + struct file *file) +{ + const auto open = pde->proc_ops->proc_open; + int rv; + + if (!pde_is_seq_file(pde)) + return open ? open(inode, file) : 0; + + rv = pde_seq_open(pde, file); + if (rv || !open) + return rv; + rv = open(inode, file); + if (rv) + pde_seq_release(pde, inode, file); + return rv; +} + static int proc_reg_open(struct inode *inode, struct file *file) { struct proc_dir_entry *pde = PDE(inode); int rv = 0; - typeof_member(struct proc_ops, proc_open) open; struct pde_opener *pdeo; if (!pde_has_proc_lseek(pde)) file->f_mode &= ~FMODE_LSEEK; - if (pde_is_permanent(pde)) { - open = pde->proc_ops->proc_open; - if (open) - rv = open(inode, file); - return rv; - } + if (pde_is_permanent(pde)) + return pde_open(pde, inode, file); /* * Ensure that @@ -514,9 +608,7 @@ static int proc_reg_open(struct inode *inode, struct file *file) } } - open = pde->proc_ops->proc_open; - if (open) - rv = open(inode, file); + rv = pde_open(pde, inode, file); if (pde->open) { if (rv == 0) { @@ -544,23 +636,45 @@ static int proc_reg_release(struct inode *inode, struct file *file) if (pde_is_permanent(pde)) { const auto release = pde->proc_ops->proc_release; if (release) - return release(inode, file); - return 0; + release(inode, file); + goto out; } if (!pde->open) - return 0; + goto out; spin_lock(&pde->pde_unload_lock); list_for_each_entry(pdeo, &pde->open->files, lh) { if (pdeo->file == file) { close_pdeo(pde, pdeo); - return 0; + goto out; } } spin_unlock(&pde->pde_unload_lock); +out: + /* ->proc_release() has run, here or in remove_proc_entry() */ + if (pde_is_seq_file(pde)) + pde_seq_release(pde, inode, file); return 0; } +/* For seq_file entries, see pde_seq_ops. */ +static const struct file_operations proc_seq_file_ops = { + .llseek = seq_lseek, + .read_iter = seq_read_iter, + .splice_read = copy_splice_read, + .write = proc_reg_write, + .poll = proc_reg_poll, + .unlocked_ioctl = proc_reg_unlocked_ioctl, + .compat_ioctl = proc_reg_compat_ioctl, + .mmap = proc_reg_mmap, + .get_unmapped_area = proc_reg_get_unmapped_area, +#ifndef CONFIG_MMU + .mmap_capabilities = proc_reg_mmap_capabilities, +#endif + .open = proc_reg_open, + .release = proc_reg_release, +}; + static const struct file_operations proc_reg_file_ops = { .llseek = proc_reg_llseek, .read = proc_reg_read, @@ -644,7 +758,9 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de) if (S_ISREG(inode->i_mode)) { inode->i_op = de->proc_iops; - if (pde_has_proc_read_iter(de)) + if (pde_is_seq_file(de)) + inode->i_fop = &proc_seq_file_ops; + else if (pde_has_proc_read_iter(de)) inode->i_fop = &proc_iter_file_ops; else inode->i_fop = &proc_reg_file_ops; diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 7dea0237f08b..674e6b4ed21e 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -46,6 +46,8 @@ struct proc_dir_entry { }; /* the open files procfs has to know, see struct pde_open_node */ struct pde_open_node *open; + /* the seq_operations of the seq_file, see pde_init_seq_direct() */ + struct seq_operations *seq_direct; proc_write_t write; void *data; unsigned int state_size; @@ -97,6 +99,22 @@ static inline bool pde_has_proc_lseek(const struct proc_dir_entry *pde) return pde->flags & PROC_ENTRY_proc_lseek; } +static inline bool pde_has_proc_seq_ops(const struct proc_dir_entry *pde) +{ + return pde->flags & PROC_ENTRY_proc_seq_ops; +} + +static inline bool pde_has_proc_show(const struct proc_dir_entry *pde) +{ + return pde->flags & PROC_ENTRY_proc_show; +} + +/* procfs owns the seq_file, see pde_seq_start() */ +static inline bool pde_is_seq_file(const struct proc_dir_entry *pde) +{ + return pde_has_proc_seq_ops(pde) || pde_has_proc_show(pde); +} + extern struct kmem_cache *proc_dir_entry_cache; void pde_free(struct proc_dir_entry *pde); @@ -343,6 +361,9 @@ void proc_init_kmemcache(void); void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock); void set_proc_pid_nlink(void); extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *); +void *pde_seq_start(struct seq_file *m, loff_t *pos); +void pde_seq_stop(struct seq_file *m, void *v); +void *pde_single_next(struct seq_file *m, void *v, loff_t *pos); extern void proc_entry_rundown(struct proc_dir_entry *); /* diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 6ad5150020bf..ea55ed85a2d6 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -27,6 +27,8 @@ enum { PROC_ENTRY_proc_read_iter = 1U << 1, PROC_ENTRY_proc_lseek = 1U << 3, + PROC_ENTRY_proc_seq_ops = 1U << 4, + PROC_ENTRY_proc_show = 1U << 5, PROC_ENTRY_FORCE_LOOKUP = 1U << 7, }; @@ -37,7 +39,10 @@ struct proc_ops { ssize_t (*proc_read)(struct file *, char __user *, size_t, loff_t *); ssize_t (*proc_read_iter)(struct kiocb *, struct iov_iter *); ssize_t (*proc_write)(struct file *, const char __user *, size_t, loff_t *); - /* mandatory unless nonseekable_open() or equivalent is used */ + /* + * mandatory unless nonseekable_open() or equivalent is used, procfs + * seeks seq_file entries itself + */ loff_t (*proc_lseek)(struct file *, loff_t, int); int (*proc_release)(struct inode *, struct file *); __poll_t (*proc_poll)(struct file *, struct poll_table_struct *); @@ -47,6 +52,21 @@ struct proc_ops { #endif int (*proc_mmap)(struct file *, struct vm_area_struct *); unsigned long (*proc_get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); + /* + * For seq_file entries procfs opens the seq_file, reads and seeks + * through it and frees it once the file is released. ->proc_open() + * finds the seq_file in ->private_data and ->proc_release() only + * releases what ->proc_open() set up. It can run while a reader still + * copies out of the seq_file, so it must leave that alone. procfs + * allocates ->proc_state_size bytes of ->private state for + * ->proc_seq_ops and points ->private at the entry's data for + * ->proc_show(). Nothing that runs with the entry pinned may wait for + * something a copy to or from userspace can hold, like the seq_file's + * lock. + */ + const struct seq_operations *proc_seq_ops; + int (*proc_show)(struct seq_file *, void *); + unsigned int proc_state_size; } __randomize_layout; /* definitions for hide_pid field */ -- 2.53.0 proc_create_seq_private(), proc_create_single_data() and their network namespace variants keep the seq_operations or the show function in the entry already. Let procfs own the seq_file for all of them. For the network namespace variants ->proc_open() and ->proc_release() keep taking and dropping the reference to the network namespace. That's most of /proc/net, among them the files of xt_hashlimit and /proc/net/dev_snmp6/. A reader that faults on its buffer no longer holds up their removal. Reported-by: Chris Mason Fixes: 786d7e1612f0 ("Fix rmmod/read/write races in /proc entries") Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 42 ++++-------------------------------------- fs/proc/proc_net.c | 40 +++++++++++++--------------------------- 2 files changed, 17 insertions(+), 65 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 59b89e251ba3..959bf9e33245 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -737,30 +737,9 @@ struct proc_dir_entry *proc_create(const char *name, umode_t mode, } EXPORT_SYMBOL(proc_create); -static int proc_seq_open(struct inode *inode, struct file *file) -{ - struct proc_dir_entry *de = PDE(inode); - - if (de->state_size) - return seq_open_private(file, de->seq_ops, de->state_size); - return seq_open(file, de->seq_ops); -} - -static int proc_seq_release(struct inode *inode, struct file *file) -{ - struct proc_dir_entry *de = PDE(inode); - - if (de->state_size) - return seq_release_private(inode, file); - return seq_release(inode, file); -} - +/* procfs owns the seq_file of these, see pde_seq_start() */ static const struct proc_ops proc_seq_ops = { /* not permanent -- can call into arbitrary seq_operations */ - .proc_open = proc_seq_open, - .proc_read_iter = seq_read_iter, - .proc_lseek = seq_lseek, - .proc_release = proc_seq_release, }; struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode, @@ -775,25 +754,11 @@ struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode, p->proc_ops = &proc_seq_ops; p->seq_ops = ops; p->state_size = state_size; + p->flags |= PROC_ENTRY_proc_seq_ops; return proc_register(parent, p); } EXPORT_SYMBOL(proc_create_seq_private); -static int proc_single_open(struct inode *inode, struct file *file) -{ - struct proc_dir_entry *de = PDE(inode); - - return single_open(file, de->single_show, de->data); -} - -static const struct proc_ops proc_single_ops = { - /* not permanent -- can call into arbitrary ->single_show */ - .proc_open = proc_single_open, - .proc_read_iter = seq_read_iter, - .proc_lseek = seq_lseek, - .proc_release = single_release, -}; - struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode, struct proc_dir_entry *parent, int (*show)(struct seq_file *, void *), void *data) @@ -803,8 +768,9 @@ struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode, p = proc_create_reg(name, mode, &parent, data); if (!p) return NULL; - p->proc_ops = &proc_single_ops; + p->proc_ops = &proc_seq_ops; p->single_show = show; + p->flags |= PROC_ENTRY_proc_show; return proc_register(parent, p); } EXPORT_SYMBOL(proc_create_single_data); diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c index df5484813d5d..9973f846c467 100644 --- a/fs/proc/proc_net.c +++ b/fs/proc/proc_net.c @@ -37,13 +37,13 @@ static struct net *get_proc_net(const struct inode *inode) return maybe_get_net(PDE_NET(PDE(inode))); } +/* procfs opened the seq_file and allocated its state, see pde_seq_start() */ static int seq_open_net(struct inode *inode, struct file *file) { - unsigned int state_size = PDE(inode)->state_size; - struct seq_net_private *p; + struct seq_net_private *p = ((struct seq_file *)file->private_data)->private; struct net *net; - WARN_ON_ONCE(state_size < sizeof(*p)); + WARN_ON_ONCE(PDE(inode)->state_size < sizeof(*p)); if (file->f_mode & FMODE_WRITE && !PDE(inode)->write) return -EACCES; @@ -51,12 +51,6 @@ static int seq_open_net(struct inode *inode, struct file *file) net = get_proc_net(inode); if (!net) return -ENXIO; - - p = __seq_open_private(file, PDE(inode)->seq_ops, state_size); - if (!p) { - put_net(net); - return -ENOMEM; - } #ifdef CONFIG_NET_NS p->net = net; netns_tracker_alloc(net, &p->ns_tracker, GFP_KERNEL); @@ -77,18 +71,13 @@ static void seq_file_net_put_net(struct seq_file *seq) static int seq_release_net(struct inode *ino, struct file *f) { - struct seq_file *seq = f->private_data; - - seq_file_net_put_net(seq); - seq_release_private(ino, f); + seq_file_net_put_net(f->private_data); return 0; } static const struct proc_ops proc_net_seq_ops = { .proc_open = seq_open_net, - .proc_read = seq_read, .proc_write = proc_simple_write, - .proc_lseek = seq_lseek, .proc_release = seq_release_net, }; @@ -125,6 +114,7 @@ struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode, p->proc_ops = &proc_net_seq_ops; p->seq_ops = ops; p->state_size = state_size; + p->flags |= PROC_ENTRY_proc_seq_ops; return proc_register(parent, p); } EXPORT_SYMBOL_GPL(proc_create_net_data); @@ -170,6 +160,7 @@ struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode p->proc_ops = &proc_net_seq_ops; p->seq_ops = ops; p->state_size = state_size; + p->flags |= PROC_ENTRY_proc_seq_ops; p->write = write; return proc_register(parent, p); } @@ -177,32 +168,25 @@ EXPORT_SYMBOL_GPL(proc_create_net_data_write); static int single_open_net(struct inode *inode, struct file *file) { - struct proc_dir_entry *de = PDE(inode); - struct net *net; - int err; + struct net *net = get_proc_net(inode); - net = get_proc_net(inode); if (!net) return -ENXIO; - - err = single_open(file, de->single_show, net); - if (err) - put_net(net); - return err; + ((struct seq_file *)file->private_data)->private = net; + return 0; } static int single_release_net(struct inode *ino, struct file *f) { struct seq_file *seq = f->private_data; + put_net(seq->private); - return single_release(ino, f); + return 0; } static const struct proc_ops proc_net_single_ops = { .proc_open = single_open_net, - .proc_read = seq_read, .proc_write = proc_simple_write, - .proc_lseek = seq_lseek, .proc_release = single_release_net, }; @@ -218,6 +202,7 @@ struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode, pde_force_lookup(p); p->proc_ops = &proc_net_single_ops; p->single_show = show; + p->flags |= PROC_ENTRY_proc_show; return proc_register(parent, p); } EXPORT_SYMBOL_GPL(proc_create_net_single); @@ -261,6 +246,7 @@ struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mo pde_force_lookup(p); p->proc_ops = &proc_net_single_ops; p->single_show = show; + p->flags |= PROC_ENTRY_proc_show; p->write = write; return proc_register(parent, p); } -- 2.53.0 Declare the show function and let procfs open the seq_file. Signed-off-by: Christian Brauner (Amutable) --- include/linux/seq_file.h | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index dc0e8c62d9e0..b813b7e63d89 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -233,16 +233,8 @@ static const struct file_operations __name ## _fops = { \ } #define DEFINE_PROC_SHOW_ATTRIBUTE(__name) \ -static int __name ## _open(struct inode *inode, struct file *file) \ -{ \ - return single_open(file, __name ## _show, pde_data(inode)); \ -} \ - \ static const struct proc_ops __name ## _proc_ops = { \ - .proc_open = __name ## _open, \ - .proc_read = seq_read, \ - .proc_lseek = seq_lseek, \ - .proc_release = single_release, \ + .proc_show = __name ## _show, \ } static inline struct user_namespace *seq_user_ns(struct seq_file *seq) -- 2.53.0 Most proc entries that bring their own struct proc_ops only call single_open(), seq_open() or seq_open_private() in ->proc_open() and free the seq_file with the matching helper. Declare the show function or the seq_operations instead and let procfs open and free the seq_file. Reading these files then no longer holds up their removal. Done with the following semantic patch. The new initializers were aligned like their neighbours afterwards: // SPDX-License-Identifier: GPL-2.0-only /// Let procfs open the seq_file of proc entries whose ->proc_open() only /// calls single_open(), seq_open() or seq_open_private(). // // Confidence: High // Options: --no-includes --include-headers virtual patch @single depends on patch@ identifier open_fn, show_fn, i, f; @@ static int open_fn(struct inode *i, struct file *f) { return single_open(f, show_fn, \(NULL\|pde_data(i)\)); } @single_ops depends on single@ identifier single.open_fn, single.show_fn, ops; symbol single_release; @@ const struct proc_ops ops = { - .proc_open = open_fn, + .proc_show = show_fn, ..., - .proc_release = single_release, ... }; @seq depends on patch@ identifier open_fn, sops, i, f; @@ static int open_fn(struct inode *i, struct file *f) { return seq_open(f, &sops); } @seq_ops depends on seq@ identifier seq.open_fn, seq.sops, ops; symbol seq_release; @@ const struct proc_ops ops = { - .proc_open = open_fn, + .proc_seq_ops = &sops, ..., - .proc_release = seq_release, ... }; @priv depends on patch@ identifier open_fn, sops, i, f; expression size; @@ static int open_fn(struct inode *i, struct file *f) { return seq_open_private(f, &sops, size); } @priv_ops depends on priv@ identifier priv.open_fn, priv.sops, ops; expression priv.size; symbol seq_release_private; @@ const struct proc_ops ops = { - .proc_open = open_fn, + .proc_seq_ops = &sops, + .proc_state_size = size, ..., - .proc_release = seq_release_private, ... }; // procfs reads and seeks through the seq_file itself @depends on single_ops || seq_ops || priv_ops@ identifier ops = {single_ops.ops, seq_ops.ops, priv_ops.ops}; symbol seq_read_iter; @@ const struct proc_ops ops = { ..., - .proc_read_iter = seq_read_iter, ... }; @depends on single_ops || seq_ops || priv_ops@ identifier ops = {single_ops.ops, seq_ops.ops, priv_ops.ops}; symbol seq_read; @@ const struct proc_ops ops = { ..., - .proc_read = seq_read, ... }; @depends on single_ops || seq_ops || priv_ops@ identifier ops = {single_ops.ops, seq_ops.ops, priv_ops.ops}; symbol seq_lseek; @@ const struct proc_ops ops = { ..., - .proc_lseek = seq_lseek, ... }; // drop the open function unless something else still uses it @open_used@ identifier open_fn = {single.open_fn, seq.open_fn, priv.open_fn}; @@ open_fn @depends on !open_used && (single_ops || seq_ops || priv_ops)@ identifier open_fn = {single.open_fn, seq.open_fn, priv.open_fn}; @@ - static int open_fn(...) { ... } Signed-off-by: Christian Brauner (Amutable) --- arch/alpha/kernel/srm_env.c | 10 +-- arch/arm/mm/alignment.c | 10 +-- arch/powerpc/kernel/rtas-proc.c | 50 ++------------ arch/powerpc/platforms/pseries/lpar.c | 20 +----- arch/powerpc/platforms/pseries/lparcfg.c | 10 +-- arch/sh/mm/alignment.c | 10 +-- arch/sparc/kernel/led.c | 10 +-- arch/um/kernel/exitcode.c | 10 +-- drivers/acpi/proc.c | 12 +--- drivers/hwmon/dell-smm-hwmon.c | 10 +-- drivers/input/input.c | 24 ++----- drivers/macintosh/via-pmu.c | 10 +-- drivers/net/wireless/intel/ipw2x00/libipw_module.c | 10 +-- drivers/platform/x86/lenovo/thinkpad_acpi.c | 10 +-- drivers/platform/x86/toshiba_acpi.c | 40 ++--------- drivers/pnp/pnpbios/proc.c | 10 +-- drivers/s390/block/dasd_proc.c | 10 +-- drivers/s390/cio/blacklist.c | 13 +--- drivers/scsi/scsi_devinfo.c | 10 +-- drivers/scsi/scsi_proc.c | 21 +----- drivers/usb/gadget/function/rndis.c | 10 +-- drivers/video/fbdev/via/viafbdev.c | 70 ++----------------- fs/jfs/jfs_debug.c | 10 +-- fs/ntfs3/super.c | 20 +----- fs/proc/cpuinfo.c | 10 +-- fs/smb/client/cifs_debug.c | 79 +++------------------- fs/smb/client/dfs_cache.c | 10 +-- kernel/irq/proc.c | 30 +------- kernel/latencytop.c | 10 +-- lib/dynamic_debug.c | 6 +- mm/slab_common.c | 10 +-- net/core/pktgen.c | 30 +------- net/sunrpc/stats.c | 10 +-- 33 files changed, 64 insertions(+), 551 deletions(-) diff --git a/arch/alpha/kernel/srm_env.c b/arch/alpha/kernel/srm_env.c index 217b4dca51dd..faafc2df7886 100644 --- a/arch/alpha/kernel/srm_env.c +++ b/arch/alpha/kernel/srm_env.c @@ -81,11 +81,6 @@ static int srm_env_proc_show(struct seq_file *m, void *v) return ret; } -static int srm_env_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, srm_env_proc_show, pde_data(inode)); -} - static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -120,10 +115,7 @@ static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer, } static const struct proc_ops srm_env_proc_ops = { - .proc_open = srm_env_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = srm_env_proc_show, .proc_write = srm_env_proc_write, }; diff --git a/arch/arm/mm/alignment.c b/arch/arm/mm/alignment.c index f6c1e6b61179..3e5e30968634 100644 --- a/arch/arm/mm/alignment.c +++ b/arch/arm/mm/alignment.c @@ -144,11 +144,6 @@ static int alignment_proc_show(struct seq_file *m, void *v) return 0; } -static int alignment_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, alignment_proc_show, NULL); -} - static ssize_t alignment_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -164,10 +159,7 @@ static ssize_t alignment_proc_write(struct file *file, const char __user *buffer } static const struct proc_ops alignment_proc_ops = { - .proc_open = alignment_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = alignment_proc_show, .proc_write = alignment_proc_write, }; #endif /* CONFIG_PROC_FS */ diff --git a/arch/powerpc/kernel/rtas-proc.c b/arch/powerpc/kernel/rtas-proc.c index 89b165986851..808023f9d1f6 100644 --- a/arch/powerpc/kernel/rtas-proc.c +++ b/arch/powerpc/kernel/rtas-proc.c @@ -154,69 +154,29 @@ static ssize_t ppc_rtas_tone_volume_write(struct file *file, static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v); static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v); -static int poweron_open(struct inode *inode, struct file *file) -{ - return single_open(file, ppc_rtas_poweron_show, NULL); -} - static const struct proc_ops ppc_rtas_poweron_proc_ops = { - .proc_open = poweron_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = ppc_rtas_poweron_show, .proc_write = ppc_rtas_poweron_write, - .proc_release = single_release, }; -static int progress_open(struct inode *inode, struct file *file) -{ - return single_open(file, ppc_rtas_progress_show, NULL); -} - static const struct proc_ops ppc_rtas_progress_proc_ops = { - .proc_open = progress_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = ppc_rtas_progress_show, .proc_write = ppc_rtas_progress_write, - .proc_release = single_release, }; -static int clock_open(struct inode *inode, struct file *file) -{ - return single_open(file, ppc_rtas_clock_show, NULL); -} - static const struct proc_ops ppc_rtas_clock_proc_ops = { - .proc_open = clock_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = ppc_rtas_clock_show, .proc_write = ppc_rtas_clock_write, - .proc_release = single_release, }; -static int tone_freq_open(struct inode *inode, struct file *file) -{ - return single_open(file, ppc_rtas_tone_freq_show, NULL); -} - static const struct proc_ops ppc_rtas_tone_freq_proc_ops = { - .proc_open = tone_freq_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = ppc_rtas_tone_freq_show, .proc_write = ppc_rtas_tone_freq_write, - .proc_release = single_release, }; -static int tone_volume_open(struct inode *inode, struct file *file) -{ - return single_open(file, ppc_rtas_tone_volume_show, NULL); -} - static const struct proc_ops ppc_rtas_tone_volume_proc_ops = { - .proc_open = tone_volume_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = ppc_rtas_tone_volume_show, .proc_write = ppc_rtas_tone_volume_write, - .proc_release = single_release, }; static int ppc_rtas_find_all_sensors(void); diff --git a/arch/powerpc/platforms/pseries/lpar.c b/arch/powerpc/platforms/pseries/lpar.c index 6a415febc53b..48fe0be71a89 100644 --- a/arch/powerpc/platforms/pseries/lpar.c +++ b/arch/powerpc/platforms/pseries/lpar.c @@ -584,17 +584,9 @@ static int vcpudispatch_stats_display(struct seq_file *p, void *v) return 0; } -static int vcpudispatch_stats_open(struct inode *inode, struct file *file) -{ - return single_open(file, vcpudispatch_stats_display, NULL); -} - static const struct proc_ops vcpudispatch_stats_proc_ops = { - .proc_open = vcpudispatch_stats_open, - .proc_read = seq_read, + .proc_show = vcpudispatch_stats_display, .proc_write = vcpudispatch_stats_write, - .proc_lseek = seq_lseek, - .proc_release = single_release, }; static ssize_t vcpudispatch_stats_freq_write(struct file *file, @@ -628,17 +620,9 @@ static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v) return 0; } -static int vcpudispatch_stats_freq_open(struct inode *inode, struct file *file) -{ - return single_open(file, vcpudispatch_stats_freq_display, NULL); -} - static const struct proc_ops vcpudispatch_stats_freq_proc_ops = { - .proc_open = vcpudispatch_stats_freq_open, - .proc_read = seq_read, + .proc_show = vcpudispatch_stats_freq_display, .proc_write = vcpudispatch_stats_freq_write, - .proc_lseek = seq_lseek, - .proc_release = single_release, }; static int __init vcpudispatch_stats_procfs_init(void) diff --git a/arch/powerpc/platforms/pseries/lparcfg.c b/arch/powerpc/platforms/pseries/lparcfg.c index 4f6b2975c6e9..cb3d96efb225 100644 --- a/arch/powerpc/platforms/pseries/lparcfg.c +++ b/arch/powerpc/platforms/pseries/lparcfg.c @@ -797,17 +797,9 @@ static int lparcfg_data(struct seq_file *m, void *v) return pseries_lparcfg_data(m, v); } -static int lparcfg_open(struct inode *inode, struct file *file) -{ - return single_open(file, lparcfg_data, NULL); -} - static const struct proc_ops lparcfg_proc_ops = { - .proc_read = seq_read, .proc_write = lparcfg_write, - .proc_open = lparcfg_open, - .proc_release = single_release, - .proc_lseek = seq_lseek, + .proc_show = lparcfg_data, }; static int __init lparcfg_init(void) diff --git a/arch/sh/mm/alignment.c b/arch/sh/mm/alignment.c index 3a76a766f423..2198c6f2ea63 100644 --- a/arch/sh/mm/alignment.c +++ b/arch/sh/mm/alignment.c @@ -132,11 +132,6 @@ static int alignment_proc_show(struct seq_file *m, void *v) return 0; } -static int alignment_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, alignment_proc_show, NULL); -} - static ssize_t alignment_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -153,10 +148,7 @@ static ssize_t alignment_proc_write(struct file *file, } static const struct proc_ops alignment_proc_ops = { - .proc_open = alignment_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = alignment_proc_show, .proc_write = alignment_proc_write, }; diff --git a/arch/sparc/kernel/led.c b/arch/sparc/kernel/led.c index a1478de9fe5d..0f7376b51a04 100644 --- a/arch/sparc/kernel/led.c +++ b/arch/sparc/kernel/led.c @@ -60,11 +60,6 @@ static int led_proc_show(struct seq_file *m, void *v) return 0; } -static int led_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, led_proc_show, NULL); -} - static ssize_t led_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) { @@ -107,10 +102,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer, } static const struct proc_ops led_proc_ops = { - .proc_open = led_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = led_proc_show, .proc_write = led_proc_write, }; #endif diff --git a/arch/um/kernel/exitcode.c b/arch/um/kernel/exitcode.c index 0230354f4812..c71657df38f5 100644 --- a/arch/um/kernel/exitcode.c +++ b/arch/um/kernel/exitcode.c @@ -31,11 +31,6 @@ static int exitcode_proc_show(struct seq_file *m, void *v) return 0; } -static int exitcode_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, exitcode_proc_show, NULL); -} - static ssize_t exitcode_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -57,10 +52,7 @@ static ssize_t exitcode_proc_write(struct file *file, } static const struct proc_ops exitcode_proc_ops = { - .proc_open = exitcode_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = exitcode_proc_show, .proc_write = exitcode_proc_write, }; diff --git a/drivers/acpi/proc.c b/drivers/acpi/proc.c index c08ead07252b..b8a0bd4580e0 100644 --- a/drivers/acpi/proc.c +++ b/drivers/acpi/proc.c @@ -121,19 +121,9 @@ acpi_system_write_wakeup_device(struct file *file, return count; } -static int -acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file) -{ - return single_open(file, acpi_system_wakeup_device_seq_show, - pde_data(inode)); -} - static const struct proc_ops acpi_system_wakeup_device_proc_ops = { - .proc_open = acpi_system_wakeup_device_open_fs, - .proc_read = seq_read, + .proc_show = acpi_system_wakeup_device_seq_show, .proc_write = acpi_system_write_wakeup_device, - .proc_lseek = seq_lseek, - .proc_release = single_release, }; void __init acpi_sleep_proc_init(void) diff --git a/drivers/hwmon/dell-smm-hwmon.c b/drivers/hwmon/dell-smm-hwmon.c index 88837f028405..492b2b6c6d7e 100644 --- a/drivers/hwmon/dell-smm-hwmon.c +++ b/drivers/hwmon/dell-smm-hwmon.c @@ -694,16 +694,8 @@ static int i8k_proc_show(struct seq_file *seq, void *offset) return 0; } -static int i8k_open_fs(struct inode *inode, struct file *file) -{ - return single_open(file, i8k_proc_show, pde_data(inode)); -} - static const struct proc_ops i8k_proc_ops = { - .proc_open = i8k_open_fs, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = i8k_proc_show, .proc_ioctl = i8k_ioctl, }; diff --git a/drivers/input/input.c b/drivers/input/input.c index 0859b60ca924..d27eecbf4a03 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -1218,18 +1218,10 @@ static const struct seq_operations input_devices_seq_ops = { .show = input_devices_seq_show, }; -static int input_proc_devices_open(struct inode *inode, struct file *file) -{ - return seq_open_private(file, &input_devices_seq_ops, - sizeof(struct input_seq_state)); -} - static const struct proc_ops input_devices_proc_ops = { - .proc_open = input_proc_devices_open, + .proc_seq_ops = &input_devices_seq_ops, + .proc_state_size = sizeof(struct input_seq_state), .proc_poll = input_proc_devices_poll, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = seq_release_private, }; static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos) @@ -1279,17 +1271,9 @@ static const struct seq_operations input_handlers_seq_ops = { .show = input_handlers_seq_show, }; -static int input_proc_handlers_open(struct inode *inode, struct file *file) -{ - return seq_open_private(file, &input_handlers_seq_ops, - sizeof(struct input_seq_state)); -} - static const struct proc_ops input_handlers_proc_ops = { - .proc_open = input_proc_handlers_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = seq_release_private, + .proc_seq_ops = &input_handlers_seq_ops, + .proc_state_size = sizeof(struct input_seq_state), }; static int __init input_proc_init(void) diff --git a/drivers/macintosh/via-pmu.c b/drivers/macintosh/via-pmu.c index 8c652c15018c..49f929ae10e4 100644 --- a/drivers/macintosh/via-pmu.c +++ b/drivers/macintosh/via-pmu.c @@ -912,11 +912,6 @@ static int pmu_options_proc_show(struct seq_file *m, void *v) return 0; } -static int pmu_options_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, pmu_options_proc_show, NULL); -} - static ssize_t pmu_options_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -962,10 +957,7 @@ static ssize_t pmu_options_proc_write(struct file *file, } static const struct proc_ops pmu_options_proc_ops = { - .proc_open = pmu_options_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = pmu_options_proc_show, .proc_write = pmu_options_proc_write, }; #endif diff --git a/drivers/net/wireless/intel/ipw2x00/libipw_module.c b/drivers/net/wireless/intel/ipw2x00/libipw_module.c index 552c8ff3928b..15d9a5efb581 100644 --- a/drivers/net/wireless/intel/ipw2x00/libipw_module.c +++ b/drivers/net/wireless/intel/ipw2x00/libipw_module.c @@ -215,11 +215,6 @@ static int debug_level_proc_show(struct seq_file *m, void *v) return 0; } -static int debug_level_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, debug_level_proc_show, NULL); -} - static ssize_t debug_level_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -240,10 +235,7 @@ static ssize_t debug_level_proc_write(struct file *file, } static const struct proc_ops debug_level_proc_ops = { - .proc_open = debug_level_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = debug_level_proc_show, .proc_write = debug_level_proc_write, }; #endif /* CONFIG_LIBIPW_DEBUG */ diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c index 4f81855ad721..2ee208c82801 100644 --- a/drivers/platform/x86/lenovo/thinkpad_acpi.c +++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c @@ -881,11 +881,6 @@ static int dispatch_proc_show(struct seq_file *m, void *v) return ibm->read(m); } -static int dispatch_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, dispatch_proc_show, pde_data(inode)); -} - static ssize_t dispatch_proc_write(struct file *file, const char __user *userbuf, size_t count, loff_t *pos) @@ -910,10 +905,7 @@ static ssize_t dispatch_proc_write(struct file *file, } static const struct proc_ops dispatch_proc_ops = { - .proc_open = dispatch_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = dispatch_proc_show, .proc_write = dispatch_proc_write, }; diff --git a/drivers/platform/x86/toshiba_acpi.c b/drivers/platform/x86/toshiba_acpi.c index 9eb8f1e5aeda..560e62da05a9 100644 --- a/drivers/platform/x86/toshiba_acpi.c +++ b/drivers/platform/x86/toshiba_acpi.c @@ -1448,11 +1448,6 @@ static int lcd_proc_show(struct seq_file *m, void *v) return 0; } -static int lcd_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, lcd_proc_show, pde_data(inode)); -} - static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value) { u32 result; @@ -1509,10 +1504,7 @@ static ssize_t lcd_proc_write(struct file *file, const char __user *buf, } static const struct proc_ops lcd_proc_ops = { - .proc_open = lcd_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = lcd_proc_show, .proc_write = lcd_proc_write, }; @@ -1549,11 +1541,6 @@ static int video_proc_show(struct seq_file *m, void *v) return 0; } -static int video_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, video_proc_show, pde_data(inode)); -} - static ssize_t video_proc_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { @@ -1614,10 +1601,7 @@ static ssize_t video_proc_write(struct file *file, const char __user *buf, } static const struct proc_ops video_proc_ops = { - .proc_open = video_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = video_proc_show, .proc_write = video_proc_write, }; @@ -1660,11 +1644,6 @@ static int fan_proc_show(struct seq_file *m, void *v) return 0; } -static int fan_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, fan_proc_show, pde_data(inode)); -} - static ssize_t fan_proc_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { @@ -1691,10 +1670,7 @@ static ssize_t fan_proc_write(struct file *file, const char __user *buf, } static const struct proc_ops fan_proc_ops = { - .proc_open = fan_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = fan_proc_show, .proc_write = fan_proc_write, }; @@ -1731,11 +1707,6 @@ static int keys_proc_show(struct seq_file *m, void *v) return 0; } -static int keys_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, keys_proc_show, pde_data(inode)); -} - static ssize_t keys_proc_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { @@ -1758,10 +1729,7 @@ static ssize_t keys_proc_write(struct file *file, const char __user *buf, } static const struct proc_ops keys_proc_ops = { - .proc_open = keys_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = keys_proc_show, .proc_write = keys_proc_write, }; diff --git a/drivers/pnp/pnpbios/proc.c b/drivers/pnp/pnpbios/proc.c index 0f0d819b157f..14632a8d0da0 100644 --- a/drivers/pnp/pnpbios/proc.c +++ b/drivers/pnp/pnpbios/proc.c @@ -171,11 +171,6 @@ static int pnpbios_proc_show(struct seq_file *m, void *v) return 0; } -static int pnpbios_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, pnpbios_proc_show, pde_data(inode)); -} - static ssize_t pnpbios_proc_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { @@ -211,10 +206,7 @@ out: } static const struct proc_ops pnpbios_proc_ops = { - .proc_open = pnpbios_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = pnpbios_proc_show, .proc_write = pnpbios_proc_write, }; diff --git a/drivers/s390/block/dasd_proc.c b/drivers/s390/block/dasd_proc.c index 48e12e81df00..86399427f01d 100644 --- a/drivers/s390/block/dasd_proc.c +++ b/drivers/s390/block/dasd_proc.c @@ -246,11 +246,6 @@ static int dasd_stats_proc_show(struct seq_file *m, void *v) return 0; } -static int dasd_stats_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, dasd_stats_proc_show, NULL); -} - static ssize_t dasd_stats_proc_write(struct file *file, const char __user *user_buf, size_t user_len, loff_t *pos) { @@ -316,10 +311,7 @@ out_error: } static const struct proc_ops dasd_stats_proc_ops = { - .proc_open = dasd_stats_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = dasd_stats_proc_show, .proc_write = dasd_stats_proc_write, }; diff --git a/drivers/s390/cio/blacklist.c b/drivers/s390/cio/blacklist.c index 020d210bde9f..4e02f78f6102 100644 --- a/drivers/s390/cio/blacklist.c +++ b/drivers/s390/cio/blacklist.c @@ -397,18 +397,9 @@ static const struct seq_operations cio_ignore_proc_seq_ops = { .show = cio_ignore_proc_seq_show, }; -static int -cio_ignore_proc_open(struct inode *inode, struct file *file) -{ - return seq_open_private(file, &cio_ignore_proc_seq_ops, - sizeof(struct ccwdev_iter)); -} - static const struct proc_ops cio_ignore_proc_ops = { - .proc_open = cio_ignore_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = seq_release_private, + .proc_seq_ops = &cio_ignore_proc_seq_ops, + .proc_state_size = sizeof(struct ccwdev_iter), .proc_write = cio_ignore_write, }; diff --git a/drivers/scsi/scsi_devinfo.c b/drivers/scsi/scsi_devinfo.c index cfc259fd0141..0b8f5ff40543 100644 --- a/drivers/scsi/scsi_devinfo.c +++ b/drivers/scsi/scsi_devinfo.c @@ -670,11 +670,6 @@ static const struct seq_operations scsi_devinfo_seq_ops = { .show = devinfo_seq_show, }; -static int proc_scsi_devinfo_open(struct inode *inode, struct file *file) -{ - return seq_open(file, &scsi_devinfo_seq_ops); -} - /* * proc_scsi_dev_info_write - allow additions to scsi_dev_info_list via /proc. * @@ -714,11 +709,8 @@ out: } static const struct proc_ops scsi_devinfo_proc_ops = { - .proc_open = proc_scsi_devinfo_open, - .proc_read = seq_read, + .proc_seq_ops = &scsi_devinfo_seq_ops, .proc_write = proc_scsi_devinfo_write, - .proc_lseek = seq_lseek, - .proc_release = seq_release, }; #endif /* CONFIG_SCSI_PROC_FS */ diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c index dbd03360b5a4..3e49c4c8704f 100644 --- a/drivers/scsi/scsi_proc.c +++ b/drivers/scsi/scsi_proc.c @@ -521,28 +521,9 @@ static const struct seq_operations scsi_seq_ops = { .show = scsi_seq_show }; -/** - * proc_scsi_open - glue function - * @inode: not used - * @file: passed to single_open() - * - * Associates proc_scsi_show with this file - */ -static int proc_scsi_open(struct inode *inode, struct file *file) -{ - /* - * We don't really need this for the write case but it doesn't - * harm either. - */ - return seq_open(file, &scsi_seq_ops); -} - static const struct proc_ops scsi_scsi_proc_ops = { - .proc_open = proc_scsi_open, - .proc_read = seq_read, + .proc_seq_ops = &scsi_seq_ops, .proc_write = proc_scsi_write, - .proc_lseek = seq_lseek, - .proc_release = seq_release, }; /** diff --git a/drivers/usb/gadget/function/rndis.c b/drivers/usb/gadget/function/rndis.c index a2fd239b7ad3..3c7bd30b8a3a 100644 --- a/drivers/usb/gadget/function/rndis.c +++ b/drivers/usb/gadget/function/rndis.c @@ -1183,16 +1183,8 @@ static ssize_t rndis_proc_write(struct file *file, const char __user *buffer, return count; } -static int rndis_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, rndis_proc_show, pde_data(inode)); -} - static const struct proc_ops rndis_proc_ops = { - .proc_open = rndis_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = rndis_proc_show, .proc_write = rndis_proc_write, }; diff --git a/drivers/video/fbdev/via/viafbdev.c b/drivers/video/fbdev/via/viafbdev.c index 2c67a18d16ad..4b617e80a77c 100644 --- a/drivers/video/fbdev/via/viafbdev.c +++ b/drivers/video/fbdev/via/viafbdev.c @@ -1120,11 +1120,6 @@ static int viafb_dvp0_proc_show(struct seq_file *m, void *v) return 0; } -static int viafb_dvp0_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, viafb_dvp0_proc_show, NULL); -} - static ssize_t viafb_dvp0_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -1173,10 +1168,7 @@ static ssize_t viafb_dvp0_proc_write(struct file *file, } static const struct proc_ops viafb_dvp0_proc_ops = { - .proc_open = viafb_dvp0_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = viafb_dvp0_proc_show, .proc_write = viafb_dvp0_proc_write, }; @@ -1190,11 +1182,6 @@ static int viafb_dvp1_proc_show(struct seq_file *m, void *v) return 0; } -static int viafb_dvp1_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, viafb_dvp1_proc_show, NULL); -} - static ssize_t viafb_dvp1_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -1237,10 +1224,7 @@ static ssize_t viafb_dvp1_proc_write(struct file *file, } static const struct proc_ops viafb_dvp1_proc_ops = { - .proc_open = viafb_dvp1_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = viafb_dvp1_proc_show, .proc_write = viafb_dvp1_proc_write, }; @@ -1252,11 +1236,6 @@ static int viafb_dfph_proc_show(struct seq_file *m, void *v) return 0; } -static int viafb_dfph_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, viafb_dfph_proc_show, NULL); -} - static ssize_t viafb_dfph_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -1271,10 +1250,7 @@ static ssize_t viafb_dfph_proc_write(struct file *file, } static const struct proc_ops viafb_dfph_proc_ops = { - .proc_open = viafb_dfph_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = viafb_dfph_proc_show, .proc_write = viafb_dfph_proc_write, }; @@ -1286,11 +1262,6 @@ static int viafb_dfpl_proc_show(struct seq_file *m, void *v) return 0; } -static int viafb_dfpl_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, viafb_dfpl_proc_show, NULL); -} - static ssize_t viafb_dfpl_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -1305,10 +1276,7 @@ static ssize_t viafb_dfpl_proc_write(struct file *file, } static const struct proc_ops viafb_dfpl_proc_ops = { - .proc_open = viafb_dfpl_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = viafb_dfpl_proc_show, .proc_write = viafb_dfpl_proc_write, }; @@ -1344,11 +1312,6 @@ static int viafb_vt1636_proc_show(struct seq_file *m, void *v) return 0; } -static int viafb_vt1636_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, viafb_vt1636_proc_show, NULL); -} - static ssize_t viafb_vt1636_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -1440,10 +1403,7 @@ static ssize_t viafb_vt1636_proc_write(struct file *file, } static const struct proc_ops viafb_vt1636_proc_ops = { - .proc_open = viafb_vt1636_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = viafb_vt1636_proc_show, .proc_write = viafb_vt1636_proc_write, }; @@ -1491,11 +1451,6 @@ static int viafb_iga1_odev_proc_show(struct seq_file *m, void *v) return 0; } -static int viafb_iga1_odev_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, viafb_iga1_odev_proc_show, NULL); -} - static ssize_t viafb_iga1_odev_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -1517,10 +1472,7 @@ static ssize_t viafb_iga1_odev_proc_write(struct file *file, } static const struct proc_ops viafb_iga1_odev_proc_ops = { - .proc_open = viafb_iga1_odev_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = viafb_iga1_odev_proc_show, .proc_write = viafb_iga1_odev_proc_write, }; @@ -1530,11 +1482,6 @@ static int viafb_iga2_odev_proc_show(struct seq_file *m, void *v) return 0; } -static int viafb_iga2_odev_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, viafb_iga2_odev_proc_show, NULL); -} - static ssize_t viafb_iga2_odev_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -1556,10 +1503,7 @@ static ssize_t viafb_iga2_odev_proc_write(struct file *file, } static const struct proc_ops viafb_iga2_odev_proc_ops = { - .proc_open = viafb_iga2_odev_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = viafb_iga2_odev_proc_show, .proc_write = viafb_iga2_odev_proc_write, }; diff --git a/fs/jfs/jfs_debug.c b/fs/jfs/jfs_debug.c index 44b62b3c322e..5eb35b0d420f 100644 --- a/fs/jfs/jfs_debug.c +++ b/fs/jfs/jfs_debug.c @@ -23,11 +23,6 @@ static int jfs_loglevel_proc_show(struct seq_file *m, void *v) return 0; } -static int jfs_loglevel_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, jfs_loglevel_proc_show, NULL); -} - static ssize_t jfs_loglevel_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) { @@ -44,10 +39,7 @@ static ssize_t jfs_loglevel_proc_write(struct file *file, } static const struct proc_ops jfs_loglevel_proc_ops = { - .proc_open = jfs_loglevel_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = jfs_loglevel_proc_show, .proc_write = jfs_loglevel_proc_write, }; #endif diff --git a/fs/ntfs3/super.c b/fs/ntfs3/super.c index 37e27b73b7a5..cd47839872b0 100644 --- a/fs/ntfs3/super.c +++ b/fs/ntfs3/super.c @@ -548,11 +548,6 @@ static int ntfs3_volinfo(struct seq_file *m, void *o) return 0; } -static int ntfs3_volinfo_open(struct inode *inode, struct file *file) -{ - return single_open(file, ntfs3_volinfo, pde_data(inode)); -} - /* read /proc/fs/ntfs3//label */ static int ntfs3_label_show(struct seq_file *m, void *o) { @@ -598,23 +593,12 @@ out: return ret; } -static int ntfs3_label_open(struct inode *inode, struct file *file) -{ - return single_open(file, ntfs3_label_show, pde_data(inode)); -} - static const struct proc_ops ntfs3_volinfo_fops = { - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, - .proc_open = ntfs3_volinfo_open, + .proc_show = ntfs3_volinfo, }; static const struct proc_ops ntfs3_label_fops = { - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, - .proc_open = ntfs3_label_open, + .proc_show = ntfs3_label_show, .proc_write = ntfs3_label_write, }; diff --git a/fs/proc/cpuinfo.c b/fs/proc/cpuinfo.c index f38bda5b83ec..d8cd718c6030 100644 --- a/fs/proc/cpuinfo.c +++ b/fs/proc/cpuinfo.c @@ -7,17 +7,9 @@ extern const struct seq_operations cpuinfo_op; -static int cpuinfo_open(struct inode *inode, struct file *file) -{ - return seq_open(file, &cpuinfo_op); -} - static const struct proc_ops cpuinfo_proc_ops = { .proc_flags = PROC_ENTRY_PERMANENT, - .proc_open = cpuinfo_open, - .proc_read_iter = seq_read_iter, - .proc_lseek = seq_lseek, - .proc_release = seq_release, + .proc_seq_ops = &cpuinfo_op, }; static int __init proc_cpuinfo_init(void) diff --git a/fs/smb/client/cifs_debug.c b/fs/smb/client/cifs_debug.c index 577f0ddede90..dfcd2cdbb2a8 100644 --- a/fs/smb/client/cifs_debug.c +++ b/fs/smb/client/cifs_debug.c @@ -357,10 +357,6 @@ static int cifs_debug_dirs_proc_show(struct seq_file *m, void *v) } #ifdef CONFIG_CIFS_DEBUG -static int cifs_debug_dirs_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, cifs_debug_dirs_proc_show, NULL); -} /* Drop all cached directory entries across all CIFS mounts. */ static ssize_t cifs_debug_dirs_proc_write(struct file *file, const char __user *buffer, @@ -393,10 +389,7 @@ static ssize_t cifs_debug_dirs_proc_write(struct file *file, const char __user * } static const struct proc_ops cifs_debug_dirs_proc_ops = { - .proc_open = cifs_debug_dirs_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = cifs_debug_dirs_proc_show, .proc_write = cifs_debug_dirs_proc_write, }; #endif /* CONFIG_CIFS_DEBUG */ @@ -859,16 +852,8 @@ static int cifs_stats_proc_show(struct seq_file *m, void *v) return 0; } -static int cifs_stats_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, cifs_stats_proc_show, NULL); -} - static const struct proc_ops cifs_stats_proc_ops = { - .proc_open = cifs_stats_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = cifs_stats_proc_show, .proc_write = cifs_stats_proc_write, }; @@ -1012,11 +997,6 @@ static int cifsFYI_proc_show(struct seq_file *m, void *v) return 0; } -static int cifsFYI_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, cifsFYI_proc_show, NULL); -} - static ssize_t cifsFYI_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) { @@ -1038,10 +1018,7 @@ static ssize_t cifsFYI_proc_write(struct file *file, const char __user *buffer, } static const struct proc_ops cifsFYI_proc_ops = { - .proc_open = cifsFYI_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = cifsFYI_proc_show, .proc_write = cifsFYI_proc_write, }; @@ -1051,11 +1028,6 @@ static int cifs_linux_ext_proc_show(struct seq_file *m, void *v) return 0; } -static int cifs_linux_ext_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, cifs_linux_ext_proc_show, NULL); -} - static ssize_t cifs_linux_ext_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) { @@ -1069,10 +1041,7 @@ static ssize_t cifs_linux_ext_proc_write(struct file *file, } static const struct proc_ops cifs_linux_ext_proc_ops = { - .proc_open = cifs_linux_ext_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = cifs_linux_ext_proc_show, .proc_write = cifs_linux_ext_proc_write, }; @@ -1082,11 +1051,6 @@ static int cifs_lookup_cache_proc_show(struct seq_file *m, void *v) return 0; } -static int cifs_lookup_cache_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, cifs_lookup_cache_proc_show, NULL); -} - static ssize_t cifs_lookup_cache_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) { @@ -1100,10 +1064,7 @@ static ssize_t cifs_lookup_cache_proc_write(struct file *file, } static const struct proc_ops cifs_lookup_cache_proc_ops = { - .proc_open = cifs_lookup_cache_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = cifs_lookup_cache_proc_show, .proc_write = cifs_lookup_cache_proc_write, }; @@ -1113,11 +1074,6 @@ static int traceSMB_proc_show(struct seq_file *m, void *v) return 0; } -static int traceSMB_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, traceSMB_proc_show, NULL); -} - static ssize_t traceSMB_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) { @@ -1131,10 +1087,7 @@ static ssize_t traceSMB_proc_write(struct file *file, const char __user *buffer, } static const struct proc_ops traceSMB_proc_ops = { - .proc_open = traceSMB_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = traceSMB_proc_show, .proc_write = traceSMB_proc_write, }; @@ -1144,11 +1097,6 @@ static int cifs_security_flags_proc_show(struct seq_file *m, void *v) return 0; } -static int cifs_security_flags_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, cifs_security_flags_proc_show, NULL); -} - /* * Ensure that if someone sets a MUST flag, that we disable all other MAY * flags except for the ones corresponding to the given MUST flag. If there are @@ -1234,10 +1182,7 @@ static ssize_t cifs_security_flags_proc_write(struct file *file, } static const struct proc_ops cifs_security_flags_proc_ops = { - .proc_open = cifs_security_flags_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = cifs_security_flags_proc_show, .proc_write = cifs_security_flags_proc_write, }; @@ -1271,16 +1216,8 @@ static int cifs_mount_params_proc_show(struct seq_file *m, void *v) return 0; } -static int cifs_mount_params_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, cifs_mount_params_proc_show, NULL); -} - static const struct proc_ops cifs_mount_params_proc_ops = { - .proc_open = cifs_mount_params_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = cifs_mount_params_proc_show, /* No need for write for now */ /* .proc_write = cifs_mount_params_proc_write, */ }; diff --git a/fs/smb/client/dfs_cache.c b/fs/smb/client/dfs_cache.c index 26956e33037e..58f16cf83d36 100644 --- a/fs/smb/client/dfs_cache.c +++ b/fs/smb/client/dfs_cache.c @@ -213,16 +213,8 @@ static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer, return count; } -static int dfscache_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, dfscache_proc_show, NULL); -} - const struct proc_ops dfscache_proc_ops = { - .proc_open = dfscache_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = dfscache_proc_show, .proc_write = dfscache_proc_write, }; diff --git a/kernel/irq/proc.c b/kernel/irq/proc.c index 0c0df16de581..aa3d5544cfc1 100644 --- a/kernel/irq/proc.c +++ b/kernel/irq/proc.c @@ -188,29 +188,13 @@ static ssize_t irq_affinity_list_proc_write(struct file *file, return write_irq_affinity(1, file, buffer, count, pos); } -static int irq_affinity_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, irq_affinity_proc_show, pde_data(inode)); -} - -static int irq_affinity_list_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, irq_affinity_list_proc_show, pde_data(inode)); -} - static const struct proc_ops irq_affinity_proc_ops = { - .proc_open = irq_affinity_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = irq_affinity_proc_show, .proc_write = irq_affinity_proc_write, }; static const struct proc_ops irq_affinity_list_proc_ops = { - .proc_open = irq_affinity_list_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = irq_affinity_list_proc_show, .proc_write = irq_affinity_list_proc_write, }; @@ -263,16 +247,8 @@ out: return err; } -static int default_affinity_open(struct inode *inode, struct file *file) -{ - return single_open(file, default_affinity_show, pde_data(inode)); -} - static const struct proc_ops default_affinity_proc_ops = { - .proc_open = default_affinity_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = default_affinity_show, .proc_write = default_affinity_write, }; diff --git a/kernel/latencytop.c b/kernel/latencytop.c index d4281d1e13a6..b764f47606d3 100644 --- a/kernel/latencytop.c +++ b/kernel/latencytop.c @@ -275,17 +275,9 @@ lstats_write(struct file *file, const char __user *buf, size_t count, return count; } -static int lstats_open(struct inode *inode, struct file *filp) -{ - return single_open(filp, lstats_show, NULL); -} - static const struct proc_ops lstats_proc_ops = { - .proc_open = lstats_open, - .proc_read = seq_read, + .proc_show = lstats_show, .proc_write = lstats_write, - .proc_lseek = seq_lseek, - .proc_release = single_release, }; static int __init init_lstats_procfs(void) diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c index 18a71a9108d3..64a211290872 100644 --- a/lib/dynamic_debug.c +++ b/lib/dynamic_debug.c @@ -1194,10 +1194,8 @@ static const struct file_operations ddebug_proc_fops = { }; static const struct proc_ops proc_fops = { - .proc_open = ddebug_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = seq_release_private, + .proc_seq_ops = &ddebug_proc_seqops, + .proc_state_size = sizeof(struct ddebug_iter), .proc_write = ddebug_proc_write }; diff --git a/mm/slab_common.c b/mm/slab_common.c index 01c64ef661e7..fbf303b45858 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1233,17 +1233,9 @@ static const struct seq_operations slabinfo_op = { .show = slab_show, }; -static int slabinfo_open(struct inode *inode, struct file *file) -{ - return seq_open(file, &slabinfo_op); -} - static const struct proc_ops slabinfo_proc_ops = { .proc_flags = PROC_ENTRY_PERMANENT, - .proc_open = slabinfo_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = seq_release, + .proc_seq_ops = &slabinfo_op, }; static int __init slab_proc_init(void) diff --git a/net/core/pktgen.c b/net/core/pktgen.c index 02af70d360bc..12363a373fb8 100644 --- a/net/core/pktgen.c +++ b/net/core/pktgen.c @@ -550,17 +550,9 @@ static ssize_t pgctrl_write(struct file *file, const char __user *buf, return count; } -static int pgctrl_open(struct inode *inode, struct file *file) -{ - return single_open(file, pgctrl_show, pde_data(inode)); -} - static const struct proc_ops pktgen_proc_ops = { - .proc_open = pgctrl_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = pgctrl_show, .proc_write = pgctrl_write, - .proc_release = single_release, }; static int pktgen_if_show(struct seq_file *seq, void *v) @@ -1870,17 +1862,9 @@ static ssize_t pktgen_if_write(struct file *file, return -EINVAL; } -static int pktgen_if_open(struct inode *inode, struct file *file) -{ - return single_open(file, pktgen_if_show, pde_data(inode)); -} - static const struct proc_ops pktgen_if_proc_ops = { - .proc_open = pktgen_if_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = pktgen_if_show, .proc_write = pktgen_if_write, - .proc_release = single_release, }; static int pktgen_thread_show(struct seq_file *seq, void *v) @@ -2010,17 +1994,9 @@ out: return ret; } -static int pktgen_thread_open(struct inode *inode, struct file *file) -{ - return single_open(file, pktgen_thread_show, pde_data(inode)); -} - static const struct proc_ops pktgen_thread_proc_ops = { - .proc_open = pktgen_thread_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, + .proc_show = pktgen_thread_show, .proc_write = pktgen_thread_write, - .proc_release = single_release, }; /* Think find or remove for NN */ diff --git a/net/sunrpc/stats.c b/net/sunrpc/stats.c index d08711bee18e..f5ee18a0eb37 100644 --- a/net/sunrpc/stats.c +++ b/net/sunrpc/stats.c @@ -64,16 +64,8 @@ static int rpc_proc_show(struct seq_file *seq, void *v) { return 0; } -static int rpc_proc_open(struct inode *inode, struct file *file) -{ - return single_open(file, rpc_proc_show, pde_data(inode)); -} - static const struct proc_ops rpc_proc_ops = { - .proc_open = rpc_proc_open, - .proc_read = seq_read, - .proc_lseek = seq_lseek, - .proc_release = single_release, + .proc_show = rpc_proc_show, }; /* -- 2.53.0 Read /proc/net/dev_snmp6/ of a tun device into a buffer whose first page is backed by userfaultfd and remove the device while the reader sleeps in its copy to userspace: - removing the entry doesn't wait for the reader - the reader still gets what was printed before the removal - and the next read fails with -EIO The entry is removed under RTNL, so a regression stalls the test for five seconds before it fails. Signed-off-by: Christian Brauner (Amutable) --- tools/testing/selftests/proc/.gitignore | 1 + tools/testing/selftests/proc/Makefile | 1 + .../selftests/proc/proc-remove-slow-reader.c | 133 +++++++++++++++++++++ 3 files changed, 135 insertions(+) diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selftests/proc/.gitignore index 3fa89f46b9c7..e2566cc0ae07 100644 --- a/tools/testing/selftests/proc/.gitignore +++ b/tools/testing/selftests/proc/.gitignore @@ -22,6 +22,7 @@ /proc-uptime-002 /proc-pidns /proc-remove-slow-sysctl +/proc-remove-slow-reader /proc-remove-slow-readlink /read /self diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile index df02699a3587..6a9bdc999f54 100644 --- a/tools/testing/selftests/proc/Makefile +++ b/tools/testing/selftests/proc/Makefile @@ -33,6 +33,7 @@ TEST_GEN_PROGS += proc-fsconfig-hidepid TEST_GEN_PROGS += proc-sysctl-readdir-churn TEST_GEN_PROGS += proc-pidns TEST_GEN_PROGS += proc-remove-slow-sysctl +TEST_GEN_PROGS += proc-remove-slow-reader TEST_GEN_PROGS += proc-remove-slow-readlink include ../lib.mk diff --git a/tools/testing/selftests/proc/proc-remove-slow-reader.c b/tools/testing/selftests/proc/proc-remove-slow-reader.c new file mode 100644 index 000000000000..a201645beca9 --- /dev/null +++ b/tools/testing/selftests/proc/proc-remove-slow-reader.c @@ -0,0 +1,133 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A read that faults while copying to userspace must not hold up removal of + * the /proc entry. /proc/net/dev_snmp6/ of a tun device is removed under + * RTNL when the device's file is closed. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#define TUN_NAME "procslow0" +#define SNMP6_PATH "/proc/net/dev_snmp6/" TUN_NAME +#define READ_PAGES 16 +#define TIMEOUT_MS 5000 + +struct reader { + int fd; + char *buf; + size_t len; + ssize_t ret; +}; + +struct closer { + int tun; + int done; +}; + +static void *read_thread(void *arg) +{ + struct reader *r = arg; + + r->ret = read(r->fd, r->buf, r->len); + return NULL; +} + +static void *close_thread(void *arg) +{ + struct closer *c = arg; + ssize_t ret; + + close(c->tun); + ret = write(c->done, "x", 1); + return (void *)ret; +} + +TEST(remove_while_reader_faults) +{ + struct uffdio_api api = { .api = UFFD_API }; + struct uffdio_register reg = {}; + struct uffdio_zeropage zero = {}; + long psz = sysconf(_SC_PAGESIZE); + struct ifreq ifr = {}; + struct reader r = {}; + struct closer c = {}; + struct uffd_msg msg; + struct pollfd pfd; + pthread_t rt, ct; + int uffd, done[2]; + char *buf; + + if (unshare(CLONE_NEWNET)) + SKIP(return, "unshare(CLONE_NEWNET): %s", strerror(errno)); + + c.tun = open("/dev/net/tun", O_RDWR | O_CLOEXEC); + if (c.tun < 0) + SKIP(return, "/dev/net/tun: %s", strerror(errno)); + strncpy(ifr.ifr_name, TUN_NAME, IFNAMSIZ - 1); + ifr.ifr_flags = IFF_TUN | IFF_NO_PI; + ASSERT_EQ(ioctl(c.tun, TUNSETIFF, &ifr), 0); + + r.fd = open(SNMP6_PATH, O_RDONLY | O_CLOEXEC); + if (r.fd < 0) + SKIP(return, SNMP6_PATH ": %s", strerror(errno)); + + uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); + if (uffd < 0) + SKIP(return, "userfaultfd: %s", strerror(errno)); + ASSERT_EQ(ioctl(uffd, UFFDIO_API, &api), 0); + + /* Only the first page of the buffer goes through userfaultfd. */ + buf = mmap(NULL, READ_PAGES * psz, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(buf, MAP_FAILED); + reg.range.start = (unsigned long)buf; + reg.range.len = psz; + reg.mode = UFFDIO_REGISTER_MODE_MISSING; + ASSERT_EQ(ioctl(uffd, UFFDIO_REGISTER, ®), 0); + + r.buf = buf; + r.len = READ_PAGES * psz; + ASSERT_EQ(pthread_create(&rt, NULL, read_thread, &r), 0); + + /* The reader now sleeps in its copy to userspace. */ + pfd = (struct pollfd){ .fd = uffd, .events = POLLIN }; + ASSERT_EQ(poll(&pfd, 1, TIMEOUT_MS), 1); + ASSERT_EQ(read(uffd, &msg, sizeof(msg)), sizeof(msg)); + ASSERT_EQ(msg.event, UFFD_EVENT_PAGEFAULT); + + /* Closing the device removes the file that is being read. */ + ASSERT_EQ(pipe2(done, O_CLOEXEC), 0); + c.done = done[1]; + ASSERT_EQ(pthread_create(&ct, NULL, close_thread, &c), 0); + pfd = (struct pollfd){ .fd = done[0], .events = POLLIN }; + EXPECT_EQ(poll(&pfd, 1, TIMEOUT_MS), 1) { + TH_LOG("removing " SNMP6_PATH " waited for the reader"); + } + + zero.range.start = (unsigned long)buf; + zero.range.len = psz; + ASSERT_EQ(ioctl(uffd, UFFDIO_ZEROPAGE, &zero), 0); + ASSERT_EQ(pthread_join(rt, NULL), 0); + ASSERT_EQ(pthread_join(ct, NULL), 0); + + /* What was printed before the removal still arrives, then nothing. */ + EXPECT_GT(r.ret, 0); + EXPECT_EQ(read(r.fd, buf, psz), -1); + EXPECT_EQ(errno, EIO); +} + +TEST_HARNESS_MAIN -- 2.53.0 single_open_size() allocates the buffer itself because it does the single_open() too. A file whose seq_file is opened by somebody else, like procfs does now, needs to do that after the fact. Add seq_preallocate() for that and use it in single_open_size(). No functional changes. Signed-off-by: Christian Brauner (Amutable) --- fs/seq_file.c | 36 ++++++++++++++++++++++++++++-------- include/linux/seq_file.h | 1 + 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/fs/seq_file.c b/fs/seq_file.c index 456c78719fd0..a4644cdda7e6 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -590,21 +590,41 @@ int single_open(struct file *file, int (*show)(struct seq_file *, void *), } EXPORT_SYMBOL(single_open); -int single_open_size(struct file *file, int (*show)(struct seq_file *, void *), - void *data, size_t size) +/** + * seq_preallocate - start out with a buffer of @size + * @m: the seq_file, before it is first read + * @size: the size of the buffer + * + * For files whose records don't fit into a page, so that the first read + * doesn't have to work its way up to the right buffer size. + */ +int seq_preallocate(struct seq_file *m, size_t size) { char *buf = seq_buf_alloc(size); - int ret; + if (!buf) return -ENOMEM; + kvfree(m->buf); + m->buf = buf; + m->size = size; + return 0; +} +EXPORT_SYMBOL(seq_preallocate); + +int single_open_size(struct file *file, int (*show)(struct seq_file *, void *), + void *data, size_t size) +{ + int ret; + ret = single_open(file, show, data); - if (ret) { - kvfree(buf); + if (ret) return ret; + ret = seq_preallocate(file->private_data, size); + if (ret) { + single_release(file_inode(file), file); + file->private_data = NULL; } - ((struct seq_file *)file->private_data)->buf = buf; - ((struct seq_file *)file->private_data)->size = size; - return 0; + return ret; } EXPORT_SYMBOL(single_open_size); diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index b813b7e63d89..834daff48b7d 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -175,6 +175,7 @@ int seq_path_root(struct seq_file *m, const struct path *path, void *single_start(struct seq_file *, loff_t *); int single_open(struct file *, int (*)(struct seq_file *, void *), void *); int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t); +int seq_preallocate(struct seq_file *m, size_t size); int single_release(struct inode *, struct file *); void *__seq_open_private(struct file *, const struct seq_operations *, int); int seq_open_private(struct file *, const struct seq_operations *, int); -- 2.53.0 Declare the show function and let procfs open and free the seq_file. ->proc_open() only preallocates the buffer now. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/stat.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fs/proc/stat.c b/fs/proc/stat.c index 79b42f2bfd00..3bfb79edc0f6 100644 --- a/fs/proc/stat.c +++ b/fs/proc/stat.c @@ -161,15 +161,13 @@ static int stat_open(struct inode *inode, struct file *file) /* minimum size to display an interrupt count : 2 bytes */ size += 2 * irq_get_nr_irqs(); - return single_open_size(file, show_stat, NULL, size); + return seq_preallocate(file->private_data, size); } static const struct proc_ops stat_proc_ops = { .proc_flags = PROC_ENTRY_PERMANENT, + .proc_show = show_stat, .proc_open = stat_open, - .proc_read_iter = seq_read_iter, - .proc_lseek = seq_lseek, - .proc_release = single_release, }; static int __init proc_stat_init(void) -- 2.53.0 proc_reg_write() pins the entry and calls ->proc_write() which copies from userspace itself. A page fault in that copy can take as long as the writer wants, just like the copy to userspace of a read, and remove_proc_entry() waits for it. So do what kernfs does for writes as well. An entry that implements ->proc_write_buf() gets a NUL-terminated kernel copy of what was written. procfs makes that copy before it pins the entry. The copy is capped at ->proc_write_size bytes, or at PAGE_SIZE - 1 like proc_simple_write() if the entry doesn't care. Larger writes fail with -EINVAL. The proc_ops go away with the entry, so procfs remembers the limit in the entry when it is registered, like the size of the seq_file state, instead of pinning the entry just to look up a number. Entries that take binary data in pieces, like the PCI config space, ALSA's data entries or a firmware image, can ask for PROC_ENTRY_SHORT_WRITES instead. procfs then copies in the first ->proc_write_size bytes and the write comes up short. ->proc_write_buf() gets the position like ->proc_write() so entries that write at an offset keep working. Note that procfs copies in the whole write before the entry looks at any of it. So a write that is too large or whose buffer can't be read fails before the entry's own checks run, permission checks included, even where the entry only ever looked at the first few bytes. An entry that implements ->proc_write_buf() and still has a ->proc_write() fails to register. No users yet. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 8 ++++++++ fs/proc/inode.c | 33 +++++++++++++++++++++++++++++++++ fs/proc/internal.h | 11 +++++++++++ include/linux/proc_fs.h | 17 +++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 959bf9e33245..4f1f83e9f288 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -466,6 +466,14 @@ static int pde_set_flags(struct proc_dir_entry *pde) if (proc_ops->proc_lseek) pde->flags |= PROC_ENTRY_proc_lseek; } + if (proc_ops->proc_write_buf) { + if (WARN_ON_ONCE(proc_ops->proc_write)) + return -EINVAL; + pde->flags |= PROC_ENTRY_proc_write_buf; + pde->write_size = proc_ops->proc_write_size ?: PAGE_SIZE - 1; + if (proc_ops->proc_flags & PROC_ENTRY_SHORT_WRITES) + pde->flags |= PROC_ENTRY_SHORT_WRITES; + } return 0; } diff --git a/fs/proc/inode.c b/fs/proc/inode.c index d51ab7548b11..df1caa0f238d 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -417,11 +417,44 @@ static ssize_t pde_write(struct proc_dir_entry *pde, struct file *file, const ch return -EIO; } +/* + * procfs copies in what is written before it pins the entry. A writer that + * faults on its buffer then doesn't hold up remove_proc_entry(). + */ +static ssize_t pde_write_buf(struct proc_dir_entry *pde, struct file *file, + const char __user *ubuf, size_t count, loff_t *ppos) +{ + char *buf __free(kvfree) = NULL; + size_t size; + ssize_t rv; + + size = pde->write_size; + if (count > size) { + if (!pde_has_short_writes(pde)) + return -EINVAL; + count = size; + } + buf = kvmalloc(count + 1, GFP_KERNEL); + if (!buf) + return -ENOMEM; + if (copy_from_user(buf, ubuf, count)) + return -EFAULT; + buf[count] = '\0'; + + if (!pde_pin(pde)) + return -EIO; + rv = pde->proc_ops->proc_write_buf(file, buf, count, ppos); + pde_unpin(pde); + return rv; +} + static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct proc_dir_entry *pde = PDE(file_inode(file)); ssize_t rv; + if (pde_has_proc_write_buf(pde)) + return pde_write_buf(pde, file, buf, count, ppos); if (!pde_pin(pde)) return -EIO; rv = pde_write(pde, file, buf, count, ppos); diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 674e6b4ed21e..da246c5ed4bf 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -51,6 +51,7 @@ struct proc_dir_entry { proc_write_t write; void *data; unsigned int state_size; + unsigned int write_size; unsigned int low_ino; nlink_t nlink; kuid_t uid; @@ -109,6 +110,16 @@ static inline bool pde_has_proc_show(const struct proc_dir_entry *pde) return pde->flags & PROC_ENTRY_proc_show; } +static inline bool pde_has_proc_write_buf(const struct proc_dir_entry *pde) +{ + return pde->flags & PROC_ENTRY_proc_write_buf; +} + +static inline bool pde_has_short_writes(const struct proc_dir_entry *pde) +{ + return pde->flags & PROC_ENTRY_SHORT_WRITES; +} + /* procfs owns the seq_file, see pde_seq_start() */ static inline bool pde_is_seq_file(const struct proc_dir_entry *pde) { diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index ea55ed85a2d6..c81c93325044 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -31,6 +31,13 @@ enum { PROC_ENTRY_proc_show = 1U << 5, PROC_ENTRY_FORCE_LOOKUP = 1U << 7, + PROC_ENTRY_proc_write_buf = 1U << 8, + + /* + * Writes larger than ->proc_write_size come up short instead of + * failing with -EINVAL. + */ + PROC_ENTRY_SHORT_WRITES = 1U << 9, }; struct proc_ops { @@ -67,6 +74,16 @@ struct proc_ops { const struct seq_operations *proc_seq_ops; int (*proc_show)(struct seq_file *, void *); unsigned int proc_state_size; + /* + * procfs copies what is written into a kernel buffer and calls + * ->proc_write_buf() with it. The buffer is NUL-terminated and holds + * at most ->proc_write_size bytes, PAGE_SIZE - 1 if that's 0. Larger + * writes fail with -EINVAL unless the entry asks for + * PROC_ENTRY_SHORT_WRITES. The position is NULL for a stream, like + * for ->write(). + */ + ssize_t (*proc_write_buf)(struct file *, char *, size_t, loff_t *); + unsigned int proc_write_size; } __randomize_layout; /* definitions for hide_pid field */ -- 2.53.0 proc_simple_write() already hands ->write() a kernel copy of what was written but makes that copy with the entry pinned. Let procfs make it before it pins the entry instead. That covers everything created with proc_create_net_data_write() and proc_create_net_single_write(), the afs files and the rxrpc stats. So a write to a proc_create_net_single() file without ->write() can fail with -EINVAL or -EFAULT now before proc_simple_write() gets to refuse it with -EACCES. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 15 ++++----------- fs/proc/internal.h | 2 +- fs/proc/proc_net.c | 4 ++-- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 4f1f83e9f288..5e2ed5a04ec9 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -947,26 +947,19 @@ void proc_remove(struct proc_dir_entry *de) EXPORT_SYMBOL(proc_remove); /* - * Pull a user buffer into memory and pass it to the file's write handler if - * one is supplied. The ->write() method is permitted to modify the - * kernel-side buffer. + * Pass what was written to the file's write handler if one is supplied. The + * ->write() method is permitted to modify the buffer. */ -ssize_t proc_simple_write(struct file *f, const char __user *ubuf, size_t size, - loff_t *_pos) +ssize_t proc_simple_write(struct file *f, char *buf, size_t size, loff_t *_pos) { struct proc_dir_entry *pde = PDE(file_inode(f)); - char *buf; int ret; if (!pde->write) return -EACCES; - if (size == 0 || size > PAGE_SIZE - 1) + if (size == 0) return -EINVAL; - buf = memdup_user_nul(ubuf, size); - if (IS_ERR(buf)) - return PTR_ERR(buf); ret = pde->write(f, buf, size); - kfree(buf); return ret == 0 ? size : ret; } diff --git a/fs/proc/internal.h b/fs/proc/internal.h index da246c5ed4bf..1d36fe4b1af5 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -344,7 +344,7 @@ static inline bool is_empty_pde(const struct proc_dir_entry *pde) { return S_ISDIR(pde->mode) && !pde->proc_iops; } -extern ssize_t proc_simple_write(struct file *, const char __user *, size_t, loff_t *); +extern ssize_t proc_simple_write(struct file *, char *, size_t, loff_t *); /* * inode.c diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c index 9973f846c467..b78d627b024f 100644 --- a/fs/proc/proc_net.c +++ b/fs/proc/proc_net.c @@ -77,7 +77,7 @@ static int seq_release_net(struct inode *ino, struct file *f) static const struct proc_ops proc_net_seq_ops = { .proc_open = seq_open_net, - .proc_write = proc_simple_write, + .proc_write_buf = proc_simple_write, .proc_release = seq_release_net, }; @@ -186,7 +186,7 @@ static int single_release_net(struct inode *ino, struct file *f) static const struct proc_ops proc_net_single_ops = { .proc_open = single_open_net, - .proc_write = proc_simple_write, + .proc_write_buf = proc_simple_write, .proc_release = single_release_net, }; -- 2.53.0 proc_reg_read() and proc_reg_read_iter() pin the entry and call ->proc_read() or ->proc_read_iter() which copy to userspace themselves. Like for seq_file reads and for writes, a reader can make a page fault in that copy take as long as it wants and remove_proc_entry() waits for it. So do what kernfs does for these reads as well. An entry that implements ->proc_read_buf() fills a kernel buffer of at most ->proc_read_size bytes, a page if the entry doesn't care, and procfs copies it to userspace after unpinning the entry. The entry moves the position like it did for ->proc_read() but that only sticks if the copy to userspace worked, like in kernfs_file_read_iter(). If the copy fails halfway the read fails with -EFAULT and the position stays, where simple_read_from_buffer() returned what it had copied until then. procfs remembers ->proc_read_size in the entry like ->proc_write_size. Larger reads come up short, like with kernfs. An entry whose readers want all of a large file in one read asks for PROC_ENTRY_FULL_READS and procfs fills the read in pieces of ->proc_read_size, unpinning the entry in between. A piece whose copy fails ends the read with what the pieces before it returned. kexec-tools treats any short read of /proc/vmcore as fatal for example (cf. [1]). These entries are read through ->read_iter() so readv(), splice(), aio, the RWF flags, kernel_read() and io_uring work for them like for the ->proc_read_iter() ones. io_uring's nonblocking attempt gets -EAGAIN though, like loop_rw_iter() gave it for ->proc_read() entries. Neither the allocation nor the entry can honor IOCB_NOWAIT, and io_uring would take a short read of a record file for an unfinished one and glue the next record to it. So it retries from a worker like before. An entry that implements ->proc_read_buf() and also declares a seq_file or one of the other read methods fails to register. No users yet. [1]: https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/tree/util_lib/elf_info.c Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 9 ++++++++ fs/proc/inode.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++- fs/proc/internal.h | 11 +++++++++ include/linux/proc_fs.h | 16 +++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 5e2ed5a04ec9..2d503250de75 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -474,6 +474,15 @@ static int pde_set_flags(struct proc_dir_entry *pde) if (proc_ops->proc_flags & PROC_ENTRY_SHORT_WRITES) pde->flags |= PROC_ENTRY_SHORT_WRITES; } + if (proc_ops->proc_read_buf) { + if (WARN_ON_ONCE(pde_is_seq_file(pde) || proc_ops->proc_read || + proc_ops->proc_read_iter)) + return -EINVAL; + pde->flags |= PROC_ENTRY_proc_read_buf; + pde->read_size = proc_ops->proc_read_size ?: PAGE_SIZE; + if (proc_ops->proc_flags & PROC_ENTRY_FULL_READS) + pde->flags |= PROC_ENTRY_FULL_READS; + } return 0; } diff --git a/fs/proc/inode.c b/fs/proc/inode.c index df1caa0f238d..caa47933c233 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include #include @@ -377,11 +379,67 @@ static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence) return rv; } +/* + * The entry fills a kernel buffer and procfs copies that to userspace after + * unpinning the entry. A reader that faults on its buffer then doesn't hold + * up remove_proc_entry(). + */ +static ssize_t pde_read_buf(struct proc_dir_entry *pde, struct kiocb *iocb, + struct iov_iter *iter) +{ + struct file *file = iocb->ki_filp; + char *buf __free(kvfree) = NULL; + ssize_t rv, done = 0; + size_t len, count; + loff_t pos; + + if (!iov_iter_count(iter)) + return 0; + /* neither the allocation nor the entry can honor IOCB_NOWAIT */ + if ((iocb->ki_flags & IOCB_NOWAIT) && !(file->f_flags & O_NONBLOCK)) + return -EAGAIN; + len = min_t(size_t, iov_iter_count(iter), pde->read_size); + buf = kvmalloc(len, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + /* PROC_ENTRY_FULL_READS fills the read in pieces */ + do { + count = min_t(size_t, iov_iter_count(iter), len); + pos = iocb->ki_pos; + if (!pde_pin(pde)) { + rv = -EIO; + break; + } + rv = pde->proc_ops->proc_read_buf(file, buf, count, &pos); + pde_unpin(pde); + if (rv <= 0) + break; + if (WARN_ON_ONCE(rv > count)) { + rv = -EIO; + break; + } + if (copy_to_iter(buf, rv, iter) != rv) { + rv = -EFAULT; + break; + } + iocb->ki_pos = pos; + done += rv; + if (!pde_has_full_reads(pde) || rv < count) + break; + cond_resched(); + } while (iov_iter_count(iter) && !fatal_signal_pending(current)); + + return done ?: rv; +} + static ssize_t proc_reg_read_iter(struct kiocb *iocb, struct iov_iter *iter) { struct proc_dir_entry *pde = PDE(file_inode(iocb->ki_filp)); ssize_t ret; + if (pde_has_proc_read_buf(pde)) + return pde_read_buf(pde, iocb, iter); if (!pde_pin(pde)) return -EIO; ret = pde->proc_ops->proc_read_iter(iocb, iter); @@ -793,7 +851,7 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de) inode->i_op = de->proc_iops; if (pde_is_seq_file(de)) inode->i_fop = &proc_seq_file_ops; - else if (pde_has_proc_read_iter(de)) + else if (pde_has_proc_read_iter(de) || pde_has_proc_read_buf(de)) inode->i_fop = &proc_iter_file_ops; else inode->i_fop = &proc_reg_file_ops; diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 1d36fe4b1af5..b5c78f4f392c 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -52,6 +52,7 @@ struct proc_dir_entry { void *data; unsigned int state_size; unsigned int write_size; + unsigned int read_size; unsigned int low_ino; nlink_t nlink; kuid_t uid; @@ -120,6 +121,16 @@ static inline bool pde_has_short_writes(const struct proc_dir_entry *pde) return pde->flags & PROC_ENTRY_SHORT_WRITES; } +static inline bool pde_has_proc_read_buf(const struct proc_dir_entry *pde) +{ + return pde->flags & PROC_ENTRY_proc_read_buf; +} + +static inline bool pde_has_full_reads(const struct proc_dir_entry *pde) +{ + return pde->flags & PROC_ENTRY_FULL_READS; +} + /* procfs owns the seq_file, see pde_seq_start() */ static inline bool pde_is_seq_file(const struct proc_dir_entry *pde) { diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index c81c93325044..0f0997898ba3 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -38,6 +38,13 @@ enum { * failing with -EINVAL. */ PROC_ENTRY_SHORT_WRITES = 1U << 9, + PROC_ENTRY_proc_read_buf = 1U << 10, + + /* + * Reads larger than ->proc_read_size are filled in pieces instead of + * coming up short. + */ + PROC_ENTRY_FULL_READS = 1U << 11, }; struct proc_ops { @@ -84,6 +91,15 @@ struct proc_ops { */ ssize_t (*proc_write_buf)(struct file *, char *, size_t, loff_t *); unsigned int proc_write_size; + /* + * procfs calls ->proc_read_buf() to fill a kernel buffer of at most + * ->proc_read_size bytes, PAGE_SIZE if that's 0, and copies it to + * userspace afterwards. Larger reads come up short unless the entry + * asks for PROC_ENTRY_FULL_READS. The position the entry moves to only + * sticks if the copy works. + */ + ssize_t (*proc_read_buf)(struct file *, char *, size_t, loff_t *); + unsigned int proc_read_size; } __randomize_layout; /* definitions for hide_pid field */ -- 2.53.0 Implement ->proc_read_buf() so procfs copies what is read to userspace after it unpinned the entry. __read_vmcore() fills procfs' buffer through a kvec iov_iter. So a read goes through that buffer now instead of straight from the old memory into the reader's, which is what splice() did already. mmap() isn't affected. On ppc64 a machine check while reading the old memory used to be recovered through the exception table of copy_to_user(), by accident. copy_oldmem_page() copies with _copy_mc_to_iter() now, so a read through procfs' buffer recovers from it as well. The buffer is 128k, which keeps the entry pinned for one such piece at a time, and /proc/vmcore asks for PROC_ENTRY_FULL_READS so that a larger read is filled in pieces. kexec-tools treats a short read of it as fatal. A read that faults on its buffer in a later piece returns the pieces before it now instead of failing with -EFAULT. fadump removes /proc/vmcore when its memory is released and that no longer waits for a reader that faults on its buffer. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/vmcore.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c index ccb795b85509..1c1e71231f4e 100644 --- a/fs/proc/vmcore.c +++ b/fs/proc/vmcore.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -427,9 +428,14 @@ static ssize_t __read_vmcore(struct iov_iter *iter, loff_t *fpos) return acc; } -static ssize_t read_vmcore(struct kiocb *iocb, struct iov_iter *iter) +static ssize_t read_vmcore(struct file *file, char *buf, size_t count, + loff_t *ppos) { - return __read_vmcore(iter, &iocb->ki_pos); + struct kvec kvec = { .iov_base = buf, .iov_len = count }; + struct iov_iter iter; + + iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, count); + return __read_vmcore(&iter, ppos); } /** @@ -709,9 +715,11 @@ static int mmap_vmcore(struct file *file, struct vm_area_struct *vma) #endif static const struct proc_ops vmcore_proc_ops = { + .proc_flags = PROC_ENTRY_FULL_READS, .proc_open = open_vmcore, .proc_release = release_vmcore, - .proc_read_iter = read_vmcore, + .proc_read_buf = read_vmcore, + .proc_read_size = SZ_128K, .proc_lseek = default_llseek, .proc_mmap = mmap_vmcore, }; -- 2.53.0 proc_reg_unlocked_ioctl() pins the entry and calls ->proc_ioctl() which copies its argument from and to userspace itself. Like for reads and writes, an ioctl can make a page fault in those copies take as long as it wants and remove_proc_entry() waits for it. The command number doesn't reliably say what an ioctl copies. Some of the procfs ones aren't encoded at all, like the PCI ones or esas2r's, and i8k's encode the wrong sizes. So keep a table of the commands, like DRM keeps one of its ioctls, and let it say what each command copies. An entry that implements ->proc_ioctl_buf() lists its commands in ->proc_ioctl_args with the number of bytes that go in and that come back out. procfs copies the argument in before it pins the entry, calls ->proc_ioctl_buf() with that kernel copy and copies it back out after unpinning the entry if the ioctl succeeded. A command that copies neither gets its argument as is, a command that isn't in the table fails with -ENOTTY. PROC_IOCTL_IOC() fills in an entry from a command that encodes its argument. Note that, like for writes, procfs copies the argument in before the entry's own checks run, permission checks included. So a bad pointer fails with -EFAULT where the entry used to say -EPERM first. The table lives with the proc_ops, which go away with the entry, so procfs pins the entry to look the command up and lets go of it again before it copies anything. 32-bit tasks go through the same table with their pointer converted by compat_ptr(). An entry that implements ->proc_ioctl_buf() and still has a ->proc_ioctl() or a ->proc_compat_ioctl() fails to register, and so does one that lists commands without implementing it or implements it without listing any. No users yet. --- fs/proc/generic.c | 17 +++++++++++++++ fs/proc/inode.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/proc/internal.h | 5 +++++ include/linux/proc_fs.h | 35 +++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 2d503250de75..1bad3b068d53 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -483,6 +483,23 @@ static int pde_set_flags(struct proc_dir_entry *pde) if (proc_ops->proc_flags & PROC_ENTRY_FULL_READS) pde->flags |= PROC_ENTRY_FULL_READS; } + if (proc_ops->proc_ioctl_buf) { + if (WARN_ON_ONCE(proc_ops->proc_ioctl)) + return -EINVAL; +#ifdef CONFIG_COMPAT + /* 32-bit tasks go through the table as well */ + if (WARN_ON_ONCE(proc_ops->proc_compat_ioctl)) + return -EINVAL; +#endif + /* the table says what procfs copies, see pde_ioctl_arg() */ + if (WARN_ON_ONCE(!proc_ops->proc_ioctl_args || + !proc_ops->proc_nr_ioctl_args)) + return -EINVAL; + pde->flags |= PROC_ENTRY_proc_ioctl_buf; + } else if (WARN_ON_ONCE(proc_ops->proc_ioctl_args || + proc_ops->proc_nr_ioctl_args)) { + return -EINVAL; + } return 0; } diff --git a/fs/proc/inode.c b/fs/proc/inode.c index caa47933c233..717cbaad65df 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -548,11 +549,66 @@ static long pde_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned in return -ENOTTY; } +/* the table lives in proc_ops, which goes away with the entry */ +static bool pde_ioctl_arg(struct proc_dir_entry *pde, unsigned int cmd, + struct proc_ioctl_arg *arg) +{ + const struct proc_ops *proc_ops = pde->proc_ops; + bool found = false; + + if (!pde_pin(pde)) + return false; + for (unsigned int i = 0; i < proc_ops->proc_nr_ioctl_args; i++) { + if (proc_ops->proc_ioctl_args[i].cmd == cmd) { + *arg = proc_ops->proc_ioctl_args[i]; + found = true; + break; + } + } + pde_unpin(pde); + return found; +} + +/* + * procfs copies the argument in before it pins the entry and copies it back + * out after unpinning it. An ioctl that faults on its argument then doesn't + * hold up remove_proc_entry(). + */ +static long pde_ioctl_buf(struct proc_dir_entry *pde, struct file *file, + unsigned int cmd, void __user *uarg, unsigned long arg) +{ + void *buf __free(kvfree) = NULL; + struct proc_ioctl_arg a; + size_t size; + long rv; + + if (!pde_ioctl_arg(pde, cmd, &a)) + return -ENOTTY; + size = max(a.in, a.out); + if (size) { + buf = kvzalloc(size, GFP_KERNEL); + if (!buf) + return -ENOMEM; + if (a.in && copy_from_user(buf, uarg, a.in)) + return -EFAULT; + } + + if (!pde_pin(pde)) + return -ENOTTY; + rv = pde->proc_ops->proc_ioctl_buf(file, cmd, buf ?: (void *)arg); + pde_unpin(pde); + if (rv >= 0 && a.out && copy_to_user(uarg, buf, a.out)) + return -EFAULT; + return rv; +} + static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct proc_dir_entry *pde = PDE(file_inode(file)); long rv; + if (pde_has_proc_ioctl_buf(pde)) + return pde_ioctl_buf(pde, file, cmd, (void __user *)arg, arg); if (!pde_pin(pde)) return -ENOTTY; rv = pde_ioctl(pde, file, cmd, arg); @@ -574,6 +630,8 @@ static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned struct proc_dir_entry *pde = PDE(file_inode(file)); long rv; + if (pde_has_proc_ioctl_buf(pde)) + return pde_ioctl_buf(pde, file, cmd, compat_ptr(arg), arg); if (!pde_pin(pde)) return -ENOTTY; rv = pde_compat_ioctl(pde, file, cmd, arg); diff --git a/fs/proc/internal.h b/fs/proc/internal.h index b5c78f4f392c..ae0f9779cf1b 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -131,6 +131,11 @@ static inline bool pde_has_full_reads(const struct proc_dir_entry *pde) return pde->flags & PROC_ENTRY_FULL_READS; } +static inline bool pde_has_proc_ioctl_buf(const struct proc_dir_entry *pde) +{ + return pde->flags & PROC_ENTRY_proc_ioctl_buf; +} + /* procfs owns the seq_file, see pde_seq_start() */ static inline bool pde_is_seq_file(const struct proc_dir_entry *pde) { diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 0f0997898ba3..2ba4e4a39e13 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -8,6 +8,7 @@ #include #include #include +#include struct proc_dir_entry; struct seq_file; @@ -45,8 +46,29 @@ enum { * coming up short. */ PROC_ENTRY_FULL_READS = 1U << 11, + PROC_ENTRY_proc_ioctl_buf = 1U << 12, }; +/* + * How many bytes of the argument of @cmd procfs copies in before calling + * ->proc_ioctl_buf() and copies back out after it. A command that copies + * neither gets its argument as is. + */ +struct proc_ioctl_arg { + unsigned int cmd; + unsigned int in; + unsigned int out; +}; + +#define PROC_IOCTL_ARG(_cmd, _in, _out) \ + { .cmd = (_cmd), .in = (_in), .out = (_out) } + +/* A command that encodes the size and direction of its argument. */ +#define PROC_IOCTL_IOC(_cmd) \ + PROC_IOCTL_ARG(_cmd, \ + (_IOC_DIR(_cmd) & _IOC_WRITE) ? _IOC_SIZE(_cmd) : 0, \ + (_IOC_DIR(_cmd) & _IOC_READ) ? _IOC_SIZE(_cmd) : 0) + struct proc_ops { unsigned int proc_flags; int (*proc_open)(struct inode *, struct file *); @@ -100,6 +122,19 @@ struct proc_ops { */ ssize_t (*proc_read_buf)(struct file *, char *, size_t, loff_t *); unsigned int proc_read_size; + /* + * procfs looks the command up in ->proc_ioctl_args, copies in what it + * passes before pinning the entry, calls ->proc_ioctl_buf() with that + * copy and copies out what it returns after unpinning the entry. + * Commands that aren't in the table fail with -ENOTTY. 32-bit tasks + * go through the same table. The copy is zeroed and as large as the + * larger of the two sizes, it only goes back out if the ioctl + * succeeded, and a command that copies neither gets its argument as + * is. + */ + long (*proc_ioctl_buf)(struct file *, unsigned int, void *); + const struct proc_ioctl_arg *proc_ioctl_args; + unsigned int proc_nr_ioctl_args; } __randomize_layout; /* definitions for hide_pid field */ -- 2.53.0 proc_reg_mmap() pins the entry around ->proc_mmap() and that's it. What the entry hands out lives on without the pin: the vm_operations it put on the mapping run on every fault, on every write to a read-only page and on every ptrace access with the entry unpinned, and the mapping itself survives remove_proc_entry(). For /proc/vmcore that means the fault handler runs after vmcore_cleanup() freed what it looks at, and a mapping of the old memory keeps pointing at it after fadump gave that memory back to the page allocator. /proc/bus/pci has the same problem when a device goes away. So do what kernfs does for the mappings of sysfs files. procfs puts vm_operations of its own on the mapping. They pin the entry around its ->open(), ->fault(), ->page_mkwrite() and ->access() and fail with SIGBUS, or do nothing, once the entry is gone. The entry's vm_operations are kept in its open node, so every mapping of an entry has to use the same ones, like kernfs asks that of every mapping of an open file. ->close() can't be wrapped, it runs when the mapping goes away and that can be after the entry is gone, so a mapping that has one is refused. When the entry is removed procfs unmaps the mappings of every open file of an entry that has been mapped before it calls ->proc_release(), so a fault into one of them finds the entry gone. That unmaps the entry's own address space, the way kernfs_drain_open_files() does it. /proc/bus/pci maps through the iomem address space that /dev/mem and the sysfs resource files share, see iomem_get_mapping(), and unmapping that would take every /dev/mem and BAR mapping on the system with it. So those mappings are left alone, like the sysfs resource files leave theirs, and keep pointing at the BAR of a device that is gone. To know the files an entry that can be mapped gets an open node like an entry with a ->proc_release() has one. The mapping is linked into the file's address space only after ->mmap() returned, so a removal that runs in between doesn't find it. kernfs has the same window. Entries that are never removed aren't wrapped, procfs never pins them. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 6 +-- fs/proc/inode.c | 126 ++++++++++++++++++++++++++++++++++++++++++++---- fs/proc/internal.h | 9 ++-- include/linux/proc_fs.h | 15 ++++++ 4 files changed, 140 insertions(+), 16 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 1bad3b068d53..81a6cb560c75 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -505,14 +505,14 @@ static int pde_set_flags(struct proc_dir_entry *pde) /* * procfs has to know the open files of an entry whose ->proc_release() it - * calls at removal. A permanent entry is never removed and skips all that, - * see proc_reg_open(). + * calls at removal and whose mappings it undoes then. A permanent entry is + * never removed and skips all that, see proc_reg_open(). */ static bool pde_needs_open_node(const struct proc_dir_entry *pde) { if (!S_ISREG(pde->mode) || pde_is_permanent(pde)) return false; - return pde->proc_ops->proc_release; + return pde->proc_ops->proc_release || pde->proc_ops->proc_mmap; } /* diff --git a/fs/proc/inode.c b/fs/proc/inode.c index 717cbaad65df..5e423bcad278 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -312,7 +312,8 @@ static void pde_seq_release(struct proc_dir_entry *pde, struct inode *inode, * * PDE is locked on entry, unlocked on exit. */ -static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo) +static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo, + bool rundown) __releases(&pde->pde_unload_lock) { /* @@ -336,7 +337,17 @@ static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo) spin_unlock(&pde->pde_unload_lock); file = pdeo->file; - pde->proc_ops->proc_release(file_inode(file), file); + /* + * A mapping of a removed entry faults, see proc_vm_ops. Like + * kernfs this unmaps the entry's own address space. A file + * that maps through another one, /proc/bus/pci through the + * iomem address space of /dev/mem, keeps its mappings like a + * sysfs resource file does. + */ + if (rundown && pde->open->vm_ops) + unmap_mapping_range(file_inode(file)->i_mapping, 0, 0, 1); + if (pde->proc_ops->proc_release) + pde->proc_ops->proc_release(file_inode(file), file); spin_lock(&pde->pde_unload_lock); /* Strictly after ->proc_release, see above. */ @@ -362,7 +373,7 @@ void proc_entry_rundown(struct proc_dir_entry *de) while (!list_empty(&de->open->files)) { struct pde_opener *pdeo; pdeo = list_first_entry(&de->open->files, struct pde_opener, lh); - close_pdeo(de, pdeo); + close_pdeo(de, pdeo, true); spin_lock(&de->pde_unload_lock); } spin_unlock(&de->pde_unload_lock); @@ -642,12 +653,106 @@ static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned #define proc_reg_compat_ioctl NULL #endif -static int pde_mmap(struct proc_dir_entry *pde, struct file *file, struct vm_area_struct *vma) +/* + * The vm_operations of an entry's mappings run with the entry pinned, like + * the entry's file methods do, and a mapping of a removed entry faults. + * proc_entry_rundown() unmaps the mappings of every open file and a fault + * after that finds the entry gone. Like kernfs_vm_ops, and like there only + * these four methods of the entry's vm_operations are called. + */ +static void proc_vma_open(struct vm_area_struct *vma) +{ + struct proc_dir_entry *pde = PDE(file_inode(vma->vm_file)); + const struct vm_operations_struct *vm_ops = pde->open->vm_ops; + + if (!pde_pin(pde)) + return; + if (vm_ops->open) + vm_ops->open(vma); + pde_unpin(pde); +} + +static vm_fault_t proc_vma_fault(struct vm_fault *vmf) +{ + struct proc_dir_entry *pde = PDE(file_inode(vmf->vma->vm_file)); + const struct vm_operations_struct *vm_ops = pde->open->vm_ops; + vm_fault_t ret = VM_FAULT_SIGBUS; + + if (!pde_pin(pde)) + return VM_FAULT_SIGBUS; + if (vm_ops->fault) + ret = vm_ops->fault(vmf); + pde_unpin(pde); + return ret; +} + +static vm_fault_t proc_vma_page_mkwrite(struct vm_fault *vmf) +{ + struct proc_dir_entry *pde = PDE(file_inode(vmf->vma->vm_file)); + const struct vm_operations_struct *vm_ops = pde->open->vm_ops; + vm_fault_t ret = 0; + + if (!pde_pin(pde)) + return VM_FAULT_SIGBUS; + if (vm_ops->page_mkwrite) + ret = vm_ops->page_mkwrite(vmf); + else + file_update_time(vmf->vma->vm_file); + pde_unpin(pde); + return ret; +} + +static int proc_vma_access(struct vm_area_struct *vma, unsigned long addr, + void *buf, int len, int write) +{ + struct proc_dir_entry *pde = PDE(file_inode(vma->vm_file)); + const struct vm_operations_struct *vm_ops = pde->open->vm_ops; + int ret = -EINVAL; + + if (!pde_pin(pde)) + return -EINVAL; + if (vm_ops->access) + ret = vm_ops->access(vma, addr, buf, len, write); + pde_unpin(pde); + return ret; +} + +static const struct vm_operations_struct proc_vm_ops = { + .open = proc_vma_open, + .fault = proc_vma_fault, + .page_mkwrite = proc_vma_page_mkwrite, + .access = proc_vma_access, +}; + +static int pde_mmap(struct proc_dir_entry *pde, struct file *file, + struct vm_area_struct *vma) { const auto mmap = pde->proc_ops->proc_mmap; - if (mmap) - return mmap(file, vma); - return -EIO; + struct pde_open_node *on = pde->open; + int rv; + + if (!mmap) + return -EIO; + rv = mmap(file, vma); + if (rv || pde_is_permanent(pde)) + return rv; + /* the entry mapped something else, that isn't ours to wrap */ + if (vma->vm_file != file) + return 0; + /* + * ->close() can't be wrapped, it runs when the mapping goes away and + * that can be after the entry is gone, see close_pdeo(). + */ + if (!vma->vm_ops || vma->vm_ops->close) + return -EINVAL; + + /* every mapping of an entry uses the same vm_operations, see above */ + guard(spinlock)(&pde->pde_unload_lock); + if (on->vm_ops && on->vm_ops != vma->vm_ops) + return -EINVAL; + on->vm_ops = vma->vm_ops; + vma->vm_ops = &proc_vm_ops; + return 0; } static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma) @@ -743,8 +848,9 @@ static int proc_reg_open(struct inode *inode, struct file *file) * 2) rmmod isn't blocked by opening file in /proc and sitting on * the descriptor (including "rmmod foo release hook. Those entries - * have an open node, see proc_register(). + * Save every "struct file" with custom ->release hook, and every + * one that can be mapped, proc_entry_rundown() unmaps those. Those + * entries have an open node, see proc_register(). */ if (!use_pde(pde)) return -ENOENT; @@ -794,7 +900,7 @@ static int proc_reg_release(struct inode *inode, struct file *file) spin_lock(&pde->pde_unload_lock); list_for_each_entry(pdeo, &pde->open->files, lh) { if (pdeo->file == file) { - close_pdeo(pde, pdeo); + close_pdeo(pde, pdeo, false); goto out; } } diff --git a/fs/proc/internal.h b/fs/proc/internal.h index ae0f9779cf1b..0b1c0585d6a6 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -373,12 +373,15 @@ struct pde_opener { } __randomize_layout; /* - * The open files of an entry whose ->proc_release() procfs has to call when - * the entry is removed while they are still open. Allocated with the entry, - * see proc_register(). + * The open files of an entry whose ->proc_release() procfs has to call and + * whose mappings it has to undo when the entry is removed while they are + * still open, that is of an entry with a ->proc_release() or a + * ->proc_mmap(). Allocated with the entry, see proc_register(). */ struct pde_open_node { struct list_head files; /* who did ->open, but not ->release */ + /* of its mappings, the same for all of them, see pde_mmap() */ + const struct vm_operations_struct *vm_ops; }; extern const struct inode_operations proc_link_inode_operations; extern const struct inode_operations proc_pid_link_inode_operations; diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 2ba4e4a39e13..5bf039d20990 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -86,6 +86,21 @@ struct proc_ops { #ifdef CONFIG_COMPAT long (*proc_compat_ioctl)(struct file *, unsigned int, unsigned long); #endif + /* + * procfs pins the entry around ->proc_mmap() and wraps the + * vm_operations of the mapping so that ->open(), ->fault(), + * ->page_mkwrite() and ->access() run with the entry pinned as well. + * Nothing else of them is called, like for a kernfs file. Every + * mapping of an entry has to use the same vm_operations. A ->close() + * can't be wrapped, it runs when the mapping goes away and that can + * be after the entry is gone, so a mapping that has one is refused. + * Removing the entry unmaps the mappings in its own address space, + * like kernfs does, and a fault into one of them is SIGBUS from then + * on. A file that maps through another address space, /proc/bus/pci + * through the one of /dev/mem, keeps its mappings like a sysfs + * resource file does. The mappings of a permanent entry are left + * alone. + */ int (*proc_mmap)(struct file *, struct vm_area_struct *); unsigned long (*proc_get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); /* -- 2.53.0 Map a memory BAR of a PCI device through /proc/bus/pci and remove the device through sysfs: - the removal doesn't wait for the mapped file - the entry is gone - and reading the file fails with -EIO The mapping itself survives. /proc/bus/pci maps through the iomem address space of /dev/mem and procfs leaves that alone, like the sysfs resource files leave theirs, so the test doesn't touch the mapping after the removal. What the BAR of a removed device reads like is between the reader and the hardware. Nothing in the test can know which device is safe to remove, so it takes the device from PROC_PCI_DEV, skips without it and rescans the bus afterwards. It also skips where PCI files can't be mapped or that isn't allowed. Signed-off-by: Christian Brauner (Amutable) --- tools/testing/selftests/proc/.gitignore | 1 + tools/testing/selftests/proc/Makefile | 1 + tools/testing/selftests/proc/proc-remove-mapped.c | 160 ++++++++++++++++++++++ 3 files changed, 162 insertions(+) diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selftests/proc/.gitignore index e2566cc0ae07..cafd8c263c9a 100644 --- a/tools/testing/selftests/proc/.gitignore +++ b/tools/testing/selftests/proc/.gitignore @@ -23,6 +23,7 @@ /proc-pidns /proc-remove-slow-sysctl /proc-remove-slow-reader +/proc-remove-mapped /proc-remove-slow-readlink /read /self diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile index 6a9bdc999f54..c38297afc244 100644 --- a/tools/testing/selftests/proc/Makefile +++ b/tools/testing/selftests/proc/Makefile @@ -34,6 +34,7 @@ TEST_GEN_PROGS += proc-sysctl-readdir-churn TEST_GEN_PROGS += proc-pidns TEST_GEN_PROGS += proc-remove-slow-sysctl TEST_GEN_PROGS += proc-remove-slow-reader +TEST_GEN_PROGS += proc-remove-mapped TEST_GEN_PROGS += proc-remove-slow-readlink include ../lib.mk diff --git a/tools/testing/selftests/proc/proc-remove-mapped.c b/tools/testing/selftests/proc/proc-remove-mapped.c new file mode 100644 index 000000000000..92860fb2a8f4 --- /dev/null +++ b/tools/testing/selftests/proc/proc-remove-mapped.c @@ -0,0 +1,160 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Removing a /proc entry with a mapped file open must not wait for that file + * and must leave the file dead. Map a memory BAR of a PCI device through + * /proc/bus/pci and remove the device, which removes its entry. Nothing here + * can know which device is safe to remove and rescan, so it is named in + * PROC_PCI_DEV, e.g. PROC_PCI_DEV=0000:00:03.0. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#define DEV_ENV "PROC_PCI_DEV" +#define SYSFS_DEVICES "/sys/bus/pci/devices/" +#define SYSFS_RESCAN "/sys/bus/pci/rescan" +#define PROC_PCI "/proc/bus/pci/" +#define IORESOURCE_MEM 0x00000200 + +static sigjmp_buf fault; + +static void on_sigbus(int sig) +{ + siglongjmp(fault, 1); +} + +/* the first memory BAR of @dev */ +static int mem_bar(const char *dev, unsigned long long *start, + unsigned long long *len) +{ + unsigned long long s, e, flags; + char path[PATH_MAX]; + int ret = -ENODEV; + FILE *f; + + snprintf(path, sizeof(path), SYSFS_DEVICES "%s/resource", dev); + f = fopen(path, "r"); + if (!f) + return -errno; + while (fscanf(f, "%llx %llx %llx", &s, &e, &flags) == 3) { + if ((flags & IORESOURCE_MEM) && e >= s) { + *start = s; + *len = e - s + 1; + ret = 0; + break; + } + } + fclose(f); + return ret; +} + +static int write_one(const char *path) +{ + int fd, ret; + + fd = open(path, O_WRONLY | O_CLOEXEC); + if (fd < 0) + return -errno; + ret = write(fd, "1", 1) < 0 ? -errno : 0; + close(fd); + return ret; +} + +FIXTURE(pci) { + char dev[16]; + char path[64]; + bool removed; +}; + +FIXTURE_SETUP(pci) +{ + const char *dev = getenv(DEV_ENV); + + self->removed = false; + if (!dev) + SKIP(return, DEV_ENV " isn't set"); + /* 0000:BB:DD.F is /proc/bus/pci/BB/DD.F */ + if (strlen(dev) != 12 || strncmp(dev, "0000:", 5)) + SKIP(return, "%s: not a device in PCI domain 0", dev); + strcpy(self->dev, dev); + snprintf(self->path, sizeof(self->path), PROC_PCI "%.2s/%s", + dev + 5, dev + 8); + if (access(self->path, R_OK)) + SKIP(return, "%s: %s", self->path, strerror(errno)); +} + +FIXTURE_TEARDOWN(pci) +{ + if (self->removed && write_one(SYSFS_RESCAN)) + TH_LOG("couldn't rescan the PCI bus for %s", self->dev); +} + +TEST_F(pci, remove_while_mapped) +{ + long psz = sysconf(_SC_PAGESIZE); + unsigned long long start = 0, len = 0; + char path[PATH_MAX]; + volatile unsigned int *p; + unsigned int v; + int fd; + + if (mem_bar(self->dev, &start, &len)) + SKIP(return, "%s has no memory BAR", self->dev); + + fd = open(self->path, O_RDONLY | O_CLOEXEC); + ASSERT_GE(fd, 0); + if (ioctl(fd, PCIIOC_MMAP_IS_MEM)) { + /* no PCI mmap on this architecture, or locked down */ + if (errno == EINVAL || errno == EPERM) { + close(fd); + SKIP(return, "PCIIOC_MMAP_IS_MEM: %s", strerror(errno)); + } + ASSERT_TRUE(false) { + TH_LOG("PCIIOC_MMAP_IS_MEM: %s", strerror(errno)); + } + } + p = mmap(NULL, psz, PROT_READ, MAP_SHARED, fd, start); + if (p == MAP_FAILED) + SKIP(return, "mmap: %s", strerror(errno)); + + /* The BAR can be read while the device is there. */ + ASSERT_NE(signal(SIGBUS, on_sigbus), SIG_ERR); + if (sigsetjmp(fault, 1) == 0) { + v = *p; + } else { + ASSERT_TRUE(false) { + TH_LOG("reading the mapping faulted before the removal"); + } + } + + /* Removing the device removes its entry. */ + snprintf(path, sizeof(path), SYSFS_DEVICES "%s/remove", self->dev); + ASSERT_EQ(write_one(path), 0); + self->removed = true; + EXPECT_EQ(access(self->path, F_OK), -1); + + /* + * The file is dead. The mapping isn't procfs's to unmap, it lives in + * the iomem address space of /dev/mem, see proc_bus_pci_open(), and + * is left alone like a sysfs resource file's. + */ + EXPECT_EQ(pread(fd, &v, sizeof(v), 0), -1); + EXPECT_EQ(errno, EIO); + + munmap((void *)p, psz); + close(fd); +} + +TEST_HARNESS_MAIN -- 2.53.0 remove_proc_entry() waits until every call into the entry has returned, and its callers like to hold locks while they wait. A method of the entry that takes one of those locks deadlocks with them. xt_recent removed its entries with a spinlock held that its seq_file took, and ALSA has to remove an entry before it takes info_mutex because its open takes it. Both of those were found by reading the code. kernfs lets lockdep find them. kernfs_get_active() is a read lock to lockdep and kernfs_drain() takes the write side around its wait. So a method that takes a lock L while active records class -> L, a removal under L records L -> class, and lockdep reports the cycle. Do the same for the pin. use_pde() records a read lock on the entry's lockdep map, unuse_pde() releases it and proc_entry_rundown() takes the write side around waiting for the last user. The key of the class is what the entries of a kind already share, their seq_operations or their proc_ops, static objects that lockdep takes as a key. The entries that only share a show function, those of proc_create_single() and friends, get a key per registration site instead, like every sysfs attribute has one: a show function lives in text and lockdep doesn't take that as a key. So all /proc/net/dev entries are one class, which is right. The same code runs pinned in every network namespace and a removal under a lock that its reader takes is a deadlock in any of them. Nothing in the tree removes an entry of its own kind from one of its methods, so there's no need for an escape hatch like kernfs_break_active_protection() yet. The map only exists on lockdep kernels. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 46 ++++++++++++++++++++++++++++++++++++---------- fs/proc/inode.c | 29 ++++++++++++++++++++++++++--- fs/proc/internal.h | 17 ++++++++++++++++- fs/proc/proc_net.c | 28 +++++++++++++++------------- include/linux/proc_fs.h | 42 +++++++++++++++++++++++++++++++++--------- 5 files changed, 126 insertions(+), 36 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 81a6cb560c75..ca4025ee7aa8 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -541,9 +541,33 @@ static int pde_init_seq_direct(struct proc_dir_entry *pde) return 0; } +static void pde_lockdep_init(struct proc_dir_entry *pde, + struct lock_class_key *key) +{ +#ifdef CONFIG_DEBUG_LOCK_ALLOC + const void *k = key; + + if (!pde_lockdep(pde)) + return; + /* + * To lockdep the pin is a read lock that proc_entry_rundown() takes + * the write side of, see use_pde(). The entries that share + * seq_operations or proc_ops run the same code pinned, so they share + * a class, and those are static objects that lockdep takes as its + * key. The entries that only share a show function get a key per + * registration site instead, see proc_create_single_data(). + */ + if (!k) + k = pde_has_proc_seq_ops(pde) ? (const void *)pde->seq_ops : + pde->proc_ops; + lockdep_init_map(&pde->dep_map, "pde->in_use", + (struct lock_class_key *)k, 0); +#endif +} + /* returns the registered entry, or frees dp and returns NULL on failure */ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, - struct proc_dir_entry *dp) + struct proc_dir_entry *dp, struct lock_class_key *key) { if (proc_alloc_inum(&dp->low_ino)) goto out_free_entry; @@ -558,6 +582,7 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, } if (pde_init_seq_direct(dp)) goto out_free_inum; + pde_lockdep_init(dp, key); write_lock(&proc_subdir_lock); dp->parent = dir; @@ -660,7 +685,7 @@ struct proc_dir_entry *proc_symlink(const char *name, ent->data = kmemdup(dest, ent->size + 1, GFP_KERNEL); if (ent->data) { ent->proc_iops = &proc_link_inode_operations; - ent = proc_register(parent, ent); + ent = proc_register(parent, ent, NULL); } else { pde_free(ent); ent = NULL; @@ -686,7 +711,7 @@ struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode, if (force_lookup) { pde_force_lookup(ent); } - ent = proc_register(parent, ent); + ent = proc_register(parent, ent, NULL); } return ent; } @@ -723,7 +748,7 @@ struct proc_dir_entry *proc_create_mount_point(const char *name) ent->data = NULL; ent->proc_dir_ops = NULL; ent->proc_iops = NULL; - ent = proc_register(parent, ent); + ent = proc_register(parent, ent, NULL); } return ent; } @@ -759,7 +784,7 @@ struct proc_dir_entry *proc_create_data(const char *name, umode_t mode, if (!p) return NULL; p->proc_ops = proc_ops; - return proc_register(parent, p); + return proc_register(parent, p, NULL); } EXPORT_SYMBOL(proc_create_data); @@ -789,13 +814,14 @@ struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode, p->seq_ops = ops; p->state_size = state_size; p->flags |= PROC_ENTRY_proc_seq_ops; - return proc_register(parent, p); + return proc_register(parent, p, NULL); } EXPORT_SYMBOL(proc_create_seq_private); -struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode, +struct proc_dir_entry *__proc_create_single_data(const char *name, umode_t mode, struct proc_dir_entry *parent, - int (*show)(struct seq_file *, void *), void *data) + int (*show)(struct seq_file *, void *), void *data, + struct lock_class_key *key) { struct proc_dir_entry *p; @@ -805,9 +831,9 @@ struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode, p->proc_ops = &proc_seq_ops; p->single_show = show; p->flags |= PROC_ENTRY_proc_show; - return proc_register(parent, p); + return proc_register(parent, p, key); } -EXPORT_SYMBOL(proc_create_single_data); +EXPORT_SYMBOL(__proc_create_single_data); void proc_set_size(struct proc_dir_entry *de, loff_t size) { diff --git a/fs/proc/inode.c b/fs/proc/inode.c index 5e423bcad278..59de4681d845 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -199,13 +199,24 @@ const struct super_operations proc_sops = { enum {BIAS = -1U<<31}; +/* + * To lockdep the pin is a read lock and proc_entry_rundown() takes the write + * side, see pde_lockdep_init(). So a method that takes a lock while pinned + * and a removal under that lock are the deadlock lockdep knows how to report. + */ static inline int use_pde(struct proc_dir_entry *pde) { - return likely(atomic_inc_unless_negative(&pde->in_use)); + if (unlikely(!atomic_inc_unless_negative(&pde->in_use))) + return 0; + if (pde_lockdep(pde)) + rwsem_acquire_read(&pde->dep_map, 0, 1, _RET_IP_); + return 1; } static void unuse_pde(struct proc_dir_entry *pde) { + if (pde_lockdep(pde)) + rwsem_release(&pde->dep_map, _RET_IP_); if (unlikely(atomic_dec_return(&pde->in_use) == BIAS)) wake_up_var(&pde->in_use); } @@ -362,9 +373,21 @@ static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo, void proc_entry_rundown(struct proc_dir_entry *de) { - /* Wait until all existing callers into module are done. */ - if (atomic_add_return(BIAS, &de->in_use) != BIAS) + /* + * Wait until all existing callers into module are done. That's the + * write side of the pin to lockdep, see use_pde(). + */ + if (pde_lockdep(de)) + rwsem_acquire(&de->dep_map, 0, 0, _RET_IP_); + if (atomic_add_return(BIAS, &de->in_use) != BIAS) { + if (pde_lockdep(de)) + lock_contended(&de->dep_map, _RET_IP_); wait_var_event(&de->in_use, atomic_read(&de->in_use) == BIAS); + } + if (pde_lockdep(de)) { + lock_acquired(&de->dep_map, _RET_IP_); + rwsem_release(&de->dep_map, _RET_IP_); + } /* ->open->files can't grow from now on. */ if (!de->open) diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 0b1c0585d6a6..7a197e2920f8 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -67,6 +68,9 @@ struct proc_dir_entry { umode_t mode; u16 flags; u8 namelen; +#ifdef CONFIG_DEBUG_LOCK_ALLOC + struct lockdep_map dep_map; /* the pin as a lock, see use_pde() */ +#endif char inline_name[]; } __randomize_layout; @@ -142,6 +146,16 @@ static inline bool pde_is_seq_file(const struct proc_dir_entry *pde) return pde_has_proc_seq_ops(pde) || pde_has_proc_show(pde); } +/* Only regular files are pinned, and only their map is initialized. */ +static inline bool pde_lockdep(const struct proc_dir_entry *pde) +{ +#ifdef CONFIG_DEBUG_LOCK_ALLOC + return S_ISREG(pde->mode); +#else + return false; +#endif +} + extern struct kmem_cache *proc_dir_entry_cache; void pde_free(struct proc_dir_entry *pde); @@ -318,7 +332,8 @@ bool proc_fill_cache(struct file *, struct dir_context *, const char *, unsigned struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode, struct proc_dir_entry **parent, void *data); struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, - struct proc_dir_entry *dp); + struct proc_dir_entry *dp, + struct lock_class_key *key); extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int); struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *); extern int proc_readdir(struct file *, struct dir_context *); diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c index b78d627b024f..9ddd7c323dfc 100644 --- a/fs/proc/proc_net.c +++ b/fs/proc/proc_net.c @@ -115,7 +115,7 @@ struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode, p->seq_ops = ops; p->state_size = state_size; p->flags |= PROC_ENTRY_proc_seq_ops; - return proc_register(parent, p); + return proc_register(parent, p, NULL); } EXPORT_SYMBOL_GPL(proc_create_net_data); @@ -162,7 +162,7 @@ struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode p->state_size = state_size; p->flags |= PROC_ENTRY_proc_seq_ops; p->write = write; - return proc_register(parent, p); + return proc_register(parent, p, NULL); } EXPORT_SYMBOL_GPL(proc_create_net_data_write); @@ -190,9 +190,10 @@ static const struct proc_ops proc_net_single_ops = { .proc_release = single_release_net, }; -struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode, +struct proc_dir_entry *__proc_create_net_single(const char *name, umode_t mode, struct proc_dir_entry *parent, - int (*show)(struct seq_file *, void *), void *data) + int (*show)(struct seq_file *, void *), void *data, + struct lock_class_key *key) { struct proc_dir_entry *p; @@ -203,9 +204,9 @@ struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode, p->proc_ops = &proc_net_single_ops; p->single_show = show; p->flags |= PROC_ENTRY_proc_show; - return proc_register(parent, p); + return proc_register(parent, p, key); } -EXPORT_SYMBOL_GPL(proc_create_net_single); +EXPORT_SYMBOL_GPL(__proc_create_net_single); /** * proc_create_net_single_write - Create a writable net_ns-specific proc file @@ -232,11 +233,12 @@ EXPORT_SYMBOL_GPL(proc_create_net_single); * pde_data() on the file inode. The network namespace must be accessed by * calling seq_file_single_net() on the seq_file struct. */ -struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode, - struct proc_dir_entry *parent, - int (*show)(struct seq_file *, void *), - proc_write_t write, - void *data) +struct proc_dir_entry *__proc_create_net_single_write(const char *name, umode_t mode, + struct proc_dir_entry *parent, + int (*show)(struct seq_file *, void *), + proc_write_t write, + void *data, + struct lock_class_key *key) { struct proc_dir_entry *p; @@ -248,9 +250,9 @@ struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mo p->single_show = show; p->flags |= PROC_ENTRY_proc_show; p->write = write; - return proc_register(parent, p); + return proc_register(parent, p, key); } -EXPORT_SYMBOL_GPL(proc_create_net_single_write); +EXPORT_SYMBOL_GPL(__proc_create_net_single_write); static struct net *get_proc_task_net(struct inode *dir) { diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 5bf039d20990..feb3228cb5af 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -204,9 +204,20 @@ struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode, proc_create_seq_private(name, mode, parent, ops, 0, data) #define proc_create_seq(name, mode, parent, ops) \ proc_create_seq_private(name, mode, parent, ops, 0, NULL) -struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode, +struct proc_dir_entry *__proc_create_single_data(const char *name, umode_t mode, struct proc_dir_entry *parent, - int (*show)(struct seq_file *, void *), void *data); + int (*show)(struct seq_file *, void *), void *data, + struct lock_class_key *key); +/* + * The entries a registration site creates share nothing that lockdep takes as + * the key of their class but the show function, which isn't a static object, + * so each site gets a key of its own, like every sysfs attribute has one. + */ +#define proc_create_single_data(name, mode, parent, show, data) \ +({ \ + static struct lock_class_key __key; \ + __proc_create_single_data(name, mode, parent, show, data, &__key); \ +}) #define proc_create_single(name, mode, parent, show) \ proc_create_single_data(name, mode, parent, show, NULL) @@ -238,19 +249,32 @@ struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode, unsigned int state_size, void *data); #define proc_create_net(name, mode, parent, ops, state_size) \ proc_create_net_data(name, mode, parent, ops, state_size, NULL) -struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode, +struct proc_dir_entry *__proc_create_net_single(const char *name, umode_t mode, struct proc_dir_entry *parent, - int (*show)(struct seq_file *, void *), void *data); + int (*show)(struct seq_file *, void *), void *data, + struct lock_class_key *key); +#define proc_create_net_single(name, mode, parent, show, data) \ +({ \ + static struct lock_class_key __key; \ + __proc_create_net_single(name, mode, parent, show, data, &__key); \ +}) struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct seq_operations *ops, proc_write_t write, unsigned int state_size, void *data); -struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode, - struct proc_dir_entry *parent, - int (*show)(struct seq_file *, void *), - proc_write_t write, - void *data); +struct proc_dir_entry *__proc_create_net_single_write(const char *name, umode_t mode, + struct proc_dir_entry *parent, + int (*show)(struct seq_file *, void *), + proc_write_t write, + void *data, + struct lock_class_key *key); +#define proc_create_net_single_write(name, mode, parent, show, write, data) \ +({ \ + static struct lock_class_key __key; \ + __proc_create_net_single_write(name, mode, parent, show, write, data, \ + &__key); \ +}) extern struct pid *tgid_pidfd_to_pid(const struct file *file); struct bpf_iter_aux_info; -- 2.53.0 An entry with a ->proc_poll() calls poll_wait() on a wait queue of its own and procfs pins the entry around that. The pin covers the call, not what it leaves behind: the poller stays queued on the entry's queue after the call, and so after remove_proc_entry() ran ->proc_release() on the still open file and the entry's owner freed the queue with the rest. sunrpc had the queue of its channel files in the per-namespace cache_detail, which is freed when the namespace goes, so a poll() or epoll on such a file across the end of the namespace was a use-after-free, fixed for stable by going back to one static queue. md works around the same problem at module unload by waking its pollers in a loop until none is left. So do what kernfs does with kernfs_open_node: procfs owns the wait queue. Every entry with a ->proc_poll() gets an open node, which lives as long as the entry does, and the entry's inode holds the entry, so the queue outlives every open file and every poll or epoll entry on it. proc_reg_poll() does the poll_wait() on it before it calls the method, the owner wakes the pollers with proc_poll_notify(), and procfs wakes them when the entry is removed, after which a poll of it reports EPOLLERR | EPOLLPRI, like kernfs_fop_poll() does. The open node tracks the open files only for the entries whose release hook procfs has to call at removal, as before. Which methods an entry has procfs keeps in the entry's flags, and whether its files are tracked comes from there too: the last close of a file can come after the entry was removed and its proc_ops freed with their module. The method still gets the poll_table for now and the entries keep sleeping on their own queues until they are converted. The last series takes the poll_table away, so that an entry can't hand procfs a queue that dies before its files do. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 38 ++++++++++++++++++++-------------- fs/proc/inode.c | 55 ++++++++++++++++++++++++++++++++++++------------- fs/proc/internal.h | 35 ++++++++++++++++++++++++++----- include/linux/proc_fs.h | 11 ++++++++++ 4 files changed, 105 insertions(+), 34 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index ca4025ee7aa8..9c073cb867e5 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -466,6 +466,12 @@ static int pde_set_flags(struct proc_dir_entry *pde) if (proc_ops->proc_lseek) pde->flags |= PROC_ENTRY_proc_lseek; } + if (proc_ops->proc_poll) + pde->flags |= PROC_ENTRY_proc_poll; + if (proc_ops->proc_release) + pde->flags |= PROC_ENTRY_proc_release; + if (proc_ops->proc_mmap) + pde->flags |= PROC_ENTRY_proc_mmap; if (proc_ops->proc_write_buf) { if (WARN_ON_ONCE(proc_ops->proc_write)) return -EINVAL; @@ -503,16 +509,22 @@ static int pde_set_flags(struct proc_dir_entry *pde) return 0; } -/* - * procfs has to know the open files of an entry whose ->proc_release() it - * calls at removal and whose mappings it undoes then. A permanent entry is - * never removed and skips all that, see proc_reg_open(). - */ -static bool pde_needs_open_node(const struct proc_dir_entry *pde) +/* The open node, see struct pde_open_node. */ +static int pde_init_open_node(struct proc_dir_entry *pde) { - if (!S_ISREG(pde->mode) || pde_is_permanent(pde)) - return false; - return pde->proc_ops->proc_release || pde->proc_ops->proc_mmap; + struct pde_open_node *on; + + if (!S_ISREG(pde->mode)) + return 0; + if (!pde_has_proc_poll(pde) && !pde_tracks_files(pde)) + return 0; + on = kzalloc(sizeof(*on), GFP_KERNEL); + if (!on) + return -ENOMEM; + INIT_LIST_HEAD(&on->files); + init_waitqueue_head(&on->poll); + pde->open = on; + return 0; } /* @@ -574,12 +586,8 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, if (!S_ISDIR(dp->mode) && pde_set_flags(dp)) goto out_free_inum; - if (pde_needs_open_node(dp)) { - dp->open = kzalloc(sizeof(*dp->open), GFP_KERNEL); - if (!dp->open) - goto out_free_inum; - INIT_LIST_HEAD(&dp->open->files); - } + if (pde_init_open_node(dp)) + goto out_free_inum; if (pde_init_seq_direct(dp)) goto out_free_inum; pde_lockdep_init(dp, key); diff --git a/fs/proc/inode.c b/fs/proc/inode.c index 59de4681d845..ab30a58879d5 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -389,9 +389,15 @@ void proc_entry_rundown(struct proc_dir_entry *de) rwsem_release(&de->dep_map, _RET_IP_); } - /* ->open->files can't grow from now on. */ if (!de->open) return; + /* a poll of a removed entry reports EPOLLERR, see proc_reg_poll() */ + if (pde_has_proc_poll(de)) + wake_up_interruptible_all(&de->open->poll); + + /* ->open->files can't grow from now on. */ + if (!pde_tracks_files(de)) + return; spin_lock(&de->pde_unload_lock); while (!list_empty(&de->open->files)) { struct pde_opener *pdeo; @@ -555,26 +561,46 @@ static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t return rv; } -static __poll_t pde_poll(struct proc_dir_entry *pde, struct file *file, struct poll_table_struct *pts) -{ - const auto poll = pde->proc_ops->proc_poll; - if (poll) - return poll(file, pts); - return DEFAULT_POLLMASK; -} - +/* + * The pollers of an entry sleep on a wait queue procfs owns, which is there + * for as long as the entry's inode is, however the entry's owner goes away. + * The owner wakes them through proc_poll_notify(), and procfs does when the + * entry is removed: a poll of a removed entry reports EPOLLERR | EPOLLPRI, + * like kernfs_fop_poll(). + */ static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts) { struct proc_dir_entry *pde = PDE(file_inode(file)); __poll_t rv; - if (!pde_pin(pde)) + if (!pde_has_proc_poll(pde)) return DEFAULT_POLLMASK; - rv = pde_poll(pde, file, pts); + poll_wait(file, &pde->open->poll, pts); + if (!pde_pin(pde)) + return DEFAULT_POLLMASK | EPOLLERR | EPOLLPRI; + rv = pde->proc_ops->proc_poll(file, pts); pde_unpin(pde); return rv; } +/** + * proc_poll_notify - wake up the pollers of an entry + * @pde: an entry with a ->proc_poll(), may be NULL + * + * May be called from any context, as long as the entry is still registered. + * procfs doesn't pin it here, the caller keeps it alive like it does for + * every other use of the pointer proc_create() returned. + */ +void proc_poll_notify(struct proc_dir_entry *pde) +{ + if (!pde) + return; + if (WARN_ON_ONCE(!pde_has_proc_poll(pde))) + return; + wake_up_interruptible_all(&pde->open->poll); +} +EXPORT_SYMBOL_GPL(proc_poll_notify); + static long pde_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg) { const auto ioctl = pde->proc_ops->proc_ioctl; @@ -878,7 +904,8 @@ static int proc_reg_open(struct inode *inode, struct file *file) if (!use_pde(pde)) return -ENOENT; - if (pde->open) { + const bool track = pde_tracks_files(pde); + if (track) { pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL); if (!pdeo) { rv = -ENOMEM; @@ -888,7 +915,7 @@ static int proc_reg_open(struct inode *inode, struct file *file) rv = pde_open(pde, inode, file); - if (pde->open) { + if (track) { if (rv == 0) { /* To know what to release. */ pdeo->file = file; @@ -918,7 +945,7 @@ static int proc_reg_release(struct inode *inode, struct file *file) goto out; } - if (!pde->open) + if (!pde_tracks_files(pde)) goto out; spin_lock(&pde->pde_unload_lock); list_for_each_entry(pdeo, &pde->open->files, lh) { diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 7a197e2920f8..d2d731706788 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -15,6 +15,7 @@ #include #include #include +#include struct ctl_table_header; struct mempolicy; @@ -140,6 +141,26 @@ static inline bool pde_has_proc_ioctl_buf(const struct proc_dir_entry *pde) return pde->flags & PROC_ENTRY_proc_ioctl_buf; } +static inline bool pde_has_proc_poll(const struct proc_dir_entry *pde) +{ + return pde->flags & PROC_ENTRY_proc_poll; +} + +/* + * procfs has to know the open files of an entry whose ->proc_release() it + * calls at removal and whose mappings it undoes then. A permanent entry is + * never removed and skips all that, see proc_reg_open(). This looks at the + * flags and not at the proc_ops: the last close of a file can come after + * the entry was removed and the proc_ops freed with their module, see + * proc_reg_release(). + */ +static inline bool pde_tracks_files(const struct proc_dir_entry *pde) +{ + if (pde_is_permanent(pde)) + return false; + return pde->flags & (PROC_ENTRY_proc_release | PROC_ENTRY_proc_mmap); +} + /* procfs owns the seq_file, see pde_seq_start() */ static inline bool pde_is_seq_file(const struct proc_dir_entry *pde) { @@ -388,15 +409,19 @@ struct pde_opener { } __randomize_layout; /* - * The open files of an entry whose ->proc_release() procfs has to call and - * whose mappings it has to undo when the entry is removed while they are - * still open, that is of an entry with a ->proc_release() or a - * ->proc_mmap(). Allocated with the entry, see proc_register(). + * What procfs keeps for the open files of an entry: the files themselves if + * it has to call their ->proc_release() and undo their mappings when the + * entry is removed, that is for an entry with a ->proc_release() or a + * ->proc_mmap(), and the wait queue their pollers sleep on for an entry with + * a ->proc_poll(). Allocated with the entry, see proc_register(). */ struct pde_open_node { - struct list_head files; /* who did ->open, but not ->release */ + /* who did ->open, but not ->release, see pde_tracks_files() */ + struct list_head files; /* of its mappings, the same for all of them, see pde_mmap() */ const struct vm_operations_struct *vm_ops; + /* what poll() and epoll wait on, see proc_poll_notify() */ + wait_queue_head_t poll; }; extern const struct inode_operations proc_link_inode_operations; extern const struct inode_operations proc_pid_link_inode_operations; diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index feb3228cb5af..8520b18ce1f7 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -47,6 +47,9 @@ enum { */ PROC_ENTRY_FULL_READS = 1U << 11, PROC_ENTRY_proc_ioctl_buf = 1U << 12, + PROC_ENTRY_proc_poll = 1U << 13, + PROC_ENTRY_proc_release = 1U << 14, + PROC_ENTRY_proc_mmap = 1U << 15, }; /* @@ -81,6 +84,12 @@ struct proc_ops { */ loff_t (*proc_lseek)(struct file *, loff_t, int); int (*proc_release)(struct inode *, struct file *); + /* + * procfs owns a wait queue for an entry with a ->proc_poll() and + * does the poll_wait() on it before it calls the method. The owner + * wakes the pollers with proc_poll_notify(). A poll on a removed + * entry reports EPOLLERR | EPOLLPRI. + */ __poll_t (*proc_poll)(struct file *, struct poll_table_struct *); long (*proc_ioctl)(struct file *, unsigned int, unsigned long); #ifdef CONFIG_COMPAT @@ -243,6 +252,7 @@ extern void *proc_get_parent_data(const struct inode *); extern void proc_remove(struct proc_dir_entry *); extern void remove_proc_entry(const char *, struct proc_dir_entry *); extern int remove_proc_subtree(const char *, struct proc_dir_entry *); +void proc_poll_notify(struct proc_dir_entry *pde); struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct seq_operations *ops, @@ -341,6 +351,7 @@ static inline void *proc_get_parent_data(const struct inode *inode) { BUG(); ret static inline void proc_remove(struct proc_dir_entry *de) {} #define remove_proc_entry(name, parent) do {} while (0) static inline int remove_proc_subtree(const char *name, struct proc_dir_entry *parent) { return 0; } +static inline void proc_poll_notify(struct proc_dir_entry *pde) {} #define proc_create_net_data(name, mode, parent, ops, state_size, data) ({NULL;}) #define proc_create_net_data_write(name, mode, parent, ops, write, state_size, data) ({NULL;}) -- 2.53.0 The example in seq_file.rst hands proc_create() file operations built from an open method that calls seq_open() and from seq_read(), seq_lseek() and seq_release(). procfs doesn't work that way anymore. It opens and releases the seq_file itself when the entry declares its seq_operations or its show function in struct proc_ops. Say so, and point the extra-simple version at ->proc_show() for /proc files. Signed-off-by: Christian Brauner (Amutable) --- Documentation/filesystems/seq_file.rst | 42 ++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/Documentation/filesystems/seq_file.rst b/Documentation/filesystems/seq_file.rst index d753d8177bcb..3e6ff1c1226d 100644 --- a/Documentation/filesystems/seq_file.rst +++ b/Documentation/filesystems/seq_file.rst @@ -68,7 +68,7 @@ kernel 3.10. Current versions require the following update:: - entry = create_proc_entry("sequence", 0, NULL); - if (entry) - entry->proc_fops = &ct_file_ops; - + entry = proc_create("sequence", 0, NULL, &ct_file_ops); + + entry = proc_create("sequence", 0, NULL, &ct_proc_ops); The iterator interface ====================== @@ -277,9 +277,10 @@ seq_file system, but we have not yet turned them into a file that a user can see. Creating a file within the kernel requires, of course, the creation of a set of file_operations which implement the operations on that file. The seq_file interface provides a set of canned operations which do -most of the work. The virtual file author still must implement the open() -method, however, to hook everything up. The open function is often a single -line, as in the example module:: +most of the work. A file in /proc is special, procfs hooks the seq_file up +itself, see below. Everywhere else the virtual file author still must +implement the open() method to do that. The open function is often a +single line:: static int ct_open(struct inode *inode, struct file *file) { @@ -343,19 +344,38 @@ file_operations structure will look like:: There is also a seq_release_private() which passes the contents of the seq_file private field to kfree() before releasing the structure. -The final step is the creation of the /proc file itself. In the example -code, that is done in the initialization code in the usual way:: +The final step is the creation of the file itself. A file in /proc is +special here: procfs opens and releases the seq_file itself, so there is +no open method and no file_operations for it. The entry declares its +seq_operations in struct proc_ops instead, and the size of a private +block in proc_state_size if it wants procfs to allocate one like +seq_open_private() would:: + + static const struct proc_ops ct_proc_ops = { + .proc_seq_ops = &ct_seq_ops, + }; static int ct_init(void) { - struct proc_dir_entry *entry; - - proc_create("sequence", 0, NULL, &ct_file_ops); + proc_create("sequence", 0, NULL, &ct_proc_ops); return 0; } module_init(ct_init); +A ->proc_open() method of such an entry finds the seq_file in +file->private_data and may set up more state; ->proc_release() releases +only that. It runs when the entry is removed, which can be while a reader +is still copying out of the seq_file, so it must leave the seq_file +alone. procfs only calls into the entry with the entry pinned, between +start() and stop() like in its other methods, so nothing that runs there +may wait for something a reader can hold while it copies to userspace, +like the seq_file's lock. +An entry that wants to start out with a larger buffer, like +single_open_size() gives, calls seq_preallocate() from ->proc_open(). +The file_operations above are what a file elsewhere, in debugfs for +example, uses. + And that is pretty much it. @@ -394,3 +414,7 @@ value given to single_open() can be found in the private field of the seq_file structure. When using single_open(), the programmer should use single_release() instead of seq_release() in the file_operations structure to avoid a memory leak. + +A file in /proc puts the show() function into the proc_show field of its +struct proc_ops instead and procfs does the rest, with the entry's data in +the private field of the seq_file structure. -- 2.53.0