From patchwork Fri Jun 30 21:19:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13298786 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 BBFC6EB64DD for ; Fri, 30 Jun 2023 21:20:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E33E8E004F; Fri, 30 Jun 2023 17:20:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 41C218E000F; Fri, 30 Jun 2023 17:20:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 295DD8E004F; Fri, 30 Jun 2023 17:20:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1C2AE8E000F for ; Fri, 30 Jun 2023 17:20:13 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E9DC4C02FC for ; Fri, 30 Jun 2023 21:20:12 +0000 (UTC) X-FDA: 80960682264.15.E648241 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf24.hostedemail.com (Postfix) with ESMTP id 31753180013 for ; Fri, 30 Jun 2023 21:20:11 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=lpQ0audC; spf=pass (imf24.hostedemail.com: domain of 3CkefZAYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3CkefZAYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688160011; a=rsa-sha256; cv=none; b=mxLF7UANvZPorvptXRsfDRdJGTpi1I0DdlEJC1I1WiJ2QDreLG/aLIiXlUFtQ7Knm8p17j G7unGn5Z96yQEZD0+PDpJigh5n2EW1REa4a5XYHEWvT+3yfJ0AiedEh1rrEFvt+hnd6P9i 6oeAOcfXh7QcfPkYEq2ISndJVhOkR58= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=lpQ0audC; spf=pass (imf24.hostedemail.com: domain of 3CkefZAYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3CkefZAYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688160011; 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=V48GaAIPa0cNnuKmjtn5vErs8F0GYQkIF6/xAME4ub8=; b=s89YZ/0siBEFXIi+EMQ7T16LIUKYrV7giGWZOIzzE+0D+9RxHWO4ubqe2OiLIbxgmzvwF9 7oY7offWQcl3Vu1CI/FIqK09n2XR3e8arU+NdeUwtf7sGgSaiEgwSxxhfQrlX7j1foZpaa M+HWVYOIpvwBZ91NHMgPHaB5kI1RqcU= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5704995f964so23257547b3.2 for ; Fri, 30 Jun 2023 14:20:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688160010; x=1690752010; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=V48GaAIPa0cNnuKmjtn5vErs8F0GYQkIF6/xAME4ub8=; b=lpQ0audC6wsRuzHnyRK+4z8E2+3FbVewuOe/E8fGl0CME/gVGP/LwtfwHeXZbkhDvB KMew6UgHQL4Hs7KuwRdK9MYyuFU0dsYVjWwGkfCXYFTo5Xs//Nxyhd+SmNnxyMNtaH6n IZ6bWkw6zolCt4THSRJw09sKyzxPh4Y1FKecn5UyWQK1gOJTMKrbgqjoyIEKWtsMVMNQ Jz3gB4ynV2sWz1sBqH2aUoCl+fRile8jnRuhnjAZbmOkvcfXYsNAYgMuiJ5VMp3R4wKn /ZDhevNPwR502yvk+OovCCRm9sub1Yr6ptFlEfcHZ7b0/NzgizbQm70kFi9gVlAZRkH1 D83A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688160010; x=1690752010; 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=V48GaAIPa0cNnuKmjtn5vErs8F0GYQkIF6/xAME4ub8=; b=N7UsYxjNJDOUc2BwuXTZ7WFunrWlIz+DV14J+ySAvxDEuAbH+dP0fYdmxJRqLoB9gk e03HzSD9YWe6yRvPUOzRF+BWC/IP5DxsCPnrHMdcCtna5kH0+2MyUHflxuZsZDy3W3np fnVgs0x0WgYa3EgXLMM7cgAyNE9SagCOm/6PFNZiqYvRrJbszEDf+hR31mb0voA5UkH2 TvvhcRSWvQ5g+mVCH0rjU+MDNZaFMdLX9oApqToKq/2JUt0Ohqbjyw4hIw0nRRK87T6M m9fQfkfpxja86Mz0RYZfslyCFnZRQNZnWYMPP3zQBwBF+Zc9lEaz4te/7maXhLLG+yYY OjjQ== X-Gm-Message-State: ABy/qLZHu1wuGljiGWv3TFjNk9FwTjhJIgM4976jKFyLpnsAPgunaYz4 0kfhhkRVuqrBJZOebyWnCbNfrNPQTKQ= X-Google-Smtp-Source: APBJJlFgzJeFB/N0Q7kM4hg4P3gJ476zCnS8Qd4r8uqOH2goCCLdS+i6OHQO2ZNJoVkai7oWS9rXpJLTShc= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:b54c:4d64:f00a:1b67]) (user=surenb job=sendgmr) by 2002:a81:414c:0:b0:576:d9ea:1331 with SMTP id f12-20020a81414c000000b00576d9ea1331mr27652ywk.4.1688160010392; Fri, 30 Jun 2023 14:20:10 -0700 (PDT) Date: Fri, 30 Jun 2023 14:19:55 -0700 In-Reply-To: <20230630211957.1341547-1-surenb@google.com> Mime-Version: 1.0 References: <20230630211957.1341547-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230630211957.1341547-5-surenb@google.com> Subject: [PATCH v7 4/6] mm: change folio_lock_or_retry to use vm_fault directly 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: rspam08 X-Rspamd-Queue-Id: 31753180013 X-Stat-Signature: 1as9a8zsmfqoq3zaywqzmke6914uwxj7 X-Rspam-User: X-HE-Tag: 1688160011-894808 X-HE-Meta: U2FsdGVkX1/EaVm9Bzcvb9VrLYJiHHxVgFKsxx1BLuN+CK04Deo0sUw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LskrJjTOCzYKEec938vRuhgPO5nhimFoe+IkPu26h7Fzv+5ryvTl//CMt3dqKVAHLeZOgk3pbW194vHT8+wUTPCJhkdqUbSzCg01NrN+EE/UcPFNEE3CjT4By5tbo9MslgEkBPQ74Y6w1SYfL2/fU3UwLMzBkujr5KXUWASwaE9AR99S4YnQAYrOP49We9T5hqA8RFnclEF4vGuq4FwiGQ5gHJVUunO4r5TCIiw3cKTnwPIMXQSNizazbeyluZWDXFVcnjdfBY5JHkqYOCvjWpk4sWY39i0IML7DygExFsEzCb0ZvHU67VejL4TyANnDQHYIN1lydHqwvNOHkwZW0RpXpZmHvw1VCzmdVMNZlxLfeeJ5T36ik5fbGZIWqBDPQ5DITokvdDm7BVCzTGWOMoteGqbAR+t3T7NoiRSzWz6mFTL65GwDUdpQ7kWWIgOU2pjCoaP1gfHJMNiT9p5PurPrs3b3VaUJk77r3Yygf1GFFirhCy/b8R0J87nd+V+JNQGjlVr1vWNVHPZJGyGaPQ479EomP64AeJSR49pnuz1oI0+ljDNN3dVou54UmiGQbNitudSgbDZmzRsfQpQgQxrrtsW6oG8= 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. Suggested-by: Matthew Wilcox Signed-off-by: Suren Baghdasaryan Acked-by: Peter Xu --- include/linux/pagemap.h | 11 ++++++----- mm/filemap.c | 22 ++++++++++++---------- mm/memory.c | 14 ++++++-------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 716953ee1ebd..de16f740b755 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -900,8 +900,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_or_retry(struct folio *folio, struct vm_fault *vmf); void unlock_page(struct page *page); void folio_unlock(struct folio *folio); @@ -1005,11 +1004,13 @@ static inline int folio_lock_killable(struct folio *folio) * Return value and mmap_lock implications depend on flags; see * __folio_lock_or_retry(). */ -static inline bool folio_lock_or_retry(struct folio *folio, - struct mm_struct *mm, unsigned int flags) +static inline vm_fault_t folio_lock_or_retry(struct folio *folio, + struct vm_fault *vmf) { might_sleep(); - return folio_trylock(folio) || __folio_lock_or_retry(folio, mm, flags); + if (!folio_trylock(folio)) + return __folio_lock_or_retry(folio, vmf); + return 0; } /* diff --git a/mm/filemap.c b/mm/filemap.c index 9e44a49bbd74..5da5ad6f7f4c 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1669,32 +1669,34 @@ 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. + * non-zero - 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_or_retry(struct folio *folio, struct vm_fault *vmf) { + struct mm_struct *mm = vmf->vma->vm_mm; + unsigned int flags = vmf->flags; + if (fault_flag_allow_retry_first(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; + return VM_FAULT_RETRY; mmap_read_unlock(mm); if (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) { bool ret; @@ -1702,13 +1704,13 @@ bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, 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 5f26c56ce979..4ae3f046f593 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3582,6 +3582,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 @@ -3594,9 +3595,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_or_retry(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, @@ -3721,7 +3723,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; @@ -3844,12 +3845,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_or_retry(folio, vmf); + if (ret & VM_FAULT_RETRY) goto out_release; - } if (swapcache) { /*