Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp18884332rwd; Wed, 28 Jun 2023 01:55:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5UI9E+635Jufw9DbSq6FC4+nVPkJQrJWEL1AGa+Ed2YTby4Pju2XnW1YtHm2nln2JvAEtP X-Received: by 2002:a05:6402:43c6:b0:51d:d1af:df15 with SMTP id p6-20020a05640243c600b0051dd1afdf15mr600479edc.23.1687942531749; Wed, 28 Jun 2023 01:55:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687942531; cv=none; d=google.com; s=arc-20160816; b=sSCC+yGPlhUiQnEqTKKJczh0VYpYWY5CRpfZQd02xHMFWQb3GwIo8oOarLaUMkTRjD oIov9pUE/cBs47MXclF0o43SE01Gh8EVWuk2N0W5CD5MiTes4jRBA+OrrKggcDeGJaYc 9C4Mu8pD6Kll50i5v0EFPxhO7kgpADNPABy7LmHmwnZUBggUNsATWBd70nuxpe4gswVA 0cKbKrv0y7Xmnlwvg0bVDJSGM6dwy30scR6Rif5kjlZeI9O871k1qMCdFjYPrxSgsUOH 1xrGw9qc+CM6gvdTUqBxL+5cw/Y5m2FtNdds4+Pp9kX4EN9yXTeJliEZjxxIoV84MCoy Xl5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=b7MqQHJHdH35CYJ6Jb1zpJ4/eRYK1pcerh43dShkH+M=; fh=RMSYO7NH5mgRq+Q9Sd8uvtKjjFjspH3ZqZT6M3GUwas=; b=tA2EFUxd3mMGFI0yC1hrNO9heXISsZrdqjlfxqkEWK9ReoCTIfcMTBTPdi016ColDn vm+XV11e5Uoe3g5bAbOhnbMUgz6FcQAz0izG5ddDmV6Z7BBF69CnGoLqPv+XiYftH+zY ZpYMYgNvDcd88leftt9r3xI4X+NW/Ysmc8A0TI9LTwnVQje922VFIV5iB/jd0YZ5RIEQ 1wC9zpCkwMZwLBD0+cetUfXxVyHXgxH7G8Ji1f8+kHa5oh23EnoaeauxZ6WAJ+ZzEEvK FvrMSbMuZooiwcLPTQwrDlT9NJg0R6wByA7ln+WBbKu1VC54U/JQEgVPi6Y0e9Edqovh 1g5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=5ask6lvB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l6-20020aa7d946000000b0051a59360fdbsi5074726eds.514.2023.06.28.01.55.07; Wed, 28 Jun 2023 01:55:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=5ask6lvB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233764AbjF1I3N (ORCPT + 99 others); Wed, 28 Jun 2023 04:29:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234754AbjF1I06 (ORCPT ); Wed, 28 Jun 2023 04:26:58 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 906C93A90 for ; Wed, 28 Jun 2023 01:19:32 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id d75a77b69052e-402cd359b19so12516041cf.2 for ; Wed, 28 Jun 2023 01:19:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687940371; x=1690532371; 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=5ask6lvBO8MfRNdFMGTOVpDmCKYeI7+nECY3mTNiIVt5DulFZta5fENZSDbFabf8Vk KUl1QkUb9usX/GJIbs6FRzUQfWi7+b0k+mtWKEobFX/IjjXb6egqu8/Si/rqQSNoPvzP Gvorcbbe4/BCVRMXx0m4kN/o84Ghp7oZw1clfye79NjkXM/xrlWjONM2CtBa/WQtj930 2W8OXisAh35+ZPW5kfXTZXeU8C6LaNm5MOinvi749Zl8mf4C1vs4sMAxxTcLIq+ru3Oe 8h0E5L6eo4l/rsc4f99jkMxIbgRlh5ruFcFAJF3QINu9bmfc1lBZJ6SRrbfg8aRANGdJ 8iuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687940371; x=1690532371; 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=ZKtKPF1QMtZIPs4qAUwbQqAyfnywL3QqGMvpiPuFlY7za24+xs7zpmxpj3b+rhHOzf CtqguM6MR3R+LSIY5IQU6ankgZq+1apFIddmMgrAZbkvJXD4K9Qzb964ntIU7NAE9NEU IqbkMYl301NtJZq6+gH6rQNkJmpXbpgiuJknGK2aS6OKjKNvcL5lVSlMYaXncfuh4cA2 IrCTaxrHl8M+eKKfDYg/9yM+K4WKYPUv9BFJD28FiBQpZPAkPbH6DjNPIccjUo3xBQKT bsVfclCnYpBn72LW0R3wUKJ6kmHHdDg9tdtDNCcxVvH1Ne4RgbOBR8HyF6eUSnPcQJNS pCMA== X-Gm-Message-State: AC+VfDyE/MxczDOvAc192ilUTCkfsCp1gl9Kn0X53mBx32l+WLR/4Tyk U1GE+RPZhQI9Bs9ZRDOtT5Ukw0NFjK0= 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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); -- 2.41.0.162.gfafddb0af9-goog