Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp635860pxy; Fri, 30 Apr 2021 12:54:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkee8MD/QobrQvga5sl6YBEKuZ+0oSqFTYQWTHt5Qqw9y4ApQTca/HVbbmIBtfmdAhCWc/ X-Received: by 2002:a62:5209:0:b029:278:648f:99b6 with SMTP id g9-20020a6252090000b0290278648f99b6mr6243599pfb.9.1619812495837; Fri, 30 Apr 2021 12:54:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619812495; cv=none; d=google.com; s=arc-20160816; b=l8ZVe6Wxd7X/mwhtlT+VM8yGusSM8aWUB13YZdca1ETNc9DCszGuetDvHXyFEX+ak0 NmEZI6ZGuJ+yCFCsb8Ls4qKTTNv8oksEZ2iQqoEknM2jUokPKblujDGj1sS9OYW+NqTE YO6n8D8oY4i/C3k7raASyCoIA8DILoIPYi2McmnZD0CtgkVW+xenx3+ub1NQa+UxOBXu /htN7omY0Cje0FrOdIUmDDediIO9v4M17yhWqUssM4qUFk4m9QCrjq7uYNmlF+AgAdWT 9DlIMndYqJs5ZnOGWt9IDQvaUzgSl0TGV0JK9LSjxwinVhNqOxkofNyJHKzB5DgIeawP z9rQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=3PnBKJvO5QY1tr24tZ6y3ew9eVWtL5kDFtGxo4ArR+E=; b=xomAArgBTPAMKT/tJWY//EzKsgdvo7WDA7jazA1WI8gx104u9WvDhM6aCIBpHwTMHc GCVbAJgD2zoqLuPPwRTuRUMEqLKWQa34AOL5inX3Q79VUAc3plyvRfTXLV9xPOHXxDIT gzBty6KX9q3zB0rQPGpc4iiH0lrll2qtS65LpoyYlRRiKXBhsjnuHcaNItLMb4bNzoCv D8W4SUOgFZvDcRuDTY0qFZ6SgtMIZj5JGYBAt2T3KevZ0bsfA/Rp5PF3ame3zqXfIxal bxkWEd6iZkJfYMC3SHMbldP8Wqg279dJjrClJKjCze9NlmzD3wZ9nw0JsS6JpgfZm9Wl hAlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (no key) header.i=@lespinasse.org header.b=lXBA0+F6; dkim=pass (test mode) header.i=@lespinasse.org header.s=srv-14-rsa header.b=dzV0TyOI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=lespinasse.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i1si3271420pja.21.2021.04.30.12.54.43; Fri, 30 Apr 2021 12:54:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=neutral (no key) header.i=@lespinasse.org header.b=lXBA0+F6; dkim=pass (test mode) header.i=@lespinasse.org header.s=srv-14-rsa header.b=dzV0TyOI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=lespinasse.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233957AbhD3Typ (ORCPT + 99 others); Fri, 30 Apr 2021 15:54:45 -0400 Received: from server.lespinasse.org ([63.205.204.226]:59681 "EHLO server.lespinasse.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232427AbhD3Txg (ORCPT ); Fri, 30 Apr 2021 15:53:36 -0400 DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=lespinasse.org; i=@lespinasse.org; q=dns/txt; s=srv-14-ed; t=1619812353; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : from; bh=3PnBKJvO5QY1tr24tZ6y3ew9eVWtL5kDFtGxo4ArR+E=; b=lXBA0+F6lchhVkLLvb5J6eYEHqc6P+VKSYX9K1A7aPH85rzdTMVLqatH87PnLBZegRP2q TKXtRyQ757vAs9hDw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lespinasse.org; i=@lespinasse.org; q=dns/txt; s=srv-14-rsa; t=1619812353; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : from; bh=3PnBKJvO5QY1tr24tZ6y3ew9eVWtL5kDFtGxo4ArR+E=; b=dzV0TyOIMhHowJCNEtGaYZ4ibmh4ZJg9dIbE7IsVg2FpdmcPq1bssfGyF7kQVPJbkmNvx Kwmh7ga4jyZjaIrleHYGhE9SMzXra/cTW9R3dwAPEXGjmhpHT/eXRkPg1Nnry2lXDsNUtpX Q1tj9sbYOJfdDX2vfKtOJWW76kguZwY67cwBe0lotTABt7pky5vRkvnSW9lK5xV/zaLk5Gj CnerETNmvyR/VGKkoRoM0uLBnldZvqYRqXnVPjbR7aVBlShhTqQB1QZHHYYT0vkuauozP3Z VQevU/v/tMkbT3rUnZW6GW59pNgyZ/TP7CaAWcoj/DU2GAB5Uq4fLz+3dcNQ== Received: from zeus.lespinasse.org (zeus.lespinasse.org [10.0.0.150]) by server.lespinasse.org (Postfix) with ESMTPS id 9870A16033F; Fri, 30 Apr 2021 12:52:33 -0700 (PDT) Received: by zeus.lespinasse.org (Postfix, from userid 1000) id 88DF119F524; Fri, 30 Apr 2021 12:52:33 -0700 (PDT) From: Michel Lespinasse To: Linux-MM , Linux-Kernel Cc: Laurent Dufour , Peter Zijlstra , Michal Hocko , Matthew Wilcox , Rik van Riel , Paul McKenney , Andrew Morton , Suren Baghdasaryan , Joel Fernandes , Andy Lutomirski , Michel Lespinasse Subject: [PATCH 21/29] mm: implement speculative handling in wp_page_copy() Date: Fri, 30 Apr 2021 12:52:22 -0700 Message-Id: <20210430195232.30491-22-michel@lespinasse.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210430195232.30491-1-michel@lespinasse.org> References: <20210430195232.30491-1-michel@lespinasse.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Change wp_page_copy() to handle the speculative case. This involves aborting speculative faults if they have to allocate an anon_vma, and using pte_map_lock() instead of pte_offset_map_lock() to complete the page fault. Also change call sites to clear vmf->pte after unmapping the page table, in order to satisfy pte_map_lock()'s preconditions. Signed-off-by: Michel Lespinasse --- mm/memory.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 286776b7795b..cb66585f5145 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2928,20 +2928,27 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) pte_t entry; int page_copied = 0; struct mmu_notifier_range range; + vm_fault_t ret = VM_FAULT_OOM; - if (unlikely(anon_vma_prepare(vma))) - goto oom; + if (unlikely(!vma->anon_vma)) { + if (vmf->flags & FAULT_FLAG_SPECULATIVE) { + ret = VM_FAULT_RETRY; + goto out; + } + if (__anon_vma_prepare(vma)) + goto out; + } if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { new_page = alloc_zeroed_user_highpage_movable(vma, vmf->address); if (!new_page) - goto oom; + goto out; } else { new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); if (!new_page) - goto oom; + goto out; if (!cow_user_page(new_page, old_page, vmf)) { /* @@ -2958,7 +2965,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) } if (mem_cgroup_charge(new_page, mm, GFP_KERNEL)) - goto oom_free_new; + goto out_free_new; cgroup_throttle_swaprate(new_page, GFP_KERNEL); __SetPageUptodate(new_page); @@ -2971,7 +2978,11 @@ static vm_fault_t 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)) { + ret = VM_FAULT_RETRY; + /* put_page() will uncharge the page */ + goto out_free_new; + } if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { if (old_page) { if (!PageAnon(old_page)) { @@ -3059,12 +3070,12 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) put_page(old_page); } return page_copied ? VM_FAULT_WRITE : 0; -oom_free_new: +out_free_new: put_page(new_page); -oom: +out: if (old_page) put_page(old_page); - return VM_FAULT_OOM; + return ret; } /** @@ -3207,6 +3218,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) return wp_pfn_shared(vmf); pte_unmap_unlock(vmf->pte, vmf->ptl); + vmf->pte = NULL; return wp_page_copy(vmf); } @@ -3245,6 +3257,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) get_page(vmf->page); pte_unmap_unlock(vmf->pte, vmf->ptl); + vmf->pte = NULL; return wp_page_copy(vmf); } -- 2.20.1