2014-02-18 22:13:00

by Rik van Riel

[permalink] [raw]
Subject: [PATCH -mm 0/3] fix numa vs kvm scalability issue

The NUMA scanning code can end up iterating over many gigabytes
of unpopulated memory, especially in the case of a freshly started
KVM guest with lots of memory.

This results in the mmu notifier code being called even when
there are no mapped pages in a virtual address range. The amount
of time wasted can be enough to trigger soft lockup warnings
with very large (>2TB) KVM guests.

This patch moves the mmu notifier call to the pmd level, which
represents 1GB areas of memory on x86-64. Furthermore, the mmu
notifier code is only called from the address in the PMD where
present mappings are first encountered.

The hugetlbfs code is left alone for now; hugetlb mappings are
not relocatable, and as such are left alone by the NUMA code,
and should never trigger this problem to begin with.

The series also adds a cond_resched to task_numa_work, to
fix another potential latency issue.


2014-02-18 22:13:02

by Rik van Riel

[permalink] [raw]
Subject: [PATCH -mm 2/3] mm,numa: reorganize change_pmd_range

From: Rik van Riel <[email protected]>

Reorganize the order of ifs in change_pmd_range a little, in
preparation for the next patch.

Signed-off-by: Rik van Riel <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Andrea Arcangeli <[email protected]>
Reported-by: Xing Gang <[email protected]>
Tested-by: Chegu Vinod <[email protected]>
---
mm/mprotect.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/mm/mprotect.c b/mm/mprotect.c
index 769a67a..6006c05 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -118,6 +118,8 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
unsigned long this_pages;

next = pmd_addr_end(addr, end);
+ if (!pmd_trans_huge(*pmd) && pmd_none_or_clear_bad(pmd))
+ continue;
if (pmd_trans_huge(*pmd)) {
if (next - addr != HPAGE_PMD_SIZE)
split_huge_page_pmd(vma, addr, pmd);
@@ -133,10 +135,9 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
continue;
}
}
- /* fall through */
+ /* fall through, the trans huge pmd just split */
}
- if (pmd_none_or_clear_bad(pmd))
- continue;
+ VM_BUG_ON(pmd_trans_huge(*pmd));
this_pages = change_pte_range(vma, pmd, addr, next, newprot,
dirty_accountable, prot_numa);
pages += this_pages;
--
1.8.5.3

2014-02-18 22:13:09

by Rik van Riel

[permalink] [raw]
Subject: [PATCH -mm 3/3] move mmu notifier call from change_protection to change_pmd_range

From: Rik van Riel <[email protected]>

The NUMA scanning code can end up iterating over many gigabytes
of unpopulated memory, especially in the case of a freshly started
KVM guest with lots of memory.

This results in the mmu notifier code being called even when
there are no mapped pages in a virtual address range. The amount
of time wasted can be enough to trigger soft lockup warnings
with very large KVM guests.

This patch moves the mmu notifier call to the pmd level, which
represents 1GB areas of memory on x86-64. Furthermore, the mmu
notifier code is only called from the address in the PMD where
present mappings are first encountered.

The hugetlbfs code is left alone for now; hugetlb mappings are
not relocatable, and as such are left alone by the NUMA code,
and should never trigger this problem to begin with.

