Skip to contentSkip to conversation

x86/e820: Use much less memory for e820/e820_saved, save up to 120k

PATCH Superseded Denys Vlasenko 3 participants 6 messages Sep 9, 2016 – Sep 18, 2016

Subsystems (5)
x86 PCI EFI Xen hypervisor x86 +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH v2 2/2 x86/e820: Use much less memory for e820/e820_saved, save up to 120k

The maximum size of e820 map array for EFI systems is defined as E820_X_MAX (E820MAX + 3 * MAX_NUMNODES).

In x86_64 defconfig, this ends up with E820_X_MAX = 320, e820 and e820_saved are 6404 bytes each.

With larger configs, for example Fedora kernels, E820_X_MAX = 3200, e820 and e820_saved are 64004 bytes each. Most of this space is wasted. Typical machines have some 20-30 e820 areas at most.

This patch turns e820 and e820_saved to pointers which initially point to __initdata tables, of the same size as before.

At the very end of setup_arch(), when we are done fiddling with these maps, allocate smaller alloc_bootmem blocks, copy maps there, and change pointers.

Run-tested.

CC Ingo Molnar <[email protected]>
CC Andy Lutomirski <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Borislav Petkov <[email protected]>
CC Brian Gerst <[email protected]>

---
Changes since v1: added __refdata annotations.

 arch/x86/include/asm/e820.h       |   6 +-
 arch/x86/kernel/e820.c            | 123 +++++++++++++++++++++++---------------
 arch/x86/kernel/early-quirks.c    |   2 +-
 arch/x86/kernel/kexec-bzimage64.c |   4 +-
 arch/x86/kernel/resource.c        |   4 +-
 arch/x86/kernel/setup.c           |  10 ++--
 arch/x86/kernel/tboot.c           |   8 +--
 arch/x86/platform/efi/efi.c       |   2 +-
 arch/x86/xen/setup.c              |   2 +-
 9 files changed, 96 insertions(+), 65 deletions(-)
arch/x86/include/asm/e820.h +4 -2
index 3ab0537..476b574 100644
@@ -10,8 +10,8 @@
 #include <uapi/asm/e820.h>
 #ifndef __ASSEMBLY__
 /* see comment in arch/x86/kernel/e820.c */
-extern struct e820map e820;
-extern struct e820map e820_saved;
+extern struct e820map *e820;
+extern struct e820map *e820_saved;
 
 extern unsigned long pci_mem_start;
 extern int e820_any_mapped(u64 start, u64 end, unsigned type);
@@ -53,6 +53,8 @@ extern void e820_reserve_resources_late(void);
 extern void setup_memory_map(void);
 extern char *default_machine_specific_memory_setup(void);
 
+extern void e820_reallocate_tables(void);
+
 /*
  * Returns true iff the specified range [s,e) is completely contained inside
  * the ISA region.
arch/x86/kernel/e820.c +75 -48

292 lines not on this page · raw message

arch/x86/kernel/early-quirks.c +1 -1
index de7501e..18bb3a6 100644
@@ -555,7 +555,7 @@ intel_graphics_stolen(int num, int slot, int func,
 
 	/* Mark this space as reserved */
 	e820_add_region(base, size, E820_RESERVED);
-	sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
+	sanitize_e820_map(e820->map, ARRAY_SIZE(e820->map), &e820->nr_map);
 }
 
 static void __init intel_graphics_quirks(int num, int slot, int func)
