Re: [PATCH v3 0/9] mm: distinguish HW PTE pointers from SW PTE value pointers

From: Muhammad Usama Anjum

Date: Wed Nov 18 2026 - 16:49:31 EST


On 16/11/2026 6:18 pm, David Hildenbrand (Arm) wrote:
> On 11/16/26 18:41, Catalin Marinas wrote:
>> Hi Usama,
>>
> Hi,
>
> David here :)
>
>> (trimmed the Cc list, some lists reject it as too many recipients)
>>
>> On Tue, Sep 22, 2026 at 06:12:28PM +0100, Muhammad Usama Anjum wrote:
>>> pte_t currently describes both a software PTE value and an element stored
>>> in a PTE table. Consequently, pte_t * can point either to a software PTE
>>> value, often a stack copy, or to a PTE-table slot. The compiler cannot
>>> distinguish these cases. A value pointer can therefore be passed to an
>>> interface that expects table storage, while table storage can be read by
>>> direct dereference instead of the architecture accessor.
>>>
>>> This series begins a staged conversion at the PTE level. It introduces
>>> hw_pte_t as the element type for PTE-table storage and converts generic
>>> MM to use hw_pte_t *. Software PTE values remain pte_t. Interfaces that
>>> intentionally return a value through pte_t *, such as install_pte,
>>> remain value interfaces; the relevant parameters are named ptentp to
>>> make that distinction explicit.
>>
>> [...]
>>
>>> PMD, PUD, P4D and PGD storage are deliberately out of scope. They can be
>>> converted in later series after the PTE boundary is agreed, avoiding the
>>> PMD-specific cases that made an all-level conversion difficult to
>>> review.
>>
>> I looked at this (and the arm64 counterpart) and I'm not fully convinced
>> it's worth the effort (but I won't oppose it if the mm folk decide to go
>> this route).
>
> I think this is the right thing to do to get type safety.
>
>>
>> First, I'd like to see all such on-stack p*d pointers addressed as a
>> prerequisite (e.g. fix pmd_populate() in __split_huge_zero_page_pmd()).
>> If for some reason we deem pmd not fixable, I don't like half (or
>> quarter) baked solutions just addressing the pte.
>
> IIRC doing the full PxD conversion is quite big; doens't make sense to post
> these on the list and instead move stepwise.
>
> Agreed that it would be nice to get rid of all stack usage first.
>
>>
>> Second, we could add a run-time page table check, at least for pmd/pte.
>> Something like below, gated by CONFIG_PAGE_TABLE_CHECK or
>> CONFIG_DEBUG_VM (untested and not entirely sure about using
>> PageTable()):
>>
>> static inline void pgtable_ptr_check(struct mm_struct *mm, const void *p)
>> {
>> if (object_is_on_stack(p)) {
>> WARN_ON_ONCE(1);
>> return;
>> }
>> if (mm != &init_mm && virt_addr_valid(p))
>> WARN_ON_ONCE(!PageTable(virt_to_page(p)));
>> }
>>
>> You already have some coccinelle patches to make the conversion. Could
>> we turn them into a coccicheck to detect new uses? With LLM reviews like
>> Sashiko, we can probably catch new uses violating the requirement (we
>> need to document it).
>>
>> LLMs and coccicheck won't catch all abuses nor does this series prevent
>> one from declaring hw_pte_t on the stack.
>>
>> Once the above are in place, I'd look again at these patches and see
>> whether they improve code clarity and are worth the churn.
>>
>> Maybe an argument in favour of these patchsets is future dynamic support
>> for FEAT_D128, we'd need to separate the software representation of a
>> pte from the hardware one but we've done this before (arm32 non-LPAE has
>> different representations).
>
> Exactly; we'll need this and go one step further and disallow any ptep++ to
> instead route this through helpers.

Agreed, after the current series, we can easily make hw_pte_t opaque like the
following (or in even better way if we spend some more time thinking about it):

diff --git a/include/linux/pgtable_types.h b/include/linux/pgtable_types.h
index ee4eace5c3e1c..9898c0613e170 100644
--- a/include/linux/pgtable_types.h
+++ b/include/linux/pgtable_types.h
@@ -7,17 +7,38 @@
#ifndef __ASSEMBLY__

#ifdef CONFIG_ARCH_HAS_HW_PTE_T
-typedef struct __hw_pte_t { pte_t __pte; } hw_pte_t;
+typedef struct __hw_pte_t hw_pte_t;
+typedef struct { pte_t __pte; } __hw_pte_storage_t;
#define __pte_from_hw(pte) ((pte).__pte)

#define hw_pte_val(x) pte_val((x).__pte)
#else
#define hw_pte_t pte_t
+typedef pte_t __hw_pte_storage_t;
#define __pte_from_hw(pte) (pte)

#define hw_pte_val(x) pte_val(x)
#endif

+static inline hw_pte_t *ptep_advance(hw_pte_t *ptep, long nr)
+{
+ return (hw_pte_t *)((__hw_pte_storage_t *)ptep + nr);
+}
+
+static inline hw_pte_t *ptep_align_down(hw_pte_t *ptep, unsigned long nr)
+{
+ unsigned long size = sizeof(__hw_pte_storage_t) * nr;
+
+ return (hw_pte_t *)((unsigned long)ptep & ~(size - 1));
+}
+
+static inline long ptep_diff(const hw_pte_t *ptep,
+ const hw_pte_t *start_ptep)
+{
+ return (const __hw_pte_storage_t *)ptep -
+ (const __hw_pte_storage_t *)start_ptep;
+}
+
#endif /* !__ASSEMBLY__ */

#endif /* _LINUX_PGTABLE_TYPES_H */


--
Thanks,
Usama