2018-02-16 19:15:02

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 00/24] Speculative page faults

This is a port on kernel 4.15 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, this is done by introducing a rwlock
which protects the access to the mm_rb tree. Previously this was done using
SRCU but it was introducing a lot of scheduling to process the VMA's freeing
operation which was hitting the performance by 20% as reported by Kemi Wang
[2].Using a rwlock to protect access to the mm_rb tree is limiting the
locking contention to these operations which are expected to be in a O(log n)
order. In addition to ensure that the VMA is not freed in our back a
reference count is added and 2 services (get_vma() and put_vma()) are
introduced to handle the reference count. When a VMA is fetch from the RB
tree using get_vma() is must be later freeed using put_vma(). Furthermore,
to allow the VMA to be used again by the classic page fault handler a
service is introduced can_reuse_spf_vma(). This service is expected to be
called with the mmap_sem hold. It checked that the VMA is still matching
the specified address and is releasing its reference count as the mmap_sem
is hold it is ensure that it will not be freed in our back. In general, the
VMA's reference count could be decremented when holding the mmap_sem but it
should not be increased as holding the mmap_sem is ensuring that the VMA is
stable. I can't see anymore the overhead I got while will-it-scale
benchmark anymore.

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 lockings 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.

The locking of the PTE is done with interrupts disabled, this allows to
check for the PMD to ensure that there is not an ongoing collapsing
operation. Since khugepaged is firstly set the PMD to pmd_none and then is
waiting for the other CPU to have catch the IPI interrupt, if the pmd is
valid at the time the PTE is locked, we have the guarantee that the
collapsing opertion will have to wait on the PTE lock to move foward. This
allows the SPF handler to map the PTE safely. If the PMD value is different
than the one recorded at the beginning of the SPF operation, the classic
page fault handler will be called to handle the operation while holding the
mmap_sem. As the PTE lock is done with the interrupts disabled, the lock is
done using spin_trylock() to avoid dead lock when handling a page fault
while a TLB invalidate is requested by an other CPU holding the PTE.

Support for THP is not done because when checking for the PMD, we can be
confused by an in progress collapsing operation done by khugepaged. The
issue is that pmd_none() could be true either if the PMD is not already
populated or if the underlying PTE are in the way to be collapsed. So we
cannot safely allocate a PMD if pmd_none() is true.

This series a new software performance event named 'speculative-faults' or
'spf'. It counts the number of successful page fault event handled in a
speculative way. When recording 'faults,spf' events, the faults one is
counting the total number of page fault events while 'spf' is only counting
the part of the faults processed in a speculative way.

There are some trace events introduced by this series. They allow to
identify why the page faults where not processed in a speculative way. This
doesn't take in account the faults generated by a monothreaded process
which directly processed while holding the mmap_sem. This trace events are
grouped in a system named 'pagefault', they are:
- pagefault:spf_pte_lock : if the pte was already locked by another thread
- pagefault:spf_vma_changed : if the VMA has been changed in our back
- pagefault:spf_vma_noanon : the vma->anon_vma field was not yet set.
- pagefault:spf_vma_notsup : the VMA's type is not supported
- pagefault:spf_vma_access : the VMA's access right are not respected
- pagefault:spf_pmd_changed : the upper PMD pointer has changed in our
back.

To record all the related events, the easier is to run perf with the
following arguments :
$ perf stat -e 'faults,spf,pagefault:*' <command>

This series builds on top of v4.15-mmotm-2018-02-06-16-41 and is
functional on x86 and PowerPC.

---------------------
Real Workload results

As mentioned in previous email, we did non official runs using a "popular
in memory multithreaded database product" on 176 cores SMT8 Power system
which showed a 30% improvements in the number of transaction processed per
second. This run has been done on the v6 series, but changes introduced in
this new verion should not impact the performance boost seen.

Here are the perf data captured during 2 of these runs :
vanilla spf
faults 89.418 101.364
spf n/a 97.989

With the SPF kernel, most of the page fault were processed in a speculative way.

------------------
Benchmarks results

Base kernel is 4.15-mmotm-2018-02-06-16-41
SPF is BASE + this series

Kernbench:
----------
Here are the results on a 16 CPUs X86 guest using kernbench on a 4.13-rc4
kernel (kernel is build 5 times):

Average Half load -j 8
Run (std deviation)
BASE SPF
Elapsed Time 147.376 (0.471413) 147.362 (0.562468) -0.01%
User Time 1006.99 (3.91733) 1006.41 (3.13905) -0.06%
System Time 114.612 (0.819433) 116.348 (1.05897) 1.51%
Percent CPU 760.6 (3.97492) 761.4 (4.15933) 0.11%
Context Switches 44241.6 (661.005) 44577.4 (642.631) 0.76%
Sleeps 85929.6 (461.657) 86141.4 (795.939) 0.25%

Average Optimal load -j 16
Run (std deviation)
BASE SPF
Elapsed Time 71.082 (0.374526) 71.374 (0.44264) 0.41%
User Time 937.35 (73.4597) 937.865 (72.2913) 0.05%
System Time 101.216 (14.1338) 102.902 (14.1995) 1.67%
Percent CPU 1052.1 (307.311) 1052 (306.364) -0.01%
Context Switches 63743.9 (20572.9) 64267.6 (20768.3) 0.82%
Sleeps 91479.1 (5870.58) 92038.3 (6253.44) 0.61%

During a run on the SPF, perf events were captured:
Performance counter stats for '../kernbench -M':
367932002 faults
227 spf
0 pagefault:spf_pte_lock
0 pagefault:spf_vma_changed
0 pagefault:spf_vma_noanon
2168 pagefault:spf_vma_notsup
0 pagefault:spf_vma_access
0 pagefault:spf_pmd_changed

Very few speculative page fault were recorded as most of the processes
involved are monothreaded (sounds that on this architecture some threads
were created during the kernel build processing).

Here are the kerbench results on a 80 CPUs Power8 system:

Average Half load -j 40
Run (std deviation)
BASE SPF
Elapsed Time 117.166 (0.254716) 117.068 (0.574952) -0.08%
User Time 4480.71 (20.0644) 4480.12 (11.0745) -0.01%
System Time 134.372 (0.31364) 136.66 (0.760164) 1.70%
Percent CPU 3938.2 (8.58487) 3943 (18.8547) 0.12%
Context Switches 92127.6 (171.901) 92288.8 (299.436) 0.17%
Sleeps 318695 (1307.96) 319125 (1278.35) 0.13%

Average Optimal load -j 80
Run (std deviation)
BASE SPF
Elapsed Time 107.816 (0.885257) 107.31 (0.639844) -0.47%
User Time 5871.04 (1465.62) 5876.96 (1472.44) 0.10%
System Time 157.682 (24.5729) 160.354 (24.9855) 1.69%
Percent CPU 5420.2 (1562.69) 5446.2 (1584.91) 0.48%
Context Switches 222110 (137020) 223067 (137867) 0.43%
Sleeps 333091 (15317.9) 334876 (17012.5) 0.54%

During a run on the SPF, perf events were captured:
Performance counter stats for '../kernbench -M':
116616867 faults
0 spf
0 pagefault:spf_pte_lock
1 pagefault:spf_vma_changed
0 pagefault:spf_vma_noanon
473 pagefault:spf_vma_notsup
0 pagefault:spf_vma_access
0 pagefault:spf_pmd_changed

Most of the processes involved are monothreaded so SPF is not activated but
there is no impact on the performance.

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. The
number is the record processes per second, the higher is the best.

BASE SPF delta
16 CPUs x86 VM 11412.55 62236.93 445.34%
80 CPUs P8 node 37659.4 80985.63 115.05%

Here are the performance counter read during a run on a 16 CPUs x86 VM:
Performance counter stats for './ebizzy -mRTp':
596987 faults
591798 spf
246 pagefault:spf_pte_lock
4033 pagefault:spf_vma_changed
0 pagefault:spf_vma_noanon
76 pagefault:spf_vma_notsup
0 pagefault:spf_vma_access
0 pagefault:spf_pmd_changed

And the ones captured during a run on a 80 CPUs Power node:
Performance counter stats for './ebizzy -mRTp':
706445 faults
697135 spf
6304 pagefault:spf_pte_lock
2595 pagefault:spf_vma_changed
0 pagefault:spf_vma_noanon
282 pagefault:spf_vma_notsup
0 pagefault:spf_vma_access
0 pagefault:spf_pmd_changed

In ebizzy's case most of the page fault were handled in a speculative way,
leading the ebizzy performance boost.

------------------
Changes since v7:
- move pte_map_lock() and pte_spinlock() upper in mm/memory.c (patch 4 & 5)
- make pte_unmap_same() compatible with the speculative page fault (patch 6)
Changes since v6:
- Rename config variable to CONFIG_SPECULATIVE_PAGE_FAULT (patch 1)
- Review the way the config variable is set (patch 1 to 3)
- Introduce mm_rb_write_*lock() in mm/mmap.c (patch 18)
- Merge patch introducing pte try locking in the patch 18.
Changes since v5:
- use rwlock agains the mm RB tree in place of SRCU
- add a VMA's reference count to protect VMA while using it without
holding the mmap_sem.
- check PMD value to detect collapsing operation
- don't try speculative page fault for mono threaded processes
- try to reuse the fetched VMA if VM_RETRY is returned
- go directly to the error path if an error is detected during the SPF
path
- fix race window when moving VMA in move_vma()
Changes since v4:
- As requested by Andrew Morton, use CONFIG_SPF and define it earlier in
the series to ease bisection.
Changes since v3:
- Don't build when CONFIG_SMP is not set
- Fixed a lock dependency warning in __vma_adjust()
- Use READ_ONCE to access p*d values in handle_speculative_fault()
- Call memcp_oom() service in handle_speculative_fault()
Changes since v2:
- Perf event is renamed in PERF_COUNT_SW_SPF
- On Power handle do_page_fault()'s cleaning
- On Power if the VM_FAULT_ERROR is returned by
handle_speculative_fault(), do not retry but jump to the error path
- If VMA's flags are not matching the fault, directly returns
VM_FAULT_SIGSEGV and not VM_FAULT_RETRY
- Check for pud_trans_huge() to avoid speculative path
- Handles _vm_normal_page()'s introduced by 6f16211df3bf
("mm/device-public-memory: device memory cache coherent with CPU")
- add and review few comments in the code
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 [5]
- 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://patchwork.kernel.org/patch/9999687/

Laurent Dufour (20):
mm: Introduce CONFIG_SPECULATIVE_PAGE_FAULT
x86/mm: Define CONFIG_SPECULATIVE_PAGE_FAULT
powerpc/mm: Define CONFIG_SPECULATIVE_PAGE_FAULT
mm: Introduce pte_spinlock for FAULT_FLAG_SPECULATIVE
mm: make pte_unmap_same compatible with SPF
mm: Protect VMA modifications using VMA sequence count
mm: protect mremap() against SPF hanlder
mm: Protect SPF handler against anon_vma changes
mm: Cache some VMA fields in the vm_fault structure
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: Protect mm_rb tree with a rwlock
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
mm: Speculative page fault handler return VMA
powerpc/mm: Add speculative page fault

Peter Zijlstra (4):
mm: Prepare for FAULT_FLAG_SPECULATIVE
mm: VMA sequence count
mm: Provide speculative fault infrastructure
x86/mm: Add speculative pagefault handling

arch/powerpc/Kconfig | 1 +
arch/powerpc/mm/fault.c | 31 +-
arch/x86/Kconfig | 1 +
arch/x86/mm/fault.c | 38 ++-
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 | 92 +++++-
include/linux/mm_types.h | 7 +
include/linux/pagemap.h | 4 +-
include/linux/rmap.h | 12 +-
include/linux/swap.h | 10 +-
include/trace/events/pagefault.h | 87 +++++
include/uapi/linux/perf_event.h | 1 +
kernel/fork.c | 3 +
mm/Kconfig | 3 +
mm/hugetlb.c | 2 +
mm/init-mm.c | 3 +
mm/internal.h | 20 ++
mm/khugepaged.c | 5 +
mm/madvise.c | 6 +-
mm/memory.c | 594 +++++++++++++++++++++++++++++-----
mm/mempolicy.c | 51 ++-
mm/migrate.c | 4 +-
mm/mlock.c | 13 +-
mm/mmap.c | 211 +++++++++---
mm/mprotect.c | 4 +-
mm/mremap.c | 13 +
mm/rmap.c | 5 +-
mm/swap.c | 6 +-
mm/swap_state.c | 8 +-
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 +
37 files changed, 1096 insertions(+), 175 deletions(-)
create mode 100644 include/trace/events/pagefault.h

--
2.7.4



2018-02-16 19:15:07

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 01/24] mm: Introduce CONFIG_SPECULATIVE_PAGE_FAULT

This configuration variable will be used to build the code needed to
handle speculative page fault.

By default it is turned off, and activated depending on architecture
support.

Suggested-by: Thomas Gleixner <[email protected]>
Signed-off-by: Laurent Dufour <[email protected]>
---
mm/Kconfig | 3 +++
1 file changed, 3 insertions(+)

diff --git a/mm/Kconfig b/mm/Kconfig
index c782e8fb7235..42be0d92336e 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -760,3 +760,6 @@ config GUP_BENCHMARK
performance of get_user_pages_fast().

See tools/testing/selftests/vm/gup_benchmark.c
+
+config SPECULATIVE_PAGE_FAULT
+ bool
--
2.7.4


2018-02-16 19:15:09

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 03/24] powerpc/mm: Define CONFIG_SPECULATIVE_PAGE_FAULT

Define CONFIG_SPECULATIVE_PAGE_FAULT for BOOK3S_64 and SMP. This enables
the Speculative Page Fault handler.

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 <[email protected]>
---
arch/powerpc/Kconfig | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 73ce5dd07642..acf2696a6505 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -233,6 +233,7 @@ config PPC
select OLD_SIGACTION if PPC32
select OLD_SIGSUSPEND
select SPARSE_IRQ
+ select SPECULATIVE_PAGE_FAULT if PPC_BOOK3S_64 && SMP
select SYSCTL_EXCEPTION_TRACE
select VIRT_TO_BUS if !PPC64
#
--
2.7.4


2018-02-16 19:15:17

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 07/24] mm: VMA sequence count

From: Peter Zijlstra <[email protected]>

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) <[email protected]>

[Port to 4.12 kernel]
[Build depends on CONFIG_SPECULATIVE_PAGE_FAULT]
[Introduce vm_write_* inline function depending on
CONFIG_SPECULATIVE_PAGE_FAULT]
[Fix lock dependency between mapping->i_mmap_rwsem and vma->vm_sequence by
using vm_raw_write* functions]
Signed-off-by: Laurent Dufour <[email protected]>
---
include/linux/mm.h | 41 +++++++++++++++++++++++++++++++++++++++++
include/linux/mm_types.h | 3 +++
mm/memory.c | 2 ++
mm/mmap.c | 35 +++++++++++++++++++++++++++++++++++
4 files changed, 81 insertions(+)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index e869adec9023..9c22b4134c5d 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1370,6 +1370,47 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
unmap_mapping_range(mapping, holebegin, holelen, 0);
}

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+static inline void vm_write_begin(struct vm_area_struct *vma)
+{
+ write_seqcount_begin(&vma->vm_sequence);
+}
+static inline void vm_write_begin_nested(struct vm_area_struct *vma,
+ int subclass)
+{
+ write_seqcount_begin_nested(&vma->vm_sequence, subclass);
+}
+static inline void vm_write_end(struct vm_area_struct *vma)
+{
+ write_seqcount_end(&vma->vm_sequence);
+}
+static inline void vm_raw_write_begin(struct vm_area_struct *vma)
+{
+ raw_write_seqcount_begin(&vma->vm_sequence);
+}
+static inline void vm_raw_write_end(struct vm_area_struct *vma)
+{
+ raw_write_seqcount_end(&vma->vm_sequence);
+}
+#else
+static inline void vm_write_begin(struct vm_area_struct *vma)
+{
+}
+static inline void vm_write_begin_nested(struct vm_area_struct *vma,
+ int subclass)
+{
+}
+static inline void vm_write_end(struct vm_area_struct *vma)
+{
+}
+static inline void vm_raw_write_begin(struct vm_area_struct *vma)
+{
+}
+static inline void vm_raw_write_end(struct vm_area_struct *vma)
+{
+}
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
+
extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
void *buf, int len, unsigned int gup_flags);
extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index fd1af6b9591d..34fde7111e88 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -333,6 +333,9 @@ struct vm_area_struct {
struct mempolicy *vm_policy; /* NUMA policy for the VMA */
#endif
struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ seqcount_t vm_sequence;
+#endif
} __randomize_layout;

struct core_thread {
diff --git a/mm/memory.c b/mm/memory.c
index a301b9003200..3cc1a13417cf 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1503,6 +1503,7 @@ void unmap_page_range(struct mmu_gather *tlb,
unsigned long next;

BUG_ON(addr >= end);
+ vm_write_begin(vma);
tlb_start_vma(tlb, vma);
pgd = pgd_offset(vma->vm_mm, addr);
do {
@@ -1512,6 +1513,7 @@ void unmap_page_range(struct mmu_gather *tlb,
next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
} while (pgd++, addr = next, addr != end);
tlb_end_vma(tlb, vma);
+ vm_write_end(vma);
}


diff --git a/mm/mmap.c b/mm/mmap.c
index 4bb038e7984b..671940b72696 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -558,6 +558,10 @@ void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
else
mm->highest_vm_end = vm_end_gap(vma);

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ seqcount_init(&vma->vm_sequence);
+#endif
+
/*
* vma->vm_prev wasn't known when we followed the rbtree to find the
* correct insertion point for that vma. As a result, we could not
@@ -692,6 +696,30 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
long adjust_next = 0;
int remove_next = 0;

+ /*
+ * Why using vm_raw_write*() functions here to avoid lockdep's warning ?
+ *
+ * Locked is complaining about a theoretical lock dependency, involving
+ * 3 locks:
+ * mapping->i_mmap_rwsem --> vma->vm_sequence --> fs_reclaim
+ *
+ * Here are the major path leading to this dependency :
+ * 1. __vma_adjust() mmap_sem -> vm_sequence -> i_mmap_rwsem
+ * 2. move_vmap() mmap_sem -> vm_sequence -> fs_reclaim
+ * 3. __alloc_pages_nodemask() fs_reclaim -> i_mmap_rwsem
+ * 4. unmap_mapping_range() i_mmap_rwsem -> vm_sequence
+ *
+ * So there is no way to solve this easily, especially because in
+ * unmap_mapping_range() the i_mmap_rwsem is grab while the impacted
+ * VMAs are not yet known.
+ * However, the way the vm_seq is used is guarantying that we will
+ * never block on it since we just check for its value and never wait
+ * for it to move, see vma_has_changed() and handle_speculative_fault().
+ */
+ vm_raw_write_begin(vma);
+ if (next)
+ vm_raw_write_begin(next);
+
if (next && !insert) {
struct vm_area_struct *exporter = NULL, *importer = NULL;

@@ -902,6 +930,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
anon_vma_merge(vma, next);
mm->map_count--;
mpol_put(vma_policy(next));
+ vm_raw_write_end(next);
kmem_cache_free(vm_area_cachep, next);
/*
* In mprotect's case 6 (see comments on vma_merge),
@@ -916,6 +945,8 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
* "vma->vm_next" gap must be updated.
*/
next = vma->vm_next;
+ if (next)
+ vm_raw_write_begin(next);
} else {
/*
* For the scope of the comment "next" and
@@ -962,6 +993,10 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
if (insert && file)
uprobe_mmap(insert);

+ if (next && next != vma)
+ vm_raw_write_end(next);
+ vm_raw_write_end(vma);
+
validate_mm(mm);

return 0;
--
2.7.4


2018-02-16 19:15:30

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 13/24] mm: Introduce __lru_cache_add_active_or_unevictable

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 <[email protected]>
---
include/linux/swap.h | 10 ++++++++--
mm/memory.c | 8 ++++----
mm/swap.c | 6 +++---
3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/include/linux/swap.h b/include/linux/swap.h
index a1a3f4ed94ce..99377b66ea93 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -337,8 +337,14 @@ extern void deactivate_file_page(struct page *page);
extern void mark_page_lazyfree(struct page *page);
extern void swap_setup(void);

-extern void lru_cache_add_active_or_unevictable(struct page *page,
- struct vm_area_struct *vma);
+extern void __lru_cache_add_active_or_unevictable(struct page *page,
+ unsigned long vma_flags);
+
+static inline void lru_cache_add_active_or_unevictable(struct page *page,
+ struct vm_area_struct *vma)
+{
+ return __lru_cache_add_active_or_unevictable(page, vma->vm_flags);
+}

/* linux/mm/vmscan.c */
extern unsigned long zone_reclaimable_pages(struct zone *zone);
diff --git a/mm/memory.c b/mm/memory.c
index bd5de1aa3f7a..bc14973b4eba 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2560,7 +2560,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);
/*
* We call the notify macro here because, when using secondary
* mmu page tables (such as kvm shadow page tables), we want the
@@ -3106,7 +3106,7 @@ int do_swap_page(struct vm_fault *vmf)
if (unlikely(page != swapcache && swapcache)) {
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);
} else {
do_page_add_anon_rmap(page, vma, vmf->address, exclusive);
mem_cgroup_commit_charge(page, memcg, true, false);
@@ -3257,7 +3257,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);

@@ -3509,7 +3509,7 @@ int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
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);
} else {
inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
page_add_file_rmap(page, false);
diff --git a/mm/swap.c b/mm/swap.c
index 2d337710218f..85fc6e78ca99 100644
--- a/mm/swap.c
+++ b/mm/swap.c
@@ -455,12 +455,12 @@ void lru_cache_add(struct page *page)
* directly back onto it's zone's unevictable list, it does NOT use a
* per cpu pagevec.
*/
-void lru_cache_add_active_or_unevictable(struct page *page,
- struct vm_area_struct *vma)
+void __lru_cache_add_active_or_unevictable(struct page *page,
+ unsigned long vma_flags)
{
VM_BUG_ON_PAGE(PageLRU(page), page);

- if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
+ if (likely((vma_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
SetPageActive(page);
else if (!TestSetPageMlocked(page)) {
/*
--
2.7.4


2018-02-16 19:15:30

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 15/24] mm: Introduce __vm_normal_page()

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 <[email protected]>
---
include/linux/mm.h | 7 +++++--
mm/memory.c | 18 ++++++++++--------
2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 49f160e56a0f..d5f17fcd2c32 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1261,8 +1261,11 @@ struct zap_details {
pgoff_t last_index; /* Highest page->index to unmap */
};

-struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
- pte_t pte, bool with_public_device);
+struct page *__vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
+ pte_t pte, bool with_public_device,
+ unsigned long vma_flags);
+#define _vm_normal_page(vma, addr, pte, with_public_device) \
+ __vm_normal_page(vma, addr, pte, with_public_device, (vma)->vm_flags)
#define vm_normal_page(vma, addr, pte) _vm_normal_page(vma, addr, pte, false)

struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
diff --git a/mm/memory.c b/mm/memory.c
index 8fd33d05ef1e..d2e441aaa96b 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -826,8 +826,9 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
#else
# define HAVE_PTE_SPECIAL 0
#endif
-struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
- pte_t pte, bool with_public_device)
+struct page *__vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
+ pte_t pte, bool with_public_device,
+ unsigned long vma_flags)
{
unsigned long pfn = pte_pfn(pte);

@@ -836,7 +837,7 @@ struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
goto check_pfn;
if (vma->vm_ops && vma->vm_ops->find_special_page)
return vma->vm_ops->find_special_page(vma, addr);
- if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
+ if (vma_flags & (VM_PFNMAP | VM_MIXEDMAP))
return NULL;
if (is_zero_pfn(pfn))
return NULL;
@@ -868,8 +869,8 @@ struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,

/* !HAVE_PTE_SPECIAL case follows: */

- if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
- if (vma->vm_flags & VM_MIXEDMAP) {
+ if (unlikely(vma_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
+ if (vma_flags & VM_MIXEDMAP) {
if (!pfn_valid(pfn))
return NULL;
goto out;
@@ -878,7 +879,7 @@ struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
off = (addr - vma->vm_start) >> PAGE_SHIFT;
if (pfn == vma->vm_pgoff + off)
return NULL;
- if (!is_cow_mapping(vma->vm_flags))
+ if (!is_cow_mapping(vma_flags))
return NULL;
}
}
@@ -2742,7 +2743,8 @@ static int do_wp_page(struct vm_fault *vmf)
{
struct vm_area_struct *vma = vmf->vma;

- vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
+ vmf->page = __vm_normal_page(vma, vmf->address, vmf->orig_pte, false,
+ vmf->vma_flags);
if (!vmf->page) {
/*
* VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
@@ -3862,7 +3864,7 @@ static int do_numa_page(struct vm_fault *vmf)
ptep_modify_prot_commit(vma->vm_mm, vmf->address, vmf->pte, pte);
update_mmu_cache(vma, vmf->address, vmf->pte);

- page = vm_normal_page(vma, vmf->address, pte);
+ page = __vm_normal_page(vma, vmf->address, pte, false, vmf->vma_flags);
if (!page) {
pte_unmap_unlock(vmf->pte, vmf->ptl);
return 0;
--
2.7.4


2018-02-16 19:15:39

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 05/24] mm: Introduce pte_spinlock for FAULT_FLAG_SPECULATIVE

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 <[email protected]>
---
mm/memory.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 42824b504434..1ca289f53dd6 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2288,6 +2288,13 @@ int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
}
EXPORT_SYMBOL_GPL(apply_to_page_range);

+static bool pte_spinlock(struct vm_fault *vmf)
+{
+ vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
+ spin_lock(vmf->ptl);
+ return true;
+}
+
static bool pte_map_lock(struct vm_fault *vmf)
{
vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
@@ -3815,8 +3822,8 @@ static int do_numa_page(struct vm_fault *vmf)
* validation through pte_unmap_same(). It's of NUMA type but
* the pfn may be screwed if the read is non atomic.
*/
- vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd);
- spin_lock(vmf->ptl);
+ if (!pte_spinlock(vmf))
+ return VM_FAULT_RETRY;
if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
pte_unmap_unlock(vmf->pte, vmf->ptl);
goto out;
@@ -4009,8 +4016,8 @@ static int handle_pte_fault(struct vm_fault *vmf)
if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
return do_numa_page(vmf);

- vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
- spin_lock(vmf->ptl);
+ if (!pte_spinlock(vmf))
+ return VM_FAULT_RETRY;
entry = vmf->orig_pte;
if (unlikely(!pte_same(*vmf->pte, entry)))
goto unlock;
--
2.7.4


2018-02-16 19:15:41

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 22/24] mm: Speculative page fault handler return VMA

When the speculative page fault handler is returning VM_RETRY, there is a
chance that VMA fetched without grabbing the mmap_sem can be reused by the
legacy page fault handler. By reusing it, we avoid calling find_vma()
again. To achieve, that we must ensure that the VMA structure will not be
freed in our back. This is done by getting the reference on it (get_vma())
and by assuming that the caller will call the new service
can_reuse_spf_vma() once it has grabbed the mmap_sem.

can_reuse_spf_vma() is first checking that the VMA is still in the RB tree
, and then that the VMA's boundaries matched the passed address and release
the reference on the VMA so that it can be freed if needed.

In the case the VMA is freed, can_reuse_spf_vma() will have returned false
as the VMA is no more in the RB tree.

Signed-off-by: Laurent Dufour <[email protected]>
---
include/linux/mm.h | 5 +-
mm/memory.c | 136 +++++++++++++++++++++++++++++++++--------------------
2 files changed, 88 insertions(+), 53 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index c383a4e2ceb3..0cd31a37bb3d 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1355,7 +1355,10 @@ extern int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
unsigned int flags);
#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
extern int handle_speculative_fault(struct mm_struct *mm,
- unsigned long address, unsigned int flags);
+ unsigned long address, unsigned int flags,
+ struct vm_area_struct **vma);
+extern bool can_reuse_spf_vma(struct vm_area_struct *vma,
+ unsigned long address);
#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
unsigned long address, unsigned int fault_flags,
diff --git a/mm/memory.c b/mm/memory.c
index 2ef686405154..1f5ce5ff79af 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4307,13 +4307,22 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
/* This is required by vm_normal_page() */
#error "Speculative page fault handler requires __HAVE_ARCH_PTE_SPECIAL"
#endif
-
/*
* vm_normal_page() adds some processing which should be done while
* hodling the mmap_sem.
*/
+
+/*
+ * Tries to handle the page fault in a speculative way, without grabbing the
+ * mmap_sem.
+ * When VM_FAULT_RETRY is returned, the vma pointer is valid and this vma must
+ * be checked later when the mmap_sem has been grabbed by calling
+ * can_reuse_spf_vma().
+ * This is needed as the returned vma is kept in memory until the call to
+ * can_reuse_spf_vma() is made.
+ */
int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
- unsigned int flags)
+ unsigned int flags, struct vm_area_struct **vma)
{
struct vm_fault vmf = {
.address = address,
@@ -4322,7 +4331,6 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
p4d_t *p4d, p4dval;
pud_t pudval;
int seq, ret = VM_FAULT_RETRY;
- struct vm_area_struct *vma;
#ifdef CONFIG_NUMA
struct mempolicy *pol;
#endif
@@ -4331,14 +4339,16 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
flags |= FAULT_FLAG_SPECULATIVE;

- vma = get_vma(mm, address);
- if (!vma)
+ *vma = get_vma(mm, address);
+ if (!*vma)
return ret;
+ vmf.vma = *vma;

- seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
+ /* rmb <-> seqlock,vma_rb_erase() */
+ seq = raw_read_seqcount(&vmf.vma->vm_sequence);
if (seq & 1) {
- trace_spf_vma_changed(_RET_IP_, vma, address);
- goto out_put;
+ trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
+ return ret;
}

/*
@@ -4346,9 +4356,9 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
* with the VMA.
* This include huge page from hugetlbfs.
*/
- if (vma->vm_ops) {
- trace_spf_vma_notsup(_RET_IP_, vma, address);
- goto out_put;
+ if (vmf.vma->vm_ops) {
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
+ return ret;
}

/*
@@ -4356,18 +4366,18 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
* because vm_next and vm_prev must be safe. This can't be guaranteed
* in the speculative path.
*/
- if (unlikely(!vma->anon_vma)) {
- trace_spf_vma_notsup(_RET_IP_, vma, address);
- goto out_put;
+ if (unlikely(!vmf.vma->anon_vma)) {
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
+ return ret;
}

- vmf.vma_flags = READ_ONCE(vma->vm_flags);
- vmf.vma_page_prot = READ_ONCE(vma->vm_page_prot);
+ vmf.vma_flags = READ_ONCE(vmf.vma->vm_flags);
+ vmf.vma_page_prot = READ_ONCE(vmf.vma->vm_page_prot);

/* Can't call userland page fault handler in the speculative path */
if (unlikely(vmf.vma_flags & VM_UFFD_MISSING)) {
- trace_spf_vma_notsup(_RET_IP_, vma, address);
- goto out_put;
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
+ return ret;
}

if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP) {
@@ -4376,48 +4386,39 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
* boundaries but we want to trace it as not supported instead
* of changed.
*/
- trace_spf_vma_notsup(_RET_IP_, vma, address);
- goto out_put;
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
+ return ret;
}

- if (address < READ_ONCE(vma->vm_start)
- || READ_ONCE(vma->vm_end) <= address) {
- trace_spf_vma_changed(_RET_IP_, vma, address);
- goto out_put;
+ if (address < READ_ONCE(vmf.vma->vm_start)
+ || READ_ONCE(vmf.vma->vm_end) <= address) {
+ trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
+ return ret;
}

- if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
+ if (!arch_vma_access_permitted(vmf.vma, flags & FAULT_FLAG_WRITE,
flags & FAULT_FLAG_INSTRUCTION,
- flags & FAULT_FLAG_REMOTE)) {
- trace_spf_vma_access(_RET_IP_, vma, address);
- ret = VM_FAULT_SIGSEGV;
- goto out_put;
- }
+ flags & FAULT_FLAG_REMOTE))
+ goto out_segv;

/* 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))) {
- trace_spf_vma_access(_RET_IP_, vma, address);
- ret = VM_FAULT_SIGSEGV;
- goto out_put;
- }
- } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE)))) {
- trace_spf_vma_access(_RET_IP_, vma, address);
- ret = VM_FAULT_SIGSEGV;
- goto out_put;
- }
+ if (unlikely(!(vmf.vma_flags & VM_WRITE)))
+ goto out_segv;
+ } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE))))
+ goto out_segv;

#ifdef CONFIG_NUMA
/*
* MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
* are not compatible with the speculative page fault processing.
*/
- pol = __get_vma_policy(vma, address);
+ pol = __get_vma_policy(vmf.vma, address);
if (!pol)
pol = get_task_policy(current);
if (pol && pol->mode == MPOL_INTERLEAVE) {
- trace_spf_vma_notsup(_RET_IP_, vma, address);
- goto out_put;
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
+ return ret;
}
#endif

@@ -4479,9 +4480,8 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
vmf.pte = NULL;
}

- vmf.vma = vma;
- vmf.pgoff = linear_page_index(vma, address);
- vmf.gfp_mask = __get_fault_gfp_mask(vma);
+ vmf.pgoff = linear_page_index(vmf.vma, address);
+ vmf.gfp_mask = __get_fault_gfp_mask(vmf.vma);
vmf.sequence = seq;
vmf.flags = flags;

@@ -4491,16 +4491,22 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
* 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)) {
- trace_spf_vma_changed(_RET_IP_, vma, address);
- goto out_put;
+ if (read_seqcount_retry(&vmf.vma->vm_sequence, seq)) {
+ trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
+ return ret;
}

mem_cgroup_oom_enable();
ret = handle_pte_fault(&vmf);
mem_cgroup_oom_disable();

- put_vma(vma);
+ /*
+ * If there is no need to retry, don't return the vma to the caller.
+ */
+ if (!(ret & VM_FAULT_RETRY)) {
+ put_vma(vmf.vma);
+ *vma = NULL;
+ }

/*
* The task may have entered a memcg OOM situation but
@@ -4513,9 +4519,35 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
return ret;

out_walk:
- trace_spf_vma_notsup(_RET_IP_, vma, address);
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
local_irq_enable();
-out_put:
+ return ret;
+
+out_segv:
+ trace_spf_vma_access(_RET_IP_, vmf.vma, address);
+ /*
+ * We don't return VM_FAULT_RETRY so the caller is not expected to
+ * retrieve the fetched VMA.
+ */
+ put_vma(vmf.vma);
+ *vma = NULL;
+ return VM_FAULT_SIGSEGV;
+}
+
+/*
+ * This is used to know if the vma fetch in the speculative page fault handler
+ * is still valid when trying the regular fault path while holding the
+ * mmap_sem.
+ * The call to put_vma(vma) must be made after checking the vma's fields, as
+ * the vma may be freed by put_vma(). In such a case it is expected that false
+ * is returned.
+ */
+bool can_reuse_spf_vma(struct vm_area_struct *vma, unsigned long address)
+{
+ bool ret;
+
+ ret = !RB_EMPTY_NODE(&vma->vm_rb) &&
+ vma->vm_start <= address && address < vma->vm_end;
put_vma(vma);
return ret;
}
--
2.7.4