arch/x86/kernel/kexec-bzimage64.c +2 -2
index f2356bd..3407b14 100644
@@ -99,14 +99,14 @@ static int setup_e820_entries(struct boot_params *params)
 {
 	unsigned int nr_e820_entries;
 
-	nr_e820_entries = e820_saved.nr_map;
+	nr_e820_entries = e820_saved->nr_map;
 
 	/* TODO: Pass entries more than E820MAX in bootparams setup data */
 	if (nr_e820_entries > E820MAX)
 		nr_e820_entries = E820MAX;
 
 	params->e820_entries = nr_e820_entries;
-	memcpy(&params->e820_map, &e820_saved.map,
+	memcpy(&params->e820_map, &e820_saved->map,
 	       nr_e820_entries * sizeof(struct e820entry));
 
 	return 0;
arch/x86/kernel/resource.c +2 -2
index 80eab01..2408c16 100644
@@ -27,8 +27,8 @@ static void remove_e820_regions(struct resource *avail)
 	int i;
 	struct e820entry *entry;
 
-	for (i = 0; i < e820.nr_map; i++) {
-		entry = &e820.map[i];
+	for (i = 0; i < e820->nr_map; i++) {
+		entry = &e820->map[i];
 
 		resource_clip(avail, entry->addr,
 			      entry->addr + entry->size - 1);
arch/x86/kernel/setup.c +6 -4
index 0fa60f5..b11517e 100644
@@ -458,8 +458,8 @@ static void __init e820_reserve_setup_data(void)
 		early_memunmap(data, sizeof(*data));
 	}
 
-	sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
-	memcpy(&e820_saved, &e820, sizeof(struct e820map));
+	sanitize_e820_map(e820->map, ARRAY_SIZE(e820->map), &e820->nr_map);
+	memcpy(e820_saved, e820, sizeof(struct e820map));
 	printk(KERN_INFO "extended physical RAM map:\n");
 	e820_print_map("reserve setup_data");
 }
@@ -763,7 +763,7 @@ static void __init trim_bios_range(void)
 	 */
 	e820_remove_range(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_RAM, 1);
 
-	sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
+	sanitize_e820_map(e820->map, ARRAY_SIZE(e820->map), &e820->nr_map);
 }
 
 /* called before trim_bios_range() to spare extra sanitize */
@@ -1032,7 +1032,7 @@ void __init setup_arch(char **cmdline_p)
 	if (ppro_with_ram_bug()) {
 		e820_update_range(0x70000000ULL, 0x40000ULL, E820_RAM,
 				  E820_RESERVED);
-		sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
+		sanitize_e820_map(e820->map, ARRAY_SIZE(e820->map), &e820->nr_map);
 		printk(KERN_INFO "fixed physical RAM map:\n");
 		e820_print_map("bad_ppro");
 	}
@@ -1262,6 +1262,8 @@ void __init setup_arch(char **cmdline_p)
 	if (efi_enabled(EFI_BOOT))
 		efi_apply_memmap_quirks();
 #endif
+
+	e820_reallocate_tables();
 }
 
 #ifdef CONFIG_X86_32
arch/x86/kernel/tboot.c +4 -4
index 654f6c6..8402907 100644
@@ -188,12 +188,12 @@ static int tboot_setup_sleep(void)
 
 	tboot->num_mac_regions = 0;
 
-	for (i = 0; i < e820.nr_map; i++) {
-		if ((e820.map[i].type != E820_RAM)
-		 && (e820.map[i].type != E820_RESERVED_KERN))
+	for (i = 0; i < e820->nr_map; i++) {
+		if ((e820->map[i].type != E820_RAM)
+		 && (e820->map[i].type != E820_RESERVED_KERN))
 			continue;
 
-		add_mac_region(e820.map[i].addr, e820.map[i].size);
+		add_mac_region(e820->map[i].addr, e820->map[i].size);
 	}
 
 	tboot->acpi_sinfo.kernel_s3_resume_vector =
arch/x86/platform/efi/efi.c +1 -1
index 1fbb408..2e34334 100644
@@ -166,7 +166,7 @@ static void __init do_add_efi_memmap(void)
 		}
 		e820_add_region(start, size, e820_type);
 	}
-	sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
+	sanitize_e820_map(e820->map, ARRAY_SIZE(e820->map), &e820->nr_map);
 }
 
 int __init efi_memblock_x86_reserve_range(void)
arch/x86/xen/setup.c +1 -1
index 1764252..f8960fc 100644
@@ -861,7 +861,7 @@ char * __init xen_memory_setup(void)
 	e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
 			E820_RESERVED);
 
-	sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
+	sanitize_e820_map(e820->map, ARRAY_SIZE(e820->map), &e820->nr_map);
 
 	/*
 	 * Check whether the kernel itself conflicts with the target E820 map.
Signature · 1 line
2.9.2
#1 lore raw
Quoted 28 lines · Denys Vlasenko

The maximum size of e820 map array for EFI systems is defined as E820_X_MAX (E820MAX + 3 * MAX_NUMNODES).

In x86_64 defconfig, this ends up with E820_X_MAX = 320, e820 and e820_saved are 6404 bytes each.

With larger configs, for example Fedora kernels, E820_X_MAX = 3200, e820 and e820_saved are 64004 bytes each. Most of this space is wasted. Typical machines have some 20-30 e820 areas at most.

This patch turns e820 and e820_saved to pointers which initially point to __initdata tables, of the same size as before.

At the very end of setup_arch(), when we are done fiddling with these maps, allocate smaller alloc_bootmem blocks, copy maps there, and change pointers.

Run-tested.

arch/x86/kernel/e820.c +10 -0
+/*
+ * Initial e820 and e820_saved are largish __initdata arrays.
+ * Copy them to (usually much smaller) dynamically allocated area.
+ * This is done after all tweaks we ever do to them.
+ */
+__init void e820_reallocate_tables(void)
+{
+	struct e820map *n;
+	int size;
+

arch/x86/kernel/e820.c __init void e820_setup_gap(void) · line 677

Quoted 10 lines
arch/x86/kernel/e820.c +10 -0
+	size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820->nr_map;
+	n = alloc_bootmem(size);
+	memcpy(n, e820, size);
+	e820 = n;
+
+	size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820_saved->nr_map;
+	n = alloc_bootmem(size);
+	memcpy(n, e820_saved, size);
+	e820_saved = n;
+}

Ok, this makes me quite nervous, could you please split this into two patches so that any fails can be nicely bisected to?

First patch only does the pointerization changes with a trivial placeholder structure (full size, static allocated), second patch does all the dangerous bits such as changing it to __initdata, allocating and copying over bits.

Also, could we please also add some minimal debugging facility to make sure the memory table does not get extended after it's been reallocated?

Thanks,

Ingo

#2 lore raw
Quoted 36 lines · Ingo Molnar
Quoted 33 lines · Denys Vlasenko

The maximum size of e820 map array for EFI systems is defined as E820_X_MAX (E820MAX + 3 * MAX_NUMNODES).

In x86_64 defconfig, this ends up with E820_X_MAX = 320, e820 and e820_saved are 6404 bytes each.

With larger configs, for example Fedora kernels, E820_X_MAX = 3200, e820 and e820_saved are 64004 bytes each. Most of this space is wasted. Typical machines have some 20-30 e820 areas at most.

This patch turns e820 and e820_saved to pointers which initially point to __initdata tables, of the same size as before.

At the very end of setup_arch(), when we are done fiddling with these maps, allocate smaller alloc_bootmem blocks, copy maps there, and change pointers.

Run-tested.

arch/x86/kernel/e820.c +15 -0
+/*
+ * Initial e820 and e820_saved are largish __initdata arrays.
+ * Copy them to (usually much smaller) dynamically allocated area.
+ * This is done after all tweaks we ever do to them.
+ */
+__init void e820_reallocate_tables(void)
+{
+	struct e820map *n;
+	int size;
+
+	size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820->nr_map;
+	n = alloc_bootmem(size);
+	memcpy(n, e820, size);
+	e820 = n;
+
Quoted 8 lines

arch/x86/kernel/e820.c __init void e820_setup_gap(void) · line 677

Quoted 5 lines
arch/x86/kernel/e820.c +5 -0
+	size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820_saved->nr_map;
+	n = alloc_bootmem(size);
+	memcpy(n, e820_saved, size);
+	e820_saved = n;
+}

Ok, this makes me quite nervous, could you please split this into two patches so that any fails can be nicely bisected to?

No problem.

Quoted 6 lines

First patch only does the pointerization changes with a trivial placeholder structure (full size, static allocated), second patch does all the dangerous bits such as changing it to __initdata, allocating and copying over bits.

