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=1796064779; b=v6TTur5OYvUW8ytP/FlIGUvces/HkLvxYBPSVezZziqGl0LWnHcTEnTxgRLdjxbX2Z6y a0OyzYvxzy1eoDPhyOXrH4Lubaz4ZY4orwkzrOgWwbA1onyLXPcJ5XeRCi70+QL+mUU01 kFY1ka0a3j8dhFkaHt2vmxb50UZCZDdxdcMlnO4hKG38SBBn0lLmfxgErIa7pWTnIZ+HX stEorPodEbWxm/JD/oL2gl2Kud+bUZxi08sqCsJa/nuV+XeLOuO37U2YU7CM1NdCaE7+W DJltccZUGApLYJZW94Q+wYuxVpPh+ed7bXU18ndXxpwARms3yFi2dCdNUtCBdzI18Vw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796064779; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=8IjyNfsl6UPtZ3gHyHpMOImZ/Gi+un+v7NsaCMgGs88=; b=gVTzFbhHYynGaWn/f/mM1VpKxzHNvn98fZA0aOt0tyvxasDhfY6+xzoY9BrYDE/j3vVK bGuj6IY81pFxK51tl12d1uIsoTYJDQAiBwo1P28K0aV/p6+30sJ1gfOo1cvNgB+LcuLlT aNzFHHV7KoEvy7GxlYe8bGjl1KgdoU8RK42j53AF1kHti2iG9be1FFBWF9SGwqiAAx3uf t7qUWFTH0qliGP8sDTepzEyUcIIo4RHc36zSIXcaRejyZrAAZ5unV0JX7JzhvFf1U0g3t LpIkKvpL94vwRG0aHrqsawCAiZMxT1AsI1dkVcTwM7domf1c4oJofGOKokXGd/o3aKw== 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=YnX+1GmB; 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 CC22B606A31C; Mon, 30 Nov 2026 18:52:59 +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=YnX+1GmB Received: by smtp.kernel.org (Postfix) with ESMTPSA id E04DA1F000C1; Mon, 30 Nov 2026 18:52:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796064779; bh=8IjyNfsl6UPtZ3gHyHpMOImZ/Gi+un+v7NsaCMgGs88=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=YnX+1GmBg9Xv3PUUCIw7U+bb7mjtTggoHB2GJfwviAwBnfP+XKtr+5VnPj+UI8riA 1LdMexbiFhcBdtYw3+31jQQO38cZ4+ps8CgjgZb6J9jkcDsRcedjMk1+mKpHF/GVtT qfbv6ReeSLropQ/fiOlpSRUMUdj4Our/7j+LhS+wF2PTxjLF9O8dmysy5veJJ9LEyx dgm9YsXydajuzlUMrlhMdqNwx2ZxrrEAhJSV3XJZFTr2q97AXqV8A9J50reTg14uBp Nr80Zwj9N+9JsxuTbTQdf4NoUTgYZ9D7HoZbucauZL1mizqCLhXUI+LmmsEuPKNLqP +krin10dFq4jg== Date: Mon, 30 Nov 2026 18:52:50 +0000 From: "Lorenzo Stoakes (ARM)" To: "Jose A. Perez de Azpillaga" Cc: David Hildenbrand , Zi Yan , Andrew Morton , Zach O'Keefe , Hugh Dickins , Kiryl Shutsemau , Baolin Wang , Dev Jain , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Barry Song , Lance Yang , Usama Arif , Vlastimil Babka , Mike Rapoport , Michal Hocko , linux-mm@kvack.org, Matthew Wilcox , Jan Kara , Johannes Weiner , Rik van Riel , Song Liu , linux-fsdevel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked Message-ID: References: <20261127091344.113688-1-azpijr@gmail.com> <20261127091344.113688-4-azpijr@gmail.com> X-Mailing-List: stable@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: <20261127091344.113688-4-azpijr@gmail.com> On Fri, Nov 27, 2026 at 10:12:16AM +0100, Jose A. Perez de Azpillaga wrote: > __do_fault() preallocates a page table only when the PMD is empty, so > that finish_fault() does not have to allocate one with the folio locked. This is written in a sort of way, so as to automatically confused, whoever reads it, with its comma. How about: __do_fault() tries to avoid page table allocation in finish_fault() by pre-allocating one if the PMD is empty. However, it is no longer safe to assume that a page table found here (thus avoiding pre-allocation) remains in place, since < stuff about locks below > ? > Since retract_page_tables() no longer takes the mmap and VMA locks, the > page table can be removed while the fault waits in ->fault(). > finish_fault() then finds an empty PMD without a preallocated table, and > calls pte_alloc() with the folio locked. > > Allocating with __GFP_FS while holding a folio lock can deadlock in > reclaim, for example the memcg reclaim deadlock on cgroup v1 described > in commit 63f3655f9501 ("mm, memcg: fix reclaim deadlock with > writeback"). Do we have to have this example in every commit message? :)) I mean I guess fine. I just find cgroup v1 very deprecate-y. > > If the folio can't be mapped with a PMD, return VM_FAULT_NOPAGE instead, > as we already do when pte_offset_map_lock() fails. The retried fault > preallocates the page table before locking the folio. Is that safe from this race, or does it just mean you could go on a VM_FAULT_NOPAGE loop again? Maybe worth mentioning? It feels like the pre-faulting mechanism is now a sort of best-effort thing since it can't now correctly detect if it's necessary or not. Maybe worth a comment at the point of pre-allocation saying that? > > Fixes: 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock") > Cc: stable@vger.kernel.org > Signed-off-by: Jose A. Perez de Azpillaga > --- > Changes in v2: > - return VM_FAULT_NOPAGE after trying do_set_pmd() (David) > - drop the ppc64 sentence, do_set_pmd() is handled on its own now > > v1: https://lore.kernel.org/20261125100058.15200-3-azpijr@gmail.com > > mm/memory.c | 12 ++++++++---- > 1 file changed, 8 insertions(+), 4 deletions(-) > > diff --git a/mm/memory.c b/mm/memory.c > index 05d03b32ce52..dd0cb09ba7ef 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -6000,10 +6000,14 @@ vm_fault_t finish_fault(struct vm_fault *vmf) > return ret; > } > > - if (vmf->prealloc_pte) > - pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); > - else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) > - return VM_FAULT_OOM; > + /* > + * The page table was removed after __do_fault() found it, so > + * nothing was preallocated. Allocating with the folio locked > + * can deadlock in reclaim, so retry the fault instead. > + */ "Dearest Diary, Today I deadlocked on an allocation and it was the most wondrous deadlock evah!11" Let's maybe deschlop here, e.g.: /* * Page table retraction can happen after we decided there was * no need to prealloc and the folio is locked, so to avoid * deadlock against concurrent reclaim, just retry the fault. */ OK not fewer words but I think clearer :) > + if (!vmf->prealloc_pte) > + return VM_FAULT_NOPAGE; > + pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); > } > > nr_pages = folio_nr_pages(folio); > -- > 2.56.1 > -- Cheers, Lorenzo