2018-02-16 19:15:43

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 23/24] x86/mm: Add speculative pagefault handling

From: Peter Zijlstra <[email protected]>

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) <[email protected]>

[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]
[Don't build SPF call if !CONFIG_SPECULATIVE_PAGE_FAULT]
[Try speculative fault path only for multi threaded processes]
[Try to the VMA fetch during the speculative path in case of retry]
Signed-off-by: Laurent Dufour <[email protected]>
---
arch/x86/mm/fault.c | 38 +++++++++++++++++++++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index 800de815519c..d9f9236ccb9a 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -1239,6 +1239,9 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
unsigned long address)
{
struct vm_area_struct *vma;
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ struct vm_area_struct *spf_vma = NULL;
+#endif
struct task_struct *tsk;
struct mm_struct *mm;
int fault, major = 0;
@@ -1336,6 +1339,27 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
if (error_code & X86_PF_INSTR)
flags |= FAULT_FLAG_INSTRUCTION;

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ if ((error_code & X86_PF_USER) && (atomic_read(&mm->mm_users) > 1)) {
+ fault = handle_speculative_fault(mm, address, flags,
+ &spf_vma);
+
+ if (!(fault & VM_FAULT_RETRY)) {
+ if (!(fault & VM_FAULT_ERROR)) {
+ perf_sw_event(PERF_COUNT_SW_SPF, 1,
+ regs, address);
+ goto done;
+ }
+ /*
+ * In case of error we need the pkey value, but
+ * can't get it from the spf_vma as it is only returned
+ * when VM_FAULT_RETRY is returned. So we have to
+ * retry the page fault with the mmap_sem grabbed.
+ */
+ }
+ }
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
+
/*
* When running in the kernel we expect faults to occur only to
* addresses in user space. All other faults represent errors in
@@ -1369,7 +1393,16 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
might_sleep();
}

- vma = find_vma(mm, address);
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ if (spf_vma) {
+ if (can_reuse_spf_vma(spf_vma, address))
+ vma = spf_vma;
+ else
+ vma = find_vma(mm, address);
+ spf_vma = NULL;
+ } else
+#endif
+ vma = find_vma(mm, address);
if (unlikely(!vma)) {
bad_area(regs, error_code, address);
return;
@@ -1455,6 +1488,9 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
return;
}

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+done:
+#endif
/*
* Major/minor page fault accounting. If any of the events
* returned VM_FAULT_MAJOR, we account it as a major fault.
--
2.7.4


2018-02-16 19:15:44

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 04/24] mm: Prepare for FAULT_FLAG_SPECULATIVE

From: Peter Zijlstra <[email protected]>

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) <[email protected]>

[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]
[move pte_map_lock()'s definition upper in the file]
Signed-off-by: Laurent Dufour <[email protected]>
---
include/linux/mm.h | 1 +
mm/memory.c | 56 ++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 47c06fd20f6a..51d950cac772 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -302,6 +302,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" }, \
diff --git a/mm/memory.c b/mm/memory.c
index 5ec6433d6a5c..42824b504434 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2288,6 +2288,13 @@ int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
}
EXPORT_SYMBOL_GPL(apply_to_page_range);

+static bool pte_map_lock(struct vm_fault *vmf)
+{
+ vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
+ vmf->address, &vmf->ptl);
+ return true;
+}
+
/*
* handle_pte_fault chooses page fault handler according to an entry which was
* read non-atomically. Before making any commitment, on those architectures
@@ -2477,6 +2484,7 @@ static int wp_page_copy(struct vm_fault *vmf)
const unsigned long mmun_start = vmf->address & PAGE_MASK;
const unsigned long mmun_end = mmun_start + PAGE_SIZE;
struct mem_cgroup *memcg;
+ int ret = VM_FAULT_OOM;

if (unlikely(anon_vma_prepare(vma)))
goto oom;
@@ -2504,7 +2512,11 @@ static int wp_page_copy(struct vm_fault *vmf)
/*
* Re-check the pte - we dropped the lock
*/
- vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
+ if (!pte_map_lock(vmf)) {
+ mem_cgroup_cancel_charge(new_page, memcg, false);
+ ret = VM_FAULT_RETRY;
+ goto oom_free_new;
+ }
if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
if (old_page) {
if (!PageAnon(old_page)) {
@@ -2596,7 +2608,7 @@ static int wp_page_copy(struct vm_fault *vmf)
oom:
if (old_page)
put_page(old_page);
- return VM_FAULT_OOM;
+ return ret;
}

/**
@@ -2617,8 +2629,8 @@ static int wp_page_copy(struct vm_fault *vmf)
int finish_mkwrite_fault(struct vm_fault *vmf)
{
WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
- vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
- &vmf->ptl);
+ if (!pte_map_lock(vmf))
+ return VM_FAULT_RETRY;
/*
* We might have raced with another page fault while we released the
* pte_offset_map_lock.
@@ -2736,8 +2748,11 @@ static int do_wp_page(struct vm_fault *vmf)
get_page(vmf->page);
pte_unmap_unlock(vmf->pte, vmf->ptl);
lock_page(vmf->page);
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
- vmf->address, &vmf->ptl);
+ if (!pte_map_lock(vmf)) {
+ unlock_page(vmf->page);
+ put_page(vmf->page);
+ return VM_FAULT_RETRY;
+ }
if (!pte_same(*vmf->pte, vmf->orig_pte)) {
unlock_page(vmf->page);
pte_unmap_unlock(vmf->pte, vmf->ptl);
@@ -2963,8 +2978,10 @@ int do_swap_page(struct vm_fault *vmf)
* Back out if somebody else faulted in this pte
* while we released the pte lock.
*/
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
- vmf->address, &vmf->ptl);
+ if (!pte_map_lock(vmf)) {
+ delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
+ return VM_FAULT_RETRY;
+ }
if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
ret = VM_FAULT_OOM;
delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
@@ -3020,8 +3037,11 @@ int do_swap_page(struct vm_fault *vmf)
/*
* Back out if somebody else already faulted in this pte.
*/
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
- &vmf->ptl);
+ if (!pte_map_lock(vmf)) {
+ ret = VM_FAULT_RETRY;
+ mem_cgroup_cancel_charge(page, memcg, false);
+ goto out_page;
+ }
if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
goto out_nomap;

@@ -3150,8 +3170,8 @@ static int do_anonymous_page(struct vm_fault *vmf)
!mm_forbids_zeropage(vma->vm_mm)) {
entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
vma->vm_page_prot));
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
- vmf->address, &vmf->ptl);
+ if (!pte_map_lock(vmf))
+ return VM_FAULT_RETRY;
if (!pte_none(*vmf->pte))
goto unlock;
ret = check_stable_address_space(vma->vm_mm);
@@ -3186,8 +3206,11 @@ static int do_anonymous_page(struct vm_fault *vmf)
if (vma->vm_flags & VM_WRITE)
entry = pte_mkwrite(pte_mkdirty(entry));

- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
- &vmf->ptl);
+ if (!pte_map_lock(vmf)) {
+ mem_cgroup_cancel_charge(page, memcg, false);
+ put_page(page);
+ return VM_FAULT_RETRY;
+ }
if (!pte_none(*vmf->pte))
goto release;

@@ -3311,8 +3334,9 @@ static int pte_alloc_one_map(struct vm_fault *vmf)
* pte_none() under vmf->ptl protection when we return to
* alloc_set_pte().
*/
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
- &vmf->ptl);
+ if (!pte_map_lock(vmf))
+ return VM_FAULT_RETRY;
+
return 0;
}

--
2.7.4


2018-02-16 19:15:46

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 20/24] perf: Add a speculative page fault sw event

Add a new software event to count succeeded speculative page faults.

Signed-off-by: Laurent Dufour <[email protected]>
---
include/uapi/linux/perf_event.h | 1 +
1 file changed, 1 insertion(+)

diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index e0739a1aa4b2..164383273147 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -112,6 +112,7 @@ enum perf_sw_ids {
PERF_COUNT_SW_EMULATION_FAULTS = 8,
PERF_COUNT_SW_DUMMY = 9,
PERF_COUNT_SW_BPF_OUTPUT = 10,
+ PERF_COUNT_SW_SPF = 11,

PERF_COUNT_SW_MAX, /* non-ABI */
};
--
2.7.4


2018-02-16 19:15:54

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 12/24] mm/migrate: Pass vm_fault pointer to migrate_misplaced_page()

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 <[email protected]>
---
include/linux/migrate.h | 4 ++--
mm/memory.c | 2 +-
mm/migrate.c | 4 ++--
3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/include/linux/migrate.h b/include/linux/migrate.h
index 0c6fe904bc97..08960ec74246 100644
--- a/include/linux/migrate.h
+++ b/include/linux/migrate.h
@@ -126,14 +126,14 @@ static inline void __ClearPageMovable(struct page *page)
#ifdef CONFIG_NUMA_BALANCING
extern bool pmd_trans_migrating(pmd_t pmd);
extern int migrate_misplaced_page(struct page *page,
- struct vm_area_struct *vma, int node);
+ struct vm_fault *vmf, int node);
#else
static inline bool pmd_trans_migrating(pmd_t pmd)
{
return false;
}
static inline int migrate_misplaced_page(struct page *page,
- struct vm_area_struct *vma, int node)
+ struct vm_fault *vmf, int node)
{
return -EAGAIN; /* can't migrate now */
}
diff --git a/mm/memory.c b/mm/memory.c
index 66a79f44f303..bd5de1aa3f7a 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3903,7 +3903,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;
diff --git a/mm/migrate.c b/mm/migrate.c
index 5d0dc7b85f90..ad8692ca6a4f 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1900,7 +1900,7 @@ bool pmd_trans_migrating(pmd_t pmd)
* node. Caller is expected to have an elevated reference count on
* the page that will be dropped by this function before returning.
*/
-int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
+int migrate_misplaced_page(struct page *page, struct vm_fault *vmf,
int node)
{
pg_data_t *pgdat = NODE_DATA(node);
@@ -1913,7 +1913,7 @@ int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
* with execute permissions as they are probably shared libraries.
*/
if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
- (vma->vm_flags & VM_EXEC))
+ (vmf->vma_flags & VM_EXEC))
goto out;

/*
--
2.7.4


2018-02-16 19:15:57

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 18/24] mm: Provide speculative fault infrastructure

From: Peter Zijlstra <[email protected]>

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) <[email protected]>

[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]
[Fetch p4d and pud]
[Set vmd.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()]
[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()]
[Don't build SPF services if !CONFIG_SPECULATIVE_PAGE_FAULT]
[Add mem cgroup oom check]
[Use READ_ONCE to access p*d entries]
[Replace deprecated ACCESS_ONCE() by READ_ONCE() in vma_has_changed()]
[Don't fetch pte again in handle_pte_fault() when running the speculative
path]
[Check PMD against concurrent collapsing operation]
[Try spin lock the pte during the speculative path to avoid deadlock with
other CPU's invalidating the TLB and requiring this CPU to catch the
inter processor's interrupt]
Signed-off-by: Laurent Dufour <[email protected]>
---
include/linux/hugetlb_inline.h | 2 +-
include/linux/mm.h | 8 +
include/linux/pagemap.h | 4 +-
mm/internal.h | 16 +-
mm/memory.c | 334 ++++++++++++++++++++++++++++++++++++++++-
5 files changed, 356 insertions(+), 8 deletions(-)

diff --git a/include/linux/hugetlb_inline.h b/include/linux/hugetlb_inline.h
index 0660a03d37d9..9e25283d6fc9 100644
--- a/include/linux/hugetlb_inline.h
+++ b/include/linux/hugetlb_inline.h
@@ -8,7 +8,7 @@

static inline bool is_vm_hugetlb_page(struct vm_area_struct *vma)
{
- return !!(vma->vm_flags & VM_HUGETLB);
+ return !!(READ_ONCE(vma->vm_flags) & VM_HUGETLB);
}

#else
diff --git a/include/linux/mm.h b/include/linux/mm.h
index d5f17fcd2c32..c383a4e2ceb3 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -331,6 +331,10 @@ struct vm_fault {
gfp_t gfp_mask; /* gfp mask to be used for allocations */
pgoff_t pgoff; /* Logical page offset based on vma */
unsigned long address; /* Faulting virtual address */
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ unsigned int sequence;
+ pmd_t orig_pmd; /* value of PMD at the time of fault */
+#endif
pmd_t *pmd; /* Pointer to pmd entry matching
* the 'address' */
pud_t *pud; /* Pointer to pud entry matching
@@ -1349,6 +1353,10 @@ int invalidate_inode_page(struct page *page);
#ifdef CONFIG_MMU
extern int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
unsigned int flags);
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+extern int handle_speculative_fault(struct mm_struct *mm,
+ unsigned long address, unsigned int flags);
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
unsigned long address, unsigned int fault_flags,
bool *unlocked);
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 34ce3ebf97d5..70e4d2688e7b 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -456,8 +456,8 @@ static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
pgoff_t pgoff;
if (unlikely(is_vm_hugetlb_page(vma)))
return linear_hugepage_index(vma, address);
- pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
- pgoff += vma->vm_pgoff;
+ pgoff = (address - READ_ONCE(vma->vm_start)) >> PAGE_SHIFT;
+ pgoff += READ_ONCE(vma->vm_pgoff);
return pgoff;
}

diff --git a/mm/internal.h b/mm/internal.h
index fb2667b20f0a..10b188c87fa4 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -44,7 +44,21 @@ int do_swap_page(struct vm_fault *vmf);
extern struct vm_area_struct *get_vma(struct mm_struct *mm,
unsigned long addr);
extern void put_vma(struct vm_area_struct *vma);
-#endif
+
+static inline bool vma_has_changed(struct vm_fault *vmf)
+{
+ int ret = RB_EMPTY_NODE(&vmf->vma->vm_rb);
+ unsigned int seq = READ_ONCE(vmf->vma->vm_sequence.sequence);
+
+ /*
+ * Matches both the wmb in write_seqlock_{begin,end}() and
+ * the wmb in vma_rb_erase().
+ */
+ smp_rmb();
+
+ return ret || seq != vmf->sequence;
+}
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */

void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
unsigned long floor, unsigned long ceiling);
diff --git a/mm/memory.c b/mm/memory.c
index 67de327c7262..efc32173264e 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -769,7 +769,8 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
if (page)
dump_page(page, "bad pte");
pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
- (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
+ (void *)addr, READ_ONCE(vma->vm_flags), vma->anon_vma,
+ mapping, index);
pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
vma->vm_file,
vma->vm_ops ? vma->vm_ops->fault : NULL,
@@ -2295,19 +2296,119 @@ int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
}
EXPORT_SYMBOL_GPL(apply_to_page_range);

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
static bool pte_spinlock(struct vm_fault *vmf)
{
+ bool ret = false;
+ pmd_t pmdval;
+
+ /* 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);
+ return true;
+ }
+
+ local_irq_disable();
+ if (vma_has_changed(vmf))
+ goto out;
+
+ /*
+ * We check if the pmd value is still the same to ensure that there
+ * is a huge collapse operation in progress in our back.
+ */
+ pmdval = READ_ONCE(*vmf->pmd);
+ if (!pmd_same(pmdval, vmf->orig_pmd))
+ goto out;
+
+ vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
+ if (unlikely(!spin_trylock(vmf->ptl)))
+ goto out;
+
+ if (vma_has_changed(vmf)) {
+ spin_unlock(vmf->ptl);
+ goto out;
+ }
+
+ ret = true;
+out:
+ local_irq_enable();
+ return ret;
+}
+
+static bool pte_map_lock(struct vm_fault *vmf)
+{
+ bool ret = false;
+ pte_t *pte;
+ pmd_t pmdval;
+ 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);
+ return true;
+ }
+
+ /*
+ * The first vma_has_changed() guarantees the page-tables are still
+ * valid, having IRQs disabled ensures they stay around, hence the
+ * second vma_has_changed() to make sure they are still valid once
+ * we've got the lock. After that a concurrent zap_pte_range() will
+ * block on the PTL and thus we're safe.
+ */
+ local_irq_disable();
+ if (vma_has_changed(vmf))
+ goto out;
+
+ /*
+ * We check if the pmd value is still the same to ensure that there
+ * is a huge collapse operation in progress in our back.
+ */
+ pmdval = READ_ONCE(*vmf->pmd);
+ if (!pmd_same(pmdval, vmf->orig_pmd))
+ goto out;
+
+ /*
+ * Same as pte_offset_map_lock() except that we call
+ * spin_trylock() in place of spin_lock() to avoid race with
+ * unmap path which may have the lock and wait for this CPU
+ * to invalidate TLB but this CPU has irq disabled.
+ * Since we are in a speculative patch, accept it could fail
+ */
+ ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
+ pte = pte_offset_map(vmf->pmd, vmf->address);
+ if (unlikely(!spin_trylock(ptl))) {
+ pte_unmap(pte);
+ goto out;
+ }
+
+ if (vma_has_changed(vmf)) {
+ pte_unmap_unlock(pte, ptl);
+ goto out;
+ }
+
+ vmf->pte = pte;
+ vmf->ptl = ptl;
+ ret = true;
+out:
+ local_irq_enable();
+ return ret;
+}
+#else
+static inline bool pte_spinlock(struct vm_fault *vmf)
+{
vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
spin_lock(vmf->ptl);
return true;
}

-static bool pte_map_lock(struct vm_fault *vmf)
+static inline bool pte_map_lock(struct vm_fault *vmf)
{
vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
vmf->address, &vmf->ptl);
return true;
}
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */

