linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: zhongjinji <zhongjinji@honor.com>
To: <mhocko@suse.com>
Cc: <rientjes@google.com>, <shakeel.butt@linux.dev>,
	<akpm@linux-foundation.org>, <linux-mm@kvack.org>,
	<linux-kernel@vger.kernel.org>, <tglx@linutronix.de>,
	<liam.howlett@oracle.com>, <lorenzo.stoakes@oracle.com>,
	<liulu.liu@honor.com>, <feng.han@honor.com>,
	<zhongjinji@honor.com>
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	[thread overview]
Message-ID: <20250825133855.30229-3-zhongjinji@honor.com> (raw)
In-Reply-To: <20250825133855.30229-1-zhongjinji@honor.com>

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 <zhongjinji@honor.com>
---
 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 = true;
-	VMA_ITERATOR(vmi, mm, 0);
+	MA_STATE(mas, &mm->mm_mt, ULONG_MAX, 0);
 
 	/*
 	 * 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);
 
-	for_each_vma(vmi, vma) {
+	/*
+	 * When two tasks unmap the same vma at the same time, they may contend for 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 = mas_find_rev(&mas, 0)) != NULL) {
 		if (vma->vm_flags & (VM_HUGETLB|VM_PFNMAP))
 			continue;
 
-- 
2.17.1



  parent reply	other threads:[~2025-08-25 13:39 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-25 13:38 [PATCH v5 0/2] Do not delay oom reaper when the victim is frozen zhongjinji
2025-08-25 13:38 ` [PATCH v5 1/2] mm/oom_kill: " zhongjinji
2025-08-25 19:41   ` Shakeel Butt
2025-08-26 13:01     ` zhongjinji
2025-08-26 12:43   ` Lorenzo Stoakes
2025-08-27 12:08   ` Michal Hocko
2025-08-27 12:14     ` zhongjinji
2025-08-25 13:38 ` zhongjinji [this message]
2025-08-26 12:53   ` [PATCH v5 2/2] mm/oom_kill: Have the OOM reaper and exit_mmap() traverse the maple tree in opposite order Lorenzo Stoakes
2025-08-26 13:37     ` Liam R. Howlett
2025-08-26 13:50       ` Lorenzo Stoakes
2025-08-26 15:21         ` Liam R. Howlett
2025-08-26 22:26           ` Shakeel Butt
2025-08-27  4:12             ` Liam R. Howlett
2025-08-27  4:25               ` Liam R. Howlett
2025-08-27  9:55               ` zhongjinji
2025-08-27 15:57                 ` Suren Baghdasaryan
2025-08-28  0:38                   ` Liam R. Howlett
2025-08-29  7:11               ` Michal Hocko
2025-08-29  7:14     ` Michal Hocko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250825133855.30229-3-zhongjinji@honor.com \
    --to=zhongjinji@honor.com \
    --cc=akpm@linux-foundation.org \
    --cc=feng.han@honor.com \
    --cc=liam.howlett@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=liulu.liu@honor.com \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=mhocko@suse.com \
    --cc=rientjes@google.com \
    --cc=shakeel.butt@linux.dev \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox