Skip to content

Commit 83d116c

Browse files
justin-hectmarinas
authored andcommitted
mm: fix double page fault on arm64 if PTE_AF is cleared
When we tested pmdk unit test [1] vmmalloc_fork TEST3 on arm64 guest, there will be a double page fault in __copy_from_user_inatomic of cow_user_page. To reproduce the bug, the cmd is as follows after you deployed everything: make -C src/test/vmmalloc_fork/ TEST_TIME=60m check Below call trace is from arm64 do_page_fault for debugging purpose: [ 110.016195] Call trace: [ 110.016826] do_page_fault+0x5a4/0x690 [ 110.017812] do_mem_abort+0x50/0xb0 [ 110.018726] el1_da+0x20/0xc4 [ 110.019492] __arch_copy_from_user+0x180/0x280 [ 110.020646] do_wp_page+0xb0/0x860 [ 110.021517] __handle_mm_fault+0x994/0x1338 [ 110.022606] handle_mm_fault+0xe8/0x180 [ 110.023584] do_page_fault+0x240/0x690 [ 110.024535] do_mem_abort+0x50/0xb0 [ 110.025423] el0_da+0x20/0x24 The pte info before __copy_from_user_inatomic is (PTE_AF is cleared): [ffff9b007000] pgd=000000023d4f8003, pud=000000023da9b003, pmd=000000023d4b3003, pte=360000298607bd3 As told by Catalin: "On arm64 without hardware Access Flag, copying from user will fail because the pte is old and cannot be marked young. So we always end up with zeroed page after fork() + CoW for pfn mappings. we don't always have a hardware-managed access flag on arm64." This patch fixes it by calling pte_mkyoung. Also, the parameter is changed because vmf should be passed to cow_user_page() Add a WARN_ON_ONCE when __copy_from_user_inatomic() returns error in case there can be some obscure use-case (by Kirill). [1] https://github.com/pmem/pmdk/tree/master/src/test/vmmalloc_fork Signed-off-by: Jia He <[email protected]> Reported-by: Yibo Cai <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Acked-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent f2c4e59 commit 83d116c

File tree

1 file changed

+89
-15
lines changed

1 file changed

+89
-15
lines changed

mm/memory.c

Lines changed: 89 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ int randomize_va_space __read_mostly =
118118
2;
119119
#endif
120120

121+
#ifndef arch_faults_on_old_pte
122+
static inline bool arch_faults_on_old_pte(void)
123+
{
124+
/*
125+
* Those arches which don't have hw access flag feature need to
126+
* implement their own helper. By default, "true" means pagefault
127+
* will be hit on old pte.
128+
*/
129+
return true;
130+
}
131+
#endif
132+
121133
static int __init disable_randmaps(char *s)
122134
{
123135
randomize_va_space = 0;
@@ -2145,32 +2157,82 @@ static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
21452157
return same;
21462158
}
21472159

2148-
static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
2160+
static inline bool cow_user_page(struct page *dst, struct page *src,
2161+
struct vm_fault *vmf)
21492162
{
2163+
bool ret;
2164+
void *kaddr;
2165+
void __user *uaddr;
2166+
bool force_mkyoung;
2167+
struct vm_area_struct *vma = vmf->vma;
2168+
struct mm_struct *mm = vma->vm_mm;
2169+
unsigned long addr = vmf->address;
2170+
21502171
debug_dma_assert_idle(src);
21512172

2173+
if (likely(src)) {
2174+
copy_user_highpage(dst, src, addr, vma);
2175+
return true;
2176+
}
2177+
21522178
/*
21532179
* If the source page was a PFN mapping, we don't have
21542180
* a "struct page" for it. We do a best-effort copy by
21552181
* just copying from the original user address. If that
21562182
* fails, we just zero-fill it. Live with it.
21572183
*/
2158-
if (unlikely(!src)) {
2159-
void *kaddr = kmap_atomic(dst);
2160-
void __user *uaddr = (void __user *)(va & PAGE_MASK);
2184+
kaddr = kmap_atomic(dst);
2185+
uaddr = (void __user *)(addr & PAGE_MASK);
2186+
2187+
/*
2188+
* On architectures with software "accessed" bits, we would
2189+
* take a double page fault, so mark it accessed here.
2190+
*/
2191+
force_mkyoung = arch_faults_on_old_pte() && !pte_young(vmf->orig_pte);
2192+
if (force_mkyoung) {
2193+
pte_t entry;
2194+
2195+
vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
2196+
if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
2197+
/*
2198+
* Other thread has already handled the fault
2199+
* and we don't need to do anything. If it's
2200+
* not the case, the fault will be triggered
2201+
* again on the same address.
2202+
*/
2203+
ret = false;
2204+
goto pte_unlock;
2205+
}
21612206

2207+
entry = pte_mkyoung(vmf->orig_pte);
2208+
if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0))
2209+
update_mmu_cache(vma, addr, vmf->pte);
2210+
}
2211+
2212+
/*
2213+
* This really shouldn't fail, because the page is there
2214+
* in the page tables. But it might just be unreadable,
2215+
* in which case we just give up and fill the result with
2216+
* zeroes.
2217+
*/
2218+
if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
21622219
/*
2163-
* This really shouldn't fail, because the page is there
2164-
* in the page tables. But it might just be unreadable,
2165-
* in which case we just give up and fill the result with
2166-
* zeroes.
2220+
* Give a warn in case there can be some obscure
2221+
* use-case
21672222
*/
2168-
if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
2169-
clear_page(kaddr);
2170-
kunmap_atomic(kaddr);
2171-
flush_dcache_page(dst);
2172-
} else
2173-
copy_user_highpage(dst, src, va, vma);
2223+
WARN_ON_ONCE(1);
2224+
clear_page(kaddr);
2225+
}
2226+
2227+
ret = true;
2228+
2229+
pte_unlock:
2230+
if (force_mkyoung)
2231+
pte_unmap_unlock(vmf->pte, vmf->ptl);
2232+
kunmap_atomic(kaddr);
2233+
flush_dcache_page(dst);
2234+
2235+
return ret;
21742236
}
21752237

21762238
static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
@@ -2327,7 +2389,19 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
23272389
vmf->address);
23282390
if (!new_page)
23292391
goto oom;
2330-
cow_user_page(new_page, old_page, vmf->address, vma);
2392+
2393+
if (!cow_user_page(new_page, old_page, vmf)) {
2394+
/*
2395+
* COW failed, if the fault was solved by other,
2396+
* it's fine. If not, userspace would re-fault on
2397+
* the same address and we will handle the fault
2398+
* from the second attempt.
2399+
*/
2400+
put_page(new_page);
2401+
if (old_page)
2402+
put_page(old_page);
2403+
return 0;
2404+
}
23312405
}
23322406

23332407
if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false))

0 commit comments

Comments
 (0)