[PATCH] execmem: enforce allocation size aligment to PAGE_SIZE

Mike Rapoport posted 1 patch 7 months, 4 weeks ago
mm/execmem.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH] execmem: enforce allocation size aligment to PAGE_SIZE
Posted by Mike Rapoport 7 months, 4 weeks ago
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>

Before introduction of ROX cache execmem allocation size was always
implicitly aligned to PAGE_SIZE inside vmalloc.

However, when allocation happens from the ROX cache, this is not
enforced.

Make sure that the allocation size is always consistently aligned to
PAGE_SIZE.

Fixes: 2e45474ab14f ("execmem: add support for cache of large ROX pages")
Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 mm/execmem.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/execmem.c b/mm/execmem.c
index e6c4f5076ca8..2b683e7d864d 100644
--- a/mm/execmem.c
+++ b/mm/execmem.c
@@ -377,6 +377,8 @@ void *execmem_alloc(enum execmem_type type, size_t size)
 	pgprot_t pgprot = range->pgprot;
 	void *p;
 
+	size = PAGE_ALIGN(size);
+
 	if (use_cache)
 		p = execmem_cache_alloc(range, size);
 	else
-- 
2.47.2
Re: [PATCH] execmem: enforce allocation size aligment to PAGE_SIZE
Posted by Andrew Morton 7 months, 4 weeks ago
On Wed, 23 Apr 2025 17:48:07 +0300 Mike Rapoport <rppt@kernel.org> wrote:

> Before introduction of ROX cache execmem allocation size was always
> implicitly aligned to PAGE_SIZE inside vmalloc.
> 
> However, when allocation happens from the ROX cache, this is not
> enforced.
> 
> Make sure that the allocation size is always consistently aligned to
> PAGE_SIZE.

Does this have any known runtime effect?
Re: [PATCH] execmem: enforce allocation size aligment to PAGE_SIZE
Posted by Peter Zijlstra 7 months, 4 weeks ago
On Wed, Apr 23, 2025 at 02:36:50PM -0700, Andrew Morton wrote:
> On Wed, 23 Apr 2025 17:48:07 +0300 Mike Rapoport <rppt@kernel.org> wrote:
> 
> > Before introduction of ROX cache execmem allocation size was always
> > implicitly aligned to PAGE_SIZE inside vmalloc.
> > 
> > However, when allocation happens from the ROX cache, this is not
> > enforced.
> > 
> > Make sure that the allocation size is always consistently aligned to
> > PAGE_SIZE.
> 
> Does this have any known runtime effect?

It should not -- currently all this code is used with PAGE_SIZE
multiples and everything just works. But whilst I was perusing this
code, I noticed that nothing actually enforced this. If someone were to
break this assumption things will go sideways.
Re: [PATCH] execmem: enforce allocation size aligment to PAGE_SIZE
Posted by Mike Rapoport 7 months, 4 weeks ago
On Wed, Apr 23, 2025 at 02:36:50PM -0700, Andrew Morton wrote:
> On Wed, 23 Apr 2025 17:48:07 +0300 Mike Rapoport <rppt@kernel.org> wrote:
> 
> > Before introduction of ROX cache execmem allocation size was always
> > implicitly aligned to PAGE_SIZE inside vmalloc.
> > 
> > However, when allocation happens from the ROX cache, this is not
> > enforced.
> > 
> > Make sure that the allocation size is always consistently aligned to
> > PAGE_SIZE.
> 
> Does this have any known runtime effect?

Right now it'll make the maple trees in execmem_cache more compact.
And it's a precaution for the case when execmem callers would want to
change permissions on unaligned range because that would WARN_ON() loudly.

-- 
Sincerely yours,
Mike.
Re: [PATCH] execmem: enforce allocation size aligment to PAGE_SIZE
Posted by Peter Zijlstra 7 months, 4 weeks ago
On Wed, Apr 23, 2025 at 05:48:07PM +0300, Mike Rapoport wrote:
> From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
> 
> Before introduction of ROX cache execmem allocation size was always
> implicitly aligned to PAGE_SIZE inside vmalloc.
> 
> However, when allocation happens from the ROX cache, this is not
> enforced.
> 
> Make sure that the allocation size is always consistently aligned to
> PAGE_SIZE.
> 
> Fixes: 2e45474ab14f ("execmem: add support for cache of large ROX pages")
> Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>

Thanks!

Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>

> ---
>  mm/execmem.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/mm/execmem.c b/mm/execmem.c
> index e6c4f5076ca8..2b683e7d864d 100644
> --- a/mm/execmem.c
> +++ b/mm/execmem.c
> @@ -377,6 +377,8 @@ void *execmem_alloc(enum execmem_type type, size_t size)
>  	pgprot_t pgprot = range->pgprot;
>  	void *p;
>  
> +	size = PAGE_ALIGN(size);
> +
>  	if (use_cache)
>  		p = execmem_cache_alloc(range, size);
>  	else
> -- 
> 2.47.2
>