From patchwork Wed Jun 28 07:17:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13295275 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 1EF07EB64D7 for ; Wed, 28 Jun 2023 07:18:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 067ED8D0009; Wed, 28 Jun 2023 03:18:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0187E8D0001; Wed, 28 Jun 2023 03:18:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E224A8D0009; Wed, 28 Jun 2023 03:18:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D445C8D0001 for ; Wed, 28 Jun 2023 03:18:18 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A5C8980CA3 for ; Wed, 28 Jun 2023 07:18:18 +0000 (UTC) X-FDA: 80951303076.21.F74BA44 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf04.hostedemail.com (Postfix) with ESMTP id D676D40005 for ; Wed, 28 Jun 2023 07:18:16 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=g48V+MJr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3uN6bZAYKCBE9B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3uN6bZAYKCBE9B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687936696; 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=b7MqQHJHdH35CYJ6Jb1zpJ4/eRYK1pcerh43dShkH+M=; b=ZY4+9Rn23YwY7j1mAnEPLodxNFkkA9J6tJfvaE86xbqUKia2z9QxM1l5rZc2+jZkd/v+Cn hndGmMlcBGmc64GjQl6CIMijUWp7hw5f0mDfoBOqcxXcuZMDiEE/Bs5S3ji74W5X7Ogplw FIe1EaVW042jrVP6sLkp6v/0jPF68MI= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=g48V+MJr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3uN6bZAYKCBE9B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3uN6bZAYKCBE9B8v4sx55x2v.t532z4BE-331Crt1.58x@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687936696; a=rsa-sha256; cv=none; b=BSrtSpgeTO5GjTAv2EF9A8D5mvb7PRCV81k4Ro85cQeExkxI0Yw/E1+0tS1JOH6AHbM0hO DIfOLV0gN/XxDaunvSiNfJ8Mi7v6BXyEZSEbF8d3Idn8RQFGwEdgYBoQcdDxrytFxuXCpR C3zdBvGc5w4wT/+ufwZurQB4lQIBuFU= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-565ba5667d5so70500357b3.0 for ; Wed, 28 Jun 2023 00:18:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687936696; x=1690528696; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=b7MqQHJHdH35CYJ6Jb1zpJ4/eRYK1pcerh43dShkH+M=; b=g48V+MJrxb5eSEjyL2iKtN/pPbN1MfYIjbyI7k+Ph5zLX9pOqSy0bI/iSTq8ynaAv2 +dtLaVTZuWBoHtPx+WE9swOOvjc4HEP18ck+X8gyP2hZHEeVhI81gYueHcbww6LxgOMa LsmCB7piWMCI+TEtSFI05OjRmaVr3SNelE0yHo6RvME1z+zbGm3FkxipazXo3FSyR9JG F5ws6BPFBksuHcM0OPJSKH8sgS6VK7KCMdA8CAmdPZUa5sb3DGy1kMimg4nYKRndAJKf p27MRa3E8cwerZeCNy05DHZ/fnLi71GnBj1QTCT1/0e7Mj+wH/0ewNx30+M4kNpzSe4W 4LTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687936696; x=1690528696; 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=b7MqQHJHdH35CYJ6Jb1zpJ4/eRYK1pcerh43dShkH+M=; b=cTvKZyybFlcR+ZdP5HKlHsk0Upt/6unmn6tejxRHrmQXsGhtwznhSxzSAyp235rV9R 8GekFgf60Qg64uB8brwdLUQX2rdY9ZxFyFOgEidMRzqQojaVQX8Vwt1FJ6wjg4XnzFj+ S3wuAHS+g8tLmI2m7XqxL9y1OzK/e+lZEvvmPTPUQly9UyN09Aix68ae52ydEWw8a17n IAEZiKYNqYQWRcoeVjCoJCtlntX/5bL7qyki3PvkdyxhUl1Pe4yL2iuTWQCdYhkNaqBd ypc4DuGovBuRKOrZSyKoaRAn81SjzoRUUnAprJhNNGBVEsT06L9DN7++9jZH+wYWL6QU c0sg== X-Gm-Message-State: AC+VfDyWWjfK+gtAuYjrIoKperwAmITRemZu3u31/GnZAkzcduaGt3Cz 4NQAkbJZTjURf6RWsa8SnoVgqhUizeU= X-Google-Smtp-Source: ACHHUZ5p7K5vwu3GLTcOloeUXHu3uFg6BFvaVURIKfx6u7AbyQoKOvh6cJprBsCrY0QmdnAunXGn4WazRWI= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6664:8bd3:57fd:c83a]) (user=surenb job=sendgmr) by 2002:a81:af22:0:b0:559:c032:eb5e with SMTP id n34-20020a81af22000000b00559c032eb5emr14112427ywh.1.1687936696048; Wed, 28 Jun 2023 00:18:16 -0700 (PDT) Date: Wed, 28 Jun 2023 00:17:59 -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-6-surenb@google.com> Subject: [PATCH v4 5/6] mm: handle swap page faults under per-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, 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-Rspam-User: X-Stat-Signature: r9kdsahzzsdo3rme7hnt9u9h5y5bdq9p X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: D676D40005 X-HE-Tag: 1687936696-382391 X-HE-Meta: U2FsdGVkX1+RBFOcoh94kQ8Ps139um++UYy+0Tmwgitoule5FiPm4utAWSbr7O2PwzfpavQHJwLNaaabq4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O194Vhb1hZvcSnH+2zotA4deim/O6OpB5bdRo444UxbNf+P3kSidNtsXmktpXcNPJiGm4JYv1roPdtmNJJCP5tbiV5nbBwJyMjif0ySYe7cLzQ0zhE9+IL8kp3wuig1G8pXbhGCnT3HgfG5VaEAT5xoYI3f/YG6DcWDU7+sbNuCd0D7YqlgEvP72Lrd0hhItEJd0Wrf4R9nZDOVm+IEmYXK7KWBvADhzzEou3qsTY7pI3dYMevd+HPTNlgp5mpJ+61pSnRHQip6QnA7f5mR8M4uTwABr1TPSWG/7eXORl3YRvXVAS0oiwgOJ0jxzFzP67aSwaXGCRqjx9RZVOO3F2hAEmIklhsjiinD5s6ElURDdVVNDsTDnH+N6J7ufNk386iLNaNX2XR1ZSO+FAV2k6XiX9sCamrVRmcPOHiCLrZulN6HhJcmY6Y84e02ys5xvWYRUc= 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: When page fault is handled under per-VMA lock protection, all swap page faults are retried with mmap_lock because folio_lock_or_retry has to drop and reacquire mmap_lock if folio could not be immediately locked. Follow the same pattern as mmap_lock to drop per-VMA lock when waiting for folio and retrying once folio is available. With this obstacle removed, enable do_swap_page to operate under per-VMA lock protection. Drivers implementing ops->migrate_to_ram might still rely on mmap_lock, therefore we have to fall back to mmap_lock in that particular case. Note that the only time do_swap_page calls synchronous swap_readpage is when SWP_SYNCHRONOUS_IO is set, which is only set for QUEUE_FLAG_SYNCHRONOUS devices: brd, zram and nvdimms (both btt and pmem). Therefore we don't sleep in this path, and there's no need to drop the mmap or per-VMA lock. Signed-off-by: Suren Baghdasaryan Acked-by: Peter Xu --- mm/filemap.c | 25 ++++++++++++++++--------- mm/memory.c | 16 ++++++++++------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 52bcf12dcdbf..7ee078e1a0d2 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1699,31 +1699,38 @@ static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait) return ret; } +static void release_fault_lock(struct vm_fault *vmf) +{ + if (vmf->flags & FAULT_FLAG_VMA_LOCK) + vma_end_read(vmf->vma); + else + mmap_read_unlock(vmf->vma->vm_mm); +} + /* * Return values: * 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. + * mmap_lock or per-VMA lock has been released (mmap_read_unlock() or + * vma_end_read()), unless flags had both FAULT_FLAG_ALLOW_RETRY and + * FAULT_FLAG_RETRY_NOWAIT set, in which case the lock is still held. * * If neither ALLOW_RETRY nor KILLABLE are set, will always return 0 - * with the folio locked and the mmap_lock unperturbed. + * with the folio locked and the mmap_lock/per-VMA lock is left unperturbed. */ 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 VM_FAULT_RETRY. + * CAUTION! In this case, mmap_lock/per-VMA lock is not + * released even though returning VM_FAULT_RETRY. */ if (flags & FAULT_FLAG_RETRY_NOWAIT) return VM_FAULT_RETRY; - mmap_read_unlock(mm); + release_fault_lock(vmf); if (flags & FAULT_FLAG_KILLABLE) folio_wait_locked_killable(folio); else @@ -1735,7 +1742,7 @@ vm_fault_t __folio_lock_or_retry(struct folio *folio, struct vm_fault *vmf) ret = __folio_lock_killable(folio); if (ret) { - mmap_read_unlock(mm); + release_fault_lock(vmf); return VM_FAULT_RETRY; } } else { diff --git a/mm/memory.c b/mm/memory.c index 345080052003..76c7907e7286 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3712,12 +3712,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (!pte_unmap_same(vmf)) goto out; - if (vmf->flags & FAULT_FLAG_VMA_LOCK) { - ret = VM_FAULT_RETRY; - vma_end_read(vma); - goto out; - } - entry = pte_to_swp_entry(vmf->orig_pte); if (unlikely(non_swap_entry(entry))) { if (is_migration_entry(entry)) { @@ -3727,6 +3721,16 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) vmf->page = pfn_swap_entry_to_page(entry); ret = remove_device_exclusive_entry(vmf); } else if (is_device_private_entry(entry)) { + if (vmf->flags & FAULT_FLAG_VMA_LOCK) { + /* + * migrate_to_ram is not yet ready to operate + * under VMA lock. + */ + vma_end_read(vma); + ret |= VM_FAULT_RETRY; + goto out; + } + vmf->page = pfn_swap_entry_to_page(entry); vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl);