Re: [PATCH RFC v2 46/54] s390: remove set_memory_rwnx()
From: Mike Rapoport
Date: Thu Dec 03 2026 - 09:30:48 EST
On Thu, Dec 03, 2026 at 11:49:25AM +0100, Heiko Carstens wrote:
> On Thu, Nov 26, 2026 at 01:14:59PM +0200, Mike Rapoport (Microsoft) wrote:
> > set_memory_rwnx() has no users since commit 33bd8d153c33 ("s390: Keep
> > inittext section writable").
> >
> > Remove it.
> >
> > Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
> > ---
> > arch/s390/include/asm/set_memory.h | 1 -
> > 1 file changed, 1 deletion(-)
>
> I'd rather keep it, since it would be useful for execmem_force_rw(), which
> is doing set_memory_nx() + set_memory_rw(), which could be replaced with
> set_memory_rwnx().
>
> set_memory*() calls are quite expensive on s390 (I guess that's true for all
> architectures). Collapsing calls doesn't seem to be the worst idea.
I agree that it makes sense. riscv also has set_memory_rwnx() and it can be
added on x86 and maybe others as well.
Since the rework is going to be slow, I can take it from here and post
a more general patch that updates other architectures too.
> Something like the below maybe?
>
> diff --git a/arch/s390/include/asm/set_memory.h b/arch/s390/include/asm/set_memory.h
> index b453dd2d6a8e..580d81324e82 100644
> --- a/arch/s390/include/asm/set_memory.h
> +++ b/arch/s390/include/asm/set_memory.h
> @@ -23,6 +23,7 @@ enum {
> int __set_memory(unsigned long addr, unsigned long numpages, unsigned long flags);
>
> #define set_memory_rox set_memory_rox
> +#define set_memory_rwnx set_memory_rwnx
>
> /*
> * Generate two variants of each set_memory() function:
> diff --git a/include/linux/set_memory.h b/include/linux/set_memory.h
> index 27c32fbabfec..0bbdb63a406d 100644
> --- a/include/linux/set_memory.h
> +++ b/include/linux/set_memory.h
> @@ -100,6 +100,30 @@ static inline int set_memory_rox(unsigned long addr, int numpages)
> }
> #endif
>
> +#ifndef set_memory_rwnx
> +/**
> + * set_memory_rwnx - make a kernel mapping writable and non-executable
> + * @addr: page aligned start of the kernel virtual address range
> + * @numpages: number of pages in the range
> + *
> + * The mapping is made non-executable before it is made writable so that it is
> + * never writable and executable at the same time. A failure may leave the range
> + * non-executable but not writable.
Heh, it is never writable and executable at the same time indeed, but a
failure in the middle of set_memory_nx() just leaves the old permissions :/
> + *
> + * Flushes the TLB for the range.
> + *
> + * Return: 0 on success, negative error code on failure.
> + */
> +static inline int set_memory_rwnx(unsigned long addr, int numpages)
> +{
> + int ret = set_memory_nx(addr, numpages);
> +
> + if (ret)
> + return ret;
> + return set_memory_rw(addr, numpages);
> +}
> +#endif
> +
> #ifndef CONFIG_ARCH_HAS_SET_DIRECT_MAP
> /**
> * set_direct_map_invalid_noflush - remove pages from the direct map
> diff --git a/mm/execmem.c b/mm/execmem.c
> index 349cadd87486..16d795097878 100644
> --- a/mm/execmem.c
> +++ b/mm/execmem.c
> @@ -118,13 +118,8 @@ static int execmem_force_rw(void *ptr, size_t size)
> {
> unsigned int nr = PAGE_ALIGN(size) >> PAGE_SHIFT;
> unsigned long addr = (unsigned long)ptr;
> - int ret;
>
> - ret = set_memory_nx(addr, nr);
> - if (ret)
> - return ret;
> -
> - return set_memory_rw(addr, nr);
> + return set_memory_rwnx(addr, nr);
And this is lovely :)
> }
>
> int execmem_restore_rox(void *ptr, size_t size)
--
Sincerely yours,
Mike.