Skip to contentSkip to conversation

mm: process_mrelease: expedited reclaim and auto-kill support

RFC In review Minchan Kim 5 participants 31 messages Apr 13 – Apr 27

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover mm: process_mrelease: expedited reclaim and auto-kill support

This patch series introduces optimizations to expedite memory reclamation in process_mrelease() and provides a secure, race-free "auto-kill" mechanism for efficient container shutdown and OOM handling.

Currently, process_mrelease() unmaps pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. Furthermore, requiring userspace to send a SIGKILL prior to invoking process_mrelease() introduces scheduling race conditions where the victim task may enter the exit path prematurely, bypassing expedited reclamation hooks.

This series addresses these limitations in three logical steps.

Patch #1: mm: process_mrelease: expedite clean file folio reclaim via mmu_gather Integrates clean file folio eviction directly into the low-level TLB batching (mmu_gather) infrastructure. Symmetrically truncates clean file folios alongside anonymous pages during the unmap loop.

Patch #2: mm: process_mrelease: skip LRU movement for exclusive file folios Skips costly LRU marking (folio_mark_accessed) for exclusive file-backed folios undergoing process_mrelease reclaim. Perf profiling reveals that LRU movement accounts for ~55% of overhead during unmap.

Patch #3: mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag Adds an auto-kill flag supporting atomic teardown. Utilizes a dedicated signal code (KILL_MRELEASE) to guarantee MMF_UNSTABLE is marked in the signal delivery path, preventing scheduling races.

Minchan Kim (3):
mm: process_mrelease: expedite clean file folio reclaim via mmu_gather
mm: process_mrelease: skip LRU movement for exclusive file folios
mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

 arch/s390/include/asm/tlb.h        |  2 +-
 include/linux/swap.h               |  9 ++++++---
 include/uapi/asm-generic/siginfo.h |  6 ++++++
 include/uapi/linux/mman.h          |  4 ++++
 kernel/signal.c                    |  4 ++++
 mm/memory.c                        | 13 ++++++++++++-
 mm/mmu_gather.c                    |  8 +++++---
 mm/oom_kill.c                      | 20 +++++++++++++++++++-
 mm/swap_state.c                    | 19 +++++++++++++++++--
 9 files changed, 74 insertions(+), 11 deletions(-)
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
#1 lore raw

RFC 1/3 mm: process_mrelease: expedite clean file folio reclaim via mmu_gather

Currently, process_mrelease() unmaps the pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. This delays the immediate recovery of system memory under OOM or container shutdown scenarios.

This patch implements an expedited eviction mechanism for clean file folios by integrating directly into the low-level TLB batching infrastructure (mmu_gather).

Instead of repeatedly locking and evicting folios one by one inside the unmap loop (zap_present_folio_ptes), we pass the MMF_UNSTABLE flag status down to free_pages_and_swap_cache(). Within this single unified loop, anonymous pages are released via free_swap_cache(), and file-backed folios are symmetrically truncated via mapping_evict_folio().

This avoids introducing unnecessary data structures, preserves TLB flush safety, and removes duplicate tree traversals, resulting in an extremely lean and highly responsive process_mrelease() implementation.

---
 arch/s390/include/asm/tlb.h |  2 +-
 include/linux/swap.h        |  9 ++++++---
 mm/mmu_gather.c             |  8 +++++---
 mm/swap_state.c             | 19 +++++++++++++++++--
 4 files changed, 29 insertions(+), 9 deletions(-)
arch/s390/include/asm/tlb.h +1 -1
index 619fd41e710e..554842345ccd 100644
@@ -62,7 +62,7 @@ static inline bool __tlb_remove_folio_pages(struct mmu_gather *tlb,
 	VM_WARN_ON_ONCE(delay_rmap);
 	VM_WARN_ON_ONCE(page_folio(page) != page_folio(page + nr_pages - 1));
 
-	free_pages_and_swap_cache(encoded_pages, ARRAY_SIZE(encoded_pages));
+	free_pages_and_caches(encoded_pages, ARRAY_SIZE(encoded_pages), false);
 	return false;
 }
 
include/linux/swap.h +6 -3
index 62fc7499b408..e7b929b062f8 100644
@@ -433,7 +433,7 @@ static inline unsigned long total_swapcache_pages(void)
 
 void free_swap_cache(struct folio *folio);
 void free_folio_and_swap_cache(struct folio *folio);
-void free_pages_and_swap_cache(struct encoded_page **, int);
+void free_pages_and_caches(struct encoded_page **pages, int nr, bool free_unmapped_file);
 /* linux/mm/swapfile.c */
 extern atomic_long_t nr_swap_pages;
 extern long total_swap_pages;
@@ -510,8 +510,11 @@ static inline void put_swap_device(struct swap_info_struct *si)
 	do { (val)->freeswap = (val)->totalswap = 0; } while (0)
 #define free_folio_and_swap_cache(folio) \
 	folio_put(folio)
-#define free_pages_and_swap_cache(pages, nr) \
-	release_pages((pages), (nr));
+static inline void free_pages_and_caches(struct encoded_page **pages,
+		int nr, bool free_unmapped_file)
+{
+	release_pages(pages, nr);
+}
 
 static inline void free_swap_cache(struct folio *folio)
 {
mm/mmu_gather.c +5 -3
index fe5b6a031717..5ce5824db07f 100644
@@ -100,7 +100,8 @@ void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma)
  */
 #define MAX_NR_FOLIOS_PER_FREE		512
 
-static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch)
+static void __tlb_batch_free_encoded_pages(struct mm_struct *mm,
+		struct mmu_gather_batch *batch)
 {
 	struct encoded_page **pages = batch->encoded_pages;
 	unsigned int nr, nr_pages;
@@ -135,7 +136,8 @@ static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch)
 			}
 		}
 
-		free_pages_and_swap_cache(pages, nr);
+		free_pages_and_caches(pages, nr,
+				      mm_flags_test(MMF_UNSTABLE, mm));
 		pages += nr;
 		batch->nr -= nr;
 
@@ -148,7 +150,7 @@ static void tlb_batch_pages_flush(struct mmu_gather *tlb)
 	struct mmu_gather_batch *batch;
 
 	for (batch = &tlb->local; batch && batch->nr; batch = batch->next)
-		__tlb_batch_free_encoded_pages(batch);
+		__tlb_batch_free_encoded_pages(tlb->mm, batch);
 	tlb->active = &tlb->local;
 }
 
mm/swap_state.c +17 -2
index 6d0eef7470be..e70a52ead6d3 100644
@@ -400,11 +400,22 @@ void free_folio_and_swap_cache(struct folio *folio)
 		folio_put(folio);
 }
 
+static inline void free_file_cache(struct folio *folio)
+{
+	if (folio_trylock(folio)) {
+		mapping_evict_folio(folio_mapping(folio), folio);
+		folio_unlock(folio);
+	}
+}
+
 /*
  * Passed an array of pages, drop them all from swapcache and then release
  * them.  They are removed from the LRU and freed if this is their last use.
+ *
+ * If @free_unmapped_file is true, this function will proactively evict clean
+ * file-backed folios if they are no longer mapped.
  */
