From patchwork Fri Jun 30 02:04:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13297514 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 D2A16EB64D9 for ; Fri, 30 Jun 2023 02:04:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B8C28D0007; Thu, 29 Jun 2023 22:04:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 642338D0001; Thu, 29 Jun 2023 22:04:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BB9C8D0007; Thu, 29 Jun 2023 22:04:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3D8758D0001 for ; Thu, 29 Jun 2023 22:04:52 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0C65A4041A for ; Fri, 30 Jun 2023 02:04:52 +0000 (UTC) X-FDA: 80957770824.13.9CCE8E7 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf29.hostedemail.com (Postfix) with ESMTP id 42A92120012 for ; Fri, 30 Jun 2023 02:04:50 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=6PpY0zxI; spf=pass (imf29.hostedemail.com: domain of 3QTieZAYKCFYGIF2Bz4CC492.0CA96BIL-AA8Jy08.CF4@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3QTieZAYKCFYGIF2Bz4CC492.0CA96BIL-AA8Jy08.CF4@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=1688090690; 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=FPLq4zv3roaklCz3lgPe63Se1fgo5eMfP7akPG/a/ow=; b=17vXsxXPeMLxDRblwHIRK4RdB1VTMKiRxoajSifgvCbhe4qXbG7Lp9lzBd+3XEN7VpNsVa 7jmg3iBvv0/ThCHICZ0GQLuqR21IA0Y8Fa1eqkjY3HYIzKlFxWzKfxHY5tLHV6CmXRzCc7 BUAkkpi8gnqgNJG3PyT3BItFhwsietk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688090690; a=rsa-sha256; cv=none; b=tMWc03p2ciD6UJvaHmoAGqkvYuCoyIOfqnyD/tdpMGQeiKbF2elWoUNRhZiQcWu1Wg/ZNI stLiBEDobDA5xk9XTfTVsksXqeHo431ChtRX5w6mW0jl6aQ//i8RCW5az0n0n5de0YZMyT UMYwmt3RE2j+hgtSH08RAoHXJBW5nfc= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=6PpY0zxI; spf=pass (imf29.hostedemail.com: domain of 3QTieZAYKCFYGIF2Bz4CC492.0CA96BIL-AA8Jy08.CF4@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3QTieZAYKCFYGIF2Bz4CC492.0CA96BIL-AA8Jy08.CF4@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-57003dac4a8so19794177b3.1 for ; Thu, 29 Jun 2023 19:04:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688090689; x=1690682689; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FPLq4zv3roaklCz3lgPe63Se1fgo5eMfP7akPG/a/ow=; b=6PpY0zxIk7j9Nuz5B+KxXIPHpGGMEYKk3CUWBJ5VjSX284Yj8oPSXI2iT2Fydz3ie6 /QZ1Jby0umP//VyvIHxtQHR0BtS/pqKJXXEpDYRxgJpDwAZ+onleEU/W1qDCWKybMN6g tlFzWNOgeUS5JUijf+PhyR2rdGYuM+DLYL0bD6UwtFtPG5KdVaAFtt9KqSwnUHLsvr5L spl/lvcjwPrcYkUgTR8WOV2P2i7CZXMnh4/X/NqLaSO4UTmN7xBwAMBb/XsIOmkRYzkf 5YmpauIoTGHRojFq+ezgT4pRCfiqWjPkdC0avAJTXDEpDjYb4dKPQbFkpIztJtKZDcQn 6k3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688090689; x=1690682689; 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=FPLq4zv3roaklCz3lgPe63Se1fgo5eMfP7akPG/a/ow=; b=MubXQ7znM3+z15XRzt0wXEychatv0PYZ1wACIjJhrXQw6oaLEgWUL4w7hzf139zsNb CUG6rOdyQqfCRqTz5pVztLGhI/PzaUuyxEY37+6/tctAb7JzfZmIOczBrrXXHJSQlpFr GBJ8TezwYZEdQtDMd1f5X5HOWr7l4sKcNeuJmtNwBw4N/2SfPivgTWi0fuyP6gym+drb R6effgj7sLL9EN6eH5xm5y4qtJh7Ejh5Mh1E5ZdRrq8728dNYUFnysiFAH80i5T/Bmri bGv47ksiva0oZCijOr6yCoklzcjKYzBrD+FaWBdIs60M4afEWrfKQKKuNbVYRZb01DiK S/0Q== X-Gm-Message-State: ABy/qLaKSOVd7B5Y+jS/n+dhvLKTL+7OEItV5h56sVCKSFTdHvjPKw0Y qUFQ6zgWSS4kWxLWHY0BAK/EbQYzye4= X-Google-Smtp-Source: APBJJlHrGzz2xdHx6UvUBVeHJ0BvkwaA6W8kb2bX6RsS6g6D2VZ4jkFr3U1teRxP9jVrOn+Hph+5c2ioFqg= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:1f11:a3d0:19a9:16e5]) (user=surenb job=sendgmr) by 2002:a05:690c:2f82:b0:56d:5db:2f07 with SMTP id ew2-20020a05690c2f8200b0056d05db2f07mr70815ywb.5.1688090689367; Thu, 29 Jun 2023 19:04:49 -0700 (PDT) Date: Thu, 29 Jun 2023 19:04:33 -0700 In-Reply-To: <20230630020436.1066016-1-surenb@google.com> Mime-Version: 1.0 References: <20230630020436.1066016-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230630020436.1066016-5-surenb@google.com> Subject: [PATCH v6 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-Queue-Id: 42A92120012 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: n68yrpz9zcoom6e8uryw1rsxutkecuc6 X-HE-Tag: 1688090690-181705 X-HE-Meta: U2FsdGVkX1+fPshSep7l5XfYH1Q25FwmZeuMuYfisSO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NiOLTVPjR2w9LCaOSWwao9cCBL6YzqSn+w24yOAUHQ9TKsG5RKRtojP53N0uGMqlmjR3vQCw5YZCNmgnZHnq8lLMMLgxsb0F1/uHiroZqQjJ8fOt51H6V7n1cuLNG/wcRoZHXX3kW94vfU8B+/aVVqE2t1cfgceC7ZLYD0EPWaYrJW9j4vd+OzoWNvp/jLFOyWEUiUEbYNPCx1jd0Nkt1rwqvxlCHjsXSPBP3mniaFD7rgYOsLiW0LNX3PTvIIk0/2UX50jhWToL8/jRGxrzTS8lfj2CfuXCL8ek4Ql5rxY+qf3pHWBkjcYp7FdeyTMjOPkbjfs7jnqrq1H+xLsR7Nfvh8FtpyuZGGeaehY2d/N4IUVQBoHVpekLlBrAK8zgxM+wdVdlALktfm+wrOD8uYwNjjCERjsvDU+TAYYUulPRy1cFAzycAfsga25i331dHo8Mw8Hv/EHhUGofr4DSCShh3tZwx4qdj0TIPrV2icHudH8YlPNJrJtAbzbHTp3eHFSwvBgjcUJb0Lo6JFCDmnHfwgUDlNNF8nnzNsIw/21g+2p+LfjhCLTS3yq3tja7u1XqQa1lSh2DkcRJ9egCbpHpdTzqxWYvG/8WkMQPPXEz2GJw= 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 | 9 ++++----- mm/filemap.c | 22 ++++++++++++---------- mm/memory.c | 14 ++++++-------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 716953ee1ebd..0026a0a8277c 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,11 @@ 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); + return folio_trylock(folio) ? 0 : __folio_lock_or_retry(folio, vmf); } /* diff --git a/mm/filemap.c b/mm/filemap.c index 9e44a49bbd74..d245bb4f7153 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. + * 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_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) { /*