/*
* handle_pte_fault chooses page fault handler according to an entry which was
@@ -3207,6 +3308,14 @@ static int do_anonymous_page(struct vm_fault *vmf)
ret = check_stable_address_space(vma->vm_mm);
if (ret)
goto unlock;
+ /*
+ * Don't call the userfaultfd during the speculative path.
+ * We already checked for the VMA to not be managed through
+ * userfaultfd, but it may be set in our back once we have lock
+ * the pte. In such a case we can ignore it this time.
+ */
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE)
+ goto setpte;
/* Deliver the page fault to userland, check inside PT lock */
if (userfaultfd_missing(vma)) {
pte_unmap_unlock(vmf->pte, vmf->ptl);
@@ -3249,7 +3358,7 @@ static int do_anonymous_page(struct vm_fault *vmf)
goto release;

/* 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);
@@ -3992,13 +4101,22 @@ static int handle_pte_fault(struct vm_fault *vmf)

if (unlikely(pmd_none(*vmf->pmd))) {
/*
+ * In the case of the speculative page fault handler we abort
+ * the speculative path immediately as the pmd is probably
+ * in the way to be converted in a huge one. We will try
+ * again holding the mmap_sem (which implies that the collapse
+ * operation is done).
+ */
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE)
+ return VM_FAULT_RETRY;
+ /*
* Leave __pte_alloc() until later: because vm_ops->fault may
* want to allocate huge page, and if we expose page table
* for an instant, it will be difficult to retract from
* concurrent faults and from rmap lookups.
*/
vmf->pte = NULL;
- } else {
+ } else if (!(vmf->flags & FAULT_FLAG_SPECULATIVE)) {
/* See comment in pte_alloc_one_map() */
if (pmd_devmap_trans_unstable(vmf->pmd))
return 0;
@@ -4007,6 +4125,9 @@ static int handle_pte_fault(struct vm_fault *vmf)
* pmd from under us anymore at this point because we hold the
* mmap_sem read mode and khugepaged takes it in write mode.
* So now it's safe to run pte_offset_map().
+ * This is not applicable to the speculative page fault handler
+ * but in that case, the pte is fetched earlier in
+ * handle_speculative_fault().
*/
vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
vmf->orig_pte = *vmf->pte;
@@ -4029,6 +4150,8 @@ static int handle_pte_fault(struct vm_fault *vmf)
if (!vmf->pte) {
if (vma_is_anonymous(vmf->vma))
return do_anonymous_page(vmf);
+ else if (vmf->flags & FAULT_FLAG_SPECULATIVE)
+ return VM_FAULT_RETRY;
else
return do_fault(vmf);
}
@@ -4126,6 +4249,9 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
vmf.pmd = pmd_alloc(mm, vmf.pud, address);
if (!vmf.pmd)
return VM_FAULT_OOM;
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ vmf.sequence = raw_read_seqcount(&vma->vm_sequence);
+#endif
if (pmd_none(*vmf.pmd) && transparent_hugepage_enabled(vma)) {
ret = create_huge_pmd(&vmf);
if (!(ret & VM_FAULT_FALLBACK))
@@ -4159,6 +4285,206 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
return handle_pte_fault(&vmf);
}

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+
+#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() 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, pgdval;
+ p4d_t *p4d, p4dval;
+ pud_t pudval;
+ int seq, ret = VM_FAULT_RETRY;
+ struct vm_area_struct *vma;
+#ifdef CONFIG_NUMA
+ struct mempolicy *pol;
+#endif
+
+ /* Clear flags that may lead to release the mmap_sem to retry */
+ flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
+ flags |= FAULT_FLAG_SPECULATIVE;
+
+ vma = get_vma(mm, address);
+ if (!vma)
+ return ret;
+
+ seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
+ if (seq & 1)
+ goto out_put;
+
+ /*
+ * 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 out_put;
+
+ /*
+ * __anon_vma_prepare() requires the mmap_sem to be held
+ * because vm_next and vm_prev must be safe. This can't be guaranteed
+ * in the speculative path.
+ */
+ if (unlikely(!vma->anon_vma))
+ goto out_put;
+
+ 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 out_put;
+
+ 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 out_put;
+
+ if (address < READ_ONCE(vma->vm_start)
+ || READ_ONCE(vma->vm_end) <= address)
+ goto out_put;
+
+ if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
+ flags & FAULT_FLAG_INSTRUCTION,
+ flags & FAULT_FLAG_REMOTE)) {
+ ret = VM_FAULT_SIGSEGV;
+ goto out_put;
+ }
+
+ /* 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))) {
+ ret = VM_FAULT_SIGSEGV;
+ goto out_put;
+ }
+ } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE)))) {
+ ret = VM_FAULT_SIGSEGV;
+ goto out_put;
+ }
+
+#ifdef CONFIG_NUMA
+ /*
+ * 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 out_put;
+#endif
+
+ /*
+ * Do a speculative lookup of the PTE entry.
+ */
+ local_irq_disable();
+ pgd = pgd_offset(mm, address);
+ pgdval = READ_ONCE(*pgd);
+ if (pgd_none(pgdval) || unlikely(pgd_bad(pgdval)))
+ goto out_walk;
+
+ p4d = p4d_offset(pgd, address);
+ p4dval = READ_ONCE(*p4d);
+ if (p4d_none(p4dval) || unlikely(p4d_bad(p4dval)))
+ goto out_walk;
+
+ vmf.pud = pud_offset(p4d, address);
+ pudval = READ_ONCE(*vmf.pud);
+ if (pud_none(pudval) || unlikely(pud_bad(pudval)))
+ goto out_walk;
+
+ /* Huge pages at PUD level are not supported. */
+ if (unlikely(pud_trans_huge(pudval)))
+ goto out_walk;
+
+ vmf.pmd = pmd_offset(vmf.pud, address);
+ vmf.orig_pmd = READ_ONCE(*vmf.pmd);
+ /*
+ * pmd_none could mean that a hugepage collapse is in progress
+ * in our back as collapse_huge_page() mark it before
+ * invalidating the pte (which is done once the IPI is catched
+ * by all CPU and we have interrupt disabled).
+ * For this reason we cannot handle THP in a speculative way since we
+ * can't safely indentify an in progress collapse operation done in our
+ * back on that PMD.
+ * Regarding the order of the following checks, see comment in
+ * pmd_devmap_trans_unstable()
+ */
+ if (unlikely(pmd_devmap(vmf.orig_pmd) ||
+ pmd_none(vmf.orig_pmd) || pmd_trans_huge(vmf.orig_pmd) ||
+ is_swap_pmd(vmf.orig_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.
+ */
+
+ vmf.pte = pte_offset_map(vmf.pmd, address);
+ vmf.orig_pte = READ_ONCE(*vmf.pte);
+ barrier(); /* See comment in handle_pte_fault() */
+ if (pte_none(vmf.orig_pte)) {
+ pte_unmap(vmf.pte);
+ vmf.pte = NULL;
+ }
+
+ vmf.vma = vma;
+ 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 out_put;
+
+ mem_cgroup_oom_enable();
+ ret = handle_pte_fault(&vmf);
+ mem_cgroup_oom_disable();
+
+ put_vma(vma);
+
+ /*
+ * The task may have entered a memcg OOM situation but
+ * if the allocation error was handled gracefully (no
+ * VM_FAULT_OOM), there is no need to kill anything.
+ * Just clean up the OOM state peacefully.
+ */
+ if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
+ mem_cgroup_oom_synchronize(false);
+ return ret;
+
+out_walk:
+ local_irq_enable();
+out_put:
+ put_vma(vma);
+ return ret;
+}
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
+
/*
* By the time we get here, we already hold the mm semaphore
*
--
2.7.4


2018-02-16 19:16:00

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 16/24] mm: Introduce __page_add_new_anon_rmap()

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 <[email protected]>
---
include/linux/rmap.h | 12 ++++++++++--
mm/memory.c | 8 ++++----
mm/rmap.c | 5 ++---
3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/include/linux/rmap.h b/include/linux/rmap.h
index 988d176472df..a5d282573093 100644
--- a/include/linux/rmap.h
+++ b/include/linux/rmap.h
@@ -174,8 +174,16 @@ void page_add_anon_rmap(struct page *, struct vm_area_struct *,
unsigned long, bool);
void do_page_add_anon_rmap(struct page *, struct vm_area_struct *,
unsigned long, int);
-void page_add_new_anon_rmap(struct page *, struct vm_area_struct *,
- unsigned long, bool);
+void __page_add_new_anon_rmap(struct page *, struct vm_area_struct *,
+ unsigned long, bool);
+static inline void page_add_new_anon_rmap(struct page *page,
+ struct vm_area_struct *vma,
+ unsigned long address, bool compound)
+{
+ VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
+ __page_add_new_anon_rmap(page, vma, address, compound);
+}
+
void page_add_file_rmap(struct page *, bool);
void page_remove_rmap(struct page *, bool);

diff --git a/mm/memory.c b/mm/memory.c
index d2e441aaa96b..67de327c7262 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2559,7 +2559,7 @@ static int wp_page_copy(struct vm_fault *vmf)
* thread doing COW.
*/
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);
/*
@@ -3106,7 +3106,7 @@ int do_swap_page(struct vm_fault *vmf)

/* ksm created a completely new copy */
if (unlikely(page != swapcache && swapcache)) {
- 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);
} else {
@@ -3257,7 +3257,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:
@@ -3509,7 +3509,7 @@ int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
/* copy-on-write page */
if (write && !(vmf->vma_flags & VM_SHARED)) {
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);
} else {
diff --git a/mm/rmap.c b/mm/rmap.c
index 47db27f8049e..6ec168ba5f73 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1136,7 +1136,7 @@ void do_page_add_anon_rmap(struct page *page,
}

/**
- * page_add_new_anon_rmap - add pte mapping to a new anonymous page
+ * __page_add_new_anon_rmap - add pte mapping to a new anonymous page
* @page: the page to add the mapping to
* @vma: the vm area in which the mapping is added
* @address: the user virtual address mapped
@@ -1146,12 +1146,11 @@ void do_page_add_anon_rmap(struct page *page,
* This means the inc-and-test can be bypassed.
* Page does not have to be locked.
*/
-void page_add_new_anon_rmap(struct page *page,
+void __page_add_new_anon_rmap(struct page *page,
struct vm_area_struct *vma, unsigned long address, bool compound)
{
int nr = compound ? hpage_nr_pages(page) : 1;

- VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
__SetPageSwapBacked(page);
if (compound) {
VM_BUG_ON_PAGE(!PageTransHuge(page), page);
--
2.7.4


2018-02-16 19:16:00

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 19/24] mm: Adding speculative page fault failure trace events

This patch a set of new trace events to collect the speculative page fault
event failures.

Signed-off-by: Laurent Dufour <[email protected]>
---
include/trace/events/pagefault.h | 87 ++++++++++++++++++++++++++++++++++++++++
mm/memory.c | 62 ++++++++++++++++++++++------
2 files changed, 136 insertions(+), 13 deletions(-)
create mode 100644 include/trace/events/pagefault.h

diff --git a/include/trace/events/pagefault.h b/include/trace/events/pagefault.h
new file mode 100644
index 000000000000..1d793f8c739b
--- /dev/null
+++ b/include/trace/events/pagefault.h
@@ -0,0 +1,87 @@
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM pagefault
+
+#if !defined(_TRACE_PAGEFAULT_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_PAGEFAULT_H
+
+#include <linux/tracepoint.h>
+#include <linux/mm.h>
+
+DECLARE_EVENT_CLASS(spf,
+
+ TP_PROTO(unsigned long caller,
+ struct vm_area_struct *vma, unsigned long address),
+
+ TP_ARGS(caller, vma, address),
+
+ TP_STRUCT__entry(
+ __field(unsigned long, caller)
+ __field(unsigned long, vm_start)
+ __field(unsigned long, vm_end)
+ __field(unsigned long, address)
+ ),
+
+ TP_fast_assign(
+ __entry->caller = caller;
+ __entry->vm_start = vma->vm_start;
+ __entry->vm_end = vma->vm_end;
+ __entry->address = address;
+ ),
+
+ TP_printk("ip:%lx vma:%lx-%lx address:%lx",
+ __entry->caller, __entry->vm_start, __entry->vm_end,
+ __entry->address)
+);
+
+DEFINE_EVENT(spf, spf_pte_lock,
+
+ TP_PROTO(unsigned long caller,
+ struct vm_area_struct *vma, unsigned long address),
+
+ TP_ARGS(caller, vma, address)
+);
+
+DEFINE_EVENT(spf, spf_vma_changed,
+
+ TP_PROTO(unsigned long caller,
+ struct vm_area_struct *vma, unsigned long address),
+
+ TP_ARGS(caller, vma, address)
+);
+
+DEFINE_EVENT(spf, spf_vma_noanon,
+
+ TP_PROTO(unsigned long caller,
+ struct vm_area_struct *vma, unsigned long address),
+
+ TP_ARGS(caller, vma, address)
+);
+
+DEFINE_EVENT(spf, spf_vma_notsup,
+
+ TP_PROTO(unsigned long caller,
+ struct vm_area_struct *vma, unsigned long address),
+
+ TP_ARGS(caller, vma, address)
+);
+
+DEFINE_EVENT(spf, spf_vma_access,
+
+ TP_PROTO(unsigned long caller,
+ struct vm_area_struct *vma, unsigned long address),
+
+ TP_ARGS(caller, vma, address)
+);
+
+DEFINE_EVENT(spf, spf_pmd_changed,
+
+ TP_PROTO(unsigned long caller,
+ struct vm_area_struct *vma, unsigned long address),
+
+ TP_ARGS(caller, vma, address)
+);
+
+#endif /* _TRACE_PAGEFAULT_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
diff --git a/mm/memory.c b/mm/memory.c
index efc32173264e..2ef686405154 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -80,6 +80,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
@@ -2310,23 +2313,30 @@ static bool pte_spinlock(struct vm_fault *vmf)
}

local_irq_disable();
- if (vma_has_changed(vmf))
+ if (vma_has_changed(vmf)) {
+ trace_spf_vma_changed(_RET_IP_, vmf->vma, vmf->address);
goto out;
+ }

/*
* We check if the pmd value is still the same to ensure that there
* is a huge collapse operation in progress in our back.
*/
pmdval = READ_ONCE(*vmf->pmd);
- if (!pmd_same(pmdval, vmf->orig_pmd))
+ if (!pmd_same(pmdval, vmf->orig_pmd)) {
+ trace_spf_pmd_changed(_RET_IP_, vmf->vma, vmf->address);
goto out;
+ }

vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
- if (unlikely(!spin_trylock(vmf->ptl)))
+ if (unlikely(!spin_trylock(vmf->ptl))) {
+ trace_spf_pte_lock(_RET_IP_, vmf->vma, vmf->address);
goto out;
+ }

if (vma_has_changed(vmf)) {
spin_unlock(vmf->ptl);
+ trace_spf_vma_changed(_RET_IP_, vmf->vma, vmf->address);
goto out;
}

@@ -2357,16 +2367,20 @@ static bool pte_map_lock(struct vm_fault *vmf)
* block on the PTL and thus we're safe.
*/
local_irq_disable();
- if (vma_has_changed(vmf))
+ if (vma_has_changed(vmf)) {
+ trace_spf_vma_changed(_RET_IP_, vmf->vma, vmf->address);
goto out;
+ }

/*
* We check if the pmd value is still the same to ensure that there
* is a huge collapse operation in progress in our back.
*/
pmdval = READ_ONCE(*vmf->pmd);
- if (!pmd_same(pmdval, vmf->orig_pmd))
+ if (!pmd_same(pmdval, vmf->orig_pmd)) {
+ trace_spf_pmd_changed(_RET_IP_, vmf->vma, vmf->address);
goto out;
+ }

/*
* Same as pte_offset_map_lock() except that we call
@@ -2379,11 +2393,13 @@ static bool pte_map_lock(struct vm_fault *vmf)
pte = pte_offset_map(vmf->pmd, vmf->address);
if (unlikely(!spin_trylock(ptl))) {
pte_unmap(pte);
+ trace_spf_pte_lock(_RET_IP_, vmf->vma, vmf->address);
goto out;
}

if (vma_has_changed(vmf)) {
pte_unmap_unlock(pte, ptl);
+ trace_spf_vma_changed(_RET_IP_, vmf->vma, vmf->address);
goto out;
}

@@ -4320,47 +4336,60 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
return ret;

seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
- if (seq & 1)
+ if (seq & 1) {
+ trace_spf_vma_changed(_RET_IP_, vma, address);
goto out_put;
+ }

/*
* 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)
+ if (vma->vm_ops) {
+ trace_spf_vma_notsup(_RET_IP_, vma, address);
goto out_put;
+ }

/*
* __anon_vma_prepare() requires the mmap_sem to be held
* because vm_next and vm_prev must be safe. This can't be guaranteed
* in the speculative path.
*/
- if (unlikely(!vma->anon_vma))
+ if (unlikely(!vma->anon_vma)) {
+ trace_spf_vma_notsup(_RET_IP_, vma, address);
goto out_put;
+ }

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);
goto out_put;
+ }

- if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP)
+ 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.
*/
+ trace_spf_vma_notsup(_RET_IP_, vma, address);
goto out_put;
+ }

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);
goto out_put;
+ }

if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
flags & FAULT_FLAG_INSTRUCTION,
flags & FAULT_FLAG_REMOTE)) {
+ trace_spf_vma_access(_RET_IP_, vma, address);
ret = VM_FAULT_SIGSEGV;
goto out_put;
}
@@ -4368,10 +4397,12 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
/* 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))) {
+ trace_spf_vma_access(_RET_IP_, vma, address);
ret = VM_FAULT_SIGSEGV;
goto out_put;
}
} else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE)))) {
+ trace_spf_vma_access(_RET_IP_, vma, address);
ret = VM_FAULT_SIGSEGV;
goto out_put;
}
@@ -4384,8 +4415,10 @@ 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);
goto out_put;
+ }
#endif

/*
@@ -4458,8 +4491,10 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
* 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))
+ if (read_seqcount_retry(&vma->vm_sequence, seq)) {
+ trace_spf_vma_changed(_RET_IP_, vma, address);
goto out_put;
+ }

mem_cgroup_oom_enable();
ret = handle_pte_fault(&vmf);
@@ -4478,6 +4513,7 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
return ret;

out_walk:
+ trace_spf_vma_notsup(_RET_IP_, vma, address);
local_irq_enable();
out_put:
put_vma(vma);
--
2.7.4


2018-02-16 19:16:07

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 09/24] mm: protect mremap() against SPF hanlder

If a thread is remapping an area while another one is faulting on the
destination area, the SPF handler may fetch the vma from the RB tree before
the pte has been moved by the other thread. This means that the moved ptes
will overwrite those create by the page fault handler leading to page
leaked.

CPU 1 CPU2
enter mremap()
unmap the dest area
copy_vma() Enter speculative page fault handler
>> at this time the dest area is present in the RB tree
fetch the vma matching dest area
create a pte as the VMA matched
Exit the SPF handler
<data written in the new page>
move_ptes()
> it is assumed that the dest area is empty,
> the move ptes overwrite the page mapped by the CPU2.

To prevent that, when the VMA matching the dest area is extended or created
by copy_vma(), it should be marked as non available to the SPF handler.
The usual way to so is to rely on vm_write_begin()/end().
This is already in __vma_adjust() called by copy_vma() (through
vma_merge()). But __vma_adjust() is calling vm_write_end() before returning
which create a window for another thread.
This patch adds a new parameter to vma_merge() which is passed down to
vma_adjust().
The assumption is that copy_vma() is returning a vma which should be
released by calling vm_raw_write_end() by the callee once the ptes have
been moved.

Signed-off-by: Laurent Dufour <[email protected]>
---
include/linux/mm.h | 16 ++++++++++++----
mm/mmap.c | 47 ++++++++++++++++++++++++++++++++++++-----------
mm/mremap.c | 13 +++++++++++++
3 files changed, 61 insertions(+), 15 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 9c22b4134c5d..dcc2a6db8419 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2187,16 +2187,24 @@ void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
extern int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin);
extern int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert,
- struct vm_area_struct *expand);
+ struct vm_area_struct *expand, bool keep_locked);
static inline int vma_adjust(struct vm_area_struct *vma, unsigned long start,
unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
{
- return __vma_adjust(vma, start, end, pgoff, insert, NULL);
+ return __vma_adjust(vma, start, end, pgoff, insert, NULL, false);
}
-extern struct vm_area_struct *vma_merge(struct mm_struct *,
+extern struct vm_area_struct *__vma_merge(struct mm_struct *,
struct vm_area_struct *prev, unsigned long addr, unsigned long end,
unsigned long vm_flags, struct anon_vma *, struct file *, pgoff_t,
- struct mempolicy *, struct vm_userfaultfd_ctx);
+ struct mempolicy *, struct vm_userfaultfd_ctx, bool keep_locked);
+static inline struct vm_area_struct *vma_merge(struct mm_struct *vma,
+ struct vm_area_struct *prev, unsigned long addr, unsigned long end,
+ unsigned long vm_flags, struct anon_vma *anon, struct file *file,
+ pgoff_t off, struct mempolicy *pol, struct vm_userfaultfd_ctx uff)
+{
+ return __vma_merge(vma, prev, addr, end, vm_flags, anon, file, off,
+ pol, uff, false);
+}
extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
extern int __split_vma(struct mm_struct *, struct vm_area_struct *,
unsigned long addr, int new_below);
diff --git a/mm/mmap.c b/mm/mmap.c
index c4d944e1be23..13c799710a8a 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -684,7 +684,7 @@ static inline void __vma_unlink_prev(struct mm_struct *mm,
*/
int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert,
- struct vm_area_struct *expand)
+ struct vm_area_struct *expand, bool keep_locked)
{
struct mm_struct *mm = vma->vm_mm;
struct vm_area_struct *next = vma->vm_next, *orig_vma = vma;
@@ -996,7 +996,8 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,

if (next && next != vma)
vm_raw_write_end(next);
- vm_raw_write_end(vma);
+ if (!keep_locked)
+ vm_raw_write_end(vma);

validate_mm(mm);

@@ -1132,12 +1133,13 @@ can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
* parameter) may establish ptes with the wrong permissions of NNNN
* instead of the right permissions of XXXX.
*/
-struct vm_area_struct *vma_merge(struct mm_struct *mm,
+struct vm_area_struct *__vma_merge(struct mm_struct *mm,
struct vm_area_struct *prev, unsigned long addr,
unsigned long end, unsigned long vm_flags,
struct anon_vma *anon_vma, struct file *file,
pgoff_t pgoff, struct mempolicy *policy,
- struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
+ struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
+ bool keep_locked)
{
pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
struct vm_area_struct *area, *next;
@@ -1185,10 +1187,11 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
/* cases 1, 6 */
err = __vma_adjust(prev, prev->vm_start,
next->vm_end, prev->vm_pgoff, NULL,
- prev);
+ prev, keep_locked);
} else /* cases 2, 5, 7 */
err = __vma_adjust(prev, prev->vm_start,
- end, prev->vm_pgoff, NULL, prev);
+ end, prev->vm_pgoff, NULL, prev,
+ keep_locked);
if (err)
return NULL;
khugepaged_enter_vma_merge(prev, vm_flags);
@@ -1205,10 +1208,12 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
vm_userfaultfd_ctx)) {
if (prev && addr < prev->vm_end) /* case 4 */
err = __vma_adjust(prev, prev->vm_start,
- addr, prev->vm_pgoff, NULL, next);
+ addr, prev->vm_pgoff, NULL, next,
+ keep_locked);
else { /* cases 3, 8 */
err = __vma_adjust(area, addr, next->vm_end,
- next->vm_pgoff - pglen, NULL, next);
+ next->vm_pgoff - pglen, NULL, next,
+ keep_locked);
/*
* In case 3 area is already equal to next and
* this is a noop, but in case 8 "area" has
@@ -3163,9 +3168,20 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,

if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
return NULL; /* should never get here */
- new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
- vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
- vma->vm_userfaultfd_ctx);
+
+ /* There is 3 cases to manage here in
+ * AAAA AAAA AAAA AAAA
+ * PPPP.... PPPP......NNNN PPPP....NNNN PP........NN
+ * PPPPPPPP(A) PPPP..NNNNNNNN(B) PPPPPPPPPPPP(1) NULL
+ * PPPPPPPPNNNN(2)
+ * PPPPNNNNNNNN(3)
+ *
+ * new_vma == prev in case A,1,2
+ * new_vma == next in case B,3
+ */
+ new_vma = __vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
+ vma->anon_vma, vma->vm_file, pgoff,
+ vma_policy(vma), vma->vm_userfaultfd_ctx, true);
if (new_vma) {
/*
* Source vma may have been merged into new_vma
@@ -3205,6 +3221,15 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
get_file(new_vma->vm_file);
if (new_vma->vm_ops && new_vma->vm_ops->open)
new_vma->vm_ops->open(new_vma);
+ /*
+ * As the VMA is linked right now, it may be hit by the
+ * speculative page fault handler. But we don't want it to
+ * to start mapping page in this area until the caller has
+ * potentially move the pte from the moved VMA. To prevent
+ * that we protect it right now, and let the caller unprotect
+ * it once the move is done.
+ */
+ vm_raw_write_begin(new_vma);
vma_link(mm, new_vma, prev, rb_link, rb_parent);
*need_rmap_locks = false;
}
diff --git a/mm/mremap.c b/mm/mremap.c
index 049470aa1e3e..8ed1a1d6eaed 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -302,6 +302,14 @@ static unsigned long move_vma(struct vm_area_struct *vma,
if (!new_vma)
return -ENOMEM;

+ /* new_vma is returned protected by copy_vma, to prevent speculative
+ * page fault to be done in the destination area before we move the pte.
+ * Now, we must also protect the source VMA since we don't want pages
+ * to be mapped in our back while we are copying the PTEs.
+ */
+ if (vma != new_vma)
+ vm_raw_write_begin(vma);
+
moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
need_rmap_locks);
if (moved_len < old_len) {
@@ -318,6 +326,8 @@ static unsigned long move_vma(struct vm_area_struct *vma,
*/
move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
true);
+ if (vma != new_vma)
+ vm_raw_write_end(vma);
vma = new_vma;
old_len = new_len;
old_addr = new_addr;
@@ -326,7 +336,10 @@ 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);
+ if (vma != new_vma)
+ vm_raw_write_end(vma);
}
+ vm_raw_write_end(new_vma);

