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=1796126216; b=qZQeFJ1fTMaGz3VJHI9e4o698r7JAZ92Yc3U7CFQXtseYlbWyJwmmewwnfPIn/D/MVxm tZgDrzFszRIwsxgqg8uGf+ElfKbJ1kVatZwn/cFYWHYZTOmQY2qxsjO8UeW4E8lHHnVSB dnpNb93ZKnC92KV+sHLnn0tIrmNDZ93fKbYguuh3hMe1OJw4is3ZVP84GQJInfEmyWfFS HSc6X8iBF87whzHhbqzdh6QlM/BqD6t6pE0z50QRrKnv1e2w8VdR+fdwsXoxQsPcwIlls r4hBrAQQSTxdFHEjp6Si9GQvXMa6sJv4Ls5/206GSgc6O6UZOPvAvuFSApq5dbrnFOg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796126216; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=gkE5anAOhVoytmIz4xBJxQrnBnRTl2TkuPOBS728X/E=; b=C4J9eytzc23ow92MXHzuKkOozbiawk2r/WBwqSp0eyP8xx4sNSmPtsM8wrRShgeTzkse 97ZmS0tDagnNchCdq1yNspVWu197w12PWoCN6o2dONP8YSAiNJtWLgmIGjZNTWRspWIhe ozY7RAke0hntskKmCDgM6fHBHZcusKWXAiTOjVjHmHKZ5I/+9BhveihmJV/UwRO7FZs9P 6WTVpEAvmbHzxh0Ak1YaQNd3WZ7749EQkjkagPCKUXcH+PWrzk7DzlSSA8qTlOo8oO23T D3TR6TtkqIq9J3QQhLIqIGXp9skpqyu0gdNVRNZr3eSTPiF/mjJ/lTaPI8Fq0uS7zFQ== 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=LeXgGcrV; 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 C77D2606A31A; Tue, 1 Dec 2026 11:56:56 +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=LeXgGcrV Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75C8C1F0089A; Tue, 1 Dec 2026 11:56:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796126216; bh=gkE5anAOhVoytmIz4xBJxQrnBnRTl2TkuPOBS728X/E=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=LeXgGcrV6Gf4lH3cbep9dc9CSHE0THNeGfgCgl2zyuSQGvDs3pO/QxsvhWK/DTAQh Dv1X31qN4tbvxuhQJPYuFN1O9FSyZbdL0tWJV5D+Fwa1G0do86LB01QGxAVtvmcBmN /C84oh+Rx+nwLNrfVNKRpQ+cukWZEO7kvriunj5Ayz0NC3z3ChuH/QWDyoyyBTBVDh GQw7stKo4gN1hlLIJ0Gwo3CSDSlIF9sLahY6J9KAhAWh4U2XUvlSB8xBX/CL6RzKvp TYlyIWRV9SMxq6Cf91FwvuLUlh8Fg5BbesvnYiXhRUFQ90hB1G6tsqONwR1gsyFDCi Ps11lTNVp73bA== Date: Tue, 1 Dec 2026 11:56:46 +0000 From: "Lorenzo Stoakes (ARM)" To: "Jose A. Perez de Azpillaga" Cc: David Hildenbrand , Zi Yan , Andrew Morton , Zach O'Keefe , Hugh Dickins , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , 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 v3 3/3] mm/memory: don't allocate in finish_fault() with the folio locked Message-ID: References: <20261201115332.88194-1-azpijr@gmail.com> <20261201115332.88194-4-azpijr@gmail.com> X-Mailing-List: linux-fsdevel@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: <20261201115332.88194-4-azpijr@gmail.com> On Tue, Dec 01, 2026 at 12:53:21PM +0100, Jose A. Perez de Azpillaga wrote: > __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 > retract_page_tables() no longer takes the mmap and VMA locks. If the > table is retracted, finish_fault() allocates one with the folio locked, > which can deadlock in reclaim. > > 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 retry > preallocates the page table, so it only loops if the table is retracted > again every time. > > Fixes: 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock") > Cc: stable@vger.kernel.org > Reviewed-by: Baolin Wang > Reviewed-by: David Hildenbrand (Arm) > Signed-off-by: Jose A. Perez de Azpillaga Thanks LGTM so: Reviewed-by: Lorenzo Stoakes (ARM) > --- > Changes in v3: > - take Lorenzo's finish_fault() comment > - note at the __do_fault() preallocation that it is best-effort > (Lorenzo) > - reword the changelog opener, mention the NOPAGE retry, drop the > memcg v1 example (Lorenzo) > > 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 > > v2: https://lore.kernel.org/20261127091344.113688-4-azpijr@gmail.com > v1: https://lore.kernel.org/20261125100058.15200-3-azpijr@gmail.com > > mm/memory.c | 14 ++++++++++---- > 1 file changed, 10 insertions(+), 4 deletions(-) > > diff --git a/mm/memory.c b/mm/memory.c > index aab038740a8c..4f4a11bca741 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -5701,6 +5701,8 @@ static vm_fault_t __do_fault(struct vm_fault *vmf) > * folio_set_writeback(B) > * folio_unlock(B) > * # flush A, B to clear the writeback > + * > + * Best-effort: the page table can be retracted after this check. > */ > if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { > vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); > @@ -5997,10 +5999,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; > + /* > + * 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. > + */ > + 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