Skip to contentSkip to conversation

mm: add basic PTE const type-safety

PATCH In review Pedro Falcato 8 participants 33 messages Aug 3 – Aug 5

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

v2 cover mm: add basic PTE const type-safety

Since forever, MM code has thrown pte_t * around with no concern for const safety, or typesafety of any kind. This is confusing. Attempt to address it by:
1) Making sure pte_get*() helpers can cope with const pte_t * arguments
2) Constifying the pte_offset_map_ro_nolock() return type, which by definition already pledges that users will not write to it.

These two simple steps were already able to uncover code smell from khugepaged + do_swap_page().

Separate steps could include introducing pte_offset_map_ro_lock() for more widespread usage of this.

Benefits of this include less confusion and better type-safety. It could also futurely aid in efforts such as [0] which may want semantic annotation of these accesses.

Based on mm-unstable and compile-tested on a handful of architectures.

No functional changes intended.

[CC list editorially trimmed for brevity reasons; apologies if you're not on it]

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
Cc Helge Deller <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Jan Kara <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Lance Yang <[email protected]>
Cc Usama Arif <[email protected]>
Cc Kevin Brodsky <[email protected]>
Cc Muhammad Usama Anjum <[email protected]>

v2:
- Small fixups on the arm64 side
- Re-order patches in a way such that bisection is preserved
- Pick up Helge's patch dropping parisc ptep_get()
- Constify s390's ptep_get() as well

Helge Deller (1):
parisc: Drop own implementations for ptep_get() and
ptep_test_and_clear_young()

Pedro Falcato (5):
mm/arm64: constify pte_get*() and contpte get logic mm/powerpc/8xx: constify ptep_get() argument mm/s390: constify ptep_get() argument
mm: constify generic pte_get*()
mm: constify the pte_offset_map_ro_nolock() return value

 arch/arm64/include/asm/pgtable.h             | 10 +++++-----
 arch/arm64/mm/contpte.c                      | 11 ++++++++---
 arch/parisc/include/asm/pgtable.h            | 20 --------------------
 arch/powerpc/include/asm/nohash/32/pte-8xx.h |  2 +-
 arch/powerpc/mm/pgtable.c                    |  2 +-
 arch/s390/include/asm/pgtable.h              |  2 +-
 include/linux/mm.h                           |  4 ++--
 include/linux/pgtable.h                      |  8 ++++----
 mm/filemap.c                                 |  2 +-
 mm/khugepaged.c                              |  2 +-
 mm/pgtable-generic.c                         |  4 ++--
 11 files changed, 26 insertions(+), 41 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/6 mm/arm64: constify pte_get*() and contpte get logic

None of the contpte code needs write access to the PTEs.

---
 arch/arm64/include/asm/pgtable.h | 10 +++++-----
 arch/arm64/mm/contpte.c          | 11 ++++++++---
 2 files changed, 13 insertions(+), 8 deletions(-)
arch/arm64/include/asm/pgtable.h +5 -5
index a2681d755358..043bc0649cee 100644
@@ -378,7 +378,7 @@ static inline void __set_pte(pte_t *ptep, pte_t pte)
 	__set_pte_complete(pte);
 }
 
-static inline pte_t __ptep_get(pte_t *ptep)
+static inline pte_t __ptep_get(const pte_t *ptep)
 {
 	return READ_ONCE(*ptep);
 }
@@ -1652,8 +1652,8 @@ extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte);
 extern void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte);
-extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte);
-extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep);
+extern pte_t contpte_ptep_get(const pte_t *ptep, pte_t orig_pte);
+extern pte_t contpte_ptep_get_lockless(const pte_t *orig_ptep);
 extern void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte, unsigned int nr);
 extern void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
@@ -1732,7 +1732,7 @@ static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte)
  */
 
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	pte_t pte = __ptep_get(ptep);
 
@@ -1743,7 +1743,7 @@ static inline pte_t ptep_get(pte_t *ptep)
 }
 
 #define ptep_get_lockless ptep_get_lockless
-static inline pte_t ptep_get_lockless(pte_t *ptep)
+static inline pte_t ptep_get_lockless(const pte_t *ptep)
 {
 	pte_t pte = __ptep_get(ptep);
 
arch/arm64/mm/contpte.c +8 -3
index 2de12656b4d8..3a5d6937fb51 100644
@@ -26,6 +26,11 @@ static inline pte_t *contpte_align_down(pte_t *ptep)
 	return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
 }
 
+#define contpte_align_down(ptep)                                                       \
+	_Generic((ptep),                                                               \
+		 const pte_t *: (const pte_t *) contpte_align_down((pte_t *) (ptep)),  \
+		 pte_t *: contpte_align_down((pte_t *) ptep))
+
 static inline pte_t *contpte_align_addr_ptep(unsigned long *start,
 					     unsigned long *end, pte_t *ptep,
 					     unsigned int nr)
@@ -310,7 +315,7 @@ void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
 }
 EXPORT_SYMBOL_GPL(__contpte_try_unfold);
 
-pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)
+pte_t contpte_ptep_get(const pte_t *ptep, pte_t orig_pte)
 {
 	/*
 	 * Gather access/dirty bits, which may be populated in any of the ptes
@@ -367,7 +372,7 @@ static inline bool contpte_is_consistent(pte_t pte, unsigned long pfn,
 			pgprot_val(prot) == pgprot_val(orig_prot);
 }
 
-pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
+pte_t contpte_ptep_get_lockless(const pte_t *orig_ptep)
 {
 	/*
 	 * The ptep_get_lockless() API requires us to read and return *orig_ptep
@@ -386,10 +391,10 @@ pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
 	 * because it is not part of a contpte range.
 	 */
 
+	const pte_t *ptep;
 	pgprot_t orig_prot;
 	unsigned long pfn;
 	pte_t orig_pte;
-	pte_t *ptep;
 	pte_t pte;
 	int i;
 
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v2 1/6] mm/arm64: constify pte_get*() and contpte get logic

Quoted 1 line · Pedro Falcato

None of the contpte code needs write access to the PTEs.

This seems like a very broad statement? Is that actually true? I see a bunch of ptep's that aren't const-ified, so you should explain why those couldn't be converted.

Also you add a new contpte_align_down() macro, you should mention that it the commit message, explain why it was needed.

In general more needed here :) it'd be ok if it was a truly trivial change that was all obvious but you're changing some pte_t *'s and not others so it's clearly not.

Quoted 21 lines
---
 arch/arm64/include/asm/pgtable.h | 10 +++++-----
 arch/arm64/mm/contpte.c          | 11 ++++++++---
 2 files changed, 13 insertions(+), 8 deletions(-)
arch/arm64/include/asm/pgtable.h +1 -1
index a2681d755358..043bc0649cee 100644
@@ -378,7 +378,7 @@ static inline void __set_pte(pte_t *ptep, pte_t pte)
 	__set_pte_complete(pte);
 }
 
-static inline pte_t __ptep_get(pte_t *ptep)
+static inline pte_t __ptep_get(const pte_t *ptep)
 {
 	return READ_ONCE(*ptep);
 }

arch/arm64/include/asm/pgtable.h extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr, · line 1656

Quoted 7 lines
arch/arm64/include/asm/pgtable.h +2 -2
@@ -1652,8 +1652,8 @@ extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte);
 extern void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte);
-extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte);
-extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep);
+extern pte_t contpte_ptep_get(const pte_t *ptep, pte_t orig_pte);
+extern pte_t contpte_ptep_get_lockless(const pte_t *orig_ptep);

This is (very) nitty but - not sure on the policy on extern's (Will/Catalin?) - but in mm we drop them when we touch the code since you don't need them these days :)

Quoted 26 lines
arch/arm64/include/asm/pgtable.h +2 -2
 extern void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte, unsigned int nr);
 extern void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
@@ -1732,7 +1732,7 @@ static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte)
  */
 
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	pte_t pte = __ptep_get(ptep);
 
@@ -1743,7 +1743,7 @@ static inline pte_t ptep_get(pte_t *ptep)
 }
 
 #define ptep_get_lockless ptep_get_lockless
-static inline pte_t ptep_get_lockless(pte_t *ptep)
+static inline pte_t ptep_get_lockless(const pte_t *ptep)
 {
 	pte_t pte = __ptep_get(ptep);
 
arch/arm64/mm/contpte.c +0 -0
index 2de12656b4d8..3a5d6937fb51 100644

arch/arm64/mm/contpte.c static inline pte_t *contpte_align_down(pte_t *ptep) · line 32

Quoted 7 lines
arch/arm64/mm/contpte.c +4 -0
@@ -26,6 +26,11 @@ static inline pte_t *contpte_align_down(pte_t *ptep)
 	return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
 }
 
+#define contpte_align_down(ptep)                                                       \
+	_Generic((ptep),                                                               \
+		 const pte_t *: (const pte_t *) contpte_align_down((pte_t *) (ptep)),  \
+		 pte_t *: contpte_align_down((pte_t *) ptep))

I really hate these _Generic() helper things. So ugly. And it's a pretty horrid cast now :(

Was it not possible to const-ify further to just be able to constify contpte_align_down itself?

It also seems to contradict the claim that contpte doesn't need write-access to pte's since you're going to lengths to allow non-const pte_t * here.

You should cover off why this was necessary in the commit message as above.

