mm: hugetlb: optionally allocate gigantic hugepages using cma
Subsystems (4)
PATCH 1/1 mm: hugetlb: optionally allocate gigantic hugepages using cma
Commit 944d9fec8d7a ("hugetlb: add support for gigantic page allocation at runtime") has added the run-time allocation of gigantic pages. However it actually works only at early stages of the system loading, when the majority of memory is free. After some time the memory gets fragmented by non-movable pages, so the chances to find a contiguous 1 GB block are getting close to zero. Even dropping caches manually doesn't help a lot.
At large scale rebooting servers in order to allocate gigantic hugepages is quite expensive and complex. At the same time keeping some constant percentage of memory in reserved hugepages even if the workload isn't using it is a big waste: not all workloads can benefit from using 1 GB pages.
The following solution can solve the problem:
1) On boot time a dedicated cma area* is reserved. The size is passed as a kernel argument.
2) Run-time allocations of gigantic hugepages are performed using the cma allocator and the dedicated cma area
In this case gigantic hugepages can be allocated successfully with a high probability, however the memory isn't completely wasted if nobody is using 1GB hugepages: it can be used for pagecache, anon memory, THPs, etc.
* On a multi-node machine a per-node cma area is allocated on each node. Following gigantic hugetlb allocation are using the first available numa node if the mask isn't specified by a user.
Usage:
1) configure the kernel to allocate a cma area for hugetlb allocations:
pass hugetlb_cma=10G as a kernel argument
2) allocate hugetlb pages as usual, e.g.
echo 10 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
If the option isn't enabled or the allocation of the cma area failed, the current behavior of the system is preserved.
Only x86 is covered by this patch, but it's trivial to extend it to cover other architectures as well.
--- .../admin-guide/kernel-parameters.txt | 7 ++ arch/x86/kernel/setup.c | 3 + include/linux/hugetlb.h | 2 + mm/hugetlb.c | 108 ++++++++++++++++++ 4 files changed, 120 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +7 -0
@@ -1452,6 +1452,13 @@ hpet_mmap= [X86, HPET_MMAP] Allow userspace to mmap HPET registers. Default set by CONFIG_HPET_MMAP_DEFAULT. + hugetlb_cma= [x86-64] The size of a cma area used for allocation + of gigantic hugepages. + Format: nn[GTPE] | nn% + + If enabled, boot-time allocation of gigantic hugepages + is skipped. + hugepages= [HW,X86-32,IA-64] HugeTLB pages to allocate at boot. hugepagesz= [HW,IA-64,PPC,X86-64] The size of the HugeTLB pages. On x86-64 and powerpc, this option can be specified
arch/x86/kernel/setup.c +3 -0
@@ -16,6 +16,7 @@#include <linux/pci.h> #include <linux/root_dev.h> #include <linux/sfi.h> +#include <linux/hugetlb.h> #include <linux/tboot.h> #include <linux/usb/xhci-dbgp.h>
@@ -1158,6 +1159,8 @@ void __init setup_arch(char **cmdline_p)initmem_init ();dma_contiguous_reserve (max_pfn_mapped <<PAGE_SHIFT ); +hugetlb_cma_reserve (); +/* * Reserve memory for crash kernel after SRAT is parsed so that it * won't consume hotpluggable memory.
include/linux/hugetlb.h +2 -0
@@ -157,6 +157,8 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud);extern int sysctl_hugetlb_shm_group;extern struct list_head huge_boot_pages; +extern void __init hugetlb_cma_reserve (void ); +/* arch callbacks */ pte_t *huge_pte_alloc(struct mm_struct *mm,
mm/hugetlb.c +108 -0
141 lines not on this page · raw message
Signature · 1 line
2.24.1
Andrew Morton
maintainerQuoted 33 lines · On Mon, 9 Mar 2020 15:32:16 -0700 Roman Gushchin <[email protected]> wrote:
Commit
944d9fec8d7a("hugetlb: add support for gigantic page allocation at runtime") has added the run-time allocation of gigantic pages. However it actually works only at early stages of the system loading, when the majority of memory is free. After some time the memory gets fragmented by non-movable pages, so the chances to find a contiguous 1 GB block are getting close to zero. Even dropping caches manually doesn't help a lot.At large scale rebooting servers in order to allocate gigantic hugepages is quite expensive and complex. At the same time keeping some constant percentage of memory in reserved hugepages even if the workload isn't using it is a big waste: not all workloads can benefit from using 1 GB pages.
The following solution can solve the problem:
1) On boot time a dedicated cma area* is reserved. The size is passed as a kernel argument.
2) Run-time allocations of gigantic hugepages are performed using the cma allocator and the dedicated cma areaIn this case gigantic hugepages can be allocated successfully with a high probability, however the memory isn't completely wasted if nobody is using 1GB hugepages: it can be used for pagecache, anon memory, THPs, etc.
* On a multi-node machine a per-node cma area is allocated on each node. Following gigantic hugetlb allocation are using the first available numa node if the mask isn't specified by a user.
Usage:
1) configure the kernel to allocate a cma area for hugetlb allocations:
pass hugetlb_cma=10G as a kernel argument
Quoted 9 lines
2) allocate hugetlb pages as usual, e.g.
echo 10 >/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesIf the option isn't enabled or the allocation of the cma area failed, the current behavior of the system is preserved.
Only x86 is covered by this patch, but it's trivial to extend it to cover other architectures as well.
Sounds promising.
I'm not seeing any dependencies on CONFIG_CMA in there. Does the code actually compile if CONFIG_CMA=n? If yes, then does it add unneeded bloat?
Quoted 45 lines · Andrew Morton
Quoted 33 lines · On Mon, 9 Mar 2020 15:32:16 -0700 Roman Gushchin <[email protected]> wrote:
Commit
944d9fec8d7a("hugetlb: add support for gigantic page allocation at runtime") has added the run-time allocation of gigantic pages. However it actually works only at early stages of the system loading, when the majority of memory is free. After some time the memory gets fragmented by non-movable pages, so the chances to find a contiguous 1 GB block are getting close to zero. Even dropping caches manually doesn't help a lot.At large scale rebooting servers in order to allocate gigantic hugepages is quite expensive and complex. At the same time keeping some constant percentage of memory in reserved hugepages even if the workload isn't using it is a big waste: not all workloads can benefit from using 1 GB pages.
The following solution can solve the problem:
1) On boot time a dedicated cma area* is reserved. The size is passed as a kernel argument.
2) Run-time allocations of gigantic hugepages are performed using the cma allocator and the dedicated cma areaIn this case gigantic hugepages can be allocated successfully with a high probability, however the memory isn't completely wasted if nobody is using 1GB hugepages: it can be used for pagecache, anon memory, THPs, etc.
* On a multi-node machine a per-node cma area is allocated on each node. Following gigantic hugetlb allocation are using the first available numa node if the mask isn't specified by a user.
Usage:
1) configure the kernel to allocate a cma area for hugetlb allocations:
pass hugetlb_cma=10G as a kernel argumentQuoted 9 lines
2) allocate hugetlb pages as usual, e.g.
echo 10 >/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesIf the option isn't enabled or the allocation of the cma area failed, the current behavior of the system is preserved.
Only x86 is covered by this patch, but it's trivial to extend it to cover other architectures as well.
Quoted 5 lines
Sounds promising.
I'm not seeing any dependencies on
CONFIG_CMAin there. Does the code actually compile ifCONFIG_CMA=n? If yes, then does it add unneeded bloat?
Good question. Let me double-check it.
Thanks!
kbuild test robot
build botThank you for the patch! Yet something to improve:
Hi Roman,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on v5.6-rc4]
[also build test ERROR on next-20200306]
[cannot apply to mmotm/master tip/x86/core linux/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Roman-Gushchin/mm-hugetlb-optionally-allocate-gigantic-hugepages-using-cma/20200310-071026 base: 98d54f81e36ba3bf92172791eba5ca5bd813989b config: i386-tinyconfig (attached as .config) compiler: gcc-7 (Debian 7.5.0-5) 7.5.0 reproduce: # save the attached .config to linux build tree make ARCH=i386
If you fix the issue, kindly add following tag
All errors (new ones prefixed by >>):
arch/x86/kernel/setup.c: In function 'setup_arch':
>> arch/x86/kernel/setup.c:1162:2: error: implicit declaration of function 'hugetlb_cma_reserve'; did you mean 'set_dma_reserve'? [-Werror=implicit-function-declaration]
hugetlb_cma_reserve();
^~~~~~~~~~~~~~~~~~~
set_dma_reservecc1: some warnings being treated as errors
vim +1162 arch/x86/kernel/setup.c
1094
1095 reserve_real_mode();
1096
1097 trim_platform_memory_ranges();
1098 trim_low_memory_range();
1099
1100 init_mem_mapping();
1101
1102 idt_setup_early_pf();
1103
1104 /*
1105 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features)
1106 * with the current CR4 value. This may not be necessary, but
1107 * auditing all the early-boot CR4 manipulation would be needed to
1108 * rule it out.
1109 *
1110 * Mask off features that don't work outside long mode (just
1111 * PCIDE for now).
1112 */
1113 mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE;
1114
1115 memblock_set_current_limit(get_max_mapped());
1116
1117 /*
1118 * NOTE: On x86-32, only from this point on, fixmaps are ready for use.
1119 */
1120
1121 #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
1122 if (init_ohci1394_dma_early)
1123 init_ohci1394_dma_on_all_controllers();
1124 #endif
1125 /* Allocate bigger log buffer */
1126 setup_log_buf(1);
1127
1128 if (efi_enabled(EFI_BOOT)) {
1129 switch (boot_params.secure_boot) {
1130 case efi_secureboot_mode_disabled:
1131 pr_info("Secure boot disabled\n");
1132 break;
1133 case efi_secureboot_mode_enabled:
1134 pr_info("Secure boot enabled\n");
1135 break;
1136 default:
1137 pr_info("Secure boot could not be determined\n");
1138 break;
1139 }
1140 }
1141
1142 reserve_initrd();
1143
1144 acpi_table_upgrade();
1145
1146 vsmp_init();
1147
1148 io_delay_init();
1149
1150 early_platform_quirks();
1151
1152 /*
1153 * Parse the ACPI tables for possible boot-time SMP configuration.
1154 */
1155 acpi_boot_table_init();
1156
1157 early_acpi_boot_init();
1158
1159 initmem_init();
1160 dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT);
1161
> 1162 hugetlb_cma_reserve();
1163
1164 /*
1165 * Reserve memory for crash kernel after SRAT is parsed so that it
1166 * won't consume hotpluggable memory.
1167 */
1168 reserve_crashkernel();
1169
1170 memblock_find_dma_reserve();
1171
1172 if (!early_xdbc_setup_hardware())
1173 early_xdbc_register_console();
1174
1175 x86_init.paging.pagetable_init();
1176
1177 kasan_init();
1178
1179 /*
1180 * Sync back kernel address range.
1181 *
1182 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
1183 * this call?
1184 */
1185 sync_initial_page_table();
1186
1187 tboot_probe();
1188
1189 map_vsyscall();
1190
1191 generic_apic_probe();
1192
1193 early_quirks();
1194
1195 /*
1196 * Read APIC and some other early information from ACPI tables.
1197 */
1198 acpi_boot_init();
1199 sfi_init();
1200 x86_dtb_init();
1201
1202 /*
1203 * get boot-time SMP configuration:
1204 */
1205 get_smp_config();
1206
1207 /*
1208 * Systems w/o ACPI and mptables might not have it mapped the local
1209 * APIC yet, but prefill_possible_map() might need to access it.
1210 */
1211 init_apic_mappings();
1212
1213 prefill_possible_map();
1214
1215 init_cpu_to_node();
1216
1217 io_apic_init_mappings();
1218
1219 x86_init.hyper.guest_late_init();
1220
1221 e820__reserve_resources();
1222 e820__register_nosave_regions(max_pfn);
1223
1224 x86_init.resources.reserve_resources();
1225
1226 e820__setup_pci_gap();
1227
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org