* [RFC 1/4] mm, thp: stop preallocating hugepages in khugepaged
2015-05-11 14:35 [RFC 0/4] Outsourcing page fault THP allocations to khugepaged Vlastimil Babka
@ 2015-05-11 14:35 ` Vlastimil Babka
2015-06-18 0:34 ` David Rientjes
2015-05-11 14:35 ` [RFC 2/4] mm, thp: khugepaged checks for THP allocability before scanning Vlastimil Babka
` (2 subsequent siblings)
3 siblings, 1 reply; 10+ messages in thread
From: Vlastimil Babka @ 2015-05-11 14:35 UTC (permalink / raw)
To: linux-mm
Cc: linux-kernel, Andrew Morton, Hugh Dickins, Andrea Arcangeli,
Kirill A. Shutemov, Rik van Riel, Mel Gorman, Michal Hocko,
Alex Thorlton, David Rientjes, Vlastimil Babka
Khugepaged tries to preallocate a hugepage before scanning for THP collapse
candidates. If the preallocation fails, scanning is not attempted. This makes
sense, but it is only restricted to !NUMA configurations, where it does not
need to predict on which node to preallocate.
Besides the !NUMA restriction, the preallocated page may also end up being
unused and put back when no collapse candidate is found. I have observed the
thp_collapse_alloc vmstat counter to have 3+ times the value of the counter
of actually collapsed pages in /sys/.../khugepaged/pages_collapsed. On the
other hand, the periodic hugepage allocation attempts involving sync
compaction can be beneficial for the antifragmentation mechanism, but that's
however harder to evaluate.
The following patch will introduce per-node THP availability tracking, which
has more benefits than current preallocation and is applicable to CONFIG_NUMA.
We can therefore remove the preallocation, which also allows a cleanup of the
functions involved in khugepaged allocations. Another small benefit of the
patch is that NUMA configs can now reuse an allocated hugepage for another
collapse attempt, if the previous one was for the same node and failed.
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
mm/huge_memory.c | 150 ++++++++++++++++++++-----------------------------------
1 file changed, 53 insertions(+), 97 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 078832c..565864b 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -765,9 +765,9 @@ static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
return 0;
}
-static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
+static inline gfp_t alloc_hugepage_gfpmask(int defrag)
{
- return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
+ return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT));
}
/* Caller must hold page table lock. */
@@ -825,7 +825,7 @@ int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
}
return 0;
}
- gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
+ gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma));
page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
if (unlikely(!page)) {
count_vm_event(THP_FAULT_FALLBACK);
@@ -1116,7 +1116,7 @@ int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
alloc:
if (transparent_hugepage_enabled(vma) &&
!transparent_hugepage_debug_cow()) {
- huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
+ huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma));
new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
} else
new_page = NULL;
@@ -2318,39 +2318,41 @@ static int khugepaged_find_target_node(void)
return target_node;
}
-static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
+static inline struct page *alloc_hugepage_node(gfp_t gfp, int node)
{
- if (IS_ERR(*hpage)) {
- if (!*wait)
- return false;
-
- *wait = false;
- *hpage = NULL;
- khugepaged_alloc_sleep();
- } else if (*hpage) {
- put_page(*hpage);
- *hpage = NULL;
- }
-
- return true;
+ gfp |= __GFP_THISNODE | __GFP_OTHER_NODE;
+ return alloc_pages_exact_node(node, gfp, HPAGE_PMD_ORDER);
+}
+#else
+static int khugepaged_find_target_node(void)
+{
+ return 0;
}
-static struct page *
-khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
- struct vm_area_struct *vma, unsigned long address,
- int node)
+static inline struct page *alloc_hugepage_node(gfp_t gfp, int node)
{
- VM_BUG_ON_PAGE(*hpage, *hpage);
+ return alloc_pages(gfp, HPAGE_PMD_ORDER);
+}
+#endif
+static struct page
+*khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
+{
/*
- * Before allocating the hugepage, release the mmap_sem read lock.
- * The allocation can take potentially a long time if it involves
- * sync compaction, and we do not need to hold the mmap_sem during
- * that. We will recheck the vma after taking it again in write mode.
+ * If we allocated a hugepage previously and failed to collapse, reuse
+ * the page, unless it's on different NUMA node.
*/
- up_read(&mm->mmap_sem);
+ if (!IS_ERR_OR_NULL(*hpage)) {
+ if (IS_ENABLED(CONFIG_NUMA) && page_to_nid(*hpage) != node) {
+ put_page(*hpage);
+ *hpage = NULL;
+ } else {
+ return *hpage;
+ }
+ }
+
+ *hpage = alloc_hugepage_node(gfp, node);
- *hpage = alloc_pages_exact_node(node, gfp, HPAGE_PMD_ORDER);
if (unlikely(!*hpage)) {
count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
*hpage = ERR_PTR(-ENOMEM);
@@ -2360,60 +2362,6 @@ khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
count_vm_event(THP_COLLAPSE_ALLOC);
return *hpage;
}
-#else
-static int khugepaged_find_target_node(void)
-{
- return 0;
-}
-
-static inline struct page *alloc_hugepage(int defrag)
-{
- return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
- HPAGE_PMD_ORDER);
-}
-
-static struct page *khugepaged_alloc_hugepage(bool *wait)
-{
- struct page *hpage;
-
- do {
- hpage = alloc_hugepage(khugepaged_defrag());
- if (!hpage) {
- count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
- if (!*wait)
- return NULL;
-
- *wait = false;
- khugepaged_alloc_sleep();
- } else
- count_vm_event(THP_COLLAPSE_ALLOC);
- } while (unlikely(!hpage) && likely(khugepaged_enabled()));
-
- return hpage;
-}
-
-static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
-{
- if (!*hpage)
- *hpage = khugepaged_alloc_hugepage(wait);
-
- if (unlikely(!*hpage))
- return false;
-
- return true;
-}
-
-static struct page *
-khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
- struct vm_area_struct *vma, unsigned long address,
- int node)
-{
- up_read(&mm->mmap_sem);
- VM_BUG_ON(!*hpage);
-
- return *hpage;
-}
-#endif
static bool hugepage_vma_check(struct vm_area_struct *vma)
{
@@ -2449,17 +2397,25 @@ static void collapse_huge_page(struct mm_struct *mm,
VM_BUG_ON(address & ~HPAGE_PMD_MASK);
- /* Only allocate from the target node */
- gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) |
- __GFP_THISNODE;
+ /*
+ * Determine the flags relevant for both hugepage allocation and memcg
+ * charge. Hugepage allocation may still add __GFP_THISNODE and
+ * __GFP_OTHER_NODE, which memcg ignores.
+ */
+ gfp = alloc_hugepage_gfpmask(khugepaged_defrag());
- /* release the mmap_sem read lock. */
- new_page = khugepaged_alloc_page(hpage, gfp, mm, vma, address, node);
+ /*
+ * Before allocating the hugepage, release the mmap_sem read lock.
+ * The allocation can take potentially a long time if it involves
+ * sync compaction, and we do not need to hold the mmap_sem during
+ * that. We will recheck the vma after taking it again in write mode.
+ */
+ up_read(&mm->mmap_sem);
+ new_page = khugepaged_alloc_page(hpage, gfp, node);
if (!new_page)
return;
- if (unlikely(mem_cgroup_try_charge(new_page, mm,
- gfp, &memcg)))
+ if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg)))
return;
/*
@@ -2788,15 +2744,9 @@ static void khugepaged_do_scan(void)
{
struct page *hpage = NULL;
unsigned int progress = 0, pass_through_head = 0;
- unsigned int pages = khugepaged_pages_to_scan;
- bool wait = true;
-
- barrier(); /* write khugepaged_pages_to_scan to local stack */
+ unsigned int pages = READ_ONCE(khugepaged_pages_to_scan);
while (progress < pages) {
- if (!khugepaged_prealloc_page(&hpage, &wait))
- break;
-
cond_resched();
if (unlikely(kthread_should_stop() || freezing(current)))
@@ -2812,6 +2762,12 @@ static void khugepaged_do_scan(void)
else
progress = pages;
spin_unlock(&khugepaged_mm_lock);
+
+ /* THP allocation has failed during collapse */
+ if (IS_ERR(hpage)) {
+ khugepaged_alloc_sleep();
+ break;
+ }
}
if (!IS_ERR_OR_NULL(hpage))
--
2.1.4
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [RFC 1/4] mm, thp: stop preallocating hugepages in khugepaged
2015-05-11 14:35 ` [RFC 1/4] mm, thp: stop preallocating hugepages in khugepaged Vlastimil Babka
@ 2015-06-18 0:34 ` David Rientjes
0 siblings, 0 replies; 10+ messages in thread
From: David Rientjes @ 2015-06-18 0:34 UTC (permalink / raw)
To: Vlastimil Babka
Cc: linux-mm, linux-kernel, Andrew Morton, Hugh Dickins,
Andrea Arcangeli, Kirill A. Shutemov, Rik van Riel, Mel Gorman,
Michal Hocko, Alex Thorlton
On Mon, 11 May 2015, Vlastimil Babka wrote:
> Khugepaged tries to preallocate a hugepage before scanning for THP collapse
> candidates. If the preallocation fails, scanning is not attempted. This makes
> sense, but it is only restricted to !NUMA configurations, where it does not
> need to predict on which node to preallocate.
>
> Besides the !NUMA restriction, the preallocated page may also end up being
> unused and put back when no collapse candidate is found. I have observed the
> thp_collapse_alloc vmstat counter to have 3+ times the value of the counter
> of actually collapsed pages in /sys/.../khugepaged/pages_collapsed. On the
> other hand, the periodic hugepage allocation attempts involving sync
> compaction can be beneficial for the antifragmentation mechanism, but that's
> however harder to evaluate.
>
> The following patch will introduce per-node THP availability tracking, which
> has more benefits than current preallocation and is applicable to CONFIG_NUMA.
> We can therefore remove the preallocation, which also allows a cleanup of the
> functions involved in khugepaged allocations. Another small benefit of the
> patch is that NUMA configs can now reuse an allocated hugepage for another
> collapse attempt, if the previous one was for the same node and failed.
>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
I think this is fine if the rest of the series is adopted, and I
understand how the removal and cleanup is easier when done first before
the following patches. I think you can unify alloc_hugepage_node() for
both NUMA and !NUMA configs and inline it in khugepaged_alloc_page().
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC 2/4] mm, thp: khugepaged checks for THP allocability before scanning
2015-05-11 14:35 [RFC 0/4] Outsourcing page fault THP allocations to khugepaged Vlastimil Babka
2015-05-11 14:35 ` [RFC 1/4] mm, thp: stop preallocating hugepages in khugepaged Vlastimil Babka
@ 2015-05-11 14:35 ` Vlastimil Babka
2015-06-18 1:00 ` David Rientjes
2015-05-11 14:35 ` [RFC 3/4] mm, thp: try fault allocations only if we expect them to succeed Vlastimil Babka
2015-05-11 14:35 ` [RFC 4/4] mm, thp: wake up khugepaged when huge page is not available Vlastimil Babka
3 siblings, 1 reply; 10+ messages in thread
From: Vlastimil Babka @ 2015-05-11 14:35 UTC (permalink / raw)
To: linux-mm
Cc: linux-kernel, Andrew Morton, Hugh Dickins, Andrea Arcangeli,
Kirill A. Shutemov, Rik van Riel, Mel Gorman, Michal Hocko,
Alex Thorlton, David Rientjes, Vlastimil Babka
Khugepaged could be scanning for collapse candidates uselessly, if it cannot
allocate a hugepage in the end. The hugepage preallocation mechanism prevented
this, but only for !NUMA configurations. It was removed by the previous patch,
and this patch replaces it with a more generic mechanism.
The patch itroduces a thp_avail_nodes nodemask, which initially assumes that
hugepage can be allocated on any node. Whenever khugepaged fails to allocate
a hugepage, it clears the corresponding node bit. Before scanning for collapse
candidates, it tries to allocate a hugepage on each online node with the bit
cleared, and set it back on success. It tries to hold on to the hugepage if
it doesn't hold any other yet. But the assumption is that even if the hugepage
is freed back, it should be possible to allocate it in near future without
further reclaim and compaction attempts.
During the scaning, khugepaged avoids collapsing on nodes with the bit cleared,
as soon as possible. If no nodes have hugepages available, scanning is skipped
altogether.
During testing, the patch did not show much difference in preventing
thp_collapse_failed events from khugepaged, but this can be attributed to the
sync compaction, which only khugepaged is allowed to use, and which is
heavyweight enough to succeed frequently enough nowadays. The next patch will
however extend the nodemask check to page fault context, where it has much
larger impact. Also, with the future plan to convert THP collapsing to
task_work context, this patch is a preparation to avoid useless scanning or
heavyweight THP allocations in that context.
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
mm/huge_memory.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 63 insertions(+), 8 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 565864b..b86a72a 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -102,7 +102,7 @@ struct khugepaged_scan {
static struct khugepaged_scan khugepaged_scan = {
.mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
};
-
+static nodemask_t thp_avail_nodes = NODE_MASK_ALL;
static int set_recommended_min_free_kbytes(void)
{
@@ -2273,6 +2273,14 @@ static bool khugepaged_scan_abort(int nid)
int i;
/*
+ * If it's clear that we are going to select a node where THP
+ * allocation is unlikely to succeed, abort
+ */
+ if (khugepaged_node_load[nid] == (HPAGE_PMD_NR / 2) &&
+ !node_isset(nid, thp_avail_nodes))
+ return true;
+
+ /*
* If zone_reclaim_mode is disabled, then no extra effort is made to
* allocate memory locally.
*/
@@ -2356,6 +2364,7 @@ static struct page
if (unlikely(!*hpage)) {
count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
*hpage = ERR_PTR(-ENOMEM);
+ node_clear(node, thp_avail_nodes);
return NULL;
}
@@ -2363,6 +2372,42 @@ static struct page
return *hpage;
}
+/* Return true, if THP should be allocatable on at least one node */
+static bool khugepaged_check_nodes(struct page **hpage)
+{
+ bool ret = false;
+ int nid;
+ struct page *newpage = NULL;
+ gfp_t gfp = alloc_hugepage_gfpmask(khugepaged_defrag());
+
+ for_each_online_node(nid) {
+ if (node_isset(nid, thp_avail_nodes)) {
+ ret = true;
+ continue;
+ }
+
+ newpage = alloc_hugepage_node(gfp, nid);
+
+ if (newpage) {
+ node_set(nid, thp_avail_nodes);
+ ret = true;
+ /*
+ * Heuristic - try to hold on to the page for collapse
+ * scanning, if we don't hold any yet.
+ */
+ if (IS_ERR_OR_NULL(*hpage)) {
+ *hpage = newpage;
+ //NIXME: should we count all/no allocations?
+ count_vm_event(THP_COLLAPSE_ALLOC);
+ } else {
+ put_page(newpage);
+ }
+ }
+ }
+
+ return ret;
+}
+
static bool hugepage_vma_check(struct vm_area_struct *vma)
{
if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
@@ -2590,6 +2635,10 @@ out_unmap:
pte_unmap_unlock(pte, ptl);
if (ret) {
node = khugepaged_find_target_node();
+ if (!node_isset(node, thp_avail_nodes)) {
+ ret = 0;
+ goto out;
+ }
/* collapse_huge_page will return with the mmap_sem released */
collapse_huge_page(mm, address, hpage, vma, node);
}
@@ -2740,12 +2789,16 @@ static int khugepaged_wait_event(void)
kthread_should_stop();
}
-static void khugepaged_do_scan(void)
+/* Return false if THP allocation failed, true otherwise */
+static bool khugepaged_do_scan(void)
{
struct page *hpage = NULL;
unsigned int progress = 0, pass_through_head = 0;
unsigned int pages = READ_ONCE(khugepaged_pages_to_scan);
+ if (!khugepaged_check_nodes(&hpage))
+ return false;
+
while (progress < pages) {
cond_resched();
@@ -2764,14 +2817,14 @@ static void khugepaged_do_scan(void)
spin_unlock(&khugepaged_mm_lock);
/* THP allocation has failed during collapse */
- if (IS_ERR(hpage)) {
- khugepaged_alloc_sleep();
- break;
- }
+ if (IS_ERR(hpage))
+ return false;
}
if (!IS_ERR_OR_NULL(hpage))
put_page(hpage);
+
+ return true;
}
static void khugepaged_wait_work(void)
@@ -2800,8 +2853,10 @@ static int khugepaged(void *none)
set_user_nice(current, MAX_NICE);
while (!kthread_should_stop()) {
- khugepaged_do_scan();
- khugepaged_wait_work();
+ if (khugepaged_do_scan())
+ khugepaged_wait_work();
+ else
+ khugepaged_alloc_sleep();
}
spin_lock(&khugepaged_mm_lock);
--
2.1.4
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [RFC 2/4] mm, thp: khugepaged checks for THP allocability before scanning
2015-05-11 14:35 ` [RFC 2/4] mm, thp: khugepaged checks for THP allocability before scanning Vlastimil Babka
@ 2015-06-18 1:00 ` David Rientjes
2015-06-23 15:41 ` Vlastimil Babka
0 siblings, 1 reply; 10+ messages in thread
From: David Rientjes @ 2015-06-18 1:00 UTC (permalink / raw)
To: Vlastimil Babka
Cc: linux-mm, linux-kernel, Andrew Morton, Hugh Dickins,
Andrea Arcangeli, Kirill A. Shutemov, Rik van Riel, Mel Gorman,
Michal Hocko, Alex Thorlton
On Mon, 11 May 2015, Vlastimil Babka wrote:
> Khugepaged could be scanning for collapse candidates uselessly, if it cannot
> allocate a hugepage in the end. The hugepage preallocation mechanism prevented
> this, but only for !NUMA configurations. It was removed by the previous patch,
> and this patch replaces it with a more generic mechanism.
>
> The patch itroduces a thp_avail_nodes nodemask, which initially assumes that
> hugepage can be allocated on any node. Whenever khugepaged fails to allocate
> a hugepage, it clears the corresponding node bit. Before scanning for collapse
> candidates, it tries to allocate a hugepage on each online node with the bit
> cleared, and set it back on success. It tries to hold on to the hugepage if
> it doesn't hold any other yet. But the assumption is that even if the hugepage
> is freed back, it should be possible to allocate it in near future without
> further reclaim and compaction attempts.
>
> During the scaning, khugepaged avoids collapsing on nodes with the bit cleared,
> as soon as possible. If no nodes have hugepages available, scanning is skipped
> altogether.
>
I'm not exactly sure what you mean by avoiding to do something as soon as
possible.
> During testing, the patch did not show much difference in preventing
> thp_collapse_failed events from khugepaged, but this can be attributed to the
> sync compaction, which only khugepaged is allowed to use, and which is
> heavyweight enough to succeed frequently enough nowadays. The next patch will
> however extend the nodemask check to page fault context, where it has much
> larger impact. Also, with the future plan to convert THP collapsing to
> task_work context, this patch is a preparation to avoid useless scanning or
> heavyweight THP allocations in that context.
>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> ---
> mm/huge_memory.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-------
> 1 file changed, 63 insertions(+), 8 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 565864b..b86a72a 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -102,7 +102,7 @@ struct khugepaged_scan {
> static struct khugepaged_scan khugepaged_scan = {
> .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
> };
> -
> +static nodemask_t thp_avail_nodes = NODE_MASK_ALL;
Seems like it should have khugepaged in its name so it's understood that
the nodemask doesn't need to be synchronized, even though it will later be
read outside of khugepaged, or at least a comment to say only khugepaged
can store to it.
>
> static int set_recommended_min_free_kbytes(void)
> {
> @@ -2273,6 +2273,14 @@ static bool khugepaged_scan_abort(int nid)
> int i;
>
> /*
> + * If it's clear that we are going to select a node where THP
> + * allocation is unlikely to succeed, abort
> + */
> + if (khugepaged_node_load[nid] == (HPAGE_PMD_NR / 2) &&
> + !node_isset(nid, thp_avail_nodes))
> + return true;
> +
> + /*
> * If zone_reclaim_mode is disabled, then no extra effort is made to
> * allocate memory locally.
> */
If khugepaged_node_load for a node doesn't reach HPAGE_PMD_NR / 2, then
this doesn't cause an abort. I don't think it's necessary to try to
optimize and abort the scan early when this is met, I think this should
only be checked before collapse_huge_page().
> @@ -2356,6 +2364,7 @@ static struct page
> if (unlikely(!*hpage)) {
> count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
> *hpage = ERR_PTR(-ENOMEM);
> + node_clear(node, thp_avail_nodes);
> return NULL;
> }
>
> @@ -2363,6 +2372,42 @@ static struct page
> return *hpage;
> }
>
> +/* Return true, if THP should be allocatable on at least one node */
> +static bool khugepaged_check_nodes(struct page **hpage)
> +{
> + bool ret = false;
> + int nid;
> + struct page *newpage = NULL;
> + gfp_t gfp = alloc_hugepage_gfpmask(khugepaged_defrag());
> +
> + for_each_online_node(nid) {
> + if (node_isset(nid, thp_avail_nodes)) {
> + ret = true;
> + continue;
> + }
> +
> + newpage = alloc_hugepage_node(gfp, nid);
> +
> + if (newpage) {
> + node_set(nid, thp_avail_nodes);
> + ret = true;
> + /*
> + * Heuristic - try to hold on to the page for collapse
> + * scanning, if we don't hold any yet.
> + */
> + if (IS_ERR_OR_NULL(*hpage)) {
> + *hpage = newpage;
> + //NIXME: should we count all/no allocations?
> + count_vm_event(THP_COLLAPSE_ALLOC);
Seems like we'd only count the event when the node load has selected a
target node and the hugepage that is allocated here is used, but if this
approach is adopted then I think you'll need to introduce a new event to
track when a hugepage is allocated and subsequently dropped.
> + } else {
> + put_page(newpage);
> + }
Eek, rather than do put_page() why not store a preallocated hugepage for
every node and let khugepaged_alloc_page() use it? It would be
unfortunate that page_to_nid(*hpage) may not equal the target node after
scanning.
> + }
> + }
> +
> + return ret;
> +}
> +
> static bool hugepage_vma_check(struct vm_area_struct *vma)
> {
> if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
> @@ -2590,6 +2635,10 @@ out_unmap:
> pte_unmap_unlock(pte, ptl);
> if (ret) {
> node = khugepaged_find_target_node();
> + if (!node_isset(node, thp_avail_nodes)) {
> + ret = 0;
> + goto out;
> + }
> /* collapse_huge_page will return with the mmap_sem released */
> collapse_huge_page(mm, address, hpage, vma, node);
> }
> @@ -2740,12 +2789,16 @@ static int khugepaged_wait_event(void)
> kthread_should_stop();
> }
>
> -static void khugepaged_do_scan(void)
> +/* Return false if THP allocation failed, true otherwise */
> +static bool khugepaged_do_scan(void)
> {
> struct page *hpage = NULL;
> unsigned int progress = 0, pass_through_head = 0;
> unsigned int pages = READ_ONCE(khugepaged_pages_to_scan);
>
> + if (!khugepaged_check_nodes(&hpage))
> + return false;
> +
> while (progress < pages) {
> cond_resched();
>
> @@ -2764,14 +2817,14 @@ static void khugepaged_do_scan(void)
> spin_unlock(&khugepaged_mm_lock);
>
> /* THP allocation has failed during collapse */
> - if (IS_ERR(hpage)) {
> - khugepaged_alloc_sleep();
> - break;
> - }
> + if (IS_ERR(hpage))
> + return false;
> }
>
> if (!IS_ERR_OR_NULL(hpage))
> put_page(hpage);
> +
> + return true;
> }
>
> static void khugepaged_wait_work(void)
> @@ -2800,8 +2853,10 @@ static int khugepaged(void *none)
> set_user_nice(current, MAX_NICE);
>
> while (!kthread_should_stop()) {
> - khugepaged_do_scan();
> - khugepaged_wait_work();
> + if (khugepaged_do_scan())
> + khugepaged_wait_work();
> + else
> + khugepaged_alloc_sleep();
> }
>
> spin_lock(&khugepaged_mm_lock);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC 2/4] mm, thp: khugepaged checks for THP allocability before scanning
2015-06-18 1:00 ` David Rientjes
@ 2015-06-23 15:41 ` Vlastimil Babka
0 siblings, 0 replies; 10+ messages in thread
From: Vlastimil Babka @ 2015-06-23 15:41 UTC (permalink / raw)
To: David Rientjes
Cc: linux-mm, linux-kernel, Andrew Morton, Hugh Dickins,
Andrea Arcangeli, Kirill A. Shutemov, Rik van Riel, Mel Gorman,
Michal Hocko, Alex Thorlton
On 06/18/2015 03:00 AM, David Rientjes wrote:
> On Mon, 11 May 2015, Vlastimil Babka wrote:
>
>> Khugepaged could be scanning for collapse candidates uselessly, if it cannot
>> allocate a hugepage in the end. The hugepage preallocation mechanism prevented
>> this, but only for !NUMA configurations. It was removed by the previous patch,
>> and this patch replaces it with a more generic mechanism.
>>
>> The patch itroduces a thp_avail_nodes nodemask, which initially assumes that
>> hugepage can be allocated on any node. Whenever khugepaged fails to allocate
>> a hugepage, it clears the corresponding node bit. Before scanning for collapse
>> candidates, it tries to allocate a hugepage on each online node with the bit
>> cleared, and set it back on success. It tries to hold on to the hugepage if
>> it doesn't hold any other yet. But the assumption is that even if the hugepage
>> is freed back, it should be possible to allocate it in near future without
>> further reclaim and compaction attempts.
>>
>> During the scaning, khugepaged avoids collapsing on nodes with the bit cleared,
>> as soon as possible. If no nodes have hugepages available, scanning is skipped
>> altogether.
>>
>
> I'm not exactly sure what you mean by avoiding to do something as soon as
> possible.
That's referring to the check when node_load is half the pmd size, which
you want me to remove :)
>> During testing, the patch did not show much difference in preventing
>> thp_collapse_failed events from khugepaged, but this can be attributed to the
>> sync compaction, which only khugepaged is allowed to use, and which is
>> heavyweight enough to succeed frequently enough nowadays. The next patch will
>> however extend the nodemask check to page fault context, where it has much
>> larger impact. Also, with the future plan to convert THP collapsing to
>> task_work context, this patch is a preparation to avoid useless scanning or
>> heavyweight THP allocations in that context.
>>
>> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
>> ---
>> mm/huge_memory.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-------
>> 1 file changed, 63 insertions(+), 8 deletions(-)
>>
>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
>> index 565864b..b86a72a 100644
>> --- a/mm/huge_memory.c
>> +++ b/mm/huge_memory.c
>> @@ -102,7 +102,7 @@ struct khugepaged_scan {
>> static struct khugepaged_scan khugepaged_scan = {
>> .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
>> };
>> -
>> +static nodemask_t thp_avail_nodes = NODE_MASK_ALL;
>
> Seems like it should have khugepaged in its name so it's understood that
> the nodemask doesn't need to be synchronized, even though it will later be
> read outside of khugepaged, or at least a comment to say only khugepaged
> can store to it.
After patch 3, bits can be cleared from the mask also outside of
khugepaged, i.e. when THP allocations fail on page fault.
But, node_set() and node_clear() use the atomic bitmap functions
set_bit() and clear_bit(), so it is in fact synchronized.
>>
>> static int set_recommended_min_free_kbytes(void)
>> {
>> @@ -2273,6 +2273,14 @@ static bool khugepaged_scan_abort(int nid)
>> int i;
>>
>> /*
>> + * If it's clear that we are going to select a node where THP
>> + * allocation is unlikely to succeed, abort
>> + */
>> + if (khugepaged_node_load[nid] == (HPAGE_PMD_NR / 2) &&
>> + !node_isset(nid, thp_avail_nodes))
>> + return true;
>> +
>> + /*
>> * If zone_reclaim_mode is disabled, then no extra effort is made to
>> * allocate memory locally.
>> */
>
> If khugepaged_node_load for a node doesn't reach HPAGE_PMD_NR / 2, then
> this doesn't cause an abort.
Yes such situation is also covered.
> I don't think it's necessary to try to
> optimize and abort the scan early when this is met, I think this should
> only be checked before collapse_huge_page().
Avoiding potentially 256 iterations of a loop sounds good to me, no?
The check shouldn't be expensive thanks to short-circuiting the other
part.:)
>> @@ -2356,6 +2364,7 @@ static struct page
>> if (unlikely(!*hpage)) {
>> count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
>> *hpage = ERR_PTR(-ENOMEM);
>> + node_clear(node, thp_avail_nodes);
>> return NULL;
>> }
>>
>> @@ -2363,6 +2372,42 @@ static struct page
>> return *hpage;
>> }
>>
>> +/* Return true, if THP should be allocatable on at least one node */
>> +static bool khugepaged_check_nodes(struct page **hpage)
>> +{
>> + bool ret = false;
>> + int nid;
>> + struct page *newpage = NULL;
>> + gfp_t gfp = alloc_hugepage_gfpmask(khugepaged_defrag());
>> +
>> + for_each_online_node(nid) {
>> + if (node_isset(nid, thp_avail_nodes)) {
>> + ret = true;
>> + continue;
>> + }
>> +
>> + newpage = alloc_hugepage_node(gfp, nid);
>> +
>> + if (newpage) {
>> + node_set(nid, thp_avail_nodes);
>> + ret = true;
>> + /*
>> + * Heuristic - try to hold on to the page for collapse
>> + * scanning, if we don't hold any yet.
>> + */
>> + if (IS_ERR_OR_NULL(*hpage)) {
>> + *hpage = newpage;
>> + //NIXME: should we count all/no allocations?
>> + count_vm_event(THP_COLLAPSE_ALLOC);
>
> Seems like we'd only count the event when the node load has selected a
> target node and the hugepage that is allocated here is used, but if this
Yeah even the node preallocation was misleading in this regard (see
commit log of patch 1).
> approach is adopted then I think you'll need to introduce a new event to
> track when a hugepage is allocated and subsequently dropped.
Alternatively add event for successful collapses (and keep the current
one for allocations). It is exported now under /sys but having that in
vmstat would be more consistent.
Then the count of pages subsequently dropped is simply the difference
between collapse allocations and collapses (with some rather negligible
amount possibly being held waiting as you suggest below).
I think this approach would be better as we wouldn't change semantic of
existing THP_COLLAPSE_ALLOC event?
>
>> + } else {
>> + put_page(newpage);
>> + }
>
> Eek, rather than do put_page() why not store a preallocated hugepage for
> every node and let khugepaged_alloc_page() use it? It would be
> unfortunate that page_to_nid(*hpage) may not equal the target node after
> scanning.
I considered that but were afraid that if those pages' nodes ended up
not selected, the stored pages would just occupy memory. But maybe I
could introduce a shrinker for freeing those?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC 3/4] mm, thp: try fault allocations only if we expect them to succeed
2015-05-11 14:35 [RFC 0/4] Outsourcing page fault THP allocations to khugepaged Vlastimil Babka
2015-05-11 14:35 ` [RFC 1/4] mm, thp: stop preallocating hugepages in khugepaged Vlastimil Babka
2015-05-11 14:35 ` [RFC 2/4] mm, thp: khugepaged checks for THP allocability before scanning Vlastimil Babka
@ 2015-05-11 14:35 ` Vlastimil Babka
2015-06-18 1:20 ` David Rientjes
2015-05-11 14:35 ` [RFC 4/4] mm, thp: wake up khugepaged when huge page is not available Vlastimil Babka
3 siblings, 1 reply; 10+ messages in thread
From: Vlastimil Babka @ 2015-05-11 14:35 UTC (permalink / raw)
To: linux-mm
Cc: linux-kernel, Andrew Morton, Hugh Dickins, Andrea Arcangeli,
Kirill A. Shutemov, Rik van Riel, Mel Gorman, Michal Hocko,
Alex Thorlton, David Rientjes, Vlastimil Babka
Since we track THP availability for khugepaged THP collapses, we can use it
also for page fault THP allocations. If khugepaged with its sync compaction
is not able to allocate a hugepage, then it's unlikely that the less involved
attempt on page fault would succeed, and the cost could be higher than THP
benefits. Also clear the THP availability flag if we do attempt and fail to
allocate during page fault, and set the flag if we are freeing a large enough
page from any context. The latter doesn't include merges, as that's a fast
path and unlikely to make much difference.
Also restructure alloc_pages_vma() a bit.
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
mm/huge_memory.c | 3 ++-
mm/internal.h | 39 +++++++++++++++++++++++++++++++++++++++
mm/mempolicy.c | 37 ++++++++++++++++++++++---------------
mm/page_alloc.c | 3 +++
4 files changed, 66 insertions(+), 16 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index b86a72a..d3081a7 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -102,7 +102,8 @@ struct khugepaged_scan {
static struct khugepaged_scan khugepaged_scan = {
.mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
};
-static nodemask_t thp_avail_nodes = NODE_MASK_ALL;
+
+nodemask_t thp_avail_nodes = NODE_MASK_ALL;
static int set_recommended_min_free_kbytes(void)
{
diff --git a/mm/internal.h b/mm/internal.h
index a25e359..6d9a711 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -162,6 +162,45 @@ extern bool is_free_buddy_page(struct page *page);
#endif
extern int user_min_free_kbytes;
+/*
+ * in mm/huge_memory.c
+ */
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+
+extern nodemask_t thp_avail_nodes;
+
+static inline bool thp_avail_isset(int nid)
+{
+ return node_isset(nid, thp_avail_nodes);
+}
+
+static inline void thp_avail_set(int nid)
+{
+ node_set(nid, thp_avail_nodes);
+}
+
+static inline void thp_avail_clear(int nid)
+{
+ node_clear(nid, thp_avail_nodes);
+}
+
+#else
+
+static inline bool thp_avail_isset(int nid)
+{
+ return true;
+}
+
+static inline void thp_avail_set(int nid)
+{
+}
+
+static inline void thp_avail_clear(int nid)
+{
+}
+
+#endif
+
#if defined CONFIG_COMPACTION || defined CONFIG_CMA
/*
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index ede2629..41923b0 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1963,17 +1963,32 @@ alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
unsigned long addr, int node, bool hugepage)
{
struct mempolicy *pol;
- struct page *page;
+ struct page *page = NULL;
unsigned int cpuset_mems_cookie;
struct zonelist *zl;
nodemask_t *nmask;
+ /* Help compiler eliminate code */
+ hugepage = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hugepage;
+
retry_cpuset:
pol = get_vma_policy(vma, addr);
cpuset_mems_cookie = read_mems_allowed_begin();
- if (unlikely(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hugepage &&
- pol->mode != MPOL_INTERLEAVE)) {
+ if (pol->mode == MPOL_INTERLEAVE) {
+ unsigned nid;
+
+ nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order);
+ mpol_cond_put(pol);
+ if (!hugepage || thp_avail_isset(nid))
+ page = alloc_page_interleave(gfp, order, nid);
+ if (hugepage && !page)
+ thp_avail_clear(nid);
+ goto out;
+ }
+
+ nmask = policy_nodemask(gfp, pol);
+ if (hugepage) {
/*
* For hugepage allocation and non-interleave policy which
* allows the current node, we only try to allocate from the
@@ -1983,25 +1998,17 @@ retry_cpuset:
* If the policy is interleave, or does not allow the current
* node in its nodemask, we allocate the standard way.
*/
- nmask = policy_nodemask(gfp, pol);
if (!nmask || node_isset(node, *nmask)) {
mpol_cond_put(pol);
- page = alloc_pages_exact_node(node,
+ if (thp_avail_isset(node))
+ page = alloc_pages_exact_node(node,
gfp | __GFP_THISNODE, order);
+ if (!page)
+ thp_avail_clear(node);
goto out;
}
}
- if (pol->mode == MPOL_INTERLEAVE) {
- unsigned nid;
-
- nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order);
- mpol_cond_put(pol);
- page = alloc_page_interleave(gfp, order, nid);
- goto out;
- }
-
- nmask = policy_nodemask(gfp, pol);
zl = policy_zonelist(gfp, pol, node);
mpol_cond_put(pol);
page = __alloc_pages_nodemask(gfp, order, zl, nmask);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index ebffa0e..f7ff90e 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -830,6 +830,9 @@ static void __free_pages_ok(struct page *page, unsigned int order)
set_freepage_migratetype(page, migratetype);
free_one_page(page_zone(page), page, pfn, order, migratetype);
local_irq_restore(flags);
+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)
+ && order >= HPAGE_PMD_ORDER)
+ thp_avail_set(page_to_nid(page));
}
void __init __free_pages_bootmem(struct page *page, unsigned int order)
--
2.1.4
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [RFC 3/4] mm, thp: try fault allocations only if we expect them to succeed
2015-05-11 14:35 ` [RFC 3/4] mm, thp: try fault allocations only if we expect them to succeed Vlastimil Babka
@ 2015-06-18 1:20 ` David Rientjes
2015-06-23 16:23 ` Vlastimil Babka
0 siblings, 1 reply; 10+ messages in thread
From: David Rientjes @ 2015-06-18 1:20 UTC (permalink / raw)
To: Vlastimil Babka
Cc: linux-mm, linux-kernel, Andrew Morton, Hugh Dickins,
Andrea Arcangeli, Kirill A. Shutemov, Rik van Riel, Mel Gorman,
Michal Hocko, Alex Thorlton
On Mon, 11 May 2015, Vlastimil Babka wrote:
> Since we track THP availability for khugepaged THP collapses, we can use it
> also for page fault THP allocations. If khugepaged with its sync compaction
> is not able to allocate a hugepage, then it's unlikely that the less involved
> attempt on page fault would succeed, and the cost could be higher than THP
> benefits. Also clear the THP availability flag if we do attempt and fail to
> allocate during page fault, and set the flag if we are freeing a large enough
> page from any context. The latter doesn't include merges, as that's a fast
> path and unlikely to make much difference.
>
That depends on how long {scan,alloc}_sleep_millisecs are, so if
khugepaged fails to allocate a hugepage on all nodes, it sleeps for
alloc_sleep_millisecs (default 60s), and then there's immediate memory
freeing, thp page faults don't happen again for 60s. That's scary to me
when thp_avail_nodes is clear, a large process terminates, and then
immediately starts back up. None of its memory is faulted as thp and
depending on how large it is, khugepaged may fail to allocate hugepages
when it wakes back up so it never scans (the only reason why
thp_avail_nodes was clear before it terminated originally).
I'm not sure that approach can work unless the inference of whether a
hugepage can be allocated at a given time is a very good indicator of
whether a hugepage can be allocated alloc_sleep_millisecs later, and I'm
afraid that's not the case.
I'm very happy that you're looking at thp fault latency and the role that
khugepaged can play in accepting responsibility for defragmentation,
though. It's an area that has caused me some trouble lately and I'd like
to be able to improve.
We see an immediate benefit when experimenting with doing synchronous
memory compactions of all memory every 15s. That's done using a cronjob
rather than khugepaged, but the idea is the same.
What would your thoughts be about doing something radical like
- having khugepaged do synchronous memory compaction of all memory at
regulary intervals,
- track how many pageblocks are free for thp memory to be allocated,
- terminate collapsing if free pageblocks are below a threshold,
- trigger a khugepaged wakeup at page fault when that number of
pageblocks falls below a threshold,
- determine the next full sync memory compaction based on how many
pageblocks were defragmented on the last wakeup, and
- avoid memory compaction for all thp page faults.
(I'd ignore what is actually the responsibility of khugepaged and what is
done in task work at this time.)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC 3/4] mm, thp: try fault allocations only if we expect them to succeed
2015-06-18 1:20 ` David Rientjes
@ 2015-06-23 16:23 ` Vlastimil Babka
0 siblings, 0 replies; 10+ messages in thread
From: Vlastimil Babka @ 2015-06-23 16:23 UTC (permalink / raw)
To: David Rientjes
Cc: linux-mm, linux-kernel, Andrew Morton, Hugh Dickins,
Andrea Arcangeli, Kirill A. Shutemov, Rik van Riel, Mel Gorman,
Michal Hocko, Alex Thorlton
On 06/18/2015 03:20 AM, David Rientjes wrote:
> On Mon, 11 May 2015, Vlastimil Babka wrote:
>
>> Since we track THP availability for khugepaged THP collapses, we can use it
>> also for page fault THP allocations. If khugepaged with its sync compaction
>> is not able to allocate a hugepage, then it's unlikely that the less involved
>> attempt on page fault would succeed, and the cost could be higher than THP
>> benefits. Also clear the THP availability flag if we do attempt and fail to
>> allocate during page fault, and set the flag if we are freeing a large enough
>> page from any context. The latter doesn't include merges, as that's a fast
>> path and unlikely to make much difference.
>>
>
> That depends on how long {scan,alloc}_sleep_millisecs are, so if
> khugepaged fails to allocate a hugepage on all nodes, it sleeps for
> alloc_sleep_millisecs (default 60s)
Waking up khugepaged earlier is handled in patch 4.
> and then there's immediate memory
> freeing, thp page faults don't happen again for 60s. That's scary to me
> when thp_avail_nodes is clear, a large process terminates, and then
> immediately starts back up.
The last hunk of this patch makes sure that freeing a >=HPAGE_PMD_ORDER
page sets the thp availability bit so that scenario should be OK. This
wouldn't handle merging of free pages to form a large enough page, but
that should be rare enough to be negligible.
> None of its memory is faulted as thp and
> depending on how large it is, khugepaged may fail to allocate hugepages
> when it wakes back up so it never scans (the only reason why
> thp_avail_nodes was clear before it terminated originally).
>
> I'm not sure that approach can work unless the inference of whether a
> hugepage can be allocated at a given time is a very good indicator of
> whether a hugepage can be allocated alloc_sleep_millisecs later, and I'm
> afraid that's not the case.
So does the explanation above solve the concern?
> I'm very happy that you're looking at thp fault latency and the role that
> khugepaged can play in accepting responsibility for defragmentation,
> though. It's an area that has caused me some trouble lately and I'd like
> to be able to improve.
Good.
> We see an immediate benefit when experimenting with doing synchronous
> memory compactions of all memory every 15s. That's done using a cronjob
> rather than khugepaged, but the idea is the same.
>
> What would your thoughts be about doing something radical like
>
> - having khugepaged do synchronous memory compaction of all memory at
> regulary intervals,
I'm also thinking towards something like this for some time, yeah. Also
maybe not khugepaged but per-node "kcompatd" that's handles just the
compation and not thp collapses.
> - track how many pageblocks are free for thp memory to be allocated,
That should be easy to determine from free lists already? There are
per-order counts AFAIK, you just have to sum up over all zones and
orders between pageblock order and MAX_ORDER (which should be just 1 or
2 orders).
> - terminate collapsing if free pageblocks are below a threshold,
Why not.
> - trigger a khugepaged wakeup at page fault when that number of
> pageblocks falls below a threshold,
>
> - determine the next full sync memory compaction based on how many
> pageblocks were defragmented on the last wakeup, and
>
> - avoid memory compaction for all thp page faults.
Right. That should also reduce the amount of GFP_TRANSHUGE decisions
done in the allocator right now...
I think there are more benefits possible when a thread is responsible
for thorough defragmentation and its activity is tuned appropriately
(and doesn't depend on the collapse scanning results as it's now the
case for khugepaged - it won't compact anything on a node if there's
nothing to collapse there).
- direct compaction can quickly skip a block of memory in migrate
scanner as soon as it finds a page that cannot be isolated. I had a
patch for that [1], but dropped it due to longer-term fragmentation
becoming worse.
- I think that direct compaction could also stop using the current free
scanner and just get free pages from free lists. In my current testing I
see that free scanner spends an awful lot of time to find those free
pages, if we are near the watermarks. I think this approach should work
better, combined with implementing the previous point:
- if the free page that came from the free list is within the
order-aligned block that the migrate scanner is processing, then of
course we don't use it as migration target. We keep the page aside on a
list so it can later merge with the pages freed by migration.
- since getting pages from free lists is done in increasing order
starting from 0, it would also have some natural antifragmentation
effects. Right now the free scanner can be easily breaking an order-8
page to obtain one or few pages as migration targets.
Of course after such modifications direct compaction is no longer truly
a "compaction", that's why complementing it with the traditional one
done by a dedicated thread would be needed to avoid regressions in
long-term fragmentation.
[1] http://www.spinics.net/lists/linux-mm/msg76307.html
> (I'd ignore what is actually the responsibility of khugepaged and what is
> done in task work at this time.)
>
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majordomo@kvack.org. For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC 4/4] mm, thp: wake up khugepaged when huge page is not available
2015-05-11 14:35 [RFC 0/4] Outsourcing page fault THP allocations to khugepaged Vlastimil Babka
` (2 preceding siblings ...)
2015-05-11 14:35 ` [RFC 3/4] mm, thp: try fault allocations only if we expect them to succeed Vlastimil Babka
@ 2015-05-11 14:35 ` Vlastimil Babka
3 siblings, 0 replies; 10+ messages in thread
From: Vlastimil Babka @ 2015-05-11 14:35 UTC (permalink / raw)
To: linux-mm
Cc: linux-kernel, Andrew Morton, Hugh Dickins, Andrea Arcangeli,
Kirill A. Shutemov, Rik van Riel, Mel Gorman, Michal Hocko,
Alex Thorlton, David Rientjes, Vlastimil Babka
After previous patch, THP page faults check the thp_avail_nodes nodemask to
determine whether to attempt allocating hugepage or fallback immediately.
The khugepaged task is responsible for attempting reclaim and compaction for
nodes where hugepages are not available, and updating the nodemask as
appropriate.
To get faster reaction on THP allocation failures, we will wake up khugepaged
whenever THP page fault has to fallback. This includes both situations when
hugepage was supposed to be available, but allocation fails, and situations
where hugepage is already marked as unavailable. In the latter case, khugepaged
will not wait according to its alloc_sleep_millisecs parameter under /sys, but
retry allocation immediately. This is done to scale the khugepaged activity
with respect to THP demand, instead of a fixed tunable. Excessive compaction
failures are still being prevented by the self-tuning deferred compaction
mechanism in this case. For this mechanism to work as intended, the check for
deferred compaction should be done on each THP allocation attempt to bump the
internal counter, and waiting full alloc_sleep_millisecs period could make the
deferred periods excessively long.
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
mm/huge_memory.c | 22 ++++++++++++++++++----
mm/internal.h | 5 +----
2 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index d3081a7..b3d08a0 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -104,6 +104,15 @@ static struct khugepaged_scan khugepaged_scan = {
};
nodemask_t thp_avail_nodes = NODE_MASK_ALL;
+static bool khugepaged_thp_requested = false;
+
+void thp_avail_clear(int nid)
+{
+ node_clear(nid, thp_avail_nodes);
+ khugepaged_thp_requested = true;
+ wake_up_interruptible(&khugepaged_wait);
+}
+
static int set_recommended_min_free_kbytes(void)
{
@@ -2263,7 +2272,8 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
static void khugepaged_alloc_sleep(void)
{
- wait_event_freezable_timeout(khugepaged_wait, false,
+ wait_event_freezable_timeout(khugepaged_wait,
+ khugepaged_thp_requested,
msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
}
@@ -2381,6 +2391,8 @@ static bool khugepaged_check_nodes(struct page **hpage)
struct page *newpage = NULL;
gfp_t gfp = alloc_hugepage_gfpmask(khugepaged_defrag());
+ khugepaged_thp_requested = false;
+
for_each_online_node(nid) {
if (node_isset(nid, thp_avail_nodes)) {
ret = true;
@@ -2780,13 +2792,15 @@ breakouterloop_mmap_sem:
static int khugepaged_has_work(void)
{
- return !list_empty(&khugepaged_scan.mm_head) &&
+ return (khugepaged_thp_requested ||
+ !list_empty(&khugepaged_scan.mm_head)) &&
khugepaged_enabled();
}
static int khugepaged_wait_event(void)
{
- return !list_empty(&khugepaged_scan.mm_head) ||
+ return khugepaged_thp_requested ||
+ !list_empty(&khugepaged_scan.mm_head) ||
kthread_should_stop();
}
@@ -2837,7 +2851,7 @@ static void khugepaged_wait_work(void)
return;
wait_event_freezable_timeout(khugepaged_wait,
- kthread_should_stop(),
+ khugepaged_thp_requested || kthread_should_stop(),
msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
return;
}
diff --git a/mm/internal.h b/mm/internal.h
index 6d9a711..5c37e4d 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -179,10 +179,7 @@ static inline void thp_avail_set(int nid)
node_set(nid, thp_avail_nodes);
}
-static inline void thp_avail_clear(int nid)
-{
- node_clear(nid, thp_avail_nodes);
-}
+extern void thp_avail_clear(int nid);
#else
--
2.1.4
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply related [flat|nested] 10+ messages in thread