Skip to content

Commit 565eb5f

Browse files
committed
Merge branch 'x86-kdump-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x865 kdump updates from Thomas Gleixner: "Yet more kexec/kdump updates: - Properly support kexec when AMD's memory encryption (SME) is enabled - Pass reserved e820 ranges to the kexec kernel so both PCI and SME can work" * 'x86-kdump-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: fs/proc/vmcore: Enable dumping of encrypted memory when SEV was active x86/kexec: Set the C-bit in the identity map page table when SEV is active x86/kexec: Do not map kexec area as decrypted when SEV is active x86/crash: Add e820 reserved ranges to kdump kernel's e820 table x86/mm: Rework ioremap resource mapping determination x86/e820, ioport: Add a new I/O resource descriptor IORES_DESC_RESERVED x86/mm: Create a workarea in the kernel for SME early encryption x86/mm: Identify the end of the kernel area to be reserved
2 parents b7d5c92 + 4eb5fec commit 565eb5f

10 files changed

Lines changed: 155 additions & 37 deletions

File tree

‎arch/x86/include/asm/sections.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ extern char __end_rodata_aligned[];
1313
extern char __end_rodata_hpage_align[];
1414
#endif
1515

16+
extern char __end_of_kernel_reserve[];
17+
1618
#endif /* _ASM_X86_SECTIONS_H */

‎arch/x86/kernel/crash.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)
375375
walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1, &cmd,
376376
memmap_entry_callback);
377377

378+
/* Add e820 reserved ranges */
379+
cmd.type = E820_TYPE_RESERVED;
380+
flags = IORESOURCE_MEM;
381+
walk_iomem_res_desc(IORES_DESC_RESERVED, flags, 0, -1, &cmd,
382+
memmap_entry_callback);
383+
378384
/* Add crashk_low_res region */
379385
if (crashk_low_res.end) {
380386
ei.addr = crashk_low_res.start;

‎arch/x86/kernel/e820.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,10 +1063,10 @@ static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
10631063
case E820_TYPE_NVS: return IORES_DESC_ACPI_NV_STORAGE;
10641064
case E820_TYPE_PMEM: return IORES_DESC_PERSISTENT_MEMORY;
10651065
case E820_TYPE_PRAM: return IORES_DESC_PERSISTENT_MEMORY_LEGACY;
1066+
case E820_TYPE_RESERVED: return IORES_DESC_RESERVED;
10661067
case E820_TYPE_RESERVED_KERN: /* Fall-through: */
10671068
case E820_TYPE_RAM: /* Fall-through: */
10681069
case E820_TYPE_UNUSABLE: /* Fall-through: */
1069-
case E820_TYPE_RESERVED: /* Fall-through: */
10701070
default: return IORES_DESC_NONE;
10711071
}
10721072
}

‎arch/x86/kernel/machine_kexec_64.c‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ static void free_transition_pgtable(struct kimage *image)
123123

124124
static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
125125
{
126+
pgprot_t prot = PAGE_KERNEL_EXEC_NOENC;
127+
unsigned long vaddr, paddr;
128+
int result = -ENOMEM;
126129
p4d_t *p4d;
127130
pud_t *pud;
128131
pmd_t *pmd;
129132
pte_t *pte;
130-
unsigned long vaddr, paddr;
131-
int result = -ENOMEM;
132133

133134
vaddr = (unsigned long)relocate_kernel;
134135
paddr = __pa(page_address(image->control_code_page)+PAGE_SIZE);
@@ -165,7 +166,11 @@ static int init_transition_pgtable(struct kimage *image, pgd_t *pgd)
165166
set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
166167
}
167168
pte = pte_offset_kernel(pmd, vaddr);
168-
set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC_NOENC));
169+
170+
if (sev_active())
171+
prot = PAGE_KERNEL_EXEC;
172+
173+
set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
169174
return 0;
170175
err:
171176
return result;
@@ -202,6 +207,11 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
202207
level4p = (pgd_t *)__va(start_pgtable);
203208
clear_page(level4p);
204209

