From patchwork Fri Jun 30 02:04:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13297504 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04B08C001B0 for ; Fri, 30 Jun 2023 02:04:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231899AbjF3CEr (ORCPT ); Thu, 29 Jun 2023 22:04:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229493AbjF3CEo (ORCPT ); Thu, 29 Jun 2023 22:04:44 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07E501FE7 for ; Thu, 29 Jun 2023 19:04:43 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-56938733c13so11684157b3.1 for ; Thu, 29 Jun 2023 19:04:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688090682; x=1690682682; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=T+U3NPNy+xyAlbM+feS8QAyMObapdpoy5/9PcF9TKLI=; b=rvyDGGahsbSARiQKNJBvs5NaGYjXW8NZzm9dcWursQkpPyYTW87vOp0jaL/v9wFn72 bVCnSMsCL0Uww5Cz2gl4BTCbB3YOvEPgid/Pz/LzO8PDWuvfrJ2G8Jiylai9nQSwghqV XTPn7m5a6XLji4dmREhc1FM19R1MCvpV3tbEnuzVLNravKUeqSKCb2E7NDFaKDZgK3oA 59VRX6qKLr9O5DBQxMvgWTxqC8FOrrGsqmH7OYYKLdSnJ0MiFxDdJVnxpCvmqod/rmwQ xQnRBu3/xHvFBzDvleXzw2kra/qngRMyB/qMKcD/nmpoEBHDjoyDF9TWDoyXcI8GVjIk 1b0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688090682; x=1690682682; 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=T+U3NPNy+xyAlbM+feS8QAyMObapdpoy5/9PcF9TKLI=; b=gQ0Cb7lrk1zfB9Hx5rxdnSxVepylumoeBfrxwrcvW2F8wrWjz0XD0JMQ7tWMyh72ly 2iCkUkhzUbMHLBiiQTrhHxgH9h62Z7jbra5k0yohJSz/uVhY1eYnvuy+P9vlXvbCczVQ KhFPnEOo5ss4OKrrEhQcChfQr3uKgXxjvySw6H50rfFKrl6Q5A2Q+2b2KdWU40ucBElu 5URi+nHkdIngN11fHlZ3fDfm5ELjYncxAISZhoCki1qsyNhYQuU0txxrE+HeJdcZ3tj8 5fbpQRoJiH+h4I6fdFKojMzNs2CZcVyDn7lmEanwNCvrPvkDqYtEEwklanVlTWpdsV1d uSzg== X-Gm-Message-State: ABy/qLbXNvsnM33GCGBI/miMqOPJYuvs0l3ZlSqWxV6ggKJYtx2jcwLH H60El8ulX1XyNTc1nje9vBNyQVu0IeA= X-Google-Smtp-Source: APBJJlFHuVnY9FeKGILHpU4FVRKk28e82VnRW+1SQlAaSM9Gd4WVwIdiE5OHAJvPzkmeNDyyPG7JbAA7D7A= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:1f11:a3d0:19a9:16e5]) (user=surenb job=sendgmr) by 2002:a0d:db8a:0:b0:576:f208:4d91 with SMTP id d132-20020a0ddb8a000000b00576f2084d91mr8639ywe.4.1688090682204; Thu, 29 Jun 2023 19:04:42 -0700 (PDT) Date: Thu, 29 Jun 2023 19:04:30 -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-2-surenb@google.com> Subject: [PATCH v6 1/6] swap: remove remnants of polling from read_swap_cache_async 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, Christoph Hellwig Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Commit [1] introduced IO polling support duding swapin to reduce swap read latency for block devices that can be polled. However later commit [2] removed polling support. Therefore it seems safe to remove do_poll parameter in read_swap_cache_async and always call swap_readpage with synchronous=false waiting for IO completion in folio_lock_or_retry. [1] commit 23955622ff8d ("swap: add block io poll in swapin path") [2] commit 9650b453a3d4 ("block: ignore RWF_HIPRI hint for sync dio") Suggested-by: "Huang, Ying" Signed-off-by: Suren Baghdasaryan Reviewed-by: "Huang, Ying" Reviewed-by: Christoph Hellwig --- mm/madvise.c | 4 ++-- mm/swap.h | 1 - mm/swap_state.c | 12 +++++------- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 886f06066622..ac6d92f74f6d 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -218,7 +218,7 @@ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start, ptep = NULL; page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE, - vma, addr, false, &splug); + vma, addr, &splug); if (page) put_page(page); } @@ -262,7 +262,7 @@ static void shmem_swapin_range(struct vm_area_struct *vma, rcu_read_unlock(); page = read_swap_cache_async(entry, mapping_gfp_mask(mapping), - vma, addr, false, &splug); + vma, addr, &splug); if (page) put_page(page); diff --git a/mm/swap.h b/mm/swap.h index 7c033d793f15..8a3c7a0ace4f 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -46,7 +46,6 @@ struct folio *filemap_get_incore_folio(struct address_space *mapping, struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct vm_area_struct *vma, unsigned long addr, - bool do_poll, struct swap_iocb **plug); struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct vm_area_struct *vma, diff --git a/mm/swap_state.c b/mm/swap_state.c index f8ea7015bad4..5a690c79cc13 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -527,15 +527,14 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, */ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct vm_area_struct *vma, - unsigned long addr, bool do_poll, - struct swap_iocb **plug) + unsigned long addr, struct swap_iocb **plug) { bool page_was_allocated; struct page *retpage = __read_swap_cache_async(entry, gfp_mask, vma, addr, &page_was_allocated); if (page_was_allocated) - swap_readpage(retpage, do_poll, plug); + swap_readpage(retpage, false, plug); return retpage; } @@ -630,7 +629,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, struct swap_info_struct *si = swp_swap_info(entry); struct blk_plug plug; struct swap_iocb *splug = NULL; - bool do_poll = true, page_allocated; + bool page_allocated; struct vm_area_struct *vma = vmf->vma; unsigned long addr = vmf->address; @@ -638,7 +637,6 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, if (!mask) goto skip; - do_poll = false; /* Read a page_cluster sized and aligned cluster around offset. */ start_offset = offset & ~mask; end_offset = offset | mask; @@ -670,7 +668,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, lru_add_drain(); /* Push any new pages onto the LRU now */ skip: /* The page was likely read above, so no need for plugging here */ - return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll, NULL); + return read_swap_cache_async(entry, gfp_mask, vma, addr, NULL); } int init_swap_address_space(unsigned int type, unsigned long nr_pages) @@ -838,7 +836,7 @@ static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask, skip: /* The page was likely read above, so no need for plugging here */ return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address, - ra_info.win == 1, NULL); + NULL); } /** From patchwork Fri Jun 30 02:04:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13297505 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7188FC001B3 for ; Fri, 30 Jun 2023 02:04:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230525AbjF3CEv (ORCPT ); Thu, 29 Jun 2023 22:04:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231882AbjF3CEq (ORCPT ); Thu, 29 Jun 2023 22:04:46 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CF9A1991 for ; Thu, 29 Jun 2023 19:04:45 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-561eb6c66f6so11566637b3.0 for ; Thu, 29 Jun 2023 19:04:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688090684; x=1690682684; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wxpT0wtXiDyXAkq9m4Ft7pVFAXex2TsyXSa3a6xpvEY=; b=O6qrIABFeVKumQb4z+O4Rwoiry/zCXrDFmQqF+Gf2qqpRo3dr3IuEVNfNulzR2+kK5 e2f359XuQFnf5KHHVWh0sdLfRObOSqXo2wiGLj6SYF/QVu5Ag1Xc42AsiOD4EFRVI3D/ 6WXUEdxIEuyZ0pcXaLJ6wNwP83t6XrfWjBfwoYQvysnINji0qB8vghNiKOp740Ul7Ukr kmeYyHgwQ/HkRJAGRDgQT17Uk6XRL3V+GxjQ5EJ8pKYtf2xZ9C7jcOmmmprH0oLA/TvH VfRZ7vZRznwMdcR48dtpA/zVhSiR/0T69uIXQOd6IX6RY44wnsHcF9ERl0Frpjr5dnO5 f7aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688090684; x=1690682684; 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=wxpT0wtXiDyXAkq9m4Ft7pVFAXex2TsyXSa3a6xpvEY=; b=JrlmYfsl066RGSFt+GD5+O18nj4MV2tU9mNezwkLnnI//JhiP/djovhtM+39NXsiG0 H42BRKOSBUU8kAZoS77s32ca+me0qaahx1jqIugnb4ZC5vpZMCe21aVbD3gIDaGBsQ43 7CV/icIOojMKZXq/XZwk0SOMrImkjpBwNDYwYorB2GKOmGvbpu/qATf32EyWdN6BZkAV hCqRmOmT+QCOgqNAqlXMCIWZjrr1/NdqSRZN+PM0ssnSXNaV/6V8vf+mDu44Fjxp/jgp 0QfUm30HRhsdaSu3rYVodDLU7AiXC+7Tz9MZfpwTqxVWeItEOmGfJi4eIaNCmflZPv4B u2gQ== X-Gm-Message-State: ABy/qLbKJ02KMrq1W+qP7SlbgNxcR6XknqxcVYLNA9A3dTpNos/1Unkn oLtB2SfXsndfUe/BCR0lk2U339YjnF8= X-Google-Smtp-Source: APBJJlFlO+/FPiatjPAMJcuTJU5ZkETGLKXrSfIwtCQca/pP1cHpPw/0ejIEECjWBi0brJ9r19L8sTxC/BM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:1f11:a3d0:19a9:16e5]) (user=surenb job=sendgmr) by 2002:a81:d106:0:b0:576:b85b:7bd1 with SMTP id w6-20020a81d106000000b00576b85b7bd1mr6970ywi.9.1688090684551; Thu, 29 Jun 2023 19:04:44 -0700 (PDT) Date: Thu, 29 Jun 2023 19:04:31 -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-3-surenb@google.com> Subject: [PATCH v6 2/6] mm: add missing VM_FAULT_RESULT_TRACE name for VM_FAULT_COMPLETED 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org VM_FAULT_RESULT_TRACE should contain an element for every vm_fault_reason to be used as flag_array inside trace_print_flags_seq(). The element for VM_FAULT_COMPLETED is missing, add it. Signed-off-by: Suren Baghdasaryan Reviewed-by: Peter Xu --- include/linux/mm_types.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index de10fc797c8e..39cd34b4dbaa 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1077,7 +1077,8 @@ enum vm_fault_reason { { VM_FAULT_RETRY, "RETRY" }, \ { VM_FAULT_FALLBACK, "FALLBACK" }, \ { VM_FAULT_DONE_COW, "DONE_COW" }, \ - { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" } + { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ + { VM_FAULT_COMPLETED, "COMPLETED" } struct vm_special_mapping { const char *name; /* The name, e.g. "[vdso]". */ From patchwork Fri Jun 30 02:04:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13297506 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7530BEB64DD for ; Fri, 30 Jun 2023 02:05:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232029AbjF3CE7 (ORCPT ); Thu, 29 Jun 2023 22:04:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231927AbjF3CEt (ORCPT ); Thu, 29 Jun 2023 22:04:49 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A6DC19A9 for ; Thu, 29 Jun 2023 19:04:47 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c3d29d2fdffso978749276.2 for ; Thu, 29 Jun 2023 19:04:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688090687; x=1690682687; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=V7p+xkeFpHpY+8imX6dpZwoTYVp943eibQ5qK+rLf3c=; b=DUV7BVM9nFkRrG4OmL2z1rbzNQPYA08eD/W6CM9ZTP8ycZScg2wT7L7+zDm47i3EEp YYXhyTKoKtmqRcvzOyTyV44bllZXd+7i1AMkEKCnp9VhDpEzAvKcS+2YEZK4NAZsYcoe y/bdw210HSJrZUvj1zbrUaU+YWomeS88ogbqihRlgl+T+b4TNg18Gr+BlED4NnaQkkkw f1IhR/XhDFUZ7ovgt4RYv6+3pQDpmsTR7as7++jPBwf7pFCGJ+iXgGbx70KDjUWJPZkG Jf9I49Iy4KlltuDt6TFCOy2p6d0dzZ9NTdp0L/acX6A4axhyfobo5rKVJIXrfSI6c/Al EGHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688090687; x=1690682687; 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=V7p+xkeFpHpY+8imX6dpZwoTYVp943eibQ5qK+rLf3c=; b=TdmkwIo/Xlw0lIiyv6hEI9Z5pf/l/3O6dOkS0flKR+zfDfJVwYGia3d1Xf/I6MaEkp W12x6GBAqbAEULc/6oDSz3W2yE/wxSRkYGGlxrx0NDyTiQ/Hey4Mtiqx8aOy2VUywDYB W/LkJnq3dB1qmBSxmKlvqb2D5ch0jw1atrCunP2ZVi1vpVC7rxtfmeCQ5JlFJWpPeEwh rH2vjG5mtEbyn3mnP901OJub3r6MymSbO1wAG0LkrjcfH11+1EOuxnN7lWsRcH01C0Qm IFhth/PkVSbN90YI3ZbF4ZEEpyYI5RIX9M62h0PXlNOfqR5Woeuq2TwMEpg/39XtzjzS pqPQ== X-Gm-Message-State: ABy/qLY4mlEYGOxlxZPhXdHkbh4rl6FgzHPSw6fzpOFJxeG9PAGget+D Rz3acoRnk9SarciB1HIc0CtOfnTO++4= X-Google-Smtp-Source: APBJJlEey6/2V3MIX/R8fPAMY5b+xzPIZFVw7itp9GblxpDJfWRGEmOCyuEH2lOm02vp1cSgPAo6FSaIDcg= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:1f11:a3d0:19a9:16e5]) (user=surenb job=sendgmr) by 2002:a5b:a01:0:b0:c1d:d3e6:d52e with SMTP id k1-20020a5b0a01000000b00c1dd3e6d52emr10821ybq.0.1688090686807; Thu, 29 Jun 2023 19:04:46 -0700 (PDT) Date: Thu, 29 Jun 2023 19:04:32 -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-4-surenb@google.com> Subject: [PATCH v6 3/6] mm: drop per-VMA lock when returning VM_FAULT_RETRY or VM_FAULT_COMPLETED 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org handle_mm_fault returning VM_FAULT_RETRY or VM_FAULT_COMPLETED means mmap_lock has been released. However with per-VMA locks behavior is different and the caller should still release it. To make the rules consistent for the caller, drop the per-VMA lock when returning VM_FAULT_RETRY or VM_FAULT_COMPLETED. Currently the only path returning VM_FAULT_RETRY under per-VMA locks is do_swap_page and no path returns VM_FAULT_COMPLETED for now. Signed-off-by: Suren Baghdasaryan Acked-by: Peter Xu --- arch/arm64/mm/fault.c | 3 ++- arch/powerpc/mm/fault.c | 3 ++- arch/s390/mm/fault.c | 3 ++- arch/x86/mm/fault.c | 3 ++- mm/memory.c | 12 ++++++++++++ 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 935f0a8911f9..9d78ff78b0e3 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -602,7 +602,8 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, goto lock_mmap; } fault = handle_mm_fault(vma, addr, mm_flags | FAULT_FLAG_VMA_LOCK, regs); - vma_end_read(vma); + if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED))) + vma_end_read(vma); if (!(fault & VM_FAULT_RETRY)) { count_vm_vma_lock_event(VMA_LOCK_SUCCESS); diff --git a/arch/powerpc/mm/fault.c b/arch/powerpc/mm/fault.c index 5bfdf6ecfa96..82954d0e6906 100644 --- a/arch/powerpc/mm/fault.c +++ b/arch/powerpc/mm/fault.c @@ -489,7 +489,8 @@ static int ___do_page_fault(struct pt_regs *regs, unsigned long address, } fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs); - vma_end_read(vma); + if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED))) + vma_end_read(vma); if (!(fault & VM_FAULT_RETRY)) { count_vm_vma_lock_event(VMA_LOCK_SUCCESS); diff --git a/arch/s390/mm/fault.c b/arch/s390/mm/fault.c index dbe8394234e2..40a71063949b 100644 --- a/arch/s390/mm/fault.c +++ b/arch/s390/mm/fault.c @@ -418,7 +418,8 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access) goto lock_mmap; } fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs); - vma_end_read(vma); + if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED))) + vma_end_read(vma); if (!(fault & VM_FAULT_RETRY)) { count_vm_vma_lock_event(VMA_LOCK_SUCCESS); goto out; diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index e8711b2cafaf..56b4f9faf8c4 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1341,7 +1341,8 @@ void do_user_addr_fault(struct pt_regs *regs, goto lock_mmap; } fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs); - vma_end_read(vma); + if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED))) + vma_end_read(vma); if (!(fault & VM_FAULT_RETRY)) { count_vm_vma_lock_event(VMA_LOCK_SUCCESS); diff --git a/mm/memory.c b/mm/memory.c index 0ae594703021..5f26c56ce979 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3730,6 +3730,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { ret = VM_FAULT_RETRY; + vma_end_read(vma); goto out; } @@ -5182,6 +5183,17 @@ static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma, !is_cow_mapping(vma->vm_flags))) return VM_FAULT_SIGSEGV; } +#ifdef CONFIG_PER_VMA_LOCK + /* + * Per-VMA locks can't be used with FAULT_FLAG_RETRY_NOWAIT because of + * the assumption that lock is dropped on VM_FAULT_RETRY. + */ + if (WARN_ON_ONCE((*flags & + (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)) == + (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT))) + return VM_FAULT_SIGSEGV; +#endif + return 0; } 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: 13297508 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72E0BEB64D9 for ; Fri, 30 Jun 2023 02:05:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232111AbjF3CFN (ORCPT ); Thu, 29 Jun 2023 22:05:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231963AbjF3CEv (ORCPT ); Thu, 29 Jun 2023 22:04:51 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BB622707 for ; Thu, 29 Jun 2023 19:04:50 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-57003dac4a8so19794197b3.1 for ; Thu, 29 Jun 2023 19:04:50 -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=IMG7y9Z7FJUrcOkeT478B5hKOyNJ67nQXICelLPev9GP7gcM7OxZ2j8vK8VaB77JuX PJ0pkagpkKuDIGWQOEmsQlaOCrcwFsmRMeQ/gfB3ryHigivlFTdKyRxNV4gjX6SAb7/I dCHlFVqZ9H1FCYV2tWFI59oJUrj/71ziVYUCIV2uRdGNEGVCo+tL092In5EhfWaNQbPC BsW1VtaXTwQx3o7tLO4zAj2I8mLWVkQJAieZM+dE/yKNv9Q8fyLFUvoYw/m4kNYJCqjS 2IJsebThpkxI4q8ZAHwGikjBvRsrsOaIhNrhP9qv6T+ZYwZaMK9mlCG9h/EBecEKghCv BPjw== X-Gm-Message-State: ABy/qLame5/sT0ZR6QlWKm7KRpYUjLY+VrIrVxVmLS6WXmuTVaHfFKI+ jkhiN8nNKXeF9DripjbblfzY2FOBrdM= 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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) { /* From patchwork Fri Jun 30 02:04:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13297507 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE38CEB64DD for ; Fri, 30 Jun 2023 02:05:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232099AbjF3CFL (ORCPT ); Thu, 29 Jun 2023 22:05:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231969AbjF3CEx (ORCPT ); Thu, 29 Jun 2023 22:04:53 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40B8D2D55 for ; Thu, 29 Jun 2023 19:04:52 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c38a4d22b39so1184917276.0 for ; Thu, 29 Jun 2023 19:04:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688090691; x=1690682691; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Gu0VoANOHpdn98KZ5A8oxTD1IO6x9vVXdvuQ30VhewY=; b=k2hpUcvzZlE/wuJXtV9O7dL4UXXxuhDpxzQcAiputSPknItCWluXlZUxpoRBIZi4+h KPN0+q/TUs10CzJQOKd+bOTOon7y7djgB0shcBBLBkGelsKwDLz9YUoVX7yZo65DufTY P4JkhlOyxl+5Y4sHtisEscpkNUPQkRg0YJdrsF7TLvPZPYDazeDOXw56kQkG6lk7IMOb LIpjhA2ZqK8avd82tEbvK3qi2QqZXfHhXxV0tM1dYGuNxYmBSvT99J+b02I50CSXB1re Y5/Z9eDJjxBwbtZTVpuSEiPowUAnw0SnBR6/RuHq31FRA8xTlhoG8GfqRwahTeOntn1C rK+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688090691; x=1690682691; 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=Gu0VoANOHpdn98KZ5A8oxTD1IO6x9vVXdvuQ30VhewY=; b=JWqLtRtzQ9apwXMaLYz7oonjGb9bDeT38Wb4EbfmQyD7AJc8tf+qHuwItpocKWH8KS LG8KkMDRHdb7eqkSTMJ1m5rEDe92zj6Q5IR5yTYYT0m8aCk8mf9/jt4/+2Dxv2464aPV cX/H5e2KTO+IXWMyFJsIYxjNu3rAQRrS3xJMJ2/879BI72HVctpdLnZu+pt7bRRCOpOr 0pIvhTvxgPSBPQVP/30R+U5rdMadMslbvAWfFxBYo2oQxaAinCK+QKop8J5Y45Xy8+3+ n3KtZvlgvINGtPg/CtMBgTKWsfsJ/yO5OKPaeEZRFP3dAgEExd4nTWe2+POTke4/CbxI KmEw== X-Gm-Message-State: ABy/qLYyQmC9rWgx5rhrzHI8IxBT0k3J179Bmk2ce/5fD6JaXuhpFEPa WDzYQ4e0TfNHM1QLPo26m/w4oPRKGFw= X-Google-Smtp-Source: APBJJlGKtiVgOdnPeKYm+NCCAggIfmI1+89sKApFSCd6E4GMafU0mEfZRNDPkjtpY2CHw7a1YIb4IaOz4KU= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:1f11:a3d0:19a9:16e5]) (user=surenb job=sendgmr) by 2002:a25:7495:0:b0:bc5:f7fe:9e7d with SMTP id p143-20020a257495000000b00bc5f7fe9e7dmr11606ybc.11.1688090691484; Thu, 29 Jun 2023 19:04:51 -0700 (PDT) Date: Thu, 29 Jun 2023 19:04:34 -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-6-surenb@google.com> Subject: [PATCH v6 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@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 Tested-by: Alistair Popple Reviewed-by: Alistair Popple Acked-by: Peter Xu --- include/linux/mm.h | 13 +++++++++++++ mm/filemap.c | 17 ++++++++--------- mm/memory.c | 16 ++++++++++------ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 39aa409e84d5..54ab11214f4f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -720,6 +720,14 @@ static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) vma->detached = detached; } +static inline 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); +} + struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, unsigned long address); @@ -735,6 +743,11 @@ static inline void vma_assert_write_locked(struct vm_area_struct *vma) {} static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) {} +static inline void release_fault_lock(struct vm_fault *vmf) +{ + mmap_read_unlock(vmf->vma->vm_mm); +} + #endif /* CONFIG_PER_VMA_LOCK */ /* diff --git a/mm/filemap.c b/mm/filemap.c index d245bb4f7153..6f4a3d83a073 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1671,27 +1671,26 @@ static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait) * 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 @@ -1703,7 +1702,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 4ae3f046f593..bb0f68a73b0c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3729,12 +3729,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)) { @@ -3744,6 +3738,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); From patchwork Fri Jun 30 02:04:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13297509 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 388F7EB64DD for ; Fri, 30 Jun 2023 02:05:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232068AbjF3CF2 (ORCPT ); Thu, 29 Jun 2023 22:05:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231950AbjF3CFK (ORCPT ); Thu, 29 Jun 2023 22:05:10 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99A013591 for ; Thu, 29 Jun 2023 19:04:54 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5707177ff8aso12069657b3.2 for ; Thu, 29 Jun 2023 19:04:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688090693; x=1690682693; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CYZzVVGdFW3BZ7BX39EfN49IRv4dv2Sl4PMSdQqxDi4=; b=OfSNNJfPU4LwQ+4p9lDzr7d1rFDuCbznmXwZGpFoQOb9UUudIpyItFW/YnfzN2Uh76 17QZyg1dnJZ0RJOiYlJksJqf+ObRNyfur60YXCCTmzLOEpNbJptma3iOBWfggxbFypeQ SuvH+35aKNV8vVQgv8xFp3l8bhmps0ul+sZaLrf42oWdSvTkXAXDfwilgWGr0jerAG4l sSY5toJRTgjNO5CA+/+11sgpLNowGp96hGOd7L/hLE+sdwuDraoAAgdGjVBoc+inBYNF /ADidYiQiiUKM6I0fDPL0+DkQgUrxvrePdheW3EmveHdBrFadg1h+kL02l0hzoDZg27p 8OYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688090693; x=1690682693; 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=CYZzVVGdFW3BZ7BX39EfN49IRv4dv2Sl4PMSdQqxDi4=; b=PltF1S5thvkxBsQm2coxImXJ0iYIuF2RrgUCHfLEJbtJkuwz/kZxQgkvWWgjiDoUbm QzaUsuUZYQpqsM9Grf2izHJn5VN5zJqnn65FdfwpfIsiCWF1IPPrxrBWD6Eb0XbO3pmN 5C5c8A+0N6VCDu20XxOYAg9DZcq7M9NEtdXHUAlJx9KvJbXKNMUdXbq5bOAu8gP+U8KE qMyp20XWSuuPwDcy1rByflOegEy96BtoB4DjVw3SReWruWOzKnabPVpbiwZytxosM+z/ ait85PgrF0i1xJ+tuGxdH46WvfXt6/KiMjrTB+LmtOUJu0NngUSMG0LivAjM3mV/wVJt WLjQ== X-Gm-Message-State: ABy/qLZC/V2+chXZ5w/4ggK04wT5r3qHjLBH3CoohoLMXnlBCh4ttgLG DqN6ISxz+hHvlkRyZLkQ11igozea59I= X-Google-Smtp-Source: APBJJlHphA7u0fNQhe1Hsg8YKxbjFOsjCu3koFUTN0LEhcBBqL04HvYnu+QQsZhyytzMLSCp+6sbkkOtcng= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:1f11:a3d0:19a9:16e5]) (user=surenb job=sendgmr) by 2002:a25:4183:0:b0:c1e:f91c:2691 with SMTP id o125-20020a254183000000b00c1ef91c2691mr12237yba.10.1688090693727; Thu, 29 Jun 2023 19:04:53 -0700 (PDT) Date: Thu, 29 Jun 2023 19:04:35 -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-7-surenb@google.com> Subject: [PATCH v6 6/6] mm: handle userfaults 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, 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Enable handle_userfault to operate under VMA lock by releasing VMA lock instead of mmap_lock and retrying. Note that FAULT_FLAG_RETRY_NOWAIT should never be used when handling faults under per-VMA lock protection because that would break the assumption that lock is dropped on retry. Signed-off-by: Suren Baghdasaryan Acked-by: Peter Xu --- fs/userfaultfd.c | 34 ++++++++++++++-------------------- include/linux/mm.h | 24 ++++++++++++++++++++++++ mm/memory.c | 9 --------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 7cecd49e078b..21a546eaf9f7 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -277,17 +277,16 @@ static inline struct uffd_msg userfault_msg(unsigned long address, * hugepmd ranges. */ static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, - struct vm_area_struct *vma, - unsigned long address, - unsigned long flags, - unsigned long reason) + struct vm_fault *vmf, + unsigned long reason) { + struct vm_area_struct *vma = vmf->vma; pte_t *ptep, pte; bool ret = true; - mmap_assert_locked(ctx->mm); + assert_fault_locked(vmf); - ptep = hugetlb_walk(vma, address, vma_mmu_pagesize(vma)); + ptep = hugetlb_walk(vma, vmf->address, vma_mmu_pagesize(vma)); if (!ptep) goto out; @@ -308,10 +307,8 @@ static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, } #else static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, - struct vm_area_struct *vma, - unsigned long address, - unsigned long flags, - unsigned long reason) + struct vm_fault *vmf, + unsigned long reason) { return false; /* should never get here */ } @@ -325,11 +322,11 @@ static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, * threads. */ static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, - unsigned long address, - unsigned long flags, + struct vm_fault *vmf, unsigned long reason) { struct mm_struct *mm = ctx->mm; + unsigned long address = vmf->address; pgd_t *pgd; p4d_t *p4d; pud_t *pud; @@ -338,7 +335,7 @@ static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, pte_t ptent; bool ret = true; - mmap_assert_locked(mm); + assert_fault_locked(vmf); pgd = pgd_offset(mm, address); if (!pgd_present(*pgd)) @@ -440,7 +437,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) * Coredumping runs without mmap_lock so we can only check that * the mmap_lock is held, if PF_DUMPCORE was not set. */ - mmap_assert_locked(mm); + assert_fault_locked(vmf); ctx = vma->vm_userfaultfd_ctx.ctx; if (!ctx) @@ -556,15 +553,12 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) spin_unlock_irq(&ctx->fault_pending_wqh.lock); if (!is_vm_hugetlb_page(vma)) - must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags, - reason); + must_wait = userfaultfd_must_wait(ctx, vmf, reason); else - must_wait = userfaultfd_huge_must_wait(ctx, vma, - vmf->address, - vmf->flags, reason); + must_wait = userfaultfd_huge_must_wait(ctx, vmf, reason); if (is_vm_hugetlb_page(vma)) hugetlb_vma_unlock_read(vma); - mmap_read_unlock(mm); + release_fault_lock(vmf); if (likely(must_wait && !READ_ONCE(ctx->released))) { wake_up_poll(&ctx->fd_wqh, EPOLLIN); diff --git a/include/linux/mm.h b/include/linux/mm.h index 54ab11214f4f..2794225b2d42 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -705,6 +705,17 @@ static inline bool vma_try_start_write(struct vm_area_struct *vma) return true; } +static inline void vma_assert_locked(struct vm_area_struct *vma) +{ + int mm_lock_seq; + + if (__is_vma_write_locked(vma, &mm_lock_seq)) + return; + + lockdep_assert_held(&vma->vm_lock->lock); + VM_BUG_ON_VMA(!rwsem_is_locked(&vma->vm_lock->lock), vma); +} + static inline void vma_assert_write_locked(struct vm_area_struct *vma) { int mm_lock_seq; @@ -728,6 +739,14 @@ static inline void release_fault_lock(struct vm_fault *vmf) mmap_read_unlock(vmf->vma->vm_mm); } +static inline void assert_fault_locked(struct vm_fault *vmf) +{ + if (vmf->flags & FAULT_FLAG_VMA_LOCK) + vma_assert_locked(vmf->vma); + else + mmap_assert_locked(vmf->vma->vm_mm); +} + struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, unsigned long address); @@ -748,6 +767,11 @@ static inline void release_fault_lock(struct vm_fault *vmf) mmap_read_unlock(vmf->vma->vm_mm); } +static inline void assert_fault_locked(struct vm_fault *vmf) +{ + mmap_assert_locked(vmf->vma->vm_mm); +} + #endif /* CONFIG_PER_VMA_LOCK */ /* diff --git a/mm/memory.c b/mm/memory.c index bb0f68a73b0c..d9f36f9392a9 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5407,15 +5407,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, if (!vma_start_read(vma)) goto inval; - /* - * Due to the possibility of userfault handler dropping mmap_lock, avoid - * it for now and fall back to page fault handling under mmap_lock. - */ - if (userfaultfd_armed(vma)) { - vma_end_read(vma); - goto inval; - } - /* Check since vm_start/vm_end might change before we lock the VMA */ if (unlikely(address < vma->vm_start || address >= vma->vm_end)) { vma_end_read(vma);