-void free_pages_and_swap_cache(struct encoded_page **pages, int nr)
+void free_pages_and_caches(struct encoded_page **pages, int nr, bool free_unmapped_file)
 {
 	struct folio_batch folios;
 	unsigned int refs[PAGEVEC_SIZE];
@@ -413,7 +424,11 @@ void free_pages_and_swap_cache(struct encoded_page **pages, int nr)
 	for (int i = 0; i < nr; i++) {
 		struct folio *folio = page_folio(encoded_page_ptr(pages[i]));
 
-		free_swap_cache(folio);
+		if (folio_test_anon(folio))
+			free_swap_cache(folio);
+		else if (unlikely(free_unmapped_file))
+			free_file_cache(folio);
+
 		refs[folios.nr] = 1;
 		if (unlikely(encoded_page_flags(pages[i]) &
 			     ENCODED_PAGE_BIT_NR_PAGES_NEXT))
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
#4 lore raw

RFC 2/3 mm: process_mrelease: skip LRU movement for exclusive file folios

In reply to Minchan Kim

For the process_mrelease reclaim, skip LRU handling for exclusive file-backed folios since they will be freed soon so pointless to move around in the LRU.

This avoids costly LRU movement which accounts for a significant portion of the time during unmap_page_range.

- 91.31% 0.00% mmap_exit_test [kernel.kallsyms] [.] exit_mm

     exit_mm
     __mmput
     exit_mmap
     unmap_vmas

- unmap_page_range
- 55.75% folio_mark_accessed
+ 48.79% __folio_batch_add_and_move
4.23% workingset_activation
+ 12.94% folio_remove_rmap_ptes
+ 9.86% page_table_check_clear
+ 3.34% tlb_flush_mmu
1.06% __page_table_check_pte_clear

---
 mm/memory.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
mm/memory.c +12 -1
index 2f815a34d924..25e17893c919 100644
@@ -1640,6 +1640,8 @@ static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb,
 	bool delay_rmap = false;
 
 	if (!folio_test_anon(folio)) {
+		bool skip_mark_accessed;
+
 		ptent = get_and_clear_full_ptes(mm, addr, pte, nr, tlb->fullmm);
 		if (pte_dirty(ptent)) {
 			folio_mark_dirty(folio);
@@ -1648,7 +1650,16 @@ static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb,
 				*force_flush = true;
 			}
 		}
-		if (pte_young(ptent) && likely(vma_has_recency(vma)))
+
+		/*
+		 * For the process_mrelease reclaim, skip LRU handling for exclusive
+		 * file-backed folios since they will be freed soon so pointless
+		 * to move around in the LRU.
+		 */
+		skip_mark_accessed = mm_flags_test(MMF_UNSTABLE, mm) &&
+				     folio_mapcount(folio) < 2;
+		if (likely(!skip_mark_accessed) && pte_young(ptent) &&
+		    likely(vma_has_recency(vma)))
 			folio_mark_accessed(folio);
 		rss[mm_counter(folio)] -= nr;
 	} else {
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
#7 lore raw

RFC 3/3 mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

In reply to Minchan Kim

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

In this case, the victim task frees its memory via the standard, unoptimized exit path, bypassing the expedited clean file folio reclamation optimization introduced in the previous patch (which relies on the MMF_UNSTABLE flag).

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

Crucially, this patch utilizes a dedicated signal code (KILL_MRELEASE) during signal injection, belonging to a new SIGKILL si_codes section. This special code ensures that the kernel's signal delivery path reliably intercepts the request and marks the target address space as unstable (MMF_UNSTABLE). This mechanism guarantees that the MMF_UNSTABLE flag is set before either the victim task or the reaper proceeds, ensuring that the expedited reclamation optimization is utilized regardless of scheduling order.

---
 include/uapi/asm-generic/siginfo.h |  6 ++++++
 include/uapi/linux/mman.h          |  4 ++++
 kernel/signal.c                    |  4 ++++
 mm/oom_kill.c                      | 20 +++++++++++++++++++-
 4 files changed, 33 insertions(+), 1 deletion(-)
include/uapi/asm-generic/siginfo.h +6 -0
index 5a1ca43b5fc6..0f59b791dab4 100644
@@ -252,6 +252,12 @@ typedef struct siginfo {
 #define BUS_MCEERR_AO	5
 #define NSIGBUS		5
 
+/*
+ * SIGKILL si_codes
+ */
+#define KILL_MRELEASE	1	/* sent by process_mrelease */
+#define NSIGKILL	1
+
 /*
  * SIGTRAP si_codes
  */
include/uapi/linux/mman.h +4 -0
index e89d00528f2f..4266976b45ad 100644
@@ -56,4 +56,8 @@ struct cachestat {
 	__u64 nr_recently_evicted;
 };
 
+/* Flags for process_mrelease */
+#define PROCESS_MRELEASE_REAP_KILL	(1 << 0)
+#define PROCESS_MRELEASE_VALID_FLAGS	(PROCESS_MRELEASE_REAP_KILL)
+
 #endif /* _UAPI_LINUX_MMAN_H */
kernel/signal.c +4 -0
index d65d0fe24bfb..c21b2176dc5e 100644
@@ -1134,6 +1134,10 @@ static int __send_signal_locked(int sig, struct kernel_siginfo *info,
 
 out_set:
 	signalfd_notify(t, sig);
+
+	if (sig == SIGKILL && !is_si_special(info) &&
+	    info->si_code == KILL_MRELEASE && t->mm)
+		mm_flags_set(MMF_UNSTABLE, t->mm);
 	sigaddset(&pending->signal, sig);
 
 	/* Let multiprocess signals appear after on-going forks */
mm/oom_kill.c +19 -1
index 5c6c95c169ee..0b5da5208707 100644
@@ -20,6 +20,8 @@
 
 #include <linux/oom.h>
 #include <linux/mm.h>
+#include <uapi/linux/mman.h>
+#include <linux/capability.h>
 #include <linux/err.h>
 #include <linux/gfp.h>
 #include <linux/sched.h>
@@ -1218,13 +1220,29 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
 	bool reap = false;
 	long ret = 0;
 
-	if (flags)
+	if (flags & ~PROCESS_MRELEASE_VALID_FLAGS)
 		return -EINVAL;
 
 	task = pidfd_get_task(pidfd, &f_flags);
 	if (IS_ERR(task))
 		return PTR_ERR(task);
 
+	if (flags & PROCESS_MRELEASE_REAP_KILL) {
+		struct kernel_siginfo info;
+
+		if (!capable(CAP_KILL)) {
+			ret = -EPERM;
+			goto put_task;
+		}
+		clear_siginfo(&info);
+		info.si_signo = SIGKILL;
+		info.si_code = KILL_MRELEASE;
+		info.si_pid = task_tgid_vnr(current);
+		info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
+
+		do_send_sig_info(SIGKILL, &info, task, PIDTYPE_TGID);
+	}
+
 	/*
 	 * Make sure to choose a thread which still has a reference to mm
 	 * during the group exit
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
maintainer
#15 lore raw
In reply to Minchan Kim
Quoted 11 lines · Minchan Kim

This patch series introduces optimizations to expedite memory reclamation in process_mrelease() and provides a secure, race-free "auto-kill" mechanism for efficient container shutdown and OOM handling.

Currently, process_mrelease() unmaps pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. Furthermore, requiring userspace to send a SIGKILL prior to invoking process_mrelease() introduces scheduling race conditions where the victim task may enter the exit path prematurely, bypassing expedited reclamation hooks.

Quoted 6 lines

This series addresses these limitations in three logical steps.

Patch #1: mm: process_mrelease: expedite clean file folio reclaim via mmu_gather Integrates clean file folio eviction directly into the low-level TLB batching (mmu_gather) infrastructure. Symmetrically truncates clean file folios alongside anonymous pages during the unmap loop.

Why do we need to care about clean page cache? Is this a form of drop_caches?

Quoted 4 lines

Patch #2: mm: process_mrelease: skip LRU movement for exclusive file folios Skips costly LRU marking (folio_mark_accessed) for exclusive file-backed folios undergoing process_mrelease reclaim. Perf profiling reveals that LRU movement accounts for ~55% of overhead during unmap.

OK, but why is this not desirable behavior fir mrelease?

Quoted 4 lines

Patch #3: mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag Adds an auto-kill flag supporting atomic teardown. Utilizes a dedicated signal code (KILL_MRELEASE) to guarantee MMF_UNSTABLE is marked in the signal delivery path, preventing scheduling races.

Could you explain why those races are a real problem?

Signature · 2 lines
Michal Hocko
SUSE Labs
#5 lore raw

Re: [RFC 2/3] mm: process_mrelease: skip LRU movement for exclusive file folios

In reply to Minchan Kim
Quoted 42 lines · Minchan Kim

For the process_mrelease reclaim, skip LRU handling for exclusive file-backed folios since they will be freed soon so pointless to move around in the LRU.

This avoids costly LRU movement which accounts for a significant portion of the time during unmap_page_range.

- 91.31% 0.00% mmap_exit_test [kernel.kallsyms] [.] exit_mm

     exit_mm
     __mmput
     exit_mmap
     unmap_vmas

- unmap_page_range
- 55.75% folio_mark_accessed
+ 48.79% __folio_batch_add_and_move
4.23% workingset_activation
+ 12.94% folio_remove_rmap_ptes
+ 9.86% page_table_check_clear
+ 3.34% tlb_flush_mmu
1.06% __page_table_check_pte_clear

---
 mm/memory.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
mm/memory.c +2 -0
index 2f815a34d924..25e17893c919 100644
@@ -1640,6 +1640,8 @@ static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb,
 	bool delay_rmap = false;
 
 	if (!folio_test_anon(folio)) {
+		bool skip_mark_accessed;
+
 		ptent = get_and_clear_full_ptes(mm, addr, pte, nr, tlb->fullmm);
 		if (pte_dirty(ptent)) {
 			folio_mark_dirty(folio);
@@ -1648,7 +1650,16 @@ static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb,
 				*force_flush = true;
 			}

mm/memory.c static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb, · line 1660

Quoted 10 lines
mm/memory.c +8 -1
 		}
-		if (pte_young(ptent) && likely(vma_has_recency(vma)))
+
+		/*
+		 * For the process_mrelease reclaim, skip LRU handling for exclusive
+		 * file-backed folios since they will be freed soon so pointless
+		 * to move around in the LRU.
+		 */
+		skip_mark_accessed = mm_flags_test(MMF_UNSTABLE, mm) &&
+				     folio_mapcount(folio) < 2;

folio_mapcount() is most certainly the wrong thing to use if you want to handle large folios properly.

Maybe !folio_likely_mapped_shared() is what you are looking for. Maybe.

Signature · 3 lines
Cheers,

David
#2 lore raw

Re: [RFC 1/3] mm: process_mrelease: expedite clean file folio reclaim via mmu_gather

In reply to Minchan Kim
Quoted 4 lines · Minchan Kim

Currently, process_mrelease() unmaps the pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. This delays the immediate recovery of system memory under OOM or container shutdown scenarios.

process_mrelease() calls __oom_reap_task_mm().

There, we skip any MAP_SHARED file mappings.

So I assume what you describe only applies to MAP_PRIVATE file mappings? What about MAP_SHARED?

Also "leaves ... on the LRU list" is rather confusing. They are not evicted and stay in the pagecache?

Quoted 4 lines

This patch implements an expedited eviction mechanism for clean file folios by integrating directly into the low-level TLB batching infrastructure (mmu_gather).

Is this a complicated way of saying "Handle clean pagecache folios similar to swapcache folios in mmu_gather code, dropping them from the swapcache (i.e., evicting them) if they are completely unmapped during reaping"?

Quoted 6 lines

Instead of repeatedly locking and evicting folios one by one inside the unmap loop (zap_present_folio_ptes), we pass the MMF_UNSTABLE flag status down to free_pages_and_swap_cache(). Within this single unified loop, anonymous pages are released via free_swap_cache(), and file-backed folios are symmetrically truncated via mapping_evict_folio().

... where you still evict them one-by-one. Rather confusing.

Quoted 4 lines

This avoids introducing unnecessary data structures, preserves TLB flush safety, and removes duplicate tree traversals, resulting in an extremely lean and highly responsive process_mrelease() implementation.

I don't think this paragraph adds a lot of value, really.

Which "duplicate tree traversal"? Which unnecessary data structures?

Is that AI generated text? A lot of the stuff here reads AI generated. I yet have to meet a developer (not a sales person) that would just say "extremely lean and highly responsive process_mrelease() implementation"

If it is AI generated, throw it away and write it yourself from scratch. Use AI only to polish your English.

Quoted 14 lines
---
 arch/s390/include/asm/tlb.h |  2 +-
 include/linux/swap.h        |  9 ++++++---
 mm/mmu_gather.c             |  8 +++++---
 mm/swap_state.c             | 19 +++++++++++++++++--
 4 files changed, 29 insertions(+), 9 deletions(-)
arch/s390/include/asm/tlb.h +0 -0
index 619fd41e710e..554842345ccd 100644

arch/s390/include/asm/tlb.h static inline bool __tlb_remove_folio_pages(struct mmu_gather *tlb, · line 65

Quoted 5 lines
arch/s390/include/asm/tlb.h +1 -1
@@ -62,7 +62,7 @@ static inline bool __tlb_remove_folio_pages(struct mmu_gather *tlb,
 	VM_WARN_ON_ONCE(delay_rmap);
 	VM_WARN_ON_ONCE(page_folio(page) != page_folio(page + nr_pages - 1));
 
-	free_pages_and_swap_cache(encoded_pages, ARRAY_SIZE(encoded_pages));
+	free_pages_and_caches(encoded_pages, ARRAY_SIZE(encoded_pages), false);

As we dislike boolean parameters, we either try to avoid them (e.g., use flags) or document the parameters using something like

"/* parameter_name= */false"

Quoted 17 lines
arch/s390/include/asm/tlb.h +0 -0
 	return false;
 }
 
include/linux/swap.h +1 -1
index 62fc7499b408..e7b929b062f8 100644
@@ -433,7 +433,7 @@ static inline unsigned long total_swapcache_pages(void)
 
 void free_swap_cache(struct folio *folio);
 void free_folio_and_swap_cache(struct folio *folio);
-void free_pages_and_swap_cache(struct encoded_page **, int);
+void free_pages_and_caches(struct encoded_page **pages, int nr, bool free_unmapped_file);
 /* linux/mm/swapfile.c */
 extern atomic_long_t nr_swap_pages;
 extern long total_swap_pages;

include/linux/swap.h static inline void put_swap_device(struct swap_info_struct *si) · line 517

Quoted 10 lines
include/linux/swap.h +5 -2
@@ -510,8 +510,11 @@ static inline void put_swap_device(struct swap_info_struct *si)
 	do { (val)->freeswap = (val)->totalswap = 0; } while (0)
 #define free_folio_and_swap_cache(folio) \
 	folio_put(folio)
-#define free_pages_and_swap_cache(pages, nr) \
-	release_pages((pages), (nr));
+static inline void free_pages_and_caches(struct encoded_page **pages,
+		int nr, bool free_unmapped_file)
+{
+	release_pages(pages, nr);
+}

Why should !CONFIG_SWAP not take care of free_unmapped_file?

Quoted 48 lines
include/linux/swap.h +0 -0
 static inline void free_swap_cache(struct folio *folio)
 {
mm/mmu_gather.c +5 -3
index fe5b6a031717..5ce5824db07f 100644
@@ -100,7 +100,8 @@ void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma)
  */
 #define MAX_NR_FOLIOS_PER_FREE		512
 
-static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch)
+static void __tlb_batch_free_encoded_pages(struct mm_struct *mm,
+		struct mmu_gather_batch *batch)
 {
 	struct encoded_page **pages = batch->encoded_pages;
 	unsigned int nr, nr_pages;
@@ -135,7 +136,8 @@ static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch)
 			}
 		}
 
-		free_pages_and_swap_cache(pages, nr);
+		free_pages_and_caches(pages, nr,
+				      mm_flags_test(MMF_UNSTABLE, mm));
 		pages += nr;
 		batch->nr -= nr;
 
@@ -148,7 +150,7 @@ static void tlb_batch_pages_flush(struct mmu_gather *tlb)
 	struct mmu_gather_batch *batch;
 
 	for (batch = &tlb->local; batch && batch->nr; batch = batch->next)
-		__tlb_batch_free_encoded_pages(batch);
+		__tlb_batch_free_encoded_pages(tlb->mm, batch);
 	tlb->active = &tlb->local;
 }
 
mm/swap_state.c +4 -0
index 6d0eef7470be..e70a52ead6d3 100644
@@ -400,11 +400,22 @@ void free_folio_and_swap_cache(struct folio *folio)
 		folio_put(folio);
 }
 
+static inline void free_file_cache(struct folio *folio)
+{
+	if (folio_trylock(folio)) {
+		mapping_evict_folio(folio_mapping(folio), folio);

mm/swap_state.c void free_folio_and_swap_cache(struct folio *folio) · line 416

Quoted 10 lines
mm/swap_state.c +7 -0
+		folio_unlock(folio);
+	}
+}
+
 /*
  * Passed an array of pages, drop them all from swapcache and then release
  * them.  They are removed from the LRU and freed if this is their last use.
+ *
+ * If @free_unmapped_file is true, this function will proactively evict clean
+ * file-backed folios if they are no longer mapped.

The parameter name is not really expressive.

You are not freeing unmapped files.

"try_evict_file_folios" maybe?

mapping_evict_folio() has exactly these semantics (unmapped, clean)

Quoted 19 lines
mm/swap_state.c +6 -2
  */
