From patchwork Mon May 1 17:50:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13227760 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 A3423C77B73 for ; Mon, 1 May 2023 17:50:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45718280002; Mon, 1 May 2023 13:50:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4061C280001; Mon, 1 May 2023 13:50:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A897280002; Mon, 1 May 2023 13:50:38 -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 1E26F280001 for ; Mon, 1 May 2023 13:50:38 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D81091C72AE for ; Mon, 1 May 2023 17:50:37 +0000 (UTC) X-FDA: 80742426114.24.32F90B9 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf21.hostedemail.com (Postfix) with ESMTP id 0B3501C002B for ; Mon, 1 May 2023 17:50:35 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Kbw2vWgf; spf=pass (imf21.hostedemail.com: domain of 36vtPZAYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=36vtPZAYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@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=1682963436; 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=oNrjh4NDZfCOCA+XspYc7I9kB51AWPXlbM0N3pX4I38=; b=f52NWu02F6skVaRyOjJY6XeW0vlG4bfapoTS0f59S24LbHmc07qK+P1csDWhmmwbFX5qOs /m9BNDoOxabOCLMzmVDGhXbJJgsFv3yovFt2pJniXz2gazWGmlob2Pf15OjRES4BuPS35Z EjRvT67V1WCC69y9n/HXyNbMeAoniBY= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Kbw2vWgf; spf=pass (imf21.hostedemail.com: domain of 36vtPZAYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=36vtPZAYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682963436; a=rsa-sha256; cv=none; b=bJL9ssVSgLAFpmAvfP/dcgGY9VJlLXELwUUEKMXRlnsSmnk90gb/TeirBadaT0z9GxG5gI n14HdH8TejrUk/hallF2btBN55YxiymD+60vrzxZT0UZjWKNRa1zFoJ1EP3Zx8qmdu1wSA 4tuTpc8IbVjICeS0CS+NwTLAvJKaCzY= Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-64115ef7234so23606162b3a.1 for ; Mon, 01 May 2023 10:50:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682963435; x=1685555435; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=oNrjh4NDZfCOCA+XspYc7I9kB51AWPXlbM0N3pX4I38=; b=Kbw2vWgfaNr9wwaOaALtstqdWrM8vexXN61YFf/VngXG0KW+KOPVkcePNZuJ8BunHG 4fpSDYYnv+oGNFNonvZma1sxd7lY+pamluLU1rV2cF//+7bWNniJgJBWEyyKJ8Vh7+6F 9iUtSaPHWAZ9eK6TXIhhIoxbL87AdOepwayQg5RNHhsGMUhaXjd/xW95LMnIJV5qHKsT ZO0KE701s1k305YVP18qbfaonF6MXOH89tuj3WV3vRzavu+9Gwau1Sh80hFUZAu7ob7d posHsah7zh+ARWr8FuaWg+eUdTnxNa8x9N/3HBaa0SrwutIbbkwlqTQY4bXjTqb8TVCJ gauQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682963435; x=1685555435; 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=oNrjh4NDZfCOCA+XspYc7I9kB51AWPXlbM0N3pX4I38=; b=h+R4IxtHIV+b/e051hE6dfTXJnu7U8yCszBBCu7S3Le07QPM9wWJgZa6DlcqKqdMBn NeQqvLfTgj8ICRAY4UvVAIKLJO4tBWWu7LWgOVuXqI+I/Ic9ErRgYtsVnr10zD/lZruU g/2TFJqYWbqLqUFrMubAYx1FfubF6DwltgM2DtlZSCKWkHf8ZDCO14Xizf5G/L5vipT+ OGLcKHj2il+XfMIvheHUG6024vEB6PJzABLswYKriUvt4bEpzv9ZUhOKbWNmD9ZOUW7M WzNmU6TJrehUVHE6145HuBpKU4sXNFrs9GE6VDHQFp3XuyrqycThgBKNSB+GmCw1Qv+N AzSQ== X-Gm-Message-State: AC+VfDziUswvnE6z28r5E3W79c3ZhWumOthIAPrXgZ3XYrMg55fjHaEk OOrWYSg6TzuNLoXfuVgOT1TySK1E4v0= X-Google-Smtp-Source: ACHHUZ6EimADaT391MpwBUkL6hz/B/GfVS5bvY+TKbOxuncQS8LbI8CgIxJ7ch0/4tBeArriD6dOerX5ZI8= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6d24:3efd:facc:7ac4]) (user=surenb job=sendgmr) by 2002:a17:90a:72cc:b0:24d:e14a:6361 with SMTP id l12-20020a17090a72cc00b0024de14a6361mr1794469pjk.3.1682963434788; Mon, 01 May 2023 10:50:34 -0700 (PDT) Date: Mon, 1 May 2023 10:50:25 -0700 In-Reply-To: <20230501175025.36233-1-surenb@google.com> Mime-Version: 1.0 References: <20230501175025.36233-1-surenb@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230501175025.36233-3-surenb@google.com> Subject: [PATCH 3/3] mm: implement folio wait under VMA lock 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, surenb@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0B3501C002B X-Rspam-User: X-Stat-Signature: zre78niwd8comctsntn94mrjnkpbo358 X-HE-Tag: 1682963435-941228 X-HE-Meta: U2FsdGVkX1/5AnWrh3uJEV+3bJiZ2uy2AOZNPZj6ww/Hg/wJAeYHc/CnVd9jL8w48RzJglB+TFsQD8Y+mUPRR0qKqZj8IQIFilpekzvV2YrrWAkY9qPMklQTJsATDMgGdPqjz6u5Lr0kaaLjL7c9P3iYxbrNjXWV7eFTdcrpdT7pNuzriArADpXLRoG/uTSSfgATf66ogZ182yCal4eb3km9KhG0apg1jQiNuXGFrkS94pib6xkGXkCgyp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bucV4Zq78GxLxgIw4+cTAPB7fdxKbEKzDJtPJ1XpfwUpn22WLDD3jmGkIJIAAwvPXj0KIXojooYBeiM9M+pGxLhJFEk5Xf59/eba3pfaYu6hRhXPycYNpC3TjsWwHesiVmitA/tt1pqom4Mn+byW2TvnBSrfkRUKhs7Js1RRtZC2D1vfo1/ktO8Kr/M7Fs1PBqRa7EbzxL1uXbLFu5suACW0hymmTRkwyaoRB/DKJHhFBkNQpVdu60g30dix2KMp1BR/6F/TiZkwwnn1sw6ahQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Follow the same pattern as mmap_lock when waiting for folio by dropping VMA lock before the wait and retrying once folio is available. Signed-off-by: Suren Baghdasaryan --- include/linux/pagemap.h | 14 ++++++++++---- mm/filemap.c | 43 ++++++++++++++++++++++------------------- mm/memory.c | 13 +++++++++---- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index a56308a9d1a4..6c9493314c21 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -896,8 +896,8 @@ 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); +bool __folio_lock_or_retry(struct folio *folio, struct vm_area_struct *vma, + unsigned int flags, bool *lock_dropped); void unlock_page(struct page *page); void folio_unlock(struct folio *folio); @@ -1002,10 +1002,16 @@ static inline int folio_lock_killable(struct folio *folio) * __folio_lock_or_retry(). */ static inline bool folio_lock_or_retry(struct folio *folio, - struct mm_struct *mm, unsigned int flags) + struct vm_area_struct *vma, unsigned int flags, + bool *lock_dropped) { might_sleep(); - return folio_trylock(folio) || __folio_lock_or_retry(folio, mm, flags); + if (folio_trylock(folio)) { + *lock_dropped = false; + return true; + } + + return __folio_lock_or_retry(folio, vma, flags, lock_dropped); } /* diff --git a/mm/filemap.c b/mm/filemap.c index 84f39114d4de..9c0fa8578b2f 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1701,37 +1701,35 @@ static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait) /* * Return values: - * true - folio is locked; mmap_lock is still held. + * true - folio is locked. * false - 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 flags had FAULT_FLAG_VMA_LOCK set, meaning the operation is performed - * with VMA lock only, the VMA lock is still held. + * + * lock_dropped indicates whether mmap_lock/VMA lock got dropped. + * mmap_lock/VMA lock is dropped when function fails to lock the folio, + * unless flags had both FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT + * set, in which case mmap_lock/VMA lock is still held. * * If neither ALLOW_RETRY nor KILLABLE are set, will always return true - * with the folio locked and the mmap_lock unperturbed. + * with the folio locked and the mmap_lock/VMA lock unperturbed. */ -bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, - unsigned int flags) +bool __folio_lock_or_retry(struct folio *folio, struct vm_area_struct *vma, + unsigned int flags, bool *lock_dropped) { - /* Can't do this if not holding mmap_lock */ - if (flags & FAULT_FLAG_VMA_LOCK) - return false; - if (fault_flag_allow_retry_first(flags)) { - /* - * CAUTION! In this case, mmap_lock is not released - * even though return 0. - */ - if (flags & FAULT_FLAG_RETRY_NOWAIT) + if (flags & FAULT_FLAG_RETRY_NOWAIT) { + *lock_dropped = false; return false; + } - mmap_read_unlock(mm); + if (flags & FAULT_FLAG_VMA_LOCK) + vma_end_read(vma); + else + mmap_read_unlock(vma->vm_mm); if (flags & FAULT_FLAG_KILLABLE) folio_wait_locked_killable(folio); else folio_wait_locked(folio); + *lock_dropped = true; return false; } if (flags & FAULT_FLAG_KILLABLE) { @@ -1739,13 +1737,18 @@ bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, ret = __folio_lock_killable(folio); if (ret) { - mmap_read_unlock(mm); + if (flags & FAULT_FLAG_VMA_LOCK) + vma_end_read(vma); + else + mmap_read_unlock(vma->vm_mm); + *lock_dropped = true; return false; } } else { __folio_lock(folio); } + *lock_dropped = false; return true; } diff --git a/mm/memory.c b/mm/memory.c index 8222acf74fd3..e1cd39f00756 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; + bool lock_dropped; /* * We need a reference to lock the folio because we don't hold @@ -3580,8 +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)) { + if (!folio_lock_or_retry(folio, vma, vmf->flags, &lock_dropped)) { folio_put(folio); + if (lock_dropped && vmf->flags & FAULT_FLAG_VMA_LOCK) + return VM_FAULT_VMA_UNLOCKED | VM_FAULT_RETRY; return VM_FAULT_RETRY; } mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, @@ -3704,7 +3707,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) bool exclusive = false; swp_entry_t entry; pte_t pte; - int locked; + bool locked; + bool lock_dropped; vm_fault_t ret = 0; void *shadow = NULL; @@ -3837,9 +3841,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out_release; } - locked = folio_lock_or_retry(folio, vma->vm_mm, vmf->flags); - + locked = folio_lock_or_retry(folio, vma, vmf->flags, &lock_dropped); if (!locked) { + if (lock_dropped && vmf->flags & FAULT_FLAG_VMA_LOCK) + ret |= VM_FAULT_VMA_UNLOCKED; ret |= VM_FAULT_RETRY; goto out_release; }