From mboxrd@z Thu Jan 1 00:00:00 1970 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 BD7B3EB64D9 for ; Tue, 27 Jun 2023 15:54:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45CFE8D0002; Tue, 27 Jun 2023 11:54:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 40CCA8D0001; Tue, 27 Jun 2023 11:54:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FB508D0002; Tue, 27 Jun 2023 11:54:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 216FB8D0001 for ; Tue, 27 Jun 2023 11:54:33 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D2CD1C0A36 for ; Tue, 27 Jun 2023 15:54:32 +0000 (UTC) X-FDA: 80948975184.21.CE3D228 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 50E6640019 for ; Tue, 27 Jun 2023 15:54:30 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cq0ytByF; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687881270; 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=blfaz25JTx0Gq1i5rQOIktQT2cQ83yiOEY0rptX8I8M=; b=2e0/sgRiSonBqBn7RrdzXf5SiWhep3rJLtVQTgoRilJ9WeWqYK15Q1H6YKnw6SgdqM/JM7 Pm7bTHsIAd1vg/aNM/3eJf1OmpQvzFKv77niOfmQhxKOOkrwsdmsKLm6gBPt1ps7Fwixo6 A6SoAxXCMEwtd0RvP8LMe3K4dLlBimk= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cq0ytByF; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687881270; a=rsa-sha256; cv=none; b=73qPgXsGsqz9HwnQ9NHyO0U7DHg1see5kaPL27w9tZQ5eIJGGY8B+jkMC7ZD+kCIOiWi0e SQCotQilBhSbucmGBpvkS3L6bJxfwwuRBryADrhZatkyko3gVheJRlZJXsMcLoQXBsSOpS XCcfSlyOfFpY07sTdcpz27W17czFoWI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687881269; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=blfaz25JTx0Gq1i5rQOIktQT2cQ83yiOEY0rptX8I8M=; b=cq0ytByFsBVljszirNtFNbWecwqTsnoUAcZcjUpjm0bcwNTs7Q25Sy0I2m++qWL+P0I8JJ OdmqPaiL3919H9aDdXy532ByT3bAVSuxJ0NQWn5SZaijyY/QAp5/o0TfPZcDOhLAg879z2 Aj2AuVKiDat4e9vTYWW0YPlPKW+3cp0= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-460-rrCMBR4tNdqTuXhEj6hn_w-1; Tue, 27 Jun 2023 11:54:28 -0400 X-MC-Unique: rrCMBR4tNdqTuXhEj6hn_w-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-402fa256023so343651cf.0 for ; Tue, 27 Jun 2023 08:54:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687881267; x=1690473267; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=blfaz25JTx0Gq1i5rQOIktQT2cQ83yiOEY0rptX8I8M=; b=XH1O+T+4Mhf5e+ulaVnKHH7soI0xp5qIORyS/ZDSBJEPisNPSNq6Fszqp/8PayC8A0 BI9FJUUV9E3IUMFX3iuxHjjU9TXvuhkVElZLW+tAZgemwN+KCxxrI16DEwy6Icg5BSYt aZ/ShazfLybtAdkABfTctcqxGfidb3uaRJ/HCKL/pS0d3nRmOIvhcAAMLEp2GLxctZBr iiANcal8bZLXNhBGlznbsO8ELpNSe6xeGlSmPLZIoLZ3fjeNa4ESwJ7keQ6WBgdbGvME BTNLKzB9pYP55SHscXsG5tlrUoDK861i4/6tPirIBAQ9ejfsc4pmAmavyioWB9kSh4T5 ZMtg== X-Gm-Message-State: AC+VfDxn7e0TJimUo+YKJj1zBgHgfOjwyu+c8HI1VUe2TCms+az+/vu8 AD3Lw2RpOAfLU3ewRJAKppPxvkSAY/8jDC/5gAhNpWMdE40doQ0VjOPFfei7kD4gesL6LprM9rK YBRoHZpLwL7g= X-Received: by 2002:ac8:5a86:0:b0:400:adc0:8306 with SMTP id c6-20020ac85a86000000b00400adc08306mr5934156qtc.4.1687881267446; Tue, 27 Jun 2023 08:54:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6el5jb9Izv6sojg638+Y+Ior7huYvpR+V+d70FMxnJoZyY0xlHgXUXCJu9qNQlG8K5lr0aQA== X-Received: by 2002:ac8:5a86:0:b0:400:adc0:8306 with SMTP id c6-20020ac85a86000000b00400adc08306mr5934118qtc.4.1687881267160; Tue, 27 Jun 2023 08:54:27 -0700 (PDT) Received: from x1n (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id cg13-20020a05622a408d00b003f4ed0ca698sm4677898qtb.49.2023.06.27.08.54.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jun 2023 08:54:26 -0700 (PDT) Date: Tue, 27 Jun 2023 11:54:24 -0400 From: Peter Xu To: Suren Baghdasaryan Cc: akpm@linux-foundation.org, 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, 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, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v3 8/8] mm: handle userfaults under VMA lock Message-ID: References: <20230627042321.1763765-1-surenb@google.com> <20230627042321.1763765-9-surenb@google.com> MIME-Version: 1.0 In-Reply-To: <20230627042321.1763765-9-surenb@google.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspamd-Queue-Id: 50E6640019 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 4wmfxfh4oaoqpmxt86jw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2E43o7AEwuMlhaJic7UPo6XYb3/gG/74f6dm/qzUhxC0OXq0XCfzLsFy9RTlV/UOtRkACVIWJjVgq2oXHDuE1vLeU37VJVIlFHdTchsqsO+Uwxq7rmlIRNPvkVUqZcxn86ksGA7SW5bjaGdUv0VLvNnDiHt4z4VkeuVl2NUTbrAAobupEZljFtnpLaA5wdWUBvYbdfrWm9zN6DZFBBDWQJHOz5wZZ/YctsWiSIJw9acJv1TwIgQtlKbkWKYnJyMDc4LDOL5mSp36xyysYCWBtB/pNDIyS+5GZCIKfTXIyEHV7KNTXFqJ4BW18AZ2QNnoChJk= 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: On Mon, Jun 26, 2023 at 09:23:21PM -0700, Suren Baghdasaryan wrote: > Enable handle_userfault to operate under VMA lock by releasing VMA lock > instead of mmap_lock and retrying. This mostly good to me (besides the new DROP flag.. of course), thanks. Still some nitpicks below. > > 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 4e800bb7d2ab..b88632c404b6 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); Maybe we can have a helper asserting proper vma protector locks (mmap for !VMA_LOCK and vma read lock for VMA_LOCK)? It basically tells the context the vma is still safe to access. > > - 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); (the assert helper can also be used here) > > 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) { > + /* WARNING: VMA can't be used after this */ > + vma_end_read(vma); > + } else > + mmap_read_unlock(mm); I also think maybe we should have a helper mm_release_fault_lock() just release different locks for with/without VMA_LOCK. It can also be used in the other patch of folio_lock_or_retry(). > + vmf->flags |= FAULT_FLAG_LOCK_DROPPED; > > 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 bdf46fdc58d6..923c1576bd14 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -5316,15 +5316,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.178.g377b9f9a00-goog > -- Peter Xu