/* Conceal VM_ACCOUNT so old reservation is not undone */
if (vm_flags & VM_ACCOUNT) {
--
2.7.4


2018-02-16 19:16:09

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 24/24] powerpc/mm: Add speculative page fault

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 VM_FAULT_RETRY, the mmap_sem is acquired and the
traditional page fault processing is done.

The speculative path is only tried for multithreaded process as there is no
risk of contention on the mmap_sem otherwise.

Build on if CONFIG_SPECULATIVE_PAGE_FAULT is defined (currently for
BOOK3S_64 && SMP).

Signed-off-by: Laurent Dufour <[email protected]>
---
arch/powerpc/mm/fault.c | 31 ++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/mm/fault.c b/arch/powerpc/mm/fault.c
index 866446cf2d9a..104f3cc86b51 100644
--- a/arch/powerpc/mm/fault.c
+++ b/arch/powerpc/mm/fault.c
@@ -392,6 +392,9 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,
unsigned long error_code)
{
struct vm_area_struct * vma;
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ struct vm_area_struct *spf_vma = NULL;
+#endif
struct mm_struct *mm = current->mm;
unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
int is_exec = TRAP(regs) == 0x400;
@@ -459,6 +462,20 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,
if (is_exec)
flags |= FAULT_FLAG_INSTRUCTION;

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ if (is_user && (atomic_read(&mm->mm_users) > 1)) {
+ /* let's try a speculative page fault without grabbing the
+ * mmap_sem.
+ */
+ fault = handle_speculative_fault(mm, address, flags, &spf_vma);
+ if (!(fault & VM_FAULT_RETRY)) {
+ perf_sw_event(PERF_COUNT_SW_SPF, 1,
+ regs, address);
+ goto done;
+ }
+ }
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
+
/* When running in the kernel we expect faults to occur only to
* addresses in user space. All other faults represent errors in the
* kernel and should generate an OOPS. Unfortunately, in the case of an
@@ -489,7 +506,16 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,
might_sleep();
}

- vma = find_vma(mm, address);
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ if (spf_vma) {
+ if (can_reuse_spf_vma(spf_vma, address))
+ vma = spf_vma;
+ else
+ vma = find_vma(mm, address);
+ spf_vma = NULL;
+ } else
+#endif
+ vma = find_vma(mm, address);
if (unlikely(!vma))
return bad_area(regs, address);
if (likely(vma->vm_start <= address))
@@ -568,6 +594,9 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,

up_read(&current->mm->mmap_sem);

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+done:
+#endif
if (unlikely(fault & VM_FAULT_ERROR))
return mm_fault_error(regs, address, fault);

--
2.7.4


2018-02-16 19:16:08

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 08/24] mm: Protect VMA modifications using VMA sequence count

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()
- 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 <[email protected]>
---
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/swap_state.c | 8 ++++++--
9 files changed, 86 insertions(+), 38 deletions(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index b66fc8de7d34..adb66e1989ee 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1158,8 +1158,11 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
goto out_mm;
}
for (vma = mm->mmap; vma; vma = vma->vm_next) {
- vma->vm_flags &= ~VM_SOFTDIRTY;
+ vm_write_begin(vma);
+ WRITE_ONCE(vma->vm_flags,
+ vma->vm_flags & ~VM_SOFTDIRTY);
vma_set_page_prot(vma);
+ vm_write_end(vma);
}
downgrade_write(&mm->mmap_sem);
break;
diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index 87a13a7c8270..1da1ba63c7dd 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -659,8 +659,11 @@ int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)

octx = vma->vm_userfaultfd_ctx.ctx;
if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
+ vm_write_begin(vma);
vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
- vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
+ WRITE_ONCE(vma->vm_flags,
+ vma->vm_flags & ~(VM_UFFD_WP | VM_UFFD_MISSING));
+ vm_write_end(vma);
return 0;
}

@@ -885,8 +888,10 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
vma = prev;
else
prev = vma;
- vma->vm_flags = new_flags;
+ vm_write_begin(vma);
+ WRITE_ONCE(vma->vm_flags, new_flags);
vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
+ vm_write_end(vma);
}
up_write(&mm->mmap_sem);
mmput(mm);
@@ -1434,8 +1439,10 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
* the next vma was merged into the current one and
* the current one has not been updated yet.
*/
- vma->vm_flags = new_flags;
+ vm_write_begin(vma);
+ WRITE_ONCE(vma->vm_flags, new_flags);
vma->vm_userfaultfd_ctx.ctx = ctx;
+ vm_write_end(vma);

skip:
prev = vma;
@@ -1592,8 +1599,10 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
* the next vma was merged into the current one and
* the current one has not been updated yet.
*/
- vma->vm_flags = new_flags;
+ vm_write_begin(vma);
+ WRITE_ONCE(vma->vm_flags, new_flags);
vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
+ vm_write_end(vma);

skip:
prev = vma;
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index b7e2268dfc9a..32314e9e48dd 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1006,6 +1006,7 @@ static void collapse_huge_page(struct mm_struct *mm,
if (mm_find_pmd(mm, address) != pmd)
goto out;

+ vm_write_begin(vma);
anon_vma_lock_write(vma->anon_vma);

pte = pte_offset_map(pmd, address);
@@ -1041,6 +1042,7 @@ static void collapse_huge_page(struct mm_struct *mm,
pmd_populate(mm, pmd, pmd_pgtable(_pmd));
spin_unlock(pmd_ptl);
anon_vma_unlock_write(vma->anon_vma);
+ vm_write_end(vma);
result = SCAN_FAIL;
goto out;
}
@@ -1075,6 +1077,7 @@ static void collapse_huge_page(struct mm_struct *mm,
set_pmd_at(mm, address, pmd, _pmd);
update_mmu_cache_pmd(vma, address, pmd);
spin_unlock(pmd_ptl);
+ vm_write_end(vma);

*hpage = NULL;

diff --git a/mm/madvise.c b/mm/madvise.c
index 4d3c922ea1a1..e328f7ab5942 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -184,7 +184,9 @@ static long madvise_behavior(struct vm_area_struct *vma,
/*
* vm_flags is protected by the mmap_sem held in write mode.
*/
- vma->vm_flags = new_flags;
+ vm_write_begin(vma);
+ WRITE_ONCE(vma->vm_flags, new_flags);
+ vm_write_end(vma);
out:
return error;
}
@@ -450,9 +452,11 @@ static void madvise_free_page_range(struct mmu_gather *tlb,
.private = tlb,
};

+ vm_write_begin(vma);
tlb_start_vma(tlb, vma);
walk_page_range(addr, end, &free_walk);
tlb_end_vma(tlb, vma);
+ vm_write_end(vma);
}

static int madvise_free_single_vma(struct vm_area_struct *vma,
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index a8b7d59002e8..49d731c39e4f 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -380,8 +380,11 @@ void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
struct vm_area_struct *vma;

down_write(&mm->mmap_sem);
- for (vma = mm->mmap; vma; vma = vma->vm_next)
+ for (vma = mm->mmap; vma; vma = vma->vm_next) {
+ vm_write_begin(vma);
mpol_rebind_policy(vma->vm_policy, new);
+ vm_write_end(vma);
+ }
up_write(&mm->mmap_sem);
}

@@ -554,9 +557,11 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,
{
int nr_updated;

+ vm_write_begin(vma);
nr_updated = change_protection(vma, addr, end, PAGE_NONE, 0, 1);
if (nr_updated)
count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated);
+ vm_write_end(vma);

return nr_updated;
}
@@ -657,6 +662,7 @@ static int vma_replace_policy(struct vm_area_struct *vma,
if (IS_ERR(new))
return PTR_ERR(new);

+ vm_write_begin(vma);
if (vma->vm_ops && vma->vm_ops->set_policy) {
err = vma->vm_ops->set_policy(vma, new);
if (err)
@@ -664,11 +670,17 @@ static int vma_replace_policy(struct vm_area_struct *vma,
}

old = vma->vm_policy;
- vma->vm_policy = new; /* protected by mmap_sem */
+ /*
+ * The speculative page fault handler access this field without
+ * hodling the mmap_sem.
+ */
+ WRITE_ONCE(vma->vm_policy, new);
+ vm_write_end(vma);
mpol_put(old);

return 0;
err_out:
+ vm_write_end(vma);
mpol_put(new);
return err;
}
@@ -1552,23 +1564,28 @@ COMPAT_SYSCALL_DEFINE6(mbind, compat_ulong_t, start, compat_ulong_t, len,
struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
unsigned long addr)
{
- struct mempolicy *pol = NULL;
+ struct mempolicy *pol;

- if (vma) {
- if (vma->vm_ops && vma->vm_ops->get_policy) {
- pol = vma->vm_ops->get_policy(vma, addr);
- } else if (vma->vm_policy) {
- pol = vma->vm_policy;
+ if (!vma)
+ return NULL;

- /*
- * shmem_alloc_page() passes MPOL_F_SHARED policy with
- * a pseudo vma whose vma->vm_ops=NULL. Take a reference
- * count on these policies which will be dropped by
- * mpol_cond_put() later
- */
- if (mpol_needs_cond_ref(pol))
- mpol_get(pol);
- }
+ if (vma->vm_ops && vma->vm_ops->get_policy)
+ return vma->vm_ops->get_policy(vma, addr);
+
+ /*
+ * This could be called without holding the mmap_sem in the
+ * speculative page fault handler's path.
+ */
+ pol = READ_ONCE(vma->vm_policy);
+ if (pol) {
+ /*
+ * shmem_alloc_page() passes MPOL_F_SHARED policy with
+ * a pseudo vma whose vma->vm_ops=NULL. Take a reference
+ * count on these policies which will be dropped by
+ * mpol_cond_put() later
+ */
+ if (mpol_needs_cond_ref(pol))
+ mpol_get(pol);
}

return pol;
diff --git a/mm/mlock.c b/mm/mlock.c
index 74e5a6547c3d..c40285c94ced 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -445,7 +445,9 @@ static unsigned long __munlock_pagevec_fill(struct pagevec *pvec,
void munlock_vma_pages_range(struct vm_area_struct *vma,
unsigned long start, unsigned long end)
{
- vma->vm_flags &= VM_LOCKED_CLEAR_MASK;
+ vm_write_begin(vma);
+ WRITE_ONCE(vma->vm_flags, vma->vm_flags & VM_LOCKED_CLEAR_MASK);
+ vm_write_end(vma);

while (start < end) {
struct page *page;
@@ -568,10 +570,11 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
* It's okay if try_to_unmap_one unmaps a page just after we
* set VM_LOCKED, populate_vma_page_range will bring it back.
*/
-
- if (lock)
- vma->vm_flags = newflags;
- else
+ if (lock) {
+ vm_write_begin(vma);
+ WRITE_ONCE(vma->vm_flags, newflags);
+ vm_write_end(vma);
+ } else
munlock_vma_pages_range(vma, start, end);

out:
diff --git a/mm/mmap.c b/mm/mmap.c
index 671940b72696..c4d944e1be23 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -847,17 +847,18 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
}

if (start != vma->vm_start) {
- vma->vm_start = start;
+ WRITE_ONCE(vma->vm_start, start);
start_changed = true;
}
if (end != vma->vm_end) {
- vma->vm_end = end;
+ WRITE_ONCE(vma->vm_end, end);
end_changed = true;
}
- vma->vm_pgoff = pgoff;
+ WRITE_ONCE(vma->vm_pgoff, pgoff);
if (adjust_next) {
- next->vm_start += adjust_next << PAGE_SHIFT;
- next->vm_pgoff += adjust_next;
+ WRITE_ONCE(next->vm_start,
+ next->vm_start + (adjust_next << PAGE_SHIFT));
+ WRITE_ONCE(next->vm_pgoff, next->vm_pgoff + adjust_next);
}

if (root) {
@@ -1781,6 +1782,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
out:
perf_event_mmap(vma);

+ vm_write_begin(vma);
vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT);
if (vm_flags & VM_LOCKED) {
if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
@@ -1803,6 +1805,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
vma->vm_flags |= VM_SOFTDIRTY;

vma_set_page_prot(vma);
+ vm_write_end(vma);

return addr;

@@ -2431,8 +2434,8 @@ int expand_downwards(struct vm_area_struct *vma,
mm->locked_vm += grow;
vm_stat_account(mm, vma->vm_flags, grow);
anon_vma_interval_tree_pre_update_vma(vma);
- vma->vm_start = address;
- vma->vm_pgoff -= grow;
+ WRITE_ONCE(vma->vm_start, address);
+ WRITE_ONCE(vma->vm_pgoff, vma->vm_pgoff - grow);
anon_vma_interval_tree_post_update_vma(vma);
vma_gap_update(vma);
spin_unlock(&mm->page_table_lock);
diff --git a/mm/mprotect.c b/mm/mprotect.c
index e3309fcf586b..9b7a71c30287 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -366,7 +366,8 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
* vm_flags and vm_page_prot are protected by the mmap_sem
* held in write mode.
*/
- vma->vm_flags = newflags;
+ vm_write_begin(vma);
+ WRITE_ONCE(vma->vm_flags, newflags);
dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
vma_set_page_prot(vma);

@@ -381,6 +382,7 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
(newflags & VM_WRITE)) {
populate_vma_page_range(vma, start, end, NULL);
}
+ vm_write_end(vma);

vm_stat_account(mm, oldflags, -nrpages);
vm_stat_account(mm, newflags, nrpages);
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 39ae7cfad90f..eeef59c2cd76 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -550,6 +550,10 @@ static unsigned long swapin_nr_pages(unsigned long offset)
* the readahead.
*
* Caller must hold down_read on the vma->vm_mm if vma is not NULL.
+ * This is needed to ensure the VMA will not be freed in our back. In the case
+ * of the speculative page fault handler, this cannot happen, even if we don't
+ * hold the mmap_sem. Callees are assumed to take care of reading VMA's fields
+ * using READ_ONCE() to read consistent values.
*/
struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
struct vm_area_struct *vma, unsigned long addr)
@@ -643,9 +647,9 @@ static inline void swap_ra_clamp_pfn(struct vm_area_struct *vma,
unsigned long *start,
unsigned long *end)
{
- *start = max3(lpfn, PFN_DOWN(vma->vm_start),
+ *start = max3(lpfn, PFN_DOWN(READ_ONCE(vma->vm_start)),
PFN_DOWN(faddr & PMD_MASK));
- *end = min3(rpfn, PFN_DOWN(vma->vm_end),
+ *end = min3(rpfn, PFN_DOWN(READ_ONCE(vma->vm_end)),
PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE));
}

--
2.7.4


2018-02-16 19:16:13

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 06/24] mm: make pte_unmap_same compatible with SPF

pte_unmap_same() is making the assumption that the page table are still
around because the mmap_sem is held.
This is no more the case when running a speculative page fault and
additional check must be made to ensure that the final page table are still
there.

This is now done by calling pte_spinlock() to check for the VMA's
consistency while locking for the page tables.

This is requiring passing a vm_fault structure to pte_unmap_same() which is
containing all the needed parameters.

As pte_spinlock() may fail in the case of a speculative page fault, if the
VMA has been touched in our back, pte_unmap_same() should now return 3
cases :
1. pte are the same (0)
2. pte are different (VM_FAULT_PTNOTSAME)
3. a VMA's changes has been detected (VM_FAULT_RETRY)

The case 2 is handled by the introduction of a new VM_FAULT flag named
VM_FAULT_PTNOTSAME which is then trapped in cow_user_page().
If VM_FAULT_RETRY is returned, it is passed up to the callers to retry the
page fault while holding the mmap_sem.

Signed-off-by: Laurent Dufour <[email protected]>
---
include/linux/mm.h | 1 +
mm/memory.c | 37 ++++++++++++++++++++++++++-----------
2 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 51d950cac772..e869adec9023 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1197,6 +1197,7 @@ static inline void clear_page_pfmemalloc(struct page *page)
#define VM_FAULT_NEEDDSYNC 0x2000 /* ->fault did not modify page tables
* and needs fsync() to complete (for
* synchronous page faults in DAX) */
+#define VM_FAULT_PTNOTSAME 0x4000 /* Page table entries have changed */

#define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | \
VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE | \
diff --git a/mm/memory.c b/mm/memory.c
index 1ca289f53dd6..a301b9003200 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2309,21 +2309,29 @@ static bool pte_map_lock(struct vm_fault *vmf)
* parts, do_swap_page must check under lock before unmapping the pte and
* proceeding (but do_wp_page is only called after already making such a check;
* and do_anonymous_page can safely check later on).
+ *
+ * pte_unmap_same() returns:
+ * 0 if the PTE are the same
+ * VM_FAULT_PTNOTSAME if the PTE are different
+ * VM_FAULT_RETRY if the VMA has changed in our back during
+ * a speculative page fault handling.
*/
-static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
- pte_t *page_table, pte_t orig_pte)
+static inline int pte_unmap_same(struct vm_fault *vmf)
{
- int same = 1;
+ int ret = 0;
+
#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
if (sizeof(pte_t) > sizeof(unsigned long)) {
- spinlock_t *ptl = pte_lockptr(mm, pmd);
- spin_lock(ptl);
- same = pte_same(*page_table, orig_pte);
- spin_unlock(ptl);
+ if (pte_spinlock(vmf)) {
+ if (!pte_same(*vmf->pte, vmf->orig_pte))
+ ret = VM_FAULT_PTNOTSAME;
+ spin_unlock(vmf->ptl);
+ } else
+ ret = VM_FAULT_RETRY;
}
#endif
- pte_unmap(page_table);
- return same;
+ pte_unmap(vmf->pte);
+ return ret;
}

static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
@@ -2912,7 +2920,7 @@ int do_swap_page(struct vm_fault *vmf)
pte_t pte;
int locked;
int exclusive = 0;
- int ret = 0;
+ int ret;
bool vma_readahead = swap_use_vma_readahead();

if (vma_readahead) {
@@ -2920,9 +2928,16 @@ int do_swap_page(struct vm_fault *vmf)
swapcache = page;
}

- if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) {
+ ret = pte_unmap_same(vmf);
+ if (ret) {
if (page)
put_page(page);
+ /*
+ * In the case the PTE are different, meaning that the
+ * page has already been processed by another CPU, we return 0.
+ */
+ if (ret == VM_FAULT_PTNOTSAME)
+ ret = 0;
goto out;
}

--
2.7.4


2018-02-16 19:16:27

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 14/24] mm: Introduce __maybe_mkwrite()

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 <[email protected]>
---
include/linux/mm.h | 9 +++++++--
mm/memory.c | 6 +++---
2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index fd4eda5195b9..49f160e56a0f 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -685,13 +685,18 @@ void free_compound_page(struct page *page);
* pte_mkwrite. But get_user_pages can cause write faults for mappings
* that do not have writing enabled, when used by access_process_vm.
*/
-static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
+static inline pte_t __maybe_mkwrite(pte_t pte, unsigned long vma_flags)
{
- if (likely(vma->vm_flags & VM_WRITE))
+ if (likely(vma_flags & VM_WRITE))
pte = pte_mkwrite(pte);
return pte;
}

+static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
+{
+ return __maybe_mkwrite(pte, vma->vm_flags);
+}
+
int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
struct page *page);
int finish_fault(struct vm_fault *vmf);
diff --git a/mm/memory.c b/mm/memory.c
index bc14973b4eba..8fd33d05ef1e 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2472,7 +2472,7 @@ static inline void wp_page_reuse(struct vm_fault *vmf)

flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
entry = pte_mkyoung(vmf->orig_pte);
- entry = maybe_mkwrite(pte_mkdirty(entry), vma);
+ entry = __maybe_mkwrite(pte_mkdirty(entry), vmf->vma_flags);
if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
update_mmu_cache(vma, vmf->address, vmf->pte);
pte_unmap_unlock(vmf->pte, vmf->ptl);
@@ -2549,8 +2549,8 @@ static int wp_page_copy(struct vm_fault *vmf)
inc_mm_counter_fast(mm, MM_ANONPAGES);
}
flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
- entry = mk_pte(new_page, vma->vm_page_prot);
- entry = maybe_mkwrite(pte_mkdirty(entry), vma);
+ entry = mk_pte(new_page, vmf->vma_page_prot);
+ entry = __maybe_mkwrite(pte_mkdirty(entry), vmf->vma_flags);
/*
* Clear the pte entry and flush it first, before updating the
* pte with the new entry. This will avoid a race condition
--
2.7.4


2018-02-16 19:16:32

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 10/24] mm: Protect SPF handler against anon_vma changes

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 <[email protected]>
---
mm/memory.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/mm/memory.c b/mm/memory.c
index 3cc1a13417cf..e37af9af4202 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -624,7 +624,9 @@ void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
* Hide vma from rmap and truncate_pagecache before freeing
* pgtables
*/
+ vm_write_begin(vma);
unlink_anon_vmas(vma);
+ vm_write_end(vma);
unlink_file_vma(vma);

if (is_vm_hugetlb_page(vma)) {
@@ -638,7 +640,9 @@ void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
&& !is_vm_hugetlb_page(next)) {
vma = next;
next = vma->vm_next;
+ vm_write_begin(vma);
unlink_anon_vmas(vma);
+ vm_write_end(vma);
unlink_file_vma(vma);
}
free_pgd_range(tlb, addr, vma->vm_end,
--
2.7.4


2018-02-16 19:16:34

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 11/24] mm: Cache some VMA fields in the vm_fault structure

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 <[email protected]>
---
include/linux/mm.h | 6 ++++++
mm/hugetlb.c | 2 ++
mm/khugepaged.c | 2 ++
mm/memory.c | 38 ++++++++++++++++++++------------------
4 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index dcc2a6db8419..fd4eda5195b9 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -361,6 +361,12 @@ struct vm_fault {
* page table to avoid allocation from
* atomic context.
*/
+ /*
+ * These entries are required when handling speculative page fault.
+ * This way the page handling is done using consistent field values.
+ */
+ unsigned long vma_flags;
+ pgprot_t vma_page_prot;
};

/* page entry size for vm->huge_fault() */
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 7c204e3d132b..22a818c7a6de 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -3716,6 +3716,8 @@ static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
.vma = vma,
.address = address,
.flags = flags,
+ .vma_flags = vma->vm_flags,
+ .vma_page_prot = vma->vm_page_prot,
/*
* Hard to debug if it ends up being
* used by a callee that assumes
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 32314e9e48dd..a946d5306160 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -882,6 +882,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 */
diff --git a/mm/memory.c b/mm/memory.c
index e37af9af4202..66a79f44f303 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2615,7 +2615,7 @@ static int wp_page_copy(struct vm_fault *vmf)
* Don't let another task, with possibly unlocked vma,
* keep the mlocked page.
*/
- if (page_copied && (vma->vm_flags & VM_LOCKED)) {
+ if (page_copied && (vmf->vma_flags & VM_LOCKED)) {
lock_page(old_page); /* LRU manipulation */
if (PageMlocked(old_page))
munlock_vma_page(old_page);
@@ -2649,7 +2649,7 @@ static int wp_page_copy(struct vm_fault *vmf)
*/
int finish_mkwrite_fault(struct vm_fault *vmf)
{
- WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
+ WARN_ON_ONCE(!(vmf->vma_flags & VM_SHARED));
if (!pte_map_lock(vmf))
return VM_FAULT_RETRY;
/*
@@ -2751,7 +2751,7 @@ static int do_wp_page(struct vm_fault *vmf)
* We should not cow pages in a shared writeable mapping.
* Just mark the pages writable and/or call ops->pfn_mkwrite.
*/
- if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
+ if ((vmf->vma_flags & (VM_WRITE|VM_SHARED)) ==
(VM_WRITE|VM_SHARED))
return wp_pfn_shared(vmf);

@@ -2798,7 +2798,7 @@ static int do_wp_page(struct vm_fault *vmf)
return VM_FAULT_WRITE;
}
unlock_page(vmf->page);
- } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
+ } else if (unlikely((vmf->vma_flags & (VM_WRITE|VM_SHARED)) ==
(VM_WRITE|VM_SHARED))) {
return wp_page_shared(vmf);
}
@@ -3090,7 +3090,7 @@ int do_swap_page(struct vm_fault *vmf)

inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
- pte = mk_pte(page, vma->vm_page_prot);
+ pte = mk_pte(page, vmf->vma_page_prot);
if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
pte = maybe_mkwrite(pte_mkdirty(pte), vma);
vmf->flags &= ~FAULT_FLAG_WRITE;
@@ -3116,7 +3116,7 @@ int do_swap_page(struct vm_fault *vmf)

swap_free(entry);
if (mem_cgroup_swap_full(page) ||
- (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
+ (vmf->vma_flags & VM_LOCKED) || PageMlocked(page))
try_to_free_swap(page);
unlock_page(page);
if (page != swapcache && swapcache) {
@@ -3173,7 +3173,7 @@ static int do_anonymous_page(struct vm_fault *vmf)
pte_t entry;

/* File mapping without ->vm_ops ? */
- if (vma->vm_flags & VM_SHARED)
+ if (vmf->vma_flags & VM_SHARED)
return VM_FAULT_SIGBUS;

/*
@@ -3197,7 +3197,7 @@ static int do_anonymous_page(struct vm_fault *vmf)
if (!(vmf->flags & FAULT_FLAG_WRITE) &&
!mm_forbids_zeropage(vma->vm_mm)) {
entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
- vma->vm_page_prot));
+ vmf->vma_page_prot));
if (!pte_map_lock(vmf))
return VM_FAULT_RETRY;
if (!pte_none(*vmf->pte))
@@ -3230,8 +3230,8 @@ static int do_anonymous_page(struct vm_fault *vmf)
*/
__SetPageUptodate(page);

- entry = mk_pte(page, vma->vm_page_prot);
- if (vma->vm_flags & VM_WRITE)
+ entry = mk_pte(page, vmf->vma_page_prot);
+ if (vmf->vma_flags & VM_WRITE)
entry = pte_mkwrite(pte_mkdirty(entry));

if (!pte_map_lock(vmf)) {
@@ -3427,7 +3427,7 @@ static int do_set_pmd(struct vm_fault *vmf, struct page *page)
for (i = 0; i < HPAGE_PMD_NR; i++)
flush_icache_page(vma, page + i);

- entry = mk_huge_pmd(page, vma->vm_page_prot);
+ entry = mk_huge_pmd(page, vmf->vma_page_prot);
if (write)
entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);

@@ -3501,11 +3501,11 @@ int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
return VM_FAULT_NOPAGE;

flush_icache_page(vma, page);
- entry = mk_pte(page, vma->vm_page_prot);
+ entry = mk_pte(page, vmf->vma_page_prot);
if (write)
entry = maybe_mkwrite(pte_mkdirty(entry), vma);
/* copy-on-write page */
- if (write && !(vma->vm_flags & VM_SHARED)) {
+ if (write && !(vmf->vma_flags & VM_SHARED)) {
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);
@@ -3544,7 +3544,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;
else
page = vmf->page;
@@ -3798,7 +3798,7 @@ static int do_fault(struct vm_fault *vmf)
ret = VM_FAULT_SIGBUS;
else if (!(vmf->flags & FAULT_FLAG_WRITE))
ret = do_read_fault(vmf);
- else if (!(vma->vm_flags & VM_SHARED))
+ else if (!(vmf->vma_flags & VM_SHARED))
ret = do_cow_fault(vmf);
else
ret = do_shared_fault(vmf);
@@ -3855,7 +3855,7 @@ static int do_numa_page(struct vm_fault *vmf)
* accessible ptes, some can allow access by kernel mode.
*/
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);
@@ -3889,7 +3889,7 @@ static int do_numa_page(struct vm_fault *vmf)
* Flag if the page is shared between multiple address spaces. This
* is later used when determining whether to group tasks together
*/
- 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);
@@ -3934,7 +3934,7 @@ static inline int wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
return vmf->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);

return VM_FAULT_FALLBACK;
@@ -4081,6 +4081,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,
};
unsigned int dirty = flags & FAULT_FLAG_WRITE;
struct mm_struct *mm = vma->vm_mm;
--
2.7.4


2018-02-16 19:16:53

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 21/24] perf tools: Add support for the SPF perf event

Add support for the new speculative faults event.

Signed-off-by: Laurent Dufour <[email protected]>
---
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 +
5 files changed, 8 insertions(+)

diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h
index e0739a1aa4b2..164383273147 100644
--- a/tools/include/uapi/linux/perf_event.h
+++ b/tools/include/uapi/linux/perf_event.h
@@ -112,6 +112,7 @@ enum perf_sw_ids {
PERF_COUNT_SW_EMULATION_FAULTS = 8,
PERF_COUNT_SW_DUMMY = 9,
PERF_COUNT_SW_BPF_OUTPUT = 10,
+ PERF_COUNT_SW_SPF = 11,

PERF_COUNT_SW_MAX, /* non-ABI */
};
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 66fa45198a11..7fddb37f44ca 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -438,6 +438,7 @@ const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
"alignment-faults",
"emulation-faults",
"dummy",
+ "speculative-faults",
};

static const char *__perf_evsel__sw_name(u64 config)
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 34589c427e52..2a8189c6d5fc 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -140,6 +140,10 @@ struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
.symbol = "bpf-output",
.alias = "",
},
+ [PERF_COUNT_SW_SPF] = {
+ .symbol = "speculative-faults",
+ .alias = "spf",
+ },
};

#define __PERF_EVENT_FIELD(config, name) \
diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
index 655ecff636a8..5d6782426b30 100644
--- a/tools/perf/util/parse-events.l
+++ b/tools/perf/util/parse-events.l
@@ -308,6 +308,7 @@ emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EM
dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
duration_time { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
+speculative-faults|spf { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_SPF); }

/*
* We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index b1e999bd21ef..100507d632fa 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -1142,6 +1142,7 @@ static struct {
PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
PERF_CONST(COUNT_SW_EMULATION_FAULTS),
PERF_CONST(COUNT_SW_DUMMY),
+ PERF_CONST(COUNT_SW_SPF),

PERF_CONST(SAMPLE_IP),
PERF_CONST(SAMPLE_TID),
--
2.7.4


2018-02-16 19:17:10

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 17/24] mm: Protect mm_rb tree with a rwlock

This change is inspired by the Peter's proposal patch [1] which was
protecting the VMA using SRCU. Unfortunately, SRCU is not scaling well in
that particular case, and it is introducing major performance degradation
due to excessive scheduling operations.

To allow access to the mm_rb tree without grabbing the mmap_sem, this patch
is protecting it access using a rwlock. As the mm_rb tree is a O(log n)
search it is safe to protect it using such a lock. The VMA cache is not
protected by the new rwlock and it should not be used without holding the
mmap_sem.

To allow the picked VMA structure to be used once the rwlock is released, a
use count is added to the VMA structure. When the VMA is allocated it is
set to 1. Each time the VMA is picked with the rwlock held its use count
is incremented. Each time the VMA is released it is decremented. When the
use count hits zero, this means that the VMA is no more used and should be
freed.

This patch is preparing for 2 kind of VMA access :
- as usual, under the control of the mmap_sem,
- without holding the mmap_sem for the speculative page fault handler.

Access done under the control the mmap_sem doesn't require to grab the
rwlock to protect read access to the mm_rb tree, but access in write must
be done under the protection of the rwlock too. This affects inserting and
removing of elements in the RB tree.

The patch is introducing 2 new functions:
- vma_get() to find a VMA based on an address by holding the new rwlock.
- vma_put() to release the VMA when its no more used.
These services are designed to be used when access are made to the RB tree
without holding the mmap_sem.

When a VMA is removed from the RB tree, its vma->vm_rb field is cleared and
we rely on the WMB done when releasing the rwlock to serialize the write
with the RMB done in a later patch to check for the VMA's validity.

When free_vma is called, the file associated with the VMA is closed
immediately, but the policy and the file structure remained in used until
the VMA's use count reach 0, which may happens later when exiting an
in progress speculative page fault.

[1] https://patchwork.kernel.org/patch/5108281/

Cc: Peter Zijlstra (Intel) <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Signed-off-by: Laurent Dufour <[email protected]>
---
include/linux/mm_types.h | 4 ++
kernel/fork.c | 3 ++
mm/init-mm.c | 3 ++
mm/internal.h | 6 +++
mm/mmap.c | 122 ++++++++++++++++++++++++++++++++++-------------
5 files changed, 106 insertions(+), 32 deletions(-)

diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 34fde7111e88..28c763ea1036 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -335,6 +335,7 @@ struct vm_area_struct {
struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
seqcount_t vm_sequence;
+ atomic_t vm_ref_count; /* see vma_get(), vma_put() */
#endif
} __randomize_layout;

@@ -353,6 +354,9 @@ struct kioctx_table;
struct mm_struct {
struct vm_area_struct *mmap; /* list of VMAs */
struct rb_root mm_rb;
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ rwlock_t mm_rb_lock;
+#endif
u32 vmacache_seqnum; /* per-thread vmacache */
#ifdef CONFIG_MMU
unsigned long (*get_unmapped_area) (struct file *filp,
diff --git a/kernel/fork.c b/kernel/fork.c
index 505195d26744..6c4dc1aa6f17 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -887,6 +887,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
mm->mmap = NULL;
mm->mm_rb = RB_ROOT;
mm->vmacache_seqnum = 0;
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ rwlock_init(&mm->mm_rb_lock);
+#endif
atomic_set(&mm->mm_users, 1);
atomic_set(&mm->mm_count, 1);
init_rwsem(&mm->mmap_sem);
diff --git a/mm/init-mm.c b/mm/init-mm.c
index f94d5d15ebc0..e71ac37a98c4 100644
--- a/mm/init-mm.c
+++ b/mm/init-mm.c
@@ -17,6 +17,9 @@

struct mm_struct init_mm = {
.mm_rb = RB_ROOT,
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ .mm_rb_lock = __RW_LOCK_UNLOCKED(init_mm.mm_rb_lock),
+#endif
.pgd = swapper_pg_dir,
.mm_users = ATOMIC_INIT(2),
.mm_count = ATOMIC_INIT(1),
diff --git a/mm/internal.h b/mm/internal.h
index 62d8c34e63d5..fb2667b20f0a 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -40,6 +40,12 @@ void page_writeback_init(void);

int do_swap_page(struct vm_fault *vmf);

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+extern struct vm_area_struct *get_vma(struct mm_struct *mm,
+ unsigned long addr);
+extern void put_vma(struct vm_area_struct *vma);
+#endif
+
void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
unsigned long floor, unsigned long ceiling);

diff --git a/mm/mmap.c b/mm/mmap.c
index 13c799710a8a..220ba8cb65fc 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -160,6 +160,27 @@ void unlink_file_vma(struct vm_area_struct *vma)
}
}

+static void __free_vma(struct vm_area_struct *vma)
+{
+ if (vma->vm_file)
+ fput(vma->vm_file);
+ mpol_put(vma_policy(vma));
+ kmem_cache_free(vm_area_cachep, vma);
+}
+
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+void put_vma(struct vm_area_struct *vma)
+{
+ if (atomic_dec_and_test(&vma->vm_ref_count))
+ __free_vma(vma);
+}
+#else
+static inline void put_vma(struct vm_area_struct *vma)
+{
+ return __free_vma(vma);
+}
+#endif
+
/*
* Close a vm structure and free it, returning the next.
*/
@@ -170,10 +191,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
might_sleep();
if (vma->vm_ops && vma->vm_ops->close)
vma->vm_ops->close(vma);
- if (vma->vm_file)
- fput(vma->vm_file);
- mpol_put(vma_policy(vma));
- kmem_cache_free(vm_area_cachep, vma);
+ put_vma(vma);
return next;
}

@@ -393,6 +411,14 @@ static void validate_mm(struct mm_struct *mm)
#define validate_mm(mm) do { } while (0)
#endif

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+#define mm_rb_write_lock(mm) write_lock(&(mm)->mm_rb_lock)
+#define mm_rb_write_unlock(mm) write_unlock(&(mm)->mm_rb_lock)
+#else
+#define mm_rb_write_lock(mm) do { } while (0)
+#define mm_rb_write_unlock(mm) do { } while (0)
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
+
RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
unsigned long, rb_subtree_gap, vma_compute_subtree_gap)

@@ -411,26 +437,37 @@ static void vma_gap_update(struct vm_area_struct *vma)
}

static inline void vma_rb_insert(struct vm_area_struct *vma,
- struct rb_root *root)
+ struct mm_struct *mm)
{
+ struct rb_root *root = &mm->mm_rb;
+
/* All rb_subtree_gap values must be consistent prior to insertion */
validate_mm_rb(root, NULL);

rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
}

-static void __vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
+static void __vma_rb_erase(struct vm_area_struct *vma, struct mm_struct *mm)
{
+ struct rb_root *root = &mm->mm_rb;
/*
* Note rb_erase_augmented is a fairly large inline function,
* so make sure we instantiate it only once with our desired
* augmented rbtree callbacks.
*/
+ mm_rb_write_lock(mm);
rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
+ mm_rb_write_unlock(mm); /* wmb */
+
+ /*
+ * Ensure the removal is complete before clearing the node.
+ * Matched by vma_has_changed()/handle_speculative_fault().
+ */
+ RB_CLEAR_NODE(&vma->vm_rb);
}

static __always_inline void vma_rb_erase_ignore(struct vm_area_struct *vma,
- struct rb_root *root,
+ struct mm_struct *mm,
struct vm_area_struct *ignore)
{
/*
@@ -438,21 +475,21 @@ static __always_inline void vma_rb_erase_ignore(struct vm_area_struct *vma,
* with the possible exception of the "next" vma being erased if
* next->vm_start was reduced.
*/
- validate_mm_rb(root, ignore);
+ validate_mm_rb(&mm->mm_rb, ignore);

- __vma_rb_erase(vma, root);
+ __vma_rb_erase(vma, mm);
}

static __always_inline void vma_rb_erase(struct vm_area_struct *vma,
- struct rb_root *root)
+ struct mm_struct *mm)
{
/*
* All rb_subtree_gap values must be consistent prior to erase,
* with the possible exception of the vma being erased.
*/
- validate_mm_rb(root, vma);
+ validate_mm_rb(&mm->mm_rb, vma);

- __vma_rb_erase(vma, root);
+ __vma_rb_erase(vma, mm);
}

/*
@@ -558,10 +595,6 @@ void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
else
mm->highest_vm_end = vm_end_gap(vma);

-#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
- seqcount_init(&vma->vm_sequence);
-#endif
-
/*
* vma->vm_prev wasn't known when we followed the rbtree to find the
* correct insertion point for that vma. As a result, we could not
@@ -571,10 +604,15 @@ void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
* immediately update the gap to the correct value. Finally we
* rebalance the rbtree after all augmented values have been set.
*/
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+ atomic_set(&vma->vm_ref_count, 1);
+#endif
+ mm_rb_write_lock(mm);
rb_link_node(&vma->vm_rb, rb_parent, rb_link);
vma->rb_subtree_gap = 0;
vma_gap_update(vma);
- vma_rb_insert(vma, &mm->mm_rb);
+ vma_rb_insert(vma, mm);
+ mm_rb_write_unlock(mm);
}

static void __vma_link_file(struct vm_area_struct *vma)
@@ -650,7 +688,7 @@ static __always_inline void __vma_unlink_common(struct mm_struct *mm,
{
struct vm_area_struct *next;

- vma_rb_erase_ignore(vma, &mm->mm_rb, ignore);
+ vma_rb_erase_ignore(vma, mm, ignore);
next = vma->vm_next;
if (has_prev)
prev->vm_next = next;
@@ -923,16 +961,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));
vm_raw_write_end(next);
- kmem_cache_free(vm_area_cachep, next);
+ put_vma(next);
/*
* In mprotect's case 6 (see comments on vma_merge),
* we must remove another next too. It would clutter
@@ -2182,15 +2217,11 @@ 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. */
-struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
+static struct vm_area_struct *__find_vma(struct mm_struct *mm,
+ unsigned long addr)
{
struct rb_node *rb_node;
- struct vm_area_struct *vma;
-
- /* Check the cache first. */
- vma = vmacache_find(mm, addr);
- if (likely(vma))
- return vma;
+ struct vm_area_struct *vma = NULL;

rb_node = mm->mm_rb.rb_node;

@@ -2208,13 +2239,40 @@ struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
rb_node = rb_node->rb_right;
}

+ return vma;
+}
+
+struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
+{
+ struct vm_area_struct *vma;
+
+ /* Check the cache first. */
+ vma = vmacache_find(mm, addr);
+ if (likely(vma))
+ return vma;
+
+ vma = __find_vma(mm, addr);
if (vma)
vmacache_update(addr, vma);
return vma;
}
-
EXPORT_SYMBOL(find_vma);