-void free_pages_and_swap_cache(struct encoded_page **pages, int nr)
+void free_pages_and_caches(struct encoded_page **pages, int nr, bool free_unmapped_file)
 {
 	struct folio_batch folios;
 	unsigned int refs[PAGEVEC_SIZE];
@@ -413,7 +424,11 @@ void free_pages_and_swap_cache(struct encoded_page **pages, int nr)
 	for (int i = 0; i < nr; i++) {
 		struct folio *folio = page_folio(encoded_page_ptr(pages[i]));
 
-		free_swap_cache(folio);
+		if (folio_test_anon(folio))
+			free_swap_cache(folio);
+		else if (unlikely(free_unmapped_file))
+			free_file_cache(folio);
+
 		refs[folios.nr] = 1;
 		if (unlikely(encoded_page_flags(pages[i]) &
 			     ENCODED_PAGE_BIT_NR_PAGES_NEXT))
Signature · 3 lines
Cheers,

David
#16 lore raw
In reply to Michal Hocko
Quoted 14 lines · Michal Hocko
Quoted 13 lines · Minchan Kim

This patch series introduces optimizations to expedite memory reclamation in process_mrelease() and provides a secure, race-free "auto-kill" mechanism for efficient container shutdown and OOM handling.

Currently, process_mrelease() unmaps pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. Furthermore, requiring userspace to send a SIGKILL prior to invoking process_mrelease() introduces scheduling race conditions where the victim task may enter the exit path prematurely, bypassing expedited reclamation hooks.

This series addresses these limitations in three logical steps.

Quoted 7 lines
Quoted 4 lines

Patch #1: mm: process_mrelease: expedite clean file folio reclaim via mmu_gather Integrates clean file folio eviction directly into the low-level TLB batching (mmu_gather) infrastructure. Symmetrically truncates clean file folios alongside anonymous pages during the unmap loop.

Why do we need to care about clean page cache? Is this a form of drop_caches?

The goal is to ensure the memory is actually freed by the time process_mrelease returns. Currently, process_mrelease unmaps pages, but page caches remain on the LRU, leaving them to be reclaimed later by kswapd or direct reclaim. This delay defeats the purpose of "expedited" release. It’s not a global drop_caches, but rather a targeted eviction for the victim process to make its memory immediately available for other urgent allocations.

Quoted 7 lines
Quoted 5 lines

Patch #2: mm: process_mrelease: skip LRU movement for exclusive file folios Skips costly LRU marking (folio_mark_accessed) for exclusive file-backed folios undergoing process_mrelease reclaim. Perf profiling reveals that LRU movement accounts for ~55% of overhead during unmap.

OK, but why is this not desirable behavior fir mrelease?

In Android, lmkd kills background apps under memory pressure and then calls process_mrelease. If the memory release is slow due to LRU overhead (~55% as noted), it cannot keep up with the allocation speed of the foreground app. This delay often leads to "over-killing" - killing more background apps than necessary because the system hasn't yet "seen" the memory freed from the first kill.

Quoted 7 lines
Quoted 5 lines

Patch #3: mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag Adds an auto-kill flag supporting atomic teardown. Utilizes a dedicated signal code (KILL_MRELEASE) to guarantee MMF_UNSTABLE is marked in the signal delivery path, preventing scheduling races.

Could you explain why those races are a real problem?

The race occurs when the victim process starts its own exit path (after SIGKILL) before the caller can invoke process_mrelease. If the victim reaches the exit path first, the caller might lose the window to apply these expedited reclamation optimizations. By combining the kill and the release into an atomic operation with a dedicated signal code, we guarantee that the process is reaped efficiently without competing with the process's own teardown logic.

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

Re: [RFC 1/3] mm: process_mrelease: expedite clean file folio reclaim via mmu_gather

In reply to David Hildenbrand (Arm)
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 4 lines · Minchan Kim

Currently, process_mrelease() unmaps the pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. This delays the immediate recovery of system memory under OOM or container shutdown scenarios.

Quoted 6 lines

process_mrelease() calls __oom_reap_task_mm().

There, we skip any MAP_SHARED file mappings.

So I assume what you describe only applies to MAP_PRIVATE file mappings? What about MAP_SHARED?

That's true. My primary target was MAP_PRIVATE because MAP_SHARED pages are often not exclusive to the target process.

Quoted 3 lines

Also "leaves ... on the LRU list" is rather confusing. They are not evicted and stay in the pagecache?

Yes, they are not evicted and remain in the pagecache, which is the problem this patch addresses.

Quoted 10 lines
Quoted 5 lines

This patch implements an expedited eviction mechanism for clean file folios by integrating directly into the low-level TLB batching infrastructure (mmu_gather).

Is this a complicated way of saying "Handle clean pagecache folios similar to swapcache folios in mmu_gather code, dropping them from the swapcache (i.e., evicting them) if they are completely unmapped during reaping"?

Much better description. Thanks.

Quoted 9 lines
Quoted 7 lines

Instead of repeatedly locking and evicting folios one by one inside the unmap loop (zap_present_folio_ptes), we pass the MMF_UNSTABLE flag status down to free_pages_and_swap_cache(). Within this single unified loop, anonymous pages are released via free_swap_cache(), and file-backed folios are symmetrically truncated via mapping_evict_folio().

... where you still evict them one-by-one. Rather confusing.

I initially considered implementing this within zap_present_folio_ptes, but concluded that mmu_gather is the appropriate place. To avoid confusion, I will remove the "Instead of...unmap_loop" line in the next revision.

Quoted 9 lines
Quoted 5 lines

This avoids introducing unnecessary data structures, preserves TLB flush safety, and removes duplicate tree traversals, resulting in an extremely lean and highly responsive process_mrelease() implementation.

I don't think this paragraph adds a lot of value, really.

Which "duplicate tree traversal"? Which unnecessary data structures?

I had considered gathering these folios into a separate data structure for cleanup, but realized mmu_gather is the best place to avoid duplicate traversals for both anon and file folios.

Quoted 4 lines

Is that AI generated text? A lot of the stuff here reads AI generated. I yet have to meet a developer (not a sales person) that would just say "extremely lean and highly responsive process_mrelease() implementation"

That's too business words, I agree. Let me removing them.

Quoted 21 lines

If it is AI generated, throw it away and write it yourself from scratch. Use AI only to polish your English.

Quoted 17 lines
---
 arch/s390/include/asm/tlb.h |  2 +-
 include/linux/swap.h        |  9 ++++++---
 mm/mmu_gather.c             |  8 +++++---
 mm/swap_state.c             | 19 +++++++++++++++++--
 4 files changed, 29 insertions(+), 9 deletions(-)
arch/s390/include/asm/tlb.h +0 -0
index 619fd41e710e..554842345ccd 100644
@@ -62,7 +62,7 @@ static inline bool __tlb_remove_folio_pages(struct mmu_gather *tlb,
 	VM_WARN_ON_ONCE(delay_rmap);
 	VM_WARN_ON_ONCE(page_folio(page) != page_folio(page + nr_pages - 1));
 
Quoted 5 lines

arch/s390/include/asm/tlb.h static inline bool __tlb_remove_folio_pages(struct mmu_gather *tlb, · line 65

Quoted 2 lines
arch/s390/include/asm/tlb.h +1 -1
-	free_pages_and_swap_cache(encoded_pages, ARRAY_SIZE(encoded_pages));
+	free_pages_and_caches(encoded_pages, ARRAY_SIZE(encoded_pages), false);

As we dislike boolean parameters, we either try to avoid them (e.g., use flags) or document the parameters using something like

I like your suggestion "try_evict_file_folios"

Quoted 24 lines

"/* parameter_name= */false"

Quoted 21 lines
arch/s390/include/asm/tlb.h +0 -0
 	return false;
 }
 
include/linux/swap.h +1 -2
index 62fc7499b408..e7b929b062f8 100644
@@ -433,7 +433,7 @@ static inline unsigned long total_swapcache_pages(void)
 
 void free_swap_cache(struct folio *folio);
 void free_folio_and_swap_cache(struct folio *folio);
-void free_pages_and_swap_cache(struct encoded_page **, int);
+void free_pages_and_caches(struct encoded_page **pages, int nr, bool free_unmapped_file);
 /* linux/mm/swapfile.c */
 extern atomic_long_t nr_swap_pages;
 extern long total_swap_pages;
@@ -510,8 +510,11 @@ static inline void put_swap_device(struct swap_info_struct *si)
 	do { (val)->freeswap = (val)->totalswap = 0; } while (0)
 #define free_folio_and_swap_cache(folio) \
 	folio_put(folio)
-#define free_pages_and_swap_cache(pages, nr) \
Quoted 8 lines

include/linux/swap.h static inline void put_swap_device(struct swap_info_struct *si) · line 517

Quoted 6 lines
include/linux/swap.h +5 -1
-	release_pages((pages), (nr));
+static inline void free_pages_and_caches(struct encoded_page **pages,
+		int nr, bool free_unmapped_file)
+{
+	release_pages(pages, nr);
+}

Why should !CONFIG_SWAP not take care of free_unmapped_file?

There is no reason to exclude it bust just missed this one. I will make sure to address this in the next respin.

Quoted 63 lines
Quoted 50 lines
include/linux/swap.h +0 -0
 static inline void free_swap_cache(struct folio *folio)
 {
mm/mmu_gather.c +5 -3
index fe5b6a031717..5ce5824db07f 100644
@@ -100,7 +100,8 @@ void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma)
  */
 #define MAX_NR_FOLIOS_PER_FREE		512
 
-static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch)
+static void __tlb_batch_free_encoded_pages(struct mm_struct *mm,
+		struct mmu_gather_batch *batch)
 {
 	struct encoded_page **pages = batch->encoded_pages;
 	unsigned int nr, nr_pages;
@@ -135,7 +136,8 @@ static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch)
 			}
 		}
 
-		free_pages_and_swap_cache(pages, nr);
+		free_pages_and_caches(pages, nr,
+				      mm_flags_test(MMF_UNSTABLE, mm));
 		pages += nr;
 		batch->nr -= nr;
 
@@ -148,7 +150,7 @@ static void tlb_batch_pages_flush(struct mmu_gather *tlb)
 	struct mmu_gather_batch *batch;
 
 	for (batch = &tlb->local; batch && batch->nr; batch = batch->next)
-		__tlb_batch_free_encoded_pages(batch);
+		__tlb_batch_free_encoded_pages(tlb->mm, batch);
 	tlb->active = &tlb->local;
 }
 
mm/swap_state.c +4 -0
index 6d0eef7470be..e70a52ead6d3 100644
@@ -400,11 +400,22 @@ void free_folio_and_swap_cache(struct folio *folio)
 		folio_put(folio);
 }
 
+static inline void free_file_cache(struct folio *folio)
+{
+	if (folio_trylock(folio)) {
+		mapping_evict_folio(folio_mapping(folio), folio);

mm/swap_state.c void free_folio_and_swap_cache(struct folio *folio) · line 416

Quoted 10 lines
mm/swap_state.c +7 -0
+		folio_unlock(folio);
+	}
+}
+
 /*
  * Passed an array of pages, drop them all from swapcache and then release
  * them.  They are removed from the LRU and freed if this is their last use.
+ *
+ * If @free_unmapped_file is true, this function will proactively evict clean
+ * file-backed folios if they are no longer mapped.

The parameter name is not really expressive.

Quoted 3 lines

You are not freeing unmapped files.

"try_evict_file_folios" maybe?

+1

Thanks.

#6 lore raw

Re: [RFC 2/3] mm: process_mrelease: skip LRU movement for exclusive file folios

In reply to David Hildenbrand (Arm)
Quoted 57 lines · David Hildenbrand (Arm)
Quoted 42 lines · Minchan Kim

For the process_mrelease reclaim, skip LRU handling for exclusive file-backed folios since they will be freed soon so pointless to move around in the LRU.

This avoids costly LRU movement which accounts for a significant portion of the time during unmap_page_range.

- 91.31% 0.00% mmap_exit_test [kernel.kallsyms] [.] exit_mm

     exit_mm
     __mmput
     exit_mmap
     unmap_vmas

- unmap_page_range
- 55.75% folio_mark_accessed
+ 48.79% __folio_batch_add_and_move
4.23% workingset_activation
+ 12.94% folio_remove_rmap_ptes
+ 9.86% page_table_check_clear
+ 3.34% tlb_flush_mmu
1.06% __page_table_check_pte_clear

---
 mm/memory.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
mm/memory.c +2 -0
index 2f815a34d924..25e17893c919 100644
@@ -1640,6 +1640,8 @@ static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb,
 	bool delay_rmap = false;
 
 	if (!folio_test_anon(folio)) {
+		bool skip_mark_accessed;
+
 		ptent = get_and_clear_full_ptes(mm, addr, pte, nr, tlb->fullmm);
 		if (pte_dirty(ptent)) {
 			folio_mark_dirty(folio);
@@ -1648,7 +1650,16 @@ static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb,
 				*force_flush = true;
 			}

mm/memory.c static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb, · line 1660

Quoted 10 lines
mm/memory.c +8 -1
 		}
-		if (pte_young(ptent) && likely(vma_has_recency(vma)))
+
+		/*
+		 * For the process_mrelease reclaim, skip LRU handling for exclusive
+		 * file-backed folios since they will be freed soon so pointless
+		 * to move around in the LRU.
+		 */
+		skip_mark_accessed = mm_flags_test(MMF_UNSTABLE, mm) &&
+				     folio_mapcount(folio) < 2;

