mm/thp: Allocate transparent hugepages on local node
Subsystems (5)
PATCH v4 1/1 mm/thp: Allocate transparent hugepages on local node
This make sure that we try to allocate hugepages from local node if allowed by mempolicy. If we can't, we fallback to small page allocation based on mempolicy. This is based on the observation that allocating pages on local node is more beneficial than allocating hugepages on remote node.
With this patch applied we may find transparent huge page allocation failures if the current node doesn't have enough freee hugepages. Before this patch such failures result in us retrying the allocation on other nodes in the numa node mask.
---
Changes from V3:
* Add more comments. Update commit message.
include/linux/gfp.h | 4 +++ mm/huge_memory.c | 24 +++++++----------- mm/mempolicy.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 15 deletions(-)
include/linux/gfp.h +4 -0
@@ -335,11 +335,15 @@ alloc_pages(gfp_t gfp_mask, unsigned int order)extern struct page *alloc_pages_vma (gfp_t gfp_mask,int order,struct vm_area_struct *vma,unsigned long addr,int node); +extern struct page *alloc_hugepage_vma (gfp_t gfp,struct vm_area_struct *vma, +unsigned long addr,int order);#else #define alloc_pages (gfp_mask, order) \ alloc_pages_node(numa_node_id(), gfp_mask, order)#define alloc_pages_vma (gfp_mask, order, vma, addr, node) \ alloc_pages(gfp_mask, order) +#define alloc_hugepage_vma (gfp_mask, vma, addr, order) \ + alloc_pages(gfp_mask, order)#endif #define alloc_page (gfp_mask) alloc_pages(gfp_mask, 0)#define alloc_page_vma (gfp_mask, vma, addr) \
mm/huge_memory.c +9 -15
@@ -766,15 +766,6 @@ static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)return (GFP_TRANSHUGE & ~(defrag ?0 : __GFP_WAIT)) | extra_gfp; } -static inline struct page *alloc_hugepage_vma (int defrag, -struct vm_area_struct *vma, -unsigned long haddr,int nd, -gfp_t extra_gfp) -{ -return alloc_pages_vma (alloc_hugepage_gfpmask (defrag, extra_gfp), -HPAGE_PMD_ORDER , vma, haddr, nd); -} -/* Caller must hold page table lock. */ static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
@@ -795,6 +786,7 @@ int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, unsigned int flags) { + gfp_t gfp;struct page *page;unsigned long haddr = address &HPAGE_PMD_MASK ;
@@ -829,8 +821,8 @@ int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, }return 0 ; } - page =alloc_hugepage_vma (transparent_hugepage_defrag (vma), - vma, haddr,numa_node_id (),0 ); + gfp =alloc_hugepage_gfpmask (transparent_hugepage_defrag (vma),0 ); + page =alloc_hugepage_vma (gfp, vma, haddr,HPAGE_PMD_ORDER );if (unlikely (!page)) {count_vm_event (THP_FAULT_FALLBACK );return VM_FAULT_FALLBACK ;
@@ -1118,10 +1110,12 @@ int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,spin_unlock (ptl); alloc:if (transparent_hugepage_enabled (vma) && - !transparent_hugepage_debug_cow ()) - new_page =alloc_hugepage_vma (transparent_hugepage_defrag (vma), - vma, haddr,numa_node_id (),0 ); -else + !transparent_hugepage_debug_cow ()) { +gfp_t gfp; + + gfp =alloc_hugepage_gfpmask (transparent_hugepage_defrag (vma),0 ); + new_page =alloc_hugepage_vma (gfp, vma, haddr,HPAGE_PMD_ORDER ); + }else new_page =NULL ;if (unlikely (!new_page)) {
mm/mempolicy.c +70 -0
76 lines not on this page · raw message
Signature · 1 line
2.1.0
Andrew Morton
maintainerQuoted 32 lines · On Tue, 20 Jan 2015 17:04:31 +0530 "Aneesh Kumar K.V"…
This make sure that we try to allocate hugepages from local node if allowed by mempolicy. If we can't, we fallback to small page allocation based on mempolicy. This is based on the observation that allocating pages on local node is more beneficial than allocating hugepages on remote node.
With this patch applied we may find transparent huge page allocation failures if the current node doesn't have enough freee hugepages. Before this patch such failures result in us retrying the allocation on other nodes in the numa node mask.
/**
+ *alloc_hugepage_vma: Allocate a hugepage for a VMA
+ * @gfp:+ * %GFP_USER user allocation. + * %GFP_KERNEL kernel allocations, + * %GFP_HIGHMEM highmem/user allocations, + * %GFP_FS allocation should not call back into a file system. + * %GFP_ATOMIC don't sleep.+ *
+ * @vma: Pointer to VMA or NULL if not available.
+ * @addr: Virtual Address of the allocation. Must be inside the VMA.
+ * @order: Order of the hugepage for gfp allocation.
+ *
+ * This functions allocate a huge page from the kernel page pool and applies
+ * a NUMA policy associated with the VMA or the current process.
+ * For policy other than %MPOL_INTERLEAVE, we make sure we allocate hugepage
+ * only from the current node if the current node is part of the node mask.
+ * If we can't allocate a hugepage we fail the allocation and don' try to fallback
+ * to other nodes in the node mask. If the current node is not part of node mask
+ * or if the NUMA policy isMPOL_INTERLEAVEwe use the allocator that can
Quoted 8 lines
+ * fallback to nodes in the policy node mask.
+ *
+ * When VMA is not NULL caller must holddown_readon themmap_semof the
+ *mm_structof the VMA to prevent it from going away. Should be used for
+ * all allocations for pages that will be mapped into
+ * user space. Returns NULL when no page can be allocated.
+ *
+ * Should be called with themm_semof the vma hold.
That's a pretty cruddy sentence, isn't it? Copied from alloc_pages_vma(). "vma->vm_mm->mmap_sem" would be better.
And it should tell us whether mmap_sem required a down_read or a down_write. What purpose is it serving?
Review
Quoted 4 lines
+ *
+ */(quoted diff) +2
-0+struct page *alloc_hugepage_vma(gfp_t gfp, struct vm_area_struct *vma, + unsigned long addr, int order)
This pointlessly bloats the kernel if CONFIG_TRANSPARENT_HUGEPAGE=n?
mm/mempolicy.c renamed from mm/mempolicy.c~mm-thp-allocate-transparent-hugepages-on-local-node-fix +3 -1
@@ -2030,6 +2030,7 @@ retry_cpuset:return page; } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE /** * alloc_hugepage_vma: Allocate a hugepage for a VMA * @gfp:
@@ -2057,7 +2058,7 @@ retry_cpuset:* all allocations for pages that will be mapped into * user space. Returns NULL when no page can be allocated. * -* Should be called with the mm_sem of the vma hold. +* Should be called with vma->vm_mm->mmap_sem held. * */ struct page *alloc_hugepage_vma(gfp_t gfp, struct vm_area_struct *vma,
@@ -2099,6 +2100,7 @@ alloc_with_fallback:*/ return alloc_pages_vma(gfp, order, vma, addr, node); } +#endif /** * alloc_pages_current - Allocate pages.
_
Quoted 8 lines · Andrew Morton
Quoted 1 line · On Tue, 20 Jan 2015 17:04:31 +0530 "Aneesh Kumar K.V"…
+ * Should be called with the
mm_semof the vma hold.That's a pretty cruddy sentence, isn't it? Copied from
alloc_pages_vma(). "vma->vm_mm->mmap_sem" would be better.And it should tell us whether
mmap_semrequired adown_reador adown_write. What purpose is it serving?
This is already said for mmap_sem further above this comment line, which should be just deleted (and from alloc_hugepage_vma comment too).
mm/mempolicy.c
Quoted 11 lines
Review
Quoted 4 lines
+ *
+ */(quoted diff) +2
-0+struct page *alloc_hugepage_vma(gfp_t gfp, struct vm_area_struct *vma, + unsigned long addr, int order)This pointlessly bloats the kernel if
CONFIG_TRANSPARENT_HUGEPAGE=n?mm/mempolicy.c renamed from mm/mempolicy.c~mm-thp-allocate-transparent-hugepages-on-local-node-fix +0
-0
How about this cleanup on top? I'm not fully decided on the GFP_TRANSHUGE test. This is potentially false positive, although I doubt anything else uses the same gfp mask bits.
Should "hugepage" be extra bool parameter instead? Should I #ifdef the parameter only for CONFIG_TRANSPARENT_HUGEPAGE, or is it not worth the ugliness?
--------8<--------
From: Vlastimil Babka <[email protected]>
Date: Mon, 26 Jan 2015 11:41:33 +0100
Subject: [PATCH] mm/mempolicy: merge alloc_hugepage_vma to alloc_pages_vma
Commit "mm/thp: allocate transparent hugepages on local node" has introduced alloc_hugepage_vma() to mm/mempolicy.c to perform a special policy for THP allocations. The function has the same interface as alloc_pages_vma(), shares a lot of boilerplate code and a long comment.
This patch merges the hugepage special case into alloc_pages_vma. The extra if condition should be cheap enough price to pay. We also prevent a (however unlikely) race with parallel mems_allowed update, which could make hugepage allocation restart only within the fallback call to alloc_hugepage_vma() and not reconsider the special rule in alloc_hugepage_vma().
Also by making sure mpol_cond_put(pol) is always called before actual allocation attempt, we can use a single exit path within the function.
Also update the comment for missing node parameter and obsolete reference to mm_sem.
--- include/linux/gfp.h | 4 +- mm/mempolicy.c | 121 ++++++++++++++++------------------------------------ 2 files changed, 39 insertions(+), 86 deletions(-)
include/linux/gfp.h +2 -2
@@ -335,8 +335,8 @@ alloc_pages(gfp_t gfp_mask, unsigned int order)extern struct page *alloc_pages_vma (gfp_t gfp_mask,int order,struct vm_area_struct *vma,unsigned long addr,int node); -extern struct page *alloc_hugepage_vma (gfp_t gfp,struct vm_area_struct *vma, -unsigned long addr,int order); +#define alloc_hugepage_vma (gfp_mask, vma, addr, order) \ + alloc_pages_vma(gfp_mask, order, vma, addr, numa_node_id())#else #define alloc_pages (gfp_mask, order) \ alloc_pages_node(numa_node_id(), gfp_mask, order)
mm/mempolicy.c +37 -84
155 lines not on this page · raw message
Signature · 9 lines
2.1.2 -- 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>
Quoted 22 lines · Vlastimil Babka
Quoted 8 lines · Andrew Morton
Quoted 1 line · On Tue, 20 Jan 2015 17:04:31 +0530 "Aneesh Kumar K.V"…
+ * Should be called with the
mm_semof the vma hold.That's a pretty cruddy sentence, isn't it? Copied from
alloc_pages_vma(). "vma->vm_mm->mmap_sem" would be better.And it should tell us whether
mmap_semrequired adown_reador adown_write. What purpose is it serving?This is already said for
mmap_semfurther above this comment line, which should be just deleted (and fromalloc_hugepage_vmacomment too).Quoted 9 lines
Review
Quoted 4 lines
+ *
+ */(quoted diff) +2
-0+struct page *alloc_hugepage_vma(gfp_t gfp, struct vm_area_struct *vma, + unsigned long addr, int order)This pointlessly bloats the kernel if
CONFIG_TRANSPARENT_HUGEPAGE=n?
Quoted 6 lines
mm/mempolicy.c
Quoted 2 lines
mm/mempolicy.c renamed from mm/mempolicy.c~mm-thp-allocate-transparent-hugepages-on-local-node-fix +0
-0How about this cleanup on top? I'm not fully decided on the
GFP_TRANSHUGEtest. This is potentially false positive, although I doubt anything else uses the same gfp mask bits.
This info on gfp mask should be in commit message.
And what about WARN_ON_ONCE() if we the matching bits with !TRANSPARENT_HUGEPAGE?
Quoted 3 lines
Should "hugepage" be extra bool parameter instead? Should I #ifdef the parameter only for
CONFIG_TRANSPARENT_HUGEPAGE, or is it not worth the ugliness?
Do we have spare gfp bit? ;)
Signature · 7 lines
Kirill A. Shutemov -- 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>
Quoted 23 lines · Kirill A. Shutemov
Quoted 22 lines · Vlastimil Babka
Quoted 8 lines · Andrew Morton
Quoted 1 line · On Tue, 20 Jan 2015 17:04:31 +0530 "Aneesh Kumar K.V"…
+ * Should be called with the
mm_semof the vma hold.That's a pretty cruddy sentence, isn't it? Copied from
alloc_pages_vma(). "vma->vm_mm->mmap_sem" would be better.And it should tell us whether
mmap_semrequired adown_reador adown_write. What purpose is it serving?This is already said for
mmap_semfurther above this comment line, which should be just deleted (and fromalloc_hugepage_vmacomment too).Quoted 9 lines
Review
Quoted 4 lines
+ *
+ */(quoted diff) +2
-0+struct page *alloc_hugepage_vma(gfp_t gfp, struct vm_area_struct *vma, + unsigned long addr, int order)This pointlessly bloats the kernel if
CONFIG_TRANSPARENT_HUGEPAGE=n?
Quoted 8 lines
Quoted 6 lines
mm/mempolicy.c
Quoted 2 lines
mm/mempolicy.c renamed from mm/mempolicy.c~mm-thp-allocate-transparent-hugepages-on-local-node-fix +0
-0How about this cleanup on top? I'm not fully decided on the
GFP_TRANSHUGEtest. This is potentially false positive, although I doubt anything else uses the same gfp mask bits.This info on gfp mask should be in commit message.
Right. Wanted to get some consensus first.
Quoted 2 lines
And what about
WARN_ON_ONCE()if we the matching bits with !TRANSPARENT_HUGEPAGE?
Hmm, can't say I like that, but could work.
Quoted 5 lines
Quoted 3 lines
Should "hugepage" be extra bool parameter instead? Should I #ifdef the parameter only for
CONFIG_TRANSPARENT_HUGEPAGE, or is it not worth the ugliness?Do we have spare gfp bit? ;)
Seems we have defined 24 out of 32. Not too much to spare, and the use case here is very narrow.
--
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>
Quoted 22 lines · Vlastimil Babka
Quoted 8 lines · Andrew Morton
Quoted 1 line · On Tue, 20 Jan 2015 17:04:31 +0530 "Aneesh Kumar K.V"…
+ * Should be called with the
mm_semof the vma hold.That's a pretty cruddy sentence, isn't it? Copied from
alloc_pages_vma(). "vma->vm_mm->mmap_sem" would be better.And it should tell us whether
mmap_semrequired adown_reador adown_write. What purpose is it serving?This is already said for
mmap_semfurther above this comment line, which should be just deleted (and fromalloc_hugepage_vmacomment too).Quoted 9 lines
Review
Quoted 4 lines
+ *
+ */(quoted diff) +2
-0+struct page *alloc_hugepage_vma(gfp_t gfp, struct vm_area_struct *vma, + unsigned long addr, int order)This pointlessly bloats the kernel if
CONFIG_TRANSPARENT_HUGEPAGE=n?
Quoted 6 lines
mm/mempolicy.c
Quoted 2 lines
mm/mempolicy.c renamed from mm/mempolicy.c~mm-thp-allocate-transparent-hugepages-on-local-node-fix +0
-0How about this cleanup on top? I'm not fully decided on the
GFP_TRANSHUGEtest. This is potentially false positive, although I doubt anything else uses the same gfp mask bits.
IMHO I found that to be more complex.
Quoted 4 lines
Should "hugepage" be extra bool parameter instead? Should I #ifdef the parameter only for
CONFIG_TRANSPARENT_HUGEPAGE, or is it not worth the ugliness?
I guess if we really want to consolidate both the functions, we should try the above, without all those #ifdef. It is just one extra arg. But then is the reason to consolidate that strong ?
-aneesh
--
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>
Quoted 24 lines · Aneesh Kumar K.V
Quoted 22 lines · Vlastimil Babka
Quoted 8 lines · Andrew Morton
Quoted 1 line · On Tue, 20 Jan 2015 17:04:31 +0530 "Aneesh Kumar K.V"…
+ * Should be called with the
mm_semof the vma hold.That's a pretty cruddy sentence, isn't it? Copied from
alloc_pages_vma(). "vma->vm_mm->mmap_sem" would be better.And it should tell us whether
mmap_semrequired adown_reador adown_write. What purpose is it serving?This is already said for
mmap_semfurther above this comment line, which should be just deleted (and fromalloc_hugepage_vmacomment too).Quoted 9 lines
Review
Quoted 4 lines
+ *
+ */(quoted diff) +2
-0+struct page *alloc_hugepage_vma(gfp_t gfp, struct vm_area_struct *vma, + unsigned long addr, int order)This pointlessly bloats the kernel if
CONFIG_TRANSPARENT_HUGEPAGE=n?
Quoted 8 lines
Quoted 6 lines
mm/mempolicy.c
Quoted 2 lines
mm/mempolicy.c renamed from mm/mempolicy.c~mm-thp-allocate-transparent-hugepages-on-local-node-fix +0
-0How about this cleanup on top? I'm not fully decided on the
GFP_TRANSHUGEtest. This is potentially false positive, although I doubt anything else uses the same gfp mask bits.IMHO I found that to be more complex.
I think it's better consolidated, but clearly there is no universal truth here. So I'll let others add their opinions.
Quoted 7 lines
Quoted 4 lines
Should "hugepage" be extra bool parameter instead? Should I #ifdef the parameter only for
CONFIG_TRANSPARENT_HUGEPAGE, or is it not worth the ugliness?I guess if we really want to consolidate both the functions, we should try the above, without all those #ifdef. It is just one extra arg. But
OK, new version below.
Quoted 1 line
then is the reason to consolidate that strong ?
It reduces duplication. I also tried some bloat-o-meter. The extra param seems to have some tiny cost, but overall it looks good.
./scripts/bloat-o-meter mm/mempolicy.o mempolicy1.o add/remove: 1/2 grow/shrink: 4/0 up/down: 195/-340 (-145)
function old new delta alloc_pages_vma 327 502 +175 init_nodemask_of_mempolicy 386 394 +8 interleave_nid.part - 6 +6 new_page 195 198 +3 interleave_nid 57 60 +3
mpol_cond_put 23 - -23alloc_hugepage_vma 317 - -317
add/remove: 0/0 grow/shrink: 1/1 up/down: 16/-28 (-12)
function old new delta do_huge_pmd_anonymous_page 968 984 +16
do_huge_pmd_wp_page 2566 2538 -28
-------8<-------
From: Vlastimil Babka <[email protected]>
Date: Mon, 26 Jan 2015 11:41:33 +0100
Subject: [PATCH] mm/mempolicy: merge alloc_hugepage_vma to alloc_pages_vma
Commit "mm/thp: Allocate transparent hugepages on local node" has introduced alloc_hugepage_vma() to mm/mempolicy.c to perform a special policy for THP allocations. The function has the same interface as alloc_pages_vma(), shares a lot of boilerplate code and a long comment.
This patch merges the hugepage special case into alloc_pages_vma. The extra if condition should be cheap enough price to pay. We also prevent a (however unlikely) race with parallel mems_allowed update, which could make hugepage allocation restart only within the fallback call to alloc_hugepage_vma() and not reconsider the special rule in alloc_hugepage_vma().
Also by making sure mpol_cond_put(pol) is always called before actual allocation attempt, we can use a single exit path within the function.
Also update the comment for missing node parameter and obsolete reference to mm_sem.
--- include/linux/gfp.h | 12 +++--- mm/mempolicy.c | 118 +++++++++++++++------------------------------------- 2 files changed, 39 insertions(+), 91 deletions(-)
include/linux/gfp.h +6 -6
@@ -334,22 +334,22 @@ alloc_pages(gfp_t gfp_mask, unsigned int order) }extern struct page *alloc_pages_vma (gfp_t gfp_mask,int order,struct vm_area_struct *vma,unsigned long addr, -int node); -extern struct page *alloc_hugepage_vma (gfp_t gfp,struct vm_area_struct *vma, -unsigned long addr,int order); +int node,bool hugepage); +#define alloc_hugepage_vma (gfp_mask, vma, addr, order) \ + alloc_pages_vma(gfp_mask, order, vma, addr, numa_node_id(), true)#else #define alloc_pages (gfp_mask, order) \ alloc_pages_node(numa_node_id(), gfp_mask, order) -#define alloc_pages_vma (gfp_mask, order, vma, addr, node) \ +#define alloc_pages_vma (gfp_mask, order, vma, addr, node, false)\ alloc_pages(gfp_mask, order)#define alloc_hugepage_vma (gfp_mask, vma, addr, order) \ alloc_pages(gfp_mask, order)#endif #define alloc_page (gfp_mask) alloc_pages(gfp_mask, 0)#define alloc_page_vma (gfp_mask, vma, addr) \ - alloc_pages_vma(gfp_mask, 0, vma, addr, numa_node_id()) + alloc_pages_vma(gfp_mask, 0, vma, addr, numa_node_id(), false)#define alloc_page_vma_node (gfp_mask, vma, addr, node) \ - alloc_pages_vma(gfp_mask, 0, vma, addr, node) + alloc_pages_vma(gfp_mask, 0, vma, addr, node, false)extern struct page *alloc_kmem_pages (gfp_t gfp_mask,unsigned int order); extern struct page *alloc_kmem_pages_node(int nid, gfp_t gfp_mask,
mm/mempolicy.c +33 -85
153 lines not on this page · raw message
Signature · 8 lines
2.1.4 -- 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>