hugetlb: search harder for memory in alloc_fresh_huge_page()
Subsystems (3)
RFC PATCH 1/4 hugetlb: search harder for memory in alloc_fresh_huge_page()
Currently, alloc_fresh_huge_page() returns NULL when it is not able to allocate a huge page on the current node, as specified by its custom interleave variable. The callers of this function, though, assume that a failure in alloc_fresh_huge_page() indicates no hugepages can be allocated on the system period. This might not be the case, for instance, if we have an uneven NUMA system, and we happen to try to allocate a hugepage on a node with less memory and fail, while there is still plenty of free memory on the other nodes.
To correct this, make alloc_fresh_huge_page() search through all online nodes before deciding no hugepages can be allocated. Add a helper function for actually allocating the hugepage. Also, while we expect particular semantics for __GFP_THISNODE, which are newly enforced -- that is, that the allocation won't go off-node -- still use page_to_nid() to guarantee we don't mess up the accounting.
Tested on 4-node ppc64 (2 memoryless nodes), 2-node IA64, 4-node x86 (NUMAQ), !NUMA x86
---
with just Christoph's patches, on a 4-node ppc64 with 2 memoryless nodes:
Trying to clear the hugetlb pool
Done. 0 free
Trying to resize the pool to 100
Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 75 Node 0 HugePages_Free: 25 Done. Initially 100 free Trying to resize the pool to 200 Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 150 Node 0 HugePages_Free: 50 Done. 200 free Trying to resize the pool back to 100 Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 100 Node 0 HugePages_Free: 0
Done. 100 free
with this patch on top (THISNODE forces allocations to stay on-node and thus are balanced):
Trying to clear the hugetlb pool
Done. 0 free
Trying to resize the pool to 100
Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 50 Node 0 HugePages_Free: 50 Done. Initially 100 free Trying to resize the pool to 200 Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 100 Node 0 HugePages_Free: 100 Done. 200 free Trying to resize the pool back to 100 Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 100 Node 0 HugePages_Free: 0
Done. 100 free
mm/hugetlb.c +42 -19
@@ -101,26 +101,13 @@ static void free_huge_page(struct page *page)spin_unlock (&hugetlb_lock); } -static int alloc_fresh_huge_page (void ) +static struct page *alloc_fresh_huge_page_node (int nid) { -static int prev_nid;struct page *page; -int nid; - -/* -* Copy static prev_nid to local nid, work on that, then copy it -* back to prev_nid afterwards: otherwise there's a window in which -* a racer might pass invalid nid MAX_NUMNODES to alloc_pages_node. -* But we don't need to use a spin_lock here: it really doesn't -* matter if occasionally a racer chooses the same nid as we do. -*/ - nid =next_node (prev_nid, node_online_map); -if (nid ==MAX_NUMNODES ) - nid =first_node (node_online_map); - prev_nid = nid; - page =alloc_pages_node (nid, htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN, -HUGETLB_PAGE_ORDER ); + page =alloc_pages_node (nid, + htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|__GFP_NOWARN, +HUGETLB_PAGE_ORDER );if (page) {set_compound_page_dtor (page, free_huge_page);spin_lock (&hugetlb_lock);
@@ -128,9 +115,45 @@ static int alloc_fresh_huge_page(void) nr_huge_pages_node[page_to_nid (page)]++;spin_unlock (&hugetlb_lock);put_page (page);/* free it into the hugepage allocator */ -return 1 ; } -return 0 ; + +return page; +} + +static int alloc_fresh_huge_page (void ) +{ +static int nid =-1 ; +struct page *page; +int start_nid; +int next_nid; +int ret =0 ; + +if (nid <0 ) + nid =first_node (node_online_map); + start_nid = nid; + +do { + page =alloc_fresh_huge_page_node (nid); +if (page) + ret =1 ; +/* +* Use a helper variable to find the next node and then +* copy it back to nid nid afterwards: otherwise there's +* a window in which a racer might pass invalid nid +* MAX_NUMNODES to alloc_pages_node. But we don't need +* to use a spin_lock here: it really doesn't matter if +* occasionally a racer chooses the same nid as we do. +* Move nid forward in the mask even if we just +* successfully allocated a hugepage so that the next +* caller gets hugepages on the next node. +*/ + next_nid =next_node (nid, node_online_map); +if (next_nid ==MAX_NUMNODES ) + next_nid =first_node (node_online_map); + nid = next_nid; + }while (!page && nid != start_nid); + +return ret; } static struct page *alloc_huge_page(struct vm_area_struct *vma,
Signature · 8 lines
Nishanth Aravamudan <[email protected]> IBM Linux Technology Center -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH 2/4 hugetlb: fix pool allocation with empty nodes
[V10] hugetlb: fix pool allocation with empty nodes
Anton found a problem with the hugetlb pool allocation when some nodes have no memory (http://marc.info/?l=linux-mm&m=118133042025995&w=2). Lee worked on versions that tried to fix it, but none were accepted. Christoph has created a set of patches which allow for GFP_THISNODE allocations to fail if the node has no memory and for exporting a nodemask indicating which nodes have memory. Simply interleave across this nodemask rather than the online nodemask.
---
Note: given that alloc_fresh_huge_page() now interleaves using GFP_THISNODE, it might be the case that this patch is no longer needed? Instead, we'll just end up skipping over those memoryless nodes more when we could just ignore them altogether.
mm/hugetlb.c +3 -3
@@ -129,7 +129,7 @@ static int alloc_fresh_huge_page(void)int ret =0 ;if (nid <0 ) - nid =first_node (node_online_map); + nid =first_node (node_states[N_HIGH_MEMORY ]); start_nid = nid; do {
@@ -147,9 +147,9 @@ static int alloc_fresh_huge_page(void)* successfully allocated a hugepage so that the next * caller gets hugepages on the next node. */ - next_nid =next_node (nid, node_online_map); + next_nid =next_node (nid, node_states[N_HIGH_MEMORY ]);if (next_nid ==MAX_NUMNODES ) - next_nid =first_node (node_online_map); + next_nid =first_node (node_states[N_HIGH_MEMORY ]); nid = next_nid; }while (!page && nid != start_nid);
Signature · 8 lines
Nishanth Aravamudan <[email protected]> IBM Linux Technology Center -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH 3/4 hugetlb: interleave dequeueing of huge pages
Currently, when shrinking the hugetlb pool, we free all of the pages on node 0, then all the pages on node 1, etc. Instead, we interleave over the nodes with memory. If some particularly node should be cleared first, the to-be-introduced sysfs allocator can be used for finer-grained control. This also helps with keeping the pool balanced as we change the pool at run-time.
Tested on 4-node ppc64 (2 memoryless nodes), 2-node IA64, 4-node x86 (NUMAQ), !NUMA x86.
---
Christoph's patches + patches 1,2/4 on a 4-node ppc64 with 2 memoryless:
Trying to clear the hugetlb pool
Done. 0 free
Trying to resize the pool to 100
Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 50 Node 0 HugePages_Free: 50 Done. Initially 100 free Trying to resize the pool to 200 Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 100 Node 0 HugePages_Free: 100 Done. 200 free Trying to resize the pool back to 100 Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 100 Node 0 HugePages_Free: 0
Done. 100 free
Christoph's patches + patches 1,2,3/4:
Trying to clear the hugetlb pool
Done. 0 free
Trying to resize the pool to 100
Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 50 Node 0 HugePages_Free: 50 Done. Initially 100 free Trying to resize the pool to 200 Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 100 Node 0 HugePages_Free: 100 Done. 200 free Trying to resize the pool back to 100 Node 3 HugePages_Free: 0 Node 2 HugePages_Free: 0 Node 1 HugePages_Free: 50 Node 0 HugePages_Free: 50
Done. 100 free
mm/hugetlb.c +51 -11
@@ -66,11 +66,56 @@ static void enqueue_huge_page(struct page *page) free_huge_pages_node[nid]++; } -static struct page *dequeue_huge_page (struct vm_area_struct *vma, +static struct page *dequeue_huge_page_node (int nid) +{ +struct page *page; + + page =list_entry (hugepage_freelists[nid].next, + struct page, lru); +list_del (&page->lru); + free_huge_pages--; + free_huge_pages_node[nid]--; +return page; +} + +static struct page *dequeue_huge_page (void ) +{ +static int nid =-1 ; +struct page *page =NULL ; +int start_nid; +int next_nid; + +if (nid <0 ) + nid =first_node (node_states[N_HIGH_MEMORY ]); + start_nid = nid; + +do { +if (!list_empty (&hugepage_freelists[nid])) + page =dequeue_huge_page_node (nid); +/* +* Use a helper variable to find the next node and then +* copy it back to nid nid afterwards: otherwise there's +* a window in which a racer might pass invalid nid +* MAX_NUMNODES to dequeue_huge_page_node. But we don't +* need to use a spin_lock here: it really doesn't +* matter if occasionally a racer chooses the same nid +* as we do. Move nid forward in the mask even if we +* just successfully allocated a hugepage so that the +* next caller frees hugepages on the next node. +*/ + next_nid =next_node (nid, node_states[N_HIGH_MEMORY ]); +if (next_nid ==MAX_NUMNODES ) + next_nid =first_node (node_states[N_HIGH_MEMORY ]); + nid = next_nid; + }while (!page && nid != start_nid); + +return page; +} + +static struct page *dequeue_huge_page_vma (struct vm_area_struct *vma,unsigned long address) {int nid; -struct page *page =NULL ;struct zonelist *zonelist =huge_zonelist (vma, address, htlb_alloc_mask);struct zone **z;
@@ -79,15 +124,10 @@ static struct page *dequeue_huge_page(struct vm_area_struct *vma, nid =zone_to_nid (*z);if (cpuset_zone_allowed_softwall (*z, htlb_alloc_mask) && !list_empty (&hugepage_freelists[nid])) { - page =list_entry (hugepage_freelists[nid].next, - struct page, lru); -list_del (&page->lru); - free_huge_pages--; - free_huge_pages_node[nid]--; -break ; +return dequeue_huge_page_node (nid); } } -return page; +return NULL ; }static void free_huge_page (struct page *page)
@@ -167,7 +207,7 @@ static struct page *alloc_huge_page(struct vm_area_struct *vma,else if (free_huge_pages <= resv_huge_pages) goto fail; - page =dequeue_huge_page (vma, addr); + page =dequeue_huge_page_vma (vma, addr);if (!page) goto fail;
@@ -275,7 +315,7 @@ static unsigned long set_max_huge_pages(unsigned long count) count =max (count, resv_huge_pages);try_to_free_low (count);while (count < nr_huge_pages) { -struct page *page =dequeue_huge_page (NULL ,0 ); +struct page *page =dequeue_huge_page ();if (!page)break ;update_and_free_page (page);
Signature · 8 lines
Nishanth Aravamudan <[email protected]> IBM Linux Technology Center -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH 4/4 hugetlb: add per-node nr_hugepages sysfs attribute
Allow specifying the number of hugepages to allocate on a particular node. Our current global sysctl will try its best to put hugepages equally on each node, but htat may not always be desired. This allows the admin to control the layout of hugepage allocation at a finer level (while not breaking the existing interface). Add callbacks in the sysfs node registration and unregistration functions into hugetlb to add the nr_hugepages attribute, which is a no-op if !NUMA or !HUGETLB.
Tested on 4-node ppc64 (2 memoryless nodes), 2-node IA64, 4-node x86 (NUMAQ), !NUMA x86.
drivers/base/node.c +2 -0
@@ -151,6 +151,7 @@ int register_node(struct node *node, int num, struct node *parent)sysdev_create_file (&node->sysdev, &attr_meminfo);sysdev_create_file (&node->sysdev, &attr_numastat);sysdev_create_file (&node->sysdev, &attr_distance); +hugetlb_register_node (node); }return error; }
@@ -168,6 +169,7 @@ void unregister_node(struct node *node)sysdev_remove_file (&node->sysdev, &attr_meminfo);sysdev_remove_file (&node->sysdev, &attr_numastat);sysdev_remove_file (&node->sysdev, &attr_distance); +hugetlb_unregister_node (node);sysdev_unregister (&node->sysdev); }
include/linux/hugetlb.h +11 -0
@@ -6,7 +6,9 @@#ifdef CONFIG_HUGETLB_PAGE #include <linux/mempolicy.h> +#include <linux/node.h> #include <linux/shm.h> +#include <linux/sysdev.h> #include <asm/tlbflush.h> struct ctl_table ;
@@ -25,6 +27,13 @@ void __unmap_hugepage_range(struct vm_area_struct *, unsigned long, unsigned lonint hugetlb_prefault (struct address_space *,struct vm_area_struct *);int hugetlb_report_meminfo (char *);int hugetlb_report_node_meminfo (int ,char *); +#ifdef CONFIG_NUMA +int hugetlb_register_node (struct node *); +void hugetlb_unregister_node (struct node *); +#else +#define hugetlb_register_node (node) 0 +#define hugetlb_unregister_node (node) ((void)0) +#endif unsigned long hugetlb_total_pages (void );int hugetlb_fault (struct mm_struct *mm,struct vm_area_struct *vma,unsigned long address,int write_access);
@@ -116,6 +125,8 @@ static inline unsigned long hugetlb_total_pages(void)#define unmap_hugepage_range (vma, start, end) BUG()#define hugetlb_report_meminfo (buf) 0#define hugetlb_report_node_meminfo (n, buf) 0 +#define hugetlb_register_node (node) 0 +#define hugetlb_unregister_node (node) ((void)0)#define follow_huge_pmd (mm, addr, pmd, write) NULL#define prepare_hugepage_range (addr,len,pgoff) (-EINVAL)#define pmd_huge (x) 0
mm/hugetlb.c +87 -15
142 lines not on this page · raw message
Signature · 8 lines
Nishanth Aravamudan <[email protected]> IBM Linux Technology Center -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC][PATCH 4/4] hugetlb: add per-node nr_hugepages sysfs attribute
include/linux/hugetlb.h void __unmap_hugepage_range(struct vm_area_struct *, unsigned long, unsigned lon · line 36
Quoted 8 lines · Nishanth Aravamudan
include/linux/hugetlb.h +7
-0+#ifdef CONFIG_NUMA +int hugetlb_register_node (struct node *); +void hugetlb_unregister_node (struct node *); +#else +#define hugetlb_register_node (node) 0 +#define hugetlb_unregister_node (node) ((void)0) +#endif
This is to keep someone from doing:
ret = hugetlb_unregister_node(node);
?
I think it's a little more standard to do:
#define hugetlb_unregister_node(node) do {} while(0)-- Dave
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC][PATCH 4/4] hugetlb: add per-node nr_hugepages sysfs attribute
Quoted 12 lines · Dave Hansen
include/linux/hugetlb.h void __unmap_hugepage_range(struct vm_area_struct *, unsigned long, unsigned lon · line 36
Quoted 8 lines · Nishanth Aravamudan
include/linux/hugetlb.h +7
-0+#ifdef CONFIG_NUMA +int hugetlb_register_node (struct node *); +void hugetlb_unregister_node (struct node *); +#else +#define hugetlb_register_node (node) 0 +#define hugetlb_unregister_node (node) ((void)0) +#endif This is to keep someone from doing:
Quoted 7 lines
ret = hugetlb_unregister_node(node);?
I think it's a little more standard to do:
#define hugetlb_unregister_node(node) do {} while(0)
That's a good point. Now that I'm back from vacation, I'll make this adjustment.
Thanks,
Nish
Signature · 8 lines
Nishanth Aravamudan <[email protected]> IBM Linux Technology Center -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>