Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yunhui Cui" <cuiyunhui@bytedance.com>
Cc: linux-perf-users@vger.kernel.org, kvm@vger.kernel.org
Subject: Re: [PATCH v2 05/15] mm: extend pte batch and leaf-size helpers
Date: Thu, 16 Jul 2026 13:06:28 +0000	[thread overview]
Message-ID: <20260716130629.7A7AE1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <e821b31e023ee0e5b1c54daed253034bd4da0398.1784201104.git.cuiyunhui@bytedance.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The new `__ptep_leaf_size` API encourages a double-read TOCTOU race in the lockless page table walker `perf_get_pgtable_size()`, violating generic lockless walk concurrency rules.
- [Low] The `__pte_batch_clear_ignored()` inline function breaks the build for architectures without an MMU (!CONFIG_MMU).
--

--- Patch [5]: [PATCH v2 05/15] mm: extend pte batch and leaf-size helpers ---
commit a6d281b7dea89e7c1eed5117dbe4f8bc7a9fd3f6
Author: Yunhui Cui <cuiyunhui@bytedance.com>

mm: extend pte batch and leaf-size helpers

This commit extends pte_batch_hint() with batch flags and introduces
__ptep_leaf_size() to give leaf-size users access to the original PTE pointer.
It also moves batch flag helpers into pgtable.h.

> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
> index 8c093c119e5a8..da14328093a86 100644
> --- a/include/linux/pgtable.h
> +++ b/include/linux/pgtable.h

[ ... ]

> @@ -1917,6 +1948,17 @@ static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
>  }
>  #endif
>  
> +static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags)
> +{
> +	if (!(flags & FPB_RESPECT_DIRTY))
> +		pte = pte_mkclean(pte);
> +	if (likely(!(flags & FPB_RESPECT_SOFT_DIRTY)))
> +		pte = pte_clear_soft_dirty(pte);
> +	if (likely(!(flags & FPB_RESPECT_WRITE)))
> +		pte = pte_wrprotect(pte);
> +	return pte_mkold(pte);
> +}
> +

[Severity: Low]
Does moving __pte_batch_clear_ignored() to the global scope in pgtable.h
break the build for !CONFIG_MMU architectures?

The functions pte_mkclean(), pte_wrprotect(), and pte_mkold() are generally
undefined without an MMU. Since this is now placed outside of any CONFIG_MMU
guard, it appears it could cause implicit function declaration errors for
nommu targets.

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 51c1200ea3fdd..37b860abe9e61 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -8551,7 +8551,7 @@ static u64 perf_get_pgtable_size(struct mm_struct *mm, unsigned long addr)
>  
>  	pte = ptep_get_lockless(ptep);
>  	if (pte_present(pte))
> -		size = __pte_leaf_size(pmd, pte);
> +		size = __ptep_leaf_size(pmd, ptep, pte);
>  	pte_unmap(ptep);
>  #endif /* CONFIG_HAVE_GUP_FAST */
>  

[Severity: Medium]
Could the new __ptep_leaf_size() API introduce a Time-Of-Check to
Time-Of-Use race in lockless walkers like perf_get_pgtable_size()?

In perf_get_pgtable_size(), we acquire an atomic snapshot of the PTE using
ptep_get_lockless(), and then pass both the snapshot (pte) and the raw
pointer (ptep) to __ptep_leaf_size().

If an architecture implementation of __ptep_leaf_size() reads the ptep
pointer directly to handle folded mappings, it would perform a second
non-atomic read. A concurrent page table modification could alter the PTE
between the initial ptep_get_lockless() snapshot and the architecture's
read, leading to a mismatched evaluation.

Lockless walkers typically need to derive all information from a single
atomic snapshot to ensure concurrency rules are respected.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1784201104.git.cuiyunhui@bytedance.com?part=5

  reply	other threads:[~2026-07-16 13:06 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-16 12:41 [PATCH v2 00/15] riscv: add Svnapot PTE folding support Yunhui Cui
2026-07-16 12:41 ` [PATCH v2 01/15] riscv: introduce raw PTE helpers Yunhui Cui
2026-07-16 12:59   ` sashiko-bot
2026-07-16 12:41 ` [PATCH v2 02/15] riscv: switch arch page-table users to " Yunhui Cui
2026-07-16 13:05   ` sashiko-bot
2026-07-16 12:41 ` [PATCH v2 03/15] riscv/mm: implement Svnapot contpte read-side helpers Yunhui Cui
2026-07-16 12:59   ` sashiko-bot
2026-07-16 12:41 ` [PATCH v2 04/15] riscv/mm: implement Svnapot contpte update helpers Yunhui Cui
2026-07-16 13:03   ` sashiko-bot
2026-07-16 12:41 ` [PATCH v2 05/15] mm: extend pte batch and leaf-size helpers Yunhui Cui
2026-07-16 13:06   ` sashiko-bot [this message]
2026-07-16 12:41 ` [PATCH v2 06/15] riscv: make pte_batch_hint() honor folio batch flags Yunhui Cui
2026-07-16 12:41 ` [PATCH v2 07/15] riscv/mm: preserve Svnapot leaf-size semantics for page-table consumers Yunhui Cui
2026-07-16 12:57   ` sashiko-bot
2026-07-16 12:41 ` [PATCH v2 08/15] riscv/mm: avoid redundant Svnapot A/D aggregation Yunhui Cui
2026-07-16 12:41 ` [PATCH v2 09/15] riscv/mm: avoid Svnapot consistency checks in ptep_get() Yunhui Cui
2026-07-16 13:03   ` sashiko-bot
2026-07-16 12:41 ` [PATCH v2 10/15] mm/gup: add fast-GUP specific lockless PTE helpers Yunhui Cui
2026-07-16 12:41 ` [PATCH v2 11/15] riscv: mm: avoid Svnapot A/D aggregation in fast-GUP Yunhui Cui
2026-07-16 12:41 ` [PATCH v2 12/15] arm64: mm: avoid contpte " Yunhui Cui
2026-07-16 12:41 ` [PATCH v2 13/15] riscv/mm: remove redundant TLB flush in napotpte_convert Yunhui Cui
2026-07-16 13:17   ` sashiko-bot
2026-07-16 12:41 ` [PATCH v2 14/15] riscv/mm: optimize mprotect for Svnapot mappings Yunhui Cui
2026-07-16 13:07   ` sashiko-bot
2026-07-16 12:41 ` [PATCH v2 15/15] riscv: mm: Request large exec folios for Svnapot Yunhui Cui

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=20260716130629.7A7AE1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=cuiyunhui@bytedance.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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