LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* Re: [PATCH V1] hugetlbfs: Extend the definition of hugepages parameter to support node allocation
@ 2021-08-24  3:53 姚振国
  0 siblings, 0 replies; 3+ messages in thread
From: 姚振国 @ 2021-08-24  3:53 UTC (permalink / raw)
  To: mike.kravetz
  Cc: willy, linux-kernel, linux-doc, linux-mm, corbet, akpm, willy

Hi Mike:

As a reference. I have tested the following cases for the patch. But It seems not enough.
It do need to add a check for the maximum number of nodes and other boundary conditions.
 
Test env:  Centos8 with master kernel, X86 virtual machine with 4 numa node

1.[default hugepagessz (old format)]:	hugepages=6	
2.[only 2M(old format)]:hugepagesz=2M hugepages=16	
3.[only 1G (old format)]:hugepagesz=1G hugepages=8	
4.[1G and 2M (old format)]:	hugepagesz=2M hugepages=16 hugepagesz=1G hugepages=8 
5.[default hugepagesz node]:	hugepages=0:1,1:2,2:3,3:4	
6.[2M all node]:hugepagesz=2M hugepages=0:1,1:2,2:3,3:4	
7.[2M port node]:hugepagesz=2M hugepages=0:1,1:2	
8.[2M all node,include zero]:	hugepagesz=2M hugepages=0:1,1:0,2:3,3:4	
9.[1G all node]	hugepagesz=1G hugepages=0:1,1:3,2:3,3:4	
10.[1G port node]:	hugepagesz=1G hugepages=0:2,1:1	
11.[1G all node,include zero]:hugepagesz=1G hugepages=0:0,1:3,2:3,3:4	
12.[2M all node; 1G all node]: hugepagesz=2M hugepages=0:1,1:2,2:3,3:4 hugepagesz=1G hugepages=0:1,1:3,2:3,3:4
13.[2M all node,include zero; 1G all node,include zero]: hugepagesz=2M hugepages=0:0,1:0,2:3,3:4 hugepagesz=1G hugepages=0:0,1:3,2:3,3:0
14.[2M all node;1G(old format)]:	hugepagesz=2M hugepages=0:1,1:2,2:3,3:4 hugepagesz=1G hugepages=6
15.[2M (old format);1G  part node]:hugepagesz=2M hugepages=16 hugepagesz=1G hugepages=0:0,1:3,2:3,3:0
16.[1G (old format);2M  all node]:hugepagesz=1G hugepages=6 hugepagesz=2M hugepages=0:1,1:2,2:3,3:4
17.[1G part node;2M (old format)]:hugepagesz=1G hugepages=0:2,1:1 hugepagesz=2M hugepages=16
18.[invalid parameter 1]:hugepagesz=2M hugepages=0:4,  [alloc 4 hugepages in node0]
19.[invalid parameter 2]:hugepagesz=2M hugepages=0:    [no hugepages allocated]
20.[invalid parameter 2]:hugepagesz=2M hugepages=0:4:   [alloc 4 hugepages in node0] 
-- 
 

