Skip to content

Commit dbe0ee4

Browse files
committed
Merge tag 'pull-18-rc1-work.fd' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
Pull file descriptor updates from Al Viro. - Descriptor handling cleanups * tag 'pull-18-rc1-work.fd' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: Unify the primitives for file descriptor closing fs: remove fget_many and fput_many interface io_uring_enter(): don't leave f.flags uninitialized
2 parents d66016c + 6319194 commit dbe0ee4

File tree

8 files changed

+55
-94
lines changed

8 files changed

+55
-94
lines changed

drivers/android/binder.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,7 @@ static void binder_deferred_fd_close(int fd)
18841884
if (!twcb)
18851885
return;
18861886
init_task_work(&twcb->twork, binder_do_fd_close);
1887-
close_fd_get_file(fd, &twcb->file);
1887+
twcb->file = close_fd_get_file(fd);
18881888
if (twcb->file) {
18891889
filp_close(twcb->file, current->files);
18901890
task_work_add(current, &twcb->twork, TWA_RESUME);

fs/file.c

Lines changed: 42 additions & 68 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)
@@ -871,7 +852,7 @@ void do_close_on_exec(struct files_struct *files)
871852
}
872853

873854
static inline struct file *__fget_files_rcu(struct files_struct *files,
874-
unsigned int fd, fmode_t mask, unsigned int refs)
855+
unsigned int fd, fmode_t mask)
875856
{
876857
for (;;) {
877858
struct file *file;
@@ -897,10 +878,9 @@ static inline struct file *__fget_files_rcu(struct files_struct *files,
897878
* Such a race can take two forms:
898879
*
899880
* (a) the file ref already went down to zero,
900-
* and get_file_rcu_many() fails. Just try
901-
* again:
881+
* and get_file_rcu() fails. Just try again:
902882
*/
903-
if (unlikely(!get_file_rcu_many(file, refs)))
883+
if (unlikely(!get_file_rcu(file)))
904884
continue;
905885

906886
/*
@@ -909,11 +889,11 @@ static inline struct file *__fget_files_rcu(struct files_struct *files,
909889
* pointer having changed, because it always goes
910890
* hand-in-hand with 'fdt'.
911891
*
912-
* If so, we need to put our refs and try again.
892+
* If so, we need to put our ref and try again.
913893
*/
914894
if (unlikely(rcu_dereference_raw(files->fdt) != fdt) ||
915895
unlikely(rcu_dereference_raw(*fdentry) != file)) {
916-
fput_many(file, refs);
896+
fput(file);
917897
continue;
918898
}
919899

@@ -926,37 +906,31 @@ static inline struct file *__fget_files_rcu(struct files_struct *files,
926906
}
927907

928908
static struct file *__fget_files(struct files_struct *files, unsigned int fd,
929-
fmode_t mask, unsigned int refs)
909+
fmode_t mask)
930910
{
931911
struct file *file;
932912

933913
rcu_read_lock();
934-
file = __fget_files_rcu(files, fd, mask, refs);
914+
file = __fget_files_rcu(files, fd, mask);
935915
rcu_read_unlock();
936916

937917
return file;
938918
}
939919

940-
static inline struct file *__fget(unsigned int fd, fmode_t mask,
941-
unsigned int refs)
942-
{
943-
return __fget_files(current->files, fd, mask, refs);
944-
}
945-
946-
struct file *fget_many(unsigned int fd, unsigned int refs)
920+
static inline struct file *__fget(unsigned int fd, fmode_t mask)
947921
{
948-
return __fget(fd, FMODE_PATH, refs);
922+
return __fget_files(current->files, fd, mask);
949923
}
950924

951925
struct file *fget(unsigned int fd)
952926
{
953-
return __fget(fd, FMODE_PATH, 1);
927+
return __fget(fd, FMODE_PATH);
954928
}
955929
EXPORT_SYMBOL(fget);
956930

957931
struct file *fget_raw(unsigned int fd)
958932
{
959-
return __fget(fd, 0, 1);
933+
return __fget(fd, 0);
960934
}
961935
EXPORT_SYMBOL(fget_raw);
962936

@@ -966,7 +940,7 @@ struct file *fget_task(struct task_struct *task, unsigned int fd)
966940

967941
task_lock(task);
968942
if (task->files)
969-
file = __fget_files(task->files, fd, 0, 1);
943+
file = __fget_files(task->files, fd, 0);
970944
task_unlock(task);
971945

972946
return file;
@@ -1035,7 +1009,7 @@ static unsigned long __fget_light(unsigned int fd, fmode_t mask)
10351009
return 0;
10361010
return (unsigned long)file;
10371011
} else {
1038-
file = __fget(fd, mask, 1);
1012+
file = __fget(fd, mask);
10391013
if (!file)
10401014
return 0;
10411015
return FDPUT_FPUT | (unsigned long)file;

fs/file_table.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,9 @@ EXPORT_SYMBOL_GPL(flush_delayed_fput);
368368

369369
static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
370370

371-
void fput_many(struct file *file, unsigned int refs)
371+
void fput(struct file *file)
372372
{
373-
if (atomic_long_sub_and_test(refs, &file->f_count)) {
373+
if (atomic_long_dec_and_test(&file->f_count)) {
374374
struct task_struct *task = current;
375375

376376
if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
@@ -389,11 +389,6 @@ void fput_many(struct file *file, unsigned int refs)
389389
}
390390
}
391391

392-
void fput(struct file *file)
393-
{
394-
fput_many(file, 1);
395-
}
396-
397392
/*
398393
* synchronous analog of fput(); for kernel threads that might be needed
399394
* in some umount() (and thus can't use flush_delayed_fput() without

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: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6039,13 +6039,10 @@ static int io_close(struct io_kiocb *req, unsigned int issue_flags)
60396039
return -EAGAIN;
60406040
}
60416041

6042-
ret = __close_fd_get_file(close->fd, &file);
6042+
file = __close_fd_get_file(close->fd);
60436043
spin_unlock(&files->file_lock);
6044-
if (ret < 0) {
6045-
if (ret == -ENOENT)
6046-
ret = -EBADF;
6044+
if (!file)
60476045
goto err;
6048-
}
60496046

60506047
/* No ->flush() or already async, safely close from here */
60516048
ret = filp_close(file, current->files);
@@ -12053,14 +12050,14 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
1205312050
return -EINVAL;
1205412051
fd = array_index_nospec(fd, IO_RINGFD_REG_MAX);
1205512052
f.file = tctx->registered_rings[fd];
12056-
if (unlikely(!f.file))
12057-
return -EBADF;
12053+
f.flags = 0;
1205812054
} else {
1205912055
f = fdget(fd);
12060-
if (unlikely(!f.file))
12061-
return -EBADF;
1206212056
}
1206312057

12058+
if (unlikely(!f.file))
12059+
return -EBADF;
12060+
1206412061
ret = -EOPNOTSUPP;
1206512062
if (unlikely(f.file->f_op != &io_uring_fops))
1206612063
goto out_fput;
@@ -12158,8 +12155,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
1215812155
out:
1215912156
percpu_ref_put(&ctx->refs);
1216012157
out_fput:
12161-
if (!(flags & IORING_ENTER_REGISTERED_RING))
12162-
fdput(f);
12158+
fdput(f);
1216312159
return ret;
1216412160
}
1216512161

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

include/linux/file.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
struct file;
1515

1616
extern void fput(struct file *);
17-
extern void fput_many(struct file *, unsigned int);
1817

1918
struct file_operations;
2019
struct task_struct;
@@ -47,7 +46,6 @@ static inline void fdput(struct fd fd)
4746
}
4847

4948
extern struct file *fget(unsigned int fd);
50-
extern struct file *fget_many(unsigned int fd, unsigned int refs);
5149
extern struct file *fget_raw(unsigned int fd);
5250
extern struct file *fget_task(struct task_struct *task, unsigned int fd);
5351
extern unsigned long __fdget(unsigned int fd);

include/linux/fs.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -974,9 +974,7 @@ static inline struct file *get_file(struct file *f)
974974
atomic_long_inc(&f->f_count);
975975
return f;
976976
}
977-
#define get_file_rcu_many(x, cnt) \
978-
atomic_long_add_unless(&(x)->f_count, (cnt), 0)
979-
#define get_file_rcu(x) get_file_rcu_many((x), 1)
977+
#define get_file_rcu(x) atomic_long_inc_not_zero(&(x)->f_count)
980978
#define file_count(x) atomic_long_read(&(x)->f_count)
981979

982980
#define MAX_NON_LFS ((1UL<<31) - 1)

0 commit comments

Comments
 (0)