linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] mm: cma: NUMA node interface
@ 2020-04-03 10:18 Aslan Bakirov
  2020-04-03 10:18 ` [PATCH 2/2] mm: hugetlb: Use node interface of cma Aslan Bakirov
  0 siblings, 1 reply; 11+ messages in thread
From: Aslan Bakirov @ 2020-04-03 10:18 UTC (permalink / raw)
  To: akpm
  Cc: linux-kernel, linux-mm, kernel-team, riel, guro, mhocko, hannes,
	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. 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.

Signed-off-by: Aslan Bakirov <aslan@fb.com>
---
 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(-)

diff --git a/include/linux/cma.h b/include/linux/cma.h
index 190184b5ff32..eae834c2162f 100644
--- a/include/linux/cma.h
+++ b/include/linux/cma.h
@@ -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,
diff --git a/include/linux/memblock.h b/include/linux/memblock.h
index 079d17d96410..6bc37a731d27 100644
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -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,
diff --git a/mm/cma.c b/mm/cma.c
index be55d1988c67..6405af3dc118 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -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,
+					alimit, nid, false);
 			if (!addr) {
 				ret = -ENOMEM;
 				goto err;
diff --git a/mm/memblock.c b/mm/memblock.c
index 4d06bbaded0f..c79ba6f9920c 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -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)
-- 
2.24.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-04-03 10:18 [PATCH 1/2] mm: cma: NUMA node interface Aslan Bakirov
@ 2020-04-03 10:18 ` Aslan Bakirov
  2020-04-03 10:36   ` Michal Hocko
  0 siblings, 1 reply; 11+ messages in thread
From: Aslan Bakirov @ 2020-04-03 10:18 UTC (permalink / raw)
  To: akpm
  Cc: linux-kernel, linux-mm, kernel-team, riel, guro, mhocko, hannes,
	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  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.

Signed-off-by: Aslan Bakirov <aslan@fb.com>
---
 mm/hugetlb.c | 40 +++++++++++-----------------------------
 1 file changed, 11 insertions(+), 29 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index b9f0c903c4cf..62989220c4ff 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -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;
 		}
 
-- 
2.24.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-04-03 10:18 ` [PATCH 2/2] mm: hugetlb: Use node interface of cma Aslan Bakirov
@ 2020-04-03 10:36   ` Michal Hocko
  2020-04-03 11:21     ` Aslan Bakirov
  0 siblings, 1 reply; 11+ messages in thread
From: Michal Hocko @ 2020-04-03 10:36 UTC (permalink / raw)
  To: Aslan Bakirov
  Cc: akpm, linux-kernel, linux-mm, kernel-team, riel, guro, hannes

On Fri 03-04-20 03:18:43, Aslan Bakirov wrote:
> 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.

I have asked to merge this one with the original patch from Roman
several times but it seems this is not going to happen. But whatever.

You have likely missed my review feedback http://lkml.kernel.org/r/20200402172404.GV22681@dhcp22.suse.cz.
The ifdef CONFIG_NUMA for the nid definition is pointless.

> Signed-off-by: Aslan Bakirov <aslan@fb.com>

After fixing that, feel free to add
Acked-by: Michal Hocko <mhocko@suse.com>

> ---
>  mm/hugetlb.c | 40 +++++++++++-----------------------------
>  1 file changed, 11 insertions(+), 29 deletions(-)
> 
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index b9f0c903c4cf..62989220c4ff 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -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;
>  		}
>  
> -- 
> 2.24.1

-- 
Michal Hocko
SUSE Labs


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-04-03 10:36   ` Michal Hocko
@ 2020-04-03 11:21     ` Aslan Bakirov
  0 siblings, 0 replies; 11+ messages in thread
From: Aslan Bakirov @ 2020-04-03 11:21 UTC (permalink / raw)
  To: Michal Hocko
  Cc: akpm, linux-kernel, linux-mm, Kernel Team, riel, Roman Gushchin, hannes

