mm: using CMA for 1 GB hugepages allocation
Subsystems (7)
v5 cover mm: using CMA for 1 GB hugepages allocation
The patchset adds a hugetlb_cma boot option, which allows to reserve a cma area which can be later used for 1 GB hugepages allocations.
v5:
1) made the hugetlb_cma work if a cma reservation fails on some nodes
v4:
1) added a patch from Aslan, which is adding a useful function to cma
2) removed the hard-coded archs list from docs
3) added a warning printing on non-supported archs
4) hugetlb_lock is temporarily dropped in update_and_free_page()
Thanks!
Aslan Bakirov (1):
mm: cma: NUMA node interface
Roman Gushchin (1):
mm: hugetlb: optionally allocate gigantic hugepages using cma
.../admin-guide/kernel-parameters.txt | 8 ++ arch/arm64/mm/init.c | 6 + arch/x86/kernel/setup.c | 4 + include/linux/cma.h | 13 ++- include/linux/hugetlb.h | 12 ++ include/linux/memblock.h | 3 + mm/cma.c | 16 +-- mm/hugetlb.c | 109 ++++++++++++++++++ mm/memblock.c | 2 +- 9 files changed, 163 insertions(+), 10 deletions(-)
Signature · 1 line
2.25.1
PATCH v5 1/2 mm: cma: NUMA node interface
From: Aslan Bakirov <[email protected]>
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. It will fallback to other nodes if the specified one doesn't work.
Implement a new method for declaring contigous memory on particular node and keep cma_declare_contiguous() as a wrapper.
--- include/linux/cma.h | 13 +++++++++++-- include/linux/memblock.h | 3 +++ mm/cma.c | 16 +++++++++------- mm/memblock.c | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-)
include/linux/cma.h +11 -2
@@ -24,10 +24,19 @@ 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 +9 -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,11 @@ 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 +338,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.25.1
Andrew Morton
maintainerRe: [PATCH v5 1/2] mm: cma: NUMA node interface
Quoted 9 lines · On Tue, 7 Apr 2020 09:38:39 -0700 Roman Gushchin <[email protected]> wrote:
From: Aslan Bakirov <[email protected]>
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. It will fallback to other nodes if the specified one doesn't work.
Quoted 5 lines
Implement a new method for declaring contigous memory on particular node and keep
cma_declare_contiguous()as a wrapper.Signed-off-by Aslan Bakirov <[email protected]>Acked-by Michal Hocko <[email protected]>
This should have had your signed-off-by, as you were on the patch delivery path. Please send it along?
Roman Gushchin
Acked-by Michal HockoRe: [PATCH v5 1/2] mm: cma: NUMA node interface
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Tue, 7 Apr 2020 09:38:39 -0700 Roman Gushchin <[email protected]> wrote:
From: Aslan Bakirov <[email protected]>
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. It will fallback to other nodes if the specified one doesn't work.
Quoted 9 lines
Quoted 5 lines
Implement a new method for declaring contigous memory on particular node and keep
cma_declare_contiguous()as a wrapper.Signed-off-by Aslan Bakirov <[email protected]>Acked-by Michal Hocko <[email protected]>This should have had your signed-off-by, as you were on the patch delivery path. Please send it along?
Will this work?
If not, below if the whole patch signed.
Thanks!
--
From 63ddb2d792b548ee480d85a42c94a9e6b4722f4f Mon Sep 17 00:00:00 2001
From: Aslan Bakirov <[email protected]>
Date: Mon, 6 Apr 2020 11:02:59 -0700
Subject: [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. It will fallback to other nodes if the specified one doesn't work.
Implement a new method for declaring contigous memory on particular node and keep cma_declare_contiguous() as a wrapper.
--- include/linux/cma.h | 13 +++++++++++-- include/linux/memblock.h | 3 +++ mm/cma.c | 16 +++++++++------- mm/memblock.c | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-)
include/linux/cma.h +11 -2
@@ -24,10 +24,19 @@ 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 +9 -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,11 @@ 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 +338,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.25.1
PATCH v5 2/2 mm: hugetlb: optionally allocate gigantic hugepages using cma
In reply to Roman GushchinCommit 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.
The patch contains clean-ups and fixes proposed and implemented by Aslan Bakirov and Randy Dunlap. It also contains ideas and suggestions proposed by Rik van Riel, Michal Hocko and Mike Kravetz. Thanks!
--- .../admin-guide/kernel-parameters.txt | 8 ++ arch/arm64/mm/init.c | 6 + arch/x86/kernel/setup.c | 4 + include/linux/hugetlb.h | 12 ++ mm/hugetlb.c | 109 ++++++++++++++++++ 5 files changed, 139 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +8 -0
@@ -1473,6 +1473,14 @@ hpet_mmap= [X86, HPET_MMAP] Allow userspace to mmap HPET registers. Default set by CONFIG_HPET_MMAP_DEFAULT. + hugetlb_cma= [HW] The size of a cma area used for allocation + of gigantic hugepages. + Format: nn[KMGTPE] + + Reserve a cma area of given size and allocate gigantic + hugepages using the cma allocator. If enabled, the + 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>
@@ -1157,6 +1158,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 +12 -0
@@ -895,4 +895,16 @@ 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); +extern void __init hugetlb_cma_check (void ); +#else +static inline __init void hugetlb_cma_reserve (int order) +{ +} +static inline __init void hugetlb_cma_check (void ) +{ +} +#endif +#endif /* _LINUX_HUGETLB_H */
mm/hugetlb.c +109 -0
156 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v5 2/2] mm: hugetlb: optionally allocate gigantic hugepages using cma
Quoted 44 lines · Roman Gushchin
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 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.
The patch contains clean-ups and fixes proposed and implemented by Aslan Bakirov and Randy Dunlap. It also contains ideas and suggestions proposed by Rik van Riel, Michal Hocko and Mike Kravetz. Thanks!
Quoted 8 lines
Signed-off-by Roman Gushchin <[email protected]>Tested-by Andreas Schaufler <[email protected]>Acked-by Mike Kravetz <[email protected]>Acked-by Michal Hocko <[email protected]>Cc Aslan Bakirov <[email protected]>Cc Randy Dunlap <[email protected]>Cc Rik van Riel <[email protected]>Cc Joonsoo Kim <[email protected]>
Thanks a lot for addressing my review feedback!
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH v5 2/2] mm: hugetlb: optionally allocate gigantic hugepages using cma
Quoted 54 lines · Michal Hocko
Quoted 44 lines · Roman Gushchin
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 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.
The patch contains clean-ups and fixes proposed and implemented by Aslan Bakirov and Randy Dunlap. It also contains ideas and suggestions proposed by Rik van Riel, Michal Hocko and Mike Kravetz. Thanks!
Quoted 8 lines
Signed-off-by Roman Gushchin <[email protected]>Tested-by Andreas Schaufler <[email protected]>Acked-by Mike Kravetz <[email protected]>Acked-by Michal Hocko <[email protected]>Cc Aslan Bakirov <[email protected]>Cc Randy Dunlap <[email protected]>Cc Rik van Riel <[email protected]>Cc Joonsoo Kim <[email protected]>
Quoted 1 line
Thanks a lot for addressing my review feedback!
Actually thank you for looking at it!
Andrew, can you, please, pick this version up instead of current patches and fixes in the mm tree?
It will also fix the build on the next tree.
Thanks!