[PATCH 2/2] mm/madvise: Use set_pte() to write page tables

Samuel Holland posted 2 patches 5 days, 8 hours ago
[PATCH 2/2] mm/madvise: Use set_pte() to write page tables
Posted by Samuel Holland 5 days, 8 hours ago
Generic code must always use the architecture-provided helper function
to write page tables.

Fixes: 662df3e5c376 ("mm: madvise: implement lightweight guard page mechanism")
Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
---

 mm/madvise.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index b617b1be0f535..4da9c32f8738a 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -1114,7 +1114,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));
 	(*nr_pages)++;
 
 	return 0;
-- 
2.47.2
Re: [PATCH 2/2] mm/madvise: Use set_pte() to write page tables
Posted by kernel test robot 4 days, 17 hours ago
Hi Samuel,

kernel test robot noticed the following build errors:

[auto build test ERROR on d358e5254674b70f34c847715ca509e46eb81e6f]

url:    https://github.com/intel-lab-lkp/linux/commits/Samuel-Holland/mm-debug_vm_pgtable-Use-set_pXd-to-write-page-tables/20251211-161254
base:   d358e5254674b70f34c847715ca509e46eb81e6f
patch link:    https://lore.kernel.org/r/20251211081117.1126521-3-samuel.holland%40sifive.com
patch subject: [PATCH 2/2] mm/madvise: Use set_pte() to write page tables
config: arm-footbridge_defconfig (https://download.01.org/0day-ci/archive/20251212/202512120735.ge1E0s5N-lkp@intel.com/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251212/202512120735.ge1E0s5N-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512120735.ge1E0s5N-lkp@intel.com/

All errors (new ones prefixed by >>):

>> mm/madvise.c:1117:2: error: call to undeclared function 'set_pte'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    1117 |         set_pte(ptep, make_pte_marker(PTE_MARKER_GUARD));
         |         ^
   mm/madvise.c:1117:2: note: did you mean 'set_ptes'?
   arch/arm/include/asm/pgtable.h:212:6: note: 'set_ptes' declared here
     212 | void set_ptes(struct mm_struct *mm, unsigned long addr,
         |      ^
   1 error generated.


vim +/set_pte +1117 mm/madvise.c

  1110	
  1111	static int guard_install_set_pte(unsigned long addr, unsigned long next,
  1112					 pte_t *ptep, struct mm_walk *walk)
  1113	{
  1114		unsigned long *nr_pages = (unsigned long *)walk->private;
  1115	
  1116		/* Simply install a PTE marker, this causes segfault on access. */
> 1117		set_pte(ptep, make_pte_marker(PTE_MARKER_GUARD));
  1118		(*nr_pages)++;
  1119	
  1120		return 0;
  1121	}
  1122	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 2/2] mm/madvise: Use set_pte() to write page tables
Posted by Ryan Roberts 5 days, 7 hours ago
On 11/12/2025 08:11, Samuel Holland wrote:
> Generic code must always use the architecture-provided helper function
> to write page tables.
> 
> Fixes: 662df3e5c376 ("mm: madvise: implement lightweight guard page mechanism")
> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
> ---
> 
>  mm/madvise.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/madvise.c b/mm/madvise.c
> index b617b1be0f535..4da9c32f8738a 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -1114,7 +1114,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));

No! As I explained in my response on the other thread (which you linked in the
cover letter), it is correct as is and should not be changed to set_pte().

Copy/pasting my explanation:

| I tried "fixing" this before. But it's correct as is. ptep is pointing to a
| value on the stack. See [2].
|
| https://lore.kernel.org/linux-mm/2308a4d0-273e-4cf8-9c9f-3008c42b6d18@arm.com/

If you go look at where this function is called from, you'll see that it's a
pointer to a stack variable:


---8<---
static int walk_pte_range_inner(pte_t *pte, unsigned long addr,
				unsigned long end, struct mm_walk *walk)
{
	const struct mm_walk_ops *ops = walk->ops;
	int err = 0;

	for (;;) {
		if (ops->install_pte && pte_none(ptep_get(pte))) {
			pte_t new_pte;

			err = ops->install_pte(addr, addr + PAGE_SIZE, &new_pte,
					       walk);
---8<---

I agree that it's extremely confusing. Perhaps, at a minimum, we should come up
with some kind of naming convention for this and update this and the other
couple of places that pass pointers to stack-based pXX_t around?

e.g. instead of calling it "ptep", call it "ptevalp" or something like that?

Thanks,
Ryan


>  	(*nr_pages)++;
>  
>  	return 0;
Re: [PATCH 2/2] mm/madvise: Use set_pte() to write page tables
Posted by Lorenzo Stoakes 1 day, 6 hours ago
On Thu, Dec 11, 2025 at 09:43:58AM +0000, Ryan Roberts wrote:
> On 11/12/2025 08:11, Samuel Holland wrote:
> > Generic code must always use the architecture-provided helper function
> > to write page tables.
> >
> > Fixes: 662df3e5c376 ("mm: madvise: implement lightweight guard page mechanism")
> > Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
> > ---
> >
> >  mm/madvise.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/mm/madvise.c b/mm/madvise.c
> > index b617b1be0f535..4da9c32f8738a 100644
> > --- a/mm/madvise.c
> > +++ b/mm/madvise.c
> > @@ -1114,7 +1114,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));
>
> No! As I explained in my response on the other thread (which you linked in the
> cover letter), it is correct as is and should not be changed to set_pte().

Yup agreed, esp. given this is my code :)

Also some arches don't define set_pte()... it seems set_xxx() functions not
really intended to be used outside of arch code - see
e.g. https://elixir.bootlin.com/linux/v6.18.1/A/ident/set_pte