folio_mapcount() is most certainly the wrong thing to use if you want to handle large folios properly.

Quoted 1 line

Maybe !folio_likely_mapped_shared() is what you are looking for. Maybe.

Didn't know that. I will use folio_maybe_mapped_shared in next revision.

Thank you!

maintainer
#17 lore raw
In reply to Minchan Kim
Quoted 20 lines · Minchan Kim
Quoted 19 lines · Michal Hocko
Quoted 17 lines · Minchan Kim

This patch series introduces optimizations to expedite memory reclamation in process_mrelease() and provides a secure, race-free "auto-kill" mechanism for efficient container shutdown and OOM handling.

Currently, process_mrelease() unmaps pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. Furthermore, requiring userspace to send a SIGKILL prior to invoking process_mrelease() introduces scheduling race conditions where the victim task may enter the exit path prematurely, bypassing expedited reclamation hooks.

This series addresses these limitations in three logical steps.

Patch #1: mm: process_mrelease: expedite clean file folio reclaim via mmu_gather Integrates clean file folio eviction directly into the low-level TLB batching (mmu_gather) infrastructure. Symmetrically truncates clean file folios alongside anonymous pages during the unmap loop.

Quoted 7 lines
Quoted 2 lines

Why do we need to care about clean page cache? Is this a form of drop_caches?

The goal is to ensure the memory is actually freed by the time process_mrelease returns. Currently, process_mrelease unmaps pages, but page caches remain on the LRU, leaving them to be reclaimed later by kswapd or direct reclaim.

Correct. This was the initial design decision because there is not much you can assume about page cache pages which are very often shared. Even if they are not mapped by all users.

Quoted 4 lines

This delay defeats the purpose of
"expedited" release. It’s not a global drop_caches, but rather a targeted eviction for the victim process to make its memory immediately available for other urgent allocations.

Clean page cache reclaim should be quite effective. Why doesn't kswapd keep up in that regards? Or is this more a per-memcg problem where there is no background reclaim and you are hitting direct reclaim to clean up those pages?

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Patch #2: mm: process_mrelease: skip LRU movement for exclusive file folios Skips costly LRU marking (folio_mark_accessed) for exclusive file-backed folios undergoing process_mrelease reclaim. Perf profiling reveals that LRU movement accounts for ~55% of overhead during unmap.

Quoted 8 lines
Quoted 1 line

OK, but why is this not desirable behavior fir mrelease?

In Android, lmkd kills background apps under memory pressure and then calls process_mrelease. If the memory release is slow due to LRU overhead (~55% as noted), it cannot keep up with the allocation speed of the foreground app. This delay often leads to "over-killing" - killing more background apps than necessary because the system hasn't yet "seen" the memory freed from the first kill.

OK, I see. More on that below.

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

Patch #3: mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag Adds an auto-kill flag supporting atomic teardown. Utilizes a dedicated signal code (KILL_MRELEASE) to guarantee MMF_UNSTABLE is marked in the signal delivery path, preventing scheduling races.

Could you explain why those races are a real problem?

The race occurs when the victim process starts its own exit path (after SIGKILL) before the caller can invoke process_mrelease. If the victim reaches the exit path first, the caller might lose the window to apply these expedited reclamation optimizations.

Isn't this the problem you are trying to solve then? You are special casing process_mrelease while you really want to expedite the process memory clean up.

The same situation happens with the global OOM and your approach doesn't really close the race anyway. You send SIGKILL first and the victim can hit the exit path right after that before you start processing the rest. That is not fundamentally different from doing that in two syscalls, race window is just smaller.

All that being said, I do not think those special hacks for process_mrelease is the right approach. I very much agree that the address space tear down for a dying process could be improved and we should be focusing on that part.

Signature · 2 lines
Michal Hocko
SUSE Labs
#18 lore raw
Quoted 24 lines · Michal Hocko
Quoted 23 lines · Minchan Kim
Quoted 21 lines · Michal Hocko
Quoted 11 lines · Minchan Kim

This patch series introduces optimizations to expedite memory reclamation in process_mrelease() and provides a secure, race-free "auto-kill" mechanism for efficient container shutdown and OOM handling.

Currently, process_mrelease() unmaps pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. Furthermore, requiring userspace to send a SIGKILL prior to invoking process_mrelease() introduces scheduling race conditions where the victim task may enter the exit path prematurely, bypassing expedited reclamation hooks.

Quoted 6 lines

This series addresses these limitations in three logical steps.

Patch #1: mm: process_mrelease: expedite clean file folio reclaim via mmu_gather Integrates clean file folio eviction directly into the low-level TLB batching (mmu_gather) infrastructure. Symmetrically truncates clean file folios alongside anonymous pages during the unmap loop.

Why do we need to care about clean page cache? Is this a form of drop_caches?

Quoted 8 lines
Quoted 4 lines

The goal is to ensure the memory is actually freed by the time process_mrelease returns. Currently, process_mrelease unmaps pages, but page caches remain on the LRU, leaving them to be reclaimed later by kswapd or direct reclaim.

Correct. This was the initial design decision because there is not much you can assume about page cache pages which are very often shared. Even if they are not mapped by all users.

Fair point. However, that's the trade-off:

Leaving unmapped caches to be reclaimed asynchronously keeps system memory pressure high for too long. In Android, this delay forces the LMKD to unnecessarily kill additional innocent background apps before the memory from the original victim is recovered.

Furthermore, this approach maintains safety: mapping_evict_folio() will naturally fail if the folio is still actively shared by other processes. Yeah, that's not perfect but minimal safety net.

I believe preventing redundant background app kills is a highly beneficial trade-off than adding more IOs since user can keep listening their music.

Quoted 10 lines
Quoted 5 lines

This delay defeats the purpose of
"expedited" release. It’s not a global drop_caches, but rather a targeted eviction for the victim process to make its memory immediately available for other urgent allocations.

Clean page cache reclaim should be quite effective. Why doesn't kswapd keep up in that regards? Or is this more a per-memcg problem where there is no background reclaim and you are hitting direct reclaim to clean up those pages?

So many reasons: Cannot say everything how kswapd is inefficient compared to memory allocation.

1. kswapd is CPU hungry.
2. Out of control which core(big, midlle or little) kswapd will run
3. Allocatoin is super fast than the memory reclaim
4. Kswapd is stuck on some lock
5. Kswapd is doing swapping out since anon/file reclaims are serialized

Quoted 33 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Patch #2: mm: process_mrelease: skip LRU movement for exclusive file folios Skips costly LRU marking (folio_mark_accessed) for exclusive file-backed folios undergoing process_mrelease reclaim. Perf profiling reveals that LRU movement accounts for ~55% of overhead during unmap.

Quoted 8 lines
Quoted 1 line

OK, but why is this not desirable behavior fir mrelease?

In Android, lmkd kills background apps under memory pressure and then calls process_mrelease. If the memory release is slow due to LRU overhead (~55% as noted), it cannot keep up with the allocation speed of the foreground app. This delay often leads to "over-killing" - killing more background apps than necessary because the system hasn't yet "seen" the memory freed from the first kill.

OK, I see. More on that below.

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

Patch #3: mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag Adds an auto-kill flag supporting atomic teardown. Utilizes a dedicated signal code (KILL_MRELEASE) to guarantee MMF_UNSTABLE is marked in the signal delivery path, preventing scheduling races.

Could you explain why those races are a real problem?

The race occurs when the victim process starts its own exit path (after SIGKILL) before the caller can invoke process_mrelease. If the victim reaches the exit path first, the caller might lose the window to apply these expedited reclamation optimizations.

Isn't this the problem you are trying to solve then? You are special casing process_mrelease while you really want to expedite the process memory clean up.

Quoted 5 lines

The same situation happens with the global OOM and your approach doesn't really close the race anyway. You send SIGKILL first and the victim can hit the exit path right after that before you start processing the rest. That is not fundamentally different from doing that in two syscalls, race window is just smaller.

No, this approach completely close the race.

When it invokes do_send_sig_info(SIGKILL) with the KILL_MRELEASE code, the kernel sets the MMF_UNSTABLE flag on the victim's mm_struct in the signal delivery path (kernel/signal.c) *before* the task begins processing the signal.

When the victim gets scheduled and wakes up to process the fatal signal, the MMF_UNSTABLE flag is already set.

This guarantees that the victim's own exit path (do_exit -> exit_mmap) will utilize the expedited reclamation optimizations automatically, regardless of whether the reaper or the victim gets scheduled first.

For the OOM, we can use the same idea.

Quoted 5 lines

All that being said, I do not think those special hacks for process_mrelease is the right approach. I very much agree that the address space tear down for a dying process could be improved and we should be focusing on that part.

I think process_mrelease is crucial here because relying on the exit path is non-deterministic.

The mm_struct can have its reference count increased by other tasks or kernel subsystems. When a process exits, the actual address space teardown (exit_mmap) is deferred until the mm's reference count reaches zero. If any component holds a reference, the tearing down is delayed indefinitely. We have observed the problem in the field quite a lot.

This is precisely why process_mrelease was introduced in the first place. It allows an external reaper to bypass the reference count delays and synchronously reap the memory of a dying process on demand.

Improving the regular exit path alone cannot substitute for this, as it would still be stalled by outstanding reference counts.

I don't know what alternative ideas you are thinking about, but please share if you have a better approach.

maintainer
#19 lore raw
Quoted 34 lines · Minchan Kim
Quoted 24 lines · Michal Hocko
Quoted 23 lines · Minchan Kim
Quoted 21 lines · Michal Hocko
Quoted 11 lines · Minchan Kim

This patch series introduces optimizations to expedite memory reclamation in process_mrelease() and provides a secure, race-free "auto-kill" mechanism for efficient container shutdown and OOM handling.

Currently, process_mrelease() unmaps pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. Furthermore, requiring userspace to send a SIGKILL prior to invoking process_mrelease() introduces scheduling race conditions where the victim task may enter the exit path prematurely, bypassing expedited reclamation hooks.

Quoted 6 lines

This series addresses these limitations in three logical steps.

Patch #1: mm: process_mrelease: expedite clean file folio reclaim via mmu_gather Integrates clean file folio eviction directly into the low-level TLB batching (mmu_gather) infrastructure. Symmetrically truncates clean file folios alongside anonymous pages during the unmap loop.

Why do we need to care about clean page cache? Is this a form of drop_caches?

Quoted 8 lines
Quoted 4 lines

The goal is to ensure the memory is actually freed by the time process_mrelease returns. Currently, process_mrelease unmaps pages, but page caches remain on the LRU, leaving them to be reclaimed later by kswapd or direct reclaim.

Correct. This was the initial design decision because there is not much you can assume about page cache pages which are very often shared. Even if they are not mapped by all users.

Quoted 6 lines

Fair point. However, that's the trade-off:

Leaving unmapped caches to be reclaimed asynchronously keeps system memory pressure high for too long. In Android, this delay forces the LMKD to unnecessarily kill additional innocent background apps before the memory from the original victim is recovered.

OK, this is really not clear to me. How come you end up triggering LMKD (or any OOM handling) when there is a considerable amount of clean page cache?

[...]

Quoted 15 lines
Quoted 9 lines
Quoted 4 lines

The race occurs when the victim process starts its own exit path (after SIGKILL) before the caller can invoke process_mrelease. If the victim reaches the exit path first, the caller might lose the window to apply these expedited reclamation optimizations.

Isn't this the problem you are trying to solve then? You are special casing process_mrelease while you really want to expedite the process memory clean up.

Quoted 5 lines

The same situation happens with the global OOM and your approach doesn't really close the race anyway. You send SIGKILL first and the victim can hit the exit path right after that before you start processing the rest. That is not fundamentally different from doing that in two syscalls, race window is just smaller.

Quoted 5 lines

No, this approach completely close the race.

