This is a note to let you know that I've just added the patch titled fs: provide locked helper variant of close_fd_get_file() to the 5.11-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: fs-provide-locked-helper-variant-of-close_fd_get_file.patch and it can be found in the queue-5.11 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From foo@baz Wed Mar 10 01:03:15 PM CET 2021 From: Pavel Begunkov <asml.silence@xxxxxxxxx> Date: Wed, 10 Mar 2021 11:30:41 +0000 Subject: fs: provide locked helper variant of close_fd_get_file() To: stable@xxxxxxxxxxxxxxx Cc: Jens Axboe <axboe@xxxxxxxxx> Message-ID: <ad604cc69f01343b6d8300d83e3a97f56f878e0d.1615375332.git.asml.silence@xxxxxxxxx> From: Jens Axboe <axboe@xxxxxxxxx> commit 53dec2ea74f2ef360e8455439be96a780baa6097 upstream Assumes current->files->file_lock is already held on invocation. Helps the caller check the file before removing the fd, if it needs to. Signed-off-by: Jens Axboe <axboe@xxxxxxxxx> Signed-off-by: Pavel Begunkov <asml.silence@xxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- fs/file.c | 36 +++++++++++++++++++++++++----------- fs/internal.h | 1 + 2 files changed, 26 insertions(+), 11 deletions(-) --- a/fs/file.c +++ b/fs/file.c @@ -22,6 +22,8 @@ #include <linux/close_range.h> #include <net/sock.h> +#include "internal.h" + unsigned int sysctl_nr_open __read_mostly = 1024*1024; unsigned int sysctl_nr_open_min = BITS_PER_LONG; /* our min() is unusable in constant expressions ;-/ */ @@ -732,36 +734,48 @@ int __close_range(unsigned fd, unsigned } /* - * variant of close_fd that gets a ref on the file for later fput. - * The caller must ensure that filp_close() called on the file, and then - * an fput(). + * See close_fd_get_file() below, this variant assumes current->files->file_lock + * is held. */ -int close_fd_get_file(unsigned int fd, struct file **res) +int __close_fd_get_file(unsigned int fd, struct file **res) { struct files_struct *files = current->files; struct file *file; struct fdtable *fdt; - spin_lock(&files->file_lock); fdt = files_fdtable(files); if (fd >= fdt->max_fds) - goto out_unlock; + goto out_err; file = fdt->fd[fd]; if (!file) - goto out_unlock; + goto out_err; rcu_assign_pointer(fdt->fd[fd], NULL); __put_unused_fd(files, fd); - spin_unlock(&files->file_lock); get_file(file); *res = file; return 0; - -out_unlock: - spin_unlock(&files->file_lock); +out_err: *res = NULL; return -ENOENT; } +/* + * variant of close_fd that gets a ref on the file for later fput. + * The caller must ensure that filp_close() called on the file, and then + * an fput(). + */ +int close_fd_get_file(unsigned int fd, struct file **res) +{ + struct files_struct *files = current->files; + int ret; + + spin_lock(&files->file_lock); + ret = __close_fd_get_file(fd, res); + spin_unlock(&files->file_lock); + + return ret; +} + void do_close_on_exec(struct files_struct *files) { unsigned i; --- a/fs/internal.h +++ b/fs/internal.h @@ -132,6 +132,7 @@ extern struct file *do_file_open_root(st const char *, const struct open_flags *); extern struct open_how build_open_how(int flags, umode_t mode); extern int build_open_flags(const struct open_how *how, struct open_flags *op); +extern int __close_fd_get_file(unsigned int fd, struct file **res); long do_sys_ftruncate(unsigned int fd, loff_t length, int small); int chmod_common(const struct path *path, umode_t mode); Patches currently in stable-queue which might be from asml.silence@xxxxxxxxx are queue-5.11/io_uring-io-wq-kill-off-now-unused-io_wq_work_no_cancel.patch queue-5.11/io_uring-get-rid-of-intermediate-ioring_op_close-stage.patch queue-5.11/io_uring-fix-inconsistent-lock-state.patch queue-5.11/fs-provide-locked-helper-variant-of-close_fd_get_file.patch queue-5.11/io_uring-io-wq-return-2-step-work-swap-scheme.patch queue-5.11/io_uring-deduplicate-core-cancellations-sequence.patch queue-5.11/io_uring-unpark-sqpoll-thread-for-cancelation.patch queue-5.11/io_uring-don-t-take-uring_lock-during-iowq-cancel.patch queue-5.11/io_uring-deduplicate-failing-task_work_add.patch