Anyway PTR_ALIGN_DOWN() is already const-safe so couldn't you anyway just collapse this to:

#define contpte_align_down(ptep) \
PTR_ALIGN_DOWN(ptep, sizeof(*(ptep)) * CONT_PTES)

Then describe in the commit message why you need to handle both cases?

Quoted 23 lines
arch/arm64/mm/contpte.c +3 -2
+
 static inline pte_t *contpte_align_addr_ptep(unsigned long *start,
 					     unsigned long *end, pte_t *ptep,
 					     unsigned int nr)
@@ -310,7 +315,7 @@ void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
 }
 EXPORT_SYMBOL_GPL(__contpte_try_unfold);
 
-pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)
+pte_t contpte_ptep_get(const pte_t *ptep, pte_t orig_pte)
 {
 	/*
 	 * Gather access/dirty bits, which may be populated in any of the ptes
@@ -367,7 +372,7 @@ static inline bool contpte_is_consistent(pte_t pte, unsigned long pfn,
 			pgprot_val(prot) == pgprot_val(orig_prot);
 }
 
-pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
+pte_t contpte_ptep_get_lockless(const pte_t *orig_ptep)
 {
 	/*
 	 * The ptep_get_lockless() API requires us to read and return *orig_ptep

arch/arm64/mm/contpte.c pte_t contpte_ptep_get_lockless(pte_t *orig_ptep) · line 394

Quoted 4 lines
arch/arm64/mm/contpte.c +1 -0
@@ -386,10 +391,10 @@ pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
 	 * because it is not part of a contpte range.
 	 */
 
+	const pte_t *ptep;

Nit but this is breaking the reverse xmas tree isn't it?

Quoted 10 lines
arch/arm64/mm/contpte.c +0 -2
 	pgprot_t orig_prot;
 	unsigned long pfn;
 	pte_t orig_pte;
-	pte_t *ptep;
 	pte_t pte;
 	int i;
 
--

2.55.0

--
Cheers, Lorenzo

#3 lore raw

Re: [PATCH v2 1/6] mm/arm64: constify pte_get*() and contpte get logic

Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 1 line · Pedro Falcato

None of the contpte code needs write access to the PTEs.

This seems like a very broad statement? Is that actually true? I see a bunch of ptep's that aren't const-ified, so you should explain why those couldn't be converted.

ACK. FTR, I think it would've been far clearer with "None of the contpte get code". There is of course contpte code that needs write access (e.g contpte_clear_full_ptes).

Quoted 32 lines

Also you add a new contpte_align_down() macro, you should mention that it the commit message, explain why it was needed.

In general more needed here :) it'd be ok if it was a truly trivial change that was all obvious but you're changing some pte_t *'s and not others so it's clearly not.

Quoted 24 lines
---
 arch/arm64/include/asm/pgtable.h | 10 +++++-----
 arch/arm64/mm/contpte.c          | 11 ++++++++---
 2 files changed, 13 insertions(+), 8 deletions(-)
arch/arm64/include/asm/pgtable.h +1 -1
index a2681d755358..043bc0649cee 100644
@@ -378,7 +378,7 @@ static inline void __set_pte(pte_t *ptep, pte_t pte)
 	__set_pte_complete(pte);
 }
 
-static inline pte_t __ptep_get(pte_t *ptep)
+static inline pte_t __ptep_get(const pte_t *ptep)
 {
 	return READ_ONCE(*ptep);
 }
@@ -1652,8 +1652,8 @@ extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte);
 extern void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte);
Quoted 8 lines

arch/arm64/include/asm/pgtable.h extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr, · line 1656

Quoted 4 lines
arch/arm64/include/asm/pgtable.h +2 -2
-extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte);
-extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep);
+extern pte_t contpte_ptep_get(const pte_t *ptep, pte_t orig_pte);
+extern pte_t contpte_ptep_get_lockless(const pte_t *orig_ptep);

This is (very) nitty but - not sure on the policy on extern's (Will/Catalin?) - but in mm we drop them when we touch the code since you don't need them these days :)

*nods*. For what it's worth, this is new code that never needed extern.

Quoted 30 lines
Quoted 30 lines
arch/arm64/include/asm/pgtable.h +2 -2
 extern void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte, unsigned int nr);
 extern void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
@@ -1732,7 +1732,7 @@ static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte)
  */
 
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	pte_t pte = __ptep_get(ptep);
 
@@ -1743,7 +1743,7 @@ static inline pte_t ptep_get(pte_t *ptep)
 }
 
 #define ptep_get_lockless ptep_get_lockless
-static inline pte_t ptep_get_lockless(pte_t *ptep)
+static inline pte_t ptep_get_lockless(const pte_t *ptep)
 {
 	pte_t pte = __ptep_get(ptep);
 
arch/arm64/mm/contpte.c +0 -0
index 2de12656b4d8..3a5d6937fb51 100644
@@ -26,6 +26,11 @@ static inline pte_t *contpte_align_down(pte_t *ptep)
 	return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
 }
 
Quoted 7 lines

arch/arm64/mm/contpte.c static inline pte_t *contpte_align_down(pte_t *ptep) · line 32

Quoted 4 lines
arch/arm64/mm/contpte.c +4 -0
+#define contpte_align_down(ptep)                                                       \
+	_Generic((ptep),                                                               \
+		 const pte_t *: (const pte_t *) contpte_align_down((pte_t *) (ptep)),  \
+		 pte_t *: contpte_align_down((pte_t *) ptep))

I really hate these _Generic() helper things. So ugly. And it's a pretty horrid cast now :(

Me too!

Quoted 4 lines

Was it not possible to const-ify further to just be able to constify contpte_align_down itself?

You can't do that because some callers want a pte_t* out of align_down, others want a const pte_t* out of align_down, depending on the param.

In A More Civilized Language(TM):

template <typename T>
T contpte_align_down(T ptr);

:P

Quoted 2 lines

It also seems to contradict the claim that contpte doesn't need write-access to pte's since you're going to lengths to allow non-const pte_t * here.

Yep, I'll admit the commit message is confusing and crap. I'll flesh it out here.

Quoted 37 lines

You should cover off why this was necessary in the commit message as above.

Anyway PTR_ALIGN_DOWN() is already const-safe so couldn't you anyway just collapse this to:

#define contpte_align_down(ptep) \
PTR_ALIGN_DOWN(ptep, sizeof(*(ptep)) * CONT_PTES)

Then describe in the commit message why you need to handle both cases?

Quoted 26 lines
arch/arm64/mm/contpte.c +3 -2
+
 static inline pte_t *contpte_align_addr_ptep(unsigned long *start,
 					     unsigned long *end, pte_t *ptep,
 					     unsigned int nr)
@@ -310,7 +315,7 @@ void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
 }
 EXPORT_SYMBOL_GPL(__contpte_try_unfold);
 
-pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)
+pte_t contpte_ptep_get(const pte_t *ptep, pte_t orig_pte)
 {
 	/*
 	 * Gather access/dirty bits, which may be populated in any of the ptes
@@ -367,7 +372,7 @@ static inline bool contpte_is_consistent(pte_t pte, unsigned long pfn,
 			pgprot_val(prot) == pgprot_val(orig_prot);
 }
 
-pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
+pte_t contpte_ptep_get_lockless(const pte_t *orig_ptep)
 {
 	/*
 	 * The ptep_get_lockless() API requires us to read and return *orig_ptep
@@ -386,10 +391,10 @@ pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
 	 * because it is not part of a contpte range.
 	 */
 
Quoted 3 lines

arch/arm64/mm/contpte.c pte_t contpte_ptep_get_lockless(pte_t *orig_ptep) · line 394

Quoted 1 line
arch/arm64/mm/contpte.c +1 -0
+	const pte_t *ptep;

Nit but this is breaking the reverse xmas tree isn't it?

Yep, seems like I mistakenly broke the coding style here and in the contpte_align_down macro above (the \ is misaligned). I'll fix it up.

(I think Andrew isn't taking more material for next cycle, and while this should have no functional effect, it is very late and you have pushback, so probably no rush here...)

Signature · 1 line
Pedro
#4 lore raw

Re: [PATCH v2 1/6] mm/arm64: constify pte_get*() and contpte get logic

Quoted 11 lines · Pedro Falcato
Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 1 line · Pedro Falcato

None of the contpte code needs write access to the PTEs.

This seems like a very broad statement? Is that actually true? I see a bunch of ptep's that aren't const-ified, so you should explain why those couldn't be converted.

ACK. FTR, I think it would've been far clearer with "None of the contpte get code". There is of course contpte code that needs write access (e.g contpte_clear_full_ptes).

yeah that sounds better. Maybe just something like 'specify const for read-only users of pte *' or similar?

Quoted 42 lines
Quoted 33 lines

Also you add a new contpte_align_down() macro, you should mention that it the commit message, explain why it was needed.

In general more needed here :) it'd be ok if it was a truly trivial change that was all obvious but you're changing some pte_t *'s and not others so it's clearly not.

Quoted 24 lines
---
 arch/arm64/include/asm/pgtable.h | 10 +++++-----
 arch/arm64/mm/contpte.c          | 11 ++++++++---
 2 files changed, 13 insertions(+), 8 deletions(-)
arch/arm64/include/asm/pgtable.h +1 -1
index a2681d755358..043bc0649cee 100644
@@ -378,7 +378,7 @@ static inline void __set_pte(pte_t *ptep, pte_t pte)
 	__set_pte_complete(pte);
 }
 
