hugetlbfs: Extend the definition of hugepages parameter to support node allocation
Subsystems (4)
PATCH v4 1/1 hugetlbfs: Extend the definition of hugepages parameter to support node allocation
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 hugepages in one node. For example: DPDK needs hugepages which are in the same node as NIC. if DPDK needs four hugepages of 1G size in node1 and system has 16 numa nodes. We must reserve 64 hugepages in kernel cmdline. But, only four hugepages are used. The others should be free after boot. If the system memory is low(for example: 64G), it will be an impossible task. So, Extending hugepages parameter to support specifying hugepages at a specific node.
For example add following parameter:
hugepagesz=1G hugepages=0:1,1:3
It will allocate 1 hugepage in node0 and 3 hugepages in node1.
---
v3 -> v4: changes
- fix wrong behavior for parameter: hugepages=0:1,1:3 default_hugepagesz=1G
- make the change of documentation more reasonable
--- .../admin-guide/kernel-parameters.txt | 8 +- Documentation/admin-guide/mm/hugetlbpage.rst | 12 +- include/linux/hugetlb.h | 1 + mm/hugetlb.c | 122 +++++++++++++++++- 4 files changed, 132 insertions(+), 11 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -3
@@ -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, the + number of pages to allocate per-node can be specified. + See also Documentation/admin-guide/mm/hugetlbpage.rst. + Format: <integer> or (node format) + <node>:<integer>[,<node>:<integer>] hugepagesz= [HW] The size of the HugeTLB pages. This is used in
Documentation/admin-guide/mm/hugetlbpage.rst +11 -1
@@ -128,7 +128,9 @@ hugepages implicitly specifies the number of huge pages of default size to allocate. If the number of huge pages of default size is implicitly specified, it can not be overwritten by a hugepagesz,hugepages - parameter pair for the default size. + parameter pair for the default size. This parameter also has a + node format. The node format specifies the number of huge pages + to allocate on specific nodes. For example, on an architecture with 2M default huge page size::
@@ -138,6 +140,14 @@ hugepages indicating that the hugepages=512 parameter is ignored. If a hugepages parameter is preceded by an invalid hugepagesz parameter, it will be ignored. + + Node format example:: + + hugepagesz=2M hugepages=0:1,1:2 + + It will allocate 1 2M hugepage on node0 and 2 2M hugepages on node1. + If the node number is invalid, the parameter will be ignored. + default_hugepagesz Specify the default huge page size. This parameter can only be specified once on the command line. default_hugepagesz can
include/linux/hugetlb.h +1 -0
@@ -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 ];
mm/hugetlb.c +115 -7
186 lines not on this page · raw message
Signature · 1 line
2.27.0
Andrew Morton
maintainerQuoted 28 lines · On Thu, 9 Sep 2021 22:16:55 +0800 yaozhenguo <[email protected]> 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 hugepages in one node. For example: DPDK needs hugepages which are in the same node as NIC. if DPDK needs four hugepages of 1G size in node1 and system has 16 numa nodes. We must reserve 64 hugepages in kernel cmdline. But, only four hugepages are used. The others should be free after boot. If the system memory is low(for example: 64G), it will be an impossible task. So, Extending hugepages parameter to support specifying hugepages at a specific node.
For example add following parameter:hugepagesz=1G hugepages=0:1,1:3
It will allocate 1 hugepage in node0 and 3 hugepages in node1.
...
mm/hugetlb.c +7
-0@@ -2842,10 +2843,75 @@ static void __init gather_bootmem_prealloc(void) } } +static void __init hugetlb_hstate_alloc_pages_onenode (struct hstate *h,int nid) +{ +unsigned long i; +char buf[32 ]; + +for (i =0 ; i < h->max_huge_pages_node[nid]; ++i) { +if (hstate_is_gigantic (h)) {
mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 2862
Quoted 10 lines
mm/hugetlb.c +10
-0+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)));
We try very hard to avoid adding BUG calls. Is there any way in which this code can emit a WARNing then permit the kernel to keep operating?
Quoted 30 lines
mm/hugetlb.c +30
-0+/* +* 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) +break ; +put_page (page);/* free it into the hugepage allocator */ + } +cond_resched (); + } +if (i == h->max_huge_pages_node[nid]) +return ; + +string_get_size (huge_page_size (h),1 ,STRING_UNITS_2 , buf,32 ); +pr_warn ("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n" , + h->max_huge_pages_node[nid], buf, nid, i); + h->max_huge_pages_node[nid] = i; + h->max_huge_pages -= (h->max_huge_pages_node[nid] - i); +} +
Quoted 36 lines · Andrew Morton <[email protected]> 于2021年9月15日周三 上午11:50写道:
Quoted 33 lines · On Thu, 9 Sep 2021 22:16:55 +0800 yaozhenguo <[email protected]> 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 hugepages in one node. For example: DPDK needs hugepages which are in the same node as NIC. if DPDK needs four hugepages of 1G size in node1 and system has 16 numa nodes. We must reserve 64 hugepages in kernel cmdline. But, only four hugepages are used. The others should be free after boot. If the system memory is low(for example: 64G), it will be an impossible task. So, Extending hugepages parameter to support specifying hugepages at a specific node.
For example add following parameter:hugepagesz=1G hugepages=0:1,1:3
It will allocate 1 hugepage in node0 and 3 hugepages in node1.
...
mm/hugetlb.c +12
-0@@ -2842,10 +2843,75 @@ static void __init gather_bootmem_prealloc(void) } } +static void __init hugetlb_hstate_alloc_pages_onenode (struct hstate *h,int nid) +{ +unsigned long i; +char buf[32 ]; + +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),
Quoted 9 lines
mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 2862
Quoted 5 lines
mm/hugetlb.c +5
-0+ 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); +if (!addr) +break ; + m = addr; +BUG_ON (!IS_ALIGNED (virt_to_phys (m),huge_page_size (h)));We try very hard to avoid adding BUG calls. Is there any way in which this code can emit a WARNing then permit the kernel to keep operating?
Maybe we can rewrite it as below:
if (WARN(!IS_ALIGNED(virt_to_phys(m),
huge_page_size(h)),
"HugeTLB: page addr:%p is not aligned\n", m)) break;
@Mike, Do you think it's OK?
Quoted 31 lines
Quoted 30 lines
mm/hugetlb.c +30
-0+/* +* 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) +break ; +put_page (page);/* free it into the hugepage allocator */ + } +cond_resched (); + } +if (i == h->max_huge_pages_node[nid]) +return ; + +string_get_size (huge_page_size (h),1 ,STRING_UNITS_2 , buf,32 ); +pr_warn ("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n" , + h->max_huge_pages_node[nid], buf, nid, i); + h->max_huge_pages_node[nid] = i; + h->max_huge_pages -= (h->max_huge_pages_node[nid] - i); +} +
Quoted 46 lines · zhenguo yao
Quoted 36 lines · Andrew Morton <[email protected]> 于2021年9月15日周三 上午11:50写道:
Quoted 33 lines · On Thu, 9 Sep 2021 22:16:55 +0800 yaozhenguo <[email protected]> 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 hugepages in one node. For example: DPDK needs hugepages which are in the same node as NIC. if DPDK needs four hugepages of 1G size in node1 and system has 16 numa nodes. We must reserve 64 hugepages in kernel cmdline. But, only four hugepages are used. The others should be free after boot. If the system memory is low(for example: 64G), it will be an impossible task. So, Extending hugepages parameter to support specifying hugepages at a specific node.
For example add following parameter:hugepagesz=1G hugepages=0:1,1:3
It will allocate 1 hugepage in node0 and 3 hugepages in node1.
...
mm/hugetlb.c +12
-0@@ -2842,10 +2843,75 @@ static void __init gather_bootmem_prealloc(void) } } +static void __init hugetlb_hstate_alloc_pages_onenode (struct hstate *h,int nid) +{ +unsigned long i; +char buf[32 ]; + +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),Quoted 9 lines
mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 2862
Quoted 5 lines
mm/hugetlb.c +5
-0+ 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); +if (!addr) +break ; + m = addr; +BUG_ON (!IS_ALIGNED (virt_to_phys (m),huge_page_size (h)));We try very hard to avoid adding BUG calls. Is there any way in which this code can emit a WARNing then permit the kernel to keep operating?
Quoted 6 lines
Maybe we can rewrite it as below:
if (WARN(!IS_ALIGNED(virt_to_phys(m),
huge_page_size(h)),
"HugeTLB: page addr:%p is not aligned\n", m)) break;
@Mike, Do you think it's OK?
Sorry, I have not yet reviewed the latest version of this patch. Quick thought on this question.
The required alignment passed to memblock_alloc_try_nid_raw() is huge_page_size(h). Therefore, we know the virtual address m is huge_page_size(h) aligned. The BUG is just checking to make sure the physical address associated with the virtual address is aligned the same. I really do not see how this could not be the case. In fact, the memblock allocator finds a physical address with the required alignment and then returns phys_to_virt(alloc). Someone please correct me if I am wrong. Otherwise, we can drop the BUG.
Adding Mike Rapport on Cc:
This allocation code and the associated BUG was copied from __alloc_bootmem_huge_page(). The BUG was added 12 years ago before the memblock allocator existed and we were using the bootmem allocator. If there is no need for a BUG in hugetlb_hstate_alloc_pages_onenode, there is no need for one in __alloc_bootmem_huge_page.
Signature · 1 line
Mike Kravetz
Now, really CC'ing Mike, and sorry for misspelling your name
Quoted 74 lines · Mike Kravetz
Quoted 46 lines · zhenguo yao
Quoted 36 lines · Andrew Morton <[email protected]> 于2021年9月15日周三 上午11:50写道:
Quoted 33 lines · On Thu, 9 Sep 2021 22:16:55 +0800 yaozhenguo <[email protected]> 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 hugepages in one node. For example: DPDK needs hugepages which are in the same node as NIC. if DPDK needs four hugepages of 1G size in node1 and system has 16 numa nodes. We must reserve 64 hugepages in kernel cmdline. But, only four hugepages are used. The others should be free after boot. If the system memory is low(for example: 64G), it will be an impossible task. So, Extending hugepages parameter to support specifying hugepages at a specific node.
For example add following parameter:hugepagesz=1G hugepages=0:1,1:3
It will allocate 1 hugepage in node0 and 3 hugepages in node1.
...
mm/hugetlb.c +12
-0@@ -2842,10 +2843,75 @@ static void __init gather_bootmem_prealloc(void) } } +static void __init hugetlb_hstate_alloc_pages_onenode (struct hstate *h,int nid) +{ +unsigned long i; +char buf[32 ]; + +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),Quoted 9 lines
mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 2862
Quoted 5 lines
mm/hugetlb.c +5
-0+ 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); +if (!addr) +break ; + m = addr; +BUG_ON (!IS_ALIGNED (virt_to_phys (m),huge_page_size (h)));We try very hard to avoid adding BUG calls. Is there any way in which this code can emit a WARNing then permit the kernel to keep operating?
Quoted 6 lines
Maybe we can rewrite it as below:
if (WARN(!IS_ALIGNED(virt_to_phys(m),
huge_page_size(h)),
"HugeTLB: page addr:%p is not aligned\n", m)) break;
@Mike, Do you think it's OK?Sorry, I have not yet reviewed the latest version of this patch. Quick thought on this question.
The required alignment passed to
memblock_alloc_try_nid_raw()ishuge_page_size(h). Therefore, we know the virtual address m ishuge_page_size(h)aligned. The BUG is just checking to make sure the physical address associated with the virtual address is aligned the same. I really do not see how this could not be the case. In fact, the memblock allocator finds a physical address with the required alignment and then returnsphys_to_virt(alloc). Someone please correct me if I am wrong. Otherwise, we can drop the BUG.
Adding Mike Rapport on Cc:This allocation code and the associated BUG was copied from
__alloc_bootmem_huge_page(). The BUG was added 12 years ago before the memblock allocator existed and we were using the bootmem allocator. If there is no need for a BUG inhugetlb_hstate_alloc_pages_onenode, there is no need for one in__alloc_bootmem_huge_page.
Signature · 1 line
Mike Kravetz
Mike Rapoport
reviewerHi Mike,
Quoted 69 lines · Mike Kravetz
Now, really CC'ing Mike, and sorry for misspelling your name
Quoted 66 lines · Mike Kravetz
Quoted 46 lines · zhenguo yao
Quoted 36 lines · Andrew Morton <[email protected]> 于2021年9月15日周三 上午11:50写道:
Quoted 33 lines · On Thu, 9 Sep 2021 22:16:55 +0800 yaozhenguo <[email protected]> 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 hugepages in one node. For example: DPDK needs hugepages which are in the same node as NIC. if DPDK needs four hugepages of 1G size in node1 and system has 16 numa nodes. We must reserve 64 hugepages in kernel cmdline. But, only four hugepages are used. The others should be free after boot. If the system memory is low(for example: 64G), it will be an impossible task. So, Extending hugepages parameter to support specifying hugepages at a specific node.
For example add following parameter:hugepagesz=1G hugepages=0:1,1:3
It will allocate 1 hugepage in node0 and 3 hugepages in node1.
...
mm/hugetlb.c +12
-0@@ -2842,10 +2843,75 @@ static void __init gather_bootmem_prealloc(void) } } +static void __init hugetlb_hstate_alloc_pages_onenode (struct hstate *h,int nid) +{ +unsigned long i; +char buf[32 ]; + +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),Quoted 9 lines
mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 2862
Quoted 5 lines
mm/hugetlb.c +5
-0+ 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); +if (!addr) +break ; + m = addr; +BUG_ON (!IS_ALIGNED (virt_to_phys (m),huge_page_size (h)));We try very hard to avoid adding BUG calls. Is there any way in which this code can emit a WARNing then permit the kernel to keep operating?
Quoted 6 lines
Maybe we can rewrite it as below:
if (WARN(!IS_ALIGNED(virt_to_phys(m),
huge_page_size(h)),
"HugeTLB: page addr:%p is not aligned\n", m)) break;
@Mike, Do you think it's OK?Sorry, I have not yet reviewed the latest version of this patch. Quick thought on this question.
The required alignment passed to
memblock_alloc_try_nid_raw()ishuge_page_size(h). Therefore, we know the virtual address m ishuge_page_size(h)aligned. The BUG is just checking to make sure the physical address associated with the virtual address is aligned the same. I really do not see how this could not be the case. In fact, the memblock allocator finds a physical address with the required alignment and then returnsphys_to_virt(alloc). Someone please correct me if I am wrong. Otherwise, we can drop the BUG.
I agree with your analysis and I also think the BUG() can be dropped entirely as well as the BUG() in __alloc_bootmem_huge_page().
Quoted 7 lines
Quoted 7 lines
Adding Mike Rapport on Cc:
This allocation code and the associated BUG was copied from
__alloc_bootmem_huge_page(). The BUG was added 12 years ago before the memblock allocator existed and we were using the bootmem allocator. If there is no need for a BUG inhugetlb_hstate_alloc_pages_onenode, there is no need for one in__alloc_bootmem_huge_page.
Hmm, even bootmem had alignment guaranties so it seems to me that the BUG() was over-protective even then.
Signature · 2 lines
Sincerely yours, Mike.
Quoted 47 lines · Mike Kravetz
Quoted 25 lines · zhenguo yao
Quoted 15 lines · Andrew Morton <[email protected]> 于2021年9月15日周三 上午11:50写道:
Quoted 12 lines · On Thu, 9 Sep 2021 22:16:55 +0800 yaozhenguo <[email protected]> wrote:
mm/hugetlb.c +12
-0+static void __init hugetlb_hstate_alloc_pages_onenode (struct hstate *h,int nid) +{ +unsigned long i; +char buf[32 ]; + +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),Quoted 9 lines
mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 2862
Quoted 5 lines
mm/hugetlb.c +5
-0+ 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid); +if (!addr) +break ; + m = addr; +BUG_ON (!IS_ALIGNED (virt_to_phys (m),huge_page_size (h)));We try very hard to avoid adding BUG calls. Is there any way in which this code can emit a WARNing then permit the kernel to keep operating?
Quoted 6 lines
Maybe we can rewrite it as below:
if (WARN(!IS_ALIGNED(virt_to_phys(m),
huge_page_size(h)),
"HugeTLB: page addr:%p is not aligned\n", m)) break;
@Mike, Do you think it's OK?Sorry, I have not yet reviewed the latest version of this patch. Quick thought on this question.
The required alignment passed to
memblock_alloc_try_nid_raw()ishuge_page_size(h). Therefore, we know the virtual address m ishuge_page_size(h)aligned. The BUG is just checking to make sure the physical address associated with the virtual address is aligned the same. I really do not see how this could not be the case. In fact, the memblock allocator finds a physical address with the required alignment and then returnsphys_to_virt(alloc). Someone please correct me if I am wrong. Otherwise, we can drop the BUG.
Adding Mike Rapport on Cc:
Quoted 5 lines
This allocation code and the associated BUG was copied from
__alloc_bootmem_huge_page(). The BUG was added 12 years ago before the memblock allocator existed and we were using the bootmem allocator. If there is no need for a BUG inhugetlb_hstate_alloc_pages_onenode, there is no need for one in__alloc_bootmem_huge_page.
One additional thought.
Architectures can provide their own version of alloc_bootmem_huge_page. powerpc is the only architecture doing so today. If an architecture does provide their own version of alloc_bootmem_huge_page, I do not think we should/can use hugetlb_hstate_alloc_pages_onenode to allocate node specific gigantic huge pages.
I think we need to disable this feature for such architectures, -OR-
provide some method to do architecture specific node allocations of gigantic pages.
Signature · 1 line
Mike Kravetz