>
> Copy/pasting my explanation:
>
> | I tried "fixing" this before. But it's correct as is. ptep is pointing to a
> | value on the stack. See [2].
> |
> | https://lore.kernel.org/linux-mm/2308a4d0-273e-4cf8-9c9f-3008c42b6d18@arm.com/
>
> If you go look at where this function is called from, you'll see that it's a
> pointer to a stack variable:
>
>
> ---8<---
> static int walk_pte_range_inner(pte_t *pte, unsigned long addr,
> 				unsigned long end, struct mm_walk *walk)
> {
> 	const struct mm_walk_ops *ops = walk->ops;
> 	int err = 0;
>
> 	for (;;) {
> 		if (ops->install_pte && pte_none(ptep_get(pte))) {
> 			pte_t new_pte;
>
> 			err = ops->install_pte(addr, addr + PAGE_SIZE, &new_pte,
> 					       walk);
> ---8<---
>
> I agree that it's extremely confusing. Perhaps, at a minimum, we should come up
> with some kind of naming convention for this and update this and the other
> couple of places that pass pointers to stack-based pXX_t around?
>
> e.g. instead of calling it "ptep", call it "ptevalp" or something like that?

Not sure that'd clarify, we already have a bit of an inconsistent mess with all
this :(

Given it's a stack variable I'm not sure using a helper is in any way helpful
other than I suppose to account for people grepping around for incorrect page
table manipulation code?

>
> Thanks,
> Ryan
>
>
> >  	(*nr_pages)++;
> >
> >  	return 0;
>

Cheers, Lorenzo
Re: [PATCH 2/2] mm/madvise: Use set_pte() to write page tables
Posted by Ryan Roberts 1 day, 5 hours ago
On 15/12/2025 10:37, Lorenzo Stoakes wrote:
> On Thu, Dec 11, 2025 at 09:43:58AM +0000, Ryan Roberts wrote:
>> On 11/12/2025 08:11, Samuel Holland wrote:
>>> Generic code must always use the architecture-provided helper function
>>> to write page tables.
>>>
>>> Fixes: 662df3e5c376 ("mm: madvise: implement lightweight guard page mechanism")
>>> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
>>> ---
>>>
>>>  mm/madvise.c | 2 +-
>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/mm/madvise.c b/mm/madvise.c
>>> index b617b1be0f535..4da9c32f8738a 100644
>>> --- a/mm/madvise.c
>>> +++ b/mm/madvise.c
>>> @@ -1114,7 +1114,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));
>>
>> No! As I explained in my response on the other thread (which you linked in the
>> cover letter), it is correct as is and should not be changed to set_pte().
> 
> Yup agreed, esp. given this is my code :)
> 
> Also some arches don't define set_pte()... it seems set_xxx() functions not
> really intended to be used outside of arch code - see
> e.g. https://elixir.bootlin.com/linux/v6.18.1/A/ident/set_pte
> 
>>
>> Copy/pasting my explanation:
>>
>> | I tried "fixing" this before. But it's correct as is. ptep is pointing to a
>> | value on the stack. See [2].
>> |
>> | https://lore.kernel.org/linux-mm/2308a4d0-273e-4cf8-9c9f-3008c42b6d18@arm.com/
>>
>> If you go look at where this function is called from, you'll see that it's a
>> pointer to a stack variable:
>>
>>
>> ---8<---
>> static int walk_pte_range_inner(pte_t *pte, unsigned long addr,
>> 				unsigned long end, struct mm_walk *walk)
>> {
>> 	const struct mm_walk_ops *ops = walk->ops;
>> 	int err = 0;
>>
>> 	for (;;) {
>> 		if (ops->install_pte && pte_none(ptep_get(pte))) {
>> 			pte_t new_pte;
>>
>> 			err = ops->install_pte(addr, addr + PAGE_SIZE, &new_pte,
>> 					       walk);
>> ---8<---
>>
>> I agree that it's extremely confusing. Perhaps, at a minimum, we should come up
>> with some kind of naming convention for this and update this and the other
>> couple of places that pass pointers to stack-based pXX_t around?
>>
>> e.g. instead of calling it "ptep", call it "ptevalp" or something like that?
> 
> Not sure that'd clarify, we already have a bit of an inconsistent mess with all
> this :(
> 
> Given it's a stack variable I'm not sure using a helper is in any way helpful
> other than I suppose to account for people grepping around for incorrect page
> table manipulation code?

I've proposed an approach to clean all of this up. I'd appreciate your opinion
if you get a few mins:

https://lore.kernel.org/all/a063f6c5-2785-4a9f-8079-25edb3e54cef@arm.com/

> 
>>
>> Thanks,
>> Ryan
>>
>>
>>>  	(*nr_pages)++;
>>>
>>>  	return 0;
>>
> 
> Cheers, Lorenzo
Re: [PATCH 2/2] mm/madvise: Use set_pte() to write page tables
Posted by Lorenzo Stoakes 6 hours ago
On Mon, Dec 15, 2025 at 10:57:31AM +0000, Ryan Roberts wrote:
> >>
> >> I agree that it's extremely confusing. Perhaps, at a minimum, we should come up
> >> with some kind of naming convention for this and update this and the other
> >> couple of places that pass pointers to stack-based pXX_t around?
> >>
> >> e.g. instead of calling it "ptep", call it "ptevalp" or something like that?
> >
> > Not sure that'd clarify, we already have a bit of an inconsistent mess with all
> > this :(
> >
> > Given it's a stack variable I'm not sure using a helper is in any way helpful
> > other than I suppose to account for people grepping around for incorrect page
> > table manipulation code?
>
> I've proposed an approach to clean all of this up. I'd appreciate your opinion
> if you get a few mins:
>
> https://lore.kernel.org/all/a063f6c5-2785-4a9f-8079-25edb3e54cef@arm.com/

Sure will take a look!

Cheers, Lorenzo