Skip to contentSkip to conversation

x86: SME: Kexec/kdump memory loading fix

PATCH New Lendacky, Thomas 7 participants 32 messages Jun 19, 2019 – Aug 20, 2019 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover x86: SME: Kexec/kdump memory loading fix

This series addresses an issue related to kexec/kdump when SME is active. The SME support uses a workarea located after the end of the kernel to perform "in-place" encryption of the kernel. When kexec/kdump is used, it is possible that some other data used by kexec/kdump could be in this area of memory which would cause the kexec/kdump of the kernel to fail.

Create a workarea section for use by SME in vmlinux.lds.S that is positioned after "_end", so that the memory it occupies will be reclaimed after its use during boot. Since it is part of the kernel image, there is no worry now that kexec/kdump will place data in the SME workarea when installing the kexec/kdump kernel. As part of this fix, clarify what occupied kernel memory is reserved and what parts of the kernel memory are discarded.

The following patches are included:
- Identify and document what parts of the kernel image are reserved (saved) and what is discarded.
- Create a new section that lives outside of the kernel proper, so that it will be reclaimed as available memory during boot, thus allowing it to be used as a workarea by SME for kernel in-place encryption during early boot..

This patch series is based on tip/master.

---

Changes from v2:
- Grammar fixes
- A more generic naming of the scratch/workarea section to indicate it is not restricted to SME usage

Changes from v1:
- Clarify how and where the kernel sections/memory is reserved.
- Add an explicit symbol, __end_of_kernel_reserve, for reserving the kernel memory rather than using __bss_stop.

Tom Lendacky (2):
x86/mm: Identify the end of the kernel area to be reserved x86/mm: Create a workarea in the kernel for SME early encryption

 arch/x86/include/asm/sections.h    |  2 ++
 arch/x86/kernel/setup.c            |  8 ++++++-
 arch/x86/kernel/vmlinux.lds.S      | 34 +++++++++++++++++++++++++++++-
 arch/x86/mm/mem_encrypt_identity.c | 22 +++++++++++++++++--
 4 files changed, 62 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 1/2 x86/mm: Identify the end of the kernel area to be reserved

The memory occupied by the kernel is reserved using memblock_reserve() in setup_arch(). Currently, the area is from symbols _text to __bss_stop. Everything after __bss_stop must be specifically reserved otherwise it is discarded. This is not clearly documented.

Add a new symbol, __end_of_kernel_reserve, that more readily identifies what is reserved, along with comments that indicate what is reserved, what is discarded and what needs to be done to prevent a section from being discarded.

---
 arch/x86/include/asm/sections.h | 2 ++
 arch/x86/kernel/setup.c         | 8 +++++++-
 arch/x86/kernel/vmlinux.lds.S   | 9 ++++++++-
 3 files changed, 17 insertions(+), 2 deletions(-)
arch/x86/include/asm/sections.h +2 -0
index 8ea1cfdbeabc..71b32f2570ab 100644
@@ -13,4 +13,6 @@ extern char __end_rodata_aligned[];
 extern char __end_rodata_hpage_align[];
 #endif
 
+extern char __end_of_kernel_reserve[];
+
 #endif	/* _ASM_X86_SECTIONS_H */
