Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arnd Bergmann" <arnd@kernel.org>, linux-mm@kvack.org
Cc: "Arnd Bergmann" <arnd@arndb.de>,
	"Andreas Larsson" <andreas@gaisler.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	"David Hildenbrand (Arm)" <david@kernel.org>,
	"H. Peter Anvin" <hpa@zytor.com>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Jason Gunthorpe" <jgg@nvidia.com>,
	"Linus Walleij" <linusw@kernel.org>,
	"Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
	"Lucas Stach" <l.stach@pengutronix.de>,
	"Madhavan Srinivasan" <maddy@linux.ibm.com>,
	"Malin Jonsson" <malin.jonsson@est.tech>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Michael Ellerman" <mpe@ellerman.id.au>,
	"Michal Simek" <monstr@monstr.eu>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Nicholas Piggin" <npiggin@gmail.com>,
	"Nishanth Menon" <nm@ti.com>,
	"Richard Weinberger" <richard@nod.at>,
	"Russell King" <linux@armlinux.org.uk>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	x86@kernel.org
Subject: Re: [PATCH v3 10/10] mm: print pte in __print_bad_page_map_pgtable()
Date: Tue, 17 Nov 2026 12:50:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-218805@kernel.org> (raw)
In-Reply-To: <20261116124832.662455-11-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm: TOCTOU race and missing RCU protection in
  __print_bad_page_map_pgtable()
--

commit ba4bdee29bb52c7a53d86e0e93403b7b06ce3b2f
Author: Arnd Bergmann <arnd@arndb.de>

mm: print pte in __print_bad_page_map_pgtable()

This commit extends __print_bad_page_map_pgtable() to also read and print
the PTE entry alongside the upper page table levels.

> diff --git a/mm/memory.c b/mm/memory.c
> index ec3d9d99ccee2..6404bb447d9c7 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -528,9 +528,11 @@ static void __print_bad_page_map_pgtable(struct mm_struct *mm, unsigned long add
>  	char p4d_str[PTVAL_STR_MAX];
>  	char pud_str[PTVAL_STR_MAX];
>  	char pmd_str[PTVAL_STR_MAX];
> +	char pte_str[PTVAL_STR_MAX];
>  	p4d_t p4d, *p4dp;
>  	pud_t pud, *pudp;
>  	pmd_t pmd, *pmdp;
> +	pte_t pte, *ptep;
>  	pgd_t *pgdp;
>  
>  	/*
> @@ -567,13 +569,18 @@ static void __print_bad_page_map_pgtable(struct mm_struct *mm, unsigned long add
>  	pmd = pmdp_get(pmdp);
>  	ptval_to_str(pmd_str, pmd_val(pmd));
>  
> -	/*
> -	 * Dumping the PTE would be nice, but it's tricky with CONFIG_HIGHPTE,
> -	 * because the table should already be mapped by the caller and
> -	 * doing another map would be bad. print_bad_page_map() should
> -	 * already take care of printing the PTE.
> -	 */
> -	pr_alert("pgd:%s p4d:%s pud:%s pmd:%s\n", pgd_str, p4d_str, pud_str, pmd_str);
> +	if (!pmd_present(pmd) || pmd_leaf(pmd)) {
> +		pr_alert("pgd:%s p4d:%s pud:%s pmd: %s\n",
> +			 pgd_str, p4d_str, pud_str, pmd_str);
> +		return;
> +	}
> +
> +	ptep = pte_offset_kernel(pmdp, addr);
> +	pte = ptep_get(ptep);
> +	ptval_to_str(pte_str, pte_val(pte));
> +
> +	pr_alert("pgd:%s p4d:%s pud:%s pmd:%s pte:%s\n",
> +		 pgd_str, p4d_str, pud_str, pmd_str, pte_str);
>  }

[Severity: Medium]
Can pte_offset_kernel(pmdp, addr) and ptep_get(ptep) race with concurrent
PTE table reclaim or collapse and read a freed or invalid PTE table?

pte_offset_kernel(pmdp, addr) dereferences *pmdp again after the local pmd
copy was validated, and accesses the PTE page table without rcu_read_lock().
Although print_bad_page_map() assumes callers keep the leaf page table
mapped, callers such as do_swap_page() unmap the PTE (dropping both the PTL
and rcu_read_lock() in pte_unmap_same()) before calling print_bad_pte():

mm/memory.c:do_swap_page() {
    ...
	if (!pte_unmap_same(vmf))
		goto out;
    ...
		} else {
			print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
}

