x86, memblock: Allocate memory near kernel image before SRAT parsed
Subsystems (8)
v7 cover x86, memblock: Allocate memory near kernel image before SRAT parsed
Hello, here is the v7 version. Any comments are welcome!
The v7 version is based on linus's tree (3.12-rc4) HEAD is:
commit d0e639c9e06d44e713170031fe05fb60ebe680af
Author: Linus Torvalds <[email protected]> Date: Sun Oct 6 14:00:20 2013 -0700
Linux 3.12-rc4
[Problem]
The current Linux cannot migrate pages used by the kerenl because of the kernel direct mapping. In Linux kernel space, va = pa + PAGE_OFFSET. When the pa is changed, we cannot simply update the pagetable and keep the va unmodified. So the kernel pages are not migratable.
There are also some other issues will cause the kernel pages not migratable. For example, the physical address may be cached somewhere and will be used. It is not to update all the caches.
When doing memory hotplug in Linux, we first migrate all the pages in one memory device somewhere else, and then remove the device. But if pages are used by the kernel, they are not migratable. As a result, memory used by the kernel cannot be hot-removed.
Modifying the kernel direct mapping mechanism is too difficult to do. And it may cause the kernel performance down and unstable. So we use the following way to do memory hotplug.
[What we are doing]
In Linux, memory in one numa node is divided into several zones. One of the zones is ZONE_MOVABLE, which the kernel won't use.
In order to implement memory hotplug in Linux, we are going to arrange all hotpluggable memory in ZONE_MOVABLE so that the kernel won't use these memory. To do this, we need ACPI's help.
In ACPI, SRAT(System Resource Affinity Table) contains NUMA info. The memory affinities in SRAT record every memory range in the system, and also, flags specifying if the memory range is hotpluggable.
(Please refer to ACPI spec 5.0 5.2.16)
With the help of SRAT, we have to do the following two things to achieve our goal:
1. When doing memory hot-add, allow the users arranging hotpluggable as ZONE_MOVABLE.
(This has been done by the MOVABLE_NODE functionality in Linux.)
2. when the system is booting, prevent bootmem allocator from allocating hotpluggable memory for the kernel before the memory initialization finishes.
The problem 2 is the key problem we are going to solve. But before solving it, we need some preparation. Please see below.
[Preparation]
Bootloader has to load the kernel image into memory. And this memory must be unhotpluggable. We cannot prevent this anyway. So in a memory hotplug system, we can assume any node the kernel resides in is not hotpluggable.
Before SRAT is parsed, we don't know which memory ranges are hotpluggable. But memblock has already started to work. In the current kernel, memblock allocates the following memory before SRAT is parsed:
setup_arch()
|->memblock_x86_fill() /* memblock is ready */ |...... |->early_reserve_e820_mpc_new() /* allocate memory under 1MB */ |->reserve_real_mode() /* allocate memory under 1MB */ |->init_mem_mapping() /* allocate page tables, about 2MB to map 1GB memory */ |->dma_contiguous_reserve() /* specified by user, should be low */ |->setup_log_buf() /* specified by user, several mega bytes */ |->relocate_initrd() /* could be large, but will be freed after boot, should reorder */ |->acpi_initrd_override() /* several mega bytes */ |->reserve_crashkernel() /* could be large, should reorder */ |...... |->initmem_init() /* Parse SRAT */
According to Tejun's advice, before SRAT is parsed, we should try our best to allocate memory near the kernel image. Since the whole node the kernel resides in won't be hotpluggable, and for a modern server, a node may have at least 16GB memory, allocating several mega bytes memory around the kernel image won't cross to hotpluggable memory.
[About this patch-set]
So this patch-set is the preparation for the problem 2 that we want to solve. It does the following:
1. Make memblock be able to allocate memory bottom up.
1) Keep all the memblock APIs' prototype unmodified.
2) When the direction is bottom up, keep the start address greater than the end of kernel image.
2. Improve init_mem_mapping() to support allocate page tables in bottom up direction.
3. Introduce "movable_node" boot option to enable and disable this functionality.
Change log v6 -> v7:
1. Add toshi's ack in several patches.
2. Make __pa_symbol() available everywhere by putting a pesudo __pa_symbol define in include/linux/mm.h. Thanks HPA.
3. Add notes about the page table allocation in bottom-up.
Change log v5 -> v6:
1. Add tejun and toshi's ack in several patches.
2. Change movablenode to movable_node boot option and update the description for movable_node and CONFIG_MOVABLE_NODE. Thanks Ingo!
3. Fix the __pa_symbol() issue pointed by Andrew Morton.
4. Update some functions' comments and names.
Change log v4 -> v5:
1. Change memblock.current_direction to a boolean memblock.bottom_up. And remove the direction enum.
2. Update and add some comments to explain things clearer.
3. Misc fixes, such as removing unnecessary #ifdef
Change log v3 -> v4:
1. Use bottom-up/top-down to unify things. Thanks tj.
2. Factor out of current top-down implementation and then introduce bottom-up mode, not mixing them in one patch. Thanks tj.
3. Changes function naming: memblock_direction_bottom_up -> memblock_bottom_up
4. Use memblock_set_bottom_up to replace memblock_set_current_direction, which makes the code simpler. Thanks tj.
5. Define two implementions of function memblock_bottom_up and memblock_set_bottom_up in order not to use #ifdef in the boot code. Thanks tj.
6. Add comments to explain why retry top-down allocation when bottom_up allocation failed. Thanks tj and toshi!
Change log v2 -> v3:
1. According to Toshi's suggestion, move the direction checking logic into memblock. And simply the code more.
Change log v1 -> v2:
1. According to tj's suggestion, implemented a new function memblock_alloc_bottom_up() to allocate memory from bottom upwards, whihc can simplify the code.
Tang Chen (6):
memblock: Factor out of top-down allocation
memblock: Introduce bottom-up allocation mode
x86/mm: Factor out of top-down direct mapping setup x86/mem-hotplug: Support initialize page tables in bottom-up x86, acpi, crash, kdump: Do reserve_crashkernel() after SRAT is
parsed.
mem-hotplug: Introduce movable_node boot option
Documentation/kernel-parameters.txt | 3 + arch/x86/kernel/setup.c | 9 ++- arch/x86/mm/init.c | 122 ++++++++++++++++++++++++++++------ arch/x86/mm/numa.c | 11 +++ include/linux/memblock.h | 24 +++++++ include/linux/mm.h | 4 + mm/Kconfig | 17 +++-- mm/memblock.c | 126 +++++++++++++++++++++++++++++++---- mm/memory_hotplug.c | 31 +++++++++ 9 files changed, 306 insertions(+), 41 deletions(-)
PATCH v7 1/6 memblock: Factor out of top-down allocation
From: Tang Chen <[email protected]>
This patch creates a new function __memblock_find_range_top_down to factor out of top-down allocation from memblock_find_in_range_node. This is a preparation because we will introduce a new bottom-up allocation mode in the following patch.
--- mm/memblock.c | 47 ++++++++++++++++++++++++++++++++++------------- 1 files changed, 34 insertions(+), 13 deletions(-)
mm/memblock.c +34 -13
@@ -83,33 +83,25 @@ static long __init_memblock memblock_overlaps_region(struct memblock_type *type, }/** -* memblock_find_in_range_node - find free area in given range and node +* __memblock_find_range_top_down - find free area utility, in top-down * @start: start of candidate range * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} * @size: size of free area to find * @align: alignment of free area to find * @nid: nid of the free area to find, %MAX_NUMNODES for any node * -* Find @size free area aligned to @align in the specified range and node. +* Utility called from memblock_find_in_range_node(), find free area top-down. * * RETURNS: * Found address on success, %0 on failure. */ -phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start, -phys_addr_t end,phys_addr_t size, -phys_addr_t align,int nid) +static phys_addr_t __init_memblock +__memblock_find_range_top_down (phys_addr_t start,phys_addr_t end, +phys_addr_t size,phys_addr_t align,int nid) {phys_addr_t this_start, this_end, cand;u64 i; -/* pump up @end */ -if (end ==MEMBLOCK_ALLOC_ACCESSIBLE ) - end = memblock.current_limit; - -/* avoid allocating the first page */ - start =max_t (phys_addr_t, start,PAGE_SIZE ); - end =max (start, end); -for_each_free_mem_range_reverse (i, nid, &this_start, &this_end,NULL ) { this_start =clamp (this_start, start, end); this_end =clamp (this_end, start, end);
@@ -121,10 +113,39 @@ phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start,if (cand >= this_start)return cand; } +return 0 ; }/** +* memblock_find_in_range_node - find free area in given range and node +* @start: start of candidate range +* @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE} +* @size: size of free area to find +* @align: alignment of free area to find +* @nid: nid of the free area to find, %MAX_NUMNODES for any node +* +* Find @size free area aligned to @align in the specified range and node. +* +* RETURNS: +* Found address on success, %0 on failure. +*/ +phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start, +phys_addr_t end,phys_addr_t size, +phys_addr_t align,int nid) +{ +/* pump up @end */ +if (end ==MEMBLOCK_ALLOC_ACCESSIBLE ) + end = memblock.current_limit; + +/* avoid allocating the first page */ + start =max_t (phys_addr_t, start,PAGE_SIZE ); + end =max (start, end); + +return __memblock_find_range_top_down (start, end, size, align, nid); +} + +/** * memblock_find_in_range - find free area in given range * @start: start of candidate range * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
Signature · 1 line
1.7.1
PATCH v7 2/6 memblock: Introduce bottom-up allocation mode
In reply to Zhang YanfeiFrom: Tang Chen <[email protected]>
The Linux kernel cannot migrate pages used by the kernel. As a result, kernel pages cannot be hot-removed. So we cannot allocate hotpluggable memory for the kernel.
ACPI SRAT (System Resource Affinity Table) contains the memory hotplug info. But before SRAT is parsed, memblock has already started to allocate memory for the kernel. So we need to prevent memblock from doing this.
In a memory hotplug system, any numa node the kernel resides in should be unhotpluggable. And for a modern server, each node could have at least 16GB memory. So memory around the kernel image is highly likely unhotpluggable.
So the basic idea is: Allocate memory from the end of the kernel image and to the higher memory. Since memory allocation before SRAT is parsed won't be too much, it could highly likely be in the same node with kernel image.
The current memblock can only allocate memory top-down. So this patch introduces a new bottom-up allocation mode to allocate memory bottom-up. And later when we use this allocation direction to allocate memory, we will limit the start address above the kernel.
--- include/linux/memblock.h | 24 +++++++++++++ include/linux/mm.h | 4 ++ mm/memblock.c | 83 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 108 insertions(+), 3 deletions(-)
include/linux/memblock.h +24 -0
@@ -35,6 +35,7 @@ struct memblock_type { };struct memblock { +bool bottom_up;/* is bottom up direction? */ phys_addr_t current_limit;struct memblock_type memory;struct memblock_type reserved;
@@ -148,6 +149,29 @@ phys_addr_t memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)phys_addr_t memblock_alloc (phys_addr_t size,phys_addr_t align); +#ifdef CONFIG_MOVABLE_NODE +/* +* Set the allocation direction to bottom-up or top-down. +*/ +static inline void memblock_set_bottom_up (bool enable) +{ + memblock.bottom_up = enable; +} + +/* +* Check if the allocation direction is bottom-up or not. +* if this is true, that said, memblock will allocate memory +* in bottom-up direction. +*/ +static inline bool memblock_bottom_up (void ) +{ +return memblock.bottom_up; +} +#else +static inline void memblock_set_bottom_up (bool enable) {} +static inline bool memblock_bottom_up (void ) {return false; } +#endif +/* Flags for memblock_alloc_base() amd __memblock_alloc_base() */ #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)#define MEMBLOCK_ALLOC_ACCESSIBLE 0
include/linux/mm.h +4 -0
@@ -50,6 +50,10 @@ extern int sysctl_legacy_va_layout;#include <asm/pgtable.h> #include <asm/processor.h> +#ifndef __pa_symbol +#define __pa_symbol (x) __pa(RELOC_HIDE((unsigned long)(x), 0)) +#endif +extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;
mm/memblock.c +80 -3
131 lines not on this page · raw message
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v7 3/6 x86/mm: Factor out of top-down direct mapping setup
In reply to Zhang YanfeiFrom: Tang Chen <[email protected]>
This patch creates a new function memory_map_top_down to factor out of the top-down direct memory mapping pagetable setup. This is also a preparation for the following patch, which will introduce the bottom-up memory mapping. That said, we will put the two ways of pagetable setup into separate functions, and choose to use which way in init_mem_mapping, which makes the code more clear.
--- arch/x86/mm/init.c | 60 ++++++++++++++++++++++++++++++++++----------------- 1 files changed, 40 insertions(+), 20 deletions(-)
arch/x86/mm/init.c +40 -20
@@ -401,27 +401,28 @@ static unsigned long __init init_range_memory_mapping(/* (PUD_SHIFT-PMD_SHIFT)/2 */ #define STEP_SIZE_SHIFT 5 -void __init init_mem_mapping (void ) + +/** +* memory_map_top_down - Map [map_start, map_end) top down +* @map_start: start address of the target memory range +* @map_end: end address of the target memory range +* +* This function will setup direct mapping for memory range +* [map_start, map_end) in top-down. That said, the page tables +* will be allocated at the end of the memory, and we map the +* memory in top-down. +*/ +static void __init memory_map_top_down (unsigned long map_start, +unsigned long map_end) { -unsigned long end, real_end, start, last_start; +unsigned long real_end, start, last_start;unsigned long step_size;unsigned long addr;unsigned long mapped_ram_size =0 ;unsigned long new_mapped_ram_size; -probe_page_size_mask (); - -#ifdef CONFIG_X86_64 - end = max_pfn <<PAGE_SHIFT ; -#else - end = max_low_pfn <<PAGE_SHIFT ; -#endif - -/* the ISA range is always mapped regardless of memory holes */ -init_memory_mapping (0 ,ISA_END_ADDRESS ); -/* xen has big range in reserved near end of ram, skip it at first.*/ - addr =memblock_find_in_range (ISA_END_ADDRESS , end,PMD_SIZE ,PMD_SIZE ); + addr =memblock_find_in_range (map_start, map_end,PMD_SIZE ,PMD_SIZE ); real_end = addr +PMD_SIZE ;/* step_size need to be small so pgt_buf from BRK could cover it */
@@ -436,13 +437,13 @@ void __init init_mem_mapping(void)* end of RAM in [min_pfn_mapped, max_pfn_mapped) used as new pages * for page table. */ -while (last_start >ISA_END_ADDRESS ) { +while (last_start > map_start) {if (last_start > step_size) { start =round_down (last_start -1 , step_size); -if (start <ISA_END_ADDRESS ) - start =ISA_END_ADDRESS ; +if (start < map_start) + start = map_start; }else - start =ISA_END_ADDRESS ; + start = map_start; new_mapped_ram_size =init_range_memory_mapping (start, last_start); last_start = start;
@@ -453,8 +454,27 @@ void __init init_mem_mapping(void) mapped_ram_size += new_mapped_ram_size; } -if (real_end < end) -init_range_memory_mapping (real_end, end); +if (real_end < map_end) +init_range_memory_mapping (real_end, map_end); +} + +void __init init_mem_mapping (void ) +{ +unsigned long end; + +probe_page_size_mask (); + +#ifdef CONFIG_X86_64 + end = max_pfn <<PAGE_SHIFT ; +#else + end = max_low_pfn <<PAGE_SHIFT ; +#endif + +/* the ISA range is always mapped regardless of memory holes */ +init_memory_mapping (0 ,ISA_END_ADDRESS ); + +/* setup direct mapping for range [ISA_END_ADDRESS, end) in top-down*/ +memory_map_top_down (ISA_END_ADDRESS , end);#ifdef CONFIG_X86_64 if (max_pfn > max_low_pfn) {
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v7 4/6 x86/mem-hotplug: Support initialize page tables in bottom-up
In reply to Zhang YanfeiFrom: Tang Chen <[email protected]>
The Linux kernel cannot migrate pages used by the kernel. As a result, kernel pages cannot be hot-removed. So we cannot allocate hotpluggable memory for the kernel.
In a memory hotplug system, any numa node the kernel resides in should be unhotpluggable. And for a modern server, each node could have at least 16GB memory. So memory around the kernel image is highly likely unhotpluggable.
ACPI SRAT (System Resource Affinity Table) contains the memory hotplug info. But before SRAT is parsed, memblock has already started to allocate memory for the kernel. So we need to prevent memblock from doing this.
So direct memory mapping page tables setup is the case. init_mem_mapping() is called before SRAT is parsed. To prevent page tables being allocated within hotpluggable memory, we will use bottom-up direction to allocate page tables from the end of kernel image to the higher memory.
Note:
As for allocating page tables in lower memory, TJ said:
[This is an optional behavior which is triggered by a very specific kernel boot param, which I suspect is gonna need to stick around to support memory hotplug in the current setup unless we add another layer of address translation to support memory hotplug.]
As for page tables may occupy too much lower memory if using 4K mapping (CONFIG_DEBUG_PAGEALLOC and CONFIG_KMEMCHECK both disable using >4k pages), TJ said:
[But as I said in the same paragraph, parsing SRAT earlier doesn't solve the problem in itself either. Ignoring the option if 4k mapping is required and memory consumption would be prohibitive should work, no? Something like that would be necessary if we're gonna worry about cases like this no matter how we implement it, but, frankly, I'm not sure this is something worth worrying about.]
--- arch/x86/mm/init.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 64 insertions(+), 2 deletions(-)
arch/x86/mm/init.c +64 -2
@@ -458,6 +458,51 @@ static void __init memory_map_top_down(unsigned long map_start,init_range_memory_mapping (real_end, map_end); } +/** +* memory_map_bottom_up - Map [map_start, map_end) bottom up +* @map_start: start address of the target memory range +* @map_end: end address of the target memory range +* +* This function will setup direct mapping for memory range +* [map_start, map_end) in bottom-up. Since we have limited the +* bottom-up allocation above the kernel, the page tables will +* be allocated just above the kernel and we map the memory +* in [map_start, map_end) in bottom-up. +*/ +static void __init memory_map_bottom_up (unsigned long map_start, +unsigned long map_end) +{ +unsigned long next, new_mapped_ram_size, start; +unsigned long mapped_ram_size =0 ; +/* step_size need to be small so pgt_buf from BRK could cover it */ +unsigned long step_size =PMD_SIZE ; + + start = map_start; + min_pfn_mapped = start >>PAGE_SHIFT ; + +/* +* We start from the bottom (@map_start) and go to the top (@map_end). +* The memblock_find_in_range() gets us a block of RAM from the +* end of RAM in [min_pfn_mapped, max_pfn_mapped) used as new pages +* for page table. +*/ +while (start < map_end) { +if (map_end - start > step_size) { + next =round_up (start +1 , step_size); +if (next > map_end) + next = map_end; + }else + next = map_end; + + new_mapped_ram_size =init_range_memory_mapping (start, next); + start = next; + +if (new_mapped_ram_size > mapped_ram_size) + step_size <<=STEP_SIZE_SHIFT ; + mapped_ram_size += new_mapped_ram_size; + } +} +void __init init_mem_mapping (void ) {unsigned long end;
@@ -473,8 +518,25 @@ void __init init_mem_mapping(void)/* the ISA range is always mapped regardless of memory holes */ init_memory_mapping (0 ,ISA_END_ADDRESS ); -/* setup direct mapping for range [ISA_END_ADDRESS, end) in top-down*/ -memory_map_top_down (ISA_END_ADDRESS , end); +/* +* If the allocation is in bottom-up direction, we setup direct mapping +* in bottom-up, otherwise we setup direct mapping in top-down. +*/ +if (memblock_bottom_up ()) { +unsigned long kernel_end =__pa_symbol (_end); + +/* +* we need two separate calls here. This is because we want to +* allocate page tables above the kernel. So we first map +* [kernel_end, end) to make memory above the kernel be mapped +* as soon as possible. And then use page tables allocated above +* the kernel to map [ISA_END_ADDRESS, kernel_end). +*/ +memory_map_bottom_up (kernel_end, end); +memory_map_bottom_up (ISA_END_ADDRESS , kernel_end); + }else { +memory_map_top_down (ISA_END_ADDRESS , end); + }#ifdef CONFIG_X86_64 if (max_pfn > max_low_pfn) {
Signature · 1 line
1.7.1
PATCH v7 5/6 x86, acpi, crash, kdump: Do reserve_crashkernel() after SRAT is parsed.
In reply to Zhang YanfeiFrom: Tang Chen <[email protected]>
Memory reserved for crashkernel could be large. So we should not allocate this memory bottom up from the end of kernel image.
When SRAT is parsed, we will be able to know whihc memory is hotpluggable, and we can avoid allocating this memory for the kernel. So reorder reserve_crashkernel() after SRAT is parsed.
--- arch/x86/kernel/setup.c | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +7 -2
@@ -1120,8 +1120,6 @@ void __init setup_arch(char **cmdline_p)acpi_initrd_override ((void *)initrd_start, initrd_end - initrd_start);#endif -reserve_crashkernel (); -vsmp_init ();io_delay_init ();
@@ -1134,6 +1132,13 @@ void __init setup_arch(char **cmdline_p)early_acpi_boot_init ();initmem_init (); + +/* +* Reserve memory for crash kernel after SRAT is parsed so that it +* won't consume hotpluggable memory. +*/ +reserve_crashkernel (); +memblock_find_dma_reserve ();#ifdef CONFIG_KVM_GUEST
Signature · 1 line
1.7.1
PATCH v7 6/6 mem-hotplug: Introduce movable_node boot option
In reply to Zhang YanfeiFrom: Tang Chen <[email protected]>
The hot-Pluggable field in SRAT specifies which memory is hotpluggable. As we mentioned before, if hotpluggable memory is used by the kernel, it cannot be hot-removed. So memory hotplug users may want to set all hotpluggable memory in ZONE_MOVABLE so that the kernel won't use it.
Memory hotplug users may also set a node as movable node, which has ZONE_MOVABLE only, so that the whole node can be hot-removed.
But the kernel cannot use memory in ZONE_MOVABLE. By doing this, the kernel cannot use memory in movable nodes. This will cause NUMA performance down. And other users may be unhappy.
So we need a way to allow users to enable and disable this functionality. In this patch, we introduce movable_node boot option to allow users to choose to not to consume hotpluggable memory at early boot time and later we can set it as ZONE_MOVABLE.
To achieve this, the movable_node boot option will control the memblock allocation direction. That said, after memblock is ready, before SRAT is parsed, we should allocate memory near the kernel image as we explained in the previous patches. So if movable_node boot option is set, the kernel does the following:
1. After memblock is ready, make memblock allocate memory bottom up.
2. After SRAT is parsed, make memblock behave as default, allocate memory top down.
Users can specify "movable_node" in kernel commandline to enable this functionality. For those who don't use memory hotplug or who don't want to lose their NUMA performance, just don't specify anything. The kernel will work as before.
--- Documentation/kernel-parameters.txt | 3 +++ arch/x86/mm/numa.c | 11 +++++++++++ mm/Kconfig | 17 ++++++++++++----- mm/memory_hotplug.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-)
Documentation/kernel-parameters.txt +3 -0
@@ -1773,6 +1773,9 @@ bytes respectively. Such letter suffixes can also be entirely omitted. that the amount of memory usable for all allocations is not too small. + movable_node [KNL,X86] Boot-time switch to enable the effects + of CONFIG_MOVABLE_NODE=y. See mm/Kconfig for details. + MTD_Partition= [MTD] Format: <name>,<region-number>,<size>,<offset>
arch/x86/mm/numa.c +11 -0
@@ -567,6 +567,17 @@ static int __init numa_init(int (*init_func)(void)) ret =init_func ();if (ret <0 )return ret; + +/* +* We reset memblock back to the top-down direction +* here because if we configured ACPI_NUMA, we have +* parsed SRAT in init_func(). It is ok to have the +* reset here even if we did't configure ACPI_NUMA +* or acpi numa init fails and fallbacks to dummy +* numa init. +*/ +memblock_set_bottom_up (false); + ret =numa_cleanup_meminfo (&numa_meminfo);if (ret <0 )return ret;
mm/Kconfig +12 -5
@@ -153,11 +153,18 @@ config MOVABLE_NODE help Allow a node to have only movable memory. Pages used by the kernel, such as direct mapping pages cannot be migrated. So the corresponding - memory device cannot be hotplugged. This option allows users to - online all the memory of a node as movable memory so that the whole - node can be hotplugged. Users who don't use the memory hotplug - feature are fine with this option on since they don't online memory - as movable. + memory device cannot be hotplugged. This option allows the following + two things: + - When the system is booting, node full of hotpluggable memory can + be arranged to have only movable memory so that the whole node can + be hot-removed. (need movable_node boot option specified). + - After the system is up, the option allows users to online all the + memory of a node as movable memory so that the whole node can be + hot-removed. + + Users who don't use the memory hotplug feature are fine with this + option on since they don't specify movable_node boot option or they + don't online memory as movable. Say Y here if you want to hotplug a whole node. Say N here if you want kernel to use memory on all nodes evenly.
mm/memory_hotplug.c +31 -0
@@ -31,6 +31,7 @@#include <linux/firmware-map.h> #include <linux/stop_machine.h> #include <linux/hugetlb.h> +#include <linux/memblock.h> #include <asm/tlbflush.h>
@@ -1412,6 +1413,36 @@ static bool can_offline_normal(struct zone *zone, unsigned long nr_pages) }#endif /* CONFIG_MOVABLE_NODE */ +static int __init cmdline_parse_movable_node (char *p) +{ +#ifdef CONFIG_MOVABLE_NODE +/* +* Memory used by the kernel cannot be hot-removed because Linux +* cannot migrate the kernel pages. When memory hotplug is +* enabled, we should prevent memblock from allocating memory +* for the kernel. +* +* ACPI SRAT records all hotpluggable memory ranges. But before +* SRAT is parsed, we don't know about it. +* +* The kernel image is loaded into memory at very early time. We +* cannot prevent this anyway. So on NUMA system, we set any +* node the kernel resides in as un-hotpluggable. +* +* Since on modern servers, one node could have double-digit +* gigabytes memory, we can assume the memory around the kernel +* image is also un-hotpluggable. So before SRAT is parsed, just +* allocate memory near the kernel image to try the best to keep +* the kernel away from hotpluggable memory. +*/ +memblock_set_bottom_up (true); +#else +pr_warn ("movable_node option not supported\n" ); +#endif +return 0 ; +} +early_param ("movable_node" , cmdline_parse_movable_node); +/* check which state of node_states will be changed when offline memory */ static void node_states_check_changes_offline (unsigned long nr_pages,struct zone *zone,struct memory_notify *arg)
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Hello Andrew,
Could you take this version now? Since the approach of this patchset is suggested by tejun, and thanks him for helping us explaining a lot to guys that have the concern about the page table location. I've added some note in the patch4 description to explain why we could be not worrisome about the approach.
Thanks.
Quoted 165 lines · Zhang Yanfei
Hello, here is the v7 version. Any comments are welcome!
The v7 version is based on linus's tree (3.12-rc4) HEAD is:
commitd0e639c9e06d44e713170031fe05fb60ebe680afAuthor: Linus Torvalds <[email protected]> Date: Sun Oct 6 14:00:20 2013 -0700Linux 3.12-rc4[Problem]
The current Linux cannot migrate pages used by the kerenl because of the kernel direct mapping. In Linux kernel space, va = pa +
PAGE_OFFSET. When the pa is changed, we cannot simply update the pagetable and keep the va unmodified. So the kernel pages are not migratable.There are also some other issues will cause the kernel pages not migratable. For example, the physical address may be cached somewhere and will be used. It is not to update all the caches.
When doing memory hotplug in Linux, we first migrate all the pages in one memory device somewhere else, and then remove the device. But if pages are used by the kernel, they are not migratable. As a result, memory used by the kernel cannot be hot-removed.
Modifying the kernel direct mapping mechanism is too difficult to do. And it may cause the kernel performance down and unstable. So we use the following way to do memory hotplug.
[What we are doing]
In Linux, memory in one numa node is divided into several zones. One of the zones is
ZONE_MOVABLE, which the kernel won't use.In order to implement memory hotplug in Linux, we are going to arrange all hotpluggable memory in
ZONE_MOVABLEso that the kernel won't use these memory. To do this, we need ACPI's help.In ACPI, SRAT(System Resource Affinity Table) contains NUMA info. The memory affinities in SRAT record every memory range in the system, and also, flags specifying if the memory range is hotpluggable.
(Please refer to ACPI spec 5.0 5.2.16)With the help of SRAT, we have to do the following two things to achieve our goal:
1. When doing memory hot-add, allow the users arranging hotpluggable as
ZONE_MOVABLE.
(This has been done by theMOVABLE_NODEfunctionality in Linux.)2. when the system is booting, prevent bootmem allocator from allocating hotpluggable memory for the kernel before the memory initialization finishes.
The problem 2 is the key problem we are going to solve. But before solving it, we need some preparation. Please see below.
[Preparation]
Bootloader has to load the kernel image into memory. And this memory must be unhotpluggable. We cannot prevent this anyway. So in a memory hotplug system, we can assume any node the kernel resides in is not hotpluggable.
Before SRAT is parsed, we don't know which memory ranges are hotpluggable. But memblock has already started to work. In the current kernel, memblock allocates the following memory before SRAT is parsed:
setup_arch()|->memblock_x86_fill() /* memblock is ready */ |...... |->early_reserve_e820_mpc_new() /* allocate memory under 1MB */ |->reserve_real_mode() /* allocate memory under 1MB */ |->init_mem_mapping() /* allocate page tables, about 2MB to map 1GB memory */ |->dma_contiguous_reserve() /* specified by user, should be low */ |->setup_log_buf() /* specified by user, several mega bytes */ |->relocate_initrd() /* could be large, but will be freed after boot, should reorder */ |->acpi_initrd_override() /* several mega bytes */ |->reserve_crashkernel() /* could be large, should reorder */ |...... |->initmem_init() /* Parse SRAT */According to Tejun's advice, before SRAT is parsed, we should try our best to allocate memory near the kernel image. Since the whole node the kernel resides in won't be hotpluggable, and for a modern server, a node may have at least 16GB memory, allocating several mega bytes memory around the kernel image won't cross to hotpluggable memory.
[About this patch-set]
So this patch-set is the preparation for the problem 2 that we want to solve. It does the following:
1. Make memblock be able to allocate memory bottom up.
1) Keep all the memblock APIs' prototype unmodified.
2) When the direction is bottom up, keep the start address greater than the end of kernel image.2. Improve
init_mem_mapping()to support allocate page tables in bottom up direction.3. Introduce "
movable_node" boot option to enable and disable this functionality.Change log v6 -> v7:
1. Add toshi's ack in several patches.
2. Make__pa_symbol()available everywhere by putting a pesudo__pa_symboldefine ininclude/linux/mm.h. Thanks HPA.
3. Add notes about the page table allocation in bottom-up.Change log v5 -> v6:
1. Add tejun and toshi's ack in several patches.
2. Change movablenode tomovable_nodeboot option and update the description formovable_nodeandCONFIG_MOVABLE_NODE. Thanks Ingo!
3. Fix the__pa_symbol()issue pointed by Andrew Morton.
4. Update some functions' comments and names.Change log v4 -> v5:
1. Change memblock.current_direction to a boolean memblock.bottom_up. And remove the direction enum.
2. Update and add some comments to explain things clearer.
3. Misc fixes, such as removing unnecessary #ifdefChange log v3 -> v4:
1. Use bottom-up/top-down to unify things. Thanks tj.
2. Factor out of current top-down implementation and then introduce bottom-up mode, not mixing them in one patch. Thanks tj.
3. Changes function naming:memblock_direction_bottom_up->memblock_bottom_up
4. Usememblock_set_bottom_upto replacememblock_set_current_direction, which makes the code simpler. Thanks tj.
5. Define two implementions of functionmemblock_bottom_upandmemblock_set_bottom_upin order not to use #ifdef in the boot code. Thanks tj.
6. Add comments to explain why retry top-down allocation whenbottom_upallocation failed. Thanks tj and toshi!Change log v2 -> v3:
1. According to Toshi's suggestion, move the direction checking logic into memblock. And simply the code more.Change log v1 -> v2:
1. According to tj's suggestion, implemented a new functionmemblock_alloc_bottom_up()to allocate memory from bottom upwards, whihc can simplify the code.Tang Chen (6):
memblock: Factor out of top-down allocation
memblock: Introduce bottom-up allocation mode
x86/mm: Factor out of top-down direct mapping setup x86/mem-hotplug: Support initialize page tables in bottom-up x86, acpi, crash, kdump: Doreserve_crashkernel()after SRAT is
parsed.
mem-hotplug: Introducemovable_nodeboot optionDocumentation/kernel-parameters.txt | 3 + arch/x86/kernel/setup.c | 9 ++- arch/x86/mm/init.c | 122 ++++++++++++++++++++++++++++------ arch/x86/mm/numa.c | 11 +++ include/linux/memblock.h | 24 +++++++ include/linux/mm.h | 4 + mm/Kconfig | 17 +++-- mm/memblock.c | 126 +++++++++++++++++++++++++++++++---- mm/memory_hotplug.c | 31 +++++++++ 9 files changed, 306 insertions(+), 41 deletions(-)
Signature · 2 lines
Thanks. Zhang Yanfei