arch/x86/kernel/setup.c +7 -1
index 08a5f4a131f5..fe0f6ebefeb7 100644
@@ -827,8 +827,14 @@ dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
 
 void __init setup_arch(char **cmdline_p)
 {
+	/*
+	 * Reserve the memory occupied by the kernel between _text and
+	 * __end_of_kernel_reserve symbols. Any kernel sections after the
+	 * __end_of_kernel_reserve symbol must be explicity reserved with a
+	 * separate memblock_reserve() or they will be discarded.
+	 */
 	memblock_reserve(__pa_symbol(_text),
-			 (unsigned long)__bss_stop - (unsigned long)_text);
+			 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
 
 	/*
 	 * Make sure page 0 is always reserved because on systems with
arch/x86/kernel/vmlinux.lds.S +8 -1
index 0850b5149345..ca2252ca6ad7 100644
@@ -368,6 +368,14 @@ SECTIONS
 		__bss_stop = .;
 	}
 
+	/*
+	 * The memory occupied from _text to here, __end_of_kernel_reserve, is
+	 * automatically reserved in setup_arch(). Anything after here must be
+	 * explicitly reserved using memblock_reserve() or it will be discarded
+	 * and treated as available memory.
+	 */
+	__end_of_kernel_reserve = .;
+
 	. = ALIGN(PAGE_SIZE);
 	.brk : AT(ADDR(.brk) - LOAD_OFFSET) {
 		__brk_base = .;
@@ -382,7 +390,6 @@ SECTIONS
 	STABS_DEBUG
 	DWARF_DEBUG
 
-	/* Sections to be discarded */
 	DISCARDS
 	/DISCARD/ : {
 		*(.eh_frame)
Signature · 1 line
2.17.1
#2 lore raw

[tip:x86/kdump] x86/mm: Identify the end of the kernel area to be reserved

x86/mm: Identify the end of the kernel area to be reserved
Commit-ID:  c603a309cc75f3dd018ddb20ee44c05047918cbf
Gitweb:     https://git.kernel.org/tip/c603a309cc75f3dd018ddb20ee44c05047918cbf
Author:     Thomas Lendacky <[email protected]>
AuthorDate: Wed, 19 Jun 2019 18:40:57 +0000
Committer:  Borislav Petkov <[email protected]>
CommitDate: Thu, 20 Jun 2019 09:22:47 +0200

x86/mm: Identify the end of the kernel area to be reserved

The memory occupied by the kernel is reserved using memblock_reserve() in setup_arch(). Currently, the area is from symbols _text to __bss_stop. Everything after __bss_stop must be specifically reserved otherwise it is discarded. This is not clearly documented.

Add a new symbol, __end_of_kernel_reserve, that more readily identifies what is reserved, along with comments that indicate what is reserved, what is discarded and what needs to be done to prevent a section from being discarded.

---
 arch/x86/include/asm/sections.h | 2 ++
 arch/x86/kernel/setup.c         | 8 +++++++-
 arch/x86/kernel/vmlinux.lds.S   | 9 ++++++++-
 3 files changed, 17 insertions(+), 2 deletions(-)
arch/x86/include/asm/sections.h +2 -0
index 8ea1cfdbeabc..71b32f2570ab 100644
@@ -13,4 +13,6 @@ extern char __end_rodata_aligned[];
 extern char __end_rodata_hpage_align[];
 #endif
 
+extern char __end_of_kernel_reserve[];
+
 #endif	/* _ASM_X86_SECTIONS_H */
arch/x86/kernel/setup.c +7 -1
index 08a5f4a131f5..dac60ad37e5e 100644
@@ -827,8 +827,14 @@ dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
 
 void __init setup_arch(char **cmdline_p)
 {
+	/*
+	 * Reserve the memory occupied by the kernel between _text and
+	 * __end_of_kernel_reserve symbols. Any kernel sections after the
+	 * __end_of_kernel_reserve symbol must be explicitly reserved with a
+	 * separate memblock_reserve() or they will be discarded.
+	 */
 	memblock_reserve(__pa_symbol(_text),
-			 (unsigned long)__bss_stop - (unsigned long)_text);
+			 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
 
 	/*
 	 * Make sure page 0 is always reserved because on systems with
arch/x86/kernel/vmlinux.lds.S +8 -1
index 0850b5149345..ca2252ca6ad7 100644
@@ -368,6 +368,14 @@ SECTIONS
 		__bss_stop = .;
 	}
 
+	/*
+	 * The memory occupied from _text to here, __end_of_kernel_reserve, is
+	 * automatically reserved in setup_arch(). Anything after here must be
+	 * explicitly reserved using memblock_reserve() or it will be discarded
+	 * and treated as available memory.
+	 */
+	__end_of_kernel_reserve = .;
+
 	. = ALIGN(PAGE_SIZE);
 	.brk : AT(ADDR(.brk) - LOAD_OFFSET) {
 		__brk_base = .;
@@ -382,7 +390,6 @@ SECTIONS
 	STABS_DEBUG
 	DWARF_DEBUG
 
-	/* Sections to be discarded */
 	DISCARDS
 	/DISCARD/ : {
 		*(.eh_frame)
#3 lore raw

PATCH v3 1/2 x86/mm: Identify the end of the kernel area to be reserved

In reply to Lendacky, Thomas

Hi

This appears to be causing issues with gold again:

axion /usr/src/linux # make
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CHK include/generated/compile.h
VDSOCHK arch/x86/entry/vdso/vdso64.so.dbg
VDSOCHK arch/x86/entry/vdso/vdso32.so.dbg

  CHK     kernel/kheaders_data.tar.xz
  CC      arch/x86/boot/compressed/misc.o
  RELOCS  arch/x86/boot/compressed/vmlinux.relocs

Invalid absolute R_X86_64_32S relocation: __end_of_kernel_reserve
make[2]: *** [arch/x86/boot/compressed/Makefile:130: arch/x86/boot/compressed/vmlinux.relocs] Error 1 make[2]: *** Deleting file 'arch/x86/boot/compressed/vmlinux.relocs'
make[1]: *** [arch/x86/boot/Makefile:112: arch/x86/boot/compressed/vmlinux] Error 2
make: *** [arch/x86/Makefile:316: bzImage] Error 2

Here's the version I'm running:

sys-devel/gcc-9.1.0-r1
sys-libs/glibc-2.29-r2
sys-devel/binutils-2.32-r1

Cheers

Mike

#4 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 9 lines · Mike Lothian

This appears to be causing issues with gold again:

axion /usr/src/linux # make
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CHK include/generated/compile.h
VDSOCHK arch/x86/entry/vdso/vdso64.so.dbg
VDSOCHK arch/x86/entry/vdso/vdso32.so.dbg

Quoted 8 lines
  CHK     kernel/kheaders_data.tar.xz
  CC      arch/x86/boot/compressed/misc.o
  RELOCS  arch/x86/boot/compressed/vmlinux.relocs

Invalid absolute R_X86_64_32S relocation: __end_of_kernel_reserve
make[2]: *** [arch/x86/boot/compressed/Makefile:130: arch/x86/boot/compressed/vmlinux.relocs] Error 1 make[2]: *** Deleting file 'arch/x86/boot/compressed/vmlinux.relocs'
make[1]: *** [arch/x86/boot/Makefile:112: arch/x86/boot/compressed/vmlinux] Error 2
make: *** [arch/x86/Makefile:316: bzImage] Error 2

That's fixed upstream already.

Thanks,

tglx

#5 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Will the fix be in the next RC?

Quoted 26 lines · Thomas Gleixner
Quoted 9 lines · Mike Lothian

This appears to be causing issues with gold again:

axion /usr/src/linux # make
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CHK include/generated/compile.h
VDSOCHK arch/x86/entry/vdso/vdso64.so.dbg
VDSOCHK arch/x86/entry/vdso/vdso32.so.dbg

Quoted 8 lines
  CHK     kernel/kheaders_data.tar.xz
  CC      arch/x86/boot/compressed/misc.o
  RELOCS  arch/x86/boot/compressed/vmlinux.relocs

Invalid absolute R_X86_64_32S relocation: __end_of_kernel_reserve
make[2]: *** [arch/x86/boot/compressed/Makefile:130: arch/x86/boot/compressed/vmlinux.relocs] Error 1 make[2]: *** Deleting file 'arch/x86/boot/compressed/vmlinux.relocs'
make[1]: *** [arch/x86/boot/Makefile:112: arch/x86/boot/compressed/vmlinux] Error 2
make: *** [arch/x86/Makefile:316: bzImage] Error 2

That's fixed upstream already.

Thanks,

tglx

#6 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 1 line · Mike Lothian

Will the fix be in the next RC?

As I said, it's upstream already, so yes.

A message that is not in the archive (sent off-list or not yet received): CAHbf0-F9yUDJ=DKug+MZqsjW+zPgwWaLUC40BLOsr5+t4kYOLQ@mail.gmail.com

#8 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

On Mon, 15 Jul 2019, Mike Lothian wrote:

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

A: No.
Q: Should I include quotations after my reply?

Quoted 3 lines

That build failure is from the current tip of Linus's tree

If the fix is in, then it hasn't fixed the issue

Sorry, my bad. There was a very similar issue and I confused the two:

013c66edf207 ("Revert "x86/build: Move _etext to actual end of .text"")

The reverted commit caused a build fail with gold as well. Let me stare at your issue.

Thanks,

tglx

#9 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 6 lines · Thomas Gleixner
Quoted 2 lines · Mike Lothian

That build failure is from the current tip of Linus's tree If the fix is in, then it hasn't fixed the issue

The reverted commit caused a build fail with gold as well. Let me stare at your issue.

So with gold the build fails in the reloc tool complaining about that relocation:

Invalid absolute R_X86_64_32S relocation: __end_of_kernel_reserve

The commit does:

+extern char __end_of_kernel_reserve[];
+

void __init setup_arch(char **cmdline_p)
{
+ /*
+ * Reserve the memory occupied by the kernel between _text and
+ * __end_of_kernel_reserve symbols. Any kernel sections after the
+ * __end_of_kernel_reserve symbol must be explicitly reserved with a
+ * separate memblock_reserve() or they will be discarded.
+ */
memblock_reserve(__pa_symbol(_text),
- (unsigned long)__bss_stop - (unsigned long)_text);
+ (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);

So it replaces __bss_stop with __end_of_kernel_reserve here.

arch/x86/kernel/vmlinux.lds.S +7 -0
@@ -368,6 +368,14 @@ SECTIONS
 		__bss_stop = .;
 	}
 
+	/*
+	 * The memory occupied from _text to here, __end_of_kernel_reserve, is
+	 * automatically reserved in setup_arch(). Anything after here must be
+	 * explicitly reserved using memblock_reserve() or it will be discarded
+	 * and treated as available memory.
+	 */
+	__end_of_kernel_reserve = .;
 

And from the linker script __bss_stop and __end_of_kernel_reserve are exactly the same. From System.map (of a successful ld build):

ffffffff82c00000 B __brk_base
ffffffff82c00000 B __bss_stop
ffffffff82c00000 B __end_bss_decrypted
ffffffff82c00000 B __end_of_kernel_reserve
ffffffff82c00000 B __start_bss_decrypted
ffffffff82c00000 B __start_bss_decrypted_unused

So how on earth can gold fail with that __end_of_kernel_reserve change?

For some unknown reason it turns that relocation into an absolute one. That's clearly a gold bug^Wfeature and TBH, I'm more than concerned about that kind of behaviour.

If we just revert that commit, then what do we achieve? We paper over the underlying problem, which is not really helping anything.

Aside of that gold still fails to build the X32 VDSO and it does so for a very long time....

Until we really understand what the problem is, this stays as is.

@H.J.: Any insight on that?

Thanks,

tglx

#10 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 66 lines · Thomas Gleixner
Quoted 6 lines · Thomas Gleixner
Quoted 2 lines · Mike Lothian

That build failure is from the current tip of Linus's tree If the fix is in, then it hasn't fixed the issue

The reverted commit caused a build fail with gold as well. Let me stare at your issue.

So with gold the build fails in the reloc tool complaining about that relocation:

Invalid absolute R_X86_64_32S relocation: __end_of_kernel_reserve

The commit does:

(quoted diff) +9 -1
+extern char __end_of_kernel_reserve[];
+
 
 void __init setup_arch(char **cmdline_p)
 {
+       /*
+        * Reserve the memory occupied by the kernel between _text and
+        * __end_of_kernel_reserve symbols. Any kernel sections after the
+        * __end_of_kernel_reserve symbol must be explicitly reserved with a
+        * separate memblock_reserve() or they will be discarded.
+        */
        memblock_reserve(__pa_symbol(_text),
-                        (unsigned long)__bss_stop - (unsigned long)_text);
+                        (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);

So it replaces __bss_stop with __end_of_kernel_reserve here.

arch/x86/kernel/vmlinux.lds.S +7 -0
@@ -368,6 +368,14 @@ SECTIONS
                __bss_stop = .;
        }
 
+       /*
+        * The memory occupied from _text to here, __end_of_kernel_reserve, is
+        * automatically reserved in setup_arch(). Anything after here must be
+        * explicitly reserved using memblock_reserve() or it will be discarded
+        * and treated as available memory.
+        */
+       __end_of_kernel_reserve = .;
 

And from the linker script __bss_stop and __end_of_kernel_reserve are exactly the same. From System.map (of a successful ld build):

ffffffff82c00000 B __brk_base
ffffffff82c00000 B __bss_stop
ffffffff82c00000 B __end_bss_decrypted
ffffffff82c00000 B __end_of_kernel_reserve
ffffffff82c00000 B __start_bss_decrypted
ffffffff82c00000 B __start_bss_decrypted_unused

So how on earth can gold fail with that __end_of_kernel_reserve change?

For some unknown reason it turns that relocation into an absolute one. That's clearly a gold bug^Wfeature and TBH, I'm more than concerned about that kind of behaviour.

If we just revert that commit, then what do we achieve? We paper over the underlying problem, which is not really helping anything.

Quoted 7 lines

Aside of that gold still fails to build the X32 VDSO and it does so for a very long time....

Until we really understand what the problem is, this stays as is.

@H.J.: Any insight on that?

Since building a workable kernel for different kernel configurations isn't a requirement for gold, I don't recommend gold for kernel.

Signature · 1 line
H.J.
#11 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 70 lines · H.J. Lu
Quoted 69 lines · Thomas Gleixner
Quoted 6 lines · Thomas Gleixner
Quoted 2 lines · Mike Lothian

That build failure is from the current tip of Linus's tree If the fix is in, then it hasn't fixed the issue

The reverted commit caused a build fail with gold as well. Let me stare at your issue.

So with gold the build fails in the reloc tool complaining about that relocation:

Invalid absolute R_X86_64_32S relocation: __end_of_kernel_reserve

The commit does:

(quoted diff) +9 -1
+extern char __end_of_kernel_reserve[];
+
 
 void __init setup_arch(char **cmdline_p)
 {
+       /*
+        * Reserve the memory occupied by the kernel between _text and
+        * __end_of_kernel_reserve symbols. Any kernel sections after the
+        * __end_of_kernel_reserve symbol must be explicitly reserved with a
+        * separate memblock_reserve() or they will be discarded.
+        */
        memblock_reserve(__pa_symbol(_text),
-                        (unsigned long)__bss_stop - (unsigned long)_text);
+                        (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);

So it replaces __bss_stop with __end_of_kernel_reserve here.

arch/x86/kernel/vmlinux.lds.S +7 -0
@@ -368,6 +368,14 @@ SECTIONS
                __bss_stop = .;
        }
 
+       /*
+        * The memory occupied from _text to here, __end_of_kernel_reserve, is
+        * automatically reserved in setup_arch(). Anything after here must be
+        * explicitly reserved using memblock_reserve() or it will be discarded
+        * and treated as available memory.
+        */
+       __end_of_kernel_reserve = .;
 

And from the linker script __bss_stop and __end_of_kernel_reserve are exactly the same. From System.map (of a successful ld build):

ffffffff82c00000 B __brk_base
ffffffff82c00000 B __bss_stop
ffffffff82c00000 B __end_bss_decrypted
ffffffff82c00000 B __end_of_kernel_reserve
ffffffff82c00000 B __start_bss_decrypted
ffffffff82c00000 B __start_bss_decrypted_unused

So how on earth can gold fail with that __end_of_kernel_reserve change?

For some unknown reason it turns that relocation into an absolute one. That's clearly a gold bug^Wfeature and TBH, I'm more than concerned about that kind of behaviour.

If we just revert that commit, then what do we achieve? We paper over the underlying problem, which is not really helping anything.

Aside of that gold still fails to build the X32 VDSO and it does so for a very long time....

Quoted 7 lines
Quoted 4 lines

Until we really understand what the problem is, this stays as is.

@H.J.: Any insight on that?

Since building a workable kernel for different kernel configurations isn't a requirement for gold, I don't recommend gold for kernel.

Um, it worked before this commit, and now it doesn't. "Some" companies are using gold for linking the kernel today...

thanks,

greg k-h

#12 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 8 lines · Greg KH
Quoted 4 lines · H.J. Lu
Quoted 1 line

Since building a workable kernel for different kernel configurations isn't a requirement for gold, I don't recommend gold for kernel.

Um, it worked before this commit, and now it doesn't. "Some" companies are using gold for linking the kernel today...

gold is known to fail the kernel build. x32 vdso linking is not working since years and just because it 'works' for some configurations and breaks for no valid reasons even with those configurations is just not good enough.

As there is obviously no priority for fixing gold to work proper with the kernel, I'm not at all interested in these 'regression' reports and in odd 'fixes' which just end up reverting or modifying perfectly valid changes without understanding the root cause, i.e. the most horrible engineering
principle: duct-taping.

TBH, I'm tired of it. We fail the build for clang if it does not support asm gotos and the clang people are actively working on fixing it and we're helping them as much as we can. The companies who used clang nevertheless have been on their own for years and if someone wants to use gold then nobody is preventing them from doing so. They can keep their duct-tape in their own trees.

See this thread for further discussion:

https://lkml.kernel.org/r/[email protected]

Thanks,

tglx

#13 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 28 lines · Thomas Gleixner
Quoted 8 lines · Greg KH
Quoted 4 lines · H.J. Lu
Quoted 1 line

Since building a workable kernel for different kernel configurations isn't a requirement for gold, I don't recommend gold for kernel.

Um, it worked before this commit, and now it doesn't. "Some" companies are using gold for linking the kernel today...

gold is known to fail the kernel build. x32 vdso linking is not working since years and just because it 'works' for some configurations and breaks for no valid reasons even with those configurations is just not good enough.

As there is obviously no priority for fixing gold to work proper with the kernel, I'm not at all interested in these 'regression' reports and in odd 'fixes' which just end up reverting or modifying perfectly valid changes without understanding the root cause, i.e. the most horrible engineering
principle: duct-taping.

TBH, I'm tired of it. We fail the build for clang if it does not support asm gotos and the clang people are actively working on fixing it and we're helping them as much as we can. The companies who used clang nevertheless have been on their own for years and if someone wants to use gold then nobody is preventing them from doing so. They can keep their duct-tape in their own trees.

Quoted 3 lines

See this thread for further discussion:

https://lkml.kernel.org/r/[email protected]

Sorry, I saw that after writing that. You are right, if the others don't object, that's fine with me. I'll go poke the various build systems that are failing right now on 5.3-rc1 and try to get them fixed for this reason.

thanks,

greg k-h

#14 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 29 lines · Greg KH
Quoted 28 lines · Thomas Gleixner
Quoted 8 lines · Greg KH
Quoted 4 lines · H.J. Lu
Quoted 1 line

Since building a workable kernel for different kernel configurations isn't a requirement for gold, I don't recommend gold for kernel.

Um, it worked before this commit, and now it doesn't. "Some" companies are using gold for linking the kernel today...

gold is known to fail the kernel build. x32 vdso linking is not working since years and just because it 'works' for some configurations and breaks for no valid reasons even with those configurations is just not good enough.

As there is obviously no priority for fixing gold to work proper with the kernel, I'm not at all interested in these 'regression' reports and in odd 'fixes' which just end up reverting or modifying perfectly valid changes without understanding the root cause, i.e. the most horrible engineering
principle: duct-taping.

TBH, I'm tired of it. We fail the build for clang if it does not support asm gotos and the clang people are actively working on fixing it and we're helping them as much as we can. The companies who used clang nevertheless have been on their own for years and if someone wants to use gold then nobody is preventing them from doing so. They can keep their duct-tape in their own trees.

Quoted 8 lines
Quoted 3 lines

See this thread for further discussion:

https://lkml.kernel.org/r/[email protected]

Sorry, I saw that after writing that. You are right, if the others don't object, that's fine with me. I'll go poke the various build systems that are failing right now on 5.3-rc1 and try to get them fixed for this reason.

Ok, I dug around and the gold linker is not being used here, only clang to build the source and GNU ld to link, and I am still seeing this error.

Hm, clang 8 does not cause this error, but clang 9 does. Let me go poke the people who are providing this version of clang to see if there's something they can figure out.

greg k-h

#15 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 9 lines · Greg KH
Quoted 4 lines · Greg KH

Sorry, I saw that after writing that. You are right, if the others don't object, that's fine with me. I'll go poke the various build systems that are failing right now on 5.3-rc1 and try to get them fixed for this reason.

Ok, I dug around and the gold linker is not being used here, only clang to build the source and GNU ld to link, and I am still seeing this error.

Odd combo.

Quoted 3 lines

Hm, clang 8 does not cause this error, but clang 9 does. Let me go poke the people who are providing this version of clang to see if there's something they can figure out.

Let me try that with my clang variant. Which version of GNU ld are you using?

Thanks,

tglx

#16 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 7 lines · Thomas Gleixner
Quoted 6 lines · Greg KH
Quoted 4 lines · Greg KH

Sorry, I saw that after writing that. You are right, if the others don't object, that's fine with me. I'll go poke the various build systems that are failing right now on 5.3-rc1 and try to get them fixed for this reason.

Quoted 5 lines
Quoted 3 lines

Ok, I dug around and the gold linker is not being used here, only clang to build the source and GNU ld to link, and I am still seeing this error.

Odd combo.

I'm not disagreeing :)

Wait, does clang link things itself and not need ld?

Quoted 6 lines
Quoted 3 lines

Hm, clang 8 does not cause this error, but clang 9 does. Let me go poke the people who are providing this version of clang to see if there's something they can figure out.

Let me try that with my clang variant. Which version of GNU ld are you using?

I think it is 2.27:

$ ./ld --version
GNU ld (binutils-2.27-44492f8) 2.27.0.20170315

Which does feel old to me.

I know 2.32 works fine.

Gotta love old tool-chains :(

greg k-h

#17 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 10 lines · Greg KH
Quoted 5 lines · Thomas Gleixner
Quoted 3 lines

Ok, I dug around and the gold linker is not being used here, only clang to build the source and GNU ld to link, and I am still seeing this error.

Odd combo.

I'm not disagreeing :)

Wait, does clang link things itself and not need ld?

Nah.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Hm, clang 8 does not cause this error, but clang 9 does. Let me go poke the people who are providing this version of clang to see if there's something they can figure out.

Let me try that with my clang variant. Which version of GNU ld are you using?

Quoted 7 lines

I think it is 2.27:

$ ./ld --version
GNU ld (binutils-2.27-44492f8) 2.27.0.20170315

Which does feel old to me.

I know 2.32 works fine.

2.31 works fine as well.

Quoted 1 line

Gotta love old tool-chains :(

Oh yes. /me does archaeology to find a VM with old stuff

#18 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 27 lines · Thomas Gleixner
Quoted 10 lines · Greg KH
Quoted 5 lines · Thomas Gleixner
Quoted 3 lines

Ok, I dug around and the gold linker is not being used here, only clang to build the source and GNU ld to link, and I am still seeing this error.

Odd combo.

I'm not disagreeing :)

Wait, does clang link things itself and not need ld?

Nah.

Quoted 13 lines
Quoted 6 lines
Quoted 3 lines

Hm, clang 8 does not cause this error, but clang 9 does. Let me go poke the people who are providing this version of clang to see if there's something they can figure out.

Let me try that with my clang variant. Which version of GNU ld are you using?

I think it is 2.27:

$ ./ld --version
GNU ld (binutils-2.27-44492f8) 2.27.0.20170315

Which does feel old to me.

Quoted 7 lines
Quoted 1 line

I know 2.32 works fine.

2.31 works fine as well.

Quoted 1 line

Gotta love old tool-chains :(

Oh yes. /me does archaeology to find a VM with old stuff

I can provide a binary if you can't find anything.

thanks,

greg k-h

#19 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 6 lines · Greg KH
Quoted 3 lines · Thomas Gleixner
Quoted 1 line

Gotta love old tool-chains :(

Oh yes. /me does archaeology to find a VM with old stuff

I can provide a binary if you can't find anything.

Found GNU ld (GNU Binutils for Debian) 2.25 and after fiddling with LD_PRELOAD it builds without failure.

ld.gold from that binutils version dies with a segfault on various files ...

Thanks,

tglx

#20 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 4 lines · Thomas Gleixner
Quoted 3 lines · Greg KH
Quoted 2 lines · Thomas Gleixner
Quoted 1 line

Gotta love old tool-chains :(

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Oh yes. /me does archaeology to find a VM with old stuff

I can provide a binary if you can't find anything.

Found GNU ld (GNU Binutils for Debian) 2.25 and after fiddling with LD_PRELOAD it builds without failure.

ld.gold from that binutils version dies with a segfault on various files ...

Then tried that old ld.bfd with GCC8 and that causes ld.bfd to segfault on every other file.

Copied that config to the clang build directory and it causes the same explosions with ld.bfd.

What a time waste...

#21 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 15 lines · Thomas Gleixner
Quoted 4 lines · Thomas Gleixner
Quoted 3 lines · Greg KH
Quoted 2 lines · Thomas Gleixner
Quoted 1 line

Gotta love old tool-chains :(

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Oh yes. /me does archaeology to find a VM with old stuff

I can provide a binary if you can't find anything.

Found GNU ld (GNU Binutils for Debian) 2.25 and after fiddling with LD_PRELOAD it builds without failure.

ld.gold from that binutils version dies with a segfault on various files ...

Quoted 10 lines

Then tried that old ld.bfd with GCC8 and that causes ld.bfd to segfault on every other file.

Copied that config to the clang build directory and it causes the same explosions with ld.bfd.

What a time waste...

Ugh, sorry about this. I can't seem to track it down either, and at this point am just going to punt and let the Android build people try to figure it out as it is their custom build system that is failing at the moment, only for x86, and if this single patch is reverted, it starts working again.

voodo...

thanks,

greg k-h

#22 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 34 lines · Greg KH
Quoted 15 lines · Thomas Gleixner
Quoted 4 lines · Thomas Gleixner
Quoted 3 lines · Greg KH
Quoted 2 lines · Thomas Gleixner
Quoted 1 line

Gotta love old tool-chains :(

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Oh yes. /me does archaeology to find a VM with old stuff

I can provide a binary if you can't find anything.

Found GNU ld (GNU Binutils for Debian) 2.25 and after fiddling with LD_PRELOAD it builds without failure.

ld.gold from that binutils version dies with a segfault on various files ...

Quoted 10 lines

Then tried that old ld.bfd with GCC8 and that causes ld.bfd to segfault on every other file.

Copied that config to the clang build directory and it causes the same explosions with ld.bfd.

What a time waste...

Ugh, sorry about this. I can't seem to track it down either, and at this point am just going to punt and let the Android build people try to figure it out as it is their custom build system that is failing at the moment, only for x86, and if this single patch is reverted, it starts working again.

Quoted 5 lines

voodo...

thanks,

greg k-h

As it's related. I've raised
https://bugzilla.kernel.org/show_bug.cgi?id=204495 about the relocatition I'm seeing since switching back to ld.bfd - is this safe to ignore? I'm guessing this is why gold isn't working as it can't do them

Cheers

Mike

#23 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 10 lines · Mike Lothian

As it's related. I've raised
https://bugzilla.kernel.org/show_bug.cgi?id=204495 about the relocatition I'm seeing since switching back to ld.bfd - is this safe to ignore? I'm guessing this is why gold isn't working as it can't do them

Cheers

Mike

Sorry am I asking in the wrong place about this?

#24 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 9 lines · Mike Lothian
Quoted 6 lines · Mike Lothian

As it's related. I've raised
https://bugzilla.kernel.org/show_bug.cgi?id=204495 about the relocatition I'm seeing since switching back to ld.bfd - is this safe to ignore? I'm guessing this is why gold isn't working as it can't do them

Sorry am I asking in the wrong place about this?

No, but I haven't come around to look at it. As your mail above is not giving any useful information and requires to fire up a browser it got on the back burner.

That bugzilla entry does not tell anything about the compiler involved, the binutils version and the .config file. I'm really bad at guesswork and my crystal ball got lost years ago :)

Care to give that information here by mail?

Thanks,

tglx

#25 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

Quoted 20 lines · Thomas Gleixner
Quoted 9 lines · Mike Lothian
Quoted 6 lines · Mike Lothian

As it's related. I've raised
https://bugzilla.kernel.org/show_bug.cgi?id=204495 about the relocatition I'm seeing since switching back to ld.bfd - is this safe to ignore? I'm guessing this is why gold isn't working as it can't do them

Sorry am I asking in the wrong place about this?

No, but I haven't come around to look at it. As your mail above is not giving any useful information and requires to fire up a browser it got on the back burner.

That bugzilla entry does not tell anything about the compiler involved, the binutils version and the .config file. I'm really bad at guesswork and my crystal ball got lost years ago :)

Quoted 5 lines

Care to give that information here by mail?

Thanks,

tglx

Hi

I'm using GCC 9.2, binutils 2.32. I used to use the gold linker but I've just switched back to using ld.bfd based on this discussion

My .config can be found at:
https://raw.githubusercontent.com/FireBurn/KernelStuff/master/dot_config_tip

Cheers

Mike

#27 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

I've updated the bug some more with more info

I can't replicate this on RHEL7 with GCC 4.8.5 or GCC 7.3.1

My GCC is as follows:

Using built-in specs.
COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/9.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with:
/var/tmp/portage/sys-devel/gcc-9.2.0/work/gcc-9.2.0/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/9.2.0 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/g++-v9 --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/python --enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --enable-checking=release --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 9.2.0 p1' --disable-esp --enable-libstdcxx-time --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-altivec --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libmudflap --disable-libssp --disable-systemtap --enable-vtable-verify --enable-lto --with-isl --disable-isl-version-check --enable-default-pie --enable-default-ssp Thread model: posix
gcc version 9.2.0 (Gentoo 9.2.0 p1)

Could this be related to the --enable-default-ssp or --enable-default-pie?

Quoted 14 lines · Thomas Gleixner
Quoted 6 lines · Mike Lothian

I'm using GCC 9.2, binutils 2.32. I used to use the gold linker but I've just switched back to using ld.bfd based on this discussion

My .config can be found at:
https://raw.githubusercontent.com/FireBurn/KernelStuff/master/dot_config_tip

Does this combo successfully build 5.2 or is this a general problem?

Thanks,

tglx

maintainer
#28 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

In reply to Greg KH
Quoted 1 line · Greg KH

Wait, does clang link things itself and not need ld?

There's ld.lld; which might be used by your clang.

#29 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

In reply to Thomas Gleixner
Quoted 7 lines · Thomas Gleixner
Quoted 6 lines · Greg KH
Quoted 4 lines · Greg KH

Sorry, I saw that after writing that. You are right, if the others don't object, that's fine with me. I'll go poke the various build systems that are failing right now on 5.3-rc1 and try to get them fixed for this reason.

Quoted 5 lines
Quoted 3 lines

Ok, I dug around and the gold linker is not being used here, only clang to build the source and GNU ld to link, and I am still seeing this error.

Odd combo.

Ha. Not really. I did not switch ld with LD=... so it's using the default GNU ld.

Quoted 6 lines
Quoted 3 lines

Hm, clang 8 does not cause this error, but clang 9 does. Let me go poke the people who are providing this version of clang to see if there's something they can figure out.

Let me try that with my clang variant. Which version of GNU ld are you using?

Works with a very recent build of the clang-9 branch and ld version 2.31.1

Thanks,

tglx

#30 lore raw

Re: [PATCH v3 1/2] x86/mm: Identify the end of the kernel area to be reserved

In reply to Greg KH
Quoted 73 lines · Greg KH
Quoted 72 lines · H.J. Lu
Quoted 71 lines · Thomas Gleixner
Quoted 6 lines · Thomas Gleixner
Quoted 2 lines · Mike Lothian

That build failure is from the current tip of Linus's tree If the fix is in, then it hasn't fixed the issue

The reverted commit caused a build fail with gold as well. Let me stare at your issue.

So with gold the build fails in the reloc tool complaining about that relocation:

Invalid absolute R_X86_64_32S relocation: __end_of_kernel_reserve

The commit does:

(quoted diff) +9 -1
+extern char __end_of_kernel_reserve[];
+
 
 void __init setup_arch(char **cmdline_p)
 {
+       /*
+        * Reserve the memory occupied by the kernel between _text and
+        * __end_of_kernel_reserve symbols. Any kernel sections after the
+        * __end_of_kernel_reserve symbol must be explicitly reserved with a
+        * separate memblock_reserve() or they will be discarded.
+        */
        memblock_reserve(__pa_symbol(_text),
-                        (unsigned long)__bss_stop - (unsigned long)_text);
+                        (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);

So it replaces __bss_stop with __end_of_kernel_reserve here.

arch/x86/kernel/vmlinux.lds.S +7 -0
@@ -368,6 +368,14 @@ SECTIONS
                __bss_stop = .;
        }
 
+       /*
+        * The memory occupied from _text to here, __end_of_kernel_reserve, is
+        * automatically reserved in setup_arch(). Anything after here must be
+        * explicitly reserved using memblock_reserve() or it will be discarded
+        * and treated as available memory.
+        */
+       __end_of_kernel_reserve = .;
 

And from the linker script __bss_stop and __end_of_kernel_reserve are exactly the same. From System.map (of a successful ld build):

ffffffff82c00000 B __brk_base
ffffffff82c00000 B __bss_stop
ffffffff82c00000 B __end_bss_decrypted
ffffffff82c00000 B __end_of_kernel_reserve
ffffffff82c00000 B __start_bss_decrypted
ffffffff82c00000 B __start_bss_decrypted_unused

So how on earth can gold fail with that __end_of_kernel_reserve change?

For some unknown reason it turns that relocation into an absolute one. That's clearly a gold bug^Wfeature and TBH, I'm more than concerned about that kind of behaviour.

If we just revert that commit, then what do we achieve? We paper over the underlying problem, which is not really helping anything.

Aside of that gold still fails to build the X32 VDSO and it does so for a very long time....

Until we really understand what the problem is, this stays as is.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

@H.J.: Any insight on that?

Since building a workable kernel for different kernel configurations isn't a requirement for gold, I don't recommend gold for kernel.

Um, it worked before this commit, and now it doesn't. "Some" companies are using gold for linking the kernel today...

Oh nevermind, I see the rest of the thread where it's now not being allowed, sorry for the noise.

greg k-h

#31 lore raw

PATCH v3 2/2 x86/mm: Create a workarea in the kernel for SME early encryption

In reply to Lendacky, Thomas

In order for the kernel to be encrypted "in place" during boot, a workarea outside of the kernel must be used. This SME workarea used during early encryption of the kernel is situated on a 2MB boundary after the end of the kernel text, data, etc. sections (_end). This works well during initial boot of a compressed kernel because of the relocation used for decompression of the kernel. But when performing a kexec boot, there's a chance that the SME workarea may not be mapped by the kexec pagetables or that some of the other data used by kexec could exist in this range.

Create a section for SME in vmlinux.lds.S. Position it after "_end", which is after "__end_of_kernel_reserve", so that the memory will be reclaimed during boot and since this area is all zeroes, it compresses well. This new section will be part of the kernel image, so kexec will account for it in pagetable mappings and placement of data after the kernel.

Here's an example of a kernel size without and with the SME section:

	without:
		vmlinux:	36,501,616
		bzImage:	 6,497,344

		100000000-47f37ffff : System RAM
		  1e4000000-1e47677d4 : Kernel code	(0x7677d4)
		  1e47677d5-1e4e2e0bf : Kernel data	(0x6c68ea)
		  1e5074000-1e5372fff : Kernel bss	(0x2fefff)

	with:
		vmlinux:	44,419,408
		bzImage:	 6,503,136

		880000000-c7ff7ffff : System RAM
		  8cf000000-8cf7677d4 : Kernel code	(0x7677d4)
		  8cf7677d5-8cfe2e0bf : Kernel data	(0x6c68ea)
		  8d0074000-8d0372fff : Kernel bss	(0x2fefff)
---
 arch/x86/kernel/vmlinux.lds.S      | 25 +++++++++++++++++++++++++
 arch/x86/mm/mem_encrypt_identity.c | 22 ++++++++++++++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)
arch/x86/kernel/vmlinux.lds.S +25 -0
index ca2252ca6ad7..147cd020516a 100644
@@ -387,6 +387,31 @@ SECTIONS
 	. = ALIGN(PAGE_SIZE);		/* keep VO_INIT_SIZE page aligned */
 	_end = .;
 
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	/*
+	 * Early scratch/workarea section: Lives outside of the kernel proper
+	 * (_text - _end).
+	 *
+	 * Resides after _end because even though the .brk section is after
+	 * __end_of_kernel_reserve, the .brk section is later reserved as a
+	 * part of the kernel. Since it is located after __end_of_kernel_reserve
+	 * it will be discarded and become part of the available memory. As
+	 * such, it can only be used by very early boot code and must not be
+	 * needed afterwards.
+	 *
+	 * Currently used by SME for performing in-place encryption of the
+	 * kernel during boot. Resides on a 2MB boundary to simplify the
+	 * pagetable setup used for SME in-place encryption.
+	 */
+	. = ALIGN(HPAGE_SIZE);
+	.init.scratch : AT(ADDR(.init.scratch) - LOAD_OFFSET) {
+		__init_scratch_begin = .;
+		*(.init.scratch)
+		. = ALIGN(HPAGE_SIZE);
+		__init_scratch_end = .;
+	}
+#endif
+
 	STABS_DEBUG
 	DWARF_DEBUG
 
arch/x86/mm/mem_encrypt_identity.c +20 -2
index 4aa9b1480866..6a8dd483f7d9 100644
@@ -73,6 +73,19 @@ struct sme_populate_pgd_data {
 	unsigned long vaddr_end;
 };
 
+/*
+ * This work area lives in the .init.scratch section, which lives outside of
+ * the kernel proper. It is sized to hold the intermediate copy buffer and
+ * more than enough pagetable pages.
+ *
+ * By using this section, the kernel can be encrypted in place and it
+ * avoids any possibility of boot parameters or initramfs images being
+ * placed such that the in-place encryption logic overwrites them.  This
+ * section is 2MB aligned to allow for simple pagetable setup using only
+ * PMD entries (see vmlinux.lds.S).
+ */
+static char sme_workarea[2 * PMD_PAGE_SIZE] __section(.init.scratch);
+
 static char sme_cmdline_arg[] __initdata = "mem_encrypt";
 static char sme_cmdline_on[]  __initdata = "on";
 static char sme_cmdline_off[] __initdata = "off";
@@ -314,8 +327,13 @@ void __init sme_encrypt_kernel(struct boot_params *bp)
 	}
 #endif
 
-	/* Set the encryption workarea to be immediately after the kernel */
-	workarea_start = kernel_end;
+	/*
+	 * We're running identity mapped, so we must obtain the address to the
+	 * SME encryption workarea using rip-relative addressing.
+	 */
+	asm ("lea sme_workarea(%%rip), %0"
+	     : "=r" (workarea_start)
+	     : "p" (sme_workarea));
 
 	/*
 	 * Calculate required number of workarea bytes needed:
Signature · 1 line
2.17.1
#32 lore raw

[tip:x86/kdump] x86/mm: Create a workarea in the kernel for SME early encryption

x86/mm: Create a workarea in the kernel for SME early encryption
Commit-ID:  e1bfa87399e372446454ecbaeba2800f0a385733
Gitweb:     https://git.kernel.org/tip/e1bfa87399e372446454ecbaeba2800f0a385733
Author:     Thomas Lendacky <[email protected]>
AuthorDate: Wed, 19 Jun 2019 18:40:59 +0000
Committer:  Borislav Petkov <[email protected]>
CommitDate: Thu, 20 Jun 2019 09:44:26 +0200

x86/mm: Create a workarea in the kernel for SME early encryption

In order for the kernel to be encrypted "in place" during boot, a workarea outside of the kernel must be used. This SME workarea used during early encryption of the kernel is situated on a 2MB boundary after the end of the kernel text, data, etc. sections (_end).

This works well during initial boot of a compressed kernel because of the relocation used for decompression of the kernel. But when performing a kexec boot, there's a chance that the SME workarea may not be mapped by the kexec pagetables or that some of the other data used by kexec could exist in this range.

Create a section for SME in vmlinux.lds.S. Position it after "_end", which is after "__end_of_kernel_reserve", so that the memory will be reclaimed during boot and since this area is all zeroes, it compresses well. This new section will be part of the kernel image, so kexec will account for it in pagetable mappings and placement of data after the kernel.

Here's an example of a kernel size without and with the SME section:

	without:
		vmlinux:	36,501,616
		bzImage:	 6,497,344

		100000000-47f37ffff : System RAM
		  1e4000000-1e47677d4 : Kernel code	(0x7677d4)
		  1e47677d5-1e4e2e0bf : Kernel data	(0x6c68ea)
		  1e5074000-1e5372fff : Kernel bss	(0x2fefff)

	with:
		vmlinux:	44,419,408
		bzImage:	 6,503,136

		880000000-c7ff7ffff : System RAM
		  8cf000000-8cf7677d4 : Kernel code	(0x7677d4)
		  8cf7677d5-8cfe2e0bf : Kernel data	(0x6c68ea)
		  8d0074000-8d0372fff : Kernel bss	(0x2fefff)
---
 arch/x86/kernel/vmlinux.lds.S      | 25 +++++++++++++++++++++++++
 arch/x86/mm/mem_encrypt_identity.c | 22 ++++++++++++++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)
arch/x86/kernel/vmlinux.lds.S +25 -0
index ca2252ca6ad7..147cd020516a 100644
@@ -387,6 +387,31 @@ SECTIONS
 	. = ALIGN(PAGE_SIZE);		/* keep VO_INIT_SIZE page aligned */
 	_end = .;
 
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	/*
+	 * Early scratch/workarea section: Lives outside of the kernel proper
+	 * (_text - _end).
+	 *
+	 * Resides after _end because even though the .brk section is after
+	 * __end_of_kernel_reserve, the .brk section is later reserved as a
+	 * part of the kernel. Since it is located after __end_of_kernel_reserve
+	 * it will be discarded and become part of the available memory. As
+	 * such, it can only be used by very early boot code and must not be
+	 * needed afterwards.
+	 *
+	 * Currently used by SME for performing in-place encryption of the
+	 * kernel during boot. Resides on a 2MB boundary to simplify the
+	 * pagetable setup used for SME in-place encryption.
+	 */
+	. = ALIGN(HPAGE_SIZE);
+	.init.scratch : AT(ADDR(.init.scratch) - LOAD_OFFSET) {
+		__init_scratch_begin = .;
+		*(.init.scratch)
+		. = ALIGN(HPAGE_SIZE);
+		__init_scratch_end = .;
+	}
+#endif
+
 	STABS_DEBUG
 	DWARF_DEBUG
 
arch/x86/mm/mem_encrypt_identity.c +20 -2
index 4aa9b1480866..6a8dd483f7d9 100644
@@ -73,6 +73,19 @@ struct sme_populate_pgd_data {
 	unsigned long vaddr_end;
 };
 
+/*
+ * This work area lives in the .init.scratch section, which lives outside of
+ * the kernel proper. It is sized to hold the intermediate copy buffer and
+ * more than enough pagetable pages.
+ *
+ * By using this section, the kernel can be encrypted in place and it
+ * avoids any possibility of boot parameters or initramfs images being
+ * placed such that the in-place encryption logic overwrites them.  This
+ * section is 2MB aligned to allow for simple pagetable setup using only
+ * PMD entries (see vmlinux.lds.S).
+ */
+static char sme_workarea[2 * PMD_PAGE_SIZE] __section(.init.scratch);
+
 static char sme_cmdline_arg[] __initdata = "mem_encrypt";
 static char sme_cmdline_on[]  __initdata = "on";
 static char sme_cmdline_off[] __initdata = "off";
@@ -314,8 +327,13 @@ void __init sme_encrypt_kernel(struct boot_params *bp)
 	}
 #endif
 
-	/* Set the encryption workarea to be immediately after the kernel */
-	workarea_start = kernel_end;
+	/*
+	 * We're running identity mapped, so we must obtain the address to the
+	 * SME encryption workarea using rip-relative addressing.
+	 */
+	asm ("lea sme_workarea(%%rip), %0"
+	     : "=r" (workarea_start)
+	     : "p" (sme_workarea));
 
 	/*
 	 * Calculate required number of workarea bytes needed: