From patchwork Tue Jun 27 04:23:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13293983 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 E4ECEC0015E for ; Tue, 27 Jun 2023 04:23:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75AFD8D0008; Tue, 27 Jun 2023 00:23:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E4428D0001; Tue, 27 Jun 2023 00:23:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C1258D0008; Tue, 27 Jun 2023 00:23:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3AA4E8D0001 for ; Tue, 27 Jun 2023 00:23:36 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 18BEF1405BA for ; Tue, 27 Jun 2023 04:23:36 +0000 (UTC) X-FDA: 80947234032.14.69BB8B1 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf06.hostedemail.com (Postfix) with ESMTP id 60C6A180003 for ; Tue, 27 Jun 2023 04:23:34 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=0LWS1fUj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3RWSaZAYKCKIUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3RWSaZAYKCKIUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687839814; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WF0nP76m0uCKG0VaTNWwLBGINdJ/4Fr3BCgpH937Cjs=; b=erWSYabXKvkwv4+NGqTbiQ8oCR80Xln9i4QnHj4wJTemOzTPB/RBh9nIxNke3Vx7FTiHlH F/wlCEd6f5dh8sqbmcCqjxsGG6RwydcZzqjGCmBpuynm4O6ALgyy4NqlbQk6WpvfjIUQDD s/7yLUMElkzSkuRROeRNJ1YGUq2yFhg= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=0LWS1fUj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3RWSaZAYKCKIUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3RWSaZAYKCKIUWTGPDIQQING.EQONKPWZ-OOMXCEM.QTI@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687839814; a=rsa-sha256; cv=none; b=cUDcXR5N3SqO0ybgbu2rJob2wg0+sz0fu6JetjtFWu/q4cyc3BI6WUS8MJE9/gfFX3rdKO GNo9YA8hNeYkDtCB+9UdOf6lKObBEhkTySZW2T09WM9kd5DF4xzHjbfBQSTPMY7MZbsr+Z lXHJWl3arFD1MV9cSvh9/QT4qrNkos4= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-bfebb1beeccso5399939276.2 for ; Mon, 26 Jun 2023 21:23:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687839813; x=1690431813; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WF0nP76m0uCKG0VaTNWwLBGINdJ/4Fr3BCgpH937Cjs=; b=0LWS1fUj8fHv91oTajKg0vJcBVpdqwwSqjUtzC56pWGWU3fl8K9dA6f0idnSLMfjRp fokb+1l28uCZoInOB6UO18g1SRkEK1ukcbhhi0rTdehONv4x9IMeidIzzGOWyKexc0OG jtPnV6XHIsb+Kk87Cid51Sqhcr8uIw/ufmfTLfVhd2MQ4RpVy1HWiF8B3Sb3sDy9uguB pjICzCL8grLgH0l9QV0PKaK54nmIU8F7wcbUEL44gNrVf8lCe7QlzZUd/Ktb4nhV/XHr Yfq48KIvtTGqDvC3g4QyYJOr3Z8pcOMXFmK7g5sWgh7pvY3BZaDvj7vYqGrfnd6T5ld/ zR/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687839813; x=1690431813; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WF0nP76m0uCKG0VaTNWwLBGINdJ/4Fr3BCgpH937Cjs=; b=G6fMgE3t8TR9n7pl8RKzWYRCL8byDMSciXeQX6EM13l+LNrtR2IvT0xl05BoOrV7Nj tXj2sO2v4cXbxS5gk8pPHsRGTZs5P0ODLuVyit0bKyPa45Ny5eGXTZI6jY88qSfubCMp XiNwlp3sVRC3rgLnr6UUmm9S871gWfToUahJS30FJdW9a0Av17MCtXaJ+2HnD8ytYr1M rHSHKPxhiAR4yuv6DAS9pM4hK8F+6klm06uEzHaRtu7Ff2iQ0q92oz034Ti+fyV5tMyR ux6HYiBxOIYOzuOeUsM+y1F0tr8xzhHLgX4ByCecQ/hLBkTPym0dnoygHpEyfpzWn7jR GRMQ== X-Gm-Message-State: AC+VfDxqUFvuUOEBc6SI+Utr1ZmPLL882rLxTrwF+FicQk5Pqk7cqX6W wpYzPQYCgc3dDGyDscKlAixfzicN+5U= X-Google-Smtp-Source: ACHHUZ56AMLtfxTBdGwHX0CowYNHyrKCFB2CGbJH8wbYjNIC5f8CBX5MSU7xISg3RwE4lnvBrqwh6dvbi6E= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:5075:f38d:ce2f:eb1b]) (user=surenb job=sendgmr) by 2002:a25:ac1:0:b0:c1d:4fce:466 with SMTP id 184-20020a250ac1000000b00c1d4fce0466mr2214106ybk.4.1687839813544; Mon, 26 Jun 2023 21:23:33 -0700 (PDT) Date: Mon, 26 Jun 2023 21:23:17 -0700 In-Reply-To: <20230627042321.1763765-1-surenb@google.com> Mime-Version: 1.0 References: <20230627042321.1763765-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230627042321.1763765-5-surenb@google.com> Subject: [PATCH v3 4/8] mm: replace folio_lock_or_retry with folio_lock_fault From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: willy@infradead.org, hannes@cmpxchg.org, mhocko@suse.com, josef@toxicpanda.com, jack@suse.cz, ldufour@linux.ibm.com, laurent.dufour@fr.ibm.com, michel@lespinasse.org, liam.howlett@oracle.com, jglisse@google.com, vbabka@suse.cz, minchan@google.com, dave@stgolabs.net, punit.agrawal@bytedance.com, lstoakes@gmail.com, hdanton@sina.com, apopple@nvidia.com, peterx@redhat.com, ying.huang@intel.com, david@redhat.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, pasha.tatashin@soleen.com, surenb@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 60C6A180003 X-Stat-Signature: ut53e73tsmqxwcapemod5wykd8aym7ap X-Rspam-User: X-HE-Tag: 1687839814-634841 X-HE-Meta: U2FsdGVkX19my55E9OZCiBHFIu9rWBf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+xlPU0Qp8ffYUyoRFVG8zRG+m+2Ho1M1vxosRi3Wa9hRUjNGMXHXo/SPLEsG8guO9T9ttBJDMauYLDHRM30Jo4RMADIUXsSRsDv7pUFICD9qVwIFRRrpXmWiJJzMGkXfDd3hcQkHootxS15GjCwN0nLuh4mGX+LqS6LyxYx/l+r8QgODDu4b4bpL2vKV8BYsVxVhDHznYwdYhJMm0Gq6kDI3A7mJ8aa7GIcHq0RyOIYYcYfZW2N8zmg5UdF/xwmlJcflQQtrUwLHha0pR27NTlA4/YQVqxbPd174Rr5Mf8olQYPbBdXplD7W6y5hywtbBbJJPDrnfAItqOk3+2dWnzRVG0IEfqLRqcksEHtkN/PcK9XQyf/0sJhIcjDfb+c2HnT1BhSyDkMl1O+7svm6MfsTRGZ9oQY8rWEOulmLpBS8U/OUN046Y1T3hcDBowHoDC6w6n5fFeMuTbRvKiKKlL71rEBOd3J5gNkFAQb4M2K5AZptOnkPGBtj0TZIJhOhjU7sVWLwOhCILLKpsxgxwQ4dtejb1iczxsMMTpwLkwPuSIlb6czwMPu4oL/ba5SRwnT7uhQCLV9bqg4gldiI9AF9+nhXy7pxIPBEQZPwbKUYNrphrGA== 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: Change folio_lock_or_retry to accept vm_fault struct and return the vm_fault_t directly. This will be used later to return additional information about the state of the mmap_lock upon return from this function. Suggested-by: Matthew Wilcox Signed-off-by: Suren Baghdasaryan --- include/linux/pagemap.h | 13 ++++++------- mm/filemap.c | 29 +++++++++++++++-------------- mm/memory.c | 14 ++++++-------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index a56308a9d1a4..0bc206c6f62c 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -896,8 +896,7 @@ static inline bool wake_page_match(struct wait_page_queue *wait_page, void __folio_lock(struct folio *folio); int __folio_lock_killable(struct folio *folio); -bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, - unsigned int flags); +vm_fault_t __folio_lock_fault(struct folio *folio, struct vm_fault *vmf); void unlock_page(struct page *page); void folio_unlock(struct folio *folio); @@ -995,17 +994,17 @@ static inline int folio_lock_killable(struct folio *folio) } /* - * folio_lock_or_retry - Lock the folio, unless this would block and the + * folio_lock_fault - Lock the folio, unless this would block and the * caller indicated that it can handle a retry. * * Return value and mmap_lock implications depend on flags; see - * __folio_lock_or_retry(). + * __folio_lock_fault(). */ -static inline bool folio_lock_or_retry(struct folio *folio, - struct mm_struct *mm, unsigned int flags) +static inline vm_fault_t folio_lock_fault(struct folio *folio, + struct vm_fault *vmf) { might_sleep(); - return folio_trylock(folio) || __folio_lock_or_retry(folio, mm, flags); + return folio_trylock(folio) ? 0 : __folio_lock_fault(folio, vmf); } /* diff --git a/mm/filemap.c b/mm/filemap.c index 00f01d8ead47..87b335a93530 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1701,46 +1701,47 @@ static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait) /* * Return values: - * true - folio is locked; mmap_lock is still held. - * false - folio is not locked. + * 0 - folio is locked. + * VM_FAULT_RETRY - folio is not locked. * mmap_lock has been released (mmap_read_unlock(), unless flags had both * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in * which case mmap_lock is still held. * - * If neither ALLOW_RETRY nor KILLABLE are set, will always return true + * If neither ALLOW_RETRY nor KILLABLE are set, will always return 0 * with the folio locked and the mmap_lock unperturbed. */ -bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, - unsigned int flags) +vm_fault_t __folio_lock_fault(struct folio *folio, struct vm_fault *vmf) { - if (fault_flag_allow_retry_first(flags)) { + struct mm_struct *mm = vmf->vma->vm_mm; + + if (fault_flag_allow_retry_first(vmf->flags)) { /* * CAUTION! In this case, mmap_lock is not released - * even though return 0. + * even though return VM_FAULT_RETRY. */ - if (flags & FAULT_FLAG_RETRY_NOWAIT) - return false; + if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) + return VM_FAULT_RETRY; mmap_read_unlock(mm); - if (flags & FAULT_FLAG_KILLABLE) + if (vmf->flags & FAULT_FLAG_KILLABLE) folio_wait_locked_killable(folio); else folio_wait_locked(folio); - return false; + return VM_FAULT_RETRY; } - if (flags & FAULT_FLAG_KILLABLE) { + if (vmf->flags & FAULT_FLAG_KILLABLE) { bool ret; ret = __folio_lock_killable(folio); if (ret) { mmap_read_unlock(mm); - return false; + return VM_FAULT_RETRY; } } else { __folio_lock(folio); } - return true; + return 0; } /** diff --git a/mm/memory.c b/mm/memory.c index 9011ad63c41b..3c2acafcd7b6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3568,6 +3568,7 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) struct folio *folio = page_folio(vmf->page); struct vm_area_struct *vma = vmf->vma; struct mmu_notifier_range range; + vm_fault_t ret; /* * We need a reference to lock the folio because we don't hold @@ -3580,9 +3581,10 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) if (!folio_try_get(folio)) return 0; - if (!folio_lock_or_retry(folio, vma->vm_mm, vmf->flags)) { + ret = folio_lock_fault(folio, vmf); + if (ret) { folio_put(folio); - return VM_FAULT_RETRY; + return ret; } mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, vma->vm_mm, vmf->address & PAGE_MASK, @@ -3704,7 +3706,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) bool exclusive = false; swp_entry_t entry; pte_t pte; - int locked; vm_fault_t ret = 0; void *shadow = NULL; @@ -3825,12 +3826,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out_release; } - locked = folio_lock_or_retry(folio, vma->vm_mm, vmf->flags); - - if (!locked) { - ret |= VM_FAULT_RETRY; + ret |= folio_lock_fault(folio, vmf); + if (ret & VM_FAULT_RETRY) goto out_release; - } if (swapcache) { /*