[-- Attachment #1: Type: text/plain, Size: 4494 bytes --]



________________________________
From: Michal Hocko <mhocko@kernel.org>
Sent: Friday, April 3, 2020 11:36 AM
To: Aslan Bakirov <aslan@fb.com>
Cc: akpm@linux-foundation.org <akpm@linux-foundation.org>; linux-kernel@vger.kernel.org <linux-kernel@vger.kernel.org>; linux-mm@kvack.org <linux-mm@kvack.org>; Kernel Team <Kernel-team@fb.com>; riel@surriel.com <riel@surriel.com>; Roman Gushchin <guro@fb.com>; hannes@cmpxchg.org <hannes@cmpxchg.org>
Subject: Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma

On Fri 03-04-20 03:18:43, Aslan Bakirov wrote:
> 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.

I have asked to merge this one with the original patch from Roman
several times but it seems this is not going to happen. But whatever.

You have likely missed my review feedback http://lkml.kernel.org/r/20200402172404.GV22681@dhcp22.suse.cz.
The ifdef CONFIG_NUMA for the nid definition is pointless.

> Signed-off-by: Aslan Bakirov <aslan@fb.com>

After fixing that, feel free to add
Acked-by: Michal Hocko <mhocko@suse.com>

Addressed  your comment, and updated the patch. Thanks Michal.

> ---
>  mm/hugetlb.c | 40 +++++++++++-----------------------------
>  1 file changed, 11 insertions(+), 29 deletions(-)
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index b9f0c903c4cf..62989220c4ff 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -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;
>                }
>
> --
> 2.24.1

--
Michal Hocko
SUSE Labs

