Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1092854yba; Thu, 18 Apr 2019 15:12:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqyIiZWB97VGO9+Bb0u/k2cKq2TKKubE4IUMPGmvO8L/y6UBOPDwhYEZfITkUC9cgP66IVZF X-Received: by 2002:a17:902:3e3:: with SMTP id d90mr15612pld.271.1555625557345; Thu, 18 Apr 2019 15:12:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555625557; cv=none; d=google.com; s=arc-20160816; b=U0w4f1zf+KhHpqj7rYFzs3eB6s4m8DsN7QCQCZStOaHBmSU4NrDlhZzsZzvienhIbw +TviJt14T22NMfO2i1KkBd66ET3Ny6sMmcezzyLyDvSgpWHD7VsRUHtPmtSMqoPB/yt4 bFdrGcUTtNFVbju62vKYMmxHbA+kNtflxUdlRpRzYKyOgLdttycFG+rPGvp8jOyMjE+J dy1YvBva1GberkE2aTaAEf4KPKahZFc7h0LOykcZ3JNB+0zDq6hldTovq56qu69zAGGV rYhgfw1lD0RPI8FSoczTA5h/JmmjR1Jz1sMR4fY4nj44WU0z1bafgEg9oviQcOYqtyhC FuuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=DBNfdHjVlyltMJlh/rgdrOde7mCb3PJbhT2ulMO54l8=; b=WONBYOZkGINX5rO/D/MQ9Qwkjc/+33HvkgFkEsbH2dDnWLyLHDJZZ1ZLCvCsOk5mjA q2ihKJkOAbFMU03x4OqNyG2ho4Vk+xEllQ9KQnjqzbGCBUMJSIsha+z1K6NuRgtKKjXs SunGkqBurJR/SsiDuQClQVuyzYcNf6NzA7JHcsI2wipUD0/sxgHMFZZt1E+uyQZBq/i1 y6bddqghoYr0g4Wn5NqIytNKxnPv6doilcwTGVbu8UfoHCRQibU+VP9UUO6kMh1I2/UL +666TtXx+YqvWc6P8FjLnsSDzbPYzqBwlZEjwmMrDpzpn691PkAaTgXf/eudHijMY6Y7 /XBw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 4si3379693pfp.185.2019.04.18.15.12.21; Thu, 18 Apr 2019 15:12:37 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726000AbfDRWKM (ORCPT + 99 others); Thu, 18 Apr 2019 18:10:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52564 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725824AbfDRWKM (ORCPT ); Thu, 18 Apr 2019 18:10:12 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7F87370D6B; Thu, 18 Apr 2019 22:10:10 +0000 (UTC) Received: from redhat.com (unknown [10.20.6.236]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A571C5D70A; Thu, 18 Apr 2019 22:10:06 +0000 (UTC) Date: Thu, 18 Apr 2019 18:10:04 -0400 From: Jerome Glisse To: Laurent Dufour Cc: akpm@linux-foundation.org, mhocko@kernel.org, peterz@infradead.org, kirill@shutemov.name, ak@linux.intel.com, dave@stgolabs.net, jack@suse.cz, Matthew Wilcox , aneesh.kumar@linux.ibm.com, benh@kernel.crashing.org, mpe@ellerman.id.au, paulus@samba.org, Thomas Gleixner , Ingo Molnar , hpa@zytor.com, Will Deacon , Sergey Senozhatsky , sergey.senozhatsky.work@gmail.com, Andrea Arcangeli , Alexei Starovoitov , kemi.wang@intel.com, Daniel Jordan , David Rientjes , Ganesh Mahendran , Minchan Kim , Punit Agrawal , vinayak menon , Yang Shi , zhong jiang , Haiyan Song , Balbir Singh , sj38.park@gmail.com, Michel Lespinasse , Mike Rapoport , linux-kernel@vger.kernel.org, linux-mm@kvack.org, haren@linux.vnet.ibm.com, npiggin@gmail.com, paulmck@linux.vnet.ibm.com, Tim Chen , linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: Re: [PATCH v12 07/31] mm: make pte_unmap_same compatible with SPF Message-ID: <20190418221004.GG11645@redhat.com> References: <20190416134522.17540-1-ldufour@linux.ibm.com> <20190416134522.17540-8-ldufour@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20190416134522.17540-8-ldufour@linux.ibm.com> User-Agent: Mutt/1.11.3 (2019-02-01) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 18 Apr 2019 22:10:11 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 16, 2019 at 03:44:58PM +0200, Laurent Dufour wrote: > 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. > > Acked-by: David Rientjes > Signed-off-by: Laurent Dufour Reviewed-by: J?r?me Glisse > --- > include/linux/mm_types.h | 6 +++++- > mm/memory.c | 37 +++++++++++++++++++++++++++---------- > 2 files changed, 32 insertions(+), 11 deletions(-) > > diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h > index 8ec38b11b361..fd7d38ee2e33 100644 > --- a/include/linux/mm_types.h > +++ b/include/linux/mm_types.h > @@ -652,6 +652,8 @@ typedef __bitwise unsigned int vm_fault_t; > * @VM_FAULT_NEEDDSYNC: ->fault did not modify page tables and needs > * fsync() to complete (for synchronous page faults > * in DAX) > + * @VM_FAULT_PTNOTSAME Page table entries have changed during a > + * speculative page fault handling. > * @VM_FAULT_HINDEX_MASK: mask HINDEX value > * > */ > @@ -669,6 +671,7 @@ enum vm_fault_reason { > VM_FAULT_FALLBACK = (__force vm_fault_t)0x000800, > VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000, > VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000, > + VM_FAULT_PTNOTSAME = (__force vm_fault_t)0x004000, > VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000, > }; > > @@ -693,7 +696,8 @@ enum vm_fault_reason { > { VM_FAULT_RETRY, "RETRY" }, \ > { VM_FAULT_FALLBACK, "FALLBACK" }, \ > { VM_FAULT_DONE_COW, "DONE_COW" }, \ > - { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" } > + { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ > + { VM_FAULT_PTNOTSAME, "PTNOTSAME" } > > struct vm_special_mapping { > const char *name; /* The name, e.g. "[vdso]". */ > diff --git a/mm/memory.c b/mm/memory.c > index 221ccdf34991..d5bebca47d98 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -2094,21 +2094,29 @@ static inline 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 vm_fault_t 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) > @@ -2714,8 +2722,17 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > int exclusive = 0; > vm_fault_t ret = 0; > > - if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) > + ret = pte_unmap_same(vmf); > + if (ret) { > + /* > + * If pte != orig_pte, this means another thread did the > + * swap operation in our back. > + * So nothing else to do. > + */ > + if (ret == VM_FAULT_PTNOTSAME) > + ret = 0; > goto out; > + } > > entry = pte_to_swp_entry(vmf->orig_pte); > if (unlikely(non_swap_entry(entry))) { > -- > 2.21.0 >