From nobody Fri Oct 3 20:28:25 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 83A33265CB3 for ; Mon, 25 Aug 2025 13:39: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=1756129153; cv=none; b=hSxcaLWX1UP2g8Wwv0zuyoEOM12SEvvkoJG2obopV7vG3ARIVLSHSDylZMJjDKlNmA5UmlRbCJceSTKn6Mxj2DCKf0FbUlQ4OPpqSOzpYM+um3+aiofYdkkX8jKuLRS1PLS83lK2DK3hGUnsczv17jo7CCkvhmajl0/m5iwmeu8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756129153; c=relaxed/simple; bh=1p2jpTVGNFS2EQACkqUgkD3BGqkqBt+R/ZdIMHBLkjk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=utKeajHNnBSABYkqhCucyfR3uwZspFAeR8eY5eimMsodTMk56CkEUDRNPOmRrSpIrKmI8MOAGd8snI9ePEBS4U4GjdX3myUVeBhCQpU17ka29O3T5LAoCVXhCSLuute9/akBbCnsyb9/U5CdPfygWFX9DhN0f+Go0m/FmlAAj6s= 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 w002.hihonor.com (unknown [10.68.28.120]) by mta21.hihonor.com (SkyGuard) with ESMTPS id 4c9X1w3NcXzYkyN7; Mon, 25 Aug 2025 21:38:44 +0800 (CST) Received: from a018.hihonor.com (10.68.17.250) by w002.hihonor.com (10.68.28.120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 25 Aug 2025 21:39:00 +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; Mon, 25 Aug 2025 21:38:59 +0800 From: zhongjinji To: CC: , , , , , , , , , , Subject: [PATCH v5 1/2] mm/oom_kill: Do not delay oom reaper when the victim is frozen Date: Mon, 25 Aug 2025 21:38:54 +0800 Message-ID: <20250825133855.30229-2-zhongjinji@honor.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250825133855.30229-1-zhongjinji@honor.com> References: <20250825133855.30229-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: w002.hihonor.com (10.68.28.120) To a018.hihonor.com (10.68.17.250) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The OOM reaper can quickly reap a process's memory when the system encounters OOM, helping the system recover. If the victim process is frozen and cannot be unfrozen in time, the reaper delayed by two seconds will cause the system to fail to recover quickly from the OOM state. When an OOM occurs, if the victim is not unfrozen, delaying the OOM reaper will keep the system in a bad state for two seconds. Before scheduling the oom_reaper task, check whether the victim is in a frozen state. If the victim is frozen, do not delay the OOM reaper. Signed-off-by: zhongjinji --- mm/oom_kill.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 25923cfec9c6..4b4d73b1e00d 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -683,6 +683,41 @@ static void wake_oom_reaper(struct timer_list *timer) wake_up(&oom_reaper_wait); } =20 +/* + * When the victim is frozen, the OOM reaper should not be delayed, because + * if the victim cannot be unfrozen promptly, it may block the system from + * quickly recovering from the OOM state. + */ +static bool should_delay_oom_reap(struct task_struct *tsk) +{ + struct mm_struct *mm =3D tsk->mm; + struct task_struct *p; + bool ret; + + if (!mm) + return true; + + if (!frozen(tsk)) + return true; + + if (atomic_read(&mm->mm_users) <=3D 1) + return false; + + rcu_read_lock(); + for_each_process(p) { + if (!process_shares_mm(p, mm)) + continue; + if (same_thread_group(tsk, p)) + continue; + ret =3D !frozen(p); + if (ret) + break; + } + rcu_read_unlock(); + + return ret; +} + /* * Give the OOM victim time to exit naturally before invoking the oom_reap= ing. * The timers timeout is arbitrary... the longer it is, the longer the wor= st @@ -694,13 +729,16 @@ static void wake_oom_reaper(struct timer_list *timer) #define OOM_REAPER_DELAY (2*HZ) 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)) return; =20 get_task_struct(tsk); + delay =3D should_delay_oom_reap(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 --=20 2.17.1 From nobody Fri Oct 3 20:28:25 2025 Received: from mta22.hihonor.com (mta22.hihonor.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 16A2A21257E for ; Mon, 25 Aug 2025 13:39:08 +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=1756129151; cv=none; b=B0wB+BN48jxxdAQaZdyTBElCeFaN6WlGtEXkn2F9m7YjBvMoE0GKRehRia2SDd6vgRWdfPXoDxPEi3Zi4/zbiH9yRGaW8xOwv4kXuL2ngUbr/D8S+y/RfwlBYdxrnjF/jbgAujlHIDwvg+WJ7ThAswZB6lmEm+kmcOpyOsfy/Pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756129151; c=relaxed/simple; bh=qVrwTRMzbhTLZFTNdYyUte8sJMxlT3Z3KdDa9Fqp9oM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ME7pKxIrYTf2PS1aDWl6Tt7u/Mbfv8OIOab0FAHYUryrt7q/uJUsFC/BgBhK/xZK62VKYMmES1ONu3KBWP6QIMvS7eIklsjV5s3MJt9hgyZz2DmjiEqQ/FFujxCgZbR1mLpa103oNal0ll5QMrCmSahfH7vskwo8s5+y+6BJnQc= 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=VlsDG2Id; 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="VlsDG2Id" dkim-signature: v=1; a=rsa-sha256; d=honor.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=To:From; bh=e01QAR5JDTSQnGFTtnf/BBtTF0FubM4Lr4gx30e0yhI=; b=VlsDG2IdtAp/2hZRJLYa/Soo4y4Mt+Q+DsvVy3NkT4QDTVovF8ZwtjyYebt3Ukp/hh6zQTr7V rp/sWixZspUkFeZa96Q+7HKRIcPxBJWExNPi4yQFnHUonoxJcnf1BjKlzDBSRIEzWz2n9xbWCpJ PPCTuy3Z1XiVPexTiHP2WKk= Received: from w002.hihonor.com (unknown [10.68.28.120]) by mta22.hihonor.com (SkyGuard) with ESMTPS id 4c9X213dMPzYkxf9; Mon, 25 Aug 2025 21:38:49 +0800 (CST) Received: from a018.hihonor.com (10.68.17.250) by w002.hihonor.com (10.68.28.120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 25 Aug 2025 21:39:00 +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; Mon, 25 Aug 2025 21:39:00 +0800 From: zhongjinji To: CC: , , , , , , , , , , Subject: [PATCH v5 2/2] mm/oom_kill: Have the OOM reaper and exit_mmap() traverse the maple tree in opposite order Date: Mon, 25 Aug 2025 21:38:55 +0800 Message-ID: <20250825133855.30229-3-zhongjinji@honor.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250825133855.30229-1-zhongjinji@honor.com> References: <20250825133855.30229-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: w002.hihonor.com (10.68.28.120) To a018.hihonor.com (10.68.17.250) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a process is OOM killed without reaper delay, the oom reaper and the exit_mmap() thread likely run simultaneously. They traverse the vma's maple tree along the same path and may easily unmap the same vma, causing them to compete for the pte spinlock. When a process exits, exit_mmap() traverses the vma's maple tree from low to high addresses. To reduce the chance of unmapping the same vma simultaneously, the OOM reaper should traverse the vma's tree from high to low address. Signed-off-by: zhongjinji --- mm/oom_kill.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 4b4d73b1e00d..a0650da9ec9c 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, 0); =20 /* * Tell all users of get_user/copy_from_user etc... that the content @@ -526,7 +526,12 @@ static bool __oom_reap_task_mm(struct mm_struct *mm) */ set_bit(MMF_UNSTABLE, &mm->flags); =20 - for_each_vma(vmi, vma) { + /* + * When two tasks unmap the same vma at the same time, they may contend f= or the + * pte spinlock. To reduce the probability of them unmapping the same vma= , the + * oom reaper traverse the vma maple tree in reverse order. + */ + while ((vma =3D mas_find_rev(&mas, 0)) !=3D NULL) { if (vma->vm_flags & (VM_HUGETLB|VM_PFNMAP)) continue; =20 --=20 2.17.1