From patchwork Wed Sep 27 05:25:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13399932 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 803BFE80A88 for ; Wed, 27 Sep 2023 05:25:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E85F68D0016; Wed, 27 Sep 2023 01:25:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E10A18D0002; Wed, 27 Sep 2023 01:25:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD88B8D0016; Wed, 27 Sep 2023 01:25:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BA8468D0002 for ; Wed, 27 Sep 2023 01:25:11 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 84DF140987 for ; Wed, 27 Sep 2023 05:25:11 +0000 (UTC) X-FDA: 81281238822.07.3C7C3DA Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf30.hostedemail.com (Postfix) with ESMTP id 3E0BC80006 for ; Wed, 27 Sep 2023 05:25:08 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=mHmn8Ro7; dmarc=none; spf=none (imf30.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695792310; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=R1cgnWd1rwQWVhfoW+i9aIbpeLTl++h04BYfoZW8rgE=; b=IGu8Ba/4mZRAK+1j42cC/MSwoS7mvDnVdMdFTPDxyav0YazLXh/xDhVFRgLOThGz00TkM8 m7DIxDI6J5CwR3GPBpa0kAs8JZ9BhlNIC+jwvRy6CDJwk903xVcvp5ebSKrwREBH4gYH7f +4ZSxkIfdQSgP3yYVqXT6mKpKq8q9gk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=mHmn8Ro7; dmarc=none; spf=none (imf30.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695792310; a=rsa-sha256; cv=none; b=fW2MXj45W5IE0y0hELKB6GS9zrHYcfRGRwO2/6hWlMHvrF9UQhMYIvorcFOwMl+VJ4bU9s 8HQWM/BTa/uQUOlWd8nkrWslUgbYHKtJb99Ny+8P6qBDZNQzuwlX+lYgxYuXX9Zw1WPyXH 43PTqkN6Hix51eUj2UI8m3BtAouNiho= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=R1cgnWd1rwQWVhfoW+i9aIbpeLTl++h04BYfoZW8rgE=; b=mHmn8Ro7tjrGPcAYhpGJcpU4ap jTzmUsOkJXJkLUe5Zripo0Q+kcFaAGeS8dRlLY7urN5xbSVedow39IvYW4cWKRlRiXfs6BX8vUqh+ VMtpyHT8T29PUjlhx70sRgL/6lJuNg8ouo/fTBTdCeSqCXQaxWf1UPXi8utcBRk+bSYvkcLhrdFFG 2cTF9D7aCnHTcmD+7PAUIydsu/LLZqjZXRXNUBtyDbwswJVlqzrr7zOVaEwWyKRPfYyKPk7iU/p+A YsWUph/7hVSy7xm/jD9zUeHLXm6D1gJdQ1kgleXNj1Izkyx+8CNMFbeCQDd6LdYn+2oSTOdzzXoKM AFJ0iI3w==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qlN2o-00Bywq-J7; Wed, 27 Sep 2023 05:25:06 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, Suren Baghdasaryan Subject: [PATCH 2/6] mm: Call wp_page_copy() under the VMA lock Date: Wed, 27 Sep 2023 06:25:00 +0100 Message-Id: <20230927052505.2855872-3-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230927052505.2855872-1-willy@infradead.org> References: <20230927052505.2855872-1-willy@infradead.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 3E0BC80006 X-Stat-Signature: heu4i87sfnhznm6mj36yc1e9w48qpi1t X-HE-Tag: 1695792308-410330 X-HE-Meta: U2FsdGVkX185rSuEzmkLpoKtys4nlxOL6DvWw3Ewf7vhjbRlfQuAA8apsJsFOATcrHczSMa/ZEfZz4EFyjV23zzgiHlczDITnocGxcqwDWNMVBu28rYY5S6ILQnC7EuYGUMBqBah4j+tICiOPGRVgCleXmE5emlrqnWTA0DGHBpsSlJjCRLV/64WAV+oas09j1sIvcs6eq+ylWt/Ad4ixB8nFpd5NDvwRs4oTxS6AyYRn9pUoHUywbznRcB9gL4O6J891xqTL+hEBUsPsHwaHCnLHVaQkqKHSYUXFiQGVEpJ+wZBn1dYSLyyDVB4BAbeZ6zul21aKGOcn8b3W7nvPIOJ7svWs9llr2S+AEZtUu+F1cls2RvV5afWCdpvQth30/57xndUHAk8aoz6pFv+JrHNLfd69AJrSCEe6HJSRslHragIUijJp8GA1qTp80KceL/Xl+3o7ZLlE0E15Q+nYgf4HGvJ5QZET4YMLfDA/F6V9p0XA6ppEQg4C2lHjs8yYxSoYXtpJPEeUzZiWVXCzVt3ME/ArILqL9ELhVfxCGTxpnYJMhbwzmQx7MHureKQmAmhP2sr49tvgAh6zaimb8ZGT+DM05DnRk3iPTDfBHwHA8Q/8I7SDmadCGepr4cPPpqFJVfzYI/JIEj9m/6WKZZ6N/uIGnTVjvA2TEcSKcDWVAKA60CDl5Tw84Q3M8mcdpULE7oQmLJ43OnIYy4skalZYF6SPsHPI1VJYhMIGT8nu5AH8VUCINkbPq2upbi/Fc2MfJCtv8hQ7RovrK0pMyIIBNimnUTb4OIsO9PVI6v6ndP+fCgI3Y7SZycSOMjTGENUxVadL1Fyo8+hBWJ/Q9af0VLTEUK2Vm4W9amxAY8k1XGGAAbeC1giAYHpA/OnbBuH5Wt+P5HVTPQkcRpt0/plSpj3Y1nWLGVD0Xo1WIEDBz4oyL3Rtpk8XUZHyy60ASOv+TSxRDwWbE6IwJq 7Pl8j7FK h8cxyFSFLeSKPVUriAqkRIt7pBjEUzO3UXLMvBkJAWsxee7fSSugUvvG1zPhzdYp6kLBYFZ6uHNJH0Wa5fP9F95xKAJ2OZXDzyr5PDRnYyWfEfR1axXQgAeUxVs7qLcLdlpf+yYMcZu7U3y2IuvO1vxkEcGnHotMyB9YzUnpGgxIbsKRcWXWo0vtTkKXF8hMmI+SNf4OFg8hYDGsm5XFpHtPWsy7K7nZxNnr8 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: It is usually safe to call wp_page_copy() under the VMA lock. The only unsafe situation is when no anon_vma has been allocated for this VMA, and we have to look at adjacent VMAs to determine if their anon_vma can be shared. Since this happens only for the first COW of a page in this VMA, the majority of calls to wp_page_copy() do not need to fall back to the mmap_sem. Add vmf_anon_prepare() as an alternative to anon_vma_prepare() which will return RETRY if we currently hold the VMA lock and need to allocate an anon_vma. This lets us drop the check in do_wp_page(). Signed-off-by: Matthew Wilcox (Oracle) --- mm/memory.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 97f860d6cd2a..cff78c496728 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3042,6 +3042,21 @@ static inline void wp_page_reuse(struct vm_fault *vmf) count_vm_event(PGREUSE); } +static vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) +{ + struct vm_area_struct *vma = vmf->vma; + + if (likely(vma->anon_vma)) + return 0; + if (vmf->flags & FAULT_FLAG_VMA_LOCK) { + vma_end_read(vma); + return VM_FAULT_RETRY; + } + if (__anon_vma_prepare(vma)) + return VM_FAULT_OOM; + return 0; +} + /* * Handle the case of a page which we actually need to copy to a new page, * either due to COW or unsharing. @@ -3069,27 +3084,29 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) pte_t entry; int page_copied = 0; struct mmu_notifier_range range; - int ret; + vm_fault_t ret; delayacct_wpcopy_start(); if (vmf->page) old_folio = page_folio(vmf->page); - if (unlikely(anon_vma_prepare(vma))) - goto oom; + ret = vmf_anon_prepare(vmf); + if (unlikely(ret)) + goto out; if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { new_folio = vma_alloc_zeroed_movable_folio(vma, vmf->address); if (!new_folio) goto oom; } else { + int err; new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vmf->address, false); if (!new_folio) goto oom; - ret = __wp_page_copy_user(&new_folio->page, vmf->page, vmf); - if (ret) { + err = __wp_page_copy_user(&new_folio->page, vmf->page, vmf); + if (err) { /* * COW failed, if the fault was solved by other, * it's fine. If not, userspace would re-fault on @@ -3102,7 +3119,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) folio_put(old_folio); delayacct_wpcopy_end(); - return ret == -EHWPOISON ? VM_FAULT_HWPOISON : 0; + return err == -EHWPOISON ? VM_FAULT_HWPOISON : 0; } kmsan_copy_page_meta(&new_folio->page, vmf->page); } @@ -3212,11 +3229,13 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) oom_free_new: folio_put(new_folio); oom: + ret = VM_FAULT_OOM; +out: if (old_folio) folio_put(old_folio); delayacct_wpcopy_end(); - return VM_FAULT_OOM; + return ret; } /** @@ -3458,12 +3477,6 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) return 0; } copy: - if ((vmf->flags & FAULT_FLAG_VMA_LOCK) && !vma->anon_vma) { - pte_unmap_unlock(vmf->pte, vmf->ptl); - vma_end_read(vmf->vma); - return VM_FAULT_RETRY; - } - /* * Ok, we need to copy. Oh, well.. */