From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 63765175A8D for ; Wed, 1 Apr 2026 17:06:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063212; cv=none; b=HM3Xts6jbxqmNIg1c0+lgfeZusJi3WswZGDyhlFbQGVji4Kr23/3M9/KNXwXESsiP5fehSLxWcj07WSJgwU0X/wubIOZU+U3xsUcwoP7nW4Va2NLKGj6gHSNc1i8zz8ZgCOKiKxbi1HBoXNU7dCEAY+3g+lRDaBWe6JHp+HxrS0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063212; c=relaxed/simple; bh=8DA3iV7PFcGoUxPeXGXDlpC8D2/ZPk+pi4bQkk7ONgk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gIEA8jpttaQfTBJQE5ZWkkVSGLhHlwdx9iQXFyg6J8MibHhPRPURow/zlIjEXogV/Vm1ILPJrfM56he9QNEPONxPhqieSa1BA2cfaBeIgnDtliaKyvlJ/0yuZM4csjMIPRPFWWWeGZ3JhZPA2pcrQd8xHohQHeKXZlDO8aRkaX4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AutTr7RH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AutTr7RH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35927C19421; Wed, 1 Apr 2026 17:06:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775063212; bh=8DA3iV7PFcGoUxPeXGXDlpC8D2/ZPk+pi4bQkk7ONgk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AutTr7RHemTKd4b9I8elayJpY53Ct7yvSYonwLZcvnGMNknGezHS6nJtsYfyo182r 48CYLfV4G9od0RvPjvz1d8a+ksufwPIuSv2yTtUp0jQOVnM9+WQShVPI1x5Ub7kLX8 7GqCld8C09GO0KJzgLSW/eEjNuIsi3m/tMHnTym/ZSkZolLE9fLGWtZzMdhsq1vxbi RH8PZTCwAVnUlnaZOb+yY1/wZ3S02+ko6AWCi1WRaC8k7toNZ/gDV7iTDDwXxh7mBH TvuaMvD0hJc+Af+grY/tNHM3dW6/kMVWwqa19SDRUlKB30jhHHB1Dbb3ifwpu+/Tro g2gfLGDq85+GQ== From: Sasha Levin To: stable@vger.kernel.org Cc: "David Hildenbrand (Arm)" , "Mike Rapoport (Microsoft)" , "Lorenzo Stoakes (Oracle)" , Liam Howlett , Michal Hocko , Peter Xu , Suren Baghdasaryan , Vlastimil Babka , Andrew Morton , Sasha Levin Subject: [PATCH 6.12.y 2/2] mm/memory: fix PMD/PUD checks in follow_pfnmap_start() Date: Wed, 1 Apr 2026 13:06:43 -0400 Message-ID: <20260401170643.151278-2-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260401170643.151278-1-sashal@kernel.org> References: <2026033034-amount-briar-a849@gregkh> <20260401170643.151278-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "David Hildenbrand (Arm)" [ Upstream commit ffef67b93aa352b34e6aeba3d52c19a63885409a ] follow_pfnmap_start() suffers from two problems: (1) We are not re-fetching the pmd/pud after taking the PTL Therefore, we are not properly stabilizing what the lock actually protects. If there is concurrent zapping, we would indicate to the caller that we found an entry, however, that entry might already have been invalidated, or contain a different PFN after taking the lock. Properly use pmdp_get() / pudp_get() after taking the lock. (2) pmd_leaf() / pud_leaf() are not well defined on non-present entries pmd_leaf()/pud_leaf() could wrongly trigger on non-present entries. There is no real guarantee that pmd_leaf()/pud_leaf() returns something reasonable on non-present entries. Most architectures indeed either perform a present check or make it work by smart use of flags. However, for example loongarch checks the _PAGE_HUGE flag in pmd_leaf(), and always sets the _PAGE_HUGE flag in __swp_entry_to_pmd(). Whereby pmd_trans_huge() explicitly checks pmd_present(), pmd_leaf() does not do that. Let's check pmd_present()/pud_present() before assuming "the is a present PMD leaf" when spotting pmd_leaf()/pud_leaf(), like other page table handling code that traverses user page tables does. Given that non-present PMD entries are likely rare in VM_IO|VM_PFNMAP, (1) is likely more relevant than (2). It is questionable how often (1) would actually trigger, but let's CC stable to be sure. This was found by code inspection. Link: https://lkml.kernel.org/r/20260323-follow_pfnmap_fix-v1-1-5b0ec10872b3@kernel.org Fixes: 6da8e9634bb7 ("mm: new follow_pfnmap API") Signed-off-by: David Hildenbrand (Arm) Acked-by: Mike Rapoport (Microsoft) Reviewed-by: Lorenzo Stoakes (Oracle) Cc: Liam Howlett Cc: Michal Hocko Cc: Peter Xu Cc: Suren Baghdasaryan Cc: Vlastimil Babka Cc: Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- mm/memory.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index d27cd9a7443ce..49ee03c4392ef 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6457,11 +6457,16 @@ int follow_pfnmap_start(struct follow_pfnmap_args *args) pudp = pud_offset(p4dp, address); pud = pudp_get(pudp); - if (pud_none(pud)) + if (!pud_present(pud)) goto out; if (pud_leaf(pud)) { lock = pud_lock(mm, pudp); - if (!unlikely(pud_leaf(pud))) { + pud = pudp_get(pudp); + + if (unlikely(!pud_present(pud))) { + spin_unlock(lock); + goto out; + } else if (unlikely(!pud_leaf(pud))) { spin_unlock(lock); goto retry; } @@ -6473,9 +6478,16 @@ int follow_pfnmap_start(struct follow_pfnmap_args *args) pmdp = pmd_offset(pudp, address); pmd = pmdp_get_lockless(pmdp); + if (!pmd_present(pmd)) + goto out; if (pmd_leaf(pmd)) { lock = pmd_lock(mm, pmdp); - if (!unlikely(pmd_leaf(pmd))) { + pmd = pmdp_get(pmdp); + + if (unlikely(!pmd_present(pmd))) { + spin_unlock(lock); + goto out; + } else if (unlikely(!pmd_leaf(pmd))) { spin_unlock(lock); goto retry; } -- 2.53.0