Skip to content

Commit b44533a

Browse files
Vasily Gorbikgregkh
authored andcommitted
s390: introduce CPU alternatives
[ Upstream commit 686140a ] Implement CPU alternatives, which allows to optionally patch newer instructions at runtime, based on CPU facilities availability. A new kernel boot parameter "noaltinstr" disables patching. Current implementation is derived from x86 alternatives. Although ideal instructions padding (when altinstr is longer then oldinstr) is added at compile time, and no oldinstr nops optimization has to be done at runtime. Also couple of compile time sanity checks are done: 1. oldinstr and altinstr must be <= 254 bytes long, 2. oldinstr and altinstr must not have an odd length. alternative(oldinstr, altinstr, facility); alternative_2(oldinstr, altinstr1, facility1, altinstr2, facility2); Both compile time and runtime padding consists of either 6/4/2 bytes nop or a jump (brcl) + 2 bytes nop filler if padding is longer then 6 bytes. .altinstructions and .altinstr_replacement sections are part of __init_begin : __init_end region and are freed after initialization. Signed-off-by: Vasily Gorbik <gor@linux.vnet.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 55c80ad commit b44533a

8 files changed

Lines changed: 337 additions & 0 deletions

File tree

‎Documentation/admin-guide/kernel-parameters.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,6 +2541,9 @@
25412541

25422542
noalign [KNL,ARM]
25432543

2544+
noaltinstr [S390] Disables alternative instructions patching
2545+
(CPU alternatives feature).
2546+
25442547
noapic [SMP,APIC] Tells the kernel to not make use of any
25452548
IOAPICs that may be present in the system.
25462549

‎arch/s390/Kconfig‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,22 @@ config ARCH_RANDOM
538538

539539
If unsure, say Y.
540540

541+
config ALTERNATIVES
542+
def_bool y
543+
prompt "Patch optimized instructions for running CPU type"
544+
help
545+
When enabled the kernel code is compiled with additional
546+
alternative instructions blocks optimized for newer CPU types.
547+
These alternative instructions blocks are patched at kernel boot
548+
time when running CPU supports them. This mechanism is used to
549+
optimize some critical code paths (i.e. spinlocks) for newer CPUs
550+
even if kernel is build to support older machine generations.
551+
552+
This mechanism could be disabled by appending "noaltinstr"
553+
option to the kernel command line.
554+
555+
If unsure, say Y.
556+
541557
endmenu
542558

543559
menu "Memory setup"
@@ -812,6 +828,7 @@ config PFAULT
812828
config SHARED_KERNEL
813829
bool "VM shared kernel support"
814830
depends on !JUMP_LABEL
831+
depends on !ALTERNATIVES
815832
help
816833
Select this option, if you want to share the text segment of the
817834
Linux kernel between different VM guests. This reduces memory
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#ifndef _ASM_S390_ALTERNATIVE_H
2+
#define _ASM_S390_ALTERNATIVE_H
3+
4+
#ifndef __ASSEMBLY__
5+
6+
#include <linux/types.h>
7+
#include <linux/stddef.h>
8+
#include <linux/stringify.h>
9+
10+
struct alt_instr {
11+
s32 instr_offset; /* original instruction */
12+
s32 repl_offset; /* offset to replacement instruction */
13+
u16 facility; /* facility bit set for replacement */
14+
u8 instrlen; /* length of original instruction */
15+
u8 replacementlen; /* length of new instruction */
16+
} __packed;
17+
18+
#ifdef CONFIG_ALTERNATIVES
19+
extern void apply_alternative_instructions(void);
20+
extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
21+
#else
22+
static inline void apply_alternative_instructions(void) {};
23+
static inline void apply_alternatives(struct alt_instr *start,
24+
struct alt_instr *end) {};
25+
#endif
26+
/*
27+
* |661: |662: |6620 |663:
28+
* +-----------+---------------------+
29+
* | oldinstr | oldinstr_padding |
30+
* | +----------+----------+
31+
* | | | |
32+
* | | >6 bytes |6/4/2 nops|
33+
* | |6 bytes jg----------->
34+
* +-----------+---------------------+
35+
* ^^ static padding ^^
36+
*
37+
* .altinstr_replacement section
38+
* +---------------------+-----------+
39+
* |6641: |6651:
40+
* | alternative instr 1 |
41+
* +-----------+---------+- - - - - -+
42+
* |6642: |6652: |
43+
* | alternative instr 2 | padding
44+
* +---------------------+- - - - - -+
45+
* ^ runtime ^
46+
*
47+
* .altinstructions section
48+
* +---------------------------------+
49+
* | alt_instr entries for each |
50+
* | alternative instr |
51+
* +---------------------------------+
52+
*/
53+
54+
#define b_altinstr(num) "664"#num
55+
#define e_altinstr(num) "665"#num
56+
57+
#define e_oldinstr_pad_end "663"
58+
#define oldinstr_len "662b-661b"
59+
#define oldinstr_total_len e_oldinstr_pad_end"b-661b"
60+
#define altinstr_len(num) e_altinstr(num)"b-"b_altinstr(num)"b"
61+
#define oldinstr_pad_len(num) \
62+
"-(((" altinstr_len(num) ")-(" oldinstr_len ")) > 0) * " \
63+
"((" altinstr_len(num) ")-(" oldinstr_len "))"
64+
65+
#define INSTR_LEN_SANITY_CHECK(len) \
66+
".if " len " > 254\n" \
67+
"\t.error \"cpu alternatives does not support instructions " \
68+
"blocks > 254 bytes\"\n" \
69+
".endif\n" \
70+
".if (" len ") %% 2\n" \
71+
"\t.error \"cpu alternatives instructions length is odd\"\n" \
72+
".endif\n"
73+
74+
#define OLDINSTR_PADDING(oldinstr, num) \
75+
".if " oldinstr_pad_len(num) " > 6\n" \
76+
"\tjg " e_oldinstr_pad_end "f\n" \
77+
"6620:\n" \
78+
"\t.fill (" oldinstr_pad_len(num) " - (6620b-662b)) / 2, 2, 0x0700\n" \
79+
".else\n" \
80+
"\t.fill " oldinstr_pad_len(num) " / 6, 6, 0xc0040000\n" \
81+
"\t.fill " oldinstr_pad_len(num) " %% 6 / 4, 4, 0x47000000\n" \
82+
"\t.fill " oldinstr_pad_len(num) " %% 6 %% 4 / 2, 2, 0x0700\n" \
83+
".endif\n"
84+
85+
#define OLDINSTR(oldinstr, num) \
86+
"661:\n\t" oldinstr "\n662:\n" \
87+
OLDINSTR_PADDING(oldinstr, num) \
88+
e_oldinstr_pad_end ":\n" \
89+
INSTR_LEN_SANITY_CHECK(oldinstr_len)
90+
91+
#define OLDINSTR_2(oldinstr, num1, num2) \
92+
"661:\n\t" oldinstr "\n662:\n" \
93+
".if " altinstr_len(num1) " < " altinstr_len(num2) "\n" \
94+
OLDINSTR_PADDING(oldinstr, num2) \
95+
".else\n" \
96+
OLDINSTR_PADDING(oldinstr, num1) \
97+
".endif\n" \
98+
e_oldinstr_pad_end ":\n" \
99+
INSTR_LEN_SANITY_CHECK(oldinstr_len)
100+
101+
#define ALTINSTR_ENTRY(facility, num) \
102+
"\t.long 661b - .\n" /* old instruction */ \
103+
"\t.long " b_altinstr(num)"b - .\n" /* alt instruction */ \
104+
"\t.word " __stringify(facility) "\n" /* facility bit */ \
105+
"\t.byte " oldinstr_total_len "\n" /* source len */ \
106+
"\t.byte " altinstr_len(num) "\n" /* alt instruction len */
107+
108+
#define ALTINSTR_REPLACEMENT(altinstr, num) /* replacement */ \
109+
b_altinstr(num)":\n\t" altinstr "\n" e_altinstr(num) ":\n" \
110+
INSTR_LEN_SANITY_CHECK(altinstr_len(num))
111+
112+
#ifdef CONFIG_ALTERNATIVES
113+
/* alternative assembly primitive: */
114+
#define ALTERNATIVE(oldinstr, altinstr, facility) \
115+
".pushsection .altinstr_replacement, \"ax\"\n" \
116+
ALTINSTR_REPLACEMENT(altinstr, 1) \
117+
".popsection\n" \
118+
OLDINSTR(oldinstr, 1) \
119+
".pushsection .altinstructions,\"a\"\n" \
120+
ALTINSTR_ENTRY(facility, 1) \
121+
".popsection\n"
122+
123+
#define ALTERNATIVE_2(oldinstr, altinstr1, facility1, altinstr2, facility2)\
124+
".pushsection .altinstr_replacement, \"ax\"\n" \
125+
ALTINSTR_REPLACEMENT(altinstr1, 1) \
126+
ALTINSTR_REPLACEMENT(altinstr2, 2) \
127+
".popsection\n" \
128+
OLDINSTR_2(oldinstr, 1, 2) \
129+
".pushsection .altinstructions,\"a\"\n" \
130+
ALTINSTR_ENTRY(facility1, 1) \
131+
ALTINSTR_ENTRY(facility2, 2) \
132+
".popsection\n"
133+
#else
134+
/* Alternative instructions are disabled, let's put just oldinstr in */
135+
#define ALTERNATIVE(oldinstr, altinstr, facility) \
136+
oldinstr "\n"
137+
138+
#define ALTERNATIVE_2(oldinstr, altinstr1, facility1, altinstr2, facility2) \
139+
oldinstr "\n"
140+
#endif
141+
142+
/*
143+
* Alternative instructions for different CPU types or capabilities.
144+
*
145+
* This allows to use optimized instructions even on generic binary
146+
* kernels.
147+
*
148+
* oldinstr is padded with jump and nops at compile time if altinstr is
149+
* longer. altinstr is padded with jump and nops at run-time during patching.
150+
*
151+
* For non barrier like inlines please define new variants
152+
* without volatile and memory clobber.
153+
*/
154+
#define alternative(oldinstr, altinstr, facility) \
155+
asm volatile(ALTERNATIVE(oldinstr, altinstr, facility) : : : "memory")
156+
157+
#define alternative_2(oldinstr, altinstr1, facility1, altinstr2, facility2) \
158+
asm volatile(ALTERNATIVE_2(oldinstr, altinstr1, facility1, \
159+
altinstr2, facility2) ::: "memory")
160+
161+
#endif /* __ASSEMBLY__ */
162+
163+
#endif /* _ASM_S390_ALTERNATIVE_H */