[-- Attachment #2: Type: text/html, Size: 10980 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-04-03 11:18 [PATCH 1/2] mm: cma: NUMA node interface Aslan Bakirov
@ 2020-04-03 11:18 ` Aslan Bakirov
  0 siblings, 0 replies; 11+ messages in thread
From: Aslan Bakirov @ 2020-04-03 11:18 UTC (permalink / raw)
  To: akpm
  Cc: linux-kernel, linux-mm, kernel-team, riel, guro, mhocko, hannes,
	Aslan Bakirov, Michal Hocko

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.

Signed-off-by: Aslan Bakirov <aslan@fb.com>
Acked-by: Michal Hocko <mhocko@suse.com>
---
 mm/hugetlb.c | 34 ++++++----------------------------
 1 file changed, 6 insertions(+), 28 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index b9f0c903c4cf..8fbf11de747b 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5573,42 +5573,20 @@ 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),
+		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;
 		}
 
-- 
2.24.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-04-02 15:20         ` Vlastimil Babka
@ 2020-04-02 17:24           ` Michal Hocko
  0 siblings, 0 replies; 11+ messages in thread
From: Michal Hocko @ 2020-04-02 17:24 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Roman Gushchin, Aslan Bakirov, akpm, linux-kernel, linux-mm,
	kernel-team, riel, hannes

On Thu 02-04-20 17:20:01, Vlastimil Babka wrote:
[...]
> 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!

> ----8<----
> >From dc10a593f2b8dfc7be920b4b088a8d55068fc6bc Mon Sep 17 00:00:00 2001
> From: Roman Gushchin <guro@fb.com>
> 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.
> 
> Link: http://lkml.kernel.org/r/20200311220920.2487528-1-guro@fb.com
> Signed-off-by: Roman Gushchin <guro@fb.com>
> Tested-by: Andreas Schaufler <andreas.schaufler@gmx.de>
> Cc: Michal Hocko <mhocko@kernel.org>
> Cc: Rik van Riel <riel@surriel.com>
> Cc: Andreas Schaufler <andreas.schaufler@gmx.de>
> Cc: Mike Kravetz <mike.kravetz@oracle.com>
> Cc: Joonsoo Kim <js1304@gmail.com>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
> 
> 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.
> 
> Signed-off-by: Aslan Bakirov <aslan@fb.com>
> Acked-by: Roman Gushchin <guro@fb.com>

Minor nit below. For the squashed version feel free to add
Acked-by: Michal Hocko <mhocko@suse.com>

> ---
>  .../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(+)
> 

[...]

> +	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.

> +		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 */
> -- 
> 2.26.0

-- 
Michal Hocko
SUSE Labs


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-03-27 15:13       ` Michal Hocko
@ 2020-04-02 15:20         ` Vlastimil Babka
  2020-04-02 17:24           ` Michal Hocko
  0 siblings, 1 reply; 11+ messages in thread
From: Vlastimil Babka @ 2020-04-02 15:20 UTC (permalink / raw)
  To: Michal Hocko, Roman Gushchin
  Cc: Aslan Bakirov, akpm, linux-kernel, linux-mm, kernel-team, riel, hannes


On 3/27/20 4:13 PM, Michal Hocko wrote:
> On Fri 27-03-20 07:41:55, Roman Gushchin wrote:
>> On Fri, Mar 27, 2020 at 09:06:10AM +0100, Michal Hocko wrote:
>> > On Thu 26-03-20 14:27:18, Aslan Bakirov wrote:
>> > > 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.
>> > 
>> > 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

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 <guro@fb.com>
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.

Link: http://lkml.kernel.org/r/20200311220920.2487528-1-guro@fb.com
Signed-off-by: Roman Gushchin <guro@fb.com>
Tested-by: Andreas Schaufler <andreas.schaufler@gmx.de>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Rik van Riel <riel@surriel.com>
Cc: Andreas Schaufler <andreas.schaufler@gmx.de>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Joonsoo Kim <js1304@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>

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.

Signed-off-by: Aslan Bakirov <aslan@fb.com>
Acked-by: Roman Gushchin <guro@fb.com>
---
 .../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(+)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index c07815d230bc..379b90c5a9aa 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -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
diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
index b65dffdfb201..e42727e3568e 100644
--- a/arch/arm64/mm/init.c
+++ b/arch/arm64/mm/init.c
@@ -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)
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index a74262c71484..fc3e326a62b9 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -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.
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 1e897e4168ac..2819c7339f20 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -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 */
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index dd8737a94bec..054fadf34c39 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -28,6 +28,7 @@
 #include <linux/jhash.h>
 #include <linux/numa.h>
 #include <linux/llist.h>
+#include <linux/cma.h>
 
 #include <asm/page.h>
 #include <asm/pgtable.h>
@@ -44,6 +45,9 @@
 int hugetlb_max_hstate __read_mostly;
 unsigned int default_hstate_idx;
 struct hstate hstates[HUGE_MAX_HSTATE];
+
+static struct cma *hugetlb_cma[MAX_NUMNODES];
+
 /*
  * Minimum page order among possible hugepage sizes, set to a proper value
  * at boot time.
@@ -1020,6 +1024,14 @@ static void destroy_compound_gigantic_page(struct page *page,
 
 static void free_gigantic_page(struct page *page, unsigned int order)
 {
+	/*
+	 * If the page isn't allocated using the cma allocator,
+	 * cma_release() returns false.
+	 */
+	if (IS_ENABLED(CONFIG_CMA) &&
+	    cma_release(hugetlb_cma[page_to_nid(page)], page, 1 << order))
+		return;
+
 	free_contig_range(page_to_pfn(page), 1 << order);
 }
 
@@ -1029,6 +1041,21 @@ static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
 {
 	unsigned long nr_pages = 1UL << huge_page_order(h);
 
+	if (IS_ENABLED(CONFIG_CMA)) {
+		struct page *page;
+		int node;
+
+		for_each_node_mask(node, *nodemask) {
+			if (!hugetlb_cma[node])
+				break;
+
+			page = cma_alloc(hugetlb_cma[node], nr_pages,
+					 huge_page_order(h), true);
+			if (page)
+				return page;
+		}
+	}
+
 	return alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask);
 }
 
