Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yunhui Cui" <cuiyunhui@bytedance.com>
Cc: kvm@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v2 14/15] riscv/mm: optimize mprotect for Svnapot mappings
Date: Thu, 16 Jul 2026 13:07:05 +0000	[thread overview]
Message-ID: <20260716130705.A94691F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <8b79ddc3311e1dc32b150b7efbaed5a187ee5ccd.1784201104.git.cuiyunhui@bytedance.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The fallback path corrupts userspace memory tracking by erroneously injecting `_PAGE_SOFT_DIRTY` via `pte_mkdirty()`.
--

--- Patch [14]: [PATCH v2 14/15] riscv/mm: optimize mprotect for Svnapot mappings ---
commit f75eddb541d9ba388068a3cbeef6571534eb416b
Author: Yunhui Cui <cuiyunhui@bytedance.com>

riscv/mm: optimize mprotect for Svnapot mappings

This commit optimizes mprotect operations for Svnapot mappings by handling
full blocks in a single batched operation. A fallback loop is retained for
partial, unaligned, or otherwise inconsistent mappings.

> diff --git a/arch/riscv/mm/contpte.c b/arch/riscv/mm/contpte.c
> index 8289f39d6e188..6a1156629d115 100644
> --- a/arch/riscv/mm/contpte.c
> +++ b/arch/riscv/mm/contpte.c

[ ... ]

> +pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
> +			     pte_t *ptep, unsigned int nr)
> +{
> +	struct mm_struct *mm = vma->vm_mm;
> +	unsigned long ptent_addr;
> +	pte_t raw, pte, ptent;
> +	unsigned int i;
> +
> +	if (napotpte_can_modify_prot_block(mm, addr, ptep, nr, &raw)) {

[ ... ]

> +			if (pte_dirty(ptent))
> +				pte = riscv_pte_mkhwdirty(pte);

[ ... ]

> +		return pte;
> +	}
> +
> +	pte = __ptep_modify_prot_start(vma, addr, ptep);
> +	while (--nr) {
> +		ptep++;
> +		addr += PAGE_SIZE;
> +		ptent = __ptep_modify_prot_start(vma, addr, ptep);
> +		if (pte_dirty(ptent))
> +			pte = pte_mkdirty(pte);

[Severity: Medium]
Does this fallback path unintentionally set the soft-dirty bit for userspace
tracking?

On RISC-V, pte_mkdirty() sets both the hardware _PAGE_DIRTY bit and the
userspace-visible _PAGE_SOFT_DIRTY bit. If a Svnapot mapping was previously
written to (hardware dirty) but its soft-dirty bit was explicitly cleared by
userspace (e.g. via /proc/pid/clear_refs), calling mprotect() on a partial
or unaligned segment of this block triggers this fallback path.

Since the fallback path uses pte_mkdirty() to accumulate the dirty state, it
would inadvertently re-set the _PAGE_SOFT_DIRTY bit on the final batched PTE.
Userspace tracking tools like CRIU could subsequently falsely record this
memory as modified.

The fast path correctly uses riscv_pte_mkhwdirty(pte) to avoid this exact
issue. Should this loop also use riscv_pte_mkhwdirty(pte) instead of
pte_mkdirty(pte)?

> +		if (pte_young(ptent))
> +			pte = pte_mkyoung(pte);
> +	}
> +
> +	return pte;
> +}

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

  reply	other threads:[~2026-07-16 13:07 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
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 [this message]
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=20260716130705.A94691F00A3D@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