+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
+struct vm_area_struct *get_vma(struct mm_struct *mm, unsigned long addr)
+{
+ struct vm_area_struct *vma = NULL;
+
+ read_lock(&mm->mm_rb_lock);
+ vma = __find_vma(mm, addr);
+ if (vma)
+ atomic_inc(&vma->vm_ref_count);
+ read_unlock(&mm->mm_rb_lock);
+
+ return vma;
+}
+#endif
+
/*
* Same as find_vma, but also return a pointer to the previous VMA in *pprev.
*/
@@ -2582,7 +2640,7 @@ detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
insertion_point = (prev ? &prev->vm_next : &mm->mmap);
vma->vm_prev = NULL;
do {
- vma_rb_erase(vma, &mm->mm_rb);
+ vma_rb_erase(vma, mm);
mm->map_count--;
tail_vma = vma;
vma = vma->vm_next;
--
2.7.4


2018-02-16 19:17:26

by Laurent Dufour

[permalink] [raw]
Subject: [PATCH v8 02/24] x86/mm: Define CONFIG_SPECULATIVE_PAGE_FAULT

Introduce CONFIG_SPECULATIVE_PAGE_FAULT which turns on the Speculative Page
Fault handler when building for 64bits with SMP.

Cc: Thomas Gleixner <[email protected]>
Signed-off-by: Laurent Dufour <[email protected]>
---
arch/x86/Kconfig | 1 +
1 file changed, 1 insertion(+)

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 52221529abf9..e7f0c6f9f877 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -29,6 +29,7 @@ config X86_64
select HAVE_ARCH_SOFT_DIRTY
select MODULES_USE_ELF_RELA
select X86_DEV_DMA_OPS
+ select SPECULATIVE_PAGE_FAULT if SMP

#
# Arch settings
--
2.7.4


2018-02-26 17:20:02

by Daniel Jordan

[permalink] [raw]
Subject: Re: [PATCH v8 18/24] mm: Provide speculative fault infrastructure

Hi Laurent,

This series doesn't build for me[*] when CONFIG_TRANSPARENT_HUGEPAGE is unset.

The problem seems to be that the BUILD_BUG() version of pmd_same is called in pte_map_lock:

On 02/16/2018 10:25 AM, Laurent Dufour wrote:
> +static bool pte_map_lock(struct vm_fault *vmf)
> +{
...snip...
> + if (!pmd_same(pmdval, vmf->orig_pmd))
> + goto out;

Since SPF can now call pmd_same without THP, maybe the way to fix it is just

diff --git a/include/asm-generic/pgtable.h b/include/asm-generic/pgtable.h
index 2cfa3075d148..e130692db24a 100644
--- a/include/asm-generic/pgtable.h
+++ b/include/asm-generic/pgtable.h
@@ -375,7 +375,8 @@ static inline int pte_unused(pte_t pte)
#endif

#ifndef __HAVE_ARCH_PMD_SAME
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
+ defined(CONFIG_SPECULATIVE_PAGE_FAULT)
static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
{
return pmd_val(pmd_a) == pmd_val(pmd_b);

?

Daniel


[*] The errors are:

In file included from /home/dmjordan/src/linux/include/linux/kernel.h:10:0,
from /home/dmjordan/src/linux/include/linux/list.h:9,
from /home/dmjordan/src/linux/include/linux/smp.h:12,
from /home/dmjordan/src/linux/include/linux/kernel_stat.h:5,
from /home/dmjordan/src/linux/mm/memory.c:41:
In function ‘pmd_same.isra.104’,
inlined from ‘pte_map_lock’ at /home/dmjordan/src/linux/mm/memory.c:2380:7:
/home/dmjordan/src/linux/include/linux/compiler.h:324:38: error: call to ‘__compiletime_assert_391’ declared with attribute error: BUILD_BUG failed
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
^
/home/dmjordan/src/linux/include/linux/compiler.h:304:4: note: in definition of macro ‘__compiletime_assert’
prefix ## suffix(); \
^~~~~~
/home/dmjordan/src/linux/include/linux/compiler.h:324:2: note: in expansion of macro ‘_compiletime_assert’
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
^~~~~~~~~~~~~~~~~~~
/home/dmjordan/src/linux/include/linux/build_bug.h:45:37: note: in expansion of macro ‘compiletime_assert’
#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
^~~~~~~~~~~~~~~~~~
/home/dmjordan/src/linux/include/linux/build_bug.h:79:21: note: in expansion of macro ‘BUILD_BUG_ON_MSG’
#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
^~~~~~~~~~~~~~~~
/home/dmjordan/src/linux/include/asm-generic/pgtable.h:391:2: note: in expansion of macro ‘BUILD_BUG’
BUILD_BUG();
^~~~~~~~~
CC block/elevator.o
CC crypto/crypto_wq.o
In function ‘pmd_same.isra.104’,
inlined from ‘pte_spinlock’ at /home/dmjordan/src/linux/mm/memory.c:2326:7,
inlined from ‘handle_pte_fault’ at /home/dmjordan/src/linux/mm/memory.c:4181:7:
/home/dmjordan/src/linux/include/linux/compiler.h:324:38: error: call to ‘__compiletime_assert_391’ declared with attribute error: BUILD_BUG failed
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
^
/home/dmjordan/src/linux/include/linux/compiler.h:304:4: note: in definition of macro ‘__compiletime_assert’
prefix ## suffix(); \
^~~~~~
/home/dmjordan/src/linux/include/linux/compiler.h:324:2: note: in expansion of macro ‘_compiletime_assert’
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
^~~~~~~~~~~~~~~~~~~
/home/dmjordan/src/linux/include/linux/build_bug.h:45:37: note: in expansion of macro ‘compiletime_assert’
#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
^~~~~~~~~~~~~~~~~~
/home/dmjordan/src/linux/include/linux/build_bug.h:79:21: note: in expansion of macro ‘BUILD_BUG_ON_MSG’
#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
^~~~~~~~~~~~~~~~
/home/dmjordan/src/linux/include/asm-generic/pgtable.h:391:2: note: in expansion of macro ‘BUILD_BUG’
BUILD_BUG();
^~~~~~~~~
...
make[2]: *** [/home/dmjordan/src/linux/scripts/Makefile.build:316: mm/memory.o] Error 1
make[1]: *** [/home/dmjordan/src/linux/Makefile:1047: mm] Error 2


2018-03-05 10:51:43

by Laurent Dufour

[permalink] [raw]
Subject: Re: [PATCH v8 18/24] mm: Provide speculative fault infrastructure

Hi Jordan,

Thanks for reporting this.

On 26/02/2018 18:16, Daniel Jordan wrote:
> Hi Laurent,
>
> This series doesn't build for me[*] when CONFIG_TRANSPARENT_HUGEPAGE is unset.
>
> The problem seems to be that the BUILD_BUG() version of pmd_same is called in
> pte_map_lock:
>
> On 02/16/2018 10:25 AM, Laurent Dufour wrote:
>> +static bool pte_map_lock(struct vm_fault *vmf)
>> +{
> ...snip...
>> +    if (!pmd_same(pmdval, vmf->orig_pmd))
>> +        goto out;
>
> Since SPF can now call pmd_same without THP, maybe the way to fix it is just
>
> diff --git a/include/asm-generic/pgtable.h b/include/asm-generic/pgtable.h
> index 2cfa3075d148..e130692db24a 100644
> --- a/include/asm-generic/pgtable.h
> +++ b/include/asm-generic/pgtable.h
> @@ -375,7 +375,8 @@ static inline int pte_unused(pte_t pte)
>  #endif
>  
>  #ifndef __HAVE_ARCH_PMD_SAME
> -#ifdef CONFIG_TRANSPARENT_HUGEPAGE
> +#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
> +    defined(CONFIG_SPECULATIVE_PAGE_FAULT)
>  static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
>  {
>         return pmd_val(pmd_a) == pmd_val(pmd_b);

We can't fix that this way because some architectures define their own
pmd_same() function (like ppc64), thus forcing the define here will be useless
since __HAVE_ARCH_PMD_SAME is set in that case.

The right way to fix that is to _not check_ for the PMD value in pte_spinlock()
when CONFIG_TRANSPARENT_HUGEPAGE is not set since there is no risk of
collapsing operation in our back if THP are disabled.

I'll fix that in the next version.

Laurent.

> ?
>
> Daniel
>
>
> [*]  The errors are:
>
> In file included from /home/dmjordan/src/linux/include/linux/kernel.h:10:0,
>                  from /home/dmjordan/src/linux/include/linux/list.h:9,
>                  from /home/dmjordan/src/linux/include/linux/smp.h:12,
>                  from /home/dmjordan/src/linux/include/linux/kernel_stat.h:5,
>                  from /home/dmjordan/src/linux/mm/memory.c:41:
> In function ‘pmd_same.isra.104’,
>     inlined from ‘pte_map_lock’ at /home/dmjordan/src/linux/mm/memory.c:2380:7:
> /home/dmjordan/src/linux/include/linux/compiler.h:324:38: error: call to
> ‘__compiletime_assert_391’ declared with attribute error: BUILD_BUG failed
>   _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
>                                       ^
> /home/dmjordan/src/linux/include/linux/compiler.h:304:4: note: in definition of
> macro ‘__compiletime_assert’
>     prefix ## suffix();    \
>     ^~~~~~
> /home/dmjordan/src/linux/include/linux/compiler.h:324:2: note: in expansion of
> macro ‘_compiletime_assert’
>   _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
>   ^~~~~~~~~~~~~~~~~~~
> /home/dmjordan/src/linux/include/linux/build_bug.h:45:37: note: in expansion of
> macro ‘compiletime_assert’
>  #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
>                                      ^~~~~~~~~~~~~~~~~~
> /home/dmjordan/src/linux/include/linux/build_bug.h:79:21: note: in expansion of
> macro ‘BUILD_BUG_ON_MSG’
>  #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
>                      ^~~~~~~~~~~~~~~~
> /home/dmjordan/src/linux/include/asm-generic/pgtable.h:391:2: note: in
> expansion of macro ‘BUILD_BUG’
>   BUILD_BUG();
>   ^~~~~~~~~
>   CC      block/elevator.o
>   CC      crypto/crypto_wq.o
> In function ‘pmd_same.isra.104’,
>     inlined from ‘pte_spinlock’ at /home/dmjordan/src/linux/mm/memory.c:2326:7,
>     inlined from ‘handle_pte_fault’ at
> /home/dmjordan/src/linux/mm/memory.c:4181:7:
> /home/dmjordan/src/linux/include/linux/compiler.h:324:38: error: call to
> ‘__compiletime_assert_391’ declared with attribute error: BUILD_BUG failed
>   _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
>                                       ^
> /home/dmjordan/src/linux/include/linux/compiler.h:304:4: note: in definition of
> macro ‘__compiletime_assert’
>     prefix ## suffix();    \
>     ^~~~~~
> /home/dmjordan/src/linux/include/linux/compiler.h:324:2: note: in expansion of
> macro ‘_compiletime_assert’
>   _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
>   ^~~~~~~~~~~~~~~~~~~
> /home/dmjordan/src/linux/include/linux/build_bug.h:45:37: note: in expansion of
> macro ‘compiletime_assert’
>  #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
>                                      ^~~~~~~~~~~~~~~~~~
> /home/dmjordan/src/linux/include/linux/build_bug.h:79:21: note: in expansion of
> macro ‘BUILD_BUG_ON_MSG’
>  #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
>                      ^~~~~~~~~~~~~~~~
> /home/dmjordan/src/linux/include/asm-generic/pgtable.h:391:2: note: in
> expansion of macro ‘BUILD_BUG’
>   BUILD_BUG();
>   ^~~~~~~~~
> ...
> make[2]: *** [/home/dmjordan/src/linux/scripts/Makefile.build:316: mm/memory.o]
> Error 1
> make[1]: *** [/home/dmjordan/src/linux/Makefile:1047: mm] Error 2
>


2018-03-29 02:28:19

by Ganesh Mahendran

[permalink] [raw]
Subject: Re: [PATCH v8 22/24] mm: Speculative page fault handler return VMA

Hi, Laurent

2018-02-16 23:25 GMT+08:00 Laurent Dufour <[email protected]>:
> When the speculative page fault handler is returning VM_RETRY, there is a
> chance that VMA fetched without grabbing the mmap_sem can be reused by the
> legacy page fault handler. By reusing it, we avoid calling find_vma()
> again. To achieve, that we must ensure that the VMA structure will not be
> freed in our back. This is done by getting the reference on it (get_vma())
> and by assuming that the caller will call the new service
> can_reuse_spf_vma() once it has grabbed the mmap_sem.
>
> can_reuse_spf_vma() is first checking that the VMA is still in the RB tree
> , and then that the VMA's boundaries matched the passed address and release
> the reference on the VMA so that it can be freed if needed.
>
> In the case the VMA is freed, can_reuse_spf_vma() will have returned false
> as the VMA is no more in the RB tree.

when I applied this patch to arm64, I got a crash:

[ 6.088296] Unable to handle kernel NULL pointer dereference at
virtual address 00000000
[ 6.088307] pgd = ffffff9d67735000
[ 6.088313] [00000000] *pgd=00000001795e3003,
*pud=00000001795e3003, *pmd=0000000000000000
[ 6.088372] ------------[ cut here ]------------
[ 6.088377] Kernel BUG at ffffff9d64f65960 [verbose debug info unavailable]
[ 6.088384] Internal error: Oops - BUG: 96000045 [#1] PREEMPT SMP
[ 6.088389] BUG: Bad rss-counter state mm:ffffffe8f3861040 idx:0 val:90
[ 6.088393] BUG: Bad rss-counter state mm:ffffffe8f3861040 idx:1 val:58
[ 6.088398] Modules linked in:
[ 6.088408] CPU: 1 PID: 621 Comm: qseecomd Not tainted 4.4.78-perf+ #88
[ 6.088413] Hardware name: Qualcomm Technologies, Inc. SDM 636
PM660 + PM660L MTP E7S (DT)
[ 6.088419] task: ffffffe8f6208000 ti: ffffffe872a8c000 task.ti:
ffffffe872a8c000
[ 6.088432] PC is at __rb_erase_color+0x108/0x240
[ 6.088441] LR is at vma_interval_tree_remove+0x244/0x24c
[ 6.088447] pc : [<ffffff9d64f65960>] lr : [<ffffff9d64d9c2d8>]
pstate: 604001c5
[ 6.088451] sp : ffffffe872a8fa50
[ 6.088455] x29: ffffffe872a8fa50 x28: 0000000000000008
[ 6.088462] x27: 0000000000000009 x26: 0000000000000000
[ 6.088470] x25: ffffffe8f458fb80 x24: 000000768ff87000
[ 6.088477] x23: 0000000000000000 x22: 0000000000000000
[ 6.088484] x21: ffffff9d64d9be7c x20: ffffffe8f3ff0680
[ 6.088492] x19: ffffffe8f212e9b0 x18: 0000000000000074
[ 6.088499] x17: 0000000000000007 x16: 000000000000000e
[ 6.088507] x15: ffffff9d65c88000 x14: 0000000000000001
[ 6.088514] x13: 0000000000192d76 x12: 0000000000989680
[ 6.088521] x11: 00000000001fffff x10: ffffff9d661ded1b
[ 6.088528] x9 : 0000007691759000 x8 : 0000000007691759
[ 6.088535] x7 : 0000000000000000 x6 : ffffffe871ebada8
[ 6.088541] x5 : 00000000000000e1 x4 : ffffffe8f212e958
[ 6.088548] x3 : 00000000000000e9 x2 : 0000000000000000
[ 6.088555] x1 : ffffffe8f212f110 x0 : ffffffe8f212e9b1
[ 6.088564]
[ 6.088564] PC: 0xffffff9d64f65920:
[ 6.088568] 5920 f9000002 aa0103e0 aa1603e1 d63f02a0 aa1603e1
f9400822 f9000662 f9000833
[ 6.088590] 5940 1400003b f9400a61 f9400020 370002c0 f9400436
b2400260 f9000a76 f9000433
[ 6.088610] 5960 f90002c0 f9400260 f9000020 f9000261 f27ef400
54000100 f9400802 eb13005f
[ 6.088630] 5980 54000061 f9000801 14000004 f9000401 14000002
f9000281 aa1303e0 d63f02a0
[ 6.088652]
[ 6.088652] LR: 0xffffff9d64d9c298:
[ 6.088656] c298 f9403083 b4000083 f9400c63 eb03005f 9a832042
f9403883 eb02007f 540000a0
[ 6.088676] c2b8 f9003882 f9402c82 927ef442 b5fffd22 b4000080
f0ffffe2 9139f042 94072561
[ 6.088695] c2d8 a8c17bfd d65f03c0 a9bf7bfd 910003fd f9400003
d2800000 b40000e3 f9400c65
[ 6.088715] c2f8 d1016063 eb0100bf 54000063 aa0303e0 97fffef2
a8c17bfd d65f03c0 a9bf7bfd
[ 6.088735]
[ 6.088735] SP: 0xffffffe872a8fa10:
[ 6.088740] fa10 64d9c2d8 ffffff9d 72a8fa50 ffffffe8 64f65960
ffffff9d 604001c5 00000000
[ 6.088759] fa30 71d67d70 ffffffe8 71c281e8 ffffffe8 00000000
00000080 64daa90c ffffff9d
[ 6.088779] fa50 72a8fa90 ffffffe8 64d9c2d8 ffffff9d 71ebada8
ffffffe8 f3ff0678 ffffffe8
[ 6.088799] fa70 72a8fb80 ffffffe8 00000000 00000000 00000000
00000000 00000001 00000000
[ 6.088818]
[ 6.088823] Process qseecomd (pid: 621, stack limit = 0xffffffe872a8c028)
[ 6.088828] Call trace:
[ 6.088834] Exception stack(0xffffffe872a8f860 to 0xffffffe872a8f990)
[ 6.088841] f860: ffffffe8f212e9b0 0000008000000000
0000000082b37000 ffffff9d64f65960
[ 6.088848] f880: 00000000604001c5 ffffff9d672c8680
ffffff9d672c9c00 ffffff9d672d3ab7
[ 6.088855] f8a0: ffffffe872a8f8f0 ffffff9d64db9bfc
0000000000000000 ffffffe8f9402c00
[ 6.088861] f8c0: ffffffe872a8c000 0000000000000000
ffffffe872a8f920 ffffff9d64db9bfc
[ 6.088867] f8e0: 0000000000000000 ffffffe8f9402b00
ffffffe872a8fa10 ffffff9d64dba568
[ 6.088874] f900: ffffffbe61c759c0 ffffffe871d67d70
ffffffe8f9402c00 1de56fb006cba396
[ 6.088881] f920: ffffffe8f212e9b1 ffffffe8f212f110
0000000000000000 00000000000000e9
[ 6.088888] f940: ffffffe8f212e958 00000000000000e1
ffffffe871ebada8 0000000000000000
[ 6.088895] f960: 0000000007691759 0000007691759000
ffffff9d661ded1b 00000000001fffff
[ 6.088901] f980: 0000000000989680 0000000000192d76
[ 6.088908] [<ffffff9d64f65960>] __rb_erase_color+0x108/0x240
[ 6.088915] [<ffffff9d64d9c2d8>] vma_interval_tree_remove+0x244/0x24c
[ 6.088924] [<ffffff9d64da4b5c>] __remove_shared_vm_struct+0x74/0x88
[ 6.088930] [<ffffff9d64da52b8>] unlink_file_vma+0x40/0x54
[ 6.088937] [<ffffff9d64d9f928>] free_pgtables+0xb8/0xfc
[ 6.088945] [<ffffff9d64da6b84>] exit_mmap+0x78/0x13c
[ 6.088953] [<ffffff9d64c9f5f4>] mmput+0x40/0xe8
[ 6.088961] [<ffffff9d64ca5af0>] do_exit+0x3ac/0x8d8
[ 6.088966] [<ffffff9d64ca6090>] do_group_exit+0x44/0x9c
[ 6.088974] [<ffffff9d64cb10d0>] get_signal+0x4e8/0x524
[ 6.088981] [<ffffff9d64c87ea0>] do_signal+0xac/0x93c
[ 6.088989] [<ffffff9d64c88a0c>] do_notify_resume+0x18/0x58
[ 6.088995] [<ffffff9d64c83038>] work_pending+0x10/0x14
[ 6.089003] Code: f9400436 b2400260 f9000a76 f9000433 (f90002c0)
[ 6.089009] ---[ end trace 224ce5f97841b6a5 ]---
[ 6.110819] Kernel panic - not syncing: Fatal exception

Thanks.

>
> Signed-off-by: Laurent Dufour <[email protected]>
> ---
> include/linux/mm.h | 5 +-
> mm/memory.c | 136 +++++++++++++++++++++++++++++++++--------------------
> 2 files changed, 88 insertions(+), 53 deletions(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index c383a4e2ceb3..0cd31a37bb3d 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1355,7 +1355,10 @@ extern int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
> unsigned int flags);
> #ifdef CONFIG_SPECULATIVE_PAGE_FAULT
> extern int handle_speculative_fault(struct mm_struct *mm,
> - unsigned long address, unsigned int flags);
> + unsigned long address, unsigned int flags,
> + struct vm_area_struct **vma);
> +extern bool can_reuse_spf_vma(struct vm_area_struct *vma,
> + unsigned long address);
> #endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
> extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
> unsigned long address, unsigned int fault_flags,
> diff --git a/mm/memory.c b/mm/memory.c
> index 2ef686405154..1f5ce5ff79af 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4307,13 +4307,22 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
> /* This is required by vm_normal_page() */
> #error "Speculative page fault handler requires __HAVE_ARCH_PTE_SPECIAL"
> #endif
> -
> /*
> * vm_normal_page() adds some processing which should be done while
> * hodling the mmap_sem.
> */
> +
> +/*
> + * Tries to handle the page fault in a speculative way, without grabbing the
> + * mmap_sem.
> + * When VM_FAULT_RETRY is returned, the vma pointer is valid and this vma must
> + * be checked later when the mmap_sem has been grabbed by calling
> + * can_reuse_spf_vma().
> + * This is needed as the returned vma is kept in memory until the call to
> + * can_reuse_spf_vma() is made.
> + */
> int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
> - unsigned int flags)
> + unsigned int flags, struct vm_area_struct **vma)
> {
> struct vm_fault vmf = {
> .address = address,
> @@ -4322,7 +4331,6 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
> p4d_t *p4d, p4dval;
> pud_t pudval;
> int seq, ret = VM_FAULT_RETRY;
> - struct vm_area_struct *vma;
> #ifdef CONFIG_NUMA
> struct mempolicy *pol;
> #endif
> @@ -4331,14 +4339,16 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
> flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
> flags |= FAULT_FLAG_SPECULATIVE;
>
> - vma = get_vma(mm, address);
> - if (!vma)
> + *vma = get_vma(mm, address);
> + if (!*vma)
> return ret;
> + vmf.vma = *vma;
>
> - seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
> + /* rmb <-> seqlock,vma_rb_erase() */
> + seq = raw_read_seqcount(&vmf.vma->vm_sequence);
> if (seq & 1) {
> - trace_spf_vma_changed(_RET_IP_, vma, address);
> - goto out_put;
> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
> + return ret;
> }
>
> /*
> @@ -4346,9 +4356,9 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
> * with the VMA.
> * This include huge page from hugetlbfs.
> */
> - if (vma->vm_ops) {
> - trace_spf_vma_notsup(_RET_IP_, vma, address);
> - goto out_put;
> + if (vmf.vma->vm_ops) {
> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
> + return ret;
> }
>
> /*
> @@ -4356,18 +4366,18 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
> * because vm_next and vm_prev must be safe. This can't be guaranteed
> * in the speculative path.
> */
> - if (unlikely(!vma->anon_vma)) {
> - trace_spf_vma_notsup(_RET_IP_, vma, address);
> - goto out_put;
> + if (unlikely(!vmf.vma->anon_vma)) {
> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
> + return ret;
> }
>
> - vmf.vma_flags = READ_ONCE(vma->vm_flags);
> - vmf.vma_page_prot = READ_ONCE(vma->vm_page_prot);
> + vmf.vma_flags = READ_ONCE(vmf.vma->vm_flags);
> + vmf.vma_page_prot = READ_ONCE(vmf.vma->vm_page_prot);
>
> /* Can't call userland page fault handler in the speculative path */
> if (unlikely(vmf.vma_flags & VM_UFFD_MISSING)) {
> - trace_spf_vma_notsup(_RET_IP_, vma, address);
> - goto out_put;
> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
> + return ret;
> }
>
> if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP) {
> @@ -4376,48 +4386,39 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
> * boundaries but we want to trace it as not supported instead
> * of changed.
> */
> - trace_spf_vma_notsup(_RET_IP_, vma, address);
> - goto out_put;
> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
> + return ret;
> }
>
> - if (address < READ_ONCE(vma->vm_start)
> - || READ_ONCE(vma->vm_end) <= address) {
> - trace_spf_vma_changed(_RET_IP_, vma, address);
> - goto out_put;
> + if (address < READ_ONCE(vmf.vma->vm_start)
> + || READ_ONCE(vmf.vma->vm_end) <= address) {
> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
> + return ret;
> }
>
> - if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
> + if (!arch_vma_access_permitted(vmf.vma, flags & FAULT_FLAG_WRITE,
> flags & FAULT_FLAG_INSTRUCTION,
> - flags & FAULT_FLAG_REMOTE)) {
> - trace_spf_vma_access(_RET_IP_, vma, address);
> - ret = VM_FAULT_SIGSEGV;
> - goto out_put;
> - }
> + flags & FAULT_FLAG_REMOTE))
> + goto out_segv;
>
> /* 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))) {
> - trace_spf_vma_access(_RET_IP_, vma, address);
> - ret = VM_FAULT_SIGSEGV;
> - goto out_put;
> - }
> - } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE)))) {
> - trace_spf_vma_access(_RET_IP_, vma, address);
> - ret = VM_FAULT_SIGSEGV;
> - goto out_put;
> - }
> + if (unlikely(!(vmf.vma_flags & VM_WRITE)))
> + goto out_segv;
> + } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE))))
> + goto out_segv;
>
> #ifdef CONFIG_NUMA
> /*
> * MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
> * are not compatible with the speculative page fault processing.
> */
> - pol = __get_vma_policy(vma, address);
> + pol = __get_vma_policy(vmf.vma, address);
> if (!pol)
> pol = get_task_policy(current);
> if (pol && pol->mode == MPOL_INTERLEAVE) {
> - trace_spf_vma_notsup(_RET_IP_, vma, address);
> - goto out_put;
> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
> + return ret;
> }
> #endif
>
> @@ -4479,9 +4480,8 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
> vmf.pte = NULL;
> }
>
> - vmf.vma = vma;
> - vmf.pgoff = linear_page_index(vma, address);
> - vmf.gfp_mask = __get_fault_gfp_mask(vma);
> + vmf.pgoff = linear_page_index(vmf.vma, address);
> + vmf.gfp_mask = __get_fault_gfp_mask(vmf.vma);
> vmf.sequence = seq;
> vmf.flags = flags;
>
> @@ -4491,16 +4491,22 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
> * 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)) {
> - trace_spf_vma_changed(_RET_IP_, vma, address);
> - goto out_put;
> + if (read_seqcount_retry(&vmf.vma->vm_sequence, seq)) {
> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
> + return ret;
> }
>
> mem_cgroup_oom_enable();
> ret = handle_pte_fault(&vmf);
> mem_cgroup_oom_disable();
>
> - put_vma(vma);
> + /*
> + * If there is no need to retry, don't return the vma to the caller.
> + */
> + if (!(ret & VM_FAULT_RETRY)) {
> + put_vma(vmf.vma);
> + *vma = NULL;
> + }
>
> /*
> * The task may have entered a memcg OOM situation but
> @@ -4513,9 +4519,35 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
> return ret;
>
> out_walk:
> - trace_spf_vma_notsup(_RET_IP_, vma, address);
> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
> local_irq_enable();
> -out_put:
> + return ret;
> +
> +out_segv:
> + trace_spf_vma_access(_RET_IP_, vmf.vma, address);
> + /*
> + * We don't return VM_FAULT_RETRY so the caller is not expected to
> + * retrieve the fetched VMA.
> + */
> + put_vma(vmf.vma);
> + *vma = NULL;
> + return VM_FAULT_SIGSEGV;
> +}
> +
> +/*
> + * This is used to know if the vma fetch in the speculative page fault handler
> + * is still valid when trying the regular fault path while holding the
> + * mmap_sem.
> + * The call to put_vma(vma) must be made after checking the vma's fields, as
> + * the vma may be freed by put_vma(). In such a case it is expected that false
> + * is returned.
> + */
> +bool can_reuse_spf_vma(struct vm_area_struct *vma, unsigned long address)
> +{
> + bool ret;
> +
> + ret = !RB_EMPTY_NODE(&vma->vm_rb) &&
> + vma->vm_start <= address && address < vma->vm_end;
> put_vma(vma);
> return ret;
> }
> --
> 2.7.4
>

2018-03-29 03:07:12

by Ganesh Mahendran

[permalink] [raw]
Subject: Re: [PATCH v8 22/24] mm: Speculative page fault handler return VMA

2018-03-29 10:26 GMT+08:00 Ganesh Mahendran <[email protected]>:
> Hi, Laurent
>
> 2018-02-16 23:25 GMT+08:00 Laurent Dufour <[email protected]>:
>> When the speculative page fault handler is returning VM_RETRY, there is a
>> chance that VMA fetched without grabbing the mmap_sem can be reused by the
>> legacy page fault handler. By reusing it, we avoid calling find_vma()
>> again. To achieve, that we must ensure that the VMA structure will not be
>> freed in our back. This is done by getting the reference on it (get_vma())
>> and by assuming that the caller will call the new service
>> can_reuse_spf_vma() once it has grabbed the mmap_sem.
>>
>> can_reuse_spf_vma() is first checking that the VMA is still in the RB tree
>> , and then that the VMA's boundaries matched the passed address and release
>> the reference on the VMA so that it can be freed if needed.
>>
>> In the case the VMA is freed, can_reuse_spf_vma() will have returned false
>> as the VMA is no more in the RB tree.
>
> when I applied this patch to arm64, I got a crash:
>
> [ 6.088296] Unable to handle kernel NULL pointer dereference at
> virtual address 00000000
> [ 6.088307] pgd = ffffff9d67735000
> [ 6.088313] [00000000] *pgd=00000001795e3003,
> *pud=00000001795e3003, *pmd=0000000000000000
> [ 6.088372] ------------[ cut here ]------------
> [ 6.088377] Kernel BUG at ffffff9d64f65960 [verbose debug info unavailable]
> [ 6.088384] Internal error: Oops - BUG: 96000045 [#1] PREEMPT SMP
> [ 6.088389] BUG: Bad rss-counter state mm:ffffffe8f3861040 idx:0 val:90
> [ 6.088393] BUG: Bad rss-counter state mm:ffffffe8f3861040 idx:1 val:58
> [ 6.088398] Modules linked in:
> [ 6.088408] CPU: 1 PID: 621 Comm: qseecomd Not tainted 4.4.78-perf+ #88
> [ 6.088413] Hardware name: Qualcomm Technologies, Inc. SDM 636
> PM660 + PM660L MTP E7S (DT)
> [ 6.088419] task: ffffffe8f6208000 ti: ffffffe872a8c000 task.ti:
> ffffffe872a8c000
> [ 6.088432] PC is at __rb_erase_color+0x108/0x240
> [ 6.088441] LR is at vma_interval_tree_remove+0x244/0x24c
> [ 6.088447] pc : [<ffffff9d64f65960>] lr : [<ffffff9d64d9c2d8>]
> pstate: 604001c5
> [ 6.088451] sp : ffffffe872a8fa50
> [ 6.088455] x29: ffffffe872a8fa50 x28: 0000000000000008
> [ 6.088462] x27: 0000000000000009 x26: 0000000000000000
> [ 6.088470] x25: ffffffe8f458fb80 x24: 000000768ff87000
> [ 6.088477] x23: 0000000000000000 x22: 0000000000000000
> [ 6.088484] x21: ffffff9d64d9be7c x20: ffffffe8f3ff0680
> [ 6.088492] x19: ffffffe8f212e9b0 x18: 0000000000000074
> [ 6.088499] x17: 0000000000000007 x16: 000000000000000e
> [ 6.088507] x15: ffffff9d65c88000 x14: 0000000000000001
> [ 6.088514] x13: 0000000000192d76 x12: 0000000000989680
> [ 6.088521] x11: 00000000001fffff x10: ffffff9d661ded1b
> [ 6.088528] x9 : 0000007691759000 x8 : 0000000007691759
> [ 6.088535] x7 : 0000000000000000 x6 : ffffffe871ebada8
> [ 6.088541] x5 : 00000000000000e1 x4 : ffffffe8f212e958
> [ 6.088548] x3 : 00000000000000e9 x2 : 0000000000000000
> [ 6.088555] x1 : ffffffe8f212f110 x0 : ffffffe8f212e9b1
> [ 6.088564]
> [ 6.088564] PC: 0xffffff9d64f65920:
> [ 6.088568] 5920 f9000002 aa0103e0 aa1603e1 d63f02a0 aa1603e1
> f9400822 f9000662 f9000833
> [ 6.088590] 5940 1400003b f9400a61 f9400020 370002c0 f9400436
> b2400260 f9000a76 f9000433
> [ 6.088610] 5960 f90002c0 f9400260 f9000020 f9000261 f27ef400
> 54000100 f9400802 eb13005f
> [ 6.088630] 5980 54000061 f9000801 14000004 f9000401 14000002
> f9000281 aa1303e0 d63f02a0
> [ 6.088652]
> [ 6.088652] LR: 0xffffff9d64d9c298:
> [ 6.088656] c298 f9403083 b4000083 f9400c63 eb03005f 9a832042
> f9403883 eb02007f 540000a0
> [ 6.088676] c2b8 f9003882 f9402c82 927ef442 b5fffd22 b4000080
> f0ffffe2 9139f042 94072561
> [ 6.088695] c2d8 a8c17bfd d65f03c0 a9bf7bfd 910003fd f9400003
> d2800000 b40000e3 f9400c65
> [ 6.088715] c2f8 d1016063 eb0100bf 54000063 aa0303e0 97fffef2
> a8c17bfd d65f03c0 a9bf7bfd
> [ 6.088735]
> [ 6.088735] SP: 0xffffffe872a8fa10:
> [ 6.088740] fa10 64d9c2d8 ffffff9d 72a8fa50 ffffffe8 64f65960
> ffffff9d 604001c5 00000000
> [ 6.088759] fa30 71d67d70 ffffffe8 71c281e8 ffffffe8 00000000
> 00000080 64daa90c ffffff9d
> [ 6.088779] fa50 72a8fa90 ffffffe8 64d9c2d8 ffffff9d 71ebada8
> ffffffe8 f3ff0678 ffffffe8
> [ 6.088799] fa70 72a8fb80 ffffffe8 00000000 00000000 00000000
> 00000000 00000001 00000000
> [ 6.088818]
> [ 6.088823] Process qseecomd (pid: 621, stack limit = 0xffffffe872a8c028)
> [ 6.088828] Call trace:
> [ 6.088834] Exception stack(0xffffffe872a8f860 to 0xffffffe872a8f990)
> [ 6.088841] f860: ffffffe8f212e9b0 0000008000000000
> 0000000082b37000 ffffff9d64f65960
> [ 6.088848] f880: 00000000604001c5 ffffff9d672c8680
> ffffff9d672c9c00 ffffff9d672d3ab7
> [ 6.088855] f8a0: ffffffe872a8f8f0 ffffff9d64db9bfc
> 0000000000000000 ffffffe8f9402c00
> [ 6.088861] f8c0: ffffffe872a8c000 0000000000000000
> ffffffe872a8f920 ffffff9d64db9bfc
> [ 6.088867] f8e0: 0000000000000000 ffffffe8f9402b00
> ffffffe872a8fa10 ffffff9d64dba568
> [ 6.088874] f900: ffffffbe61c759c0 ffffffe871d67d70
> ffffffe8f9402c00 1de56fb006cba396
> [ 6.088881] f920: ffffffe8f212e9b1 ffffffe8f212f110
> 0000000000000000 00000000000000e9
> [ 6.088888] f940: ffffffe8f212e958 00000000000000e1
> ffffffe871ebada8 0000000000000000
> [ 6.088895] f960: 0000000007691759 0000007691759000
> ffffff9d661ded1b 00000000001fffff
> [ 6.088901] f980: 0000000000989680 0000000000192d76
> [ 6.088908] [<ffffff9d64f65960>] __rb_erase_color+0x108/0x240
> [ 6.088915] [<ffffff9d64d9c2d8>] vma_interval_tree_remove+0x244/0x24c
> [ 6.088924] [<ffffff9d64da4b5c>] __remove_shared_vm_struct+0x74/0x88
> [ 6.088930] [<ffffff9d64da52b8>] unlink_file_vma+0x40/0x54
> [ 6.088937] [<ffffff9d64d9f928>] free_pgtables+0xb8/0xfc
> [ 6.088945] [<ffffff9d64da6b84>] exit_mmap+0x78/0x13c
> [ 6.088953] [<ffffff9d64c9f5f4>] mmput+0x40/0xe8
> [ 6.088961] [<ffffff9d64ca5af0>] do_exit+0x3ac/0x8d8
> [ 6.088966] [<ffffff9d64ca6090>] do_group_exit+0x44/0x9c
> [ 6.088974] [<ffffff9d64cb10d0>] get_signal+0x4e8/0x524
> [ 6.088981] [<ffffff9d64c87ea0>] do_signal+0xac/0x93c
> [ 6.088989] [<ffffff9d64c88a0c>] do_notify_resume+0x18/0x58
> [ 6.088995] [<ffffff9d64c83038>] work_pending+0x10/0x14
> [ 6.089003] Code: f9400436 b2400260 f9000a76 f9000433 (f90002c0)
> [ 6.089009] ---[ end trace 224ce5f97841b6a5 ]---
> [ 6.110819] Kernel panic - not syncing: Fatal exception
>
> Thanks.

Fixed by below patch:

diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index f6838c0..9c61b0e 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -240,18 +240,18 @@

static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
unsigned int mm_flags, unsigned long vm_flags,
- struct task_struct *tsk, struct vm_area_struct *spf_vma)
+ struct task_struct *tsk, struct vm_area_struct **spf_vma)
{
struct vm_area_struct *vma;
int fault;

#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
- if (spf_vma) {
- if (can_reuse_spf_vma(spf_vma, addr))
- vma = spf_vma;
+ if (*spf_vma) {
+ if (can_reuse_spf_vma(*spf_vma, addr))
+ vma = *spf_vma;
else
vma = find_vma(mm, addr);
- spf_vma = NULL;
+ *spf_vma = NULL;
} else
#endif
vma = find_vma(mm, addr);
@@ -393,7 +389,7 @@
#endif
}

- fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk, spf_vma);
+ fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk, &spf_vma);