在 2021/8/23 下午9:03,“yaozhenguo”<yaozhenguo1@gmail.com> 写入:

    We can specify the number of hugepages to allocate at boot. But the
    hugepages is balanced in all nodes at present. In some scenarios,
    we only need hugepags in one node. For example: DPDK needs hugepages
    which is in the same node as NIC. if DPDK needs four hugepags of 1G
    size in node1 and system has 16 numa nodes. We must reserve 64 hugepags
    in kernel cmdline. But, only four hugepages is used. The others should
    be free after boot.If the system memory is low(for example: 64G), it will
    be an impossible task. So, extend hugepages kernel parameter to specify
    node number of hugepages to allocate at boot.
    For example add following parameter:
    
    hugepagesz=1G hugepages=0:1,1:3
    
    It will allocate 1 hugepags in node0 and 3 hugepages in node1.
    
    Signed-off-by: yaozhenguo <yaozhenguo1@gmail.com>
    ---
     .../admin-guide/kernel-parameters.txt         |   8 +-
     include/linux/hugetlb.h                       |   1 +
     mm/hugetlb.c                                  | 122 ++++++++++++++++--
     3 files changed, 115 insertions(+), 16 deletions(-)
    
    diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
    index bdb22006f..64a128924 100644
    --- a/Documentation/admin-guide/kernel-parameters.txt
    +++ b/Documentation/admin-guide/kernel-parameters.txt
    @@ -1588,9 +1588,11 @@
     			the number of pages of hugepagesz to be allocated.
     			If this is the first HugeTLB parameter on the command
     			line, it specifies the number of pages to allocate for
    -			the default huge page size.  See also
    -			Documentation/admin-guide/mm/hugetlbpage.rst.
    -			Format: <integer>
    +			the default huge page size. If using node format, It
    +			specifies numbers of hugepage in a specific node.
    +			See also Documentation/admin-guide/mm/hugetlbpage.rst.
    +			Format: <integer> or (node format)
    +				<node>:<numbers>[,<node>:<numbers>]
     
     	hugepagesz=
     			[HW] The size of the HugeTLB pages.  This is used in
    diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
    index f7ca1a387..5939ecd4f 100644
    --- a/include/linux/hugetlb.h
    +++ b/include/linux/hugetlb.h
    @@ -605,6 +605,7 @@ struct hstate {
     	unsigned long nr_overcommit_huge_pages;
     	struct list_head hugepage_activelist;
     	struct list_head hugepage_freelists[MAX_NUMNODES];
    +	unsigned int max_huge_pages_node[MAX_NUMNODES];
     	unsigned int nr_huge_pages_node[MAX_NUMNODES];
     	unsigned int free_huge_pages_node[MAX_NUMNODES];
     	unsigned int surplus_huge_pages_node[MAX_NUMNODES];
    diff --git a/mm/hugetlb.c b/mm/hugetlb.c
    index dfc940d52..b898cddcd 100644
    --- a/mm/hugetlb.c
    +++ b/mm/hugetlb.c
    @@ -66,6 +66,7 @@ static struct hstate * __initdata parsed_hstate;
     static unsigned long __initdata default_hstate_max_huge_pages;
     static bool __initdata parsed_valid_hugepagesz = true;
     static bool __initdata parsed_default_hugepagesz;
    +static unsigned int default_hugepages_in_node[MAX_NUMNODES] __initdata;
     
     /*
      * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
    @@ -2842,10 +2843,56 @@ static void __init gather_bootmem_prealloc(void)
     	}
     }
     
    +static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)
    +{
    +	unsigned long i;
    +
    +	for (i = 0; i < h->max_huge_pages_node[nid]; i++) {
    +		if (hstate_is_gigantic(h)) {
    +			struct huge_bootmem_page *m;
    +			void *addr;
    +
    +			addr = memblock_alloc_try_nid_raw(
    +					huge_page_size(h), huge_page_size(h),
    +					0, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
    +			if (!addr)
    +				break;
    +			m = addr;
    +			BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h)));
    +			/* Put them into a private list first because mem_map is not up yet */
    +			INIT_LIST_HEAD(&m->list);
    +			list_add(&m->list, &huge_boot_pages);
    +			m->hstate = h;
    +		} else {
    +			struct page *page;
    +
    +			gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
    +
    +			page = alloc_fresh_huge_page(h, gfp_mask, nid,
    +					&node_states[N_MEMORY], NULL);
    +			if (page)
    +				put_page(page); /* free it into the hugepage allocator */
    +
    +		}
    +	}
    +}
    +
     static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
     {
     	unsigned long i;
     	nodemask_t *node_alloc_noretry;
    +	bool hugetlb_node_set = false;
    +
    +	/* do node alloc */
    +	for (i = 0; i < nodes_weight(node_states[N_MEMORY]); i++) {
    +		if (h->max_huge_pages_node[i] > 0) {
    +			hugetlb_hstate_alloc_pages_onenode(h, i);
    +			hugetlb_node_set = true;
    +		}
    +	}
    +
    +	if (hugetlb_node_set)
    +		return;
     
     	if (!hstate_is_gigantic(h)) {
     		/*
    @@ -3580,6 +3627,9 @@ static int __init hugetlb_init(void)
     				default_hstate_max_huge_pages;
     		}
     	}
    +	for (i = 0; i < nodes_weight(node_states[N_MEMORY]); i++)
    +		if (default_hugepages_in_node[i] > 0)
    +			default_hstate.max_huge_pages_node[i] = default_hugepages_in_node[i];
     
     	hugetlb_cma_check();
     	hugetlb_init_hstates();
    @@ -3649,6 +3699,11 @@ static int __init hugepages_setup(char *s)
     {
     	unsigned long *mhp;
     	static unsigned long *last_mhp;
    +	unsigned int node = NUMA_NO_NODE;
    +	int ret;
    +	int count;
    +	unsigned long tmp;
    +	char *p = s;
     
     	if (!parsed_valid_hugepagesz) {
     		pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s);
    @@ -3656,25 +3711,66 @@ static int __init hugepages_setup(char *s)
     		return 0;
     	}
     
    -	/*
    -	 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
    -	 * yet, so this hugepages= parameter goes to the "default hstate".
    -	 * Otherwise, it goes with the previously parsed hugepagesz or
    -	 * default_hugepagesz.
    -	 */
    -	else if (!hugetlb_max_hstate)
    -		mhp = &default_hstate_max_huge_pages;
    -	else
    -		mhp = &parsed_hstate->max_huge_pages;
    +	while (*p) {
    +		count = 0;
    +		ret = sscanf(p, "%lu%n", &tmp, &count);
    +		if (ret != 1) {
    +			pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p);
    +			break;
    +		}
    +		/* Parameter is not node format */
    +		if (p[count] != ':') {
    +			/*
    +			 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
    +			 * yet, so this hugepages= parameter goes to the "default hstate".
    +			 * Otherwise, it goes with the previously parsed hugepagesz or
    +			 * default_hugepagesz.
    +			 */
    +			if (!hugetlb_max_hstate) {
    +				default_hstate_max_huge_pages = tmp;
    +				mhp = &default_hstate_max_huge_pages;
    +			} else {
    +				parsed_hstate->max_huge_pages = tmp;
    +				mhp = &parsed_hstate->max_huge_pages;
    +			}
    +			break;
    +		}
    +		/* Parameter is node format */
    +		node = tmp;
    +		p += count + 1;
    +		if (node < 0) {
    +			pr_warn("HugeTLB: Invalid hugepages parameter node:%d\n", node);
    +			break;
    +		}
    +		if (!hugetlb_max_hstate)
    +			mhp = (unsigned long *)
    +				&(default_hugepages_in_node[node]);
    +		else
    +			mhp = (unsigned long *)
    +				&(parsed_hstate->max_huge_pages_node[node]);
    +		/* Parse hugepages */
    +		ret = sscanf(p, "%lu%n", mhp, &count);
    +		if (ret != 1) {
    +			pr_warn("HugeTLB: Invalid parameter %s\n", p);
    +			*mhp = 0;
    +			break;
    +		}
    +		if (!hugetlb_max_hstate)
    +			default_hstate_max_huge_pages += *mhp;
    +		else
    +			parsed_hstate->max_huge_pages += *mhp;
    +		/* Go to parse next node*/
    +		if (p[count] == ',')
    +			p += count + 1;
    +		else
    +			break;
    +	}
     
     	if (mhp == last_mhp) {
     		pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s);
     		return 0;
     	}
     
    -	if (sscanf(s, "%lu", mhp) <= 0)
    -		*mhp = 0;
    -
     	/*
     	 * Global state is always initialized later in hugetlb_init.
     	 * But we need to allocate gigantic hstates here early to still
    -- 
    2.27.0
    
    


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

* Re: [PATCH V1] hugetlbfs: Extend the definition of hugepages parameter to support node allocation
  2021-08-23 13:01 yaozhenguo
@ 2021-08-23 19:17 ` David Hildenbrand
  0 siblings, 0 replies; 3+ messages in thread
From: David Hildenbrand @ 2021-08-23 19:17 UTC (permalink / raw)
  To: yaozhenguo, mike.kravetz, corbet, akpm
  Cc: yaozhenguo, willy, linux-kernel, linux-doc, linux-mm

On 23.08.21 15:01, yaozhenguo wrote:
> We can specify the number of hugepages to allocate at boot. But the
> hugepages is balanced in all nodes at present. In some scenarios,
> we only need hugepags in one node. For example: DPDK needs hugepages
> which is in the same node as NIC. if DPDK needs four hugepags of 1G
> size in node1 and system has 16 numa nodes. We must reserve 64 hugepags
> in kernel cmdline. But, only four hugepages is used. The others should
> be free after boot.If the system memory is low(for example: 64G), it will
> be an impossible task. So, extend hugepages kernel parameter to specify
> node number of hugepages to allocate at boot.
> For example add following parameter:
> 
> hugepagesz=1G hugepages=0:1,1:3
> 
> It will allocate 1 hugepags in node0 and 3 hugepages in node1.

I like this format.


-- 
Thanks,

David / dhildenb


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

* [PATCH V1] hugetlbfs: Extend the definition of hugepages parameter to support node allocation
@ 2021-08-23 13:01 yaozhenguo
  2021-08-23 19:17 ` David Hildenbrand
  0 siblings, 1 reply; 3+ messages in thread
From: yaozhenguo @ 2021-08-23 13:01 UTC (permalink / raw)
  To: mike.kravetz, corbet, akpm
  Cc: yaozhenguo, willy, linux-kernel, linux-doc, linux-mm, yaozhenguo

We can specify the number of hugepages to allocate at boot. But the
hugepages is balanced in all nodes at present. In some scenarios,
we only need hugepags in one node. For example: DPDK needs hugepages
which is in the same node as NIC. if DPDK needs four hugepags of 1G
size in node1 and system has 16 numa nodes. We must reserve 64 hugepags
in kernel cmdline. But, only four hugepages is used. The others should
be free after boot.If the system memory is low(for example: 64G), it will
be an impossible task. So, extend hugepages kernel parameter to specify
node number of hugepages to allocate at boot.
For example add following parameter:

hugepagesz=1G hugepages=0:1,1:3

It will allocate 1 hugepags in node0 and 3 hugepages in node1.

Signed-off-by: yaozhenguo <yaozhenguo1@gmail.com>
---
 .../admin-guide/kernel-parameters.txt         |   8 +-
 include/linux/hugetlb.h                       |   1 +
 mm/hugetlb.c                                  | 122 ++++++++++++++++--
 3 files changed, 115 insertions(+), 16 deletions(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index bdb22006f..64a128924 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1588,9 +1588,11 @@
 			the number of pages of hugepagesz to be allocated.
 			If this is the first HugeTLB parameter on the command
 			line, it specifies the number of pages to allocate for
-			the default huge page size.  See also
-			Documentation/admin-guide/mm/hugetlbpage.rst.
-			Format: <integer>
+			the default huge page size. If using node format, It
+			specifies numbers of hugepage in a specific node.
+			See also Documentation/admin-guide/mm/hugetlbpage.rst.
+			Format: <integer> or (node format)
+				<node>:<numbers>[,<node>:<numbers>]
 
 	hugepagesz=
 			[HW] The size of the HugeTLB pages.  This is used in
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index f7ca1a387..5939ecd4f 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -605,6 +605,7 @@ struct hstate {
 	unsigned long nr_overcommit_huge_pages;
 	struct list_head hugepage_activelist;
 	struct list_head hugepage_freelists[MAX_NUMNODES];
+	unsigned int max_huge_pages_node[MAX_NUMNODES];
 	unsigned int nr_huge_pages_node[MAX_NUMNODES];
 	unsigned int free_huge_pages_node[MAX_NUMNODES];
 	unsigned int surplus_huge_pages_node[MAX_NUMNODES];
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index dfc940d52..b898cddcd 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -66,6 +66,7 @@ static struct hstate * __initdata parsed_hstate;
 static unsigned long __initdata default_hstate_max_huge_pages;
 static bool __initdata parsed_valid_hugepagesz = true;
 static bool __initdata parsed_default_hugepagesz;
+static unsigned int default_hugepages_in_node[MAX_NUMNODES] __initdata;
 
 /*
  * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
@@ -2842,10 +2843,56 @@ static void __init gather_bootmem_prealloc(void)
 	}
 }
 
+static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)
+{
+	unsigned long i;
+
+	for (i = 0; i < h->max_huge_pages_node[nid]; i++) {
+		if (hstate_is_gigantic(h)) {
+			struct huge_bootmem_page *m;
+			void *addr;
+
+			addr = memblock_alloc_try_nid_raw(
+					huge_page_size(h), huge_page_size(h),
+					0, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+			if (!addr)
+				break;
+			m = addr;
+			BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h)));
+			/* Put them into a private list first because mem_map is not up yet */
+			INIT_LIST_HEAD(&m->list);
+			list_add(&m->list, &huge_boot_pages);
+			m->hstate = h;
+		} else {
+			struct page *page;
+
+			gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
+
+			page = alloc_fresh_huge_page(h, gfp_mask, nid,
+					&node_states[N_MEMORY], NULL);
+			if (page)
+				put_page(page); /* free it into the hugepage allocator */
+
+		}
+	}
+}
+
 static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
 {
 	unsigned long i;
 	nodemask_t *node_alloc_noretry;
+	bool hugetlb_node_set = false;
+
+	/* do node alloc */
+	for (i = 0; i < nodes_weight(node_states[N_MEMORY]); i++) {
+		if (h->max_huge_pages_node[i] > 0) {
+			hugetlb_hstate_alloc_pages_onenode(h, i);
+			hugetlb_node_set = true;
+		}
+	}
+
+	if (hugetlb_node_set)
+		return;
 
 	if (!hstate_is_gigantic(h)) {
 		/*
@@ -3580,6 +3627,9 @@ static int __init hugetlb_init(void)
 				default_hstate_max_huge_pages;
 		}
 	}
+	for (i = 0; i < nodes_weight(node_states[N_MEMORY]); i++)
+		if (default_hugepages_in_node[i] > 0)
+			default_hstate.max_huge_pages_node[i] = default_hugepages_in_node[i];
 
 	hugetlb_cma_check();
 	hugetlb_init_hstates();
@@ -3649,6 +3699,11 @@ static int __init hugepages_setup(char *s)
 {
 	unsigned long *mhp;
 	static unsigned long *last_mhp;
+	unsigned int node = NUMA_NO_NODE;
+	int ret;
+	int count;
+	unsigned long tmp;
+	char *p = s;
 
 	if (!parsed_valid_hugepagesz) {
 		pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s);
@@ -3656,25 +3711,66 @@ static int __init hugepages_setup(char *s)
 		return 0;
 	}
 
-	/*
-	 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
-	 * yet, so this hugepages= parameter goes to the "default hstate".
-	 * Otherwise, it goes with the previously parsed hugepagesz or
-	 * default_hugepagesz.
-	 */
-	else if (!hugetlb_max_hstate)
-		mhp = &default_hstate_max_huge_pages;
-	else
-		mhp = &parsed_hstate->max_huge_pages;
+	while (*p) {
+		count = 0;
+		ret = sscanf(p, "%lu%n", &tmp, &count);
+		if (ret != 1) {
+			pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p);
+			break;
+		}
+		/* Parameter is not node format */
+		if (p[count] != ':') {
+			/*
+			 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
+			 * yet, so this hugepages= parameter goes to the "default hstate".
+			 * Otherwise, it goes with the previously parsed hugepagesz or
+			 * default_hugepagesz.
+			 */
+			if (!hugetlb_max_hstate) {
+				default_hstate_max_huge_pages = tmp;
+				mhp = &default_hstate_max_huge_pages;
+			} else {
+				parsed_hstate->max_huge_pages = tmp;
+				mhp = &parsed_hstate->max_huge_pages;
+			}
+			break;
+		}
+		/* Parameter is node format */
+		node = tmp;
+		p += count + 1;
+		if (node < 0) {
+			pr_warn("HugeTLB: Invalid hugepages parameter node:%d\n", node);
+			break;
+		}
+		if (!hugetlb_max_hstate)
+			mhp = (unsigned long *)
+				&(default_hugepages_in_node[node]);
+		else
+			mhp = (unsigned long *)
+				&(parsed_hstate->max_huge_pages_node[node]);
+		/* Parse hugepages */
+		ret = sscanf(p, "%lu%n", mhp, &count);
+		if (ret != 1) {
+			pr_warn("HugeTLB: Invalid parameter %s\n", p);
+			*mhp = 0;
+			break;
+		}
+		if (!hugetlb_max_hstate)
+			default_hstate_max_huge_pages += *mhp;
+		else
+			parsed_hstate->max_huge_pages += *mhp;
+		/* Go to parse next node*/
+		if (p[count] == ',')
+			p += count + 1;
+		else
+			break;
+	}
 
 	if (mhp == last_mhp) {
 		pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s);
 		return 0;
 	}
 
-	if (sscanf(s, "%lu", mhp) <= 0)
-		*mhp = 0;
-
 	/*
 	 * Global state is always initialized later in hugetlb_init.
 	 * But we need to allocate gigantic hstates here early to still
-- 
2.27.0


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

end of thread, other threads:[~2021-08-24  4:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-24  3:53 [PATCH V1] hugetlbfs: Extend the definition of hugepages parameter to support node allocation 姚振国
  -- strict thread matches above, loose matches on Subject: below --
2021-08-23 13:01 yaozhenguo
2021-08-23 19:17 ` David Hildenbrand

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