‎arch/s390/kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ obj-$(CONFIG_KPROBES) += kprobes.o
7575
obj-$(CONFIG_FUNCTION_TRACER) += mcount.o ftrace.o
7676
obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
7777
obj-$(CONFIG_UPROBES) += uprobes.o
78+
obj-$(CONFIG_ALTERNATIVES) += alternative.o
7879

7980
obj-$(CONFIG_PERF_EVENTS) += perf_event.o perf_cpum_cf.o perf_cpum_sf.o
8081
obj-$(CONFIG_PERF_EVENTS) += perf_cpum_cf_events.o

‎arch/s390/kernel/alternative.c‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <linux/module.h>
2+
#include <asm/alternative.h>
3+
#include <asm/facility.h>
4+
5+
#define MAX_PATCH_LEN (255 - 1)
6+
7+
static int __initdata_or_module alt_instr_disabled;
8+
9+
static int __init disable_alternative_instructions(char *str)
10+
{
11+
alt_instr_disabled = 1;
12+
return 0;
13+
}
14+
15+
early_param("noaltinstr", disable_alternative_instructions);
16+
17+
struct brcl_insn {
18+
u16 opc;
19+
s32 disp;
20+
} __packed;
21+
22+
static u16 __initdata_or_module nop16 = 0x0700;
23+
static u32 __initdata_or_module nop32 = 0x47000000;
24+
static struct brcl_insn __initdata_or_module nop48 = {
25+
0xc004, 0
26+
};
27+
28+
static const void *nops[] __initdata_or_module = {
29+
&nop16,
30+
&nop32,
31+
&nop48
32+
};
33+
34+
static void __init_or_module add_jump_padding(void *insns, unsigned int len)
35+
{
36+
struct brcl_insn brcl = {
37+
0xc0f4,
38+
len / 2
39+
};
40+
41+
memcpy(insns, &brcl, sizeof(brcl));
42+
insns += sizeof(brcl);
43+
len -= sizeof(brcl);
44+
45+
while (len > 0) {
46+
memcpy(insns, &nop16, 2);
47+
insns += 2;
48+
len -= 2;
49+
}
50+
}
51+
52+
static void __init_or_module add_padding(void *insns, unsigned int len)
53+
{
54+
if (len > 6)
55+
add_jump_padding(insns, len);
56+
else if (len >= 2)
57+
memcpy(insns, nops[len / 2 - 1], len);
58+
}
59+
60+
static void __init_or_module __apply_alternatives(struct alt_instr *start,
61+
struct alt_instr *end)
62+
{
63+
struct alt_instr *a;
64+
u8 *instr, *replacement;
65+
u8 insnbuf[MAX_PATCH_LEN];
66+
67+
/*
68+
* The scan order should be from start to end. A later scanned
69+
* alternative code can overwrite previously scanned alternative code.
70+
*/
71+
for (a = start; a < end; a++) {
72+
int insnbuf_sz = 0;
73+
74+
instr = (u8 *)&a->instr_offset + a->instr_offset;
75+
replacement = (u8 *)&a->repl_offset + a->repl_offset;
76+
77+
if (!test_facility(a->facility))
78+
continue;
79+
80+
if (unlikely(a->instrlen % 2 || a->replacementlen % 2)) {
81+
WARN_ONCE(1, "cpu alternatives instructions length is "
82+
"odd, skipping patching\n");
83+
continue;
84+
}
85+
86+
memcpy(insnbuf, replacement, a->replacementlen);
87+
insnbuf_sz = a->replacementlen;
88+
89+
if (a->instrlen > a->replacementlen) {
90+
add_padding(insnbuf + a->replacementlen,
91+
a->instrlen - a->replacementlen);
92+
insnbuf_sz += a->instrlen - a->replacementlen;
93+
}
94+
95+
s390_kernel_write(instr, insnbuf, insnbuf_sz);
96+
}
97+
}
98+
99+
void __init_or_module apply_alternatives(struct alt_instr *start,
100+
struct alt_instr *end)
101+
{
102+
if (!alt_instr_disabled)
103+
__apply_alternatives(start, end);
104+
}
105+
106+
extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
107+
void __init apply_alternative_instructions(void)
108+
{
109+
apply_alternatives(__alt_instructions, __alt_instructions_end);
110+
}