/*
* If we need to retry but a fatal signal is pending, handle the
@@ -480,6 +476,11 @@
return 0;

no_context:
+ if (spf_vma) {
+ put_vma(spf_vma);
+ spf_vma = NULL;
+ }
+
__do_kernel_fault(mm, addr, esr, regs);
return 0;
}


>
>>
>> Signed-off-by: Laurent Dufour <[email protected]>
>> ---
>> include/linux/mm.h | 5 +-
>> mm/memory.c | 136 +++++++++++++++++++++++++++++++++--------------------
>> 2 files changed, 88 insertions(+), 53 deletions(-)
>>
>> diff --git a/include/linux/mm.h b/include/linux/mm.h
>> index c383a4e2ceb3..0cd31a37bb3d 100644
>> --- a/include/linux/mm.h
>> +++ b/include/linux/mm.h
>> @@ -1355,7 +1355,10 @@ extern int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
>> unsigned int flags);
>> #ifdef CONFIG_SPECULATIVE_PAGE_FAULT
>> extern int handle_speculative_fault(struct mm_struct *mm,
>> - unsigned long address, unsigned int flags);
>> + unsigned long address, unsigned int flags,
>> + struct vm_area_struct **vma);
>> +extern bool can_reuse_spf_vma(struct vm_area_struct *vma,
>> + unsigned long address);
>> #endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
>> extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
>> unsigned long address, unsigned int fault_flags,
>> diff --git a/mm/memory.c b/mm/memory.c
>> index 2ef686405154..1f5ce5ff79af 100644
>> --- a/mm/memory.c
>> +++ b/mm/memory.c
>> @@ -4307,13 +4307,22 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
>> /* This is required by vm_normal_page() */
>> #error "Speculative page fault handler requires __HAVE_ARCH_PTE_SPECIAL"
>> #endif
>> -
>> /*
>> * vm_normal_page() adds some processing which should be done while
>> * hodling the mmap_sem.
>> */
>> +
>> +/*
>> + * Tries to handle the page fault in a speculative way, without grabbing the
>> + * mmap_sem.
>> + * When VM_FAULT_RETRY is returned, the vma pointer is valid and this vma must
>> + * be checked later when the mmap_sem has been grabbed by calling
>> + * can_reuse_spf_vma().
>> + * This is needed as the returned vma is kept in memory until the call to
>> + * can_reuse_spf_vma() is made.
>> + */
>> int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>> - unsigned int flags)
>> + unsigned int flags, struct vm_area_struct **vma)
>> {
>> struct vm_fault vmf = {
>> .address = address,
>> @@ -4322,7 +4331,6 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>> p4d_t *p4d, p4dval;
>> pud_t pudval;
>> int seq, ret = VM_FAULT_RETRY;
>> - struct vm_area_struct *vma;
>> #ifdef CONFIG_NUMA
>> struct mempolicy *pol;
>> #endif
>> @@ -4331,14 +4339,16 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>> flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
>> flags |= FAULT_FLAG_SPECULATIVE;
>>
>> - vma = get_vma(mm, address);
>> - if (!vma)
>> + *vma = get_vma(mm, address);
>> + if (!*vma)
>> return ret;
>> + vmf.vma = *vma;
>>
>> - seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
>> + /* rmb <-> seqlock,vma_rb_erase() */
>> + seq = raw_read_seqcount(&vmf.vma->vm_sequence);
>> if (seq & 1) {
>> - trace_spf_vma_changed(_RET_IP_, vma, address);
>> - goto out_put;
>> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
>> + return ret;
>> }
>>
>> /*
>> @@ -4346,9 +4356,9 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>> * with the VMA.
>> * This include huge page from hugetlbfs.
>> */
>> - if (vma->vm_ops) {
>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>> - goto out_put;
>> + if (vmf.vma->vm_ops) {
>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>> + return ret;
>> }
>>
>> /*
>> @@ -4356,18 +4366,18 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>> * because vm_next and vm_prev must be safe. This can't be guaranteed
>> * in the speculative path.
>> */
>> - if (unlikely(!vma->anon_vma)) {
>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>> - goto out_put;
>> + if (unlikely(!vmf.vma->anon_vma)) {
>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>> + return ret;
>> }
>>
>> - vmf.vma_flags = READ_ONCE(vma->vm_flags);
>> - vmf.vma_page_prot = READ_ONCE(vma->vm_page_prot);
>> + vmf.vma_flags = READ_ONCE(vmf.vma->vm_flags);
>> + vmf.vma_page_prot = READ_ONCE(vmf.vma->vm_page_prot);
>>
>> /* Can't call userland page fault handler in the speculative path */
>> if (unlikely(vmf.vma_flags & VM_UFFD_MISSING)) {
>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>> - goto out_put;
>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>> + return ret;
>> }
>>
>> if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP) {
>> @@ -4376,48 +4386,39 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>> * boundaries but we want to trace it as not supported instead
>> * of changed.
>> */
>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>> - goto out_put;
>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>> + return ret;
>> }
>>
>> - if (address < READ_ONCE(vma->vm_start)
>> - || READ_ONCE(vma->vm_end) <= address) {
>> - trace_spf_vma_changed(_RET_IP_, vma, address);
>> - goto out_put;
>> + if (address < READ_ONCE(vmf.vma->vm_start)
>> + || READ_ONCE(vmf.vma->vm_end) <= address) {
>> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
>> + return ret;
>> }
>>
>> - if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
>> + if (!arch_vma_access_permitted(vmf.vma, flags & FAULT_FLAG_WRITE,
>> flags & FAULT_FLAG_INSTRUCTION,
>> - flags & FAULT_FLAG_REMOTE)) {
>> - trace_spf_vma_access(_RET_IP_, vma, address);
>> - ret = VM_FAULT_SIGSEGV;
>> - goto out_put;
>> - }
>> + flags & FAULT_FLAG_REMOTE))
>> + goto out_segv;
>>
>> /* 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))) {
>> - trace_spf_vma_access(_RET_IP_, vma, address);
>> - ret = VM_FAULT_SIGSEGV;
>> - goto out_put;
>> - }
>> - } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE)))) {
>> - trace_spf_vma_access(_RET_IP_, vma, address);
>> - ret = VM_FAULT_SIGSEGV;
>> - goto out_put;
>> - }
>> + if (unlikely(!(vmf.vma_flags & VM_WRITE)))
>> + goto out_segv;
>> + } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE))))
>> + goto out_segv;
>>
>> #ifdef CONFIG_NUMA
>> /*
>> * MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
>> * are not compatible with the speculative page fault processing.
>> */
>> - pol = __get_vma_policy(vma, address);
>> + pol = __get_vma_policy(vmf.vma, address);
>> if (!pol)
>> pol = get_task_policy(current);
>> if (pol && pol->mode == MPOL_INTERLEAVE) {
>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>> - goto out_put;
>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>> + return ret;
>> }
>> #endif
>>
>> @@ -4479,9 +4480,8 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>> vmf.pte = NULL;
>> }
>>
>> - vmf.vma = vma;
>> - vmf.pgoff = linear_page_index(vma, address);
>> - vmf.gfp_mask = __get_fault_gfp_mask(vma);
>> + vmf.pgoff = linear_page_index(vmf.vma, address);
>> + vmf.gfp_mask = __get_fault_gfp_mask(vmf.vma);
>> vmf.sequence = seq;
>> vmf.flags = flags;
>>
>> @@ -4491,16 +4491,22 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>> * 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)) {
>> - trace_spf_vma_changed(_RET_IP_, vma, address);
>> - goto out_put;
>> + if (read_seqcount_retry(&vmf.vma->vm_sequence, seq)) {
>> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
>> + return ret;
>> }
>>
>> mem_cgroup_oom_enable();
>> ret = handle_pte_fault(&vmf);
>> mem_cgroup_oom_disable();
>>
>> - put_vma(vma);
>> + /*
>> + * If there is no need to retry, don't return the vma to the caller.
>> + */
>> + if (!(ret & VM_FAULT_RETRY)) {
>> + put_vma(vmf.vma);
>> + *vma = NULL;
>> + }
>>
>> /*
>> * The task may have entered a memcg OOM situation but
>> @@ -4513,9 +4519,35 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>> return ret;
>>
>> out_walk:
>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>> local_irq_enable();
>> -out_put:
>> + return ret;
>> +
>> +out_segv:
>> + trace_spf_vma_access(_RET_IP_, vmf.vma, address);
>> + /*
>> + * We don't return VM_FAULT_RETRY so the caller is not expected to
>> + * retrieve the fetched VMA.
>> + */
>> + put_vma(vmf.vma);
>> + *vma = NULL;
>> + return VM_FAULT_SIGSEGV;
>> +}
>> +
>> +/*
>> + * This is used to know if the vma fetch in the speculative page fault handler
>> + * is still valid when trying the regular fault path while holding the
>> + * mmap_sem.
>> + * The call to put_vma(vma) must be made after checking the vma's fields, as
>> + * the vma may be freed by put_vma(). In such a case it is expected that false
>> + * is returned.
>> + */
>> +bool can_reuse_spf_vma(struct vm_area_struct *vma, unsigned long address)
>> +{
>> + bool ret;
>> +
>> + ret = !RB_EMPTY_NODE(&vma->vm_rb) &&
>> + vma->vm_start <= address && address < vma->vm_end;
>> put_vma(vma);
>> return ret;
>> }
>> --
>> 2.7.4
>>

2018-03-29 07:52:16

by Laurent Dufour

[permalink] [raw]
Subject: Re: [PATCH v8 22/24] mm: Speculative page fault handler return VMA

On 29/03/2018 05:06, Ganesh Mahendran wrote:
> 2018-03-29 10:26 GMT+08:00 Ganesh Mahendran <[email protected]>:
>> Hi, Laurent
>>
>> 2018-02-16 23:25 GMT+08:00 Laurent Dufour <[email protected]>:
>>> When the speculative page fault handler is returning VM_RETRY, there is a
>>> chance that VMA fetched without grabbing the mmap_sem can be reused by the
>>> legacy page fault handler. By reusing it, we avoid calling find_vma()
>>> again. To achieve, that we must ensure that the VMA structure will not be
>>> freed in our back. This is done by getting the reference on it (get_vma())
>>> and by assuming that the caller will call the new service
>>> can_reuse_spf_vma() once it has grabbed the mmap_sem.
>>>
>>> can_reuse_spf_vma() is first checking that the VMA is still in the RB tree
>>> , and then that the VMA's boundaries matched the passed address and release
>>> the reference on the VMA so that it can be freed if needed.
>>>
>>> In the case the VMA is freed, can_reuse_spf_vma() will have returned false
>>> as the VMA is no more in the RB tree.
>>
>> when I applied this patch to arm64, I got a crash:

Hi Ganesh,

Glad to see that you're enabling it on arm64.

I didn't give this arch a try, so feel free to propose patches on top of the
SPF series for this, I'll do my best to give them updated.

>>
>> [ 6.088296] Unable to handle kernel NULL pointer dereference at
>> virtual address 00000000
>> [ 6.088307] pgd = ffffff9d67735000
>> [ 6.088313] [00000000] *pgd=00000001795e3003,
>> *pud=00000001795e3003, *pmd=0000000000000000
>> [ 6.088372] ------------[ cut here ]------------
>> [ 6.088377] Kernel BUG at ffffff9d64f65960 [verbose debug info unavailable]
>> [ 6.088384] Internal error: Oops - BUG: 96000045 [#1] PREEMPT SMP
>> [ 6.088389] BUG: Bad rss-counter state mm:ffffffe8f3861040 idx:0 val:90
>> [ 6.088393] BUG: Bad rss-counter state mm:ffffffe8f3861040 idx:1 val:58
>> [ 6.088398] Modules linked in:
>> [ 6.088408] CPU: 1 PID: 621 Comm: qseecomd Not tainted 4.4.78-perf+ #88
>> [ 6.088413] Hardware name: Qualcomm Technologies, Inc. SDM 636
>> PM660 + PM660L MTP E7S (DT)
>> [ 6.088419] task: ffffffe8f6208000 ti: ffffffe872a8c000 task.ti:
>> ffffffe872a8c000
>> [ 6.088432] PC is at __rb_erase_color+0x108/0x240
>> [ 6.088441] LR is at vma_interval_tree_remove+0x244/0x24c
>> [ 6.088447] pc : [<ffffff9d64f65960>] lr : [<ffffff9d64d9c2d8>]
>> pstate: 604001c5
>> [ 6.088451] sp : ffffffe872a8fa50
>> [ 6.088455] x29: ffffffe872a8fa50 x28: 0000000000000008
>> [ 6.088462] x27: 0000000000000009 x26: 0000000000000000
>> [ 6.088470] x25: ffffffe8f458fb80 x24: 000000768ff87000
>> [ 6.088477] x23: 0000000000000000 x22: 0000000000000000
>> [ 6.088484] x21: ffffff9d64d9be7c x20: ffffffe8f3ff0680
>> [ 6.088492] x19: ffffffe8f212e9b0 x18: 0000000000000074
>> [ 6.088499] x17: 0000000000000007 x16: 000000000000000e
>> [ 6.088507] x15: ffffff9d65c88000 x14: 0000000000000001
>> [ 6.088514] x13: 0000000000192d76 x12: 0000000000989680
>> [ 6.088521] x11: 00000000001fffff x10: ffffff9d661ded1b
>> [ 6.088528] x9 : 0000007691759000 x8 : 0000000007691759
>> [ 6.088535] x7 : 0000000000000000 x6 : ffffffe871ebada8
>> [ 6.088541] x5 : 00000000000000e1 x4 : ffffffe8f212e958
>> [ 6.088548] x3 : 00000000000000e9 x2 : 0000000000000000
>> [ 6.088555] x1 : ffffffe8f212f110 x0 : ffffffe8f212e9b1
>> [ 6.088564]
>> [ 6.088564] PC: 0xffffff9d64f65920:
>> [ 6.088568] 5920 f9000002 aa0103e0 aa1603e1 d63f02a0 aa1603e1
>> f9400822 f9000662 f9000833
>> [ 6.088590] 5940 1400003b f9400a61 f9400020 370002c0 f9400436
>> b2400260 f9000a76 f9000433
>> [ 6.088610] 5960 f90002c0 f9400260 f9000020 f9000261 f27ef400
>> 54000100 f9400802 eb13005f
>> [ 6.088630] 5980 54000061 f9000801 14000004 f9000401 14000002
>> f9000281 aa1303e0 d63f02a0
>> [ 6.088652]
>> [ 6.088652] LR: 0xffffff9d64d9c298:
>> [ 6.088656] c298 f9403083 b4000083 f9400c63 eb03005f 9a832042
>> f9403883 eb02007f 540000a0
>> [ 6.088676] c2b8 f9003882 f9402c82 927ef442 b5fffd22 b4000080
>> f0ffffe2 9139f042 94072561
>> [ 6.088695] c2d8 a8c17bfd d65f03c0 a9bf7bfd 910003fd f9400003
>> d2800000 b40000e3 f9400c65
>> [ 6.088715] c2f8 d1016063 eb0100bf 54000063 aa0303e0 97fffef2
>> a8c17bfd d65f03c0 a9bf7bfd
>> [ 6.088735]
>> [ 6.088735] SP: 0xffffffe872a8fa10:
>> [ 6.088740] fa10 64d9c2d8 ffffff9d 72a8fa50 ffffffe8 64f65960
>> ffffff9d 604001c5 00000000
>> [ 6.088759] fa30 71d67d70 ffffffe8 71c281e8 ffffffe8 00000000
>> 00000080 64daa90c ffffff9d
>> [ 6.088779] fa50 72a8fa90 ffffffe8 64d9c2d8 ffffff9d 71ebada8
>> ffffffe8 f3ff0678 ffffffe8
>> [ 6.088799] fa70 72a8fb80 ffffffe8 00000000 00000000 00000000
>> 00000000 00000001 00000000
>> [ 6.088818]
>> [ 6.088823] Process qseecomd (pid: 621, stack limit = 0xffffffe872a8c028)
>> [ 6.088828] Call trace:
>> [ 6.088834] Exception stack(0xffffffe872a8f860 to 0xffffffe872a8f990)
>> [ 6.088841] f860: ffffffe8f212e9b0 0000008000000000
>> 0000000082b37000 ffffff9d64f65960
>> [ 6.088848] f880: 00000000604001c5 ffffff9d672c8680
>> ffffff9d672c9c00 ffffff9d672d3ab7
>> [ 6.088855] f8a0: ffffffe872a8f8f0 ffffff9d64db9bfc
>> 0000000000000000 ffffffe8f9402c00
>> [ 6.088861] f8c0: ffffffe872a8c000 0000000000000000
>> ffffffe872a8f920 ffffff9d64db9bfc
>> [ 6.088867] f8e0: 0000000000000000 ffffffe8f9402b00
>> ffffffe872a8fa10 ffffff9d64dba568
>> [ 6.088874] f900: ffffffbe61c759c0 ffffffe871d67d70
>> ffffffe8f9402c00 1de56fb006cba396
>> [ 6.088881] f920: ffffffe8f212e9b1 ffffffe8f212f110
>> 0000000000000000 00000000000000e9
>> [ 6.088888] f940: ffffffe8f212e958 00000000000000e1
>> ffffffe871ebada8 0000000000000000
>> [ 6.088895] f960: 0000000007691759 0000007691759000
>> ffffff9d661ded1b 00000000001fffff
>> [ 6.088901] f980: 0000000000989680 0000000000192d76
>> [ 6.088908] [<ffffff9d64f65960>] __rb_erase_color+0x108/0x240
>> [ 6.088915] [<ffffff9d64d9c2d8>] vma_interval_tree_remove+0x244/0x24c
>> [ 6.088924] [<ffffff9d64da4b5c>] __remove_shared_vm_struct+0x74/0x88
>> [ 6.088930] [<ffffff9d64da52b8>] unlink_file_vma+0x40/0x54
>> [ 6.088937] [<ffffff9d64d9f928>] free_pgtables+0xb8/0xfc
>> [ 6.088945] [<ffffff9d64da6b84>] exit_mmap+0x78/0x13c
>> [ 6.088953] [<ffffff9d64c9f5f4>] mmput+0x40/0xe8
>> [ 6.088961] [<ffffff9d64ca5af0>] do_exit+0x3ac/0x8d8
>> [ 6.088966] [<ffffff9d64ca6090>] do_group_exit+0x44/0x9c
>> [ 6.088974] [<ffffff9d64cb10d0>] get_signal+0x4e8/0x524
>> [ 6.088981] [<ffffff9d64c87ea0>] do_signal+0xac/0x93c
>> [ 6.088989] [<ffffff9d64c88a0c>] do_notify_resume+0x18/0x58
>> [ 6.088995] [<ffffff9d64c83038>] work_pending+0x10/0x14
>> [ 6.089003] Code: f9400436 b2400260 f9000a76 f9000433 (f90002c0)
>> [ 6.089009] ---[ end trace 224ce5f97841b6a5 ]---
>> [ 6.110819] Kernel panic - not syncing: Fatal exception
>>
>> Thanks.
>
> Fixed by below patch:

I guess you don't have only the following patch to enable that feature on arm64...

Please send the complete set of patch so that I could review this.

Thanks,
Laurent.

>
> diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> index f6838c0..9c61b0e 100644
> --- a/arch/arm64/mm/fault.c
> +++ b/arch/arm64/mm/fault.c
> @@ -240,18 +240,18 @@
>
> static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
> unsigned int mm_flags, unsigned long vm_flags,
> - struct task_struct *tsk, struct vm_area_struct *spf_vma)
> + struct task_struct *tsk, struct vm_area_struct **spf_vma)
> {
> struct vm_area_struct *vma;
> int fault;
>
> #ifdef CONFIG_SPECULATIVE_PAGE_FAULT
> - if (spf_vma) {
> - if (can_reuse_spf_vma(spf_vma, addr))
> - vma = spf_vma;
> + if (*spf_vma) {
> + if (can_reuse_spf_vma(*spf_vma, addr))
> + vma = *spf_vma;
> else
> vma = find_vma(mm, addr);
> - spf_vma = NULL;
> + *spf_vma = NULL;
> } else
> #endif
> vma = find_vma(mm, addr);
> @@ -393,7 +389,7 @@
> #endif
> }
>
> - fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk, spf_vma);
> + fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk, &spf_vma);
>
> /*
> * If we need to retry but a fatal signal is pending, handle the
> @@ -480,6 +476,11 @@
> return 0;
>
> no_context:
> + if (spf_vma) {
> + put_vma(spf_vma);
> + spf_vma = NULL;
> + }
> +
> __do_kernel_fault(mm, addr, esr, regs);
> return 0;
> }
>
>
>>
>>>
>>> Signed-off-by: Laurent Dufour <[email protected]>
>>> ---
>>> include/linux/mm.h | 5 +-
>>> mm/memory.c | 136 +++++++++++++++++++++++++++++++++--------------------
>>> 2 files changed, 88 insertions(+), 53 deletions(-)
>>>
>>> diff --git a/include/linux/mm.h b/include/linux/mm.h
>>> index c383a4e2ceb3..0cd31a37bb3d 100644
>>> --- a/include/linux/mm.h
>>> +++ b/include/linux/mm.h
>>> @@ -1355,7 +1355,10 @@ extern int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
>>> unsigned int flags);
>>> #ifdef CONFIG_SPECULATIVE_PAGE_FAULT
>>> extern int handle_speculative_fault(struct mm_struct *mm,
>>> - unsigned long address, unsigned int flags);
>>> + unsigned long address, unsigned int flags,
>>> + struct vm_area_struct **vma);
>>> +extern bool can_reuse_spf_vma(struct vm_area_struct *vma,
>>> + unsigned long address);
>>> #endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
>>> extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
>>> unsigned long address, unsigned int fault_flags,
>>> diff --git a/mm/memory.c b/mm/memory.c
>>> index 2ef686405154..1f5ce5ff79af 100644
>>> --- a/mm/memory.c
>>> +++ b/mm/memory.c
>>> @@ -4307,13 +4307,22 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
>>> /* This is required by vm_normal_page() */
>>> #error "Speculative page fault handler requires __HAVE_ARCH_PTE_SPECIAL"
>>> #endif
>>> -
>>> /*
>>> * vm_normal_page() adds some processing which should be done while
>>> * hodling the mmap_sem.
>>> */
>>> +
>>> +/*
>>> + * Tries to handle the page fault in a speculative way, without grabbing the
>>> + * mmap_sem.
>>> + * When VM_FAULT_RETRY is returned, the vma pointer is valid and this vma must
>>> + * be checked later when the mmap_sem has been grabbed by calling
>>> + * can_reuse_spf_vma().
>>> + * This is needed as the returned vma is kept in memory until the call to
>>> + * can_reuse_spf_vma() is made.
>>> + */
>>> int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>> - unsigned int flags)
>>> + unsigned int flags, struct vm_area_struct **vma)
>>> {
>>> struct vm_fault vmf = {
>>> .address = address,
>>> @@ -4322,7 +4331,6 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>> p4d_t *p4d, p4dval;
>>> pud_t pudval;
>>> int seq, ret = VM_FAULT_RETRY;
>>> - struct vm_area_struct *vma;
>>> #ifdef CONFIG_NUMA
>>> struct mempolicy *pol;
>>> #endif
>>> @@ -4331,14 +4339,16 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>> flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
>>> flags |= FAULT_FLAG_SPECULATIVE;
>>>
>>> - vma = get_vma(mm, address);
>>> - if (!vma)
>>> + *vma = get_vma(mm, address);
>>> + if (!*vma)
>>> return ret;
>>> + vmf.vma = *vma;
>>>
>>> - seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
>>> + /* rmb <-> seqlock,vma_rb_erase() */
>>> + seq = raw_read_seqcount(&vmf.vma->vm_sequence);
>>> if (seq & 1) {
>>> - trace_spf_vma_changed(_RET_IP_, vma, address);
>>> - goto out_put;
>>> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
>>> + return ret;
>>> }
>>>
>>> /*
>>> @@ -4346,9 +4356,9 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>> * with the VMA.
>>> * This include huge page from hugetlbfs.
>>> */
>>> - if (vma->vm_ops) {
>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>> - goto out_put;
>>> + if (vmf.vma->vm_ops) {
>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>> + return ret;
>>> }
>>>
>>> /*
>>> @@ -4356,18 +4366,18 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>> * because vm_next and vm_prev must be safe. This can't be guaranteed
>>> * in the speculative path.
>>> */
>>> - if (unlikely(!vma->anon_vma)) {
>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>> - goto out_put;
>>> + if (unlikely(!vmf.vma->anon_vma)) {
>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>> + return ret;
>>> }
>>>
>>> - vmf.vma_flags = READ_ONCE(vma->vm_flags);
>>> - vmf.vma_page_prot = READ_ONCE(vma->vm_page_prot);
>>> + vmf.vma_flags = READ_ONCE(vmf.vma->vm_flags);
>>> + vmf.vma_page_prot = READ_ONCE(vmf.vma->vm_page_prot);
>>>
>>> /* Can't call userland page fault handler in the speculative path */
>>> if (unlikely(vmf.vma_flags & VM_UFFD_MISSING)) {
>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>> - goto out_put;
>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>> + return ret;
>>> }
>>>
>>> if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP) {
>>> @@ -4376,48 +4386,39 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>> * boundaries but we want to trace it as not supported instead
>>> * of changed.
>>> */
>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>> - goto out_put;
>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>> + return ret;
>>> }
>>>
>>> - if (address < READ_ONCE(vma->vm_start)
>>> - || READ_ONCE(vma->vm_end) <= address) {
>>> - trace_spf_vma_changed(_RET_IP_, vma, address);
>>> - goto out_put;
>>> + if (address < READ_ONCE(vmf.vma->vm_start)
>>> + || READ_ONCE(vmf.vma->vm_end) <= address) {
>>> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
>>> + return ret;
>>> }
>>>
>>> - if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
>>> + if (!arch_vma_access_permitted(vmf.vma, flags & FAULT_FLAG_WRITE,
>>> flags & FAULT_FLAG_INSTRUCTION,
>>> - flags & FAULT_FLAG_REMOTE)) {
>>> - trace_spf_vma_access(_RET_IP_, vma, address);
>>> - ret = VM_FAULT_SIGSEGV;
>>> - goto out_put;
>>> - }
>>> + flags & FAULT_FLAG_REMOTE))
>>> + goto out_segv;
>>>
>>> /* 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))) {
>>> - trace_spf_vma_access(_RET_IP_, vma, address);
>>> - ret = VM_FAULT_SIGSEGV;
>>> - goto out_put;
>>> - }
>>> - } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE)))) {
>>> - trace_spf_vma_access(_RET_IP_, vma, address);
>>> - ret = VM_FAULT_SIGSEGV;
>>> - goto out_put;
>>> - }
>>> + if (unlikely(!(vmf.vma_flags & VM_WRITE)))
>>> + goto out_segv;
>>> + } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE))))
>>> + goto out_segv;
>>>
>>> #ifdef CONFIG_NUMA
>>> /*
>>> * MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
>>> * are not compatible with the speculative page fault processing.
>>> */
>>> - pol = __get_vma_policy(vma, address);
>>> + pol = __get_vma_policy(vmf.vma, address);
>>> if (!pol)
>>> pol = get_task_policy(current);
>>> if (pol && pol->mode == MPOL_INTERLEAVE) {
>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>> - goto out_put;
>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>> + return ret;
>>> }
>>> #endif
>>>
>>> @@ -4479,9 +4480,8 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>> vmf.pte = NULL;
>>> }
>>>
>>> - vmf.vma = vma;
>>> - vmf.pgoff = linear_page_index(vma, address);
>>> - vmf.gfp_mask = __get_fault_gfp_mask(vma);
>>> + vmf.pgoff = linear_page_index(vmf.vma, address);
>>> + vmf.gfp_mask = __get_fault_gfp_mask(vmf.vma);
>>> vmf.sequence = seq;
>>> vmf.flags = flags;
>>>
>>> @@ -4491,16 +4491,22 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>> * 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)) {
>>> - trace_spf_vma_changed(_RET_IP_, vma, address);
>>> - goto out_put;
>>> + if (read_seqcount_retry(&vmf.vma->vm_sequence, seq)) {
>>> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
>>> + return ret;
>>> }
>>>
>>> mem_cgroup_oom_enable();
>>> ret = handle_pte_fault(&vmf);
>>> mem_cgroup_oom_disable();
>>>
>>> - put_vma(vma);
>>> + /*
>>> + * If there is no need to retry, don't return the vma to the caller.
>>> + */
>>> + if (!(ret & VM_FAULT_RETRY)) {
>>> + put_vma(vmf.vma);
>>> + *vma = NULL;
>>> + }
>>>
>>> /*
>>> * The task may have entered a memcg OOM situation but
>>> @@ -4513,9 +4519,35 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>> return ret;
>>>
>>> out_walk:
>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>> local_irq_enable();
>>> -out_put:
>>> + return ret;
>>> +
>>> +out_segv:
>>> + trace_spf_vma_access(_RET_IP_, vmf.vma, address);
>>> + /*
>>> + * We don't return VM_FAULT_RETRY so the caller is not expected to
>>> + * retrieve the fetched VMA.
>>> + */
>>> + put_vma(vmf.vma);
>>> + *vma = NULL;
>>> + return VM_FAULT_SIGSEGV;
>>> +}
>>> +
>>> +/*
>>> + * This is used to know if the vma fetch in the speculative page fault handler
>>> + * is still valid when trying the regular fault path while holding the
>>> + * mmap_sem.
>>> + * The call to put_vma(vma) must be made after checking the vma's fields, as
>>> + * the vma may be freed by put_vma(). In such a case it is expected that false
>>> + * is returned.
>>> + */
>>> +bool can_reuse_spf_vma(struct vm_area_struct *vma, unsigned long address)
>>> +{
>>> + bool ret;
>>> +
>>> + ret = !RB_EMPTY_NODE(&vma->vm_rb) &&
>>> + vma->vm_start <= address && address < vma->vm_end;
>>> put_vma(vma);
>>> return ret;
>>> }
>>> --
>>> 2.7.4
>>>
>


