Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Guilherme Giacomo Simoes" <trintaeoitogc@gmail.com>,
	akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org,
	liam@infradead.org, vbabka@kernel.org, rppt@kernel.org,
	surenb@google.com, mhocko@suse.com
Cc: linux-mm@kvack.org
Subject: Re: [PATCH] mm: clear pmd in pmdp_invalidate()
Date: Wed, 28 Oct 2026 11:38:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-187827@kernel.org> (raw)
In-Reply-To: <20261027113813.1332096-1-trintaeoitogc@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm: double decrement in pmdp_invalidate() via page_table_check_pmd_clear()
--

commit df830550fc83c05eb08f00e78d36d32212821daf
Author: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>

mm: clear pmd in pmdp_invalidate()

This commit adds a call to page_table_check_pmd_clear() inside generic
pmdp_invalidate() after calling pmdp_establish() with pmd_mkinvalid(*pmdp).

> diff --git a/mm/pgtable-generic.c b/mm/pgtable-generic.c
> index 6e83ce3801b8a..bbf74c73a1471 100644
> --- a/mm/pgtable-generic.c
> +++ b/mm/pgtable-generic.c
> @@ -215,6 +215,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
>  {
>  	VM_WARN_ON_ONCE(!pmd_present(*pmdp));
>  	pmd_t old = pmdp_establish(vma, address, pmdp, pmd_mkinvalid(*pmdp));
> +	page_table_check_pmd_clear(vma->vm_mm, address, old);

[Severity: High]
Does calling page_table_check_pmd_clear() here cause a double decrement of
ptc->anon_map_count or ptc->file_map_count when CONFIG_PAGE_TABLE_CHECK is
enabled, triggering a BUG_ON() in page_table_check_clear()?

On architectures that support CONFIG_PAGE_TABLE_CHECK and use the generic
pmdp_invalidate() implementation (x86, arm64, and riscv), pmdp_establish()
already invokes page_table_check_pmd_set() before replacing *pmdp. For
example, in arch/x86/include/asm/pgtable.h:pmdp_establish():

	page_table_check_pmd_set(vma->vm_mm, address, pmdp, pmd);
	if (IS_ENABLED(CONFIG_SMP)) {
		return xchg(pmdp, pmd);

And similarly in arch/arm64/include/asm/pgtable.h:pmdp_establish() and
arch/riscv/include/asm/pgtable.h:pmdp_establish() (as well as
generic_pmdp_establish() via set_pmd_at()).

Inside mm/page_table_check.c:__page_table_check_pmds_set(), the existing
present PMD (*pmdp, which has the same value later returned as old) is
cleared first, and the new invalidated PMD is only accounted if
pmd_user_accessible_page() is true:

	for (i = 0; i < nr; i++)
		__page_table_check_pmd_clear(mm, addr + PMD_SIZE * i, *(pmdp + i));
	if (pmd_user_accessible_page(mm, addr, pmd) &&
	    !page_table_check_huge_zero_pmd(pmd))
		page_table_check_set(pmd_pfn(pmd), stride * nr, pmd_write(pmd));

Because pmd_mkinvalid() clears the present/valid bits (including on riscv
since commit bdda2887cbf3 ("riscv: mm: Fixup compat arch_want_old_prefaulted_pte()
and page_table_check")), pmd_user_accessible_page() returns false for the new
invalidated PMD and page_table_check_set() is not called.

When pmdp_invalidate() then calls page_table_check_pmd_clear() on old a
second time, mm/page_table_check.c:page_table_check_clear() decrements the
counter again:

		if (anon) {
			BUG_ON(atomic_read(&ptc->file_map_count));
			BUG_ON(atomic_dec_return(&ptc->anon_map_count) < 0);
		} else {
			BUG_ON(atomic_read(&ptc->anon_map_count));
			BUG_ON(atomic_dec_return(&ptc->file_map_count) < 0);
		}

Wouldn't any call to pmdp_invalidate() on a present user-accessible PMD (such
as during THP splitting, madvise(), mprotect(), or mremap()) underflow
anon_map_count or file_map_count below zero and hit this BUG_ON()?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261027113813.1332096-1-trintaeoitogc@gmail.com?part=1


      parent reply	other threads:[~2026-10-28 11:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 11:38 [PATCH] mm: clear pmd in pmdp_invalidate() Guilherme Giacomo Simoes
2026-10-27 12:11 ` David Hildenbrand (Arm)
2026-10-27 14:09   ` Guilherme Giacomo Simoes
2026-10-27 14:50     ` Guilherme Giacomo Simoes
2026-11-04 11:12   ` Guilherme Giacomo Simoes
2026-11-07 19:28     ` David Hildenbrand (Arm)
2026-11-08 10:22       ` Guilherme Giacomo Simoes
2026-10-27 23:10 ` [syzbot ci] " syzbot ci
2026-10-28 11:38 ` sashiko-bot [this message]

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-187827@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=surenb@google.com \
    --cc=trintaeoitogc@gmail.com \
    --cc=vbabka@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