From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796319594; b=TbiyV36U5Nq+74YvsNNh8RfLp0+xOgAg8UFpqn734uSpbpp7PA4N7AbTyLoOgrgTrrIE RTBqrWV4fc7ZuKx/dgHJI8H+lcr6Izq21q/5Z/AxyPZZjg7lTudnKoB3juJBsh9IP4SO5 zV1QZ9iQ/19DRb2IKBXfzbKizKc9/gtav1Rd7xCJatHXPdt29QlUaCd90jBHtzEj3mO8b yNmvYB6wl4STiSxuY5eHT1Tl9jsGOhzhI3UHfv3qeD6XHlmRabDBzoL1Yh4Si0cuNg7dN 4sqL07goz46y2IJaSAwBLl9zzDgwa00lYpBZwj7ILZqthAUlDkMfIOHv+y7P2IaU4mw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796319594; h=Received:DKIM-Signature:Received:Date:From:To:Cc:Subject:Message-ID: Reply-To:References:MIME-Version:Content-Type:Content-Disposition: In-Reply-To; bh=OnqpdyhESKIDEev66tOX8dFiZucGe3NhMTlL36dw5ac=; b=L2WYkF3idbNL8M2r+sss/WG9AcNutwyIEv0lZs+LPd3R4JjQaWL59P+0HJzy3ccWXNEO roV/QqR5Uo2tsmQSJ1VWaHJAR0qA2NdaGtFpq2ff+r5XN62dtJz3UBjgGJJNvUx7SlHHk 2oxvQMyaqH7dx+lgcAHznabRsr7O0bK+mkEaJn2KQJa+e++oqyN1OIhC/XGR8WVby7ux0 GPE2FhAhqqDq0+FcEnqPjucoaYx//8BRrbLJYxJr1RHoH9qRlEuBkpIAC5Ji3bAWd7JGZ pbRGQVL+BBbRtkfkipmoimvk35pyPCPywITszPb3om84GN5ffgxCsi7sSCAB/V5bgzg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mTva1xRr; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07A70606A322 for ; Thu, 3 Dec 2026 17:39:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mTva1xRr Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B5B81F000C1; Thu, 3 Dec 2026 17:39:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796319593; bh=OnqpdyhESKIDEev66tOX8dFiZucGe3NhMTlL36dw5ac=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To; b=mTva1xRr4FFUebH22u0Sjo8hpd3Ym5jiah3Vw27JH88xR8i+5jRC+MwzpsAZWpPvc dQ3oM3Z9Bza3wCU6hXvCO9HKdAVsEiTlR2qtRhTMzSUZYTu4NtB0yfL9LNVsdYvPfL INi5pxFrdufnkHvNRoNSUZcJvZyUqqRTzRO9s9yo1xhyi8InJq9q0MvfmgyVA0jHLr WhOBCScEXTCsLSubOOcLRlheEoADaZjZJ7UJXdJtgdAyAChCASV1hziuXmNMssiGrO AXO8qt9VEG1p2p59ivhrcG6XhVHs2jc7Gwik6KpkexkMXZV3ZhSsSG3QPu8isL6POw lFFZ++pCWdqQA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 53963CE06FF; Thu, 3 Dec 2026 09:39:53 -0800 (PST) Date: Thu, 3 Dec 2026 09:39:53 -0800 From: "Paul E. McKenney" To: "Lorenzo Stoakes (ARM)" Cc: Michal Hocko , David Hildenbrand , Andrew Morton , David Rientjes , Ben Gardon , Johannes Weiner , linux-mm@kvack.org, LKML , Michal Hocko , Chris Mason Subject: Re: [PATCH v2] mm, oom: add rcu_barrier before hiding victim from the oom killer Message-ID: Reply-To: paulmck@kernel.org References: <20261202095450.296675-10-mhocko@kernel.org> <20261202212147.379270-1-mhocko@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Thu, Dec 03, 2026 at 12:24:37PM +0000, Lorenzo Stoakes (ARM) wrote: > OK so for this one I think worth bringing in the big guns, i.e. Paul :) > > On Wed, Dec 02, 2026 at 10:21:47PM +0100, Michal Hocko wrote: > > From: Michal Hocko > > > > OOM victim could have released quite a lot of memory through rcu_call > > path (e.g. page tables). Call rcu_barrier before setting MMF_OOM_SKIP > > on the victim to wait for those to execute before the oom killer is > > allowed to pick another victim. > > > > Also move MMF_OOM_SKIP in exit_mmap path after page tables are freed > > to capture as much memory as possible to be freed. This is safe from > > the forward progress POV because oom_reaper will run concurrently in > > case the exit path gets stuck. We are going to see less memory freed > > Ah OK sorry I only just read this, but commented below. > > How certain are we of this though? > > Both calls have no timeout and both take rcu_state.barrier_mutex I think so I > fear that actually we _might_ have a forward-progress issue here? > > Have you been able to somehow pressure test this? I hate to say it but AI is > pretty good at coming up with this kind of thing and iterating on it. > > We should definitely tread carefully here anyway. > > > by the oom reaper because it cannot tear down page tables directly > > but that should be acceptable. > > > > Reported-by: Chris Mason > > Signed-off-by: Michal Hocko > > --- > > mm/mmap.c | 19 ++++++++++--------- > > mm/oom_kill.c | 7 +++++++ > > 2 files changed, 17 insertions(+), 9 deletions(-) > > > > diff --git a/mm/mmap.c b/mm/mmap.c > > index 4cb18b217048..0b19c720473a 100644 > > --- a/mm/mmap.c > > +++ b/mm/mmap.c > > @@ -1314,19 +1314,11 @@ void exit_mmap(struct mm_struct *mm) > > /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */ > > unmap_vmas(&tlb, &unmap); > > > > - /* > > - * Free the memory before marking MMF_OOM_SKIP so a re-triggered OOM > > - * killer will only skip this mm if it's already freed all it can. > > - */ > > + /* Free batched pages during unmap_vmas ASAP for oom victims */ > > if (mm_is_oom_victim(mm)) > > tlb_flush_mmu(&tlb); > > mmap_read_unlock(mm); > > > > - /* > > - * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper > > - * because the memory has been already unmapped. > > - */ > > - mm_flags_set(MMF_OOM_SKIP, mm); > > mmap_write_lock(mm); > > unmap.mm_wr_locked = true; > > mt_clear_in_rcu(&mm->mm_mt); > > @@ -1334,6 +1326,15 @@ void exit_mmap(struct mm_struct *mm) > > free_pgtables(&tlb, &unmap); > > tlb_finish_mmu(&tlb); > > > > + /* > > + * Free the memory before marking MMF_OOM_SKIP so a re-triggered OOM > > + * killer will only skip this mm if it's already freed all it can. > > + */ > > Shouldn't we say this is paired with oom_reap_task(). Is it right to say that > Paul? :) You could say that an rcu_barrier() is paired with every callback function that it waits for, and also with every call_rcu*() invocation that queued one of those callbacks. I hadn't really ever thought of it this way, but it might well be useful when documenting and analyzing the code. > > + if (mm_is_oom_victim(mm)) { > > + rcu_barrier(); > > So this is a fairly heavyweight barrier I think, it waits for _all_ in-flight > call_rcu() callbacks to complete. > > Reading the rcu_barrier() code briefly suggests it does a _lot_ there. Oh yes! ;-) For each and every normal-RCU call_rcu*() callback that has already been queued, it waits for that callback function to complete executing after having been invoked. Among other things, this waiting time might include up to (almost) two normal-RCU grace periods. This waiting time might also include the invocation of millions of RCU callback functions. These are extremes, but they could happen. > And this is a super sensitive point - will this always result in forward > progress? Could it be significantly delayed? Normal grace period latencies tend to be in the tens or hundreds of milliseconds. And RCU CPU stall warnings happen when a given grace period extends for more than 21 *seconds*, so long RCU grace periods can happen. On the other hand, grace periods this long are (and should be) treated as bugs. > Are there any lock dependencies that could be problematic? Given that you are asking this, I am assuming that if this rcu_barrier() gets stuck, reclaim also gets stuck. You are not allowed to acquire sleeplocks in RCU callback functions, and you are not allowed to hold spinlocks across an rcu_barrier(), so at a high level, no. One theoretical problem would be if RCU allocated memory while doing a grace period, and the call to rcu_barrier() blocked the grace period that its callback functions were in turn waiting on. And there might be something like this that could happen when a CPU was coming online for the very first time, especially if that CPU was a rcu_nocbs CPU, which needs kthreads to be created. No locks involved, but self-deadlock nevertheless. So maybe make that rcu_barrier() execute asynchronously? And/or do a synchronize_rcu_expedited(), also asynchronously? > > + mm_flags_set(MMF_OOM_SKIP, mm); > > + } > > + > > /* > > * Walk the list again, actually closing and freeing it, with preemption > > * enabled, without holding any MM locks besides the unreachable > > diff --git a/mm/oom_kill.c b/mm/oom_kill.c > > index 6ff1e93ec6e3..e0c2e3e647aa 100644 > > --- a/mm/oom_kill.c > > +++ b/mm/oom_kill.c > > @@ -628,6 +628,13 @@ static void oom_reap_task(struct task_struct *tsk) > > done: > > tsk->oom_reaper_list = NULL; > > > > + /* > > + * Make sure that pre-exising call_rcu users will get a chance to > > + * free up memory. This might have been exiting task itself as well > > + * so call this before we hide the current victim from the oom killer. > > + */ > > + rcu_barrier(); > > Similar comments here. Paul what do you think? If this can block reclaim, same comments as above. Assisted-by: An allegedly well-trained biological LLM. ;-) Thanx, Paul > > + > > /* > > * Hide this mm from OOM killer because it has been either reaped or > > * somebody can't call mmap_write_unlock(mm). > > -- > > 2.56.1 > > > > -- > Cheers, Lorenzo