Skip to content

Commit 6319194

Browse files
author
Al Viro
committed
Unify the primitives for file descriptor closing
Currently we have 3 primitives for removing an opened file from descriptor table - pick_file(), __close_fd_get_file() and close_fd_get_file(). Their calling conventions are rather odd and there's a code duplication for no good reason. They can be unified - 1) have __range_close() cap max_fd in the very beginning; that way we don't need separate way for pick_file() to report being past the end of descriptor table. 2) make {__,}close_fd_get_file() return file (or NULL) directly, rather than returning it via struct file ** argument. Don't bother with (bogus) return value - nobody wants that -ENOENT. 3) make pick_file() return NULL on unopened descriptor - the only caller that used to care about the distinction between descriptor past the end of descriptor table and finding NULL in descriptor table doesn't give a damn after (1). 4) lift ->files_lock out of pick_file() That actually simplifies the callers, as well as the primitives themselves. Code duplication is also gone... Reviewed-by: Christian Brauner (Microsoft) <brauner@kernel.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 81132a3 commit 6319194

File tree

5 files changed

+34
-56
lines changed

5 files changed

+34
-56
lines changed

drivers/android/binder.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ static void binder_deferred_fd_close(int fd)
18551855
if (!twcb)
18561856
return;
18571857
init_task_work(&twcb->twork, binder_do_fd_close);
1858-
close_fd_get_file(fd, &twcb->file);
1858+
twcb->file = close_fd_get_file(fd);
18591859
if (twcb->file) {
18601860
filp_close(twcb->file, current->files);
18611861
task_work_add(current, &twcb->twork, TWA_RESUME);

fs/file.c

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -630,32 +630,23 @@ EXPORT_SYMBOL(fd_install);
630630
* @files: file struct to retrieve file from
631631
* @fd: file descriptor to retrieve file for
632632
*
633-
* If this functions returns an EINVAL error pointer the fd was beyond the
634-
* current maximum number of file descriptors for that fdtable.
633+
* Context: files_lock must be held.
635634
*
636-
* Returns: The file associated with @fd, on error returns an error pointer.
635+
* Returns: The file associated with @fd (NULL if @fd is not open)
637636
*/
638637
static struct file *pick_file(struct files_struct *files, unsigned fd)
639638
{
639+
struct fdtable *fdt = files_fdtable(files);
640640
struct file *file;
641-
struct fdtable *fdt;
642641

643-
spin_lock(&files->file_lock);
644-
fdt = files_fdtable(files);
645-
if (fd >= fdt->max_fds) {
646-
file = ERR_PTR(-EINVAL);
647-
goto out_unlock;
648-
}
642+
if (fd >= fdt->max_fds)
643+
return NULL;
644+
649645
file = fdt->fd[fd];
650-
if (!file) {
651-
file = ERR_PTR(-EBADF);
652-
goto out_unlock;
646+
if (file) {
647+
rcu_assign_pointer(fdt->fd[fd], NULL);
648+
__put_unused_fd(files, fd);
653649
}
654-
rcu_assign_pointer(fdt->fd[fd], NULL);
655-
__put_unused_fd(files, fd);
656-
657-
out_unlock:
658-
spin_unlock(&files->file_lock);
659650
return file;
660651
}
661652

@@ -664,8 +655,10 @@ int close_fd(unsigned fd)
664655
struct files_struct *files = current->files;
665656
struct file *file;
666657

658+
spin_lock(&files->file_lock);
667659
file = pick_file(files, fd);
668-
if (IS_ERR(file))
660+
spin_unlock(&files->file_lock);
661+
if (!file)
669662
return -EBADF;
670663

671664
return filp_close(file, files);
@@ -702,20 +695,25 @@ static inline void __range_cloexec(struct files_struct *cur_fds,
702695
static inline void __range_close(struct files_struct *cur_fds, unsigned int fd,
703696
unsigned int max_fd)
704697
{
698+
unsigned n;
699+
700+
rcu_read_lock();
701+
n = last_fd(files_fdtable(cur_fds));
702+
rcu_read_unlock();
703+
max_fd = min(max_fd, n);
704+
705705
while (fd <= max_fd) {
706706
struct file *file;
707707

708+
spin_lock(&cur_fds->file_lock);
708709
file = pick_file(cur_fds, fd++);
709-
if (!IS_ERR(file)) {
710+
spin_unlock(&cur_fds->file_lock);
711+
712+
if (file) {
710713
/* found a valid file to close */
711714
filp_close(file, cur_fds);
712715
cond_resched();
713-
continue;
714716
}
715-
716-
/* beyond the last fd in that table */
717-
if (PTR_ERR(file) == -EINVAL)
718-
return;
719717
}
720718
}
721719

@@ -795,43 +793,26 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
795793
* See close_fd_get_file() below, this variant assumes current->files->file_lock
796794
* is held.
797795
*/
798-
int __close_fd_get_file(unsigned int fd, struct file **res)
796+
struct file *__close_fd_get_file(unsigned int fd)
799797
{
800-
struct files_struct *files = current->files;
801-
struct file *file;
802-
struct fdtable *fdt;
803-
804-
fdt = files_fdtable(files);
805-
if (fd >= fdt->max_fds)
806-
goto out_err;
807-
file = fdt->fd[fd];
808-
if (!file)
809-
goto out_err;
810-
rcu_assign_pointer(fdt->fd[fd], NULL);
811-
__put_unused_fd(files, fd);
812-
get_file(file);
813-
*res = file;
814-
return 0;
815-
out_err:
816-
*res = NULL;
817-
return -ENOENT;
798+
return pick_file(current->files, fd);
818799
}
819800

820801
/*
821802
* variant of close_fd that gets a ref on the file for later fput.
822803
* The caller must ensure that filp_close() called on the file, and then
823804
* an fput().
824805
*/
825-
int close_fd_get_file(unsigned int fd, struct file **res)
806+
struct file *close_fd_get_file(unsigned int fd)
826807
{
827808
struct files_struct *files = current->files;
828-
int ret;
809+
struct file *file;
829810

830811
spin_lock(&files->file_lock);
831-
ret = __close_fd_get_file(fd, res);
812+
file = pick_file(files, fd);
832813
spin_unlock(&files->file_lock);
833814

834-
return ret;
815+
return file;
835816
}
836817

837818
void do_close_on_exec(struct files_struct *files)

fs/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ extern struct file *do_file_open_root(const struct path *,
125125
const char *, const struct open_flags *);
126126
extern struct open_how build_open_how(int flags, umode_t mode);
127127
extern int build_open_flags(const struct open_how *how, struct open_flags *op);
128-
extern int __close_fd_get_file(unsigned int fd, struct file **res);
128+
extern struct file *__close_fd_get_file(unsigned int fd);
129129

130130
long do_sys_ftruncate(unsigned int fd, loff_t length, int small);
131131
int chmod_common(const struct path *path, umode_t mode);

fs/io_uring.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5137,13 +5137,10 @@ static int io_close(struct io_kiocb *req, unsigned int issue_flags)
51375137
return -EAGAIN;
51385138
}
51395139

5140-
ret = __close_fd_get_file(close->fd, &file);
5140+
file = __close_fd_get_file(close->fd);
51415141
spin_unlock(&files->file_lock);
5142-
if (ret < 0) {
5143-
if (ret == -ENOENT)
5144-
ret = -EBADF;
5142+
if (!file)
51455143
goto err;
5146-
}
51475144

51485145
/* No ->flush() or already async, safely close from here */
51495146
ret = filp_close(file, current->files);

include/linux/fdtable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ int iterate_fd(struct files_struct *, unsigned,
125125

126126
extern int close_fd(unsigned int fd);
127127
extern int __close_range(unsigned int fd, unsigned int max_fd, unsigned int flags);
128-
extern int close_fd_get_file(unsigned int fd, struct file **res);
128+
extern struct file *close_fd_get_file(unsigned int fd);
129129
extern int unshare_fd(unsigned long unshare_flags, unsigned int max_fds,
130130
struct files_struct **new_fdp);
131131

0 commit comments

Comments
 (0)