From nobody Fri Oct 3 00:01:47 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 56CC8275B1C; Tue, 9 Sep 2025 09:07:06 +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=1757408828; cv=none; b=l1P3/phnUC9xj3zPeNVrXyapmy6s/jsiAkdxuYC1K+7knh6+w9PrMSdMqTiHOTEl0kKFqB+OMpZtTmQhwuviJETiHGtCDrLxySEYizD8nBMQRYyhdJ4jAdRaE0nX85P4oIEqd37c8GGOmGSYlbImXs7gDhlXMYzNLykjlDILzmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757408828; c=relaxed/simple; bh=7skizSUOygvtS5PJuQyyoPDDGCQB5spdhLE3ItvC2uA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FV47XkeRcmCDirNmJhQadztdS2h8VArLbw716dbsBQzGYA2SCsFdWVbhlj0PyfNU0OKX123wv8x7ByE6SW875Fi0+jhpQtGljm2NTc+Qv0JO3qIKn8VjwM4r1aFl880BbEwy8lGSShwq7QqskX2UjqnrMkcRkH3NV4tDDAj5mEc= 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 4cLdH22Tf2zYl7DQ; Tue, 9 Sep 2025 17:06:38 +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; Tue, 9 Sep 2025 17:07:03 +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; Tue, 9 Sep 2025 17:07:03 +0800 From: zhongjinji To: CC: , , , , , , , , , , , , , , , Subject: [PATCH v8 1/3] mm/oom_kill: Introduce thaw_oom_process() for thawing OOM victims Date: Tue, 9 Sep 2025 17:06:57 +0800 Message-ID: <20250909090659.26400-2-zhongjinji@honor.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250909090659.26400-1-zhongjinji@honor.com> References: <20250909090659.26400-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: w003.hihonor.com (10.68.17.88) To a018.hihonor.com (10.68.17.250) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" OOM killer is a mechanism that selects and kills processes when the system runs out of memory to reclaim resources and keep the system stable. However, the oom victim cannot terminate on its own when it is frozen, because __thaw_task() only thaws one thread of the victim, while the other threads remain in the frozen state. Since __thaw_task did not fully thaw the OOM victim for self-termination, introduce thaw_oom_process() to properly thaw OOM victims. Signed-off-by: zhongjinji Acked-by: Michal Hocko --- include/linux/freezer.h | 2 ++ kernel/freezer.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/linux/freezer.h b/include/linux/freezer.h index b303472255be..19a4b57950cd 100644 --- a/include/linux/freezer.h +++ b/include/linux/freezer.h @@ -47,6 +47,7 @@ extern int freeze_processes(void); extern int freeze_kernel_threads(void); extern void thaw_processes(void); extern void thaw_kernel_threads(void); +extern void thaw_oom_process(struct task_struct *p); =20 static inline bool try_to_freeze(void) { @@ -80,6 +81,7 @@ static inline int freeze_processes(void) { return -ENOSYS= ; } static inline int freeze_kernel_threads(void) { return -ENOSYS; } static inline void thaw_processes(void) {} static inline void thaw_kernel_threads(void) {} +static inline void thaw_oom_process(struct task_struct *p) {} =20 static inline bool try_to_freeze(void) { return false; } =20 diff --git a/kernel/freezer.c b/kernel/freezer.c index 6a96149aede9..17970e0be8a7 100644 --- a/kernel/freezer.c +++ b/kernel/freezer.c @@ -206,6 +206,25 @@ void __thaw_task(struct task_struct *p) wake_up_state(p, TASK_FROZEN); } =20 +/* + * thaw_oom_process - thaw the OOM victim process + * @p: process to be thawed + * + * Sets TIF_MEMDIE for all threads in the process group and thaws them. + * Threads with TIF_MEMDIE are ignored by the freezer. + */ +void thaw_oom_process(struct task_struct *p) +{ + struct task_struct *t; + + rcu_read_lock(); + for_each_thread(p, t) { + set_tsk_thread_flag(t, TIF_MEMDIE); + __thaw_task(t); + } + rcu_read_unlock(); +} + /** * set_freezable - make %current freezable * --=20 2.17.1 From nobody Fri Oct 3 00:01:47 2025 Received: from mta21.hihonor.com (mta21.honor.com [81.70.160.142]) (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 AC42B27C842; Tue, 9 Sep 2025 09:07:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.160.142 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757408833; cv=none; b=gXvdO8/q8L7+j/GwyP+gODzJ8ft2QDieV2d92OQ3Tt0sHM2/xikRhoSZtxrClC3mXD2hfVzbj1KRiMVBPaI06OvVrDT/OAnbKhG7KqAQ9zStt2Yfz/1bveogSAiMUkmZ7tXCcPhtdPg1Jb04a1hi8o0Lulc87Zhdf0V7iA/V0vk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757408833; c=relaxed/simple; bh=JMYFiA/QfCfFUsD7yvBLT33vWqnUtTC2cKiz085S7WE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=g8IkGQEjy/RxRs3HgaNZ9/IWWYWDRxGpnbU6S13vl6sBYymCPVsAqs7NUkJGFW4OH9rJtxfVD72rwFS9Joti9A0TGZXvim+PruspjPiIOLfpTTnPsZSlaAbhy4NEPlzE2cENquB7sUOVQB/I8TQCIBDDUemz60Pf1ko0Sa9fEvU= 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.160.142 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 mta21.hihonor.com (SkyGuard) with ESMTPS id 4cLdH13hltzYl69p; Tue, 9 Sep 2025 17:06:37 +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; Tue, 9 Sep 2025 17:07:03 +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; Tue, 9 Sep 2025 17:07:03 +0800 From: zhongjinji To: CC: , , , , , , , , , , , , , , , Subject: [PATCH v8 2/3] mm/oom_kill: Thaw the entire OOM victim process Date: Tue, 9 Sep 2025 17:06:58 +0800 Message-ID: <20250909090659.26400-3-zhongjinji@honor.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250909090659.26400-1-zhongjinji@honor.com> References: <20250909090659.26400-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: w003.hihonor.com (10.68.17.88) To a018.hihonor.com (10.68.17.250) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" OOM killer is a mechanism that selects and kills processes when the system runs out of memory to reclaim resources and keep the system stable. However, the oom victim cannot terminate on its own when it is frozen, because __thaw_task() only thaws one thread of the victim, while the other threads remain in the frozen state. This change will thaw the entire victim process when OOM occurs, ensuring that the oom victim can terminate on its own. Signed-off-by: zhongjinji Reviewed-by: Suren Baghdasaryan --- mm/oom_kill.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 25923cfec9c6..ffa50a1f0132 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -772,12 +772,11 @@ static void mark_oom_victim(struct task_struct *tsk) mmgrab(tsk->signal->oom_mm); =20 /* - * Make sure that the task is woken up from uninterruptible sleep + * Make sure that the process is woken up from uninterruptible sleep * if it is frozen because OOM killer wouldn't be able to free - * any memory and livelock. freezing_slow_path will tell the freezer - * that TIF_MEMDIE tasks should be ignored. + * any memory and livelock. */ - __thaw_task(tsk); + thaw_oom_process(tsk); atomic_inc(&oom_victims); cred =3D get_task_cred(tsk); trace_mark_victim(tsk, cred->uid.val); --=20 2.17.1 From nobody Fri Oct 3 00:01:47 2025 Received: from mta22.hihonor.com (mta22.honor.com [81.70.192.198]) (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 C5E9D27FB21; Tue, 9 Sep 2025 09:07:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.192.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757408835; cv=none; b=FDATU9srEpeMKbGaw7yyugDZ7n+0gN7zmjFrPn7KkJdYu+iks0j0Sn/CcbrRRGxZ0OuYxJu+H6Ie/Lf3J1H7eKDmEkSF+L4qebihIaseb3/pzXLn6FpS9Le4GnXwd+EswZxFNcMoVYg8JKSzZ9ZKLMtkW58s4GN1D+xssRWaWks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757408835; c=relaxed/simple; bh=ZfbceCL/kPcMjFmDMsLXquAjuUpKU4CTRa0m/UcIQQg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ETGWF/aknziVd8ZobBVVuhoUXzbpyeBgAVVtH5KjAcb1XgmPahJHc7iuCvyH14lgYOT3cVVN8j7PJk+o3FtO4hugjUQldwR2yjTV6hPHLF+cwOchIxqliRadjgP7Vkld0u62Qw4M0XsIuAN2RglTjP0cBLHtQM2NwRGYTUsu/Pg= 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; dkim=pass (1024-bit key) header.d=honor.com header.i=@honor.com header.b=RVzD15/d; arc=none smtp.client-ip=81.70.192.198 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=honor.com header.i=@honor.com header.b="RVzD15/d" dkim-signature: v=1; a=rsa-sha256; d=honor.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=To:From; bh=QFwPNIhzx5/vlFX4tifco6Ucy1u1xTtsZP1BKdZxa8A=; b=RVzD15/dqePYVNtMOHbfcWz9+AGkmjUfqnuo2/JMGvOoXcYqCMiMgofugKojvVQAc9YvHcyv8 3j2xI5Ud1d3KkZJPS7yQsWX/oiX5UHgNCYkf8BlBGeahAmQ38SsjiuN3nJeDN5L7ACWEwoRs2Kb NxsDLg4a1BNwDvblvdKpy/Y= Received: from w012.hihonor.com (unknown [10.68.27.189]) by mta22.hihonor.com (SkyGuard) with ESMTPS id 4cLdHC1hpWzYlsST; Tue, 9 Sep 2025 17:06:47 +0800 (CST) Received: from a018.hihonor.com (10.68.17.250) by w012.hihonor.com (10.68.27.189) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 9 Sep 2025 17:07:04 +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; Tue, 9 Sep 2025 17:07:03 +0800 From: zhongjinji To: CC: , , , , , , , , , , , , , , , Subject: [PATCH v8 3/3] mm/oom_kill: The OOM reaper traverses the VMA maple tree in reverse order Date: Tue, 9 Sep 2025 17:06:59 +0800 Message-ID: <20250909090659.26400-4-zhongjinji@honor.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250909090659.26400-1-zhongjinji@honor.com> References: <20250909090659.26400-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: w003.hihonor.com (10.68.17.88) To a018.hihonor.com (10.68.17.250) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Although the oom_reaper is delayed and it gives the oom victim chance to clean up its address space this might take a while especially for processes with a large address space footprint. In those cases oom_reaper might start racing with the dying task and compete for shared resources - e.g. page table lock contention has been observed. Reduce those races by reaping the oom victim from the other end of the address space. It is also a significant improvement for process_mrelease(). When a process is killed, process_mrelease is used to reap the killed process and often runs concurrently with the dying task. The test data shows that after applying the patch, lock contention is greatly reduced during the procedure of reaping the killed process. The test is based on arm64. Without the patch: |--99.57%-- oom_reaper | |--0.28%-- [hit in function] | |--73.58%-- unmap_page_range | | |--8.67%-- [hit in function] | | |--41.59%-- __pte_offset_map_lock | | |--29.47%-- folio_remove_rmap_ptes | | |--16.11%-- tlb_flush_mmu | | |--1.66%-- folio_mark_accessed | | |--0.74%-- free_swap_and_cache_nr | | |--0.69%-- __tlb_remove_folio_pages | |--19.94%-- tlb_finish_mmu | |--3.21%-- folio_remove_rmap_ptes | |--1.16%-- __tlb_remove_folio_pages | |--1.16%-- folio_mark_accessed | |--0.36%-- __pte_offset_map_lock With the patch: |--99.53%-- oom_reaper | |--55.77%-- unmap_page_range | | |--20.49%-- [hit in function] | | |--58.30%-- folio_remove_rmap_ptes | | |--11.48%-- tlb_flush_mmu | | |--3.33%-- folio_mark_accessed | | |--2.65%-- __tlb_remove_folio_pages | | |--1.37%-- _raw_spin_lock | | |--0.68%-- __mod_lruvec_page_state | | |--0.51%-- __pte_offset_map_lock | |--32.21%-- tlb_finish_mmu | |--6.93%-- folio_remove_rmap_ptes | |--1.90%-- __tlb_remove_folio_pages | |--1.55%-- folio_mark_accessed | |--0.69%-- __pte_offset_map_lock Signed-off-by: zhongjinji Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes Acked-by: Shakeel Butt Acked-by: Michal Hocko Reviewed-by: Suren Baghdasaryan Reviewed-by: Suren Baghdsaryan --- mm/oom_kill.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index ffa50a1f0132..52d285da5ba4 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -516,7 +516,7 @@ static bool __oom_reap_task_mm(struct mm_struct *mm) { struct vm_area_struct *vma; bool ret =3D true; - VMA_ITERATOR(vmi, mm, 0); + MA_STATE(mas, &mm->mm_mt, ULONG_MAX, ULONG_MAX); =20 /* * Tell all users of get_user/copy_from_user etc... that the content @@ -526,7 +526,13 @@ static bool __oom_reap_task_mm(struct mm_struct *mm) */ set_bit(MMF_UNSTABLE, &mm->flags); =20 - for_each_vma(vmi, vma) { + /* + * It might start racing with the dying task and compete for shared + * resources - e.g. page table lock contention has been observed. + * Reduce those races by reaping the oom victim from the other end + * of the address space. + */ + mas_for_each_rev(&mas, vma, 0) { if (vma->vm_flags & (VM_HUGETLB|VM_PFNMAP)) continue; =20 --=20 2.17.1