Re: [RFC PATCH 1/4] mm: use proper PTE accessors in madvise() and mremap()

From: Kevin Brodsky

Date: Fri Jul 17 2026 - 13:14:21 EST


On 17/07/2026 15:32, Alexander Gordeev wrote:
> Follow the pattern established by commit c33c794828f2 ("mm:
> ptep_get() conversion") and use proper accessors instead of
> direct pointer dereferences.
>
> Signed-off-by: Alexander Gordeev <agordeev@xxxxxxxxxxxxx>
> ---
> mm/madvise.c | 2 +-
> mm/mremap.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/mm/madvise.c b/mm/madvise.c
> index 77552b03d318..7d369b1ff8e5 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -1106,7 +1106,7 @@ static int guard_install_set_pte(unsigned long addr, unsigned long next,
> unsigned long *nr_pages = (unsigned long *)walk->private;
>
> /* Simply install a PTE marker, this causes segfault on access. */
> - *ptep = make_pte_marker(PTE_MARKER_GUARD);
> + set_pte(ptep, make_pte_marker(PTE_MARKER_GUARD));

AFAICT ptep is not pointing to a real page table entry, but a local copy
- see walk_pte_range_inner().

Such direct write to page tables are prevented by the pkeys-based
mechanism I proposed [1], and I can confirm that the guard-regions mm
kselftests do not cause any crash with that feature enabled.

- Kevin

[1] https://lore.kernel.org/all/20260526-kpkeys-v8-0-eaaacdacc67c@xxxxxxx/

> (*nr_pages)++;
>
> return 0;
> diff --git a/mm/mremap.c b/mm/mremap.c
> index e9c8b1d05832..fc3b90274a57 100644
> --- a/mm/mremap.c
> +++ b/mm/mremap.c
> @@ -264,7 +264,7 @@ static int move_ptes(struct pagetable_move_control *pmc,
>
> for (; old_addr < old_end; old_ptep += nr_ptes, old_addr += nr_ptes * PAGE_SIZE,
> new_ptep += nr_ptes, new_addr += nr_ptes * PAGE_SIZE) {
> - VM_WARN_ON_ONCE(!pte_none(*new_ptep));
> + VM_WARN_ON_ONCE(!pte_none(ptep_get(new_ptep)));
>
> nr_ptes = 1;
> max_nr_ptes = (old_end - old_addr) >> PAGE_SHIFT;