Signed-off-by: Rik van Riel <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Andrea Arcangeli <[email protected]>
Reported-by: Xing Gang <[email protected]>
Tested-by: Chegu Vinod <[email protected]>
---
mm/mprotect.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/mm/mprotect.c b/mm/mprotect.c
index 6006c05..44850ee 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -109,9 +109,11 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
pgprot_t newprot, int dirty_accountable, int prot_numa)
{
pmd_t *pmd;
+ struct mm_struct *mm = vma->vm_mm;
unsigned long next;
unsigned long pages = 0;
unsigned long nr_huge_updates = 0;
+ unsigned long mni_start = 0;

pmd = pmd_offset(pud, addr);
do {
@@ -120,6 +122,13 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
next = pmd_addr_end(addr, end);
if (!pmd_trans_huge(*pmd) && pmd_none_or_clear_bad(pmd))
continue;
+
+ /* invoke the mmu notifier if the pmd is populated */
+ if (!mni_start) {
+ mni_start = addr;
+ mmu_notifier_invalidate_range_start(mm, mni_start, end);
+ }
+
if (pmd_trans_huge(*pmd)) {
if (next - addr != HPAGE_PMD_SIZE)
split_huge_page_pmd(vma, addr, pmd);
@@ -143,6 +152,9 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
pages += this_pages;
} while (pmd++, addr = next, addr != end);

+ if (mni_start)
+ mmu_notifier_invalidate_range_end(mm, mni_start, end);
+
if (nr_huge_updates)
count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
return pages;
@@ -205,12 +217,12 @@ unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
struct mm_struct *mm = vma->vm_mm;
unsigned long pages;

- mmu_notifier_invalidate_range_start(mm, start, end);
- if (is_vm_hugetlb_page(vma))
+ if (is_vm_hugetlb_page(vma)) {
+ mmu_notifier_invalidate_range_start(mm, start, end);
pages = hugetlb_change_protection(vma, start, end, newprot);
- else
+ mmu_notifier_invalidate_range_end(mm, start, end);
+ } else
pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
- mmu_notifier_invalidate_range_end(mm, start, end);

return pages;
}
--
1.8.5.3

2014-02-18 22:12:58

by Rik van Riel

[permalink] [raw]
Subject: [PATCH -mm 1/3] sched,numa: add cond_resched to task_numa_work

From: Rik van Riel <[email protected]>

Normally task_numa_work scans over a fairly small amount of memory,
but it is possible to run into a large unpopulated part of virtual
memory, with no pages mapped. In that case, task_numa_work can run
for a while, and it may make sense to reschedule as required.

Signed-off-by: Rik van Riel <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Andrea Arcangeli <[email protected]>
Reported-by: Xing Gang <[email protected]>
Tested-by: Chegu Vinod <[email protected]>
---
kernel/sched/fair.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 966cc2b..7815709 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1757,6 +1757,8 @@ void task_numa_work(struct callback_head *work)
start = end;
if (pages <= 0)
goto out;
+
+ cond_resched();
} while (end != vma->vm_end);
}

--
1.8.5.3

2014-02-19 02:22:28

by David Rientjes

[permalink] [raw]
Subject: Re: [PATCH -mm 2/3] mm,numa: reorganize change_pmd_range

On Tue, 18 Feb 2014, [email protected] wrote:

> From: Rik van Riel <[email protected]>
>
> Reorganize the order of ifs in change_pmd_range a little, in
> preparation for the next patch.
>
> Signed-off-by: Rik van Riel <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Andrea Arcangeli <[email protected]>
> Reported-by: Xing Gang <[email protected]>
> Tested-by: Chegu Vinod <[email protected]>

Acked-by: David Rientjes <[email protected]>

> ---
> mm/mprotect.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/mm/mprotect.c b/mm/mprotect.c
> index 769a67a..6006c05 100644
> --- a/mm/mprotect.c
> +++ b/mm/mprotect.c
> @@ -118,6 +118,8 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
> unsigned long this_pages;
>
> next = pmd_addr_end(addr, end);
> + if (!pmd_trans_huge(*pmd) && pmd_none_or_clear_bad(pmd))
> + continue;
> if (pmd_trans_huge(*pmd)) {
> if (next - addr != HPAGE_PMD_SIZE)
> split_huge_page_pmd(vma, addr, pmd);

Extra tab there, though.

2014-02-19 02:24:40

by David Rientjes

[permalink] [raw]
Subject: Re: [PATCH -mm 3/3] move mmu notifier call from change_protection to change_pmd_range

On Tue, 18 Feb 2014, [email protected] wrote:

> From: Rik van Riel <[email protected]>
>
> The NUMA scanning code can end up iterating over many gigabytes
> of unpopulated memory, especially in the case of a freshly started
> KVM guest with lots of memory.
>
> This results in the mmu notifier code being called even when
> there are no mapped pages in a virtual address range. The amount
> of time wasted can be enough to trigger soft lockup warnings
> with very large KVM guests.
>
> This patch moves the mmu notifier call to the pmd level, which
> represents 1GB areas of memory on x86-64. Furthermore, the mmu
> notifier code is only called from the address in the PMD where
> present mappings are first encountered.
>
> The hugetlbfs code is left alone for now; hugetlb mappings are
> not relocatable, and as such are left alone by the NUMA code,
> and should never trigger this problem to begin with.
>
> Signed-off-by: Rik van Riel <[email protected]>
> Cc: Peter Zijlstra <[email protected]>
> Cc: Andrea Arcangeli <[email protected]>
> Reported-by: Xing Gang <[email protected]>
> Tested-by: Chegu Vinod <[email protected]>

Acked-by: David Rientjes <[email protected]>

Might have been cleaner to move the
mmu_notifier_invalidate_range_{start,end}() to hugetlb_change_protection()
as well, though.

2014-02-19 03:08:58

by Rik van Riel

[permalink] [raw]
Subject: Re: [PATCH -mm 3/3] move mmu notifier call from change_protection to change_pmd_range

On 02/18/2014 09:24 PM, David Rientjes wrote:
> On Tue, 18 Feb 2014, [email protected] wrote:
>
>> From: Rik van Riel <[email protected]>
>>
>> The NUMA scanning code can end up iterating over many gigabytes
>> of unpopulated memory, especially in the case of a freshly started
>> KVM guest with lots of memory.
>>
>> This results in the mmu notifier code being called even when
>> there are no mapped pages in a virtual address range. The amount
>> of time wasted can be enough to trigger soft lockup warnings
>> with very large KVM guests.
>>
>> This patch moves the mmu notifier call to the pmd level, which
>> represents 1GB areas of memory on x86-64. Furthermore, the mmu
>> notifier code is only called from the address in the PMD where
>> present mappings are first encountered.
>>
>> The hugetlbfs code is left alone for now; hugetlb mappings are
>> not relocatable, and as such are left alone by the NUMA code,
>> and should never trigger this problem to begin with.
>>
>> Signed-off-by: Rik van Riel <[email protected]>
>> Cc: Peter Zijlstra <[email protected]>
>> Cc: Andrea Arcangeli <[email protected]>
>> Reported-by: Xing Gang <[email protected]>
>> Tested-by: Chegu Vinod <[email protected]>
>
> Acked-by: David Rientjes <[email protected]>
>
> Might have been cleaner to move the
> mmu_notifier_invalidate_range_{start,end}() to hugetlb_change_protection()
> as well, though.

I can certainly do that if you want. Just let me know
and I'll send a v2 of patch 3 :)

--
All rights reversed

2014-02-19 03:19:09

by Rik van Riel

[permalink] [raw]
Subject: [PATCH -mm v2 3/3] move mmu notifier call from change_protection to change_pmd_range

On Tue, 18 Feb 2014 18:24:36 -0800 (PST)
David Rientjes <[email protected]> wrote:

> Acked-by: David Rientjes <[email protected]>
>
> Might have been cleaner to move the
> mmu_notifier_invalidate_range_{start,end}() to hugetlb_change_protection()
> as well, though.

Way cleaner! Second version attached :)

Thanks, David.

---8<---

Subject: move mmu notifier call from change_protection to change_pmd_range

The NUMA scanning code can end up iterating over many gigabytes
of unpopulated memory, especially in the case of a freshly started
KVM guest with lots of memory.

This results in the mmu notifier code being called even when
there are no mapped pages in a virtual address range. The amount
of time wasted can be enough to trigger soft lockup warnings
with very large KVM guests.

This patch moves the mmu notifier call to the pmd level, which
represents 1GB areas of memory on x86-64. Furthermore, the mmu
notifier code is only called from the address in the PMD where
present mappings are first encountered.

The hugetlbfs code is left alone for now; hugetlb mappings are
not relocatable, and as such are left alone by the NUMA code,
and should never trigger this problem to begin with.

Signed-off-by: Rik van Riel <[email protected]>
Acked-by: David Rientjes <[email protected]>
---
mm/hugetlb.c | 2 ++
mm/mprotect.c | 15 ++++++++++++---
2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index c0d930f..f0c5dfb 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -3065,6 +3065,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
BUG_ON(address >= end);
flush_cache_range(vma, address, end);

+ mmu_notifier_invalidate_range_start(mm, start, end);
mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
for (; address < end; address += huge_page_size(h)) {
spinlock_t *ptl;
@@ -3094,6 +3095,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
*/
flush_tlb_range(vma, start, end);
mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
+ mmu_notifier_invalidate_range_end(mm, start, end);

return pages << h->order;
}
diff --git a/mm/mprotect.c b/mm/mprotect.c
index d790166..76146fa 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -115,9 +115,11 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
pgprot_t newprot, int dirty_accountable, int prot_numa)
{
pmd_t *pmd;
+ struct mm_struct *mm = vma->vm_mm;
unsigned long next;
unsigned long pages = 0;
unsigned long nr_huge_updates = 0;
+ unsigned long mni_start = 0;

pmd = pmd_offset(pud, addr);
do {
@@ -126,6 +128,13 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
next = pmd_addr_end(addr, end);
if (!pmd_trans_huge(*pmd) && pmd_none_or_clear_bad(pmd))
continue;
+
+ /* invoke the mmu notifier if the pmd is populated */
+ if (!mni_start) {
+ mni_start = addr;
+ mmu_notifier_invalidate_range_start(mm, mni_start, end);
+ }
+
if (pmd_trans_huge(*pmd)) {
if (next - addr != HPAGE_PMD_SIZE)
split_huge_page_pmd(vma, addr, pmd);
@@ -149,6 +158,9 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
pages += this_pages;
} while (pmd++, addr = next, addr != end);

+ if (mni_start)
+ mmu_notifier_invalidate_range_end(mm, mni_start, end);
+
if (nr_huge_updates)
count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
return pages;
@@ -208,15 +220,12 @@ unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
unsigned long end, pgprot_t newprot,
int dirty_accountable, int prot_numa)
{
- struct mm_struct *mm = vma->vm_mm;
unsigned long pages;

- mmu_notifier_invalidate_range_start(mm, start, end);
if (is_vm_hugetlb_page(vma))
pages = hugetlb_change_protection(vma, start, end, newprot);
else
pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
- mmu_notifier_invalidate_range_end(mm, start, end);

return pages;
}

2014-02-19 08:59:23

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH -mm 0/3] fix numa vs kvm scalability issue