210+
if (sev_active()) {
211+
info.page_flag |= _PAGE_ENC;
212+
info.kernpg_flag |= _PAGE_ENC;
213+
}
214+
205215
if (direct_gbpages)
206216
info.direct_gbpages = true;
207217

@@ -644,8 +654,20 @@ void arch_kexec_unprotect_crashkres(void)
644654
kexec_mark_crashkres(false);
645655
}
646656

657+
/*
658+
* During a traditional boot under SME, SME will encrypt the kernel,
659+
* so the SME kexec kernel also needs to be un-encrypted in order to
660+
* replicate a normal SME boot.
661+
*
662+
* During a traditional boot under SEV, the kernel has already been
663+
* loaded encrypted, so the SEV kexec kernel needs to be encrypted in
664+
* order to replicate a normal SEV boot.
665+
*/
647666
int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp)
648667
{
668+
if (sev_active())
669+
return 0;
670+
649671
/*
650672
* If SME is active we need to be sure that kexec pages are
651673
* not encrypted because when we boot to the new kernel the
@@ -656,6 +678,9 @@ int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp)
656678

657679
void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages)
658680
{
681+
if (sev_active())
682+
return;
683+
659684
/*
660685
* If SME is active we need to reset the pages back to being
661686
* an encrypted mapping before freeing them.

‎arch/x86/kernel/setup.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,14 @@ dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
836836

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

842848
/*
843849
* Make sure page 0 is always reserved because on systems with

‎arch/x86/kernel/vmlinux.lds.S‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,14 @@ SECTIONS
368368
__bss_stop = .;
369369
}
370370

371+
/*
372+
* The memory occupied from _text to here, __end_of_kernel_reserve, is
373+
* automatically reserved in setup_arch(). Anything after here must be
374+
* explicitly reserved using memblock_reserve() or it will be discarded
375+
* and treated as available memory.
376+
*/
377+
__end_of_kernel_reserve = .;
378+
371379
. = ALIGN(PAGE_SIZE);
372380
.brk : AT(ADDR(.brk) - LOAD_OFFSET) {
373381
__brk_base = .;
@@ -379,10 +387,34 @@ SECTIONS
379387
. = ALIGN(PAGE_SIZE); /* keep VO_INIT_SIZE page aligned */
380388
_end = .;
381389

390+
#ifdef CONFIG_AMD_MEM_ENCRYPT
391+
/*
392+
* Early scratch/workarea section: Lives outside of the kernel proper
393+
* (_text - _end).
394+
*
395+
* Resides after _end because even though the .brk section is after
396+
* __end_of_kernel_reserve, the .brk section is later reserved as a
397+
* part of the kernel. Since it is located after __end_of_kernel_reserve
398+
* it will be discarded and become part of the available memory. As
399+
* such, it can only be used by very early boot code and must not be
400+
* needed afterwards.
401+
*
402+
* Currently used by SME for performing in-place encryption of the
403+
* kernel during boot. Resides on a 2MB boundary to simplify the
404+
* pagetable setup used for SME in-place encryption.
405+
*/
406+
. = ALIGN(HPAGE_SIZE);
407+
.init.scratch : AT(ADDR(.init.scratch) - LOAD_OFFSET) {
408+
__init_scratch_begin = .;
409+
*(.init.scratch)
410+
. = ALIGN(HPAGE_SIZE);
411+
__init_scratch_end = .;
412+
}
413+
#endif
414+
382415
STABS_DEBUG
383416
DWARF_DEBUG
384417

385-
/* Sections to be discarded */
386418
DISCARDS
387419
/DISCARD/ : {
388420
*(.eh_frame)

‎arch/x86/mm/ioremap.c‎

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
#include "physaddr.h"
3030

31-
struct ioremap_mem_flags {
32-
bool system_ram;
33-
bool desc_other;
31+
/*
32+
* Descriptor controlling ioremap() behavior.
33+
*/
34+
struct ioremap_desc {
35+
unsigned int flags;
3436
};
3537

3638
/*
@@ -62,42 +64,59 @@ int ioremap_change_attr(unsigned long vaddr, unsigned long size,
6264
return err;
6365
}
6466

65-
static bool __ioremap_check_ram(struct resource *res)
67+
/* Does the range (or a subset of) contain normal RAM? */
68+
static unsigned int __ioremap_check_ram(struct resource *res)
6669
{
6770
unsigned long start_pfn, stop_pfn;
6871
unsigned long i;
6972

7073
if ((res->flags & IORESOURCE_SYSTEM_RAM) != IORESOURCE_SYSTEM_RAM)
71-
return false;
74+
return 0;
7275

7376
start_pfn = (res->start + PAGE_SIZE - 1) >> PAGE_SHIFT;
7477
stop_pfn = (res->end + 1) >> PAGE_SHIFT;
7578
if (stop_pfn > start_pfn) {
7679
for (i = 0; i < (stop_pfn - start_pfn); ++i)
7780
if (pfn_valid(start_pfn + i) &&
7881
!PageReserved(pfn_to_page(start_pfn + i)))
79-
return true;
82+
return IORES_MAP_SYSTEM_RAM;
8083
}
8184

82-
return false;
85+
return 0;
8386
}
8487

85-
static int __ioremap_check_desc_other(struct resource *res)
88+
/*
89+
* In a SEV guest, NONE and RESERVED should not be mapped encrypted because
90+
* there the whole memory is already encrypted.
91+
*/
92+
static unsigned int __ioremap_check_encrypted(struct resource *res)
8693
{
87-
return (res->desc != IORES_DESC_NONE);
94+
if (!sev_active())
95+
return 0;
96+
97+
switch (res->desc) {
98+
case IORES_DESC_NONE:
99+
case IORES_DESC_RESERVED:
100+
break;
101+
default:
102+
return IORES_MAP_ENCRYPTED;
103+
}
104+
105+
return 0;
88106
}
89107

90-
static int __ioremap_res_check(struct resource *res, void *arg)
108+
static int __ioremap_collect_map_flags(struct resource *res, void *arg)
91109
{
92-
struct ioremap_mem_flags *flags = arg;
110+
struct ioremap_desc *desc = arg;
93111

94-
if (!flags->system_ram)
95-
flags->system_ram = __ioremap_check_ram(res);
112+
if (!(desc->flags & IORES_MAP_SYSTEM_RAM))
113+
desc->flags |= __ioremap_check_ram(res);
96114

97-
if (!flags->desc_other)
98-
flags->desc_other = __ioremap_check_desc_other(res);
115+
if (!(desc->flags & IORES_MAP_ENCRYPTED))
116+
desc->flags |= __ioremap_check_encrypted(res);
99117

100-
return flags->system_ram && flags->desc_other;
118+
return ((desc->flags & (IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED)) ==
119+
(IORES_MAP_SYSTEM_RAM | IORES_MAP_ENCRYPTED));
101120
}
102121

103122
/*
@@ -106,15 +125,15 @@ static int __ioremap_res_check(struct resource *res, void *arg)
106125
* resource described not as IORES_DESC_NONE (e.g. IORES_DESC_ACPI_TABLES).
107126
*/
108127
static void __ioremap_check_mem(resource_size_t addr, unsigned long size,
109-
struct ioremap_mem_flags *flags)
128+
struct ioremap_desc *desc)
110129
{
111130
u64 start, end;
112131

113132
start = (u64)addr;
114133
end = start + size - 1;
115-
memset(flags, 0, sizeof(*flags));
134+
memset(desc, 0, sizeof(struct ioremap_desc));
116135

117-
walk_mem_res(start, end, flags, __ioremap_res_check);
136+
walk_mem_res(start, end, desc, __ioremap_collect_map_flags);
118137
}
119138

120139
/*
@@ -131,15 +150,15 @@ static void __ioremap_check_mem(resource_size_t addr, unsigned long size,
131150
* have to convert them into an offset in a page-aligned mapping, but the
132151
* caller shouldn't need to know that small detail.
133152
*/
134-
static void __iomem *__ioremap_caller(resource_size_t phys_addr,
135-
unsigned long size, enum page_cache_mode pcm,
136-
void *caller, bool encrypted)
153+
static void __iomem *
154+
__ioremap_caller(resource_size_t phys_addr, unsigned long size,
155+
enum page_cache_mode pcm, void *caller, bool encrypted)
137156
{
138157
unsigned long offset, vaddr;
139158
resource_size_t last_addr;
140159
const resource_size_t unaligned_phys_addr = phys_addr;
141160
const unsigned long unaligned_size = size;
142-
struct ioremap_mem_flags mem_flags;
161+
struct ioremap_desc io_desc;
143162
struct vm_struct *area;
144163
enum page_cache_mode new_pcm;
145164
pgprot_t prot;
@@ -158,12 +177,12 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr,
158177
return NULL;
159178
}
160179

161-
__ioremap_check_mem(phys_addr, size, &mem_flags);
180+
__ioremap_check_mem(phys_addr, size, &io_desc);
162181

163182
/*
164183
* Don't allow anybody to remap normal RAM that we're using..
165184
*/
166-
if (mem_flags.system_ram) {
185+
if (io_desc.flags & IORES_MAP_SYSTEM_RAM) {
167186
WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n",
168187
&phys_addr, &last_addr);
169188
return NULL;
@@ -201,7 +220,7 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr,
201220
* resulting mapping.
202221
*/
203222
prot = PAGE_KERNEL_IO;
204-
if ((sev_active() && mem_flags.desc_other) || encrypted)
223+
if ((io_desc.flags & IORES_MAP_ENCRYPTED) || encrypted)
205224
prot = pgprot_encrypted(prot);
206225

207226
switch (pcm) {

‎arch/x86/mm/mem_encrypt_identity.c‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ struct sme_populate_pgd_data {
7070
unsigned long vaddr_end;
7171
};
7272

73+
/*
74+
* This work area lives in the .init.scratch section, which lives outside of
75+
* the kernel proper. It is sized to hold the intermediate copy buffer and
76+
* more than enough pagetable pages.
77+
*
78+
* By using this section, the kernel can be encrypted in place and it
79+
* avoids any possibility of boot parameters or initramfs images being
80+
* placed such that the in-place encryption logic overwrites them. This
81+
* section is 2MB aligned to allow for simple pagetable setup using only
82+
* PMD entries (see vmlinux.lds.S).
83+
*/
84+
static char sme_workarea[2 * PMD_PAGE_SIZE] __section(.init.scratch);
85+
7386
static char sme_cmdline_arg[] __initdata = "mem_encrypt";
7487
static char sme_cmdline_on[] __initdata = "on";
7588
static char sme_cmdline_off[] __initdata = "off";
@@ -311,8 +324,13 @@ void __init sme_encrypt_kernel(struct boot_params *bp)
311324
}
312325
#endif
313326

314-
/* Set the encryption workarea to be immediately after the kernel */
315-
workarea_start = kernel_end;
327+
/*
328+
* We're running identity mapped, so we must obtain the address to the
329+
* SME encryption workarea using rip-relative addressing.
330+
*/
331+
asm ("lea sme_workarea(%%rip), %0"
332+
: "=r" (workarea_start)
333+
: "p" (sme_workarea));
316334

317335
/*
318336
* Calculate required number of workarea bytes needed:

‎fs/proc/vmcore.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ void __weak elfcorehdr_free(unsigned long long addr)
166166
*/
167167
ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
168168
{
169-
return read_from_oldmem(buf, count, ppos, 0, false);
169+
return read_from_oldmem(buf, count, ppos, 0, sev_active());
170170
}
171171

172172
/*
173173
* Architectures may override this function to read from notes sections
174174
*/
175175
ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
176176
{
177-
return read_from_oldmem(buf, count, ppos, 0, sme_active());
177+
return read_from_oldmem(buf, count, ppos, 0, mem_encrypt_active());
178178
}
179179

180180
/*
@@ -374,7 +374,7 @@ static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos,
374374
buflen);
375375
start = m->paddr + *fpos - m->offset;
376376
tmp = read_from_oldmem(buffer, tsz, &start,
377-
userbuf, sme_active());
377+
userbuf, mem_encrypt_active());
378378
if (tmp < 0)
379379
return tmp;
380380
buflen -= tsz;

0 commit comments

Comments
 (0)