When it invokes do_send_sig_info(SIGKILL) with the KILL_MRELEASE code, the kernel sets the MMF_UNSTABLE flag on the victim's mm_struct in the signal delivery path (kernel/signal.c) *before* the task begins processing the signal.

OK, I have missed this part. I haven't really looked into specific patches at this stage. I am still trying to understand the motivation and your reasoning. So effectivelly you want to get SIGOOMKILL more or less.

Quoted 10 lines

When the victim gets scheduled and wakes up to process the fatal signal, the MMF_UNSTABLE flag is already set.

This guarantees that the victim's own exit path (do_exit -> exit_mmap) will utilize the expedited reclamation optimizations automatically, regardless of whether the reaper or the victim gets scheduled first.

For the OOM, we can use the same idea.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

All that being said, I do not think those special hacks for process_mrelease is the right approach. I very much agree that the address space tear down for a dying process could be improved and we should be focusing on that part.

I think process_mrelease is crucial here because relying on the exit path is non-deterministic.

I suspect you are missing my point. I am arguing that those special hacks in the address space release path shouldn't be process_mrelease specific. I do recognize the value of the sync tear down need. I am also in favor of something like SIGOOMKILL. process_mrelease might even be the right syscall for that purpose.

Signature · 2 lines
Michal Hocko
SUSE Labs
#8 lore raw

Re: [RFC 3/3] mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

In reply to Minchan Kim
Quoted 94 lines · Minchan Kim

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

In this case, the victim task frees its memory via the standard, unoptimized exit path, bypassing the expedited clean file folio reclamation optimization introduced in the previous patch (which relies on the MMF_UNSTABLE flag).

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

Crucially, this patch utilizes a dedicated signal code (KILL_MRELEASE) during signal injection, belonging to a new SIGKILL si_codes section. This special code ensures that the kernel's signal delivery path reliably intercepts the request and marks the target address space as unstable (MMF_UNSTABLE). This mechanism guarantees that the MMF_UNSTABLE flag is set before either the victim task or the reaper proceeds, ensuring that the expedited reclamation optimization is utilized regardless of scheduling order.

---
 include/uapi/asm-generic/siginfo.h |  6 ++++++
 include/uapi/linux/mman.h          |  4 ++++
 kernel/signal.c                    |  4 ++++
 mm/oom_kill.c                      | 20 +++++++++++++++++++-
 4 files changed, 33 insertions(+), 1 deletion(-)
include/uapi/asm-generic/siginfo.h +6 -0
index 5a1ca43b5fc6..0f59b791dab4 100644
@@ -252,6 +252,12 @@ typedef struct siginfo {
 #define BUS_MCEERR_AO	5
 #define NSIGBUS		5
 
+/*
+ * SIGKILL si_codes
+ */
+#define KILL_MRELEASE	1	/* sent by process_mrelease */
+#define NSIGKILL	1
+
 /*
  * SIGTRAP si_codes
  */
include/uapi/linux/mman.h +4 -0
index e89d00528f2f..4266976b45ad 100644
@@ -56,4 +56,8 @@ struct cachestat {
 	__u64 nr_recently_evicted;
 };
 
+/* Flags for process_mrelease */
+#define PROCESS_MRELEASE_REAP_KILL	(1 << 0)
+#define PROCESS_MRELEASE_VALID_FLAGS	(PROCESS_MRELEASE_REAP_KILL)
+
 #endif /* _UAPI_LINUX_MMAN_H */
kernel/signal.c +4 -0
index d65d0fe24bfb..c21b2176dc5e 100644
@@ -1134,6 +1134,10 @@ static int __send_signal_locked(int sig, struct kernel_siginfo *info,
 
 out_set:
 	signalfd_notify(t, sig);
+
+	if (sig == SIGKILL && !is_si_special(info) &&
+	    info->si_code == KILL_MRELEASE && t->mm)
+		mm_flags_set(MMF_UNSTABLE, t->mm);
 	sigaddset(&pending->signal, sig);
 
 	/* Let multiprocess signals appear after on-going forks */
mm/oom_kill.c +3 -1
index 5c6c95c169ee..0b5da5208707 100644
@@ -20,6 +20,8 @@
 
 #include <linux/oom.h>
 #include <linux/mm.h>
+#include <uapi/linux/mman.h>
+#include <linux/capability.h>
 #include <linux/err.h>
 #include <linux/gfp.h>
 #include <linux/sched.h>
@@ -1218,13 +1220,29 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
 	bool reap = false;
 	long ret = 0;
 
-	if (flags)
+	if (flags & ~PROCESS_MRELEASE_VALID_FLAGS)

mm/oom_kill.c SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) · line 1233

Quoted 10 lines
mm/oom_kill.c +4 -0
 		return -EINVAL;
 
 	task = pidfd_get_task(pidfd, &f_flags);
 	if (IS_ERR(task))
 		return PTR_ERR(task);
 
+	if (flags & PROCESS_MRELEASE_REAP_KILL) {
+		struct kernel_siginfo info;
+
+		if (!capable(CAP_KILL)) {

Why? Just call a function that uses check_kill_permission() before firing the signal? What's the rational for doing it this way?

Tbh, I really hate that process_mrelease() now has a kill side effect with non-standard permission handling as well.

Seems like bad api design. Why can't you just raise the MMF_UNSTABLE bit before the SIGKILL as that's the problem you're trying to solve.

mm/oom_kill.c SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) · line 1241

Quoted 8 lines
mm/oom_kill.c +8 -0
+			ret = -EPERM;
+			goto put_task;
+		}
+		clear_siginfo(&info);
+		info.si_signo = SIGKILL;
+		info.si_code = KILL_MRELEASE;
+		info.si_pid = task_tgid_vnr(current);
+		info.si_uid = from_kuid_munged(current_user_ns(), current_uid());

This should not be open-coded like this.

Quoted 10 lines
mm/oom_kill.c +4 -0
+
+		do_send_sig_info(SIGKILL, &info, task, PIDTYPE_TGID);
+	}
+
 	/*
 	 * Make sure to choose a thread which still has a reference to mm
 	 * during the group exit
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
#20 lore raw
In reply to Michal Hocko
Quoted 37 lines · Michal Hocko
Quoted 36 lines · Minchan Kim
Quoted 24 lines · Michal Hocko
Quoted 23 lines · Minchan Kim
Quoted 21 lines · Michal Hocko
Quoted 11 lines · Minchan Kim

This patch series introduces optimizations to expedite memory reclamation in process_mrelease() and provides a secure, race-free "auto-kill" mechanism for efficient container shutdown and OOM handling.

Currently, process_mrelease() unmaps pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. Furthermore, requiring userspace to send a SIGKILL prior to invoking process_mrelease() introduces scheduling race conditions where the victim task may enter the exit path prematurely, bypassing expedited reclamation hooks.

Quoted 6 lines

This series addresses these limitations in three logical steps.

Patch #1: mm: process_mrelease: expedite clean file folio reclaim via mmu_gather Integrates clean file folio eviction directly into the low-level TLB batching (mmu_gather) infrastructure. Symmetrically truncates clean file folios alongside anonymous pages during the unmap loop.

Why do we need to care about clean page cache? Is this a form of drop_caches?

Quoted 8 lines
Quoted 4 lines

The goal is to ensure the memory is actually freed by the time process_mrelease returns. Currently, process_mrelease unmaps pages, but page caches remain on the LRU, leaving them to be reclaimed later by kswapd or direct reclaim.

Correct. This was the initial design decision because there is not much you can assume about page cache pages which are very often shared. Even if they are not mapped by all users.

Fair point. However, that's the trade-off:

Quoted 8 lines
Quoted 4 lines

Leaving unmapped caches to be reclaimed asynchronously keeps system memory pressure high for too long. In Android, this delay forces the LMKD to unnecessarily kill additional innocent background apps before the memory from the original victim is recovered.

OK, this is really not clear to me. How come you end up triggering LMKD (or any OOM handling) when there is a considerable amount of clean page cache?

It's not simple to explain all the heuristics, but basically, LMKD is triggered by PSI pressure (usually contributed by kswapd rather than other components like refault, kcompactd, or workingset operations).

It then checks the current free memory against system watermarks. Depending on the free memory size, file cache, and free swap, it decides to start killing background apps.

In other words, LMKD acts as a "userspace kswapd" to assist kernel kswapd's reclamation speed. It is smarter than kswapd because it has high-level knowledge of which processes are okay to be killed rather than forcing slow, unnecessary paing out.

Whenever LMKD is running, kswapd is usually running alongside it. You might wonder why LMKD kills background apps even when there are plenty of clean file pages. That's because the system cannot predict current memory allocation rates. If the allocation is bursty, kswapd can never catch up with the allocation speed. This forces the foreground apps into direct reclaim, resulting in visible UI jank. Android prioritizes UI smoothness and chooses to kill background apps.

Furthermore, when LMKD kills a background app, it expects immediate memory relief. If the clean file pages of the killed process are left on the LRU to be reclaimed asynchronously later, the system's memory pressure (PSI) remains high. This forces LMKD to unnecessarily kill *additional* background apps before the memory from the first victim is fully recovered.

Again, this is why I want process_mrelease expedite clean file reclamation synchronously.

Quoted 20 lines

[...]

Quoted 17 lines
Quoted 9 lines
Quoted 4 lines

The race occurs when the victim process starts its own exit path (after SIGKILL) before the caller can invoke process_mrelease. If the victim reaches the exit path first, the caller might lose the window to apply these expedited reclamation optimizations.

Isn't this the problem you are trying to solve then? You are special casing process_mrelease while you really want to expedite the process memory clean up.

Quoted 5 lines

The same situation happens with the global OOM and your approach doesn't really close the race anyway. You send SIGKILL first and the victim can hit the exit path right after that before you start processing the rest. That is not fundamentally different from doing that in two syscalls, race window is just smaller.

No, this approach completely close the race.

Quoted 8 lines
Quoted 3 lines

When it invokes do_send_sig_info(SIGKILL) with the KILL_MRELEASE code, the kernel sets the MMF_UNSTABLE flag on the victim's mm_struct in the signal delivery path (kernel/signal.c) *before* the task begins processing the signal.

OK, I have missed this part. I haven't really looked into specific patches at this stage. I am still trying to understand the motivation and your reasoning. So effectivelly you want to get SIGOOMKILL more or less.

The fundamental problem with signals is their unpredictability. The time between sending a signal and when the victim task actually handles it is highly non-deterministic. Furthermore, as mentioned earlier, outstanding reference counts on the mm_struct can delay the teardown indefinitely.

Quoted 16 lines
Quoted 16 lines

When the victim gets scheduled and wakes up to process the fatal signal, the MMF_UNSTABLE flag is already set.

This guarantees that the victim's own exit path (do_exit -> exit_mmap) will utilize the expedited reclamation optimizations automatically, regardless of whether the reaper or the victim gets scheduled first.

For the OOM, we can use the same idea.

Quoted 5 lines

All that being said, I do not think those special hacks for process_mrelease is the right approach. I very much agree that the address space tear down for a dying process could be improved and we should be focusing on that part.

Quoted 5 lines
Quoted 2 lines

I think process_mrelease is crucial here because relying on the exit path is non-deterministic.

I suspect you are missing my point. I am arguing that those special hacks in the address space release path shouldn't be process_mrelease

I am a bit confused now. Do you mean you want to apply these expedited reclamation optimizations to ALL dying processes in the common exit path, rather than making them specific to process_mrelease?

I don't think so since you mentioned below "process_mrelease might .. right syscall" but I wanted to clarify what you means.

Quoted 3 lines

specific. I do recognize the value of the sync tear down need. I am also in favor of something like SIGOOMKILL. process_mrelease might even be the right syscall for that purpose.

I am glad we are aligned on the necessity of this feature.

#9 lore raw

Re: [RFC 3/3] mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

In reply to Christian Brauner
Quoted 101 lines · Christian Brauner
Quoted 100 lines · Minchan Kim

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

In this case, the victim task frees its memory via the standard, unoptimized exit path, bypassing the expedited clean file folio reclamation optimization introduced in the previous patch (which relies on the MMF_UNSTABLE flag).

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

Crucially, this patch utilizes a dedicated signal code (KILL_MRELEASE) during signal injection, belonging to a new SIGKILL si_codes section. This special code ensures that the kernel's signal delivery path reliably intercepts the request and marks the target address space as unstable (MMF_UNSTABLE). This mechanism guarantees that the MMF_UNSTABLE flag is set before either the victim task or the reaper proceeds, ensuring that the expedited reclamation optimization is utilized regardless of scheduling order.

---
 include/uapi/asm-generic/siginfo.h |  6 ++++++
 include/uapi/linux/mman.h          |  4 ++++
 kernel/signal.c                    |  4 ++++
 mm/oom_kill.c                      | 20 +++++++++++++++++++-
 4 files changed, 33 insertions(+), 1 deletion(-)
include/uapi/asm-generic/siginfo.h +6 -0
index 5a1ca43b5fc6..0f59b791dab4 100644
@@ -252,6 +252,12 @@ typedef struct siginfo {
 #define BUS_MCEERR_AO	5
 #define NSIGBUS		5
 
+/*
+ * SIGKILL si_codes
+ */
+#define KILL_MRELEASE	1	/* sent by process_mrelease */
+#define NSIGKILL	1
+
 /*
  * SIGTRAP si_codes
  */
