Skip to contentSkip to conversation

mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

PATCH Superseded Minchan Kim 5 participants 16 messages Apr 29 – May 15

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

Currently, process_mrelease() requires userspace to send a SIGKILL signal prior to invocation. This separation introduces a scheduling race window where the victim task may receive the signal and enter the exit path before the reaper can invoke process_mrelease().

When the victim enters the exit path (do_exit -> exit_mm), it clears its task->mm immediately. This causes process_mrelease() to fail with -ESRCH, leaving the actual address space teardown (exit_mmap) to be deferred until the mm's reference count drops to zero. In the field (e.g., Android), arbitrary reference counts (reading /proc/<pid>/cmdline, or various other remote VM accesses) frequently delay this teardown indefinitely, defeating the purpose of expedited reclamation.

In Android's LMKD scenarios, this delay keeps memory pressure high, forcing the system to unnecessarily kill additional innocent background apps before the memory from the first victim is recovered.

This patch introduces the PROCESS_MRELEASE_REAP_KILL UAPI flag to support an integrated auto-kill mode. When specified, process_mrelease() directly injects a SIGKILL into the target task after finding its mm.

To solve the race condition, we grab the mm reference via mmgrab() before sending the SIGKILL. If the user passed PROCESS_MRELEASE_REAP_KILL, we assume it will free its memory and proceed with reaping, making the logic as simple as reap = reap_kill || task_will_free_mem(p).

To handle shared address spaces safely in the auto-kill mode, we bail out immediately if the mm is marked with MMF_MULTIPROCESS when PROCESS_MRELEASE_REAP_KILL is specified. This protects existing users of process_mrelease() from behavior changes while preventing unsafe reaping of shared memory.

This policy differs from the global OOM killer, which kills all processes sharing the same mm to guarantee memory reclamation at all costs (preventing system hangs). However, process_mrelease() is invoked by userspace policy. If it fails due to sharing, userspace can simply adapt and select another victim process (such as another background app in Android case) to release memory. We do not need to force success or affect processes that were not targeted.

Fundamentally, this allows process_mrelease() to trigger targeted memory reclaim (via oom_reaper infrastructure) quickly, even if the victim is not yet in the exit path, while reusing existing race handling between reaper and exit_mmap.

---
 include/uapi/linux/mman.h |  4 ++++
 mm/oom_kill.c             | 27 ++++++++++++++++++++-------
 2 files changed, 24 insertions(+), 7 deletions(-)
include/uapi/linux/mman.h +4 -0
index e89d00528f2f..4266976b45ad 100644
@@ -56,4 +56,8 @@ struct cachestat {
 	__u64 nr_recently_evicted;
 };
 
+/* Flags for process_mrelease */
+#define PROCESS_MRELEASE_REAP_KILL	(1 << 0)
+#define PROCESS_MRELEASE_VALID_FLAGS	(PROCESS_MRELEASE_REAP_KILL)
+
 #endif /* _UAPI_LINUX_MMAN_H */
mm/oom_kill.c +20 -7
index 5c6c95c169ee..efa6541b1c47 100644
@@ -20,6 +20,7 @@
 
 #include <linux/oom.h>
 #include <linux/mm.h>
+#include <uapi/linux/mman.h>
 #include <linux/err.h>
 #include <linux/gfp.h>
 #include <linux/sched.h>
@@ -1217,9 +1218,11 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
 	unsigned int f_flags;
 	bool reap = false;
 	long ret = 0;
+	bool reap_kill;
 
-	if (flags)
+	if (flags & ~PROCESS_MRELEASE_VALID_FLAGS)
 		return -EINVAL;
+	reap_kill = !!(flags & PROCESS_MRELEASE_REAP_KILL);
 
 	task = pidfd_get_task(pidfd, &f_flags);
 	if (IS_ERR(task))
@@ -1236,19 +1239,29 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
 	}
 
 	mm = p->mm;
-	mmgrab(mm);
+	if (reap_kill && mm_flags_test(MMF_MULTIPROCESS, mm)) {
+		ret = -EINVAL;
+		task_unlock(p);
+		goto put_task;
+	}
 
