Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1400874rwd; Thu, 8 Jun 2023 18:00:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4dRlsJGAaHpvOEF3aCIQxSCtMM0q3c8/iY4VatwH1d9Y1BWii0WiGfbmNm1JtGfCQL5YMY X-Received: by 2002:a17:903:284:b0:1af:b682:7a78 with SMTP id j4-20020a170903028400b001afb6827a78mr8143941plr.52.1686272420335; Thu, 08 Jun 2023 18:00:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686272420; cv=none; d=google.com; s=arc-20160816; b=Ajhhq8DnJBfM+yx7Nmg3BqnTFKbnxuE+ERjmQp8Kb+9iPZXLsQCGl7A0rfRYETWDJZ 6wxCsX/t397I/Nvk00b9d6zDpjoIculxCjJ8RpZCpKxCkM2zGKJJ1KeOi1A5ke7aZTg0 Uz+PvTcVpXu9BYj8B446w4N6x8g2VWVRkIHK5tTUzklil0cbEs44lZMGHpSgwLUkh/R3 45LiIuOHun0nLC8ImW9gAbuH/mK2EicGxRIXa8W2/L24YLVQkofr4zqBDCLv1jb4tSe3 1+Bw4BvDUtLW9xhel0Ix/gbiuoT4rx/HEJrvw/DbpfSSgxE75Y9E4fVYn5PI7Yu6yayV umaw== 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=R1WyFySTZ0XkKYIf3B7+wOl2DNVNhKC+z/byy1yFxnw=; b=cr0/mtNaVjpGL2QfTAW1UDzP+i9zTTpdpWepBKR3CYKYN7LrKi2GkCC3Ax/GhAC0Xn NRYL5WMDiwJ/RDOCdnBR4PdVRfwDTyHrW+E2qOiYEFry8g6HVoiGVK1HM12STxqjAfab vL4I3kPP5+7uXiuK9GTjNsSg0sHfuEu5fcr1+GNE2dXskKEU4ceqSmIRNec4BWAFXNrf LdoURMt6wYYnPww/AMHisUbWxKDt7CaU9w9o8wJfASjfd0tfQjNpt9lVSKxAJLXpJUSW zeEc6h6ZqmqlxJJwFelS43dkDT34OeaN8rIuvi1KvVc1Xfqi36WsRfLsrv3cFZKanyvB k/nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=4hO9nbpC; 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 r1-20020a170902be0100b001b042d1eccfsi1830374pls.530.2023.06.08.18.00.06; Thu, 08 Jun 2023 18:00:20 -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=4hO9nbpC; 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 S236602AbjFIAwn (ORCPT + 99 others); Thu, 8 Jun 2023 20:52:43 -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-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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 = vmf->vma; pte_t *ptep, pte; bool ret = true; - mmap_assert_locked(ctx->mm); + if (!(vmf->flags & FAULT_FLAG_VMA_LOCK)) + mmap_assert_locked(ctx->mm); - 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 +308,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 +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 = ctx->mm; + unsigned long address = vmf->address; pgd_t *pgd; p4d_t *p4d; pud_t *pud; @@ -337,7 +335,8 @@ static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, pte_t *pte; bool ret = true; - mmap_assert_locked(mm); + if (!(vmf->flags & FAULT_FLAG_VMA_LOCK)) + mmap_assert_locked(mm); pgd = pgd_offset(mm, address); if (!pgd_present(*pgd)) @@ -445,7 +444,8 @@ 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); + if (!(vmf->flags & FAULT_FLAG_VMA_LOCK)) + mmap_assert_locked(mm); ctx = vma->vm_userfaultfd_ctx.ctx; if (!ctx) @@ -561,15 +561,17 @@ 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); + if (vmf->flags & FAULT_FLAG_VMA_LOCK) { + vma_end_read(vma); + /* WARNING: VMA can't be used after this */ + ret |= VM_FAULT_VMA_UNLOCKED; + } else + mmap_read_unlock(mm); 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; - /* - * 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