@@ -2190,6 +2217,10 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
 
 	for (i = 0; i < h->max_huge_pages; ++i) {
 		if (hstate_is_gigantic(h)) {
+			if (IS_ENABLED(CONFIG_CMA) && hugetlb_cma[0]) {
+				pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n");
+				break;
+			}
 			if (!alloc_bootmem_huge_page(h))
 				break;
 		} else if (!alloc_pool_huge_page(h,
@@ -5073,3 +5104,70 @@ void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
 		spin_unlock(&hugetlb_lock);
 	}
 }
+
+#ifdef CONFIG_CMA
+static unsigned long hugetlb_cma_size __initdata;
+
+static int __init cmdline_parse_hugetlb_cma(char *p)
+{
+	hugetlb_cma_size = memparse(p, &p);
+	return 0;
+}
+
+early_param("hugetlb_cma", cmdline_parse_hugetlb_cma);
+
+void __init hugetlb_cma_reserve(int order)
+{
+	unsigned long size, reserved, per_node;
+	int nid;
+
+	if (!hugetlb_cma_size)
+		return;
+
+	if (hugetlb_cma_size < (PAGE_SIZE << order)) {
+		pr_warn("hugetlb_cma: cma area should be at least %lu MiB\n",
+			(PAGE_SIZE << order) / SZ_1M);
+		return;
+	}
+
+	/*
+	 * If 3 GB area is requested on a machine with 4 numa nodes,
+	 * let's allocate 1 GB on first three nodes and ignore the last one.
+	 */
+	per_node = DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes);
+	pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n",
+		hugetlb_cma_size / SZ_1M, per_node / SZ_1M);
+
+	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		
+		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 */
-- 
2.26.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-03-27 14:41     ` Roman Gushchin
@ 2020-03-27 15:13       ` Michal Hocko
  2020-04-02 15:20         ` Vlastimil Babka
  0 siblings, 1 reply; 11+ messages in thread
From: Michal Hocko @ 2020-03-27 15:13 UTC (permalink / raw)
  To: Roman Gushchin
  Cc: Aslan Bakirov, akpm, linux-kernel, linux-mm, kernel-team, riel, hannes

On Fri 27-03-20 07:41:55, Roman Gushchin wrote:
> On Fri, Mar 27, 2020 at 09:06:10AM +0100, Michal Hocko wrote:
> > On Thu 26-03-20 14:27:18, Aslan Bakirov wrote:
> > > 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.
> > 
> > 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.

> > > Signed-off-by: Aslan Bakirov <aslan@fb.com>
> 
> Acked-by: Roman Gushchin <guro@fb.com>
> 
> Thanks!
> 
> > 
> > Thanks!
> > 
> > > ---
> > >  mm/hugetlb.c | 40 +++++++++++-----------------------------
> > >  1 file changed, 11 insertions(+), 29 deletions(-)
> > > 
> > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> > > index b9f0c903c4cf..62989220c4ff 100644
> > > --- a/mm/hugetlb.c
> > > +++ b/mm/hugetlb.c
> > > @@ -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;
> > >  		}
> > >  
> > > -- 
> > > 2.17.1
> > 
> > -- 
> > Michal Hocko
> > SUSE Labs

-- 
Michal Hocko
SUSE Labs


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-03-27  8:06   ` Michal Hocko
@ 2020-03-27 14:41     ` Roman Gushchin
  2020-03-27 15:13       ` Michal Hocko
  0 siblings, 1 reply; 11+ messages in thread
From: Roman Gushchin @ 2020-03-27 14:41 UTC (permalink / raw)
  To: Michal Hocko
  Cc: Aslan Bakirov, akpm, linux-kernel, linux-mm, kernel-team, riel, hannes