-	if (task_will_free_mem(p))
-		reap = true;
-	else {
+	reap = reap_kill || task_will_free_mem(p);
+	if (!reap) {
 		/* Error only if the work has not been done already */
 		if (!mm_flags_test(MMF_OOM_SKIP, mm))
 			ret = -EINVAL;
+		task_unlock(p);
+		goto put_task;
 	}
+
+	mmgrab(mm);
 	task_unlock(p);
 
-	if (!reap)
-		goto drop_mm;
+	if (reap_kill) {
+		ret = kill_pid(task_tgid(task), SIGKILL, 0);
+		if (ret)
+			goto drop_mm;
+	}
 
 	if (mmap_read_lock_killable(mm)) {
 		ret = -EINTR;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
maintainer
#1 lore raw
Quoted 3 lines · Minchan Kim

This policy differs from the global OOM killer, which kills all processes sharing the same mm to guarantee memory reclamation at all costs (preventing system hangs).

Incorrect, we do the same for memcg OOM killer as well. This is not about preventing system hands. But rather to

Quoted 5 lines

However, process_mrelease() is invoked by userspace policy.
If it fails due to sharing, userspace can simply adapt and select another victim process (such as another background app in Android case) to release memory. We do not need to force success or affect processes that were not targeted.

This is a wrong justification for the proposed semantic. You seem to be assuming this is just fine rather than this would be problematic for reasons a), b) and c). If there are no strong reasons _against_ following the global policy then we should stick with it. There are very good reasons why we are doing that on the global level.

If for no other reasons then the proposed semantic severly criples the shared MM case. You are left with a racy kill and call process_mrelease approach. You certainly do not want to allow a simple way for tasks to evade your LMK, do you? So just choose something else is a very bad approach.

So unless you are aware of a specific reason(s) where collective kill is a clearly an incorrect behavior then I believe the proper way is to kill all processes sharing the mm (unless you are crossing any security boundary when doing that).

Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 26 lines · Michal Hocko
Quoted 3 lines · Minchan Kim

This policy differs from the global OOM killer, which kills all processes sharing the same mm to guarantee memory reclamation at all costs (preventing system hangs).

Incorrect, we do the same for memcg OOM killer as well. This is not about preventing system hands. But rather to

Quoted 5 lines

However, process_mrelease() is invoked by userspace policy.
If it fails due to sharing, userspace can simply adapt and select another victim process (such as another background app in Android case) to release memory. We do not need to force success or affect processes that were not targeted.

This is a wrong justification for the proposed semantic. You seem to be assuming this is just fine rather than this would be problematic for reasons a), b) and c). If there are no strong reasons _against_ following the global policy then we should stick with it. There are very good reasons why we are doing that on the global level.

If for no other reasons then the proposed semantic severly criples the shared MM case. You are left with a racy kill and call process_mrelease approach. You certainly do not want to allow a simple way for tasks to evade your LMK, do you? So just choose something else is a very bad approach.

Quoted 4 lines

So unless you are aware of a specific reason(s) where collective kill is a clearly an incorrect behavior then I believe the proper way is to kill all processes sharing the mm (unless you are crossing any security boundary when doing that).

I agree that in the case of a global or memcg OOM, the kernel deals with an emergency, system-wide crisis where killing all sibling processes sharing the same mm is an absolute necessity for system survival, bypassing user-space privilege screening.

However, process_mrelease() is an explicit user-space initiated system call, and I am still hesitant to place that same raw, destructive policy blindly at the UAPI syscall level even though I don't know of any known security issues right now.

If we really want to go that way for the collective kill, at least, we should evaluate signal authorization (kill permission) against *every single* sibling process beforehand instead of only the target task of process_mrelease. Do you agree?

Also, I wonder what the signal/process maintainer thinks about this approach. Christian Brauner <[email protected]>?

maintainer
#3 lore raw
Quoted 32 lines · Minchan Kim
Quoted 26 lines · Michal Hocko
Quoted 3 lines · Minchan Kim

This policy differs from the global OOM killer, which kills all processes sharing the same mm to guarantee memory reclamation at all costs (preventing system hangs).

Incorrect, we do the same for memcg OOM killer as well. This is not about preventing system hands. But rather to

Quoted 5 lines

However, process_mrelease() is invoked by userspace policy.
If it fails due to sharing, userspace can simply adapt and select another victim process (such as another background app in Android case) to release memory. We do not need to force success or affect processes that were not targeted.

This is a wrong justification for the proposed semantic. You seem to be assuming this is just fine rather than this would be problematic for reasons a), b) and c). If there are no strong reasons _against_ following the global policy then we should stick with it. There are very good reasons why we are doing that on the global level.

If for no other reasons then the proposed semantic severly criples the shared MM case. You are left with a racy kill and call process_mrelease approach. You certainly do not want to allow a simple way for tasks to evade your LMK, do you? So just choose something else is a very bad approach.

Quoted 4 lines