On Tue, Feb 18, 2014 at 05:12:43PM -0500, [email protected] wrote:
> The NUMA scanning code can end up iterating over many gigabytes
> of unpopulated memory, especially in the case of a freshly started
> KVM guest with lots of memory.
>
> This results in the mmu notifier code being called even when
> there are no mapped pages in a virtual address range. The amount
> of time wasted can be enough to trigger soft lockup warnings
> with very large (>2TB) KVM guests.
>
> This patch moves the mmu notifier call to the pmd level, which
> represents 1GB areas of memory on x86-64. Furthermore, the mmu
> notifier code is only called from the address in the PMD where
> present mappings are first encountered.
>
> The hugetlbfs code is left alone for now; hugetlb mappings are
> not relocatable, and as such are left alone by the NUMA code,
> and should never trigger this problem to begin with.
>
> The series also adds a cond_resched to task_numa_work, to
> fix another potential latency issue.

Andrew, I'll pick up the first kernel/sched/ patch; do you want the
other two mm/ patches?

2014-02-19 19:28:07

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH -mm 0/3] fix numa vs kvm scalability issue

On Wed, 19 Feb 2014 09:59:17 +0100 Peter Zijlstra <[email protected]> wrote:

> On Tue, Feb 18, 2014 at 05:12:43PM -0500, [email protected] wrote:
> > The NUMA scanning code can end up iterating over many gigabytes
> > of unpopulated memory, especially in the case of a freshly started
> > KVM guest with lots of memory.
> >
> > This results in the mmu notifier code being called even when
> > there are no mapped pages in a virtual address range. The amount
> > of time wasted can be enough to trigger soft lockup warnings
> > with very large (>2TB) KVM guests.
> >
> > This patch moves the mmu notifier call to the pmd level, which
> > represents 1GB areas of memory on x86-64. Furthermore, the mmu
> > notifier code is only called from the address in the PMD where
> > present mappings are first encountered.
> >
> > The hugetlbfs code is left alone for now; hugetlb mappings are
> > not relocatable, and as such are left alone by the NUMA code,
> > and should never trigger this problem to begin with.
> >
> > The series also adds a cond_resched to task_numa_work, to
> > fix another potential latency issue.
>
> Andrew, I'll pick up the first kernel/sched/ patch; do you want the
> other two mm/ patches?

That works, thanks.

Subject: [tip:sched/urgent] sched,numa: add cond_resched to task_numa_work

Commit-ID: 3cf1962cdbf6b3a9e3ef21116d215bbab350ea37
Gitweb: http://git.kernel.org/tip/3cf1962cdbf6b3a9e3ef21116d215bbab350ea37
Author: Rik van Riel <[email protected]>
AuthorDate: Tue, 18 Feb 2014 17:12:44 -0500
Committer: Thomas Gleixner <[email protected]>
CommitDate: Fri, 21 Feb 2014 21:27:10 +0100

sched,numa: add cond_resched to task_numa_work

Normally task_numa_work scans over a fairly small amount of memory,
but it is possible to run into a large unpopulated part of virtual
memory, with no pages mapped. In that case, task_numa_work can run
for a while, and it may make sense to reschedule as required.

Cc: [email protected]
Cc: Andrea Arcangeli <[email protected]>
Signed-off-by: Rik van Riel <[email protected]>
Reported-by: Xing Gang <[email protected]>
Tested-by: Chegu Vinod <[email protected]>
Signed-off-by: Peter Zijlstra <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Thomas Gleixner <[email protected]>
---
kernel/sched/fair.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 966cc2b..7815709 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1757,6 +1757,8 @@ void task_numa_work(struct callback_head *work)
start = end;
if (pages <= 0)
goto out;
+
+ cond_resched();
} while (end != vma->vm_end);
}