-static inline pte_t __ptep_get(pte_t *ptep)
+static inline pte_t __ptep_get(const pte_t *ptep)
 {
 	return READ_ONCE(*ptep);
 }
@@ -1652,8 +1652,8 @@ extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte);
 extern void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte);
Quoted 8 lines

arch/arm64/include/asm/pgtable.h extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr, · line 1656

Quoted 4 lines
arch/arm64/include/asm/pgtable.h +2 -2
-extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte);
-extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep);
+extern pte_t contpte_ptep_get(const pte_t *ptep, pte_t orig_pte);
+extern pte_t contpte_ptep_get_lockless(const pte_t *orig_ptep);

This is (very) nitty but - not sure on the policy on extern's (Will/Catalin?) - but in mm we drop them when we touch the code since you don't need them these days :)

Quoted 1 line

*nods*. For what it's worth, this is new code that never needed extern.

Yup, this isn't a big deal anyway :)

Quoted 46 lines
Quoted 31 lines
Quoted 31 lines
arch/arm64/include/asm/pgtable.h +2 -2
 extern void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
 				pte_t *ptep, pte_t pte, unsigned int nr);
 extern void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
@@ -1732,7 +1732,7 @@ static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte)
  */
 
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	pte_t pte = __ptep_get(ptep);
 
@@ -1743,7 +1743,7 @@ static inline pte_t ptep_get(pte_t *ptep)
 }
 
 #define ptep_get_lockless ptep_get_lockless
-static inline pte_t ptep_get_lockless(pte_t *ptep)
+static inline pte_t ptep_get_lockless(const pte_t *ptep)
 {
 	pte_t pte = __ptep_get(ptep);
 
arch/arm64/mm/contpte.c +0 -0
index 2de12656b4d8..3a5d6937fb51 100644
@@ -26,6 +26,11 @@ static inline pte_t *contpte_align_down(pte_t *ptep)
 	return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
 }
 
Quoted 7 lines

arch/arm64/mm/contpte.c static inline pte_t *contpte_align_down(pte_t *ptep) · line 32

Quoted 4 lines
arch/arm64/mm/contpte.c +4 -0
+#define contpte_align_down(ptep)                                                       \
+	_Generic((ptep),                                                               \
+		 const pte_t *: (const pte_t *) contpte_align_down((pte_t *) (ptep)),  \
+		 pte_t *: contpte_align_down((pte_t *) ptep))

I really hate these _Generic() helper things. So ugly. And it's a pretty horrid cast now :(

Me too!

Quoted 4 lines

Was it not possible to const-ify further to just be able to constify contpte_align_down itself?

Quoted 7 lines

You can't do that because some callers want a pte_t* out of align_down, others want a const pte_t* out of align_down, depending on the param.

In A More Civilized Language(TM):

template <typename T>
T contpte_align_down(T ptr);

Now do it in rust ;)

Quoted 2 lines

:P

Well as per below you can just avoid the whole issue with:

#define contpte_align_down(ptep) \
PTR_ALIGN_DOWN(ptep, sizeof(*(ptep)) * CONT_PTES)

And avoid this mess? You're ultimately calling into a macro anyway.

The generic thing Seems like a lot of hassle for an align-down, and then you're forcing a macro and nasty casts anyway so I don't really see the downside of just making it a macro in general?

Quoted 6 lines
Quoted 3 lines

It also seems to contradict the claim that contpte doesn't need write-access to pte's since you're going to lengths to allow non-const pte_t * here.

Yep, I'll admit the commit message is confusing and crap. I'll flesh it out here.

Thanks!

Quoted 42 lines
Quoted 38 lines

You should cover off why this was necessary in the commit message as above.

Anyway PTR_ALIGN_DOWN() is already const-safe so couldn't you anyway just collapse this to:

#define contpte_align_down(ptep) \
PTR_ALIGN_DOWN(ptep, sizeof(*(ptep)) * CONT_PTES)

Then describe in the commit message why you need to handle both cases?

Quoted 26 lines
arch/arm64/mm/contpte.c +3 -2
+
 static inline pte_t *contpte_align_addr_ptep(unsigned long *start,
 					     unsigned long *end, pte_t *ptep,
 					     unsigned int nr)
@@ -310,7 +315,7 @@ void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
 }
 EXPORT_SYMBOL_GPL(__contpte_try_unfold);
 
-pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)
+pte_t contpte_ptep_get(const pte_t *ptep, pte_t orig_pte)
 {
 	/*
 	 * Gather access/dirty bits, which may be populated in any of the ptes
@@ -367,7 +372,7 @@ static inline bool contpte_is_consistent(pte_t pte, unsigned long pfn,
 			pgprot_val(prot) == pgprot_val(orig_prot);
 }
 
-pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
+pte_t contpte_ptep_get_lockless(const pte_t *orig_ptep)
 {
 	/*
 	 * The ptep_get_lockless() API requires us to read and return *orig_ptep
@@ -386,10 +391,10 @@ pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
 	 * because it is not part of a contpte range.
 	 */
 
Quoted 3 lines

arch/arm64/mm/contpte.c pte_t contpte_ptep_get_lockless(pte_t *orig_ptep) · line 394

Quoted 1 line
arch/arm64/mm/contpte.c +1 -0
+	const pte_t *ptep;

Nit but this is breaking the reverse xmas tree isn't it?

Quoted 6 lines

Yep, seems like I mistakenly broke the coding style here and in the contpte_align_down macro above (the \ is misaligned). I'll fix it up.

(I think Andrew isn't taking more material for next cycle, and while this should have no functional effect, it is very late and you have pushback, so probably no rush here...)

Yeah I assumed this was for 7.4 :) I mean the series is fine for 7.3 too AFAIC (with feedback addressed obviously).

Quoted 3 lines

--
Pedro

--
Cheers, Lorenzo

#5 lore raw

PATCH v2 2/6 parisc: Drop own implementations for ptep_get() and ptep_test_and_clear_young()

In reply to Pedro Falcato

From: Helge Deller <[email protected]>

Switch to the generic implementations, which are identical.

---
 arch/parisc/include/asm/pgtable.h | 20 --------------------
 1 file changed, 20 deletions(-)
arch/parisc/include/asm/pgtable.h +0 -20
index 467b8547ac8b..d92dee9fef3f 100644
@@ -432,25 +432,6 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)
 	return pte;
 }
 
-static inline pte_t ptep_get(pte_t *ptep)
-{
-	return READ_ONCE(*ptep);
-}
-#define ptep_get ptep_get
-
-static inline bool ptep_test_and_clear_young(struct vm_area_struct *vma,
-		unsigned long addr, pte_t *ptep)
-{
-	pte_t pte;
-
-	pte = ptep_get(ptep);
-	if (!pte_young(pte)) {
-		return false;
-	}
-	set_pte_at(vma->vm_mm, addr, ptep, pte_mkold(pte));
-	return true;
-}
-
 bool ptep_clear_flush_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep);
 pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep);
 
@@ -491,7 +472,6 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,
 #define HAVE_ARCH_UNMAPPED_AREA
 #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
 
-#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
 #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
Signature · 1 line
2.55.0
reviewer Reviewed-by on v2 2/6
#6 lore raw

Re: [PATCH v2 2/6] parisc: Drop own implementations for ptep_get() and ptep_test_and_clear_young()

Quoted 3 lines · Pedro Falcato

From: Helge Deller <[email protected]>

Switch to the generic implementations, which are identical.

You sure do like succinct commit messages :)

Maybe worth saying by dropping the __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG and ptep_get defines you get the generic versions from include/pgtable.h which are functionally identical.

(Being pedantic, they're not quite strictly identical as the ptep_test_and_clear_young() generic function does some weird unnecessary indirection with a local variable and the single {} is dropped etc.)

Quoted 5 lines

Nits above notwithstanding, LGTM so:

Quoted 43 lines
---
 arch/parisc/include/asm/pgtable.h | 20 --------------------
 1 file changed, 20 deletions(-)
arch/parisc/include/asm/pgtable.h +0 -21
index 467b8547ac8b..d92dee9fef3f 100644
@@ -432,25 +432,6 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)
 	return pte;
 }
 
-static inline pte_t ptep_get(pte_t *ptep)
-{
-	return READ_ONCE(*ptep);
-}
-#define ptep_get ptep_get
-
-static inline bool ptep_test_and_clear_young(struct vm_area_struct *vma,
-		unsigned long addr, pte_t *ptep)
-{
-	pte_t pte;
-
-	pte = ptep_get(ptep);
-	if (!pte_young(pte)) {
-		return false;
-	}
-	set_pte_at(vma->vm_mm, addr, ptep, pte_mkold(pte));
-	return true;
-}
-
 bool ptep_clear_flush_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep);
 pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep);
 
@@ -491,7 +472,6 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,
 #define HAVE_ARCH_UNMAPPED_AREA
 #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
 
-#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
 #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
--
Quoted 2 lines

2.55.0

--
Cheers, Lorenzo

#7 lore raw

Re: [PATCH v2 2/6] parisc: Drop own implementations for ptep_get() and ptep_test_and_clear_young()

Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Pedro Falcato

From: Helge Deller <[email protected]>

