From patchwork Wed Jun 28 07:17:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13295274 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 8A9F7EB64DC for ; Wed, 28 Jun 2023 07:18:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E9A278D0008; Wed, 28 Jun 2023 03:18:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E4A738D0001; Wed, 28 Jun 2023 03:18:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC44C8D0008; Wed, 28 Jun 2023 03:18:16 -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 B4BDD8D0001 for ; Wed, 28 Jun 2023 03:18:16 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6A86B120C7D for ; Wed, 28 Jun 2023 07:18:16 +0000 (UTC) X-FDA: 80951302992.10.5BD1C1E Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf10.hostedemail.com (Postfix) with ESMTP id ADBB9C0009 for ; Wed, 28 Jun 2023 07:18:14 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=2j6rC6JP; spf=pass (imf10.hostedemail.com: domain of 3td6bZAYKCA4685s1pu22uzs.q20zw18B-00y9oqy.25u@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3td6bZAYKCA4685s1pu22uzs.q20zw18B-00y9oqy.25u@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687936694; a=rsa-sha256; cv=none; b=VK2Wo68uoeN6mmnJ9HLY/HmHXr1jTazTWZEz2dayEP1p283MqM/rs9NRZzzBf4ldrUztES r1rS80PXJRABuQfvXK2NEDVfCaeDMepeGmWSwrPFcieyjMunpZxlkTuLEpWg7nrtF0tS9q mr3JTTzaMIpLA3wYqENms5vMTtzWiLs= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=2j6rC6JP; spf=pass (imf10.hostedemail.com: domain of 3td6bZAYKCA4685s1pu22uzs.q20zw18B-00y9oqy.25u@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3td6bZAYKCA4685s1pu22uzs.q20zw18B-00y9oqy.25u@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=1687936694; 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=70kkRXsfLM/8BYxAf0BjvmYkgZM27G9ch/j23+KotKk=; b=t3irNISBc2MPlvJhrHROs0flWPl2PuSoii+gBZ9rZbZ0Je57Fi3mU9Z0NNgBPGFJXr4xui ZebAlRf9TM4NXXR8S/Bu+K9iAUjxdLoeW1eytG01mHFa5Le2S8+QHmRVns/Ktu+DAVD8xE e0au8COasHOJHtYu1P675gr0VQe5ThQ= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-564fb1018bcso75968167b3.0 for ; Wed, 28 Jun 2023 00:18:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687936694; x=1690528694; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=70kkRXsfLM/8BYxAf0BjvmYkgZM27G9ch/j23+KotKk=; b=2j6rC6JPL2YZjVDNjSuz3XfLikJeKjCdOIy/hDMUWQUPhsVDGLQM+MPzx5VEdlFP0u tw9yulb3bHX/HNYybGu6MWDWSnlAve77hIC5hqvxjrYRhbozZ17KLrLgvT89FAn2r6EG 0JB59bf3q6b+kSXpcJHknOiZXeTuNJBA8QnapuoeA+oj8SEfuNgdIAiLdLl3GIuWcoXx V8EoIxH9py5ZVGwJQIZunXZLjOEfmySzy5MMfjsKPtv4ngFUclZxJpqeFEAbLEDJmHXy NK+CZUORLtGlSzG2HegFkG9DxXO3P7tbtF1tXW6/UhlOixp+bzIpA9nES6tPKWvBi/jD bXsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687936694; x=1690528694; 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=70kkRXsfLM/8BYxAf0BjvmYkgZM27G9ch/j23+KotKk=; b=VwejDwGv9YgS10VIroVFzye/r0jvBQc9Fv/A6dXKFz99ZjgEBkZLiTZJ4BIUkF1qvu UqnTdvTV8oQp8TMwTVZoUVx7BIFJ0ZVJ8TjpoDJ+0ZtDVRy51PUaJTyKRXB+zuC0WXZL jbMlV7lRq3p+SHyLpz6QT7tjVSTw1864LOZtm2Y6lYDPVzmvqQ8E6RlRHuHCoqmfqGUa S4K+yKMQpEI9wv60ddngaXmOdbjKZiBbLVm5HVXFLMb9xsES1kjLFrnUNxuANgaHv2lk QnIv3AbXZai1fhi+/fVfsV3pkz9s0CyTFX97kJLZJCHSY8Z1NX6qldOSayk6v9YF+8IA 4SBw== X-Gm-Message-State: AC+VfDx/6xpZno4Tk+5MjE9Xbgd6zoHWNQoi6ce2AkL1Re3tPVkulPFu BdkMBOgpy0mwmlc7wiKRizUIfRQhPd0= X-Google-Smtp-Source: ACHHUZ6OQi+GrrsFt6L2aYE+Bh/8JjB2CJChJJQK4V9w8+uL61Sq3ukd625ySw4N4Q1XugZY6mxhmwtxYNQ= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6664:8bd3:57fd:c83a]) (user=surenb job=sendgmr) by 2002:a25:11c4:0:b0:c2a:b486:1085 with SMTP id 187-20020a2511c4000000b00c2ab4861085mr2504239ybr.10.1687936693767; Wed, 28 Jun 2023 00:18:13 -0700 (PDT) Date: Wed, 28 Jun 2023 00:17:58 -0700 In-Reply-To: <20230628071800.544800-1-surenb@google.com> Mime-Version: 1.0 References: <20230628071800.544800-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230628071800.544800-5-surenb@google.com> Subject: [PATCH v4 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: ADBB9C0009 X-Stat-Signature: 6g7mi64qjgud3dxp3g4y74dadbqz6bjy X-Rspam-User: X-HE-Tag: 1687936694-877964 X-HE-Meta: U2FsdGVkX19IzJDtltBOrGPtA/qBA5QH8zBZqqBsNYY18t5/vld/Q8rdo+GRgYvpbRF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BWZ9Q0KgPrWPwVZniwou5qZvcAvDAyUAyC6PKjL7hwK4sUj6X8LrIG3co4vHxzexhGvDPNW/w+aNKndkdOzp+LStJ54ERHuBM8HVzhfeMqE1992LrkeaACqANvTDBNfn/fTSj9WumdRFsb6un88sT4Ix3JfrPfqml5wpmHvnTGuyhPTw4AQFfbn7DtlfpblDugzyydQC6ZRnYKwlRA0SId3uks/Nhq98g0mZY0h82Di+yOv0jY0N0OLYWMbcYjSFBWyGAr5uNfaO1lnm4MnZyK3hEsDrQOYRrRMHFheImy824aozhOedS+mgWDW2cPLo0y5zft7IOvnSBRBeLo6ag1diIFnlWU2KK+f5byzJoS6sQzC3lkbfFqUco0y46k0FKyK9JBa3hxzkgRrrdZj3IMnhu82zPoQc5qNovQbGQpoTr+slvqyB8Wpip3l4qYMu6aKbPk1PlYeQPBHKp9xiqIyTCd6eF2HMO+IyDPscyqT2xYrnu0fiTWbKLIjdMNXUoz+rxlfN8aggrym06sKSJMNIaBMH5shPTPm0UcxSGBD9qpVz9SWB8Laxm4w== 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 a56308a9d1a4..59d070c55c97 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_or_retry(struct folio *folio, struct vm_fault *vmf); void unlock_page(struct page *page); void folio_unlock(struct folio *folio); @@ -1001,11 +1000,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 00f01d8ead47..52bcf12dcdbf 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1701,32 +1701,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; @@ -1734,13 +1736,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 f14d45957b83..345080052003 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_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, @@ -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; @@ -3826,12 +3827,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) { /*