mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lance Yang <lance.yang@linux.dev>
To: david@kernel.org
Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, mhocko@kernel.org,
	qi.zheng@linux.dev, shakeel.butt@linux.dev, ljs@kernel.org,
	kasong@tencent.com, baohua@kernel.org, axelrasmussen@google.com,
	yuanchu@google.com, weixugc@google.com, baoquan.he@linux.dev,
	baolin.wang@linux.alibaba.com, liam@infradead.org,
	vbabka@kernel.org, rppt@kernel.org, surenb@google.com,
	mhocko@suse.com, kees@kernel.org, peterz@infradead.org,
	jannh@google.com, pfalcato@suse.de, hughd@google.com,
	pasha.tatashin@soleen.com, pratyush@kernel.org,
	willy@infradead.org, jack@suse.cz, ziy@nvidia.com,
	lance.yang@linux.dev, riel@surriel.com, harry@kernel.org,
	apopple@nvidia.com, nico.pache@linux.dev, ryan.roberts@arm.com,
	dev.jain@arm.com, usama.arif@linux.dev, kas@kernel.org,
	matthew.brost@intel.com, jhubbard@nvidia.com, peterx@redhat.com,
	will@kernel.org, aneesh.kumar@kernel.org, npiggin@gmail.com,
	osalvador@suse.de, minchan@kernel.org, muchun.song@linux.dev,
	xu.xin@linux.dev, chengming.zhou@linux.dev, chrisl@kernel.org,
	shikemeng@huaweicloud.com, nphamcs@gmail.com,
	youngjun.park@lge.com, gourry@gourry.net,
	joshua.hahnjy@gmail.com, rakie.kim@sk.com,
	ying.huang@linux.alibaba.com, jgg@ziepe.ca, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, kexec@lists.infradead.org,
	linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org,
	linux-cxl@vger.kernel.org, josef@toxicpanda.com
Subject: Re: [PATCH 32/46] mm/memory_hotplug: avoid races in pfn_to_online_page() with dying pgmap
Date: Tue, 27 Oct 2026 20:13:07 +0800	[thread overview]
Message-ID: <20261027121307.40431-1-lance.yang@linux.dev> (raw)
In-Reply-To: <20261027-fixes_core-v1-32-6f74bec9a5bb@kernel.org>