Switch to the generic implementations, which are identical.

You sure do like succinct commit messages :)

I didn't even write this one! See the From: :))

Quoted 5 lines

Maybe worth saying by dropping the __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG and ptep_get defines you get the generic versions from include/pgtable.h which are functionally identical.

I can touch it up though, if you insist.

Quoted 5 lines

(Being pedantic, they're not quite strictly identical as the ptep_test_and_clear_young() generic function does some weird unnecessary indirection with a local variable and the single {} is dropped etc.)

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

Nits above notwithstanding, LGTM so:

Thanks!

Signature · 1 line
Pedro
#8 lore raw

Re: [PATCH v2 2/6] parisc: Drop own implementations for ptep_get() and ptep_test_and_clear_young()

Quoted 9 lines · Pedro Falcato
Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Pedro Falcato

From: Helge Deller <[email protected]>

Switch to the generic implementations, which are identical.

You sure do like succinct commit messages :)

I didn't even write this one! See the From: :))

Lol well then feedback addressed to Helge ;)

Quoted 8 lines
Quoted 6 lines

Maybe worth saying by dropping the __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG and ptep_get defines you get the generic versions from include/pgtable.h which are functionally identical.

I can touch it up though, if you insist.

Yeah, I'm being pedantic here it's not vital to have this change :) but be nice if you could, I'm sure Helge probably wouldn't mind too much? :)

Quoted 11 lines
Quoted 11 lines

(Being pedantic, they're not quite strictly identical as the ptep_test_and_clear_young() generic function does some weird unnecessary indirection with a local variable and the single {} is dropped etc.)

Quoted 5 lines
Quoted 8 lines
Quoted 3 lines

Nits above notwithstanding, LGTM so:

Thanks!

--
Pedro

--
Cheers, Lorenzo

maintainer
#9 lore raw

Re: [PATCH v2 2/6] parisc: Drop own implementations for ptep_get() and ptep_test_and_clear_young()

Quoted 5 lines · Lorenzo Stoakes (ARM)
Quoted 4 lines · Pedro Falcato
Quoted 3 lines · Lorenzo Stoakes (ARM)
Quoted 2 lines · Pedro Falcato

From: Helge Deller <[email protected]>

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Switch to the generic implementations, which are identical.

You sure do like succinct commit messages :)

I didn't even write this one! See the From: :))

Lol well then feedback addressed to Helge ;)

Noted :-)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Maybe worth saying by dropping the __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG and ptep_get defines you get the generic versions from include/pgtable.h which are functionally identical.

Isn't that basically the same as:
"Switch to the generic implementations, which are identical." ;-)

Quoted 4 lines
Quoted 1 line

I can touch it up though, if you insist.

Yeah, I'm being pedantic here it's not vital to have this change :) but be nice if you could, I'm sure Helge probably wouldn't mind too much? :)

Yes, I'm fine with any cleanup/rephrasing of the commit message. Thanks!
Helge

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

(Being pedantic, they're not quite strictly identical as the ptep_test_and_clear_young() generic function does some weird unnecessary indirection with a local variable and the single {} is dropped etc.)

#10 lore raw

Re: [PATCH v2 2/6] parisc: Drop own implementations for ptep_get() and ptep_test_and_clear_young()

Quoted 16 lines · Helge Deller
Quoted 5 lines · Lorenzo Stoakes (ARM)
Quoted 4 lines · Pedro Falcato
Quoted 3 lines · Lorenzo Stoakes (ARM)
Quoted 2 lines · Pedro Falcato

From: Helge Deller <[email protected]>

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Switch to the generic implementations, which are identical.

You sure do like succinct commit messages :)

I didn't even write this one! See the From: :))

Lol well then feedback addressed to Helge ;)

Noted :-)

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Maybe worth saying by dropping the __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG and ptep_get defines you get the generic versions from include/pgtable.h which are functionally identical.

Isn't that basically the same as:
"Switch to the generic implementations, which are identical." ;-)

I am being _exceedingly_, possibly outrageously pedantic here :>)

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I can touch it up though, if you insist.

Yeah, I'm being pedantic here it's not vital to have this change :) but be nice if you could, I'm sure Helge probably wouldn't mind too much? :)

Yes, I'm fine with any cleanup/rephrasing of the commit message.

Thanks :)

Quoted 6 lines

Thanks!
Helge

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

