From patchwork Fri Jun 30 21:19:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13298788 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D887EC001B0 for ; Fri, 30 Jun 2023 21:20:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7EE078E0051; Fri, 30 Jun 2023 17:20:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 79E218E000F; Fri, 30 Jun 2023 17:20:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 61ACD8E0051; Fri, 30 Jun 2023 17:20:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 548A68E000F for ; Fri, 30 Jun 2023 17:20:18 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 32EB41604AD for ; Fri, 30 Jun 2023 21:20:18 +0000 (UTC) X-FDA: 80960682516.30.3B9A065 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf25.hostedemail.com (Postfix) with ESMTP id 6B18AA0016 for ; Fri, 30 Jun 2023 21:20:16 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=VRHbgEe+; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3D0efZAYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3D0efZAYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688160016; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CYZzVVGdFW3BZ7BX39EfN49IRv4dv2Sl4PMSdQqxDi4=; b=hAkFT732UGFme2namF7bxzlLVrP7/bSG55xDIksldU3Ln8EGXCYcnIrJlzjLNvvTNnr2UK PfD84vrwkMU/E05vYzCfuEFGaNf3KqoTAG+KFhm1qMKUTPfD5nBagEAl8zfE5F+aYyEmXB vuc3poZvU+CpIjSujorhmWB/kqimNqI= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=VRHbgEe+; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3D0efZAYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3D0efZAYKCEY02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688160016; a=rsa-sha256; cv=none; b=KYHYmmNT833HcHHjaFqXz5EzSiZ3yfHgbVE/ih4aZ6KNs8waxE1vStW33FNUMd9+AOFTfW p9jwYlILpoORSm/ypdxkzo7hzEb6O6SOTvLLr6iwTvsuB74mCHsB1sKDDHvuDgLBqGLhep MQ6fVPPo4ckwT2wIA2iOOia3tZSaSFk= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5707177ff8aso22546807b3.2 for ; Fri, 30 Jun 2023 14:20:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688160015; x=1690752015; 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=VRHbgEe+4tmyXojwe94SagE/3BdWRl9HljjtCM10oZH/2lirmShVuqPWYhXZZyEPVa PHSvriOmhmIWRrb97shs8wqeA/tP9lhy9ofKmbZu25SK7Md122TbXLrK9ljgsXGJnI7k 2Mai/hmZVv0XaioP1GuSs9ziPWNt6fGSHdy1nJYlIY/ldiZVysVd4eUGJdPsUGTJU1jG HkPIOLGsrmKHdMyj/vDsrtFzgjcyUb0RruqV5uD7mMn2bM5ITwFRVeLw4+orENuWMhoI SxIeBb8YpgDHnftP3wphxALr9hOaqHUGaMOTSEgl5omQxW0Qu6paJ/+V+fHiSgMem/mA FuTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688160015; x=1690752015; 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=gIbOsV9fdpJKEyYY40XCpG5H1Dlrwp4OdEi3BwqY7Cw6mho+roD52fw0ab4GowUz1e 0+xjvNEza6W98MXe/aNZDrFJtwQwyCCyRDJMLIG3PwV1VEjqPQ7JeytvBSGZBahasTb0 Ika7lkDFPNF6gFirMWI2TDgIliXpjwzH86qvNApzTo39aPp2K7g5M+ss4j7wo+f5Ht5J c1/T4Kn9g1oo6sgpNyJbw4VzT5DkeRfv0ziQ+tNzjcTFaE0GL9qYz/Ao9RCdtjHmkuIv OvJqMw0wT8aaulqsy3Nm5vikPMIxxWAUZ1IubuLXt1oVRVb+XTkchN6nAZn7O1SacKXj zdvg== X-Gm-Message-State: ABy/qLYPY7tHIdR3s4zvU6N+FvwKw15UTpnYynZaSkgZGP7Aaz9BlOTi gWsQq4/EHsanHvw7IuunIfsGrNVsJ1E= X-Google-Smtp-Source: APBJJlGXAJjNagMRUpJSstior0oEerrgAMTnrbJJ1mJiBLKiXUVNW+JBvG48qVTyNdlMCtXmqetsT5NLqOQ= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:b54c:4d64:f00a:1b67]) (user=surenb job=sendgmr) by 2002:a81:414c:0:b0:56d:21a1:16a1 with SMTP id f12-20020a81414c000000b0056d21a116a1mr30032ywk.5.1688160015604; Fri, 30 Jun 2023 14:20:15 -0700 (PDT) Date: Fri, 30 Jun 2023 14:19:57 -0700 In-Reply-To: <20230630211957.1341547-1-surenb@google.com> Mime-Version: 1.0 References: <20230630211957.1341547-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230630211957.1341547-7-surenb@google.com> Subject: [PATCH v7 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 X-Rspamd-Queue-Id: 6B18AA0016 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: f1zggo5nfar13ejmeq3dgcnqa8bw4k6d X-HE-Tag: 1688160016-183873 X-HE-Meta: U2FsdGVkX1/dXuQPLJunmKiEtJtsgeq3kWhvsZVgXMBONyW8Ckf6ZpEMUWwMUH1yuMNogEOBvxP2Odm3W6hp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ud7Euaf0ptM1u19DU+TpnPlt/Lm4yJ1T7zXzQqs5YTVrfg2Afw9udXSZmFQgKUUVqTf5cnRUG+iH6xG8VuTiwfZG66MsgUL4irY8iiir+6xDpz+bUjF3o8s5vsqp8bsg+K6kmsSqlv9r/C0CDE3hWV5MWTyPEdi6AEPalc1pv7ADlvVE+TBQ5JxFaoRPlc5npD8WbaFrdxlQh8u/xK2nqkAZihWWVEPd/iRFydo8Y3Oz7hlqmOk06v1QNcX8vmUf4QAq9fUAfNwHV4/oKqlEJA4/F+u6X8H4XLKMVIzRw2tlDooso7FSXbKpwK2jOu436X7Pgvw3AJCXFvPkogKZzSgstnM21oKzKG5F7O1nuBd82uikaL82H4oK5JnIDfw+bONxA5+zs8N9LnyHTeKLImSmlRVIA/3nYEkH3QR0OrJCY2a6e6UYdSfZap+CthnJqyc X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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);