[PATCH 1/8] mm: Add ptep_try_set() for lockless empty-slot installs
Will Deacon
will at kernel.org
Sun Jun 14 02:28:02 PDT 2026
On Fri, May 22, 2026 at 07:22:12AM -1000, Tejun Heo wrote:
> Add ptep_try_set(ptep, new_pte): atomically set *ptep to new_pte iff it is
> currently pte_none(). Returns true on success, false if the slot was already
> populated or the arch has no implementation.
>
> The intended caller is the upcoming bpf_arena kernel-side fault recovery
> path. The install runs from a page fault that can be nested under locks
> held by the faulting kernel caller (e.g. a BPF program holding
> raw_res_spin_lock_irqsave on its arena's spinlock), so trylock-and-retry
> would A-A deadlock. Lock-free cmpxchg is the only viable option, which
> constrains this helper to special kernel page tables where concurrent
> writers cooperate via atomic accessors.
>
> The generic version in <linux/pgtable.h> returns false. x86 and arm64
> override with try_cmpxchg-based implementations on the underlying pteval.
> Other architectures get the false stub - the callers there already fall
> through to oops.
>
> v2: Rename to ptep_try_set(). Tighten kerneldoc. (David, Alexei)
> v3: Note that strict-zero cmpxchg is narrower than pte_none(). (Andrea)
>
> Suggested-by: Kumar Kartikeya Dwivedi <memxor at gmail.com>
> Suggested-by: Alexei Starovoitov <ast at kernel.org>
> Signed-off-by: Tejun Heo <tj at kernel.org>
> Reviewed-by: Andrea Righi <arighi at nvidia.com>
> Cc: David Hildenbrand <david at kernel.org>
> ---
> arch/arm64/include/asm/pgtable.h | 12 ++++++++++++
> arch/x86/include/asm/pgtable.h | 12 ++++++++++++
> include/linux/pgtable.h | 25 +++++++++++++++++++++++++
> 3 files changed, 49 insertions(+)
>
> diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
> index 9029b81ccbe8..28bada97d443 100644
> --- a/arch/arm64/include/asm/pgtable.h
> +++ b/arch/arm64/include/asm/pgtable.h
> @@ -1830,6 +1830,18 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
> return __ptep_get_and_clear(mm, addr, ptep);
> }
>
> +/*
> + * Note: strictly-zero compare is narrower than pte_none(), but the gap is
> + * harmless: a fresh kernel PTE has no software bits set.
> + */
This comment really confused me :/
What is a "fresh" kernel PTE and why do you specifically call out "software
bits" if the CAS requires all 64 bits to be 0? Why is that narrower than
pte_none() given that pte_none() for arm64 is:
#define pte_none(pte) (!pte_val(pte))
Will
More information about the linux-arm-kernel
mailing list