Skip to contentSkip to conversation

mm: introduce process_reap system call

PATCH Superseded Suren Baghdasaryan 10 participants 35 messages Jun 23, 2021 – Jul 12, 2021

Subsystems (18)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: introduce process_reap system call

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

Previously I proposed a number of alternatives to accomplish this:
- https://lore.kernel.org/patchwork/patch/1060407 extending pidfd_send_signal to allow memory reaping using oom_reaper thread;
- https://lore.kernel.org/patchwork/patch/1338196 extending pidfd_send_signal to reap memory of the target process synchronously from the context of the caller;
- https://lore.kernel.org/patchwork/patch/1344419/ to add MADV_DONTNEED support for process_madvise implementing synchronous memory reaping.

The end of the last discussion culminated with suggestion to introduce a dedicated system call (https://lore.kernel.org/patchwork/patch/1344418/#1553875)
The reasoning was that the new variant of process_madvise
a) does not work on an address range
b) is destructive
c) doesn't share much code at all with the rest of process_madvise
From the userspace point of view it was awkward and inconvenient to provide memory range for this operation that operates on the entire address space. Using special flags or address values to specify the entire address space was too hacky.

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

---
 arch/alpha/kernel/syscalls/syscall.tbl      |  1 +
 arch/arm/tools/syscall.tbl                  |  1 +
 arch/arm64/include/asm/unistd.h             |  2 +-
 arch/arm64/include/asm/unistd32.h           |  2 +
 arch/ia64/kernel/syscalls/syscall.tbl       |  1 +
 arch/m68k/kernel/syscalls/syscall.tbl       |  1 +
 arch/microblaze/kernel/syscalls/syscall.tbl |  1 +
 arch/mips/kernel/syscalls/syscall_n32.tbl   |  1 +
 arch/mips/kernel/syscalls/syscall_n64.tbl   |  1 +
 arch/mips/kernel/syscalls/syscall_o32.tbl   |  1 +
 arch/parisc/kernel/syscalls/syscall.tbl     |  1 +
 arch/powerpc/kernel/syscalls/syscall.tbl    |  1 +
 arch/s390/kernel/syscalls/syscall.tbl       |  1 +
 arch/sh/kernel/syscalls/syscall.tbl         |  1 +
 arch/sparc/kernel/syscalls/syscall.tbl      |  1 +
 arch/x86/entry/syscalls/syscall_32.tbl      |  1 +
 arch/x86/entry/syscalls/syscall_64.tbl      |  1 +
 arch/xtensa/kernel/syscalls/syscall.tbl     |  1 +
 include/linux/syscalls.h                    |  1 +
 include/uapi/asm-generic/unistd.h           |  4 +-
 kernel/sys_ni.c                             |  1 +
 mm/oom_kill.c                               | 50 +++++++++++++++++++++
 22 files changed, 74 insertions(+), 2 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
index 3000a2e8ee21..14b9e81d2fc4 100644
@@ -486,3 +486,4 @@
 554	common	landlock_create_ruleset		sys_landlock_create_ruleset
 555	common	landlock_add_rule		sys_landlock_add_rule
 556	common	landlock_restrict_self		sys_landlock_restrict_self
+557	common	process_reap			sys_process_reap
arch/arm/tools/syscall.tbl +1 -0
index 28e03b5fec00..889b78d0f63f 100644
@@ -460,3 +460,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/arm64/include/asm/unistd.h +1 -1
index 727bfc3be99b..fb7a0be2f3d9 100644
@@ -38,7 +38,7 @@
 #define __ARM_NR_compat_set_tls		(__ARM_NR_COMPAT_BASE + 5)
 #define __ARM_NR_COMPAT_END		(__ARM_NR_COMPAT_BASE + 0x800)
 
-#define __NR_compat_syscalls		447
+#define __NR_compat_syscalls		448
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +2 -0
index 5dab69d2c22b..80593454173e 100644
@@ -900,6 +900,8 @@ __SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset)
 __SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule)
 #define __NR_landlock_restrict_self 446
 __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)
+#define __NR_process_reap 447
+__SYSCALL(__NR_process_reap, sys_process_reap)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
index bb11fe4c875a..6c94feedf086 100644
@@ -367,3 +367,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
index 79c2d24c89dd..e80a7fa55696 100644
@@ -446,3 +446,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
index b11395a20c20..511b2bd61fc1 100644
@@ -452,3 +452,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
index 9220909526f9..1775704c6a24 100644
@@ -385,3 +385,4 @@
 444	n32	landlock_create_ruleset		sys_landlock_create_ruleset
 445	n32	landlock_add_rule		sys_landlock_add_rule
 446	n32	landlock_restrict_self		sys_landlock_restrict_self
+447	n32	process_reap			sys_process_reap
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
index 9cd1c34f31b5..d769daca3f79 100644
@@ -361,3 +361,4 @@
 444	n64	landlock_create_ruleset		sys_landlock_create_ruleset
 445	n64	landlock_add_rule		sys_landlock_add_rule
 446	n64	landlock_restrict_self		sys_landlock_restrict_self
+447	n64	process_reap			sys_process_reap
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
index d560c467a8c6..1bd2fc056677 100644
@@ -434,3 +434,4 @@
 444	o32	landlock_create_ruleset		sys_landlock_create_ruleset
 445	o32	landlock_add_rule		sys_landlock_add_rule
 446	o32	landlock_restrict_self		sys_landlock_restrict_self
+447	o32	process_reap			sys_process_reap
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
index aabc37f8cae3..0012561ca557 100644
@@ -444,3 +444,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
index 8f052ff4058c..89cbcc732b18 100644
@@ -526,3 +526,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/s390/kernel/syscalls/syscall.tbl +1 -0
index 0690263df1dd..7ebd4d809b5e 100644
@@ -449,3 +449,4 @@
 444  common	landlock_create_ruleset	sys_landlock_create_ruleset	sys_landlock_create_ruleset
 445  common	landlock_add_rule	sys_landlock_add_rule		sys_landlock_add_rule
 446  common	landlock_restrict_self	sys_landlock_restrict_self	sys_landlock_restrict_self
+447  common	process_reap		sys_process_reap		sys_process_reap
arch/sh/kernel/syscalls/syscall.tbl +1 -0
index 0b91499ebdcf..178fd47b372e 100644
@@ -449,3 +449,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
index e34cc30ef22c..faee121b7ae2 100644
@@ -492,3 +492,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index 4bbc267fb36b..cbe070de9884 100644
@@ -451,3 +451,4 @@
 444	i386	landlock_create_ruleset	sys_landlock_create_ruleset
 445	i386	landlock_add_rule	sys_landlock_add_rule
 446	i386	landlock_restrict_self	sys_landlock_restrict_self
+447	i386	process_reap		sys_process_reap
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index ce18119ea0d0..e6765646731b 100644
@@ -368,6 +368,7 @@
 444	common	landlock_create_ruleset	sys_landlock_create_ruleset
 445	common	landlock_add_rule	sys_landlock_add_rule
 446	common	landlock_restrict_self	sys_landlock_restrict_self
+447	common	process_reap		sys_process_reap
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
index fd2f30227d96..f0e9dbee1a5b 100644
@@ -417,3 +417,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
include/linux/syscalls.h +1 -0
index 050511e8f1f8..b6659e09bf0d 100644
@@ -915,6 +915,7 @@ asmlinkage long sys_mincore(unsigned long start, size_t len,
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
 asmlinkage long sys_process_madvise(int pidfd, const struct iovec __user *vec,
 			size_t vlen, int behavior, unsigned int flags);
+asmlinkage long sys_process_reap(int pidfd, unsigned int flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
 			unsigned long flags);
include/uapi/asm-generic/unistd.h +3 -1
index d2a942086fcb..b3bf57b928af 100644
@@ -871,9 +871,11 @@ __SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset)
 __SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule)
 #define __NR_landlock_restrict_self 446
 __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)
+#define __NR_process_reap 447
+__SYSCALL(__NR_process_reap, sys_process_reap)
 
 #undef __NR_syscalls