include/uapi/linux/mman.h +4 -0
index e89d00528f2f..4266976b45ad 100644
@@ -56,4 +56,8 @@ struct cachestat {
 	__u64 nr_recently_evicted;
 };
 
+/* Flags for process_mrelease */
+#define PROCESS_MRELEASE_REAP_KILL	(1 << 0)
+#define PROCESS_MRELEASE_VALID_FLAGS	(PROCESS_MRELEASE_REAP_KILL)
+
 #endif /* _UAPI_LINUX_MMAN_H */
kernel/signal.c +4 -0
index d65d0fe24bfb..c21b2176dc5e 100644
@@ -1134,6 +1134,10 @@ static int __send_signal_locked(int sig, struct kernel_siginfo *info,
 
 out_set:
 	signalfd_notify(t, sig);
+
+	if (sig == SIGKILL && !is_si_special(info) &&
+	    info->si_code == KILL_MRELEASE && t->mm)
+		mm_flags_set(MMF_UNSTABLE, t->mm);
 	sigaddset(&pending->signal, sig);
 
 	/* Let multiprocess signals appear after on-going forks */
mm/oom_kill.c +3 -1
index 5c6c95c169ee..0b5da5208707 100644
@@ -20,6 +20,8 @@
 
 #include <linux/oom.h>
 #include <linux/mm.h>
+#include <uapi/linux/mman.h>
+#include <linux/capability.h>
 #include <linux/err.h>
 #include <linux/gfp.h>
 #include <linux/sched.h>
@@ -1218,13 +1220,29 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
 	bool reap = false;
 	long ret = 0;
 
-	if (flags)
+	if (flags & ~PROCESS_MRELEASE_VALID_FLAGS)
 		return -EINVAL;
 
 	task = pidfd_get_task(pidfd, &f_flags);
 	if (IS_ERR(task))
 		return PTR_ERR(task);
 
Quoted 7 lines

mm/oom_kill.c SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) · line 1233

Quoted 4 lines
mm/oom_kill.c +4 -0
+	if (flags & PROCESS_MRELEASE_REAP_KILL) {
+		struct kernel_siginfo info;
+
+		if (!capable(CAP_KILL)) {

Why? Just call a function that uses check_kill_permission() before firing the signal? What's the rational for doing it this way?

Thanks for pointing that out. I wasn't aware of check_kill_permission().

I took a look at it, and it seems check_kill_permission() handles permissions primarily for signals sent from userspace. Since we are injecting the signal from the kernel side using a positive si_code (KILL_MRELEASE), check_kill_permission() would just return 0 and skip the permission checks entirely.

I am open to better ideas if there is a more standard way to handle permission checks for kernel-injected signals.

Quoted 6 lines

Tbh, I really hate that process_mrelease() now has a kill side effect with non-standard permission handling as well.

Seems like bad api design. Why can't you just raise the MMF_UNSTABLE bit before the SIGKILL as that's the problem you're trying to solve.

The problem is that process_mrelease() strictly requires the target process to already have a pending fatal signal or be in the exit path before it allows any operation.

Therefore, we cannot invoke process_mrelease() to just set the MMF_UNSTABLE flag *before* the SIGKILL is sent.

If I send the SIGKILL first to satisfy the process_mrelease() requirement, we immediately run into the scheduling race condition where the victim can enter the exit path before the reaper can set the flag.

This circular dependency is exactly why I had to integrate the kill operation into process_mrelease() to make it atomic.

Quoted 11 lines

mm/oom_kill.c SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) · line 1241

Quoted 9 lines
mm/oom_kill.c +8 -0
+			ret = -EPERM;
+			goto put_task;
+		}
+		clear_siginfo(&info);
+		info.si_signo = SIGKILL;
+		info.si_code = KILL_MRELEASE;
+		info.si_pid = task_tgid_vnr(current);
+		info.si_uid = from_kuid_munged(current_user_ns(), current_uid());

This should not be open-coded like this.

Good point.
Maybe, I can reuse prepare_kill_siginfo.

Quoted 11 lines
Quoted 11 lines
mm/oom_kill.c +4 -0
+
+		do_send_sig_info(SIGKILL, &info, task, PIDTYPE_TGID);
+	}
+
 	/*
 	 * Make sure to choose a thread which still has a reference to mm
 	 * during the group exit
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
maintainer
#10 lore raw

Re: [RFC 3/3] mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

Quoted 3 lines · Minchan Kim

If I send the SIGKILL first to satisfy the process_mrelease() requirement, we immediately run into the scheduling race condition where the victim can enter the exit path before the reaper can set the flag.

Why don't you just grab the mm before you send the signal and then continue with reaping? You just want to avoid a race where the victim manages to process fatal signal, start its exit path and mrelease path losing that race so you rely on the exit path, right?

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#21 lore raw
In reply to Minchan Kim
Quoted 67 lines · Minchan Kim
Quoted 37 lines · Michal Hocko
Quoted 36 lines · Minchan Kim
Quoted 24 lines · Michal Hocko
Quoted 23 lines · Minchan Kim
Quoted 21 lines · Michal Hocko
Quoted 11 lines · Minchan Kim

This patch series introduces optimizations to expedite memory reclamation in process_mrelease() and provides a secure, race-free "auto-kill" mechanism for efficient container shutdown and OOM handling.

Currently, process_mrelease() unmaps pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. Furthermore, requiring userspace to send a SIGKILL prior to invoking process_mrelease() introduces scheduling race conditions where the victim task may enter the exit path prematurely, bypassing expedited reclamation hooks.

Quoted 6 lines

This series addresses these limitations in three logical steps.

Patch #1: mm: process_mrelease: expedite clean file folio reclaim via mmu_gather Integrates clean file folio eviction directly into the low-level TLB batching (mmu_gather) infrastructure. Symmetrically truncates clean file folios alongside anonymous pages during the unmap loop.

Why do we need to care about clean page cache? Is this a form of drop_caches?

Quoted 8 lines
Quoted 4 lines

The goal is to ensure the memory is actually freed by the time process_mrelease returns. Currently, process_mrelease unmaps pages, but page caches remain on the LRU, leaving them to be reclaimed later by kswapd or direct reclaim.

Correct. This was the initial design decision because there is not much you can assume about page cache pages which are very often shared. Even if they are not mapped by all users.

Fair point. However, that's the trade-off:

Quoted 8 lines
Quoted 4 lines

Leaving unmapped caches to be reclaimed asynchronously keeps system memory pressure high for too long. In Android, this delay forces the LMKD to unnecessarily kill additional innocent background apps before the memory from the original victim is recovered.

OK, this is really not clear to me. How come you end up triggering LMKD (or any OOM handling) when there is a considerable amount of clean page cache?

It's not simple to explain all the heuristics, but basically, LMKD is triggered by PSI pressure (usually contributed by kswapd rather than other components like refault, kcompactd, or workingset operations).

It then checks the current free memory against system watermarks. Depending on the free memory size, file cache, and free swap, it decides to start killing background apps.

In other words, LMKD acts as a "userspace kswapd" to assist kernel kswapd's reclamation speed. It is smarter than kswapd because it has high-level knowledge of which processes are okay to be killed rather than forcing slow, unnecessary paing out.

Whenever LMKD is running, kswapd is usually running alongside it. You might wonder why LMKD kills background apps even when there are plenty of clean file pages. That's because the system cannot predict current memory allocation rates. If the allocation is bursty, kswapd can never catch up with the allocation speed. This forces the foreground apps into direct reclaim, resulting in visible UI jank. Android prioritizes UI smoothness and chooses to kill background apps.

Quoted 8 lines

Furthermore, when LMKD kills a background app, it expects immediate memory relief. If the clean file pages of the killed process are left on the LRU to be reclaimed asynchronously later, the system's memory pressure (PSI) remains high. This forces LMKD to unnecessarily kill *additional* background apps before the memory from the first victim is fully recovered.

Again, this is why I want process_mrelease expedite clean file reclamation synchronously.

How much of a clean page cache do you usually drop this way?

[...]

Quoted 6 lines
Quoted 2 lines

I suspect you are missing my point. I am arguing that those special hacks in the address space release path shouldn't be process_mrelease

I am a bit confused now. Do you mean you want to apply these expedited reclamation optimizations to ALL dying processes in the common exit path, rather than making them specific to process_mrelease?

Yes. All which make sense, really. I am still not convinced about the clean page cache because that just seems like a hack to workaround wrong userspace oom heuristics.

Signature · 2 lines
Michal Hocko
SUSE Labs
#11 lore raw

Re: [RFC 3/3] mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

In reply to Michal Hocko
Quoted 9 lines · Michal Hocko
Quoted 3 lines · Minchan Kim

If I send the SIGKILL first to satisfy the process_mrelease() requirement, we immediately run into the scheduling race condition where the victim can enter the exit path before the reaper can set the flag.

Why don't you just grab the mm before you send the signal and then continue with reaping? You just want to avoid a race where the victim manages to process fatal signal, start its exit path and mrelease path losing that race so you rely on the exit path, right?

The problem is that process_mrelease() operates on a task obtained from a pidfd.

Once the victim process receives the SIGKILL and enters the exit path (exit_mm), the kernel sets task->mm to NULL.

Even if we could somehow hold a reference to the mm_struct beforehand, process_mrelease() would still fail because mm_struct via task returns NULL after exit_mm() has been called.

Therefore, we cannot simply "grab the mm" before sending the signal and expect process_mrelease() to work after the victim starts exiting.

#22 lore raw
In reply to Michal Hocko
Quoted 74 lines · Michal Hocko
Quoted 73 lines · Minchan Kim
Quoted 37 lines · Michal Hocko
Quoted 36 lines · Minchan Kim
Quoted 24 lines · Michal Hocko
Quoted 23 lines · Minchan Kim
Quoted 21 lines · Michal Hocko
Quoted 11 lines · Minchan Kim

This patch series introduces optimizations to expedite memory reclamation in process_mrelease() and provides a secure, race-free "auto-kill" mechanism for efficient container shutdown and OOM handling.

Currently, process_mrelease() unmaps pages but leaves clean file folios on the LRU list, relying on standard memory reclaim to eventually free them. Furthermore, requiring userspace to send a SIGKILL prior to invoking process_mrelease() introduces scheduling race conditions where the victim task may enter the exit path prematurely, bypassing expedited reclamation hooks.

Quoted 6 lines

This series addresses these limitations in three logical steps.

Patch #1: mm: process_mrelease: expedite clean file folio reclaim via mmu_gather Integrates clean file folio eviction directly into the low-level TLB batching (mmu_gather) infrastructure. Symmetrically truncates clean file folios alongside anonymous pages during the unmap loop.

Why do we need to care about clean page cache? Is this a form of drop_caches?

Quoted 8 lines
Quoted 4 lines

The goal is to ensure the memory is actually freed by the time process_mrelease returns. Currently, process_mrelease unmaps pages, but page caches remain on the LRU, leaving them to be reclaimed later by kswapd or direct reclaim.

Correct. This was the initial design decision because there is not much you can assume about page cache pages which are very often shared. Even if they are not mapped by all users.

Fair point. However, that's the trade-off:

Quoted 8 lines
Quoted 4 lines

Leaving unmapped caches to be reclaimed asynchronously keeps system memory pressure high for too long. In Android, this delay forces the LMKD to unnecessarily kill additional innocent background apps before the memory from the original victim is recovered.

OK, this is really not clear to me. How come you end up triggering LMKD (or any OOM handling) when there is a considerable amount of clean page cache?

It's not simple to explain all the heuristics, but basically, LMKD is triggered by PSI pressure (usually contributed by kswapd rather than other components like refault, kcompactd, or workingset operations).

It then checks the current free memory against system watermarks. Depending on the free memory size, file cache, and free swap, it decides to start killing background apps.

In other words, LMKD acts as a "userspace kswapd" to assist kernel kswapd's reclamation speed. It is smarter than kswapd because it has high-level knowledge of which processes are okay to be killed rather than forcing slow, unnecessary paing out.

Whenever LMKD is running, kswapd is usually running alongside it. You might wonder why LMKD kills background apps even when there are plenty of clean file pages. That's because the system cannot predict current memory allocation rates. If the allocation is bursty, kswapd can never catch up with the allocation speed. This forces the foreground apps into direct reclaim, resulting in visible UI jank. Android prioritizes UI smoothness and chooses to kill background apps.

Furthermore, when LMKD kills a background app, it expects immediate memory relief. If the clean file pages of the killed process are left on the LRU to be reclaimed asynchronously later, the system's memory pressure (PSI) remains high. This forces LMKD to unnecessarily kill *additional* background apps before the memory from the first victim is fully recovered.

Quoted 4 lines
Quoted 2 lines

Again, this is why I want process_mrelease expedite clean file reclamation synchronously.

How much of a clean page cache do you usually drop this way?

Based on some measurements on typical device, the numbers are actually quite significant. The total amount of exclusive clean page cache across all killable apps was around 800 to 900 MB.

While even typical background apps often hold tens to hundreds of megabytes, heavier applications(e.g., modern on-device AI workloads) can easily hold several gigabytes of clean file cache all by themselves.

So by using this expedited reclaim, we can grab anywhere from hundreds of megabytes to several gigabytes instantly when a process is killed. That's definitely enough to relieve the pressure right away and stop unnecessary redundant/perceptible kills.

Quoted 5 lines

[...]

Quoted 3 lines
Quoted 2 lines

I suspect you are missing my point. I am arguing that those special hacks in the address space release path shouldn't be process_mrelease

Quoted 7 lines
Quoted 3 lines

I am a bit confused now. Do you mean you want to apply these expedited reclamation optimizations to ALL dying processes in the common exit path, rather than making them specific to process_mrelease?

Yes. All which make sense, really. I am still not convinced about the clean page cache because that just seems like a hack to workaround wrong userspace oom heuristics.

I see it a bit differently. When paltform decides to kill a process to free up memory, they want that memory back right away.

So it doesn't make much sense for the kernel to ignore that and leave the clean file pages to be picked up slowly by kswapd later.

In some aspects, you can think of LMKD as a more specialized, userspace version of kswapd. It has high-level knowledge of process priorities and knows exactly which process is safe to kill to get memory instantly. The kernel's kswapd, however, operates globally without this specific process-level awareness, which makes it less suited for this kind of targeted reclamation.

If we force LMKD to rely on the slower global kswapd to actually free the clean pages, it defeats the whole purpose of targeting a specific process.

So letting process_mrelease speed this up isn't a hack at all. It's just helping the kernel do what the admin wanted in the first place: fast, targeted memory.

maintainer
#12 lore raw

Re: [RFC 3/3] mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

In reply to Minchan Kim
Quoted 16 lines · Minchan Kim
Quoted 9 lines · Michal Hocko
Quoted 3 lines · Minchan Kim

If I send the SIGKILL first to satisfy the process_mrelease() requirement, we immediately run into the scheduling race condition where the victim can enter the exit path before the reaper can set the flag.

Why don't you just grab the mm before you send the signal and then continue with reaping? You just want to avoid a race where the victim manages to process fatal signal, start its exit path and mrelease path losing that race so you rely on the exit path, right?

The problem is that process_mrelease() operates on a task obtained from a pidfd.

Once the victim process receives the SIGKILL and enters the exit path (exit_mm), the kernel sets task->mm to NULL.

Quoted 6 lines

Even if we could somehow hold a reference to the mm_struct beforehand, process_mrelease() would still fail because mm_struct via task returns NULL after exit_mm() has been called.

Therefore, we cannot simply "grab the mm" before sending the signal and expect process_mrelease() to work after the victim starts exiting.

I do not follow. Why cannot you simply do this

mm/oom_kill.c +7 -2
index 5c6c95c169ee..b80a96f5460a 100644
@@ -1241,9 +1241,14 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
 	if (task_will_free_mem(p))
 		reap = true;
 	else {
+		if (flags & PROCESS_MRELEASE_REAP_KILL) {
+		} else {
+			/* send SIGKILL */
+			reap = true;
 		/* Error only if the work has not been done already */
