From patchwork Fri Jan 28 13:09:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michel Lespinasse X-Patchwork-Id: 12728524 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C26A0C433F5 for ; Fri, 28 Jan 2022 13:10:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 35C086B009B; Fri, 28 Jan 2022 08:10:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EF6BC6B0082; Fri, 28 Jan 2022 08:10:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A9AD6B00A4; Fri, 28 Jan 2022 08:10:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0191.hostedemail.com [216.40.44.191]) by kanga.kvack.org (Postfix) with ESMTP id 62D386B0095 for ; Fri, 28 Jan 2022 08:10:13 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 1A3BA9008D for ; Fri, 28 Jan 2022 13:10:13 +0000 (UTC) X-FDA: 79079729106.06.D97130B Received: from server.lespinasse.org (server.lespinasse.org [63.205.204.226]) by imf19.hostedemail.com (Postfix) with ESMTP id 9E6331A000F for ; Fri, 28 Jan 2022 13:10:12 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=lespinasse.org; i=@lespinasse.org; q=dns/txt; s=srv-52-ed; t=1643375407; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : from; bh=h0OB3itXHSsaegtYCAjglB0dKK8hw9ET+M16zSiFNyU=; b=xS5mCESfy6y0TI6ONSNvUVVbt4kZJ5FEuG4+8W9WsmR3WuvRkxdSZBFUA3lpIapuHDyRM TMjVq3VB/OcODo8Ag== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lespinasse.org; i=@lespinasse.org; q=dns/txt; s=srv-52-rsa; t=1643375407; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : from; bh=h0OB3itXHSsaegtYCAjglB0dKK8hw9ET+M16zSiFNyU=; b=LpDorKuWKqjFoH3XY0OMq4Pa43KItEaXJXyu7mK08bI5Ji3kWxrbak6QdNfKkOzLKUbCq g/OAyfTR+1m765H905fFlfVQTEnIZ+zosJ9T/4AzVAt0gMs8SyYyFQw39rGQjnFLfRGi4F1 ctyEGWUGnRTs+tkjbfCYjUOWAy4OWcwwldTmyaAJblOakogBGqRnIWQgODeVVB2GUbgxd8y +f/1QjxSujsaarrkwFvlDtv+U5AY46icuP5CKcmDxBKHPmbJHL/5y4goGC/ec0/9Pk5wjO6 ZYZwyrtUbEv+9K8WSaeZLhXGIJ5xGAxye2z04RQAbIsNL7WeoXk5J/d+8WLA== Received: from zeus.lespinasse.org (zeus.lespinasse.org [IPv6:fd00::150:0]) by server.lespinasse.org (Postfix) with ESMTPS id 33B8A160AAD; Fri, 28 Jan 2022 05:10:07 -0800 (PST) Received: by zeus.lespinasse.org (Postfix, from userid 1000) id 16FC920F8E; Fri, 28 Jan 2022 05:10:07 -0800 (PST) From: Michel Lespinasse To: Linux-MM , linux-kernel@vger.kernel.org, Andrew Morton Cc: kernel-team@fb.com, Laurent Dufour , Jerome Glisse , Peter Zijlstra , Michal Hocko , Vlastimil Babka , Davidlohr Bueso , Matthew Wilcox , Liam Howlett , Rik van Riel , Paul McKenney , Song Liu , Suren Baghdasaryan , Minchan Kim , Joel Fernandes , David Rientjes , Axel Rasmussen , Andy Lutomirski , Michel Lespinasse Subject: [PATCH v2 27/35] mm: implement and enable speculative fault handling in handle_pte_fault() Date: Fri, 28 Jan 2022 05:09:58 -0800 Message-Id: <20220128131006.67712-28-michel@lespinasse.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20220128131006.67712-1-michel@lespinasse.org> References: <20220128131006.67712-1-michel@lespinasse.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9E6331A000F X-Rspam-User: nil Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=lespinasse.org header.s=srv-52-ed header.b=xS5mCESf; dkim=pass header.d=lespinasse.org header.s=srv-52-rsa header.b=LpDorKuW; dmarc=pass (policy=none) header.from=lespinasse.org; spf=pass (imf19.hostedemail.com: domain of walken@lespinasse.org designates 63.205.204.226 as permitted sender) smtp.mailfrom=walken@lespinasse.org X-Stat-Signature: 6sicjyr8ctemqkrf3w5e7x3tf47gbfft X-Rspamd-Server: rspam08 X-HE-Tag: 1643375412-288620 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In handle_pte_fault(), allow speculative execution to proceed. Use pte_spinlock() to validate the mmap sequence count when locking the page table. If speculative execution proceeds through do_wp_page(), ensure that we end up in the wp_page_reuse() or wp_page_copy() paths, rather than wp_pfn_shared() or wp_page_shared() (both unreachable as we only handle anon vmas so far) or handle_userfault() (needs an explicit abort to handle non-speculatively). Signed-off-by: Michel Lespinasse --- mm/memory.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index fd8984d89109..7f8dbd729dce 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3293,6 +3293,7 @@ static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; + VM_BUG_ON(vmf->flags & FAULT_FLAG_SPECULATIVE); if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { vm_fault_t ret; @@ -3313,6 +3314,8 @@ static vm_fault_t wp_page_shared(struct vm_fault *vmf) struct vm_area_struct *vma = vmf->vma; vm_fault_t ret = VM_FAULT_WRITE; + VM_BUG_ON(vmf->flags & FAULT_FLAG_SPECULATIVE); + get_page(vmf->page); if (vma->vm_ops && vma->vm_ops->page_mkwrite) { @@ -3366,6 +3369,8 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) if (userfaultfd_pte_wp(vma, *vmf->pte)) { pte_unmap_unlock(vmf->pte, vmf->ptl); + if (vmf->flags & FAULT_FLAG_SPECULATIVE) + return VM_FAULT_RETRY; return handle_userfault(vmf, VM_UFFD_WP); } @@ -4646,13 +4651,8 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) return do_numa_page(vmf); - if (vmf->flags & FAULT_FLAG_SPECULATIVE) { - pte_unmap(vmf->pte); + if (!pte_spinlock(vmf)) return VM_FAULT_RETRY; - } - - vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); - spin_lock(vmf->ptl); entry = vmf->orig_pte; if (unlikely(!pte_same(*vmf->pte, entry))) { update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);