So unless you are aware of a specific reason(s) where collective kill is a clearly an incorrect behavior then I believe the proper way is to kill all processes sharing the mm (unless you are crossing any security boundary when doing that).

Quoted 4 lines

I agree that in the case of a global or memcg OOM, the kernel deals with an emergency, system-wide crisis where killing all sibling processes sharing the same mm is an absolute necessity for system survival, bypassing user-space privilege screening.

You are misinterpreting or missing my point. I am not suggesting to cross privilege boundaries. The syscall should fail if the mm is shared with tasks the caller cannot kill (same as it does now).

Quoted 4 lines

However, process_mrelease() is an explicit user-space initiated system call, and I am still hesitant to place that same raw, destructive policy blindly at the UAPI syscall level even though I don't know of any known security issues right now.

This is very wrong argument to introduce a potentially crippled syscall semantic.

Quoted 4 lines

If we really want to go that way for the collective kill, at least, we should evaluate signal authorization (kill permission) against *every single* sibling process beforehand instead of only the target task of process_mrelease. Do you agree?

This is what I've proposed already.

Quoted 2 lines

Also, I wonder what the signal/process maintainer thinks about this approach. Christian Brauner <[email protected]>?

Yes, this makes sense. There might be a very good reason why we might not want to introduce a way to kill cross thread groups when they share mm from userspace. I do not see any as long as you keep the proper permissions for all affected tasks. Maybe we cannot do that sanely now. But these reasons have to be properly documented. You whole argument that this is different from in-kernel oom killing is just not valid.

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 50 lines · Michal Hocko
Quoted 32 lines · Minchan Kim
Quoted 26 lines · Michal Hocko
Quoted 3 lines · Minchan Kim

This policy differs from the global OOM killer, which kills all processes sharing the same mm to guarantee memory reclamation at all costs (preventing system hangs).

Incorrect, we do the same for memcg OOM killer as well. This is not about preventing system hands. But rather to

Quoted 5 lines

However, process_mrelease() is invoked by userspace policy.
If it fails due to sharing, userspace can simply adapt and select another victim process (such as another background app in Android case) to release memory. We do not need to force success or affect processes that were not targeted.

This is a wrong justification for the proposed semantic. You seem to be assuming this is just fine rather than this would be problematic for reasons a), b) and c). If there are no strong reasons _against_ following the global policy then we should stick with it. There are very good reasons why we are doing that on the global level.

If for no other reasons then the proposed semantic severly criples the shared MM case. You are left with a racy kill and call process_mrelease approach. You certainly do not want to allow a simple way for tasks to evade your LMK, do you? So just choose something else is a very bad approach.

Quoted 4 lines

So unless you are aware of a specific reason(s) where collective kill is a clearly an incorrect behavior then I believe the proper way is to kill all processes sharing the mm (unless you are crossing any security boundary when doing that).

Quoted 4 lines

I agree that in the case of a global or memcg OOM, the kernel deals with an emergency, system-wide crisis where killing all sibling processes sharing the same mm is an absolute necessity for system survival, bypassing user-space privilege screening.

You are misinterpreting or missing my point. I am not suggesting to cross privilege boundaries. The syscall should fail if the mm is shared with tasks the caller cannot kill (same as it does now).

Quoted 4 lines

However, process_mrelease() is an explicit user-space initiated system call, and I am still hesitant to place that same raw, destructive policy blindly at the UAPI syscall level even though I don't know of any known security issues right now.

This is very wrong argument to introduce a potentially crippled syscall semantic.

Quoted 6 lines
Quoted 4 lines

If we really want to go that way for the collective kill, at least, we should evaluate signal authorization (kill permission) against *every single* sibling process beforehand instead of only the target task of process_mrelease. Do you agree?

This is what I've proposed already.

Sounds good.

One thing to note is that this approach is still not perfect, as some sibling processes sharing the mm might not be killed due to different UID or SELinux policies while others are. In such cases, the actual memory reaping via process_mrelease() will still fail anyway.

If we are okay with this limitation - meaning it acts as a best-effort approach where we might end up killing some processes without successfully releasing the memory — then I can proceed with this design.

Quoted 10 lines
Quoted 3 lines

Also, I wonder what the signal/process maintainer thinks about this approach. Christian Brauner <[email protected]>?

Yes, this makes sense. There might be a very good reason why we might not want to introduce a way to kill cross thread groups when they share mm from userspace. I do not see any as long as you keep the proper permissions for all affected tasks. Maybe we cannot do that sanely now. But these reasons have to be properly documented. You whole argument that this is different from in-kernel oom killing is just not valid.

