From nobody Sun Oct 5 14:35:18 2025 Received: from mta20.hihonor.com (mta20.hihonor.com [81.70.206.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF87B26CE06 for ; Fri, 1 Aug 2025 15:37:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.206.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754062626; cv=none; b=tGAdMUWCA9qUn+uuHvNt/NUloYoe/FWxjfYu4K46g+bPv3hVx1HB82T82dUNFJXD95/cYM1s6splU8suPSIeBwUcYvlabAXdoXEaLSonrHQlN2jUiwO/0n5lwegRJe/tRVprGUq/kGIXX7Jrbbt4E4XhJdX2L7QXZOwu3sxtm2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754062626; c=relaxed/simple; bh=6uYcKYYXmIgA5WR9TND4uBOl3tk1Lbk52OhKyLXMXfs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kwN2797ndkpCAoEAKWvaSuaK6QeoJYILyp+NSwBrY8ZW4YwUfnXxc6EBlwnwwA7FwBIC/2xERTUcLCbZukK6O6pfic/L8ZZh6qdvg5V2JDux6GEOSP8Ns9llsPQrni3+6Ok7qfbUkpGwB1LAwQZ2xIVf6p9sx2KhJA5knn5mO0M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com; spf=pass smtp.mailfrom=honor.com; arc=none smtp.client-ip=81.70.206.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=honor.com Received: from w001.hihonor.com (unknown [10.68.25.235]) by mta20.hihonor.com (SkyGuard) with ESMTPS id 4btqjv6SPqzYmG8V; Fri, 1 Aug 2025 23:33:55 +0800 (CST) Received: from a018.hihonor.com (10.68.17.250) by w001.hihonor.com (10.68.25.235) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 1 Aug 2025 23:36:54 +0800 Received: from localhost.localdomain (10.144.20.219) by a018.hihonor.com (10.68.17.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 1 Aug 2025 23:36:53 +0800 From: To: CC: , , , , , , , , , , , , , Subject: [[PATCH v2] 2/2] futex: Only delay OOM reaper for processes using robust futex Date: Fri, 1 Aug 2025 23:36:49 +0800 Message-ID: <20250801153649.23244-2-zhongjinji@honor.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250801153649.23244-1-zhongjinji@honor.com> References: <20250801153649.23244-1-zhongjinji@honor.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: w010.hihonor.com (10.68.28.113) To a018.hihonor.com (10.68.17.250) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: zhongjinji After merging the patch https://lore.kernel.org/all/20220414144042.677008-1-npache@redhat.com/T/#u, the OOM reaper runs less frequently because many processes exit within 2 se= conds. However, when a process is killed, timely handling by the OOM reaper allows its memory to be freed faster. Since relatively few processes use robust futex, delaying the OOM reaper for all processes is undesirable, as many killed processes cannot release memory more quickly. This patch modifies the behavior so that only processes using robust futex are delayed by the OOM reaper, allowing the OOM reaper to handle more processes in a timely manner. Signed-off-by: zhongjinji --- mm/oom_kill.c | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 25923cfec9c6..3ecb21a1c870 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -692,7 +693,7 @@ static void wake_oom_reaper(struct timer_list *timer) * before the exit path is able to wake the futex waiters. */ #define OOM_REAPER_DELAY (2*HZ) -static void queue_oom_reaper(struct task_struct *tsk) +static void queue_oom_reaper(struct task_struct *tsk, bool delay) { /* mm is already queued? */ if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags)) @@ -700,7 +701,7 @@ static void queue_oom_reaper(struct task_struct *tsk) =20 get_task_struct(tsk); timer_setup(&tsk->oom_reaper_timer, wake_oom_reaper, 0); - tsk->oom_reaper_timer.expires =3D jiffies + OOM_REAPER_DELAY; + tsk->oom_reaper_timer.expires =3D jiffies + (delay ? OOM_REAPER_DELAY : 0= ); add_timer(&tsk->oom_reaper_timer); } =20 @@ -742,7 +743,7 @@ static int __init oom_init(void) } subsys_initcall(oom_init) #else -static inline void queue_oom_reaper(struct task_struct *tsk) +static inline void queue_oom_reaper(struct task_struct *tsk, bool delay) { } #endif /* CONFIG_MMU */ @@ -871,11 +872,12 @@ static inline bool __task_will_free_mem(struct task_s= truct *task) * Caller has to make sure that task->mm is stable (hold task_lock or * it operates on the current). */ -static bool task_will_free_mem(struct task_struct *task) +static bool task_will_free_mem(struct task_struct *task, bool *delay_reap) { struct mm_struct *mm =3D task->mm; struct task_struct *p; bool ret =3D true; + bool has_robust =3D !delay_reap; =20 /* * Skip tasks without mm because it might have passed its exit_mm and @@ -888,6 +890,15 @@ static bool task_will_free_mem(struct task_struct *tas= k) if (!__task_will_free_mem(task)) return false; =20 + /* + * Check if a process is using robust futexes. If so, delay its handling = by the + * OOM reaper. The reason is that if the owner of a robust futex lock is = killed + * while waiters are still alive, the OOM reaper might free the robust fu= tex + * resources before futex_cleanup runs, causing the waiters to wait indef= initely. + */ + if (!has_robust) + has_robust =3D check_robust_futex(task); + /* * This task has already been drained by the oom reaper so there are * only small chances it will free some more @@ -912,8 +923,12 @@ static bool task_will_free_mem(struct task_struct *tas= k) ret =3D __task_will_free_mem(p); if (!ret) break; + if (!has_robust) + has_robust =3D __check_robust_futex(p); } rcu_read_unlock(); + if (delay_reap) + *delay_reap =3D has_robust; =20 return ret; } @@ -923,6 +938,7 @@ static void __oom_kill_process(struct task_struct *vict= im, const char *message) struct task_struct *p; struct mm_struct *mm; bool can_oom_reap =3D true; + bool delay_reap; =20 p =3D find_lock_task_mm(victim); if (!p) { @@ -950,6 +966,7 @@ static void __oom_kill_process(struct task_struct *vict= im, const char *message) * reserves from the user space under its control. */ do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID); + delay_reap =3D check_robust_futex(victim); mark_oom_victim(victim); pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-r= ss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%hd\n", message, task_pid_nr(victim), victim->comm, K(mm->total_vm), @@ -990,11 +1007,13 @@ static void __oom_kill_process(struct task_struct *v= ictim, const char *message) if (unlikely(p->flags & PF_KTHREAD)) continue; do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID); + if (!delay_reap) + delay_reap =3D __check_robust_futex(p); } rcu_read_unlock(); =20 if (can_oom_reap) - queue_oom_reaper(victim); + queue_oom_reaper(victim, delay_reap); =20 mmdrop(mm); put_task_struct(victim); @@ -1020,6 +1039,7 @@ static void oom_kill_process(struct oom_control *oc, = const char *message) struct mem_cgroup *oom_group; static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST); + bool delay_reap =3D false; =20 /* * If the task is already exiting, don't alarm the sysadmin or kill @@ -1027,9 +1047,9 @@ static void oom_kill_process(struct oom_control *oc, = const char *message) * so it can die quickly */ task_lock(victim); - if (task_will_free_mem(victim)) { + if (task_will_free_mem(victim, &delay_reap)) { mark_oom_victim(victim); - queue_oom_reaper(victim); + queue_oom_reaper(victim, delay_reap); task_unlock(victim); put_task_struct(victim); return; @@ -1112,6 +1132,7 @@ EXPORT_SYMBOL_GPL(unregister_oom_notifier); bool out_of_memory(struct oom_control *oc) { unsigned long freed =3D 0; + bool delay_reap =3D false; =20 if (oom_killer_disabled) return false; @@ -1128,9 +1149,9 @@ bool out_of_memory(struct oom_control *oc) * select it. The goal is to allow it to allocate so that it may * quickly exit and free its memory. */ - if (task_will_free_mem(current)) { + if (task_will_free_mem(current, &delay_reap)) { mark_oom_victim(current); - queue_oom_reaper(current); + queue_oom_reaper(current, delay_reap); return true; } =20 @@ -1231,7 +1252,7 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigne= d int, flags) mm =3D p->mm; mmgrab(mm); =20 - if (task_will_free_mem(p)) + if (task_will_free_mem(p, NULL)) reap =3D true; else { /* Error only if the work has not been done already */ --=20 2.17.1