(Being pedantic, they're not quite strictly identical as the ptep_test_and_clear_young() generic function does some weird unnecessary indirection with a local variable and the single {} is dropped etc.)

--
Cheers, Lorenzo

#11 lore raw

PATCH v2 3/6 mm/powerpc/8xx: constify ptep_get() argument

In reply to Pedro Falcato

There is no need for write access to the PTE.

---
 arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +1 -1
index e2ea8ba9f8ca..f2bab31040c7 100644
@@ -226,7 +226,7 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p
 
 #ifdef CONFIG_PPC_16K_PAGES
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	pte_basic_t val = READ_ONCE(ptep->pte);
 	pte_t pte = {val, val, val, val};
Signature · 1 line
2.55.0
reviewer Reviewed-by on v2 3/6
#12 lore raw

Re: [PATCH v2 3/6] mm/powerpc/8xx: constify ptep_get() argument

Quoted 3 lines · Pedro Falcato

There is no need for write access to the PTE.

With below addressed LGTM so:

Quoted 9 lines
---
 arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +0 -0
index e2ea8ba9f8ca..f2bab31040c7 100644

arch/powerpc/include/asm/nohash/32/pte-8xx.h static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p · line 232

Quoted 8 lines
arch/powerpc/include/asm/nohash/32/pte-8xx.h +1 -1
@@ -226,7 +226,7 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p
 
 #ifdef CONFIG_PPC_16K_PAGES
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	pte_basic_t val = READ_ONCE(ptep->pte);
 	pte_t pte = {val, val, val, val};

Worth making these const too (that {val, val, val, val} horrifies me btw :)?

Quoted 3 lines

--
2.55.0

--
Cheers, Lorenzo

#13 lore raw

Re: [PATCH v2 3/6] mm/powerpc/8xx: constify ptep_get() argument

Quoted 19 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Pedro Falcato

There is no need for write access to the PTE.

With below addressed LGTM so:

Quoted 10 lines
---
 arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +0 -0
index e2ea8ba9f8ca..f2bab31040c7 100644
@@ -226,7 +226,7 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p
 
Quoted 9 lines

arch/powerpc/include/asm/nohash/32/pte-8xx.h static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p · line 232

Quoted 7 lines
arch/powerpc/include/asm/nohash/32/pte-8xx.h +1 -1
 #ifdef CONFIG_PPC_16K_PAGES
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	pte_basic_t val = READ_ONCE(ptep->pte);
 	pte_t pte = {val, val, val, val};

Worth making these const too (that {val, val, val, val} horrifies me btw :)?

This is PPC code, so I don't know if they have any particular opinion here, but I could definitely do this.

(FWIW, it seems we're more aggressive in MM in doing this than other places in the kernel?)

Signature · 1 line
Pedro
#14 lore raw

Re: [PATCH v2 3/6] mm/powerpc/8xx: constify ptep_get() argument

Quoted 30 lines · Pedro Falcato
Quoted 19 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Pedro Falcato

There is no need for write access to the PTE.

With below addressed LGTM so:

Quoted 10 lines
---
 arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +0 -0
index e2ea8ba9f8ca..f2bab31040c7 100644
@@ -226,7 +226,7 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p
 
Quoted 9 lines

arch/powerpc/include/asm/nohash/32/pte-8xx.h static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p · line 232

Quoted 7 lines
arch/powerpc/include/asm/nohash/32/pte-8xx.h +1 -1
 #ifdef CONFIG_PPC_16K_PAGES
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	pte_basic_t val = READ_ONCE(ptep->pte);
 	pte_t pte = {val, val, val, val};

Worth making these const too (that {val, val, val, val} horrifies me btw :)?

Quoted 5 lines

This is PPC code, so I don't know if they have any particular opinion here, but I could definitely do this.

(FWIW, it seems we're more aggressive in MM in doing this than other places in the kernel?)

I mean I can't see why there'd be any objection given you're already const-ing here :)

Quoted 3 lines

--
Pedro

--
Cheers, Lorenzo

#15 lore raw

Re: [PATCH v2 3/6] mm/powerpc/8xx: constify ptep_get() argument

In reply to Lorenzo Stoakes (ARM)

Hi Lorenzo,

Le 04/08/2026 à 13:13, Lorenzo Stoakes (ARM) a écrit :

Quoted 19 lines · Pedro Falcato
Quoted 3 lines

There is no need for write access to the PTE.

With below addressed LGTM so:

Quoted 10 lines
---
  arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +0 -0
index e2ea8ba9f8ca..f2bab31040c7 100644
@@ -226,7 +226,7 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p
 
Quoted 9 lines

arch/powerpc/include/asm/nohash/32/pte-8xx.h static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p · line 232

Quoted 7 lines
arch/powerpc/include/asm/nohash/32/pte-8xx.h +1 -1
  #ifdef CONFIG_PPC_16K_PAGES
  #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
  {
  	pte_basic_t val = READ_ONCE(ptep->pte);
  	pte_t pte = {val, val, val, val};

Worth making these const too (that {val, val, val, val} horrifies me btw :)?

Any suggestion welcome.

powerpc 8xx page table is independant on page size. When you use 16k pages, depending on the address you hit the page for the first time, the HW assist page table walk will fetch one of four 4k cells in page table that need to be identical as they all four define the same 16k page. Not sure I'm clear.

Christophe

#16 lore raw

Re: [PATCH v2 3/6] mm/powerpc/8xx: constify ptep_get() argument

Quoted 32 lines · Christophe Leroy (CS GROUP)

Hi Lorenzo,

Le 04/08/2026 à 13:13, Lorenzo Stoakes (ARM) a écrit :

Quoted 19 lines · Pedro Falcato
Quoted 3 lines

There is no need for write access to the PTE.

With below addressed LGTM so:

Quoted 10 lines
---
  arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +0 -0
index e2ea8ba9f8ca..f2bab31040c7 100644
@@ -226,7 +226,7 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p
 
Quoted 9 lines

arch/powerpc/include/asm/nohash/32/pte-8xx.h static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p · line 232

Quoted 7 lines
arch/powerpc/include/asm/nohash/32/pte-8xx.h +1 -1
  #ifdef CONFIG_PPC_16K_PAGES
  #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
  {
  	pte_basic_t val = READ_ONCE(ptep->pte);
  	pte_t pte = {val, val, val, val};

Worth making these const too (that {val, val, val, val} horrifies me btw :)?

Quoted 1 line

Any suggestion welcome.

I'm being super nitty, all I mean is:

- pte_basic_t val = READ_ONCE(ptep->pte);
- pte_t pte = {val, val, val, val};
+ const pte_basic_t val = READ_ONCE(ptep->pte);
+ const pte_t pte = {val, val, val, val};

:)

Quoted 5 lines

powerpc 8xx page table is independant on page size. When you use 16k pages, depending on the address you hit the page for the first time, the HW assist page table walk will fetch one of four 4k cells in page table that need to be identical as they all four define the same 16k page. Not sure I'm clear.

No that's clear, thanks!

(I say 'horrifying' because I am looking into RCU page table freeing which this may complicate, though perhaps not in practice, to be continued :)

Quoted 3 lines

Christophe

--
Cheers, Lorenzo

#17 lore raw

Re: [PATCH v2 3/6] mm/powerpc/8xx: constify ptep_get() argument

Le 04/08/2026 à 14:59, Lorenzo Stoakes (ARM) a écrit :

Quoted 42 lines · Christophe Leroy (CS GROUP)
Quoted 32 lines

Hi Lorenzo,

Le 04/08/2026 à 13:13, Lorenzo Stoakes (ARM) a écrit :

Quoted 19 lines · Pedro Falcato
Quoted 3 lines

There is no need for write access to the PTE.

With below addressed LGTM so:

Quoted 10 lines
---
   arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +0 -0
index e2ea8ba9f8ca..f2bab31040c7 100644
@@ -226,7 +226,7 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p
 
Quoted 9 lines

arch/powerpc/include/asm/nohash/32/pte-8xx.h static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p · line 232

Quoted 7 lines
arch/powerpc/include/asm/nohash/32/pte-8xx.h +1 -1
   #ifdef CONFIG_PPC_16K_PAGES
   #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
   {
   	pte_basic_t val = READ_ONCE(ptep->pte);
   	pte_t pte = {val, val, val, val};

Worth making these const too (that {val, val, val, val} horrifies me btw :)?

Quoted 1 line

Any suggestion welcome.

I'm being super nitty, all I mean is:

(quoted diff) +2 -2
-   	pte_basic_t val = READ_ONCE(ptep->pte);
-  	pte_t pte = {val, val, val, val};
+   	const pte_basic_t val = READ_ONCE(ptep->pte);
+  	const pte_t pte = {val, val, val, val};
Quoted 1 line

:)

I'm fine with that, I was reacting on the "horrifying".

Quoted 11 lines
Quoted 6 lines

powerpc 8xx page table is independant on page size. When you use 16k pages, depending on the address you hit the page for the first time, the HW assist page table walk will fetch one of four 4k cells in page table that need to be identical as they all four define the same 16k page. Not sure I'm clear.

No that's clear, thanks!

(I say 'horrifying' because I am looking into RCU page table freeing which this may complicate, though perhaps not in practice, to be continued :)

A few more details here if needed: 55c8fc3f4930 ("powerpc/8xx:
reintroduce 16K pages with HW assistance")

Christophe

#18 lore raw

Re: [PATCH v2 3/6] mm/powerpc/8xx: constify ptep_get() argument

In reply to Lorenzo Stoakes (ARM)

Le 04/08/2026 à 14:59, Lorenzo Stoakes (ARM) a écrit :

Quoted 42 lines · Christophe Leroy (CS GROUP)
Quoted 32 lines

Hi Lorenzo,

Le 04/08/2026 à 13:13, Lorenzo Stoakes (ARM) a écrit :

Quoted 19 lines · Pedro Falcato
Quoted 3 lines

There is no need for write access to the PTE.

With below addressed LGTM so:

Quoted 10 lines
---
   arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +0 -0
index e2ea8ba9f8ca..f2bab31040c7 100644
@@ -226,7 +226,7 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p
 
Quoted 9 lines

arch/powerpc/include/asm/nohash/32/pte-8xx.h static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p · line 232

Quoted 7 lines
arch/powerpc/include/asm/nohash/32/pte-8xx.h +1 -1
   #ifdef CONFIG_PPC_16K_PAGES
   #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
   {
   	pte_basic_t val = READ_ONCE(ptep->pte);
   	pte_t pte = {val, val, val, val};

Worth making these const too (that {val, val, val, val} horrifies me btw :)?

Quoted 1 line

Any suggestion welcome.

I'm being super nitty, all I mean is:

(quoted diff) +2 -2
-   	pte_basic_t val = READ_ONCE(ptep->pte);
-  	pte_t pte = {val, val, val, val};
+   	const pte_basic_t val = READ_ONCE(ptep->pte);
+  	const pte_t pte = {val, val, val, val};
Quoted 1 line

:)

I'm fine with that, I was reacting on the "horrifying".

Quoted 11 lines
Quoted 6 lines

powerpc 8xx page table is independant on page size. When you use 16k pages, depending on the address you hit the page for the first time, the HW assist page table walk will fetch one of four 4k cells in page table that need to be identical as they all four define the same 16k page. Not sure I'm clear.

No that's clear, thanks!

(I say 'horrifying' because I am looking into RCU page table freeing which this may complicate, though perhaps not in practice, to be continued :)

A few more details here if needed: 55c8fc3f4930 ("powerpc/8xx: reintroduce 16K pages with HW assistance")

Christophe

#19 lore raw

PATCH v2 4/6 mm/s390: constify ptep_get() argument

In reply to Pedro Falcato

It's a simple READ_ONCE() dereference, therefore it can be const.

---
 arch/s390/include/asm/pgtable.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/include/asm/pgtable.h +1 -1
index e882663a58e7..ec331ec92943 100644
@@ -986,7 +986,7 @@ static inline void set_pte(pte_t *ptep, pte_t pte)
 }
 
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	return READ_ONCE(*ptep);
 }
Signature · 1 line
2.55.0
reviewer Reviewed-by on v2 4/6
#20 lore raw

Re: [PATCH v2 4/6] mm/s390: constify ptep_get() argument

Quoted 3 lines · Pedro Falcato

It's a simple READ_ONCE() dereference, therefore it can be const.

Even I can't be pedantic about this one... :) so:

Quoted 18 lines
---
 arch/s390/include/asm/pgtable.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/include/asm/pgtable.h +1 -2
index e882663a58e7..ec331ec92943 100644
@@ -986,7 +986,7 @@ static inline void set_pte(pte_t *ptep, pte_t pte)
 }
 
 #define ptep_get ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	return READ_ONCE(*ptep);
 }
--
Quoted 2 lines

2.55.0

--
Cheers, Lorenzo

#21 lore raw

PATCH v2 5/6 mm: constify generic pte_get*()

In reply to Pedro Falcato

None of the helpers need write access to the PTE. Constifying the param allows for const typesafety.

---
 include/linux/pgtable.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/pgtable.h +3 -3
index 8c093c119e5a..dc418553e57a 100644
@@ -490,7 +490,7 @@ static inline int pudp_set_access_flags(struct vm_area_struct *vma,
 #endif
 
 #ifndef ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	return READ_ONCE(*ptep);
 }
@@ -739,7 +739,7 @@ static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
  * present bit set *unless* it is 'l'. Because get_user_pages_fast() only
  * operates on present ptes we're safe.
  */
-static inline pte_t ptep_get_lockless(pte_t *ptep)
+static inline pte_t ptep_get_lockless(const pte_t *ptep)
 {
 	pte_t pte;
 
@@ -777,7 +777,7 @@ static inline pmd_t pmdp_get_lockless(pmd_t *pmdp)
  * We require that the PTE can be read atomically.
  */
 #ifndef ptep_get_lockless
-static inline pte_t ptep_get_lockless(pte_t *ptep)
+static inline pte_t ptep_get_lockless(const pte_t *ptep)
 {
 	return ptep_get(ptep);
 }
Signature · 1 line
2.55.0
reviewer Reviewed-by on v2 5/6
#22 lore raw

Re: [PATCH v2 5/6] mm: constify generic pte_get*()

Quoted 2 lines · Pedro Falcato

None of the helpers need write access to the PTE. Constifying the param allows for const typesafety.

constity?

Quoted 2 lines

LGTM, so:

Quoted 36 lines
---
 include/linux/pgtable.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/pgtable.h +3 -4
index 8c093c119e5a..dc418553e57a 100644
@@ -490,7 +490,7 @@ static inline int pudp_set_access_flags(struct vm_area_struct *vma,
 #endif
 
 #ifndef ptep_get
-static inline pte_t ptep_get(pte_t *ptep)
+static inline pte_t ptep_get(const pte_t *ptep)
 {
 	return READ_ONCE(*ptep);
 }
@@ -739,7 +739,7 @@ static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
  * present bit set *unless* it is 'l'. Because get_user_pages_fast() only
  * operates on present ptes we're safe.
  */
-static inline pte_t ptep_get_lockless(pte_t *ptep)
+static inline pte_t ptep_get_lockless(const pte_t *ptep)
 {
 	pte_t pte;
 
@@ -777,7 +777,7 @@ static inline pmd_t pmdp_get_lockless(pmd_t *pmdp)
  * We require that the PTE can be read atomically.
  */
 #ifndef ptep_get_lockless
-static inline pte_t ptep_get_lockless(pte_t *ptep)
+static inline pte_t ptep_get_lockless(const pte_t *ptep)
 {
 	return ptep_get(ptep);
 }
--
Quoted 2 lines

2.55.0

--
Cheers, Lorenzo

#24 lore raw

PATCH v2 6/6 mm: constify the pte_offset_map_ro_nolock() return value

In reply to Pedro Falcato

Constify the pte_t * retval from pte_offset_map_ro_nolock(), for which it is already pledged that accesses must be read-only. With it, convert the three treewide users to use const pte_t *.

khugepaged passes the result right down to fault code (do_swap_page()). This leads to a complicated set of conditions that, in order to be correct, must not install anything into *vmf->pte. This is not trivial to work around in fault code, and as such just trivially cast to non-const pte_t* in the meantime.

The other users are far more trivial and the conversion is equally trivially simple.

---
 arch/powerpc/mm/pgtable.c | 2 +-
 include/linux/mm.h        | 4 ++--
 include/linux/pgtable.h   | 2 +-
 mm/filemap.c              | 2 +-
 mm/khugepaged.c           | 2 +-
 mm/pgtable-generic.c      | 4 ++--
 6 files changed, 8 insertions(+), 8 deletions(-)
arch/powerpc/mm/pgtable.c +1 -1
index a9be337be3e4..e29db41b6043 100644
@@ -390,7 +390,7 @@ void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
 	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
-	pte_t *pte;
+	const pte_t *pte;
 	spinlock_t *ptl;
 
 	if (mm == &init_mm)
include/linux/mm.h +2 -2
index 7fabe6c66b4b..acf5a5e31d34 100644
@@ -3885,8 +3885,8 @@ static inline pte_t *pte_offset_map(pmd_t *pmd, unsigned long addr)
 pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd,
 			   unsigned long addr, spinlock_t **ptlp);
 
-pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
-				unsigned long addr, spinlock_t **ptlp);
+const pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
+				      unsigned long addr, spinlock_t **ptlp);
 pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd,
 				unsigned long addr, pmd_t *pmdvalp,
 				spinlock_t **ptlp);