Okay, let's wait for any valid reasons or concerns they might raise.

Thanks.

#5 lore raw
In reply to Michal Hocko
Quoted 60 lines · Michal Hocko
Quoted 32 lines · Minchan Kim
Quoted 26 lines · Michal Hocko
Quoted 3 lines · Minchan Kim

This policy differs from the global OOM killer, which kills all processes sharing the same mm to guarantee memory reclamation at all costs (preventing system hangs).

Incorrect, we do the same for memcg OOM killer as well. This is not about preventing system hands. But rather to

Quoted 5 lines

However, process_mrelease() is invoked by userspace policy.
If it fails due to sharing, userspace can simply adapt and select another victim process (such as another background app in Android case) to release memory. We do not need to force success or affect processes that were not targeted.

This is a wrong justification for the proposed semantic. You seem to be assuming this is just fine rather than this would be problematic for reasons a), b) and c). If there are no strong reasons _against_ following the global policy then we should stick with it. There are very good reasons why we are doing that on the global level.

If for no other reasons then the proposed semantic severly criples the shared MM case. You are left with a racy kill and call process_mrelease approach. You certainly do not want to allow a simple way for tasks to evade your LMK, do you? So just choose something else is a very bad approach.

Quoted 4 lines

So unless you are aware of a specific reason(s) where collective kill is a clearly an incorrect behavior then I believe the proper way is to kill all processes sharing the mm (unless you are crossing any security boundary when doing that).

Quoted 4 lines

I agree that in the case of a global or memcg OOM, the kernel deals with an emergency, system-wide crisis where killing all sibling processes sharing the same mm is an absolute necessity for system survival, bypassing user-space privilege screening.

You are misinterpreting or missing my point. I am not suggesting to cross privilege boundaries. The syscall should fail if the mm is shared with tasks the caller cannot kill (same as it does now).

Quoted 4 lines

However, process_mrelease() is an explicit user-space initiated system call, and I am still hesitant to place that same raw, destructive policy blindly at the UAPI syscall level even though I don't know of any known security issues right now.

This is very wrong argument to introduce a potentially crippled syscall semantic.

Quoted 4 lines

If we really want to go that way for the collective kill, at least, we should evaluate signal authorization (kill permission) against *every single* sibling process beforehand instead of only the target task of process_mrelease. Do you agree?

This is what I've proposed already.

Quoted 2 lines

Also, I wonder what the signal/process maintainer thinks about this approach. Christian Brauner <[email protected]>?

Quoted 6 lines

Yes, this makes sense. There might be a very good reason why we might not want to introduce a way to kill cross thread groups when they share mm from userspace. I do not see any as long as you keep the proper permissions for all affected tasks. Maybe we cannot do that sanely now. But these reasons have to be properly documented. You whole argument that this is different from in-kernel oom killing is just not valid.

IIUC, then the OOM kill if invoked from the kernel just takes down without permission checking what it wants to take down. That makes a lot of sense and is mostly safe - after all it is the kernel that initiates the kill.

However, when userspace initiates the kill we need at least the semantics you proposed, Michal. You can only kill processes that you have the necessary privileges over otherwise you end up allowing to SIGKILL setuid binaries over which you hold no privileged possibly generating information leaks or worse.

The other thing to keep in mind is that currently pidfds explicitly do not to allow to signal taks that are outside of their pid namespace hierarchy - see pidfd_send_signal()'s permission checking. I don't want to break these semantics - it's just very bad api design if signaling suddenly behaves differently and pidfd suddenly convey the ability to do a very wide signal scope.

The other thing is that pidfds are handles that can be sent around using SCM_RIGHTS which means they could be forwarded to a container or another privileged user that then initiates kill semantics.

The other thing is that the type of pidfd selects the scope of the signaling operation:

* If the pidfd was created via PIDFD_THREAD then the scope of the signal is by default the individual thread - unless the signal itself is thread-group oriented ofc.

* If the pidfd was created wihout PIDFD_THREAD then the scope of the signal is by default the thread-group.

* pidfd_send_signal() provides explicitly scope overrides:

(1) PIDFD_SIGNAL_THREAD
(2) PIDFD_SIGNAL_THREAD_GROUP
(3) PIDFD_SIGNAL_PROCESS_GROUP

The flags should be mostly self-explanatory.

So I really dislike the idea of now letting the pidfd passed to process_mrelease() to have an implicit scope suddenly. The problem is that this is very opaque to userspace and introduces another way to signal a group of processes.

