Skip to contentSkip to conversation

execmem: enforce allocation size aligment to PAGE_SIZE

PATCH Applied Mike Rapoport 3 participants 5 messages Apr 23, 2025 – Apr 24, 2025

Subsystems (2)
lore
Share & tools
lore

Mike Rapoport

author maintainer
#0 lore raw

PATCH 1/1 execmem: enforce allocation size aligment to PAGE_SIZE

From: "Mike Rapoport (Microsoft)" <[email protected]>

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")
---
 mm/execmem.c | 2 ++
 1 file changed, 2 insertions(+)
mm/execmem.c +2 -0
index e6c4f5076ca8..2b683e7d864d 100644
@@ -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
Signature · 1 line
2.47.2
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

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.

Quoted 6 lines

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

Fixes 2e45474ab14f ("execmem: add support for cache of large ROX pages")

Thanks!

Quoted 20 lines
---
 mm/execmem.c | 2 ++
 1 file changed, 2 insertions(+)
mm/execmem.c +2 -0
index e6c4f5076ca8..2b683e7d864d 100644
@@ -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
Signature · 1 line
2.47.2
maintainer
#2 lore raw
In reply to Mike Rapoport
Quoted 8 lines · On Wed, 23 Apr 2025 17:48:07 +0300 Mike Rapoport <[email protected]> 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?

author maintainer
#3 lore raw
Quoted 5 lines · Andrew Morton
Quoted 3 lines · On Wed, 23 Apr 2025 17:48:07 +0300 Mike Rapoport <[email protected]> wrote:

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

Quoted 7 lines
Quoted 5 lines

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.

Signature · 2 lines
Sincerely yours,
Mike.
#4 lore raw
In reply to Andrew Morton
Quoted 5 lines · Andrew Morton
Quoted 3 lines · On Wed, 23 Apr 2025 17:48:07 +0300 Mike Rapoport <[email protected]> wrote:

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

Quoted 7 lines
Quoted 5 lines

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.