include/linux/pgtable.h +1 -1
index dc418553e57a..dd51e722c535 100644
@@ -112,7 +112,7 @@ static inline pte_t *__pte_map(pmd_t *pmd, unsigned long address)
 {
 	return pte_offset_kernel(pmd, address);
 }
-static inline void pte_unmap(pte_t *pte)
+static inline void pte_unmap(const pte_t *pte)
 {
 	rcu_read_unlock();
 }
mm/filemap.c +1 -1
index 6afec636881f..af5d3fcd1b05 100644
@@ -3490,7 +3490,7 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	vm_fault_t ret = 0;
-	pte_t *ptep;
+	const pte_t *ptep;
 
 	/*
 	 * We might have COW'ed a pagecache folio and might now have an mlocked
mm/khugepaged.c +1 -1
index b237f6e7662a..09efac93a8c6 100644
@@ -1170,7 +1170,7 @@ static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm,
 			 * Here the ptl is only used to check pte_same() in
 			 * do_swap_page(), so readonly version is enough.
 			 */
-			pte = pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);
+			pte = (pte_t *) pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);
 			if (!pte) {
 				mmap_read_unlock(mm);
 				result = SCAN_NO_PTE_TABLE;
mm/pgtable-generic.c +2 -2
index b91b1a98029c..2cfc6e608ef4 100644
@@ -308,8 +308,8 @@ pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
 	return NULL;
 }
 
-pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
-				unsigned long addr, spinlock_t **ptlp)
+const pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
+				      unsigned long addr, spinlock_t **ptlp)
 {
 	pmd_t pmdval;
 	pte_t *pte;
Signature · 1 line
2.55.0
reviewer Reviewed-by on v2 6/6
#25 lore raw

Re: [PATCH v2 6/6] mm: constify the pte_offset_map_ro_nolock() return value

Quoted 4 lines · Pedro Falcato

Constify the pte_t * retval from pte_offset_map_ro_nolock(), for which it is already pledged that accesses must be read-only. With it, convert the three treewide users to use const pte_t *.

Quoted 8 lines

khugepaged passes the result right down to fault code (do_swap_page()). This leads to a complicated set of conditions that, in order to be correct, must not install anything into *vmf->pte. This is not trivial to work around in fault code, and as such just trivially cast to non-const pte_t* in the meantime.

The other users are far more trivial and the conversion is equally trivially simple.

Ah finally more words! :)

Quoted 2 lines

With comment updated as below and nits addressed, LGTM so:

Quoted 42 lines
---
 arch/powerpc/mm/pgtable.c | 2 +-
 include/linux/mm.h        | 4 ++--
 include/linux/pgtable.h   | 2 +-
 mm/filemap.c              | 2 +-
 mm/khugepaged.c           | 2 +-
 mm/pgtable-generic.c      | 4 ++--
 6 files changed, 8 insertions(+), 8 deletions(-)
arch/powerpc/mm/pgtable.c +1 -1
index a9be337be3e4..e29db41b6043 100644
@@ -390,7 +390,7 @@ void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
 	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
-	pte_t *pte;
+	const pte_t *pte;
 	spinlock_t *ptl;
 
 	if (mm == &init_mm)
include/linux/mm.h +2 -2
index 7fabe6c66b4b..acf5a5e31d34 100644
@@ -3885,8 +3885,8 @@ static inline pte_t *pte_offset_map(pmd_t *pmd, unsigned long addr)
 pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd,
 			   unsigned long addr, spinlock_t **ptlp);
 
-pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
-				unsigned long addr, spinlock_t **ptlp);
+const pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
+				      unsigned long addr, spinlock_t **ptlp);
 pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd,
 				unsigned long addr, pmd_t *pmdvalp,
 				spinlock_t **ptlp);
include/linux/pgtable.h +0 -0
index dc418553e57a..dd51e722c535 100644

include/linux/pgtable.h static inline pte_t *__pte_map(pmd_t *pmd, unsigned long address) · line 115

Quoted 5 lines
include/linux/pgtable.h +1 -1
@@ -112,7 +112,7 @@ static inline pte_t *__pte_map(pmd_t *pmd, unsigned long address)
 {
 	return pte_offset_kernel(pmd, address);
 }
-static inline void pte_unmap(pte_t *pte)
+static inline void pte_unmap(const pte_t *pte)

I was going to question this based on whether the contract holds for CONFIG_HIGHPTE but actually:

#define pte_unmap(pte)	do {	\
	kunmap_local((pte));	\
	rcu_read_unlock();	\
} while (0)

#define kunmap_local(__addr) \

do {								\
	BUILD_BUG_ON(__same_type((__addr), struct page *));	\
	__kunmap_local(__addr);					\
} while (0)

static inline void __kunmap_local(const void *vaddr) <-- const!

{
	kunmap_local_indexed(vaddr);
}

So nice (CONFIG_HIGHPTE is going to go away at some point though, right? I hope... :)

Quoted 21 lines
include/linux/pgtable.h +0 -0
 {
 	rcu_read_unlock();
 }
mm/filemap.c +1 -1
index 6afec636881f..af5d3fcd1b05 100644
@@ -3490,7 +3490,7 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	vm_fault_t ret = 0;
-	pte_t *ptep;
+	const pte_t *ptep;
 
 	/*
 	 * We might have COW'ed a pagecache folio and might now have an mlocked
mm/khugepaged.c +0 -0
index b237f6e7662a..09efac93a8c6 100644

mm/khugepaged.c static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm, · line 1173

Quoted 5 lines
mm/khugepaged.c +1 -1
@@ -1170,7 +1170,7 @@ static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm,
 			 * Here the ptl is only used to check pte_same() in
 			 * do_swap_page(), so readonly version is enough.
 			 */
-			pte = pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);
+			pte = (pte_t *) pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);

Hmm yeah this is nasty, but you explain why in the commit message. Could you extend the comment to explain it?

Quoted 8 lines
mm/khugepaged.c +0 -0
 			if (!pte) {
 				mmap_read_unlock(mm);
 				result = SCAN_NO_PTE_TABLE;
mm/pgtable-generic.c +0 -0
index b91b1a98029c..2cfc6e608ef4 100644

mm/pgtable-generic.c pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) · line 311

