file: Introduce iterate_fd_locked
Subsystems (3)
RFC v3 1/3 file: Introduce iterate_fd_locked
Callers holding the files->file_lock lock can call iterate_fd_locked instead of iterate_fd
--- fs/file.c | 21 +++++++++++++++------ include/linux/fdtable.h | 3 +++ 2 files changed, 18 insertions(+), 6 deletions(-)
fs/file.c +15 -6
@@ -1295,15 +1295,12 @@ int f_dupfd(unsigned int from, struct file *file, unsigned flags)return err; } -int iterate_fd (struct files_struct *files,unsigned n, -int (*f)(const void *,struct file *,unsigned ), -const void *p) +int iterate_fd_locked (struct files_struct *files,unsigned n, +int (*f)(const void *,struct file *,unsigned ), +const void *p) {struct fdtable *fdt;int res =0 ; -if (!files) -return 0 ; -spin_lock (&files->file_lock);for (fdt =files_fdtable (files); n < fdt->max_fds; n++) {struct file *file; file =rcu_dereference_check_fdtable (files, fdt->fd[n]);
@@ -1313,6 +1310,18 @@ int iterate_fd(struct files_struct *files, unsigned n,if (res)break ; } +return res; +} + +int iterate_fd (struct files_struct *files,unsigned n, +int (*f)(const void *,struct file *,unsigned ), +const void *p) +{ +int res =0 ; +if (!files) +return 0 ; +spin_lock (&files->file_lock); + res =iterate_fd_locked (files, n, f, p);spin_unlock (&files->file_lock);return res; }
include/linux/fdtable.h +3 -0
@@ -122,6 +122,9 @@ void do_close_on_exec(struct files_struct *);int iterate_fd (struct files_struct *,unsigned ,int (*)(const void *,struct file *,unsigned ),const void *); +int iterate_fd_locked (struct files_struct *,unsigned , +int (*)(const void *,struct file *,unsigned ), +const void *);extern int close_fd (unsigned int fd);extern int __close_range (unsigned int fd,unsigned int max_fd,unsigned int flags);
Signature · 1 line
2.34.1
RFC v3 2/3 file: allow callers to free the old file descriptor after dup2
Allow callers of do_dup2 to get a reference to the file pointer being dup'ed over. The callers can then replace the file with the new file in the eventpoll interface or the file table before freeing it.
---
Changes in v2:
- Make the commit message more clearer.
- Address review comment to make the interface cleaner so that the caller cannot forget to initialize the fdfile.
--- fs/file.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
fs/file.c +13 -5
@@ -1086,7 +1086,7 @@ bool get_close_on_exec(unsigned int fd) }static int do_dup2 (struct files_struct *files, -struct file *file,unsigned fd,unsigned flags) +struct file *file,unsigned fd,struct file **fdfile,unsigned flags) __releases(&files->file_lock) {struct file *tofree;
@@ -1119,8 +1119,12 @@ __releases(&files->file_lock)__clear_close_on_exec (fd, fdt);spin_unlock (&files->file_lock); -if (tofree) -filp_close (tofree, files); +if (fdfile) { + *fdfile = tofree; + }else { +if (tofree) +filp_close (tofree, files); + }return fd;
@@ -1132,6 +1136,7 @@ __releases(&files->file_lock)int replace_fd (unsigned fd,struct file *file,unsigned flags) {int err; +struct file *fdfile =NULL ;struct files_struct *files = current->files;if (!file)
@@ -1144,7 +1149,10 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) err =expand_files (files, fd);if (unlikely (err <0 )) goto out_unlock; -return do_dup2 (files, file, fd, flags); + err =do_dup2 (files, file, fd, &fdfile, flags); +if (fdfile) +filp_close (fdfile, files); +return err; out_unlock:spin_unlock (&files->file_lock);
@@ -1237,7 +1245,7 @@ static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags) goto Ebadf; goto out_unlock; } -return do_dup2 (files, file, newfd, flags); +return do_dup2 (files, file, newfd,NULL , flags); Ebadf: err = -EBADF ;
Signature · 1 line
2.34.1
Re: [RFC v3 2/3] file: allow callers to free the old file descriptor after dup2
fs/file.c __releases(&files->file_lock) · line 1127
Quoted 12 lines · aloktiagi
fs/file.c +6
-2@@ -1119,8 +1119,12 @@ __releases(&files->file_lock)__clear_close_on_exec (fd, fdt);spin_unlock (&files->file_lock); -if (tofree) -filp_close (tofree, files); +if (fdfile) { + *fdfile = tofree; + }else { +if (tofree) +filp_close (tofree, files); + }
Why not:
if (fdfile) *fdfile = tofree; else if (tofree) filp_close(tofree, files);
Shorter and makes the parallel structure more obvious.
Re: [RFC v3 2/3] file: allow callers to free the old file descriptor after dup2
Quoted 16 lines · Matthew Wilcox
fs/file.c __releases(&files->file_lock) · line 1127
Quoted 12 lines · aloktiagi
fs/file.c +6
-2@@ -1119,8 +1119,12 @@ __releases(&files->file_lock)__clear_close_on_exec (fd, fdt);spin_unlock (&files->file_lock); -if (tofree) -filp_close (tofree, files); +if (fdfile) { + *fdfile = tofree; + }else { +if (tofree) +filp_close (tofree, files); + }Why not:
Quoted 7 lines
if (fdfile) *fdfile = tofree; else if (tofree) filp_close(tofree, files);Shorter and makes the parallel structure more obvious.
Agreed. Thank you for suggesting that. I'll fix this in v4.
RFC v3 3/3 file, epoll: Implement do_replace() and eventpoll_replace()
In reply to aloktiagiIntroduce a mechanism to replace a file linked in the epoll interface or a file that has been dup'ed by a file received via the replace_fd() interface.
eventpoll_replace() is called from do_replace() and finds all instances of the file to be replaced and replaces them with the new file.
do_replace() also replaces the file in the file descriptor table for all fd numbers referencing it with the new file.
We have a use case where multiple IPv6 only network namespaces can use a single IPv4 network namespace for IPv4 only egress connectivity by switching their sockets from IPv6 to IPv4 network namespace. This allows for migration of systems to IPv6 only while keeping their connectivity to IPv4 only destinations intact.
Today, we achieve this by setting up seccomp filter to intercept network system calls like connect() from a container in a container manager which runs in an IPv4 only network namespace. The container manager creates a new IPv4 connection and injects the new file descriptor through SECCOMP_NOTIFY_IOCTL_ADDFD replacing the original file descriptor from the connect() call. This does not work for cases where the original file descriptor is handed off to a system like epoll before the connect() call. After a new file descriptor is injected the original file descriptor being referenced by the epoll fd is not longer valid leading to failures. As a workaround the container manager when intercepting connect() loops through all open socket file descriptors to check if they are referencing the socket attempting the connect() and replace the reference with the to be injected file descriptor. This workaround is cumbersome and makes the solution prone to similar yet to be discovered issues.
The above change will enable us remove the workaround in the container manager and let the kernel handle the replacement correctly.
---
Changes in v3:
- address review comment and iterate over the file table while holding the
spin_lock(&files->file_lock).
- address review comment and call filp_close() outside the
spin_lock(&files->file_lock).
---
fs/eventpoll.c | 38 ++++++++
fs/file.c | 56 +++++++++++
include/linux/eventpoll.h | 18 ++++
include/linux/file.h | 1 +
tools/testing/selftests/seccomp/seccomp_bpf.c | 97 +++++++++++++++++++
5 files changed, 210 insertions(+)fs/eventpoll.c +38 -0
@@ -935,6 +935,44 @@ void eventpoll_release_file(struct file *file)mutex_unlock (&epmutex); } +static int ep_insert (struct eventpoll *ep,const struct epoll_event *event, +struct file *tfile,int fd,int full_check); + +/* +* This is called from eventpoll_replace() to replace a linked file in the epoll +* interface with a new file received from another process. This is useful in +* cases where a process is trying to install a new file for an existing one +* that is linked in the epoll interface +*/ +void eventpoll_replace_file (struct file *toreplace,struct file *file) +{ +int fd; +struct eventpoll *ep; +struct epitem *epi; +struct hlist_node *next; +struct epoll_event event; + +if (!file_can_poll (file)) +return ; + +mutex_lock (&epmutex); +if (unlikely (!toreplace->f_ep)) { +mutex_unlock (&epmutex); +return ; + } + +hlist_for_each_entry_safe (epi, next, toreplace->f_ep, fllink) { + ep = epi->ep; +mutex_lock (&ep->mtx); + fd = epi->ffd.fd; + event = epi->event; +ep_remove (ep, epi); +ep_insert (ep, &event, file, fd,1 ); +mutex_unlock (&ep->mtx); + } +mutex_unlock (&epmutex); +} +static int ep_alloc (struct eventpoll **pep) {int error;
fs/file.c +56 -0
80 lines not on this page · raw message
include/linux/eventpoll.h +18 -0
@@ -25,6 +25,8 @@ struct file *get_epoll_tfile_raw_ptr(struct file *file, int tfd, unsigned long t/* Used to release the epoll bits inside the "struct file" */ void eventpoll_release_file (struct file *file); +void eventpoll_replace_file (struct file *toreplace,struct file *file); +/* * This is called from inside fs/file_table.c:__fput() to unlink files * from the eventpoll interface. We need to have this facility to cleanup
@@ -53,6 +55,22 @@ static inline void eventpoll_release(struct file *file)eventpoll_release_file (file); } + +/* +* This is called from fs/file.c:do_replace() to replace a linked file in the +* epoll interface with a new file received from another process. This is useful +* in cases where a process is trying to install a new file for an existing one +* that is linked in the epoll interface +*/ +static inline void eventpoll_replace (struct file *toreplace,struct file *file) +{ +/* +* toreplace is the file being replaced. Install the new file for the +* existing one that is linked in the epoll interface +*/ +eventpoll_replace_file (toreplace, file); +} +int do_epoll_ctl (int epfd,int op,int fd,struct epoll_event *epds,bool nonblock);
include/linux/file.h +1 -0
@@ -36,6 +36,7 @@ struct fd {struct file *file;unsigned int flags; }; +#define FDPUT_FPUT 1#define FDPUT_POS_UNLOCK 2
tools/testing/selftests/seccomp/seccomp_bpf.c +97 -0
109 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC v3 3/3] file, epoll: Implement do_replace() and eventpoll_replace()
Quoted 22 lines · aloktiagi
include/linux/eventpoll.h +7
-0@@ -25,6 +25,8 @@ struct file *get_epoll_tfile_raw_ptr(struct file *file, int tfd, unsigned long t/* Used to release the epoll bits inside the "struct file" */ void eventpoll_release_file (struct file *file); +void eventpoll_replace_file (struct file *toreplace,struct file *file); +/* * This is called from inside fs/file_table.c:__fput() to unlink files * from the eventpoll interface. We need to have this facility to cleanup @@ -53,6 +55,22 @@ static inline void eventpoll_release(struct file *file)eventpoll_release_file (file); } + +/* +* This is called from fs/file.c:do_replace() to replace a linked file in the +* epoll interface with a new file received from another process. This is useful +* in cases where a process is trying to install a new file for an existing one
include/linux/eventpoll.h static inline void eventpoll_release(struct file *file) · line 72
Quoted 10 lines
include/linux/eventpoll.h +10
-0+* that is linked in the epoll interface +*/ +static inline void eventpoll_replace (struct file *toreplace,struct file *file) +{ +/* +* toreplace is the file being replaced. Install the new file for the +* existing one that is linked in the epoll interface +*/ +eventpoll_replace_file (toreplace, file); +}
Why do we have both eventpoll_replace() and eventpoll_replace_file()? They seem identical?
include/linux/file.h struct fd { · line 42
Quoted 12 lines
include/linux/file.h +1
-0@@ -36,6 +36,7 @@ struct fd {struct file *file;unsigned int flags; }; +#define FDPUT_FPUT 1#define FDPUT_POS_UNLOCK 2
You should drop this hunk of the patch.
Re: [RFC v3 3/3] file, epoll: Implement do_replace() and eventpoll_replace()
Quoted 28 lines · Matthew Wilcox
Quoted 27 lines · aloktiagi
include/linux/eventpoll.h +12
-0@@ -25,6 +25,8 @@ struct file *get_epoll_tfile_raw_ptr(struct file *file, int tfd, unsigned long t/* Used to release the epoll bits inside the "struct file" */ void eventpoll_release_file (struct file *file); +void eventpoll_replace_file (struct file *toreplace,struct file *file); +/* * This is called from inside fs/file_table.c:__fput() to unlink files * from the eventpoll interface. We need to have this facility to cleanup @@ -53,6 +55,22 @@ static inline void eventpoll_release(struct file *file)eventpoll_release_file (file); } + +/* +* This is called from fs/file.c:do_replace() to replace a linked file in the +* epoll interface with a new file received from another process. This is useful +* in cases where a process is trying to install a new file for an existing one +* that is linked in the epoll interface +*/ +static inline void eventpoll_replace (struct file *toreplace,struct file *file) +{ +/*
Quoted 8 lines
include/linux/eventpoll.h static inline void eventpoll_release(struct file *file) · line 72
Quoted 5 lines
include/linux/eventpoll.h +5
-0+* toreplace is the file being replaced. Install the new file for the +* existing one that is linked in the epoll interface +*/ +eventpoll_replace_file (toreplace, file); +}Why do we have both
eventpoll_replace()andeventpoll_replace_file()? They seem identical?
I was following the eventpoll_release_file() convention but yes seems unnecessary. I'll retain eventpoll_replace_file() and remove eventpoll_replace() in v4.
Quoted 15 lines
include/linux/file.h struct fd { · line 42
Quoted 13 lines
include/linux/file.h +1
-0@@ -36,6 +36,7 @@ struct fd {struct file *file;unsigned int flags; }; +#define FDPUT_FPUT 1#define FDPUT_POS_UNLOCK 2You should drop this hunk of the patch.
I overlooked this. Will fix it in v4.
kernel test robot
build botRe: [RFC v3 3/3] file, epoll: Implement do_replace() and eventpoll_replace()
In reply to aloktiagi[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in…
Hi aloktiagi,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kees/for-next/seccomp]
[also build test ERROR on vfs-idmapping/for-next kees/for-next/pstore kees/for-next/kspp linus/master v6.3-rc4 next-20230329] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/aloktiagi/file-allow-callers-to-free-the-old-file-descriptor-after-dup2/20230324-131704 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/seccomp patch link: https://lore.kernel.org/r/20230324051526.963702-3-aloktiagi%40gmail.com patch subject: [RFC v3 3/3] file, epoll: Implement do_replace() and eventpoll_replace() config: riscv-nommu_k210_sdcard_defconfig (https://download.01.org/0day-ci/archive/20230329/[email protected]/config) compiler: riscv64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/42c4f34040dfc73e03bb3649290748120de7db60
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review aloktiagi/file-allow-callers-to-free-the-old-file-descriptor-after-dup2/20230324-131704 git checkout 42c4f34040dfc73e03bb3649290748120de7db60
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=riscv olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=riscv SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
fs/file.c: In function 'do_replace':
>> fs/file.c:1178:31: error: 'struct file' has no member named 'f_ep'; did you mean 'f_op'?
1178 | if (fdfile && fdfile->f_ep) {
| ^~~~
| f_op
>> fs/file.c:1179:17: error: implicit declaration of function 'eventpoll_replace'; did you mean 'eventpoll_release'? [-Werror=implicit-function-declaration]
1179 | eventpoll_replace(fdfile, file);
| ^~~~~~~~~~~~~~~~~
| eventpoll_releasecc1: some warnings being treated as errors
vim +1178 fs/file.c
1161
1162 static void do_replace(struct files_struct *files,
1163 struct file *file, unsigned fd, struct file *fdfile)
1164 {
1165 unsigned n = 0;
1166 struct replace_filefd ffd = {
1167 .files = files,
1168 .fd = fd,
1169 .fdfile = fdfile,
1170 .file = file
1171 };
1172
1173 /*
1174 * Check if the file referenced by the fd number is linked to the epoll
1175 * interface. If yes, replace the reference with the received file in
1176 * the epoll interface.
1177 */
> 1178 if (fdfile && fdfile->f_ep) {
> 1179 eventpoll_replace(fdfile, file); 1180 }
1181 /*
1182 * Install the received file in the file descriptor table for all fd
1183 * numbers referencing the same file as the one we are trying to
1184 * replace. Do not install it for the fd number received since that is
1185 * handled in do_dup2()
1186 */
1187 iterate_fd(files, n, do_replace_fd_array, &ffd);
1188 }
1189
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Quoted 2 lines · aloktiagi
Callers holding the
files->file_locklock can calliterate_fd_lockedinstead ofiterate_fd
You no longer call iterate_fd_locked() in patch 3/3, so this patch can be dropped?
Quoted 6 lines · Matthew Wilcox
Quoted 2 lines · aloktiagi
Callers holding the
files->file_locklock can calliterate_fd_lockedinstead ofiterate_fdYou no longer call
iterate_fd_locked()in patch 3/3, so this patch can be dropped?
yes looks like we won't need this patch and can be dropped. Thank you.