Also, could we please also add some minimal debugging facility to make sure the memory table does not get extended after it's been reallocated?

I have another idea: run e820_reallocate_tables() later, just before we free __init and __initdata. Then e820 tables _can't_ be_ changed - all functions which do that are __init functions.

Will test this now, and send a patchset.

#3 lore raw
Quoted 52 lines · Denys Vlasenko
Quoted 36 lines · Ingo Molnar
Quoted 33 lines · Denys Vlasenko

The maximum size of e820 map array for EFI systems is defined as E820_X_MAX (E820MAX + 3 * MAX_NUMNODES).

In x86_64 defconfig, this ends up with E820_X_MAX = 320, e820 and e820_saved are 6404 bytes each.

With larger configs, for example Fedora kernels, E820_X_MAX = 3200, e820 and e820_saved are 64004 bytes each. Most of this space is wasted. Typical machines have some 20-30 e820 areas at most.

This patch turns e820 and e820_saved to pointers which initially point to __initdata tables, of the same size as before.

At the very end of setup_arch(), when we are done fiddling with these maps, allocate smaller alloc_bootmem blocks, copy maps there, and change pointers.

Run-tested.

arch/x86/kernel/e820.c +15 -0
+/*
+ * Initial e820 and e820_saved are largish __initdata arrays.
+ * Copy them to (usually much smaller) dynamically allocated area.
+ * This is done after all tweaks we ever do to them.
+ */
+__init void e820_reallocate_tables(void)
+{
+	struct e820map *n;
+	int size;
+
+	size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820->nr_map;
+	n = alloc_bootmem(size);
+	memcpy(n, e820, size);
+	e820 = n;
+
Quoted 8 lines

arch/x86/kernel/e820.c __init void e820_setup_gap(void) · line 677

Quoted 5 lines
arch/x86/kernel/e820.c +5 -0
+	size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820_saved->nr_map;
+	n = alloc_bootmem(size);
+	memcpy(n, e820_saved, size);
+	e820_saved = n;
+}

Ok, this makes me quite nervous, could you please split this into two patches so that any fails can be nicely bisected to?

No problem.

Quoted 4 lines

First patch only does the pointerization changes with a trivial placeholder structure (full size, static allocated), second patch does all the dangerous bits such as changing it to __initdata, allocating and copying over bits.

Quoted 8 lines
Quoted 2 lines

Also, could we please also add some minimal debugging facility to make sure the memory table does not get extended after it's been reallocated?

I have another idea: run e820_reallocate_tables() later, just before we free __init and __initdata. Then e820 tables _can't_ be_ changed - all functions which do that are __init functions.

Will test this now, and send a patchset.

Could we also mark it __ro_after_init?

Thanks,

Ingo

#4 lore raw
Quoted 55 lines · Ingo Molnar
Quoted 54 lines · Denys Vlasenko
Quoted 35 lines · Ingo Molnar
Quoted 33 lines · Denys Vlasenko

The maximum size of e820 map array for EFI systems is defined as E820_X_MAX (E820MAX + 3 * MAX_NUMNODES).

In x86_64 defconfig, this ends up with E820_X_MAX = 320, e820 and e820_saved are 6404 bytes each.

With larger configs, for example Fedora kernels, E820_X_MAX = 3200, e820 and e820_saved are 64004 bytes each. Most of this space is wasted. Typical machines have some 20-30 e820 areas at most.

This patch turns e820 and e820_saved to pointers which initially point to __initdata tables, of the same size as before.

At the very end of setup_arch(), when we are done fiddling with these maps, allocate smaller alloc_bootmem blocks, copy maps there, and change pointers.

Run-tested.

