This is a port on kernel 4.13 of the work done by Peter Zijlstra to
handle page fault without holding the mm semaphore [1].
The idea is to try to handle user space page faults without holding the
mmap_sem. This should allow better concurrency for massively threaded
process since the page fault handler will not wait for other threads memory
layout change to be done, assuming that this change is done in another part
of the process's memory space. This type page fault is named speculative
page fault. If the speculative page fault fails because of a concurrency is
detected or because underlying PMD or PTE tables are not yet allocating, it
is failing its processing and a classic page fault is then tried.
The speculative page fault (SPF) has to look for the VMA matching the fault
address without holding the mmap_sem, so the VMA list is now managed using
SRCU allowing lockless walking. The only impact would be the deferred file
derefencing in the case of a file mapping, since the file pointer is
released once the SRCU cleaning is done. This patch relies on the change
done recently by Paul McKenney in SRCU which now runs a callback per CPU
instead of per SRCU structure [1].
The VMA's attributes checked during the speculative page fault processing
have to be protected against parallel changes. This is done by using a per
VMA sequence lock. This sequence lock allows the speculative page fault
handler to fast check for parallel changes in progress and to abort the
speculative page fault in that case.
Once the VMA is found, the speculative page fault handler would check for
the VMA's attributes to verify that the page fault has to be handled
correctly or not. Thus the VMA is protected through a sequence lock which
allows fast detection of concurrent VMA changes. If such a change is
detected, the speculative page fault is aborted and a *classic* page fault
is tried. VMA sequence locks are added when VMA attributes which are
checked during the page fault are modified.
When the PTE is fetched, the VMA is checked to see if it has been changed,
so once the page table is locked, the VMA is valid, so any other changes
leading to touching this PTE will need to lock the page table, so no
parallel change is possible at this time.
Compared to the Peter's initial work, this series introduces a spin_trylock
when dealing with speculative page fault. This is required to avoid dead
lock when handling a page fault while a TLB invalidate is requested by an
other CPU holding the PTE. Another change due to a lock dependency issue
with mapping->i_mmap_rwsem.
In addition some VMA field values which are used once the PTE is unlocked
at the end the page fault path are saved into the vm_fault structure to
used the values matching the VMA at the time the PTE was locked.
This series builds on top of v4.13-rc5 and is functional on x86 and
PowerPC.
Tests have been made using a large commercial in-memory database on a
PowerPC system with 752 CPU using RFC v5. The results are very encouraging
since the loading of the 2TB database was faster by 14% with the
speculative page fault.
Using ebizzy test [3], which spreads a lot of threads, the result are good
when running on both a large or a small system. When using kernbench, the
result are quite similar which expected as not so much multithreaded
processes are involved. But there is no performance degradation neither
which is good.
------------------
Benchmarks results
Note these test have been made on top of 4.13-rc3 with the following patch
from Paul McKenney applied:
"srcu: Provide ordering for CPU not involved in grace period" [5]
Ebizzy:
-------
The test is counting the number of records per second it can manage, the
higher is the best. I run it like this 'ebizzy -mTRp'. To get consistent
result I repeated the test 100 times and measure the average result, mean
deviation, max and min.
- 16 CPUs x86 VM
Records/s 4.13-rc5 4.13-rc5-spf
Average 11350.29 21760.36
Mean deviation 396.56 881.40
Max 13773 26194
Min 10567 19223
- 80 CPUs Power 8 node:
Records/s 4.13-rc5 4.13-rc5-spf
Average 33904.67 58847.91
Mean deviation 789.40 1753.19
Max 36703 68958
Min 31759 55125
The number of record per second is far better with the speculative page
fault.
The mean deviation is higher with the speculative page fault, may be
because sometime the fault are not handled in a speculative way leading to
more variation.
Kernbench:
----------
This test is building a 4.12 kernel using platform default config. The
build has been run 5 times each time.
- 16 CPUs x86 VM
Average Half load -j 8 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 166.574 (0.340779) 145.754 (0.776325)
User Time 1080.77 (2.05871) 999.272 (4.12142)
System Time 204.594 (1.02449) 116.362 (1.22974)
Percent CPU 771.2 (1.30384) 765 (0.707107)
Context Switches 46590.6 (935.591) 66316.4 (744.64)
Sleeps 84421.2 (596.612) 85186 (523.041)
Average Optimal load -j 16 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 85.422 (0.42293) 74.81 (0.419345)
User Time 1031.79 (51.6557) 954.912 (46.8439)
System Time 186.528 (19.0575) 107.514 (9.36902)
Percent CPU 1059.2 (303.607) 1056.8 (307.624)
Context Switches 67240.3 (21788.9) 89360.6 (24299.9)
Sleeps 89607.8 (5511.22) 90372.5 (5490.16)
The elapsed time is a bit shorter in the case of the SPF release, but the
impact less important since there are less multithreaded processes involved
here.
- 80 CPUs Power 8 node:
Average Half load -j 40 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 117.176 (0.824093) 116.792 (0.695392)
User Time 4412.34 (24.29) 4396.02 (24.4819)
System Time 131.106 (1.28343) 133.452 (0.708851)
Percent CPU 3876.8 (18.1439) 3877.6 (21.9955)
Context Switches 72470.2 (466.181) 72971 (673.624)
Sleeps 161294 (2284.85) 161946 (2217.9)
Average Optimal load -j 80 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 111.176 (1.11123) 111.242 (0.801542)
User Time 5930.03 (1600.07) 5929.89 (1617)
System Time 166.258 (37.0662) 169.337 (37.8419)
Percent CPU 5378.5 (1584.16) 5385.6 (1590.24)
Context Switches 117389 (47350.1) 130132 (60256.3)
Sleeps 163354 (4153.9) 163219 (2251.27)
Here the elapsed time is a bit shorter using the spf release, but we
remain in the error margin. It has to be noted that this system is not
correctly balanced on the NUMA point of view as all the available memory is
attached to one core.
------------------------
Changes since v1:
- Remove PERF_COUNT_SW_SPF_FAILED perf event.
- Add tracing events to details speculative page fault failures.
- Cache VMA fields values which are used once the PTE is unlocked at the
end of the page fault events.
- Ensure that fields read during the speculative path are written and read
using WRITE_ONCE and READ_ONCE.
- Add checks at the beginning of the speculative path to abort it if the
VMA is known to not be supported.
Changes since RFC V5 [6]
- Port to 4.13 kernel
- Merging patch fixing lock dependency into the original patch
- Replace the 2 parameters of vma_has_changed() with the vmf pointer
- In patch 7, don't call __do_fault() in the speculative path as it may
want to unlock the mmap_sem.
- In patch 11-12, don't check for vma boundaries when
page_add_new_anon_rmap() is called during the spf path and protect against
anon_vma pointer's update.
- In patch 13-16, add performance events to report number of successful
and failed speculative events.
[1] http://linux-kernel.2935.n7.nabble.com/RFC-PATCH-0-6-Another-go-at-speculative-page-faults-tt965642.html#none
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=da915ad5cf25b5f5d358dd3670c3378d8ae8c03e
[3] http://ebizzy.sourceforge.net/
[4] http://ck.kolivas.org/apps/kernbench/kernbench-0.50/
[5] https://lkml.org/lkml/2017/7/24/829
[6] https://lwn.net/Articles/725607/
Laurent Dufour (14):
mm: Introduce pte_spinlock for FAULT_FLAG_SPECULATIVE
mm: Protect VMA modifications using VMA sequence count
mm: Cache some VMA fields in the vm_fault structure
mm: Protect SPF handler against anon_vma changes
mm/migrate: Pass vm_fault pointer to migrate_misplaced_page()
mm: Introduce __lru_cache_add_active_or_unevictable
mm: Introduce __maybe_mkwrite()
mm: Introduce __vm_normal_page()
mm: Introduce __page_add_new_anon_rmap()
mm: Try spin lock in speculative path
mm: Adding speculative page fault failure trace events
perf: Add a speculative page fault sw event
perf tools: Add support for the SPF perf event
powerpc/mm: Add speculative page fault
Peter Zijlstra (6):
mm: Dont assume page-table invariance during faults
mm: Prepare for FAULT_FLAG_SPECULATIVE
mm: VMA sequence count
mm: RCU free VMAs
mm: Provide speculative fault infrastructure
x86/mm: Add speculative pagefault handling
arch/powerpc/include/asm/book3s/64/pgtable.h | 5 +
arch/powerpc/mm/fault.c | 30 +-
arch/x86/include/asm/pgtable_types.h | 7 +
arch/x86/mm/fault.c | 19 ++
fs/proc/task_mmu.c | 5 +-
fs/userfaultfd.c | 17 +-
include/linux/hugetlb_inline.h | 2 +-
include/linux/migrate.h | 4 +-
include/linux/mm.h | 21 +-
include/linux/mm_types.h | 3 +
include/linux/pagemap.h | 4 +-
include/linux/rmap.h | 12 +-
include/linux/swap.h | 11 +-
include/trace/events/pagefault.h | 87 +++++
include/uapi/linux/perf_event.h | 1 +
kernel/fork.c | 1 +
mm/hugetlb.c | 2 +
mm/init-mm.c | 1 +
mm/internal.h | 19 ++
mm/khugepaged.c | 5 +
mm/madvise.c | 6 +-
mm/memory.c | 474 ++++++++++++++++++++++-----
mm/mempolicy.c | 51 ++-
mm/migrate.c | 4 +-
mm/mlock.c | 13 +-
mm/mmap.c | 138 ++++++--
mm/mprotect.c | 4 +-
mm/mremap.c | 7 +
mm/rmap.c | 5 +-
mm/swap.c | 12 +-
tools/include/uapi/linux/perf_event.h | 1 +
tools/perf/util/evsel.c | 1 +
tools/perf/util/parse-events.c | 4 +
tools/perf/util/parse-events.l | 1 +
tools/perf/util/python.c | 1 +
35 files changed, 803 insertions(+), 175 deletions(-)
create mode 100644 include/trace/events/pagefault.h
--
2.7.4
From: Peter Zijlstra <peterz@infradead.org>
One of the side effects of speculating on faults (without holding
mmap_sem) is that we can race with free_pgtables() and therefore we
cannot assume the page-tables will stick around.
Remove the reliance on the pte pointer.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
mm/memory.c | 27 ---------------------------
1 file changed, 27 deletions(-)
When handling page fault without holding the mmap_sem the fetch of the
pte lock pointer and the locking will have to be done while ensuring
that the VMA is not touched in our back.
So move the fetch and locking operations in a dedicated function.
Signed-off-by: Laurent Dufour <redacted>
---
mm/memory.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
From: Peter Zijlstra <peterz@infradead.org>
Wrap the VMA modifications (vma_adjust/unmap_page_range) with sequence
counts such that we can easily test if a VMA is changed.
The unmap_page_range() one allows us to make assumptions about
page-tables; when we find the seqcount hasn't changed we can assume
page-tables are still valid.
The flip side is that we cannot distinguish between a vma_adjust() and
the unmap_page_range() -- where with the former we could have
re-checked the vma bounds against the address.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
[Port to 4.12 kernel]
[Fix lock dependency between mapping->i_mmap_rwsem and vma->vm_sequence]
Signed-off-by: Laurent Dufour <redacted>
---
include/linux/mm_types.h | 1 +
mm/memory.c | 2 ++
mm/mmap.c | 21 ++++++++++++++++++---
3 files changed, 21 insertions(+), 3 deletions(-)
@@ -342,6 +342,7 @@ struct vm_area_struct {structmempolicy*vm_policy;/* NUMA policy for the VMA */#endifstructvm_userfaultfd_ctxvm_userfaultfd_ctx;+seqcount_tvm_sequence;}__randomize_layout;structcore_thread{
The VMA sequence count has been introduced to allow fast detection of
VMA modification when running a page fault handler without holding
the mmap_sem.
This patch provides protection against the VMA modification done in :
- madvise()
- mremap()
- mpol_rebind_policy()
- vma_replace_policy()
- change_prot_numa()
- mlock(), munlock()
- mprotect()
- mmap_region()
- collapse_huge_page()
- userfaultd registering services
In addition, VMA fields which will be read during the speculative fault
path needs to be written using WRITE_ONCE to prevent write to be split
and intermediate values to be pushed to other CPUs.
Signed-off-by: Laurent Dufour <redacted>
---
fs/proc/task_mmu.c | 5 ++++-
fs/userfaultfd.c | 17 +++++++++++++----
mm/khugepaged.c | 3 +++
mm/madvise.c | 6 +++++-
mm/mempolicy.c | 51 ++++++++++++++++++++++++++++++++++-----------------
mm/mlock.c | 13 ++++++++-----
mm/mmap.c | 17 ++++++++++-------
mm/mprotect.c | 4 +++-
mm/mremap.c | 7 +++++++
9 files changed, 87 insertions(+), 36 deletions(-)
@@ -301,6 +301,10 @@ static unsigned long move_vma(struct vm_area_struct *vma,if(!new_vma)return-ENOMEM;+write_seqcount_begin(&vma->vm_sequence);+write_seqcount_begin_nested(&new_vma->vm_sequence,+SINGLE_DEPTH_NESTING);+moved_len=move_page_tables(vma,old_addr,new_vma,new_addr,old_len,need_rmap_locks);if(moved_len<old_len){
@@ -317,6 +321,7 @@ static unsigned long move_vma(struct vm_area_struct *vma,*/move_page_tables(new_vma,new_addr,vma,old_addr,moved_len,true);+write_seqcount_end(&vma->vm_sequence);vma=new_vma;old_len=new_len;old_addr=new_addr;
@@ -325,7 +330,9 @@ static unsigned long move_vma(struct vm_area_struct *vma,mremap_userfaultfd_prep(new_vma,uf);arch_remap(mm,old_addr,old_addr+old_len,new_addr,new_addr+new_len);+write_seqcount_end(&vma->vm_sequence);}+write_seqcount_end(&new_vma->vm_sequence);/* Conceal VM_ACCOUNT so old reservation is not undone */if(vm_flags&VM_ACCOUNT){
When handling speculative page fault, the vma->vm_flags and
vma->vm_page_prot fields are read once the page table lock is released. So
there is no more guarantee that these fields would not change in our back.
They will be saved in the vm_fault structure before the VMA is checked for
changes.
This patch also set the fields in hugetlb_no_page() and
__collapse_huge_page_swapin even if it is not need for the callee.
Signed-off-by: Laurent Dufour <redacted>
---
include/linux/mm.h | 6 ++++++
mm/hugetlb.c | 2 ++
mm/khugepaged.c | 2 ++
mm/memory.c | 38 ++++++++++++++++++++------------------
4 files changed, 30 insertions(+), 18 deletions(-)
@@ -881,6 +881,8 @@ static bool __collapse_huge_page_swapin(struct mm_struct *mm,.flags=FAULT_FLAG_ALLOW_RETRY,.pmd=pmd,.pgoff=linear_page_index(vma,address),+.vma_flags=vma->vm_flags,+.vma_page_prot=vma->vm_page_prot,};/* we only decide to swapin, if there is enough young ptes */
@@ -3261,7 +3261,7 @@ int finish_fault(struct vm_fault *vmf)/* Did we COW the page? */if((vmf->flags&FAULT_FLAG_WRITE)&&-!(vmf->vma->vm_flags&VM_SHARED))+!(vmf->vma_flags&VM_SHARED))page=vmf->cow_page;elsepage=vmf->page;
@@ -3507,7 +3507,7 @@ static int do_fault(struct vm_fault *vmf)ret=VM_FAULT_SIGBUS;elseif(!(vmf->flags&FAULT_FLAG_WRITE))ret=do_read_fault(vmf);-elseif(!(vma->vm_flags&VM_SHARED))+elseif(!(vmf->vma_flags&VM_SHARED))ret=do_cow_fault(vmf);elseret=do_shared_fault(vmf);
@@ -3564,7 +3564,7 @@ static int do_numa_page(struct vm_fault *vmf)*accessibleptes,somecanallowaccessbykernelmode.*/pte=ptep_modify_prot_start(vma->vm_mm,vmf->address,vmf->pte);-pte=pte_modify(pte,vma->vm_page_prot);+pte=pte_modify(pte,vmf->vma_page_prot);pte=pte_mkyoung(pte);if(was_writable)pte=pte_mkwrite(pte);
@@ -3598,7 +3598,7 @@ static int do_numa_page(struct vm_fault *vmf)*Flagifthepageissharedbetweenmultipleaddressspaces.This*islaterusedwhendeterminingwhethertogrouptaskstogether*/-if(page_mapcount(page)>1&&(vma->vm_flags&VM_SHARED))+if(page_mapcount(page)>1&&(vmf->vma_flags&VM_SHARED))flags|=TNF_SHARED;last_cpupid=page_cpupid_last(page);
@@ -3642,7 +3642,7 @@ static int wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)returnvmf->vma->vm_ops->huge_fault(vmf,PE_SIZE_PMD);/* COW handled on pte level: split pmd */-VM_BUG_ON_VMA(vmf->vma->vm_flags&VM_SHARED,vmf->vma);+VM_BUG_ON_VMA(vmf->vma_flags&VM_SHARED,vmf->vma);__split_huge_pmd(vmf->vma,vmf->pmd,vmf->address,false,NULL);returnVM_FAULT_FALLBACK;
@@ -3789,6 +3789,8 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,.flags=flags,.pgoff=linear_page_index(vma,address),.gfp_mask=__get_fault_gfp_mask(vma),+.vma_flags=vma->vm_flags,+.vma_page_prot=vma->vm_page_prot,};structmm_struct*mm=vma->vm_mm;pgd_t*pgd;
The current maybe_mkwrite() is getting passed the pointer to the vma
structure to fetch the vm_flags field.
When dealing with the speculative page fault handler, it will be better to
rely on the cached vm_flags value stored in the vm_fault structure.
This patch introduce a __maybe_mkwrite() service which can be called by
passing the value of the vm_flags field.
There is no change functional changes expected for the other callers of
maybe_mkwrite().
Signed-off-by: Laurent Dufour <redacted>
---
include/linux/mm.h | 9 +++++++--
mm/memory.c | 6 +++---
2 files changed, 10 insertions(+), 5 deletions(-)
When dealing with speculative page fault handler, we may race with VMA
being split or merged. In this case the vma->vm_start and vm->vm_end
fields may not match the address the page fault is occurring.
This can only happens when the VMA is split but in that case, the
anon_vma pointer of the new VMA will be the same as the original one,
because in __split_vma the new->anon_vma is set to src->anon_vma when
*new = *vma.
So even if the VMA boundaries are not correct, the anon_vma pointer is
still valid.
If the VMA has been merged, then the VMA in which it has been merged
must have the same anon_vma pointer otherwise the merge can't be done.
So in all the case we know that the anon_vma is valid, since we have
checked before starting the speculative page fault that the anon_vma
pointer is valid for this VMA and since there is an anon_vma this
means that at one time a page has been backed and that before the VMA
is cleaned, the page table lock would have to be grab to clean the
PTE, and the anon_vma field is checked once the PTE is locked.
This patch introduce a new __page_add_new_anon_rmap() service which
doesn't check for the VMA boundaries, and create a new inline one
which do the check.
When called from a page fault handler, if this is not a speculative one,
there is a guarantee that vm_start and vm_end match the faulting address,
so this check is useless. In the context of the speculative page fault
handler, this check may be wrong but anon_vma is still valid as explained
above.
Signed-off-by: Laurent Dufour <redacted>
---
include/linux/rmap.h | 12 ++++++++++--
mm/memory.c | 8 ++++----
mm/rmap.c | 5 ++---
3 files changed, 16 insertions(+), 9 deletions(-)
@@ -2376,7 +2376,7 @@ static int wp_page_copy(struct vm_fault *vmf)*threaddoingCOW.*/ptep_clear_flush_notify(vma,vmf->address,vmf->pte);-page_add_new_anon_rmap(new_page,vma,vmf->address,false);+__page_add_new_anon_rmap(new_page,vma,vmf->address,false);mem_cgroup_commit_charge(new_page,memcg,false,false);__lru_cache_add_active_or_unevictable(new_page,vmf->vma_flags);/*
@@ -2847,7 +2847,7 @@ int do_swap_page(struct vm_fault *vmf)mem_cgroup_commit_charge(page,memcg,true,false);activate_page(page);}else{/* ksm created a completely new copy */-page_add_new_anon_rmap(page,vma,vmf->address,false);+__page_add_new_anon_rmap(page,vma,vmf->address,false);mem_cgroup_commit_charge(page,memcg,false,false);__lru_cache_add_active_or_unevictable(page,vmf->vma_flags);}
@@ -2985,7 +2985,7 @@ static int do_anonymous_page(struct vm_fault *vmf)}inc_mm_counter_fast(vma->vm_mm,MM_ANONPAGES);-page_add_new_anon_rmap(page,vma,vmf->address,false);+__page_add_new_anon_rmap(page,vma,vmf->address,false);mem_cgroup_commit_charge(page,memcg,false,false);__lru_cache_add_active_or_unevictable(page,vmf->vma_flags);setpte:
There is a deadlock when a CPU is doing a speculative page fault and
another one is calling do_unmap().
The deadlock occurred because the speculative path try to spinlock the
pte while the interrupt are disabled. When the other CPU in the
unmap's path has locked the pte then is waiting for all the CPU to
invalidate the TLB. As the CPU doing the speculative fault have the
interrupt disable it can't invalidate the TLB, and can't get the lock.
Since we are in a speculative path, we can race with other mm action.
So let assume that the lock may not get acquired and fail the
speculative page fault.
Here are the stacks captured during the deadlock:
CPU 0
native_flush_tlb_others+0x7c/0x260
flush_tlb_mm_range+0x6a/0x220
tlb_flush_mmu_tlbonly+0x63/0xc0
unmap_page_range+0x897/0x9d0
? unmap_single_vma+0x7d/0xe0
? release_pages+0x2b3/0x360
unmap_single_vma+0x7d/0xe0
unmap_vmas+0x51/0xa0
unmap_region+0xbd/0x130
do_munmap+0x279/0x460
SyS_munmap+0x53/0x70
CPU 1
do_raw_spin_lock+0x14e/0x160
_raw_spin_lock+0x5d/0x80
? pte_map_lock+0x169/0x1b0
pte_map_lock+0x169/0x1b0
handle_pte_fault+0xbf2/0xd80
? trace_hardirqs_on+0xd/0x10
handle_speculative_fault+0x272/0x280
handle_speculative_fault+0x5/0x280
__do_page_fault+0x187/0x580
trace_do_page_fault+0x52/0x260
do_async_page_fault+0x19/0x70
async_page_fault+0x28/0x30
Signed-off-by: Laurent Dufour <redacted>
---
mm/memory.c | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
@@ -79,6 +79,9 @@#include"internal.h"+#define CREATE_TRACE_POINTS+#include<trace/events/pagefault.h>+#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.#endif
@@ -3989,27 +4001,40 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,*ValidatetheVMAfoundbythelocklesslookup.*/dead=RB_EMPTY_NODE(&vma->vm_rb);+if(dead){+trace_spf_vma_dead(_RET_IP_,vma,address);+gotounlock;+}+seq=raw_read_seqcount(&vma->vm_sequence);/* rmb <-> seqlock,vma_rb_erase() */-if((seq&1)||dead)+if(seq&1){+trace_spf_vma_changed(_RET_IP_,vma,address);gotounlock;+}/**Can'tcallvm_opsservicehaswedon'tknowwhattheywoulddo*withtheVMA.*Thisincludehugepagefromhugetlbfs.*/-if(vma->vm_ops)+if(vma->vm_ops){+trace_spf_vma_notsup(_RET_IP_,vma,address);gotounlock;+}-if(unlikely(!vma->anon_vma))+if(unlikely(!vma->anon_vma)){+trace_spf_vma_notsup(_RET_IP_,vma,address);gotounlock;+}vmf.vma_flags=READ_ONCE(vma->vm_flags);vmf.vma_page_prot=READ_ONCE(vma->vm_page_prot);/* Can't call userland page fault handler in the speculative path */-if(unlikely(vmf.vma_flags&VM_UFFD_MISSING))+if(unlikely(vmf.vma_flags&VM_UFFD_MISSING)){+trace_spf_vma_notsup(_RET_IP_,vma,address);gotounlock;+}/**MPOL_INTERLEAVEimpliesadditionalcheckinmpol_misplaced()which
@@ -4018,20 +4043,26 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,pol=__get_vma_policy(vma,address);if(!pol)pol=get_task_policy(current);-if(pol&&pol->mode==MPOL_INTERLEAVE)+if(pol&&pol->mode==MPOL_INTERLEAVE){+trace_spf_vma_notsup(_RET_IP_,vma,address);gotounlock;+}-if(vmf.vma_flags&VM_GROWSDOWN||vmf.vma_flags&VM_GROWSUP)+if(vmf.vma_flags&VM_GROWSDOWN||vmf.vma_flags&VM_GROWSUP){/**ThiscouldbedetectedbythecheckaddressagainstVMA's*boundariesbutwewanttotraceitasnotsupportedinstead*ofchanged.*/+trace_spf_vma_notsup(_RET_IP_,vma,address);gotounlock;+}if(address<READ_ONCE(vma->vm_start)-||READ_ONCE(vma->vm_end)<=address)+||READ_ONCE(vma->vm_end)<=address){+trace_spf_vma_changed(_RET_IP_,vma,address);gotounlock;+}/**Thethreefollowingchecksarecopiedfromaccess_errorfrom
@@ -4039,16 +4070,22 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,*/if(!arch_vma_access_permitted(vma,flags&FAULT_FLAG_WRITE,flags&FAULT_FLAG_INSTRUCTION,-flags&FAULT_FLAG_REMOTE))+flags&FAULT_FLAG_REMOTE)){+trace_spf_vma_access(_RET_IP_,vma,address);gotounlock;+}/* This is one is required to check that the VMA has write access set */if(flags&FAULT_FLAG_WRITE){-if(unlikely(!(vmf.vma_flags&VM_WRITE)))+if(unlikely(!(vmf.vma_flags&VM_WRITE))){+trace_spf_vma_access(_RET_IP_,vma,address);gotounlock;+}}else{-if(unlikely(!(vmf.vma_flags&(VM_READ|VM_EXEC|VM_WRITE))))+if(unlikely(!(vmf.vma_flags&(VM_READ|VM_EXEC|VM_WRITE)))){+trace_spf_vma_access(_RET_IP_,vma,address);gotounlock;+}}/*
@@ -4097,8 +4134,10 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,*Weneedtore-validatetheVMAaftercheckingthebounds,otherwise*wemighthaveafalsepositiveonthebounds.*/-if(read_seqcount_retry(&vma->vm_sequence,seq))+if(read_seqcount_retry(&vma->vm_sequence,seq)){+trace_spf_vma_changed(_RET_IP_,vma,address);gotounlock;+}ret=handle_pte_fault(&vmf);
@@ -4107,6 +4146,7 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,returnret;out_walk:+trace_spf_vma_notsup(_RET_IP_,vma,address);local_irq_enable();gotounlock;}
This patch enable the speculative page fault on the PowerPC
architecture.
This will try a speculative page fault without holding the mmap_sem,
if it returns with WM_FAULT_RETRY, the mmap_sem is acquired and the
traditional page fault processing is done.
Support is only provide for BOOK3S_64 currently because:
- require CONFIG_PPC_STD_MMU because checks done in
set_access_flags_filter()
- require BOOK3S because we can't support for book3e_hugetlb_preload()
called by update_mmu_cache()
Signed-off-by: Laurent Dufour <redacted>
---
arch/powerpc/include/asm/book3s/64/pgtable.h | 5 +++++
arch/powerpc/mm/fault.c | 30 +++++++++++++++++++++++++++-
2 files changed, 34 insertions(+), 1 deletion(-)
@@ -313,6 +313,11 @@ extern unsigned long pci_io_base;/* Advertise support for _PAGE_SPECIAL */#define __HAVE_ARCH_PTE_SPECIAL+/* Advertise that we call the Speculative Page Fault handler */+#if defined(CONFIG_PPC_BOOK3S_64)+#define __HAVE_ARCH_CALL_SPF+#endif+#ifndef __ASSEMBLY__/*
@@ -291,9 +291,36 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,if(is_write&&is_user)store_update_sp=store_updates_sp(regs);-if(is_user)+if(is_user){flags|=FAULT_FLAG_USER;+#if defined(__HAVE_ARCH_CALL_SPF)+/* let's try a speculative page fault without grabbing the+*mmap_sem.+*/++/*+*flagsissetlaterbasedontheVMA'sflags,forthecommon+*speculativeservice,weneedsomeflagstobeset.+*/+if(is_write)+flags|=FAULT_FLAG_WRITE;++fault=handle_speculative_fault(mm,address,flags);+if(!(fault&VM_FAULT_RETRY||fault&VM_FAULT_ERROR)){+perf_sw_event(PERF_COUNT_SW_SPF_DONE,1,+regs,address);+gotodone;+}++/*+*Resettingflagssincethefollowingcodeassumes+*FAULT_FLAG_WRITEisnotset.+*/+flags&=~FAULT_FLAG_WRITE;+#endif /* defined(__HAVE_ARCH_CALL_SPF) */+}+/* When running in the kernel we expect faults to occur only to*addressesinuserspace.Allotherfaultsrepresenterrorsinthe*kernelandshouldgenerateanOOPS.Unfortunately,inthecaseofan
@@ -479,6 +506,7 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,rc=0;}+done:/**Major/minorpagefaultaccounting.*/
From: Peter Zijlstra <peterz@infradead.org>
Try a speculative fault before acquiring mmap_sem, if it returns with
VM_FAULT_RETRY continue with the mmap_sem acquisition and do the
traditional fault.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
[Clearing of FAULT_FLAG_ALLOW_RETRY is now done in
handle_speculative_fault()]
[Retry with usual fault path in the case VM_ERROR is returned by
handle_speculative_fault(). This allows signal to be delivered]
Signed-off-by: Laurent Dufour <redacted>
---
arch/x86/include/asm/pgtable_types.h | 7 +++++++
arch/x86/mm/fault.c | 19 +++++++++++++++++++
2 files changed, 26 insertions(+)
From: Peter Zijlstra <peterz@infradead.org>
Provide infrastructure to do a speculative fault (not holding
mmap_sem).
The not holding of mmap_sem means we can race against VMA
change/removal and page-table destruction. We use the SRCU VMA freeing
to keep the VMA around. We use the VMA seqcount to detect change
(including umapping / page-table deletion) and we use gup_fast() style
page-table walking to deal with page-table races.
Once we've obtained the page and are ready to update the PTE, we
validate if the state we started the fault with is still valid, if
not, we'll fail the fault with VM_FAULT_RETRY, otherwise we update the
PTE and we're done.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
[Manage the newly introduced pte_spinlock() for speculative page
fault to fail if the VMA is touched in our back]
[Rename vma_is_dead() to vma_has_changed() and declare it here]
[Call p4d_alloc() as it is safe since pgd is valid]
[Call pud_alloc() as it is safe since p4d is valid]
[Set fe.sequence in __handle_mm_fault()]
[Abort speculative path when handle_userfault() has to be called]
[Add additional VMA's flags checks in handle_speculative_fault()]
[Clear FAULT_FLAG_ALLOW_RETRY in handle_speculative_fault()]
[Don't set vmf->pte and vmf->ptl if pte_map_lock() failed]
[Remove warning comment about waiting for !seq&1 since we don't want
to wait]
[Remove warning about no huge page support, mention it explictly]
[Don't call do_fault() in the speculative path as __do_fault() calls
vma->vm_ops->fault() which may want to release mmap_sem]
[Only vm_fault pointer argument for vma_has_changed()]
[Fix check against huge page, calling pmd_trans_huge()]
[Introduce __HAVE_ARCH_CALL_SPF to declare the SPF handler only when
architecture is supporting it]
[Use READ_ONCE() when reading VMA's fields in the speculative path]
[Explicitly check for __HAVE_ARCH_PTE_SPECIAL as we can't support for
processing done in vm_normal_page()]
[Check that vma->anon_vma is already set when starting the speculative
path]
[Check for memory policy as we can't support MPOL_INTERLEAVE case due to
the processing done in mpol_misplaced()]
[Don't support VMA growing up or down]
[Move check on vm_sequence just before calling handle_pte_fault()]
Signed-off-by: Laurent Dufour <redacted>
---
include/linux/hugetlb_inline.h | 2 +-
include/linux/mm.h | 5 +
include/linux/pagemap.h | 4 +-
mm/internal.h | 14 +++
mm/memory.c | 237 ++++++++++++++++++++++++++++++++++++++++-
5 files changed, 254 insertions(+), 8 deletions(-)
@@ -315,6 +315,7 @@ struct vm_fault {gfp_tgfp_mask;/* gfp mask to be used for allocations */pgoff_tpgoff;/* Logical page offset based on vma */unsignedlongaddress;/* Faulting virtual address */+unsignedintsequence;pmd_t*pmd;/* Pointer to pmd entry matching*the'address'*/pud_t*pud;/* Pointer to pud entry matching
@@ -2285,15 +2286,69 @@ static inline void wp_page_reuse(struct vm_fault *vmf)staticboolpte_spinlock(structvm_fault*vmf){+boolret=false;++/* Check if vma is still valid */+if(!(vmf->flags&FAULT_FLAG_SPECULATIVE)){+vmf->ptl=pte_lockptr(vmf->vma->vm_mm,vmf->pmd);+spin_lock(vmf->ptl);+returntrue;+}++local_irq_disable();+if(vma_has_changed(vmf))+gotoout;+vmf->ptl=pte_lockptr(vmf->vma->vm_mm,vmf->pmd);spin_lock(vmf->ptl);-returntrue;++if(vma_has_changed(vmf)){+spin_unlock(vmf->ptl);+gotoout;+}++ret=true;+out:+local_irq_enable();+returnret;}staticboolpte_map_lock(structvm_fault*vmf){-vmf->pte=pte_offset_map_lock(vmf->vma->vm_mm,vmf->pmd,vmf->address,&vmf->ptl);-returntrue;+boolret=false;+pte_t*pte;+spinlock_t*ptl;++if(!(vmf->flags&FAULT_FLAG_SPECULATIVE)){+vmf->pte=pte_offset_map_lock(vmf->vma->vm_mm,vmf->pmd,+vmf->address,&vmf->ptl);+returntrue;+}++/*+*Thefirstvma_has_changed()guaranteesthepage-tablesarestill+*valid,havingIRQsdisabledensurestheystayaround,hencethe+*secondvma_has_changed()tomakesuretheyarestillvalidonce+*we'vegotthelock.Afterthataconcurrentzap_pte_range()will+*blockonthePTLandthuswe'resafe.+*/+local_irq_disable();+if(vma_has_changed(vmf))+gotoout;++pte=pte_offset_map_lock(vmf->vma->vm_mm,vmf->pmd,+vmf->address,&ptl);+if(vma_has_changed(vmf)){+pte_unmap_unlock(pte,ptl);+gotoout;+}++vmf->pte=pte;+vmf->ptl=ptl;+ret=true;+out:+local_irq_enable();+returnret;}/*
@@ -2939,6 +2994,14 @@ static int do_anonymous_page(struct vm_fault *vmf)returnVM_FAULT_RETRY;if(!pte_none(*vmf->pte))gotounlock;+/*+*Don'tcalltheuserfaultfdduringthespeculativepath.+*WealreadycheckedfortheVMAtonotbemanagedthrough+*userfaultfd,butitmaybesetinourbackoncewehavelock+*thepte.Insuchacasewecanignoreitthistime.+*/+if(vmf->flags&FAULT_FLAG_SPECULATIVE)+gotosetpte;/* Deliver the page fault to userland, check inside PT lock */if(userfaultfd_missing(vma)){pte_unmap_unlock(vmf->pte,vmf->ptl);
@@ -2977,7 +3040,7 @@ static int do_anonymous_page(struct vm_fault *vmf)gotorelease;/* Deliver the page fault to userland, check inside PT lock */-if(userfaultfd_missing(vma)){+if(!(vmf->flags&FAULT_FLAG_SPECULATIVE)&&userfaultfd_missing(vma)){pte_unmap_unlock(vmf->pte,vmf->ptl);mem_cgroup_cancel_charge(page,memcg,false);put_page(page);
@@ -3748,6 +3811,8 @@ static int handle_pte_fault(struct vm_fault *vmf)if(!vmf->pte){if(vma_is_anonymous(vmf->vma))returndo_anonymous_page(vmf);+elseif(vmf->flags&FAULT_FLAG_SPECULATIVE)+returnVM_FAULT_RETRY;elsereturndo_fault(vmf);}
@@ -3845,6 +3910,7 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,vmf.pmd=pmd_alloc(mm,vmf.pud,address);if(!vmf.pmd)returnVM_FAULT_OOM;+vmf.sequence=raw_read_seqcount(&vma->vm_sequence);if(pmd_none(*vmf.pmd)&&transparent_hugepage_enabled(vma)){ret=create_huge_pmd(&vmf);if(!(ret&VM_FAULT_FALLBACK))
@@ -3872,6 +3938,167 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,returnhandle_pte_fault(&vmf);}+#ifdef __HAVE_ARCH_CALL_SPF++#ifndef __HAVE_ARCH_PTE_SPECIAL+/* This is required by vm_normal_page() */+#error "Speculative page fault handler requires __HAVE_ARCH_PTE_SPECIAL"+#endif++/*+*vm_normal_page()addssomeprocessingwhichshouldbedonewhile+*hodlingthemmap_sem.+*/+inthandle_speculative_fault(structmm_struct*mm,unsignedlongaddress,+unsignedintflags)+{+structvm_faultvmf={+.address=address,+};+pgd_t*pgd;+p4d_t*p4d;+pud_t*pud;+pmd_t*pmd;+intdead,seq,idx,ret=VM_FAULT_RETRY;+structvm_area_struct*vma;+structmempolicy*pol;++/* Clear flags that may lead to release the mmap_sem to retry */+flags&=~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);+flags|=FAULT_FLAG_SPECULATIVE;++idx=srcu_read_lock(&vma_srcu);+vma=find_vma_srcu(mm,address);+if(!vma)+gotounlock;++/*+*ValidatetheVMAfoundbythelocklesslookup.+*/+dead=RB_EMPTY_NODE(&vma->vm_rb);+seq=raw_read_seqcount(&vma->vm_sequence);/* rmb <-> seqlock,vma_rb_erase() */+if((seq&1)||dead)+gotounlock;++/*+*Can'tcallvm_opsservicehaswedon'tknowwhattheywoulddo+*withtheVMA.+*Thisincludehugepagefromhugetlbfs.+*/+if(vma->vm_ops)+gotounlock;++if(unlikely(!vma->anon_vma))+gotounlock;++vmf.vma_flags=READ_ONCE(vma->vm_flags);+vmf.vma_page_prot=READ_ONCE(vma->vm_page_prot);++/* Can't call userland page fault handler in the speculative path */+if(unlikely(vmf.vma_flags&VM_UFFD_MISSING))+gotounlock;++/*+*MPOL_INTERLEAVEimpliesadditionalcheckinmpol_misplaced()which+*arenotcompatiblewiththespeculativepagefaultprocessing.+*/+pol=__get_vma_policy(vma,address);+if(!pol)+pol=get_task_policy(current);+if(pol&&pol->mode==MPOL_INTERLEAVE)+gotounlock;++if(vmf.vma_flags&VM_GROWSDOWN||vmf.vma_flags&VM_GROWSUP)+/*+*ThiscouldbedetectedbythecheckaddressagainstVMA's+*boundariesbutwewanttotraceitasnotsupportedinstead+*ofchanged.+*/+gotounlock;++if(address<READ_ONCE(vma->vm_start)+||READ_ONCE(vma->vm_end)<=address)+gotounlock;++/*+*Thethreefollowingchecksarecopiedfromaccess_errorfrom+*arch/x86/mm/fault.c+*/+if(!arch_vma_access_permitted(vma,flags&FAULT_FLAG_WRITE,+flags&FAULT_FLAG_INSTRUCTION,+flags&FAULT_FLAG_REMOTE))+gotounlock;++/* This is one is required to check that the VMA has write access set */+if(flags&FAULT_FLAG_WRITE){+if(unlikely(!(vmf.vma_flags&VM_WRITE)))+gotounlock;+}else{+if(unlikely(!(vmf.vma_flags&(VM_READ|VM_EXEC|VM_WRITE))))+gotounlock;+}++/*+*DoaspeculativelookupofthePTEentry.+*/+local_irq_disable();+pgd=pgd_offset(mm,address);+if(pgd_none(*pgd)||unlikely(pgd_bad(*pgd)))+gotoout_walk;++p4d=p4d_alloc(mm,pgd,address);+if(p4d_none(*p4d)||unlikely(p4d_bad(*p4d)))+gotoout_walk;++pud=pud_alloc(mm,p4d,address);+if(pud_none(*pud)||unlikely(pud_bad(*pud)))+gotoout_walk;++pmd=pmd_offset(pud,address);+if(pmd_none(*pmd)||unlikely(pmd_bad(*pmd)))+gotoout_walk;++/*+*Theabovedoesnotallocate/instantiatepage-tablesbecausedoingso+*wouldleadtothepossibilityofinstantiatingpage-tablesafter+*free_pgtables()--andconsequentlyleakingthem.+*+*Theresultisthatwetakeatleastone!speculativefaultperPMD+*inordertoinstantiateit.+*/++/* Transparent huge pages are not supported. */+if(unlikely(pmd_trans_huge(*pmd)))+gotoout_walk;++vmf.vma=vma;+vmf.pmd=pmd;+vmf.pgoff=linear_page_index(vma,address);+vmf.gfp_mask=__get_fault_gfp_mask(vma);+vmf.sequence=seq;+vmf.flags=flags;++local_irq_enable();++/*+*Weneedtore-validatetheVMAaftercheckingthebounds,otherwise+*wemighthaveafalsepositiveonthebounds.+*/+if(read_seqcount_retry(&vma->vm_sequence,seq))+gotounlock;++ret=handle_pte_fault(&vmf);++unlock:+srcu_read_unlock(&vma_srcu,idx);+returnret;++out_walk:+local_irq_enable();+gotounlock;+}+#endif /* __HAVE_ARCH_CALL_SPF */+/**Bythetimewegethere,wealreadyholdthemmsemaphore*
When dealing with the speculative fault path we should use the VMA's field
cached value stored in the vm_fault structure.
Currently vm_normal_page() is using the pointer to the VMA to fetch the
vm_flags value. This patch provides a new __vm_normal_page() which is
receiving the vm_flags flags value as parameter.
Note: The speculative path is turned on for architecture providing support
for special PTE flag. So only the first block of vm_normal_page is used
during the speculative path.
Signed-off-by: Laurent Dufour <redacted>
---
mm/memory.c | 25 +++++++++++++++++--------
1 file changed, 17 insertions(+), 8 deletions(-)
The speculative page fault handler which is run without holding the
mmap_sem is calling lru_cache_add_active_or_unevictable() but the vm_flags
is not guaranteed to remain constant.
Introducing __lru_cache_add_active_or_unevictable() which has the vma flags
value parameter instead of the vma pointer.
Signed-off-by: Laurent Dufour <redacted>
---
include/linux/swap.h | 11 +++++++++--
mm/memory.c | 8 ++++----
mm/swap.c | 12 ++++++------
3 files changed, 19 insertions(+), 12 deletions(-)
@@ -2370,7 +2370,7 @@ static int wp_page_copy(struct vm_fault *vmf)ptep_clear_flush_notify(vma,vmf->address,vmf->pte);page_add_new_anon_rmap(new_page,vma,vmf->address,false);mem_cgroup_commit_charge(new_page,memcg,false,false);-lru_cache_add_active_or_unevictable(new_page,vma);+__lru_cache_add_active_or_unevictable(new_page,vmf->vma_flags);/**Wecallthenotifymacroherebecause,whenusingsecondary*mmupagetables(suchaskvmshadowpagetables),wewantthe
@@ -2840,7 +2840,7 @@ int do_swap_page(struct vm_fault *vmf)}else{/* ksm created a completely new copy */page_add_new_anon_rmap(page,vma,vmf->address,false);mem_cgroup_commit_charge(page,memcg,false,false);-lru_cache_add_active_or_unevictable(page,vma);+__lru_cache_add_active_or_unevictable(page,vmf->vma_flags);}swap_free(entry);
@@ -2978,7 +2978,7 @@ static int do_anonymous_page(struct vm_fault *vmf)inc_mm_counter_fast(vma->vm_mm,MM_ANONPAGES);page_add_new_anon_rmap(page,vma,vmf->address,false);mem_cgroup_commit_charge(page,memcg,false,false);-lru_cache_add_active_or_unevictable(page,vma);+__lru_cache_add_active_or_unevictable(page,vmf->vma_flags);setpte:set_pte_at(vma->vm_mm,vmf->address,vmf->pte,entry);
migrate_misplaced_page() is only called during the page fault handling so
it's better to pass the pointer to the struct vm_fault instead of the vma.
This way during the speculative page fault path the saved vma->vm_flags
could be used.
Signed-off-by: Laurent Dufour <redacted>
---
include/linux/migrate.h | 4 ++--
mm/memory.c | 2 +-
mm/migrate.c | 4 ++--
3 files changed, 5 insertions(+), 5 deletions(-)
@@ -3616,7 +3616,7 @@ static int do_numa_page(struct vm_fault *vmf)}/* Migrate to the requested node */-migrated=migrate_misplaced_page(page,vma,target_nid);+migrated=migrate_misplaced_page(page,vmf,target_nid);if(migrated){page_nid=target_nid;flags|=TNF_MIGRATED;
The speculative page fault handler must be protected against anon_vma
changes. This is because page_add_new_anon_rmap() is called during the
speculative path.
In addition, don't try speculative page fault if the VMA don't have an
anon_vma structure allocated because its allocation should be
protected by the mmap_sem.
In __vma_adjust() when importer->anon_vma is set, there is no need to
protect against speculative page faults since speculative page fault
is aborted if the vma->anon_vma is not set.
When calling page_add_new_anon_rmap() vma->anon_vma is necessarily
valid since we checked for it when locking the pte and the anon_vma is
removed once the pte is unlocked. So even if the speculative page
fault handler is running concurrently with do_unmap(), as the pte is
locked in unmap_region() - through unmap_vmas() - and the anon_vma
unlinked later, because we check for the vma sequence counter which is
updated in unmap_page_range() before locking the pte, and then in
free_pgtables() so when locking the pte the change will be detected.
Signed-off-by: Laurent Dufour <redacted>
---
mm/memory.c | 4 ++++
1 file changed, 4 insertions(+)
From: Peter Zijlstra <peterz@infradead.org>
Manage the VMAs with SRCU such that we can do a lockless VMA lookup.
We put the fput(vma->vm_file) in the SRCU callback, this keeps files
valid during speculative faults, this is possible due to the delayed
fput work by Al Viro -- do we need srcu_barrier() in unmount
someplace?
We guard the mm_rb tree with a seqlock (this could be a seqcount but
we'd have to disable preemption around the write side in order to make
the retry loop in __read_seqcount_begin() work) such that we can know
if the rb tree walk was correct. We cannot trust the restult of a
lockless tree walk in the face of concurrent tree rotations; although
we can trust on the termination of such walks -- tree rotations
guarantee the end result is a tree again after all.
Furthermore, we rely on the WMB implied by the
write_seqlock/count_begin() to separate the VMA initialization and the
publishing stores, analogous to the RELEASE in rcu_assign_pointer().
We also rely on the RMB from read_seqretry() to separate the vma load
from further loads like the smp_read_barrier_depends() in regular
RCU.
We must not touch the vmacache while doing SRCU lookups as that is not
properly serialized against changes. We update gap information after
publishing the VMA, but A) we don't use that and B) the seqlock
read side would fix that anyhow.
We clear vma->vm_rb for nodes removed from the vma tree such that we
can easily detect such 'dead' nodes, we rely on the WMB from
write_sequnlock() to separate the tree removal and clearing the node.
Provide find_vma_srcu() which wraps the required magic.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
[Remove the warnings in description about the SRCU global lock which
has been removed now]
[Rename vma_is_dead() to vma_has_changed() and move its adding to the next
patch]
Signed-off-by: Laurent Dufour <redacted>
---
include/linux/mm_types.h | 2 +
kernel/fork.c | 1 +
mm/init-mm.c | 1 +
mm/internal.h | 5 +++
mm/mmap.c | 100 +++++++++++++++++++++++++++++++++++------------
5 files changed, 83 insertions(+), 26 deletions(-)
@@ -901,15 +929,13 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,}if(remove_next){-if(file){+if(file)uprobe_munmap(next,next->vm_start,next->vm_end);-fput(file);-}if(next->anon_vma)anon_vma_merge(vma,next);mm->map_count--;mpol_put(vma_policy(next));-kmem_cache_free(vm_area_cachep,next);+free_vma(next);write_seqcount_end(&next->vm_sequence);/**Inmprotect'scase6(seecommentsonvma_merge),
@@ -2130,15 +2156,10 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,EXPORT_SYMBOL(get_unmapped_area);/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */-structvm_area_struct*find_vma(structmm_struct*mm,unsignedlongaddr)+staticstructvm_area_struct*__find_vma(structmm_struct*mm,unsignedlongaddr){structrb_node*rb_node;-structvm_area_struct*vma;--/* Check the cache first. */-vma=vmacache_find(mm,addr);-if(likely(vma))-returnvma;+structvm_area_struct*vma=NULL;rb_node=mm->mm_rb.rb_node;
@@ -2156,13 +2177,40 @@ struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)rb_node=rb_node->rb_right;}+returnvma;+}++structvm_area_struct*find_vma(structmm_struct*mm,unsignedlongaddr)+{+structvm_area_struct*vma;++/* Check the cache first. */+vma=vmacache_find(mm,addr);+if(likely(vma))+returnvma;++vma=__find_vma(mm,addr);if(vma)vmacache_update(addr,vma);returnvma;}-EXPORT_SYMBOL(find_vma);+structvm_area_struct*find_vma_srcu(structmm_struct*mm,unsignedlongaddr)+{+structvm_area_struct*vma;+unsignedintseq;++WARN_ON_ONCE(!srcu_read_lock_held(&vma_srcu));++do{+seq=read_seqbegin(&mm->mm_seq);+vma=__find_vma(mm,addr);+}while(read_seqretry(&mm->mm_seq,seq));++returnvma;+}+/**Sameasfind_vma,butalsoreturnapointertothepreviousVMAin*pprev.*/
From: Peter Zijlstra <peterz@infradead.org>
When speculating faults (without holding mmap_sem) we need to validate
that the vma against which we loaded pages is still valid when we're
ready to install the new PTE.
Therefore, replace the pte_offset_map_lock() calls that (re)take the
PTL with pte_map_lock() which can fail in case we find the VMA changed
since we started the fault.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
[Port to 4.12 kernel]
[Remove the comment about the fault_env structure which has been
implemented as the vm_fault structure in the kernel]
Signed-off-by: Laurent Dufour <redacted>
---
include/linux/mm.h | 1 +
mm/memory.c | 55 ++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 40 insertions(+), 16 deletions(-)
@@ -286,6 +286,7 @@ extern pgprot_t protection_map[16];#define FAULT_FLAG_USER 0x40 /* The fault originated in userspace */#define FAULT_FLAG_REMOTE 0x80 /* faulting for non current tsk/mm */#define FAULT_FLAG_INSTRUCTION 0x100 /* The fault was during an instruction fetch */+#define FAULT_FLAG_SPECULATIVE 0x200 /* Speculative fault, not holding mmap_sem */#define FAULT_FLAG_TRACE \{FAULT_FLAG_WRITE,"WRITE"},\
+ /*
+ * MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
+ * are not compatible with the speculative page fault processing.
+ */
+ pol = __get_vma_policy(vma, address);
+ if (!pol)
+ pol = get_task_policy(current);
+ if (pol && pol->mode == MPOL_INTERLEAVE)
+ goto unlock;
include/linux/mempolicy.h defines
struct mempolicy *get_task_policy(struct task_struct *p);
struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
unsigned long addr);
only for CONFIG_NUMA configs.
-ss
This is a port on kernel 4.13 of the work done by Peter Zijlstra to
handle page fault without holding the mm semaphore [1].
The idea is to try to handle user space page faults without holding the
mmap_sem. This should allow better concurrency for massively threaded
process since the page fault handler will not wait for other threads memory
layout change to be done, assuming that this change is done in another part
of the process's memory space. This type page fault is named speculative
page fault. If the speculative page fault fails because of a concurrency is
detected or because underlying PMD or PTE tables are not yet allocating, it
is failing its processing and a classic page fault is then tried.
The speculative page fault (SPF) has to look for the VMA matching the fault
address without holding the mmap_sem, so the VMA list is now managed using
SRCU allowing lockless walking. The only impact would be the deferred file
derefencing in the case of a file mapping, since the file pointer is
released once the SRCU cleaning is done. This patch relies on the change
done recently by Paul McKenney in SRCU which now runs a callback per CPU
instead of per SRCU structure [1].
The VMA's attributes checked during the speculative page fault processing
have to be protected against parallel changes. This is done by using a per
VMA sequence lock. This sequence lock allows the speculative page fault
handler to fast check for parallel changes in progress and to abort the
speculative page fault in that case.
Once the VMA is found, the speculative page fault handler would check for
the VMA's attributes to verify that the page fault has to be handled
correctly or not. Thus the VMA is protected through a sequence lock which
allows fast detection of concurrent VMA changes. If such a change is
detected, the speculative page fault is aborted and a *classic* page fault
is tried. VMA sequence locks are added when VMA attributes which are
checked during the page fault are modified.
When the PTE is fetched, the VMA is checked to see if it has been changed,
so once the page table is locked, the VMA is valid, so any other changes
leading to touching this PTE will need to lock the page table, so no
parallel change is possible at this time.
This is a port on kernel 4.13 of the work done by Peter Zijlstra to
handle page fault without holding the mm semaphore [1].
The idea is to try to handle user space page faults without holding the
mmap_sem. This should allow better concurrency for massively threaded
process since the page fault handler will not wait for other threads memory
layout change to be done, assuming that this change is done in another part
of the process's memory space. This type page fault is named speculative
page fault. If the speculative page fault fails because of a concurrency is
detected or because underlying PMD or PTE tables are not yet allocating, it
is failing its processing and a classic page fault is then tried.
The speculative page fault (SPF) has to look for the VMA matching the fault
address without holding the mmap_sem, so the VMA list is now managed using
SRCU allowing lockless walking. The only impact would be the deferred file
derefencing in the case of a file mapping, since the file pointer is
released once the SRCU cleaning is done. This patch relies on the change
done recently by Paul McKenney in SRCU which now runs a callback per CPU
instead of per SRCU structure [1].
The VMA's attributes checked during the speculative page fault processing
have to be protected against parallel changes. This is done by using a per
VMA sequence lock. This sequence lock allows the speculative page fault
handler to fast check for parallel changes in progress and to abort the
speculative page fault in that case.
Once the VMA is found, the speculative page fault handler would check for
the VMA's attributes to verify that the page fault has to be handled
correctly or not. Thus the VMA is protected through a sequence lock which
allows fast detection of concurrent VMA changes. If such a change is
detected, the speculative page fault is aborted and a *classic* page fault
is tried. VMA sequence locks are added when VMA attributes which are
checked during the page fault are modified.
When the PTE is fetched, the VMA is checked to see if it has been changed,
so once the page table is locked, the VMA is valid, so any other changes
leading to touching this PTE will need to lock the page table, so no
parallel change is possible at this time.
Compared to the Peter's initial work, this series introduces a spin_trylock
when dealing with speculative page fault. This is required to avoid dead
lock when handling a page fault while a TLB invalidate is requested by an
other CPU holding the PTE. Another change due to a lock dependency issue
with mapping->i_mmap_rwsem.
In addition some VMA field values which are used once the PTE is unlocked
at the end the page fault path are saved into the vm_fault structure to
used the values matching the VMA at the time the PTE was locked.
This series builds on top of v4.13-rc5 and is functional on x86 and
PowerPC.
Tests have been made using a large commercial in-memory database on a
PowerPC system with 752 CPU using RFC v5. The results are very encouraging
since the loading of the 2TB database was faster by 14% with the
speculative page fault.
You specifically mention loading as most of the page faults will
happen at that time and then the working set will settle down with
very less page faults there after ? That means unless there is
another wave of page faults we wont notice performance improvement
during the runtime.
Using ebizzy test [3], which spreads a lot of threads, the result are good
when running on both a large or a small system. When using kernbench, the
The performance improvements are greater as there is a lot of creation
and destruction of anon mappings which generates constant flow of page
faults to be handled.
result are quite similar which expected as not so much multi threaded
processes are involved. But there is no performance degradation neither
which is good.
If we compile with 'make -j N' there would be a lot of threads but I
guess the problem is SPF does not support handling file mapping IIUC
which limits the performance improvement for some workloads.
------------------
Benchmarks results
Note these test have been made on top of 4.13-rc3 with the following patch
from Paul McKenney applied:
"srcu: Provide ordering for CPU not involved in grace period" [5]
Is this patch an improvement for SRCU which we are using for walking VMAs.
Ebizzy:
-------
The test is counting the number of records per second it can manage, the
higher is the best. I run it like this 'ebizzy -mTRp'. To get consistent
result I repeated the test 100 times and measure the average result, mean
deviation, max and min.
- 16 CPUs x86 VM
Records/s 4.13-rc5 4.13-rc5-spf
Average 11350.29 21760.36
Mean deviation 396.56 881.40
Max 13773 26194
Min 10567 19223
- 80 CPUs Power 8 node:
Records/s 4.13-rc5 4.13-rc5-spf
Average 33904.67 58847.91
Mean deviation 789.40 1753.19
Max 36703 68958
Min 31759 55125
Can you also mention % improvement or degradation in a new column.
The number of record per second is far better with the speculative page
fault.
The mean deviation is higher with the speculative page fault, may be
because sometime the fault are not handled in a speculative way leading to
more variation.
we need to analyze that. Why speculative page faults failed on those
occasions for exact same workload.
Kernbench:
----------
This test is building a 4.12 kernel using platform default config. The
build has been run 5 times each time.
- 16 CPUs x86 VM
Average Half load -j 8 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 166.574 (0.340779) 145.754 (0.776325)
User Time 1080.77 (2.05871) 999.272 (4.12142)
System Time 204.594 (1.02449) 116.362 (1.22974)
Percent CPU 771.2 (1.30384) 765 (0.707107)
Context Switches 46590.6 (935.591) 66316.4 (744.64)
Sleeps 84421.2 (596.612) 85186 (523.041)
Average Optimal load -j 16 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 85.422 (0.42293) 74.81 (0.419345)
User Time 1031.79 (51.6557) 954.912 (46.8439)
System Time 186.528 (19.0575) 107.514 (9.36902)
Percent CPU 1059.2 (303.607) 1056.8 (307.624)
Context Switches 67240.3 (21788.9) 89360.6 (24299.9)
Sleeps 89607.8 (5511.22) 90372.5 (5490.16)
The elapsed time is a bit shorter in the case of the SPF release, but the
impact less important since there are less multithreaded processes involved
here.
- 80 CPUs Power 8 node:
Average Half load -j 40 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 117.176 (0.824093) 116.792 (0.695392)
User Time 4412.34 (24.29) 4396.02 (24.4819)
System Time 131.106 (1.28343) 133.452 (0.708851)
Percent CPU 3876.8 (18.1439) 3877.6 (21.9955)
Context Switches 72470.2 (466.181) 72971 (673.624)
Sleeps 161294 (2284.85) 161946 (2217.9)
Average Optimal load -j 80 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 111.176 (1.11123) 111.242 (0.801542)
User Time 5930.03 (1600.07) 5929.89 (1617)
System Time 166.258 (37.0662) 169.337 (37.8419)
Percent CPU 5378.5 (1584.16) 5385.6 (1590.24)
Context Switches 117389 (47350.1) 130132 (60256.3)
Sleeps 163354 (4153.9) 163219 (2251.27)
Can you also mention % improvement or degradation in a new column.
Here the elapsed time is a bit shorter using the spf release, but we
remain in the error margin. It has to be noted that this system is not
correctly balanced on the NUMA point of view as all the available memory is
attached to one core.
Why different NUMA configuration would have changed the outcome ?
------------------------
Changes since v1:
- Remove PERF_COUNT_SW_SPF_FAILED perf event.
- Add tracing events to details speculative page fault failures.
- Cache VMA fields values which are used once the PTE is unlocked at the
end of the page fault events.
This patch enable the speculative page fault on the PowerPC
architecture.
This will try a speculative page fault without holding the mmap_sem,
if it returns with WM_FAULT_RETRY, the mmap_sem is acquired and the
s/WM_FAULT_RETRY/VM_FAULT_RETRY/
traditional page fault processing is done.
Support is only provide for BOOK3S_64 currently because:
- require CONFIG_PPC_STD_MMU because checks done in
set_access_flags_filter()
What checks are done in set_access_flags_filter() ? We are just
adding the code block in do_page_fault().
quoted hunk
- require BOOK3S because we can't support for book3e_hugetlb_preload()
called by update_mmu_cache()
Signed-off-by: Laurent Dufour <redacted>
---
arch/powerpc/include/asm/book3s/64/pgtable.h | 5 +++++
arch/powerpc/mm/fault.c | 30 +++++++++++++++++++++++++++-
2 files changed, 34 insertions(+), 1 deletion(-)
@@ -313,6 +313,11 @@ extern unsigned long pci_io_base;/* Advertise support for _PAGE_SPECIAL */#define __HAVE_ARCH_PTE_SPECIAL+/* Advertise that we call the Speculative Page Fault handler */+#if defined(CONFIG_PPC_BOOK3S_64)+#define __HAVE_ARCH_CALL_SPF+#endif+#ifndef __ASSEMBLY__/*
@@ -291,9 +291,36 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,if(is_write&&is_user)store_update_sp=store_updates_sp(regs);-if(is_user)+if(is_user){flags|=FAULT_FLAG_USER;+#if defined(__HAVE_ARCH_CALL_SPF)+/* let's try a speculative page fault without grabbing the+*mmap_sem.+*/++/*+*flagsissetlaterbasedontheVMA'sflags,forthecommon+*speculativeservice,weneedsomeflagstobeset.+*/+if(is_write)+flags|=FAULT_FLAG_WRITE;++fault=handle_speculative_fault(mm,address,flags);+if(!(fault&VM_FAULT_RETRY||fault&VM_FAULT_ERROR)){+perf_sw_event(PERF_COUNT_SW_SPF_DONE,1,+regs,address);+gotodone;
Why we should retry with classical page fault on VM_FAULT_ERROR ?
We should always return VM_FAULT_RETRY in case there is a clear
collision some where which requires retry with classical method
and return VM_FAULT_ERROR in cases where we know that it cannot
be retried and fail for good. Should not handle_speculative_fault()
be changed to accommodate this ?
+ }
+
+ /*
+ * Resetting flags since the following code assumes
+ * FAULT_FLAG_WRITE is not set.
+ */
+ flags &= ~FAULT_FLAG_WRITE;
+#endif /* defined(__HAVE_ARCH_CALL_SPF) */
Setting and resetting of FAULT_FLAG_WRITE seems confusing. Why you
say that some flags need to be set for handle_speculative_fault()
function. Could you elaborate on this ?
From: Peter Zijlstra <peterz@infradead.org>
Try a speculative fault before acquiring mmap_sem, if it returns with
VM_FAULT_RETRY continue with the mmap_sem acquisition and do the
traditional fault.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
[Clearing of FAULT_FLAG_ALLOW_RETRY is now done in
handle_speculative_fault()]
[Retry with usual fault path in the case VM_ERROR is returned by
handle_speculative_fault(). This allows signal to be delivered]
Signed-off-by: Laurent Dufour <redacted>
---
arch/x86/include/asm/pgtable_types.h | 7 +++++++
arch/x86/mm/fault.c | 19 +++++++++++++++++++
2 files changed, 26 insertions(+)
Cant mm_fault_error() be called inside handle_speculative_fault() ?
Falling through the normal page fault path again just to raise a
signal seems overkill. Looking into mm_fault_error(), it seems they
are different for x86 and powerpc.
X86:
mm_fault_error(struct pt_regs *regs, unsigned long error_code,
unsigned long address, struct vm_area_struct *vma,
unsigned int fault)
powerpc:
mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
Even in case of X86, I guess we would have reference to the faulting
VMA (after the SRCU search) which can be used to call this function
directly.
From: Paul E. McKenney <hidden> Date: 2017-08-22 00:41:33
On Mon, Aug 21, 2017 at 11:58:03AM +0530, Anshuman Khandual wrote:
On 08/18/2017 03:34 AM, Laurent Dufour wrote:
quoted
This is a port on kernel 4.13 of the work done by Peter Zijlstra to
handle page fault without holding the mm semaphore [1].
The idea is to try to handle user space page faults without holding the
mmap_sem. This should allow better concurrency for massively threaded
process since the page fault handler will not wait for other threads memory
layout change to be done, assuming that this change is done in another part
of the process's memory space. This type page fault is named speculative
page fault. If the speculative page fault fails because of a concurrency is
detected or because underlying PMD or PTE tables are not yet allocating, it
is failing its processing and a classic page fault is then tried.
The speculative page fault (SPF) has to look for the VMA matching the fault
address without holding the mmap_sem, so the VMA list is now managed using
SRCU allowing lockless walking. The only impact would be the deferred file
derefencing in the case of a file mapping, since the file pointer is
released once the SRCU cleaning is done. This patch relies on the change
done recently by Paul McKenney in SRCU which now runs a callback per CPU
instead of per SRCU structure [1].
The VMA's attributes checked during the speculative page fault processing
have to be protected against parallel changes. This is done by using a per
VMA sequence lock. This sequence lock allows the speculative page fault
handler to fast check for parallel changes in progress and to abort the
speculative page fault in that case.
Once the VMA is found, the speculative page fault handler would check for
the VMA's attributes to verify that the page fault has to be handled
correctly or not. Thus the VMA is protected through a sequence lock which
allows fast detection of concurrent VMA changes. If such a change is
detected, the speculative page fault is aborted and a *classic* page fault
is tried. VMA sequence locks are added when VMA attributes which are
checked during the page fault are modified.
When the PTE is fetched, the VMA is checked to see if it has been changed,
so once the page table is locked, the VMA is valid, so any other changes
leading to touching this PTE will need to lock the page table, so no
parallel change is possible at this time.
Compared to the Peter's initial work, this series introduces a spin_trylock
when dealing with speculative page fault. This is required to avoid dead
lock when handling a page fault while a TLB invalidate is requested by an
other CPU holding the PTE. Another change due to a lock dependency issue
with mapping->i_mmap_rwsem.
In addition some VMA field values which are used once the PTE is unlocked
at the end the page fault path are saved into the vm_fault structure to
used the values matching the VMA at the time the PTE was locked.
This series builds on top of v4.13-rc5 and is functional on x86 and
PowerPC.
Tests have been made using a large commercial in-memory database on a
PowerPC system with 752 CPU using RFC v5. The results are very encouraging
since the loading of the 2TB database was faster by 14% with the
speculative page fault.
You specifically mention loading as most of the page faults will
happen at that time and then the working set will settle down with
very less page faults there after ? That means unless there is
another wave of page faults we wont notice performance improvement
during the runtime.
quoted
Using ebizzy test [3], which spreads a lot of threads, the result are good
when running on both a large or a small system. When using kernbench, the
The performance improvements are greater as there is a lot of creation
and destruction of anon mappings which generates constant flow of page
faults to be handled.
quoted
result are quite similar which expected as not so much multi threaded
processes are involved. But there is no performance degradation neither
which is good.
If we compile with 'make -j N' there would be a lot of threads but I
guess the problem is SPF does not support handling file mapping IIUC
which limits the performance improvement for some workloads.
quoted
------------------
Benchmarks results
Note these test have been made on top of 4.13-rc3 with the following patch
from Paul McKenney applied:
"srcu: Provide ordering for CPU not involved in grace period" [5]
Is this patch an improvement for SRCU which we are using for walking VMAs.
It is a tweak to an earlier patch that parallelizes SRCU callback
handling.
Thanx, Paul
quoted
Ebizzy:
-------
The test is counting the number of records per second it can manage, the
higher is the best. I run it like this 'ebizzy -mTRp'. To get consistent
result I repeated the test 100 times and measure the average result, mean
deviation, max and min.
- 16 CPUs x86 VM
Records/s 4.13-rc5 4.13-rc5-spf
Average 11350.29 21760.36
Mean deviation 396.56 881.40
Max 13773 26194
Min 10567 19223
- 80 CPUs Power 8 node:
Records/s 4.13-rc5 4.13-rc5-spf
Average 33904.67 58847.91
Mean deviation 789.40 1753.19
Max 36703 68958
Min 31759 55125
Can you also mention % improvement or degradation in a new column.
quoted
The number of record per second is far better with the speculative page
fault.
The mean deviation is higher with the speculative page fault, may be
because sometime the fault are not handled in a speculative way leading to
more variation.
we need to analyze that. Why speculative page faults failed on those
occasions for exact same workload.
quoted
Kernbench:
----------
This test is building a 4.12 kernel using platform default config. The
build has been run 5 times each time.
- 16 CPUs x86 VM
Average Half load -j 8 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 166.574 (0.340779) 145.754 (0.776325)
User Time 1080.77 (2.05871) 999.272 (4.12142)
System Time 204.594 (1.02449) 116.362 (1.22974)
Percent CPU 771.2 (1.30384) 765 (0.707107)
Context Switches 46590.6 (935.591) 66316.4 (744.64)
Sleeps 84421.2 (596.612) 85186 (523.041)
quoted
Average Optimal load -j 16 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 85.422 (0.42293) 74.81 (0.419345)
User Time 1031.79 (51.6557) 954.912 (46.8439)
System Time 186.528 (19.0575) 107.514 (9.36902)
Percent CPU 1059.2 (303.607) 1056.8 (307.624)
Context Switches 67240.3 (21788.9) 89360.6 (24299.9)
Sleeps 89607.8 (5511.22) 90372.5 (5490.16)
The elapsed time is a bit shorter in the case of the SPF release, but the
impact less important since there are less multithreaded processes involved
here.
- 80 CPUs Power 8 node:
Average Half load -j 40 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 117.176 (0.824093) 116.792 (0.695392)
User Time 4412.34 (24.29) 4396.02 (24.4819)
System Time 131.106 (1.28343) 133.452 (0.708851)
Percent CPU 3876.8 (18.1439) 3877.6 (21.9955)
Context Switches 72470.2 (466.181) 72971 (673.624)
Sleeps 161294 (2284.85) 161946 (2217.9)
Average Optimal load -j 80 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 111.176 (1.11123) 111.242 (0.801542)
User Time 5930.03 (1600.07) 5929.89 (1617)
System Time 166.258 (37.0662) 169.337 (37.8419)
Percent CPU 5378.5 (1584.16) 5385.6 (1590.24)
Context Switches 117389 (47350.1) 130132 (60256.3)
Sleeps 163354 (4153.9) 163219 (2251.27)
Can you also mention % improvement or degradation in a new column.
quoted
Here the elapsed time is a bit shorter using the spf release, but we
remain in the error margin. It has to be noted that this system is not
correctly balanced on the NUMA point of view as all the available memory is
attached to one core.
Why different NUMA configuration would have changed the outcome ?
quoted
------------------------
Changes since v1:
- Remove PERF_COUNT_SW_SPF_FAILED perf event.
- Add tracing events to details speculative page fault failures.
- Cache VMA fields values which are used once the PTE is unlocked at the
end of the page fault events.
+ /*
+ * MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
+ * are not compatible with the speculative page fault processing.
+ */
+ pol = __get_vma_policy(vma, address);
+ if (!pol)
+ pol = get_task_policy(current);
+ if (pol && pol->mode == MPOL_INTERLEAVE)
+ goto unlock;
include/linux/mempolicy.h defines
struct mempolicy *get_task_policy(struct task_struct *p);
struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
unsigned long addr);
only for CONFIG_NUMA configs.
This is a port on kernel 4.13 of the work done by Peter Zijlstra to
handle page fault without holding the mm semaphore [1].
The idea is to try to handle user space page faults without holding the
mmap_sem. This should allow better concurrency for massively threaded
process since the page fault handler will not wait for other threads memory
layout change to be done, assuming that this change is done in another part
of the process's memory space. This type page fault is named speculative
page fault. If the speculative page fault fails because of a concurrency is
detected or because underlying PMD or PTE tables are not yet allocating, it
is failing its processing and a classic page fault is then tried.
The speculative page fault (SPF) has to look for the VMA matching the fault
address without holding the mmap_sem, so the VMA list is now managed using
SRCU allowing lockless walking. The only impact would be the deferred file
derefencing in the case of a file mapping, since the file pointer is
released once the SRCU cleaning is done. This patch relies on the change
done recently by Paul McKenney in SRCU which now runs a callback per CPU
instead of per SRCU structure [1].
The VMA's attributes checked during the speculative page fault processing
have to be protected against parallel changes. This is done by using a per
VMA sequence lock. This sequence lock allows the speculative page fault
handler to fast check for parallel changes in progress and to abort the
speculative page fault in that case.
Once the VMA is found, the speculative page fault handler would check for
the VMA's attributes to verify that the page fault has to be handled
correctly or not. Thus the VMA is protected through a sequence lock which
allows fast detection of concurrent VMA changes. If such a change is
detected, the speculative page fault is aborted and a *classic* page fault
is tried. VMA sequence locks are added when VMA attributes which are
checked during the page fault are modified.
When the PTE is fetched, the VMA is checked to see if it has been changed,
so once the page table is locked, the VMA is valid, so any other changes
leading to touching this PTE will need to lock the page table, so no
parallel change is possible at this time.
Compared to the Peter's initial work, this series introduces a spin_trylock
when dealing with speculative page fault. This is required to avoid dead
lock when handling a page fault while a TLB invalidate is requested by an
other CPU holding the PTE. Another change due to a lock dependency issue
with mapping->i_mmap_rwsem.
In addition some VMA field values which are used once the PTE is unlocked
at the end the page fault path are saved into the vm_fault structure to
used the values matching the VMA at the time the PTE was locked.
This series builds on top of v4.13-rc5 and is functional on x86 and
PowerPC.
Tests have been made using a large commercial in-memory database on a
PowerPC system with 752 CPU using RFC v5. The results are very encouraging
since the loading of the 2TB database was faster by 14% with the
speculative page fault.
You specifically mention loading as most of the page faults will
happen at that time and then the working set will settle down with
very less page faults there after ? That means unless there is
another wave of page faults we wont notice performance improvement
during the runtime.
I just captured performance statistic during the database loading then
since the database was not stimulated, there was no page faults generated.
Further tests will be made while the database is running but I didn't have
the framework to do so right now.
quoted
Using ebizzy test [3], which spreads a lot of threads, the result are good
when running on both a large or a small system. When using kernbench, the
The performance improvements are greater as there is a lot of creation
and destruction of anon mappings which generates constant flow of page
faults to be handled.
quoted
result are quite similar which expected as not so much multi threaded
processes are involved. But there is no performance degradation neither
which is good.
If we compile with 'make -j N' there would be a lot of threads but I
guess the problem is SPF does not support handling file mapping IIUC
which limits the performance improvement for some workloads.
Yes but that test is showing that there is no performance degradation which
is good.
quoted
------------------
Benchmarks results
Note these test have been made on top of 4.13-rc3 with the following patch
from Paul McKenney applied:
"srcu: Provide ordering for CPU not involved in grace period" [5]
Is this patch an improvement for SRCU which we are using for walking VMAs.
quoted
Ebizzy:
-------
The test is counting the number of records per second it can manage, the
higher is the best. I run it like this 'ebizzy -mTRp'. To get consistent
result I repeated the test 100 times and measure the average result, mean
deviation, max and min.
- 16 CPUs x86 VM
Records/s 4.13-rc5 4.13-rc5-spf
Average 11350.29 21760.36
Mean deviation 396.56 881.40
Max 13773 26194
Min 10567 19223
- 80 CPUs Power 8 node:
Records/s 4.13-rc5 4.13-rc5-spf
Average 33904.67 58847.91
Mean deviation 789.40 1753.19
Max 36703 68958
Min 31759 55125
Can you also mention % improvement or degradation in a new column.
Fair enough:
- 16 CPUs x86 VM
Records/s 4.13-rc5 4.13-rc5-spf
Average 11350.29 21760.36 +92%
Mean deviation 396.56 881.40 +122%
Max 13773 26194 +90%
Min 10567 19223 +82%
- 80 CPUs Power 8 node:
Records/s 4.13-rc5 4.13-rc5-spf
Average 33904.67 58847.91 +74%
Mean deviation 789.40 1753.19 +122%
Max 36703 68958 +88%
Min 31759 55125 +74%
quoted
The number of record per second is far better with the speculative page
fault.
The mean deviation is higher with the speculative page fault, may be
because sometime the fault are not handled in a speculative way leading to
more variation.
we need to analyze that. Why speculative page faults failed on those
occasions for exact same workload.
That's not even clear that the mean deviation increasing is due to
speculative page fault failure. This will need to be study, but even if the
mean deviation is more important, the result are far better anyway.
quoted
Kernbench:
----------
This test is building a 4.12 kernel using platform default config. The
build has been run 5 times each time.
- 16 CPUs x86 VM
Average Half load -j 8 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 166.574 (0.340779) 145.754 (0.776325)
User Time 1080.77 (2.05871) 999.272 (4.12142)
System Time 204.594 (1.02449) 116.362 (1.22974)
Percent CPU 771.2 (1.30384) 765 (0.707107)
Context Switches 46590.6 (935.591) 66316.4 (744.64)
Sleeps 84421.2 (596.612) 85186 (523.041)
quoted
Average Optimal load -j 16 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 85.422 (0.42293) 74.81 (0.419345)
User Time 1031.79 (51.6557) 954.912 (46.8439)
System Time 186.528 (19.0575) 107.514 (9.36902)
Percent CPU 1059.2 (303.607) 1056.8 (307.624)
Context Switches 67240.3 (21788.9) 89360.6 (24299.9)
Sleeps 89607.8 (5511.22) 90372.5 (5490.16)
The elapsed time is a bit shorter in the case of the SPF release, but the
impact less important since there are less multithreaded processes involved
here.
- 80 CPUs Power 8 node:
Average Half load -j 40 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 117.176 (0.824093) 116.792 (0.695392)
User Time 4412.34 (24.29) 4396.02 (24.4819)
System Time 131.106 (1.28343) 133.452 (0.708851)
Percent CPU 3876.8 (18.1439) 3877.6 (21.9955)
Context Switches 72470.2 (466.181) 72971 (673.624)
Sleeps 161294 (2284.85) 161946 (2217.9)
Average Optimal load -j 80 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 111.176 (1.11123) 111.242 (0.801542)
User Time 5930.03 (1600.07) 5929.89 (1617)
System Time 166.258 (37.0662) 169.337 (37.8419)
Percent CPU 5378.5 (1584.16) 5385.6 (1590.24)
Context Switches 117389 (47350.1) 130132 (60256.3)
Sleeps 163354 (4153.9) 163219 (2251.27)
Can you also mention % improvement or degradation in a new column.
Fair enough:
- 16 CPUs x86 VM
Average Half load -j 8 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 166.574 (0.340779) 145.754 (0.776325) -12.5%
User Time 1080.77 (2.05871) 999.272 (4.12142) -7.54%
System Time 204.594 (1.02449) 116.362 (1.22974) -43.13%
Percent CPU 771.2 (1.30384) 765 (0.707107) -0.8%
Context Switches 46590.6 (935.591) 66316.4 (744.64) +42.34%
Sleeps 84421.2 (596.612) 85186 (523.041) +0.9%
Average Optimal load -j 16 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 85.422 (0.42293) 74.81 (0.419345) -12.42%
User Time 1031.79 (51.6557) 954.912 (46.8439) -7.45%
System Time 186.528 (19.0575) 107.514 (9.36902) -42.36%
Percent CPU 1059.2 (303.607) 1056.8 (307.624) -0.23%
Context Switches 67240.3 (21788.9) 89360.6 (24299.9) +32.9%
Sleeps 89607.8 (5511.22) 90372.5 (5490.16) +0.85%
- 80 CPUs Power 8 node:
Average Half load -j 40 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 117.176 (0.824093) 116.792 (0.695392) -0.33%
User Time 4412.34 (24.29) 4396.02 (24.4819) -0.37%
System Time 131.106 (1.28343) 133.452 (0.708851) +1.79%
Percent CPU 3876.8 (18.1439) 3877.6 (21.9955) +0.02%
Context Switches 72470.2 (466.181) 72971 (673.624) +0.69%
Sleeps 161294 (2284.85) 161946 (2217.9) +0.40%
Average Optimal load -j 80 Run (std deviation)
4.13.0-rc5 4.13.0-rc5-spf
Elapsed Time 111.176 (1.11123) 111.242 (0.801542) +0.06%
User Time 5930.03 (1600.07) 5929.89 (1617) +0%
System Time 166.258 (37.0662) 169.337 (37.8419) +1.85%
Percent CPU 5378.5 (1584.16) 5385.6 (1590.24) +0.13%
Context Switches 117389 (47350.1) 130132 (60256.3) +10.86%
Sleeps 163354 (4153.9) 163219 (2251.27) -0.08%
quoted
Here the elapsed time is a bit shorter using the spf release, but we
remain in the error margin. It has to be noted that this system is not
correctly balanced on the NUMA point of view as all the available memory is
attached to one core.
Why different NUMA configuration would have changed the outcome ?
I guess, process will have been scheduled nearest the memory, or spread in
a different way on the core if memory will be attached to.
quoted
------------------------
Changes since v1:
- Remove PERF_COUNT_SW_SPF_FAILED perf event.
- Add tracing events to details speculative page fault failures.
- Cache VMA fields values which are used once the PTE is unlocked at the
end of the page fault events.
Why is this required ?
Please see patch 07/20 for details.
Cheers,
Laurent.
From: Kirill A. Shutemov <hidden> Date: 2017-08-27 00:18:35
On Fri, Aug 18, 2017 at 12:05:13AM +0200, Laurent Dufour wrote:
+/*
+ * vm_normal_page() adds some processing which should be done while
+ * hodling the mmap_sem.
+ */
+int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
+ unsigned int flags)
+{
+ struct vm_fault vmf = {
+ .address = address,
+ };
+ pgd_t *pgd;
+ p4d_t *p4d;
+ pud_t *pud;
+ pmd_t *pmd;
+ int dead, seq, idx, ret = VM_FAULT_RETRY;
+ struct vm_area_struct *vma;
+ struct mempolicy *pol;
+
+ /* Clear flags that may lead to release the mmap_sem to retry */
+ flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
+ flags |= FAULT_FLAG_SPECULATIVE;
+
+ idx = srcu_read_lock(&vma_srcu);
+ vma = find_vma_srcu(mm, address);
+ if (!vma)
+ goto unlock;
+
+ /*
+ * Validate the VMA found by the lockless lookup.
+ */
+ dead = RB_EMPTY_NODE(&vma->vm_rb);
+ seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
+ if ((seq & 1) || dead)
+ goto unlock;
+
+ /*
+ * Can't call vm_ops service has we don't know what they would do
+ * with the VMA.
+ * This include huge page from hugetlbfs.
+ */
+ if (vma->vm_ops)
+ goto unlock;
I think we need to have a way to white-list safe ->vm_ops.
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
+
+ vmf.vma_flags = READ_ONCE(vma->vm_flags);
+ vmf.vma_page_prot = READ_ONCE(vma->vm_page_prot);
+
+ /* Can't call userland page fault handler in the speculative path */
+ if (unlikely(vmf.vma_flags & VM_UFFD_MISSING))
+ goto unlock;
+
+ /*
+ * MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
+ * are not compatible with the speculative page fault processing.
+ */
+ pol = __get_vma_policy(vma, address);
+ if (!pol)
+ pol = get_task_policy(current);
+ if (pol && pol->mode == MPOL_INTERLEAVE)
+ goto unlock;
+
+ if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP)
+ /*
+ * This could be detected by the check address against VMA's
+ * boundaries but we want to trace it as not supported instead
+ * of changed.
+ */
+ goto unlock;
+
+ if (address < READ_ONCE(vma->vm_start)
+ || READ_ONCE(vma->vm_end) <= address)
+ goto unlock;
+
+ /*
+ * The three following checks are copied from access_error from
+ * arch/x86/mm/fault.c
+ */
+ if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
+ flags & FAULT_FLAG_INSTRUCTION,
+ flags & FAULT_FLAG_REMOTE))
+ goto unlock;
+
+ /* This is one is required to check that the VMA has write access set */
+ if (flags & FAULT_FLAG_WRITE) {
+ if (unlikely(!(vmf.vma_flags & VM_WRITE)))
+ goto unlock;
+ } else {
+ if (unlikely(!(vmf.vma_flags & (VM_READ | VM_EXEC | VM_WRITE))))
+ goto unlock;
+ }
+
+ /*
+ * Do a speculative lookup of the PTE entry.
+ */
+ local_irq_disable();
+ pgd = pgd_offset(mm, address);
+ if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
+ goto out_walk;
+
+ p4d = p4d_alloc(mm, pgd, address);
+ if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
+ goto out_walk;
+
+ pud = pud_alloc(mm, p4d, address);
+ if (pud_none(*pud) || unlikely(pud_bad(*pud)))
+ goto out_walk;
+
+ pmd = pmd_offset(pud, address);
+ if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
+ goto out_walk;
+
+ /*
+ * The above does not allocate/instantiate page-tables because doing so
+ * would lead to the possibility of instantiating page-tables after
+ * free_pgtables() -- and consequently leaking them.
+ *
+ * The result is that we take at least one !speculative fault per PMD
+ * in order to instantiate it.
+ */
Doing all this job and just give up because we cannot allocate page tables
looks very wasteful to me.
Have you considered to look how we can hand over from speculative to
non-speculative path without starting from scratch (when possible)?
+ /* Transparent huge pages are not supported. */
+ if (unlikely(pmd_trans_huge(*pmd)))
+ goto out_walk;
That's looks like a blocker to me.
Is there any problem with making it supported (besides plain coding)?
+
+ vmf.vma = vma;
+ vmf.pmd = pmd;
+ vmf.pgoff = linear_page_index(vma, address);
+ vmf.gfp_mask = __get_fault_gfp_mask(vma);
+ vmf.sequence = seq;
+ vmf.flags = flags;
+
+ local_irq_enable();
+
+ /*
+ * We need to re-validate the VMA after checking the bounds, otherwise
+ * we might have a false positive on the bounds.
+ */
+ if (read_seqcount_retry(&vma->vm_sequence, seq))
+ goto unlock;
+
+ ret = handle_pte_fault(&vmf);
+
+unlock:
+ srcu_read_unlock(&vma_srcu, idx);
+ return ret;
+
+out_walk:
+ local_irq_enable();
+ goto unlock;
+}
+#endif /* __HAVE_ARCH_CALL_SPF */
+
/*
* By the time we get here, we already hold the mm semaphore
*
--
2.7.4
From: Peter Zijlstra <peterz@infradead.org> Date: 2017-08-28 09:37:59
On Sun, Aug 27, 2017 at 03:18:23AM +0300, Kirill A. Shutemov wrote:
On Fri, Aug 18, 2017 at 12:05:13AM +0200, Laurent Dufour wrote:
quoted
+ /*
+ * Can't call vm_ops service has we don't know what they would do
+ * with the VMA.
+ * This include huge page from hugetlbfs.
+ */
+ if (vma->vm_ops)
+ goto unlock;
I think we need to have a way to white-list safe ->vm_ops.
Either that, or simply teach all ->fault() callbacks about speculative
faults. Shouldn't be too hard, just 'work'.
quoted
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
Yes, that was very much not intended. It wrecks most of the fun. This
really _should_ work for file maps too.
quoted
+ /*
+ * Do a speculative lookup of the PTE entry.
+ */
+ local_irq_disable();
+ pgd = pgd_offset(mm, address);
+ if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
+ goto out_walk;
+
+ p4d = p4d_alloc(mm, pgd, address);
+ if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
+ goto out_walk;
+
+ pud = pud_alloc(mm, p4d, address);
+ if (pud_none(*pud) || unlikely(pud_bad(*pud)))
+ goto out_walk;
+
+ pmd = pmd_offset(pud, address);
+ if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
+ goto out_walk;
+
+ /*
+ * The above does not allocate/instantiate page-tables because doing so
+ * would lead to the possibility of instantiating page-tables after
+ * free_pgtables() -- and consequently leaking them.
+ *
+ * The result is that we take at least one !speculative fault per PMD
+ * in order to instantiate it.
+ */
Doing all this job and just give up because we cannot allocate page tables
looks very wasteful to me.
Have you considered to look how we can hand over from speculative to
non-speculative path without starting from scratch (when possible)?
So we _can_ in fact allocate and install page-tables, but we have to be
very careful about it. The interesting case is where we race with
free_pgtables() and install a page that was just taken out.
But since we already have the VMA I think we can do something like:
if (p*g_none()) {
p*d_t *new = p*d_alloc_one(mm, address);
spin_lock(&mm->page_table_lock);
if (!vma_changed_or_dead(vma,seq)) {
if (p*d_none())
p*d_populate(mm, p*d, new);
else
p*d_free(new);
new = NULL;
}
spin_unlock(&mm->page_table_lock);
if (new) {
p*d_free(new);
goto out_walk;
}
}
I just never bothered with that, figured we ought to get the basics
working before trying to be clever.
quoted
+ /* Transparent huge pages are not supported. */
+ if (unlikely(pmd_trans_huge(*pmd)))
+ goto out_walk;
That's looks like a blocker to me.
Is there any problem with making it supported (besides plain coding)?
Not that I can remember, but I never really looked at THP, I don't think
we even had that when I did the first versions.
From: Benjamin Herrenschmidt <benh@kernel.crashing.org> Date: 2017-08-28 21:24:57
On Mon, 2017-08-28 at 11:37 +0200, Peter Zijlstra wrote:
quoted
Doing all this job and just give up because we cannot allocate page tables
looks very wasteful to me.
Have you considered to look how we can hand over from speculative to
non-speculative path without starting from scratch (when possible)?
So we _can_ in fact allocate and install page-tables, but we have to be
very careful about it. The interesting case is where we race with
free_pgtables() and install a page that was just taken out.
But since we already have the VMA I think we can do something like:
That makes me extremely nervous... there could be all sort of
assumptions esp. in arch code about the fact that we never populate the
tree without the mm sem.
We'd have to audit archs closely. Things like the page walk cache
flushing on power etc...
I don't mind the "retry" .. .we've brought stuff in the L1 cache
already which I would expect to be the bulk of the overhead, and the
allocation case isn't that common. Do we have numbers to show how
destrimental this is today ?
Cheers,
Ben.
That makes me extremely nervous... there could be all sort of
assumptions esp. in arch code about the fact that we never populate the
tree without the mm sem.
We'd have to audit archs closely. Things like the page walk cache
flushing on power etc...
Yes the whole thing is quite risky. Probably will need some
kind of per architecture opt-in scheme?
-Andi
On Fri, Aug 18, 2017 at 12:05:13AM +0200, Laurent Dufour wrote:
quoted
+/*
+ * vm_normal_page() adds some processing which should be done while
+ * hodling the mmap_sem.
+ */
+int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
+ unsigned int flags)
+{
+ struct vm_fault vmf = {
+ .address = address,
+ };
+ pgd_t *pgd;
+ p4d_t *p4d;
+ pud_t *pud;
+ pmd_t *pmd;
+ int dead, seq, idx, ret = VM_FAULT_RETRY;
+ struct vm_area_struct *vma;
+ struct mempolicy *pol;
+
+ /* Clear flags that may lead to release the mmap_sem to retry */
+ flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
+ flags |= FAULT_FLAG_SPECULATIVE;
+
+ idx = srcu_read_lock(&vma_srcu);
+ vma = find_vma_srcu(mm, address);
+ if (!vma)
+ goto unlock;
+
+ /*
+ * Validate the VMA found by the lockless lookup.
+ */
+ dead = RB_EMPTY_NODE(&vma->vm_rb);
+ seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
+ if ((seq & 1) || dead)
+ goto unlock;
+
+ /*
+ * Can't call vm_ops service has we don't know what they would do
+ * with the VMA.
+ * This include huge page from hugetlbfs.
+ */
+ if (vma->vm_ops)
+ goto unlock;
I think we need to have a way to white-list safe ->vm_ops.
Hi Kirill,
Yes this would be a good optimization done in a next step.
quoted
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
You're right I'll add it in the next version.
For the record, the root cause is that __anon_vma_prepare() requires the
mmap_sem to be held because vm_next and vm_prev must be safe.
quoted
+
+ vmf.vma_flags = READ_ONCE(vma->vm_flags);
+ vmf.vma_page_prot = READ_ONCE(vma->vm_page_prot);
+
+ /* Can't call userland page fault handler in the speculative path */
+ if (unlikely(vmf.vma_flags & VM_UFFD_MISSING))
+ goto unlock;
+
+ /*
+ * MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
+ * are not compatible with the speculative page fault processing.
+ */
+ pol = __get_vma_policy(vma, address);
+ if (!pol)
+ pol = get_task_policy(current);
+ if (pol && pol->mode == MPOL_INTERLEAVE)
+ goto unlock;
+
+ if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP)
+ /*
+ * This could be detected by the check address against VMA's
+ * boundaries but we want to trace it as not supported instead
+ * of changed.
+ */
+ goto unlock;
+
+ if (address < READ_ONCE(vma->vm_start)
+ || READ_ONCE(vma->vm_end) <= address)
+ goto unlock;
+
+ /*
+ * The three following checks are copied from access_error from
+ * arch/x86/mm/fault.c
+ */
+ if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
+ flags & FAULT_FLAG_INSTRUCTION,
+ flags & FAULT_FLAG_REMOTE))
+ goto unlock;
+
+ /* This is one is required to check that the VMA has write access set */
+ if (flags & FAULT_FLAG_WRITE) {
+ if (unlikely(!(vmf.vma_flags & VM_WRITE)))
+ goto unlock;
+ } else {
+ if (unlikely(!(vmf.vma_flags & (VM_READ | VM_EXEC | VM_WRITE))))
+ goto unlock;
+ }
+
+ /*
+ * Do a speculative lookup of the PTE entry.
+ */
+ local_irq_disable();
+ pgd = pgd_offset(mm, address);
+ if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
+ goto out_walk;
+
+ p4d = p4d_alloc(mm, pgd, address);
+ if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
+ goto out_walk;
+
+ pud = pud_alloc(mm, p4d, address);
+ if (pud_none(*pud) || unlikely(pud_bad(*pud)))
+ goto out_walk;
+
+ pmd = pmd_offset(pud, address);
+ if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
+ goto out_walk;
+
+ /*
+ * The above does not allocate/instantiate page-tables because doing so
+ * would lead to the possibility of instantiating page-tables after
+ * free_pgtables() -- and consequently leaking them.
+ *
+ * The result is that we take at least one !speculative fault per PMD
+ * in order to instantiate it.
+ */
Doing all this job and just give up because we cannot allocate page tables
looks very wasteful to me.
Have you considered to look how we can hand over from speculative to
non-speculative path without starting from scratch (when possible)?
Not really, but as mentioned by Benjamin and Andy, this will require care
from the architecture code.
This may be a future optimization, but it will require guarantee from the
architecture code as well.
quoted
+ /* Transparent huge pages are not supported. */
+ if (unlikely(pmd_trans_huge(*pmd)))
+ goto out_walk;
That's looks like a blocker to me.
Is there any problem with making it supported (besides plain coding)?
To be honest, I can't remember why I added such a check, may be for safety
reason, but I need to double check that again. I'll do so and come back
later with a statement.
Thanks,
Laurent.
quoted
+
+ vmf.vma = vma;
+ vmf.pmd = pmd;
+ vmf.pgoff = linear_page_index(vma, address);
+ vmf.gfp_mask = __get_fault_gfp_mask(vma);
+ vmf.sequence = seq;
+ vmf.flags = flags;
+
+ local_irq_enable();
+
+ /*
+ * We need to re-validate the VMA after checking the bounds, otherwise
+ * we might have a false positive on the bounds.
+ */
+ if (read_seqcount_retry(&vma->vm_sequence, seq))
+ goto unlock;
+
+ ret = handle_pte_fault(&vmf);
+
+unlock:
+ srcu_read_unlock(&vma_srcu, idx);
+ return ret;
+
+out_walk:
+ local_irq_enable();
+ goto unlock;
+}
+#endif /* __HAVE_ARCH_CALL_SPF */
+
/*
* By the time we get here, we already hold the mm semaphore
*
--
2.7.4
From: Peter Zijlstra <peterz@infradead.org> Date: 2017-08-29 08:34:21
On Tue, Aug 29, 2017 at 07:14:37AM +1000, Benjamin Herrenschmidt wrote:
On Mon, 2017-08-28 at 11:37 +0200, Peter Zijlstra wrote:
quoted
quoted
Doing all this job and just give up because we cannot allocate page tables
looks very wasteful to me.
Have you considered to look how we can hand over from speculative to
non-speculative path without starting from scratch (when possible)?
So we _can_ in fact allocate and install page-tables, but we have to be
very careful about it. The interesting case is where we race with
free_pgtables() and install a page that was just taken out.
But since we already have the VMA I think we can do something like:
That makes me extremely nervous... there could be all sort of
assumptions esp. in arch code about the fact that we never populate the
tree without the mm sem.
That _would_ be somewhat dodgy, because that means it needs to rely on
taking mmap_sem for _writing_ to undo things and arch/powerpc/ doesn't
have many down_write.*mmap_sem:
$ git grep "down_write.*mmap_sem" arch/powerpc/
arch/powerpc/kernel/vdso.c: if (down_write_killable(&mm->mmap_sem))
arch/powerpc/kvm/book3s_64_vio.c: down_write(¤t->mm->mmap_sem);
arch/powerpc/mm/mmu_context_iommu.c: down_write(&mm->mmap_sem);
arch/powerpc/mm/subpage-prot.c: down_write(&mm->mmap_sem);
arch/powerpc/mm/subpage-prot.c: down_write(&mm->mmap_sem);
arch/powerpc/mm/subpage-prot.c: down_write(&mm->mmap_sem);
Then again, I suppose it could be relying on the implicit down_write
from things like munmap() and the like..
And things _ought_ to be ordered by the various PTLs
(mm->page_table_lock and pmd->lock) which of course doesn't mean
something accidentally snuck through.
We'd have to audit archs closely. Things like the page walk cache
flushing on power etc...
If you point me where to look, I'll have a poke around. I'm not
quite sure what you mean with pagewalk cache flushing. Your hash thing
flushes everything inside the PTL IIRC and the radix code appears fairly
'normal'.
I don't mind the "retry" .. .we've brought stuff in the L1 cache
already which I would expect to be the bulk of the overhead, and the
allocation case isn't that common. Do we have numbers to show how
destrimental this is today ?
No numbers, afaik. And like I said, I didn't consider this an actual
problem when I did these patches. But since Kirill asked ;-)
From: Peter Zijlstra <peterz@infradead.org> Date: 2017-08-29 11:28:22
On Tue, Aug 29, 2017 at 10:33:52AM +0200, Peter Zijlstra wrote:
On Tue, Aug 29, 2017 at 07:14:37AM +1000, Benjamin Herrenschmidt wrote:
quoted
We'd have to audit archs closely. Things like the page walk cache
flushing on power etc...
If you point me where to look, I'll have a poke around. I'm not
quite sure what you mean with pagewalk cache flushing. Your hash thing
flushes everything inside the PTL IIRC and the radix code appears fairly
'normal'.
mpe helped me out and explained that is the PWC hint to TBLIE.
So, you set need_flush_all when you unhook pud/pmd/pte which you then
use to set PWC. So free_pgtables() will do the PWC when it unhooks
higher level pages.
But you're right that there's some issues, free_pgtables() itself
doesn't seem to use mm->page_table_lock,pmd->lock _AT_ALL_ to unhook the
pages.
If it were to do that, things should work fine since those locks would
then serialize against the speculative faults, we would never install a
page if the VMA would be under tear-down and it would thus not be
visible to your caches either.
From: Peter Zijlstra <peterz@infradead.org> Date: 2017-08-29 12:04:54
On Tue, Aug 29, 2017 at 09:59:30AM +0200, Laurent Dufour wrote:
On 27/08/2017 02:18, Kirill A. Shutemov wrote:
quoted
quoted
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
You're right I'll add it in the next version.
For the record, the root cause is that __anon_vma_prepare() requires the
mmap_sem to be held because vm_next and vm_prev must be safe.
But should that test not be:
if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma))
goto unlock;
Because !anon vmas will never have ->anon_vma set and you don't want to
exclude those.
On Tue, Aug 29, 2017 at 09:59:30AM +0200, Laurent Dufour wrote:
quoted
On 27/08/2017 02:18, Kirill A. Shutemov wrote:
quoted
quoted
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
You're right I'll add it in the next version.
For the record, the root cause is that __anon_vma_prepare() requires the
mmap_sem to be held because vm_next and vm_prev must be safe.
But should that test not be:
if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma))
goto unlock;
Because !anon vmas will never have ->anon_vma set and you don't want to
exclude those.
Yes in the case we later allow non anonymous vmas to be handled.
Currently only anonymous vmas are supported so the check is good enough,
isn't it ?
From: Peter Zijlstra <peterz@infradead.org> Date: 2017-08-29 13:46:29
On Tue, Aug 29, 2017 at 03:18:25PM +0200, Laurent Dufour wrote:
On 29/08/2017 14:04, Peter Zijlstra wrote:
quoted
On Tue, Aug 29, 2017 at 09:59:30AM +0200, Laurent Dufour wrote:
quoted
On 27/08/2017 02:18, Kirill A. Shutemov wrote:
quoted
quoted
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
You're right I'll add it in the next version.
For the record, the root cause is that __anon_vma_prepare() requires the
mmap_sem to be held because vm_next and vm_prev must be safe.
But should that test not be:
if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma))
goto unlock;
Because !anon vmas will never have ->anon_vma set and you don't want to
exclude those.
Yes in the case we later allow non anonymous vmas to be handled.
Currently only anonymous vmas are supported so the check is good enough,
isn't it ?
That wasn't at all clear from reading the code. This makes it clear
->anon_vma is only ever looked at for anonymous.
And like Kirill says, we _really_ should start allowing some (if not
all) vm_ops. Large file based mappings aren't particularly rare.
I'm not sure we want to introduce a white-list or just bite the bullet
and audit all ->fault() implementations. But either works and isn't
terribly difficult, auditing all is more work though.
From: Peter Zijlstra <peterz@infradead.org>
Try a speculative fault before acquiring mmap_sem, if it returns with
VM_FAULT_RETRY continue with the mmap_sem acquisition and do the
traditional fault.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
[Clearing of FAULT_FLAG_ALLOW_RETRY is now done in
handle_speculative_fault()]
[Retry with usual fault path in the case VM_ERROR is returned by
handle_speculative_fault(). This allows signal to be delivered]
Signed-off-by: Laurent Dufour <redacted>
---
arch/x86/include/asm/pgtable_types.h | 7 +++++++
arch/x86/mm/fault.c | 19 +++++++++++++++++++
2 files changed, 26 insertions(+)
Cant mm_fault_error() be called inside handle_speculative_fault() ?
Falling through the normal page fault path again just to raise a
signal seems overkill. Looking into mm_fault_error(), it seems they
are different for x86 and powerpc.
X86:
mm_fault_error(struct pt_regs *regs, unsigned long error_code,
unsigned long address, struct vm_area_struct *vma,
unsigned int fault)
powerpc:
mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
Even in case of X86, I guess we would have reference to the faulting
VMA (after the SRCU search) which can be used to call this function
directly.
From: Peter Zijlstra <peterz@infradead.org>
Try a speculative fault before acquiring mmap_sem, if it returns with
VM_FAULT_RETRY continue with the mmap_sem acquisition and do the
traditional fault.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
[Clearing of FAULT_FLAG_ALLOW_RETRY is now done in
handle_speculative_fault()]
[Retry with usual fault path in the case VM_ERROR is returned by
handle_speculative_fault(). This allows signal to be delivered]
Signed-off-by: Laurent Dufour <redacted>
---
arch/x86/include/asm/pgtable_types.h | 7 +++++++
arch/x86/mm/fault.c | 19 +++++++++++++++++++
2 files changed, 26 insertions(+)
Cant mm_fault_error() be called inside handle_speculative_fault() ?
Falling through the normal page fault path again just to raise a
signal seems overkill. Looking into mm_fault_error(), it seems they
are different for x86 and powerpc.
X86:
mm_fault_error(struct pt_regs *regs, unsigned long error_code,
unsigned long address, struct vm_area_struct *vma,
unsigned int fault)
powerpc:
mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
Even in case of X86, I guess we would have reference to the faulting
VMA (after the SRCU search) which can be used to call this function
directly.
Yes I think this is doable in the case of x86.
Indeed this is not doable as the vma pointer is not returned by
handle_speculative_fault() and this is not possible to return it because
once srcu_read_unlock() is called, the pointer is no more safe.
This patch enable the speculative page fault on the PowerPC
architecture.
This will try a speculative page fault without holding the mmap_sem,
if it returns with WM_FAULT_RETRY, the mmap_sem is acquired and the
s/WM_FAULT_RETRY/VM_FAULT_RETRY/
Good catch ;)
quoted
traditional page fault processing is done.
Support is only provide for BOOK3S_64 currently because:
- require CONFIG_PPC_STD_MMU because checks done in
set_access_flags_filter()
What checks are done in set_access_flags_filter() ? We are just
adding the code block in do_page_fault().
set_access_flags_filter() is checking for vm_flags & VM_EXEC which may be
changed in our back, leading to a spurious WARN displayed.
This being said, I focused on the BOOK3S as this meaningful for large
system, and I didn't get time to check for embedded systems.
quoted
- require BOOK3S because we can't support for book3e_hugetlb_preload()
called by update_mmu_cache()
Signed-off-by: Laurent Dufour <redacted>
---
arch/powerpc/include/asm/book3s/64/pgtable.h | 5 +++++
arch/powerpc/mm/fault.c | 30 +++++++++++++++++++++++++++-
2 files changed, 34 insertions(+), 1 deletion(-)
@@ -313,6 +313,11 @@ extern unsigned long pci_io_base;/* Advertise support for _PAGE_SPECIAL */#define __HAVE_ARCH_PTE_SPECIAL+/* Advertise that we call the Speculative Page Fault handler */+#if defined(CONFIG_PPC_BOOK3S_64)+#define __HAVE_ARCH_CALL_SPF+#endif+#ifndef __ASSEMBLY__/*
@@ -291,9 +291,36 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,if(is_write&&is_user)store_update_sp=store_updates_sp(regs);-if(is_user)+if(is_user){flags|=FAULT_FLAG_USER;+#if defined(__HAVE_ARCH_CALL_SPF)+/* let's try a speculative page fault without grabbing the+*mmap_sem.+*/++/*+*flagsissetlaterbasedontheVMA'sflags,forthecommon+*speculativeservice,weneedsomeflagstobeset.+*/+if(is_write)+flags|=FAULT_FLAG_WRITE;++fault=handle_speculative_fault(mm,address,flags);+if(!(fault&VM_FAULT_RETRY||fault&VM_FAULT_ERROR)){+perf_sw_event(PERF_COUNT_SW_SPF_DONE,1,+regs,address);+gotodone;
Why we should retry with classical page fault on VM_FAULT_ERROR ?
We should always return VM_FAULT_RETRY in case there is a clear
collision some where which requires retry with classical method
and return VM_FAULT_ERROR in cases where we know that it cannot
be retried and fail for good. Should not handle_speculative_fault()
be changed to accommodate this ?
There is no need to change handle_speculative_fault(), it should return
VM_FAULT_RETRY when a retry is required. If VM_FAULT_ERROR is return, we
should be able to jump to the block dealing with VM_FAULT_ERROR and calling
vm_fault_error().
quoted
+ }
+
+ /*
+ * Resetting flags since the following code assumes
+ * FAULT_FLAG_WRITE is not set.
+ */
+ flags &= ~FAULT_FLAG_WRITE;
+#endif /* defined(__HAVE_ARCH_CALL_SPF) */
Setting and resetting of FAULT_FLAG_WRITE seems confusing. Why you
say that some flags need to be set for handle_speculative_fault()
function. Could you elaborate on this ?
FAULT_FLAG_WRITE is required to handle write access. In the case we retry
with the classical path, the flag is reset and will be set later if
!is_exec and is_write.
From: Benjamin Herrenschmidt <benh@kernel.crashing.org> Date: 2017-08-29 21:54:45
On Tue, 2017-08-29 at 13:27 +0200, Peter Zijlstra wrote:
mpe helped me out and explained that is the PWC hint to TBLIE.
So, you set need_flush_all when you unhook pud/pmd/pte which you then
use to set PWC. So free_pgtables() will do the PWC when it unhooks
higher level pages.
But you're right that there's some issues, free_pgtables() itself
doesn't seem to use mm->page_table_lock,pmd->lock _AT_ALL_ to unhook the
pages.
If it were to do that, things should work fine since those locks would
then serialize against the speculative faults, we would never install a
page if the VMA would be under tear-down and it would thus not be
visible to your caches either.
That's one case. I don't remember of *all* the cases to be honest, but
I do remember several times over the past few years thinking "ah we are
fine because the mm sem taken for writing protects us from any
concurrent tree structure change" :-)
Cheers,
Ben.
On Tue, Aug 29, 2017 at 09:59:30AM +0200, Laurent Dufour wrote:
quoted
On 27/08/2017 02:18, Kirill A. Shutemov wrote:
quoted
quoted
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
You're right I'll add it in the next version.
For the record, the root cause is that __anon_vma_prepare() requires the
mmap_sem to be held because vm_next and vm_prev must be safe.
But should that test not be:
if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma))
goto unlock;
This makes more sense. We are backing off from speculative path
because struct anon_vma has not been created for this anonymous
vma and we cannot do that without holding mmap_sem. This should
have nothing to do with vma->vm_ops availability.
On Tue, Aug 29, 2017 at 03:18:25PM +0200, Laurent Dufour wrote:
quoted
On 29/08/2017 14:04, Peter Zijlstra wrote:
quoted
On Tue, Aug 29, 2017 at 09:59:30AM +0200, Laurent Dufour wrote:
quoted
On 27/08/2017 02:18, Kirill A. Shutemov wrote:
quoted
quoted
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
You're right I'll add it in the next version.
For the record, the root cause is that __anon_vma_prepare() requires the
mmap_sem to be held because vm_next and vm_prev must be safe.
But should that test not be:
if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma))
goto unlock;
Because !anon vmas will never have ->anon_vma set and you don't want to
exclude those.
Yes in the case we later allow non anonymous vmas to be handled.
Currently only anonymous vmas are supported so the check is good enough,
isn't it ?
That wasn't at all clear from reading the code. This makes it clear
->anon_vma is only ever looked at for anonymous.
And like Kirill says, we _really_ should start allowing some (if not
all) vm_ops. Large file based mappings aren't particularly rare.
I'm not sure we want to introduce a white-list or just bite the bullet
and audit all ->fault() implementations. But either works and isn't
terribly difficult, auditing all is more work though.
filemap_fault() is used as vma-vm_ops->fault() for most of the file
systems. Changing it can enable speculative fault support for all of
them. It will still exclude other driver based vma-vm_ops->fault()
implementation. AFAICS, __lock_page_or_retry() function can drop
mm->mmap_sem if the page could not be locked right away. As suggested
by Peterz, making it understand FAULT_FLAG_SPECULATIVE should be good
enough. The patch is lightly tested for file mappings on top of this
series.
+ /* Transparent huge pages are not supported. */
+ if (unlikely(pmd_trans_huge(*pmd)))
+ goto out_walk;
That's looks like a blocker to me.
Is there any problem with making it supported (besides plain coding)?
IIUC we would have to reattempt once for each PMD level fault because
of the lack of a page table entry there. Besides do we want to support
huge pages in general as part of speculative page fault path ? The
number of faults will be very less (256 times lower on POWER and 512
times lower on X86). So is it worth it ? BTW calling hugetlb_fault()
after figuring out the VMA, works correctly inside handle_speculative
_fault() last time I checked.
@@ -1181,6 +1181,18 @@ int __lock_page_killable(struct page *__page)int__lock_page_or_retry(structpage*page,structmm_struct*mm,unsignedintflags){+if(flags&FAULT_FLAG_SPECULATIVE){+if(flags&FAULT_FLAG_KILLABLE){+intret;++ret=__lock_page_killable(page);+if(ret)+return0;+}else+__lock_page(page);+return1;+}+if(flags&FAULT_FLAG_ALLOW_RETRY){/**CAUTION!Inthiscase,mmap_semisnotreleased
Yeah, that looks right.
quoted hunk
@@ -4012,17 +4010,7 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address, goto unlock; }+ if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { trace_spf_vma_notsup(_RET_IP_, vma, address); goto unlock; }
As riel pointed out on IRC slightly later, private file maps also need
->anon_vma and those actually have ->vm_ops IIRC so the condition needs
to be slightly more complicated.
From: Peter Zijlstra <peterz@infradead.org> Date: 2017-08-30 06:14:00
On Wed, Aug 30, 2017 at 07:19:30AM +1000, Benjamin Herrenschmidt wrote:
On Tue, 2017-08-29 at 13:27 +0200, Peter Zijlstra wrote:
quoted
mpe helped me out and explained that is the PWC hint to TBLIE.
So, you set need_flush_all when you unhook pud/pmd/pte which you then
use to set PWC. So free_pgtables() will do the PWC when it unhooks
higher level pages.
But you're right that there's some issues, free_pgtables() itself
doesn't seem to use mm->page_table_lock,pmd->lock _AT_ALL_ to unhook the
pages.
If it were to do that, things should work fine since those locks would
then serialize against the speculative faults, we would never install a
page if the VMA would be under tear-down and it would thus not be
visible to your caches either.
That's one case. I don't remember of *all* the cases to be honest, but
I do remember several times over the past few years thinking "ah we are
fine because the mm sem taken for writing protects us from any
concurrent tree structure change" :-)
Well, installing always seems to use the locks (it needs to, because its
always done with down_read()), that only leaves removal, and the only
place I know that removes stuff is free_pgtables().
But I think I found another fun place, copy_page_range(). While it
(pointlessly) takes all the PTLs on the dst mm it walks the src page
tables without any PTLs.
This means that if we have a multi-threaded process doing fork() a
thread of the src mm could instantiate page-tables that will not be
copied over.
Of course, this is highly dubious behaviour to begin with, and I don't
think there's anything fundamentally wrong with missing those pages but
we should document this stuff.
On Fri, Aug 18, 2017 at 12:05:13AM +0200, Laurent Dufour wrote:
quoted
+/*
+ * vm_normal_page() adds some processing which should be done while
+ * hodling the mmap_sem.
+ */
+int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
+ unsigned int flags)
+{
+ struct vm_fault vmf = {
+ .address = address,
+ };
+ pgd_t *pgd;
+ p4d_t *p4d;
+ pud_t *pud;
+ pmd_t *pmd;
+ int dead, seq, idx, ret = VM_FAULT_RETRY;
+ struct vm_area_struct *vma;
+ struct mempolicy *pol;
+
+ /* Clear flags that may lead to release the mmap_sem to retry */
+ flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
+ flags |= FAULT_FLAG_SPECULATIVE;
+
+ idx = srcu_read_lock(&vma_srcu);
+ vma = find_vma_srcu(mm, address);
+ if (!vma)
+ goto unlock;
+
+ /*
+ * Validate the VMA found by the lockless lookup.
+ */
+ dead = RB_EMPTY_NODE(&vma->vm_rb);
+ seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
+ if ((seq & 1) || dead)
+ goto unlock;
+
+ /*
+ * Can't call vm_ops service has we don't know what they would do
+ * with the VMA.
+ * This include huge page from hugetlbfs.
+ */
+ if (vma->vm_ops)
+ goto unlock;
I think we need to have a way to white-list safe ->vm_ops.
quoted
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
quoted
+
+ vmf.vma_flags = READ_ONCE(vma->vm_flags);
+ vmf.vma_page_prot = READ_ONCE(vma->vm_page_prot);
+
+ /* Can't call userland page fault handler in the speculative path */
+ if (unlikely(vmf.vma_flags & VM_UFFD_MISSING))
+ goto unlock;
+
+ /*
+ * MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
+ * are not compatible with the speculative page fault processing.
+ */
+ pol = __get_vma_policy(vma, address);
+ if (!pol)
+ pol = get_task_policy(current);
+ if (pol && pol->mode == MPOL_INTERLEAVE)
+ goto unlock;
+
+ if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP)
+ /*
+ * This could be detected by the check address against VMA's
+ * boundaries but we want to trace it as not supported instead
+ * of changed.
+ */
+ goto unlock;
+
+ if (address < READ_ONCE(vma->vm_start)
+ || READ_ONCE(vma->vm_end) <= address)
+ goto unlock;
+
+ /*
+ * The three following checks are copied from access_error from
+ * arch/x86/mm/fault.c
+ */
+ if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
+ flags & FAULT_FLAG_INSTRUCTION,
+ flags & FAULT_FLAG_REMOTE))
+ goto unlock;
+
+ /* This is one is required to check that the VMA has write access set */
+ if (flags & FAULT_FLAG_WRITE) {
+ if (unlikely(!(vmf.vma_flags & VM_WRITE)))
+ goto unlock;
+ } else {
+ if (unlikely(!(vmf.vma_flags & (VM_READ | VM_EXEC | VM_WRITE))))
+ goto unlock;
+ }
+
+ /*
+ * Do a speculative lookup of the PTE entry.
+ */
+ local_irq_disable();
+ pgd = pgd_offset(mm, address);
+ if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
+ goto out_walk;
+
+ p4d = p4d_alloc(mm, pgd, address);
+ if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
+ goto out_walk;
+
+ pud = pud_alloc(mm, p4d, address);
+ if (pud_none(*pud) || unlikely(pud_bad(*pud)))
+ goto out_walk;
+
+ pmd = pmd_offset(pud, address);
+ if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
+ goto out_walk;
+
+ /*
+ * The above does not allocate/instantiate page-tables because doing so
+ * would lead to the possibility of instantiating page-tables after
+ * free_pgtables() -- and consequently leaking them.
+ *
+ * The result is that we take at least one !speculative fault per PMD
+ * in order to instantiate it.
+ */
Doing all this job and just give up because we cannot allocate page tables
looks very wasteful to me.
Have you considered to look how we can hand over from speculative to
non-speculative path without starting from scratch (when possible)?
quoted
+ /* Transparent huge pages are not supported. */
+ if (unlikely(pmd_trans_huge(*pmd)))
+ goto out_walk;
That's looks like a blocker to me.
Is there any problem with making it supported (besides plain coding)?
This is not straight forward, as the THP are mainly handled in
__handle_mm_fault() and it is not called during the speculative path.
Having THP handled in the speculative path sounds doable but I'd have to
double check all the callees deeper, and this will required either
redesigning __handle_mm_fault() or doing the job in a dedicated way in
handle_speculative_fault() .
Furthermore, we should handle both PUD and PMD's level huge pages.
This being said, I can't see any blocking issue at this time except plain
coding but I'd prefer to get it done in a next step, as an optimization,
since huge page's faults are far less frequent per design.
Having _standard_ page's fault handled in a speculative way is already
providing good performance improvement, we should consider having it
upstreamed and then adding support for THP as well as other compatible
vm_ops like hugetlb, isn't it ?
Cheers,
Laurent.
quoted
+
+ vmf.vma = vma;
+ vmf.pmd = pmd;
+ vmf.pgoff = linear_page_index(vma, address);
+ vmf.gfp_mask = __get_fault_gfp_mask(vma);
+ vmf.sequence = seq;
+ vmf.flags = flags;
+
+ local_irq_enable();
+
+ /*
+ * We need to re-validate the VMA after checking the bounds, otherwise
+ * we might have a false positive on the bounds.
+ */
+ if (read_seqcount_retry(&vma->vm_sequence, seq))
+ goto unlock;
+
+ ret = handle_pte_fault(&vmf);
+
+unlock:
+ srcu_read_unlock(&vma_srcu, idx);
+ return ret;
+
+out_walk:
+ local_irq_enable();
+ goto unlock;
+}
+#endif /* __HAVE_ARCH_CALL_SPF */
+
/*
* By the time we get here, we already hold the mm semaphore
*
--
2.7.4
@@ -1181,6 +1181,18 @@ int __lock_page_killable(struct page *__page)int__lock_page_or_retry(structpage*page,structmm_struct*mm,unsignedintflags){+if(flags&FAULT_FLAG_SPECULATIVE){+if(flags&FAULT_FLAG_KILLABLE){+intret;++ret=__lock_page_killable(page);+if(ret)+return0;+}else+__lock_page(page);+return1;+}+if(flags&FAULT_FLAG_ALLOW_RETRY){/**CAUTION!Inthiscase,mmap_semisnotreleased
Yeah, that looks right.
Hum, I'm wondering if FAULT_FLAG_RETRY_NOWAIT should be forced in the
speculative path in that case to match the semantics of
__lock_page_or_retry().
quoted
@@ -4012,17 +4010,7 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address, goto unlock; }+ if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { trace_spf_vma_notsup(_RET_IP_, vma, address); goto unlock; }
As riel pointed out on IRC slightly later, private file maps also need
->anon_vma and those actually have ->vm_ops IIRC so the condition needs
to be slightly more complicated.
Yes I read again the code and lead to the same conclusion.
On Tue, Aug 29, 2017 at 03:18:25PM +0200, Laurent Dufour wrote:
quoted
On 29/08/2017 14:04, Peter Zijlstra wrote:
quoted
On Tue, Aug 29, 2017 at 09:59:30AM +0200, Laurent Dufour wrote:
quoted
On 27/08/2017 02:18, Kirill A. Shutemov wrote:
quoted
quoted
+
+ if (unlikely(!vma->anon_vma))
+ goto unlock;
It deserves a comment.
You're right I'll add it in the next version.
For the record, the root cause is that __anon_vma_prepare() requires the
mmap_sem to be held because vm_next and vm_prev must be safe.
But should that test not be:
if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma))
goto unlock;
Because !anon vmas will never have ->anon_vma set and you don't want to
exclude those.
Yes in the case we later allow non anonymous vmas to be handled.
Currently only anonymous vmas are supported so the check is good enough,
isn't it ?
That wasn't at all clear from reading the code. This makes it clear
->anon_vma is only ever looked at for anonymous.
And like Kirill says, we _really_ should start allowing some (if not
all) vm_ops. Large file based mappings aren't particularly rare.
I'm not sure we want to introduce a white-list or just bite the bullet
and audit all ->fault() implementations. But either works and isn't
terribly difficult, auditing all is more work though.
filemap_fault() is used as vma-vm_ops->fault() for most of the file
systems. Changing it can enable speculative fault support for all of
them. It will still exclude other driver based vma-vm_ops->fault()
implementation. AFAICS, __lock_page_or_retry() function can drop
mm->mmap_sem if the page could not be locked right away. As suggested
by Peterz, making it understand FAULT_FLAG_SPECULATIVE should be good
enough. The patch is lightly tested for file mappings on top of this
series.
Hi Anshuman,
This sounds pretty good, except for the FAULT_FLAG_RETRY_NOWAIT's case I
mentioned in another mail.
The next step would be to find a way to discriminate between the vm_fault()
functions. Any idea ?
Thanks,
Laurent.
@@ -1181,6 +1181,18 @@ int __lock_page_killable(struct page *__page)int__lock_page_or_retry(structpage*page,structmm_struct*mm,unsignedintflags){+if(flags&FAULT_FLAG_SPECULATIVE){+if(flags&FAULT_FLAG_KILLABLE){+intret;++ret=__lock_page_killable(page);+if(ret)+return0;+}else+__lock_page(page);+return1;+}+if(flags&FAULT_FLAG_ALLOW_RETRY){/**CAUTION!Inthiscase,mmap_semisnotreleased
Yeah, that looks right.
Hum, I'm wondering if FAULT_FLAG_RETRY_NOWAIT should be forced in the
speculative path in that case to match the semantics of
__lock_page_or_retry().
Doing that would force us to have another retry through classic fault
path wasting all the work done till now through SPF. Hence it may be
better to just wait, get the lock here and complete the fault. Peterz,
would you agree ? Or we should do as suggested by Laurent. More over,
forcing FAULT_FLAG_RETRY_NOWAIT on FAULT_FLAG_SPECULTIVE at this point
would look like a hack.
@@ -1181,6 +1181,18 @@ int __lock_page_killable(struct page *__page)int__lock_page_or_retry(structpage*page,structmm_struct*mm,unsignedintflags){+if(flags&FAULT_FLAG_SPECULATIVE){+if(flags&FAULT_FLAG_KILLABLE){+intret;++ret=__lock_page_killable(page);+if(ret)+return0;+}else+__lock_page(page);+return1;+}+if(flags&FAULT_FLAG_ALLOW_RETRY){/**CAUTION!Inthiscase,mmap_semisnotreleased
Yeah, that looks right.
Hum, I'm wondering if FAULT_FLAG_RETRY_NOWAIT should be forced in the
speculative path in that case to match the semantics of
__lock_page_or_retry().
Doing that would force us to have another retry through classic fault
path wasting all the work done till now through SPF. Hence it may be
better to just wait, get the lock here and complete the fault. Peterz,
would you agree ? Or we should do as suggested by Laurent. More over,
forcing FAULT_FLAG_RETRY_NOWAIT on FAULT_FLAG_SPECULTIVE at this point
would look like a hack.
Is there ever a situation where SPECULATIVE and NOWAIT are used
together? That seems like something to avoid.
A git-grep seems to suggest gup() can set it, but gup() will not be
doing speculative faults. s390 also sets it, but then again, they don't
have speculative fault support yet and when they do they can avoid
setting them together.
So maybe put in a WARN_ON_ONCE() on having both of them, it is not
something that makes sense to me, but maybe someone sees a rationale for
it?
This is a port on kernel 4.13 of the work done by Peter Zijlstra to
handle page fault without holding the mm semaphore [1].
The idea is to try to handle user space page faults without holding the
mmap_sem. This should allow better concurrency for massively threaded
process since the page fault handler will not wait for other threads memory
layout change to be done, assuming that this change is done in another part
of the process's memory space. This type page fault is named speculative
page fault. If the speculative page fault fails because of a concurrency is
detected or because underlying PMD or PTE tables are not yet allocating, it
is failing its processing and a classic page fault is then tried.
The speculative page fault (SPF) has to look for the VMA matching the fault
address without holding the mmap_sem, so the VMA list is now managed using
SRCU allowing lockless walking. The only impact would be the deferred file
derefencing in the case of a file mapping, since the file pointer is
released once the SRCU cleaning is done. This patch relies on the change
done recently by Paul McKenney in SRCU which now runs a callback per CPU
instead of per SRCU structure [1].
The VMA's attributes checked during the speculative page fault processing
have to be protected against parallel changes. This is done by using a per
VMA sequence lock. This sequence lock allows the speculative page fault
handler to fast check for parallel changes in progress and to abort the
speculative page fault in that case.
Once the VMA is found, the speculative page fault handler would check for
the VMA's attributes to verify that the page fault has to be handled
correctly or not. Thus the VMA is protected through a sequence lock which
allows fast detection of concurrent VMA changes. If such a change is
detected, the speculative page fault is aborted and a *classic* page fault
is tried. VMA sequence locks are added when VMA attributes which are
checked during the page fault are modified.
When the PTE is fetched, the VMA is checked to see if it has been changed,
so once the page table is locked, the VMA is valid, so any other changes
leading to touching this PTE will need to lock the page table, so no
parallel change is possible at this time.
Hi Sergey,
I can't see where such a chain could happen.
I tried to recreate it on top of the latest mm tree, to latest stack output
but I can't get it.
How did you raised this one ?
Thanks,
Laurent.
Hi Sergey,
I can't see where such a chain could happen.
I tried to recreate it on top of the latest mm tree, to latest stack output
but I can't get it.
How did you raised this one ?
Hi Laurent,
didn't do anything special, the box even wasn't under severe memory
pressure. can re-test your new patch set.
-ss
Hi Sergey,
I can't see where such a chain could happen.
I tried to recreate it on top of the latest mm tree, to latest stack output
but I can't get it.
How did you raised this one ?
Hi Laurent,
didn't do anything special, the box even wasn't under severe memory
pressure. can re-test your new patch set.
Hi Sergey,
I sent a v3 series, would you please give it a try ?
Thanks,
Laurent.