-#define __NR_syscalls 447
+#define __NR_syscalls 448
 
 /*
  * 32 bit systems traditionally used different
kernel/sys_ni.c +1 -0
index 0ea8128468c3..56eb7c9f8356 100644
@@ -289,6 +289,7 @@ COND_SYSCALL(munlockall);
 COND_SYSCALL(mincore);
 COND_SYSCALL(madvise);
 COND_SYSCALL(process_madvise);
+COND_SYSCALL(process_reap);
 COND_SYSCALL(remap_file_pages);
 COND_SYSCALL(mbind);
 COND_SYSCALL_COMPAT(mbind);
mm/oom_kill.c +50 -0

59 lines not on this page · raw message

Signature · 1 line
2.32.0.93.g670b81a890-goog
#1 lore raw
Quoted 52 lines · Suren Baghdasaryan

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

Previously I proposed a number of alternatives to accomplish this:
- https://lore.kernel.org/patchwork/patch/1060407 extending pidfd_send_signal to allow memory reaping using oom_reaper thread;
- https://lore.kernel.org/patchwork/patch/1338196 extending pidfd_send_signal to reap memory of the target process synchronously from the context of the caller;
- https://lore.kernel.org/patchwork/patch/1344419/ to add MADV_DONTNEED support for process_madvise implementing synchronous memory reaping.

The end of the last discussion culminated with suggestion to introduce a dedicated system call (https://lore.kernel.org/patchwork/patch/1344418/#1553875)
The reasoning was that the new variant of process_madvise
a) does not work on an address range
b) is destructive
c) doesn't share much code at all with the rest of process_madvise
From the userspace point of view it was awkward and inconvenient to provide memory range for this operation that operates on the entire address space. Using special flags or address values to specify the entire address space was too hacky.

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

Quoted 7 lines

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

I noticed that the patch does not apply to linux-next because of the new memfd_secret syscall introduced on x86 architecture only. It still applies to Linus' ToT. If needed I can change it to apply on top of linux-next.

Quoted 308 lines
---
 arch/alpha/kernel/syscalls/syscall.tbl      |  1 +
 arch/arm/tools/syscall.tbl                  |  1 +
 arch/arm64/include/asm/unistd.h             |  2 +-
 arch/arm64/include/asm/unistd32.h           |  2 +
 arch/ia64/kernel/syscalls/syscall.tbl       |  1 +
 arch/m68k/kernel/syscalls/syscall.tbl       |  1 +
 arch/microblaze/kernel/syscalls/syscall.tbl |  1 +
 arch/mips/kernel/syscalls/syscall_n32.tbl   |  1 +
 arch/mips/kernel/syscalls/syscall_n64.tbl   |  1 +
 arch/mips/kernel/syscalls/syscall_o32.tbl   |  1 +
 arch/parisc/kernel/syscalls/syscall.tbl     |  1 +
 arch/powerpc/kernel/syscalls/syscall.tbl    |  1 +
 arch/s390/kernel/syscalls/syscall.tbl       |  1 +
 arch/sh/kernel/syscalls/syscall.tbl         |  1 +
 arch/sparc/kernel/syscalls/syscall.tbl      |  1 +
 arch/x86/entry/syscalls/syscall_32.tbl      |  1 +
 arch/x86/entry/syscalls/syscall_64.tbl      |  1 +
 arch/xtensa/kernel/syscalls/syscall.tbl     |  1 +
 include/linux/syscalls.h                    |  1 +
 include/uapi/asm-generic/unistd.h           |  4 +-
 kernel/sys_ni.c                             |  1 +
 mm/oom_kill.c                               | 50 +++++++++++++++++++++
 22 files changed, 74 insertions(+), 2 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
index 3000a2e8ee21..14b9e81d2fc4 100644
@@ -486,3 +486,4 @@
 554    common  landlock_create_ruleset         sys_landlock_create_ruleset
 555    common  landlock_add_rule               sys_landlock_add_rule
 556    common  landlock_restrict_self          sys_landlock_restrict_self
+557    common  process_reap                    sys_process_reap
arch/arm/tools/syscall.tbl +1 -0
index 28e03b5fec00..889b78d0f63f 100644
@@ -460,3 +460,4 @@
 444    common  landlock_create_ruleset         sys_landlock_create_ruleset
 445    common  landlock_add_rule               sys_landlock_add_rule
 446    common  landlock_restrict_self          sys_landlock_restrict_self
+447    common  process_reap                    sys_process_reap
arch/arm64/include/asm/unistd.h +1 -1
index 727bfc3be99b..fb7a0be2f3d9 100644
@@ -38,7 +38,7 @@
 #define __ARM_NR_compat_set_tls                (__ARM_NR_COMPAT_BASE + 5)
 #define __ARM_NR_COMPAT_END            (__ARM_NR_COMPAT_BASE + 0x800)
 
-#define __NR_compat_syscalls           447
+#define __NR_compat_syscalls           448
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +2 -0
index 5dab69d2c22b..80593454173e 100644
@@ -900,6 +900,8 @@ __SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset)
 __SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule)
 #define __NR_landlock_restrict_self 446
 __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)
+#define __NR_process_reap 447
+__SYSCALL(__NR_process_reap, sys_process_reap)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
index bb11fe4c875a..6c94feedf086 100644
@@ -367,3 +367,4 @@
 444    common  landlock_create_ruleset         sys_landlock_create_ruleset
 445    common  landlock_add_rule               sys_landlock_add_rule
 446    common  landlock_restrict_self          sys_landlock_restrict_self
+447    common  process_reap                    sys_process_reap
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
index 79c2d24c89dd..e80a7fa55696 100644
@@ -446,3 +446,4 @@
 444    common  landlock_create_ruleset         sys_landlock_create_ruleset
 445    common  landlock_add_rule               sys_landlock_add_rule
 446    common  landlock_restrict_self          sys_landlock_restrict_self
+447    common  process_reap                    sys_process_reap
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
index b11395a20c20..511b2bd61fc1 100644
@@ -452,3 +452,4 @@
 444    common  landlock_create_ruleset         sys_landlock_create_ruleset
 445    common  landlock_add_rule               sys_landlock_add_rule
 446    common  landlock_restrict_self          sys_landlock_restrict_self
+447    common  process_reap                    sys_process_reap
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
index 9220909526f9..1775704c6a24 100644
@@ -385,3 +385,4 @@
 444    n32     landlock_create_ruleset         sys_landlock_create_ruleset
 445    n32     landlock_add_rule               sys_landlock_add_rule
 446    n32     landlock_restrict_self          sys_landlock_restrict_self
+447    n32     process_reap                    sys_process_reap
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
index 9cd1c34f31b5..d769daca3f79 100644
@@ -361,3 +361,4 @@
 444    n64     landlock_create_ruleset         sys_landlock_create_ruleset
 445    n64     landlock_add_rule               sys_landlock_add_rule
 446    n64     landlock_restrict_self          sys_landlock_restrict_self
+447    n64     process_reap                    sys_process_reap
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
index d560c467a8c6..1bd2fc056677 100644
@@ -434,3 +434,4 @@
 444    o32     landlock_create_ruleset         sys_landlock_create_ruleset
 445    o32     landlock_add_rule               sys_landlock_add_rule
 446    o32     landlock_restrict_self          sys_landlock_restrict_self
+447    o32     process_reap                    sys_process_reap
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
index aabc37f8cae3..0012561ca557 100644
@@ -444,3 +444,4 @@
 444    common  landlock_create_ruleset         sys_landlock_create_ruleset
 445    common  landlock_add_rule               sys_landlock_add_rule
 446    common  landlock_restrict_self          sys_landlock_restrict_self
+447    common  process_reap                    sys_process_reap
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
index 8f052ff4058c..89cbcc732b18 100644
@@ -526,3 +526,4 @@
 444    common  landlock_create_ruleset         sys_landlock_create_ruleset
 445    common  landlock_add_rule               sys_landlock_add_rule
 446    common  landlock_restrict_self          sys_landlock_restrict_self
+447    common  process_reap                    sys_process_reap
arch/s390/kernel/syscalls/syscall.tbl +1 -0
index 0690263df1dd..7ebd4d809b5e 100644
@@ -449,3 +449,4 @@
 444  common    landlock_create_ruleset sys_landlock_create_ruleset     sys_landlock_create_ruleset
 445  common    landlock_add_rule       sys_landlock_add_rule           sys_landlock_add_rule
 446  common    landlock_restrict_self  sys_landlock_restrict_self      sys_landlock_restrict_self
+447  common    process_reap            sys_process_reap                sys_process_reap
arch/sh/kernel/syscalls/syscall.tbl +1 -0
index 0b91499ebdcf..178fd47b372e 100644
@@ -449,3 +449,4 @@
 444    common  landlock_create_ruleset         sys_landlock_create_ruleset
 445    common  landlock_add_rule               sys_landlock_add_rule
 446    common  landlock_restrict_self          sys_landlock_restrict_self
+447    common  process_reap                    sys_process_reap
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
index e34cc30ef22c..faee121b7ae2 100644
@@ -492,3 +492,4 @@
 444    common  landlock_create_ruleset         sys_landlock_create_ruleset
 445    common  landlock_add_rule               sys_landlock_add_rule
 446    common  landlock_restrict_self          sys_landlock_restrict_self
+447    common  process_reap                    sys_process_reap
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index 4bbc267fb36b..cbe070de9884 100644
@@ -451,3 +451,4 @@
 444    i386    landlock_create_ruleset sys_landlock_create_ruleset
 445    i386    landlock_add_rule       sys_landlock_add_rule
 446    i386    landlock_restrict_self  sys_landlock_restrict_self
+447    i386    process_reap            sys_process_reap
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index ce18119ea0d0..e6765646731b 100644
@@ -368,6 +368,7 @@
 444    common  landlock_create_ruleset sys_landlock_create_ruleset
 445    common  landlock_add_rule       sys_landlock_add_rule
 446    common  landlock_restrict_self  sys_landlock_restrict_self
+447    common  process_reap            sys_process_reap
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
index fd2f30227d96..f0e9dbee1a5b 100644
@@ -417,3 +417,4 @@
 444    common  landlock_create_ruleset         sys_landlock_create_ruleset
 445    common  landlock_add_rule               sys_landlock_add_rule
 446    common  landlock_restrict_self          sys_landlock_restrict_self
+447    common  process_reap                    sys_process_reap
include/linux/syscalls.h +1 -0
index 050511e8f1f8..b6659e09bf0d 100644
@@ -915,6 +915,7 @@ asmlinkage long sys_mincore(unsigned long start, size_t len,
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
 asmlinkage long sys_process_madvise(int pidfd, const struct iovec __user *vec,
                        size_t vlen, int behavior, unsigned int flags);
+asmlinkage long sys_process_reap(int pidfd, unsigned int flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
                        unsigned long prot, unsigned long pgoff,
                        unsigned long flags);
include/uapi/asm-generic/unistd.h +3 -1
index d2a942086fcb..b3bf57b928af 100644
@@ -871,9 +871,11 @@ __SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset)
 __SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule)
 #define __NR_landlock_restrict_self 446
 __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)
+#define __NR_process_reap 447
+__SYSCALL(__NR_process_reap, sys_process_reap)
 
 #undef __NR_syscalls
-#define __NR_syscalls 447
+#define __NR_syscalls 448
 
 /*
  * 32 bit systems traditionally used different
kernel/sys_ni.c +1 -0
index 0ea8128468c3..56eb7c9f8356 100644
@@ -289,6 +289,7 @@ COND_SYSCALL(munlockall);
 COND_SYSCALL(mincore);
 COND_SYSCALL(madvise);
 COND_SYSCALL(process_madvise);
+COND_SYSCALL(process_reap);
 COND_SYSCALL(remap_file_pages);
 COND_SYSCALL(mbind);
 COND_SYSCALL_COMPAT(mbind);
mm/oom_kill.c +50 -1
index eefd3f5fde46..0f85a0442fa5 100644
@@ -28,6 +28,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/debug.h>
 #include <linux/swap.h>
+#include <linux/syscalls.h>
 #include <linux/timex.h>
 #include <linux/jiffies.h>
 #include <linux/cpuset.h>
@@ -1141,3 +1142,52 @@ void pagefault_out_of_memory(void)
        out_of_memory(&oc);
        mutex_unlock(&oom_lock);
 }
+
+SYSCALL_DEFINE2(process_reap, int, pidfd, unsigned int, flags)
+{
+       struct pid *pid;
+       struct task_struct *task;
+       struct mm_struct *mm = NULL;
+       unsigned int f_flags;
+       long ret = 0;
+
+       if (flags != 0)
+               return -EINVAL;
+
+       pid = pidfd_get_pid(pidfd, &f_flags);
+       if (IS_ERR(pid))
+               return PTR_ERR(pid);
+
+       task = get_pid_task(pid, PIDTYPE_PID);
+       if (!task) {
+               ret = -ESRCH;
+               goto put_pid;
+       }
+
+       /*
+        * If the task is dying and in the process of releasing its memory
+        * then get its mm.
+        */
+       task_lock(task);
+       if (task_will_free_mem(task) && (task->flags & PF_KTHREAD) == 0) {
+               mm = task->mm;
+               mmget(mm);
+       }
+       task_unlock(task);
+       if (!mm) {
+               ret = -EINVAL;
+               goto put_task;
+       }
+
+       mmap_read_lock(mm);
+       if (!__oom_reap_task_mm(mm))
+               ret = -EAGAIN;
+       mmap_read_unlock(mm);
+
+       mmput(mm);
+put_task:
+       put_task_struct(task);
+put_pid:
+       put_pid(pid);
+       return ret;
+}
--

2.32.0.93.g670b81a890-goog

#2 lore raw
In reply to Suren Baghdasaryan
I love your patch! Yet something to improve:

Hi Suren,

I love your patch! Yet something to improve:

[auto build test ERROR on arm64/for-next/core]
[also build test ERROR on tip/x86/asm linus/master v5.13-rc7] [cannot apply to hnaz-linux-mm/master next-20210623]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Suren-Baghdasaryan/mm-introduce-process_reap-system-call/20210624-032923
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
config: sh-allmodconfig (attached as .config)
compiler: sh4-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/64bd00602e706bc1b2e2267ca1434646c0900fa1
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Suren-Baghdasaryan/mm-introduce-process_reap-system-call/20210624-032923 git checkout 64bd00602e706bc1b2e2267ca1434646c0900fa1
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

sh4-linux-ld: mm/oom_kill.o: in function `sys_process_reap':

>> (.text+0x3528): undefined reference to `__oom_reap_task_mm'

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for SND_ATMEL_SOC_PDC
Depends on SOUND && !UML && SND && SND_SOC && SND_ATMEL_SOC && HAS_DMA Selected by
- SND_ATMEL_SOC_SSC && SOUND && !UML && SND && SND_SOC && SND_ATMEL_SOC
- SND_ATMEL_SOC_SSC_PDC && SOUND && !UML && SND && SND_SOC && SND_ATMEL_SOC && ATMEL_SSC

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw
In reply to Suren Baghdasaryan
I love your patch! Perhaps something to improve:

Hi Suren,

I love your patch! Perhaps something to improve:

[auto build test WARNING on arm64/for-next/core]
[also build test WARNING on tip/x86/asm linus/master v5.13-rc7] [cannot apply to hnaz-linux-mm/master next-20210623]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Suren-Baghdasaryan/mm-introduce-process_reap-system-call/20210624-032923
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
config: arm64-randconfig-r006-20210622 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 7c8a507272587f181ec29401453949ebcd8fec65)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/64bd00602e706bc1b2e2267ca1434646c0900fa1
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Suren-Baghdasaryan/mm-introduce-process_reap-system-call/20210624-032923 git checkout 64bd00602e706bc1b2e2267ca1434646c0900fa1
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:152:1: note: expanded from here
   __arm64_sys_msync
   ^
   kernel/sys_ni.c:284:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:285:1: warning: no previous prototype for function '__arm64_sys_mlock' [-Wmissing-prototypes]
   COND_SYSCALL(mlock);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:153:1: note: expanded from here
   __arm64_sys_mlock
   ^
   kernel/sys_ni.c:285:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:286:1: warning: no previous prototype for function '__arm64_sys_munlock' [-Wmissing-prototypes]
   COND_SYSCALL(munlock);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:154:1: note: expanded from here
   __arm64_sys_munlock
   ^
   kernel/sys_ni.c:286:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:287:1: warning: no previous prototype for function '__arm64_sys_mlockall' [-Wmissing-prototypes]
   COND_SYSCALL(mlockall);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:155:1: note: expanded from here
   __arm64_sys_mlockall
   ^
   kernel/sys_ni.c:287:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:288:1: warning: no previous prototype for function '__arm64_sys_munlockall' [-Wmissing-prototypes]
   COND_SYSCALL(munlockall);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:156:1: note: expanded from here
   __arm64_sys_munlockall
   ^
   kernel/sys_ni.c:288:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:289:1: warning: no previous prototype for function '__arm64_sys_mincore' [-Wmissing-prototypes]
   COND_SYSCALL(mincore);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:157:1: note: expanded from here
   __arm64_sys_mincore
   ^
   kernel/sys_ni.c:289:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:290:1: warning: no previous prototype for function '__arm64_sys_madvise' [-Wmissing-prototypes]
   COND_SYSCALL(madvise);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:158:1: note: expanded from here
   __arm64_sys_madvise
   ^
   kernel/sys_ni.c:290:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:291:1: warning: no previous prototype for function '__arm64_sys_process_madvise' [-Wmissing-prototypes]
   COND_SYSCALL(process_madvise);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:159:1: note: expanded from here
   __arm64_sys_process_madvise
   ^
   kernel/sys_ni.c:291:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
>> kernel/sys_ni.c:292:1: warning: no previous prototype for function '__arm64_sys_process_reap' [-Wmissing-prototypes]
   COND_SYSCALL(process_reap);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:160:1: note: expanded from here
   __arm64_sys_process_reap
   ^
   kernel/sys_ni.c:292:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:293:1: warning: no previous prototype for function '__arm64_sys_remap_file_pages' [-Wmissing-prototypes]
   COND_SYSCALL(remap_file_pages);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:161:1: note: expanded from here
   __arm64_sys_remap_file_pages
   ^
   kernel/sys_ni.c:293:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:294:1: warning: no previous prototype for function '__arm64_sys_mbind' [-Wmissing-prototypes]
   COND_SYSCALL(mbind);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:162:1: note: expanded from here
   __arm64_sys_mbind
   ^
   kernel/sys_ni.c:294:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:295:1: warning: no previous prototype for function '__arm64_compat_sys_mbind' [-Wmissing-prototypes]
   COND_SYSCALL_COMPAT(mbind);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:41:25: note: expanded from macro 'COND_SYSCALL_COMPAT'
           asmlinkage long __weak __arm64_compat_sys_##name(const struct pt_regs *regs)    \
                                  ^
   <scratch space>:163:1: note: expanded from here
   __arm64_compat_sys_mbind
   ^
   kernel/sys_ni.c:295:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:41:13: note: expanded from macro 'COND_SYSCALL_COMPAT'
           asmlinkage long __weak __arm64_compat_sys_##name(const struct pt_regs *regs)    \
                      ^
   kernel/sys_ni.c:296:1: warning: no previous prototype for function '__arm64_sys_get_mempolicy' [-Wmissing-prototypes]
   COND_SYSCALL(get_mempolicy);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:164:1: note: expanded from here
   __arm64_sys_get_mempolicy
   ^
   kernel/sys_ni.c:296:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:297:1: warning: no previous prototype for function '__arm64_compat_sys_get_mempolicy' [-Wmissing-prototypes]
   COND_SYSCALL_COMPAT(get_mempolicy);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:41:25: note: expanded from macro 'COND_SYSCALL_COMPAT'
           asmlinkage long __weak __arm64_compat_sys_##name(const struct pt_regs *regs)    \
                                  ^
   <scratch space>:165:1: note: expanded from here
   __arm64_compat_sys_get_mempolicy
   ^
   kernel/sys_ni.c:297:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:41:13: note: expanded from macro 'COND_SYSCALL_COMPAT'
           asmlinkage long __weak __arm64_compat_sys_##name(const struct pt_regs *regs)    \
                      ^
   kernel/sys_ni.c:298:1: warning: no previous prototype for function '__arm64_sys_set_mempolicy' [-Wmissing-prototypes]
   COND_SYSCALL(set_mempolicy);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                                  ^
   <scratch space>:166:1: note: expanded from here
   __arm64_sys_set_mempolicy
   ^
   kernel/sys_ni.c:298:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
           asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs)   \
                      ^
   kernel/sys_ni.c:299:1: warning: no previous prototype for function '__arm64_compat_sys_set_mempolicy' [-Wmissing-prototypes]
   COND_SYSCALL_COMPAT(set_mempolicy);
   ^
   arch/arm64/include/asm/syscall_wrapper.h:41:25: note: expanded from macro 'COND_SYSCALL_COMPAT'
           asmlinkage long __weak __arm64_compat_sys_##name(const struct pt_regs *regs)    \
                                  ^

<scratch space>:167:1: note: expanded from here
__arm64_compat_sys_set_mempolicy
^
kernel/sys_ni.c:299:1: note: declare 'static' if the function is not intended to be used outside of this translation unit

vim +/__arm64_sys_process_reap +292 kernel/sys_ni.c

279
280 /* mm/, CONFIG_MMU only */
281 COND_SYSCALL(swapon);
282 COND_SYSCALL(swapoff);
283 COND_SYSCALL(mprotect);
284 COND_SYSCALL(msync);
285 COND_SYSCALL(mlock);
286 COND_SYSCALL(munlock);
287 COND_SYSCALL(mlockall);
288 COND_SYSCALL(munlockall);
289 COND_SYSCALL(mincore);
290 COND_SYSCALL(madvise);
291 COND_SYSCALL(process_madvise);

 > 292	COND_SYSCALL(process_reap);

293 COND_SYSCALL(remap_file_pages);
294 COND_SYSCALL(mbind);
295 COND_SYSCALL_COMPAT(mbind);
296 COND_SYSCALL(get_mempolicy);
297 COND_SYSCALL_COMPAT(get_mempolicy);
298 COND_SYSCALL(set_mempolicy);
299 COND_SYSCALL_COMPAT(set_mempolicy);
300 COND_SYSCALL(migrate_pages);
301 COND_SYSCALL_COMPAT(migrate_pages);
302 COND_SYSCALL(move_pages);
303 COND_SYSCALL_COMPAT(move_pages);
304

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#4 lore raw
In reply to Suren Baghdasaryan
I love your patch! Yet something to improve:

Hi Suren,

I love your patch! Yet something to improve:

[auto build test ERROR on arm64/for-next/core]
[also build test ERROR on tip/x86/asm linus/master v5.13-rc7] [cannot apply to hnaz-linux-mm/master next-20210623]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Suren-Baghdasaryan/mm-introduce-process_reap-system-call/20210624-032923
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
config: arm-randconfig-s032-20210622 (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-341-g8af24329-dirty
# https://github.com/0day-ci/linux/commit/64bd00602e706bc1b2e2267ca1434646c0900fa1
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Suren-Baghdasaryan/mm-introduce-process_reap-system-call/20210624-032923 git checkout 64bd00602e706bc1b2e2267ca1434646c0900fa1
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' W=1 ARCH=arm

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

arm-linux-gnueabi-ld: mm/oom_kill.o: in function `__se_sys_process_reap':

>> oom_kill.c:(.text+0x157e): undefined reference to `__oom_reap_task_mm'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#5 lore raw
Quoted 28 lines · kernel test robot

Hi Suren,

I love your patch! Yet something to improve:

[auto build test ERROR on arm64/for-next/core]
[also build test ERROR on tip/x86/asm linus/master v5.13-rc7] [cannot apply to hnaz-linux-mm/master next-20210623]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Suren-Baghdasaryan/mm-introduce-process_reap-system-call/20210624-032923
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
config: arm-randconfig-s032-20210622 (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-341-g8af24329-dirty
# https://github.com/0day-ci/linux/commit/64bd00602e706bc1b2e2267ca1434646c0900fa1
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Suren-Baghdasaryan/mm-introduce-process_reap-system-call/20210624-032923 git checkout 64bd00602e706bc1b2e2267ca1434646c0900fa1
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' W=1 ARCH=arm

Quoted 7 lines

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

arm-linux-gnueabi-ld: mm/oom_kill.o: in function `__se_sys_process_reap':

Quoted 1 line
Quoted 1 line

oom_kill.c:(.text+0x157e): undefined reference to `__oom_reap_task_mm'

Thanks kernel test robot! I missed that __oom_reap_task_mm is undefined when CONFIG_MMU=n. Will fix it in the next revision. I think returning -ENOSYS in this case would be appropriate for the syscall.

Quoted 4 lines

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#6 lore raw
In reply to Suren Baghdasaryan
Quoted 311 lines · Suren Baghdasaryan

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

Previously I proposed a number of alternatives to accomplish this:
- https://lore.kernel.org/patchwork/patch/1060407 extending pidfd_send_signal to allow memory reaping using oom_reaper thread;
- https://lore.kernel.org/patchwork/patch/1338196 extending pidfd_send_signal to reap memory of the target process synchronously from the context of the caller;
- https://lore.kernel.org/patchwork/patch/1344419/ to add MADV_DONTNEED support for process_madvise implementing synchronous memory reaping.

The end of the last discussion culminated with suggestion to introduce a dedicated system call (https://lore.kernel.org/patchwork/patch/1344418/#1553875)
The reasoning was that the new variant of process_madvise
a) does not work on an address range
b) is destructive
c) doesn't share much code at all with the rest of process_madvise
From the userspace point of view it was awkward and inconvenient to provide memory range for this operation that operates on the entire address space. Using special flags or address values to specify the entire address space was too hacky.

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

---
 arch/alpha/kernel/syscalls/syscall.tbl      |  1 +
 arch/arm/tools/syscall.tbl                  |  1 +
 arch/arm64/include/asm/unistd.h             |  2 +-
 arch/arm64/include/asm/unistd32.h           |  2 +
 arch/ia64/kernel/syscalls/syscall.tbl       |  1 +
 arch/m68k/kernel/syscalls/syscall.tbl       |  1 +
 arch/microblaze/kernel/syscalls/syscall.tbl |  1 +
 arch/mips/kernel/syscalls/syscall_n32.tbl   |  1 +
 arch/mips/kernel/syscalls/syscall_n64.tbl   |  1 +
 arch/mips/kernel/syscalls/syscall_o32.tbl   |  1 +
 arch/parisc/kernel/syscalls/syscall.tbl     |  1 +
 arch/powerpc/kernel/syscalls/syscall.tbl    |  1 +
 arch/s390/kernel/syscalls/syscall.tbl       |  1 +
 arch/sh/kernel/syscalls/syscall.tbl         |  1 +
 arch/sparc/kernel/syscalls/syscall.tbl      |  1 +
 arch/x86/entry/syscalls/syscall_32.tbl      |  1 +
 arch/x86/entry/syscalls/syscall_64.tbl      |  1 +
 arch/xtensa/kernel/syscalls/syscall.tbl     |  1 +
 include/linux/syscalls.h                    |  1 +
 include/uapi/asm-generic/unistd.h           |  4 +-
 kernel/sys_ni.c                             |  1 +
 mm/oom_kill.c                               | 50 +++++++++++++++++++++
 22 files changed, 74 insertions(+), 2 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
index 3000a2e8ee21..14b9e81d2fc4 100644
@@ -486,3 +486,4 @@
 554	common	landlock_create_ruleset		sys_landlock_create_ruleset
 555	common	landlock_add_rule		sys_landlock_add_rule
 556	common	landlock_restrict_self		sys_landlock_restrict_self
+557	common	process_reap			sys_process_reap
arch/arm/tools/syscall.tbl +1 -0
index 28e03b5fec00..889b78d0f63f 100644
@@ -460,3 +460,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/arm64/include/asm/unistd.h +1 -1
index 727bfc3be99b..fb7a0be2f3d9 100644
@@ -38,7 +38,7 @@
 #define __ARM_NR_compat_set_tls		(__ARM_NR_COMPAT_BASE + 5)
 #define __ARM_NR_COMPAT_END		(__ARM_NR_COMPAT_BASE + 0x800)
 
-#define __NR_compat_syscalls		447
+#define __NR_compat_syscalls		448
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +2 -0
index 5dab69d2c22b..80593454173e 100644
@@ -900,6 +900,8 @@ __SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset)
 __SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule)
 #define __NR_landlock_restrict_self 446
 __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)
+#define __NR_process_reap 447
+__SYSCALL(__NR_process_reap, sys_process_reap)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
index bb11fe4c875a..6c94feedf086 100644
@@ -367,3 +367,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
index 79c2d24c89dd..e80a7fa55696 100644
@@ -446,3 +446,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
index b11395a20c20..511b2bd61fc1 100644
@@ -452,3 +452,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
index 9220909526f9..1775704c6a24 100644
@@ -385,3 +385,4 @@
 444	n32	landlock_create_ruleset		sys_landlock_create_ruleset
 445	n32	landlock_add_rule		sys_landlock_add_rule
 446	n32	landlock_restrict_self		sys_landlock_restrict_self
+447	n32	process_reap			sys_process_reap
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
index 9cd1c34f31b5..d769daca3f79 100644
@@ -361,3 +361,4 @@
 444	n64	landlock_create_ruleset		sys_landlock_create_ruleset
 445	n64	landlock_add_rule		sys_landlock_add_rule
 446	n64	landlock_restrict_self		sys_landlock_restrict_self
+447	n64	process_reap			sys_process_reap
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
index d560c467a8c6..1bd2fc056677 100644
@@ -434,3 +434,4 @@
 444	o32	landlock_create_ruleset		sys_landlock_create_ruleset
 445	o32	landlock_add_rule		sys_landlock_add_rule
 446	o32	landlock_restrict_self		sys_landlock_restrict_self
+447	o32	process_reap			sys_process_reap
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
index aabc37f8cae3..0012561ca557 100644
@@ -444,3 +444,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
index 8f052ff4058c..89cbcc732b18 100644
@@ -526,3 +526,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/s390/kernel/syscalls/syscall.tbl +1 -0
index 0690263df1dd..7ebd4d809b5e 100644
@@ -449,3 +449,4 @@
 444  common	landlock_create_ruleset	sys_landlock_create_ruleset	sys_landlock_create_ruleset
 445  common	landlock_add_rule	sys_landlock_add_rule		sys_landlock_add_rule
 446  common	landlock_restrict_self	sys_landlock_restrict_self	sys_landlock_restrict_self
+447  common	process_reap		sys_process_reap		sys_process_reap
arch/sh/kernel/syscalls/syscall.tbl +1 -0
index 0b91499ebdcf..178fd47b372e 100644
@@ -449,3 +449,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
index e34cc30ef22c..faee121b7ae2 100644
@@ -492,3 +492,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index 4bbc267fb36b..cbe070de9884 100644
@@ -451,3 +451,4 @@
 444	i386	landlock_create_ruleset	sys_landlock_create_ruleset
 445	i386	landlock_add_rule	sys_landlock_add_rule
 446	i386	landlock_restrict_self	sys_landlock_restrict_self
+447	i386	process_reap		sys_process_reap
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index ce18119ea0d0..e6765646731b 100644
@@ -368,6 +368,7 @@
 444	common	landlock_create_ruleset	sys_landlock_create_ruleset
 445	common	landlock_add_rule	sys_landlock_add_rule
 446	common	landlock_restrict_self	sys_landlock_restrict_self
+447	common	process_reap		sys_process_reap
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
index fd2f30227d96..f0e9dbee1a5b 100644
@@ -417,3 +417,4 @@
 444	common	landlock_create_ruleset		sys_landlock_create_ruleset
 445	common	landlock_add_rule		sys_landlock_add_rule
 446	common	landlock_restrict_self		sys_landlock_restrict_self
+447	common	process_reap			sys_process_reap
include/linux/syscalls.h +1 -0
index 050511e8f1f8..b6659e09bf0d 100644
@@ -915,6 +915,7 @@ asmlinkage long sys_mincore(unsigned long start, size_t len,
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
 asmlinkage long sys_process_madvise(int pidfd, const struct iovec __user *vec,
 			size_t vlen, int behavior, unsigned int flags);
+asmlinkage long sys_process_reap(int pidfd, unsigned int flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
 			unsigned long prot, unsigned long pgoff,
 			unsigned long flags);
include/uapi/asm-generic/unistd.h +3 -1
index d2a942086fcb..b3bf57b928af 100644
@@ -871,9 +871,11 @@ __SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset)
 __SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule)
 #define __NR_landlock_restrict_self 446
 __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)
+#define __NR_process_reap 447
+__SYSCALL(__NR_process_reap, sys_process_reap)
 
 #undef __NR_syscalls