-		if (!mm_flags_test(MMF_OOM_SKIP, mm))
-			ret = -EINVAL;
+			if (!mm_flags_test(MMF_OOM_SKIP, mm))
+				ret = -EINVAL;
+		}
 	}
 	task_unlock(p);
 
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#23 lore raw
In reply to Minchan Kim
Quoted 1 line · Minchan Kim

On Fri, Apr 17, 2026 at 09:11:21AM +0200, Michal Hocko wrote:

[...]

Quoted 16 lines
Quoted 3 lines

Yes. All which make sense, really. I am still not convinced about the clean page cache because that just seems like a hack to workaround wrong userspace oom heuristics.

I see it a bit differently. When paltform decides to kill a process to free up memory, they want that memory back right away.

So it doesn't make much sense for the kernel to ignore that and leave the clean file pages to be picked up slowly by kswapd later.

In some aspects, you can think of LMKD as a more specialized, userspace version of kswapd. It has high-level knowledge of process priorities and knows exactly which process is safe to kill to get memory instantly. The kernel's kswapd, however, operates globally without this specific process-level awareness, which makes it less suited for this kind of targeted reclamation.

Quoted 5 lines

If we force LMKD to rely on the slower global kswapd to actually free the clean pages, it defeats the whole purpose of targeting a specific process.

So letting process_mrelease speed this up isn't a hack at all. It's just helping the kernel do what the admin wanted in the first place: fast, targeted memory.

This is a very creative/disruptive way to do a memory reclaim. From a user POV I would much rather see clean page cache reclaimed before my apps start to disappear. But this is obviously your call and your users that will care.

Anyway, I still maintain my position. I do not think it is a good idea to drop clean page cache as you do not know whether there are other users. I am NOT NAKing this patch though but please make sure you have a wider support for this idea before this gets merged. Also make sure that all the above reasoning is part of the changelog if you want to get this merged.

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

On Fri, Apr 17, 2026 at 09:11:21AM +0200, Michal Hocko wrote:

[...]

Quoted 16 lines
Quoted 3 lines

Yes. All which make sense, really. I am still not convinced about the clean page cache because that just seems like a hack to workaround wrong userspace oom heuristics.

I see it a bit differently. When paltform decides to kill a process to free up memory, they want that memory back right away.

So it doesn't make much sense for the kernel to ignore that and leave the clean file pages to be picked up slowly by kswapd later.

In some aspects, you can think of LMKD as a more specialized, userspace version of kswapd. It has high-level knowledge of process priorities and knows exactly which process is safe to kill to get memory instantly. The kernel's kswapd, however, operates globally without this specific process-level awareness, which makes it less suited for this kind of targeted reclamation.

Quoted 5 lines

If we force LMKD to rely on the slower global kswapd to actually free the clean pages, it defeats the whole purpose of targeting a specific process.

So letting process_mrelease speed this up isn't a hack at all. It's just helping the kernel do what the admin wanted in the first place: fast, targeted memory.

Quoted 8 lines

This is a very creative/disruptive way to do a memory reclaim. From a user POV I would much rather see clean page cache reclaimed before my apps start to disappear. But this is obviously your call and your users that will care.

Anyway, I still maintain my position. I do not think it is a good idea to drop clean page cache as you do not know whether there are other users.

IIRC, Johannes raised in the past the we cannot predict the future.

For example, if an app gets OOM-killed, wouldn't we usually try restarting it, re-consuming the clean pagecache pages we would be evicting here?

Just a thought.

Signature · 3 lines
Cheers,

David
#25 lore raw
Quoted 27 lines · David Hildenbrand (Arm)
Quoted 25 lines · Michal Hocko
Quoted 1 line · Minchan Kim

On Fri, Apr 17, 2026 at 09:11:21AM +0200, Michal Hocko wrote:

[...]

Quoted 21 lines
Quoted 3 lines

Yes. All which make sense, really. I am still not convinced about the clean page cache because that just seems like a hack to workaround wrong userspace oom heuristics.

I see it a bit differently. When paltform decides to kill a process to free up memory, they want that memory back right away.

So it doesn't make much sense for the kernel to ignore that and leave the clean file pages to be picked up slowly by kswapd later.

In some aspects, you can think of LMKD as a more specialized, userspace version of kswapd. It has high-level knowledge of process priorities and knows exactly which process is safe to kill to get memory instantly. The kernel's kswapd, however, operates globally without this specific process-level awareness, which makes it less suited for this kind of targeted reclamation.

If we force LMKD to rely on the slower global kswapd to actually free the clean pages, it defeats the whole purpose of targeting a specific process.

So letting process_mrelease speed this up isn't a hack at all. It's just helping the kernel do what the admin wanted in the first place: fast, targeted memory.

Quoted 8 lines
Quoted 8 lines

This is a very creative/disruptive way to do a memory reclaim. From a user POV I would much rather see clean page cache reclaimed before my apps start to disappear. But this is obviously your call and your users that will care.

Anyway, I still maintain my position. I do not think it is a good idea to drop clean page cache as you do not know whether there are other users.

I'm very much familiar with these issues in Android and really want to find a good solution for them. IIUC, this RFC tries to address 2 things at once:
1. handling clean private page cache when reaping memory of a kill victim;
2. addressing a race between kill() and process_release() when process_release() can't happen before the kill() but if it happens too late after the victim passed its exit_mm() then process_release() fails to find the mm to reap. This defeats the purpose of process_release() call because the actual memory (released by exit_mmap()) might not yet be free and a successful process_release() would be very beneficial.

I see these two as separate issues and I'm not sure combining them into a single discussion is a good idea.

Quoted 5 lines

IIRC, Johannes raised in the past the we cannot predict the future.

For example, if an app gets OOM-killed, wouldn't we usually try restarting it, re-consuming the clean pagecache pages we would be evicting here?

Sure, we can't predict which app the user will use next, so when killing we usually kill the least recently used one. That's a reasonable strategy in most cases.
In general, if speeding up the victim's reclaim negatively affects the overall user workflow then this would mean we are selecting wrong kill targets. In that case, we would need to adjust the target selection strategy.

Thanks for tackling this Minchan! I'll try to review the patches this weekend and provide my feedback.
Thanks,
Suren.

Quoted 7 lines

Just a thought.

--
Cheers,

David

#13 lore raw

Re: [RFC 3/3] mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

In reply to Michal Hocko
Quoted 17 lines · Michal Hocko
Quoted 16 lines · Minchan Kim
Quoted 9 lines · Michal Hocko
Quoted 3 lines · Minchan Kim

If I send the SIGKILL first to satisfy the process_mrelease() requirement, we immediately run into the scheduling race condition where the victim can enter the exit path before the reaper can set the flag.

Why don't you just grab the mm before you send the signal and then continue with reaping? You just want to avoid a race where the victim manages to process fatal signal, start its exit path and mrelease path losing that race so you rely on the exit path, right?

The problem is that process_mrelease() operates on a task obtained from a pidfd.

Once the victim process receives the SIGKILL and enters the exit path (exit_mm), the kernel sets task->mm to NULL.

Quoted 8 lines
Quoted 6 lines

Even if we could somehow hold a reference to the mm_struct beforehand, process_mrelease() would still fail because mm_struct via task returns NULL after exit_mm() has been called.

Therefore, we cannot simply "grab the mm" before sending the signal and expect process_mrelease() to work after the victim starts exiting.

I do not follow. Why cannot you simply do this

I misunderstood your point. Do you mean this?

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

There are more details to figure out.

Quoted 25 lines
mm/oom_kill.c +7 -2
index 5c6c95c169ee..b80a96f5460a 100644
@@ -1241,9 +1241,14 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
 	if (task_will_free_mem(p))
 		reap = true;
 	else {
+		if (flags & PROCESS_MRELEASE_REAP_KILL) {
+		} else {
+			/* send SIGKILL */
+			reap = true;
 		/* Error only if the work has not been done already */
-		if (!mm_flags_test(MMF_OOM_SKIP, mm))
-			ret = -EINVAL;
+			if (!mm_flags_test(MMF_OOM_SKIP, mm))
+				ret = -EINVAL;
+		}
 	}
 	task_unlock(p);
 
Signature · 3 lines
-- 
Michal Hocko
SUSE Labs
#30 lore raw
In reply to Michal Hocko
Quoted 22 lines · Michal Hocko
Quoted 1 line · Minchan Kim