IOW, I still dislike the fact that process_mrelease() is suddenly turned into a signal sending syscall and I really dislike the fact that it implies a "kill everything with that mm and cross other thread-groups".

I wonder if you couldn't just add PIDFD_SIGNAL_MM_GROUP or something to pidfd_send_signal() instead.

maintainer
#6 lore raw
Quoted 10 lines · Christian Brauner

IIUC, then the OOM kill if invoked from the kernel just takes down without permission checking what it wants to take down. That makes a lot of sense and is mostly safe - after all it is the kernel that initiates the kill.

However, when userspace initiates the kill we need at least the semantics you proposed, Michal. You can only kill processes that you have the necessary privileges over otherwise you end up allowing to SIGKILL setuid binaries over which you hold no privileged possibly generating information leaks or worse.

Agreed!

Quoted 6 lines

The other thing to keep in mind is that currently pidfds explicitly do not to allow to signal taks that are outside of their pid namespace hierarchy - see pidfd_send_signal()'s permission checking. I don't want to break these semantics - it's just very bad api design if signaling suddenly behaves differently and pidfd suddenly convey the ability to do a very wide signal scope.

Agreed!

Quoted 20 lines

The other thing is that pidfds are handles that can be sent around using SCM_RIGHTS which means they could be forwarded to a container or another privileged user that then initiates kill semantics.

The other thing is that the type of pidfd selects the scope of the signaling operation:

* If the pidfd was created via PIDFD_THREAD then the scope of the signal is by default the individual thread - unless the signal itself is thread-group oriented ofc.

* If the pidfd was created wihout PIDFD_THREAD then the scope of the signal is by default the thread-group.

* pidfd_send_signal() provides explicitly scope overrides:

(1) PIDFD_SIGNAL_THREAD
(2) PIDFD_SIGNAL_THREAD_GROUP
(3) PIDFD_SIGNAL_PROCESS_GROUP

Quoted 6 lines

The flags should be mostly self-explanatory.

So I really dislike the idea of now letting the pidfd passed to process_mrelease() to have an implicit scope suddenly. The problem is that this is very opaque to userspace and introduces another way to signal a group of processes.

I do see your point. Unfortunately the whole concept of mm shared across thread (signal) groups is not fitting well into the overall model. For the most usecases this is not a big problem. But oom handlers do care. If you do not kill all owners of the mm you are not releasing any memory.

Quoted 6 lines

IOW, I still dislike the fact that process_mrelease() is suddenly turned into a signal sending syscall and I really dislike the fact that it implies a "kill everything with that mm and cross other thread-groups".

I wonder if you couldn't just add PIDFD_SIGNAL_MM_GROUP or something to pidfd_send_signal() instead.

That would be a clean interface for sure. The thing we are struggling here is not just the killing side of things but also grabbing the mm before it disappears which is the primary reason why process_mrelease is turning into signal sending syscall (which you seem to be not in favor of).

So I can see these options on the table
1) keep process_mrelease as is and live with the race. This sucks because it makes userspace low memory (oom) killers harder to predict.
2) we add the proposed option to kill&release into process_mrelease that is not aware of shared mm case. This sucks because it creates an easy way to evade from the said oom killer
3) same as 2 but add PIDFD_SIGNAL_MM_GROUP that would do the right thing on the signal handling side. You seem to like the idea from the pidfd_send_signal POV but I am not sure you are OK with that being implanted into process_mrelease.

Signature · 2 lines
Michal Hocko
SUSE Labs
#7 lore raw
Quoted 69 lines · Michal Hocko
Quoted 10 lines · Christian Brauner

IIUC, then the OOM kill if invoked from the kernel just takes down without permission checking what it wants to take down. That makes a lot of sense and is mostly safe - after all it is the kernel that initiates the kill.

However, when userspace initiates the kill we need at least the semantics you proposed, Michal. You can only kill processes that you have the necessary privileges over otherwise you end up allowing to SIGKILL setuid binaries over which you hold no privileged possibly generating information leaks or worse.

Agreed!

Quoted 6 lines

The other thing to keep in mind is that currently pidfds explicitly do not to allow to signal taks that are outside of their pid namespace hierarchy - see pidfd_send_signal()'s permission checking. I don't want to break these semantics - it's just very bad api design if signaling suddenly behaves differently and pidfd suddenly convey the ability to do a very wide signal scope.

Agreed!

Quoted 20 lines

The other thing is that pidfds are handles that can be sent around using SCM_RIGHTS which means they could be forwarded to a container or another privileged user that then initiates kill semantics.

The other thing is that the type of pidfd selects the scope of the signaling operation:

* If the pidfd was created via PIDFD_THREAD then the scope of the signal is by default the individual thread - unless the signal itself is thread-group oriented ofc.

* If the pidfd was created wihout PIDFD_THREAD then the scope of the signal is by default the thread-group.

* pidfd_send_signal() provides explicitly scope overrides:

(1) PIDFD_SIGNAL_THREAD
(2) PIDFD_SIGNAL_THREAD_GROUP
(3) PIDFD_SIGNAL_PROCESS_GROUP

Quoted 6 lines

The flags should be mostly self-explanatory.

So I really dislike the idea of now letting the pidfd passed to process_mrelease() to have an implicit scope suddenly. The problem is that this is very opaque to userspace and introduces another way to signal a group of processes.

I do see your point. Unfortunately the whole concept of mm shared across thread (signal) groups is not fitting well into the overall model. For the most usecases this is not a big problem. But oom handlers do care. If you do not kill all owners of the mm you are not releasing any memory.

Quoted 6 lines

IOW, I still dislike the fact that process_mrelease() is suddenly turned into a signal sending syscall and I really dislike the fact that it implies a "kill everything with that mm and cross other thread-groups".

I wonder if you couldn't just add PIDFD_SIGNAL_MM_GROUP or something to pidfd_send_signal() instead.

That would be a clean interface for sure. The thing we are struggling here is not just the killing side of things but also grabbing the mm before it disappears which is the primary reason why process_mrelease is turning into signal sending syscall (which you seem to be not in favor of).

Quoted 10 lines

So I can see these options on the table
1) keep process_mrelease as is and live with the race. This sucks because it makes userspace low memory (oom) killers harder to predict.
2) we add the proposed option to kill&release into process_mrelease that is not aware of shared mm case. This sucks because it creates an easy way to evade from the said oom killer
3) same as 2 but add PIDFD_SIGNAL_MM_GROUP that would do the right thing on the signal handling side. You seem to like the idea from the pidfd_send_signal POV but I am not sure you are OK with that being implanted into process_mrelease.

For 3, maybe something likle this?
(Just to show the concept for further discussion)

---
 include/linux/signal.h    |  4 +++
 include/uapi/linux/mman.h |  4 +++
 kernel/signal.c           | 29 ++++++++++++++++++---
 mm/oom_kill.c             | 55 ++++++++++++++++++++++++++++++++++-----
 4 files changed, 81 insertions(+), 11 deletions(-)
include/linux/signal.h +4 -0
index f19816832f05..bdbe6b3addec 100644
@@ -276,6 +276,8 @@ static inline int valid_signal(unsigned long sig)
 
 struct timespec;
 struct pt_regs;
+struct mm_struct;
+struct pid;
 enum pid_type;
 
 extern int next_signal(struct sigpending *pending, sigset_t *mask);
@@ -283,6 +285,8 @@ extern int do_send_sig_info(int sig, struct kernel_siginfo *info,
 				struct task_struct *p, enum pid_type type);
 extern int group_send_sig_info(int sig, struct kernel_siginfo *info,
 			       struct task_struct *p, enum pid_type type);
+extern int do_pidfd_send_signal_pidns(struct pid *pid, int sig, enum pid_type type,
+				      siginfo_t __user *info, unsigned int flags);
 extern int send_signal_locked(int sig, struct kernel_siginfo *info,
 			      struct task_struct *p, enum pid_type type);
 extern int sigprocmask(int, sigset_t *, sigset_t *);
include/uapi/linux/mman.h +4 -0
index e89d00528f2f..4266976b45ad 100644
@@ -56,4 +56,8 @@ struct cachestat {
 	__u64 nr_recently_evicted;
 };
 
+/* Flags for process_mrelease */
+#define PROCESS_MRELEASE_REAP_KILL	(1 << 0)
+#define PROCESS_MRELEASE_VALID_FLAGS	(PROCESS_MRELEASE_REAP_KILL)
+
 #endif /* _UAPI_LINUX_MMAN_H */
kernel/signal.c +25 -4
index d65d0fe24bfb..b2dc08a9bdd3 100644
@@ -4046,6 +4046,30 @@ static int do_pidfd_send_signal(struct pid *pid, int sig, enum pid_type type,
 	return kill_pid_info_type(sig, &kinfo, pid, type);
 }
 