On Fri, Mar 27, 2020 at 09:06:10AM +0100, Michal Hocko wrote:
> On Thu 26-03-20 14:27:18, Aslan Bakirov wrote:
> > 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.
> 
> 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.

> 
> > Signed-off-by: Aslan Bakirov <aslan@fb.com>

Acked-by: Roman Gushchin <guro@fb.com>

Thanks!

> 
> Thanks!
> 
> > ---
> >  mm/hugetlb.c | 40 +++++++++++-----------------------------
> >  1 file changed, 11 insertions(+), 29 deletions(-)
> > 
> > diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> > index b9f0c903c4cf..62989220c4ff 100644
> > --- a/mm/hugetlb.c
> > +++ b/mm/hugetlb.c
> > @@ -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;
> >  		}
> >  
> > -- 
> > 2.17.1
> 
> -- 
> Michal Hocko
> SUSE Labs


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-03-26 21:27 ` [PATCH 2/2] mm: hugetlb: Use node interface of cma Aslan Bakirov
@ 2020-03-27  8:06   ` Michal Hocko
  2020-03-27 14:41     ` Roman Gushchin
  0 siblings, 1 reply; 11+ messages in thread
From: Michal Hocko @ 2020-03-27  8:06 UTC (permalink / raw)
  To: Aslan Bakirov
  Cc: akpm, linux-kernel, linux-mm, kernel-team, riel, guro, hannes

On Thu 26-03-20 14:27:18, Aslan Bakirov wrote:
> 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.

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.

> Signed-off-by: Aslan Bakirov <aslan@fb.com>

Thanks!

> ---
>  mm/hugetlb.c | 40 +++++++++++-----------------------------
>  1 file changed, 11 insertions(+), 29 deletions(-)
> 
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index b9f0c903c4cf..62989220c4ff 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -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;
>  		}
>  
> -- 
> 2.17.1

-- 
Michal Hocko
SUSE Labs


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 2/2] mm: hugetlb: Use node interface of cma
  2020-03-26 21:27 [PATCH 1/2] mm: cma: NUMA node interface Aslan Bakirov
@ 2020-03-26 21:27 ` Aslan Bakirov
  2020-03-27  8:06   ` Michal Hocko
  0 siblings, 1 reply; 11+ messages in thread
From: Aslan Bakirov @ 2020-03-26 21:27 UTC (permalink / raw)
  To: akpm
  Cc: linux-kernel, linux-mm, kernel-team, riel, guro, mhocko, hannes,
	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  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.

Signed-off-by: Aslan Bakirov <aslan@fb.com>
---
 mm/hugetlb.c | 40 +++++++++++-----------------------------
 1 file changed, 11 insertions(+), 29 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index b9f0c903c4cf..62989220c4ff 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -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;
 		}
 
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-04-03 11:21 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-03 10:18 [PATCH 1/2] mm: cma: NUMA node interface Aslan Bakirov
2020-04-03 10:18 ` [PATCH 2/2] mm: hugetlb: Use node interface of cma Aslan Bakirov
2020-04-03 10:36   ` Michal Hocko
2020-04-03 11:21     ` Aslan Bakirov
  -- strict thread matches above, loose matches on Subject: below --
2020-04-03 11:18 [PATCH 1/2] mm: cma: NUMA node interface Aslan Bakirov
2020-04-03 11:18 ` [PATCH 2/2] mm: hugetlb: Use node interface of cma Aslan Bakirov
2020-03-26 21:27 [PATCH 1/2] mm: cma: NUMA node interface Aslan Bakirov
2020-03-26 21:27 ` [PATCH 2/2] mm: hugetlb: Use node interface of cma Aslan Bakirov
2020-03-27  8:06   ` Michal Hocko
2020-03-27 14:41     ` Roman Gushchin
2020-03-27 15:13       ` Michal Hocko
2020-04-02 15:20         ` Vlastimil Babka
2020-04-02 17:24           ` Michal Hocko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).