mm: cma: NUMA node interface
Subsystems (7)
PATCH 1/2 mm: cma: NUMA node interface
I've noticed that there is no interfaces exposed by CMA which would let me to declare contigous memory on particular NUMA node.
This patchset adds the ability to try to allocate contiguous memory on specific node.
Implement a new method for declaring contigous memory on particular node and keep cma_declare_contiguous() as a wrapper.
--- include/linux/cma.h | 14 ++++++++++++-- include/linux/memblock.h | 3 +++ mm/cma.c | 15 ++++++++------- mm/memblock.c | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-)
include/linux/cma.h +12 -2
@@ -24,10 +24,20 @@ extern phys_addr_t cma_get_base(const struct cma *cma);extern unsigned long cma_get_size (const struct cma *cma);extern const char *cma_get_name (const struct cma *cma); -extern int __init cma_declare_contiguous (phys_addr_t base, +extern int __init cma_declare_contiguous_nid (phys_addr_t base,phys_addr_t size,phys_addr_t limit,phys_addr_t alignment,unsigned int order_per_bit, -bool fixed,const char *name,struct cma **res_cma); +bool fixed,const char *name,struct cma **res_cma, +int nid); +static inline int __init cma_declare_contiguous (phys_addr_t base, +phys_addr_t size,phys_addr_t limit, +phys_addr_t alignment,unsigned int order_per_bit, +bool fixed,const char *name,struct cma **res_cma) + { +return cma_declare_contiguous_nid (base, size, + limit, alignment, order_per_bit, + fixed, name, res_cma,NUMA_NO_NODE ); + } extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, unsigned int order_per_bit, const char *name,
include/linux/memblock.h +3 -0
@@ -348,6 +348,9 @@ static inline int memblock_get_region_node(const struct memblock_region *r)phys_addr_t memblock_phys_alloc_range (phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end); +phys_addr_t memblock_alloc_range_nid (phys_addr_t size, +phys_addr_t align,phys_addr_t start, +phys_addr_t end,int nid,bool exact_nid);phys_addr_t memblock_phys_alloc_try_nid (phys_addr_t size,phys_addr_t align,int nid); static inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
mm/cma.c +8 -7
@@ -220,7 +220,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, }/** -* cma_declare_contiguous() - reserve custom contiguous area +* cma_declare_contiguous_nid() - reserve custom contiguous area * @base: Base address of the reserved area optional, use 0 for any * @size: Size of the reserved area (in bytes), * @limit: End address of the reserved memory (optional, 0 for any).
@@ -229,6 +229,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,* @fixed: hint about where to place the reserved area * @name: The name of the area. See function cma_init_reserved_mem() * @res_cma: Pointer to store the created cma region. +* @nid: nid of the free area to find, %NUMA_NO_NODE for any node * * This function reserves memory from early allocator. It should be * called by arch specific code once the early allocator (memblock or bootmem)
@@ -238,10 +239,10 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,* If @fixed is true, reserve contiguous area at exactly @base. If false, * reserve in range from @base to @limit. */ -int __init cma_declare_contiguous (phys_addr_t base, +int __init cma_declare_contiguous_nid (phys_addr_t base,phys_addr_t size,phys_addr_t limit,phys_addr_t alignment,unsigned int order_per_bit, -bool fixed,const char *name,struct cma **res_cma) +bool fixed,const char *name,struct cma **res_cma,int nid) {phys_addr_t memblock_end =memblock_end_of_DRAM ();phys_addr_t highmem_start;
@@ -336,14 +337,14 @@ int __init cma_declare_contiguous(phys_addr_t base,* memory in case of failure. */ if (base < highmem_start && limit > highmem_start) { - addr =memblock_phys_alloc_range (size, alignment, - highmem_start, limit); + addr =memblock_alloc_range_nid (size, alignment, + highmem_start, limit, nid, false); limit = highmem_start; }if (!addr) { - addr =memblock_phys_alloc_range (size, alignment, base, - limit); + addr =memblock_alloc_range_nid (size, alignment, base, + limit, nid, false);if (!addr) { ret = -ENOMEM ; goto err;
mm/memblock.c +1 -1
@@ -1349,7 +1349,7 @@ __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,* Return: * Physical address of allocated memory block on success, %0 on failure. */ -static phys_addr_t __init memblock_alloc_range_nid (phys_addr_t size, +phys_addr_t __init memblock_alloc_range_nid (phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end,int nid,bool exact_nid)
Signature · 1 line
2.17.1
PATCH 2/2 mm: hugetlb: Use node interface of cma
With introduction of numa node interface for CMA, this patch is for using that interface for allocating memory on numa nodes if NUMA is configured. This will be more efficient and cleaner because first, instead of iterating mem range of each numa node, cma_declare_contigueous_nid() will do its own address finding if we pass 0 for both min_pfn and max_pfn, second, it can also handle caseswhere NUMA is not configured
by passing NUMA_NO_NODE as an argument.
In addition, checking if desired size of memory is available or not, is happening in cma_declare_contiguous_nid() because base and limit will be determined there, since 0(any) for base and 0(any) for limit is passed as argument to the function.
--- mm/hugetlb.c | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-)
mm/hugetlb.c +11 -29
@@ -5573,42 +5573,24 @@ void __init hugetlb_cma_reserve(int order) reserved =0 ;for_each_node_state (nid,N_ONLINE ) { -unsigned long min_pfn =0 , max_pfn =0 ;int res; -#ifdef CONFIG_NUMA -unsigned long start_pfn, end_pfn; -int i; -for_each_mem_pfn_range (i, nid, &start_pfn, &end_pfn,NULL ) { -if (!min_pfn) - min_pfn = start_pfn; - max_pfn = end_pfn; - } -#else - min_pfn = min_low_pfn; - max_pfn = max_low_pfn; -#endif size =min (per_node, hugetlb_cma_size - reserved); size =round_up (size,PAGE_SIZE << order); - -if (size > ((max_pfn - min_pfn) <<PAGE_SHIFT ) /2 ) { -pr_warn ("hugetlb_cma: cma_area is too big, please try less than %lu MiB\n" , -round_down (((max_pfn - min_pfn) <<PAGE_SHIFT ) * - nr_online_nodes /2 /SZ_1M , -PAGE_SIZE << order)); -break ; - } - - res =cma_declare_contiguous (PFN_PHYS (min_pfn), size, -PFN_PHYS (max_pfn), + + +#ifndef CONFIG_NUMA + nid = NUMA_NO_NODE +#endif + res =cma_declare_contiguous_nid (0 , size, +0 ,PAGE_SIZE << order,0 , false, -"hugetlb" , &hugetlb_cma[nid]); +"hugetlb" , &hugetlb_cma[nid], nid); +if (res) { -phys_addr_t begpa =PFN_PHYS (min_pfn); -phys_addr_t endpa =PFN_PHYS (max_pfn); -pr_warn ("%s: reservation failed: err %d, node %d, [%pap, %pap)\n" , - __func__, res, nid, &begpa, &endpa); +pr_warn ("%s: reservation failed: err %d, node %d\n" , + __func__, res, nid);break ; }
Signature · 1 line
2.17.1
Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
Quoted 8 lines · Aslan Bakirov
With introduction of numa node interface for CMA, this patch is for using that interface for allocating memory on numa nodes if NUMA is configured. This will be more efficient and cleaner because first, instead of iterating mem range of each numa node,
cma_declare_contigueous_nid()will do its own address finding if we pass 0 for bothmin_pfnandmax_pfn, second, it can also handle caseswhere NUMA is not configured
by passingNUMA_NO_NODEas an argument.
Quoted 4 lines
In addition, checking if desired size of memory is available or not, is happening in
cma_declare_contiguous_nid()because base and limit will be determined there, since 0(any) for base and 0(any) for limit is passed as argument to the function.
This looks much better than the original patch. Can we simply squash your and Roman's patch in the mmotm tree and post it for the review in one piece? It would be slightly easier to review that way.
Quoted 1 line
Signed-off-by Aslan Bakirov <[email protected]>
Thanks!
Quoted 64 lines
--- mm/hugetlb.c | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-)mm/hugetlb.c +11
-29@@ -5573,42 +5573,24 @@ void __init hugetlb_cma_reserve(int order) reserved =0 ;for_each_node_state (nid,N_ONLINE ) { -unsigned long min_pfn =0 , max_pfn =0 ;int res; -#ifdef CONFIG_NUMA -unsigned long start_pfn, end_pfn; -int i; -for_each_mem_pfn_range (i, nid, &start_pfn, &end_pfn,NULL ) { -if (!min_pfn) - min_pfn = start_pfn; - max_pfn = end_pfn; - } -#else - min_pfn = min_low_pfn; - max_pfn = max_low_pfn; -#endif size =min (per_node, hugetlb_cma_size - reserved); size =round_up (size,PAGE_SIZE << order); - -if (size > ((max_pfn - min_pfn) <<PAGE_SHIFT ) /2 ) { -pr_warn ("hugetlb_cma: cma_area is too big, please try less than %lu MiB\n" , -round_down (((max_pfn - min_pfn) <<PAGE_SHIFT ) * - nr_online_nodes /2 /SZ_1M , -PAGE_SIZE << order)); -break ; - } - - res =cma_declare_contiguous (PFN_PHYS (min_pfn), size, -PFN_PHYS (max_pfn), + + +#ifndef CONFIG_NUMA + nid = NUMA_NO_NODE +#endif + res =cma_declare_contiguous_nid (0 , size, +0 ,PAGE_SIZE << order,0 , false, -"hugetlb" , &hugetlb_cma[nid]); +"hugetlb" , &hugetlb_cma[nid], nid); +if (res) { -phys_addr_t begpa =PFN_PHYS (min_pfn); -phys_addr_t endpa =PFN_PHYS (max_pfn); -pr_warn ("%s: reservation failed: err %d, node %d, [%pap, %pap)\n" , - __func__, res, nid, &begpa, &endpa); +pr_warn ("%s: reservation failed: err %d, node %d\n" , + __func__, res, nid);break ; }Signature · 1 line
2.17.1
Signature · 2 lines
Michal Hocko SUSE Labs
Roman Gushchin
Acked-byRe: [PATCH 2/2] mm: hugetlb: Use node interface of cma
Quoted 9 lines · Michal Hocko
Quoted 8 lines · Aslan Bakirov
With introduction of numa node interface for CMA, this patch is for using that interface for allocating memory on numa nodes if NUMA is configured. This will be more efficient and cleaner because first, instead of iterating mem range of each numa node,
cma_declare_contigueous_nid()will do its own address finding if we pass 0 for bothmin_pfnandmax_pfn, second, it can also handle caseswhere NUMA is not configured
by passingNUMA_NO_NODEas an argument.
Quoted 8 lines
Quoted 4 lines
In addition, checking if desired size of memory is available or not, is happening in
cma_declare_contiguous_nid()because base and limit will be determined there, since 0(any) for base and 0(any) for limit is passed as argument to the function.This looks much better than the original patch. Can we simply squash your and Roman's patch in the mmotm tree and post it for the review in one piece? It would be slightly easier to review that way.
I'm glad you liked it! I agree, it's much nicer now, thanks to Aslan!
I think it's simpler to keep it as a separate patch, because there was already a fix by Randy Dunlap on top of my original version.
Quoted 2 lines
Quoted 2 lines
Signed-off-by Aslan Bakirov <[email protected]>
Thanks!
Quoted 71 lines
Thanks!
Quoted 64 lines
--- mm/hugetlb.c | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-)mm/hugetlb.c +11
-29@@ -5573,42 +5573,24 @@ void __init hugetlb_cma_reserve(int order) reserved =0 ;for_each_node_state (nid,N_ONLINE ) { -unsigned long min_pfn =0 , max_pfn =0 ;int res; -#ifdef CONFIG_NUMA -unsigned long start_pfn, end_pfn; -int i; -for_each_mem_pfn_range (i, nid, &start_pfn, &end_pfn,NULL ) { -if (!min_pfn) - min_pfn = start_pfn; - max_pfn = end_pfn; - } -#else - min_pfn = min_low_pfn; - max_pfn = max_low_pfn; -#endif size =min (per_node, hugetlb_cma_size - reserved); size =round_up (size,PAGE_SIZE << order); - -if (size > ((max_pfn - min_pfn) <<PAGE_SHIFT ) /2 ) { -pr_warn ("hugetlb_cma: cma_area is too big, please try less than %lu MiB\n" , -round_down (((max_pfn - min_pfn) <<PAGE_SHIFT ) * - nr_online_nodes /2 /SZ_1M , -PAGE_SIZE << order)); -break ; - } - - res =cma_declare_contiguous (PFN_PHYS (min_pfn), size, -PFN_PHYS (max_pfn), + + +#ifndef CONFIG_NUMA + nid = NUMA_NO_NODE +#endif + res =cma_declare_contiguous_nid (0 , size, +0 ,PAGE_SIZE << order,0 , false, -"hugetlb" , &hugetlb_cma[nid]); +"hugetlb" , &hugetlb_cma[nid], nid); +if (res) { -phys_addr_t begpa =PFN_PHYS (min_pfn); -phys_addr_t endpa =PFN_PHYS (max_pfn); -pr_warn ("%s: reservation failed: err %d, node %d, [%pap, %pap)\n" , - __func__, res, nid, &begpa, &endpa); +pr_warn ("%s: reservation failed: err %d, node %d\n" , + __func__, res, nid);break ; }Signature · 1 line
2.17.1Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
Quoted 15 lines · Roman Gushchin
Quoted 14 lines · Michal Hocko
Quoted 12 lines · Aslan Bakirov
With introduction of numa node interface for CMA, this patch is for using that interface for allocating memory on numa nodes if NUMA is configured. This will be more efficient and cleaner because first, instead of iterating mem range of each numa node,
cma_declare_contigueous_nid()will do its own address finding if we pass 0 for bothmin_pfnandmax_pfn, second, it can also handle caseswhere NUMA is not configured
by passingNUMA_NO_NODEas an argument.In addition, checking if desired size of memory is available or not, is happening in
cma_declare_contiguous_nid()because base and limit will be determined there, since 0(any) for base and 0(any) for limit is passed as argument to the function.
Quoted 8 lines
Quoted 3 lines
This looks much better than the original patch. Can we simply squash your and Roman's patch in the mmotm tree and post it for the review in one piece? It would be slightly easier to review that way.
I'm glad you liked it! I agree, it's much nicer now, thanks to Aslan!
I think it's simpler to keep it as a separate patch, because there was already a fix by Randy Dunlap on top of my original version.
Why would be squashing all those into a single one be a problem. I will not insist of course but I would much rather see a single patch which is easy to review and doesn't contain any intermediate hacks to prevent from compile time issues than 3 patches with the last one removing hacks and a large part of the implementation. You can easily record the multiple authors can be easily recorded in the s-o-b chain.
Quoted 77 lines
Quoted 1 line
Quoted 1 line
Signed-off-by Aslan Bakirov <[email protected]>Acked-by Roman Gushchin <[email protected]>Thanks!
Quoted 71 lines
Thanks!
Quoted 64 lines
--- mm/hugetlb.c | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-)mm/hugetlb.c +11
-29@@ -5573,42 +5573,24 @@ void __init hugetlb_cma_reserve(int order) reserved =0 ;for_each_node_state (nid,N_ONLINE ) { -unsigned long min_pfn =0 , max_pfn =0 ;int res; -#ifdef CONFIG_NUMA -unsigned long start_pfn, end_pfn; -int i; -for_each_mem_pfn_range (i, nid, &start_pfn, &end_pfn,NULL ) { -if (!min_pfn) - min_pfn = start_pfn; - max_pfn = end_pfn; - } -#else - min_pfn = min_low_pfn; - max_pfn = max_low_pfn; -#endif size =min (per_node, hugetlb_cma_size - reserved); size =round_up (size,PAGE_SIZE << order); - -if (size > ((max_pfn - min_pfn) <<PAGE_SHIFT ) /2 ) { -pr_warn ("hugetlb_cma: cma_area is too big, please try less than %lu MiB\n" , -round_down (((max_pfn - min_pfn) <<PAGE_SHIFT ) * - nr_online_nodes /2 /SZ_1M , -PAGE_SIZE << order)); -break ; - } - - res =cma_declare_contiguous (PFN_PHYS (min_pfn), size, -PFN_PHYS (max_pfn), + + +#ifndef CONFIG_NUMA + nid = NUMA_NO_NODE +#endif + res =cma_declare_contiguous_nid (0 , size, +0 ,PAGE_SIZE << order,0 , false, -"hugetlb" , &hugetlb_cma[nid]); +"hugetlb" , &hugetlb_cma[nid], nid); +if (res) { -phys_addr_t begpa =PFN_PHYS (min_pfn); -phys_addr_t endpa =PFN_PHYS (max_pfn); -pr_warn ("%s: reservation failed: err %d, node %d, [%pap, %pap)\n" , - __func__, res, nid, &begpa, &endpa); +pr_warn ("%s: reservation failed: err %d, node %d\n" , + __func__, res, nid);break ; }Signature · 1 line
2.17.1Signature · 2 lines
Michal Hocko SUSE Labs
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
Quoted 20 lines · Michal Hocko
Quoted 19 lines · Roman Gushchin
Quoted 17 lines · Michal Hocko
Quoted 8 lines · Aslan Bakirov
With introduction of numa node interface for CMA, this patch is for using that interface for allocating memory on numa nodes if NUMA is configured. This will be more efficient and cleaner because first, instead of iterating mem range of each numa node,
cma_declare_contigueous_nid()will do its own address finding if we pass 0 for bothmin_pfnandmax_pfn, second, it can also handle caseswhere NUMA is not configured
by passingNUMA_NO_NODEas an argument.Quoted 4 lines
In addition, checking if desired size of memory is available or not, is happening in
cma_declare_contiguous_nid()because base and limit will be determined there, since 0(any) for base and 0(any) for limit is passed as argument to the function.This looks much better than the original patch. Can we simply squash your and Roman's patch in the mmotm tree and post it for the review in one piece? It would be slightly easier to review that way.
Quoted 8 lines
Quoted 4 lines
I'm glad you liked it! I agree, it's much nicer now, thanks to Aslan!
I think it's simpler to keep it as a separate patch, because there was already a fix by Randy Dunlap on top of my original version.
Why would be squashing all those into a single one be a problem. I will not insist of course but I would much rather see a single patch which is easy to review and doesn't contain any intermediate hacks to prevent
FWIW, for review purposes, this is Roman's patch with all followups from mmotm/next (hopefully didn't miss any) and then squashed with patch 2/2 from this thread. It can be applied like this:
- checkout v5.6
- apply patch 1/2 from this thread
- apply below
----8<----
From dc10a593f2b8dfc7be920b4b088a8d55068fc6bc Mon Sep 17 00:00:00 2001
From: Roman Gushchin <[email protected]>
Date: Thu, 2 Apr 2020 13:49:04 +1100
Subject: [PATCH] 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.
x86 and arm-64 are covered by this patch, other architectures can be trivially added later.
mm: hugetlb: Use node interface of cma
With introduction of numa node interface for CMA, this patch is for using that interface for allocating memory on numa nodes if NUMA is configured. This will be more efficient and cleaner because first, instead of iterating mem range of each numa node, cma_declare_contigueous_nid() will do its own address finding if we pass 0 for both min_pfn and max_pfn, second, it can also handle caseswhere NUMA is not configured
by passing NUMA_NO_NODE as an argument.
In addition, checking if desired size of memory is available or not, is happening in cma_declare_contiguous_nid() because base and limit will be determined there, since 0(any) for base and 0(any) for limit is passed as argument to the function.
--- .../admin-guide/kernel-parameters.txt | 7 ++ arch/arm64/mm/init.c | 6 ++ arch/x86/kernel/setup.c | 4 + include/linux/hugetlb.h | 8 ++ mm/hugetlb.c | 98 +++++++++++++++++++ 5 files changed, 123 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +7 -0
@@ -1445,6 +1445,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[KMGTPE] + + 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/arm64/mm/init.c +6 -0
@@ -29,6 +29,7 @@#include <linux/mm.h> #include <linux/kexec.h> #include <linux/crash_dump.h> +#include <linux/hugetlb.h> #include <asm/boot.h> #include <asm/fixmap.h>
@@ -457,6 +458,11 @@ void __init arm64_memblock_init(void) high_memory =__va (memblock_end_of_DRAM () -1 ) +1 ;dma_contiguous_reserve (arm64_dma32_phys_limit); + +#ifdef CONFIG_ARM64_4K_PAGES +hugetlb_cma_reserve (PUD_SHIFT -PAGE_SHIFT ); +#endif + }void __init bootmem_init (void )
arch/x86/kernel/setup.c +4 -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,9 @@ void __init setup_arch(char **cmdline_p)initmem_init ();dma_contiguous_reserve (max_pfn_mapped <<PAGE_SHIFT ); +if (boot_cpu_has (X86_FEATURE_GBPAGES )) +hugetlb_cma_reserve (PUD_SHIFT -PAGE_SHIFT ); +/* * Reserve memory for crash kernel after SRAT is parsed so that it * won't consume hotpluggable memory.
include/linux/hugetlb.h +8 -0
@@ -845,4 +845,12 @@ static inline spinlock_t *huge_pte_lock(struct hstate *h, return ptl; } +#if defined(CONFIG_HUGETLB_PAGE ) && defined(CONFIG_CMA ) +extern void __init hugetlb_cma_reserve (int order); +#else +static inline __init void hugetlb_cma_reserve (int order) +{ +} +#endif +#endif /* _LINUX_HUGETLB_H */
mm/hugetlb.c +98 -0
131 lines not on this page · raw message
Signature · 1 line
2.26.0
Michal Hocko
Acked-byRe: [PATCH 2/2] mm: hugetlb: Use node interface of cma
On Thu 02-04-20 17:20:01, Vlastimil Babka wrote:
[...]
Quoted 7 lines
FWIW, for review purposes, this is Roman's patch with all followups from mmotm/next (hopefully didn't miss any) and then squashed with patch 2/2 from this thread. It can be applied like this:
- checkout v5.6
- apply patch 1/2 from this thread
- apply below
Thanks!
Quoted 67 lines
----8<----
Quoted 1 line
From
dc10a593f2b8dfc7be920b4b088a8d55068fc6bcMon Sep 17 00:00:00 2001From: Roman Gushchin <[email protected]>
Date: Thu, 2 Apr 2020 13:49:04 +1100
Subject: [PATCH] mm: hugetlb: optionally allocate gigantic hugepages using cmaCommit
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 argument2) 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.
x86 and arm-64 are covered by this patch, other architectures can be trivially added later.
Signed-off-by Roman Gushchin <[email protected]>Tested-by Andreas Schaufler <[email protected]>Cc Michal Hocko <[email protected]>Cc Rik van Riel <[email protected]>Cc Andreas Schaufler <[email protected]>Cc Mike Kravetz <[email protected]>Cc Joonsoo Kim <[email protected]>Signed-off-by Andrew Morton <[email protected]>Signed-off-by Stephen Rothwell <[email protected]>mm: hugetlb: Use node interface of cma
With introduction of numa node interface for CMA, this patch is for using that interface for allocating memory on numa nodes if NUMA is configured. This will be more efficient and cleaner because first, instead of iterating mem range of each numa node,
cma_declare_contigueous_nid()will do its own address finding if we pass 0 for bothmin_pfnandmax_pfn, second, it can also handle caseswhere NUMA is not configured
by passingNUMA_NO_NODEas an argument.
Quoted 7 lines
In addition, checking if desired size of memory is available or not, is happening in
cma_declare_contiguous_nid()because base and limit will be determined there, since 0(any) for base and 0(any) for limit is passed as argument to the function.Signed-off-by Aslan Bakirov <[email protected]>Acked-by Roman Gushchin <[email protected]>
Minor nit below. For the squashed version feel free to add
Quoted 8 lines
--- .../admin-guide/kernel-parameters.txt | 7 ++ arch/arm64/mm/init.c | 6 ++ arch/x86/kernel/setup.c | 4 + include/linux/hugetlb.h | 8 ++ mm/hugetlb.c | 98 +++++++++++++++++++ 5 files changed, 123 insertions(+)
[...]
mm/hugetlb.c void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason) · line 5151
Quoted 11 lines
mm/hugetlb.c +11
-0+ reserved =0 ; +for_each_node_state (nid,N_ONLINE ) { +int res; + + size =min (per_node, hugetlb_cma_size - reserved); + size =round_up (size,PAGE_SIZE << order); + + +#ifndef CONFIG_NUMA + nid = NUMA_NO_NODE +#endif
This can be dropped. UMA will simply use node 0 and the memblock allocator will just do the right thing.
Quoted 24 lines
mm/hugetlb.c +22
-0+ res =cma_declare_contiguous_nid (0 , size, +0 , +PAGE_SIZE << order, +0 , false, +"hugetlb" , &hugetlb_cma[nid], nid); + +if (res) { +pr_warn ("%s: reservation failed: err %d, node %d\n" , + __func__, res, nid); +break ; + } + + reserved += size; +pr_info ("hugetlb_cma: reserved %lu MiB on node %d\n" , + size /SZ_1M , nid); + +if (reserved >= hugetlb_cma_size) + break; + } +} + +#endif /* CONFIG_CMA */ Signature · 1 line
2.26.0
Signature · 2 lines
Michal Hocko SUSE Labs
kbuild test robot
build botThank you for the patch! Yet something to improve:
Hi Aslan,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on next-20200326]
[also build test ERROR on v5.6-rc7]
[cannot apply to mmotm/master linus/master linux/master v5.6-rc7 v5.6-rc6 v5.6-rc5] [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/Aslan-Bakirov/mm-cma-NUMA-node-interface/20200327-082701 base: 89295c59c1f063b533d071ca49d0fa0c0783ca6f config: i386-allyesconfig (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 >>):
In file included from drivers/dma-buf/heaps/cma_heap.c:9:0:
include/linux/cma.h: In function 'cma_declare_contiguous':
>> include/linux/cma.h:39:29: error: 'NUMA_NO_NODE' undeclared (first use in this function)
fixed, name, res_cma, NUMA_NO_NODE);
^~~~~~~~~~~~ include/linux/cma.h:39:29: note: each undeclared identifier is reported only once for each function it appears in
vim +/NUMA_NO_NODE +39 include/linux/cma.h
26
27 extern int __init cma_declare_contiguous_nid(phys_addr_t base,
28 phys_addr_t size, phys_addr_t limit,
29 phys_addr_t alignment, unsigned int order_per_bit,
30 bool fixed, const char *name, struct cma **res_cma,
31 int nid);
32 static inline int __init cma_declare_contiguous(phys_addr_t base,
33 phys_addr_t size, phys_addr_t limit,
34 phys_addr_t alignment, unsigned int order_per_bit,
35 bool fixed, const char *name, struct cma **res_cma)
36 {
37 return cma_declare_contiguous_nid(base, size,
38 limit, alignment, order_per_bit,
> 39 fixed, name, res_cma, NUMA_NO_NODE);
40 }
41 extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
42 unsigned int order_per_bit,
43 const char *name,
44 struct cma **res_cma);
45 extern struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
46 bool no_warn);
47 extern bool cma_release(struct cma *cma, const struct page *pages, unsigned int count);
48 ---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 8 lines · Aslan Bakirov
I've noticed that there is no interfaces exposed by CMA which would let me to declare contigous memory on particular NUMA node.
This patchset adds the ability to try to allocate contiguous memory on specific node.
Implement a new method for declaring contigous memory on particular node and keep
cma_declare_contiguous()as a wrapper.
I am not an expert on CMA but this looks very reasonable to me.
Quoted 117 lines
Signed-off-by Aslan Bakirov <[email protected]>--- include/linux/cma.h | 14 ++++++++++++-- include/linux/memblock.h | 3 +++ mm/cma.c | 15 ++++++++------- mm/memblock.c | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-)include/linux/cma.h +12
-2@@ -24,10 +24,20 @@ extern phys_addr_t cma_get_base(const struct cma *cma);extern unsigned long cma_get_size (const struct cma *cma);extern const char *cma_get_name (const struct cma *cma); -extern int __init cma_declare_contiguous (phys_addr_t base, +extern int __init cma_declare_contiguous_nid (phys_addr_t base,phys_addr_t size,phys_addr_t limit,phys_addr_t alignment,unsigned int order_per_bit, -bool fixed,const char *name,struct cma **res_cma); +bool fixed,const char *name,struct cma **res_cma, +int nid); +static inline int __init cma_declare_contiguous (phys_addr_t base, +phys_addr_t size,phys_addr_t limit, +phys_addr_t alignment,unsigned int order_per_bit, +bool fixed,const char *name,struct cma **res_cma) + { +return cma_declare_contiguous_nid (base, size, + limit, alignment, order_per_bit, + fixed, name, res_cma,NUMA_NO_NODE ); + } extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, unsigned int order_per_bit, const char *name,include/linux/memblock.h +3
-0@@ -348,6 +348,9 @@ static inline int memblock_get_region_node(const struct memblock_region *r)phys_addr_t memblock_phys_alloc_range (phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end); +phys_addr_t memblock_alloc_range_nid (phys_addr_t size, +phys_addr_t align,phys_addr_t start, +phys_addr_t end,int nid,bool exact_nid);phys_addr_t memblock_phys_alloc_try_nid (phys_addr_t size,phys_addr_t align,int nid); static inline phys_addr_t memblock_phys_alloc(phys_addr_t size,mm/cma.c +8
-7@@ -220,7 +220,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, }/** -* cma_declare_contiguous() - reserve custom contiguous area +* cma_declare_contiguous_nid() - reserve custom contiguous area * @base: Base address of the reserved area optional, use 0 for any * @size: Size of the reserved area (in bytes), * @limit: End address of the reserved memory (optional, 0 for any). @@ -229,6 +229,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,* @fixed: hint about where to place the reserved area * @name: The name of the area. See function cma_init_reserved_mem() * @res_cma: Pointer to store the created cma region. +* @nid: nid of the free area to find, %NUMA_NO_NODE for any node * * This function reserves memory from early allocator. It should be * called by arch specific code once the early allocator (memblock or bootmem) @@ -238,10 +239,10 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,* If @fixed is true, reserve contiguous area at exactly @base. If false, * reserve in range from @base to @limit. */ -int __init cma_declare_contiguous (phys_addr_t base, +int __init cma_declare_contiguous_nid (phys_addr_t base,phys_addr_t size,phys_addr_t limit,phys_addr_t alignment,unsigned int order_per_bit, -bool fixed,const char *name,struct cma **res_cma) +bool fixed,const char *name,struct cma **res_cma,int nid) {phys_addr_t memblock_end =memblock_end_of_DRAM ();phys_addr_t highmem_start;@@ -336,14 +337,14 @@ int __init cma_declare_contiguous(phys_addr_t base,* memory in case of failure. */ if (base < highmem_start && limit > highmem_start) { - addr =memblock_phys_alloc_range (size, alignment, - highmem_start, limit); + addr =memblock_alloc_range_nid (size, alignment, + highmem_start, limit, nid, false); limit = highmem_start; }if (!addr) { - addr =memblock_phys_alloc_range (size, alignment, base, - limit); + addr =memblock_alloc_range_nid (size, alignment, base, + limit, nid, false);if (!addr) { ret = -ENOMEM ; goto err;mm/memblock.c +1
-1@@ -1349,7 +1349,7 @@ __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,* Return: * Physical address of allocated memory block on success, %0 on failure. */ -static phys_addr_t __init memblock_alloc_range_nid (phys_addr_t size, +phys_addr_t __init memblock_alloc_range_nid (phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end,int nid,bool exact_nid)Signature · 1 line
2.17.1
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 5 lines · Aslan Bakirov
I've noticed that there is no interfaces exposed by CMA which would let me to declare contigous memory on particular NUMA node.
This patchset adds the ability to try to allocate contiguous memory on specific node.
I would say more explicitly that 'try' here means it will fallback to other nodes if the specific one doesn't work. At least AFAICS that's what it does by calling memblock_alloc_range_nid() with exact_nid=false.
Quoted 2 lines
Implement a new method for declaring contigous memory on particular node and keep
cma_declare_contiguous()as a wrapper.
Should there be also support for using this node spcification in the cma=X boot param?
Quoted 1 line
Signed-off-by Aslan Bakirov <[email protected]>
...
Quoted 12 lines
mm/cma.c +1
-1@@ -220,7 +220,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, }/** -* cma_declare_contiguous() - reserve custom contiguous area +* cma_declare_contiguous_nid() - reserve custom contiguous area * @base: Base address of the reserved area optional, use 0 for any * @size: Size of the reserved area (in bytes), * @limit: End address of the reserved memory (optional, 0 for any).
mm/cma.c int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, · line 232
Quoted 4 lines
mm/cma.c +1
-0@@ -229,6 +229,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,* @fixed: hint about where to place the reserved area * @name: The name of the area. See function cma_init_reserved_mem() * @res_cma: Pointer to store the created cma region. +* @nid: nid of the free area to find, %NUMA_NO_NODE for any node
The bit about fallback should be also specified here.
Quoted 16 lines
mm/cma.c +2
-2* * This function reserves memory from early allocator. It should be * called by arch specific code once the early allocator (memblock or bootmem) @@ -238,10 +239,10 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,* If @fixed is true, reserve contiguous area at exactly @base. If false, * reserve in range from @base to @limit. */ -int __init cma_declare_contiguous (phys_addr_t base, +int __init cma_declare_contiguous_nid (phys_addr_t base,phys_addr_t size,phys_addr_t limit,phys_addr_t alignment,unsigned int order_per_bit, -bool fixed,const char *name,struct cma **res_cma) +bool fixed,const char *name,struct cma **res_cma,int nid) {phys_addr_t memblock_end =memblock_end_of_DRAM ();phys_addr_t highmem_start;
Quoted 5 lines · Vlastimil Babka
Quoted 1 line · Aslan Bakirov
I've noticed that there is no interfaces exposed by CMA which would let
me
Quoted 2 lines
to declare contigous memory on particular NUMA node.
Quoted 8 lines
Quoted 2 lines
This patchset adds the ability to try to allocate contiguous memory on specific node.
I would say more explicitly that 'try' here means it will fallback to other nodes if the specific one doesn't work. At least AFAICS that's what it does by
callingmemblock_alloc_range_nid()with exact_nid=false.
Thanks for the comments, Vlastimil. I created the next version with mentioning fallback case in the changelog.
Quoted 9 lines
Quoted 3 lines
Implement a new method for declaring contigous memory on particular node and keep
cma_declare_contiguous()as a wrapper.Should there be also support for using this node spcification in the cma=X boot
param?For this, I would wait for a real usecase for now, because adding it is
easy but removing is hard.
Quoted 47 lines
Quoted 1 line
Signed-off-by Aslan Bakirov <[email protected]>...
mm/cma.c int __init cma_init_reserved_mem(phys_addr_t base,
Quoted 3 lines
mm/cma.c +0
-0@@ -220,7 +220,7 @@ int __init cma_init_reserved_mem(phys_addr_t base,
phys_addr_tsize,mm/cma.c int __init cma_init_reserved_mem(phys_addr_t base,
Quoted 9 lines
mm/cma.c +1
-1}/** -* cma_declare_contiguous() - reserve custom contiguous area +* cma_declare_contiguous_nid() - reserve custom contiguous area * @base: Base address of the reserved area optional, use 0 for any * @size: Size of the reserved area (in bytes), * @limit: End address of the reserved memory (optional, 0 for any). @@ -229,6 +229,7 @@ int __init cma_init_reserved_mem(phys_addr_t base,
phys_addr_tsize,mm/cma.c int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, · line 232
Quoted 4 lines
mm/cma.c +1
-0* @fixed: hint about where to place the reserved area * @name: The name of the area. See function cma_init_reserved_mem() * @res_cma: Pointer to store the created cma region. +* @nid: nid of the free area to find, %NUMA_NO_NODE for any node The bit about fallback should be also specified here.
mm/cma.c int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
Quoted 3 lines
mm/cma.c +0
-0* * This function reserves memory from early allocator. It should be * called by arch specific code once the early allocator (memblock or bootmem)
mm/cma.c int __init cma_init_reserved_mem(phys_addr_t base,
Quoted 1 line
mm/cma.c +0
-0@@ -238,10 +239,10 @@ int __init cma_init_reserved_mem(phys_addr_t base,
phys_addr_tsize,mm/cma.c int __init cma_init_reserved_mem(phys_addr_t base, · line 239
Quoted 1 line
mm/cma.c +0
-0* If @fixed is true, reserve contiguous area at exactly @base. If false,
mm/cma.c int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
Quoted 8 lines
mm/cma.c +2
-2* reserve in range from @base to @limit. */ -int __init cma_declare_contiguous (phys_addr_t base, +int __init cma_declare_contiguous_nid (phys_addr_t base,phys_addr_t size,phys_addr_t limit,phys_addr_t alignment,unsigned int order_per_bit, -bool fixed,const char *name,struct cma **res_cma) + bool fixed, const char *name, struct cma**res_cma, int nid)
Quoted 3 lines
mm/cma.c +0
-0{phys_addr_t memblock_end =memblock_end_of_DRAM ();phys_addr_t highmem_start;
Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Aslan Bakirov
I've noticed that there is no interfaces exposed by CMA which would let me to declare contigous memory on particular NUMA node.
This patchset adds the ability to try to allocate contiguous memory on specific node.
Hello, Vlastimil!
Quoted 4 lines
I would say more explicitly that 'try' here means it will fallback to other nodes if the specific one doesn't work. At least AFAICS that's what it does by calling
memblock_alloc_range_nid()with exact_nid=false.
Hm, maybe we need exact_nid=true for this case? The whole point here is to have a per-node cma zone, so if it's not possible to allocate one on the specific node, maybe it's better to just skip it?
Quoted 6 lines
Quoted 3 lines
Implement a new method for declaring contigous memory on particular node and keep
cma_declare_contiguous()as a wrapper.Should there be also support for using this node spcification in the cma=X boot param?
I'd wait for a first real usecase. It's fairly easy to add one, and very hard to remove, so I'd be conservative here.
Also, in the future we might want to allocate it automatically and shrink on demand.
Btw, thank you very much for gathering all patches for Michal.
Thanks!