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 7E163345729; Mon, 23 Mar 2026 22:18:37 +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=1774304317; cv=none; b=OJSbwftXh7yqmE4KfDINB0DGVJJl5n8mmdrvE4R46QS/tl5pO4l1eDRpLnN6/oDK+7Uiqnvvra0w+2M2ohCVVHLqor7Eyt6Y6mOVl4AC1coqx24O1z+yr07+5fP4z3Bf9ceh96qgsie2DIhjydQzj8YiIk48xX6tfM1UncGAgN0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774304317; c=relaxed/simple; bh=o/+Kg9/bBW5TqV/W+gUBtz++JcR9+Hvj9c2EEedZ2p0=; h=Date:To:From:Subject:Message-Id; b=D9tzMGI/uoHHieUKQZr7tf+C27x3N8XsjqwpfJxVks6K3Lie8QGpD3qTKtKIf9h6DyZqtpJemkc7qUj6nyR/iczwQtkJTSwfnEc4VYIqgie8RObfSwfddc4IGJzA/gLF/wYgY5DkqGMsoqakEWi6zfDTp510gQJ4ZOKgGLr5wH8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=pX7+NvzI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="pX7+NvzI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 086ADC4CEF7; Mon, 23 Mar 2026 22:18:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1774304317; bh=o/+Kg9/bBW5TqV/W+gUBtz++JcR9+Hvj9c2EEedZ2p0=; h=Date:To:From:Subject:From; b=pX7+NvzIQqr6UB4fcGx+BBTcKjUsFHx94e6tLxVbeLUImPlVR476AYVOL6wPsZmGc rQNH9khtuoqWpza8w86kS2yTHMKZ0U3RWaZqRU5dZynkZH5GKeyeeYmPbrvMgp6K7Q W7rCm7bmW09crHzoX4zjjtJfWvRwLwe43m4gKXJI= Date: Mon, 23 Mar 2026 15:18:36 -0700 To: mm-commits@vger.kernel.org,vbabka@kernel.org,surenb@google.com,stable@vger.kernel.org,rppt@kernel.org,peterx@redhat.com,mhocko@suse.com,ljs@kernel.org,liam.howlett@oracle.com,david@kernel.org,akpm@linux-foundation.org From: Andrew Morton Subject: + mm-memory-fix-pmd-pud-checks-in-follow_pfnmap_start.patch added to mm-hotfixes-unstable branch Message-Id: <20260323221837.086ADC4CEF7@smtp.kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: The patch titled Subject: mm/memory: fix PMD/PUD checks in follow_pfnmap_start() has been added to the -mm mm-hotfixes-unstable branch. Its filename is mm-memory-fix-pmd-pud-checks-in-follow_pfnmap_start.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-memory-fix-pmd-pud-checks-in-follow_pfnmap_start.patch This patch will later appear in the mm-hotfixes-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: "David Hildenbrand (Arm)" Subject: mm/memory: fix PMD/PUD checks in follow_pfnmap_start() Date: Mon, 23 Mar 2026 21:20:18 +0100 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 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) Cc: Liam Howlett Cc: Lorenzo Stoakes (Oracle) Cc: Michal Hocko Cc: Mike Rapoport Cc: Peter Xu Cc: Suren Baghdasaryan Cc: Vlastimil Babka Cc: Signed-off-by: Andrew Morton --- mm/memory.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) --- a/mm/memory.c~mm-memory-fix-pmd-pud-checks-in-follow_pfnmap_start +++ a/mm/memory.c @@ -6815,11 +6815,16 @@ retry: 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; } @@ -6831,9 +6836,16 @@ retry: 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; } _ Patches currently in -mm which might be from david@kernel.org are mm-memory-fix-pmd-pud-checks-in-follow_pfnmap_start.patch mm-centralizefix-comments-about-compound_mapcount-in-new-sync_with_folio_pmd_zap.patch mm-pagewalk-drop-fw_migration.patch mm-madvise-drop-range-checks-in-madvise_free_single_vma.patch mm-memory-remove-zap_details-parameter-from-zap_page_range_single.patch mm-memory-remove-zap_details-parameter-from-zap_page_range_single-fix.patch mm-memory-inline-unmap_mapping_range_vma-into-unmap_mapping_range_tree.patch mm-memory-simplify-calculation-in-unmap_mapping_range_tree.patch mm-oom_kill-use-mmu_notify_clear-in-__oom_reap_task_mm.patch mm-oom_kill-factor-out-zapping-of-vma-into-zap_vma_for_reaping.patch mm-memory-rename-unmap_single_vma-to-__zap_vma_range.patch mm-memory-move-adjusting-of-address-range-to-unmap_vmas.patch mm-memory-convert-details-even_cows-into-details-skip_cows.patch mm-memory-use-__zap_vma_range-in-zap_vma_for_reaping.patch mm-memory-inline-unmap_page_range-into-__zap_vma_range.patch mm-memory-inline-unmap_page_range-into-__zap_vma_range-fix.patch mm-rename-zap_vma_pages-to-zap_vma.patch mm-rename-zap_page_range_single_batched-to-zap_vma_range_batched.patch mm-rename-zap_page_range_single-to-zap_vma_range.patch mm-rename-zap_vma_ptes-to-zap_special_vma_range.patch mm-memory-support-vm_mixedmap-in-zap_special_vma_range.patch kasan-docs-slub-is-the-only-remaining-slab-implementation.patch mm-move-vma_kernel_pagesize-from-hugetlb-to-mmh.patch mm-move-vma_mmu_pagesize-from-hugetlb-to-vmac.patch kvm-remove-hugetlbh-inclusion.patch kvm-ppc-remove-hugetlbh-inclusion.patch mm-memory_hotplug-remove-for_each_valid_pfn-usage.patch mm-sparse-remove-warn_ons-from-onlineoffline_mem_sections.patch mm-kconfig-make-config_memory_hotplug-depend-on-config_sparsemem_vmemmap.patch mm-memory_hotplug-simplify-check_pfn_span.patch mm-sparse-remove-config_sparsemem_vmemmap-leftovers-for-config_memory_hotplug.patch mm-bootmem_info-remove-handling-for-config_sparsemem_vmemmap.patch mm-bootmem_info-avoid-using-sparse_decode_mem_map.patch mm-sparse-remove-sparse_decode_mem_map.patch mm-sparse-remove-config_memory_hotplug-specific-usemap-allocation-handling.patch mm-prepare-to-move-subsection_map_init-to-mm-sparse-vmemmapc.patch mm-sparse-drop-set_section_nid-from-sparse_add_section.patch mm-sparse-move-sparse_init_one_section-to-internalh.patch mm-sparse-move-sparse_init_one_section-to-internalh-fix.patch mm-sparse-move-__section_mark_present-to-internalh.patch mm-sparse-move-memory-hotplug-bits-to-sparse-vmemmapc.patch mm-remove-config_arch_enable_memory_hotremove.patch mm-introduce-config_numa_migration-and-simplify-config_migration.patch