‎arch/s390/kernel/module.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/kernel.h>
3232
#include <linux/moduleloader.h>
3333
#include <linux/bug.h>
34+
#include <asm/alternative.h>
3435

3536
#if 0
3637
#define DEBUGP printk
@@ -429,6 +430,22 @@ int module_finalize(const Elf_Ehdr *hdr,
429430
const Elf_Shdr *sechdrs,
430431
struct module *me)
431432
{
433+
const Elf_Shdr *s;
434+
char *secstrings;
435+
436+
if (IS_ENABLED(CONFIG_ALTERNATIVES)) {
437+
secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
438+
for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
439+
if (!strcmp(".altinstructions",
440+
secstrings + s->sh_name)) {
441+
/* patch .altinstructions */
442+
void *aseg = (void *)s->sh_addr;
443+
444+
apply_alternatives(aseg, aseg + s->sh_size);
445+
}
446+
}
447+
}
448+
432449
jump_label_apply_nops(me);
433450
return 0;
434451
}

‎arch/s390/kernel/setup.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include <asm/sclp.h>
6767
#include <asm/sysinfo.h>
6868
#include <asm/numa.h>
69+
#include <asm/alternative.h>
6970
#include "entry.h"
7071

7172
/*
@@ -955,6 +956,8 @@ void __init setup_arch(char **cmdline_p)
955956
conmode_default();
956957
set_preferred_console();
957958

959+
apply_alternative_instructions();
960+
958961
/* Setup zfcpdump support */
959962
setup_zfcpdump();
960963

0 commit comments

Comments
 (0)