From nobody Sun Feb 8 14:51:17 2026 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 858E1C87FDD for ; Fri, 9 Jun 2023 00:52:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237247AbjFIAwN (ORCPT ); Thu, 8 Jun 2023 20:52:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236404AbjFIAwH (ORCPT ); Thu, 8 Jun 2023 20:52:07 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E4C31FDF for ; Thu, 8 Jun 2023 17:52:06 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-568960f4596so16974237b3.1 for ; Thu, 08 Jun 2023 17:52:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686271925; x=1688863925; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yuiOMQ0aSy0KwUuwsgymDjnxP8/lpeb7/V1M3j1Xb1E=; b=IyAXoXjV7xCg1A6CTIB0VVGoVOFSNHNynjRms7ZT4ZJuasxkgD/xINqmOy3R2QWmj6 LweWil7YBo9DB23KcYDwjIFg2tzhu/sNAVap1M78azP9/yh5rbc8NVuFliEYHm7NBXMo soQpcPklJ87/HIwm9iXTXt6sGz2j9ugeHoaBCIPXu786yDSkXPG2N+lpjRD03pJqKf4U ynS64LhxDlPhl3xfkAFkbEO/hQz6xagu8ZpHoJtj8fgQugYSIR8+59/FTuzy8cFImM2J 9Fgql8U7ice3Cs5matPGhf41n4PEh7vFyVlNzgNeB1JUE14tptvSHOdWg+sF+J8fGOVy muaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686271925; x=1688863925; 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=yuiOMQ0aSy0KwUuwsgymDjnxP8/lpeb7/V1M3j1Xb1E=; b=lsMU9RM6oDJbviGoQqOnTv5JR0Obnwvn+GkhndVfPScSpkY3ZR84zH0vRxdpSoAvE6 jdgO4kKvi0rCIiJiqakpasTbR1TcXTuDsVDjC41gENd6lr1b6oZ1oKPvmc7SGN2SFtdY nbda6kfgovLgviDwEIejZAUH7HmVvBZhCtxooOvKY1OEVTd8iEjl1dWlbTP44ZGJLee2 bVfkqaK+2eV8xza8JKkRKHneJA+zn9ESSJ+v8mVR2x1uMNH9WWBPf4QKXkrBD6P0XDdp UTsymvHgLtY7WFKQso5AthBM8Jpe+Mv8doE2iuzg0arpw+9Fc1YtQEGGu+zK/3H3J/pl Iuqw== X-Gm-Message-State: AC+VfDyyj822LeYz2bBk4yXhezGiq8Uk66hOv6OirgMm+rh83EL95t+I Uphmpjh7/m/W3ZUZvF3bz3ms8zpuL0U= X-Google-Smtp-Source: ACHHUZ7vpO/mcmJj2VKORN5it7Uqz0TT+6LPQeqOxr4F1AyBIL+6jgbr7cmENYkI/PhrqFOCmtLfaWiUoNY= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:c03e:d3b7:767a:9467]) (user=surenb job=sendgmr) by 2002:a81:b2c9:0:b0:55d:8472:4597 with SMTP id q192-20020a81b2c9000000b0055d84724597mr729567ywh.10.1686271925574; Thu, 08 Jun 2023 17:52:05 -0700 (PDT) Date: Thu, 8 Jun 2023 17:51:53 -0700 In-Reply-To: <20230609005158.2421285-1-surenb@google.com> Mime-Version: 1.0 References: <20230609005158.2421285-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230609005158.2421285-2-surenb@google.com> Subject: [PATCH v2 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit [1] introduced IO polling support during 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=3Dfalse 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 b5ffbaf616f5..b1e8adf1234e 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -215,7 +215,7 @@ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned l= ong start, continue; =20 page =3D read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE, - vma, index, false, &splug); + vma, index, &splug); if (page) put_page(page); } @@ -252,7 +252,7 @@ static void force_shm_swapin_readahead(struct vm_area_s= truct *vma, rcu_read_unlock(); =20 page =3D read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE, - NULL, 0, false, &splug); + NULL, 0, &splug); if (page) put_page(page); =20 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_spa= ce *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 b76a65ac28b3..a3839de71f3f 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -517,15 +517,14 @@ struct page *__read_swap_cache_async(swp_entry_t entr= y, 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 =3D __read_swap_cache_async(entry, gfp_mask, vma, addr, &page_was_allocated); =20 if (page_was_allocated) - swap_readpage(retpage, do_poll, plug); + swap_readpage(retpage, false, plug); =20 return retpage; } @@ -620,7 +619,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, = gfp_t gfp_mask, struct swap_info_struct *si =3D swp_swap_info(entry); struct blk_plug plug; struct swap_iocb *splug =3D NULL; - bool do_poll =3D true, page_allocated; + bool page_allocated; struct vm_area_struct *vma =3D vmf->vma; unsigned long addr =3D vmf->address; =20 @@ -628,7 +627,6 @@ struct page *swap_cluster_readahead(swp_entry_t entry, = gfp_t gfp_mask, if (!mask) goto skip; =20 - do_poll =3D false; /* Read a page_cluster sized and aligned cluster around offset. */ start_offset =3D offset & ~mask; end_offset =3D offset | mask; @@ -660,7 +658,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); } =20 int init_swap_address_space(unsigned int type, unsigned long nr_pages) @@ -825,7 +823,7 @@ static struct page *swap_vma_readahead(swp_entry_t fent= ry, 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 =3D=3D 1, NULL); + NULL); } =20 /** --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 14:51:17 2026 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 5C208C7EE37 for ; Fri, 9 Jun 2023 00:52:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237283AbjFIAwP (ORCPT ); Thu, 8 Jun 2023 20:52:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236900AbjFIAwL (ORCPT ); Thu, 8 Jun 2023 20:52:11 -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 BB85826B9 for ; Thu, 8 Jun 2023 17:52:08 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bac6a453dd5so1724792276.2 for ; Thu, 08 Jun 2023 17:52:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686271928; x=1688863928; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PkmNN9bDB8D6ky9B1T3x3QkjIj3UMnGVw+OccLVTcQI=; b=gXDUez/eWeoXfvtzBxoMFqX6yzChXfTjDT+EJvNOmhXnnvN012eKsotIpflrgQF8Lp 2fyTd8do5aQagZSZ9UACoo1VzNRvUvy51OShxvvsm8sbDywK/VrQvSv+sRssJTGwZVIC O3ehpAZc1poO5bYiy+cj10MKTJW6cZFiOgpPC3NY9qfT+DR6GY8WwwiHc1OHk8IdiUQQ 0ZsriYgMP54GSTwssMvXRD/MDAyF/nV4E+VqHs3oPq/UGb1xuVtyNqZYuiLhpsjiEGIs wnGJGf8nQGo+PvFSaShaFDV7MeHfsOJeL0uf+hxmZhAZVLSptRntOtdojyaNM4xUuxW5 WCcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686271928; x=1688863928; 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=PkmNN9bDB8D6ky9B1T3x3QkjIj3UMnGVw+OccLVTcQI=; b=Gy9CNPgzvSqHDR0xBJK6pJOcNvBDuLrV75bDLHsoccSZsnjtsq586fLs2HsHipsCAs nDmlMNUZZfVn5pRuUBXFoI3+r8CzKO4lpHMOViUCWptCrWc+3+8uyW+e0ZQAB6arKA2u aflDQNh94/rg6ZknDuq1wx+1HcsXgwtXscVmk3dGZ89AbLEfJGU/BYZzTliFoSMaz7GX 2er9hqxE2/v6IKucTxjdpQIKC4uj9RIZsYGPMU9g6tDpGXtCc7154lVo26rAWJjxwWC7 ApdxT/KncwVEm7EVzMO/MJo+z5zaj65hG5dvI9qyhsRwZl8r8XtEeNGuGuSDUaUWtQVM Sb0w== X-Gm-Message-State: AC+VfDw78pZZgM0XlNPcLrMFQHQCy5Ti+G8BymHC+jbD+Q5iuC1pFSBN y025uiB/Tcti44K7XPgmrDXYSpK9CE8= X-Google-Smtp-Source: ACHHUZ5ErqzNYue3IsR81EKLpgdV2ofXzmHeKkote+bFDE5P/CfrHNwNpRNHxN6f/RnaMXvb7Ljn6yc+ads= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:c03e:d3b7:767a:9467]) (user=surenb job=sendgmr) by 2002:a05:6902:691:b0:ba8:1f20:ff4f with SMTP id i17-20020a056902069100b00ba81f20ff4fmr691767ybt.12.1686271927895; Thu, 08 Jun 2023 17:52:07 -0700 (PDT) Date: Thu, 8 Jun 2023 17:51:54 -0700 In-Reply-To: <20230609005158.2421285-1-surenb@google.com> Mime-Version: 1.0 References: <20230609005158.2421285-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230609005158.2421285-3-surenb@google.com> Subject: [PATCH v2 2/6] mm: handle swap page faults under VMA lock if page is uncontended 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-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When page fault is handled under VMA lock protection, all swap page faults are retried with mmap_lock because folio_lock_or_retry implementation has to drop and reacquire mmap_lock if folio could not be immediately locked. Instead of retrying all swapped page faults, retry only when folio locking fails. 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. Drivers implementing ops->migrate_to_ram might still rely on mmap_lock, therefore fall back to mmap_lock in this case. Signed-off-by: Suren Baghdasaryan --- mm/filemap.c | 6 ++++++ mm/memory.c | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index b4c9bd368b7e..7cb0a3776a07 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1706,6 +1706,8 @@ static int __folio_lock_async(struct folio *folio, st= ruct wait_page_queue *wait) * mmap_lock has been released (mmap_read_unlock(), unless flags had b= oth * 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 perf= ormed + * with VMA lock only, the 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. @@ -1713,6 +1715,10 @@ static int __folio_lock_async(struct folio *folio, s= truct wait_page_queue *wait) bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, unsigned int flags) { + /* 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 diff --git a/mm/memory.c b/mm/memory.c index f69fbc251198..41f45819a923 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3711,11 +3711,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (!pte_unmap_same(vmf)) goto out; =20 - if (vmf->flags & FAULT_FLAG_VMA_LOCK) { - ret =3D VM_FAULT_RETRY; - goto out; - } - entry =3D pte_to_swp_entry(vmf->orig_pte); if (unlikely(non_swap_entry(entry))) { if (is_migration_entry(entry)) { @@ -3725,6 +3720,15 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) vmf->page =3D pfn_swap_entry_to_page(entry); ret =3D 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. + */ + ret |=3D VM_FAULT_RETRY; + goto out; + } + vmf->page =3D pfn_swap_entry_to_page(entry); vmf->pte =3D pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl); --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 14:51:17 2026 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 79CEDC7EE23 for ; Fri, 9 Jun 2023 00:52:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237371AbjFIAwS (ORCPT ); Thu, 8 Jun 2023 20:52:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237040AbjFIAwL (ORCPT ); Thu, 8 Jun 2023 20:52:11 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9241D270B for ; Thu, 8 Jun 2023 17:52:10 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-bb0d11a56abso1692787276.2 for ; Thu, 08 Jun 2023 17:52:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686271930; x=1688863930; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JqcDdPqSbAUSGbTjmvLRcpjiA0c+o9DbrViIiTIy+Ug=; b=zXF0/SOz02AqM5BWf2P/zwcWEHhQEqJOZyXi3UZpbx8VPRWxCUcaRMvo1gco/xsra2 1KRka8Fv7Cz5TdkzcXaQgC6O85k85tL3gyx/iy3cDrh6mP1AOUruXIGz0ftmdJxZ1KRm ZZx2kRtlsPb6qeLOZNriXbpH5lr8UltQBDA01BaTWp8gpunXX21xn4/zrUa5rAnUvMfl mCkY25J/3lnSqtjOJla8DOtGiyoeA+L2vJd0IiqnWI7c7ZShrlyswyCHOWMMkPwl9HjW QFj7gjWymWSTqWnDk2iKhq+C7yb+32Ze1ontuD7AeEZ5CFFfLh23UfHmunZhj1vbgoCU wHiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686271930; x=1688863930; 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=JqcDdPqSbAUSGbTjmvLRcpjiA0c+o9DbrViIiTIy+Ug=; b=UBnyLi/2DLzARoYV4eE8mDzbju5vBjorNJ2JyX81L02vBWs8GofXZVVJ3717yDQQ3/ K6AjrrKpIP07t9Zz3SaKRavdBaoZ2YeBEEzPADEo+mjHO/oV6F2VrLFGR1EU89b8UkfY 0ARx/4lAqMhChER8l/K3Z9knWKSlubPFt0fSuS458c8/vgHppx2mat/JTB/7237qrM08 prh7zGUDdTFKXvPJvzmLqJh+drYohABluiVDtayj68h8dK6B52ppy+4EZpatZP1r/NZ0 KvszoHcypPR7XgJzPC0+mE2UEkOdkxWB5jgK9y5lYla7M5cfCC0YzeHIGadpz/1SdMpO tKOQ== X-Gm-Message-State: AC+VfDxCGW1Eca/0ggEQ2DLGPX8AWsySpMM9FTCekBweqgLpwCe6vZHV FN5+yhfxmoboTmSiwdIMulpln+VmT2U= X-Google-Smtp-Source: ACHHUZ5YbGKMEwsx1wWALVdFMCpP8nhO2N5qAulNG9AhZTP2wq6KaJAphozg0TQUY00Q3mKklpdD+wtUbrM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:c03e:d3b7:767a:9467]) (user=surenb job=sendgmr) by 2002:a25:db09:0:b0:bb1:5962:6fde with SMTP id g9-20020a25db09000000b00bb159626fdemr677035ybf.13.1686271929776; Thu, 08 Jun 2023 17:52:09 -0700 (PDT) Date: Thu, 8 Jun 2023 17:51:55 -0700 In-Reply-To: <20230609005158.2421285-1-surenb@google.com> Mime-Version: 1.0 References: <20230609005158.2421285-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230609005158.2421285-4-surenb@google.com> Subject: [PATCH v2 3/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-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 306a3d1a0fa6..79765e3dd8f3 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1070,7 +1070,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" } =20 struct vm_special_mapping { const char *name; /* The name, e.g. "[vdso]". */ --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 14:51:17 2026 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 45D73C7EE23 for ; Fri, 9 Jun 2023 00:52:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229796AbjFIAwh (ORCPT ); Thu, 8 Jun 2023 20:52:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234892AbjFIAwO (ORCPT ); Thu, 8 Jun 2023 20:52:14 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C68AC2685 for ; Thu, 8 Jun 2023 17:52:12 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-53b9eb7bda0so220334a12.0 for ; Thu, 08 Jun 2023 17:52:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686271932; x=1688863932; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=uuo3Il/2NbgOWAM0rjqTihVZSZhUnoZMQ/ESG1PbGOc=; b=aoHc7JPCZ32CAYt9q46WDYGEOxxS+8Zg8n+MkQ8tBFVy2AxaNM8+3Gds918auhPTHa q1i93JJIOBjYqWdat77Bw43ovYfILELgsQ3A15M4ncn74YtVwsuP81alu9yqHz/6QEf4 0ZCIwnE+WqrId9F8Np2B9UzySwNVPcW9qaPUxrGBEsK8nkCGUUE7v/JS22HdzPoOb4MB Chi4Dc24/vK44G+H3O7mb/dNtV3gVAS4dKZye3QS8d/JCKeQ8FR8f6Rt4chkxNrZVvil NBHO4/LslE98QkZt8lZcAJ+QXDZaDtGSI1vY2Loous6hPUzUbnAktXCDF6R5gPFfIwoD hcMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686271932; x=1688863932; 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=uuo3Il/2NbgOWAM0rjqTihVZSZhUnoZMQ/ESG1PbGOc=; b=W696qUNB0XBmguvWzibGh1+7V+4mPcC0oBIxXjHuCYY6D1P/iaEob+oQwyEqnNJK1g 0Zqr5AJlb2yOWOHKIUejie2Woq4h52fgBSoMPTySj1ZCRDTVL/zaDG/eyY/BmHXEM0gu kqSSXR9ohk6muSNs9oppWIVU14CflfpdV9nyuO+iGx+e0ENHt1eYWjJ+Usv7UklU0CNI U8aN11MIDeeWk1KXYOAu/cn10B2jkU2e3REG/3gRPkfkYVVG5y5sKL6l9xxFErZLriAy lvQwAiM5U68S8yivgpSGetwBJAhIpIt2H9CA8wn9/SCGb6RXJEhm0grSF7f6c+D+J81s J1/g== X-Gm-Message-State: AC+VfDw7AMq/UqIMU/CQtkye9W+oLEpgZJDAeVs4iiL5kKRM8aKXd+XZ ZGcMoJepZiCK1G7VutpyXz/VzcaZXSM= X-Google-Smtp-Source: ACHHUZ6b1B6MYsfuF9F0w1PhABF3eJ3XJNF2fY5wRRyw5gd4GldbBtWiJ9mZhA7X4yciara2bOj4oc1yZEs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:c03e:d3b7:767a:9467]) (user=surenb job=sendgmr) by 2002:a63:d44:0:b0:503:77c9:45aa with SMTP id 4-20020a630d44000000b0050377c945aamr225410pgn.9.1686271932107; Thu, 08 Jun 2023 17:52:12 -0700 (PDT) Date: Thu, 8 Jun 2023 17:51:56 -0700 In-Reply-To: <20230609005158.2421285-1-surenb@google.com> Mime-Version: 1.0 References: <20230609005158.2421285-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230609005158.2421285-5-surenb@google.com> Subject: [PATCH v2 4/6] mm: drop VMA lock before waiting for migration 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-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" migration_entry_wait does not need VMA lock, therefore it can be dropped before waiting. Introduce VM_FAULT_VMA_UNLOCKED to indicate that VMA lock was dropped while in handle_mm_fault(). Note that once VMA lock is dropped, the VMA reference can't be used as there are no guarantees it was not freed. Signed-off-by: Suren Baghdasaryan --- arch/arm64/mm/fault.c | 3 ++- arch/powerpc/mm/fault.c | 3 ++- arch/s390/mm/fault.c | 3 ++- arch/x86/mm/fault.c | 3 ++- include/linux/mm_types.h | 6 +++++- mm/memory.c | 12 ++++++++++-- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 6045a5117ac1..8f59badbffb5 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -601,7 +601,8 @@ static int __kprobes do_page_fault(unsigned long far, u= nsigned long esr, goto lock_mmap; } fault =3D handle_mm_fault(vma, addr, mm_flags | FAULT_FLAG_VMA_LOCK, regs= ); - vma_end_read(vma); + if (!(fault & VM_FAULT_VMA_UNLOCKED)) + vma_end_read(vma); =20 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 531177a4ee08..b27730f07141 100644 --- a/arch/powerpc/mm/fault.c +++ b/arch/powerpc/mm/fault.c @@ -494,7 +494,8 @@ static int ___do_page_fault(struct pt_regs *regs, unsig= ned long address, } =20 fault =3D handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs= ); - vma_end_read(vma); + if (!(fault & VM_FAULT_VMA_UNLOCKED)) + vma_end_read(vma); =20 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 b65144c392b0..cc923dbb0821 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 *r= egs, int access) goto lock_mmap; } fault =3D handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs= ); - vma_end_read(vma); + if (!(fault & VM_FAULT_VMA_UNLOCKED)) + 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 e4399983c50c..ef62ab2fd211 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1347,7 +1347,8 @@ void do_user_addr_fault(struct pt_regs *regs, goto lock_mmap; } fault =3D handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs= ); - vma_end_read(vma); + if (!(fault & VM_FAULT_VMA_UNLOCKED)) + vma_end_read(vma); =20 if (!(fault & VM_FAULT_RETRY)) { count_vm_vma_lock_event(VMA_LOCK_SUCCESS); diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 79765e3dd8f3..bd6b95c82f7a 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1030,6 +1030,8 @@ typedef __bitwise unsigned int vm_fault_t; * fsync() to complete (for synchronous page faults * in DAX) * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released + * @VM_FAULT_VMA_UNLOCKED: VMA lock was released, vmf->vma should no longer + * be accessed * @VM_FAULT_HINDEX_MASK: mask HINDEX value * */ @@ -1047,6 +1049,7 @@ enum vm_fault_reason { VM_FAULT_DONE_COW =3D (__force vm_fault_t)0x001000, VM_FAULT_NEEDDSYNC =3D (__force vm_fault_t)0x002000, VM_FAULT_COMPLETED =3D (__force vm_fault_t)0x004000, + VM_FAULT_VMA_UNLOCKED =3D (__force vm_fault_t)0x008000, VM_FAULT_HINDEX_MASK =3D (__force vm_fault_t)0x0f0000, }; =20 @@ -1071,7 +1074,8 @@ enum vm_fault_reason { { VM_FAULT_FALLBACK, "FALLBACK" }, \ { VM_FAULT_DONE_COW, "DONE_COW" }, \ { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ - { VM_FAULT_COMPLETED, "COMPLETED" } + { VM_FAULT_COMPLETED, "COMPLETED" }, \ + { VM_FAULT_VMA_UNLOCKED, "VMA_UNLOCKED" } =20 struct vm_special_mapping { const char *name; /* The name, e.g. "[vdso]". */ diff --git a/mm/memory.c b/mm/memory.c index 41f45819a923..c234f8085f1e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3714,8 +3714,16 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) entry =3D pte_to_swp_entry(vmf->orig_pte); if (unlikely(non_swap_entry(entry))) { if (is_migration_entry(entry)) { - migration_entry_wait(vma->vm_mm, vmf->pmd, - vmf->address); + /* Save mm in case VMA lock is dropped */ + struct mm_struct *mm =3D vma->vm_mm; + + if (vmf->flags & FAULT_FLAG_VMA_LOCK) { + /* No need to hold VMA lock for migration */ + vma_end_read(vma); + /* WARNING: VMA can't be used after this */ + ret |=3D VM_FAULT_VMA_UNLOCKED; + } + migration_entry_wait(mm, vmf->pmd, vmf->address); } else if (is_device_exclusive_entry(entry)) { vmf->page =3D pfn_swap_entry_to_page(entry); ret =3D remove_device_exclusive_entry(vmf); --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 14:51:17 2026 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 06D7AC83003 for ; Fri, 9 Jun 2023 00:52:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237593AbjFIAwl (ORCPT ); Thu, 8 Jun 2023 20:52:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233412AbjFIAwf (ORCPT ); Thu, 8 Jun 2023 20:52:35 -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 397E530DD for ; Thu, 8 Jun 2023 17:52:15 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-babb7aaa605so1713276276.3 for ; Thu, 08 Jun 2023 17:52:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686271934; x=1688863934; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yDKE+Wkkd9TQZuY1u1+D5VbBuChdz1DMs8UdXXj0e2g=; b=oVug16yEjdlnNEpZ7pnd5qNolPTgsrM9t75qLEfhGOZfvwQQTTtBUSAfR3QIEEr+oC v/fzE6FqQXocpbRK9V3XWMbF2lwE13bpKfYmrqjm6uNGGYsi/lgDYeC/9YEof7hp5FqS 5A0uKzNV8LOj4fKrnAEEjVJwZBkiAOghnhCcJkEwlpwZR4xAR7bH/08Zl/fuOCMap4kM hmRM8jBS/+AwHvOnOyZBcktaKtQpOMW1qGAdckEP5f61vXaEekTcAIlIX8pWgUawmGin 2woY8gWii5fyujAxxvv+l/JMuwNA3M044wzqA3CPsKBvTCAFqHIUtCTz2+/Qo24VtWBB JIiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686271934; x=1688863934; 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=yDKE+Wkkd9TQZuY1u1+D5VbBuChdz1DMs8UdXXj0e2g=; b=GDFWn0J11Yeva9bzNIbbxaPbmkgtOQwwTBNCHJh/Nabt6GKvMTW52sE+gCypgMN9AA ELOljTSe8KG+vqQO/dfmd5IQAdCpInbPZFjQPYTNTKuEv0h+fLpja4Tzv5YoWR8REe2W 0nVRToKLCZpPi6EhOGKYvLuv6+AgvdJQZ8BiRAqD2lY1pe6Eck1HF/aRJH4PeYQWHq7D j/SE6WQSFE9lyD2eyVkZvFinEln+GXkLL+jMGIvuQPIrV4lW1dOHXwzGjFX1Ua4t6Xcd bqyeFi65Kffs3TVeCwfWozboRfUFVFMJGWRS666hKuEPHkQTe3m/Ad7m0Vqy58NZ3RVY INmA== X-Gm-Message-State: AC+VfDyiXrWV1r6LryzvmFdioTwYn7PuctbjdzKAQc1Nx1wQ742/PIv6 Dgc+dLZYOVnibFy69sUQvDMsrEHNLmA= X-Google-Smtp-Source: ACHHUZ6qGzpVQ3JwgFBzFsFEN7W5Nqjbd5tmkwDtF5dFLLF9fdY1OW4nuRJzWHLumkRKhGTOuLgN6g2TsCw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:c03e:d3b7:767a:9467]) (user=surenb job=sendgmr) by 2002:a25:3216:0:b0:bb3:ac6a:6d61 with SMTP id y22-20020a253216000000b00bb3ac6a6d61mr657350yby.3.1686271934456; Thu, 08 Jun 2023 17:52:14 -0700 (PDT) Date: Thu, 8 Jun 2023 17:51:57 -0700 In-Reply-To: <20230609005158.2421285-1-surenb@google.com> Mime-Version: 1.0 References: <20230609005158.2421285-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230609005158.2421285-6-surenb@google.com> Subject: [PATCH v2 5/6] 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, 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-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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, 41 insertions(+), 29 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_que= ue *wait_page, =20 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); =20 @@ -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 =3D false; + return true; + } + + return __folio_lock_or_retry(folio, vma, flags, lock_dropped); } =20 /* diff --git a/mm/filemap.c b/mm/filemap.c index 7cb0a3776a07..838955635fbc 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) =20 /* * 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 b= oth - * 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 perf= ormed - * 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_N= OWAIT + * 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 =3D false; return false; + } =20 - 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 =3D true; return false; } if (flags & FAULT_FLAG_KILLABLE) { @@ -1739,13 +1737,18 @@ bool __folio_lock_or_retry(struct folio *folio, str= uct mm_struct *mm, =20 ret =3D __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 =3D true; return false; } } else { __folio_lock(folio); } =20 + *lock_dropped =3D false; return true; } =20 diff --git a/mm/memory.c b/mm/memory.c index c234f8085f1e..acb09a3aad53 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3568,6 +3568,7 @@ static vm_fault_t remove_device_exclusive_entry(struc= t vm_fault *vmf) struct folio *folio =3D page_folio(vmf->page); struct vm_area_struct *vma =3D vmf->vma; struct mmu_notifier_range range; + bool lock_dropped; =20 /* * 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(stru= ct vm_fault *vmf) if (!folio_try_get(folio)) return 0; =20 - 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,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) bool exclusive =3D false; swp_entry_t entry; pte_t pte; - int locked; + bool lock_dropped; vm_fault_t ret =3D 0; void *shadow =3D NULL; =20 @@ -3837,9 +3840,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out_release; } =20 - locked =3D folio_lock_or_retry(folio, vma->vm_mm, vmf->flags); - - if (!locked) { + if (!folio_lock_or_retry(folio, vma, vmf->flags, &lock_dropped)) { + if (lock_dropped && vmf->flags & FAULT_FLAG_VMA_LOCK) + ret |=3D VM_FAULT_VMA_UNLOCKED; ret |=3D VM_FAULT_RETRY; goto out_release; } --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 14:51:17 2026 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 B8E25C7EE29 for ; Fri, 9 Jun 2023 00:52:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236735AbjFIAwo (ORCPT ); Thu, 8 Jun 2023 20:52:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237164AbjFIAwf (ORCPT ); Thu, 8 Jun 2023 20:52:35 -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 B2F7530E5 for ; Thu, 8 Jun 2023 17:52:17 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bacfa4eefcbso2666570276.1 for ; Thu, 08 Jun 2023 17:52:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686271937; x=1688863937; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R1WyFySTZ0XkKYIf3B7+wOl2DNVNhKC+z/byy1yFxnw=; b=4hO9nbpC9/B59lz7lu7ipLmvPx0TPiRR1YFcnoIIsLsACw+RsmBoY1hg9fpZtuMvoW AsVsIpTf8LfYB4lwQ5sT6jbE8vVaylpAww88SyjrA1LKqGMRXyWLDVtjUMHa3DShm/3L oCZtGpYklKgkoDTmolhi1ndUET6n652IDLkTIObECyb0yackpoS0TrORfZJOHg5/tfhJ JNKbEXfUKTJj0MNVLKCwTOpswbGOlStCgQHRqPjpQAo80ZcJR3KseQgmY2Wv1ZWl+SgC d+U9Bx2apmjK27kmJGDzWSJpSU7aLQV3FnJPhxOI/r8/brluCy0xQxyPUn6xCpotTzHq ehCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686271937; x=1688863937; 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=R1WyFySTZ0XkKYIf3B7+wOl2DNVNhKC+z/byy1yFxnw=; b=RMPk9MbSqpNicbzqDaUnCVI6OJ5fJTEo5sR4l0bvMHoGm2+ANIMCSsj26WxL1tAwnw IRyQAAbkPkiZ/pEqo2yYqPJPi+TsjDY4o+LKGac+Z8NQewMPGMFf0VrHBQgmRWtcxbMY 6HWsqe9l1pQK9buFfCvCm1rgbcMrIXstQdN6McsZmbyy61iDrc54oFX5cC1C1/yn76mq AfoGFFOiSVKd87hISuQ/T3c+8meoRRhbV6QY5fo4u+oIZh4f+0U+StfH5BZAxQblRvjH vE4oTmn45T4q7gLAemmH8S6ks8QhLnbTSTYtIkbWwbUG+kCgSn2U9qBm+vMgpNlhmUCS ABKA== X-Gm-Message-State: AC+VfDzLBNPnc4eJuedogepga2IibVSFTqkNTmX16VwD3HTyztUhjOO0 a020eYYU6yDBeZiPhKQnYD1AhswjzRE= X-Google-Smtp-Source: ACHHUZ4bWROsb838DrUUPzvA00RwKpJHV0L0dTRYqvNM1IO6yvKJkT1erTMTIADmBKvxMqLYZ9xOLKXYFnE= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:c03e:d3b7:767a:9467]) (user=surenb job=sendgmr) by 2002:a05:6902:211:b0:bb1:569c:f381 with SMTP id j17-20020a056902021100b00bb1569cf381mr138781ybs.1.1686271936956; Thu, 08 Jun 2023 17:52:16 -0700 (PDT) Date: Thu, 8 Jun 2023 17:51:58 -0700 In-Reply-To: <20230609005158.2421285-1-surenb@google.com> Mime-Version: 1.0 References: <20230609005158.2421285-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230609005158.2421285-7-surenb@google.com> Subject: [PATCH v2 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-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Enable handle_userfault to operate under VMA lock by releasing VMA lock instead of mmap_lock and retrying with VM_FAULT_VMA_UNLOCKED set. Signed-off-by: Suren Baghdasaryan --- fs/userfaultfd.c | 42 ++++++++++++++++++++++-------------------- mm/memory.c | 9 --------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 0fd96d6e39ce..23c3a4ce45d9 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -277,17 +277,17 @@ 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 =3D vmf->vma; pte_t *ptep, pte; bool ret =3D true; =20 - mmap_assert_locked(ctx->mm); + if (!(vmf->flags & FAULT_FLAG_VMA_LOCK)) + mmap_assert_locked(ctx->mm); =20 - ptep =3D hugetlb_walk(vma, address, vma_mmu_pagesize(vma)); + ptep =3D hugetlb_walk(vma, vmf->address, vma_mmu_pagesize(vma)); if (!ptep) goto out; =20 @@ -308,10 +308,8 @@ static inline bool userfaultfd_huge_must_wait(struct u= serfaultfd_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 +323,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 =3D ctx->mm; + unsigned long address =3D vmf->address; pgd_t *pgd; p4d_t *p4d; pud_t *pud; @@ -337,7 +335,8 @@ static inline bool userfaultfd_must_wait(struct userfau= ltfd_ctx *ctx, pte_t *pte; bool ret =3D true; =20 - mmap_assert_locked(mm); + if (!(vmf->flags & FAULT_FLAG_VMA_LOCK)) + mmap_assert_locked(mm); =20 pgd =3D pgd_offset(mm, address); if (!pgd_present(*pgd)) @@ -445,7 +444,8 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsig= ned 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); + if (!(vmf->flags & FAULT_FLAG_VMA_LOCK)) + mmap_assert_locked(mm); =20 ctx =3D vma->vm_userfaultfd_ctx.ctx; if (!ctx) @@ -561,15 +561,17 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, uns= igned long reason) spin_unlock_irq(&ctx->fault_pending_wqh.lock); =20 if (!is_vm_hugetlb_page(vma)) - must_wait =3D userfaultfd_must_wait(ctx, vmf->address, vmf->flags, - reason); + must_wait =3D userfaultfd_must_wait(ctx, vmf, reason); else - must_wait =3D userfaultfd_huge_must_wait(ctx, vma, - vmf->address, - vmf->flags, reason); + must_wait =3D userfaultfd_huge_must_wait(ctx, vmf, reason); if (is_vm_hugetlb_page(vma)) hugetlb_vma_unlock_read(vma); - mmap_read_unlock(mm); + if (vmf->flags & FAULT_FLAG_VMA_LOCK) { + vma_end_read(vma); + /* WARNING: VMA can't be used after this */ + ret |=3D VM_FAULT_VMA_UNLOCKED; + } else + mmap_read_unlock(mm); =20 if (likely(must_wait && !READ_ONCE(ctx->released))) { wake_up_poll(&ctx->fd_wqh, EPOLLIN); diff --git a/mm/memory.c b/mm/memory.c index acb09a3aad53..b2ea015dcb87 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5306,15 +5306,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_= struct *mm, if (!vma_start_read(vma)) goto inval; =20 - /* - * 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 >=3D vma->vm_end)) { vma_end_read(vma); --=20 2.41.0.162.gfafddb0af9-goog