+/**
+ * do_pidfd_send_signal_pidns - Send a signal to a process via its struct pid
+ *                              while validating PID namespace hierarchy.
+ * @pid:   the struct pid of the target process
+ * @sig:   signal to send
+ * @type:  scope of the signal (e.g. PIDTYPE_TGID)
+ * @info:  signal info payload
+ * @flags: signaling flags
+ *
+ * Verify that the target pid resides inside the caller's PID namespace
+ * hierarchy prior to signal delivery.
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+int do_pidfd_send_signal_pidns(struct pid *pid, int sig, enum pid_type type,
+			       siginfo_t __user *info, unsigned int flags)
+{
+	/* Enforce PID namespace hierarchy boundary */
+	if (!access_pidfd_pidns(pid))
+		return -EINVAL;
+
+	return do_pidfd_send_signal(pid, sig, type, info, flags);
+}
+
 /**
  * sys_pidfd_send_signal - Signal a process through a pidfd
  * @pidfd:  file descriptor of the process
@@ -4094,16 +4118,13 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
 		if (IS_ERR(pid))
 			return PTR_ERR(pid);
 
-		if (!access_pidfd_pidns(pid))
-			return -EINVAL;
-
 		/* Infer scope from the type of pidfd. */
 		if (fd_file(f)->f_flags & PIDFD_THREAD)
 			type = PIDTYPE_PID;
 		else
 			type = PIDTYPE_TGID;
 
-		return do_pidfd_send_signal(pid, sig, type, info, flags);
+		return do_pidfd_send_signal_pidns(pid, sig, type, info, flags);
 	}
 	}
 
mm/oom_kill.c +48 -7

88 lines not on this page · raw message

Signature · 1 line
2.54.0.545.g6539524ca2-goog
#8 lore raw
Quoted 81 lines · Minchan Kim
Quoted 69 lines · Michal Hocko
Quoted 10 lines · Christian Brauner

IIUC, then the OOM kill if invoked from the kernel just takes down without permission checking what it wants to take down. That makes a lot of sense and is mostly safe - after all it is the kernel that initiates the kill.

However, when userspace initiates the kill we need at least the semantics you proposed, Michal. You can only kill processes that you have the necessary privileges over otherwise you end up allowing to SIGKILL setuid binaries over which you hold no privileged possibly generating information leaks or worse.

Agreed!

Quoted 6 lines

The other thing to keep in mind is that currently pidfds explicitly do not to allow to signal taks that are outside of their pid namespace hierarchy - see pidfd_send_signal()'s permission checking. I don't want to break these semantics - it's just very bad api design if signaling suddenly behaves differently and pidfd suddenly convey the ability to do a very wide signal scope.

Agreed!

Quoted 20 lines

The other thing is that pidfds are handles that can be sent around using SCM_RIGHTS which means they could be forwarded to a container or another privileged user that then initiates kill semantics.

The other thing is that the type of pidfd selects the scope of the signaling operation:

* If the pidfd was created via PIDFD_THREAD then the scope of the signal is by default the individual thread - unless the signal itself is thread-group oriented ofc.

* If the pidfd was created wihout PIDFD_THREAD then the scope of the signal is by default the thread-group.

* pidfd_send_signal() provides explicitly scope overrides:

(1) PIDFD_SIGNAL_THREAD
(2) PIDFD_SIGNAL_THREAD_GROUP
(3) PIDFD_SIGNAL_PROCESS_GROUP

Quoted 6 lines

The flags should be mostly self-explanatory.

So I really dislike the idea of now letting the pidfd passed to process_mrelease() to have an implicit scope suddenly. The problem is that this is very opaque to userspace and introduces another way to signal a group of processes.

I do see your point. Unfortunately the whole concept of mm shared across thread (signal) groups is not fitting well into the overall model. For the most usecases this is not a big problem. But oom handlers do care. If you do not kill all owners of the mm you are not releasing any memory.

Quoted 6 lines

IOW, I still dislike the fact that process_mrelease() is suddenly turned into a signal sending syscall and I really dislike the fact that it implies a "kill everything with that mm and cross other thread-groups".

I wonder if you couldn't just add PIDFD_SIGNAL_MM_GROUP or something to pidfd_send_signal() instead.

That would be a clean interface for sure. The thing we are struggling here is not just the killing side of things but also grabbing the mm before it disappears which is the primary reason why process_mrelease is turning into signal sending syscall (which you seem to be not in favor of).

Quoted 10 lines

So I can see these options on the table
1) keep process_mrelease as is and live with the race. This sucks because it makes userspace low memory (oom) killers harder to predict.
2) we add the proposed option to kill&release into process_mrelease that is not aware of shared mm case. This sucks because it creates an easy way to evade from the said oom killer
3) same as 2 but add PIDFD_SIGNAL_MM_GROUP that would do the right thing on the signal handling side. You seem to like the idea from the pidfd_send_signal POV but I am not sure you are OK with that being implanted into process_mrelease.

Quoted 2 lines

For 3, maybe something likle this?
(Just to show the concept for further discussion)

Posted v3 - https://lore.kernel.org/linux-mm/[email protected]/

Thank you.

maintainer
#9 lore raw
Quoted 1 line · Minchan Kim

Posted v3 - https://lore.kernel.org/linux-mm/[email protected]/

Is there any reason to fragment the discussion into yet another email threat? I believe Christian is still catching up after LSFMMBPF last week so give him some time to have a look at this proposal before sending yet another version please.

Signature · 2 lines
Michal Hocko
SUSE Labs
#10 lore raw
Quoted 7 lines · Michal Hocko
Quoted 1 line · Minchan Kim

Posted v3 - https://lore.kernel.org/linux-mm/[email protected]/

Is there any reason to fragment the discussion into yet another email threat? I believe Christian is still catching up after LSFMMBPF last week so give him some time to have a look at this proposal before sending yet another version please.

My only reason for sending v3 earlier was to demonstrate the exact code, hoping the concrete implementation would clarify the intent.

I will gladly wait for his feedback before taking any further steps. Thank you.

#11 lore raw
In reply to Michal Hocko
Quoted 7 lines · Michal Hocko
Quoted 1 line · Minchan Kim

Posted v3 - https://lore.kernel.org/linux-mm/[email protected]/

Is there any reason to fragment the discussion into yet another email threat? I believe Christian is still catching up after LSFMMBPF last week so give him some time to have a look at this proposal before sending yet another version please.

Yeah, thanks. Frankly, it's very hard to follow what's going on in this patch series. Let me see if I can find the latest posting.

maintainer
#12 lore raw
In reply to Minchan Kim
Quoted 4 lines · On Wed, 29 Apr 2026 14:13:59 -0700 Minchan Kim <[email protected]> wrote:

Currently, process_mrelease() requires userspace to send a SIGKILL signal prior to invocation. This separation introduces a scheduling race window where the victim task may receive the signal and enter the exit path before the reaper can invoke process_mrelease().

Does process_mrelease() have a manpage? My googling was a fail.

maintainer
#13 lore raw
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Wed, 29 Apr 2026 14:13:59 -0700 Minchan Kim <[email protected]> wrote:

Currently, process_mrelease() requires userspace to send a SIGKILL signal prior to invocation. This separation introduces a scheduling race window where the victim task may receive the signal and enter the exit path before the reaper can invoke process_mrelease().

Does process_mrelease() have a manpage? My googling was a fail.

It does. Very well hidden in 884a7e5964e06

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#14 lore raw
Quoted 11 lines · On Thu, 30 Apr 2026 17:32:40 +0200 Michal Hocko <[email protected]> wrote:
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Wed, 29 Apr 2026 14:13:59 -0700 Minchan Kim <[email protected]> wrote:

Currently, process_mrelease() requires userspace to send a SIGKILL signal prior to invocation. This separation introduces a scheduling race window where the victim task may receive the signal and enter the exit path before the reaper can invoke process_mrelease().

Does process_mrelease() have a manpage? My googling was a fail.

It does. Very well hidden in 884a7e5964e06

Well, that didn't appear to make it into the manpages project and it doesn't describe the expected usage: need to kill the process first. But I guess all the needed info is in tools/testing/selftests/mm/mrelease_test.c.

https://lwn.net/Articles/864184/ is useful.

#15 lore raw
Quoted 16 lines · Andrew Morton
Quoted 11 lines · On Thu, 30 Apr 2026 17:32:40 +0200 Michal Hocko <[email protected]> wrote:
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Wed, 29 Apr 2026 14:13:59 -0700 Minchan Kim <[email protected]> wrote:

Currently, process_mrelease() requires userspace to send a SIGKILL signal prior to invocation. This separation introduces a scheduling race window where the victim task may receive the signal and enter the exit path before the reaper can invoke process_mrelease().

Does process_mrelease() have a manpage? My googling was a fail.

It does. Very well hidden in 884a7e5964e06

Quoted 6 lines

Well, that didn't appear to make it into the manpages project and it doesn't describe the expected usage: need to kill the process first. But I guess all the needed info is in tools/testing/selftests/mm/mrelease_test.c.

https://lwn.net/Articles/864184/ is useful.

I'll try to carve out some time to post a proper manpage for it. Thanks for pointing this out!