Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp18886116rwd; Wed, 28 Jun 2023 01:57:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7sMfJw9tqblfV3NuLzJtZsFRZKAXOciK9BZDTgQMmqhixPCZSGGFKVX6N1XEVVGG+laoKj X-Received: by 2002:aa7:cac3:0:b0:514:a565:6e28 with SMTP id l3-20020aa7cac3000000b00514a5656e28mr25053802edt.18.1687942662276; Wed, 28 Jun 2023 01:57:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687942662; cv=none; d=google.com; s=arc-20160816; b=G1l8hEnJhWfOZjZqAiYDNVWJyPb4g/uqB+VceOuiu4VttmQ2pPWKRGD4YeEM++cygA wycBJT+4qb6TPMu5c5t4G74Wrdd6M3CVMGYpQjEV1VIEBY/GeqGFMEwcpRu3d1ERJl7F rtb/Qgzguf0nVPG90UsTLl/xMqpHUqMUSUT6HJ35FMIM+S4HbVkRPoNACiB71uptvUHG Om1EyJkAFcv1QCWbKZADkjowU4mM15Yk86cL1v57joTDrNNbfgejpwKZxUWBkEtnJvCF 9tmTeENedVP7A4pQltirmkbJi0F2uDJcgpXKoaLhLy2eHyh5rheP7swMg7J4t3OHAGKQ FYJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=P/PrTNNebDD3ET79IWIr+QJG2wgn5QmJDayPL9+A73A=; fh=RMSYO7NH5mgRq+Q9Sd8uvtKjjFjspH3ZqZT6M3GUwas=; b=rbbrujYfbhnLsWxrkQJ4/GZ33nX8z/6G1MpXQQ5jsvkxAGbCPX4xjGR1qfDdQ30JPH waAsVGoHLUmRQvcAdrV1lEcRYj7nogbaQGX/B+DaxDdyO+8cDqb842AouLWx8BGE95y4 Becl5PT5OcXNioJeLHAygmKuNUQZbR1K7tMSpcq0mQHtSu1wGzxb06xlmuUdwNaDi/iq JdCjVI1bkGeV3k6aqe00qd5u6awIuNZItQ2XSANLhhq7MCdjn506c28Liro49pihX+3S Ebh2LqYQPphHZXdZWuFO3czpnlVAShGh0+hlEnZ1QBaLAuo1XeC76ueg/d3sjhqnXXTv Pr4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=HgU6PDIa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j26-20020a50ed1a000000b0051da43cc408si2427459eds.244.2023.06.28.01.57.07; Wed, 28 Jun 2023 01:57:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=HgU6PDIa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234255AbjF1Iiy (ORCPT + 99 others); Wed, 28 Jun 2023 04:38:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235234AbjF1Ift (ORCPT ); Wed, 28 Jun 2023 04:35:49 -0400 Received: from mail-oo1-xc4a.google.com (mail-oo1-xc4a.google.com [IPv6:2607:f8b0:4864:20::c4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3468F3582 for ; Wed, 28 Jun 2023 01:27:08 -0700 (PDT) Received: by mail-oo1-xc4a.google.com with SMTP id 006d021491bc7-56340665b09so2716650eaf.2 for ; Wed, 28 Jun 2023 01:27:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687940827; x=1690532827; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=P/PrTNNebDD3ET79IWIr+QJG2wgn5QmJDayPL9+A73A=; b=HgU6PDIaGLI3i9IQDTlFfVp8Tq3qnFFEnHPHIV/6IN3ChvwT+7vqljCmxVTAopTOPx ZyZhwz0qkw1w5pQL05SM+sYKlVqVWYoMTfgCJb20YfVCqVAbM/Qs194aLplgtFKc9VTZ n3AcFDQk6gsUiAnZ25G8NZtYoQ2drUX+Hil92tIFJyG+bsgp4enFYHyRRg7tRo0BUNrq qPSZLe+WsA07DYHM6UR718iLA0Q9TumlE3VuDFWgkcK/G37Pr7CATqjy53T/8HN01HLK 18CsOhqu6MGIzRk/rhwv+xZpxe2huEAdbt3eNQdax5ZQ1kH5sFtMmIWnzf+QoGLeCG1N u2uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687940827; x=1690532827; 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=P/PrTNNebDD3ET79IWIr+QJG2wgn5QmJDayPL9+A73A=; b=kEwNEKokY+1ANb4H0nv+PvJSKEcFbAZDPRwDArCvIWK7yc0B7PEYgYmcI9wn/AiePf GCZ2Lw+87akLDLAMxL/2LRS6Lz1+HvDoiDfW3SgjzvwaDtGGsHW9I1GjT+bMmkfYdvgS hWPjA7tiT9tx9knRbaad0yTJboHwjIirSh8iUcLzYLzK/t4LxvySfOYe8UrLpHQSEk1p okwxK2DSb03QVoR4q330q/S6HSB2w3mJrEMJzWdko1zAVFiWqdiNhKW1TgQ6BsXPSRWL dXBxgD0pi5DUy1N681E7aQRL1SNlG5BDUickSNzhq3pX+vZThJoSlsDYZzipb5M6K+bW zUgg== X-Gm-Message-State: AC+VfDyjBW8orWKOz4+s1N3e0RJvOIXwz4p2HWjMXwFlPHio8TXVeOmz +4ZHU5xCsfHo3q+ouigR0yMxtZd6F2A= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6664:8bd3:57fd:c83a]) (user=surenb job=sendgmr) by 2002:a25:198a:0:b0:c00:a33:7 with SMTP id 132-20020a25198a000000b00c000a330007mr9718444ybz.8.1687936698504; Wed, 28 Jun 2023 00:18:18 -0700 (PDT) Date: Wed, 28 Jun 2023 00:18:00 -0700 In-Reply-To: <20230628071800.544800-1-surenb@google.com> Mime-Version: 1.0 References: <20230628071800.544800-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230628071800.544800-7-surenb@google.com> Subject: [PATCH v4 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- fs/userfaultfd.c | 39 ++++++++++++++++++--------------------- include/linux/mm.h | 39 +++++++++++++++++++++++++++++++++++++++ mm/filemap.c | 8 -------- mm/memory.c | 9 --------- 4 files changed, 57 insertions(+), 38 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 4e800bb7d2ab..d019e7df6f15 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(ctx->mm, 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; @@ -337,7 +334,7 @@ static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, pte_t *pte; bool ret = true; - mmap_assert_locked(mm); + assert_fault_locked(mm, vmf); pgd = pgd_offset(mm, address); if (!pgd_present(*pgd)) @@ -445,7 +442,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(mm, vmf); ctx = vma->vm_userfaultfd_ctx.ctx; if (!ctx) @@ -522,8 +519,11 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) * and wait. */ ret = VM_FAULT_RETRY; - if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) + if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) { + /* Per-VMA lock is expected to be dropped on VM_FAULT_RETRY */ + BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT); goto out; + } /* take the reference before dropping the mmap_lock */ userfaultfd_ctx_get(ctx); @@ -561,15 +561,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 fec149585985..70bb2f923e33 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; @@ -723,6 +734,23 @@ static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, unsigned long address); +static inline +void assert_fault_locked(struct mm_struct *mm, struct vm_fault *vmf) +{ + if (vmf->flags & FAULT_FLAG_VMA_LOCK) + vma_assert_locked(vmf->vma); + else + mmap_assert_locked(mm); +} + +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); +} + #else /* CONFIG_PER_VMA_LOCK */ static inline void vma_init_lock(struct vm_area_struct *vma) {} @@ -736,6 +764,17 @@ 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 assert_fault_locked(struct mm_struct *mm, struct vm_fault *vmf) +{ + mmap_assert_locked(mm); +} + +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 7ee078e1a0d2..d4d8f474e0c5 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1699,14 +1699,6 @@ static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait) return ret; } -static void release_fault_lock(struct vm_fault *vmf) -{ - if (vmf->flags & FAULT_FLAG_VMA_LOCK) - vma_end_read(vmf->vma); - else - mmap_read_unlock(vmf->vma->vm_mm); -} - /* * Return values: * 0 - folio is locked. diff --git a/mm/memory.c b/mm/memory.c index 76c7907e7286..c6c759922f39 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5294,15 +5294,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); -- 2.41.0.162.gfafddb0af9-goog