-#define __NR_syscalls 447
+#define __NR_syscalls 448
 
 /*
  * 32 bit systems traditionally used different
kernel/sys_ni.c +1 -0
index 0ea8128468c3..56eb7c9f8356 100644
@@ -289,6 +289,7 @@ COND_SYSCALL(munlockall);
 COND_SYSCALL(mincore);
 COND_SYSCALL(madvise);
 COND_SYSCALL(process_madvise);
+COND_SYSCALL(process_reap);
 COND_SYSCALL(remap_file_pages);
 COND_SYSCALL(mbind);
 COND_SYSCALL_COMPAT(mbind);
mm/oom_kill.c +1 -0
index eefd3f5fde46..0f85a0442fa5 100644
@@ -28,6 +28,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/debug.h>
 #include <linux/swap.h>
+#include <linux/syscalls.h>
 #include <linux/timex.h>
 #include <linux/jiffies.h>
 #include <linux/cpuset.h>

mm/oom_kill.c void pagefault_out_of_memory(void) · line 1146

Quoted 5 lines
mm/oom_kill.c +2 -0
@@ -1141,3 +1142,52 @@ void pagefault_out_of_memory(void)
 	out_of_memory(&oc);
 	mutex_unlock(&oom_lock);
 }
+
+SYSCALL_DEFINE2(process_reap, int, pidfd, unsigned int, flags)

Hey Suren,

Wouldn't
- process_memory_reap()
- process_reap_memory()
- process_mreap()
be better names?

Quoted 9 lines
mm/oom_kill.c +9 -0
+{
+	struct pid *pid;
+	struct task_struct *task;
+	struct mm_struct *mm = NULL;
+	unsigned int f_flags;
+	long ret = 0;
+
+	if (flags != 0)
+		return -EINVAL;

mm/oom_kill.c void pagefault_out_of_memory(void) · line 1165

Quoted 10 lines
mm/oom_kill.c +10 -0
+
+	pid = pidfd_get_pid(pidfd, &f_flags);
+	if (IS_ERR(pid))
+		return PTR_ERR(pid);
+
+	task = get_pid_task(pid, PIDTYPE_PID);
+	if (!task) {
+		ret = -ESRCH;
+		goto put_pid;
+	}

You have a similar pattern in process_madvise():

	pid = pidfd_get_pid(pidfd, &f_flags);
	if (IS_ERR(pid)) {
		ret = PTR_ERR(pid);
		goto free_iov;
	}

	task = get_pid_task(pid, PIDTYPE_PID);
	if (!task) {
		ret = -ESRCH;
		goto put_pid;
	}

I'd suggest you add a tiny helper to kernel/pid.c and call it in both places.

#7 lore raw
Quoted 321 lines · Christian Brauner
Quoted 311 lines · Suren Baghdasaryan

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

Previously I proposed a number of alternatives to accomplish this:
- https://lore.kernel.org/patchwork/patch/1060407 extending pidfd_send_signal to allow memory reaping using oom_reaper thread;
- https://lore.kernel.org/patchwork/patch/1338196 extending pidfd_send_signal to reap memory of the target process synchronously from the context of the caller;
- https://lore.kernel.org/patchwork/patch/1344419/ to add MADV_DONTNEED support for process_madvise implementing synchronous memory reaping.

The end of the last discussion culminated with suggestion to introduce a dedicated system call (https://lore.kernel.org/patchwork/patch/1344418/#1553875)
The reasoning was that the new variant of process_madvise
a) does not work on an address range
b) is destructive
c) doesn't share much code at all with the rest of process_madvise
From the userspace point of view it was awkward and inconvenient to provide memory range for this operation that operates on the entire address space. Using special flags or address values to specify the entire address space was too hacky.

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

---
 arch/alpha/kernel/syscalls/syscall.tbl      |  1 +
 arch/arm/tools/syscall.tbl                  |  1 +
 arch/arm64/include/asm/unistd.h             |  2 +-
 arch/arm64/include/asm/unistd32.h           |  2 +
 arch/ia64/kernel/syscalls/syscall.tbl       |  1 +
 arch/m68k/kernel/syscalls/syscall.tbl       |  1 +
 arch/microblaze/kernel/syscalls/syscall.tbl |  1 +
 arch/mips/kernel/syscalls/syscall_n32.tbl   |  1 +
 arch/mips/kernel/syscalls/syscall_n64.tbl   |  1 +
 arch/mips/kernel/syscalls/syscall_o32.tbl   |  1 +
 arch/parisc/kernel/syscalls/syscall.tbl     |  1 +
 arch/powerpc/kernel/syscalls/syscall.tbl    |  1 +
 arch/s390/kernel/syscalls/syscall.tbl       |  1 +
 arch/sh/kernel/syscalls/syscall.tbl         |  1 +
 arch/sparc/kernel/syscalls/syscall.tbl      |  1 +
 arch/x86/entry/syscalls/syscall_32.tbl      |  1 +
 arch/x86/entry/syscalls/syscall_64.tbl      |  1 +
 arch/xtensa/kernel/syscalls/syscall.tbl     |  1 +
 include/linux/syscalls.h                    |  1 +
 include/uapi/asm-generic/unistd.h           |  4 +-
 kernel/sys_ni.c                             |  1 +
 mm/oom_kill.c                               | 50 +++++++++++++++++++++
 22 files changed, 74 insertions(+), 2 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
index 3000a2e8ee21..14b9e81d2fc4 100644
@@ -486,3 +486,4 @@
 554  common  landlock_create_ruleset         sys_landlock_create_ruleset
 555  common  landlock_add_rule               sys_landlock_add_rule
 556  common  landlock_restrict_self          sys_landlock_restrict_self
+557  common  process_reap                    sys_process_reap
arch/arm/tools/syscall.tbl +1 -0
index 28e03b5fec00..889b78d0f63f 100644
@@ -460,3 +460,4 @@
 444  common  landlock_create_ruleset         sys_landlock_create_ruleset
 445  common  landlock_add_rule               sys_landlock_add_rule
 446  common  landlock_restrict_self          sys_landlock_restrict_self
+447  common  process_reap                    sys_process_reap
arch/arm64/include/asm/unistd.h +1 -1
index 727bfc3be99b..fb7a0be2f3d9 100644
@@ -38,7 +38,7 @@
 #define __ARM_NR_compat_set_tls              (__ARM_NR_COMPAT_BASE + 5)
 #define __ARM_NR_COMPAT_END          (__ARM_NR_COMPAT_BASE + 0x800)
 
-#define __NR_compat_syscalls         447
+#define __NR_compat_syscalls         448
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +2 -0
index 5dab69d2c22b..80593454173e 100644
@@ -900,6 +900,8 @@ __SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset)
 __SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule)
 #define __NR_landlock_restrict_self 446
 __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)
+#define __NR_process_reap 447
+__SYSCALL(__NR_process_reap, sys_process_reap)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
index bb11fe4c875a..6c94feedf086 100644
@@ -367,3 +367,4 @@
 444  common  landlock_create_ruleset         sys_landlock_create_ruleset
 445  common  landlock_add_rule               sys_landlock_add_rule
 446  common  landlock_restrict_self          sys_landlock_restrict_self
+447  common  process_reap                    sys_process_reap
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
index 79c2d24c89dd..e80a7fa55696 100644
@@ -446,3 +446,4 @@
 444  common  landlock_create_ruleset         sys_landlock_create_ruleset
 445  common  landlock_add_rule               sys_landlock_add_rule
 446  common  landlock_restrict_self          sys_landlock_restrict_self
+447  common  process_reap                    sys_process_reap
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
index b11395a20c20..511b2bd61fc1 100644
@@ -452,3 +452,4 @@
 444  common  landlock_create_ruleset         sys_landlock_create_ruleset
 445  common  landlock_add_rule               sys_landlock_add_rule
 446  common  landlock_restrict_self          sys_landlock_restrict_self
+447  common  process_reap                    sys_process_reap
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
index 9220909526f9..1775704c6a24 100644
@@ -385,3 +385,4 @@
 444  n32     landlock_create_ruleset         sys_landlock_create_ruleset
 445  n32     landlock_add_rule               sys_landlock_add_rule
 446  n32     landlock_restrict_self          sys_landlock_restrict_self
+447  n32     process_reap                    sys_process_reap
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
index 9cd1c34f31b5..d769daca3f79 100644
@@ -361,3 +361,4 @@
 444  n64     landlock_create_ruleset         sys_landlock_create_ruleset
 445  n64     landlock_add_rule               sys_landlock_add_rule
 446  n64     landlock_restrict_self          sys_landlock_restrict_self
+447  n64     process_reap                    sys_process_reap
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
index d560c467a8c6..1bd2fc056677 100644
@@ -434,3 +434,4 @@
 444  o32     landlock_create_ruleset         sys_landlock_create_ruleset
 445  o32     landlock_add_rule               sys_landlock_add_rule
 446  o32     landlock_restrict_self          sys_landlock_restrict_self
+447  o32     process_reap                    sys_process_reap
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
index aabc37f8cae3..0012561ca557 100644
@@ -444,3 +444,4 @@
 444  common  landlock_create_ruleset         sys_landlock_create_ruleset
 445  common  landlock_add_rule               sys_landlock_add_rule
 446  common  landlock_restrict_self          sys_landlock_restrict_self
+447  common  process_reap                    sys_process_reap
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
index 8f052ff4058c..89cbcc732b18 100644
@@ -526,3 +526,4 @@
 444  common  landlock_create_ruleset         sys_landlock_create_ruleset
 445  common  landlock_add_rule               sys_landlock_add_rule
 446  common  landlock_restrict_self          sys_landlock_restrict_self
+447  common  process_reap                    sys_process_reap
arch/s390/kernel/syscalls/syscall.tbl +1 -0
index 0690263df1dd..7ebd4d809b5e 100644
@@ -449,3 +449,4 @@
 444  common  landlock_create_ruleset sys_landlock_create_ruleset     sys_landlock_create_ruleset
 445  common  landlock_add_rule       sys_landlock_add_rule           sys_landlock_add_rule
 446  common  landlock_restrict_self  sys_landlock_restrict_self      sys_landlock_restrict_self
+447  common  process_reap            sys_process_reap                sys_process_reap
arch/sh/kernel/syscalls/syscall.tbl +1 -0
index 0b91499ebdcf..178fd47b372e 100644
@@ -449,3 +449,4 @@
 444  common  landlock_create_ruleset         sys_landlock_create_ruleset
 445  common  landlock_add_rule               sys_landlock_add_rule
 446  common  landlock_restrict_self          sys_landlock_restrict_self
+447  common  process_reap                    sys_process_reap
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
index e34cc30ef22c..faee121b7ae2 100644
@@ -492,3 +492,4 @@
 444  common  landlock_create_ruleset         sys_landlock_create_ruleset
 445  common  landlock_add_rule               sys_landlock_add_rule
 446  common  landlock_restrict_self          sys_landlock_restrict_self
+447  common  process_reap                    sys_process_reap
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index 4bbc267fb36b..cbe070de9884 100644
@@ -451,3 +451,4 @@
 444  i386    landlock_create_ruleset sys_landlock_create_ruleset
 445  i386    landlock_add_rule       sys_landlock_add_rule
 446  i386    landlock_restrict_self  sys_landlock_restrict_self
+447  i386    process_reap            sys_process_reap
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index ce18119ea0d0..e6765646731b 100644
@@ -368,6 +368,7 @@
 444  common  landlock_create_ruleset sys_landlock_create_ruleset
 445  common  landlock_add_rule       sys_landlock_add_rule
 446  common  landlock_restrict_self  sys_landlock_restrict_self
+447  common  process_reap            sys_process_reap
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
index fd2f30227d96..f0e9dbee1a5b 100644
@@ -417,3 +417,4 @@
 444  common  landlock_create_ruleset         sys_landlock_create_ruleset
 445  common  landlock_add_rule               sys_landlock_add_rule
 446  common  landlock_restrict_self          sys_landlock_restrict_self
+447  common  process_reap                    sys_process_reap
include/linux/syscalls.h +1 -0
index 050511e8f1f8..b6659e09bf0d 100644
@@ -915,6 +915,7 @@ asmlinkage long sys_mincore(unsigned long start, size_t len,
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
 asmlinkage long sys_process_madvise(int pidfd, const struct iovec __user *vec,
                      size_t vlen, int behavior, unsigned int flags);
+asmlinkage long sys_process_reap(int pidfd, unsigned int flags);
 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
                      unsigned long prot, unsigned long pgoff,
                      unsigned long flags);
include/uapi/asm-generic/unistd.h +3 -1
index d2a942086fcb..b3bf57b928af 100644
@@ -871,9 +871,11 @@ __SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset)
 __SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule)
 #define __NR_landlock_restrict_self 446
 __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)
+#define __NR_process_reap 447
+__SYSCALL(__NR_process_reap, sys_process_reap)
 
 #undef __NR_syscalls
-#define __NR_syscalls 447
+#define __NR_syscalls 448
 
 /*
  * 32 bit systems traditionally used different
kernel/sys_ni.c +1 -0
index 0ea8128468c3..56eb7c9f8356 100644
@@ -289,6 +289,7 @@ COND_SYSCALL(munlockall);
 COND_SYSCALL(mincore);
 COND_SYSCALL(madvise);
 COND_SYSCALL(process_madvise);
+COND_SYSCALL(process_reap);
 COND_SYSCALL(remap_file_pages);
 COND_SYSCALL(mbind);
 COND_SYSCALL_COMPAT(mbind);
mm/oom_kill.c +1 -0
index eefd3f5fde46..0f85a0442fa5 100644
@@ -28,6 +28,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/debug.h>
 #include <linux/swap.h>
+#include <linux/syscalls.h>
 #include <linux/timex.h>
 #include <linux/jiffies.h>
 #include <linux/cpuset.h>

mm/oom_kill.c void pagefault_out_of_memory(void) · line 1146

Quoted 5 lines
mm/oom_kill.c +2 -0
@@ -1141,3 +1142,52 @@ void pagefault_out_of_memory(void)
      out_of_memory(&oc);
      mutex_unlock(&oom_lock);
 }
+
+SYSCALL_DEFINE2(process_reap, int, pidfd, unsigned int, flags)

Hey Suren,

Quoted 5 lines

Wouldn't
- process_memory_reap()
- process_reap_memory()
- process_mreap()
be better names?

Hi Christian,
I'm open to other names, whichever sounds better. From the list process_reap_memory() sounds best to me but I'm open to others as well.

Quoted 35 lines
Quoted 10 lines
mm/oom_kill.c +9 -0
+{
+     struct pid *pid;
+     struct task_struct *task;
+     struct mm_struct *mm = NULL;
+     unsigned int f_flags;
+     long ret = 0;
+
+     if (flags != 0)
+             return -EINVAL;

mm/oom_kill.c void pagefault_out_of_memory(void) · line 1165

Quoted 10 lines
mm/oom_kill.c +10 -0
+
+     pid = pidfd_get_pid(pidfd, &f_flags);
+     if (IS_ERR(pid))
+             return PTR_ERR(pid);
+
+     task = get_pid_task(pid, PIDTYPE_PID);
+     if (!task) {
+             ret = -ESRCH;
+             goto put_pid;
+     }

You have a similar pattern in process_madvise():

        pid = pidfd_get_pid(pidfd, &f_flags);
        if (IS_ERR(pid)) {
                ret = PTR_ERR(pid);
                goto free_iov;
        }

        task = get_pid_task(pid, PIDTYPE_PID);
        if (!task) {
                ret = -ESRCH;
                goto put_pid;
        }
Quoted 2 lines

I'd suggest you add a tiny helper to kernel/pid.c and call it in both places.

Agree. I'll post the new rev next week to give some more time for reviews of this version.
Thanks!

Quoted 4 lines

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

#8 lore raw
In reply to Suren Baghdasaryan

Hi Suren,

Quoted 44 lines · Suren Baghdasaryan

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

Previously I proposed a number of alternatives to accomplish this:
- https://lore.kernel.org/patchwork/patch/1060407 extending pidfd_send_signal to allow memory reaping using oom_reaper thread;
- https://lore.kernel.org/patchwork/patch/1338196 extending pidfd_send_signal to reap memory of the target process synchronously from the context of the caller;
- https://lore.kernel.org/patchwork/patch/1344419/ to add MADV_DONTNEED support for process_madvise implementing synchronous memory reaping.

The end of the last discussion culminated with suggestion to introduce a dedicated system call (https://lore.kernel.org/patchwork/patch/1344418/#1553875)
The reasoning was that the new variant of process_madvise
a) does not work on an address range
b) is destructive
c) doesn't share much code at all with the rest of process_madvise
From the userspace point of view it was awkward and inconvenient to provide memory range for this operation that operates on the entire address space. Using special flags or address values to specify the entire address space was too hacky.

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

Quoted 7 lines

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

*pidfd_open

Quoted 9 lines

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

Thanks for continuously pushing this. One question I have is how do you envision this syscall to be used for the cgroup based workloads. Traverse the target tree, read pids from cgroup.procs files, pidfd_open them, send SIGKILL and then process_reap them. Is that right?

Orthogonal to this patch I wonder if we should have an optimized way to reap processes from a cgroup. Something similar to cgroup.kill (or maybe overload cgroup.kill with reaping as well).

[...]

Quoted 18 lines
mm/oom_kill.c +18 -0
+
+SYSCALL_DEFINE2(process_reap, int, pidfd, unsigned int, flags)
+{
+       struct pid *pid;
+       struct task_struct *task;
+       struct mm_struct *mm = NULL;
+       unsigned int f_flags;
+       long ret = 0;
+
+       if (flags != 0)
+               return -EINVAL;
+
+       pid = pidfd_get_pid(pidfd, &f_flags);
+       if (IS_ERR(pid))
+               return PTR_ERR(pid);
+
+       task = get_pid_task(pid, PIDTYPE_PID);
+       if (!task) {

mm/oom_kill.c void pagefault_out_of_memory(void) · line 1172

Quoted 10 lines
mm/oom_kill.c +10 -0
+               ret = -ESRCH;
+               goto put_pid;
+       }
+
+       /*
+        * If the task is dying and in the process of releasing its memory
+        * then get its mm.
+        */
+       task_lock(task);
+       if (task_will_free_mem(task) && (task->flags & PF_KTHREAD) == 0) {

task_will_free_mem() is fine here but I think in parallel we should optimize this function. At the moment it is traversing all the processes on the machine. It is very normal to have tens of thousands of processes on big machines, so it would be really costly when reaping a bunch of processes.

maintainer
#12 lore raw
In reply to Suren Baghdasaryan
Quoted 11 lines · Suren Baghdasaryan

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core

Quoted 8 lines

the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

At the risk of asking a potentially silly question, should this just be a file in procfs?

Also, please consider removing all mention of the word "reap" from the user API. For better or for worse, "reap" in UNIX refers to what happens when a dead task gets wait()ed. I sincerely wish I could go back in time and gently encourage whomever invented that particular abomination to change their mind, but my time machine doesn't work.

--Andy

#9 lore raw
In reply to Shakeel Butt
Quoted 52 lines · Shakeel Butt

Hi Suren,

Quoted 48 lines · Suren Baghdasaryan

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

Previously I proposed a number of alternatives to accomplish this:
- https://lore.kernel.org/patchwork/patch/1060407 extending pidfd_send_signal to allow memory reaping using oom_reaper thread;
- https://lore.kernel.org/patchwork/patch/1338196 extending pidfd_send_signal to reap memory of the target process synchronously from the context of the caller;
- https://lore.kernel.org/patchwork/patch/1344419/ to add MADV_DONTNEED support for process_madvise implementing synchronous memory reaping.

The end of the last discussion culminated with suggestion to introduce a dedicated system call (https://lore.kernel.org/patchwork/patch/1344418/#1553875)
The reasoning was that the new variant of process_madvise
a) does not work on an address range
b) is destructive
c) doesn't share much code at all with the rest of process_madvise
From the userspace point of view it was awkward and inconvenient to provide memory range for this operation that operates on the entire address space. Using special flags or address values to specify the entire address space was too hacky.

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

