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 07/15] riscv/mm: preserve Svnapot leaf-size semantics for page-table consumers
Date: Thu, 16 Jul 2026 12:57:34 +0000	[thread overview]
Message-ID: <20260716125735.8CDD91F000E9@smtp.kernel.org> (raw)
In-Reply-To: <180084b20de76f7ad61641e9b7abe6d103caf5f3.1784201104.git.cuiyunhui@bytedance.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Single-CPU deadlock in `napotpte_ptep_get_lockless` when called from NMI/IRQ context during a partial Svnapot block update.
--

--- Patch [7]: [PATCH v2 07/15] riscv/mm: preserve Svnapot leaf-size semantics for page-table consumers ---
commit 0488a9106519eab47850cf758ecce3d0c6bdf46e
Author: Yunhui Cui <cuiyunhui@bytedance.com>

riscv/mm: preserve Svnapot leaf-size semantics for page-table consumers

Svnapot keeps a folded 64KB mapping encoded in the page table, while the
public PTE helpers may expose a logical per-page sub-PTE view. This patch
uses the original page-table entry for leaf-size queries so consumers
continue to observe the Svnapot leaf size instead of treating a folded
mapping as PAGE_SIZE entries.

> diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
> index da7ffea0dcd98..e4b9c05014c00 100644
> --- a/arch/riscv/include/asm/pgtable.h
> +++ b/arch/riscv/include/asm/pgtable.h
[ ... ]
> @@ -580,6 +580,17 @@ static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
>  					napot_cont_size(napot_cont_order(pte)) :\
>  					PAGE_SIZE)
>  
> +#define __ptep_leaf_size __ptep_leaf_size
> +static inline unsigned long __ptep_leaf_size(pmd_t pmd, pte_t *ptep, pte_t pte)
> +{
> +	pte_t raw_pte = READ_ONCE(*ptep);
> +
> +	if (pte_present_napot(raw_pte))
> +		return pte_leaf_size(raw_pte);
> +
> +	return PAGE_SIZE;
> +}

[Severity: High]
Can this lead to a single-CPU deadlock in NMI/IRQ context during a partial
Svnapot block update?

If a thread executing napotpte_convert() holds the page table lock (which
disables preemption but leaves NMIs and hardware IRQs enabled) and
sequentially clears PTEs in a 64KB Svnapot block:

arch/riscv/mm/contpte.c:napotpte_convert() {
...
	for (i = 0; i < nr; i++) {
		ptent_addr = start_addr + i * PAGE_SIZE;
		ptent = __ptep_get_and_clear_noptc(start_ptep + i);
...
}

And a Performance Monitoring Interrupt (PMI) interrupts this CPU after some
but not all PTEs are cleared, the PMI handler may invoke
perf_get_pgtable_size(), which eventually invokes the lockless PTE reader.

This would lead to napotpte_ptep_get_lockless() entering a retry loop:

arch/riscv/mm/contpte.c:napotpte_ptep_get_lockless() {
...
retry:
	orig_pte = READ_ONCE(*orig_ptep);
	if (!pte_present_napot(orig_pte))
		return orig_pte;

	ptep = napot_align_ptep(orig_ptep);

	for (i = 0; i < nr; i++, ptep++) {
		pte = READ_ONCE(*ptep);

		if (!napotpte_is_consistent(pte, orig_pte))
			goto retry;
...
}

Because the NMI/IRQ handler prevents the interrupted thread on the same CPU
from finishing the update, the block remains partially cleared. Does this
cause the consistency check to fail repeatedly, resulting in an infinite
loop and hard lockup?

Userspace could potentially trigger this via perf_event_open by sampling
memory accesses during Svnapot page migrations or unmapping.

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

  reply	other threads:[~2026-07-16 12:57 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
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 [this message]
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=20260716125735.8CDD91F000E9@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