On Tue, Oct 27, 2026 at 12:17:52AM +0100, David Hildenbrand (Arm) wrote:
>From: Josef Bacik <josef@toxicpanda.com>
>
>If ZONE_DEVICE memory shares a memory section with ordinary memory, the
>section is online and the pfns of the device are valid.
>pfn_to_online_page() tells them apart by asking get_dev_pagemap() whether
>the pfn belongs to a dev_pagemap.
>
>get_dev_pagemap() only returns a dev_pagemap whose reference is alive.
>memunmap_pages() kills the reference first, waits until all pages are
>idle, and then removes the memmap. During all that time the function
>finds nothing, and pfn_to_online_page() hands out the struct pages of the
>device as if they were online memory. A pfn walker reads them while they
>are poisoned and freed.
>
>The entry in pgmap_array is only deleted after the pages have been
>removed. Look at that instead of trying to get a reference.
>
>This is probably not worth CCing stable.
>
>Fix generated by an LLM and sent by Josef Bacik in private.
>
>Fixes: 1f90a3477df3 ("mm: teach pfn_to_online_page() about ZONE_DEVICE section collisions")
>Signed-off-by: Josef Bacik <josef@toxicpanda.com>
>Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
>---
> include/linux/memremap.h |  6 ++++++
> mm/memory_hotplug.c      | 13 +++++--------
> mm/memremap.c            | 13 +++++++++++++
> 3 files changed, 24 insertions(+), 8 deletions(-)
>
>diff --git a/include/linux/memremap.h b/include/linux/memremap.h
>index e3c2ccf872a8a..b5e5e626c7a59 100644
>--- a/include/linux/memremap.h
>+++ b/include/linux/memremap.h
>@@ -231,6 +231,7 @@ void memunmap_pages(struct dev_pagemap *pgmap);
> void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);
> void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);
> struct dev_pagemap *get_dev_pagemap(unsigned long pfn);
>+bool pfn_has_dev_pagemap(unsigned long pfn);
> bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn);
> 
> unsigned long memremap_compat_align(void);
>@@ -284,6 +285,11 @@ static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn)
> 	return NULL;
> }
> 
>+static inline bool pfn_has_dev_pagemap(unsigned long pfn)
>+{
>+	return false;
>+}
>+
> static inline bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn)
> {
> 	return false;
>diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
>index 796af1028ee23..5a972d7a9e478 100644
>--- a/mm/memory_hotplug.c
>+++ b/mm/memory_hotplug.c
>@@ -342,7 +342,6 @@ static int check_pfn_span(unsigned long pfn, unsigned long nr_pages)
> struct page *pfn_to_online_page(unsigned long pfn)
> {
> 	unsigned long nr = pfn_to_section_nr(pfn);
>-	struct dev_pagemap *pgmap;
> 	struct mem_section *ms;
> 
> 	BUILD_BUG_ON(IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID));
>@@ -363,14 +362,12 @@ struct page *pfn_to_online_page(unsigned long pfn)
> 	/*
> 	 * Slowpath: when ZONE_DEVICE collides with
> 	 * ZONE_{NORMAL,MOVABLE} within the same section some pfns in
>-	 * the section may be 'offline' but 'valid'. Only
>-	 * get_dev_pagemap() can determine sub-section online status.
>+	 * the section may be 'offline' but 'valid'. Only the presence of a
>+	 * pgmap can determine sub-section online status: it indicates a
>+	 * ZONE_DEVICE offline pfn. That includes a pgmap whose reference has
>+	 * been killed, its memmap is about to go away.
> 	 */
>-	pgmap = get_dev_pagemap(pfn);
>-	put_dev_pagemap(pgmap);
>-
>-	/* The presence of a pgmap indicates ZONE_DEVICE offline pfn */
>-	if (pgmap)
>+	if (pfn_has_dev_pagemap(pfn))
> 		return NULL;
> 
> 	return pfn_to_page(pfn);


LLM stumbled upon a possible race in kpage_read() ... 

Say the reader passes pfn_section_valid() for a device PFN in a section
shared with online RAM, gets preempted, and resumes after
pgmap_array_delete(). We'd then find no pgmap and return pfn_to_page(pfn),
even though the subsection is no longer valid ...

Could we take the memory hotplug read lock in kpage_read(), covering both
the lookup and the metadata reads? We can drop it before put_user(), once
we're done with the page.

Maybe along these lines?

---8<---
diff --git a/fs/proc/page.c b/fs/proc/page.c
index 6aa99e42ec22..12bed36d76dc 100644
--- a/fs/proc/page.c
+++ b/fs/proc/page.c
@@ -12,6 +12,7 @@
 #include <linux/hugetlb.h>
 #include <linux/memremap.h>
 #include <linux/memcontrol.h>
+#include <linux/memory_hotplug.h>
 #include <linux/mmu_notifier.h>
 #include <linux/page_idle.h>
 #include <linux/kernel-page-flags.h>
@@ -77,6 +78,8 @@ static ssize_t kpage_read(struct file *file, char __user *buf,
 	count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);

 	while (count > 0) {
+		/* Prevent memory removal while inspecting the memmap. */
+		get_online_mems();
 		/*
 		 * TODO: ZONE_DEVICE support requires to identify
 		 * memmaps that were actually initialized.
@@ -101,6 +104,8 @@ static ssize_t kpage_read(struct file *file, char __user *buf,
 		} else
 			info = 0;

+		put_online_mems();
+
 		if (put_user(info, out)) {
 			ret = -EFAULT;
 			break;
--

Cheers, Lance

  reply	other threads:[~2026-10-27 12:13 UTC|newest]

Thread overview: 122+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26 23:17 [PATCH 00/46] mm: various (urgent?) fixes David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 01/46] mm/mglru: don't use stack pointers when walking PUD and PMD tables David Hildenbrand (Arm)
2026-10-27  1:32   ` Rik van Riel
2026-10-27  3:05   ` Qi Zheng
2026-10-27  7:20   ` Dev Jain
2026-10-27  8:06   ` Lorenzo Stoakes (ARM)
2026-10-27 19:06     ` David Hildenbrand (Arm)
2026-10-27 10:22   ` Kairui Song
2026-10-26 23:17 ` [PATCH 02/46] fork: take mmap_lock before copying mm_struct David Hildenbrand (Arm)
2026-10-27  1:38   ` Rik van Riel
2026-10-27  8:18   ` Vlastimil Babka (SUSE)
2026-10-27  9:45   ` Barry Song
2026-10-27 10:38   ` Pedro Falcato
2026-10-27 19:07     ` David Hildenbrand (Arm)
2026-10-27 19:48       ` Barry Song
2026-10-27 13:56   ` Harry Yoo
2026-10-26 23:17 ` [PATCH 03/46] mm/memfd: check for real shmem inodes before accessing shmem state David Hildenbrand (Arm)
2026-10-27  2:04   ` Rik van Riel
2026-10-27  8:13   ` Mike Rapoport
2026-10-27 13:42     ` David Hildenbrand (Arm)
2026-10-27 14:41   ` Pratyush Yadav
2026-10-26 23:17 ` [PATCH 04/46] mm/memfd: make f_pos accesses atomic David Hildenbrand (Arm)
2026-10-27  2:06   ` Rik van Riel
2026-10-27  3:34   ` Gregory Price
2026-10-27  8:44     ` Lorenzo Stoakes (ARM)
2026-10-27 19:09     ` David Hildenbrand (Arm)
2026-10-27  8:15   ` Mike Rapoport
2026-10-27  8:46   ` Lorenzo Stoakes (ARM)
2026-10-27 10:39   ` Pedro Falcato
2026-10-26 23:17 ` [PATCH 05/46] mm/truncate: look again if a folio left the mapping David Hildenbrand (Arm)
2026-10-27  2:17   ` Rik van Riel
2026-10-26 23:17 ` [PATCH 06/46] mm/filemap: read mapping->a_ops->free_folio under i_lock David Hildenbrand (Arm)
2026-10-27  2:43   ` Rik van Riel
2026-10-27 19:10     ` David Hildenbrand (Arm)
2026-10-27 15:47   ` Pedro Falcato
2026-10-26 23:17 ` [PATCH 07/46] mm/filemap: don't lose a folio lock wakeup to a signal David Hildenbrand (Arm)
2026-10-27  3:03   ` Rik van Riel
2026-10-27 16:10   ` Pedro Falcato
2026-10-27 19:18     ` David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 08/46] mm/filemap: finish fault cleanup before dropping the fault lock David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 09/46] mm/rmap: handle device-exclusive entries in folio_referenced_one() David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 10/46] mm/huge_memory: fix list corruption when deferred_split_scan() races with deferred_split_folio() David Hildenbrand (Arm)
2026-10-27 14:10   ` Johannes Weiner
2026-10-27 15:15     ` David Hildenbrand (Arm)
2026-10-27 18:03       ` Mani Tofigh
2026-10-27 19:19         ` David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 11/46] mm/huge_memory: properly handle refcounted PUD mappings on fork David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 12/46] mm/huge_memory: fix livelock on device private folio David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 13/46] mm/huge_memory: fix truncate racing with PMD zapping David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 14/46] mm/huge_memory: fix zapping migration entries for device-private THPs David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 15/46] mm/khugepaged: always recheck the pmd in try_collapse_pte_mapped_thp() David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 16/46] mm/khugepaged: fix PMD table lock leak " David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 17/46] mm/khugepaged: preserve dirty bits when collapsing PTE-mapped THPs David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 18/46] mm/khugepaged: check if PTE table is empty in try_collapse_pte_mapped_thp() David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 19/46] mm/gup: recheck the PUD under PTL in follow_pud_mask() David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 20/46] mm/gup: fix clearing folios in memfd_pin_folios() David Hildenbrand (Arm)
2026-10-27 15:00   ` Pratyush Yadav
2026-10-27 19:20     ` David Hildenbrand (Arm)
2026-10-27 19:43   ` Samiullah Khawaja
2026-10-26 23:17 ` [PATCH 21/46] mm/gup: fix out-of-bounds write for nr_pages == 0 in GUP-fast David Hildenbrand (Arm)
2026-10-27  9:37   ` Lorenzo Stoakes (ARM)
2026-10-27  9:40   ` Muchun Song
2026-10-26 23:17 ` [PATCH 22/46] mm/gup: check for overflow when converting nr_pages to bytes " David Hildenbrand (Arm)
2026-10-27  9:34   ` Lorenzo Stoakes (ARM)
2026-10-27  9:44   ` Muchun Song
2026-10-26 23:17 ` [PATCH 23/46] mm/userfaultfd: write-lock the VMA before waking waiters when unregistering David Hildenbrand (Arm)
2026-10-27  8:16   ` Mike Rapoport
2026-10-27  8:43   ` Lorenzo Stoakes (ARM)
2026-10-27 17:38     ` Suren Baghdasaryan
2026-10-27 19:21     ` David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 24/46] mm/userfaultfd: wake all waiters on unregister David Hildenbrand (Arm)
2026-10-27  8:17   ` Mike Rapoport
2026-10-27  8:36   ` Lorenzo Stoakes (ARM)
2026-10-26 23:17 ` [PATCH 25/46] mm/userfaultfd: wake waiters when registration modes are removed David Hildenbrand (Arm)
2026-10-27  5:10   ` Gregory Price
2026-10-27  8:24     ` Lorenzo Stoakes (ARM)
2026-10-27 19:31       ` David Hildenbrand (Arm)
2026-10-27  8:21   ` Mike Rapoport
2026-10-27  8:31   ` Lorenzo Stoakes (ARM)
2026-10-26 23:17 ` [PATCH 26/46] mm/mmu_gather: fix flushing ranges with 4 GiB or larger David Hildenbrand (Arm)
2026-10-27  3:06   ` Qi Zheng
2026-10-27  3:40   ` Gregory Price
2026-10-27  7:35   ` Dev Jain
2026-10-27 19:25     ` David Hildenbrand (Arm)
2026-10-27  9:50   ` Muchun Song
2026-10-27 15:42   ` Pedro Falcato
2026-10-26 23:17 ` [PATCH 27/46] mm/mmu_notifier: finish non-blocking interval invalidation on error David Hildenbrand (Arm)
2026-10-27  9:33   ` Lorenzo Stoakes (ARM)
2026-10-26 23:17 ` [PATCH 28/46] mm/mmu_notifier: set ->mm only after mmgrab() David Hildenbrand (Arm)
2026-10-27  3:42   ` Gregory Price
2026-10-27  8:50   ` Lorenzo Stoakes (ARM)
2026-10-26 23:17 ` [PATCH 29/46] mm/mremap: use pudp_huge_get_and_clear() when moving huge PUDs David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 30/46] mm/mremap: recheck PUDs under the page table lock David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 31/46] mm/mremap: fix moving PUD leaves David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 32/46] mm/memory_hotplug: avoid races in pfn_to_online_page() with dying pgmap David Hildenbrand (Arm)
2026-10-27 12:13   ` Lance Yang [this message]
2026-10-27 19:41     ` David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 33/46] mm/madvise: fix data loss from MADV_FREE on a KSM page David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 34/46] mm/memory: reject remote COW faults on PFN mappings David Hildenbrand (Arm)
2026-10-27 16:34   ` Pedro Falcato
2026-10-26 23:17 ` [PATCH 35/46] mm: fix unmap_mapping_range() beyond 4 GiB on 32-bit David Hildenbrand (Arm)
2026-10-27 16:27   ` Pedro Falcato
2026-10-26 23:17 ` [PATCH 36/46] mm: fix unkillable fault loop on MAP_DROPPABLE memory David Hildenbrand (Arm)
2026-10-26 23:17 ` [PATCH 37/46] mm: fix deadlock of mremap() against UFFDIO_MOVE David Hildenbrand (Arm)
2026-10-27  9:23   ` Lorenzo Stoakes (ARM)
2026-10-27  9:46   ` Muchun Song
2026-10-26 23:17 ` [PATCH 38/46] mm/ksm: reset the unstable trees of all nodes David Hildenbrand (Arm)
2026-10-27  9:14   ` Lorenzo Stoakes (ARM)
2026-10-26 23:17 ` [PATCH 39/46] mm/ksm: fix use of a freed anon_vma at swap-in David Hildenbrand (Arm)
2026-10-26 23:18 ` [PATCH 40/46] mm/ksm: check large swap-cache folios before mapping them David Hildenbrand (Arm)
2026-10-27  9:00   ` Lorenzo Stoakes (ARM)
2026-10-26 23:18 ` [PATCH 41/46] mm/vma: call ->may_split() before a partial merge David Hildenbrand (Arm)
2026-10-27  3:45   ` Gregory Price
2026-10-26 23:18 ` [PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries David Hildenbrand (Arm)
2026-10-26 23:18 ` [PATCH 43/46] mm/vma: don't leak the charge for stack growth David Hildenbrand (Arm)
2026-10-27 16:25   ` Pedro Falcato
2026-10-26 23:18 ` [PATCH 44/46] mm/cma: don't release CMA pages still in use David Hildenbrand (Arm)
2026-10-27  8:55   ` Lorenzo Stoakes (ARM)
2026-10-27  9:48   ` Muchun Song
2026-10-26 23:18 ` [PATCH 45/46] mm/memory-tiers: unregister empty tiers outside memory_tier_lock David Hildenbrand (Arm)
2026-10-26 23:18 ` [PATCH 46/46] fs/proc/task_mmu: refuse addresses beyond 32 bits on 32bit David Hildenbrand (Arm)
2026-10-27 16:13   ` Pedro Falcato

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261027121307.40431-1-lance.yang@linux.dev \
    --to=lance.yang@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=aneesh.kumar@kernel.org \
    --cc=apopple@nvidia.com \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=chengming.zhou@linux.dev \
    --cc=chrisl@kernel.org \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=gourry@gourry.net \
    --cc=hannes@cmpxchg.org \
    --cc=harry@kernel.org \
    --cc=hughd@google.com \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=josef@toxicpanda.com \
    --cc=joshua.hahnjy@gmail.com \
    --cc=kas@kernel.org \
    --cc=kasong@tencent.com \
    --cc=kees@kernel.org \
    --cc=kexec@lists.infradead.org \
    --cc=liam@infradead.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=mhocko@kernel.org \
    --cc=mhocko@suse.com \
    --cc=minchan@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=nico.pache@linux.dev \
    --cc=nphamcs@gmail.com \
    --cc=npiggin@gmail.com \
    --cc=osalvador@suse.de \
    --cc=pasha.tatashin@soleen.com \
    --cc=peterx@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pfalcato@suse.de \
    --cc=pratyush@kernel.org \
    --cc=qi.zheng@linux.dev \
    --cc=rakie.kim@sk.com \
    --cc=riel@surriel.com \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=shakeel.butt@linux.dev \
    --cc=shikemeng@huaweicloud.com \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=weixugc@google.com \
    --cc=will@kernel.org \
    --cc=willy@infradead.org \
    --cc=xu.xin@linux.dev \
    --cc=ying.huang@linux.alibaba.com \
    --cc=youngjun.park@lge.com \
    --cc=yuanchu@google.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®