Quoted 5 lines
Quoted 3 lines

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

*pidfd_open

Ack

Quoted 9 lines
Quoted 9 lines

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

Quoted 7 lines
Quoted 1 line

Thanks for continuously pushing this. One question I have is how do you envision this syscall to be used for the cgroup based workloads. Traverse the target tree, read pids from cgroup.procs files, pidfd_open them, send SIGKILL and then process_reap them. Is that right?

Yes, at least that's how Android does that. It's a bit more involved but it's a technical detail. Userspace low memory killer kills a process (sends SIGKILL and calls process_reap) and another system component detects that a process died and will kill all processes belonging to the same cgroup (that's how we identify related processes).

Quoted 4 lines

Orthogonal to this patch I wonder if we should have an optimized way to reap processes from a cgroup. Something similar to cgroup.kill (or maybe overload cgroup.kill with reaping as well).

Seems reasonable to me. We could use that in the above scenario.

Quoted 29 lines

[...]

Quoted 26 lines
mm/oom_kill.c +26 -0
+
+SYSCALL_DEFINE2(process_reap, int, pidfd, unsigned int, flags)
+{
+       struct pid *pid;
+       struct task_struct *task;
+       struct mm_struct *mm = NULL;
+       unsigned int f_flags;
+       long ret = 0;
+
+       if (flags != 0)
+               return -EINVAL;
+
+       pid = pidfd_get_pid(pidfd, &f_flags);
+       if (IS_ERR(pid))
+               return PTR_ERR(pid);
+
+       task = get_pid_task(pid, PIDTYPE_PID);
+       if (!task) {
+               ret = -ESRCH;
+               goto put_pid;
+       }
+
+       /*
+        * If the task is dying and in the process of releasing its memory
+        * then get its mm.
+        */
Quoted 8 lines

mm/oom_kill.c void pagefault_out_of_memory(void) · line 1172

Quoted 2 lines
mm/oom_kill.c +2 -0
+       task_lock(task);
+       if (task_will_free_mem(task) && (task->flags & PF_KTHREAD) == 0) {

task_will_free_mem() is fine here but I think in parallel we should optimize this function. At the moment it is traversing all the processes on the machine. It is very normal to have tens of thousands of processes on big machines, so it would be really costly when reaping a bunch of processes.

Hmm. But I think we still need to make sure that the mm is not shared with another non-dying process. IIUC that's the point of that traversal. Am I mistaken?

#13 lore raw
In reply to Andy Lutomirski
Quoted 22 lines · Andy Lutomirski
Quoted 11 lines · Suren Baghdasaryan

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core

Quoted 8 lines

the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

Quoted 2 lines

At the risk of asking a potentially silly question, should this just be a file in procfs?

Hmm. I guess it's doable if procfs will not disappear too soon before memory is released... syscall also supports parameters, in this case flags can be used in the future to support PIDs in addition to PIDFDs for example.
Before looking more in that direction, a silly question from my side:
why procfs interface would be preferable to a syscall?

Quoted 6 lines

Also, please consider removing all mention of the word "reap" from the user API. For better or for worse, "reap" in UNIX refers to what happens when a dead task gets wait()ed. I sincerely wish I could go back in time and gently encourage whomever invented that particular abomination to change their mind, but my time machine doesn't work.

I see. Thanks for the note. How about process_mem_release() and replacing reap with release everywhere?

Quoted 2 lines

--Andy

#10 lore raw
In reply to Suren Baghdasaryan
Quoted 1 line · Suren Baghdasaryan

[...]

Quoted 13 lines
Quoted 7 lines

mm/oom_kill.c void pagefault_out_of_memory(void) · line 1172

Quoted 6 lines
mm/oom_kill.c +6 -0
+       /*
+        * If the task is dying and in the process of releasing its memory
+        * then get its mm.
+        */
+       task_lock(task);
+       if (task_will_free_mem(task) && (task->flags & PF_KTHREAD) == 0) {
Quoted 5 lines

task_will_free_mem() is fine here but I think in parallel we should optimize this function. At the moment it is traversing all the processes on the machine. It is very normal to have tens of thousands of processes on big machines, so it would be really costly when reaping a bunch of processes.

Quoted 3 lines

Hmm. But I think we still need to make sure that the mm is not shared with another non-dying process. IIUC that's the point of that traversal. Am I mistaken?

You are right. I am talking about efficiently finding all processes which are sharing mm (maybe linked into another list) instead of traversing all the processes on the system.

#11 lore raw
Quoted 17 lines · Shakeel Butt
Quoted 1 line · Suren Baghdasaryan

[...]

Quoted 13 lines
Quoted 12 lines

mm/oom_kill.c void pagefault_out_of_memory(void) · line 1172

Quoted 6 lines
mm/oom_kill.c +6 -0
+       /*
+        * If the task is dying and in the process of releasing its memory
+        * then get its mm.
+        */
+       task_lock(task);
+       if (task_will_free_mem(task) && (task->flags & PF_KTHREAD) == 0) {

task_will_free_mem() is fine here but I think in parallel we should optimize this function. At the moment it is traversing all the processes on the machine. It is very normal to have tens of thousands of processes on big machines, so it would be really costly when reaping a bunch of processes.

Quoted 7 lines
Quoted 3 lines

Hmm. But I think we still need to make sure that the mm is not shared with another non-dying process. IIUC that's the point of that traversal. Am I mistaken?

You are right. I am talking about efficiently finding all processes which are sharing mm (maybe linked into another list) instead of traversing all the processes on the system.

Oh, I see. I think that's a good idea but belongs to a separate patch as an optimization for task_will_free_mem().
Thanks for reviewing and for good suggestions!

#14 lore raw
In reply to Suren Baghdasaryan
Quoted 9 lines · Suren Baghdasaryan
Quoted 5 lines · Andy Lutomirski

Also, please consider removing all mention of the word "reap" from the user API. For better or for worse, "reap" in UNIX refers to what happens when a dead task gets wait()ed. I sincerely wish I could go back in time and gently encourage whomever invented that particular abomination to change their mind, but my time machine doesn't work.

I see. Thanks for the note. How about process_mem_release() and replacing reap with release everywhere?

I don't quite understand the objection. This syscall works on tasks that are at the end of their life, right? Isn't something like process_mreap() establishing exactly the mental link we want here? Release is less descriptive for what this thing is to be used for.

maintainer
#15 lore raw
Quoted 9 lines · Johannes Weiner
Quoted 7 lines · Suren Baghdasaryan
Quoted 5 lines · Andy Lutomirski

Also, please consider removing all mention of the word "reap" from the user API. For better or for worse, "reap" in UNIX refers to what happens when a dead task gets wait()ed. I sincerely wish I could go back in time and gently encourage whomever invented that particular abomination to change their mind, but my time machine doesn't work.

Quoted 7 lines
Quoted 2 lines

I see. Thanks for the note. How about process_mem_release() and replacing reap with release everywhere?

I don't quite understand the objection. This syscall works on tasks that are at the end of their life, right? Isn't something like process_mreap() establishing exactly the mental link we want here? Release is less descriptive for what this thing is to be used for.

For better or for worse, "reap" means to make a zombie pid go away. From the description, this new operation takes a dying process (not necessarily a zombie yet) and aggressively frees its memory. This is a different optioneration.

How about "free_dying_process_memory"?

maintainer
#23 lore raw
In reply to Suren Baghdasaryan
Quoted 27 lines · Suren Baghdasaryan
Quoted 22 lines · Andy Lutomirski
Quoted 11 lines · Suren Baghdasaryan

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core

Quoted 8 lines

the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

Quoted 2 lines

At the risk of asking a potentially silly question, should this just be a file in procfs?

Quoted 6 lines

Hmm. I guess it's doable if procfs will not disappear too soon before memory is released... syscall also supports parameters, in this case flags can be used in the future to support PIDs in addition to PIDFDs for example.
Before looking more in that direction, a silly question from my side:
why procfs interface would be preferable to a syscall?

It avoids using a syscall nr. (Admittedly a syscall nr is not *that* precious of a resource.) It also makes it possible to use a shell script to do this, which is maybe useful.

--Andy

#16 lore raw
In reply to Andy Lutomirski
Quoted 19 lines · Andy Lutomirski
Quoted 9 lines · Johannes Weiner
Quoted 7 lines · Suren Baghdasaryan
Quoted 5 lines · Andy Lutomirski

Also, please consider removing all mention of the word "reap" from the user API. For better or for worse, "reap" in UNIX refers to what happens when a dead task gets wait()ed. I sincerely wish I could go back in time and gently encourage whomever invented that particular abomination to change their mind, but my time machine doesn't work.

Quoted 7 lines
Quoted 2 lines

I see. Thanks for the note. How about process_mem_release() and replacing reap with release everywhere?

I don't quite understand the objection. This syscall works on tasks that are at the end of their life, right? Isn't something like process_mreap() establishing exactly the mental link we want here? Release is less descriptive for what this thing is to be used for.

Quoted 6 lines

For better or for worse, "reap" means to make a zombie pid go away. From the description, this new operation takes a dying process (not necessarily a zombie yet) and aggressively frees its memory. This is a different optioneration.

How about "free_dying_process_memory"?

process_mreap sounds definitely better and in line with names like process_madvise. So maybe we can use it?

#24 lore raw
In reply to Andy Lutomirski

)

Quoted 36 lines · Andy Lutomirski
Quoted 27 lines · Suren Baghdasaryan
Quoted 22 lines · Andy Lutomirski
Quoted 11 lines · Suren Baghdasaryan

In modern systems it's not unusual to have a system component monitoring memory conditions of the system and tasked with keeping system memory pressure under control. One way to accomplish that is to kill non-essential processes to free up memory for more important ones. Examples of this are Facebook's OOM killer daemon called oomd and Android's low memory killer daemon called lmkd.
For such system component it's important to be able to free memory quickly and efficiently. Unfortunately the time process takes to free up its memory after receiving a SIGKILL might vary based on the state of the process (uninterruptible sleep), size and OPP level of the core

Quoted 8 lines

the process is running. A mechanism to free resources of the target process in a more predictable way would improve system's ability to control its memory pressure.
Introduce process_reap system call that reclaims memory of a dying process from the context of the caller. This way the memory in freed in a more controllable way with CPU affinity and priority of the caller. The workload of freeing the memory will also be charged to the caller.
The operation is allowed only on a dying process.

Quoted 2 lines

At the risk of asking a potentially silly question, should this just be a file in procfs?

Quoted 6 lines

Hmm. I guess it's doable if procfs will not disappear too soon before memory is released... syscall also supports parameters, in this case flags can be used in the future to support PIDs in addition to PIDFDs for example.
Before looking more in that direction, a silly question from my side:
why procfs interface would be preferable to a syscall?

Quoted 3 lines

It avoids using a syscall nr. (Admittedly a syscall nr is not *that* precious of a resource.) It also makes it possible to use a shell script to do this, which is maybe useful.

I see. Not really sure if the shell usage is a big usecase for this operation but let's see if more people like that approach. For my specific usecase one syscall (process_reap) is better than three syscalls (open, write, close) and the possibility to extend the functionality using flags might be of value for the future.

Quoted 2 lines

--Andy

#17 lore raw
In reply to Suren Baghdasaryan
Quoted 25 lines · Suren Baghdasaryan
Quoted 24 lines · Andy Lutomirski
Quoted 9 lines · Johannes Weiner
Quoted 7 lines · Suren Baghdasaryan
Quoted 5 lines · Andy Lutomirski

Also, please consider removing all mention of the word "reap" from the user API. For better or for worse, "reap" in UNIX refers to what happens when a dead task gets wait()ed. I sincerely wish I could go back in time and gently encourage whomever invented that particular abomination to change their mind, but my time machine doesn't work.

Quoted 7 lines
Quoted 2 lines

I see. Thanks for the note. How about process_mem_release() and replacing reap with release everywhere?

I don't quite understand the objection. This syscall works on tasks that are at the end of their life, right? Isn't something like process_mreap() establishing exactly the mental link we want here? Release is less descriptive for what this thing is to be used for.

For better or for worse, "reap" means to make a zombie pid go away. From the description, this new operation takes a dying process (not necessarily a zombie yet) and aggressively frees its memory. This is a different optioneration.

Quoted 4 lines
Quoted 1 line

How about "free_dying_process_memory"?

process_mreap sounds definitely better and in line with names like process_madvise. So maybe we can use it?

That one was my favorite from the list I gave too but maybe we can satisfy Andy too if we use one of:
- process_mfree()
- process_mrelease()

#18 lore raw
Quoted 28 lines · Christian Brauner
Quoted 27 lines · Suren Baghdasaryan
Quoted 25 lines · Andy Lutomirski
Quoted 9 lines · Johannes Weiner
Quoted 7 lines · Suren Baghdasaryan
Quoted 5 lines · Andy Lutomirski

Also, please consider removing all mention of the word "reap" from the user API. For better or for worse, "reap" in UNIX refers to what happens when a dead task gets wait()ed. I sincerely wish I could go back in time and gently encourage whomever invented that particular abomination to change their mind, but my time machine doesn't work.

Quoted 7 lines
Quoted 2 lines

I see. Thanks for the note. How about process_mem_release() and replacing reap with release everywhere?

I don't quite understand the objection. This syscall works on tasks that are at the end of their life, right? Isn't something like process_mreap() establishing exactly the mental link we want here? Release is less descriptive for what this thing is to be used for.

For better or for worse, "reap" means to make a zombie pid go away.
From the description, this new operation takes a dying process (not
necessarily a zombie yet) and aggressively frees its memory. This is a different optioneration.

How about "free_dying_process_memory"?

Quoted 8 lines
Quoted 2 lines

process_mreap sounds definitely better and in line with names like process_madvise. So maybe we can use it?

That one was my favorite from the list I gave too but maybe we can satisfy Andy too if we use one of:
- process_mfree()
- process_mrelease()

FWIW, I tend to like process_mrelease(), due to the implied "release" ("free the memory if there are no other references") semantics. Further, a new syscall feels cleaner than some magic sysfs/procfs toggle. Just my 2 cents.

Signature · 3 lines
Thanks,

David / dhildenb
#25 lore raw
In reply to Suren Baghdasaryan

* Suren Baghdasaryan:

Quoted 12 lines

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

Quoted 6 lines

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

I think the manual page should mention what it means for a process to be “dying”, and how to move a process to this state.

Thanks,
Florian

maintainer
#19 lore raw
In reply to David Hildenbrand
Quoted 1 line · David Hildenbrand

On 02.07.21 17:27, Christian Brauner wrote:

[...]

Quoted 8 lines
Quoted 5 lines

That one was my favorite from the list I gave too but maybe we can satisfy Andy too if we use one of:
- process_mfree()
- process_mrelease()

FWIW, I tend to like process_mrelease(), due to the implied "release" ("free the memory if there are no other references") semantics.

Agreed.

Quoted 2 lines

Further, a new
syscall feels cleaner than some magic sysfs/procfs toggle. Just my 2 cents.

Yeah, proc based interface is both tricky to use and kinda ugly now that pidfd can solve all at in once.

My original preference was a more generic kill syscall to allow flags but a dedicated syscall doesn't look really bad either.

Signature · 2 lines
Michal Hocko
SUSE Labs
#26 lore raw
In reply to Florian Weimer
Quoted 18 lines · Florian Weimer

* Suren Baghdasaryan:

Quoted 15 lines

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

The flags argument is reserved for future use; currently, this argument must be specified as 0.

Quoted 6 lines
Quoted 3 lines

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

I think the manual page should mention what it means for a process to be “dying”, and how to move a process to this state.

Thanks for the suggestion, Florian! Would replacing "dying process" with "process which was sent a SIGKILL signal" be sufficient?

Quoted 4 lines

Thanks,
Florian

#20 lore raw
In reply to Michal Hocko
Quoted 10 lines · Michal Hocko
Quoted 1 line · David Hildenbrand

On 02.07.21 17:27, Christian Brauner wrote:

[...]

Quoted 6 lines
Quoted 5 lines

That one was my favorite from the list I gave too but maybe we can satisfy Andy too if we use one of:
- process_mfree()
- process_mrelease()

Quoted 4 lines
Quoted 2 lines

FWIW, I tend to like process_mrelease(), due to the implied "release" ("free the memory if there are no other references") semantics.

Agreed.

Ok, sounds like process_mrelease() would be an acceptable compromise.

Quoted 6 lines
Quoted 3 lines

Further, a new
syscall feels cleaner than some magic sysfs/procfs toggle. Just my 2 cents.

Yeah, proc based interface is both tricky to use and kinda ugly now that pidfd can solve all at in once.

Sounds good. Will keep it as is then.

Quoted 2 lines

My original preference was a more generic kill syscall to allow flags but a dedicated syscall doesn't look really bad either.

Yeah, I have tried that direction unsuccessfully before arriving at this one. Hopefully it represents the right compromise which can satisfy everyone's usecase.

Quoted 3 lines

--
Michal Hocko
SUSE Labs

#27 lore raw
In reply to Suren Baghdasaryan

* Suren Baghdasaryan:

Quoted 23 lines · Florian Weimer
Quoted 22 lines

* Suren Baghdasaryan:

Quoted 18 lines

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

Quoted 5 lines
Quoted 2 lines

I think the manual page should mention what it means for a process to be “dying”, and how to move a process to this state.

Thanks for the suggestion, Florian! Would replacing "dying process" with "process which was sent a SIGKILL signal" be sufficient?

That explains very clearly the requirement, but it raises the question why this isn't an si_code flag for rt_sigqueueinfo, reusing the existing system call.

Thanks,
Florian

#28 lore raw
Quoted 29 lines · Florian Weimer

* Suren Baghdasaryan:

Quoted 26 lines · Florian Weimer
Quoted 24 lines

* Suren Baghdasaryan:

Quoted 12 lines

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

Quoted 6 lines

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

I think the manual page should mention what it means for a process to be “dying”, and how to move a process to this state.

Quoted 6 lines
Quoted 2 lines

Thanks for the suggestion, Florian! Would replacing "dying process" with "process which was sent a SIGKILL signal" be sufficient?

That explains very clearly the requirement, but it raises the question why this isn't an si_code flag for rt_sigqueueinfo, reusing the existing system call.

I think you are suggesting to use sigqueue() to deliver the signal and perform the reaping when a special value accompanies it. This would be somewhat similar to my early suggestion to use a flag in pidfd_send_signal() (see:
https://lore.kernel.org/patchwork/patch/1060407) to implement memory reaping which has another advantage of operation on PIDFDs instead of PIDs which can be recycled. kill()/pidfd_send_signal()/sigqueue() are supposed to deliver the signal and return without blocking. Changing that behavior was considered unacceptable in these discussions. On the other hand using some kthread to do the reaping asynchronously has its disadvantages:
userspace can't control the priority and cpu affinity of the thread doing the reaping and this work is not charged towards the caller. In the end a separate blocking syscall was deemed appropriate for this operation. More details can be found in the links I posted in the description of the patch.

Quoted 4 lines

Thanks,
Florian

#29 lore raw

* Suren Baghdasaryan:

Quoted 37 lines · Florian Weimer
Quoted 29 lines

* Suren Baghdasaryan:

Quoted 26 lines · Florian Weimer
Quoted 24 lines

* Suren Baghdasaryan:

Quoted 12 lines

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

Quoted 6 lines

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

I think the manual page should mention what it means for a process to be “dying”, and how to move a process to this state.

Quoted 6 lines
Quoted 2 lines

Thanks for the suggestion, Florian! Would replacing "dying process" with "process which was sent a SIGKILL signal" be sufficient?

That explains very clearly the requirement, but it raises the question why this isn't an si_code flag for rt_sigqueueinfo, reusing the existing system call.

Quoted 10 lines

I think you are suggesting to use sigqueue() to deliver the signal and perform the reaping when a special value accompanies it. This would be somewhat similar to my early suggestion to use a flag in pidfd_send_signal() (see:
https://lore.kernel.org/patchwork/patch/1060407) to implement memory reaping which has another advantage of operation on PIDFDs instead of PIDs which can be recycled. kill()/pidfd_send_signal()/sigqueue() are supposed to deliver the signal and return without blocking. Changing that behavior was considered unacceptable in these discussions.

Does this mean that you need two threads, one that sends SIGKILL, and one that calls process_reap? Given that sending SIGKILL is blocking with the existing interfaces?

Please also note that asynchronous deallocation of resources leads to bugs and can cause unrelated workloads to fail. For example, in some configurations, clone can fail with EAGAIN even in cases where the total number of tasks is clearly bounded because the kernel signals task exit to applications before all resources are deallocated. I'm worried that the new interface makes things quite a bit worse in this regard.

Thanks,
Florian

#30 lore raw
Quoted 51 lines · Florian Weimer

* Suren Baghdasaryan:

Quoted 37 lines · Florian Weimer
Quoted 29 lines

* Suren Baghdasaryan:

Quoted 26 lines · Florian Weimer
Quoted 24 lines

* Suren Baghdasaryan:

Quoted 12 lines

The API is as follows,

int process_reap(int pidfd, unsigned int flags);

DESCRIPTION
The process_reap() system call is used to free the memory of a dying process.

The pidfd selects the process referred to by the PID file descriptor.
(See pidofd_open(2) for further information)

Quoted 6 lines

The flags argument is reserved for future use; currently, this argument must be specified as 0.

RETURN VALUE
On success, process_reap() returns 0. On error, -1 is returned and errno is set to indicate the error.

I think the manual page should mention what it means for a process to be “dying”, and how to move a process to this state.

Quoted 6 lines
Quoted 2 lines

Thanks for the suggestion, Florian! Would replacing "dying process" with "process which was sent a SIGKILL signal" be sufficient?

That explains very clearly the requirement, but it raises the question why this isn't an si_code flag for rt_sigqueueinfo, reusing the existing system call.

Quoted 10 lines

I think you are suggesting to use sigqueue() to deliver the signal and perform the reaping when a special value accompanies it. This would be somewhat similar to my early suggestion to use a flag in pidfd_send_signal() (see:
https://lore.kernel.org/patchwork/patch/1060407) to implement memory reaping which has another advantage of operation on PIDFDs instead of PIDs which can be recycled. kill()/pidfd_send_signal()/sigqueue() are supposed to deliver the signal and return without blocking. Changing that behavior was considered unacceptable in these discussions.

Quoted 3 lines

Does this mean that you need two threads, one that sends SIGKILL, and one that calls process_reap? Given that sending SIGKILL is blocking with the existing interfaces?

Sending SIGKILL is blocking in terms of delivering the signal, but it does not block waiting for SIGKILL to be processed by the signal recipient and memory to be released. When I was talking about "blocking", I meant that current kill() and friends do not block to wait for SIGKILL to be processed.
process_reap() will block until the memory is released. Whether the userspace caller is using it right after sending a SIGKILL to reclaim the memory synchronously or spawns a separate thread to reclaim memory asynchronously is up to the user. Both patterns are supported.

Quoted 6 lines

Please also note that asynchronous deallocation of resources leads to bugs and can cause unrelated workloads to fail. For example, in some configurations, clone can fail with EAGAIN even in cases where the total number of tasks is clearly bounded because the kernel signals task exit to applications before all resources are deallocated. I'm worried that the new interface makes things quite a bit worse in this regard.

The process_reap() releases memory synchronously, no kthreads are being used. If asynchronous release is required, the userspace would need to spawn a userspace thread and issue this syscall from it. I hope this clears your concerns, which I think are about asynchronous deallocations within the kernel.
Thanks!

Quoted 4 lines

Thanks,
Florian

#31 lore raw

* Suren Baghdasaryan:

Quoted 9 lines

Sending SIGKILL is blocking in terms of delivering the signal, but it does not block waiting for SIGKILL to be processed by the signal recipient and memory to be released. When I was talking about "blocking", I meant that current kill() and friends do not block to wait for SIGKILL to be processed.
process_reap() will block until the memory is released. Whether the userspace caller is using it right after sending a SIGKILL to reclaim the memory synchronously or spawns a separate thread to reclaim memory asynchronously is up to the user. Both patterns are supported.

I see, this makes sense.

Considering that the pidfd sticks around after process_reap returns, the issue described in bug 154011 probably does not apply to process_reap. (This relates to asynchronous resource deallocation, as discussed before.)

Thanks,
Florian

#21 lore raw
In reply to Suren Baghdasaryan
Quoted 25 lines · Suren Baghdasaryan
Quoted 10 lines · Michal Hocko
Quoted 1 line · David Hildenbrand

On 02.07.21 17:27, Christian Brauner wrote:

[...]

Quoted 6 lines
Quoted 5 lines

That one was my favorite from the list I gave too but maybe we can satisfy Andy too if we use one of:
- process_mfree()
- process_mrelease()

Quoted 4 lines
Quoted 2 lines

FWIW, I tend to like process_mrelease(), due to the implied "release" ("free the memory if there are no other references") semantics.

Agreed.

Ok, sounds like process_mrelease() would be an acceptable compromise.

Quoted 6 lines
Quoted 3 lines

Further, a new
syscall feels cleaner than some magic sysfs/procfs toggle. Just my 2 cents.

Yeah, proc based interface is both tricky to use and kinda ugly now that pidfd can solve all at in once.

Quoted 8 lines

Sounds good. Will keep it as is then.

Quoted 2 lines

My original preference was a more generic kill syscall to allow flags but a dedicated syscall doesn't look really bad either.

Yeah, I have tried that direction unsuccessfully before arriving at this one. Hopefully it represents the right compromise which can satisfy everyone's usecase.

I think a syscall is fine and it's not we're running out of numbers (anymore). :)

Christian

#22 lore raw
Quoted 32 lines · Christian Brauner
Quoted 30 lines · Suren Baghdasaryan
Quoted 10 lines · Michal Hocko
Quoted 1 line · David Hildenbrand

On 02.07.21 17:27, Christian Brauner wrote:

[...]

Quoted 6 lines
Quoted 5 lines

That one was my favorite from the list I gave too but maybe we can satisfy Andy too if we use one of:
- process_mfree()
- process_mrelease()

Quoted 4 lines
Quoted 2 lines

FWIW, I tend to like process_mrelease(), due to the implied "release" ("free the memory if there are no other references") semantics.

Agreed.

Ok, sounds like process_mrelease() would be an acceptable compromise.

Quoted 6 lines
Quoted 3 lines

Further, a new
syscall feels cleaner than some magic sysfs/procfs toggle. Just my 2 cents.

Yeah, proc based interface is both tricky to use and kinda ugly now that pidfd can solve all at in once.

Sounds good. Will keep it as is then.

Quoted 2 lines

My original preference was a more generic kill syscall to allow flags but a dedicated syscall doesn't look really bad either.

Quoted 6 lines
Quoted 3 lines

Yeah, I have tried that direction unsuccessfully before arriving at this one. Hopefully it represents the right compromise which can satisfy everyone's usecase.

I think a syscall is fine and it's not we're running out of numbers (anymore). :)

Thanks everyone for the input!
So far I collected:
1. rename the syscall to process_mrelease()
2. replace "dying process" with "process which was sent a SIGKILL signal" in the manual page text

I'll respin a v2 with these changes next week.
Have a great weekend!
Suren.

Quoted 2 lines

Christian

#32 lore raw
In reply to Suren Baghdasaryan
Quoted 5 lines · Suren Baghdasaryan
Quoted 4 lines

That explains very clearly the requirement, but it raises the question why this isn't an si_code flag for rt_sigqueueinfo, reusing the existing system call.

Quoted 10 lines

I think you are suggesting to use sigqueue() to deliver the signal and perform the reaping when a special value accompanies it. This would be somewhat similar to my early suggestion to use a flag in pidfd_send_signal() (see:
https://lore.kernel.org/patchwork/patch/1060407) to implement memory reaping which has another advantage of operation on PIDFDs instead of PIDs which can be recycled. kill()/pidfd_send_signal()/sigqueue() are supposed to deliver the signal and return without blocking. Changing that behavior was considered unacceptable in these discussions.

The way I understood the request is that a userspace program (or perhaps two, if so desired) should issue _two_ calls, one to deliver the signal, one to perform the reap portion:

	uinfo.si_code = SI_QUEUE;
	sigqueue(pid, SIGKILL, &uinfo);
	uinfo.si_code = SI_REAP;
	sigqueue(pid, SIGKILL, &uinfo);
#33 lore raw
Quoted 19 lines · Jan Engelhardt
Quoted 5 lines · Suren Baghdasaryan
Quoted 4 lines

That explains very clearly the requirement, but it raises the question why this isn't an si_code flag for rt_sigqueueinfo, reusing the existing system call.

Quoted 10 lines

I think you are suggesting to use sigqueue() to deliver the signal and perform the reaping when a special value accompanies it. This would be somewhat similar to my early suggestion to use a flag in pidfd_send_signal() (see:
https://lore.kernel.org/patchwork/patch/1060407) to implement memory reaping which has another advantage of operation on PIDFDs instead of PIDs which can be recycled. kill()/pidfd_send_signal()/sigqueue() are supposed to deliver the signal and return without blocking. Changing that behavior was considered unacceptable in these discussions.

Quoted 8 lines

The way I understood the request is that a userspace program (or perhaps two, if so desired) should issue _two_ calls, one to deliver the signal, one to perform the reap portion:

        uinfo.si_code = SI_QUEUE;
        sigqueue(pid, SIGKILL, &uinfo);
        uinfo.si_code = SI_REAP;
        sigqueue(pid, SIGKILL, &uinfo);

This approach would still lead to the same discussion: by design, sigqueue/kill/pidfd_send_signal deliver the signal but do not wait for the signal to be processed by the recipient. Changing that would be a behavior change. Therefore we would have to follow this pattern and implement memory reaping in an asynchronous manner using a kthread/workqueue and it won't be done in the context of the calling process. This is undesirable because we lose the ability to control priority and cpu affinity for this operation and work won't be charged to the caller.
That's why the proposed syscall performs memory reaping in the caller's context and blocks until the operation is done. In this proposal, your sequence looks like this:

pidfd_send_signal(pidfd, SIGKILL, NULL, 0);
process_reap(pidfd, 0);

except we decided to rename process_reap() to process_mrelease() in the next revision.

#34 lore raw
Quoted 5 lines · Suren Baghdasaryan
Quoted 5 lines

The way I understood the request is that a userspace program (or perhaps two, if so desired) should issue _two_ calls, one to deliver the signal, one to perform the reap portion:

Quoted 8 lines
Quoted 4 lines
        uinfo.si_code = SI_QUEUE;
        sigqueue(pid, SIGKILL, &uinfo);
        uinfo.si_code = SI_REAP;
        sigqueue(pid, SIGKILL, &uinfo);

This approach would still lead to the same discussion: by design, sigqueue/kill/pidfd_send_signal deliver the signal but do not wait for the signal to be processed by the recipient.

Oh, so the only reason not to do that is because there is some POSIX specification that says the sigqueue API should be non-waiting for all possible parameter values (with an implied "present and future values!"), not because there's some hurdle to actually add a wait inside within rt_sigqueueinfo if the REAP flag is set. Gotcha.