Skip to content

Commit ee417a8

Browse files
hcahcaAlexander Gordeev
authored andcommitted
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 <[email protected]> Signed-off-by: Heiko Carstens <[email protected]> Signed-off-by: Alexander Gordeev <[email protected]>
1 parent 6fe0ea9 commit ee417a8

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

arch/s390/include/asm/skey.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __ASM_SKEY_H
3+
#define __ASM_SKEY_H
4+
5+
#include <asm/rwonce.h>
6+
7+
struct skey_region {
8+
unsigned long start;
9+
unsigned long end;
10+
};
11+
12+
#define SKEY_REGION(_start, _end) \
13+
stringify_in_c(.section .skey_region,"a";) \
14+
stringify_in_c(.balign 8;) \
15+
stringify_in_c(.quad (_start);) \
16+
stringify_in_c(.quad (_end);) \
17+
stringify_in_c(.previous)
18+
19+
extern int skey_regions_initialized;
20+
extern struct skey_region __skey_region_start[];
21+
extern struct skey_region __skey_region_end[];
22+
23+
void __skey_regions_initialize(void);
24+
25+
static inline void skey_regions_initialize(void)
26+
{
27+
if (READ_ONCE(skey_regions_initialized))
28+
return;
29+
__skey_regions_initialize();
30+
}
31+
32+
#endif /* __ASM_SKEY_H */

arch/s390/kernel/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ obj-y += processor.o syscall.o ptrace.o signal.o cpcmd.o ebcdic.o nmi.o
4141
obj-y += debug.o irq.o ipl.o dis.o vdso.o cpufeature.o
4242
obj-y += sysinfo.o lgr.o os_info.o ctlreg.o
4343
obj-y += runtime_instr.o cache.o fpu.o dumpstack.o guarded_storage.o sthyi.o
44-
obj-y += entry.o reipl.o kdebugfs.o alternative.o
44+
obj-y += entry.o reipl.o kdebugfs.o alternative.o skey.o
4545
obj-y += nospec-branch.o ipl_vmparm.o machine_kexec_reloc.o unwind_bc.o
4646
obj-y += smp.o text_amode31.o stacktrace.o abs_lowcore.o facility.o uv.o wti.o
4747
obj-y += diag/

arch/s390/kernel/skey.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <asm/rwonce.h>
4+
#include <asm/page.h>
5+
#include <asm/skey.h>
6+
7+
int skey_regions_initialized;
8+
9+
static inline unsigned long load_real_address(unsigned long address)
10+
{
11+
unsigned long real;
12+
13+
asm volatile(
14+
" lra %[real],0(%[address])\n"
15+
: [real] "=d" (real)
16+
: [address] "a" (address)
17+
: "cc");
18+
return real;
19+
}
20+
21+
/*
22+
* Initialize storage keys of registered memory regions with the
23+
* default key. This is useful for code which is executed with a
24+
* non-default access key.
25+
*/
26+
void __skey_regions_initialize(void)
27+
{
28+
unsigned long address, real;
29+
struct skey_region *r, *end;
30+
31+
r = __skey_region_start;
32+
end = __skey_region_end;
33+
while (r < end) {
34+
address = r->start & PAGE_MASK;
35+
do {
36+
real = load_real_address(address);
37+
page_set_storage_key(real, PAGE_DEFAULT_KEY, 1);
38+
address += PAGE_SIZE;
39+
} while (address < r->end);
40+
r++;
41+
}
42+
/*
43+
* Make sure storage keys are initialized before
44+
* skey_regions_initialized is changed.
45+
*/
46+
barrier();
47+
WRITE_ONCE(skey_regions_initialized, 1);
48+
}

arch/s390/kernel/vmlinux.lds.S

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ SECTIONS
7171
. = ALIGN(PAGE_SIZE);
7272
__end_ro_after_init = .;
7373

74+
. = ALIGN(8);
75+
.skey_region_table : {
76+
__skey_region_start = .;
77+
KEEP(*(.skey_region))
78+
__skey_region_end = .;
79+
}
80+
7481
.data.rel.ro : {
7582
*(.data.rel.ro .data.rel.ro.*)
7683
}

0 commit comments

Comments
 (0)