On Fri, Apr 17, 2026 at 09:11:21AM +0200, Michal Hocko wrote:

[...]

Quoted 19 lines
Quoted 3 lines

Yes. All which make sense, really. I am still not convinced about the clean page cache because that just seems like a hack to workaround wrong userspace oom heuristics.

I see it a bit differently. When paltform decides to kill a process to free up memory, they want that memory back right away.

So it doesn't make much sense for the kernel to ignore that and leave the clean file pages to be picked up slowly by kswapd later.

In some aspects, you can think of LMKD as a more specialized, userspace version of kswapd. It has high-level knowledge of process priorities and knows exactly which process is safe to kill to get memory instantly. The kernel's kswapd, however, operates globally without this specific process-level awareness, which makes it less suited for this kind of targeted reclamation.

If we force LMKD to rely on the slower global kswapd to actually free the clean pages, it defeats the whole purpose of targeting a specific process.

Quoted 7 lines
Quoted 2 lines

So letting process_mrelease speed this up isn't a hack at all. It's just helping the kernel do what the admin wanted in the first place: fast, targeted memory.

This is a very creative/disruptive way to do a memory reclaim. From a user POV I would much rather see clean page cache reclaimed before my apps start to disappear. But this is obviously your call and your users that will care.

The problem we see in practice is that kswapd is too slow to react to sudden memory spikes from foreground apps. By the time LMKD decides to kill a background process to make room, the foreground app is already stuck suffering from direct reclaim stalls, which can trigger sluggish/ jank issues which is high priority rather than sustaning background frozen apps user didn't use recently.

Quoted 7 lines

Anyway, I still maintain my position. I do not think it is a good idea to drop clean page cache as you do not know whether there are other users. I am NOT NAKing this patch though but please make sure you have a wider support for this idea before this gets merged. Also make sure that all the above reasoning is part of the changelog if you want to get this merged.

Sure, I will make sure to include all this reasoning in the changelog of the next version.

Thanks for review.

#26 lore raw
In reply to Suren Baghdasaryan
Quoted 50 lines · Suren Baghdasaryan
Quoted 27 lines · David Hildenbrand (Arm)
Quoted 25 lines · Michal Hocko
Quoted 1 line · Minchan Kim

On Fri, Apr 17, 2026 at 09:11:21AM +0200, Michal Hocko wrote:

[...]

Quoted 21 lines
Quoted 3 lines

Yes. All which make sense, really. I am still not convinced about the clean page cache because that just seems like a hack to workaround wrong userspace oom heuristics.

I see it a bit differently. When paltform decides to kill a process to free up memory, they want that memory back right away.

So it doesn't make much sense for the kernel to ignore that and leave the clean file pages to be picked up slowly by kswapd later.

In some aspects, you can think of LMKD as a more specialized, userspace version of kswapd. It has high-level knowledge of process priorities and knows exactly which process is safe to kill to get memory instantly. The kernel's kswapd, however, operates globally without this specific process-level awareness, which makes it less suited for this kind of targeted reclamation.

If we force LMKD to rely on the slower global kswapd to actually free the clean pages, it defeats the whole purpose of targeting a specific process.

So letting process_mrelease speed this up isn't a hack at all. It's just helping the kernel do what the admin wanted in the first place: fast, targeted memory.

Quoted 8 lines
Quoted 8 lines

This is a very creative/disruptive way to do a memory reclaim. From a user POV I would much rather see clean page cache reclaimed before my apps start to disappear. But this is obviously your call and your users that will care.

Anyway, I still maintain my position. I do not think it is a good idea to drop clean page cache as you do not know whether there are other users.

I'm very much familiar with these issues in Android and really want to find a good solution for them. IIUC, this RFC tries to address 2 things at once:
1. handling clean private page cache when reaping memory of a kill victim;
2. addressing a race between kill() and process_release() when process_release() can't happen before the kill() but if it happens too late after the victim passed its exit_mm() then process_release() fails to find the mm to reap. This defeats the purpose of process_release() call because the actual memory (released by exit_mmap()) might not yet be free and a successful process_release() would be very beneficial.

Quoted 2 lines

I see these two as separate issues and I'm not sure combining them into a single discussion is a good idea.

Yeah, they are two different issues so I tried to show those problems in cover-letter and address each issues one by one from each patch.

I can easily drop either of them if it's not received well. I am fine to send them separately, too if that's confused. No problem.

Quoted 15 lines
Quoted 6 lines

IIRC, Johannes raised in the past the we cannot predict the future.

For example, if an app gets OOM-killed, wouldn't we usually try restarting it, re-consuming the clean pagecache pages we would be evicting here?

Sure, we can't predict which app the user will use next, so when killing we usually kill the least recently used one. That's a reasonable strategy in most cases.
In general, if speeding up the victim's reclaim negatively affects the overall user workflow then this would mean we are selecting wrong kill targets. In that case, we would need to adjust the target selection strategy.

Quoted 2 lines

Thanks for tackling this Minchan! I'll try to review the patches this weekend and provide my feedback.

Please go with second patchset.
https://lore.kernel.org/linux-mm/[email protected]/

Thanks!

maintainer
#14 lore raw

Re: [RFC 3/3] mm: process_mrelease: introduce PROCESS_MRELEASE_REAP_KILL flag

In reply to Minchan Kim
Quoted 25 lines · Minchan Kim
Quoted 24 lines · Michal Hocko
Quoted 22 lines · Minchan Kim
Quoted 9 lines · Michal Hocko
Quoted 3 lines · Minchan Kim

If I send the SIGKILL first to satisfy the process_mrelease() requirement, we immediately run into the scheduling race condition where the victim can enter the exit path before the reaper can set the flag.

Why don't you just grab the mm before you send the signal and then continue with reaping? You just want to avoid a race where the victim manages to process fatal signal, start its exit path and mrelease path losing that race so you rely on the exit path, right?

The problem is that process_mrelease() operates on a task obtained from a pidfd.

Once the victim process receives the SIGKILL and enters the exit path (exit_mm), the kernel sets task->mm to NULL.

Even if we could somehow hold a reference to the mm_struct beforehand, process_mrelease() would still fail because mm_struct via task returns NULL after exit_mm() has been called.

Therefore, we cannot simply "grab the mm" before sending the signal and expect process_mrelease() to work after the victim starts exiting.

Quoted 7 lines
Quoted 1 line

I do not follow. Why cannot you simply do this

I misunderstood your point. Do you mean this?

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

There are more details to figure out.

Yes, there are couple of details to iron out. The existing reaping has some hard requirements in place. I am all for relaxing those where possible (ideally without much of special casing for this specific use case) but this is much more viable solution than KILL_MRELEASE you are introducing here. Keep in mind that fundamentally this should be a really as simple as trigger exit_mmap after sending SIGKILL. We are reusing a large part of oom_reaper functionality because it was comfortable but if there are constrains that stand in the way and they make no sense for this usecase then do not sick with them.

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#27 lore raw
In reply to Suren Baghdasaryan
Quoted 50 lines · Suren Baghdasaryan
Quoted 27 lines · David Hildenbrand (Arm)
Quoted 25 lines · Michal Hocko
Quoted 1 line · Minchan Kim

On Fri, Apr 17, 2026 at 09:11:21AM +0200, Michal Hocko wrote:

[...]

Quoted 21 lines
Quoted 3 lines

Yes. All which make sense, really. I am still not convinced about the clean page cache because that just seems like a hack to workaround wrong userspace oom heuristics.

I see it a bit differently. When paltform decides to kill a process to free up memory, they want that memory back right away.

So it doesn't make much sense for the kernel to ignore that and leave the clean file pages to be picked up slowly by kswapd later.

In some aspects, you can think of LMKD as a more specialized, userspace version of kswapd. It has high-level knowledge of process priorities and knows exactly which process is safe to kill to get memory instantly. The kernel's kswapd, however, operates globally without this specific process-level awareness, which makes it less suited for this kind of targeted reclamation.

If we force LMKD to rely on the slower global kswapd to actually free the clean pages, it defeats the whole purpose of targeting a specific process.

So letting process_mrelease speed this up isn't a hack at all. It's just helping the kernel do what the admin wanted in the first place: fast, targeted memory.

Quoted 8 lines
Quoted 8 lines

This is a very creative/disruptive way to do a memory reclaim. From a user POV I would much rather see clean page cache reclaimed before my apps start to disappear. But this is obviously your call and your users that will care.

Anyway, I still maintain my position. I do not think it is a good idea to drop clean page cache as you do not know whether there are other users.

I'm very much familiar with these issues in Android and really want to find a good solution for them. IIUC, this RFC tries to address 2 things at once:
1. handling clean private page cache when reaping memory of a kill victim;
2. addressing a race between kill() and process_release() when process_release() can't happen before the kill() but if it happens too late after the victim passed its exit_mm() then process_release() fails to find the mm to reap. This defeats the purpose of process_release() call because the actual memory (released by exit_mmap()) might not yet be free and a successful process_release() would be very beneficial.

Quoted 2 lines

I see these two as separate issues and I'm not sure combining them into a single discussion is a good idea.

Fully agreed!

Signature · 2 lines
Michal Hocko
SUSE Labs
#28 lore raw
In reply to Suren Baghdasaryan
Quoted 31 lines · Suren Baghdasaryan
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 2 lines · Michal Hocko

[...]

Quoted 8 lines
Quoted 8 lines

This is a very creative/disruptive way to do a memory reclaim. From a user POV I would much rather see clean page cache reclaimed before my apps start to disappear. But this is obviously your call and your users that will care.

Anyway, I still maintain my position. I do not think it is a good idea to drop clean page cache as you do not know whether there are other users.

I'm very much familiar with these issues in Android and really want to find a good solution for them. IIUC, this RFC tries to address 2 things at once:
1. handling clean private page cache when reaping memory of a kill victim;
2. addressing a race between kill() and process_release() when process_release() can't happen before the kill() but if it happens too late after the victim passed its exit_mm() then process_release() fails to find the mm to reap. This defeats the purpose of process_release() call because the actual memory (released by exit_mmap()) might not yet be free and a successful process_release() would be very beneficial.

I see these two as separate issues and I'm not sure combining them into a single discussion is a good idea.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

IIRC, Johannes raised in the past the we cannot predict the future.

For example, if an app gets OOM-killed, wouldn't we usually try restarting it, re-consuming the clean pagecache pages we would be evicting here?

Sure, we can't predict which app the user will use next, so when killing we usually kill the least recently used one. That's a reasonable strategy in most cases.

That makes sense. As long as other apps you open next won't need the same libraries etc that you just evicted.

Signature · 3 lines
Cheers,

David
#29 lore raw
Quoted 38 lines · David Hildenbrand (Arm)
Quoted 36 lines · Suren Baghdasaryan
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 2 lines · Michal Hocko

[...]

Quoted 8 lines
Quoted 8 lines

This is a very creative/disruptive way to do a memory reclaim. From a user POV I would much rather see clean page cache reclaimed before my apps start to disappear. But this is obviously your call and your users that will care.

Anyway, I still maintain my position. I do not think it is a good idea to drop clean page cache as you do not know whether there are other users.

I'm very much familiar with these issues in Android and really want to find a good solution for them. IIUC, this RFC tries to address 2 things at once:
1. handling clean private page cache when reaping memory of a kill victim;
2. addressing a race between kill() and process_release() when process_release() can't happen before the kill() but if it happens too late after the victim passed its exit_mm() then process_release() fails to find the mm to reap. This defeats the purpose of process_release() call because the actual memory (released by exit_mmap()) might not yet be free and a successful process_release() would be very beneficial.

I see these two as separate issues and I'm not sure combining them into a single discussion is a good idea.

Quoted 5 lines

IIRC, Johannes raised in the past the we cannot predict the future.

For example, if an app gets OOM-killed, wouldn't we usually try restarting it, re-consuming the clean pagecache pages we would be evicting here?

Quoted 6 lines
Quoted 3 lines

Sure, we can't predict which app the user will use next, so when killing we usually kill the least recently used one. That's a reasonable strategy in most cases.

That makes sense. As long as other apps you open next won't need the same libraries etc that you just evicted.

True. However shared libraries have a high chance of being used by more that one process when we kill a background apps. So, while it's possible that we might evict a page from a library that will be used shortly after, it's more likely that pages from such libraries would not be private to the victim. Again, all this is probabilistic and no ideal strategy exists without knowing the future. All we can do is optimize for the most likely scenario.

Quoted 5 lines

--
Cheers,

David