coredump, files: exit files on request
Christian Brauner
maintainerv3 cover coredump, files: exit files on request
We've had quite a few proposal for exiting files before generating the coredump (see [1]-[8]). The various proposed solution were quite unacceptable. So I took some time to look what a _remotely_ acceptable version of this could look like. Here it is.
The gist is that we allow the coredump socket to request the thread-group shed the various fdtables closed synchronously before generating the coredump.
The intricate task is that this requires us to add a primitive to close fdtables synchronously. Coredump is not enough to justify this but performance numbers for this look pretty convincing. For tasks exiting with a large file descriptor table this means getting rid of _a lot_ of cmpxchg()s and hammering on task->pi_lock. The cost of this was mentioned in various threads over the years. I found at least recent comments by Oleg and Neil.
The obvious problematic case are kernel threads. fput() punts their final __fput() to a workqueue. kthreads never return to userspace to run task work and some of them may need to finish a umount and so cannot ->release() inline. No path we change is taken by a kthread. They don't own a descriptor table and kthreadd is created with CLONE_FILES and every kthread inherits it. So all of them share init_files and init_task pins that forever. Their exit_files() never drops the last reference.
kernel_execve() refuses PF_KTHREAD outright. Usermodehelpers are spawned without the kthread flag. close_range() is a syscall. The one indirect way is a failing copy_process() calling exit_files() on a child from a kthread parent. A dup_fd() in copy_process() cannot hold the last reference to any of its files while the source table is alive.
Worker threads such as io_uring workers are user threads without PF_KTHREAD that share the fdtable. One of them can be the last to put the fdtable at exit. Today fput() hands their final __fput()s to task work. The worker runs that itself in exit_task_work() a few lines later. Now the same thread does the same work in exit_files() instead. vhost workers are created without a descriptor table at all, so they never even put an fdtable. Neither kind ever execs or calls close_range().
Now the performance numbers. I measured this on a 64 vCPU KVM guest (AMD EPYC 9754) on top of vfs-7.4.coredump with and without the series and the same x86_64_defconfig for both. The benchmark opens N files in a forked worker, moves them to random positions in the descriptor table so that the table order has nothing to do with the allocation order, and times the teardown:
- process exit
- execve() with N close-on-exec descriptors
- close_range() with and without CLOSE_RANGE_UNSHARE
Here are the medians over two boots which agree within 4%. With /dev/null as the file and 4096, 65536 and 1M descriptors:
4096 fds 65536 fds 1M fds
----------------------------------------------------------------------
exit 1045 -> 895 us 18.9 -> 14.9 ms 429 -> 306 ms
-14.4% -21.2% -28.7%
----------------------------------------------------------------------
execve, close-on-exec 1393 -> 1190 us 21.4 -> 16.9 ms 471 -> 325 ms
-14.6% -21.0% -31.1%
----------------------------------------------------------------------
close_range() 1179 -> 988 us 21.9 -> 17.4 ms 484 -> 337 ms
-16.1% -20.6% -30.3%
----------------------------------------------------------------------
close_range(UNSHARE) 1118 -> 958 us 22.1 -> 17.8 ms 487 -> 333 ms
-14.3% -19.8% -31.7%So that's a 16-21% win or 40-150 ns per closed file. This sheds init_task_work() and task_work_add() per file with its cmpxchg() and the TIF_NOTIFY_RESUME test-and-set, the list walk with the indirect call in the path out, and then a second pass over every struct file that isn't cache-hot anymore by then.
The performance boost holds from 256 to 1M descriptors and is largest at 1M. Note the thread count doesn't matter since one thread does the close anyway.
So with eight processes tearing down 65536 descriptors each at the same time the win is 16-21% (34.9 -> 29.5 ms for exit, 34.4 -> 27.0 ms for execve). The old code walks the table once to queue the task work and then walks all 16 MB of struct file a second time from task_work_run().
So once eight CPUs compete for the cache that second pass gets very expensive. With 32 or 64 processes at once both kernels are bound by pushing millions of files through slab and RCU. So then the difference shrinks to 4-8%. Still though...
Files whose own release dominates the cleanup gain a little less. 13% for eventfds, 11% for pipes, 6% for AF_UNIX sockets and 7% for distinct tmpfs inodes. For them a release costs about 2 us per descriptor anyway.
Using function profiling a 65536 descriptor exit spends 4.6 ms in exit_files() and 11.1 ms in task_work_run() before and now 13.7 ms in exit_files() and nothing in task_work_run() after.
close(2) is untouched and measures the same. will-it-scale (open1, open3, dup1, eventfd1, unix1, pipe1, signal1, processes and threads) is within 2% either way. dup1_threads at 16 tasks and getppid1 at 64 tasks a few percent better which I'd put down to layout.
So, back to coredumps. With COREDUMP_CLOSE_FILES coredumps switch to an empty file descriptor table before generating the coredump. That requires a bit of synchronization but I think I got the basics down.
The coredumping thread allocates a new empty file descriptor table. It then wakes all threads in the thread-group and tells them to switch to the empty file descriptor table and get rid of the old one. They report back once they're done.
So that handles most cases where locks would be held for an unreasonable time until the coredump is generated but since files can be shared between completely unrelated processes that's not a guarantee and we can't give one. But it solves the reported issue without resorting to even grosser hacks.
---
Changes in v3:
- Drop the llist. This brings yet another performance boost.
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Revamp.
- Link to v1: https://patch.msgid.link/[email protected]
---
Christian Brauner (15):
fs: don't open-code file_close_fd() in close_fd()
fs: add switch_files_struct()
fs: move unshare_fd() to fs/file.c
fs: remove unshare_files()
fs: move filp_flush() to other file helpers
fs: make close_files() synchronous
fs: make close_range() synchronous
fs: rename do_close_on_exec() to close_cloexec_files()
fs: make close_cloexec_files() synchronous
coredump: s/startup/done/g
coredump: factor out coredump_wait_inactive()
fs: add alloc_files_struct()
coredump: add COREDUMP_CLOSE_FILES
tools: sync coredump.h header selftests/coredump: test COREDUMP_CLOSE_FILES
fs/coredump.c | 91 +++- fs/exec.c | 13 +- fs/file.c | 115 ++-- fs/internal.h | 1 + fs/open.c | 2 +- include/linux/fdtable.h | 7 +- include/linux/sched/signal.h | 5 +- include/uapi/linux/coredump.h | 8 + kernel/exit.c | 15 +- kernel/fork.c | 48 +- tools/include/uapi/linux/coredump.h | 8 + tools/testing/selftests/coredump/Makefile | 4 +- .../selftests/coredump/coredump_close_files_test.c | 592 +++++++++++++++++++++ .../coredump/coredump_socket_protocol_test.c | 6 + .../selftests/coredump/coredump_test_helpers.c | 3 +- 15 files changed, 810 insertions(+), 108 deletions(-)
---
base-commit: a5625efa7a0e77c26e857de147284a21d53fa3ad
change-id: 20260824-work-coredump-unlock-self-63a898912870
Christian Brauner
maintainerRFC PATCH v3 1/15 fs: don't open-code file_close_fd() in close_fd()
close_fd() takes the lock, calls file_close_fd_locked() and drops the lock, which is exactly what file_close_fd() does. Use it.
No functional changes.
--- fs/file.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
fs/file.c +2 -5
@@ -732,16 +732,13 @@ struct file *file_close_fd_locked(struct files_struct *files, unsigned fd)int close_fd (unsigned fd) { -struct files_struct *files = current->files;struct file *file; -spin_lock (&files->file_lock); - file =file_close_fd_locked (files, fd); -spin_unlock (&files->file_lock); + file =file_close_fd (fd);if (!file)return -EBADF ; -return filp_close (file, files); +return filp_close (file, current->files); }EXPORT_SYMBOL (close_fd);
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 2/15 fs: add switch_files_struct()
In reply to Christian BraunerAdd switch_files_struct() to install another table on a task. It consumes the reference to the new table and hands the old one back for the caller to put. Convert every place that switches a descriptor table except unshare_files().
No functional changes.
--- fs/file.c | 28 ++++++++++++++++------------ include/linux/fdtable.h | 2 ++ kernel/fork.c | 6 ++---- 3 files changed, 20 insertions(+), 16 deletions(-)
fs/file.c +16 -12
@@ -515,16 +515,23 @@ void put_files_struct(struct files_struct *files) } } -void exit_files (struct task_struct *tsk) +/* Install @files on @tsk, consuming the reference; returns the old table. */ +struct files_struct *switch_files_struct (struct task_struct *tsk, +struct files_struct *files) { -struct files_struct * files = tsk->files; +struct files_struct *old; -if (files) { -task_lock (tsk); - tsk->files =NULL ; -task_unlock (tsk); -put_files_struct (files); - } +task_lock (tsk); + old = tsk->files; + tsk->files = files; +task_unlock (tsk); +return old; +} + +void exit_files (struct task_struct *tsk) +{ +if (tsk->files) +put_files_struct (switch_files_struct (tsk,NULL )); }struct files_struct init_files = {
@@ -855,10 +862,7 @@ SYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd,* We're done closing the files we were supposed to. Time to install * the new file descriptor table and drop the old one. */ -task_lock (me); - me->files = cur_fds; -task_unlock (me); -put_files_struct (fds); +put_files_struct (switch_files_struct (me, cur_fds)); }return 0 ;
include/linux/fdtable.h +2 -0
@@ -100,6 +100,8 @@ static inline bool close_on_exec(unsigned int fd, const struct files_struct *filstruct task_struct ;void put_files_struct (struct files_struct *fs); +struct files_struct *switch_files_struct (struct task_struct *tsk, +struct files_struct *files);int unshare_files (void );struct fd_range {unsigned int from, to;
kernel/fork.c +2 -4
@@ -3320,10 +3320,8 @@ int ksys_unshare(unsigned long unshare_flags)if (new_fs) new_fs =switch_fs_struct (new_fs); -if (new_fd) { -guard (task_lock)(current); -swap (current->files, new_fd); - } +if (new_fd) + new_fd =switch_files_struct (current, new_fd);if (new_cred) {/* Install the new user namespace */
Signature · 1 line
2.53.0
Re: [PATCH RFC v3 02/15] fs: add switch_files_struct()
Quoted 31 lines · Christian Brauner
Add
switch_files_struct()to install another table on a task. It consumes the reference to the new table and hands the old one back for the caller to put. Convert every place that switches a descriptor table exceptunshare_files().No functional changes.
Signed-off-by Christian Brauner (Amutable) <[email protected]>--- fs/file.c | 28 ++++++++++++++++------------ include/linux/fdtable.h | 2 ++ kernel/fork.c | 6 ++---- 3 files changed, 20 insertions(+), 16 deletions(-)fs/file.c +4
-3@@ -515,16 +515,23 @@ void put_files_struct(struct files_struct *files) } } -void exit_files (struct task_struct *tsk) +/* Install @files on @tsk, consuming the reference; returns the old table. */ +struct files_struct *switch_files_struct (struct task_struct *tsk, +struct files_struct *files) { -struct files_struct * files = tsk->files; +struct files_struct *old; -if (files) {
fs/file.c void put_files_struct(struct files_struct *files) · line 528
Quoted 10 lines
fs/file.c +5
-5-task_lock (tsk); - tsk->files =NULL ; -task_unlock (tsk); -put_files_struct (files); - } +task_lock (tsk); + old = tsk->files; + tsk->files = files; +task_unlock (tsk); +return old;
guard(task_lock)(tsk); swap(tsk->files, files); return files;
???
I saw guard() and swap() in the code you were removing and thought they were worth preserving :-)
NeilBrown
Quoted 56 lines
fs/file.c +7
-4+} + +void exit_files (struct task_struct *tsk) +{ +if (tsk->files) +put_files_struct (switch_files_struct (tsk,NULL )); }struct files_struct init_files = {@@ -855,10 +862,7 @@ SYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd,* We're done closing the files we were supposed to. Time to install * the new file descriptor table and drop the old one. */ -task_lock (me); - me->files = cur_fds; -task_unlock (me); -put_files_struct (fds); +put_files_struct (switch_files_struct (me, cur_fds)); }return 0 ;include/linux/fdtable.h +2
-0@@ -100,6 +100,8 @@ static inline bool close_on_exec(unsigned int fd, const struct files_struct *filstruct task_struct ;void put_files_struct (struct files_struct *fs); +struct files_struct *switch_files_struct (struct task_struct *tsk, +struct files_struct *files);int unshare_files (void );struct fd_range {unsigned int from, to;kernel/fork.c +2
-4@@ -3320,10 +3320,8 @@ int ksys_unshare(unsigned long unshare_flags)if (new_fs) new_fs =switch_fs_struct (new_fs); -if (new_fd) { -guard (task_lock)(current); -swap (current->files, new_fd); - } +if (new_fd) + new_fd =switch_files_struct (current, new_fd);if (new_cred) {/* Install the new user namespace */ Signature · 2 lines
-- 2.53.0
Christian Brauner
maintainerRFC PATCH v3 3/15 fs: move unshare_fd() to fs/file.c
In reply to Christian BraunerMove unshare_fd() where the rest of the descriptor table lifecycle helpers live.
No functional changes.
--- fs/file.c | 18 ++++++++++++++++++ include/linux/fdtable.h | 1 + kernel/fork.c | 18 ------------------ 3 files changed, 19 insertions(+), 18 deletions(-)
fs/file.c +18 -0
@@ -471,6 +471,24 @@ struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_horeturn newf; } +/* +* Unshare file descriptor table if it is being shared +*/ +int unshare_fd (unsigned long unshare_flags,struct files_struct **new_fdp) +{ +struct files_struct *fd = current->files; + +if ((unshare_flags &CLONE_FILES ) && + (fd &&atomic_read (&fd->count) >1 )) { + fd =dup_fd (fd,NULL ); +if (IS_ERR (fd)) +return PTR_ERR (fd); + *new_fdp = fd; + } + +return 0 ; +} +static struct fdtable *close_files (struct files_struct * files) {/*
include/linux/fdtable.h +1 -0
@@ -103,6 +103,7 @@ void put_files_struct(struct files_struct *fs);struct files_struct *switch_files_struct (struct task_struct *tsk,struct files_struct *files);int unshare_files (void ); +int unshare_fd (unsigned long unshare_flags,struct files_struct **new_fdp);struct fd_range {unsigned int from, to; };
kernel/fork.c +0 -18
@@ -3207,24 +3207,6 @@ static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)return 0 ; } -/* -* Unshare file descriptor table if it is being shared -*/ -static int unshare_fd (unsigned long unshare_flags,struct files_struct **new_fdp) -{ -struct files_struct *fd = current->files; - -if ((unshare_flags &CLONE_FILES ) && - (fd &&atomic_read (&fd->count) >1 )) { - fd =dup_fd (fd,NULL ); -if (IS_ERR (fd)) -return PTR_ERR (fd); - *new_fdp = fd; - } - -return 0 ; -} -/* * unshare allows a process to 'unshare' part of the process * context which was originally shared using clone. copy_*
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 4/15 fs: remove unshare_files()
In reply to Christian Braunerexec is the only caller left since commit 433967cab51e ("coredump: stop unsharing the file descriptor table"). All it does is call unshare_fd() with CLONE_FILES and install the copy. Kill the pointless helper and open-code it.
No functional changes.
--- fs/exec.c | 5 ++++- include/linux/fdtable.h | 1 - kernel/fork.c | 24 ------------------------ 3 files changed, 4 insertions(+), 26 deletions(-)
fs/exec.c +4 -1
@@ -1124,6 +1124,7 @@ static struct file *bprm_identity_file(const struct linux_binprm *bprm)int begin_new_exec (struct linux_binprm * bprm) {struct task_struct *me = current; +struct files_struct *files =NULL ;int retval;/* A pending PT_INTERP substitution this format cannot consume. */
@@ -1160,9 +1161,11 @@ int begin_new_exec(struct linux_binprm * bprm)io_uring_task_cancel ();/* Ensure the files table is not shared. */ - retval =unshare_files (); + retval =unshare_fd (CLONE_FILES , &files);if (retval) goto out; +if (files) +put_files_struct (switch_files_struct (me, files));/* * We have to apply CLOEXEC before we change whether the process is
include/linux/fdtable.h +0 -1
@@ -102,7 +102,6 @@ struct task_struct;void put_files_struct (struct files_struct *fs);struct files_struct *switch_files_struct (struct task_struct *tsk,struct files_struct *files); -int unshare_files (void );int unshare_fd (unsigned long unshare_flags,struct files_struct **new_fdp);struct fd_range {unsigned int from, to;
kernel/fork.c +0 -24
@@ -3336,30 +3336,6 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)return ksys_unshare (unshare_flags); } -/* -* Helper to unshare the files of the current task. -* We don't want to expose copy_files internals to -* the exec layer of the kernel. -*/ - -int unshare_files (void ) -{ -struct task_struct *task = current; -struct files_struct *old, *copy =NULL ; -int error; - - error =unshare_fd (CLONE_FILES , ©); -if (error || !copy) -return error; - - old = task->files; -task_lock (task); - task->files = copy; -task_unlock (task); -put_files_struct (old); -return 0 ; -} -static int sysctl_max_threads (const struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) {
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 5/15 fs: move filp_flush() to other file helpers
In reply to Christian BraunerVarious functions will flush and put each file inline the way close(2) does in follow-up patches. Drop the static and add a prototype next to file_close_fd_locked().
--- fs/internal.h | 1 + fs/open.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
fs/internal.h +1 -0
@@ -198,6 +198,7 @@ extern struct file *do_file_open_root(const struct path *,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);struct file *file_close_fd_locked (struct files_struct *files,unsigned fd); +int filp_flush (struct file *filp,fl_owner_t id);int do_ftruncate (struct file *file,loff_t length,unsigned int flags);int chmod_common (const struct path *path,umode_t mode);
fs/open.c +1 -1
@@ -1506,7 +1506,7 @@ SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)* "id" is the POSIX thread ID. We use the * files pointer for this.. */ -static int filp_flush (struct file *filp,fl_owner_t id) +int filp_flush (struct file *filp,fl_owner_t id) {int retval =0 ;
Signature · 1 line
2.53.0
Re: [PATCH RFC v3 05/15] fs: move filp_flush() to other file helpers
Quoted 3 lines · Christian Brauner
Various functions will flush and put each file inline the way close(2) does in follow-up patches. Drop the static and add a prototype next to
file_close_fd_locked().
As every caller of fput_close_sync() will be preceded by a call to filp_flush(), would it not make more sense to hav fput_close_sync() call filp_flush() itself? And keep it static.
Less room for error that way.
Thanks,
NeilBrown
Quoted 37 lines
Signed-off-by Christian Brauner (Amutable) <[email protected]>--- fs/internal.h | 1 + fs/open.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)fs/internal.h +1
-0@@ -198,6 +198,7 @@ extern struct file *do_file_open_root(const struct path *,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);struct file *file_close_fd_locked (struct files_struct *files,unsigned fd); +int filp_flush (struct file *filp,fl_owner_t id);int do_ftruncate (struct file *file,loff_t length,unsigned int flags);int chmod_common (const struct path *path,umode_t mode);fs/open.c +1
-1@@ -1506,7 +1506,7 @@ SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)* "id" is the POSIX thread ID. We use the * files pointer for this.. */ -static int filp_flush (struct file *filp,fl_owner_t id) +int filp_flush (struct file *filp,fl_owner_t id) {int retval =0 ;Signature · 2 lines
-- 2.53.0
Christian Brauner
maintainerRFC PATCH v3 6/15 fs: make close_files() synchronous
In reply to Christian BraunerWhen the last reference to a descriptor table is dropped close_files() closes every file but punts the actual work to task work. For an exiting task that task work only runs in exit_task_work().
Before commit 4a9d4b024a31 ("switch fput to task_work_add") fput() was synchronous everywhere and exit released its files in exit_files().
The deferral made fput() safe from any context. And exit_files() offloaded to task work as a side-effect. And that has downsides.
Oleg and Neil noticed that some time ago. A task that exits with a big descriptor table ends up queueing a very large number of files on task work. That leaves a list for any later task_work_cancel() to search under ->pi_lock and costs a lot of atomics too.
Let close_files() close right away. Flush and put each file inline the way close(2) does. The final __fput() runs during the table walk now instead of from task_work_run() in exit_task_work(). One difference is the order: task work ran the final __fput()s in reverse and now they run in table order.
Every put of a dying table is synchronous now:
- exit_files()
- copy_process()
- close_range(CLOSE_RANGE_UNSHARE)
- unshare(2)
- exec
Kernel threads don't own a file descriptor table and exec already splats were they to exec. kthreadd and every kthread share init_files and init_task pins that forever.
--- fs/file.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/file.c +3 -2
@@ -489,7 +489,7 @@ int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)return 0 ; } -static struct fdtable *close_files (struct files_struct * files) +static struct fdtable *close_files (struct files_struct *files) {/* * It is safe to dereference the fd table without RCU or
@@ -509,7 +509,8 @@ static struct fdtable *close_files(struct files_struct * files)if (set &1 ) {struct file *file = fdt->fd[i];if (file) { -filp_close (file, files); +filp_flush (file, files); +fput_close_sync (file);cond_resched (); } }
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 7/15 fs: make close_range() synchronous
In reply to Christian Brauner__range_close() closes through filp_close() so every file the caller held the last reference to is punted to task work. That costs one cmpxchg per file plus a list entry for any later task_work_cancel() to search under ->pi_lock. close_range(2) exists to close many descriptors in one go fast. So convert it to the same synchronous treatment as close(2) and close_files().
Flush and put each file inline while ->file_lock is dropped. close_range(2) now behaves like close(2).
--- fs/file.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/file.c +2 -1
@@ -813,7 +813,8 @@ static inline void __range_close(struct files_struct *files, unsigned int fd, file =file_close_fd_locked (files, fd);if (file) {spin_unlock (&files->file_lock); -filp_close (file, files); +filp_flush (file, files); +fput_close_sync (file);cond_resched ();spin_lock (&files->file_lock); fdt =files_fdtable (files);
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 8/15 fs: rename do_close_on_exec() to close_cloexec_files()
In reply to Christian BraunerRename the helper and align it with close_files().
No functional changes.
--- fs/exec.c | 2 +- fs/file.c | 2 +- include/linux/fdtable.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
fs/exec.c +1 -1
@@ -1179,7 +1179,7 @@ int begin_new_exec(struct linux_binprm * bprm)* This must happen after the point of no return, and after unsharing * the FD table. */ -do_close_on_exec (me->files); +close_cloexec_files (me->files);/* * Must be called _before_ exec_mmap() as bprm->mm is
fs/file.c +1 -1
@@ -908,7 +908,7 @@ struct file *file_close_fd(unsigned int fd)return file; } -void do_close_on_exec (struct files_struct *files) +void close_cloexec_files (struct files_struct *files) {unsigned i;struct fdtable *fdt;
include/linux/fdtable.h +1 -1
@@ -107,7 +107,7 @@ struct fd_range {unsigned int from, to; };struct files_struct *dup_fd (struct files_struct *,struct fd_range *) __latent_entropy; -void do_close_on_exec (struct files_struct *); +void close_cloexec_files (struct files_struct *);int iterate_fd (struct files_struct *,unsigned ,int (*)(const void *,struct file *,unsigned ),const void *);
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 9/15 fs: make close_cloexec_files() synchronous
In reply to Christian BraunerPunting file closing to task work during exec slows down exec significantly when its done with a bunch of file descriptors. We can do this in-band instead. Flush already runs synchronous. Jann moved close-on-exec in e780259b54e6 ("exec: do_close_on_exec() before taking exec_update_lock") outside of exec_update_lock.
The only lock that's still held now is cred_guard_mutex. It's deprecated and has five takers
(1) exec
(2) ptrace_attach()
(3) seccomp() with SECCOMP_FILTER_FLAG_TSYNC
(4) writes to /proc/<pid>/attr/*
(5) lsm_set_self_attr()
Four of them take the task's own cred_guard_mutex. When close_cloexec_files() runs de_thread() ensured that the calling task is the only one alive in its thread-group. That leaves ptrace() waiting on cred_guard_mutex of the tracee going through exec. exec already sleeps under cred_guard_mutex in de_thread() when it reads binary and interpreter. So while we add wait-time to an attaching ptracer no new lock dependency is added. vfork() als waits but that's a dup_fd() copy of the fdtable and rarely holds the last reference. If that's an issue we can always change that later.
--- fs/exec.c | 6 +++--- fs/file.c | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-)
fs/exec.c +3 -3
@@ -1173,9 +1173,9 @@ int begin_new_exec(struct linux_binprm * bprm)* trying to access the should-be-closed file descriptors of a process * undergoing exec(2). * -* This can block on filesystem ->flush() handlers, including waiting -* for FUSE daemons, so do it before exec_mmap takes the -* exec_update_lock. +* This can block on filesystem ->flush() and ->release() handlers, +* including waiting for FUSE daemons, so do it before exec_mmap +* takes the exec_update_lock. * This must happen after the point of no return, and after unsharing * the FD table. */
fs/file.c +2 -1
@@ -935,7 +935,8 @@ void close_cloexec_files(struct files_struct *files)rcu_assign_pointer (fdt->fd[fd],NULL );__put_unused_fd (files, fd);spin_unlock (&files->file_lock); -filp_close (file, files); +filp_flush (file, files); +fput_close_sync (file);cond_resched ();spin_lock (&files->file_lock); }
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 10/15 coredump: s/startup/done/g
In reply to Christian BraunerWe're going to use that in a bit. Rename it to something easier to understand.
No functional changes.
--- fs/coredump.c | 4 ++-- include/linux/sched/signal.h | 3 ++- kernel/exit.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-)
fs/coredump.c +2 -2
@@ -523,7 +523,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)struct task_struct *tsk = current;int core_waiters = -EBUSY ; -init_completion (&core_state->startup); +init_completion (&core_state->done); core_state->dumper.task = tsk; core_state->dumper.next =NULL ;
@@ -531,7 +531,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)if (core_waiters >0 ) {struct core_thread *ptr; -wait_for_completion_state (&core_state->startup, +wait_for_completion_state (&core_state->done,TASK_UNINTERRUPTIBLE |TASK_FREEZABLE );/* * Wait for all the threads to become inactive, so that
include/linux/sched/signal.h +2 -1
@@ -81,7 +81,8 @@ struct core_thread {struct core_state {atomic_t nr_threads;struct core_thread dumper; -struct completion startup; +/* Completed by the last thread to park. */ +struct completion done; };/*
kernel/exit.c +1 -1
@@ -443,7 +443,7 @@ static void coredump_task_exit(struct task_struct *tsk,* to core_state->dumper. */ if (atomic_dec_and_test (&core_state->nr_threads)) -complete (&core_state->startup); +complete (&core_state->done);for (;;) {set_current_state (TASK_IDLE |TASK_FREEZABLE );
Signature · 1 line
2.53.0
Re: [PATCH RFC v3 10/15] coredump: s/startup/done/g
Quoted 39 lines · Christian Brauner
We're going to use that in a bit. Rename it to something easier to understand.
No functional changes.
Signed-off-by Christian Brauner (Amutable) <[email protected]>--- fs/coredump.c | 4 ++-- include/linux/sched/signal.h | 3 ++- kernel/exit.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-)fs/coredump.c +2
-2@@ -523,7 +523,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)struct task_struct *tsk = current;int core_waiters = -EBUSY ; -init_completion (&core_state->startup); +init_completion (&core_state->done); core_state->dumper.task = tsk; core_state->dumper.next =NULL ;@@ -531,7 +531,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)if (core_waiters >0 ) {struct core_thread *ptr; -wait_for_completion_state (&core_state->startup, +wait_for_completion_state (&core_state->done,TASK_UNINTERRUPTIBLE |TASK_FREEZABLE );/* * Wait for all the threads to become inactive, so that include/linux/sched/signal.h +0
-0
include/linux/sched/signal.h struct core_thread { · line 85
Quoted 6 lines
include/linux/sched/signal.h +2
-1@@ -81,7 +81,8 @@ struct core_thread {struct core_state {atomic_t nr_threads;struct core_thread dumper; -struct completion startup; +/* Completed by the last thread to park. */ +struct completion done;
It isn't clear that "done" is a whole lot better than "startup". "What is done?" is the same question as "what did we start up?". Given the comment (which is the best part of this patch), maybe call the completion "all_parked". But "parked" make my think of kthreads which can be parked and unparked ... but maybe that is just me.
It appears the wait is for all tasks to have registered themselves on the core_state->dumper list of tasks. Why is the list called "dumper"? Maybe the first task on the list is the task which will perform the dump? Blending the two concepts doesn't aid clarity... I was thinking of referencing the list in the name of the completion, but that might be confusing.
I'm leaning towards "threads_listed" as a good name.
But naming is hard...
NeilBrown
Quoted 21 lines
include/linux/sched/signal.h +0
-0};/* kernel/exit.c +1
-1@@ -443,7 +443,7 @@ static void coredump_task_exit(struct task_struct *tsk,* to core_state->dumper. */ if (atomic_dec_and_test (&core_state->nr_threads)) -complete (&core_state->startup); +complete (&core_state->done);for (;;) {set_current_state (TASK_IDLE |TASK_FREEZABLE );Signature · 2 lines
-- 2.53.0
Christian Brauner
maintainerRFC PATCH v3 11/15 coredump: factor out coredump_wait_inactive()
In reply to Christian BraunerFactor out a new coredump_wait_inactive() helper that COREDUMP_CLOSE_FILES can consume in a bit.
No functional changes.
--- fs/coredump.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-)
fs/coredump.c +20 -16
@@ -518,6 +518,24 @@ static int zap_threads(struct task_struct *tsk,return nr; } +static void coredump_wait_inactive (struct core_state *core_state) +{ +struct core_thread *ptr; + +wait_for_completion_state (&core_state->done, +TASK_UNINTERRUPTIBLE |TASK_FREEZABLE ); +/* +* Wait for all the threads to become inactive, so that +* all the thread context (extended register state, like +* fpu etc) gets copied to the memory. +*/ + ptr = core_state->dumper.next; +while (ptr !=NULL ) { +wait_task_inactive (ptr->task,TASK_ANY ); + ptr = ptr->next; + } +} +static int coredump_wait (int exit_code,struct core_state *core_state) {struct task_struct *tsk = current;
@@ -528,22 +546,8 @@ static int coredump_wait(int exit_code, struct core_state *core_state) core_state->dumper.next =NULL ; core_waiters =zap_threads (tsk, core_state, exit_code); -if (core_waiters >0 ) { -struct core_thread *ptr; - -wait_for_completion_state (&core_state->done, -TASK_UNINTERRUPTIBLE |TASK_FREEZABLE ); -/* -* Wait for all the threads to become inactive, so that -* all the thread context (extended register state, like -* fpu etc) gets copied to the memory. -*/ - ptr = core_state->dumper.next; -while (ptr !=NULL ) { -wait_task_inactive (ptr->task,TASK_ANY ); - ptr = ptr->next; - } - } +if (core_waiters >0 ) +coredump_wait_inactive (core_state);return core_waiters; }
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 12/15 fs: add alloc_files_struct()
In reply to Christian BraunerAdd a way to get an empty descriptor table with one reference. Let dup_fd() share the allocation. The coredump code will use it too in a bit.
No functional changes.
--- fs/file.c | 49 ++++++++++++++++++++++++++++++++++++++----------- include/linux/fdtable.h | 1 + 2 files changed, 39 insertions(+), 11 deletions(-)
fs/file.c +38 -11
@@ -375,21 +375,15 @@ static unsigned int sane_fdtable_size(struct fdtable *fdt, struct fd_range *puncreturn ALIGN (last +1 ,BITS_PER_LONG ); } -/* -* Allocate a new descriptor table and copy contents from the passed in -* instance. Returns a pointer to cloned table on success, ERR_PTR() -* on failure. For 'punch_hole' see sane_fdtable_size(). -*/ -struct files_struct *dup_fd (struct files_struct *oldf,struct fd_range *punch_hole) +/* A table with one reference and the embedded fdtable, nothing copied yet. */ +static struct files_struct *alloc_files (gfp_t gfp) {struct files_struct *newf; -struct file **old_fds, **new_fds; -unsigned int open_files, i; -struct fdtable *old_fdt, *new_fdt; +struct fdtable *new_fdt; - newf =kmem_cache_alloc (files_cachep,GFP_KERNEL ); + newf =kmem_cache_alloc (files_cachep, gfp);if (!newf) -return ERR_PTR (-ENOMEM ); +return NULL ;atomic_set (&newf->count,1 );
@@ -404,6 +398,39 @@ struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_ho new_fdt->full_fds_bits = newf->full_fds_bits_init; new_fdt->fd = &newf->fd_array[0 ]; +return newf; +} + +/* An empty descriptor table with one reference. */ +struct files_struct *alloc_files_struct (void ) +{ +struct files_struct *newf; + + newf =alloc_files (GFP_KERNEL | __GFP_ZERO); +if (!newf) +return NULL ; + +rcu_assign_pointer (newf->fdt, &newf->fdtab); +return newf; +} + +/* +* Allocate a new descriptor table and copy contents from the passed in +* instance. Returns a pointer to cloned table on success, ERR_PTR() +* on failure. For 'punch_hole' see sane_fdtable_size(). +*/ +struct files_struct *dup_fd (struct files_struct *oldf,struct fd_range *punch_hole) +{ +struct files_struct *newf; +struct file **old_fds, **new_fds; +unsigned int open_files, i; +struct fdtable *old_fdt, *new_fdt; + + newf =alloc_files (GFP_KERNEL ); +if (!newf) +return ERR_PTR (-ENOMEM ); + new_fdt = &newf->fdtab; +spin_lock (&oldf->file_lock); old_fdt =files_fdtable (oldf); open_files =sane_fdtable_size (old_fdt, punch_hole);
include/linux/fdtable.h +1 -0
@@ -100,6 +100,7 @@ static inline bool close_on_exec(unsigned int fd, const struct files_struct *filstruct task_struct ;void put_files_struct (struct files_struct *fs); +struct files_struct *alloc_files_struct (void );struct files_struct *switch_files_struct (struct task_struct *tsk,struct files_struct *files);int unshare_fd (unsigned long unshare_flags,struct files_struct **new_fdp);
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 13/15 coredump: add COREDUMP_CLOSE_FILES
In reply to Christian BraunerAdd COREDUMP_CLOSE_FILES and allow a coredump server to request that the thread-group closes all files before creating the coredump.
There have been several attempts to let the dumping process decide through a new fcntl() flag, a new procfs file or a new coredump_filter bit that its descriptors go away early. That's just broken imho.
Tools like systemd-coredump walk /proc/<pid>/fd and /proc/<pid>/fdinfo and some use pidfd_getfd() to preserve files of the crashing process. Only the coredump server knows whether it still needs the descriptors.
So let the coredump server ask for it. Add a new COREDUMP_CLOSE_FILES feature bit. If the coredump server raises it the kernel drops the descriptor tables of the thread group right after the handshake and before it generates the coredump.
COREDUMP_CLOSE_FILES doesn't work with COREDUMP_REJECT. A rejected task exits and closes everything right away anyway.
We switch to an empty fdtable instead of simply clearing because io_uring_files_cancel() runs task work in do_exit() before exit_signals() sets PF_EXITING and may reissue requests that dereference current->files.
--- fs/coredump.c | 55 ++++++++++++++++++++++++++++++++++++++++++- include/linux/sched/signal.h | 4 +++- include/uapi/linux/coredump.h | 8 +++++++ kernel/exit.c | 13 ++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-)
fs/coredump.c +54 -1
@@ -552,6 +552,48 @@ static int coredump_wait(int exit_code, struct core_state *core_state)return core_waiters; } +/* +* Allocate a new empty fdtable and switch the whole thread-group to it. +* Put all the old fdtables freeing up resources and locks before writing the +* coredump. +*/ +static bool coredump_close_files (struct core_state *core_state) +{ +struct files_struct *files; +struct core_thread *ct; +int nr =0 ; + + files =alloc_files_struct (); +if (!files) +return false; + +/* Tasks without a table such as vhost workers can be skipped. */ +for (ct = core_state->dumper.next; ct; ct = ct->next) +if (ct->task->files) + nr++; +atomic_set (&core_state->nr_threads, nr); +reinit_completion (&core_state->done); + +for (ct = core_state->dumper.next; ct; ct = ct->next) { +if (!ct->task->files) +continue ; +/* ct->files holds a reference until the thread switches to it. */ +atomic_inc (&files->count); +/* Pairs with the acquire in coredump_task_exit(). */ +smp_store_release (&ct->files, files); +wake_up_process (ct->task); + } + +/* Use the dumper's real creds not the overriden ones. */ +scoped_with_creds (current_real_cred()) +put_files_struct (switch_files_struct(current ,files )); + +if (nr) +coredump_wait_inactive (core_state); + +return true; +} +static void coredump_finish (enum coredump_state state) {struct core_thread *curr, *next;
@@ -841,7 +883,8 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params * .mask = COREDUMP_KERNEL | COREDUMP_USERSPACE |COREDUMP_REJECT |COREDUMP_WAIT |COREDUMP_RECORDS |COREDUMP_SPARSE | - COREDUMP_MEMORY_TYPES, +COREDUMP_MEMORY_TYPES | + COREDUMP_CLOSE_FILES, .size_ack = sizeof(struct coredump_ack), .memory_types = cprm->memory_types, .memory_types_mask = COREDUMP_MEMORY_ALL,
@@ -909,6 +952,12 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params *return false; } +/* A rejected task exits right away and closes everything anyway. */ +if ((ack.mask &COREDUMP_CLOSE_FILES ) && (ack.mask &COREDUMP_REJECT )) { +coredump_sock_mark (cprm->file,COREDUMP_MARK_CONFLICTING ); +return false; + } +if (ack.mask &COREDUMP_MEMORY_TYPES ) {/* The memory types need the whole field. */ if (usize <COREDUMP_ACK_SIZE_VER1 ) {
@@ -1224,6 +1273,10 @@ static void do_coredump(struct core_name *cn, struct coredump_params *cprm,if (cprm->mask &COREDUMP_REJECT )return ; +if ((cprm->mask &COREDUMP_CLOSE_FILES ) && + !coredump_close_files (current->signal->core_state)) +return ; +if ((cprm->mask &COREDUMP_KERNEL ) && !coredump_write (cprm, binfmt))return ;
include/linux/sched/signal.h +3 -1
@@ -76,12 +76,14 @@ struct multiprocess_signals {struct core_thread {struct task_struct *task;struct core_thread *next; +/* The empty table to switch to, published by the dumping thread. */ +struct files_struct *files; };struct core_state {atomic_t nr_threads;struct core_thread dumper; -/* Completed by the last thread to park. */ +/* Completed by the last thread to park, reused for the table switch. */ struct completion done; };
include/uapi/linux/coredump.h +8 -0
@@ -19,6 +19,9 @@* @COREDUMP_MEMORY_TYPES: dump the memory types in * coredump_ack->memory_types instead of the ones * the task selected; requires COREDUMP_KERNEL +* @COREDUMP_CLOSE_FILES: close all file descriptors of the task before the +* coredump is generated; incompatible with +* COREDUMP_REJECT */ enum {COREDUMP_KERNEL = (1ULL <<0 ),
@@ -28,6 +31,7 @@ enum {COREDUMP_RECORDS = (1ULL <<4 ),COREDUMP_SPARSE = (1ULL <<5 ),COREDUMP_MEMORY_TYPES = (1ULL <<6 ), + COREDUMP_CLOSE_FILES = (1ULL << 7), };/**
@@ -137,6 +141,10 @@ enum {* Note that @memory_types must be zero if COREDUMP_MEMORY_TYPES isn't * raised. COREDUMP_MEMORY_TYPES requires COREDUMP_KERNEL and an ack of * at least COREDUMP_ACK_SIZE_VER1 bytes. +* +* If COREDUMP_CLOSE_FILES is raised in @mask the kernel closes the file +* descriptors of the coredumping task before it generates the coredump. +* The task ends up with an empty descriptor table. */ struct coredump_ack {__u32 size;
kernel/exit.c +13 -0
@@ -25,6 +25,7 @@#include <linux/acct.h> #include <linux/tsacct_kern.h> #include <linux/file.h> +#include <linux/fdtable.h> #include <linux/freezer.h> #include <linux/binfmts.h> #include <linux/nsproxy.h>
@@ -431,9 +432,11 @@ kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)static void coredump_task_exit (struct task_struct *tsk,struct core_state *core_state) { +struct files_struct *files;struct core_thread self; self.task = tsk; + self.files =NULL ;if (self.task->flags &PF_SIGNALED ) self.next =xchg (&core_state->dumper.next, &self);else
@@ -449,6 +452,16 @@ static void coredump_task_exit(struct task_struct *tsk,set_current_state (TASK_IDLE |TASK_FREEZABLE );if (!self.task)/* see coredump_finish() */ break ; +/* Pairs with the release in coredump_close_files(). */ + files =smp_load_acquire (&self.files); +if (files) { +__set_current_state (TASK_RUNNING ); + self.files =NULL ; +put_files_struct (switch_files_struct (tsk, files)); +if (atomic_dec_and_test (&core_state->nr_threads)) +complete (&core_state->done); +continue ; + }schedule (); }__set_current_state (TASK_RUNNING );
Signature · 1 line
2.53.0
Re: [PATCH RFC v3 13/15] coredump: add COREDUMP_CLOSE_FILES
Quoted 129 lines · Christian Brauner
Add
COREDUMP_CLOSE_FILESand allow a coredump server to request that the thread-group closes all files before creating the coredump.There have been several attempts to let the dumping process decide through a new
fcntl()flag, a new procfs file or a newcoredump_filterbit that its descriptors go away early. That's just broken imho.Tools like systemd-coredump walk /proc/<pid>/fd and /proc/<pid>/fdinfo and some use
pidfd_getfd()to preserve files of the crashing process. Only the coredump server knows whether it still needs the descriptors.So let the coredump server ask for it. Add a new
COREDUMP_CLOSE_FILESfeature bit. If the coredump server raises it the kernel drops the descriptor tables of the thread group right after the handshake and before it generates the coredump.
COREDUMP_CLOSE_FILESdoesn't work withCOREDUMP_REJECT. A rejected task exits and closes everything right away anyway.We switch to an empty fdtable instead of simply clearing because
io_uring_files_cancel()runs task work indo_exit()beforeexit_signals()setsPF_EXITINGand may reissue requests that dereferencecurrent->files.Reported-by Xin Zhao <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>--- fs/coredump.c | 55 ++++++++++++++++++++++++++++++++++++++++++- include/linux/sched/signal.h | 4 +++- include/uapi/linux/coredump.h | 8 +++++++ kernel/exit.c | 13 ++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-)fs/coredump.c +54
-1@@ -552,6 +552,48 @@ static int coredump_wait(int exit_code, struct core_state *core_state)return core_waiters; } +/* +* Allocate a new empty fdtable and switch the whole thread-group to it. +* Put all the old fdtables freeing up resources and locks before writing the +* coredump. +*/ +static bool coredump_close_files (struct core_state *core_state) +{ +struct files_struct *files; +struct core_thread *ct; +int nr =0 ; + + files =alloc_files_struct (); +if (!files) +return false; + +/* Tasks without a table such as vhost workers can be skipped. */ +for (ct = core_state->dumper.next; ct; ct = ct->next) +if (ct->task->files) + nr++; +atomic_set (&core_state->nr_threads, nr); +reinit_completion (&core_state->done); + +for (ct = core_state->dumper.next; ct; ct = ct->next) { +if (!ct->task->files) +continue ; +/* ct->files holds a reference until the thread switches to it. */ +atomic_inc (&files->count); +/* Pairs with the acquire in coredump_task_exit(). */ +smp_store_release (&ct->files, files); +wake_up_process (ct->task); + } + +/* Use the dumper's real creds not the overriden ones. */ +scoped_with_creds (current_real_cred()) +put_files_struct (switch_files_struct(current ,files )); + +if (nr) +coredump_wait_inactive (core_state); + +return true; +} +static void coredump_finish (enum coredump_state state) {struct core_thread *curr, *next;@@ -841,7 +883,8 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params * .mask = COREDUMP_KERNEL | COREDUMP_USERSPACE |COREDUMP_REJECT |COREDUMP_WAIT |COREDUMP_RECORDS |COREDUMP_SPARSE | - COREDUMP_MEMORY_TYPES, +COREDUMP_MEMORY_TYPES | + COREDUMP_CLOSE_FILES, .size_ack = sizeof(struct coredump_ack), .memory_types = cprm->memory_types, .memory_types_mask = COREDUMP_MEMORY_ALL,@@ -909,6 +952,12 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params *return false; } +/* A rejected task exits right away and closes everything anyway. */ +if ((ack.mask &COREDUMP_CLOSE_FILES ) && (ack.mask &COREDUMP_REJECT )) { +coredump_sock_mark (cprm->file,COREDUMP_MARK_CONFLICTING ); +return false; + } +if (ack.mask &COREDUMP_MEMORY_TYPES ) {/* The memory types need the whole field. */ if (usize <COREDUMP_ACK_SIZE_VER1 ) {@@ -1224,6 +1273,10 @@ static void do_coredump(struct core_name *cn, struct coredump_params *cprm,if (cprm->mask &COREDUMP_REJECT )return ; +if ((cprm->mask &COREDUMP_CLOSE_FILES ) && + !coredump_close_files (current->signal->core_state)) +return ; +if ((cprm->mask &COREDUMP_KERNEL ) && !coredump_write (cprm, binfmt))return ;include/linux/sched/signal.h +0
-0@@ -76,12 +76,14 @@ struct multiprocess_signals {struct core_thread {struct task_struct *task;struct core_thread *next;
include/linux/sched/signal.h struct multiprocess_signals { · line 87
Quoted 10 lines
include/linux/sched/signal.h +3
-1+/* The empty table to switch to, published by the dumping thread. */ +struct files_struct *files; };struct core_state {atomic_t nr_threads;struct core_thread dumper; -/* Completed by the last thread to park. */ +/* Completed by the last thread to park, reused for the table switch. */ struct completion done;
Ok, now I see why you chose the bland "done".
I would discard the completion altogether.
Rename "nr_threads" to "threads_remaining".
Use atomic_dec_and_wake_up(&core_state.threads_remaining), and wait_var_event(atomic_read(core_state.threads_remaining) == 0).
And please - can "dumper" become a "struct task_struct *" and have a separate "struct core_thread *threads".
Thanks,
NeilBrown
Quoted 81 lines
include/linux/sched/signal.h +0
-0};include/uapi/linux/coredump.h +8
-0@@ -19,6 +19,9 @@* @COREDUMP_MEMORY_TYPES: dump the memory types in * coredump_ack->memory_types instead of the ones * the task selected; requires COREDUMP_KERNEL +* @COREDUMP_CLOSE_FILES: close all file descriptors of the task before the +* coredump is generated; incompatible with +* COREDUMP_REJECT */ enum {COREDUMP_KERNEL = (1ULL <<0 ),@@ -28,6 +31,7 @@ enum {COREDUMP_RECORDS = (1ULL <<4 ),COREDUMP_SPARSE = (1ULL <<5 ),COREDUMP_MEMORY_TYPES = (1ULL <<6 ), + COREDUMP_CLOSE_FILES = (1ULL << 7), };/** @@ -137,6 +141,10 @@ enum {* Note that @memory_types must be zero if COREDUMP_MEMORY_TYPES isn't * raised. COREDUMP_MEMORY_TYPES requires COREDUMP_KERNEL and an ack of * at least COREDUMP_ACK_SIZE_VER1 bytes. +* +* If COREDUMP_CLOSE_FILES is raised in @mask the kernel closes the file +* descriptors of the coredumping task before it generates the coredump. +* The task ends up with an empty descriptor table. */ struct coredump_ack {__u32 size;kernel/exit.c +13
-0@@ -25,6 +25,7 @@#include <linux/acct.h> #include <linux/tsacct_kern.h> #include <linux/file.h> +#include <linux/fdtable.h> #include <linux/freezer.h> #include <linux/binfmts.h> #include <linux/nsproxy.h> @@ -431,9 +432,11 @@ kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)static void coredump_task_exit (struct task_struct *tsk,struct core_state *core_state) { +struct files_struct *files;struct core_thread self; self.task = tsk; + self.files =NULL ;if (self.task->flags &PF_SIGNALED ) self.next =xchg (&core_state->dumper.next, &self);else @@ -449,6 +452,16 @@ static void coredump_task_exit(struct task_struct *tsk,set_current_state (TASK_IDLE |TASK_FREEZABLE );if (!self.task)/* see coredump_finish() */ break ; +/* Pairs with the release in coredump_close_files(). */ + files =smp_load_acquire (&self.files); +if (files) { +__set_current_state (TASK_RUNNING ); + self.files =NULL ; +put_files_struct (switch_files_struct (tsk, files)); +if (atomic_dec_and_test (&core_state->nr_threads)) +complete (&core_state->done); +continue ; + }schedule (); }__set_current_state (TASK_RUNNING );Signature · 2 lines
-- 2.53.0
Re: [PATCH RFC v3 13/15] coredump: add COREDUMP_CLOSE_FILES
Quoted 3 lines · NeilBrown
And please - can "dumper" become a "struct
task_struct*" and have a separate "structcore_thread*threads".
It turns out that "dumper" is only ever used to get "dumper.next" so we don't need that "dumper" at all.
NeilBrown
fs/binfmt_elf.c +1 -1
@@ -1875,7 +1875,7 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,return 0 ; info->thread->task = dump_task; -for (ct = dump_task->signal->core_state->dumper.next; ct; ct = ct->next) { +for (ct = dump_task->signal->core_state->tasks; ct; ct = ct->next) { t =kzalloc_flex (*t, notes, info->thread_notes);if (unlikely (!t))return 0 ;
fs/binfmt_elf_fdpic.c +1 -1
@@ -1504,7 +1504,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)if (!psinfo) goto end_coredump; -for (ct = current->signal->core_state->dumper.next; +for (ct = current->signal->core_state->tasks; ct; ct = ct->next) { tmp =elf_dump_thread_status (cprm->siginfo->si_signo, ct->task, &thread_status_size);
fs/coredump.c +3 -4
@@ -526,8 +526,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)int core_waiters = -EBUSY ;init_completion (&core_state->startup); - core_state->dumper.task = tsk; - core_state->dumper.next =NULL ; + core_state->tasks =NULL ; core_waiters =zap_threads (tsk, core_state, exit_code);if (core_waiters >0 ) {
@@ -540,7 +539,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)* all the thread context (extended register state, like * fpu etc) gets copied to the memory. */ - ptr = core_state->dumper.next; + ptr = core_state->tasks;while (ptr !=NULL ) {wait_task_inactive (ptr->task,TASK_ANY ); ptr = ptr->next;
@@ -558,7 +557,7 @@ static void coredump_finish(bool core_dumped)spin_lock_irq (¤t->sighand->siglock);if (core_dumped && !__fatal_signal_pending (current)) current->signal->group_exit_code |=0x80 ; - next = current->signal->core_state->dumper.next; + next = current->signal->core_state->tasks; current->signal->core_state =NULL ;spin_unlock_irq (¤t->sighand->siglock);
include/linux/sched/signal.h +1 -1
@@ -80,7 +80,7 @@ struct core_thread {struct core_state {atomic_t nr_threads; -struct core_thread dumper; +struct core_thread *tasks;struct completion startup; };
kernel/exit.c +2 -2
@@ -432,12 +432,12 @@ static void coredump_task_exit(struct task_struct *tsk, self.task = tsk;if (self.task->flags &PF_SIGNALED ) - self.next =xchg (&core_state->dumper.next, &self); + self.next =xchg (&core_state->tasks, &self);else self.task =NULL ;/* * Implies mb(), the result of xchg() must be visible -* to core_state->dumper. +* to the dumper. */ if (atomic_dec_and_test (&core_state->nr_threads))complete (&core_state->startup);
Christian Brauner
maintainerRe: [PATCH RFC v3 13/15] coredump: add COREDUMP_CLOSE_FILES
Quoted 6 lines · NeilBrown
Quoted 3 lines · NeilBrown
And please - can "dumper" become a "struct
task_struct*" and have a separate "structcore_thread*threads".It turns out that "dumper" is only ever used to get "dumper.next" so we
Yeah, I went spelunking and it was historically used to ensure that the dumping thread came first in elf notes but that got reworked so it's been unused for a long time.
Thanks for the review. This was really helpful and made this even simpler. I've taken everything you commented on. New version out soon.
Christian Brauner
maintainerRFC PATCH v3 14/15 tools: sync coredump.h header
In reply to Christian BraunerSync the headers for the selftests.
--- tools/include/uapi/linux/coredump.h | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/include/uapi/linux/coredump.h +8 -0
@@ -19,6 +19,9 @@* @COREDUMP_MEMORY_TYPES: dump the memory types in * coredump_ack->memory_types instead of the ones * the task selected; requires COREDUMP_KERNEL +* @COREDUMP_CLOSE_FILES: close all file descriptors of the task before the +* coredump is generated; incompatible with +* COREDUMP_REJECT */ enum {COREDUMP_KERNEL = (1ULL <<0 ),
@@ -28,6 +31,7 @@ enum {COREDUMP_RECORDS = (1ULL <<4 ),COREDUMP_SPARSE = (1ULL <<5 ),COREDUMP_MEMORY_TYPES = (1ULL <<6 ), + COREDUMP_CLOSE_FILES = (1ULL << 7), };/**
@@ -137,6 +141,10 @@ enum {* Note that @memory_types must be zero if COREDUMP_MEMORY_TYPES isn't * raised. COREDUMP_MEMORY_TYPES requires COREDUMP_KERNEL and an ack of * at least COREDUMP_ACK_SIZE_VER1 bytes. +* +* If COREDUMP_CLOSE_FILES is raised in @mask the kernel closes the file +* descriptors of the coredumping task before it generates the coredump. +* The task ends up with an empty descriptor table. */ struct coredump_ack {__u32 size;
Signature · 1 line
2.53.0
Christian Brauner
maintainerRFC PATCH v3 15/15 selftests/coredump: test COREDUMP_CLOSE_FILES
In reply to Christian BraunerTest COREDUMP_CLOSE_FILES.
--- tools/testing/selftests/coredump/Makefile | 4 +- .../selftests/coredump/coredump_close_files_test.c | 592 +++++++++++++++++++++ .../coredump/coredump_socket_protocol_test.c | 6 + .../selftests/coredump/coredump_test_helpers.c | 3 +- 4 files changed, 603 insertions(+), 2 deletions(-)
tools/testing/selftests/coredump/Makefile +3 -1
@@ -3,7 +3,8 @@ CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) TEST_GEN_PROGS := stackdump_test \ coredump_socket_test \ - coredump_socket_protocol_test + coredump_socket_protocol_test \ + coredump_close_files_test TEST_FILES := stackdump include ../lib.mk
@@ -11,3 +12,4 @@ include ../lib.mk $(OUTPUT)/stackdump_test: coredump_test_helpers.c $(OUTPUT)/coredump_socket_test: coredump_test_helpers.c $(OUTPUT)/coredump_socket_protocol_test: coredump_test_helpers.c +$(OUTPUT)/coredump_close_files_test: coredump_test_helpers.c
tools/testing/selftests/coredump/coredump_close_files_test.c new file +592 -0
592 lines not on this page · raw message
tools/testing/selftests/coredump/coredump_socket_protocol_test.c +6 -0
@@ -2377,6 +2377,12 @@ TEST_F(coredump, socket_request_memory_types_without_kernel)check_conflicting_ack (_metadata, self,COREDUMP_USERSPACE |COREDUMP_MEMORY_TYPES ); } +/* A rejected task closes everything on its way out anyway. */ +TEST_F(coredump, socket_request_close_files_reject) +{ +check_conflicting_ack (_metadata, self,COREDUMP_REJECT |COREDUMP_CLOSE_FILES ); +} +/* * A server built with the first structs reads the request it knows, * discards the rest and acks with the ack it knows. It raises nothing
tools/testing/selftests/coredump/coredump_test_helpers.c +2 -1
@@ -1612,7 +1612,8 @@ bool send_coredump_ack(int fd, const struct coredump_req *req,#define TEST_REQ_MASK_ALL \ (COREDUMP_KERNEL | COREDUMP_USERSPACE | \ COREDUMP_REJECT | COREDUMP_WAIT | \ - COREDUMP_RECORDS | COREDUMP_SPARSE | COREDUMP_MEMORY_TYPES) + COREDUMP_RECORDS | COREDUMP_SPARSE | COREDUMP_MEMORY_TYPES | \ + COREDUMP_CLOSE_FILES)bool check_coredump_req (const struct coredump_req *req) {
Signature · 1 line
2.53.0