Quoted 6 lines
mm/pgtable-generic.c +1 -2
@@ -308,8 +308,8 @@ pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
 	return NULL;
 }
 
-pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
-				unsigned long addr, spinlock_t **ptlp)
+const pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,

Can pmd be const too?

Quoted 7 lines
mm/pgtable-generic.c +1 -1
+				      unsigned long addr, spinlock_t **ptlp)
 {
 	pmd_t pmdval;
 	pte_t *pte;
--

2.55.0

--
Cheers, Lorenzo

#26 lore raw

Re: [PATCH v2 6/6] mm: constify the pte_offset_map_ro_nolock() return value

Quoted 11 lines · Lorenzo Stoakes (ARM)
Quoted 10 lines · Pedro Falcato

Constify the pte_t * retval from pte_offset_map_ro_nolock(), for which it is already pledged that accesses must be read-only. With it, convert the three treewide users to use const pte_t *.

khugepaged passes the result right down to fault code (do_swap_page()). This leads to a complicated set of conditions that, in order to be correct, must not install anything into *vmf->pte. This is not trivial to work around in fault code, and as such just trivially cast to non-const pte_t* in the meantime.

Quoted 4 lines
Quoted 2 lines

The other users are far more trivial and the conversion is equally trivially simple.

Ah finally more words! :)

:)

Quoted 93 lines
Quoted 3 lines

With comment updated as below and nits addressed, LGTM so:

Quoted 42 lines
---
 arch/powerpc/mm/pgtable.c | 2 +-
 include/linux/mm.h        | 4 ++--
 include/linux/pgtable.h   | 2 +-
 mm/filemap.c              | 2 +-
 mm/khugepaged.c           | 2 +-
 mm/pgtable-generic.c      | 4 ++--
 6 files changed, 8 insertions(+), 8 deletions(-)
arch/powerpc/mm/pgtable.c +1 -1
index a9be337be3e4..e29db41b6043 100644
@@ -390,7 +390,7 @@ void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
 	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
-	pte_t *pte;
+	const pte_t *pte;
 	spinlock_t *ptl;
 
 	if (mm == &init_mm)
include/linux/mm.h +2 -2
index 7fabe6c66b4b..acf5a5e31d34 100644
@@ -3885,8 +3885,8 @@ static inline pte_t *pte_offset_map(pmd_t *pmd, unsigned long addr)
 pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd,
 			   unsigned long addr, spinlock_t **ptlp);
 
-pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
-				unsigned long addr, spinlock_t **ptlp);
+const pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
+				      unsigned long addr, spinlock_t **ptlp);
 pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd,
 				unsigned long addr, pmd_t *pmdvalp,
 				spinlock_t **ptlp);
include/linux/pgtable.h +0 -0
index dc418553e57a..dd51e722c535 100644

include/linux/pgtable.h static inline pte_t *__pte_map(pmd_t *pmd, unsigned long address) · line 115

Quoted 5 lines
include/linux/pgtable.h +1 -1
@@ -112,7 +112,7 @@ static inline pte_t *__pte_map(pmd_t *pmd, unsigned long address)
 {
 	return pte_offset_kernel(pmd, address);
 }
-static inline void pte_unmap(pte_t *pte)
+static inline void pte_unmap(const pte_t *pte)

I was going to question this based on whether the contract holds for CONFIG_HIGHPTE but actually:

#define pte_unmap(pte)	do {	\
	kunmap_local((pte));	\
	rcu_read_unlock();	\
} while (0)

#define kunmap_local(__addr) \

do {								\
	BUILD_BUG_ON(__same_type((__addr), struct page *));	\
	__kunmap_local(__addr);					\
} while (0)

static inline void __kunmap_local(const void *vaddr) <-- const!

{
	kunmap_local_indexed(vaddr);
}

So nice (CONFIG_HIGHPTE is going to go away at some point though, right? I hope... :)

Quoted 14 lines
include/linux/pgtable.h +0 -0
 {
 	rcu_read_unlock();
 }
mm/filemap.c +1 -1
index 6afec636881f..af5d3fcd1b05 100644
@@ -3490,7 +3490,7 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	vm_fault_t ret = 0;
-	pte_t *ptep;
+	const pte_t *ptep;
 
Quoted 15 lines

mm/khugepaged.c static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm, · line 1173

Quoted 12 lines
mm/filemap.c +0 -0
 	/*
 	 * We might have COW'ed a pagecache folio and might now have an mlocked
mm/khugepaged.c +1 -1
index b237f6e7662a..09efac93a8c6 100644
@@ -1170,7 +1170,7 @@ static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm,
 			 * Here the ptl is only used to check pte_same() in
 			 * do_swap_page(), so readonly version is enough.
 			 */
-			pte = pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);
+			pte = (pte_t *) pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);

Hmm yeah this is nasty, but you explain why in the commit message. Could you extend the comment to explain it?

Yep, I'll do it.

Quoted 12 lines
Quoted 12 lines
mm/khugepaged.c +0 -0
 			if (!pte) {
 				mmap_read_unlock(mm);
 				result = SCAN_NO_PTE_TABLE;
mm/pgtable-generic.c +0 -0
index b91b1a98029c..2cfc6e608ef4 100644
@@ -308,8 +308,8 @@ pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
 	return NULL;
 }
 
Quoted 5 lines

mm/pgtable-generic.c pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) · line 311

Quoted 3 lines
mm/pgtable-generic.c +1 -2
-pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
-				unsigned long addr, spinlock_t **ptlp)
+const pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,

Can pmd be const too?

I was going to say "yes but then pmdp_get() also needs to be constfified" but actually no, it can't:

pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) {
        if (unlikely(pmd_bad(pmdval))) {
                pmd_clear_bad(pmd);
                goto nomap;
        }
}

so PTE mapping actually needs to write to the pmdp if the pmd looks bad. Tricky stuff :)

Signature · 1 line
Pedro
#27 lore raw

Re: [PATCH v2 6/6] mm: constify the pte_offset_map_ro_nolock() return value

Le 04/08/2026 à 21:22, Pedro Falcato a écrit :

Quoted 30 lines · Lorenzo Stoakes (ARM)
Quoted 13 lines
Quoted 13 lines

  			if (!pte) {
  				mmap_read_unlock(mm);
  				result = SCAN_NO_PTE_TABLE;
mm/pgtable-generic.c +0 -0
index b91b1a98029c..2cfc6e608ef4 100644
@@ -308,8 +308,8 @@ pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
  	return NULL;
  }
 
Quoted 5 lines

mm/pgtable-generic.c pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) · line 311

Quoted 3 lines
mm/pgtable-generic.c +1 -2
-pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
-				unsigned long addr, spinlock_t **ptlp)
+const pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,

Can pmd be const too?

I was going to say "yes but then pmdp_get() also needs to be constfified" but actually no, it can't:

pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) {
         if (unlikely(pmd_bad(pmdval))) {
                 pmd_clear_bad(pmd);
                 goto nomap;
         }
}
Quoted 2 lines

so PTE mapping actually needs to write to the pmdp if the pmd looks bad. Tricky stuff :)

But if pmd is const, can it be bad at all ?

Christophe

#28 lore raw

Re: [PATCH v2 6/6] mm: constify the pte_offset_map_ro_nolock() return value

Quoted 33 lines · Christophe Leroy (CS GROUP)

Le 04/08/2026 à 21:22, Pedro Falcato a écrit :

Quoted 30 lines · Lorenzo Stoakes (ARM)
Quoted 13 lines
Quoted 13 lines

  			if (!pte) {
  				mmap_read_unlock(mm);
  				result = SCAN_NO_PTE_TABLE;
mm/pgtable-generic.c +0 -0
index b91b1a98029c..2cfc6e608ef4 100644
@@ -308,8 +308,8 @@ pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
  	return NULL;
  }
 
Quoted 5 lines

mm/pgtable-generic.c pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) · line 311

Quoted 3 lines
mm/pgtable-generic.c +1 -2
-pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
-				unsigned long addr, spinlock_t **ptlp)
+const pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,

Can pmd be const too?

I was going to say "yes but then pmdp_get() also needs to be constfified" but actually no, it can't:

pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) {
         if (unlikely(pmd_bad(pmdval))) {
                 pmd_clear_bad(pmd);
                 goto nomap;
         }
}
Quoted 4 lines
Quoted 2 lines

so PTE mapping actually needs to write to the pmdp if the pmd looks bad. Tricky stuff :)

But if pmd is const, can it be bad at all ?

Yes, you just need a stray write or a bit of memory corruption and it can go bad. And then we need to do clear_bad() :)

(it's unclear to me whether this is actually common or useful enough these days; the way this was explained to me, page tables can be best-effort redundant; but it's not like we know clearing the whole range is ok, and the way pmd_ERROR, etc work they don't even communicate to userspace what happened, unlike normal hwpoison mechanisms)

Signature · 1 line
Pedro
Reviewed-by on the v2 series
#29 lore raw
In reply to Pedro Falcato
Quoted 13 lines · Pedro Falcato

Since forever, MM code has thrown pte_t * around with no concern for const safety, or typesafety of any kind. This is confusing. Attempt to address it by:
1) Making sure pte_get*() helpers can cope with const pte_t * arguments
2) Constifying the pte_offset_map_ro_nolock() return type, which by definition already pledges that users will not write to it.

