From patchwork Fri Jun 9 00:51:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13273057 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 96244C7EE29 for ; Fri, 9 Jun 2023 00:52:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 051BF8E0009; Thu, 8 Jun 2023 20:52:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F1BE28E0001; Thu, 8 Jun 2023 20:52:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D47638E0009; Thu, 8 Jun 2023 20:52:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C0DB78E0001 for ; Thu, 8 Jun 2023 20:52:19 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 8DEA81C7863 for ; Fri, 9 Jun 2023 00:52:19 +0000 (UTC) X-FDA: 80881383198.17.EF9BCA5 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf13.hostedemail.com (Postfix) with ESMTP id D5D2120011 for ; Fri, 9 Jun 2023 00:52:17 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=4hO9nbpC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3wHeCZAYKCOMXZWJSGLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3wHeCZAYKCOMXZWJSGLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686271937; 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=R1WyFySTZ0XkKYIf3B7+wOl2DNVNhKC+z/byy1yFxnw=; b=6QcpH+0XMrN2epEIHQxOtLW3+j+Pv2KcziP0UouRt8pWx7rMlzSJBd+rK0yk8CUpOF72Wf yIvroIGXu8MuJT6RhcQOhGkFz7AKP/urOdaoIvgeSWJjTPCISgLZIOHKslvNi9dYQRpTAe QkS5vuDlR7RgLqyktGxuZoGXZ513GBA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=4hO9nbpC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3wHeCZAYKCOMXZWJSGLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3wHeCZAYKCOMXZWJSGLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686271937; a=rsa-sha256; cv=none; b=7OfDq5pQbgkuzKmiveZIqm02JWlg+gjbYmwvVRi4oDCZqh6mELlUuol3Hm8oqjBX1AcbxF MYWMJuRqBcvST7z8UK2qyn90WmwgMPEDpEVrB5ed+t+c/vgF28fdTgslau5zSf8mPHuLxb EGp1+/LNEjfrnN5olUt4PunODsvQTFw= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-bb39aebdd87so2696854276.0 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=KnUegYLKTqaUTIbpy79eo2sbOVzM9+7H8sQMbw52XQpa5DdO8uE8RaTjKYrHEp9vpW lf+jmluDq2PNM4QpbCjzJeduAwpZz3UhI6r8OyVqvZQ46Zc3ddD6YPAOQeU3Vz/5B4Th IpIZ6RsyZo6VDPdfEEyrTp+RZZTVappIPp8H2ix+4G9agxOH+K9fIZ+eS/0TGlgR8DoE skafsaEhTu9qvsuzNVjOlEBg3hgTRy5TNsYn+UQP3v1IEJxA6cTtMG/2P2gvcMBzGroy Ha63v7Da9Hn54xv41KNnqalbK+18/miQzX5Gt0a9zWqBCpaOX6QKK/q5aTIaPrZgKC/l Xlpw== X-Gm-Message-State: AC+VfDxknKOhVL4KQ5IdqwIYl6sCpzwucxD3okPpEeTXI/6qmiASOBRQ fpm4MStA5mlu9Fk+Eke8u382gL7ltlo= 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 X-Rspamd-Queue-Id: D5D2120011 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: tfz63iskkjd5fnq7xn484m3wxtndqbdu X-HE-Tag: 1686271937-240694 X-HE-Meta: U2FsdGVkX19OphwCBjAQ9bi4ukGFTdR6c4M9Jd1arDzItZVvC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MrYEq4hi6WVAf7xqRX9HgT5qDMMBkPCON9XeCG3ZY050F2MC5+dhoRR3G03XwKwBSmGZSawOJLg3CEwhhrNrDzi/RrnmqsqljZMSWKfIWFKCT9DcQWPMWsE6X+8Km7V30yF+n2XOPbXjSHyWWSApZzu7QRrEwfOnTA+DWN6FCskMUwROje08d56TCjyZwYDEyD4Po0r29jMdhX6XdNsdmxDWid5mUjqAfOYOZlVgjjfPaERhr+9+qcg9q3nomE65NnsqVMs58XRLDZZWCm3vWHSPdw4bNpnn1YFgm23HX80dSFguBOcxMHfDeJUfTUkK24EkYj0+19IRWZWDRXtqZd3h79ONtbx+rqSYhTBCYYulnZMq3VFlCHx/4mkV5ItIBAUSjGE9ISa2mMvTi/Hp9kjJqt5JVBz79BX8FKtTqG6XrMTEwUPE91xA= 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 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);