Other paths such as get_gate_page() and hmm_vma_handle_pte() also call
vm_normal_page() under pte_offset_map() without holding the leaf PTL:

CPU0                                  CPU1
do_swap_page()
  pte_unmap_same(vmf)
  print_bad_pte()
    ...
    pmd = pmdp_get(pmdp)
    if (!pmd_present(pmd) || ...)
                                      retract_page_tables() / zap_pte_range()
                                        pmdp_collapse_flush() / pmd_clear()
                                        pte_free_defer()
                                        /* RCU grace period elapses */
    ptep = pte_offset_kernel(pmdp, addr)
    pte = ptep_get(ptep)  <-- bogus address or use-after-free

If CPU1 clears *pmdp or replaces it with a huge leaf PMD and frees the PTE
page table via RCU between pmdp_get(pmdp) and ptep_get(ptep), CPU0 will
re-read the modified *pmdp and dereference a bogus kernel address or freed
PTE table.  Using pte_offset_map() and pte_unmap() here avoids re-reading
*pmdp and keeps the PTE page table RCU-protected while reading the entry.

Also, this isn't a bug, but the early-return pr_alert() above adds an extra
space in "pmd: %s" compared to "pmd:%s" in the other messages.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116124832.662455-1-arnd@kernel.org?part=10


  parent reply	other threads:[~2026-11-17 12:50 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 12:48 [PATCH v3 00/10] mm: highmem cleanups Arnd Bergmann
2026-11-16 12:48 ` [PATCH v3 01/10] ARM: remove support for highmem on VIVT Arnd Bergmann
2026-11-17 12:49   ` sashiko-bot
2026-11-16 12:48 ` [PATCH v3 02/10] ARM: drop CONFIG_HIGHPTE support Arnd Bergmann
2026-11-16 12:48 ` [PATCH v3 03/10] ARM: pxa: support arbitrary PAGE_OFFSET Arnd Bergmann
2026-11-16 12:48 ` [PATCH v3 04/10] powerpc: kvm: allow flexible PAGE_OFFSET Arnd Bergmann
2026-11-16 13:38   ` Christophe Leroy (CS GROUP)
2026-11-16 22:54     ` Arnd Bergmann
2026-11-17 12:49   ` sashiko-bot
2026-11-16 12:48 ` [PATCH v3 05/10] ARM: add CONFIG_VMSPLIT_2G_OPT option Arnd Bergmann
2026-11-16 12:48 ` [PATCH v3 06/10] arch/*: increase lowmem size to avoid highmem use Arnd Bergmann
2026-11-17 12:49   ` sashiko-bot
2026-11-16 12:48 ` [PATCH v3 07/10] ARM: defconfig: turn off highmem in favor of CONFIG_VMSPLIT_2G_OPT Arnd Bergmann
2026-11-16 12:48 ` [PATCH v3 08/10] mm: remove ARCH_NEEDS_KMAP_HIGH_GET Arnd Bergmann
2026-11-17 10:05   ` David Hildenbrand (Arm)
2026-11-16 12:48 ` [PATCH v3 09/10] mm: drop HIGHPTE support altogether Arnd Bergmann
2026-11-16 12:48 ` [PATCH v3 10/10] mm: print pte in __print_bad_page_map_pgtable() Arnd Bergmann
2026-11-16 13:00   ` Mike Rapoport
2026-11-17 12:50   ` sashiko-bot [this message]
2026-11-17 10:07 ` [PATCH v3 00/10] mm: highmem cleanups David Hildenbrand (Arm)

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=sashiko-outbox-218805@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andreas@gaisler.com \
    --cc=arnd@arndb.de \
    --cc=arnd@kernel.org \
    --cc=chleroy@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@kernel.org \
    --cc=hpa@zytor.com \
    --cc=jgg@nvidia.com \
    --cc=l.stach@pengutronix.de \
    --cc=linusw@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux@armlinux.org.uk \
    --cc=ljs@kernel.org \
    --cc=maddy@linux.ibm.com \
    --cc=malin.jonsson@est.tech \
    --cc=mingo@redhat.com \
    --cc=monstr@monstr.eu \
    --cc=mpe@ellerman.id.au \
    --cc=nm@ti.com \
    --cc=npiggin@gmail.com \
    --cc=richard@nod.at \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tglx@linutronix.de \
    --cc=willy@infradead.org \
    --cc=x86@kernel.org \
    /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