x86: PIE support and option to extend KASLR randomization
Subsystems (15)
Changes:
- v2:
- Add support for global stack cookie while compiler default to fs without mcmodel=kernel
- Change patch 7 to correctly jump out of the identity mapping on kexec load preserve.
These patches make the changes necessary to build the kernel as Position Independent Executable (PIE) on x86_64. A PIE kernel can be relocated below the top 2G of the virtual address space. It allows to optionally extend the KASLR randomization range from 1G to 3G.
Thanks a lot to Ard Biesheuvel & Kees Cook on their feedback on compiler changes, PIE support and KASLR in general.
The patches:
- 1-3, 5-15: Change in assembly code to be PIE compliant.
- 4: Add a new _ASM_GET_PTR macro to fetch a symbol address generically.
- 16: Adapt percpu design to work correctly when PIE is enabled.
- 17: Provide an option to default visibility to hidden except for key symbols. It removes errors between compilation units.
- 18: Adapt relocation tool to handle PIE binary correctly.
- 19: Add support for global cookie
- 20: Add the CONFIG_X86_PIE option (off by default)
- 21: Adapt relocation tool to generate a 64-bit relocation table.
- 22: Add options to build modules as mcmodel=large and dynamically create a PLT for relative references out of range (adapted from arm64).
- 23: Add the CONFIG_RANDOMIZE_BASE_LARGE option to increase relocation range from 1G to 3G (off by default).
Performance/Size impact:
Hackbench (50% and 1600% loads):
- PIE disabled: no significant change (-0.50% / +0.50%)
- PIE enabled: 7% to 8% on half load, 10% on heavy load.
These results are aligned with the different research on user-mode PIE impact on cpu intensive benchmarks (around 10% on x86_64).
slab_test (average of 10 runs):
- PIE disabled: no significant change (-1% / +1%)
- PIE enabled: 3% to 4%
Kernbench (average of 10 Half and Optimal runs):
Elapsed Time:
- PIE disabled: no significant change (-0.22% / +0.06%)
- PIE enabled: around 0.50%
System Time:
- PIE disabled: no significant change (-0.99% / -1.28%)
- PIE enabled: 5% to 6%
Size of vmlinux (Ubuntu configuration):
File size:
- PIE disabled: 472928672 bytes (-0.000169% from baseline)
- PIE enabled: 216878461 bytes (-54.14% from baseline) .text sections:
- PIE disabled: 9373572 bytes (+0.04% from baseline)
- PIE enabled: 9499138 bytes (+1.38% from baseline)
The big decrease in vmlinux file size is due to the lower number of relocations appended to the file.
diffstat:
arch/x86/Kconfig | 42 +++++ arch/x86/Makefile | 28 +++ arch/x86/boot/boot.h | 2 arch/x86/boot/compressed/Makefile | 5 arch/x86/boot/compressed/misc.c | 10 + arch/x86/crypto/aes-x86_64-asm_64.S | 45 +++--- arch/x86/crypto/aesni-intel_asm.S | 14 + arch/x86/crypto/aesni-intel_avx-x86_64.S | 6 arch/x86/crypto/camellia-aesni-avx-asm_64.S | 42 ++--- arch/x86/crypto/camellia-aesni-avx2-asm_64.S | 44 +++--- arch/x86/crypto/camellia-x86_64-asm_64.S | 8 - arch/x86/crypto/cast5-avx-x86_64-asm_64.S | 50 +++--- arch/x86/crypto/cast6-avx-x86_64-asm_64.S | 44 +++--- arch/x86/crypto/des3_ede-asm_64.S | 96 ++++++++----- arch/x86/crypto/ghash-clmulni-intel_asm.S | 4 arch/x86/crypto/glue_helper-asm-avx.S | 4 arch/x86/crypto/glue_helper-asm-avx2.S | 6 arch/x86/entry/entry_32.S | 3 arch/x86/entry/entry_64.S | 29 ++- arch/x86/include/asm/asm.h | 13 + arch/x86/include/asm/bug.h | 2 arch/x86/include/asm/jump_label.h | 8 - arch/x86/include/asm/kvm_host.h | 6 arch/x86/include/asm/module.h | 17 ++ arch/x86/include/asm/page_64_types.h | 9 + arch/x86/include/asm/paravirt_types.h | 12 + arch/x86/include/asm/percpu.h | 25 ++- arch/x86/include/asm/pm-trace.h | 2 arch/x86/include/asm/processor.h | 11 - arch/x86/include/asm/setup.h | 2 arch/x86/include/asm/stackprotector.h | 19 +- arch/x86/kernel/Makefile | 2 arch/x86/kernel/acpi/wakeup_64.S | 31 ++-- arch/x86/kernel/asm-offsets.c | 3 arch/x86/kernel/asm-offsets_32.c | 3 arch/x86/kernel/asm-offsets_64.c | 3 arch/x86/kernel/cpu/common.c | 7 arch/x86/kernel/head64.c | 30 +++- arch/x86/kernel/head_32.S | 3 arch/x86/kernel/head_64.S | 46 +++++- arch/x86/kernel/kvm.c | 6 arch/x86/kernel/module-plts.c | 198 +++++++++++++++++++++++++++ arch/x86/kernel/module.c | 18 +- arch/x86/kernel/module.lds | 4 arch/x86/kernel/process.c | 5 arch/x86/kernel/relocate_kernel_64.S | 8 - arch/x86/kernel/setup_percpu.c | 2 arch/x86/kernel/vmlinux.lds.S | 13 + arch/x86/kvm/svm.c | 4 arch/x86/lib/cmpxchg16b_emu.S | 8 - arch/x86/power/hibernate_asm_64.S | 4 arch/x86/tools/relocs.c | 134 +++++++++++++++--- arch/x86/tools/relocs.h | 4 arch/x86/tools/relocs_common.c | 15 +- arch/x86/xen/xen-asm.S | 12 - arch/x86/xen/xen-asm.h | 3 arch/x86/xen/xen-head.S | 9 - include/asm-generic/sections.h | 6 include/linux/compiler.h | 8 + init/Kconfig | 9 + kernel/kallsyms.c | 16 +- 61 files changed, 923 insertions(+), 299 deletions(-)
RFC v2 1/23 x86/crypto: Adapt assembly for PIE support
Change the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/crypto/aes-x86_64-asm_64.S | 45 ++++++++----- arch/x86/crypto/aesni-intel_asm.S | 14 ++-- arch/x86/crypto/aesni-intel_avx-x86_64.S | 6 +- arch/x86/crypto/camellia-aesni-avx-asm_64.S | 42 ++++++------ arch/x86/crypto/camellia-aesni-avx2-asm_64.S | 44 ++++++------- arch/x86/crypto/camellia-x86_64-asm_64.S | 8 ++- arch/x86/crypto/cast5-avx-x86_64-asm_64.S | 50 ++++++++------- arch/x86/crypto/cast6-avx-x86_64-asm_64.S | 44 +++++++------ arch/x86/crypto/des3_ede-asm_64.S | 96 ++++++++++++++++++---------- arch/x86/crypto/ghash-clmulni-intel_asm.S | 4 +- arch/x86/crypto/glue_helper-asm-avx.S | 4 +- arch/x86/crypto/glue_helper-asm-avx2.S | 6 +- 12 files changed, 211 insertions(+), 152 deletions(-)
arch/x86/crypto/aes-x86_64-asm_64.S +29 -16
@@ -48,8 +48,12 @@ #define R10 %r10 #define R11 %r11 +/* Hold global for PIE suport */ +#define RBASE %r12 + #define prologue(FUNC,KEY,B128,B192,r1,r2,r5,r6,r7,r8,r9,r10,r11) \ ENTRY(FUNC); \ + pushq RBASE; \ movq r1,r2; \ leaq KEY+48(r8),r9; \ movq r10,r11; \
@@ -74,54 +78,63 @@ movl r6 ## E,4(r9); \ movl r7 ## E,8(r9); \ movl r8 ## E,12(r9); \ + popq RBASE; \ ret; \ ENDPROC(FUNC); +#define round_mov(tab_off, reg_i, reg_o) \ + leaq tab_off(%rip), RBASE; \ + movl (RBASE,reg_i,4), reg_o; + +#define round_xor(tab_off, reg_i, reg_o) \ + leaq tab_off(%rip), RBASE; \ + xorl (RBASE,reg_i,4), reg_o; + #define round(TAB,OFFSET,r1,r2,r3,r4,r5,r6,r7,r8,ra,rb,rc,rd) \ movzbl r2 ## H,r5 ## E; \ movzbl r2 ## L,r6 ## E; \ - movl TAB+1024(,r5,4),r5 ## E;\ + round_mov(TAB+1024, r5, r5 ## E)\ movw r4 ## X,r2 ## X; \ - movl TAB(,r6,4),r6 ## E; \ + round_mov(TAB, r6, r6 ## E) \ roll $16,r2 ## E; \ shrl $16,r4 ## E; \ movzbl r4 ## L,r7 ## E; \ movzbl r4 ## H,r4 ## E; \ xorl OFFSET(r8),ra ## E; \ xorl OFFSET+4(r8),rb ## E; \ - xorl TAB+3072(,r4,4),r5 ## E;\ - xorl TAB+2048(,r7,4),r6 ## E;\ + round_xor(TAB+3072, r4, r5 ## E)\ + round_xor(TAB+2048, r7, r6 ## E)\ movzbl r1 ## L,r7 ## E; \ movzbl r1 ## H,r4 ## E; \ - movl TAB+1024(,r4,4),r4 ## E;\ + round_mov(TAB+1024, r4, r4 ## E)\ movw r3 ## X,r1 ## X; \ roll $16,r1 ## E; \ shrl $16,r3 ## E; \ - xorl TAB(,r7,4),r5 ## E; \ + round_xor(TAB, r7, r5 ## E) \ movzbl r3 ## L,r7 ## E; \ movzbl r3 ## H,r3 ## E; \ - xorl TAB+3072(,r3,4),r4 ## E;\ - xorl TAB+2048(,r7,4),r5 ## E;\ + round_xor(TAB+3072, r3, r4 ## E)\ + round_xor(TAB+2048, r7, r5 ## E)\ movzbl r1 ## L,r7 ## E; \ movzbl r1 ## H,r3 ## E; \ shrl $16,r1 ## E; \ - xorl TAB+3072(,r3,4),r6 ## E;\ - movl TAB+2048(,r7,4),r3 ## E;\ + round_xor(TAB+3072, r3, r6 ## E)\ + round_mov(TAB+2048, r7, r3 ## E)\ movzbl r1 ## L,r7 ## E; \ movzbl r1 ## H,r1 ## E; \ - xorl TAB+1024(,r1,4),r6 ## E;\ - xorl TAB(,r7,4),r3 ## E; \ + round_xor(TAB+1024, r1, r6 ## E)\ + round_xor(TAB, r7, r3 ## E) \ movzbl r2 ## H,r1 ## E; \ movzbl r2 ## L,r7 ## E; \ shrl $16,r2 ## E; \ - xorl TAB+3072(,r1,4),r3 ## E;\ - xorl TAB+2048(,r7,4),r4 ## E;\ + round_xor(TAB+3072, r1, r3 ## E)\ + round_xor(TAB+2048, r7, r4 ## E)\ movzbl r2 ## H,r1 ## E; \ movzbl r2 ## L,r2 ## E; \ xorl OFFSET+8(r8),rc ## E; \ xorl OFFSET+12(r8),rd ## E; \ - xorl TAB+1024(,r1,4),r3 ## E;\ - xorl TAB(,r2,4),r4 ## E; + round_xor(TAB+1024, r1, r3 ## E)\ + round_xor(TAB, r2, r4 ## E) #define move_regs(r1,r2,r3,r4) \ movl r3 ## E,r1 ## E; \
arch/x86/crypto/aesni-intel_asm.S +8 -6
@@ -325,7 +325,8 @@ _get_AAD_rest0\num_initial_blocks\operation: vpshufb and an array of shuffle masks */ movq %r12, %r11 salq $4, %r11 - movdqu aad_shift_arr(%r11), \TMP1 + leaq aad_shift_arr(%rip), %rax + movdqu (%rax,%r11,), \TMP1 PSHUFB_XMM \TMP1, %xmm\i _get_AAD_rest_final\num_initial_blocks\operation: PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
@@ -584,7 +585,8 @@ _get_AAD_rest0\num_initial_blocks\operation: vpshufb and an array of shuffle masks */ movq %r12, %r11 salq $4, %r11 - movdqu aad_shift_arr(%r11), \TMP1 + leaq aad_shift_arr(%rip), %rax + movdqu (%rax,%r11,), \TMP1 PSHUFB_XMM \TMP1, %xmm\i _get_AAD_rest_final\num_initial_blocks\operation: PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
@@ -2722,7 +2724,7 @@ ENDPROC(aesni_cbc_dec) */ .align 4 _aesni_inc_init: - movaps .Lbswap_mask, BSWAP_MASK + movaps .Lbswap_mask(%rip), BSWAP_MASK movaps IV, CTR PSHUFB_XMM BSWAP_MASK CTR mov $1, TCTR_LOW
@@ -2850,12 +2852,12 @@ ENTRY(aesni_xts_crypt8) cmpb $0, %cl movl $0, %ecx movl $240, %r10d - leaq _aesni_enc4, %r11 - leaq _aesni_dec4, %rax + leaq _aesni_enc4(%rip), %r11 + leaq _aesni_dec4(%rip), %rax cmovel %r10d, %ecx cmoveq %rax, %r11 - movdqa .Lgf128mul_x_ble_mask, GF128MUL_MASK + movdqa .Lgf128mul_x_ble_mask(%rip), GF128MUL_MASK movups (IVP), IV mov 480(KEYP), KLEN
arch/x86/crypto/aesni-intel_avx-x86_64.S +4 -2
@@ -454,7 +454,8 @@ _get_AAD_rest0\@: vpshufb and an array of shuffle masks */ movq %r12, %r11 salq $4, %r11 - movdqu aad_shift_arr(%r11), \T1 + leaq aad_shift_arr(%rip), %rax + movdqu (%rax,%r11,), \T1 vpshufb \T1, reg_i, reg_i _get_AAD_rest_final\@: vpshufb SHUF_MASK(%rip), reg_i, reg_i
@@ -1761,7 +1762,8 @@ _get_AAD_rest0\@: vpshufb and an array of shuffle masks */ movq %r12, %r11 salq $4, %r11 - movdqu aad_shift_arr(%r11), \T1 + leaq aad_shift_arr(%rip), %rax + movdqu (%rax,%r11,), \T1 vpshufb \T1, reg_i, reg_i _get_AAD_rest_final\@: vpshufb SHUF_MASK(%rip), reg_i, reg_i
arch/x86/crypto/camellia-aesni-avx-asm_64.S +21 -21
126 lines not on this page · raw message
arch/x86/crypto/camellia-aesni-avx2-asm_64.S +22 -22
127 lines not on this page · raw message
arch/x86/crypto/camellia-x86_64-asm_64.S +5 -3
@@ -92,11 +92,13 @@ #define RXORbl %r9b #define xor2ror16(T0, T1, tmp1, tmp2, ab, dst) \ + leaq T0(%rip), tmp1; \ movzbl ab ## bl, tmp2 ## d; \ + xorq (tmp1, tmp2, 8), dst; \ + leaq T1(%rip), tmp2; \ movzbl ab ## bh, tmp1 ## d; \ - rorq $16, ab; \ - xorq T0(, tmp2, 8), dst; \ - xorq T1(, tmp1, 8), dst; + xorq (tmp2, tmp1, 8), dst; \ + rorq $16, ab; /********************************************************************** 1-way camellia
arch/x86/crypto/cast5-avx-x86_64-asm_64.S +27 -23
98 lines not on this page · raw message
arch/x86/crypto/cast6-avx-x86_64-asm_64.S +24 -20
82 lines not on this page · raw message
arch/x86/crypto/des3_ede-asm_64.S +64 -32
150 lines not on this page · raw message
arch/x86/crypto/ghash-clmulni-intel_asm.S +2 -2
@@ -97,7 +97,7 @@ ENTRY(clmul_ghash_mul) FRAME_BEGIN movups (%rdi), DATA movups (%rsi), SHASH - movaps .Lbswap_mask, BSWAP + movaps .Lbswap_mask(%rip), BSWAP PSHUFB_XMM BSWAP DATA call __clmul_gf128mul_ble PSHUFB_XMM BSWAP DATA
@@ -114,7 +114,7 @@ ENTRY(clmul_ghash_update) FRAME_BEGIN cmp $16, %rdx jb .Lupdate_just_ret # check length - movaps .Lbswap_mask, BSWAP + movaps .Lbswap_mask(%rip), BSWAP movups (%rdi), DATA movups (%rcx), SHASH PSHUFB_XMM BSWAP DATA
arch/x86/crypto/glue_helper-asm-avx.S +2 -2
@@ -54,7 +54,7 @@ #define load_ctr_8way(iv, bswap, x0, x1, x2, x3, x4, x5, x6, x7, t0, t1, t2) \ vpcmpeqd t0, t0, t0; \ vpsrldq $8, t0, t0; /* low: -1, high: 0 */ \ - vmovdqa bswap, t1; \ + vmovdqa bswap(%rip), t1; \ \ /* load IV and byteswap */ \ vmovdqu (iv), x7; \
@@ -99,7 +99,7 @@ #define load_xts_8way(iv, src, dst, x0, x1, x2, x3, x4, x5, x6, x7, tiv, t0, \ t1, xts_gf128mul_and_shl1_mask) \ - vmovdqa xts_gf128mul_and_shl1_mask, t0; \ + vmovdqa xts_gf128mul_and_shl1_mask(%rip), t0; \ \ /* load IV */ \ vmovdqu (iv), tiv; \
arch/x86/crypto/glue_helper-asm-avx2.S +3 -3
@@ -67,7 +67,7 @@ vmovdqu (iv), t2x; \ vmovdqa t2x, t3x; \ inc_le128(t2x, t0x, t1x); \ - vbroadcasti128 bswap, t1; \ + vbroadcasti128 bswap(%rip), t1; \ vinserti128 $1, t2x, t3, t2; /* ab: le0 ; cd: le1 */ \ vpshufb t1, t2, x0; \ \
@@ -124,13 +124,13 @@ tivx, t0, t0x, t1, t1x, t2, t2x, t3, \ xts_gf128mul_and_shl1_mask_0, \ xts_gf128mul_and_shl1_mask_1) \ - vbroadcasti128 xts_gf128mul_and_shl1_mask_0, t1; \ + vbroadcasti128 xts_gf128mul_and_shl1_mask_0(%rip), t1; \ \ /* load IV and construct second IV */ \ vmovdqu (iv), tivx; \ vmovdqa tivx, t0x; \ gf128mul_x_ble(tivx, t1x, t2x); \ - vbroadcasti128 xts_gf128mul_and_shl1_mask_1, t2; \ + vbroadcasti128 xts_gf128mul_and_shl1_mask_1(%rip), t2; \ vinserti128 $1, tivx, t0, tiv; \ vpxor (0*32)(src), tiv, x0; \ vmovdqu tiv, (0*32)(dst); \
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 2/23 x86: Use symbol name on bug table for PIE support
In reply to Thomas GarnierReplace the %c constraint with %P. The %c is incompatible with PIE because it implies an immediate value whereas %P reference a symbol.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/include/asm/bug.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bug.h +1 -1
@@ -37,7 +37,7 @@ do { \ asmvolatile ("1:\t" ins"\n" \".pushsection __bug_table,\"aw\"\n" \ "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \ - "\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \ + "\t" __BUG_REL(%P0) "\t# bug_entry::file\n" \"\t.word %c1" "\t# bug_entry::line\n" \"\t.word %c2" "\t# bug_entry::flags\n" \"\t.org 2b+%c3\n" \
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 3/23 x86: Use symbol name in jump table for PIE support
In reply to Thomas GarnierReplace the %c constraint with %P. The %c is incompatible with PIE because it implies an immediate value whereas %P reference a symbol.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/include/asm/jump_label.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/jump_label.h +4 -4
@@ -36,9 +36,9 @@ static __always_inline bool arch_static_branch(struct static_key *key, bool bran ".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t" ".pushsection __jump_table, \"aw\" \n\t" _ASM_ALIGN "\n\t" - _ASM_PTR"1b, %l[l_yes], %c0 + %c1 \n\t" + _ASM_PTR "1b, %l[l_yes], %P0 \n\t" ".popsection \n\t" - : : "i" (key), "i" (branch) : : l_yes); + : : "X" (&((char *)key)[branch]) : : l_yes);return false; l_yes:
@@ -52,9 +52,9 @@ static __always_inline bool arch_static_branch_jump(struct static_key *key, bool "2:\n\t" ".pushsection __jump_table, \"aw\" \n\t" _ASM_ALIGN "\n\t" - _ASM_PTR"1b, %l[l_yes], %c0 + %c1 \n\t" + _ASM_PTR "1b, %l[l_yes], %P0 \n\t" ".popsection \n\t" - : : "i" (key), "i" (branch) : : l_yes); + : : "X" (&((char *)key)[branch]) : : l_yes);return false; l_yes:
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 4/23 x86: Add macro to get symbol address for PIE support
In reply to Thomas GarnierAdd a new _ASM_GET_PTR macro to fetch a symbol address. It will be used to replace "_ASM_MOV $<symbol>, %dst" code construct that are not compatible with PIE.
--- arch/x86/include/asm/asm.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/x86/include/asm/asm.h +13 -0
@@ -55,6 +55,19 @@# define CC_OUT (c) [_cc_ ## c] "=qm"#endif +/* Macros to get a global variable address with PIE support on 64-bit */ +#ifdef CONFIG_X86_32 +#define __ASM_GET_PTR_PRE (_src) __ASM_FORM_COMMA(movl $##_src) +#else +#ifdef __ASSEMBLY__ +#define __ASM_GET_PTR_PRE (_src) __ASM_FORM_COMMA(leaq (_src)(%rip)) +#else +#define __ASM_GET_PTR_PRE (_src) __ASM_FORM_COMMA(leaq (_src)(%%rip)) +#endif +#endif +#define _ASM_GET_PTR (_src, _dst) \ + __ASM_GET_PTR_PRE(_src) __ASM_FORM(_dst) +/* Exception table entry */ #ifdef __ASSEMBLY__# define _ASM_EXTABLE_HANDLE (from, to, handler) \
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 5/23 xen: Adapt assembly for PIE support
In reply to Thomas GarnierChange the assembly code to use the new _ASM_GET_PTR macro which get a symbol reference while being PIE compatible. Modify the RELOC macro that was using an assignment generating a non-relative reference.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/xen/xen-asm.h | 3 ++- arch/x86/xen/xen-head.S | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-)
arch/x86/xen/xen-asm.h +2 -1
@@ -2,8 +2,9 @@#define _XEN_XEN_ASM_H#include <linux/linkage.h> +#include <asm/asm.h> -#define RELOC (x, v) .globl x##_reloc; x##_reloc=v +#define RELOC (x, v) .globl x##_reloc; x##_reloc: _ASM_PTR v#define ENDPATCH (x) .globl x##_end; x##_end=./* Pseudo-flag used for virtual NMI, which we don't implement yet */
arch/x86/xen/xen-head.S +5 -4
@@ -23,14 +23,15 @@ ENTRY(startup_xen) /* Clear .bss */ xor %eax,%eax - mov $__bss_start, %_ASM_DI - mov $__bss_stop, %_ASM_CX + _ASM_GET_PTR(__bss_start, %_ASM_DI) + _ASM_GET_PTR(__bss_stop, %_ASM_CX) sub %_ASM_DI, %_ASM_CX shr $__ASM_SEL(2, 3), %_ASM_CX rep __ASM_SIZE(stos) - mov %_ASM_SI, xen_start_info - mov $init_thread_union+THREAD_SIZE, %_ASM_SP + _ASM_GET_PTR(xen_start_info, %_ASM_AX) + mov %_ASM_SI, (%_ASM_AX) + _ASM_GET_PTR(init_thread_union+THREAD_SIZE, %_ASM_SP) jmp xen_start_kernel
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 6/23 kvm: Adapt assembly for PIE support
In reply to Thomas GarnierChange the assembly code to use only relative references of symbols for the kernel to be PIE compatible. The new __ASM_GET_PTR_PRE macro is used to get the address of a symbol on both 32 and 64-bit with PIE support.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/include/asm/kvm_host.h | 6 ++++-- arch/x86/kernel/kvm.c | 6 ++++-- arch/x86/kvm/svm.c | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -2
@@ -1352,9 +1352,11 @@ asmlinkage void kvm_spurious_fault(void);".pushsection .fixup, \"ax\" \n" \"667: \n\t" \ cleanup_insn"\n\t" \ -"cmpb $0, kvm_rebooting \n\t" \ + "cmpb $0, kvm_rebooting" __ASM_SEL(,(%%rip)) " \n\t" \"jne 668b \n\t" \ - __ASM_SIZE(push) " $666b \n\t" \ + __ASM_SIZE(push) "%%" _ASM_AX " \n\t" \ + __ASM_GET_PTR_PRE(666b) "%%" _ASM_AX "\n\t" \ +"xchg %%" _ASM_AX", (%%" _ASM_SP") \n\t" \"call kvm_spurious_fault \n\t" \".popsection \n\t" \ _ASM_EXTABLE(666b ,667b )
arch/x86/kernel/kvm.c +4 -2
@@ -620,8 +620,10 @@ asm(".global __raw_callee_save___kvm_vcpu_is_preempted;" ".type __raw_callee_save___kvm_vcpu_is_preempted, @function;" "__raw_callee_save___kvm_vcpu_is_preempted:" -"movq __per_cpu_offset(,%rdi,8), %rax;" -"cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);" +"leaq __per_cpu_offset(%rip), %rax;" +"movq (%rax,%rdi,8), %rax;" +"addq " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rip), %rax;" +"cmpb $0, (%rax);" "setne %al;" "ret;" ".popsection");
arch/x86/kvm/svm.c +2 -2
@@ -554,12 +554,12 @@ static u32 svm_msrpm_offset(u32 msr)static inline void clgi (void ) { - asm volatile (__ex(SVM_CLGI)); + asm volatile (__ex(SVM_CLGI) : :); }static inline void stgi (void ) { - asm volatile (__ex(SVM_STGI)); + asm volatile (__ex(SVM_STGI) : :); }static inline void invlpga (unsigned long addr,u32 asid)
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 7/23 x86: relocate_kernel - Adapt assembly for PIE support
In reply to Thomas GarnierChange the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/kernel/relocate_kernel_64.S | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kernel/relocate_kernel_64.S +5 -3
@@ -200,9 +200,11 @@ identity_mapped: movq %rax, %cr3 lea PAGE_SIZE(%r8), %rsp call swap_pages - movq $virtual_mapped, %rax - pushq %rax - ret + jmp *virtual_mapped_addr(%rip) + + /* Absolute value for PIE support */ +virtual_mapped_addr: + .quad virtual_mapped virtual_mapped: movq RSP(%r8), %rsp
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 8/23 x86/entry/64: Adapt assembly for PIE support
In reply to Thomas GarnierChange the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/entry/entry_64.S | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
arch/x86/entry/entry_64.S +15 -7
@@ -199,12 +199,15 @@ entry_SYSCALL_64_fastpath: ja 1f /* return -ENOSYS (already in pt_regs->ax) */ movq %r10, %rcx + /* Ensures the call is position independent */ + leaq sys_call_table(%rip), %r11 + /* * This call instruction is handled specially in stub_ptregs_64. * It might end up jumping to the slow path. If it jumps, RAX * and all argument registers are clobbered. */ - call *sys_call_table(, %rax, 8) + call *(%r11, %rax, 8) .Lentry_SYSCALL_64_after_fastpath_call: movq %rax, RAX(%rsp)
@@ -339,7 +342,8 @@ ENTRY(stub_ptregs_64) * RAX stores a pointer to the C function implementing the syscall. * IRQs are on. */ - cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp) + leaq .Lentry_SYSCALL_64_after_fastpath_call(%rip), %r11 + cmpq %r11, (%rsp) jne 1f /*
@@ -1210,7 +1214,8 @@ ENTRY(error_entry) movl %ecx, %eax /* zero extend */ cmpq %rax, RIP+8(%rsp) je .Lbstep_iret - cmpq $.Lgs_change, RIP+8(%rsp) + leaq .Lgs_change(%rip), %rcx + cmpq %rcx, RIP+8(%rsp) jne .Lerror_entry_done /*
@@ -1430,10 +1435,10 @@ ENTRY(nmi) * resume the outer NMI. */ - movq $repeat_nmi, %rdx + leaq repeat_nmi(%rip), %rdx cmpq 8(%rsp), %rdx ja 1f - movq $end_repeat_nmi, %rdx + leaq end_repeat_nmi(%rip), %rdx cmpq 8(%rsp), %rdx ja nested_nmi_out 1:
@@ -1487,7 +1492,8 @@ nested_nmi: pushq %rdx pushfq pushq $__KERNEL_CS - pushq $repeat_nmi + leaq repeat_nmi(%rip), %rdx + pushq %rdx /* Put stack back */ addq $(6*8), %rsp
@@ -1526,7 +1532,9 @@ first_nmi: addq $8, (%rsp) /* Fix up RSP */ pushfq /* RFLAGS */ pushq $__KERNEL_CS /* CS */ - pushq $1f /* RIP */ + pushq %rax /* Support Position Independent Code */ + leaq 1f(%rip), %rax /* RIP */ + xchgq %rax, (%rsp) /* Restore RAX, put 1f */ INTERRUPT_RETURN /* continues at repeat_nmi below */ UNWIND_HINT_IRET_REGS 1:
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 9/23 x86: pm-trace - Adapt assembly for PIE support
In reply to Thomas GarnierChange assembly to use the new _ASM_GET_PTR macro instead of _ASM_MOV for the assembly to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/include/asm/pm-trace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/pm-trace.h +1 -1
@@ -7,7 +7,7 @@ do { \ if (pm_trace_enabled) { \ const void *tracedata; \ - asm volatile(_ASM_MOV " $1f,%0\n" \ + asm volatile(_ASM_GET_PTR(1f, %0) "\n" \ ".section .tracedata,\"a\"\n" \ "1:\t.word %c1\n\t" \ _ASM_PTR " %c2\n" \
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 10/23 x86/CPU: Adapt assembly for PIE support
In reply to Thomas GarnierChange the assembly code to use only relative references of symbols for the kernel to be PIE compatible. Use the new _ASM_GET_PTR macro instead of the 'mov $symbol, %dst' construct to not have an absolute reference.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/include/asm/processor.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/include/asm/processor.h +5 -3
@@ -49,7 +49,7 @@ static inline void *current_text_addr(void) {void *pc; - asmvolatile ("mov $1f, %0; 1:" :"=r" (pc)); + asm volatile(_ASM_GET_PTR(1f, %0) "; 1:":"=r" (pc));return pc; }
@@ -689,6 +689,7 @@ static inline void sync_core(void) : "+r" (__sp) : : "memory");#else unsigned int tmp; +unsigned long tmp2; asm volatile ( UNWIND_HINT_SAVE
@@ -699,11 +700,12 @@ static inline void sync_core(void)"pushfq\n\t" "mov %%cs, %0\n\t" "pushq %q0\n\t" -"pushq $1f\n\t" +"leaq 1f(%%rip), %1\n\t" +"pushq %1\n\t" "iretq\n\t" UNWIND_HINT_RESTORE "1:" - : "=&r" (tmp), "+r" (__sp) : : "cc", "memory"); + : "=&r" (tmp), "=&r" (tmp2), "+r" (__sp) : : "cc", "memory"); #endif }
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 11/23 x86/acpi: Adapt assembly for PIE support
In reply to Thomas GarnierChange the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/kernel/acpi/wakeup_64.S | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
arch/x86/kernel/acpi/wakeup_64.S +16 -15
@@ -14,7 +14,7 @@ * Hooray, we are in Long 64-bit mode (but still running in low memory) */ ENTRY(wakeup_long64) - movq saved_magic, %rax + movq saved_magic(%rip), %rax movq $0x123456789abcdef0, %rdx cmpq %rdx, %rax jne bogus_64_magic
@@ -25,14 +25,14 @@ ENTRY(wakeup_long64) movw %ax, %es movw %ax, %fs movw %ax, %gs - movq saved_rsp, %rsp + movq saved_rsp(%rip), %rsp - movq saved_rbx, %rbx - movq saved_rdi, %rdi - movq saved_rsi, %rsi - movq saved_rbp, %rbp + movq saved_rbx(%rip), %rbx + movq saved_rdi(%rip), %rdi + movq saved_rsi(%rip), %rsi + movq saved_rbp(%rip), %rbp - movq saved_rip, %rax + movq saved_rip(%rip), %rax jmp *%rax ENDPROC(wakeup_long64)
@@ -45,7 +45,7 @@ ENTRY(do_suspend_lowlevel) xorl %eax, %eax call save_processor_state - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax) movq %rsi, pt_regs_si(%rax)
@@ -64,13 +64,14 @@ ENTRY(do_suspend_lowlevel) pushfq popq pt_regs_flags(%rax) - movq $.Lresume_point, saved_rip(%rip) + leaq .Lresume_point(%rip), %rax + movq %rax, saved_rip(%rip) - movq %rsp, saved_rsp - movq %rbp, saved_rbp - movq %rbx, saved_rbx - movq %rdi, saved_rdi - movq %rsi, saved_rsi + movq %rsp, saved_rsp(%rip) + movq %rbp, saved_rbp(%rip) + movq %rbx, saved_rbx(%rip) + movq %rdi, saved_rdi(%rip) + movq %rsi, saved_rsi(%rip) addq $8, %rsp movl $3, %edi
@@ -82,7 +83,7 @@ ENTRY(do_suspend_lowlevel) .align 4 .Lresume_point: /* We don't restore %rax, it must be 0 anyway */ - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq saved_context_cr4(%rax), %rbx movq %rbx, %cr4 movq saved_context_cr3(%rax), %rbx
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 12/23 x86/boot/64: Adapt assembly for PIE support
In reply to Thomas GarnierChange the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Early at boot, the kernel is mapped at a temporary address while preparing the page table. To know the changes needed for the page table with KASLR, the boot code calculate the difference between the expected address of the kernel and the one chosen by KASLR. It does not work with PIE because all symbols in code are relatives. Instead of getting the future relocated virtual address, you will get the current temporary mapping. The solution is using global variables that will be relocated as expected.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/kernel/head_64.S | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
arch/x86/kernel/head_64.S +23 -8
@@ -85,8 +85,23 @@ startup_64: popq %rsi /* Form the CR3 value being sure to include the CR3 modifier */ - addq $(early_top_pgt - __START_KERNEL_map), %rax + addq _early_top_pgt_offset(%rip), %rax jmp 1f + + /* + * Position Independent Code takes only relative references in code + * meaning a global variable address is relative to RIP and not its + * future virtual address. Global variables can be used instead as they + * are still relocated on the expected kernel mapping address. + */ + .align 8 +_early_top_pgt_offset: + .quad early_top_pgt - __START_KERNEL_map +_init_top_offset: + .quad init_top_pgt - __START_KERNEL_map +_va_jump: + .quad 2f + ENTRY(secondary_startup_64) /* * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0,
@@ -114,7 +129,7 @@ ENTRY(secondary_startup_64) popq %rsi /* Form the CR3 value being sure to include the CR3 modifier */ - addq $(init_top_pgt - __START_KERNEL_map), %rax + addq _init_top_offset(%rip), %rax 1: /* Enable PAE mode, PGE and LA57 */
@@ -129,9 +144,8 @@ ENTRY(secondary_startup_64) movq %rax, %cr3 /* Ensure I am executing from virtual addresses */ - movq $1f, %rax - jmp *%rax -1: + jmp *_va_jump(%rip) +2: /* Check if nx is implemented */ movl $0x80000001, %eax
@@ -227,11 +241,12 @@ ENTRY(secondary_startup_64) * REX.W + FF /5 JMP m16:64 Jump far, absolute indirect, * address given in m16:64. */ - pushq $.Lafter_lret # put return address on stack for unwinder + leaq .Lafter_lret(%rip), %rax + pushq %rax # put return address on stack for unwinder xorq %rbp, %rbp # clear frame pointer - movq initial_code(%rip), %rax + leaq initial_code(%rip), %rax pushq $__KERNEL_CS # set correct cs - pushq %rax # target address in negative space + pushq (%rax) # target address in negative space lretq .Lafter_lret: ENDPROC(secondary_startup_64)
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 13/23 x86/power/64: Adapt assembly for PIE support
In reply to Thomas GarnierChange the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/power/hibernate_asm_64.S | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/power/hibernate_asm_64.S +2 -2
@@ -24,7 +24,7 @@ #include <asm/frame.h> ENTRY(swsusp_arch_suspend) - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax) movq %rsi, pt_regs_si(%rax)
@@ -115,7 +115,7 @@ ENTRY(restore_registers) movq %rax, %cr4; # turn PGE back on /* We don't restore %rax, it must be 0 anyway */ - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq pt_regs_sp(%rax), %rsp movq pt_regs_bp(%rax), %rbp movq pt_regs_si(%rax), %rsi
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
[kernel-hardening] Re: [RFC v2 13/23] x86/power/64: Adapt assembly for PIE support
Quoted 7 lines · Thomas Garnier
Change the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
Signed-off-by Thomas Garnier <[email protected]>
Quoted 20 lines
arch/x86/power/hibernate_asm_64.S +2
-2@@ -24,7 +24,7 @@ #include <asm/frame.h> ENTRY(swsusp_arch_suspend) - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax) movq %rsi, pt_regs_si(%rax)@@ -115,7 +115,7 @@ ENTRY(restore_registers) movq %rax, %cr4; # turn PGE back on /* We don't restore %rax, it must be 0 anyway */ - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq pt_regs_sp(%rax), %rsp movq pt_regs_bp(%rax), %rbp movq pt_regs_si(%rax), %rsi
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
[kernel-hardening] Re: [RFC v2 13/23] x86/power/64: Adapt assembly for PIE support
Quoted 10 lines · Pavel Machek
Quoted 7 lines · Thomas Garnier
Change the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
Signed-off-by Thomas Garnier <[email protected]>Acked-by Pavel Machek <[email protected]>
Thanks Pavel!
Quoted 25 lines
Quoted 13 lines
arch/x86/power/hibernate_asm_64.S +1
-1@@ -24,7 +24,7 @@ #include <asm/frame.h> ENTRY(swsusp_arch_suspend) - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax) movq %rsi, pt_regs_si(%rax)arch/x86/power/hibernate_asm_64.S ENTRY(restore_registers) · line 121
Quoted 8 lines
arch/x86/power/hibernate_asm_64.S +1
-1@@ -115,7 +115,7 @@ ENTRY(restore_registers) movq %rax, %cr4; # turn PGE back on /* We don't restore %rax, it must be 0 anyway */ - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq pt_regs_sp(%rax), %rsp movq pt_regs_bp(%rax), %rbp movq pt_regs_si(%rax), %rsi--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Signature · 1 line
Thomas
Re: [RFC v2 13/23] x86/power/64: Adapt assembly for PIE support
In reply to Pavel MachekQuoted 10 lines · Pavel Machek
Quoted 7 lines · Thomas Garnier
Change the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
Signed-off-by Thomas Garnier <[email protected]>Acked-by Pavel Machek <[email protected]>
Thanks Pavel!
Quoted 25 lines
Quoted 13 lines
arch/x86/power/hibernate_asm_64.S +1
-1@@ -24,7 +24,7 @@ #include <asm/frame.h> ENTRY(swsusp_arch_suspend) - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax) movq %rsi, pt_regs_si(%rax)arch/x86/power/hibernate_asm_64.S ENTRY(restore_registers) · line 121
Quoted 8 lines
arch/x86/power/hibernate_asm_64.S +1
-1@@ -115,7 +115,7 @@ ENTRY(restore_registers) movq %rax, %cr4; # turn PGE back on /* We don't restore %rax, it must be 0 anyway */ - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq pt_regs_sp(%rax), %rsp movq pt_regs_bp(%rax), %rbp movq pt_regs_si(%rax), %rsi--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [RFC v2 13/23] x86/power/64: Adapt assembly for PIE support
In reply to Thomas GarnierQuoted 7 lines · Thomas Garnier
Change the assembly code to use only relative references of symbols for the kernel to be PIE compatible.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
Signed-off-by Thomas Garnier <[email protected]>
Quoted 20 lines
arch/x86/power/hibernate_asm_64.S +2
-2@@ -24,7 +24,7 @@ #include <asm/frame.h> ENTRY(swsusp_arch_suspend) - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax) movq %rsi, pt_regs_si(%rax)@@ -115,7 +115,7 @@ ENTRY(restore_registers) movq %rax, %cr4; # turn PGE back on /* We don't restore %rax, it must be 0 anyway */ - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq pt_regs_sp(%rax), %rsp movq pt_regs_bp(%rax), %rbp movq pt_regs_si(%rax), %rsi
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
RFC v2 14/23 x86/paravirt: Adapt assembly for PIE support
In reply to Thomas Garnierif PIE is enabled, switch the paravirt assembly constraints to be compatible. The %c/i constrains generate smaller code so is kept by default.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/include/asm/paravirt_types.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/include/asm/paravirt_types.h +10 -2
@@ -347,9 +347,17 @@ extern struct pv_lock_ops pv_lock_ops;#define PARAVIRT_PATCH (x) \ (offsetof(struct paravirt_patch_template, x) / sizeof(void *)) +#ifdef CONFIG_X86_PIE +#define paravirt_opptr_call "a" +#define paravirt_opptr_type "p" +#else +#define paravirt_opptr_call "c" +#define paravirt_opptr_type "i" +#endif +#define paravirt_type (op) \ [paravirt_typenum] "i" (PARAVIRT_PATCH(op)), \ - [paravirt_opptr] "i" (&(op)) + [paravirt_opptr] paravirt_opptr_type (&(op))#define paravirt_clobber (clobber) \ [paravirt_clobber] "i" (clobber)
@@ -403,7 +411,7 @@ int paravirt_disable_iospace(void);* offset into the paravirt_patch_template structure, and can therefore be * freely converted back into a structure offset. */ -#define PARAVIRT_CALL "call *%c[paravirt_opptr];" +#define PARAVIRT_CALL "call *%" paravirt_opptr_call "[paravirt_opptr];"/* * These macros are intended to wrap calls through one of the paravirt
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 15/23 x86/boot/64: Use _text in a global for PIE support
In reply to Thomas GarnierBy default PIE generated code create only relative references so _text points to the temporary virtual address. Instead use a global variable so the relocation is done as expected.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/kernel/head64.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kernel/head64.c +9 -3
@@ -45,8 +45,14 @@ static void __head *fixup_pointer(void *ptr, unsigned long physaddr)return ptr - (void *)_text + (void *)physaddr; } -unsigned long __head __startup_64 (unsigned long physaddr, -struct boot_params *bp) +/* +* Use a global variable to properly calculate _text delta on PIE. By default +* a PIE binary do a RIP relative difference instead of the relocated address. +*/ +unsigned long _text_offset = (unsigned long )(_text - __START_KERNEL_map); + +unsigned long __head notrace __startup_64(unsigned long physaddr, +struct boot_params *bp) {unsigned long load_delta, *p;unsigned long pgtable_flags;
@@ -64,7 +70,7 @@ unsigned long __head __startup_64(unsigned long physaddr,* Compute the delta between the address I am compiled to run at * and the address I am actually running at. */ - load_delta = physaddr - (unsigned long )(_text - __START_KERNEL_map); + load_delta = physaddr - _text_offset;/* Is the address not 2M aligned? */ if (load_delta & ~PMD_PAGE_MASK )
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 16/23 x86/percpu: Adapt percpu for PIE support
In reply to Thomas GarnierPerpcu uses a clever design where the .percu ELF section has a virtual address of zero and the relocation code avoid relocating specific symbols. It makes the code simple and easily adaptable with or without SMP support.
This design is incompatible with PIE because generated code always try to access the zero virtual address relative to the default mapping address. It becomes impossible when KASLR is configured to go below -2G. This patch solves this problem by removing the zero mapping and adapting the GS base to be relative to the expected address. These changes are done only when PIE is enabled. The original implementation is kept as-is by default.
The assembly and PER_CPU macros are changed to use relative references when PIE is enabled.
The KALLSYMS_ABSOLUTE_PERCPU configuration is disabled with PIE given percpu symbols are not absolute in this case.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/entry/entry_64.S | 4 ++-- arch/x86/include/asm/percpu.h | 25 +++++++++++++++++++------ arch/x86/kernel/cpu/common.c | 4 +++- arch/x86/kernel/head_64.S | 4 ++++ arch/x86/kernel/setup_percpu.c | 2 +- arch/x86/kernel/vmlinux.lds.S | 13 +++++++++++-- arch/x86/lib/cmpxchg16b_emu.S | 8 ++++---- arch/x86/xen/xen-asm.S | 12 ++++++------ init/Kconfig | 2 +- 9 files changed, 51 insertions(+), 23 deletions(-)
arch/x86/entry/entry_64.S +2 -2
@@ -397,7 +397,7 @@ ENTRY(__switch_to_asm) #ifdef CONFIG_CC_STACKPROTECTOR movq TASK_stack_canary(%rsi), %rbx - movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset + movq %rbx, PER_CPU_VAR(irq_stack_union + stack_canary_offset) #endif /* restore callee-saved registers */
@@ -831,7 +831,7 @@ apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt /* * Exception entry points. */ -#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8) +#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss + (TSS_ist + ((x) - 1) * 8)) .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 ENTRY(\sym)
arch/x86/include/asm/percpu.h +19 -6
@@ -4,9 +4,11 @@#ifdef CONFIG_X86_64 #define __percpu_seg gs#define __percpu_mov_op movq +#define __percpu_rel (%rip)#else #define __percpu_seg fs#define __percpu_mov_op movl +#define __percpu_rel#endif #ifdef __ASSEMBLY__
@@ -27,10 +29,14 @@#define PER_CPU (var, reg) \ __percpu_mov_op %__percpu_seg:this_cpu_off, reg; \ lea var(reg), reg -#define PER_CPU_VAR (var) %__percpu_seg:var +/* Compatible with Position Independent Code */ +#define PER_CPU_VAR (var) %__percpu_seg:(var)##__percpu_rel +/* Rare absolute reference */ +#define PER_CPU_VAR_ABS (var) %__percpu_seg:var#else /* ! SMP */ #define PER_CPU (var, reg) __percpu_mov_op $var, reg -#define PER_CPU_VAR (var) var +#define PER_CPU_VAR (var) (var)##__percpu_rel +#define PER_CPU_VAR_ABS (var) var#endif /* SMP */ #ifdef CONFIG_X86_64_SMP
@@ -208,27 +214,34 @@ do { \ pfo_ret__; \ }) +/* Position Independent code uses relative addresses only */ +#ifdef CONFIG_X86_PIE +#define __percpu_stable_arg __percpu_arg(a1) +#else +#define __percpu_stable_arg __percpu_arg(P1) +#endif +#define percpu_stable_op (op, var) \ ({ \ typeof(var) pfo_ret__; \ switch (sizeof(var)) { \ case 1: \ - asm(op "b "__percpu_arg(P1)",%0" \ + asm(op "b "__percpu_stable_arg ",%0" \ : "=q" (pfo_ret__) \ : "p" (&(var))); \ break; \ case 2: \ - asm(op "w "__percpu_arg(P1)",%0" \ + asm(op "w "__percpu_stable_arg ",%0" \ : "=r" (pfo_ret__) \ : "p" (&(var))); \ break; \ case 4: \ - asm(op "l "__percpu_arg(P1)",%0" \ + asm(op "l "__percpu_stable_arg ",%0" \ : "=r" (pfo_ret__) \ : "p" (&(var))); \ break; \ case 8: \ - asm(op "q "__percpu_arg(P1)",%0" \ + asm(op "q "__percpu_stable_arg ",%0" \ : "=r" (pfo_ret__) \ : "p" (&(var))); \ break; \
arch/x86/kernel/cpu/common.c +3 -1
@@ -480,7 +480,9 @@ void load_percpu_segment(int cpu)loadsegment (fs, __KERNEL_PERCPU);#else __loadsegment_simple (gs,0 ); -wrmsrl (MSR_GS_BASE , (unsigned long )per_cpu (irq_stack_union.gs_base, cpu)); +wrmsrl (MSR_GS_BASE , + (unsigned long )per_cpu (irq_stack_union.gs_base, cpu) - + (unsigned long )__per_cpu_start);#endif load_stack_canary_segment (); }
arch/x86/kernel/head_64.S +4 -0
@@ -271,7 +271,11 @@ ENDPROC(start_cpu0) GLOBAL(initial_code) .quad x86_64_start_kernel GLOBAL(initial_gs) +#ifdef CONFIG_X86_PIE + .quad 0 +#else .quad INIT_PER_CPU_VAR(irq_stack_union) +#endif GLOBAL(initial_stack) /* * The SIZEOF_PTREGS gap is a convention which helps the in-kernel
arch/x86/kernel/setup_percpu.c +1 -1
@@ -25,7 +25,7 @@DEFINE_PER_CPU_READ_MOSTLY (int, cpu_number);EXPORT_PER_CPU_SYMBOL (cpu_number); -#ifdef CONFIG_X86_64 +#if defined(CONFIG_X86_64 ) && !defined(CONFIG_X86_PIE )#define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)#else #define BOOT_PERCPU_OFFSET 0
arch/x86/kernel/vmlinux.lds.S +11 -2
@@ -186,9 +186,14 @@ SECTIONS /* * percpu offsets are zero-based on SMP. PERCPU_VADDR() changes the * output PHDR, so the next output section - .init.text - should - * start another segment - init. + * start another segment - init. For Position Independent Code, the + * per-cpu section cannot be zero-based because everything is relative. */ +#ifdef CONFIG_X86_PIE + PERCPU_SECTION(INTERNODE_CACHE_BYTES) +#else PERCPU_VADDR(INTERNODE_CACHE_BYTES, 0, :percpu) +#endif ASSERT(SIZEOF(.data..percpu) < CONFIG_PHYSICAL_START, "per-CPU data too large - increase CONFIG_PHYSICAL_START") #endif
@@ -364,7 +369,11 @@ SECTIONS * Per-cpu symbols which need to be offset from __per_cpu_load * for the boot processor. */ +#ifdef CONFIG_X86_PIE +#define INIT_PER_CPU(x) init_per_cpu__##x = x +#else #define INIT_PER_CPU(x) init_per_cpu__##x = x + __per_cpu_load +#endif INIT_PER_CPU(gdt_page); INIT_PER_CPU(irq_stack_union);
@@ -374,7 +383,7 @@ INIT_PER_CPU(irq_stack_union); . = ASSERT((_end - _text <= KERNEL_IMAGE_SIZE), "kernel image bigger than KERNEL_IMAGE_SIZE"); -#ifdef CONFIG_SMP +#if defined(CONFIG_SMP) && !defined(CONFIG_X86_PIE) . = ASSERT((irq_stack_union == 0), "irq_stack_union is not at start of per-cpu area"); #endif
arch/x86/lib/cmpxchg16b_emu.S +4 -4
@@ -33,13 +33,13 @@ ENTRY(this_cpu_cmpxchg16b_emu) pushfq cli - cmpq PER_CPU_VAR((%rsi)), %rax + cmpq PER_CPU_VAR_ABS((%rsi)), %rax jne .Lnot_same - cmpq PER_CPU_VAR(8(%rsi)), %rdx + cmpq PER_CPU_VAR_ABS(8(%rsi)), %rdx jne .Lnot_same - movq %rbx, PER_CPU_VAR((%rsi)) - movq %rcx, PER_CPU_VAR(8(%rsi)) + movq %rbx, PER_CPU_VAR_ABS((%rsi)) + movq %rcx, PER_CPU_VAR_ABS(8(%rsi)) popfq mov $1, %al
arch/x86/xen/xen-asm.S +6 -6
@@ -26,7 +26,7 @@ ENTRY(xen_irq_enable_direct) FRAME_BEGIN /* Unmask events */ - movb $0, PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_mask + movb $0, PER_CPU_VAR(xen_vcpu_info + XEN_vcpu_info_mask) /* * Preempt here doesn't matter because that will deal with any
@@ -35,7 +35,7 @@ ENTRY(xen_irq_enable_direct) */ /* Test for pending */ - testb $0xff, PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_pending + testb $0xff, PER_CPU_VAR(xen_vcpu_info + XEN_vcpu_info_pending) jz 1f 2: call check_events
@@ -52,7 +52,7 @@ ENDPATCH(xen_irq_enable_direct) * non-zero. */ ENTRY(xen_irq_disable_direct) - movb $1, PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_mask + movb $1, PER_CPU_VAR(xen_vcpu_info + XEN_vcpu_info_mask) ENDPATCH(xen_irq_disable_direct) ret ENDPROC(xen_irq_disable_direct)
@@ -68,7 +68,7 @@ ENDPATCH(xen_irq_disable_direct) * x86 use opposite senses (mask vs enable). */ ENTRY(xen_save_fl_direct) - testb $0xff, PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_mask + testb $0xff, PER_CPU_VAR(xen_vcpu_info + XEN_vcpu_info_mask) setz %ah addb %ah, %ah ENDPATCH(xen_save_fl_direct)
@@ -91,7 +91,7 @@ ENTRY(xen_restore_fl_direct) #else testb $X86_EFLAGS_IF>>8, %ah #endif - setz PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_mask + setz PER_CPU_VAR(xen_vcpu_info + XEN_vcpu_info_mask) /* * Preempt here doesn't matter because that will deal with any * pending interrupts. The pending check may end up being run
@@ -99,7 +99,7 @@ ENTRY(xen_restore_fl_direct) */ /* check for unmasked and pending */ - cmpw $0x0001, PER_CPU_VAR(xen_vcpu_info) + XEN_vcpu_info_pending + cmpw $0x0001, PER_CPU_VAR(xen_vcpu_info + XEN_vcpu_info_pending) jnz 1f 2: call check_events 1:
init/Kconfig +1 -1
@@ -1201,7 +1201,7 @@ config KALLSYMS_ALL config KALLSYMS_ABSOLUTE_PERCPU bool depends on KALLSYMS - default X86_64 && SMP + default X86_64 && SMP && !X86_PIE config KALLSYMS_BASE_RELATIVE bool
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 17/23 compiler: Option to default to hidden symbols
In reply to Thomas GarnierProvide an option to default visibility to hidden except for key symbols. This option is disabled by default and will be used by x86_64 PIE support to remove errors between compilation units.
--- arch/x86/boot/boot.h | 2 +- arch/x86/include/asm/setup.h | 2 +- include/asm-generic/sections.h | 6 ++++++ include/linux/compiler.h | 8 ++++++++ init/Kconfig | 7 +++++++ kernel/kallsyms.c | 16 ++++++++-------- 6 files changed, 31 insertions(+), 10 deletions(-)
arch/x86/boot/boot.h +1 -1
@@ -193,7 +193,7 @@ static inline bool memcmp_gs(const void *s1, addr_t s2, size_t len) }/* Heap -- available for dynamic lists. */ -extern char _end[]; +extern char _end[] __default_visibility;extern char *HEAP ;extern char *heap_end;#define RESET_HEAP () ((void *)( HEAP = _end ))
arch/x86/include/asm/setup.h +1 -1
@@ -67,7 +67,7 @@ static inline void x86_ce4100_early_setup(void) { }* This is set up by the setup-routine at boot-time */ extern struct boot_params boot_params; -extern char _text[]; +extern char _text[] __default_visibility;static inline bool kaslr_enabled (void ) {
include/asm-generic/sections.h +6 -0
@@ -28,6 +28,9 @@* __entry_text_start, __entry_text_end * __ctors_start, __ctors_end */ +#ifdef CONFIG_DEFAULT_HIDDEN +#pragma GCC visibility push(default) +#endif extern char _text[], _stext[], _etext[];extern char _data[], _sdata[], _edata[];extern char __bss_start[], __bss_stop[];
@@ -42,6 +45,9 @@ extern char __start_rodata[], __end_rodata[];/* Start and end of .ctors section - used for constructor calls. */ extern char __ctors_start[], __ctors_end[]; +#ifdef CONFIG_DEFAULT_HIDDEN +#pragma GCC visibility pop +#endif extern __visible const void __nosave_begin, __nosave_end;
include/linux/compiler.h +8 -0
@@ -78,6 +78,14 @@ extern void __chk_io_ptr(const volatile void __iomem *);#include <linux/compiler-clang.h> #endif +/* Useful for Position Independent Code to reduce global references */ +#ifdef CONFIG_DEFAULT_HIDDEN +#pragma GCC visibility push(hidden) +#define __default_visibility __attribute__((visibility ("default"))) +#else +#define __default_visibility +#endif +/* * Generic compiler-dependent macros required for kernel * build go below this comment. Actual compiler/compiler version
init/Kconfig +7 -0
@@ -1640,6 +1640,13 @@ config PROFILING config TRACEPOINTS bool +# +# Default to hidden visibility for all symbols. +# Useful for Position Independent Code to reduce global references. +# +config DEFAULT_HIDDEN + bool + source "arch/Kconfig" endmenu # General setup
kernel/kallsyms.c +8 -8
@@ -32,24 +32,24 @@* These will be re-linked against their real values * during the second link stage. */ -extern const unsigned long kallsyms_addresses[]__weak ; -extern const int kallsyms_offsets[]__weak ; -extern const u8 kallsyms_names[]__weak ; +extern const unsigned long kallsyms_addresses[] __weak __default_visibility; +extern const int kallsyms_offsets[] __weak __default_visibility; +extern const u8 kallsyms_names[] __weak __default_visibility;/* * Tell the compiler that the count isn't in the small data section if the arch * has one (eg: FRV). */ extern const unsigned long kallsyms_num_syms -__attribute__((weak, section(".rodata"))); +__attribute__((weak,section (".rodata" ))) __default_visibility;extern const unsigned long kallsyms_relative_base -__attribute__((weak, section(".rodata"))); +__attribute__((weak,section (".rodata" ))) __default_visibility; -extern const u8 kallsyms_token_table[]__weak ; -extern const u16 kallsyms_token_index[]__weak ; +extern const u8 kallsyms_token_table[] __weak __default_visibility; +extern const u16 kallsyms_token_index[] __weak __default_visibility; -extern const unsigned long kallsyms_markers[]__weak ; +extern const unsigned long kallsyms_markers[] __weak __default_visibility;static inline int is_kernel_inittext (unsigned long addr) {
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 18/23 x86/relocs: Handle DYN relocations for PIE support
In reply to Thomas GarnierChange the relocation tool to correctly handle DYN/PIE kernel where the relocation table does not reference symbols and percpu support is not needed. Also add support for R_X86_64_RELATIVE relocations that can be handled like a 64-bit relocation due to the usage of -Bsymbolic.
Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit.
--- arch/x86/tools/relocs.c | 74 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 9 deletions(-)
arch/x86/tools/relocs.c +65 -9
@@ -642,6 +642,13 @@ static void add_reloc(struct relocs *r, uint32_t offset) r->offset[r->count++] = offset; } +/* Relocation found in a DYN binary, support only for 64-bit PIE */ +static int is_dyn_reloc (struct section *sec) +{ +return ELF_BITS ==64 && ehdr.e_type ==ET_DYN && + sec->shdr.sh_info ==SHT_NULL ; +} +static void walk_relocs (int (*process)(struct section *sec,Elf_Rel *rel,Elf_Sym *sym,const char *symname)) {
@@ -652,6 +659,7 @@ static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,Elf_Sym *sh_symtab;struct section *sec_applies, *sec_symtab;int j; +int dyn_reloc =0 ;struct section *sec = &secs[i];if (sec->shdr.sh_type !=SHT_REL_TYPE ) {
@@ -660,14 +668,20 @@ static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, sec_symtab = sec->link; sec_applies = &secs[sec->shdr.sh_info];if (!(sec_applies->shdr.sh_flags &SHF_ALLOC )) { -continue ; +if (!is_dyn_reloc (sec_applies)) +continue ; + dyn_reloc =1 ; } sh_symtab = sec_symtab->symtab; sym_strtab = sec_symtab->link->strtab;for (j =0 ; j < sec->shdr.sh_size/sizeof (Elf_Rel); j++) {Elf_Rel *rel = &sec->reltab[j]; -Elf_Sym *sym = &sh_symtab[ELF_R_SYM (rel->r_info)]; -const char *symname =sym_name (sym_strtab, sym); +Elf_Sym *sym =NULL ; +const char *symname =NULL ; +if (!dyn_reloc) { + sym = &sh_symtab[ELF_R_SYM (rel->r_info)]; + symname =sym_name (sym_strtab, sym); + }process (sec, rel, sym, symname); }
@@ -746,16 +760,21 @@ static int is_percpu_sym(ElfW(Sym) *sym, const char *symname)strncmp (symname,"init_per_cpu_" ,13 ); } -static int do_reloc64 (struct section *sec,Elf_Rel *rel,ElfW (Sym) *sym,const char *symname) {unsigned r_type =ELF64_R_TYPE (rel->r_info);ElfW (Addr) offset = rel->r_offset; -int shn_abs = (sym->st_shndx ==SHN_ABS ) && !is_reloc (S_REL , symname); +int shn_abs =0 ; +int dyn_reloc =is_dyn_reloc (sec); -if (sym->st_shndx ==SHN_UNDEF ) -return 0 ; +if (!dyn_reloc) { + shn_abs = (sym->st_shndx ==SHN_ABS ) && + !is_reloc (S_REL , symname); + +if (sym->st_shndx ==SHN_UNDEF ) +return 0 ; + }/* * Adjust the offset if this reloc applies to the percpu section.
@@ -769,6 +788,9 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,break ;case R_X86_64_PC32 : +if (dyn_reloc) +die ("PC32 reloc in PIE DYN binary" ); +/* * PC relative relocations don't need to be adjusted unless * referencing a percpu symbol.
@@ -783,7 +805,7 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,/* * References to the percpu area don't need to be adjusted. */ -if (is_percpu_sym (sym, symname)) +if (!dyn_reloc &&is_percpu_sym (sym, symname))break ;if (shn_abs) {
@@ -814,6 +836,14 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,add_reloc (&relocs32, offset);break ; +case R_X86_64_RELATIVE : +/* +* -Bsymbolic means we don't need the addend and we can reuse +* the original relocs64. +*/ +add_reloc (&relocs64, offset); +break ; +default :die ("Unsupported relocation type: %s (%d)\n" ,rel_type (r_type), r_type);
@@ -1044,6 +1074,21 @@ static void emit_relocs(int as_text, int use_real_mode) } } +/* Print a different header based on the type of relocation */ +static void print_reloc_header (struct section *sec) { +static int header_printed =0 ; +int header_type =is_dyn_reloc (sec) ?2 :1 ; + +if (header_printed == header_type) +return ; + header_printed = header_type; + +if (header_type ==2 ) +printf ("reloc type\toffset\tvalue\n" ); +else +printf ("reloc section\treloc type\tsymbol\tsymbol section\n" ); +} +/* * As an aid to debugging problems with different linkers * print summary information about the relocs.
@@ -1053,6 +1098,18 @@ static void emit_relocs(int as_text, int use_real_mode)static int do_reloc_info (struct section *sec,Elf_Rel *rel,ElfW (Sym) *sym,const char *symname) { + +print_reloc_header (sec); + +#if ELF_BITS ==64 +if (is_dyn_reloc (sec)) { +printf ("%s\t0x%lx\t0x%lx\n" , +rel_type (ELF_R_TYPE (rel->r_info)), + rel->r_offset, + rel->r_addend); +return 0 ; + } +#endif printf("%s\t%s\t%s\t%s\n", sec_name(sec->shdr.sh_info), rel_type(ELF_R_TYPE(rel->r_info)),
@@ -1063,7 +1120,6 @@ static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,static void print_reloc_info (void ) { -printf ("reloc section\treloc type\tsymbol\tsymbol section\n" );walk_relocs (do_reloc_info); }
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 19/23 x86: Support global stack cookie
In reply to Thomas GarnierAdd an off-by-default configuration option to use a global stack cookie instead of the default TLS. This configuration option will only be used with PIE binaries.
For kernel stack cookie, the compiler uses the mcmodel=kernel to switch between the fs segment to gs segment. A PIE binary does not use mcmodel=kernel because it can be relocated anywhere, therefore the compiler will default to the fs segment register. This is going to be fixed with a compiler change allowing to pick the segment register as done on PowerPC. In the meantime, this configuration can be used to support older compilers.
--- arch/x86/Kconfig | 4 ++++ arch/x86/Makefile | 9 +++++++++ arch/x86/entry/entry_32.S | 3 ++- arch/x86/entry/entry_64.S | 3 ++- arch/x86/include/asm/processor.h | 3 ++- arch/x86/include/asm/stackprotector.h | 19 ++++++++++++++----- arch/x86/kernel/asm-offsets.c | 3 ++- arch/x86/kernel/asm-offsets_32.c | 3 ++- arch/x86/kernel/asm-offsets_64.c | 3 ++- arch/x86/kernel/cpu/common.c | 3 ++- arch/x86/kernel/head_32.S | 3 ++- arch/x86/kernel/process.c | 5 +++++ 12 files changed, 48 insertions(+), 13 deletions(-)
arch/x86/Kconfig +4 -0
@@ -2128,6 +2128,10 @@ config RANDOMIZE_MEMORY_PHYSICAL_PADDING If unsure, leave at the default value. +config X86_GLOBAL_STACKPROTECTOR + bool + depends on CC_STACKPROTECTOR + config HOTPLUG_CPU bool "Support for hot-pluggable CPUs" depends on SMP
arch/x86/Makefile +9 -0
@@ -58,6 +58,15 @@ endif KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow KBUILD_CFLAGS += $(call cc-option,-mno-avx,) +ifdef CONFIG_X86_GLOBAL_STACKPROTECTOR + ifeq ($(call cc-option, -mstack-protector-guard=global),) + $(error Cannot use CONFIG_X86_GLOBAL_STACKPROTECTOR: \ + -mstack-protector-guard=global not supported \ + by compiler) + endif + KBUILD_CFLAGS += -mstack-protector-guard=global +endif + ifeq ($(CONFIG_X86_32),y) BITS := 32 UTS_MACHINE := i386
arch/x86/entry/entry_32.S +2 -1
@@ -237,7 +237,8 @@ ENTRY(__switch_to_asm) movl %esp, TASK_threadsp(%eax) movl TASK_threadsp(%edx), %esp -#ifdef CONFIG_CC_STACKPROTECTOR +#if defined(CONFIG_CC_STACKPROTECTOR) && \ + !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR) movl TASK_stack_canary(%edx), %ebx movl %ebx, PER_CPU_VAR(stack_canary)+stack_canary_offset #endif
arch/x86/entry/entry_64.S +2 -1
@@ -395,7 +395,8 @@ ENTRY(__switch_to_asm) movq %rsp, TASK_threadsp(%rdi) movq TASK_threadsp(%rsi), %rsp -#ifdef CONFIG_CC_STACKPROTECTOR +#if defined(CONFIG_CC_STACKPROTECTOR) && \ + !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR) movq TASK_stack_canary(%rsi), %rbx movq %rbx, PER_CPU_VAR(irq_stack_union + stack_canary_offset) #endif
arch/x86/include/asm/processor.h +2 -1
@@ -394,7 +394,8 @@ DECLARE_PER_CPU(char *, irq_stack_ptr); DECLARE_PER_CPU(unsigned int, irq_count); extern asmlinkage void ignore_sysret(void);#else /* X86_64 */ -#ifdef CONFIG_CC_STACKPROTECTOR +#if defined(CONFIG_CC_STACKPROTECTOR ) && \ + defined(CONFIG_X86_GLOBAL_STACKPROTECTOR )/* * Make sure stack canary segment base is cached-aligned: * "For Intel Atom processors, avoid non zero segment base address
arch/x86/include/asm/stackprotector.h +14 -5
@@ -51,6 +51,10 @@#define GDT_STACK_CANARY_INIT \ [GDT_ENTRY_STACK_CANARY] = GDT_ENTRY_INIT(0x4090, 0, 0x18), +#ifdef CONFIG_X86_GLOBAL_STACKPROTECTOR +extern unsigned long __stack_chk_guard; +#endif +/* * Initialize the stackprotector canary value. *
@@ -62,7 +66,7 @@ static __always_inline void boot_init_stack_canary(void)u64 canary;u64 tsc; -#ifdef CONFIG_X86_64 +#if defined(CONFIG_X86_64 ) && !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR )BUILD_BUG_ON (offsetof(union irq_stack_union , stack_canary) !=40 );#endif /*
@@ -76,17 +80,22 @@ static __always_inline void boot_init_stack_canary(void) canary += tsc + (tsc <<32UL ); canary &=CANARY_MASK ; +#ifdef CONFIG_X86_GLOBAL_STACKPROTECTOR +if (__stack_chk_guard ==0 ) + __stack_chk_guard = canary ?:1 ; +#else /* !CONFIG_X86_GLOBAL_STACKPROTECTOR */ current->stack_canary = canary;#ifdef CONFIG_X86_64 this_cpu_write (irq_stack_union.stack_canary, canary); -#else +#else /* CONFIG_X86_32 */ this_cpu_write (stack_canary.canary, canary);#endif +#endif }static inline void setup_stack_canary_segment (int cpu) { -#ifdef CONFIG_X86_32 +#if defined(CONFIG_X86_32 ) && !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR )unsigned long canary = (unsigned long )&per_cpu (stack_canary, cpu);struct desc_struct *gdt_table =get_cpu_gdt_rw (cpu);struct desc_struct desc;
@@ -99,7 +108,7 @@ static inline void setup_stack_canary_segment(int cpu)static inline void load_stack_canary_segment (void ) { -#ifdef CONFIG_X86_32 +#if defined(CONFIG_X86_32 ) && !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR ) asm("mov %0, %%gs" : :"r" (__KERNEL_STACK_CANARY) :"memory" );#endif }
@@ -115,7 +124,7 @@ static inline void setup_stack_canary_segment(int cpu)static inline void load_stack_canary_segment (void ) { -#ifdef CONFIG_X86_32 +#if defined(CONFIG_X86_32 ) && !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR ) asmvolatile ("mov %0, %%gs" : :"r" (0 ));#endif }
arch/x86/kernel/asm-offsets.c +2 -1
@@ -30,7 +30,8 @@void common (void ) {BLANK ();OFFSET (TASK_threadsp, task_struct, thread.sp); -#ifdef CONFIG_CC_STACKPROTECTOR +#if defined(CONFIG_CC_STACKPROTECTOR ) && \ + !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR )OFFSET (TASK_stack_canary, task_struct, stack_canary);#endif
arch/x86/kernel/asm-offsets_32.c +2 -1
@@ -57,7 +57,8 @@ void foo(void)/* Size of SYSENTER_stack */ DEFINE (SIZEOF_SYSENTER_stack,sizeof (((struct tss_struct *)0 )->SYSENTER_stack)); -#ifdef CONFIG_CC_STACKPROTECTOR +#if defined(CONFIG_CC_STACKPROTECTOR ) && \ + !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR )BLANK ();OFFSET (stack_canary_offset, stack_canary, canary);#endif
arch/x86/kernel/asm-offsets_64.c +2 -1
@@ -65,7 +65,8 @@ int main(void)OFFSET (TSS_sp0, tss_struct, x86_tss.sp0);BLANK (); -#ifdef CONFIG_CC_STACKPROTECTOR +#if defined(CONFIG_CC_STACKPROTECTOR ) && \ + !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR )DEFINE (stack_canary_offset, offsetof(union irq_stack_union , stack_canary));BLANK ();#endif
arch/x86/kernel/cpu/common.c +2 -1
@@ -1454,7 +1454,8 @@ DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = (unsigned long )&init_thread_union +THREAD_SIZE ;EXPORT_PER_CPU_SYMBOL (cpu_current_top_of_stack); -#ifdef CONFIG_CC_STACKPROTECTOR +#if defined(CONFIG_CC_STACKPROTECTOR ) && \ + !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR ) DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);#endif
arch/x86/kernel/head_32.S +2 -1
@@ -411,7 +411,8 @@ setup_once: addl $8,%edi loop 2b -#ifdef CONFIG_CC_STACKPROTECTOR +#if defined(CONFIG_CC_STACKPROTECTOR) && \ + !defined(CONFIG_X86_GLOBAL_STACKPROTECTOR) /* * Configure the stack canary. The linker can't handle this by * relocation. Manually set base address in stack canary
arch/x86/kernel/process.c +5 -0
@@ -73,6 +73,11 @@ EXPORT_PER_CPU_SYMBOL(cpu_tss);DEFINE_PER_CPU (bool, __tss_limit_invalid);EXPORT_PER_CPU_SYMBOL_GPL (__tss_limit_invalid); +#ifdef CONFIG_X86_GLOBAL_STACKPROTECTOR +unsigned long __stack_chk_guard__read_mostly ; +EXPORT_SYMBOL (__stack_chk_guard); +#endif +/* * this gets called so that we can store lazy state into memory and copy the * current task into the new thread.
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 20/23 x86/pie: Add option to build the kernel as PIE for x86_64
In reply to Thomas GarnierAdd the CONFIG_X86_PIE option which builds the kernel as a Position Independent Executable (PIE). The kernel is currently build with the mcmodel=kernel option which forces it to stay on the top 2G of the virtual address space. With PIE, the kernel will be able to move below the -2G limit increasing the KASLR range from 1GB to 3GB.
The modules do not support PIE due to how they are linked. Disable PIE for them and default to mcmodel=kernel for now.
The PIE configuration is not yet compatible with XEN_PVH. Xen PVH generates 32-bit assembly and uses a long jump to transition to 64-bit. A long jump require an absolute reference that is not compatible with PIE.
Performance/Size impact:
Hackbench (50% and 1600% loads):
- PIE disabled: no significant change (-0.50% / +0.50%)
- PIE enabled: 7% to 8% on half load, 10% on heavy load.
These results are aligned with the different research on user-mode PIE impact on cpu intensive benchmarks (around 10% on x86_64).
slab_test (average of 10 runs):
- PIE disabled: no significant change (-1% / +1%)
- PIE enabled: 3% to 4%
Kernbench (average of 10 Half and Optimal runs):
Elapsed Time:
- PIE disabled: no significant change (-0.22% / +0.06%)
- PIE enabled: around 0.50%
System Time:
- PIE disabled: no significant change (-0.99% / -1.28%)
- PIE enabled: 5% to 6%
Size of vmlinux (Ubuntu configuration):
File size:
- PIE disabled: 472928672 bytes (-0.000169% from baseline)
- PIE enabled: 216878461 bytes (-54.14% from baseline) .text sections:
- PIE disabled: 9373572 bytes (+0.04% from baseline)
- PIE enabled: 9499138 bytes (+1.38% from baseline)
The big decrease in vmlinux file size is due to the lower number of relocations appended to the file.
--- arch/x86/Kconfig | 7 +++++++ arch/x86/Makefile | 9 +++++++++ 2 files changed, 16 insertions(+)
arch/x86/Kconfig +7 -0
@@ -2132,6 +2132,13 @@ config X86_GLOBAL_STACKPROTECTOR bool depends on CC_STACKPROTECTOR +config X86_PIE + bool + depends on X86_64 && !XEN_PVH + select DEFAULT_HIDDEN + select MODULE_REL_CRCS if MODVERSIONS + select X86_GLOBAL_STACKPROTECTOR if CC_STACKPROTECTOR + config HOTPLUG_CPU bool "Support for hot-pluggable CPUs" depends on SMP
arch/x86/Makefile +9 -0
@@ -45,8 +45,12 @@ export REALMODE_CFLAGS export BITS ifdef CONFIG_X86_NEED_RELOCS +ifdef CONFIG_X86_PIE + LDFLAGS_vmlinux := -pie -shared -Bsymbolic +else LDFLAGS_vmlinux := --emit-relocs endif +endif # # Prevent GCC from generating any FP code by mistake.
@@ -141,7 +145,12 @@ else KBUILD_CFLAGS += $(cflags-y) KBUILD_CFLAGS += -mno-red-zone +ifdef CONFIG_X86_PIE + KBUILD_CFLAGS += -fPIC + KBUILD_CFLAGS_MODULE += -fno-PIC -mcmodel=kernel +else KBUILD_CFLAGS += -mcmodel=kernel +endif # -funit-at-a-time shrinks the kernel .text considerably # unfortunately it makes reading oopses harder.
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 21/23 x86/relocs: Add option to generate 64-bit relocations
In reply to Thomas GarnierThe x86 relocation tool generates a list of 32-bit signed integers. There was no need to use 64-bit integers because all addresses where above the 2G top of the memory.
This change add a large-reloc option to generate 64-bit unsigned integers. It can be used when the kernel plan to go below the top 2G and 32-bit integers are not enough.
--- arch/x86/tools/relocs.c | 60 +++++++++++++++++++++++++++++++++--------- arch/x86/tools/relocs.h | 4 +-- arch/x86/tools/relocs_common.c | 15 +++++++---- 3 files changed, 60 insertions(+), 19 deletions(-)
arch/x86/tools/relocs.c +48 -12
@@ -12,8 +12,14 @@static Elf_Ehdr ehdr; +#if ELF_BITS ==64 +typedef uint64_t rel_off_t ; +#else +typedef uint32_t rel_off_t ; +#endif +struct relocs { -uint32_t *offset; +rel_off_t *offset;unsigned long count;unsigned long size; };
@@ -627,7 +633,7 @@ static void print_absolute_relocs(void)printf ("\n" ); } -static void add_reloc (struct relocs *r,uint32_t offset) +static void add_reloc (struct relocs *r,rel_off_t offset) {if (r->count == r->size) {unsigned long newsize = r->size +50000 ;
@@ -983,26 +989,48 @@ static void sort_relocs(struct relocs *r)qsort (r->offset, r->count,sizeof (r->offset[0 ]), cmp_relocs); } -static int write32 (uint32_t v,FILE *f) +static int write32 (rel_off_t rel,FILE *f) { -unsigned char buf[4 ]; +unsigned char buf[sizeof (uint32_t )]; +uint32_t v = (uint32_t )rel;put_unaligned_le32 (v, buf); -return fwrite (buf,1 ,4 , f) ==4 ?0 :-1 ; +return fwrite (buf,1 ,sizeof (buf), f) ==sizeof (buf) ?0 :-1 ; } -static int write32_as_text (uint32_t v,FILE *f) +static int write32_as_text (rel_off_t rel,FILE *f) { +uint32_t v = (uint32_t )rel;return fprintf (f,"\t.long 0x%08" PRIx32"\n" , v) >0 ?0 :-1 ; } -static void emit_relocs (int as_text,int use_real_mode) +static int write64 (rel_off_t rel,FILE *f) +{ +unsigned char buf[sizeof (uint64_t )]; +uint64_t v = (uint64_t )rel; + +put_unaligned_le64 (v, buf); +return fwrite (buf,1 ,sizeof (buf), f) ==sizeof (buf) ?0 :-1 ; +} + +static int write64_as_text (rel_off_t rel,FILE *f) +{ +uint64_t v = (uint64_t )rel; +return fprintf (f,"\t.quad 0x%016" PRIx64"\n" , v) >0 ?0 :-1 ; +} + +static void emit_relocs (int as_text,int use_real_mode,int use_large_reloc) {int i; -int (*write_reloc)(uint32_t ,FILE *) = write32; +int (*write_reloc)(rel_off_t ,FILE *);int (*do_reloc)(struct section *sec,Elf_Rel *rel,Elf_Sym *sym,const char *symname); +if (use_large_reloc) + write_reloc = write64; +else + write_reloc = write32; +#if ELF_BITS ==64 if (!use_real_mode) do_reloc = do_reloc64;
@@ -1013,6 +1041,9 @@ static void emit_relocs(int as_text, int use_real_mode) do_reloc = do_reloc32; else do_reloc = do_reloc_real; + +/* Large relocations only for 64-bit */ + use_large_reloc =0 ;#endif /* Collect up the relocations */
@@ -1036,8 +1067,13 @@ static void emit_relocs(int as_text, int use_real_mode)* gas will like. */ printf (".section \".data.reloc\",\"a\"\n" ); -printf (".balign 4\n" ); - write_reloc = write32_as_text; +if (use_large_reloc) { +printf (".balign 8\n" ); + write_reloc = write64_as_text; + }else { +printf (".balign 4\n" ); + write_reloc = write32_as_text; + } }if (use_real_mode) {
@@ -1131,7 +1167,7 @@ static void print_reloc_info(void)void process (FILE *fp,int use_real_mode,int as_text,int show_absolute_syms,int show_absolute_relocs, -int show_reloc_info) +int show_reloc_info,int use_large_reloc) {regex_init (use_real_mode);read_ehdr (fp);
@@ -1153,5 +1189,5 @@ void process(FILE *fp, int use_real_mode, int as_text,print_reloc_info ();return ; } -emit_relocs (as_text, use_real_mode); +emit_relocs (as_text, use_real_mode, use_large_reloc); }
arch/x86/tools/relocs.h +2 -2
@@ -30,8 +30,8 @@ enum symtype {void process_32 (FILE *fp,int use_real_mode,int as_text,int show_absolute_syms,int show_absolute_relocs, -int show_reloc_info); +int show_reloc_info,int use_large_reloc);void process_64 (FILE *fp,int use_real_mode,int as_text,int show_absolute_syms,int show_absolute_relocs, -int show_reloc_info); +int show_reloc_info,int use_large_reloc);#endif /* RELOCS_H */
arch/x86/tools/relocs_common.c +10 -5
@@ -11,14 +11,14 @@ void die(char *fmt, ...)static void usage (void ) { -die ("relocs [--abs-syms|--abs-relocs|--reloc-info|--text|--realmode]" \ -" vmlinux\n" ); +die ("relocs [--abs-syms|--abs-relocs|--reloc-info|--text|--realmode|" \ +"--large-reloc] vmlinux\n" ); }int main (int argc,char **argv) {int show_absolute_syms, show_absolute_relocs, show_reloc_info; -int as_text, use_real_mode; +int as_text, use_real_mode, use_large_reloc;const char *fname;FILE *fp;int i;
@@ -29,6 +29,7 @@ int main(int argc, char **argv) show_reloc_info =0 ; as_text =0 ; use_real_mode =0 ; + use_large_reloc =0 ; fname =NULL ;for (i =1 ; i < argc; i++) {char *arg = argv[i];
@@ -53,6 +54,10 @@ int main(int argc, char **argv) use_real_mode =1 ;continue ; } +if (strcmp (arg,"--large-reloc" ) ==0 ) { + use_large_reloc =1 ; +continue ; + } }else if (!fname) { fname = arg;
@@ -74,11 +79,11 @@ int main(int argc, char **argv)if (e_ident[EI_CLASS ] ==ELFCLASS64 )process_64 (fp, use_real_mode, as_text, show_absolute_syms, show_absolute_relocs, - show_reloc_info); + show_reloc_info, use_large_reloc);else process_32 (fp, use_real_mode, as_text, show_absolute_syms, show_absolute_relocs, - show_reloc_info); + show_reloc_info, use_large_reloc);fclose (fp);return 0 ; }
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 22/23 x86/module: Add support for mcmodel large and PLTs
In reply to Thomas GarnierWith PIE support and KASLR extended range, the modules may be further away from the kernel than before breaking mcmodel=kernel expectations.
Add an option to build modules with mcmodel=large. The modules generated code will make no assumptions on placement in memory.
Despite this option, modules still expect kernel functions to be within 2G and generate relative calls. To solve this issue, the PLT arm64 code was adapted for x86_64. When a relative relocation go outside its range, a dynamic PLT entry is used to correctly jump to the destination.
--- arch/x86/Kconfig | 10 +++ arch/x86/Makefile | 10 ++- arch/x86/include/asm/module.h | 17 ++++ arch/x86/kernel/Makefile | 2 + arch/x86/kernel/module-plts.c | 198 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kernel/module.c | 18 ++-- arch/x86/kernel/module.lds | 4 + 7 files changed, 252 insertions(+), 7 deletions(-) create mode 100644 arch/x86/kernel/module-plts.c create mode 100644 arch/x86/kernel/module.lds
arch/x86/Kconfig +10 -0
@@ -2139,6 +2139,16 @@ config X86_PIE select MODULE_REL_CRCS if MODVERSIONS select X86_GLOBAL_STACKPROTECTOR if CC_STACKPROTECTOR +config X86_MODULE_MODEL_LARGE + bool + depends on X86_64 && X86_PIE + +config X86_MODULE_PLTS + bool + depends on X86_64 + select X86_MODULE_MODEL_LARGE + select HAVE_MOD_ARCH_SPECIFIC + config HOTPLUG_CPU bool "Support for hot-pluggable CPUs" depends on SMP
arch/x86/Makefile +9 -1
@@ -147,10 +147,18 @@ else KBUILD_CFLAGS += -mno-red-zone ifdef CONFIG_X86_PIE KBUILD_CFLAGS += -fPIC - KBUILD_CFLAGS_MODULE += -fno-PIC -mcmodel=kernel + KBUILD_CFLAGS_MODULE += -fno-PIC else KBUILD_CFLAGS += -mcmodel=kernel endif +ifdef CONFIG_X86_MODULE_MODEL_LARGE + KBUILD_CFLAGS_MODULE += -mcmodel=large +else + KBUILD_CFLAGS_MODULE += -mcmodel=kernel +endif +ifdef CONFIG_X86_MODULE_PLTS + KBUILD_LDFLAGS_MODULE += -T $(srctree)/arch/x86/kernel/module.lds +endif # -funit-at-a-time shrinks the kernel .text considerably # unfortunately it makes reading oopses harder.
arch/x86/include/asm/module.h +17 -0
@@ -4,12 +4,26 @@#include <asm-generic/module.h> #include <asm/orc_types.h> +#ifdef CONFIG_X86_MODULE_PLTS +struct mod_plt_sec { +struct elf64_shdr *plt; +int plt_num_entries; +int plt_max_entries; +}; +#endif + + +struct mod_arch_specific {#ifdef CONFIG_ORC_UNWINDER unsigned int num_orcs;int *orc_unwind_ip;struct orc_entry *orc_unwind;#endif +#ifdef CONFIG_X86_MODULE_PLTS +struct mod_plt_sec core; +struct mod_plt_sec init; +#endif };#ifdef CONFIG_X86_64
@@ -70,4 +84,7 @@ struct mod_arch_specific {# define MODULE_ARCH_VERMAGIC MODULE_PROC_FAMILY#endif +u64 module_emit_plt_entry (struct module *mod,void *loc,const Elf64_Rela *rela, +Elf64_Sym *sym); +#endif /* _ASM_X86_MODULE_H */
arch/x86/kernel/Makefile +2 -0
@@ -140,4 +140,6 @@ ifeq ($(CONFIG_X86_64),y) obj-$(CONFIG_PCI_MMCONFIG) += mmconf-fam10h_64.o obj-y += vsmp_64.o + + obj-$(CONFIG_X86_MODULE_PLTS) += module-plts.o endif
arch/x86/kernel/module-plts.c new file +198 -0
198 lines not on this page · raw message
arch/x86/kernel/module.c +12 -6
@@ -187,10 +187,15 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,case R_X86_64_PC32 : val -= (u64 )loc; *(u32 *)loc = val; -#if 0 -if ((s64 )val != *(s32 *)loc) - goto overflow; -#endif +if (IS_ENABLED (CONFIG_X86_MODULE_MODEL_LARGE ) && + (s64 )val != *(s32 *)loc) { + val =module_emit_plt_entry (me, loc, &rel[i], + sym); + val -= (u64 )loc; + *(u32 *)loc = val; +if ((s64 )val != *(s32 *)loc) + goto overflow; + }break ;default : pr_err("%s: Unknown rela relocation: %llu\n",
@@ -203,8 +208,9 @@ int apply_relocate_add(Elf64_Shdr *sechdrs, overflow:pr_err ("overflow in relocation type %d val %Lx\n" , (int )ELF64_R_TYPE (rel[i].r_info), val); -pr_err ("`%s' likely not compiled with -mcmodel=kernel\n" , - me->name); +pr_err ("`%s' likely not compiled with -mcmodel=%s\n" , + me->name, +IS_ENABLED (CONFIG_X86_MODULE_MODEL_LARGE ) ?"large" :"kernel" ); return -ENOEXEC; }#endif
arch/x86/kernel/module.lds new file +4 -0
@@ -0,0 +1,4 @@ +SECTIONS { + .plt (NOLOAD) : { BYTE(0) } + .init.plt (NOLOAD) : { BYTE(0) } +}
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
RFC v2 23/23 x86/kaslr: Add option to extend KASLR range from 1GB to 3GB
In reply to Thomas GarnierAdd a new CONFIG_RANDOMIZE_BASE_LARGE option to benefit from PIE support. It increases the KASLR range from 1GB to 3GB. The new range stars at 0xffffffff00000000 just above the EFI memory region. This option is off by default.
The boot code is adapted to create the appropriate page table spanning three PUD pages.
The relocation table uses 64-bit integers generated with the updated relocation tool with the large-reloc option.
--- arch/x86/Kconfig | 21 +++++++++++++++++++++ arch/x86/boot/compressed/Makefile | 5 +++++ arch/x86/boot/compressed/misc.c | 10 +++++++++- arch/x86/include/asm/page_64_types.h | 9 +++++++++ arch/x86/kernel/head64.c | 18 ++++++++++++++---- arch/x86/kernel/head_64.S | 11 ++++++++++- 6 files changed, 68 insertions(+), 6 deletions(-)
arch/x86/Kconfig +21 -0
@@ -2149,6 +2149,27 @@ config X86_MODULE_PLTS select X86_MODULE_MODEL_LARGE select HAVE_MOD_ARCH_SPECIFIC +config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations. + + If unsure say N + config HOTPLUG_CPU bool "Support for hot-pluggable CPUs" depends on SMP
arch/x86/boot/compressed/Makefile +5 -0
@@ -112,7 +112,12 @@ $(obj)/vmlinux.bin: vmlinux FORCE targets += $(patsubst $(obj)/%,%,$(vmlinux-objs-y)) vmlinux.bin.all vmlinux.relocs +# Large randomization require bigger relocation table +ifeq ($(CONFIG_RANDOMIZE_BASE_LARGE),y) +CMD_RELOCS = arch/x86/tools/relocs --large-reloc +else CMD_RELOCS = arch/x86/tools/relocs +endif quiet_cmd_relocs = RELOCS $@ cmd_relocs = $(CMD_RELOCS) $< > $@;$(CMD_RELOCS) --abs-relocs $< $(obj)/vmlinux.relocs: vmlinux FORCE
arch/x86/boot/compressed/misc.c +9 -1
@@ -170,10 +170,18 @@ void __puthex(unsigned long value) }#if CONFIG_X86_NEED_RELOCS + +/* Large randomization go lower than -2G and use large relocation table */ +#ifdef CONFIG_RANDOMIZE_BASE_LARGE +typedef long rel_t ; +#else +typedef int rel_t ; +#endif +static void handle_relocations (void *output,unsigned long output_len,unsigned long virt_addr) { -int *reloc; +rel_t *reloc;unsigned long delta, map, ptr;unsigned long min_addr = (unsigned long )output;unsigned long max_addr = min_addr + (VO___bss_start - VO__text);
arch/x86/include/asm/page_64_types.h +9 -0
@@ -48,7 +48,11 @@#define __PAGE_OFFSET __PAGE_OFFSET_BASE#endif /* CONFIG_RANDOMIZE_MEMORY */ +#ifdef CONFIG_RANDOMIZE_BASE_LARGE +#define __START_KERNEL_map _AC(0xffffffff00000000, UL) +#else #define __START_KERNEL_map _AC(0xffffffff80000000, UL) +#endif /* CONFIG_RANDOMIZE_BASE_LARGE */ /* See Documentation/x86/x86_64/mm.txt for a description of the memory map. */ #ifdef CONFIG_X86_5LEVEL
@@ -65,9 +69,14 @@* 512MiB by default, leaving 1.5GiB for modules once the page tables * are fully set up. If kernel ASLR is configured, it can extend the * kernel page table mapping, reducing the size of the modules area. +* On PIE, we relocate the binary 2G lower so add this extra space. */ #if defined(CONFIG_RANDOMIZE_BASE ) +#ifdef CONFIG_RANDOMIZE_BASE_LARGE +#define KERNEL_IMAGE_SIZE (_AC(3, UL) * 1024 * 1024 * 1024) +#else #define KERNEL_IMAGE_SIZE (1024 * 1024 * 1024) +#endif #else #define KERNEL_IMAGE_SIZE (512 * 1024 * 1024)#endif
arch/x86/kernel/head64.c +14 -4
@@ -39,6 +39,7 @@ static unsigned int __initdata next_early_pgt;pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);#define __head __section(.head.text) +#define pud_count (x) (((x + (PUD_SIZE - 1)) & ~(PUD_SIZE - 1)) >> PUD_SHIFT) static void __head *fixup_pointer(void *ptr, unsigned long physaddr) {
@@ -56,6 +57,8 @@ unsigned long __head notrace __startup_64(unsigned long physaddr, {unsigned long load_delta, *p;unsigned long pgtable_flags; +unsigned long level3_kernel_start, level3_kernel_count; +unsigned long level3_fixmap_start;pgdval_t *pgd;p4dval_t *p4d;pudval_t *pud;
@@ -82,6 +85,11 @@ unsigned long __head notrace __startup_64(unsigned long physaddr,/* Include the SME encryption mask in the fixup value */ load_delta +=sme_get_me_mask (); +/* Look at the randomization spread to adapt page table used */ + level3_kernel_start =pud_index (__START_KERNEL_map); + level3_kernel_count =pud_count (KERNEL_IMAGE_SIZE ); + level3_fixmap_start = level3_kernel_start + level3_kernel_count; +/* Fixup the physical addresses in the page table */ pgd =fixup_pointer (&early_top_pgt, physaddr);
@@ -93,8 +101,9 @@ unsigned long __head notrace __startup_64(unsigned long physaddr, } pud =fixup_pointer (&level3_kernel_pgt, physaddr); - pud[510 ] += load_delta; - pud[511 ] += load_delta; +for (i =0 ; i < level3_kernel_count; i++) + pud[level3_kernel_start + i] += load_delta; + pud[level3_fixmap_start] += load_delta; pmd =fixup_pointer (level2_fixmap_pgt, physaddr); pmd[506 ] += load_delta;
@@ -147,7 +156,7 @@ unsigned long __head notrace __startup_64(unsigned long physaddr,*/ pmd =fixup_pointer (level2_kernel_pgt, physaddr); -for (i =0 ; i <PTRS_PER_PMD ; i++) { +for (i =0 ; i <PTRS_PER_PMD * level3_kernel_count; i++) {if (pmd[i] & _PAGE_PRESENT) pmd[i] += load_delta; }
@@ -322,7 +331,8 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)*/ BUILD_BUG_ON (MODULES_VADDR < __START_KERNEL_map);BUILD_BUG_ON (MODULES_VADDR - __START_KERNEL_map <KERNEL_IMAGE_SIZE ); -BUILD_BUG_ON (MODULES_LEN +KERNEL_IMAGE_SIZE >2 *PUD_SIZE ); +BUILD_BUG_ON (!IS_ENABLED (CONFIG_RANDOMIZE_BASE_LARGE ) && +MODULES_LEN +KERNEL_IMAGE_SIZE >2 *PUD_SIZE );BUILD_BUG_ON ((__START_KERNEL_map & ~PMD_MASK ) !=0 );BUILD_BUG_ON ((MODULES_VADDR & ~PMD_MASK ) !=0 );BUILD_BUG_ON (!(MODULES_VADDR > __START_KERNEL));
arch/x86/kernel/head_64.S +10 -1
@@ -39,11 +39,15 @@ #define p4d_index(x) (((x) >> P4D_SHIFT) & (PTRS_PER_P4D-1)) #define pud_index(x) (((x) >> PUD_SHIFT) & (PTRS_PER_PUD-1)) +#define pud_count(x) (((x + (PUD_SIZE - 1)) & ~(PUD_SIZE - 1)) >> PUD_SHIFT) PGD_PAGE_OFFSET = pgd_index(__PAGE_OFFSET_BASE) PGD_START_KERNEL = pgd_index(__START_KERNEL_map) L3_START_KERNEL = pud_index(__START_KERNEL_map) +/* Adapt page table L3 space based on range of randomization */ +L3_KERNEL_ENTRY_COUNT = pud_count(KERNEL_IMAGE_SIZE) + .text __HEAD .code64
@@ -411,7 +415,12 @@ NEXT_PAGE(level4_kernel_pgt) NEXT_PAGE(level3_kernel_pgt) .fill L3_START_KERNEL,8,0 /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */ - .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC + i = 0 + .rept L3_KERNEL_ENTRY_COUNT + .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC \ + + PAGE_SIZE*i + i = i + 1 + .endr .quad level2_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC NEXT_PAGE(level2_kernel_pgt)
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
Quoted 11 lines · Thomas Garnier
Changes:
- v2:
- Add support for global stack cookie while compiler default to fs without mcmodel=kernel
- Change patch 7 to correctly jump out of the identity mapping on kexec load preserve.These patches make the changes necessary to build the kernel as Position Independent Executable (PIE) on
x86_64. A PIE kernel can be relocated below the top 2G of the virtual address space. It allows to optionally extend the KASLR randomization range from 1G to 3G.
So this:
61 files changed, 923 insertions(+), 299 deletions(-)
... is IMHO an _awful_ lot of churn and extra complexity in pretty fragile pieces of code, to gain what appears to be only ~1.5 more bits of randomization!
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Thanks,
Ingo
Quoted 15 lines · Ingo Molnar
Quoted 11 lines · Thomas Garnier
Changes:
- v2:
- Add support for global stack cookie while compiler default to fs without mcmodel=kernel
- Change patch 7 to correctly jump out of the identity mapping on kexec load preserve.These patches make the changes necessary to build the kernel as Position Independent Executable (PIE) on
x86_64. A PIE kernel can be relocated below the top 2G of the virtual address space. It allows to optionally extend the KASLR randomization range from 1G to 3G.
Quoted 6 lines
So this:
61 files changed, 923 insertions(+), 299 deletions(-)... is IMHO an _awful_ lot of churn and extra complexity in pretty fragile pieces of code, to gain what appears to be only ~1.5 more bits of randomization!
The range increase is a way to use PIE right away.
Quoted 4 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
Quoted 4 lines
Thanks,
Ingo
Signature · 1 line
Thomas
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
+config RANDOMIZE_BASE_LARGE
+ bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
I think the fundamental flaw is the assumption that we need a PIE executable to have a freely relocatable kernel on 64-bit CPUs.
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
It should have far less performance impact than the register-losing and overhead-inducing -fpie / -mcmodel=large (for modules) execution models.
My quick guess is tha the performance impact might be close to zero in fact.
Thanks,
Ingo
Hello,
I write to put different perspective into the topic. I'm glad that kernel developers care about performance optimizations and I see how 10% overhead can be a problem for some. On the other hand last ten years gave us 1000% faster hardware which trumps anything software can do. For many users like us performance isn't a problem, we have plenty of it and if we haven't we can buy it. It can be money problem, not software engineering problem.
For security it's not that easy for us. We can't buy more of it as it's impossible to buy something that doesn't exist. All new proposed kernel security features are either disabled by default or have option to disable them. Users who prioritizes performance can disable every costly security feature that exist in kernel, they simply doesn't matter for them. Users who prioritize security can enable those security options but they can't enable those which aren't available at all which means they're often left with nothing.
I hope you see that those two kind of users and their priorities are treated very unequally in current kernel development. On the one hand kernel developers for years made many painstaking optimizations, on the other hand security related work of other developers which is same as hard get constant opposition and it's often completely rejected.
I believe there is room for both performance and security in kernel as long as they have optionality built in . My point is that kernel developers shouldn't "a priori" decide what's more important as there are more different usecases and needs they can imagine.
Quoted 60 lines
-------- Original Message --------
Subject: [kernel-hardening] Re: x86: PIE support and option to extend KASLR randomization
Local Time: August 15, 2017 7:56 AM
UTC Time: August 15, 2017 7:56 AM
From: [email protected]
To: Thomas Garnier <[email protected]>
Herbert Xu <[email protected]>, David S . Miller <[email protected]>, Thomas Gleixner <[email protected]>, Ingo Molnar <[email protected]>, H . Peter Anvin <[email protected]>, Peter Zijlstra <[email protected]>, Josh Poimboeuf <[email protected]>, Arnd Bergmann <[email protected]>, Matthias Kaehlcke <[email protected]>, Boris Ostrovsky <[email protected]>, Juergen Gross <[email protected]>, Paolo Bonzini <[email protected]>, Radim Krčmář <[email protected]>, Joerg Roedel <[email protected]>, Tom Lendacky <[email protected]>, Andy Lutomirski <[email protected]>, Borislav Petkov <[email protected]>, Brian Gerst <[email protected]>, Kirill A . Shutemov <[email protected]>, Rafael J . Wysocki <[email protected]>, Len Brown <[email protected]>, Pavel Machek <[email protected]>, Tejun Heo <[email protected]>, Christoph Lameter <[email protected]>, Paul Gortmaker <[email protected]>, Chris Metcalf <[email protected]>, Andrew Morton <[email protected]>, Paul E . McKenney <[email protected]>, Nicolas Pitre <[email protected]>, Christopher Li <[email protected]>, Rafael J . Wysocki <[email protected]>, Lukas Wunner <[email protected]>, Mika Westerberg <[email protected]>, Dou Liyang <[email protected]>, Daniel Borkmann <[email protected]>, Alexei Starovoitov <[email protected]>, Masahiro Yamada <[email protected]>, Markus Trippelsdorf <[email protected]>, Steven Rostedt <[email protected]>, Kees Cook <[email protected]>, Rik van Riel <[email protected]>, David Howells <[email protected]>, Waiman Long <[email protected]>, Kyle Huey <[email protected]>, Peter Foley <[email protected]>, Tim Chen <[email protected]>, Catalin Marinas <[email protected]>, Ard Biesheuvel <[email protected]>, Michal Hocko <[email protected]>, Matthew Wilcox <[email protected]>, H . J . Lu <[email protected]>, Paul Bolle <[email protected]>, Rob Landley <[email protected]>, Baoquan He <[email protected]>, Daniel Micay <[email protected]>, thearch/x86maintainers <[email protected]>, [email protected], LKML <[email protected]>, [email protected], kvm list <[email protected]>, Linux PM list <[email protected]>, linux-arch <[email protected]>, [email protected], Kernel Hardening <[email protected]>, Linus Torvalds <[email protected]>, Peter Zijlstra <[email protected]>, Borislav Petkov <[email protected]>Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we"ve done to keep the kernel fast ... (!!)
I think the fundamental flaw is the assumption that we need a PIE executable to have a freely relocatable kernel on 64-bit CPUs.
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
It should have far less performance impact than the register-losing and overhead-inducing -fpie / -mcmodel=large (for modules) execution models.
My quick guess is tha the performance impact might be close to zero in fact.
Thanks,
Ingo
Quoted 8 lines · Jordan Glover
Hello,
I write to put different perspective into the topic. I'm glad that kernel developers care about performance optimizations and I see how 10% overhead can be a problem for some. On the other hand last ten years gave us 1000% faster hardware which trumps anything software can do. For many users like us performance isn't a problem, we have plenty of it and if we haven't we can buy it. It can be money problem, not software engineering problem.
CPUs stopped getting faster at a dramatic rate, though, while network cards continue to get faster.
In some areas, the kernel is more squeezed for CPU cycles today than it has ever been before.
Ingo is suggesting that the security enhancement be implemented in a way that doesn't come with a 10% performance gain. There are other ways to relocate a kernel than with PIE.
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
Quoted 8 lines
I think the fundamental flaw is the assumption that we need a PIE executable to have a freely relocatable kernel on 64-bit CPUs.
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
I am going to start doing performance testing on -mcmodel=large to see if it is faster than -fPIE.
Quoted 5 lines
It should have far less performance impact than the register-losing and overhead-inducing -fpie / -mcmodel=large (for modules) execution models.
My quick guess is tha the performance impact might be close to zero in fact.
If mcmodel=small/medium was possible for kernel, I don't think it would have less performance impact than mcmodel=large. It would still need to set the high 32-bit to be a static value, only the relocation would be a different size.
Quoted 4 lines
Thanks,
Ingo
Signature · 1 line
Thomas
Quoted 34 lines · Thomas Garnier
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
Quoted 5 lines
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
The cost can be reduced by using -fno-plt these days but some work might be required to make that work with the kernel.
Where does that 10% estimate in the kernel config docs come from? I'd be surprised if it really cost that much on x86_64. That's a realistic cost for i386 with modern GCC (it used to be worse) but I'd expect x86_64 to be closer to 2% even for CPU intensive workloads. It should be very close to zero with -fno-plt.
Quoted 41 lines · Daniel Micay
Quoted 34 lines · Thomas Garnier
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.Quoted 5 lines
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
Quoted 8 lines
The cost can be reduced by using -fno-plt these days but some work might be required to make that work with the kernel.
Where does that 10% estimate in the kernel config docs come from? I'd be surprised if it really cost that much on
x86_64. That's a realistic cost for i386 with modern GCC (it used to be worse) but I'd expectx86_64to be closer to 2% even for CPU intensive workloads. It should be very close to zero with -fno-plt.
I got 8 to 10% on hackbench. Other benchmarks were 4% or lower.
I will do look at more recent compiler and no-plt as well.
Signature · 1 line
Thomas
Quoted 41 lines · Daniel Micay
Quoted 34 lines · Thomas Garnier
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.Quoted 5 lines
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
Quoted 8 lines
The cost can be reduced by using -fno-plt these days but some work might be required to make that work with the kernel.
Where does that 10% estimate in the kernel config docs come from? I'd be surprised if it really cost that much on
x86_64. That's a realistic cost for i386 with modern GCC (it used to be worse) but I'd expectx86_64to be closer to 2% even for CPU intensive workloads. It should be very close to zero with -fno-plt.
I got 8 to 10% on hackbench. Other benchmarks were 4% or lower.
I will do look at more recent compiler and no-plt as well.
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 34 lines · Thomas Garnier
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
Quoted 5 lines
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
The cost can be reduced by using -fno-plt these days but some work might be required to make that work with the kernel.
Where does that 10% estimate in the kernel config docs come from? I'd be surprised if it really cost that much on x86_64. That's a realistic cost for i386 with modern GCC (it used to be worse) but I'd expect x86_64 to be closer to 2% even for CPU intensive workloads. It should be very close to zero with -fno-plt.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 34 lines · Thomas Garnier
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
Quoted 5 lines
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
Note that the 8-10% hackbench or even a 2%-4% range would be 'huge' in terms of modern kernel performance. In many cases we are literally applying cycle level optimizations that are barely measurable. A 0.1% speedup in linear execution speed is already a big success.
Quoted 2 lines
I am going to start doing performance testing on -mcmodel=large to see if it is faster than -fPIE.
Unfortunately mcmodel=large looks pretty heavy too AFAICS, at the machine instruction level.
Function calls look like this:
-mcmodel=medium:
757: e8 98 ff ff ff callq 6f4 <test_code>
-mcmodel=large
77b: 48 b8 10 f7 df ff ff movabs $0xffffffffffdff710,%rax 782: ff ff ff 785: 48 8d 04 03 lea (%rbx,%rax,1),%rax 789: ff d0 callq *%rax
And we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
Quoted 10 lines
Quoted 7 lines
I think the fundamental flaw is the assumption that we need a PIE executable to have a freely relocatable kernel on 64-bit CPUs.
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I.e. is there no GCC code generation mode where code can be placed anywhere in the canonical address space, yet call and jump distance is within 31 bits so that the generated code is fast?
Thanks,
Ingo
Quoted 2 lines · Ingo Molnar
And we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
Ahh finally a limit is in sight as to how much security hardening etc can reduce kernel performance.
Quoted 2 lines · Ingo Molnar
And we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
Ahh finally a limit is in sight as to how much security hardening etc can reduce kernel performance.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 10 lines
How are these assumptions hardcoded by GCC? Most of the instructions should be
relocatable straight away, as most call/jump/branch instructions are RIP-relative.I.e. is there no GCC code generation mode where code can be placed anywhere in the
canonical address space, yet call and jump distance is within 31 bits so that the
generated code is fast?
That's what PIE is meant to do. However, not disabling support for lazy linking (-fno-plt) / symbol interposition (-Bsymbolic) is going to cause it to add needless overhead.
arm64 is using -pie -shared -Bsymbolic in arch/arm64/Makefile for their CONFIG_RELOCATABLE option. See 08cc55b2afd97a654f71b3bebf8bb0ec89fdc498.
Quoted 11 lines · Daniel Micay
Quoted 10 lines
How are these assumptions hardcoded by GCC? Most of the instructions should be
relocatable straight away, as most call/jump/branch instructions are RIP-relative.I.e. is there no GCC code generation mode where code can be placed anywhere in the
canonical address space, yet call and jump distance is within 31 bits so that the
generated code is fast?
Quoted 6 lines
That's what PIE is meant to do. However, not disabling support for lazy linking (-fno-plt) / symbol interposition (-Bsymbolic) is going to cause it to add needless overhead.
arm64 is using -pie -shared -Bsymbolic in
arch/arm64/Makefilefor theirCONFIG_RELOCATABLEoption. See08cc55b2afd97a654f71b3bebf8bb0ec89fdc498.
The difference with arm64 is that its generic small code model is already position independent, so we don't have to pass -fpic or -fpie to the compiler. We only link in PIE mode to get the linker to emit the dynamic relocation tables into the ELF binary. Relative branches have a range of +/- 128 MB, which covers the kernel and modules (unless the option to randomize the module region independently has been selected, in which case branches between the kernel and modules may be resolved via PLT entries that are emitted at module load time)
I am not sure how this extrapolates to x86, just adding some context.
Quoted 11 lines · Daniel Micay
Quoted 10 lines
How are these assumptions hardcoded by GCC? Most of the instructions should be
relocatable straight away, as most call/jump/branch instructions are RIP-relative.I.e. is there no GCC code generation mode where code can be placed anywhere in the
canonical address space, yet call and jump distance is within 31 bits so that the
generated code is fast?
Quoted 6 lines
That's what PIE is meant to do. However, not disabling support for lazy linking (-fno-plt) / symbol interposition (-Bsymbolic) is going to cause it to add needless overhead.
arm64 is using -pie -shared -Bsymbolic in
arch/arm64/Makefilefor theirCONFIG_RELOCATABLEoption. See08cc55b2afd97a654f71b3bebf8bb0ec89fdc498.
The difference with arm64 is that its generic small code model is already position independent, so we don't have to pass -fpic or -fpie to the compiler. We only link in PIE mode to get the linker to emit the dynamic relocation tables into the ELF binary. Relative branches have a range of +/- 128 MB, which covers the kernel and modules (unless the option to randomize the module region independently has been selected, in which case branches between the kernel and modules may be resolved via PLT entries that are emitted at module load time)
I am not sure how this extrapolates to x86, just adding some context.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 10 lines
How are these assumptions hardcoded by GCC? Most of the instructions should be
relocatable straight away, as most call/jump/branch instructions are RIP-relative.I.e. is there no GCC code generation mode where code can be placed anywhere in the
canonical address space, yet call and jump distance is within 31 bits so that the
generated code is fast?
That's what PIE is meant to do. However, not disabling support for lazy linking (-fno-plt) / symbol interposition (-Bsymbolic) is going to cause it to add needless overhead.
arm64 is using -pie -shared -Bsymbolic in arch/arm64/Makefile for their CONFIG_RELOCATABLE option. See 08cc55b2afd97a654f71b3bebf8bb0ec89fdc498.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 63 lines · Ingo Molnar
Quoted 34 lines · Thomas Garnier
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.Quoted 5 lines
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
Note that the 8-10% hackbench or even a 2%-4% range would be 'huge' in terms of modern kernel performance. In many cases we are literally applying cycle level optimizations that are barely measurable. A 0.1% speedup in linear execution speed is already a big success.
Quoted 2 lines
I am going to start doing performance testing on -mcmodel=large to see if it is faster than -fPIE.
Unfortunately mcmodel=large looks pretty heavy too AFAICS, at the machine instruction level.
Function calls look like this:
-mcmodel=medium:
757: e8 98 ff ff ff callq 6f4 <
test_code>-mcmodel=large
Quoted 8 lines
77b: 48 b8 10 f7 df ff ff movabs $0xffffffffffdff710,%rax 782: ff ff ff 785: 48 8d 04 03 lea (%rbx,%rax,1),%rax 789: ff d0 callq *%raxAnd we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
I started looking into mcmodel=large and ran into multiple issues. In the meantime, i thought I would
try difference configurations and compilers.
I did 10 hackbench runs accross 10 reboots with and without pie (same commit) with gcc 4.9. I copied
the result below and based on the hackbench configuration we are between -0.29% and 1.92% (average
across is 0.8%) which seems more aligned with what people discussed in this thread.
I don't know how I got 10% maximum on hackbench, I am still investigating. It could be the configuration
I used or my base compiler being too old.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
I think the fundamental flaw is the assumption that we need a PIE executable to have a freely relocatable kernel on 64-bit CPUs.
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
Quoted 5 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
Quoted 4 lines
I.e. is there no GCC code generation mode where code can be placed anywhere in the canonical address space, yet call and jump distance is within 31 bits so that the generated code is fast?
I think that's basically PIE. With PIE, you have the assumption everything is close, the main issue is any assembly referencing absolute addresses.
Quoted 4 lines
Thanks,
Ingo
process-pipe-1600 ------
baseline_samecommit pie % diff
0 16.985 16.999 0.082 1 17.065 17.071 0.033 2 17.188 17.130 -0.342 3 17.148 17.107 -0.240 4 17.217 17.170 -0.275 5 17.216 17.145 -0.415 6 17.161 17.109 -0.304 7 17.202 17.122 -0.465 8 17.169 17.173 0.024 9 17.217 17.178 -0.227 average 17.157 17.120 -0.213 median 17.169 17.122 -0.271 min 16.985 16.999 0.082 max 17.217 17.178 -0.228
[14 rows x 3 columns]
threads-pipe-1600 ------
baseline_samecommit pie % diff
0 17.914 18.041 0.707 1 18.337 18.352 0.083 2 18.233 18.457 1.225 3 18.334 18.402 0.366 4 18.381 18.369 -0.066 5 18.370 18.408 0.207 6 18.337 18.400 0.345 7 18.368 18.372 0.020 8 18.328 18.588 1.421 9 18.369 18.344 -0.138 average 18.297 18.373 0.415 median 18.337 18.373 0.200 min 17.914 18.041 0.707 max 18.381 18.588 1.126
[14 rows x 3 columns]
threads-pipe-50 ------
baseline_samecommit pie % diff
0 23.491 22.794 -2.965 1 23.219 23.542 1.387 2 22.886 23.638 3.286 3 23.233 23.778 2.343 4 23.228 23.703 2.046 5 23.000 23.376 1.636 6 23.589 23.335 -1.079 7 23.043 23.543 2.169 8 23.117 23.350 1.007 9 23.059 23.420 1.564 average 23.187 23.448 1.127 median 23.187 23.448 1.127 min 22.886 22.794 -0.399 max 23.589 23.778 0.800
[14 rows x 3 columns]
process-socket-50 ------
baseline_samecommit pie % diff
0 20.333 20.430 0.479 1 20.198 20.371 0.856 2 20.494 20.737 1.185 3 20.445 21.264 4.008 4 20.530 20.911 1.854 5 20.281 20.487 1.015 6 20.311 20.871 2.757 7 20.472 20.890 2.044 8 20.568 20.422 -0.710 9 20.415 20.647 1.134 average 20.405 20.703 1.462 median 20.415 20.703 1.410 min 20.198 20.371 0.856 max 20.568 21.264 3.385
[14 rows x 3 columns]
process-pipe-50 ------
baseline_samecommit pie % diff
0 20.131 20.643 2.541 1 20.184 20.658 2.349 2 20.359 20.907 2.693 3 20.365 21.284 4.514 4 20.506 20.578 0.352 5 20.393 20.599 1.010 6 20.245 20.515 1.331 7 20.627 20.964 1.636 8 20.519 20.862 1.670 9 20.505 20.741 1.150 average 20.383 20.775 1.922 median 20.383 20.741 1.753 min 20.131 20.515 1.907 max 20.627 21.284 3.186
[14 rows x 3 columns]
threads-socket-50 ------
baseline_samecommit pie % diff
0 23.197 23.728 2.286 1 23.304 23.585 1.205 2 23.098 23.379 1.217 3 23.028 23.787 3.295 4 23.242 23.122 -0.517 5 23.036 23.512 2.068 6 23.139 23.258 0.512 7 22.801 23.458 2.881 8 23.319 23.276 -0.187 9 22.989 23.577 2.557 average 23.115 23.468 1.526 median 23.115 23.468 1.526 min 22.801 23.122 1.407 max 23.319 23.787 2.006
[14 rows x 3 columns]
process-socket-1600 ------
baseline_samecommit pie % diff
0 17.214 17.168 -0.262 1 17.172 17.195 0.135 2 17.278 17.137 -0.817 3 17.173 17.102 -0.414 4 17.211 17.153 -0.335 5 17.220 17.160 -0.345 6 17.224 17.161 -0.365 7 17.224 17.224 -0.004 8 17.176 17.135 -0.236 9 17.242 17.188 -0.311 average 17.213 17.162 -0.296 median 17.214 17.161 -0.306 min 17.172 17.102 -0.405 max 17.278 17.224 -0.315
[14 rows x 3 columns]
threads-socket-1600 ------
baseline_samecommit pie % diff 0 18.395 18.389 -0.031 1 18.459 18.404 -0.296 2 18.427 18.445 0.096 3 18.449 18.421 -0.150 4 18.416 18.411 -0.026 5 18.409 18.443 0.185 6 18.325 18.308 -0.092 7 18.491 18.317 -0.940 8 18.496 18.375 -0.656 9 18.436 18.385 -0.279 average 18.430 18.390 -0.219 median 18.430 18.390 -0.219 min 18.325 18.308 -0.092 max 18.496 18.445 -0.278
[14 rows x 3 columns]
Total stats ======
baseline_samecommit pie % diff
average 19.773 19.930 0.791 median 19.773 19.930 0.791 min 16.985 16.999 0.082 max 23.589 23.787 0.839
[4 rows x 3 columns]
Signature · 1 line
Thomas
Quoted 9 lines · Thomas Garnier
Quoted 6 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
So if the numbers in your changelog and Kconfig text cannot be trusted, there's this description of the size impact which I suspect is less susceptible to measurement error:
+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
So I'm still unhappy about this all, and about the attitude surrounding it.
Thanks,
Ingo
Quoted 28 lines · Ingo Molnar
Quoted 9 lines · Thomas Garnier
Quoted 6 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
So if the numbers in your changelog and
Kconfigtext cannot be trusted, there's this description of the size impact which I suspect is less susceptible to measurement error:(quoted diff) +3
-0+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Quoted 3 lines
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
I will try to gather more information on the size increase. The size increase might be smaller with gcc 4.9 given performance was much better.
Quoted 6 lines
So I'm still unhappy about this all, and about the attitude surrounding it.
Thanks,
Ingo
Signature · 1 line
Thomas
Quoted 30 lines · Thomas Garnier
Quoted 28 lines · Ingo Molnar
Quoted 9 lines · Thomas Garnier
Quoted 6 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
So if the numbers in your changelog and
Kconfigtext cannot be trusted, there's this description of the size impact which I suspect is less susceptible to measurement error:(quoted diff) +3
-0+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Quoted 7 lines
Quoted 3 lines
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
I will try to gather more information on the size increase. The size increase might be smaller with gcc 4.9 given performance was much better.
Coming back on this thread as I identified the root cause of the performance issue.
My original performance testing was done with an Ubuntu generic configuration. This configuration has the CONFIG_FUNCTION_TRACER option which was incompatible with PIE. The tracer failed to replace the __fentry__ call by a nop slide on each traceable function because the instruction was not the one expected. If PIE is enabled, gcc generates a difference call instruction based on the GOT without checking the visibility options (basically call *__fentry__@GOTPCREL).
With the fix for function tracing, the hackbench results have an average of +0.8 to +1.4% (from +8% to +10% before). With a default configuration, the numbers are closer to 0.8%.
On the .text size, with gcc 4.9 I see +0.8% on default configuration and +1.180% on the ubuntu configuration.
Next iteration should have an updated set of performance metrics (will try to use gcc 6.0 or higher) and incorporate the fix on function tracing.
Let me know if you have questions and feedback.
Quoted 13 lines
Quoted 7 lines
So I'm still unhappy about this all, and about the attitude surrounding it.
Thanks,
Ingo
--
Thomas
Signature · 1 line
Thomas
Quoted 8 lines · Thomas Garnier
My original performance testing was done with an Ubuntu generic configuration. This configuration has the
CONFIG_FUNCTION_TRACERoption which was incompatible with PIE. The tracer failed to replace the__fentry__call by a nop slide on each traceable function because the instruction was not the one expected. If PIE is enabled, gcc generates a difference call instruction based on the GOT without checking the visibility options (basically call *__fentry__@GOTPCREL).
Gah.
Don't we actually have *more* address bits for randomization at the low end, rather than getting rid of -mcmodel=kernel?
Has anybody looked at just moving kernel text by smaller values than the page size? Yeah, yeah, the kernel has several sections that need page alignment, but I think we could relocate normal text by just the cacheline size, and that sounds like it would give several bits of randomness with little downside.
Or has somebody already looked at it and I just missed it?
Linus
Quoted 11 lines · Linus Torvalds
Quoted 8 lines · Thomas Garnier
My original performance testing was done with an Ubuntu generic configuration. This configuration has the
CONFIG_FUNCTION_TRACERoption which was incompatible with PIE. The tracer failed to replace the__fentry__call by a nop slide on each traceable function because the instruction was not the one expected. If PIE is enabled, gcc generates a difference call instruction based on the GOT without checking the visibility options (basically call *__fentry__@GOTPCREL).
Quoted 4 lines
Gah.
Don't we actually have *more* address bits for randomization at the low end, rather than getting rid of -mcmodel=kernel?
We have but I think we use most of it for potential modules and the fixmap but it is not that big. The increase in range from 1G to 3G is just an example and a way to ensure PIE work as expected. The long term goal is being able to put the kernel where we want in memory, randomizing the position and the order of almost all memory sections.
That would be valuable against BTB attack [1] for example where randomization on the low 32-bit is ineffective.
[1] https://github.com/felixwilhelm/mario_baslr
Quoted 6 lines
Has anybody looked at just moving kernel text by smaller values than the page size? Yeah, yeah, the kernel has several sections that need page alignment, but I think we could relocate normal text by just the cacheline size, and that sounds like it would give several bits of randomness with little downside.
I didn't look into it. There is value in it depending on performance impact. I think both PIE and lower grain randomization would be useful.
Quoted 4 lines
Or has somebody already looked at it and I just missed it?
Linus
Signature · 1 line
Thomas
Quoted 11 lines · Linus Torvalds
Quoted 8 lines · Thomas Garnier
My original performance testing was done with an Ubuntu generic configuration. This configuration has the
CONFIG_FUNCTION_TRACERoption which was incompatible with PIE. The tracer failed to replace the__fentry__call by a nop slide on each traceable function because the instruction was not the one expected. If PIE is enabled, gcc generates a difference call instruction based on the GOT without checking the visibility options (basically call *__fentry__@GOTPCREL).
Quoted 4 lines
Gah.
Don't we actually have *more* address bits for randomization at the low end, rather than getting rid of -mcmodel=kernel?
We have but I think we use most of it for potential modules and the fixmap but it is not that big. The increase in range from 1G to 3G is just an example and a way to ensure PIE work as expected. The long term goal is being able to put the kernel where we want in memory, randomizing the position and the order of almost all memory sections.
That would be valuable against BTB attack [1] for example where randomization on the low 32-bit is ineffective.
[1] https://github.com/felixwilhelm/mario_baslr
Quoted 6 lines
Has anybody looked at just moving kernel text by smaller values than the page size? Yeah, yeah, the kernel has several sections that need page alignment, but I think we could relocate normal text by just the cacheline size, and that sounds like it would give several bits of randomness with little downside.
I didn't look into it. There is value in it depending on performance impact. I think both PIE and lower grain randomization would be useful.
Quoted 4 lines
Or has somebody already looked at it and I just missed it?
Linus
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 8 lines · Thomas Garnier
My original performance testing was done with an Ubuntu generic configuration. This configuration has the
CONFIG_FUNCTION_TRACERoption which was incompatible with PIE. The tracer failed to replace the__fentry__call by a nop slide on each traceable function because the instruction was not the one expected. If PIE is enabled, gcc generates a difference call instruction based on the GOT without checking the visibility options (basically call *__fentry__@GOTPCREL).
Gah.
Don't we actually have *more* address bits for randomization at the low end, rather than getting rid of -mcmodel=kernel?
Has anybody looked at just moving kernel text by smaller values than the page size? Yeah, yeah, the kernel has several sections that need page alignment, but I think we could relocate normal text by just the cacheline size, and that sounds like it would give several bits of randomness with little downside.
Or has somebody already looked at it and I just missed it?
Linus
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Steven Rostedt
reviewerQuoted 3 lines · On Thu, 24 Aug 2017 14:13:38 -0700 Thomas Garnier <[email protected]> wrote:
With the fix for function tracing, the hackbench results have an average of +0.8 to +1.4% (from +8% to +10% before). With a default configuration, the numbers are closer to 0.8%.
Wow, an empty fentry function not "nop"ed out only added 8% to 10% overhead. I never did the benchmarks of that since I did it before fentry was introduced, which was with the old "mcount". That gave an average of 13% overhead in hackbench.
-- Steve
Quoted 6 lines · Thomas Garnier
With the fix for function tracing, the hackbench results have an average of +0.8 to +1.4% (from +8% to +10% before). With a default configuration, the numbers are closer to 0.8%.
On the .text size, with gcc 4.9 I see +0.8% on default configuration and +1.180% on the ubuntu configuration.
A 1% text size increase is still significant. Could you look at the disassembly, where does the size increase come from?
Thanks,
Ingo
Quoted 7 lines · Ingo Molnar
Quoted 4 lines · Thomas Garnier
With the fix for function tracing, the hackbench results have an average of +0.8 to +1.4% (from +8% to +10% before). With a default configuration, the numbers are closer to 0.8%.
Quoted 5 lines
Quoted 2 lines
On the .text size, with gcc 4.9 I see +0.8% on default configuration and +1.180% on the ubuntu configuration.
A 1% text size increase is still significant. Could you look at the disassembly, where does the size increase come from?
I will take a look, in this current iteration I added the .got and .got.plt so removing them will remove a big (even if they are small, we don't use them to increase perf).
What do you think about the perf numbers in general so far?
Quoted 4 lines
Thanks,
Ingo
Signature · 1 line
Thomas
Quoted 11 lines · Thomas Garnier
Quoted 10 lines · Ingo Molnar
Quoted 6 lines · Thomas Garnier
With the fix for function tracing, the hackbench results have an average of +0.8 to +1.4% (from +8% to +10% before). With a default configuration, the numbers are closer to 0.8%.
On the .text size, with gcc 4.9 I see +0.8% on default configuration and +1.180% on the ubuntu configuration.
Quoted 8 lines
Quoted 2 lines
A 1% text size increase is still significant. Could you look at the disassembly, where does the size increase come from?
I will take a look, in this current iteration I added the .got and .got.plt so removing them will remove a big (even if they are small, we don't use them to increase perf).
What do you think about the perf numbers in general so far?
I looked at the size increase. I could identify two common cases:
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
For example, this instruction plays on the sign extension (mcmodel=kernel):
mov r9,QWORD PTR [r11*8-0x7e3da060] (8 bytes)
The address 0xffffffff81c25fa0 can be represented as -0x7e3da060 using a 32S relocation.
with PIE:
lea rbx,[rip+<off>] (7 bytes)
mov r9,QWORD PTR [rbx+r11*8] (6 bytes)
2) GCC does not optimize switches in PIE in order to reduce relocations:
For example the switch in phy_modes [1]:
static inline const char *phy_modes(phy_interface_t interface)
{
switch (interface) {
case PHY_INTERFACE_MODE_NA:
return "";
case PHY_INTERFACE_MODE_INTERNAL:
return "internal";
case PHY_INTERFACE_MODE_MII:
return "mii";Without PIE (gcc 7.2.0), the whole table is optimize to be one instruction:
0x000000000040045b <+27>: mov rdi,QWORD PTR [rax*8+0x400660]
With PIE (gcc 7.2.0):
0x0000000000000641 <+33>: movsxd rax,DWORD PTR [rdx+rax*4] 0x0000000000000645 <+37>: add rax,rdx 0x0000000000000648 <+40>: jmp rax .... 0x000000000000065d <+61>: lea rdi,[rip+0x264] # 0x8c8 0x0000000000000664 <+68>: jmp 0x651 <main+49> 0x0000000000000666 <+70>: lea rdi,[rip+0x2bc] # 0x929 0x000000000000066d <+77>: jmp 0x651 <main+49> 0x000000000000066f <+79>: lea rdi,[rip+0x2a8] # 0x91e 0x0000000000000676 <+86>: jmp 0x651 <main+49> 0x0000000000000678 <+88>: lea rdi,[rip+0x294] # 0x913 0x000000000000067f <+95>: jmp 0x651 <main+49>
That's a deliberate choice, clang is able to optimize it (clang-3.8):
0x0000000000000963 <+19>: lea rcx,[rip+0x200406] # 0x200d70 0x000000000000096a <+26>: mov rdi,QWORD PTR [rcx+rax*8]
I checked gcc and the code deciding to fold the switch basically do not do it for pic to reduce relocations [2].
The switches are the biggest increase on small functions but I don't think they represent a large portion of the difference (number 1 is).
A side note, while testing gcc 7.2.0 on hackbench I have seen the PIE kernel being faster by 1% across multiple runs (comparing 50 runs done across 5 reboots twice). I don't think PIE is faster than a mcmodel=kernel but recent versions of gcc makes them fairly similar.
[1] http://elixir.free-electrons.com/linux/v4.13-rc7/source/include/linux/phy.h#L113
[2] https://github.com/gcc-mirror/gcc/blob/7977b0509f07e42fbe0f06efcdead2b7e4a5135f/gcc/tree-switch-conversion.c#L828
Quoted 10 lines
Quoted 5 lines
Thanks,
Ingo
--
Thomas
Signature · 1 line
Thomas
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )
Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 2 lines
The switches are the biggest increase on small functions but I don't think they represent a large portion of the difference (number 1 is).
Ok.
Quoted 4 lines
A side note, while testing gcc 7.2.0 on hackbench I have seen the PIE kernel being faster by 1% across multiple runs (comparing 50 runs done across 5 reboots twice). I don't think PIE is faster than a mcmodel=kernel but recent versions of gcc makes them fairly similar.
So I think we are down to an overhead range where the inherent noise (both random and systematic one) in 'hackbench' overwhelms the signal we are trying to measure.
So I think it's the kernel .text size change that is the best noise-free proxy for the overhead impact of PIE.
It doesn't hurt to double check actual real performance as well, just don't expect there to be much of a signal for anything but fully cached microbenchmark workloads.
Thanks,
Ingo
Quoted 13 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Quoted 9 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
There are somewhat related concerns in the ARM world, so it would be good if we could work with the GCC developers to get a more high level and arch neutral command line option (-mkernel-pie? sounds yummy!) that stops the compiler from making inferences that only hold for shared libraries and/or other hosted executables (GOT indirections, avoiding text relocations etc). That way, we will also be able to drop the 'hidden' visibility override at some point, which we currently need to prevent the compiler from redirecting all global symbol references via entries in the GOT.
All we really need is the ability to move the image around in virtual memory, and things like reducing the CoW footprint or enabling ELF symbol preemption are completely irrelevant for us.
Quoted 25 lines · Ard Biesheuvel
Quoted 13 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Quoted 9 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 9 lines
There are somewhat related concerns in the ARM world, so it would be good if we could work with the GCC developers to get a more high level and arch neutral command line option (-mkernel-pie? sounds yummy!) that stops the compiler from making inferences that only hold for shared libraries and/or other hosted executables (GOT indirections, avoiding text relocations etc). That way, we will also be able to drop the 'hidden' visibility override at some point, which we currently need to prevent the compiler from redirecting all global symbol references via entries in the GOT.
My plan was to add a -mtls-reg=<fs|gs> to switch the default segment register for stack cookies but I can see great benefits in having a more general kernel flag that would allow to get rid of the GOT and PLT when you are building position independent code for the kernel. It could also include optimizations like folding switch tables etc...
Should we start a separate discussion on that? Anyone that would be more experienced than I to push that to gcc & clang upstream?
Quoted 4 lines
All we really need is the ability to move the image around in virtual memory, and things like reducing the CoW footprint or enabling ELF symbol preemption are completely irrelevant for us.
Signature · 1 line
Thomas
Quoted 37 lines · Thomas Garnier
Quoted 25 lines · Ard Biesheuvel
Quoted 13 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Quoted 9 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 9 lines
There are somewhat related concerns in the ARM world, so it would be good if we could work with the GCC developers to get a more high level and arch neutral command line option (-mkernel-pie? sounds yummy!) that stops the compiler from making inferences that only hold for shared libraries and/or other hosted executables (GOT indirections, avoiding text relocations etc). That way, we will also be able to drop the 'hidden' visibility override at some point, which we currently need to prevent the compiler from redirecting all global symbol references via entries in the GOT.
Quoted 8 lines
My plan was to add a -mtls-reg=<fs|gs> to switch the default segment register for stack cookies but I can see great benefits in having a more general kernel flag that would allow to get rid of the GOT and PLT when you are building position independent code for the kernel. It could also include optimizations like folding switch tables etc...
Should we start a separate discussion on that? Anyone that would be more experienced than I to push that to gcc & clang upstream?
Just open a gcc bug. See
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81708 as an example.
Signature · 1 line
Markus
Quoted 44 lines · Markus Trippelsdorf
Quoted 43 lines · Thomas Garnier
Quoted 25 lines · Ard Biesheuvel
Quoted 13 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Quoted 9 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 9 lines
There are somewhat related concerns in the ARM world, so it would be good if we could work with the GCC developers to get a more high level and arch neutral command line option (-mkernel-pie? sounds yummy!) that stops the compiler from making inferences that only hold for shared libraries and/or other hosted executables (GOT indirections, avoiding text relocations etc). That way, we will also be able to drop the 'hidden' visibility override at some point, which we currently need to prevent the compiler from redirecting all global symbol references via entries in the GOT.
My plan was to add a -mtls-reg=<fs|gs> to switch the default segment register for stack cookies but I can see great benefits in having a more general kernel flag that would allow to get rid of the GOT and PLT when you are building position independent code for the kernel. It could also include optimizations like folding switch tables etc...
Quoted 5 lines
Quoted 2 lines
Should we start a separate discussion on that? Anyone that would be more experienced than I to push that to gcc & clang upstream?
Just open a gcc bug. See
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81708 as an example.
Make sense, I will look into this. Thanks Andy for the stack cookie bug!
Quoted 3 lines
--
Markus
Signature · 1 line
Thomas
Quoted 44 lines · Markus Trippelsdorf
Quoted 43 lines · Thomas Garnier
Quoted 25 lines · Ard Biesheuvel
Quoted 13 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Quoted 9 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 9 lines
There are somewhat related concerns in the ARM world, so it would be good if we could work with the GCC developers to get a more high level and arch neutral command line option (-mkernel-pie? sounds yummy!) that stops the compiler from making inferences that only hold for shared libraries and/or other hosted executables (GOT indirections, avoiding text relocations etc). That way, we will also be able to drop the 'hidden' visibility override at some point, which we currently need to prevent the compiler from redirecting all global symbol references via entries in the GOT.
My plan was to add a -mtls-reg=<fs|gs> to switch the default segment register for stack cookies but I can see great benefits in having a more general kernel flag that would allow to get rid of the GOT and PLT when you are building position independent code for the kernel. It could also include optimizations like folding switch tables etc...
Quoted 5 lines
Quoted 2 lines
Should we start a separate discussion on that? Anyone that would be more experienced than I to push that to gcc & clang upstream?
Just open a gcc bug. See
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81708 as an example.
Make sense, I will look into this. Thanks Andy for the stack cookie bug!
Quoted 3 lines
--
Markus
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 37 lines · Thomas Garnier
Quoted 25 lines · Ard Biesheuvel
Quoted 13 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Quoted 9 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 9 lines
There are somewhat related concerns in the ARM world, so it would be good if we could work with the GCC developers to get a more high level and arch neutral command line option (-mkernel-pie? sounds yummy!) that stops the compiler from making inferences that only hold for shared libraries and/or other hosted executables (GOT indirections, avoiding text relocations etc). That way, we will also be able to drop the 'hidden' visibility override at some point, which we currently need to prevent the compiler from redirecting all global symbol references via entries in the GOT.
Quoted 8 lines
My plan was to add a -mtls-reg=<fs|gs> to switch the default segment register for stack cookies but I can see great benefits in having a more general kernel flag that would allow to get rid of the GOT and PLT when you are building position independent code for the kernel. It could also include optimizations like folding switch tables etc...
Should we start a separate discussion on that? Anyone that would be more experienced than I to push that to gcc & clang upstream?
After separate discussion, opened:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82303
Quoted 11 lines
Quoted 5 lines
All we really need is the ability to move the image around in virtual memory, and things like reducing the CoW footprint or enabling ELF symbol preemption are completely irrelevant for us.
--
Thomas
Signature · 1 line
Thomas
Quoted 37 lines · Thomas Garnier
Quoted 25 lines · Ard Biesheuvel
Quoted 13 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Quoted 9 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 9 lines
There are somewhat related concerns in the ARM world, so it would be good if we could work with the GCC developers to get a more high level and arch neutral command line option (-mkernel-pie? sounds yummy!) that stops the compiler from making inferences that only hold for shared libraries and/or other hosted executables (GOT indirections, avoiding text relocations etc). That way, we will also be able to drop the 'hidden' visibility override at some point, which we currently need to prevent the compiler from redirecting all global symbol references via entries in the GOT.
Quoted 8 lines
My plan was to add a -mtls-reg=<fs|gs> to switch the default segment register for stack cookies but I can see great benefits in having a more general kernel flag that would allow to get rid of the GOT and PLT when you are building position independent code for the kernel. It could also include optimizations like folding switch tables etc...
Should we start a separate discussion on that? Anyone that would be more experienced than I to push that to gcc & clang upstream?
After separate discussion, opened:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82303
Quoted 11 lines
Quoted 5 lines
All we really need is the ability to move the image around in virtual memory, and things like reducing the CoW footprint or enabling ELF symbol preemption are completely irrelevant for us.
--
Thomas
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 25 lines · Ard Biesheuvel
Quoted 13 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Quoted 9 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 9 lines
There are somewhat related concerns in the ARM world, so it would be good if we could work with the GCC developers to get a more high level and arch neutral command line option (-mkernel-pie? sounds yummy!) that stops the compiler from making inferences that only hold for shared libraries and/or other hosted executables (GOT indirections, avoiding text relocations etc). That way, we will also be able to drop the 'hidden' visibility override at some point, which we currently need to prevent the compiler from redirecting all global symbol references via entries in the GOT.
My plan was to add a -mtls-reg=<fs|gs> to switch the default segment register for stack cookies but I can see great benefits in having a more general kernel flag that would allow to get rid of the GOT and PLT when you are building position independent code for the kernel. It could also include optimizations like folding switch tables etc...
Should we start a separate discussion on that? Anyone that would be more experienced than I to push that to gcc & clang upstream?
Quoted 4 lines
All we really need is the ability to move the image around in virtual memory, and things like reducing the CoW footprint or enabling ELF symbol preemption are completely irrelevant for us.
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 13 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Quoted 9 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
There are somewhat related concerns in the ARM world, so it would be good if we could work with the GCC developers to get a more high level and arch neutral command line option (-mkernel-pie? sounds yummy!) that stops the compiler from making inferences that only hold for shared libraries and/or other hosted executables (GOT indirections, avoiding text relocations etc). That way, we will also be able to drop the 'hidden' visibility override at some point, which we currently need to prevent the compiler from redirecting all global symbol references via entries in the GOT.
All we really need is the ability to move the image around in virtual memory, and things like reducing the CoW footprint or enabling ELF symbol preemption are completely irrelevant for us.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 5 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )
Quoted 7 lines
Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
I am not sure what is the best way to measure that.
Quoted 4 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
I used the default configuration which uses frame-pointer. I built all the different binaries with ORC and I see an improvement in size:
On latest revision (just built and ran performance tests this week):
With framepointer: PIE .text is 0.837324% than baseline
With ORC: PIE .text is 0.814224% than baseline
Comparing baselines only, ORC is -2.849832% than frame-pointers.
Quoted 16 lines
Quoted 2 lines
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 2 lines
The switches are the biggest increase on small functions but I don't think they represent a large portion of the difference (number 1 is).
Ok.
Quoted 4 lines
A side note, while testing gcc 7.2.0 on hackbench I have seen the PIE kernel being faster by 1% across multiple runs (comparing 50 runs done across 5 reboots twice). I don't think PIE is faster than a mcmodel=kernel but recent versions of gcc makes them fairly similar.
Quoted 5 lines
So I think we are down to an overhead range where the inherent noise (both random and systematic one) in 'hackbench' overwhelms the signal we are trying to measure.
So I think it's the kernel .text size change that is the best noise-free proxy for the overhead impact of PIE.
I agree but it might be hard to measure the exact impact. What is acceptable and what is not?
Quoted 4 lines
It doesn't hurt to double check actual real performance as well, just don't expect there to be much of a signal for anything but fully cached microbenchmark workloads.
That's aligned with what I see in the latest performance testing. Performance is close enough that it is hard to get exact numbers (pie is just a bit slower than baseline on hackench (~1%)).
Quoted 4 lines
Thanks,
Ingo
Signature · 1 line
Thomas
Quoted 9 lines · Thomas Garnier
Quoted 7 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )* Thomas Garnier <[email protected]> wrote:
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
I am not sure what is the best way to measure that.
A very approximate approach would be to look at each instruction using the signed trick with a _32S relocation. All _32S relocations won't be translated to more instructions because some are just relocating part of an absolute mov which would be actually smaller if relative.
Used this command to get a relative estimate:
objdump -dr ./baseline/vmlinux | egrep -A 2 '\-0x[0-9a-f]{8}' | grep _32S | wc -l
Got 6130 places, if you assume each add at least 7 bytes. It adds at least 42910 bytes on the .text section. The text section is 78599 bytes bigger from baseline to PIE. That's at least 54% of the size difference. Assuming we found all of them and we can't factor the impact on using an additional register.
Similar approach with the switch table but a bit more complex:
1) Find all constructs as with an lea (%rip) followed by a jmp instruction inside a function (typical unfolded switch case).
2) Remove occurrences of less than 4 for the destination address
Result: 480 switch cases in 49 functions. Each case take at least 9 bytes and the switch itself takes 16 bytes (assuming one per function).
That's 5104 bytes for easy to identify switches (less than 7% of the increase).
I am certainly missing a lot of differences. I checked if the percpu changes impacted the size and it doesn't (only 3 bytes added on PIE).
I also tried different ways to compare the .text section like size of symbols or number of bytes on full disassembly but the results are really off from the whole .text size so I am not sure if it is the right way to go about it.
Quoted 60 lines
Quoted 5 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
I used the default configuration which uses frame-pointer. I built all the different binaries with ORC and I see an improvement in size:
On latest revision (just built and ran performance tests this week):
With framepointer: PIE .text is 0.837324% than baseline
With ORC: PIE .text is 0.814224% than baseline
Comparing baselines only, ORC is -2.849832% than frame-pointers.
Quoted 16 lines
Quoted 2 lines
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 2 lines
The switches are the biggest increase on small functions but I don't think they represent a large portion of the difference (number 1 is).
Ok.
Quoted 4 lines
A side note, while testing gcc 7.2.0 on hackbench I have seen the PIE kernel being faster by 1% across multiple runs (comparing 50 runs done across 5 reboots twice). I don't think PIE is faster than a mcmodel=kernel but recent versions of gcc makes them fairly similar.
Quoted 5 lines
So I think we are down to an overhead range where the inherent noise (both random and systematic one) in 'hackbench' overwhelms the signal we are trying to measure.
So I think it's the kernel .text size change that is the best noise-free proxy for the overhead impact of PIE.
I agree but it might be hard to measure the exact impact. What is acceptable and what is not?
Quoted 4 lines
It doesn't hurt to double check actual real performance as well, just don't expect there to be much of a signal for anything but fully cached microbenchmark workloads.
That's aligned with what I see in the latest performance testing. Performance is close enough that it is hard to get exact numbers (pie is just a bit slower than baseline on hackench (~1%)).
Quoted 4 lines
Thanks,
Ingo
--
Thomas
Signature · 1 line
Thomas
Quoted 9 lines · Thomas Garnier
Quoted 7 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )* Thomas Garnier <[email protected]> wrote:
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
I am not sure what is the best way to measure that.
A very approximate approach would be to look at each instruction using the signed trick with a _32S relocation. All _32S relocations won't be translated to more instructions because some are just relocating part of an absolute mov which would be actually smaller if relative.
Used this command to get a relative estimate:
objdump -dr ./baseline/vmlinux | egrep -A 2 '\-0x[0-9a-f]{8}' | grep _32S | wc -l
Got 6130 places, if you assume each add at least 7 bytes. It adds at least 42910 bytes on the .text section. The text section is 78599 bytes bigger from baseline to PIE. That's at least 54% of the size difference. Assuming we found all of them and we can't factor the impact on using an additional register.
Similar approach with the switch table but a bit more complex:
1) Find all constructs as with an lea (%rip) followed by a jmp instruction inside a function (typical unfolded switch case).
2) Remove occurrences of less than 4 for the destination address
Result: 480 switch cases in 49 functions. Each case take at least 9 bytes and the switch itself takes 16 bytes (assuming one per function).
That's 5104 bytes for easy to identify switches (less than 7% of the increase).
I am certainly missing a lot of differences. I checked if the percpu changes impacted the size and it doesn't (only 3 bytes added on PIE).
I also tried different ways to compare the .text section like size of symbols or number of bytes on full disassembly but the results are really off from the whole .text size so I am not sure if it is the right way to go about it.
Quoted 60 lines
Quoted 5 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
I used the default configuration which uses frame-pointer. I built all the different binaries with ORC and I see an improvement in size:
On latest revision (just built and ran performance tests this week):
With framepointer: PIE .text is 0.837324% than baseline
With ORC: PIE .text is 0.814224% than baseline
Comparing baselines only, ORC is -2.849832% than frame-pointers.
Quoted 16 lines
Quoted 2 lines
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 2 lines
The switches are the biggest increase on small functions but I don't think they represent a large portion of the difference (number 1 is).
Ok.
Quoted 4 lines
A side note, while testing gcc 7.2.0 on hackbench I have seen the PIE kernel being faster by 1% across multiple runs (comparing 50 runs done across 5 reboots twice). I don't think PIE is faster than a mcmodel=kernel but recent versions of gcc makes them fairly similar.
Quoted 5 lines
So I think we are down to an overhead range where the inherent noise (both random and systematic one) in 'hackbench' overwhelms the signal we are trying to measure.
So I think it's the kernel .text size change that is the best noise-free proxy for the overhead impact of PIE.
I agree but it might be hard to measure the exact impact. What is acceptable and what is not?
Quoted 4 lines
It doesn't hurt to double check actual real performance as well, just don't expect there to be much of a signal for anything but fully cached microbenchmark workloads.
That's aligned with what I see in the latest performance testing. Performance is close enough that it is hard to get exact numbers (pie is just a bit slower than baseline on hackench (~1%)).
Quoted 4 lines
Thanks,
Ingo
--
Thomas
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 8 lines · Thomas Garnier
Quoted 7 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )* Thomas Garnier <[email protected]> wrote:
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
I am not sure what is the best way to measure that.
If this is the dominant factor then 'sizeof vmlinux' ought to be enough:
Quoted 1 line
With ORC: PIE .text is 0.814224% than baseline
I.e. the overhead is +0.81% in both size and (roughly) in number of instructions executed.
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
Ok, I suspect we can try it, but my preconditions for merging it would be:
1) Linus doesn't NAK it (obviously)
2) we first implement the additional entropy bits that Linus suggested.
does this work for you?
Thanks,
Ingo
Quoted 21 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 7 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )* Thomas Garnier <[email protected]> wrote:
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
I am not sure what is the best way to measure that.
If this is the dominant factor then 'sizeof vmlinux' ought to be enough:
Quoted 8 lines
Quoted 1 line
With ORC: PIE .text is 0.814224% than baseline
I.e. the overhead is +0.81% in both size and (roughly) in number of instructions executed.
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
That make sense.
Quoted 4 lines
Ok, I suspect we can try it, but my preconditions for merging it would be:
1) Linus doesn't NAK it (obviously)
Of course.
Quoted 3 lines
2) we first implement the additional entropy bits that Linus suggested.
does this work for you?
Sure, I can look at how feasible that is. If it is, can I send everything as part of the same patch set? The additional entropy would be enabled for all KASLR but PIE will be off-by-default of course.
Quoted 4 lines
Thanks,
Ingo
Signature · 1 line
Thomas
Quoted 7 lines · Thomas Garnier
Quoted 3 lines
2) we first implement the additional entropy bits that Linus suggested.
does this work for you?
Sure, I can look at how feasible that is. If it is, can I send everything as part of the same patch set? The additional entropy would be enabled for all KASLR but PIE will be off-by-default of course.
Sure, can all be part of the same series.
Thanks,
Ingo
Quoted 5 lines · Ingo Molnar
Quoted 2 lines · Thomas Garnier
Quoted 2 lines
2) we first implement the additional entropy bits that Linus suggested.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
does this work for you?
Sure, I can look at how feasible that is. If it is, can I send everything as part of the same patch set? The additional entropy would be enabled for all KASLR but PIE will be off-by-default of course.
Sure, can all be part of the same series.
I looked deeper in the change Linus proposed (moving the .text section based on the cacheline). I think the complexity is too high for the value of this change.
To move only the .text section would require at least the following changes:
- Overall change on how relocations are processed, need to separate
relocations in and outside of the .text section.
- Break assumptions on _text alignment while keeping calculation on
size accurate (for example _end - _text).
With a rough attempt at this, I managed to pass early boot and still crash later on.
This change would be valuable if you leak the address of a section other than .text and you want to know where .text is. Meaning the main bug that you are trying to exploit only allow you to execute code (and you are trying to ROP in .text). I would argue that a better mitigation for this type of bugs is moving function pointer to read-only sections and using stack cookies (for ret address). This change won't prevent other type of attacks, like data corruption.
I think it would be more valuable to look at something like selfrando / pagerando [1] but maybe wait a bit for it to be more mature (especially on the debugging side).
What do you think?
[1] http://lists.llvm.org/pipermail/llvm-dev/2017-June/113794.html
Quoted 4 lines
Thanks,
Ingo
Signature · 1 line
Thomas
Quoted 5 lines · Ingo Molnar
Quoted 2 lines · Thomas Garnier
Quoted 2 lines
2) we first implement the additional entropy bits that Linus suggested.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
does this work for you?
Sure, I can look at how feasible that is. If it is, can I send everything as part of the same patch set? The additional entropy would be enabled for all KASLR but PIE will be off-by-default of course.
Sure, can all be part of the same series.
I looked deeper in the change Linus proposed (moving the .text section based on the cacheline). I think the complexity is too high for the value of this change.
To move only the .text section would require at least the following changes:
- Overall change on how relocations are processed, need to separate
relocations in and outside of the .text section.
- Break assumptions on _text alignment while keeping calculation on
size accurate (for example _end - _text).
With a rough attempt at this, I managed to pass early boot and still crash later on.
This change would be valuable if you leak the address of a section other than .text and you want to know where .text is. Meaning the main bug that you are trying to exploit only allow you to execute code (and you are trying to ROP in .text). I would argue that a better mitigation for this type of bugs is moving function pointer to read-only sections and using stack cookies (for ret address). This change won't prevent other type of attacks, like data corruption.
I think it would be more valuable to look at something like selfrando / pagerando [1] but maybe wait a bit for it to be more mature (especially on the debugging side).
What do you think?
[1] http://lists.llvm.org/pipermail/llvm-dev/2017-June/113794.html
Quoted 4 lines
Thanks,
Ingo
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 7 lines · Thomas Garnier
Quoted 3 lines
2) we first implement the additional entropy bits that Linus suggested.
does this work for you?
Sure, I can look at how feasible that is. If it is, can I send everything as part of the same patch set? The additional entropy would be enabled for all KASLR but PIE will be off-by-default of course.
Sure, can all be part of the same series.
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 21 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 7 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )* Thomas Garnier <[email protected]> wrote:
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
I am not sure what is the best way to measure that.
If this is the dominant factor then 'sizeof vmlinux' ought to be enough:
Quoted 8 lines
Quoted 1 line
With ORC: PIE .text is 0.814224% than baseline
I.e. the overhead is +0.81% in both size and (roughly) in number of instructions executed.
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
That make sense.
Quoted 4 lines
Ok, I suspect we can try it, but my preconditions for merging it would be:
1) Linus doesn't NAK it (obviously)
Of course.
Quoted 3 lines
2) we first implement the additional entropy bits that Linus suggested.
does this work for you?
Sure, I can look at how feasible that is. If it is, can I send everything as part of the same patch set? The additional entropy would be enabled for all KASLR but PIE will be off-by-default of course.
Quoted 4 lines
Thanks,
Ingo
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
H. Peter Anvin
reviewerQuoted 5 lines · Ingo Molnar
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
I'm somewhat confused how we can have as much as almost 1% overhead. I suspect that we end up making a GOT and maybe even a PLT for no good reason.
-hpa
Quoted 2 lines · H. Peter Anvin
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
FWIW, since gcc 5, the PIC register isn't totally lost. It is now reusable, and that seems to have improved performance:
https://gcc.gnu.org/gcc-5/changes.html
-Kees
Signature · 2 lines
Kees Cook Pixel Security
H. Peter Anvin
reviewerQuoted 7 lines · Kees Cook
Quoted 2 lines · H. Peter Anvin
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
FWIW, since gcc 5, the PIC register isn't totally lost. It is now reusable, and that seems to have improved performance:
https://gcc.gnu.org/gcc-5/changes.html
It still talks about a PIC register on x86-64, which confuses me. Perhaps older gcc's would allocate a PIC register under certain circumstances, and then lose it for the entire function?
For i386, the PIC register is required by the ABI to be %ebx at the point any PLT entry is called. Not an issue with -mno-plt which goes straight to the GOT, although in most cases there needs to be a PIC register to find the GOT unless load-time relocation is permitted.
-hpa
Quoted 16 lines · H. Peter Anvin
Quoted 1 line · Kees Cook
On Fri, Sep 22, 2017 at 11:38 AM, H. Peter Anvin <[email protected]>
Quoted 1 line · wrote:
Quoted 1 line
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since
x86-64
Quoted 1 line
Quoted 1 line
has RIP-relative addressing there is no need for a dedicated PIC
register.
Quoted 4 lines
FWIW, since gcc 5, the PIC register isn't totally lost. It is now reusable, and that seems to have improved performance:
https://gcc.gnu.org/gcc-5/changes.htmlIt still talks about a PIC register on x86-64, which confuses me. Perhaps older gcc's would allocate a PIC register under certain circumstances, and then lose it for the entire function?
Quoted 6 lines
For i386, the PIC register is required by the ABI to be %ebx at the point any PLT entry is called. Not an issue with -mno-plt which goes straight to the GOT, although in most cases there needs to be a PIC register to find the GOT unless load-time relocation is permitted.
-hpa
We need a static PIE option so that compiler can optimize it without using hidden visibility.
H.J.
Sent from my Android device with K-9 Mail. Please excuse my brevity.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
<[email protected]>,Andrew Morton <[email protected]>,"Paul E . McKenney" <[email protected]>,Nicolas Pitre <[email protected]>,Christopher Li <[email protected]>,"Rafael J . Wysocki" <[email protected]>,Lukas Wunner <[email protected]>,Mika Westerberg <[email protected]>,Dou Liyang <[email protected]>,Daniel Borkmann <[email protected]>,Alexei Starovoitov <[email protected]>,Masahiro Yamada <[email protected]>,Markus Trippelsdorf <[email protected]>,Steven Rostedt <[email protected]>,Rik van Riel <[email protected]>,David Howells <[email protected]>,Waiman Long <[email protected]>,Kyle Huey <[email protected]>,Peter Foley <[email protected]>,Tim Chen <[email protected]>,Catalin Marinas <[email protected]>,Ard Biesheuvel <[email protected]>,Michal Hocko <[email protected]>,Matthew Wilcox <[email protected]>,Paul Bolle <[email protected]>,Rob Landley <[email protected]>,Baoquan He <[email protected]>,Daniel Micay <[email protected]>,the arch/x86 maintainers <[email protected]>,Linux Crypto Mailing List <[email protected]>,LKML <[email protected]>,xen-devel <[email protected]>,kvm list <[email protected]>,Linux PM list <[email protected]>,linux-arch <[email protected]>,Sparse Mailing-list <[email protected]>,Kernel Hardening <[email protected]>,Linus Torvalds <[email protected]>,Peter Zijlstra <[email protected]>,Borislav Petkov <[email protected]>
From: "H.J. Lu" <[email protected]>
Message-ID: <[email protected]>
Quoted 16 lines · H. Peter Anvin
Quoted 1 line · Kees Cook
On Fri, Sep 22, 2017 at 11:38 AM, H. Peter Anvin <[email protected]>
Quoted 1 line · wrote:
Quoted 1 line
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since
x86-64
Quoted 1 line
Quoted 1 line
has RIP-relative addressing there is no need for a dedicated PIC
register.
Quoted 4 lines
FWIW, since gcc 5, the PIC register isn't totally lost. It is now reusable, and that seems to have improved performance:
https://gcc.gnu.org/gcc-5/changes.htmlIt still talks about a PIC register on x86-64, which confuses me. Perhaps older gcc's would allocate a PIC register under certain circumstances, and then lose it for the entire function?
Quoted 6 lines
For i386, the PIC register is required by the ABI to be %ebx at the point any PLT entry is called. Not an issue with -mno-plt which goes straight to the GOT, although in most cases there needs to be a PIC register to find the GOT unless load-time relocation is permitted.
-hpa
We need a static PIE option so that compiler can optimize it without using hidden visibility.
Signature · 6 lines
Sent from my Android device with K-9 Mail. Please excuse my brevity. _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
H. Peter Anvin
reviewerQuoted 7 lines · Kees Cook
Quoted 2 lines · H. Peter Anvin
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
FWIW, since gcc 5, the PIC register isn't totally lost. It is now reusable, and that seems to have improved performance:
https://gcc.gnu.org/gcc-5/changes.html
It still talks about a PIC register on x86-64, which confuses me. Perhaps older gcc's would allocate a PIC register under certain circumstances, and then lose it for the entire function?
For i386, the PIC register is required by the ABI to be %ebx at the point any PLT entry is called. Not an issue with -mno-plt which goes straight to the GOT, although in most cases there needs to be a PIC register to find the GOT unless load-time relocation is permitted.
-hpa
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 2 lines · H. Peter Anvin
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
FWIW, since gcc 5, the PIC register isn't totally lost. It is now reusable, and that seems to have improved performance:
https://gcc.gnu.org/gcc-5/changes.html
-Kees
Signature · 7 lines
Kees Cook Pixel Security _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 7 lines · H. Peter Anvin
Quoted 5 lines · Ingo Molnar
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
Quoted 5 lines
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
I'm somewhat confused how we can have as much as almost 1% overhead. I suspect that we end up making a GOT and maybe even a PLT for no good reason.
We have a GOT with very few entries, mainly linker script globals that I think we can work to reduce or remove.
We have a PLT but it is empty. On latest iteration (not sent yet), modules have PLT32 relocations but no PLT entry. I got rid of mcmodel=large for modules and instead I move the beginning of the module section just after the kernel so relative relocations work.
Quoted 2 lines
-hpa
Signature · 1 line
Thomas
Quoted 7 lines · H. Peter Anvin
Quoted 5 lines · Ingo Molnar
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
Quoted 5 lines
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
I'm somewhat confused how we can have as much as almost 1% overhead. I suspect that we end up making a GOT and maybe even a PLT for no good reason.
We have a GOT with very few entries, mainly linker script globals that I think we can work to reduce or remove.
We have a PLT but it is empty. On latest iteration (not sent yet), modules have PLT32 relocations but no PLT entry. I got rid of mcmodel=large for modules and instead I move the beginning of the module section just after the kernel so relative relocations work.
Quoted 2 lines
-hpa
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Ingo Molnar
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
Indeed, but we'd use a new register _a lot_ for constructs, transforming:
mov r9,QWORD PTR [r11*8-0x7e3da060] (8 bytes)
into:
lea rbx,[rip+<off>] (7 bytes)
mov r9,QWORD PTR [rbx+r11*8] (6 bytes)
... which I suppose is quite close to (but not the same as) 'losing' RBX.
Of course the compiler can pick other registers as well, not that it matters much to register pressure in larger functions in the end. Plus if the compiler has to pick a callee-saved register there's the additional saving/restoring overhead of that as well.
Right?
Quoted 2 lines
I'm somewhat confused how we can have as much as almost 1% overhead. I suspect that we end up making a GOT and maybe even a PLT for no good reason.
So the above transformation alone would explain a good chunk of the overhead I think.
Thanks,
Ingo
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Ingo Molnar
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
Indeed, but we'd use a new register _a lot_ for constructs, transforming:
mov r9,QWORD PTR [r11*8-0x7e3da060] (8 bytes)
into:
lea rbx,[rip+<off>] (7 bytes)
mov r9,QWORD PTR [rbx+r11*8] (6 bytes)
... which I suppose is quite close to (but not the same as) 'losing' RBX.
Of course the compiler can pick other registers as well, not that it matters much to register pressure in larger functions in the end. Plus if the compiler has to pick a callee-saved register there's the additional saving/restoring overhead of that as well.
Right?
Quoted 2 lines
I'm somewhat confused how we can have as much as almost 1% overhead. I suspect that we end up making a GOT and maybe even a PLT for no good reason.
So the above transformation alone would explain a good chunk of the overhead I think.
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
H. Peter Anvin
reviewerQuoted 5 lines · Ingo Molnar
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
We lose EBX on 32 bits, but we don't lose RBX on 64 bits - since x86-64 has RIP-relative addressing there is no need for a dedicated PIC register.
I'm somewhat confused how we can have as much as almost 1% overhead. I suspect that we end up making a GOT and maybe even a PLT for no good reason.
-hpa
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 8 lines · Thomas Garnier
Quoted 7 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )* Thomas Garnier <[email protected]> wrote:
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
I am not sure what is the best way to measure that.
If this is the dominant factor then 'sizeof vmlinux' ought to be enough:
Quoted 1 line
With ORC: PIE .text is 0.814224% than baseline
I.e. the overhead is +0.81% in both size and (roughly) in number of instructions executed.
BTW., I think things improved with ORC because with ORC we have RBP as an extra register and with PIE we lose RBX - so register pressure in code generation is lower.
Ok, I suspect we can try it, but my preconditions for merging it would be:
1) Linus doesn't NAK it (obviously)
2) we first implement the additional entropy bits that Linus suggested.
does this work for you?
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 5 lines · Ingo Molnar
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )
Quoted 7 lines
Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
I am not sure what is the best way to measure that.
Quoted 4 lines
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
I used the default configuration which uses frame-pointer. I built all the different binaries with ORC and I see an improvement in size:
On latest revision (just built and ran performance tests this week):
With framepointer: PIE .text is 0.837324% than baseline
With ORC: PIE .text is 0.814224% than baseline
Comparing baselines only, ORC is -2.849832% than frame-pointers.
Quoted 16 lines
Quoted 2 lines
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 2 lines
The switches are the biggest increase on small functions but I don't think they represent a large portion of the difference (number 1 is).
Ok.
Quoted 4 lines
A side note, while testing gcc 7.2.0 on hackbench I have seen the PIE kernel being faster by 1% across multiple runs (comparing 50 runs done across 5 reboots twice). I don't think PIE is faster than a mcmodel=kernel but recent versions of gcc makes them fairly similar.
Quoted 5 lines
So I think we are down to an overhead range where the inherent noise (both random and systematic one) in 'hackbench' overwhelms the signal we are trying to measure.
So I think it's the kernel .text size change that is the best noise-free proxy for the overhead impact of PIE.
I agree but it might be hard to measure the exact impact. What is acceptable and what is not?
Quoted 4 lines
It doesn't hurt to double check actual real performance as well, just don't expect there to be much of a signal for anything but fully cached microbenchmark workloads.
That's aligned with what I see in the latest performance testing. Performance is close enough that it is hard to get exact numbers (pie is just a bit slower than baseline on hackench (~1%)).
Quoted 4 lines
Thanks,
Ingo
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
( Sorry about the delay in answering this. I could blame the delay on the merge
window, but in reality I've been procrastinating this is due to the permanent, non-trivial impact PIE has on generated C code. )
Quoted 2 lines · Thomas Garnier
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
What again is the typical frequency of this occurring in an x86-64 defconfig kernel, with the very latest GCC?
Also, to make sure: which unwinder did you use for your measurements, frame-pointers or ORC? Please use ORC only for future numbers, as frame-pointers is obsolete from a performance measurement POV.
Quoted 1 line
2) GCC does not optimize switches in PIE in order to reduce relocations:
Hopefully this can either be fixed in GCC or at least influenced via a compiler switch in the future.
Quoted 2 lines
The switches are the biggest increase on small functions but I don't think they represent a large portion of the difference (number 1 is).
Ok.
Quoted 4 lines
A side note, while testing gcc 7.2.0 on hackbench I have seen the PIE kernel being faster by 1% across multiple runs (comparing 50 runs done across 5 reboots twice). I don't think PIE is faster than a mcmodel=kernel but recent versions of gcc makes them fairly similar.
So I think we are down to an overhead range where the inherent noise (both random and systematic one) in 'hackbench' overwhelms the signal we are trying to measure.
So I think it's the kernel .text size change that is the best noise-free proxy for the overhead impact of PIE.
It doesn't hurt to double check actual real performance as well, just don't expect there to be much of a signal for anything but fully cached microbenchmark workloads.
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 11 lines · Thomas Garnier
Quoted 10 lines · Ingo Molnar
Quoted 6 lines · Thomas Garnier
With the fix for function tracing, the hackbench results have an average of +0.8 to +1.4% (from +8% to +10% before). With a default configuration, the numbers are closer to 0.8%.
On the .text size, with gcc 4.9 I see +0.8% on default configuration and +1.180% on the ubuntu configuration.
Quoted 8 lines
Quoted 2 lines
A 1% text size increase is still significant. Could you look at the disassembly, where does the size increase come from?
I will take a look, in this current iteration I added the .got and .got.plt so removing them will remove a big (even if they are small, we don't use them to increase perf).
What do you think about the perf numbers in general so far?
I looked at the size increase. I could identify two common cases:
1) PIE sometime needs two instructions to represent a single instruction on mcmodel=kernel.
For example, this instruction plays on the sign extension (mcmodel=kernel):
mov r9,QWORD PTR [r11*8-0x7e3da060] (8 bytes)
The address 0xffffffff81c25fa0 can be represented as -0x7e3da060 using a 32S relocation.
with PIE:
lea rbx,[rip+<off>] (7 bytes)
mov r9,QWORD PTR [rbx+r11*8] (6 bytes)
2) GCC does not optimize switches in PIE in order to reduce relocations:
For example the switch in phy_modes [1]:
static inline const char *phy_modes(phy_interface_t interface)
{
switch (interface) {
case PHY_INTERFACE_MODE_NA:
return "";
case PHY_INTERFACE_MODE_INTERNAL:
return "internal";
case PHY_INTERFACE_MODE_MII:
return "mii";Without PIE (gcc 7.2.0), the whole table is optimize to be one instruction:
0x000000000040045b <+27>: mov rdi,QWORD PTR [rax*8+0x400660]
With PIE (gcc 7.2.0):
0x0000000000000641 <+33>: movsxd rax,DWORD PTR [rdx+rax*4] 0x0000000000000645 <+37>: add rax,rdx 0x0000000000000648 <+40>: jmp rax .... 0x000000000000065d <+61>: lea rdi,[rip+0x264] # 0x8c8 0x0000000000000664 <+68>: jmp 0x651 <main+49> 0x0000000000000666 <+70>: lea rdi,[rip+0x2bc] # 0x929 0x000000000000066d <+77>: jmp 0x651 <main+49> 0x000000000000066f <+79>: lea rdi,[rip+0x2a8] # 0x91e 0x0000000000000676 <+86>: jmp 0x651 <main+49> 0x0000000000000678 <+88>: lea rdi,[rip+0x294] # 0x913 0x000000000000067f <+95>: jmp 0x651 <main+49>
That's a deliberate choice, clang is able to optimize it (clang-3.8):
0x0000000000000963 <+19>: lea rcx,[rip+0x200406] # 0x200d70 0x000000000000096a <+26>: mov rdi,QWORD PTR [rcx+rax*8]
I checked gcc and the code deciding to fold the switch basically do not do it for pic to reduce relocations [2].
The switches are the biggest increase on small functions but I don't think they represent a large portion of the difference (number 1 is).
A side note, while testing gcc 7.2.0 on hackbench I have seen the PIE kernel being faster by 1% across multiple runs (comparing 50 runs done across 5 reboots twice). I don't think PIE is faster than a mcmodel=kernel but recent versions of gcc makes them fairly similar.
[1] http://elixir.free-electrons.com/linux/v4.13-rc7/source/include/linux/phy.h#L113
[2] https://github.com/gcc-mirror/gcc/blob/7977b0509f07e42fbe0f06efcdead2b7e4a5135f/gcc/tree-switch-conversion.c#L828
Quoted 10 lines
Quoted 5 lines
Thanks,
Ingo
--
Thomas
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 7 lines · Ingo Molnar
Quoted 4 lines · Thomas Garnier
With the fix for function tracing, the hackbench results have an average of +0.8 to +1.4% (from +8% to +10% before). With a default configuration, the numbers are closer to 0.8%.
Quoted 5 lines
Quoted 2 lines
On the .text size, with gcc 4.9 I see +0.8% on default configuration and +1.180% on the ubuntu configuration.
A 1% text size increase is still significant. Could you look at the disassembly, where does the size increase come from?
I will take a look, in this current iteration I added the .got and .got.plt so removing them will remove a big (even if they are small, we don't use them to increase perf).
What do you think about the perf numbers in general so far?
Quoted 4 lines
Thanks,
Ingo
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 6 lines · Thomas Garnier
With the fix for function tracing, the hackbench results have an average of +0.8 to +1.4% (from +8% to +10% before). With a default configuration, the numbers are closer to 0.8%.
On the .text size, with gcc 4.9 I see +0.8% on default configuration and +1.180% on the ubuntu configuration.
A 1% text size increase is still significant. Could you look at the disassembly, where does the size increase come from?
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 30 lines · Thomas Garnier
Quoted 28 lines · Ingo Molnar
Quoted 9 lines · Thomas Garnier
Quoted 6 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
So if the numbers in your changelog and
Kconfigtext cannot be trusted, there's this description of the size impact which I suspect is less susceptible to measurement error:(quoted diff) +3
-0+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Quoted 7 lines
Quoted 3 lines
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
I will try to gather more information on the size increase. The size increase might be smaller with gcc 4.9 given performance was much better.
Coming back on this thread as I identified the root cause of the performance issue.
My original performance testing was done with an Ubuntu generic configuration. This configuration has the CONFIG_FUNCTION_TRACER option which was incompatible with PIE. The tracer failed to replace the __fentry__ call by a nop slide on each traceable function because the instruction was not the one expected. If PIE is enabled, gcc generates a difference call instruction based on the GOT without checking the visibility options (basically call *__fentry__@GOTPCREL).
With the fix for function tracing, the hackbench results have an average of +0.8 to +1.4% (from +8% to +10% before). With a default configuration, the numbers are closer to 0.8%.
On the .text size, with gcc 4.9 I see +0.8% on default configuration and +1.180% on the ubuntu configuration.
Next iteration should have an updated set of performance metrics (will try to use gcc 6.0 or higher) and incorporate the fix on function tracing.
Let me know if you have questions and feedback.
Quoted 13 lines
Quoted 7 lines
So I'm still unhappy about this all, and about the attitude surrounding it.
Thanks,
Ingo
--
Thomas
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 28 lines · Ingo Molnar
Quoted 9 lines · Thomas Garnier
Quoted 6 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
So if the numbers in your changelog and
Kconfigtext cannot be trusted, there's this description of the size impact which I suspect is less susceptible to measurement error:(quoted diff) +3
-0+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Quoted 3 lines
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
I will try to gather more information on the size increase. The size increase might be smaller with gcc 4.9 given performance was much better.
Quoted 6 lines
So I'm still unhappy about this all, and about the attitude surrounding it.
Thanks,
Ingo
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 30 lines · Ingo Molnar
Quoted 9 lines · Thomas Garnier
Quoted 6 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
So if the numbers in your changelog and
Kconfigtext cannot be trusted, there's this description of the size impact which I suspect is less susceptible to measurement error:(quoted diff) +3
-0+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
Quoted 5 lines
So I'm still unhappy about this all, and about the attitude surrounding it.
Thanks,
Ingo
Is the expectation then to have security functions also decrease size and operational latency? Seems a bit unrealistic if so. 1-2% performance hit on systems which have become at least several hundred % faster over recent years is not a significant performance regression compared to the baseline before.
While nobody is saying that performance and size concerns are irrelevant, the paradigm of leveraging single digit losses in performance metrics as a reason to leave security functions out has made Linux the equivalent of the broad side of the barn in security terms.
If it has real-world benefit to the security posture of the system, it should be a configurable option for users to decide if a percentage point or two of op time is worth the mitigation/improvement provided in security terms.
Separately, reading this thread i've noticed that people are using different compiler versions in their efforts which makes any of these sub 10% deltas moot - building a kernel with GCC4.9 vs 7.1 has more of an impact, and not having all the same compiler flags available (like the no PLT thing) flat out creates confusion. Shouldn't all of these tests be executed on a standardized build config and toolchain?
Thanks,
-Boris
Quoted 5 lines · Boris Lukashev
Is the expectation then to have security functions also decrease size and operational latency? Seems a bit unrealistic if so. 1-2% performance hit on systems which have become at least several hundred % faster over recent years is not a significant performance regression compared to the baseline before.
Where do you get these fantastic numbers? Where can I buy a system like that? Commonly we see regressions with single threaded integer performance on most newer processor generations.
These hundreds of percent improvement can only come from floating point performance using specialized instructions. There are only a very limited number of applications that can make use of it.
Quoted 10 lines · Christopher Lameter
Quoted 5 lines · Boris Lukashev
Is the expectation then to have security functions also decrease size and operational latency? Seems a bit unrealistic if so. 1-2% performance hit on systems which have become at least several hundred % faster over recent years is not a significant performance regression compared to the baseline before.
Quoted 8 lines
Where do you get these fantastic numbers? Where can I buy a system like that? Commonly we see regressions with single threaded integer performance on most newer processor generations.
These hundreds of percent improvement can only come from floating point performance using specialized instructions. There are only a very limited number of applications that can make use of it.
An example of these fantastic numbers can be seen in
https://www.cpubenchmark.net/cpu.php?cpu=Intel+Pentium+4+3.00GHz which shows a 9 year old P4 compared to today's desktop chips. Point taken on the specific types of maths being performed though. I personally can't make an educated guess at how much of the "modern" functionality compilers could leverage to optimize these code paths.
I am by no means a chip or compiler architect, but have built a fair number of systems and service oriented architectures; and in the business-end of planning and design, a deficit of security function doesn't just slow down execution by several percent - it can halt the project or scuttle it altogether as stakeholders do not wish to put their posteriors on the line.
The intent of my original email was to point out that the balance between performance and security isn't as clear-cut as "all bugs are created equal and hackbench outputs tell the whole story," since aspects of performance and security are often evaluated by separate parties in different segments of the decision making process. Providing as many avenues as possible to enhance security posture with clearly labeled performance penalties may be the difference between adoption/execution and another cycle of "wait and see." While decisions around future development agility based on changes in these optional configurations are definitely a major point to consider, the decision between a performance penalty and an exposed attack surface is likely a useful option to leave for the people building the final systems.
-Boris
Quoted 10 lines · Christopher Lameter
Quoted 5 lines · Boris Lukashev
Is the expectation then to have security functions also decrease size and operational latency? Seems a bit unrealistic if so. 1-2% performance hit on systems which have become at least several hundred % faster over recent years is not a significant performance regression compared to the baseline before.
Quoted 8 lines
Where do you get these fantastic numbers? Where can I buy a system like that? Commonly we see regressions with single threaded integer performance on most newer processor generations.
These hundreds of percent improvement can only come from floating point performance using specialized instructions. There are only a very limited number of applications that can make use of it.
An example of these fantastic numbers can be seen in
https://www.cpubenchmark.net/cpu.php?cpu=Intel+Pentium+4+3.00GHz which shows a 9 year old P4 compared to today's desktop chips. Point taken on the specific types of maths being performed though. I personally can't make an educated guess at how much of the "modern" functionality compilers could leverage to optimize these code paths.
I am by no means a chip or compiler architect, but have built a fair number of systems and service oriented architectures; and in the business-end of planning and design, a deficit of security function doesn't just slow down execution by several percent - it can halt the project or scuttle it altogether as stakeholders do not wish to put their posteriors on the line.
The intent of my original email was to point out that the balance between performance and security isn't as clear-cut as "all bugs are created equal and hackbench outputs tell the whole story," since aspects of performance and security are often evaluated by separate parties in different segments of the decision making process. Providing as many avenues as possible to enhance security posture with clearly labeled performance penalties may be the difference between adoption/execution and another cycle of "wait and see." While decisions around future development agility based on changes in these optional configurations are definitely a major point to consider, the decision between a performance penalty and an exposed attack surface is likely a useful option to leave for the people building the final systems.
-Boris
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 5 lines · Boris Lukashev
Is the expectation then to have security functions also decrease size and operational latency? Seems a bit unrealistic if so. 1-2% performance hit on systems which have become at least several hundred % faster over recent years is not a significant performance regression compared to the baseline before.
Where do you get these fantastic numbers? Where can I buy a system like that? Commonly we see regressions with single threaded integer performance on most newer processor generations.
These hundreds of percent improvement can only come from floating point performance using specialized instructions. There are only a very limited number of applications that can make use of it.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Hi!
Quoted 13 lines
Quoted 13 lines
(quoted diff) +3
-0+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
Quoted 7 lines
Quoted 1 line
So I'm still unhappy about this all, and about the attitude surrounding it.
Is the expectation then to have security functions also decrease size and operational latency? Seems a bit unrealistic if so. 1-2% performance hit on systems which have become at least several hundred % faster over recent years is not a significant performance regression compared to the baseline before.
We are probably willing to trade security for 2% performance impact... if you can show that same security advantage can't be achieved without the impact (and it is opt-in and documented and so on).
Kernel is not really a bottleneck for many people. For me, even CPUs are not bottleneck, disk is.
But what is not okay is "hey, this is security, I can slow things down. Merge it, because... security!".
Best regards,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Hi!
Quoted 13 lines
Quoted 13 lines
(quoted diff) +3
-0+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
Quoted 7 lines
Quoted 1 line
So I'm still unhappy about this all, and about the attitude surrounding it.
Is the expectation then to have security functions also decrease size and operational latency? Seems a bit unrealistic if so. 1-2% performance hit on systems which have become at least several hundred % faster over recent years is not a significant performance regression compared to the baseline before.
We are probably willing to trade security for 2% performance impact... if you can show that same security advantage can't be achieved without the impact (and it is opt-in and documented and so on).
Kernel is not really a bottleneck for many people. For me, even CPUs are not bottleneck, disk is.
But what is not okay is "hey, this is security, I can slow things down. Merge it, because... security!".
Best regards,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 30 lines · Ingo Molnar
Quoted 9 lines · Thomas Garnier
Quoted 6 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
So if the numbers in your changelog and
Kconfigtext cannot be trusted, there's this description of the size impact which I suspect is less susceptible to measurement error:(quoted diff) +3
-0+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
Quoted 5 lines
So I'm still unhappy about this all, and about the attitude surrounding it.
Thanks,
Ingo
Is the expectation then to have security functions also decrease size and operational latency? Seems a bit unrealistic if so. 1-2% performance hit on systems which have become at least several hundred % faster over recent years is not a significant performance regression compared to the baseline before.
While nobody is saying that performance and size concerns are irrelevant, the paradigm of leveraging single digit losses in performance metrics as a reason to leave security functions out has made Linux the equivalent of the broad side of the barn in security terms.
If it has real-world benefit to the security posture of the system, it should be a configurable option for users to decide if a percentage point or two of op time is worth the mitigation/improvement provided in security terms.
Separately, reading this thread i've noticed that people are using different compiler versions in their efforts which makes any of these sub 10% deltas moot - building a kernel with GCC4.9 vs 7.1 has more of an impact, and not having all the same compiler flags available (like the no PLT thing) flat out creates confusion. Shouldn't all of these tests be executed on a standardized build config and toolchain?
Thanks,
-Boris
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 9 lines · Thomas Garnier
Quoted 6 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
So if the numbers in your changelog and Kconfig text cannot be trusted, there's this description of the size impact which I suspect is less susceptible to measurement error:
+ The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
... but describing a 1-2% kernel text size increase as "slightly more assembly" shows a gratituous disregard to kernel code generation quality! In reality that's a huge size increase that in most cases will almost directly transfer to a 1-2% slowdown for kernel intense workloads.
Where does that size increase come from, if PIE is capable of using relative instructins well? Does it come from the loss of a generic register and the resulting increase in register pressure, stack spills, etc.?
So I'm still unhappy about this all, and about the attitude surrounding it.
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 63 lines · Ingo Molnar
Quoted 34 lines · Thomas Garnier
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.Quoted 5 lines
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
Note that the 8-10% hackbench or even a 2%-4% range would be 'huge' in terms of modern kernel performance. In many cases we are literally applying cycle level optimizations that are barely measurable. A 0.1% speedup in linear execution speed is already a big success.
Quoted 2 lines
I am going to start doing performance testing on -mcmodel=large to see if it is faster than -fPIE.
Unfortunately mcmodel=large looks pretty heavy too AFAICS, at the machine instruction level.
Function calls look like this:
-mcmodel=medium:
757: e8 98 ff ff ff callq 6f4 <
test_code>-mcmodel=large
Quoted 8 lines
77b: 48 b8 10 f7 df ff ff movabs $0xffffffffffdff710,%rax 782: ff ff ff 785: 48 8d 04 03 lea (%rbx,%rax,1),%rax 789: ff d0 callq *%raxAnd we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
I started looking into mcmodel=large and ran into multiple issues. In the meantime, i thought I would
try difference configurations and compilers.
I did 10 hackbench runs accross 10 reboots with and without pie (same commit) with gcc 4.9. I copied
the result below and based on the hackbench configuration we are between -0.29% and 1.92% (average
across is 0.8%) which seems more aligned with what people discussed in this thread.
I don't know how I got 10% maximum on hackbench, I am still investigating. It could be the configuration
I used or my base compiler being too old.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
I think the fundamental flaw is the assumption that we need a PIE executable to have a freely relocatable kernel on 64-bit CPUs.
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
Quoted 5 lines
Quoted 2 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I think PIE is capable to use relative instructions well. mcmodel=large assumes symbols can be anywhere.
Quoted 4 lines
I.e. is there no GCC code generation mode where code can be placed anywhere in the canonical address space, yet call and jump distance is within 31 bits so that the generated code is fast?
I think that's basically PIE. With PIE, you have the assumption everything is close, the main issue is any assembly referencing absolute addresses.
Quoted 4 lines
Thanks,
Ingo
process-pipe-1600 ------
baseline_samecommit pie % diff
0 16.985 16.999 0.082 1 17.065 17.071 0.033 2 17.188 17.130 -0.342 3 17.148 17.107 -0.240 4 17.217 17.170 -0.275 5 17.216 17.145 -0.415 6 17.161 17.109 -0.304 7 17.202 17.122 -0.465 8 17.169 17.173 0.024 9 17.217 17.178 -0.227 average 17.157 17.120 -0.213 median 17.169 17.122 -0.271 min 16.985 16.999 0.082 max 17.217 17.178 -0.228
[14 rows x 3 columns]
threads-pipe-1600 ------
baseline_samecommit pie % diff
0 17.914 18.041 0.707 1 18.337 18.352 0.083 2 18.233 18.457 1.225 3 18.334 18.402 0.366 4 18.381 18.369 -0.066 5 18.370 18.408 0.207 6 18.337 18.400 0.345 7 18.368 18.372 0.020 8 18.328 18.588 1.421 9 18.369 18.344 -0.138 average 18.297 18.373 0.415 median 18.337 18.373 0.200 min 17.914 18.041 0.707 max 18.381 18.588 1.126
[14 rows x 3 columns]
threads-pipe-50 ------
baseline_samecommit pie % diff
0 23.491 22.794 -2.965 1 23.219 23.542 1.387 2 22.886 23.638 3.286 3 23.233 23.778 2.343 4 23.228 23.703 2.046 5 23.000 23.376 1.636 6 23.589 23.335 -1.079 7 23.043 23.543 2.169 8 23.117 23.350 1.007 9 23.059 23.420 1.564 average 23.187 23.448 1.127 median 23.187 23.448 1.127 min 22.886 22.794 -0.399 max 23.589 23.778 0.800
[14 rows x 3 columns]
process-socket-50 ------
baseline_samecommit pie % diff
0 20.333 20.430 0.479 1 20.198 20.371 0.856 2 20.494 20.737 1.185 3 20.445 21.264 4.008 4 20.530 20.911 1.854 5 20.281 20.487 1.015 6 20.311 20.871 2.757 7 20.472 20.890 2.044 8 20.568 20.422 -0.710 9 20.415 20.647 1.134 average 20.405 20.703 1.462 median 20.415 20.703 1.410 min 20.198 20.371 0.856 max 20.568 21.264 3.385
[14 rows x 3 columns]
process-pipe-50 ------
baseline_samecommit pie % diff
0 20.131 20.643 2.541 1 20.184 20.658 2.349 2 20.359 20.907 2.693 3 20.365 21.284 4.514 4 20.506 20.578 0.352 5 20.393 20.599 1.010 6 20.245 20.515 1.331 7 20.627 20.964 1.636 8 20.519 20.862 1.670 9 20.505 20.741 1.150 average 20.383 20.775 1.922 median 20.383 20.741 1.753 min 20.131 20.515 1.907 max 20.627 21.284 3.186
[14 rows x 3 columns]
threads-socket-50 ------
baseline_samecommit pie % diff
0 23.197 23.728 2.286 1 23.304 23.585 1.205 2 23.098 23.379 1.217 3 23.028 23.787 3.295 4 23.242 23.122 -0.517 5 23.036 23.512 2.068 6 23.139 23.258 0.512 7 22.801 23.458 2.881 8 23.319 23.276 -0.187 9 22.989 23.577 2.557 average 23.115 23.468 1.526 median 23.115 23.468 1.526 min 22.801 23.122 1.407 max 23.319 23.787 2.006
[14 rows x 3 columns]
process-socket-1600 ------
baseline_samecommit pie % diff
0 17.214 17.168 -0.262 1 17.172 17.195 0.135 2 17.278 17.137 -0.817 3 17.173 17.102 -0.414 4 17.211 17.153 -0.335 5 17.220 17.160 -0.345 6 17.224 17.161 -0.365 7 17.224 17.224 -0.004 8 17.176 17.135 -0.236 9 17.242 17.188 -0.311 average 17.213 17.162 -0.296 median 17.214 17.161 -0.306 min 17.172 17.102 -0.405 max 17.278 17.224 -0.315
[14 rows x 3 columns]
threads-socket-1600 ------
baseline_samecommit pie % diff 0 18.395 18.389 -0.031 1 18.459 18.404 -0.296 2 18.427 18.445 0.096 3 18.449 18.421 -0.150 4 18.416 18.411 -0.026 5 18.409 18.443 0.185 6 18.325 18.308 -0.092 7 18.491 18.317 -0.940 8 18.496 18.375 -0.656 9 18.436 18.385 -0.279 average 18.430 18.390 -0.219 median 18.430 18.390 -0.219 min 18.325 18.308 -0.092 max 18.496 18.445 -0.278
[14 rows x 3 columns]
Total stats ======
baseline_samecommit pie % diff
average 19.773 19.930 0.791 median 19.773 19.930 0.791 min 16.985 16.999 0.082 max 23.589 23.787 0.839
[4 rows x 3 columns]
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Peter Zijlstra
maintainerQuoted 11 lines · Ingo Molnar
Unfortunately mcmodel=large looks pretty heavy too AFAICS, at the machine instruction level.
Function calls look like this:
-mcmodel=medium:
757: e8 98 ff ff ff callq 6f4 <
test_code>-mcmodel=large
Quoted 7 lines
77b: 48 b8 10 f7 df ff ff movabs $0xffffffffffdff710,%rax 782: ff ff ff 785: 48 8d 04 03 lea (%rbx,%rax,1),%rax 789: ff d0 callq *%raxAnd we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
So why does this need to be computed for every single call? How often will we move the kernel around at runtime?
Why can't we process the relocation at load time and then discard the relocation tables along with the rest of __init ?
Peter Zijlstra
maintainerQuoted 17 lines · Peter Zijlstra
Quoted 16 lines · Ingo Molnar
Unfortunately mcmodel=large looks pretty heavy too AFAICS, at the machine instruction level.
Function calls look like this:
-mcmodel=medium:
757: e8 98 ff ff ff callq 6f4 <
test_code>-mcmodel=large
77b: 48 b8 10 f7 df ff ff movabs $0xffffffffffdff710,%rax 782: ff ff ff 785: 48 8d 04 03 lea (%rbx,%rax,1),%rax 789: ff d0 callq *%rax
Quoted 8 lines
Quoted 2 lines
And we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
So why does this need to be computed for every single call? How often will we move the kernel around at runtime?
Why can't we process the relocation at load time and then discard the relocation tables along with the rest of
__init?
Ah, I see, this is large mode and that needs to use MOVABS to load 64bit immediates. Still, small RIP relative should be able to live at any point as long as everything lives inside the same 2G relative range, so would still allow the goal of increasing the KASLR range.
So I'm not seeing how we need large mode for that. That said, after reading up on all this, RIP relative will not be too pretty either, while CALL is naturally RIP relative, data still needs an explicit %rip offset, still loads better than the large model.
H. Peter Anvin
reviewerQuoted 11 lines · Peter Zijlstra
Ah, I see, this is large mode and that needs to use MOVABS to load 64bit immediates. Still, small RIP relative should be able to live at any point as long as everything lives inside the same 2G relative range, so would still allow the goal of increasing the KASLR range.
So I'm not seeing how we need large mode for that. That said, after reading up on all this, RIP relative will not be too pretty either, while CALL is naturally RIP relative, data still needs an explicit %rip offset, still loads better than the large model.
The large model makes no sense whatsoever. I think what we're actually looking for is the small-PIC model.
Ingo asked:
Quoted 3 lines
I.e. is there no GCC code generation mode where code can be placed anywhere in the canonical address space, yet call and jump distance is within 31 bits so that the generated code is fast?
That's the small-PIC model. I think if all symbols are forced to hidden then it won't even need a GOT/PLT.
We do need to consider how we want modules to fit into whatever model we choose, though. They can be adjacent, or we could go with a more traditional dynamic link model where the modules can be separate, and chained together with the main kernel via the GOT.
-hpa
Quoted 4 lines · H. Peter Anvin
We do need to consider how we want modules to fit into whatever model we choose, though. They can be adjacent, or we could go with a more traditional dynamic link model where the modules can be separate, and chained together with the main kernel via the GOT.
So I believe we should start with 'adjacent'. The thing is, having modules separately randomized mostly helps if any of the secret locations fails and we want to prevent hopping from one to the other. But if one the kernel-privileged secret location fails then KASLR has already failed to a significant degree...
So I think the large-PIC model for modules does not buy us any real advantages in practice, and the disadvantages of large-PIC are real and most Linux users have to pay that cost unconditionally, as distro kernels have half of their kernel functionality living in modules.
But I do see fundamental value in being able to hide the kernel somewhere in a ~48 bits address space, especially if we also implement Linus's suggestion to utilize the lower bits as well. 0..281474976710656 is a nicely large range and will get larger with time.
But it should all be done smartly and carefully:
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use mmap(MAP_FIXED) probing to figure out where the kernel lives?
Thanks,
Ingo
Hi!
Quoted 20 lines
Quoted 4 lines
We do need to consider how we want modules to fit into whatever model we choose, though. They can be adjacent, or we could go with a more traditional dynamic link model where the modules can be separate, and chained together with the main kernel via the GOT.
So I believe we should start with 'adjacent'. The thing is, having modules separately randomized mostly helps if any of the secret locations fails and we want to prevent hopping from one to the other. But if one the kernel-privileged secret location fails then KASLR has already failed to a significant degree...
So I think the large-PIC model for modules does not buy us any real advantages in practice, and the disadvantages of large-PIC are real and most Linux users have to pay that cost unconditionally, as distro kernels have half of their kernel functionality living in modules.
But I do see fundamental value in being able to hide the kernel somewhere in a ~48 bits address space, especially if we also implement Linus's suggestion to utilize the lower bits as well. 0..281474976710656 is a nicely large range and will get larger with time.
Quoted 5 lines
But it should all be done smartly and carefully:
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use
mmap(MAP_FIXED)probing to figure out where the kernel lives?
Local unpriviledged users can probably get your secret bits using cache probing and jump prediction buffers.
Yes, you don't want to leak the information using mmap(MAP_FIXED), but CPU will leak it for you, anyway.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Quoted 9 lines · Pavel Machek
Quoted 3 lines
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use
mmap(MAP_FIXED)probing to figure out where the kernel lives?Local unpriviledged users can probably get your secret bits using cache probing and jump prediction buffers.
Yes, you don't want to leak the information using
mmap(MAP_FIXED), but CPU will leak it for you, anyway.
Depends on the CPU I think, and CPU vendors are busy trying to mitigate this angle.
Thanks,
Ingo
Quoted 7 lines · Ingo Molnar
Quoted 4 lines · Pavel Machek
Quoted 3 lines
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use
mmap(MAP_FIXED)probing to figure out where the kernel lives?
Quoted 8 lines
Quoted 5 lines
Local unpriviledged users can probably get your secret bits using cache probing and jump prediction buffers.
Yes, you don't want to leak the information using
mmap(MAP_FIXED), but CPU will leak it for you, anyway.Depends on the CPU I think, and CPU vendors are busy trying to mitigate this angle.
I believe any x86 CPU running Linux will leak it. And with CPU vendors putting "artifical inteligence" into branch prediction, no, I don't think it is going to get better.
That does not mean we shoudl not prevent mmap() info leak, but...
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Quoted 14 lines · Pavel Machek
Quoted 13 lines · Ingo Molnar
Quoted 9 lines · Pavel Machek
Quoted 3 lines
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use
mmap(MAP_FIXED)probing to figure out where the kernel lives?Local unpriviledged users can probably get your secret bits using cache probing and jump prediction buffers.
Yes, you don't want to leak the information using
mmap(MAP_FIXED), but CPU will leak it for you, anyway.
Quoted 8 lines
Quoted 2 lines
Depends on the CPU I think, and CPU vendors are busy trying to mitigate this angle.
I believe any x86 CPU running Linux will leak it. And with CPU vendors putting "artifical inteligence" into branch prediction, no, I don't think it is going to get better.
That does not mean we shoudl not prevent
mmap()info leak, but...
That might or might not be so, but there's a world of a difference between running a relatively long statistical attack figuring out the kernel's location, versus being able to programmatically probe the kernel's location by using large MAP_FIXED user-space mmap()s, within a few dozen microseconds or so and a 100% guaranteed, non-statistical result.
Thanks,
Ingo
Quoted 14 lines · Pavel Machek
Quoted 13 lines · Ingo Molnar
Quoted 9 lines · Pavel Machek
Quoted 3 lines
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use
mmap(MAP_FIXED)probing to figure out where the kernel lives?Local unpriviledged users can probably get your secret bits using cache probing and jump prediction buffers.
Yes, you don't want to leak the information using
mmap(MAP_FIXED), but CPU will leak it for you, anyway.
Quoted 8 lines
Quoted 2 lines
Depends on the CPU I think, and CPU vendors are busy trying to mitigate this angle.
I believe any x86 CPU running Linux will leak it. And with CPU vendors putting "artifical inteligence" into branch prediction, no, I don't think it is going to get better.
That does not mean we shoudl not prevent
mmap()info leak, but...
That might or might not be so, but there's a world of a difference between running a relatively long statistical attack figuring out the kernel's location, versus being able to programmatically probe the kernel's location by using large MAP_FIXED user-space mmap()s, within a few dozen microseconds or so and a 100% guaranteed, non-statistical result.
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 7 lines · Ingo Molnar
Quoted 4 lines · Pavel Machek
Quoted 3 lines
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use
mmap(MAP_FIXED)probing to figure out where the kernel lives?
Quoted 8 lines
Quoted 5 lines
Local unpriviledged users can probably get your secret bits using cache probing and jump prediction buffers.
Yes, you don't want to leak the information using
mmap(MAP_FIXED), but CPU will leak it for you, anyway.Depends on the CPU I think, and CPU vendors are busy trying to mitigate this angle.
I believe any x86 CPU running Linux will leak it. And with CPU vendors putting "artifical inteligence" into branch prediction, no, I don't think it is going to get better.
That does not mean we shoudl not prevent mmap() info leak, but...
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 9 lines · Pavel Machek
Quoted 3 lines
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use
mmap(MAP_FIXED)probing to figure out where the kernel lives?Local unpriviledged users can probably get your secret bits using cache probing and jump prediction buffers.
Yes, you don't want to leak the information using
mmap(MAP_FIXED), but CPU will leak it for you, anyway.
Depends on the CPU I think, and CPU vendors are busy trying to mitigate this angle.
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Hi!
Quoted 20 lines
Quoted 4 lines
We do need to consider how we want modules to fit into whatever model we choose, though. They can be adjacent, or we could go with a more traditional dynamic link model where the modules can be separate, and chained together with the main kernel via the GOT.
So I believe we should start with 'adjacent'. The thing is, having modules separately randomized mostly helps if any of the secret locations fails and we want to prevent hopping from one to the other. But if one the kernel-privileged secret location fails then KASLR has already failed to a significant degree...
So I think the large-PIC model for modules does not buy us any real advantages in practice, and the disadvantages of large-PIC are real and most Linux users have to pay that cost unconditionally, as distro kernels have half of their kernel functionality living in modules.
But I do see fundamental value in being able to hide the kernel somewhere in a ~48 bits address space, especially if we also implement Linus's suggestion to utilize the lower bits as well. 0..281474976710656 is a nicely large range and will get larger with time.
Quoted 5 lines
But it should all be done smartly and carefully:
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use
mmap(MAP_FIXED)probing to figure out where the kernel lives?
Local unpriviledged users can probably get your secret bits using cache probing and jump prediction buffers.
Yes, you don't want to leak the information using mmap(MAP_FIXED), but CPU will leak it for you, anyway.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 4 lines · H. Peter Anvin
We do need to consider how we want modules to fit into whatever model we choose, though. They can be adjacent, or we could go with a more traditional dynamic link model where the modules can be separate, and chained together with the main kernel via the GOT.
So I believe we should start with 'adjacent'. The thing is, having modules separately randomized mostly helps if any of the secret locations fails and we want to prevent hopping from one to the other. But if one the kernel-privileged secret location fails then KASLR has already failed to a significant degree...
So I think the large-PIC model for modules does not buy us any real advantages in practice, and the disadvantages of large-PIC are real and most Linux users have to pay that cost unconditionally, as distro kernels have half of their kernel functionality living in modules.
But I do see fundamental value in being able to hide the kernel somewhere in a ~48 bits address space, especially if we also implement Linus's suggestion to utilize the lower bits as well. 0..281474976710656 is a nicely large range and will get larger with time.
But it should all be done smartly and carefully:
For example, there would be collision with regular user-space mappings, right? Can local unprivileged users use mmap(MAP_FIXED) probing to figure out where the kernel lives?
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
H. Peter Anvin
reviewerQuoted 11 lines · Peter Zijlstra
Ah, I see, this is large mode and that needs to use MOVABS to load 64bit immediates. Still, small RIP relative should be able to live at any point as long as everything lives inside the same 2G relative range, so would still allow the goal of increasing the KASLR range.
So I'm not seeing how we need large mode for that. That said, after reading up on all this, RIP relative will not be too pretty either, while CALL is naturally RIP relative, data still needs an explicit %rip offset, still loads better than the large model.
The large model makes no sense whatsoever. I think what we're actually looking for is the small-PIC model.
Ingo asked:
Quoted 3 lines
I.e. is there no GCC code generation mode where code can be placed anywhere in the canonical address space, yet call and jump distance is within 31 bits so that the generated code is fast?
That's the small-PIC model. I think if all symbols are forced to hidden then it won't even need a GOT/PLT.
We do need to consider how we want modules to fit into whatever model we choose, though. They can be adjacent, or we could go with a more traditional dynamic link model where the modules can be separate, and chained together with the main kernel via the GOT.
-hpa
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Peter Zijlstra
maintainerQuoted 17 lines · Peter Zijlstra
Quoted 16 lines · Ingo Molnar
Unfortunately mcmodel=large looks pretty heavy too AFAICS, at the machine instruction level.
Function calls look like this:
-mcmodel=medium:
757: e8 98 ff ff ff callq 6f4 <
test_code>-mcmodel=large
77b: 48 b8 10 f7 df ff ff movabs $0xffffffffffdff710,%rax 782: ff ff ff 785: 48 8d 04 03 lea (%rbx,%rax,1),%rax 789: ff d0 callq *%rax
Quoted 8 lines
Quoted 2 lines
And we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
So why does this need to be computed for every single call? How often will we move the kernel around at runtime?
Why can't we process the relocation at load time and then discard the relocation tables along with the rest of
__init?
Ah, I see, this is large mode and that needs to use MOVABS to load 64bit immediates. Still, small RIP relative should be able to live at any point as long as everything lives inside the same 2G relative range, so would still allow the goal of increasing the KASLR range.
So I'm not seeing how we need large mode for that. That said, after reading up on all this, RIP relative will not be too pretty either, while CALL is naturally RIP relative, data still needs an explicit %rip offset, still loads better than the large model.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Peter Zijlstra
maintainerQuoted 11 lines · Ingo Molnar
Unfortunately mcmodel=large looks pretty heavy too AFAICS, at the machine instruction level.
Function calls look like this:
-mcmodel=medium:
757: e8 98 ff ff ff callq 6f4 <
test_code>-mcmodel=large
Quoted 7 lines
77b: 48 b8 10 f7 df ff ff movabs $0xffffffffffdff710,%rax 782: ff ff ff 785: 48 8d 04 03 lea (%rbx,%rax,1),%rax 789: ff d0 callq *%raxAnd we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
So why does this need to be computed for every single call? How often will we move the kernel around at runtime?
Why can't we process the relocation at load time and then discard the relocation tables along with the rest of __init ?
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 34 lines · Thomas Garnier
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
Quoted 5 lines
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
Note that the 8-10% hackbench or even a 2%-4% range would be 'huge' in terms of modern kernel performance. In many cases we are literally applying cycle level optimizations that are barely measurable. A 0.1% speedup in linear execution speed is already a big success.
Quoted 2 lines
I am going to start doing performance testing on -mcmodel=large to see if it is faster than -fPIE.
Unfortunately mcmodel=large looks pretty heavy too AFAICS, at the machine instruction level.
Function calls look like this:
-mcmodel=medium:
757: e8 98 ff ff ff callq 6f4 <test_code>
-mcmodel=large
77b: 48 b8 10 f7 df ff ff movabs $0xffffffffffdff710,%rax 782: ff ff ff 785: 48 8d 04 03 lea (%rbx,%rax,1),%rax 789: ff d0 callq *%rax
And we'd do this for _EVERY_ function call in the kernel. That kind of crap is totally unacceptable.
Quoted 10 lines
Quoted 7 lines
I think the fundamental flaw is the assumption that we need a PIE executable to have a freely relocatable kernel on 64-bit CPUs.
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
How are these assumptions hardcoded by GCC? Most of the instructions should be relocatable straight away, as most call/jump/branch instructions are RIP-relative.
I.e. is there no GCC code generation mode where code can be placed anywhere in the canonical address space, yet call and jump distance is within 31 bits so that the generated code is fast?
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Peter Zijlstra
maintainerQuoted 10 lines · Thomas Garnier
Quoted 4 lines · Ingo Molnar
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
That's a compiler fail, right? Because the SDM states that for "CALL rel32" the 32bit displacement is sign extended on x86_64.
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Thomas Garnier
Quoted 4 lines · Ingo Molnar
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
Quoted 7 lines
Quoted 3 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
That's a compiler fail, right? Because the SDM states that for "CALL rel32" the 32bit displacement is sign extended on
x86_64.
That's different than what I expected at first too.
Now, I think I have an alternative of using mcmodel=large. I could use -fPIC and ensure modules are never far away from the main kernel (moving the module section start close to the random kernel end). I looked at it and that seems possible but will require more work. I plan to start with the mcmodel=large support and add this mode in a way that could benefit classic KASLR (without -fPIC) because it randomize where modules start based on the kernel.
Signature · 1 line
Thomas
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Thomas Garnier
Quoted 4 lines · Ingo Molnar
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
Quoted 7 lines
Quoted 3 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
That's a compiler fail, right? Because the SDM states that for "CALL rel32" the 32bit displacement is sign extended on
x86_64.
That's different than what I expected at first too.
Now, I think I have an alternative of using mcmodel=large. I could use -fPIC and ensure modules are never far away from the main kernel (moving the module section start close to the random kernel end). I looked at it and that seems possible but will require more work. I plan to start with the mcmodel=large support and add this mode in a way that could benefit classic KASLR (without -fPIC) because it randomize where modules start based on the kernel.
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
H. Peter Anvin
reviewerQuoted 8 lines · Peter Zijlstra
Quoted 7 lines · Thomas Garnier
Quoted 4 lines · Ingo Molnar
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
Quoted 7 lines
Quoted 3 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
That's a compiler fail, right? Because the SDM states that for "CALL rel32" the 32bit displacement is sign extended on
x86_64.
No. It is about whether you can do something like:
movl $variable, %eax /* rax = &variable; */
or
addl %ecx,variable(,%rsi,4) /* variable[rsi] += ecx */
H. Peter Anvin
reviewerQuoted 8 lines · Peter Zijlstra
Quoted 7 lines · Thomas Garnier
Quoted 4 lines · Ingo Molnar
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
Quoted 7 lines
Quoted 3 lines
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
That's a compiler fail, right? Because the SDM states that for "CALL rel32" the 32bit displacement is sign extended on
x86_64.
No. It is about whether you can do something like:
movl $variable, %eax /* rax = &variable; */
or
addl %ecx,variable(,%rsi,4) /* variable[rsi] += ecx */
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Peter Zijlstra
maintainerQuoted 10 lines · Thomas Garnier
Quoted 4 lines · Ingo Molnar
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
That's a compiler fail, right? Because the SDM states that for "CALL rel32" the 32bit displacement is sign extended on x86_64.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 33 lines · Ingo Molnar
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
(quoted diff) +18
-0+config RANDOMIZE_BASE_LARGE + bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
Quoted 3 lines
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
Note that 10% is the high-bound of a CPU intensive workload.
Quoted 8 lines
I think the fundamental flaw is the assumption that we need a PIE executable to have a freely relocatable kernel on 64-bit CPUs.
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
-model=small/medium assume you are on the low 32-bit. It generates instructions where the virtual addresses have the high 32-bit to be zero.
I am going to start doing performance testing on -mcmodel=large to see if it is faster than -fPIE.
Quoted 5 lines
It should have far less performance impact than the register-losing and overhead-inducing -fpie / -mcmodel=large (for modules) execution models.
My quick guess is tha the performance impact might be close to zero in fact.
If mcmodel=small/medium was possible for kernel, I don't think it would have less performance impact than mcmodel=large. It would still need to set the high 32-bit to be a static value, only the relocation would be a different size.
Quoted 4 lines
Thanks,
Ingo
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 8 lines · Thomas Garnier
Quoted 3 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
So I _really_ dislike the whole PIE approach, because of the huge slowdown:
+config RANDOMIZE_BASE_LARGE
+ bool "Increase the randomization range of the kernel image" + depends on X86_64 && RANDOMIZE_BASE + select X86_PIE + select X86_MODULE_PLTS if MODULES + default n + ---help--- + Build the kernel as a Position Independent Executable (PIE) and + increase the available randomization range from 1GB to 3GB. + + This option impacts performance on kernel CPU intensive workloads up + to 10% due to PIE generated code. Impact on user-mode processes and + typical usage would be significantly less (0.50% when you build the + kernel). + + The kernel and modules will generate slightly more assembly (1 to 2% + increase on the .text sections). The vmlinux binary will be + significantly smaller due to less relocations.
To put 10% kernel overhead into perspective: enabling this option wipes out about 5-10 years worth of painstaking optimizations we've done to keep the kernel fast ... (!!)
I think the fundamental flaw is the assumption that we need a PIE executable to have a freely relocatable kernel on 64-bit CPUs.
Have you considered a kernel with -mcmodel=small (or medium) instead of -fpie -mcmodel=large? We can pick a random 2GB window in the (non-kernel) canonical x86-64 address space to randomize the location of kernel text. The location of modules can be further randomized within that 2GB window.
It should have far less performance impact than the register-losing and overhead-inducing -fpie / -mcmodel=large (for modules) execution models.
My quick guess is tha the performance impact might be close to zero in fact.
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 15 lines · Ingo Molnar
Quoted 11 lines · Thomas Garnier
Changes:
- v2:
- Add support for global stack cookie while compiler default to fs without mcmodel=kernel
- Change patch 7 to correctly jump out of the identity mapping on kexec load preserve.These patches make the changes necessary to build the kernel as Position Independent Executable (PIE) on
x86_64. A PIE kernel can be relocated below the top 2G of the virtual address space. It allows to optionally extend the KASLR randomization range from 1G to 3G.
Quoted 6 lines
So this:
61 files changed, 923 insertions(+), 299 deletions(-)... is IMHO an _awful_ lot of churn and extra complexity in pretty fragile pieces of code, to gain what appears to be only ~1.5 more bits of randomization!
The range increase is a way to use PIE right away.
Quoted 4 lines
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Yes, PIE allows us to put the kernel anywhere in memory. It will allow us to have a full randomized address space where position and order of sections are completely random. There is still some work to get there but being able to build a PIE kernel is a significant step.
Quoted 4 lines
Thanks,
Ingo
Signature · 6 lines
Thomas _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 11 lines · Thomas Garnier
Changes:
- v2:
- Add support for global stack cookie while compiler default to fs without mcmodel=kernel
- Change patch 7 to correctly jump out of the identity mapping on kexec load preserve.These patches make the changes necessary to build the kernel as Position Independent Executable (PIE) on
x86_64. A PIE kernel can be relocated below the top 2G of the virtual address space. It allows to optionally extend the KASLR randomization range from 1G to 3G.
So this:
61 files changed, 923 insertions(+), 299 deletions(-)
... is IMHO an _awful_ lot of churn and extra complexity in pretty fragile pieces of code, to gain what appears to be only ~1.5 more bits of randomization!
Do these changes get us closer to being able to build the kernel as truly position independent, i.e. to place it anywhere in the valid x86-64 address space? Or any other advantages?
Thanks,
Ingo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel