[PATCH v2 13/31] mm/khugepaged: stop MADV_COLLAPSE on a fatal signal

From: David Hildenbrand (Arm)

Date: Fri Dec 04 2026 - 07:54:47 EST


A task that gets a fatal signal during MADV_COLLAPSE keeps collapsing
the rest of the range. Consequently, the exit of the task can be delayed
for a while for a large range.

Let's fix it by checking for a fatal signal before each PMD, simply
breaking out of the loop. User space will never see the return value,
so no need to return -EINTR.

This has low urgency, as it only delays the exit of the task. No need to
CC stable.

Fixes: 7d8faaf15545 ("mm/madvise: introduce MADV_COLLAPSE sync hugepage collapse")
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>
Acked-by: Zi Yan <ziy@xxxxxxxxxx>
Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
Reviewed-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
Reviewed-by: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
Reviewed-by: Dev Jain <dev.jain@xxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/khugepaged.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index de00e0d2a6d5f..1c9fb79abd627 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -4,6 +4,7 @@
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/sched/mm.h>
+#include <linux/sched/signal.h>
#include <linux/mmu_notifier.h>
#include <linux/rmap.h>
#include <linux/swap.h>
@@ -3368,6 +3369,9 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
enum scan_result result = SCAN_FAIL;

+ if (fatal_signal_pending(current))
+ break;
+
if (mmap_unlocked) {
cond_resched();
mmap_read_lock(mm);

--
2.56.1