Skip to content

Commit 03ba0fe

Browse files
author
Christian Brauner
committed
file: simplify logic in __close_range()
It never looked too pleasant and it doesn't really buy us anything anymore now that CLOSE_RANGE_CLOEXEC exists and we need to retake the current maximum under the lock for it anyway. This also makes the logic easier to follow. Cc: Christoph Hellwig <[email protected]> Cc: Giuseppe Scrivano <[email protected]> Cc: Al Viro <[email protected]> Cc: [email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent f49fd6d commit 03ba0fe

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

fs/file.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,6 @@ static inline void __range_close(struct files_struct *cur_fds, unsigned int fd,
701701
*/
702702
int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
703703
{
704-
unsigned int cur_max;
705704
struct task_struct *me = current;
706705
struct files_struct *cur_fds = me->files, *fds = NULL;
707706

@@ -711,26 +710,26 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
711710
if (fd > max_fd)
712711
return -EINVAL;
713712

714-
rcu_read_lock();
715-
cur_max = files_fdtable(cur_fds)->max_fds;
716-
rcu_read_unlock();
717-
718-
/* cap to last valid index into fdtable */
719-
cur_max--;
720-
721713
if (flags & CLOSE_RANGE_UNSHARE) {
722714
int ret;
723715
unsigned int max_unshare_fds = NR_OPEN_MAX;
724716

725717
/*
726-
* If the requested range is greater than the current maximum,
727-
* we're closing everything so only copy all file descriptors
728-
* beneath the lowest file descriptor.
729-
* If the caller requested all fds to be made cloexec copy all
730-
* of the file descriptors since they still want to use them.
718+
* If the caller requested all fds to be made cloexec we always
719+
* copy all of the file descriptors since they still want to
720+
* use them.
731721
*/
732-
if (!(flags & CLOSE_RANGE_CLOEXEC) && (max_fd >= cur_max))
733-
max_unshare_fds = fd;
722+
if (!(flags & CLOSE_RANGE_CLOEXEC)) {
723+
/*
724+
* If the requested range is greater than the current
725+
* maximum, we're closing everything so only copy all
726+
* file descriptors beneath the lowest file descriptor.
727+
*/
728+
rcu_read_lock();
729+
if (max_fd >= last_fd(files_fdtable(cur_fds)))
730+
max_unshare_fds = fd;
731+
rcu_read_unlock();
732+
}
734733

735734
ret = unshare_fd(CLONE_FILES, max_unshare_fds, &fds);
736735
if (ret)
@@ -744,8 +743,6 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
744743
swap(cur_fds, fds);
745744
}
746745

747-
max_fd = min(max_fd, cur_max);
748-
749746
if (flags & CLOSE_RANGE_CLOEXEC)
750747
__range_cloexec(cur_fds, fd, max_fd);
751748
else

0 commit comments

Comments
 (0)