Skip to contentSkip to conversation

coredump: exit_files() in coredump_wait() if MMF_DUMP_MAPPED_SHARED is not set

PATCH Superseded Xin Zhao 2 participants 4 messages Jun 18 – Jun 19

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Xin Zhao

author
#0 lore raw

PATCH v2 1/1 coredump: exit_files() in coredump_wait() if MMF_DUMP_MAPPED_SHARED is not set

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.

To address this, call exit_files() in the end of coredump_wait(), if MMF_DUMP_MAPPED_SHARED is not set.

---

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.

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

---
 fs/coredump.c | 3 +++
 1 file changed, 3 insertions(+)
fs/coredump.c +3 -0
index bb6fdb1f4..e20baf44f 100644
@@ -548,6 +548,9 @@ static int coredump_wait(int exit_code, struct core_state *core_state)
 		}
 	}
 
+	if (!mm_flags_test(MMF_DUMP_MAPPED_SHARED, tsk->mm))
+		exit_files(tsk);
+
 	return core_waiters;
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 32 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.

To address this, call exit_files() in the end of coredump_wait(), if MMF_DUMP_MAPPED_SHARED is not set.

---

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.

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

---
 fs/coredump.c | 3 +++
 1 file changed, 3 insertions(+)
fs/coredump.c +0 -0
index bb6fdb1f4..e20baf44f 100644

fs/coredump.c static int coredump_wait(int exit_code, struct core_state *core_state) · line 552

Quoted 5 lines
fs/coredump.c +2 -0
@@ -548,6 +548,9 @@ static int coredump_wait(int exit_code, struct core_state *core_state)
 		}
 	}
 
+	if (!mm_flags_test(MMF_DUMP_MAPPED_SHARED, tsk->mm))
+		exit_files(tsk);

Memory mapped files keep their own separate references to the files (in struct vm_area_struct::vm_file), so there is no need to attempt to work around this. Unless I'm misunderstanding what you're attempting to work around.

Signature · 1 line
Pedro
#2 lore raw
Quoted 36 lines · Pedro Falcato
Quoted 35 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.

To address this, call exit_files() in the end of coredump_wait(), if MMF_DUMP_MAPPED_SHARED is not set.

---

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.

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

---
 fs/coredump.c | 3 +++
 1 file changed, 3 insertions(+)
fs/coredump.c +0 -0
index bb6fdb1f4..e20baf44f 100644
@@ -548,6 +548,9 @@ static int coredump_wait(int exit_code, struct core_state *core_state)
 		}
 	}
 
Quoted 7 lines

fs/coredump.c static int coredump_wait(int exit_code, struct core_state *core_state) · line 552

Quoted 2 lines
fs/coredump.c +2 -0
+	if (!mm_flags_test(MMF_DUMP_MAPPED_SHARED, tsk->mm))
+		exit_files(tsk);

Memory mapped files keep their own separate references to the files (in struct vm_area_struct::vm_file), so there is no need to attempt to work around this. Unless I'm misunderstanding what you're attempting to work around.

Waiit, I think I get it - you have a flock on a file, and you're scared that if you unlock early, some other process can lock it and modify some other file we have mapped? If so, that does make some sense. Please add that as a comment and/or into the git log, because it feels very much non-obvious to me.

Signature · 1 line
Pedro
author
#3 lore raw
Quoted 8 lines · On Thu, 18 Jun 2026 18:19:16 +0100 Pedro Falcato <[email protected]> wrote:
Quoted 7 lines

fs/coredump.c static int coredump_wait(int exit_code, struct core_state *core_state) · line 552

Quoted 2 lines
fs/coredump.c +2 -0
+	if (!mm_flags_test(MMF_DUMP_MAPPED_SHARED, tsk->mm))
+		exit_files(tsk);

Memory mapped files keep their own separate references to the files (in struct vm_area_struct::vm_file), so there is no need to attempt to work around this. Unless I'm misunderstanding what you're attempting to work around.

Quoted 5 lines

Waiit, I think I get it - you have a flock on a file, and you're scared that if you unlock early, some other process can lock it and modify some other file we have mapped? If so, that does make some sense. Please add that as a comment and/or into the git log, because it feels very much non-obvious to me.

I did forget that mmap also increases the reference count, but the case you mentioned is one we also want to work around. I will add this information to both the commit log and the comments as below:

Early unlocking a flock on a file allows other processes to lock and modify the mapped data protected by the flock.

Thanks
Xin Zhao