s390/skey: Provide infrastructure for executing with non-default access key

The current assumption is that kernel code is always executed with access
key zero, which means that storage key protection does not apply.

However this assumption is not correct: cmpxchg_user_key() may be executed
with a non-zero key; if then the storage key of the page which belongs to
the cmpxchg_user_key() code contains a key with fetch-protection enabled
the result is a protection exception.

For several performance optimizations storage keys are not initialized on
system boot. To keep these optimizations add infrastructure which allows to
define code ranges within functions which are executed with a non-default
key. When such code is executed such functions must explicitly call
skey_regions_initialize().

This will initialize all storage keys belonging to such code ranges in a
way that no protection exceptions happen when the code is executed with a
non-default access key.

Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
Signed-off-by: Alexander Gordeev <agordeev@linux.ibm.com>
This commit is contained in:
Heiko Carstens
2025-06-16 17:00:30 +02:00
committed by Alexander Gordeev
parent 6fe0ea914d
commit ee417a84d0
4 changed files with 88 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __ASM_SKEY_H
#define __ASM_SKEY_H
#include <asm/rwonce.h>
struct skey_region {
unsigned long start;
unsigned long end;
};
#define SKEY_REGION(_start, _end) \
stringify_in_c(.section .skey_region,"a";) \
stringify_in_c(.balign 8;) \
stringify_in_c(.quad (_start);) \
stringify_in_c(.quad (_end);) \
stringify_in_c(.previous)
extern int skey_regions_initialized;
extern struct skey_region __skey_region_start[];
extern struct skey_region __skey_region_end[];
void __skey_regions_initialize(void);
static inline void skey_regions_initialize(void)
{
if (READ_ONCE(skey_regions_initialized))
return;
__skey_regions_initialize();
}
#endif /* __ASM_SKEY_H */

View File

@@ -41,7 +41,7 @@ obj-y += processor.o syscall.o ptrace.o signal.o cpcmd.o ebcdic.o nmi.o
obj-y += debug.o irq.o ipl.o dis.o vdso.o cpufeature.o
obj-y += sysinfo.o lgr.o os_info.o ctlreg.o
obj-y += runtime_instr.o cache.o fpu.o dumpstack.o guarded_storage.o sthyi.o
obj-y += entry.o reipl.o kdebugfs.o alternative.o
obj-y += entry.o reipl.o kdebugfs.o alternative.o skey.o
obj-y += nospec-branch.o ipl_vmparm.o machine_kexec_reloc.o unwind_bc.o
obj-y += smp.o text_amode31.o stacktrace.o abs_lowcore.o facility.o uv.o wti.o
obj-y += diag/

48
arch/s390/kernel/skey.c Normal file
View File

@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-2.0
#include <asm/rwonce.h>
#include <asm/page.h>
#include <asm/skey.h>
int skey_regions_initialized;
static inline unsigned long load_real_address(unsigned long address)
{
unsigned long real;
asm volatile(
" lra %[real],0(%[address])\n"
: [real] "=d" (real)
: [address] "a" (address)
: "cc");
return real;
}
/*
* Initialize storage keys of registered memory regions with the
* default key. This is useful for code which is executed with a
* non-default access key.
*/
void __skey_regions_initialize(void)
{
unsigned long address, real;
struct skey_region *r, *end;
r = __skey_region_start;
end = __skey_region_end;
while (r < end) {
address = r->start & PAGE_MASK;
do {
real = load_real_address(address);
page_set_storage_key(real, PAGE_DEFAULT_KEY, 1);
address += PAGE_SIZE;
} while (address < r->end);
r++;
}
/*
* Make sure storage keys are initialized before
* skey_regions_initialized is changed.
*/
barrier();
WRITE_ONCE(skey_regions_initialized, 1);
}

View File

@@ -71,6 +71,13 @@ SECTIONS
. = ALIGN(PAGE_SIZE);
__end_ro_after_init = .;
. = ALIGN(8);
.skey_region_table : {
__skey_region_start = .;
KEEP(*(.skey_region))
__skey_region_end = .;
}
.data.rel.ro : {
*(.data.rel.ro .data.rel.ro.*)
}