2018-05-02 08:11:36

by Ganesh Mahendran

[permalink] [raw]
Subject: Re: [PATCH v8 22/24] mm: Speculative page fault handler return VMA

2018-03-29 15:50 GMT+08:00 Laurent Dufour <[email protected]>:
> On 29/03/2018 05:06, Ganesh Mahendran wrote:
>> 2018-03-29 10:26 GMT+08:00 Ganesh Mahendran <[email protected]>:
>>> Hi, Laurent
>>>
>>> 2018-02-16 23:25 GMT+08:00 Laurent Dufour <[email protected]>:
>>>> When the speculative page fault handler is returning VM_RETRY, there is a
>>>> chance that VMA fetched without grabbing the mmap_sem can be reused by the
>>>> legacy page fault handler. By reusing it, we avoid calling find_vma()
>>>> again. To achieve, that we must ensure that the VMA structure will not be
>>>> freed in our back. This is done by getting the reference on it (get_vma())
>>>> and by assuming that the caller will call the new service
>>>> can_reuse_spf_vma() once it has grabbed the mmap_sem.
>>>>
>>>> can_reuse_spf_vma() is first checking that the VMA is still in the RB tree
>>>> , and then that the VMA's boundaries matched the passed address and release
>>>> the reference on the VMA so that it can be freed if needed.
>>>>
>>>> In the case the VMA is freed, can_reuse_spf_vma() will have returned false
>>>> as the VMA is no more in the RB tree.
>>>
>>> when I applied this patch to arm64, I got a crash:
>
> Hi Ganesh,
>
> Glad to see that you're enabling it on arm64.
>
> I didn't give this arch a try, so feel free to propose patches on top of the
> SPF series for this, I'll do my best to give them updated.
>
>>>
>>> [ 6.088296] Unable to handle kernel NULL pointer dereference at
>>> virtual address 00000000
>>> [ 6.088307] pgd = ffffff9d67735000
>>> [ 6.088313] [00000000] *pgd=00000001795e3003,
>>> *pud=00000001795e3003, *pmd=0000000000000000
>>> [ 6.088372] ------------[ cut here ]------------
>>> [ 6.088377] Kernel BUG at ffffff9d64f65960 [verbose debug info unavailable]
>>> [ 6.088384] Internal error: Oops - BUG: 96000045 [#1] PREEMPT SMP
>>> [ 6.088389] BUG: Bad rss-counter state mm:ffffffe8f3861040 idx:0 val:90
>>> [ 6.088393] BUG: Bad rss-counter state mm:ffffffe8f3861040 idx:1 val:58
>>> [ 6.088398] Modules linked in:
>>> [ 6.088408] CPU: 1 PID: 621 Comm: qseecomd Not tainted 4.4.78-perf+ #88
>>> [ 6.088413] Hardware name: Qualcomm Technologies, Inc. SDM 636
>>> PM660 + PM660L MTP E7S (DT)
>>> [ 6.088419] task: ffffffe8f6208000 ti: ffffffe872a8c000 task.ti:
>>> ffffffe872a8c000
>>> [ 6.088432] PC is at __rb_erase_color+0x108/0x240
>>> [ 6.088441] LR is at vma_interval_tree_remove+0x244/0x24c
>>> [ 6.088447] pc : [<ffffff9d64f65960>] lr : [<ffffff9d64d9c2d8>]
>>> pstate: 604001c5
>>> [ 6.088451] sp : ffffffe872a8fa50
>>> [ 6.088455] x29: ffffffe872a8fa50 x28: 0000000000000008
>>> [ 6.088462] x27: 0000000000000009 x26: 0000000000000000
>>> [ 6.088470] x25: ffffffe8f458fb80 x24: 000000768ff87000
>>> [ 6.088477] x23: 0000000000000000 x22: 0000000000000000
>>> [ 6.088484] x21: ffffff9d64d9be7c x20: ffffffe8f3ff0680
>>> [ 6.088492] x19: ffffffe8f212e9b0 x18: 0000000000000074
>>> [ 6.088499] x17: 0000000000000007 x16: 000000000000000e
>>> [ 6.088507] x15: ffffff9d65c88000 x14: 0000000000000001
>>> [ 6.088514] x13: 0000000000192d76 x12: 0000000000989680
>>> [ 6.088521] x11: 00000000001fffff x10: ffffff9d661ded1b
>>> [ 6.088528] x9 : 0000007691759000 x8 : 0000000007691759
>>> [ 6.088535] x7 : 0000000000000000 x6 : ffffffe871ebada8
>>> [ 6.088541] x5 : 00000000000000e1 x4 : ffffffe8f212e958
>>> [ 6.088548] x3 : 00000000000000e9 x2 : 0000000000000000
>>> [ 6.088555] x1 : ffffffe8f212f110 x0 : ffffffe8f212e9b1
>>> [ 6.088564]
>>> [ 6.088564] PC: 0xffffff9d64f65920:
>>> [ 6.088568] 5920 f9000002 aa0103e0 aa1603e1 d63f02a0 aa1603e1
>>> f9400822 f9000662 f9000833
>>> [ 6.088590] 5940 1400003b f9400a61 f9400020 370002c0 f9400436
>>> b2400260 f9000a76 f9000433
>>> [ 6.088610] 5960 f90002c0 f9400260 f9000020 f9000261 f27ef400
>>> 54000100 f9400802 eb13005f
>>> [ 6.088630] 5980 54000061 f9000801 14000004 f9000401 14000002
>>> f9000281 aa1303e0 d63f02a0
>>> [ 6.088652]
>>> [ 6.088652] LR: 0xffffff9d64d9c298:
>>> [ 6.088656] c298 f9403083 b4000083 f9400c63 eb03005f 9a832042
>>> f9403883 eb02007f 540000a0
>>> [ 6.088676] c2b8 f9003882 f9402c82 927ef442 b5fffd22 b4000080
>>> f0ffffe2 9139f042 94072561
>>> [ 6.088695] c2d8 a8c17bfd d65f03c0 a9bf7bfd 910003fd f9400003
>>> d2800000 b40000e3 f9400c65
>>> [ 6.088715] c2f8 d1016063 eb0100bf 54000063 aa0303e0 97fffef2
>>> a8c17bfd d65f03c0 a9bf7bfd
>>> [ 6.088735]
>>> [ 6.088735] SP: 0xffffffe872a8fa10:
>>> [ 6.088740] fa10 64d9c2d8 ffffff9d 72a8fa50 ffffffe8 64f65960
>>> ffffff9d 604001c5 00000000
>>> [ 6.088759] fa30 71d67d70 ffffffe8 71c281e8 ffffffe8 00000000
>>> 00000080 64daa90c ffffff9d
>>> [ 6.088779] fa50 72a8fa90 ffffffe8 64d9c2d8 ffffff9d 71ebada8
>>> ffffffe8 f3ff0678 ffffffe8
>>> [ 6.088799] fa70 72a8fb80 ffffffe8 00000000 00000000 00000000
>>> 00000000 00000001 00000000
>>> [ 6.088818]
>>> [ 6.088823] Process qseecomd (pid: 621, stack limit = 0xffffffe872a8c028)
>>> [ 6.088828] Call trace:
>>> [ 6.088834] Exception stack(0xffffffe872a8f860 to 0xffffffe872a8f990)
>>> [ 6.088841] f860: ffffffe8f212e9b0 0000008000000000
>>> 0000000082b37000 ffffff9d64f65960
>>> [ 6.088848] f880: 00000000604001c5 ffffff9d672c8680
>>> ffffff9d672c9c00 ffffff9d672d3ab7
>>> [ 6.088855] f8a0: ffffffe872a8f8f0 ffffff9d64db9bfc
>>> 0000000000000000 ffffffe8f9402c00
>>> [ 6.088861] f8c0: ffffffe872a8c000 0000000000000000
>>> ffffffe872a8f920 ffffff9d64db9bfc
>>> [ 6.088867] f8e0: 0000000000000000 ffffffe8f9402b00
>>> ffffffe872a8fa10 ffffff9d64dba568
>>> [ 6.088874] f900: ffffffbe61c759c0 ffffffe871d67d70
>>> ffffffe8f9402c00 1de56fb006cba396
>>> [ 6.088881] f920: ffffffe8f212e9b1 ffffffe8f212f110
>>> 0000000000000000 00000000000000e9
>>> [ 6.088888] f940: ffffffe8f212e958 00000000000000e1
>>> ffffffe871ebada8 0000000000000000
>>> [ 6.088895] f960: 0000000007691759 0000007691759000
>>> ffffff9d661ded1b 00000000001fffff
>>> [ 6.088901] f980: 0000000000989680 0000000000192d76
>>> [ 6.088908] [<ffffff9d64f65960>] __rb_erase_color+0x108/0x240
>>> [ 6.088915] [<ffffff9d64d9c2d8>] vma_interval_tree_remove+0x244/0x24c
>>> [ 6.088924] [<ffffff9d64da4b5c>] __remove_shared_vm_struct+0x74/0x88
>>> [ 6.088930] [<ffffff9d64da52b8>] unlink_file_vma+0x40/0x54
>>> [ 6.088937] [<ffffff9d64d9f928>] free_pgtables+0xb8/0xfc
>>> [ 6.088945] [<ffffff9d64da6b84>] exit_mmap+0x78/0x13c
>>> [ 6.088953] [<ffffff9d64c9f5f4>] mmput+0x40/0xe8
>>> [ 6.088961] [<ffffff9d64ca5af0>] do_exit+0x3ac/0x8d8
>>> [ 6.088966] [<ffffff9d64ca6090>] do_group_exit+0x44/0x9c
>>> [ 6.088974] [<ffffff9d64cb10d0>] get_signal+0x4e8/0x524
>>> [ 6.088981] [<ffffff9d64c87ea0>] do_signal+0xac/0x93c
>>> [ 6.088989] [<ffffff9d64c88a0c>] do_notify_resume+0x18/0x58
>>> [ 6.088995] [<ffffff9d64c83038>] work_pending+0x10/0x14
>>> [ 6.089003] Code: f9400436 b2400260 f9000a76 f9000433 (f90002c0)
>>> [ 6.089009] ---[ end trace 224ce5f97841b6a5 ]---
>>> [ 6.110819] Kernel panic - not syncing: Fatal exception
>>>
>>> Thanks.
>>
>> Fixed by below patch:
>
> I guess you don't have only the following patch to enable that feature on arm64...
>
> Please send the complete set of patch so that I could review this.

Hi, Laurent

We now use spf in our product. Thanks for your patches.

I send 2 patches to enable spf for arm64, they are on top of your v10 spf.
If ok, please add them in your next version.

Thanks.

>
> Thanks,
> Laurent.
>
>>
>> diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
>> index f6838c0..9c61b0e 100644
>> --- a/arch/arm64/mm/fault.c
>> +++ b/arch/arm64/mm/fault.c
>> @@ -240,18 +240,18 @@
>>
>> static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
>> unsigned int mm_flags, unsigned long vm_flags,
>> - struct task_struct *tsk, struct vm_area_struct *spf_vma)
>> + struct task_struct *tsk, struct vm_area_struct **spf_vma)
>> {
>> struct vm_area_struct *vma;
>> int fault;
>>
>> #ifdef CONFIG_SPECULATIVE_PAGE_FAULT
>> - if (spf_vma) {
>> - if (can_reuse_spf_vma(spf_vma, addr))
>> - vma = spf_vma;
>> + if (*spf_vma) {
>> + if (can_reuse_spf_vma(*spf_vma, addr))
>> + vma = *spf_vma;
>> else
>> vma = find_vma(mm, addr);
>> - spf_vma = NULL;
>> + *spf_vma = NULL;
>> } else
>> #endif
>> vma = find_vma(mm, addr);
>> @@ -393,7 +389,7 @@
>> #endif
>> }
>>
>> - fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk, spf_vma);
>> + fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk, &spf_vma);
>>
>> /*
>> * If we need to retry but a fatal signal is pending, handle the
>> @@ -480,6 +476,11 @@
>> return 0;
>>
>> no_context:
>> + if (spf_vma) {
>> + put_vma(spf_vma);
>> + spf_vma = NULL;
>> + }
>> +
>> __do_kernel_fault(mm, addr, esr, regs);
>> return 0;
>> }
>>
>>
>>>
>>>>
>>>> Signed-off-by: Laurent Dufour <[email protected]>
>>>> ---
>>>> include/linux/mm.h | 5 +-
>>>> mm/memory.c | 136 +++++++++++++++++++++++++++++++++--------------------
>>>> 2 files changed, 88 insertions(+), 53 deletions(-)
>>>>
>>>> diff --git a/include/linux/mm.h b/include/linux/mm.h
>>>> index c383a4e2ceb3..0cd31a37bb3d 100644
>>>> --- a/include/linux/mm.h
>>>> +++ b/include/linux/mm.h
>>>> @@ -1355,7 +1355,10 @@ extern int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
>>>> unsigned int flags);
>>>> #ifdef CONFIG_SPECULATIVE_PAGE_FAULT
>>>> extern int handle_speculative_fault(struct mm_struct *mm,
>>>> - unsigned long address, unsigned int flags);
>>>> + unsigned long address, unsigned int flags,
>>>> + struct vm_area_struct **vma);
>>>> +extern bool can_reuse_spf_vma(struct vm_area_struct *vma,
>>>> + unsigned long address);
>>>> #endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
>>>> extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
>>>> unsigned long address, unsigned int fault_flags,
>>>> diff --git a/mm/memory.c b/mm/memory.c
>>>> index 2ef686405154..1f5ce5ff79af 100644
>>>> --- a/mm/memory.c
>>>> +++ b/mm/memory.c
>>>> @@ -4307,13 +4307,22 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
>>>> /* This is required by vm_normal_page() */
>>>> #error "Speculative page fault handler requires __HAVE_ARCH_PTE_SPECIAL"
>>>> #endif
>>>> -
>>>> /*
>>>> * vm_normal_page() adds some processing which should be done while
>>>> * hodling the mmap_sem.
>>>> */
>>>> +
>>>> +/*
>>>> + * Tries to handle the page fault in a speculative way, without grabbing the
>>>> + * mmap_sem.
>>>> + * When VM_FAULT_RETRY is returned, the vma pointer is valid and this vma must
>>>> + * be checked later when the mmap_sem has been grabbed by calling
>>>> + * can_reuse_spf_vma().
>>>> + * This is needed as the returned vma is kept in memory until the call to
>>>> + * can_reuse_spf_vma() is made.
>>>> + */
>>>> int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>>> - unsigned int flags)
>>>> + unsigned int flags, struct vm_area_struct **vma)
>>>> {
>>>> struct vm_fault vmf = {
>>>> .address = address,
>>>> @@ -4322,7 +4331,6 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>>> p4d_t *p4d, p4dval;
>>>> pud_t pudval;
>>>> int seq, ret = VM_FAULT_RETRY;
>>>> - struct vm_area_struct *vma;
>>>> #ifdef CONFIG_NUMA
>>>> struct mempolicy *pol;
>>>> #endif
>>>> @@ -4331,14 +4339,16 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>>> flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
>>>> flags |= FAULT_FLAG_SPECULATIVE;
>>>>
>>>> - vma = get_vma(mm, address);
>>>> - if (!vma)
>>>> + *vma = get_vma(mm, address);
>>>> + if (!*vma)
>>>> return ret;
>>>> + vmf.vma = *vma;
>>>>
>>>> - seq = raw_read_seqcount(&vma->vm_sequence); /* rmb <-> seqlock,vma_rb_erase() */
>>>> + /* rmb <-> seqlock,vma_rb_erase() */
>>>> + seq = raw_read_seqcount(&vmf.vma->vm_sequence);
>>>> if (seq & 1) {
>>>> - trace_spf_vma_changed(_RET_IP_, vma, address);
>>>> - goto out_put;
>>>> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
>>>> + return ret;
>>>> }
>>>>
>>>> /*
>>>> @@ -4346,9 +4356,9 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>>> * with the VMA.
>>>> * This include huge page from hugetlbfs.
>>>> */
>>>> - if (vma->vm_ops) {
>>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>>> - goto out_put;
>>>> + if (vmf.vma->vm_ops) {
>>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>>> + return ret;
>>>> }
>>>>
>>>> /*
>>>> @@ -4356,18 +4366,18 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>>> * because vm_next and vm_prev must be safe. This can't be guaranteed
>>>> * in the speculative path.
>>>> */
>>>> - if (unlikely(!vma->anon_vma)) {
>>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>>> - goto out_put;
>>>> + if (unlikely(!vmf.vma->anon_vma)) {
>>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>>> + return ret;
>>>> }
>>>>
>>>> - vmf.vma_flags = READ_ONCE(vma->vm_flags);
>>>> - vmf.vma_page_prot = READ_ONCE(vma->vm_page_prot);
>>>> + vmf.vma_flags = READ_ONCE(vmf.vma->vm_flags);
>>>> + vmf.vma_page_prot = READ_ONCE(vmf.vma->vm_page_prot);
>>>>
>>>> /* Can't call userland page fault handler in the speculative path */
>>>> if (unlikely(vmf.vma_flags & VM_UFFD_MISSING)) {
>>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>>> - goto out_put;
>>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>>> + return ret;
>>>> }
>>>>
>>>> if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP) {
>>>> @@ -4376,48 +4386,39 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>>> * boundaries but we want to trace it as not supported instead
>>>> * of changed.
>>>> */
>>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>>> - goto out_put;
>>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>>> + return ret;
>>>> }
>>>>
>>>> - if (address < READ_ONCE(vma->vm_start)
>>>> - || READ_ONCE(vma->vm_end) <= address) {
>>>> - trace_spf_vma_changed(_RET_IP_, vma, address);
>>>> - goto out_put;
>>>> + if (address < READ_ONCE(vmf.vma->vm_start)
>>>> + || READ_ONCE(vmf.vma->vm_end) <= address) {
>>>> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
>>>> + return ret;
>>>> }
>>>>
>>>> - if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
>>>> + if (!arch_vma_access_permitted(vmf.vma, flags & FAULT_FLAG_WRITE,
>>>> flags & FAULT_FLAG_INSTRUCTION,
>>>> - flags & FAULT_FLAG_REMOTE)) {
>>>> - trace_spf_vma_access(_RET_IP_, vma, address);
>>>> - ret = VM_FAULT_SIGSEGV;
>>>> - goto out_put;
>>>> - }
>>>> + flags & FAULT_FLAG_REMOTE))
>>>> + goto out_segv;
>>>>
>>>> /* 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))) {
>>>> - trace_spf_vma_access(_RET_IP_, vma, address);
>>>> - ret = VM_FAULT_SIGSEGV;
>>>> - goto out_put;
>>>> - }
>>>> - } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE)))) {
>>>> - trace_spf_vma_access(_RET_IP_, vma, address);
>>>> - ret = VM_FAULT_SIGSEGV;
>>>> - goto out_put;
>>>> - }
>>>> + if (unlikely(!(vmf.vma_flags & VM_WRITE)))
>>>> + goto out_segv;
>>>> + } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE))))
>>>> + goto out_segv;
>>>>
>>>> #ifdef CONFIG_NUMA
>>>> /*
>>>> * MPOL_INTERLEAVE implies additional check in mpol_misplaced() which
>>>> * are not compatible with the speculative page fault processing.
>>>> */
>>>> - pol = __get_vma_policy(vma, address);
>>>> + pol = __get_vma_policy(vmf.vma, address);
>>>> if (!pol)
>>>> pol = get_task_policy(current);
>>>> if (pol && pol->mode == MPOL_INTERLEAVE) {
>>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>>> - goto out_put;
>>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>>> + return ret;
>>>> }
>>>> #endif
>>>>
>>>> @@ -4479,9 +4480,8 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>>> vmf.pte = NULL;
>>>> }
>>>>
>>>> - vmf.vma = vma;
>>>> - vmf.pgoff = linear_page_index(vma, address);
>>>> - vmf.gfp_mask = __get_fault_gfp_mask(vma);
>>>> + vmf.pgoff = linear_page_index(vmf.vma, address);
>>>> + vmf.gfp_mask = __get_fault_gfp_mask(vmf.vma);
>>>> vmf.sequence = seq;
>>>> vmf.flags = flags;
>>>>
>>>> @@ -4491,16 +4491,22 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>>> * 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)) {
>>>> - trace_spf_vma_changed(_RET_IP_, vma, address);
>>>> - goto out_put;
>>>> + if (read_seqcount_retry(&vmf.vma->vm_sequence, seq)) {
>>>> + trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
>>>> + return ret;
>>>> }
>>>>
>>>> mem_cgroup_oom_enable();
>>>> ret = handle_pte_fault(&vmf);
>>>> mem_cgroup_oom_disable();
>>>>
>>>> - put_vma(vma);
>>>> + /*
>>>> + * If there is no need to retry, don't return the vma to the caller.
>>>> + */
>>>> + if (!(ret & VM_FAULT_RETRY)) {
>>>> + put_vma(vmf.vma);
>>>> + *vma = NULL;
>>>> + }
>>>>
>>>> /*
>>>> * The task may have entered a memcg OOM situation but
>>>> @@ -4513,9 +4519,35 @@ int handle_speculative_fault(struct mm_struct *mm, unsigned long address,
>>>> return ret;
>>>>
>>>> out_walk:
>>>> - trace_spf_vma_notsup(_RET_IP_, vma, address);
>>>> + trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
>>>> local_irq_enable();
>>>> -out_put:
>>>> + return ret;
>>>> +
>>>> +out_segv:
>>>> + trace_spf_vma_access(_RET_IP_, vmf.vma, address);
>>>> + /*
>>>> + * We don't return VM_FAULT_RETRY so the caller is not expected to
>>>> + * retrieve the fetched VMA.
>>>> + */
>>>> + put_vma(vmf.vma);
>>>> + *vma = NULL;
>>>> + return VM_FAULT_SIGSEGV;
>>>> +}
>>>> +
>>>> +/*
>>>> + * This is used to know if the vma fetch in the speculative page fault handler
>>>> + * is still valid when trying the regular fault path while holding the
>>>> + * mmap_sem.
>>>> + * The call to put_vma(vma) must be made after checking the vma's fields, as
>>>> + * the vma may be freed by put_vma(). In such a case it is expected that false
>>>> + * is returned.
>>>> + */
>>>> +bool can_reuse_spf_vma(struct vm_area_struct *vma, unsigned long address)
>>>> +{
>>>> + bool ret;
>>>> +
>>>> + ret = !RB_EMPTY_NODE(&vma->vm_rb) &&
>>>> + vma->vm_start <= address && address < vma->vm_end;
>>>> put_vma(vma);
>>>> return ret;
>>>> }
>>>> --
>>>> 2.7.4
>>>>
>>
>