Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp516161ybe; Wed, 18 Sep 2019 22:31:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqzf7CV9sE3EOcXG6UJ2lteI3hbiwrTNQOfyO5MW7FVULRFxMhoiASk1mRZRD0K5LIt/45Zo X-Received: by 2002:a50:d49c:: with SMTP id s28mr13994816edi.101.1568871090244; Wed, 18 Sep 2019 22:31:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568871090; cv=none; d=google.com; s=arc-20160816; b=hSP6R8a/hev59JwnfPCePjbJAgWTu/2A056IYvTIi413NTjcReSMeShXW4yG+HsHQs s6hqNJPeYX7PUQYBvMFHS6fbZPsfvY2sQ7K8K8Yqwi5hlrP+CX/m0hYz0moTehU+6Y6I aO3G/CUZAw8Ci3GBb4LIsrPbuuXC2BXzY3YF2iUyOKYWiaT02wJf25ARmZs9EeCjiHC7 CWAetYCMZdDJ4sM/QAj0eHhe8Hf6lrH/0FFbXm9wX/nvV4V3JAIX9aSGsFxwAhMXa96w n5ca6fNu/GcGY5j3Tjf1Y+U9a58bMgbmIAuwaT4mlozt/WTTu11cZD0KVmKoE47Gy7Ht FR/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject:dkim-signature; bh=v8j+KKtmRVgMXlvD0CNN4PMpgxJdXZjDz2rJhuUyCXk=; b=qfVKJ7MBP2gRqwBd9YRUxZZr25e2qcjpmhzymmLiGvX5ag2+7H7UVi/TScn3tGnaG8 DTC121FQ6Wmc+yeOtRR777yIqlU5Xh4zRO0GSvC1HWQ7g1GMzCms6P63ppJowlQM72la 6CweIr2/qv/m6AamrFdBSBzcmLVRcFAtmkW5ncd4px1r/jJ8SYX3GMvDSeIoVzJeeRER O84UOo1IvWSSY5bWxdCyyLqd4c5rUtU9i6SPrNI3ZW081rBEucOCA5CZ/HkmUorcBaCk XULiMBth8qvetJa3fFXho8DNvKWdV6NOIBLwWAzDTeEKD9FlPBIJdhoNgTdxOUL6rQ2N 9McA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HOgLAhQl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ch10si3903095ejb.89.2019.09.18.22.31.07; Wed, 18 Sep 2019 22:31:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HOgLAhQl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387984AbfISCQ6 (ORCPT + 99 others); Wed, 18 Sep 2019 22:16:58 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:36780 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387957AbfISCQ6 (ORCPT ); Wed, 18 Sep 2019 22:16:58 -0400 Received: by mail-pf1-f193.google.com with SMTP id y22so1207737pfr.3 for ; Wed, 18 Sep 2019 19:16:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=v8j+KKtmRVgMXlvD0CNN4PMpgxJdXZjDz2rJhuUyCXk=; b=HOgLAhQlZVOSPHKYoDkAVD3oL1lCw1GE1ZHBOF2nQQIdMqGoTCHK4zJ4hjAcCbHwth 0+Y43Ifzp50k4DuSGywzI5fGgz0Zz4nBLdqJzK/n4l7DoKyRyrjyS96/QtCjKoswlzE+ nbWD/skOa3B5bs5MB6VP04wPKrGvVCiFmp/1uaHGlU7WgWbe6dK9tuAJjloMhL5mFAZo KuJkdAvWs0L875tb9h1ARdEkmGJv0t/QgMoaAyVGFRrL0GBfbuvcSR5SNsqOKgJFf6wy IiSKa/IC/WSC1njRHr+BebkeMwE363WpPVOkUYFAZjaunXzKUCOkVDDcu/rTdUPKnkYQ v5aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=v8j+KKtmRVgMXlvD0CNN4PMpgxJdXZjDz2rJhuUyCXk=; b=s49jJEln/HQ1yMXd2WUz6NHBki6foJdWl6NB/7bNnhbrONZDi75vttV/5ChBwXjMB9 ybgCfVP2oZohPVOoLzoT8hYPRplnpoBZHH5qNydQYm6HsTN2BkFgYgspni1eDM8TKZQV GJCIq0DYOZNZsn3q6E5rPCEkOAoAR1Jafhg2/hugny7tIwN2ZE21KgqR8zAdoDsecpgB eGEuhJPMp+AkDWv2A8TOlmThDFXt24z+ujJ2a9DjdFQkL1xVUJHnyUQwzEJn+OHlP3FK j8LeRFU6DXloCyEYXH6aRKQWSar2w/W9d3nQM4kfTWQ/H9U448PgStmlOpJIN/Ef3Cp0 rMjA== X-Gm-Message-State: APjAAAWUd2avOw4U0POPg9ojgrpMpyaMYDzWIwv46MH0AP3WUnNfNQVI WGd8mEFn0rQXV+2SjEAs3vE= X-Received: by 2002:a62:7d81:: with SMTP id y123mr7630365pfc.133.1568859416096; Wed, 18 Sep 2019 19:16:56 -0700 (PDT) Received: from [0.0.0.0] (104.129.187.94.16clouds.com. [104.129.187.94]) by smtp.gmail.com with ESMTPSA id z12sm12749012pfj.41.2019.09.18.19.16.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Sep 2019 19:16:55 -0700 (PDT) Subject: Re: [PATCH v4 3/3] mm: fix double page fault on arm64 if PTE_AF is cleared To: "Kirill A. Shutemov" , Jia He Cc: Catalin Marinas , Will Deacon , Mark Rutland , James Morse , Marc Zyngier , Matthew Wilcox , "Kirill A. Shutemov" , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Suzuki Poulose , Punit Agrawal , Anshuman Khandual , Jun Yao , Alex Van Brunt , Robin Murphy , Thomas Gleixner , Andrew Morton , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Ralph Campbell , Kaly Xin References: <20190918131914.38081-1-justin.he@arm.com> <20190918131914.38081-4-justin.he@arm.com> <20190918140027.ckj32xnryyyesc23@box> From: Jia He Message-ID: Date: Thu, 19 Sep 2019 10:16:34 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20190918140027.ckj32xnryyyesc23@box> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Kirill [On behalf of justin.he@arm.com because some mails are filted...] On 2019/9/18 22:00, Kirill A. Shutemov wrote: > On Wed, Sep 18, 2019 at 09:19:14PM +0800, Jia He wrote: >> When we tested pmdk unit test [1] vmmalloc_fork TEST1 in arm64 guest, there >> will be a double page fault in __copy_from_user_inatomic of cow_user_page. >> >> 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 fix it by calling pte_mkyoung. Also, the parameter is >> changed because vmf should be passed to cow_user_page() >> >> [1] https://github.com/pmem/pmdk/tree/master/src/test/vmmalloc_fork >> >> Reported-by: Yibo Cai >> Signed-off-by: Jia He >> --- >> mm/memory.c | 35 ++++++++++++++++++++++++++++++----- >> 1 file changed, 30 insertions(+), 5 deletions(-) >> >> diff --git a/mm/memory.c b/mm/memory.c >> index e2bb51b6242e..d2c130a5883b 100644 >> --- a/mm/memory.c >> +++ b/mm/memory.c >> @@ -118,6 +118,13 @@ int randomize_va_space __read_mostly = >> 2; >> #endif >> >> +#ifndef arch_faults_on_old_pte >> +static inline bool arch_faults_on_old_pte(void) >> +{ >> + return false; >> +} >> +#endif >> + >> static int __init disable_randmaps(char *s) >> { >> randomize_va_space = 0; >> @@ -2140,8 +2147,12 @@ static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, >> return same; >> } >> >> -static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) >> +static inline void cow_user_page(struct page *dst, struct page *src, >> + struct vm_fault *vmf) >> { >> + struct vm_area_struct *vma = vmf->vma; >> + unsigned long addr = vmf->address; >> + >> debug_dma_assert_idle(src); >> >> /* >> @@ -2152,20 +2163,34 @@ static inline void cow_user_page(struct page *dst, struct page *src, unsigned lo >> */ >> if (unlikely(!src)) { >> void *kaddr = kmap_atomic(dst); >> - void __user *uaddr = (void __user *)(va & PAGE_MASK); >> + void __user *uaddr = (void __user *)(addr & PAGE_MASK); >> + pte_t entry; >> >> /* >> * This really shouldn't fail, because the page is there >> * in the page tables. But it might just be unreadable, >> * in which case we just give up and fill the result with >> - * zeroes. >> + * zeroes. On architectures with software "accessed" bits, >> + * we would take a double page fault here, so mark it >> + * accessed here. >> */ >> + if (arch_faults_on_old_pte() && !pte_young(vmf->orig_pte)) { >> + spin_lock(vmf->ptl); >> + if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { >> + entry = pte_mkyoung(vmf->orig_pte); >> + if (ptep_set_access_flags(vma, addr, >> + vmf->pte, entry, 0)) >> + update_mmu_cache(vma, addr, vmf->pte); >> + } > I don't follow. > > So if pte has changed under you, you don't set the accessed bit, but never > the less copy from the user. > > What makes you think it will not trigger the same problem? > > I think we need to make cow_user_page() fail in this case and caller -- > wp_page_copy() -- return zero. If the fault was solved by other thread, we > are fine. If not userspace would re-fault on the same address and we will > handle the fault from the second attempt. Thanks for the pointing. How about make cow_user_page() be returned VM_FAULT_RETRY? Then in do_page_fault(), it can retry the page fault? --- Cheers, Justin (Jia He) > >> + spin_unlock(vmf->ptl); >> + } >> + >> if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) >> clear_page(kaddr); >> kunmap_atomic(kaddr); >> flush_dcache_page(dst); >> } else >> - copy_user_highpage(dst, src, va, vma); >> + copy_user_highpage(dst, src, addr, vma); >> } >> >> static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) >> @@ -2318,7 +2343,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) >> vmf->address); >> if (!new_page) >> goto oom; >> - cow_user_page(new_page, old_page, vmf->address, vma); >> + cow_user_page(new_page, old_page, vmf); >> } >> >> if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false)) >> -- >> 2.17.1 >> >> --