arch/x86/kernel/e820.c +15 -0
+/*
+ * Initial e820 and e820_saved are largish __initdata arrays.
+ * Copy them to (usually much smaller) dynamically allocated area.
+ * This is done after all tweaks we ever do to them.
+ */
+__init void e820_reallocate_tables(void)
+{
+	struct e820map *n;
+	int size;
+
+	size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820->nr_map;
+	n = alloc_bootmem(size);
+	memcpy(n, e820, size);
+	e820 = n;
+
Quoted 8 lines

arch/x86/kernel/e820.c __init void e820_setup_gap(void) · line 677

Quoted 5 lines
arch/x86/kernel/e820.c +5 -0
+	size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820_saved->nr_map;
+	n = alloc_bootmem(size);
+	memcpy(n, e820_saved, size);
+	e820_saved = n;
+}

Ok, this makes me quite nervous, could you please split this into two patches so that any fails can be nicely bisected to?

No problem.

Quoted 6 lines

First patch only does the pointerization changes with a trivial placeholder structure (full size, static allocated), second patch does all the dangerous bits such as changing it to __initdata, allocating and copying over bits.

Also, could we please also add some minimal debugging facility to make sure the memory table does not get extended after it's been reallocated?

Quoted 7 lines
Quoted 5 lines

I have another idea: run e820_reallocate_tables() later, just before we free __init and __initdata. Then e820 tables _can't_ be_ changed - all functions which do that are __init functions.

Will test this now, and send a patchset.

Could we also mark it __ro_after_init?

__ro_after_init makes sense only for statically allocated objects. e820_reallocate_tables() copies e280 maps to kmalloced memory.

#5 lore raw
Quoted 68 lines · Denys Vlasenko
Quoted 66 lines · Ingo Molnar
Quoted 64 lines · Denys Vlasenko
Quoted 38 lines · Ingo Molnar
Quoted 35 lines · Denys Vlasenko

The maximum size of e820 map array for EFI systems is defined as E820_X_MAX (E820MAX + 3 * MAX_NUMNODES).

In x86_64 defconfig, this ends up with E820_X_MAX = 320, e820 and e820_saved are 6404 bytes each.

With larger configs, for example Fedora kernels, E820_X_MAX = 3200, e820 and e820_saved are 64004 bytes each. Most of this space is wasted. Typical machines have some 20-30 e820 areas at most.

This patch turns e820 and e820_saved to pointers which initially point to __initdata tables, of the same size as before.

At the very end of setup_arch(), when we are done fiddling with these maps, allocate smaller alloc_bootmem blocks, copy maps there, and change pointers.

Run-tested.

arch/x86/kernel/e820.c +16 -0
+/*
+ * Initial e820 and e820_saved are largish __initdata arrays.
+ * Copy them to (usually much smaller) dynamically allocated area.
+ * This is done after all tweaks we ever do to them.
+ */
+__init void e820_reallocate_tables(void)
+{
+       struct e820map *n;
+       int size;
+
+       size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820->nr_map;
+       n = alloc_bootmem(size);
+       memcpy(n, e820, size);
+       e820 = n;
+
+       size = offsetof(struct e820map, map) + sizeof(struct e820entry) * e820_saved->nr_map;
Quoted 8 lines

arch/x86/kernel/e820.c __init void e820_setup_gap(void) · line 677

Quoted 4 lines
arch/x86/kernel/e820.c +4 -0
+       n = alloc_bootmem(size);
+       memcpy(n, e820_saved, size);
+       e820_saved = n;
+}

Ok, this makes me quite nervous, could you please split this into two patches so that any fails can be nicely bisected to?

No problem.

Quoted 6 lines

First patch only does the pointerization changes with a trivial placeholder structure (full size, static allocated), second patch does all the dangerous bits such as changing it to __initdata, allocating and copying over bits.

Also, could we please also add some minimal debugging facility to make sure the memory table does not get extended after it's been reallocated?

I have another idea: run e820_reallocate_tables() later, just before we free __init and __initdata. Then e820 tables _can't_ be_ changed - all functions which do that are __init functions.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Will test this now, and send a patchset.

Could we also mark it __ro_after_init?

__ro_after_init makes sense only for statically allocated objects. e820_reallocate_tables() copies e280 maps to kmalloced memory.

The pointer can be __ro_after_init, though. You could also set_memory_ro() the thing if it's big enough that page-aligning it wouldn't be a problem.