Skip to contentSkip to conversation

coredump: Add /proc/<pid>/coredump_pre_exit for pre-exit before dumping

PATCH NAK Xin Zhao 6 participants 13 messages Jun 24 – Jun 30

Subsystems (9)
lore
Share & tools
lore

Xin Zhao

author
#0 lore raw

PATCH v4 1/1 coredump: Add /proc/<pid>/coredump_pre_exit for pre-exit before dumping

A coredump typically takes some time to complete. If we happen to hold a write lock with flock just before triggering the coredump, that write lock will not be released during the entire coredump process. As a result, other processes attempting to acquire the same write lock may experience significant delays. Another typical scenario is that shared memory, such as dma-buf, remains occupied and is not released for a long time due to core dumps.

To address this, add /proc/<pid>/coredump_pre_exit node so that people can specify which resources they want to release before dumping core. This patch implements the early release of two types of resources: flock files and file-backed shared memory. Default settings are NOT pre-exit anything.

A temporary bit, O_TMPCLOS, is added to mark vma->vm_file->f_flags during the execution of the newly introduced exit_mmap_mapped_shared() function. In this way, the subsequent exit_files_pre_exit() function does not need to find the corresponding vma through the file to check for the VM_SHARED attribute, thereby reducing the traversal cost.

---

Change in v4:
- Christian pointed out that the coredump process will traverse file descriptors (fd), so certain fds should not be closed by default. Rework the whole feature, add /proc/<pid>/coredump_pre_exit for user pre-exit resources selection, default is NOT pre-exit anything.
- Mateusz suggested that walking the fd table and release the file-lock is reasonable. No longer release all the fd(s). Based on user config, only the flock fd(s) and the fd(s) correspondent to file-backed shared memory will be released at most.

Change in v3:
- Add comment and commit-log to explain why do the MMF_DUMP_MAPPED_SHARED mm_flags_test() check, note that memory mapped files keep their own separate references to the files. The case to work around is that early unlocking a flock on a file allows other processes to lock and modify
the mapped data protected by the flock,
as suggested by Pedro Falcato.
- Link to v3: https://lore.kernel.org/all/[email protected]/

Change in v2:
- Get rid of the implement of adding new fcntl API, the issue does not worth inflicting the cost on everyone,
as suggested by Al Viro.
- Call exit_files() in coredump_wait(),
as suggested by Eric W. Biederman.
Add MMF_DUMP_MAPPED_SHARED mm_flags_test() check to filter cases that need to dump file-backed shared memory.
- Link to v2: https://lore.kernel.org/lkml/[email protected]/

v1:
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 .../admin-guide/kernel-parameters.txt         |  5 ++
 Documentation/filesystems/proc.rst            | 58 +++++++++-----
 fs/coredump.c                                 | 23 ++++++
 fs/file.c                                     | 46 +++++++++++
 fs/proc/base.c                                | 78 +++++++++++++++++++
 include/linux/mm.h                            |  1 +
 include/linux/mm_types.h                      |  9 +++
 include/linux/sched/task.h                    |  1 +
 include/uapi/asm-generic/fcntl.h              |  4 +
 kernel/fork.c                                 | 12 +++
 mm/mmap.c                                     | 21 +++++
 11 files changed, 238 insertions(+), 20 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -0
index f575d4508..bc6d3859f 100644
@@ -1024,6 +1024,11 @@ Kernel parameters
 			/proc/<pid>/coredump_filter.
 			See also Documentation/filesystems/proc.rst.
 
+	coredump_pre_exit=
+			[KNL] Change the default value for
+			/proc/<pid>/coredump_pre_exit.
+			See also Documentation/filesystems/proc.rst.
+
 	coresight_cpu_debug.enable
 			[ARM,ARM64]
 			Format: <bool>
Documentation/filesystems/proc.rst +38 -20

124 lines not on this page · raw message

fs/coredump.c +23 -0
index bb6fdb1f4..e08a8a6c4 100644
@@ -521,6 +521,27 @@ static int zap_threads(struct task_struct *tsk,
 	return nr;
 }
 
+static void coredump_pre_exit(void)
+{
+	struct task_struct *tsk = current;
+	unsigned long flags = __mm_flags_get_dumpable(tsk->mm);
+
+	if (!likely(flags & MMF_DUMP_PRE_EXIT_MASK))
+		return;
+
+	/*
+	 * Set O_TMPCLOS of file f_flags if file needs to be closed.
+	 */
+	if (test_bit(MMF_DUMP_PRE_EXIT_FILE_BACKED_SHARED, &flags) &&
+	    !test_bit(MMF_DUMP_MAPPED_SHARED, &flags))
+		exit_mmap_mapped_shared(tsk->mm);
+
+	/*
+	 * Check O_TMPCLOS of file f_flags to close file and clear it.
+	 */
+	exit_files_pre_exit(tsk, mm_flags_test(MMF_DUMP_PRE_EXIT_FLOCK, tsk->mm));
+}
+
 static int coredump_wait(int exit_code, struct core_state *core_state)
 {
 	struct task_struct *tsk = current;
@@ -1100,6 +1121,8 @@ static void do_coredump(struct core_name *cn, struct coredump_params *cprm,
 		return;
 	}
 
+	coredump_pre_exit();
+
 	switch (cn->core_type) {
 	case COREDUMP_FILE:
 		if (!coredump_file(cn, cprm, binfmt))
fs/file.c +46 -0

58 lines not on this page · raw message

fs/proc/base.c +78 -0

90 lines not on this page · raw message

include/linux/mm.h +1 -0
index af23453e9..dfd4717c7 100644
@@ -4066,6 +4066,7 @@ void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
 extern int __vm_enough_memory(const struct mm_struct *mm, long pages, int cap_sys_admin);
 extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
 extern void exit_mmap(struct mm_struct *);
+extern void exit_mmap_mapped_shared(struct mm_struct *mm);
 bool mmap_read_lock_maybe_expand(struct mm_struct *mm, struct vm_area_struct *vma,
 				 unsigned long addr, bool write);
 
include/linux/mm_types.h +9 -0
index c7db35be6..0555aaf50 100644
@@ -1963,6 +1963,15 @@ enum {
 	(BIT(MMF_DUMP_ANON_PRIVATE) | BIT(MMF_DUMP_ANON_SHARED) | \
 	 BIT(MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
 
+/* coredump pre-exit bits */
+#define MMF_DUMP_PRE_EXIT_FLOCK	11
+#define MMF_DUMP_PRE_EXIT_FILE_BACKED_SHARED 12
+
+#define MMF_DUMP_PRE_EXIT_SHIFT	(MMF_DUMPABLE_BITS + MMF_DUMP_FILTER_BITS)
+#define MMF_DUMP_PRE_EXIT_BITS	2
+#define MMF_DUMP_PRE_EXIT_MASK	\
+	(((1 << MMF_DUMP_PRE_EXIT_BITS) - 1) << MMF_DUMP_PRE_EXIT_SHIFT)
+
 #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
 # define MMF_DUMP_MASK_DEFAULT_ELF	BIT(MMF_DUMP_ELF_HEADERS)
 #else
include/linux/sched/task.h +1 -0
index 41ed884cf..b4becbf6c 100644
@@ -93,6 +93,7 @@ static inline void exit_thread(struct task_struct *tsk)
 extern __noreturn void do_group_exit(int);
 
 extern void exit_files(struct task_struct *);
+extern void exit_files_pre_exit(struct task_struct *, bool);
 extern void exit_itimers(struct task_struct *);
 
 extern pid_t kernel_clone(struct kernel_clone_args *kargs);
include/uapi/asm-generic/fcntl.h +4 -0
index 613475285..360604d65 100644
@@ -95,6 +95,10 @@
 #define O_NDELAY	O_NONBLOCK
 #endif
 
+#ifndef O_TMPCLOS
+#define O_TMPCLOS	0x80000000	/* tag need close, temporarily used */
+#endif
+
 #define F_DUPFD		0	/* dup */
 #define F_GETFD		1	/* get close_on_exec */
 #define F_SETFD		2	/* set/clear close_on_exec */
kernel/fork.c +12 -0
index a679b2448..84f1ee7f3 100644
@@ -1030,6 +1030,18 @@ static int __init coredump_filter_setup(char *s)
 
 __setup("coredump_filter=", coredump_filter_setup);
 
+static unsigned long default_dump_pre_exit;
+
+static int __init coredump_pre_exit_setup(char *s)
+{
+	default_dump_pre_exit =
+		(simple_strtoul(s, NULL, 0) << MMF_DUMP_PRE_EXIT_SHIFT) &
+		MMF_DUMP_PRE_EXIT_MASK;
+	return 1;
+}
+
+__setup("coredump_pre_exit=", coredump_pre_exit_setup);
+
 #include <linux/init_task.h>
 
 static void mm_init_aio(struct mm_struct *mm)
mm/mmap.c +21 -0
index 5754d1c36..b955c47c0 100644
@@ -1326,6 +1326,27 @@ void exit_mmap(struct mm_struct *mm)
 	vm_unacct_memory(nr_accounted);
 }
 
+void exit_mmap_mapped_shared(struct mm_struct *mm)
+{
+	struct vm_area_struct *vma;
+	VMA_ITERATOR(vmi, mm, 0);
+
+	mmap_write_lock(mm);
+	lru_add_drain();
+
+	for_each_vma(vmi, vma) {
+		if (vma->vm_flags & VM_HUGETLB)
+			continue;
+		if (!(vma->vm_flags & VM_SHARED) || !file_inode(vma->vm_file)->i_nlink)
+			continue;
+		vma->vm_file->f_flags |= O_TMPCLOS;
+		do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
+		cond_resched();
+	}
+
+	mmap_write_unlock(mm);
+}
+
 /*
  * Return true if the calling process may expand its vm space by the passed
  * number of pages
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 18 lines · Xin Zhao
fs/file.c +18 -0
+void exit_files_pre_exit(struct task_struct *tsk, bool checkflock)
+{
+	struct files_struct *files = tsk->files;
+	struct fdtable *fdt;
+	struct file *file;
+	unsigned int i, j = 0;
+
+	if (!files)
+		return;
+
+	fdt = rcu_dereference_raw(files->fdt);
+	for (;;) {
+		unsigned long set;
+
+		i = j * BITS_PER_LONG;
+		if (i >= fdt->max_fds)
+			break;
+		set = fdt->open_fds[j++];

fs/file.c void exit_files(struct task_struct *tsk) · line 558

Quoted 10 lines
fs/file.c +10 -0
+		while (set) {
+			if (!(set & 1))
+				goto next_fd;
+			file = fdt->fd[i];
+			if (!file)
+				goto next_fd;
+			if (file->f_flags & O_TMPCLOS) {
+				file->f_flags &= ~O_TMPCLOS;
+				goto close_fd;
+			}

*blink*

How could that possibly make sense? Many descriptors
may refer to the same file; what's more, many descriptor tables may contain such descriptors, so... just what is that code trying to do?

author
#2 lore raw
Quoted 11 lines · On Wed, 24 Jun 2026 17:28:44 +0100 Al Viro <[email protected]> wrote:

fs/file.c void exit_files(struct task_struct *tsk) · line 558

Quoted 4 lines
fs/file.c +4 -0
+			if (file->f_flags & O_TMPCLOS) {
+				file->f_flags &= ~O_TMPCLOS;
+				goto close_fd;
+			}

*blink*

How could that possibly make sense? Many descriptors
may refer to the same file; what's more, many descriptor tables may contain such descriptors, so... just what is that code trying to do?

This is yet another serious mistake. Perhaps my test scenarios were not complex enough, or I was overly confident in removing the logic that cleared the O_TMPCLOS flag and performed debug printing only when the reference count dropped to zero during that single close operation, without conducting further tests.

In v5, I plan to avoid clearing the O_TMPCLOS flag to handle the situation where multiple file descriptors map to a single file. Of course, there are some cases where the lifecycle of this file may extend beyond the process exit, but AFICT such situations either cannot last long or do not involve memory in the case where i_nlink != 0. Therefore, keeping this flag seems unlikely to cause any issues.

Since this flag is no longer used temporarily (it will never be cleared), I would like to rename it to O_PRECLOS.

Thanks
Xin Zhao

#3 lore raw
In reply to Xin Zhao
Quoted 26 lines

A coredump typically takes some time to complete. If we happen to hold a write lock with flock just before triggering the coredump, that write lock will not be released during the entire coredump process. As a result, other processes attempting to acquire the same write lock may experience significant delays. Another typical scenario is that shared memory, such as dma-buf, remains occupied and is not released for a long time due to core dumps.

To address this, add /proc/<pid>/coredump_pre_exit node so that people can specify which resources they want to release before dumping core. This patch implements the early release of two types of resources: flock files and file-backed shared memory. Default settings are NOT pre-exit anything.

A temporary bit, O_TMPCLOS, is added to mark vma->vm_file->f_flags during the execution of the newly introduced exit_mmap_mapped_shared() function. In this way, the subsequent exit_files_pre_exit() function does not need to find the corresponding vma through the file to check for the VM_SHARED attribute, thereby reducing the traversal cost.

Documentation/admin-guide/kernel-parameters.txt +0 -0
index f575d450861e..bc6d3859f874 100644

Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 1030

Quoted 7 lines
Documentation/admin-guide/kernel-parameters.txt +4 -0
@@ -1024,6 +1024,11 @@ Kernel parameters
 			/proc/<pid>/coredump_filter.
 			See also Documentation/filesystems/proc.rst.
 
+	coredump_pre_exit=
+			[KNL] Change the default value for
+			/proc/<pid>/coredump_pre_exit.
+			See also Documentation/filesystems/proc.rst.

Nah, we're not doing a separate file for this. That makes no sense whatsoever. I've already explained this in the first mail. There are effectively three modes:

(1) dump to a file
(2) spawn super-privileged usermode helper process connect coredumping process and said helper via pipe
(3) coredumping process connects to AF_UNIX socket

Parameterize (1) and (2) via a command line arguments. I strongly suspect you're using some AI tooling so it should be able to figure out how this was done in the past.

(3) can be extended by just introducing a new flag value for struct coredump_req. That is also illustrated by previous work.

We're not spreading procfs files. It's terrible api design especially for security sensitive changes.

Quoted 242 lines
Documentation/admin-guide/kernel-parameters.txt +23 -0
+static void coredump_pre_exit(void)
+{
+	struct task_struct *tsk = current;
+	unsigned long flags = __mm_flags_get_dumpable(tsk->mm);
+
+	if (!likely(flags & MMF_DUMP_PRE_EXIT_MASK))
+		return;
+
+	/*
+	 * Set O_TMPCLOS of file f_flags if file needs to be closed.
+	 */
+	if (test_bit(MMF_DUMP_PRE_EXIT_FILE_BACKED_SHARED, &flags) &&
+	    !test_bit(MMF_DUMP_MAPPED_SHARED, &flags))
+		exit_mmap_mapped_shared(tsk->mm);
+
+	/*
+	 * Check O_TMPCLOS of file f_flags to close file and clear it.
+	 */
+	exit_files_pre_exit(tsk, mm_flags_test(MMF_DUMP_PRE_EXIT_FLOCK, tsk->mm));
+}
+
 static int coredump_wait(int exit_code, struct core_state *core_state)
 {
 	struct task_struct *tsk = current;
@@ -1100,6 +1121,8 @@ static void do_coredump(struct core_name *cn, struct coredump_params *cprm,
 		return;
 	}
 
+	coredump_pre_exit();
+
 	switch (cn->core_type) {
 	case COREDUMP_FILE:
 		if (!coredump_file(cn, cprm, binfmt))
fs/file.c +46 -0
index 2c81c0b162d0..a58ffffcc31d 100644
@@ -23,6 +23,7 @@
 #include <linux/file_ref.h>
 #include <net/sock.h>
 #include <linux/init_task.h>
+#include <linux/filelock.h>
 
 #include "internal.h"
 
@@ -527,6 +528,51 @@ void exit_files(struct task_struct *tsk)
 	}
 }
 
+void exit_files_pre_exit(struct task_struct *tsk, bool checkflock)
+{
+	struct files_struct *files = tsk->files;
+	struct fdtable *fdt;
+	struct file *file;
+	unsigned int i, j = 0;
+
+	if (!files)
+		return;
+
+	fdt = rcu_dereference_raw(files->fdt);
+	for (;;) {
+		unsigned long set;
+
+		i = j * BITS_PER_LONG;
+		if (i >= fdt->max_fds)
+			break;
+		set = fdt->open_fds[j++];
+		while (set) {
+			if (!(set & 1))
+				goto next_fd;
+			file = fdt->fd[i];
+			if (!file)
+				goto next_fd;
+			if (file->f_flags & O_TMPCLOS) {
+				file->f_flags &= ~O_TMPCLOS;
+				goto close_fd;
+			}
+			if (!checkflock)
+				goto next_fd;
+			if (!vfs_inode_has_locks(file_inode(file)))
+				goto next_fd;
+
+close_fd:
+			fdt->fd[i] = NULL;
+			filp_close(file, files);
+			cond_resched();
+
+next_fd:
+			i++;
+			set >>= 1;
+		}
+	}
+}
+
 struct files_struct init_files = {
 	.count		= ATOMIC_INIT(1),
 	.fdt		= &init_files.fdtab,
fs/proc/base.c +78 -0
index d9acfa89c894..99b5f219f7fa 100644
@@ -3026,6 +3026,83 @@ static const struct file_operations proc_coredump_filter_operations = {
 	.write		= proc_coredump_filter_write,
 	.llseek		= generic_file_llseek,
 };
+
+static ssize_t proc_coredump_pre_exit_read(struct file *file, char __user *buf,
+					   size_t count, loff_t *ppos)
+{
+	struct task_struct *task = get_proc_task(file_inode(file));
+	struct mm_struct *mm;
+	char buffer[PROC_NUMBUF];
+	size_t len;
+	int ret;
+
+	if (!task)
+		return -ESRCH;
+
+	ret = 0;
+	mm = get_task_mm(task);
+	if (mm) {
+		unsigned long flags = __mm_flags_get_dumpable(mm);
+
+		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
+			       ((flags & MMF_DUMP_PRE_EXIT_MASK) >>
+				MMF_DUMP_PRE_EXIT_SHIFT));
+		mmput(mm);
+		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
+	}
+
+	put_task_struct(task);
+
+	return ret;
+}
+
+static ssize_t proc_coredump_pre_exit_write(struct file *file,
+					    const char __user *buf,
+					    size_t count,
+					    loff_t *ppos)
+{
+	struct task_struct *task;
+	struct mm_struct *mm;
+	unsigned int val;
+	int ret;
+	int i;
+	unsigned long mask;
+
+	ret = kstrtouint_from_user(buf, count, 0, &val);
+	if (ret < 0)
+		return ret;
+
+	ret = -ESRCH;
+	task = get_proc_task(file_inode(file));
+	if (!task)
+		goto out_no_task;
+
+	mm = get_task_mm(task);
+	if (!mm)
+		goto out_no_mm;
+	ret = 0;
+
+	for (i = 0, mask = 1; i < MMF_DUMP_PRE_EXIT_BITS; i++, mask <<= 1) {
+		if (val & mask)
+			mm_flags_set(i + MMF_DUMP_PRE_EXIT_SHIFT, mm);
+		else
+			mm_flags_clear(i + MMF_DUMP_PRE_EXIT_SHIFT, mm);
+	}
+
+	mmput(mm);
+ out_no_mm:
+	put_task_struct(task);
+ out_no_task:
+	if (ret < 0)
+		return ret;
+	return count;
+}
+
+static const struct file_operations proc_coredump_pre_exit_operations = {
+	.read		= proc_coredump_pre_exit_read,
+	.write		= proc_coredump_pre_exit_write,
+	.llseek		= generic_file_llseek,
+};
 #endif
 
 #ifdef CONFIG_TASK_IO_ACCOUNTING
@@ -3391,6 +3468,7 @@ static const struct pid_entry tgid_base_stuff[] = {
 #endif
 #ifdef CONFIG_ELF_CORE
 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
+	REG("coredump_pre_exit", S_IRUGO|S_IWUSR, proc_coredump_pre_exit_operations),
 #endif
 #ifdef CONFIG_TASK_IO_ACCOUNTING
 	ONE("io",	S_IRUSR, proc_tgid_io_accounting),
include/linux/mm.h +1 -0
index af23453e9dbd..dfd4717c7e3e 100644
@@ -4066,6 +4066,7 @@ void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
 extern int __vm_enough_memory(const struct mm_struct *mm, long pages, int cap_sys_admin);
 extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
 extern void exit_mmap(struct mm_struct *);
+extern void exit_mmap_mapped_shared(struct mm_struct *mm);
 bool mmap_read_lock_maybe_expand(struct mm_struct *mm, struct vm_area_struct *vma,
 				 unsigned long addr, bool write);
 
include/linux/mm_types.h +9 -0
index c7db35be6a30..0555aaf50001 100644
@@ -1963,6 +1963,15 @@ enum {
 	(BIT(MMF_DUMP_ANON_PRIVATE) | BIT(MMF_DUMP_ANON_SHARED) | \
 	 BIT(MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
 
+/* coredump pre-exit bits */
+#define MMF_DUMP_PRE_EXIT_FLOCK	11
+#define MMF_DUMP_PRE_EXIT_FILE_BACKED_SHARED 12
+
+#define MMF_DUMP_PRE_EXIT_SHIFT	(MMF_DUMPABLE_BITS + MMF_DUMP_FILTER_BITS)
+#define MMF_DUMP_PRE_EXIT_BITS	2
+#define MMF_DUMP_PRE_EXIT_MASK	\
+	(((1 << MMF_DUMP_PRE_EXIT_BITS) - 1) << MMF_DUMP_PRE_EXIT_SHIFT)
+
 #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
 # define MMF_DUMP_MASK_DEFAULT_ELF	BIT(MMF_DUMP_ELF_HEADERS)
 #else
include/linux/sched/task.h +1 -0
index 41ed884cffc9..b4becbf6c0eb 100644
@@ -93,6 +93,7 @@ static inline void exit_thread(struct task_struct *tsk)
 extern __noreturn void do_group_exit(int);
 
 extern void exit_files(struct task_struct *);
+extern void exit_files_pre_exit(struct task_struct *, bool);
 extern void exit_itimers(struct task_struct *);
 
 extern pid_t kernel_clone(struct kernel_clone_args *kargs);
include/uapi/asm-generic/fcntl.h +0 -0
index 613475285643..360604d653b4 100644

include/uapi/asm-generic/fcntl.h line 100

Quoted 6 lines
include/uapi/asm-generic/fcntl.h +3 -0
@@ -95,6 +95,10 @@
 #define O_NDELAY	O_NONBLOCK
 #endif
 
+#ifndef O_TMPCLOS
+#define O_TMPCLOS	0x80000000	/* tag need close, temporarily used */
+#endif

Sorry, not going to happen. This doesn't not justify the addition of a new uapi value at all.

I'm also including various Sashkio comments:

sashiko.dev <[email protected]>:

[Severity: Medium]
Is it safe to expose an internal, temporary kernel flag in a UAPI header? Userspace applications could intentionally or accidentally pass O_TMPCLOS to open(), which might permanently pollute the userspace ABI and trigger unexpected behavior during a coredump.

via: https://sashiko.dev/#/message/[email protected]

Quoted 13 lines
include/uapi/asm-generic/fcntl.h +1 -0
+
 #define F_DUPFD		0	/* dup */
 #define F_GETFD		1	/* get close_on_exec */
 #define F_SETFD		2	/* set/clear close_on_exec */
kernel/fork.c +1 -0
index a679b2448234..84f1ee7f32cf 100644
@@ -1030,6 +1030,18 @@ static int __init coredump_filter_setup(char *s)
 
 __setup("coredump_filter=", coredump_filter_setup);
 
+static unsigned long default_dump_pre_exit;

kernel/fork.c static int __init coredump_filter_setup(char *s) · line 1043

Quoted 10 lines
kernel/fork.c +10 -0
+
+static int __init coredump_pre_exit_setup(char *s)
+{
+	default_dump_pre_exit =
+		(simple_strtoul(s, NULL, 0) << MMF_DUMP_PRE_EXIT_SHIFT) &
+		MMF_DUMP_PRE_EXIT_MASK;
+	return 1;
+}
+
+__setup("coredump_pre_exit=", coredump_pre_exit_setup);

This makes no sense. I think you really need to sit down and think about a design for this that doesn't introduce state machinery for boot, mm, and the VFS in one shot to solve a fringe problem...

sashiko.dev <[email protected]>:

[Severity: High]
Does modifying the VMA maple tree via do_munmap() during the for_each_vma() iteration invalidate the outer iterator? The loop traverses the maple tree using the iterator vmi. However, do_munmap() creates its own internal VMA_ITERATOR and removes the VMA from the tree. Because the outer vmi iterator is not updated to reflect these structural changes, its cached state becomes stale, which can lead to a use-after-free when vma_next() is subsequently called.

via: https://sashiko.dev/#/message/[email protected]

sashiko.dev <[email protected]>:

[Severity: High]
Is it safe to iterate the file descriptor table without holding rcu_read_lock()? Because coredump_pre_exit() is called before zap_threads() kills other threads, concurrent threads can still trigger expand_files(), which replaces the fdt and frees the old one after an RCU grace period.

via: https://sashiko.dev/#/message/[email protected]

sashiko.dev <[email protected]>:

[Severity: Medium]
Similar to the issue in exit_mmap_mapped_shared(), this non-atomic update of file->f_flags risks losing concurrent fcntl() updates since it doesn't hold file->f_lock.

Also, if a file has duplicated file descriptors (e.g., via dup()), will clearing O_TMPCLOS here prematurely skip the closure of the remaining descriptors? When encountering the duplicated descriptor later, the flag will already be cleared, leaving the shared file actively referenced.

via: https://sashiko.dev/#/message/[email protected]

Signature · 1 line
Christian Brauner <[email protected]>
author
#4 lore raw
Quoted 18 lines · On Thu, 25 Jun 2026 09:28:08 +0200 Christian Brauner <[email protected]> wrote:

Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 1030

Quoted 4 lines
Documentation/admin-guide/kernel-parameters.txt +4 -0
+	coredump_pre_exit=
+			[KNL] Change the default value for
+			/proc/<pid>/coredump_pre_exit.
+			See also Documentation/filesystems/proc.rst.

Nah, we're not doing a separate file for this. That makes no sense whatsoever. I've already explained this in the first mail. There are effectively three modes:

(1) dump to a file
(2) spawn super-privileged usermode helper process connect coredumping process and said helper via pipe
(3) coredumping process connects to AF_UNIX socket

Parameterize (1) and (2) via a command line arguments. I strongly suspect you're using some AI tooling so it should be able to figure out how this was done in the past.

Quoted 5 lines

(3) can be extended by just introducing a new flag value for struct coredump_req. That is also illustrated by previous work.

We're not spreading procfs files. It's terrible api design especially for security sensitive changes.

The coredump socket approach is easier to implement because it allows for interaction between the server and client, enabling the customization of protocols. However, for the coredump file method, I can only think of defining "r" and "R" through core_pattern to release flock and file-backed shared data in advance. I'm unsure if this is feasible, as it changes the original definition of core_pattern.

Regarding the coredump pipe, there is also a lack of a mechanism for the pipe program to notify the coredump process, so it might still require adding "r" and "R" at the end of core_pattern to indicate this, allowing the coredump process to handle the early release on its own. I'm not sure if my understanding is correct.

Even if the coredump pipe program obtains the file pointer from the process that generated the coredump, it cannot reduce the reference count of the file (which I understand is a very bad attempt). Since it cannot decrease the reference count of the file, the early release must still be performed by the task that generated the coredump. Given this situation, it seems that we indeed need to use core_pattern for marking. I've thought for a long time about more suitable solutions, but I haven't come up with any.

Quoted 6 lines

include/uapi/asm-generic/fcntl.h line 100

Quoted 3 lines
include/uapi/asm-generic/fcntl.h +3 -0
+#ifndef O_TMPCLOS
+#define O_TMPCLOS	0x80000000	/* tag need close, temporarily used */
+#endif

Sorry, not going to happen. This doesn't not justify the addition of a new uapi value at all.

OK, if I use it at last, I will not put it in user header file.

Quoted 6 lines

kernel/fork.c static int __init coredump_filter_setup(char *s) · line 1043

Quoted 2 lines
kernel/fork.c +2 -0
+
+__setup("coredump_pre_exit=", coredump_pre_exit_setup);

This makes no sense. I think you really need to sit down and think about a design for this that doesn't introduce state machinery for boot, mm, and the VFS in one shot to solve a fringe problem...

I'll get rid of the attempt to add a new boot-up argument for this feature.

Quoted 10 lines

[Severity: High]
Does modifying the VMA maple tree via do_munmap() during the for_each_vma() iteration invalidate the outer iterator? The loop traverses the maple tree using the iterator vmi. However, do_munmap() creates its own internal VMA_ITERATOR and removes the VMA from the tree. Because the outer vmi iterator is not updated to reflect these structural changes, its cached state becomes stale, which can lead to a use-after-free when vma_next() is subsequently called.

via: https://sashiko.dev/#/message/[email protected]

When executing this traversal logic, we have already acquired a lock, and the process has been frozen. The traversal logic goes from start to finish. Are you sure that this approach could still have issues?

Quoted 5 lines

[Severity: High]
Is it safe to iterate the file descriptor table without holding rcu_read_lock()? Because coredump_pre_exit() is called before zap_threads() kills other threads, concurrent threads can still trigger expand_files(), which replaces the fdt and frees the old one after an RCU grace period.

Since the process has already been frozen, shouldn't we not need to consider such concurrency issues?

Quoted 15 lines

[Severity: Medium]
Similar to the issue in exit_mmap_mapped_shared(), this non-atomic update of file->f_flags risks losing concurrent fcntl() updates since it doesn't hold file->f_lock.

Also, if a file has duplicated file descriptors (e.g., via dup()), will clearing O_TMPCLOS here prematurely skip the closure of the remaining descriptors? When encountering the duplicated descriptor later, the flag will already be cleared, leaving the shared file actively referenced.

[Severity: Medium]
Similar to the issue in exit_mmap_mapped_shared(), this non-atomic update of file->f_flags risks losing concurrent fcntl() updates since it doesn't hold file->f_lock.

Quoted 4 lines

Also, if a file has duplicated file descriptors (e.g., via dup()), will clearing O_TMPCLOS here prematurely skip the closure of the remaining descriptors? When encountering the duplicated descriptor later, the flag will already be cleared, leaving the shared file actively referenced.

Currently, this flag will only be used by the logic we added, so I believe there won't be any issues.

Thanks
Xin Zhao

maintainer
#5 lore raw
Quoted 5 lines · Xin Zhao
Quoted 5 lines

[Severity: Medium]
Similar to the issue in exit_mmap_mapped_shared(), this non-atomic update of file->f_flags risks losing concurrent fcntl() updates since it doesn't hold file->f_lock.

Quoted 7 lines
Quoted 4 lines

Also, if a file has duplicated file descriptors (e.g., via dup()), will clearing O_TMPCLOS here prematurely skip the closure of the remaining descriptors? When encountering the duplicated descriptor later, the flag will already be cleared, leaving the shared file actively referenced.

Currently, this flag will only be used by the logic we added, so I believe there won't be any issues.

What makes you (or whatever LLM you happen to use) think that file is referenced only by descriptor table of the coredumping process? Or that only one coredumping process exists at any time, for that matter - and each might hold references to the same struct file.

#6 lore raw
In reply to Christian Brauner
Quoted 12 lines
Quoted 12 lines
include/uapi/asm-generic/fcntl.h +1 -0
+
 #define F_DUPFD		0	/* dup */
 #define F_GETFD		1	/* get close_on_exec */
 #define F_SETFD		2	/* set/clear close_on_exec */
kernel/fork.c +0 -0
index a679b2448234..84f1ee7f32cf 100644
@@ -1030,6 +1030,18 @@ static int __init coredump_filter_setup(char *s)
 
 __setup("coredump_filter=", coredump_filter_setup);
 
Quoted 15 lines

kernel/fork.c static int __init coredump_filter_setup(char *s) · line 1043

Quoted 11 lines
kernel/fork.c +11 -0
+static unsigned long default_dump_pre_exit;
+
+static int __init coredump_pre_exit_setup(char *s)
+{
+	default_dump_pre_exit =
+		(simple_strtoul(s, NULL, 0) << MMF_DUMP_PRE_EXIT_SHIFT) &
+		MMF_DUMP_PRE_EXIT_MASK;
+	return 1;
+}
+
+__setup("coredump_pre_exit=", coredump_pre_exit_setup);

This makes no sense. I think you really need to sit down and think about a design for this that doesn't introduce state machinery for boot, mm, and the VFS in one shot to solve a fringe problem...

Staring at exit_mmap_mapped_shared(), ... this looks rather hacky ("let's fake munmap and set some magical flags").

We're essentially saying "we don't want (pretty much) anything that's MAP_SHARED in the coredump". And for some reason someone should configure that, that's a rather weird toggle tbh.

And the granularity ("file-backed shared memory") is completely odd.

Aren't there other ways we could optimize this internally?

Like, if we know that a process is dead and cannot run anymore, downgrade writes to reads (and make sure we block GUP write attempts accordingly), or would that also not be sufficient?

Another thought:

fs/coredump.c calls get_dump_page().

get_dump_page() will not fault in any memory. So if a page is not in the page tables at the time of the dump, it will not get included in the coredump. Which means, that whether most non-anonymous memory will be included in a coredump is already like playing the lottery.

This is true for MAP_SHARED file mappings and MAP_PRIVATE file mappings without private modifications.

Which makes me wonder: How much is tooling relying on file-backed pages to end up in a coredump?

Signature · 3 lines
Cheers,

David
reviewer
#7 lore raw
Quoted 54 lines · David Hildenbrand (Arm)
Quoted 12 lines
Quoted 12 lines
include/uapi/asm-generic/fcntl.h +1 -0
+
 #define F_DUPFD		0	/* dup */
 #define F_GETFD		1	/* get close_on_exec */
 #define F_SETFD		2	/* set/clear close_on_exec */
kernel/fork.c +0 -0
index a679b2448234..84f1ee7f32cf 100644
@@ -1030,6 +1030,18 @@ static int __init coredump_filter_setup(char *s)
 
 __setup("coredump_filter=", coredump_filter_setup);
 
Quoted 15 lines

kernel/fork.c static int __init coredump_filter_setup(char *s) · line 1043

Quoted 11 lines
kernel/fork.c +11 -0
+static unsigned long default_dump_pre_exit;
+
+static int __init coredump_pre_exit_setup(char *s)
+{
+	default_dump_pre_exit =
+		(simple_strtoul(s, NULL, 0) << MMF_DUMP_PRE_EXIT_SHIFT) &
+		MMF_DUMP_PRE_EXIT_MASK;
+	return 1;
+}
+
+__setup("coredump_pre_exit=", coredump_pre_exit_setup);

This makes no sense. I think you really need to sit down and think about a design for this that doesn't introduce state machinery for boot, mm, and the VFS in one shot to solve a fringe problem...

Staring at exit_mmap_mapped_shared(), ... this looks rather hacky ("let's fake munmap and set some magical flags").

We're essentially saying "we don't want (pretty much) anything that's MAP_SHARED in the coredump". And for some reason someone should configure that, that's a rather weird toggle tbh.

And the granularity ("file-backed shared memory") is completely odd.

Aren't there other ways we could optimize this internally?

Like, if we know that a process is dead and cannot run anymore, downgrade writes to reads (and make sure we block GUP write attempts accordingly), or would that also not be sufficient?

Another thought:

fs/coredump.c calls get_dump_page().

get_dump_page() will not fault in any memory. So if a page is not in the page tables at the time of the dump, it will not get included in the coredump. Which means, that whether most non-anonymous memory will be included in a coredump is already like playing the lottery.

Quoted 5 lines

This is true for MAP_SHARED file mappings and MAP_PRIVATE file mappings without private modifications.

Which makes me wonder: How much is tooling relying on file-backed pages to end up in a coredump?

FWIW this mechanism already exists, see /proc/self/coredump_filter. The default is bits 0, 1, 4 and 5 (see core(5)), which maps back to no file pages being dumped to a core dump, apart from ELF headers (these help the debugger trace back the mapped binary to the debug info using the buildid).

So the answer to this question is "approximately none" :)

Signature · 1 line
Pedro
#8 lore raw
Quoted 38 lines · Pedro Falcato
Quoted 31 lines · David Hildenbrand (Arm)
Quoted 4 lines

This makes no sense. I think you really need to sit down and think about a design for this that doesn't introduce state machinery for boot, mm, and the VFS in one shot to solve a fringe problem...

Staring at exit_mmap_mapped_shared(), ... this looks rather hacky ("let's fake munmap and set some magical flags").

We're essentially saying "we don't want (pretty much) anything that's MAP_SHARED in the coredump". And for some reason someone should configure that, that's a rather weird toggle tbh.

And the granularity ("file-backed shared memory") is completely odd.

Aren't there other ways we could optimize this internally?

Like, if we know that a process is dead and cannot run anymore, downgrade writes to reads (and make sure we block GUP write attempts accordingly), or would that also not be sufficient?

Another thought:

fs/coredump.c calls get_dump_page().

get_dump_page() will not fault in any memory. So if a page is not in the page tables at the time of the dump, it will not get included in the coredump. Which means, that whether most non-anonymous memory will be included in a coredump is already like playing the lottery.

Quoted 5 lines

This is true for MAP_SHARED file mappings and MAP_PRIVATE file mappings without private modifications.

Which makes me wonder: How much is tooling relying on file-backed pages to end up in a coredump?

Quoted 7 lines

FWIW this mechanism already exists, see /proc/self/coredump_filter. The default is bits 0, 1, 4 and 5 (see core(5)), which maps back to no file pages being dumped to a core dump, apart from ELF headers (these help the debugger trace back the mapped binary to the debug info using the buildid).

So the answer to this question is "approximately none" :)

Ah, thanks! vma_dump_size() honors this, and I am sure through some magical routing the information stored in m->dump_size will end up not dumping these pages.

Staring at elf_core_dump(), this "unmap some stuff" part is really, really nasty, as it effectively removes the VMAs->segments from the dump. (unless I am missing something important)

Signature · 3 lines
Cheers,

David
#9 lore raw
Quoted 47 lines · David Hildenbrand (Arm)
Quoted 38 lines · Pedro Falcato
Quoted 31 lines · David Hildenbrand (Arm)
Quoted 4 lines

This makes no sense. I think you really need to sit down and think about a design for this that doesn't introduce state machinery for boot, mm, and the VFS in one shot to solve a fringe problem...

Staring at exit_mmap_mapped_shared(), ... this looks rather hacky ("let's fake munmap and set some magical flags").

We're essentially saying "we don't want (pretty much) anything that's MAP_SHARED in the coredump". And for some reason someone should configure that, that's a rather weird toggle tbh.

And the granularity ("file-backed shared memory") is completely odd.

Aren't there other ways we could optimize this internally?

Like, if we know that a process is dead and cannot run anymore, downgrade writes to reads (and make sure we block GUP write attempts accordingly), or would that also not be sufficient?

Another thought:

fs/coredump.c calls get_dump_page().

get_dump_page() will not fault in any memory. So if a page is not in the page tables at the time of the dump, it will not get included in the coredump. Which means, that whether most non-anonymous memory will be included in a coredump is already like playing the lottery.

Quoted 5 lines

This is true for MAP_SHARED file mappings and MAP_PRIVATE file mappings without private modifications.

Which makes me wonder: How much is tooling relying on file-backed pages to end up in a coredump?

Quoted 7 lines

FWIW this mechanism already exists, see /proc/self/coredump_filter. The default is bits 0, 1, 4 and 5 (see core(5)), which maps back to no file pages being dumped to a core dump, apart from ELF headers (these help the debugger trace back the mapped binary to the debug info using the buildid).

So the answer to this question is "approximately none" :)

Quoted 6 lines

Ah, thanks! vma_dump_size() honors this, and I am sure through some magical routing the information stored in m->dump_size will end up not dumping these pages.

Staring at elf_core_dump(), this "unmap some stuff" part is really, really nasty, as it effectively removes the VMAs->segments from the dump. (unless I am missing something important)

I would not mind if you would clean the mm portion of coredump up as I think it's pretty ancient and - as you can see - rather questionable.

Fwiw, for the coredump socket I added a "userspace" mode a little while ago where the kernel doesn't actually generate the coredump at all but let's userspace do it. Android has the same model but uses signal handler tricks (iirc) to do this.

author
#10 lore raw
In reply to Christian Brauner

Dear Christian,

Quoted 13 lines · On Thu, 25 Jun 2026 09:28:08 +0200 Christian Brauner <[email protected]> wrote:

Nah, we're not doing a separate file for this. That makes no sense whatsoever. I've already explained this in the first mail. There are effectively three modes:

(1) dump to a file
(2) spawn super-privileged usermode helper process connect coredumping process and said helper via pipe
(3) coredumping process connects to AF_UNIX socket

Parameterize (1) and (2) via a command line arguments. I strongly suspect you're using some AI tooling so it should be able to figure out how this was done in the past.

Quoted 5 lines

(3) can be extended by just introducing a new flag value for struct coredump_req. That is also illustrated by previous work.

We're not spreading procfs files. It's terrible api design especially for security sensitive changes.

I am unsure whether the addition of a bit 9 for coredump_filter in version v5 will be acceptable to everyone, as there are still many projects using the coredump file and coredump pipe mechanisms. I found that coredump files are used in elf_coredump_extra_notes_write, which makes it difficult to distinguish between these two types and determine whether early release of the files is possible without setting parameters. I am reluctant to add another bit for this purpose, but it seems necessary. I am not sure if my understanding is correct or if you have better suggestions, as I really do not want to address only the coredump socket situation.

Thanks
Xin Zhao

maintainer NAK
#11 lore raw
In reply to Xin Zhao

+cc missing maintainers, lists.

NAK.

This is un-upstreamable for numerous reasons.

The stuff you're doing in mm is broken, wrong and invasive and you've not even bothered to cc- mm people. I'm annoyed by this.

You're also doing incredibly silly mistakes at v4 of something that should have been an RFC.

You don't seem to understand the concept of patch _series_ (break it up into smaller patches!!!) and you haven't bothered cc'ing maintainers whose subsystems you're radically alterting.

I'm annoyed as you have a history where you were told not to add insane hacks before ([0], my reply at [1]).

[0]:https://lore.kernel.org/all/[email protected]/ [1]:https://lore.kernel.org/all/[email protected]/

Was I wasting my time there? Am I wasting my time responding now?

And how hard is it to run a simple perl script?

Let me run it for you for _just_ the maintainers:

$ scripts/get_maintainer.pl --nogit --nogit-fallback --nor your_patch.patch
Jonathan Corbet <[email protected]> (maintainer:DOCUMENTATION)
Alexander Viro <[email protected]> (maintainer:FILESYSTEMS (VFS and infrastructure))
Christian Brauner <[email protected]> (maintainer:FILESYSTEMS (VFS and infrastructure))
Andrew Morton <[email protected]> (maintainer:MEMORY MANAGEMENT - CORE)
David Hildenbrand <[email protected]> (maintainer:MEMORY MANAGEMENT - CORE)
Arnd Bergmann <[email protected]> (maintainer:GENERIC INCLUDE/ASM HEADER FILES)
Ingo Molnar <[email protected]> (maintainer:SCHEDULER)
Peter Zijlstra <[email protected]> (maintainer:SCHEDULER)
Juri Lelli <[email protected]> (maintainer:SCHEDULER)
Vincent Guittot <[email protected]> (maintainer:SCHEDULER)
Kees Cook <[email protected]> (maintainer:EXEC & BINFMT API, ELF)
"Liam R. Howlett" <[email protected]> (maintainer:MEMORY MAPPING)
Lorenzo Stoakes <[email protected]> (maintainer:MEMORY MAPPING)
[email protected] (open list:DOCUMENTATION)
[email protected] (open list)
[email protected] (open list:PROC FILESYSTEM)
[email protected] (open list:MEMORY MANAGEMENT - CORE)
[email protected] (open list:GENERIC INCLUDE/ASM HEADER FILES)
EXEC & BINFMT API, ELF status: Supported

You're missing the majority of these. That's _not OK_.

Quoted 9 lines · Xin Zhao

A coredump typically takes some time to complete. If we happen to hold a write lock with flock just before triggering the coredump, that write lock will not be released during the entire coredump process. As a result, other processes attempting to acquire the same write lock may experience significant delays. Another typical scenario is that shared memory, such as dma-buf, remains occupied and is not released for a long time due to core dumps.

To address this, add /proc/<pid>/coredump_pre_exit node so that people can

This is a horrible idea.

Quoted 3 lines

specify which resources they want to release before dumping core. This patch implements the early release of two types of resources: flock files and file-backed shared memory. Default settings are NOT pre-exit anything.

What, people set this ahead of time? For a dynamic thing like files?

Quoted 6 lines

A temporary bit, O_TMPCLOS, is added to mark vma->vm_file->f_flags during the execution of the newly introduced exit_mmap_mapped_shared() function. In this way, the subsequent exit_files_pre_exit() function does not need to find the corresponding vma through the file to check for the VM_SHARED attribute, thereby reducing the traversal cost.

This sentence doesn't even make sense?

And also !VM_SHARED means !vma->vm_file so your code would NULL deref if you didn't check that. But !VM_SHARED VMAs can absolutely be file-backed...

Quoted 35 lines

---

Change in v4:
- Christian pointed out that the coredump process will traverse file descriptors (fd), so certain fds should not be closed by default. Rework the whole feature, add /proc/<pid>/coredump_pre_exit for user pre-exit resources selection, default is NOT pre-exit anything.
- Mateusz suggested that walking the fd table and release the file-lock is reasonable. No longer release all the fd(s). Based on user config, only the flock fd(s) and the fd(s) correspondent to file-backed shared memory will be released at most.

Change in v3:
- Add comment and commit-log to explain why do the MMF_DUMP_MAPPED_SHARED mm_flags_test() check, note that memory mapped files keep their own separate references to the files. The case to work around is that early unlocking a flock on a file allows other processes to lock and modify
the mapped data protected by the flock,
as suggested by Pedro Falcato.
- Link to v3: https://lore.kernel.org/all/[email protected]/

Change in v2:
- Get rid of the implement of adding new fcntl API, the issue does not worth inflicting the cost on everyone,
as suggested by Al Viro.
- Call exit_files() in coredump_wait(),
as suggested by Eric W. Biederman.
Add MMF_DUMP_MAPPED_SHARED mm_flags_test() check to filter cases that need to dump file-backed shared memory.
- Link to v2: https://lore.kernel.org/lkml/[email protected]/

v1:
- Link to v1: https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
---
 .../admin-guide/kernel-parameters.txt         |  5 ++
 Documentation/filesystems/proc.rst            | 58 +++++++++-----
 fs/coredump.c                                 | 23 ++++++
 fs/file.c                                     | 46 +++++++++++
 fs/proc/base.c                                | 78 +++++++++++++++++++
 include/linux/mm.h                            |  1 +

No.

Quoted 1 line
 include/linux/mm_types.h                      |  9 +++

No.

Quoted 4 lines
 include/linux/sched/task.h                    |  1 +
 include/uapi/asm-generic/fcntl.h              |  4 +
 kernel/fork.c                                 | 12 +++
 mm/mmap.c                                     | 21 +++++

No.

Quoted 1 line
 11 files changed, 238 insertions(+), 20 deletions(-)

This is a completely insane diffstat for a single patch. Ridiculous.

AND YOU HAVEN'T ADDED A SINGLE TEST.

Quoted 168 lines
Documentation/admin-guide/kernel-parameters.txt +5 -0
index f575d4508..bc6d3859f 100644
@@ -1024,6 +1024,11 @@ Kernel parameters
 			/proc/<pid>/coredump_filter.
 			See also Documentation/filesystems/proc.rst.
 
+	coredump_pre_exit=
+			[KNL] Change the default value for
+			/proc/<pid>/coredump_pre_exit.
+			See also Documentation/filesystems/proc.rst.
+
 	coresight_cpu_debug.enable
 			[ARM,ARM64]
 			Format: <bool>
Documentation/filesystems/proc.rst +38 -20
index db6167bef..6a637d31d 100644
@@ -39,16 +39,17 @@ fixes/update part 1.1  Stefani Seibold <[email protected]>    June 9 2009
   3.2	/proc/<pid>/oom_score - Display current oom-killer score
   3.3	/proc/<pid>/io - Display the IO accounting fields
   3.4	/proc/<pid>/coredump_filter - Core dump filtering settings
-  3.5	/proc/<pid>/mountinfo - Information about mounts
-  3.6	/proc/<pid>/comm  & /proc/<pid>/task/<tid>/comm
-  3.7   /proc/<pid>/task/<tid>/children - Information about task children
-  3.8   /proc/<pid>/fdinfo/<fd> - Information about opened file
-  3.9   /proc/<pid>/map_files - Information about memory mapped files
-  3.10  /proc/<pid>/timerslack_ns - Task timerslack value
-  3.11	/proc/<pid>/patch_state - Livepatch patch operation state
-  3.12	/proc/<pid>/arch_status - Task architecture specific information
-  3.13  /proc/<pid>/fd - List of symlinks to open files
-  3.14  /proc/<pid>/ksm_stat - Information about the process's ksm status.
+  3.5  /proc/<pid>/coredump_pre_exit - Core dump pre-exit settings
+  3.6	/proc/<pid>/mountinfo - Information about mounts
+  3.7	/proc/<pid>/comm  & /proc/<pid>/task/<tid>/comm
+  3.8   /proc/<pid>/task/<tid>/children - Information about task children
+  3.9   /proc/<pid>/fdinfo/<fd> - Information about opened file
+  3.10   /proc/<pid>/map_files - Information about memory mapped files
+  3.11  /proc/<pid>/timerslack_ns - Task timerslack value
+  3.12	/proc/<pid>/patch_state - Livepatch patch operation state
+  3.13	/proc/<pid>/arch_status - Task architecture specific information
+  3.14  /proc/<pid>/fd - List of symlinks to open files
+  3.15  /proc/<pid>/ksm_stat - Information about the process's ksm status.
 
   4	Configuring procfs
   4.1	Mount options
@@ -1961,7 +1962,24 @@ For example::
   $ echo 0x7 > /proc/self/coredump_filter
   $ ./some_program
 
-3.5	/proc/<pid>/mountinfo - Information about mounts
+3.5 /proc/<pid>/coredump_pre_exit - Core dump pre-exit settings
+---------------------------------------------------------------
+A coredump typically takes some time to complete. If we happen to hold a write
+lock with flock just before triggering the coredump, that write lock will not
+be released during the entire coredump process. As a result, other processes
+attempting to acquire the same write lock may experience significant delays.
+Another typical scenario is that shared memory, such as dma-buf, remains
+occupied and is not released for a long time due to core dumps.
+
+/proc/<pid>/coredump_pre_exit allows you to pre-exit some resources before
+dumping core.
+
+The following two types are supported:
+
+  - (bit 0) flock files
+  - (bit 1) file-backed shared memory
+
+3.6	/proc/<pid>/mountinfo - Information about mounts
 --------------------------------------------------------
 
 This file contains lines of the form::
@@ -2001,7 +2019,7 @@ For more information on mount propagation see:
   Documentation/filesystems/sharedsubtree.rst
 
 
-3.6	/proc/<pid>/comm  & /proc/<pid>/task/<tid>/comm
+3.7	/proc/<pid>/comm  & /proc/<pid>/task/<tid>/comm
 --------------------------------------------------------
 These files provide a method to access a task's comm value. It also allows for
 a task to set its own or one of its thread siblings comm value. The comm value
@@ -2010,7 +2028,7 @@ then the kernel's TASK_COMM_LEN (currently 16 chars, including the NUL
 terminator) will result in a truncated comm value.
 
 
-3.7	/proc/<pid>/task/<tid>/children - Information about task children
+3.8	/proc/<pid>/task/<tid>/children - Information about task children
 -------------------------------------------------------------------------
 This file provides a fast way to retrieve first level children pids
 of a task pointed by <pid>/<tid> pair. The format is a space separated
@@ -2027,7 +2045,7 @@ pids, so one needs to either stop or freeze processes being inspected
 if precise results are needed.
 
 
-3.8	/proc/<pid>/fdinfo/<fd> - Information about opened file
+3.9	/proc/<pid>/fdinfo/<fd> - Information about opened file
 ---------------------------------------------------------------
 This file provides information associated with an opened file. The regular
 files have at least four fields -- 'pos', 'flags', 'mnt_id' and 'ino'.
@@ -2198,7 +2216,7 @@ VFIO Device files
 where 'vfio-device-syspath' is the sysfs path corresponding to the VFIO device
 file.
 
-3.9	/proc/<pid>/map_files - Information about memory mapped files
+3.10	/proc/<pid>/map_files - Information about memory mapped files
 ---------------------------------------------------------------------
 This directory contains symbolic links which represent memory mapped files
 the process is maintaining.  Example output::
@@ -2220,7 +2238,7 @@ time one can open(2) mappings from the listings of two processes and
 comparing their inode numbers to figure out which anonymous memory areas
 are actually shared.
 
-3.10	/proc/<pid>/timerslack_ns - Task timerslack value
+3.11	/proc/<pid>/timerslack_ns - Task timerslack value
 ---------------------------------------------------------
 This file provides the value of the task's timerslack value in nanoseconds.
 This value specifies an amount of time that normal timers may be deferred
@@ -2236,7 +2254,7 @@ Valid values are from 0 - ULLONG_MAX
 An application setting the value must have PTRACE_MODE_ATTACH_FSCREDS level
 permissions on the task specified to change its timerslack_ns value.
 
-3.11	/proc/<pid>/patch_state - Livepatch patch operation state
+3.12	/proc/<pid>/patch_state - Livepatch patch operation state
 -----------------------------------------------------------------
 When CONFIG_LIVEPATCH is enabled, this file displays the value of the
 patch state for the task.
@@ -2253,7 +2271,7 @@ patched.  If the patch is being enabled, then the task has already been
 patched.  If the patch is being disabled, then the task hasn't been
 unpatched yet.
 
-3.12 /proc/<pid>/arch_status - task architecture specific status
+3.13 /proc/<pid>/arch_status - task architecture specific status
 -------------------------------------------------------------------
 When CONFIG_PROC_PID_ARCH_STATUS is enabled, this file displays the
 architecture specific status of the task.
@@ -2298,7 +2316,7 @@ AVX512_elapsed_ms
   the task is unlikely an AVX512 user, but depends on the workload and the
   scheduling scenario, it also could be a false negative mentioned above.
 
-3.13 /proc/<pid>/fd - List of symlinks to open files
+3.14 /proc/<pid>/fd - List of symlinks to open files
 -------------------------------------------------------
 This directory contains symbolic links which represent open files
 the process is maintaining.  Example output::
@@ -2313,7 +2331,7 @@ The number of open files for the process is stored in 'size' member
 of stat() output for /proc/<pid>/fd for fast access.
 -------------------------------------------------------
 
-3.14 /proc/<pid>/ksm_stat - Information about the process's ksm status
+3.15 /proc/<pid>/ksm_stat - Information about the process's ksm status
 ----------------------------------------------------------------------
 When CONFIG_KSM is enabled, each process has this file which displays
 the information of ksm merging status.
fs/coredump.c +4 -0
index bb6fdb1f4..e08a8a6c4 100644
@@ -521,6 +521,27 @@ static int zap_threads(struct task_struct *tsk,
 	return nr;
 }
 
+static void coredump_pre_exit(void)
+{
+	struct task_struct *tsk = current;
+	unsigned long flags = __mm_flags_get_dumpable(tsk->mm);

fs/coredump.c static int zap_threads(struct task_struct *tsk, · line 537

Quoted 10 lines
fs/coredump.c +10 -0
+
+	if (!likely(flags & MMF_DUMP_PRE_EXIT_MASK))
+		return;
+
+	/*
+	 * Set O_TMPCLOS of file f_flags if file needs to be closed.
+	 */
+	if (test_bit(MMF_DUMP_PRE_EXIT_FILE_BACKED_SHARED, &flags) &&
+	    !test_bit(MMF_DUMP_MAPPED_SHARED, &flags))
+		exit_mmap_mapped_shared(tsk->mm);

What the hell are you doing?

This is not where we unmap VMAs?

This is likely broken in subtle ways.

Quoted 68 lines
fs/coredump.c +9 -0
+
+	/*
+	 * Check O_TMPCLOS of file f_flags to close file and clear it.
+	 */
+	exit_files_pre_exit(tsk, mm_flags_test(MMF_DUMP_PRE_EXIT_FLOCK, tsk->mm));
+}
+
 static int coredump_wait(int exit_code, struct core_state *core_state)
 {
 	struct task_struct *tsk = current;
@@ -1100,6 +1121,8 @@ static void do_coredump(struct core_name *cn, struct coredump_params *cprm,
 		return;
 	}
 
+	coredump_pre_exit();
+
 	switch (cn->core_type) {
 	case COREDUMP_FILE:
 		if (!coredump_file(cn, cprm, binfmt))
fs/file.c +34 -0
index 2c81c0b16..a58ffffcc 100644
@@ -23,6 +23,7 @@
 #include <linux/file_ref.h>
 #include <net/sock.h>
 #include <linux/init_task.h>
+#include <linux/filelock.h>
 
 #include "internal.h"
 
@@ -527,6 +528,51 @@ void exit_files(struct task_struct *tsk)
 	}
 }
 
+void exit_files_pre_exit(struct task_struct *tsk, bool checkflock)
+{
+	struct files_struct *files = tsk->files;
+	struct fdtable *fdt;
+	struct file *file;
+	unsigned int i, j = 0;
+
+	if (!files)
+		return;
+
+	fdt = rcu_dereference_raw(files->fdt);
+	for (;;) {
+		unsigned long set;
+
+		i = j * BITS_PER_LONG;
+		if (i >= fdt->max_fds)
+			break;
+		set = fdt->open_fds[j++];
+		while (set) {
+			if (!(set & 1))
+				goto next_fd;
+			file = fdt->fd[i];
+			if (!file)
+				goto next_fd;
+			if (file->f_flags & O_TMPCLOS) {
+				file->f_flags &= ~O_TMPCLOS;
+				goto close_fd;
+			}
+			if (!checkflock)
+				goto next_fd;
+			if (!vfs_inode_has_locks(file_inode(file)))
+				goto next_fd;
+

fs/file.c void exit_files(struct task_struct *tsk) · line 573

Quoted 10 lines
fs/file.c +10 -0
+close_fd:
+			fdt->fd[i] = NULL;
+			filp_close(file, files);
+			cond_resched();
+
+next_fd:
+			i++;
+			set >>= 1;
+		}
+	}

This code hurts my eyes.

Quoted 10 lines
fs/file.c +2 -0
+}
+
 struct files_struct init_files = {
 	.count		= ATOMIC_INIT(1),
 	.fdt		= &init_files.fdtab,
fs/proc/base.c +0 -0
index d9acfa89c..99b5f219f 100644

fs/proc/base.c static const struct file_operations proc_coredump_filter_operations = { · line 3029

Quoted 4 lines
fs/proc/base.c +1 -0
@@ -3026,6 +3026,83 @@ static const struct file_operations proc_coredump_filter_operations = {
 	.write		= proc_coredump_filter_write,
 	.llseek		= generic_file_llseek,
 };
+

No comment, obviously.

Quoted 19 lines
fs/proc/base.c +19 -0
+static ssize_t proc_coredump_pre_exit_read(struct file *file, char __user *buf,
+					   size_t count, loff_t *ppos)
+{
+	struct task_struct *task = get_proc_task(file_inode(file));
+	struct mm_struct *mm;
+	char buffer[PROC_NUMBUF];
+	size_t len;
+	int ret;
+
+	if (!task)
+		return -ESRCH;
+
+	ret = 0;
+	mm = get_task_mm(task);
+	if (mm) {
+		unsigned long flags = __mm_flags_get_dumpable(mm);
+
+		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
+			       ((flags & MMF_DUMP_PRE_EXIT_MASK) >>

fs/proc/base.c static const struct file_operations proc_coredump_filter_operations = { · line 3058

Quoted 10 lines
fs/proc/base.c +10 -0
+				MMF_DUMP_PRE_EXIT_SHIFT));
+		mmput(mm);
+		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
+	}
+
+	put_task_struct(task);
+
+	return ret;
+}
+

Yeah who needs a comment...

Quoted 17 lines
fs/proc/base.c +17 -0
+static ssize_t proc_coredump_pre_exit_write(struct file *file,
+					    const char __user *buf,
+					    size_t count,
+					    loff_t *ppos)
+{
+	struct task_struct *task;
+	struct mm_struct *mm;
+	unsigned int val;
+	int ret;
+	int i;
+	unsigned long mask;
+
+	ret = kstrtouint_from_user(buf, count, 0, &val);
+	if (ret < 0)
+		return ret;
+
+	ret = -ESRCH;

fs/proc/base.c static const struct file_operations proc_coredump_filter_operations = { · line 3085

Quoted 10 lines
fs/proc/base.c +10 -0
+	task = get_proc_task(file_inode(file));
+	if (!task)
+		goto out_no_task;
+
+	mm = get_task_mm(task);
+	if (!mm)
+		goto out_no_mm;
+	ret = 0;
+
+	for (i = 0, mask = 1; i < MMF_DUMP_PRE_EXIT_BITS; i++, mask <<= 1) {

What?

Quoted 36 lines
fs/proc/base.c +21 -0
+		if (val & mask)
+			mm_flags_set(i + MMF_DUMP_PRE_EXIT_SHIFT, mm);
+		else
+			mm_flags_clear(i + MMF_DUMP_PRE_EXIT_SHIFT, mm);
+	}
+
+	mmput(mm);
+ out_no_mm:
+	put_task_struct(task);
+ out_no_task:
+	if (ret < 0)
+		return ret;
+	return count;
+}
+
+static const struct file_operations proc_coredump_pre_exit_operations = {
+	.read		= proc_coredump_pre_exit_read,
+	.write		= proc_coredump_pre_exit_write,
+	.llseek		= generic_file_llseek,
+};
 #endif
 
 #ifdef CONFIG_TASK_IO_ACCOUNTING
@@ -3391,6 +3468,7 @@ static const struct pid_entry tgid_base_stuff[] = {
 #endif
 #ifdef CONFIG_ELF_CORE
 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
+	REG("coredump_pre_exit", S_IRUGO|S_IWUSR, proc_coredump_pre_exit_operations),
 #endif
 #ifdef CONFIG_TASK_IO_ACCOUNTING
 	ONE("io",	S_IRUSR, proc_tgid_io_accounting),
include/linux/mm.h +0 -0
index af23453e9..dfd4717c7 100644

include/linux/mm.h void anon_vma_interval_tree_verify(struct anon_vma_chain *node); · line 4069

Quoted 4 lines
include/linux/mm.h +1 -0
@@ -4066,6 +4066,7 @@ void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
 extern int __vm_enough_memory(const struct mm_struct *mm, long pages, int cap_sys_admin);
 extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
 extern void exit_mmap(struct mm_struct *);
+extern void exit_mmap_mapped_shared(struct mm_struct *mm);

You don't use extern.

Quoted 8 lines
include/linux/mm.h +0 -0
 bool mmap_read_lock_maybe_expand(struct mm_struct *mm, struct vm_area_struct *vma,
 				 unsigned long addr, bool write);
 
include/linux/mm_types.h +0 -0
index c7db35be6..0555aaf50 100644

include/linux/mm_types.h enum { · line 1968

Quoted 6 lines
include/linux/mm_types.h +3 -0
@@ -1963,6 +1963,15 @@ enum {
 	(BIT(MMF_DUMP_ANON_PRIVATE) | BIT(MMF_DUMP_ANON_SHARED) | \
 	 BIT(MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
 
+/* coredump pre-exit bits */
+#define MMF_DUMP_PRE_EXIT_FLOCK	11
+#define MMF_DUMP_PRE_EXIT_FILE_BACKED_SHARED 12

Err do we have space for this?

You really want to add 2 more bits to mm_struct flags for this insanity?

include/linux/mm_types.h enum { · line 1973

Quoted 5 lines
include/linux/mm_types.h +5 -0
+
+#define MMF_DUMP_PRE_EXIT_SHIFT	(MMF_DUMPABLE_BITS + MMF_DUMP_FILTER_BITS)
+#define MMF_DUMP_PRE_EXIT_BITS	2
+#define MMF_DUMP_PRE_EXIT_MASK	\
+	(((1 << MMF_DUMP_PRE_EXIT_BITS) - 1) << MMF_DUMP_PRE_EXIT_SHIFT)

So are these dumpable bits or not? Why are you not just incrementing MMF_DUMPABLE_BITS?

Quoted 59 lines
include/linux/mm_types.h +1 -0
+
 #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
 # define MMF_DUMP_MASK_DEFAULT_ELF	BIT(MMF_DUMP_ELF_HEADERS)
 #else
include/linux/sched/task.h +1 -0
index 41ed884cf..b4becbf6c 100644
@@ -93,6 +93,7 @@ static inline void exit_thread(struct task_struct *tsk)
 extern __noreturn void do_group_exit(int);
 
 extern void exit_files(struct task_struct *);
+extern void exit_files_pre_exit(struct task_struct *, bool);
 extern void exit_itimers(struct task_struct *);
 
 extern pid_t kernel_clone(struct kernel_clone_args *kargs);
include/uapi/asm-generic/fcntl.h +4 -0
index 613475285..360604d65 100644
@@ -95,6 +95,10 @@
 #define O_NDELAY	O_NONBLOCK
 #endif
 
+#ifndef O_TMPCLOS
+#define O_TMPCLOS	0x80000000	/* tag need close, temporarily used */
+#endif
+
 #define F_DUPFD		0	/* dup */
 #define F_GETFD		1	/* get close_on_exec */
 #define F_SETFD		2	/* set/clear close_on_exec */
kernel/fork.c +12 -0
index a679b2448..84f1ee7f3 100644
@@ -1030,6 +1030,18 @@ static int __init coredump_filter_setup(char *s)
 
 __setup("coredump_filter=", coredump_filter_setup);
 
+static unsigned long default_dump_pre_exit;
+
+static int __init coredump_pre_exit_setup(char *s)
+{
+	default_dump_pre_exit =
+		(simple_strtoul(s, NULL, 0) << MMF_DUMP_PRE_EXIT_SHIFT) &
+		MMF_DUMP_PRE_EXIT_MASK;
+	return 1;
+}
+
+__setup("coredump_pre_exit=", coredump_pre_exit_setup);
+
 #include <linux/init_task.h>
 
 static void mm_init_aio(struct mm_struct *mm)
mm/mmap.c +0 -0
index 5754d1c36..b955c47c0 100644

mm/mmap.c void exit_mmap(struct mm_struct *mm) · line 1335

Quoted 10 lines
mm/mmap.c +7 -0
@@ -1326,6 +1326,27 @@ void exit_mmap(struct mm_struct *mm)
 	vm_unacct_memory(nr_accounted);
 }
 
+void exit_mmap_mapped_shared(struct mm_struct *mm)
+{
+	struct vm_area_struct *vma;
+	VMA_ITERATOR(vmi, mm, 0);
+
+	mmap_write_lock(mm);
+	lru_add_drain();

Why?

mm/mmap.c void exit_mmap(struct mm_struct *mm) · line 1337

Quoted 2 lines
mm/mmap.c +2 -0
+
+	for_each_vma(vmi, vma) {

Literally every single VMA? Including the gate VMA too?

No VMA locks... so that's already broken.

mm/mmap.c void exit_mmap(struct mm_struct *mm) · line 1339

Quoted 2 lines
mm/mmap.c +2 -0
+		if (vma->vm_flags & VM_HUGETLB)
+			continue;

That's not how you test for hugetlb.

mm/mmap.c void exit_mmap(struct mm_struct *mm) · line 1340

Quoted 1 line
mm/mmap.c +1 -0
+		if (!(vma->vm_flags & VM_SHARED) || !file_inode(vma->vm_file)->i_nlink)

This isn't how we work with flags any more.

mm/mmap.c void exit_mmap(struct mm_struct *mm) · line 1342

Quoted 2 lines
mm/mmap.c +2 -0
+			continue;
+		vma->vm_file->f_flags |= O_TMPCLOS;

Not sure directly manipulating file flags like this is valid in any way, shape, or form.

mm/mmap.c void exit_mmap(struct mm_struct *mm) · line 1343

Quoted 1 line
mm/mmap.c +1 -0
+		do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);

This is utterly broken, the outer loop will be invalidated by you removing these, do_munmap() has its own iterator...

And this is just madly inefficient. Why wouldn't you just loop over the VMAs to alter flags then unmap the whole range?

But this is also introducing a completely separate, duplicative, version of exit_mmap().

You're not doing any of what that function does. You're just very inefficiently unmapping everything?

mm/mmap.c void exit_mmap(struct mm_struct *mm) · line 1344

Quoted 1 line
mm/mmap.c +1 -0
+		cond_resched();

Of course!

mm/mmap.c void exit_mmap(struct mm_struct *mm) · line 1347

Quoted 3 lines
mm/mmap.c +3 -0
+	}
+
+	mmap_write_unlock(mm);

And VMAs can be mapped again now?

Quoted 8 lines
mm/mmap.c +2 -1
+}
+
 /*
  * Return true if the calling process may expand its vm space by the passed
  * number of pages
--

2.34.1

I'm not sure if this idea can be made upstreamble in any way. But this patch or anything that looks like it or fundamentally alters mm is just not acceptable, sorry.

Lorenzo

author
#12 lore raw
Quoted 19 lines · On Thu, 25 Jun 2026 13:48:10 +0100 Lorenzo Stoakes <[email protected]> wrote:

+cc missing maintainers, lists.

NAK.

This is un-upstreamable for numerous reasons.

The stuff you're doing in mm is broken, wrong and invasive and you've not even bothered to cc- mm people. I'm annoyed by this.

You're also doing incredibly silly mistakes at v4 of something that should have been an RFC.

You don't seem to understand the concept of patch _series_ (break it up into smaller patches!!!) and you haven't bothered cc'ing maintainers whose subsystems you're radically alterting.

I'm annoyed as you have a history where you were told not to add insane hacks before ([0], my reply at [1]).

Quoted 8 lines

[0]:https://lore.kernel.org/all/[email protected]/ [1]:https://lore.kernel.org/all/[email protected]/

Was I wasting my time there? Am I wasting my time responding now?

And how hard is it to run a simple perl script?

Let me run it for you for _just_ the maintainers:

I probably shouldn't reply to this email to waste more of your time, but I can't help but respond because your comments have been very beneficial to me, and I enjoy the process.

The v4 version has changed too much compared to the v3 version. I should have re-executed the "get maintainer" script, but I mistakenly copied the previous email list and sent it out. I sincerely apologize for that.

There are quite a few issues now, and I haven't come up with a good overall solution. I actually want to resolve the problems we encountered in our project with minimal kernel modifications, but I can't think of a good way to do it. It seems that the v4 version has turned out to be a complete disaster of a patch, and I sincerely hope that my example won't be used as a counterexample in the future. Thank you for that.

Suddenly, I have some thoughts about this issue, but I even question whether I should have these ideas. Let me sit down and sort things out properly. I hope the v5 version won't be a disaster.

Thanks
Xin Zhao