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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79502C7EE2D for ; Wed, 1 Mar 2023 19:07:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229553AbjCATHp (ORCPT ); Wed, 1 Mar 2023 14:07:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229451AbjCATHm (ORCPT ); Wed, 1 Mar 2023 14:07:42 -0500 Received: from mail-yw1-x112a.google.com (mail-yw1-x112a.google.com [IPv6:2607:f8b0:4864:20::112a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAF454DBCA for ; Wed, 1 Mar 2023 11:07:33 -0800 (PST) Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-536b7ffdd34so382746057b3.6 for ; Wed, 01 Mar 2023 11:07:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1677697653; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=BlFKPaHxV4jQ7dMxp37PMOa6KmgzjBmeyyNBQBavDx8=; b=Tc+Lfvs4SS8u1UCDBpYQvIJ9La6W49lFb2CZ+De1a11FXObIXWCF3BlBujuIEcWfXR K8XAAg8gxr2xVbV3LBMHTffyEYatSSzLtxaJ1thDWIz6aZ6tnyaWtD/HgJU6tyGJYf3q jp3IKu6nyN7tV0mKfmzcHMG74wGuu0zdP7wmqBrP/k7QLFJgyU3CURBojezcQXpqpSdo Fxa0y0DporTUWfiigj1CH1bgpxQ8jMFxnIOpFeF8j43Xe59cuxyCaa5vcLCOhN+5MFUV PmN47d5ZRn6yecRBiQTEpYT20b93V+dsHO/LeLSVH6nyRn1/7A+vnbXttfD2d7rTtMJ5 iIiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677697653; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BlFKPaHxV4jQ7dMxp37PMOa6KmgzjBmeyyNBQBavDx8=; b=urgGYz+DaIndetQZEttNJdw5Uj+docGvS+WoR3Ouys/PdJKziLp/FTAN0VilqZLy3n gHpWZweS97r04CE4NWTSco5+vQh5K6HdDiPriba1W021PEUvHulePbqpsu7WIkWIsoBd K9Kuo/r+o9XoZUvisPG6EHstItfQnYFo2Hx+55GNjcUnmmAvUzKp7X41q5csEuENlpCM sGTINWNu0QEHJYzvKrIAfIBe2yeNAWkkiSym487Q5dN3RYX0rrwz83ilYu7ReGThzKhC MSfVE5ASJBVdux2CN7L1bJR4oLkAwVOjYlyjglD/rJxzdt317kD4H/C7hM20wdrIP0kh NLfQ== X-Gm-Message-State: AO0yUKVdepgjhF7BRRbgpIs+OET1aSUkhzcWY39HSLaJ/V8nyX3I4xlB a5kz6dH1yvjmivGQ9fxpygqoFNOsOTKLYijhdq2PXw== X-Google-Smtp-Source: AK7set9ZsSbg+lkgd2HRMSAvJ/YkGIQu9aJDVRfhrq6ARtmp15/GeLDk6fPjITpwN8ANRiWlqv4jLhfWt2ZLpWW0OEA= X-Received: by 2002:a81:ac16:0:b0:536:4194:e6eb with SMTP id k22-20020a81ac16000000b005364194e6ebmr4616550ywh.0.1677697652553; Wed, 01 Mar 2023 11:07:32 -0800 (PST) MIME-Version: 1.0 References: <20230227173632.3292573-1-surenb@google.com> <20230227173632.3292573-19-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Wed, 1 Mar 2023 11:07:21 -0800 Message-ID: Subject: Re: [PATCH v4 18/33] mm: write-lock VMAs before removing them from VMA tree To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: akpm@linux-foundation.org, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, peterz@infradead.org, ldufour@linux.ibm.com, paulmck@kernel.org, mingo@redhat.com, will@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, chriscli@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, rppt@kernel.org, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, leewalsh@google.com, posk@google.com, michalechner92@googlemail.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 1, 2023 at 10:34=E2=80=AFAM Suren Baghdasaryan wrote: > > On Tue, Feb 28, 2023 at 11:57=E2=80=AFPM Hyeonggon Yoo <42.hyeyoo@gmail.c= om> wrote: > > > > On Wed, Mar 01, 2023 at 07:43:33AM +0000, Hyeonggon Yoo wrote: > > > On Mon, Feb 27, 2023 at 09:36:17AM -0800, Suren Baghdasaryan wrote: > > > > Write-locking VMAs before isolating them ensures that page fault > > > > handlers don't operate on isolated VMAs. > > > > > > > > Signed-off-by: Suren Baghdasaryan > > > > --- > > > > mm/mmap.c | 1 + > > > > mm/nommu.c | 5 +++++ > > > > 2 files changed, 6 insertions(+) > > > > > > > > diff --git a/mm/mmap.c b/mm/mmap.c > > > > index 1f42b9a52b9b..f7ed357056c4 100644 > > > > --- a/mm/mmap.c > > > > +++ b/mm/mmap.c > > > > @@ -2255,6 +2255,7 @@ int split_vma(struct vma_iterator *vmi, struc= t vm_area_struct *vma, > > > > static inline int munmap_sidetree(struct vm_area_struct *vma, > > > > struct ma_state *mas_detach) > > > > { > > > > + vma_start_write(vma); > > > > mas_set_range(mas_detach, vma->vm_start, vma->vm_end - 1); > > > > > > I may be missing something, but have few questions: > > > > > > 1) Why does a writer need to both write-lock a VMA and mark the= VMA detached > > > when unmapping it, isn't it enough to just only write-lock a= VMA? > > We need to mark the VMA detached to avoid handling page fault in a > detached VMA. The possible scenario is: > > lock_vma_under_rcu > vma =3D mas_walk(&mas) > munmap_sidetree > vma_start_write= (vma) > > mas_store_gfp() // remove VMA from the tree > vma_end_write_a= ll() > vma_start_read(vma) > // we locked the VMA but it is not part of the tree anymore. > > So, marking the VMA locked before vma_end_write_all() and checking > vma->detached after vma_start_read() helps us avoid handling faults in > the detached VMA. > > > > > > > > 2) as VMAs that are going to be removed are already locked in v= ma_prepare(), > > > so I think this hunk could be dropped? > > > > After sending this just realized that I did not consider simple munmap = case :) > > But I still think 1) and 3) are valid question. > > > > > > > > > if (mas_store_gfp(mas_detach, vma, GFP_KERNEL)) > > > > return -ENOMEM; > > > > diff --git a/mm/nommu.c b/mm/nommu.c > > > > index 57ba243c6a37..2ab162d773e2 100644 > > > > --- a/mm/nommu.c > > > > +++ b/mm/nommu.c > > > > @@ -588,6 +588,7 @@ static int delete_vma_from_mm(struct vm_area_st= ruct *vma) > > > > current->pid); > > > > return -ENOMEM; > > > > } > > > > + vma_start_write(vma); > > > > cleanup_vma_from_mm(vma); > > > > > > 3) I think this hunk could be dropped as Per-VMA lock depends o= n MMU anyway. > > Ah, yes, you are right. We can safely remove the changes in nommu.c > Andrew, should I post a fixup or you can make the removal directly in > mm-unstable? I went ahead and posted the fixup for this at: https://lore.kernel.org/all/20230301190457.1498985-1-surenb@google.com/ > Thanks, > Suren. > > > > > > > Thanks, > > > Hyeonggon > > > > > > > > > > > /* remove from the MM's tree and list */ > > > > @@ -1519,6 +1520,10 @@ void exit_mmap(struct mm_struct *mm) > > > > */ > > > > mmap_write_lock(mm); > > > > for_each_vma(vmi, vma) { > > > > + /* > > > > + * No need to lock VMA because this is the only mm user= and no > > > > + * page fault handled can race with it. > > > > + */ > > > > cleanup_vma_from_mm(vma); > > > > delete_vma(mm, vma); > > > > cond_resched(); > > > > -- > > > > 2.39.2.722.g9855ee24e9-goog > > > > > > > > > > > > > > > -- > > To unsubscribe from this group and stop receiving emails from it, send = an email to kernel-team+unsubscribe@android.com. > >