These two simple steps were already able to uncover code smell from khugepaged + do_swap_page().

Separate steps could include introducing pte_offset_map_ro_lock() for more widespread usage of this.

Quoted 7 lines

Benefits of this include less confusion and better type-safety. It could also futurely aid in efforts such as [0] which may want semantic annotation of these accesses.

Based on mm-unstable and compile-tested on a handful of architectures.

No functional changes intended.

I've reviewed the entire series. Hence:

Quoted 65 lines

[CC list editorially trimmed for brevity reasons; apologies if you're not on it]

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
Cc Helge Deller <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Jan Kara <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Lance Yang <[email protected]>
Cc Usama Arif <[email protected]>
Cc Kevin Brodsky <[email protected]>
Cc Muhammad Usama Anjum <[email protected]>

v2:
- Small fixups on the arm64 side
- Re-order patches in a way such that bisection is preserved
- Pick up Helge's patch dropping parisc ptep_get()
- Constify s390's ptep_get() as well

Helge Deller (1):
parisc: Drop own implementations for ptep_get() and
ptep_test_and_clear_young()

Pedro Falcato (5):
mm/arm64: constify pte_get*() and contpte get logic mm/powerpc/8xx: constify ptep_get() argument mm/s390: constify ptep_get() argument
mm: constify generic pte_get*()
mm: constify the pte_offset_map_ro_nolock() return value

 arch/arm64/include/asm/pgtable.h             | 10 +++++-----
 arch/arm64/mm/contpte.c                      | 11 ++++++++---
 arch/parisc/include/asm/pgtable.h            | 20 --------------------
 arch/powerpc/include/asm/nohash/32/pte-8xx.h |  2 +-
 arch/powerpc/mm/pgtable.c                    |  2 +-
 arch/s390/include/asm/pgtable.h              |  2 +-
 include/linux/mm.h                           |  4 ++--
 include/linux/pgtable.h                      |  8 ++++----
 mm/filemap.c                                 |  2 +-
 mm/khugepaged.c                              |  2 +-
 mm/pgtable-generic.c                         |  4 ++--
 11 files changed, 26 insertions(+), 41 deletions(-)
Signature · 2 lines
Thanks,
Usama
reviewer Reviewed-by on the v2 series
#30 lore raw
In reply to Pedro Falcato

Le 03/08/2026 à 18:43, Pedro Falcato a écrit :

Quoted 3 lines

Since forever, MM code has thrown pte_t * around with no concern for const safety, or typesafety of any kind. This is confusing. Attempt to address it by:

What do you mean by "typesafety of any kind" ?

On powerpc64, pte_t is a struct so you can't play-up too much with it.

On powerpc32, pte_t is a long int because having it as a struct is counter-performant, but we have it as a struct when __CHECKER__ is defined, ie when doing a sparse check with 'make C=2'.

Quoted 10 lines

1) Making sure pte_get*() helpers can cope with const pte_t * arguments
2) Constifying the pte_offset_map_ro_nolock() return type, which by definition already pledges that users will not write to it.

These two simple steps were already able to uncover code smell from khugepaged + do_swap_page().

Separate steps could include introducing pte_offset_map_ro_lock() for more widespread usage of this.

Quoted 8 lines

Benefits of this include less confusion and better type-safety. It could also futurely aid in efforts such as [0] which may want semantic annotation of these accesses.

Based on mm-unstable and compile-tested on a handful of architectures.

No functional changes intended.

For the series,

Quoted 64 lines

[CC list editorially trimmed for brevity reasons; apologies if you're not on it]

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
Cc Helge Deller <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Jan Kara <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Lance Yang <[email protected]>
Cc Usama Arif <[email protected]>
Cc Kevin Brodsky <[email protected]>
Cc Muhammad Usama Anjum <[email protected]>

v2:
- Small fixups on the arm64 side
- Re-order patches in a way such that bisection is preserved
- Pick up Helge's patch dropping parisc ptep_get()
- Constify s390's ptep_get() as well

Helge Deller (1):
parisc: Drop own implementations for ptep_get() and
ptep_test_and_clear_young()

Pedro Falcato (5):
mm/arm64: constify pte_get*() and contpte get logic mm/powerpc/8xx: constify ptep_get() argument mm/s390: constify ptep_get() argument
mm: constify generic pte_get*()
mm: constify the pte_offset_map_ro_nolock() return value

  arch/arm64/include/asm/pgtable.h             | 10 +++++-----
  arch/arm64/mm/contpte.c                      | 11 ++++++++---
  arch/parisc/include/asm/pgtable.h            | 20 --------------------
  arch/powerpc/include/asm/nohash/32/pte-8xx.h |  2 +-
  arch/powerpc/mm/pgtable.c                    |  2 +-
  arch/s390/include/asm/pgtable.h              |  2 +-
  include/linux/mm.h                           |  4 ++--
  include/linux/pgtable.h                      |  8 ++++----
  mm/filemap.c                                 |  2 +-
  mm/khugepaged.c                              |  2 +-
  mm/pgtable-generic.c                         |  4 ++--
  11 files changed, 26 insertions(+), 41 deletions(-)
#31 lore raw
In reply to Pedro Falcato
Quoted 13 lines · Pedro Falcato

Since forever, MM code has thrown pte_t * around with no concern for const safety, or typesafety of any kind. This is confusing. Attempt to address it by:
1) Making sure pte_get*() helpers can cope with const pte_t * arguments
2) Constifying the pte_offset_map_ro_nolock() return type, which by definition already pledges that users will not write to it.

These two simple steps were already able to uncover code smell from khugepaged + do_swap_page().

Separate steps could include introducing pte_offset_map_ro_lock() for more widespread usage of this.

Quoted 7 lines

Benefits of this include less confusion and better type-safety. It could also futurely aid in efforts such as [0] which may want semantic annotation of these accesses.

Based on mm-unstable and compile-tested on a handful of architectures.

No functional changes intended.

Even though the pte accesses should always be type-safe, this series does not really address the problem completely and instead changes things only for a small set of pte access sites. So just wondering how much beneficial this series really is ?

Quoted 68 lines

[CC list editorially trimmed for brevity reasons; apologies if you're not on it]

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
Cc Helge Deller <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Jan Kara <[email protected]>
Cc Zi Yan <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Lance Yang <[email protected]>
Cc Usama Arif <[email protected]>
Cc Kevin Brodsky <[email protected]>
Cc Muhammad Usama Anjum <[email protected]>

v2:
- Small fixups on the arm64 side
- Re-order patches in a way such that bisection is preserved
- Pick up Helge's patch dropping parisc ptep_get()
- Constify s390's ptep_get() as well

Helge Deller (1):
parisc: Drop own implementations for ptep_get() and
ptep_test_and_clear_young()

Pedro Falcato (5):
mm/arm64: constify pte_get*() and contpte get logic mm/powerpc/8xx: constify ptep_get() argument mm/s390: constify ptep_get() argument
mm: constify generic pte_get*()
mm: constify the pte_offset_map_ro_nolock() return value

 arch/arm64/include/asm/pgtable.h             | 10 +++++-----
 arch/arm64/mm/contpte.c                      | 11 ++++++++---
 arch/parisc/include/asm/pgtable.h            | 20 --------------------
 arch/powerpc/include/asm/nohash/32/pte-8xx.h |  2 +-
 arch/powerpc/mm/pgtable.c                    |  2 +-
 arch/s390/include/asm/pgtable.h              |  2 +-
 include/linux/mm.h                           |  4 ++--
 include/linux/pgtable.h                      |  8 ++++----
 mm/filemap.c                                 |  2 +-
 mm/khugepaged.c                              |  2 +-
 mm/pgtable-generic.c                         |  4 ++--
 11 files changed, 26 insertions(+), 41 deletions(-)
Signature · 1 line
2.55.0
#32 lore raw
Quoted 18 lines · Anshuman Khandual
Quoted 17 lines · Pedro Falcato

Since forever, MM code has thrown pte_t * around with no concern for const safety, or typesafety of any kind. This is confusing. Attempt to address it by:
1) Making sure pte_get*() helpers can cope with const pte_t * arguments
2) Constifying the pte_offset_map_ro_nolock() return type, which by definition already pledges that users will not write to it.

These two simple steps were already able to uncover code smell from khugepaged + do_swap_page().

Separate steps could include introducing pte_offset_map_ro_lock() for more widespread usage of this.

Benefits of this include less confusion and better type-safety. It could also futurely aid in efforts such as [0] which may want semantic annotation of these accesses.

Quoted 8 lines
Quoted 3 lines

Based on mm-unstable and compile-tested on a handful of architectures.

No functional changes intended.

Even though the pte accesses should always be type-safe, this series does not really address the problem completely and instead changes things only for a small set of pte access sites. So just wondering how much beneficial this series really is ?

This series is meant to be a small step in the right direction (while feeling out what the community thinks, which seems to be receptive). The obvious next steps would be to introduce some sort of

const pte_t *pte_offset_map_ro_lock(struct mm_struct *mm, pmd_t *pmd,

			   unsigned long addr, spinlock_t **ptlp);

and use it in more places. That will obviously involve a bit of churn.

Signature · 1 line
Pedro