Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp427699rdb; Thu, 19 Oct 2023 08:20:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEaWZQ+K/ldG49s9LrmEuZbL2eICQ22si+m+Gm0SnzvVZJXrQATg35+wUiq9/Fl6tOmC525 X-Received: by 2002:a17:903:2305:b0:1c8:a63a:2087 with SMTP id d5-20020a170903230500b001c8a63a2087mr2337322plh.65.1697728827826; Thu, 19 Oct 2023 08:20:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697728827; cv=none; d=google.com; s=arc-20160816; b=IUJTMX1QkhEuKl1ELKvSDB47sSTuaU15YVi7ACCiwiXjs9hycw5/nsJh2v4AUGuXO9 kNWMVtzvv7B+Ff9/S7uM/t0L7a0raVAZda6onLvK2J6xfbW8/h217B2OUF79HAmwz9J4 ZCBYQPJSyVpvG5g04bh2JYzryR5WyHWukN/AtEn/EDpsXvBatMDzPSHuuEMb7p1Mq7uW 5K6bDc1F/WFtk1ATE7Yh93z5VaD+Jdl5L4iwq33e09GU4Rs70fzW8VYSR3uKY+WumRaY PphyWa2Vy/iAFDEFPqr1o1OWfCQlsNqpO9RDVwM1ZHw9Osgoa/oMBO6zEebr81KFZLOR 3Hxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=FRnUGTvblTffidsZLvzhyxUVdZqdHJIWMYZoMmdQD9g=; fh=/yWRANWYd0dgFr7ria3T2K+/VbrYH6Dj/HpxAYu019I=; b=rMcPXbLk7g2RALqY2+U6BVmgzCTi3cH9tFN+szyqBcb+XlEk8LAm7groYMbbQuZJ5y Rdj0nEQE93+894DRL2ugwqOgnCEpcAz/Ts6mrcFvHtb8Lals4Y/R2b05cprDU+kOjIr0 gsKJGqcnzIrGHERduHlBm5l6lCB2NHZ/ckWNNcnIaMiXQmeWDVyciAXuCNKif/H33M7V mVVfz0FpQYe4hqwwaGYgpGLVGg07mXUv6CIQ76/kI8YHNC41Y5a/jSV+6JvWm7wuldva 3I6TbIFXJqLp6mDCNhYULnv+qDHHm+1V80pVvJYYwwz7vfPlTeqWldoaCcM5eeIvAWpa oCxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JojRJhIq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id s9-20020a170902ea0900b001c9af74feaesi2657447plg.215.2023.10.19.08.20.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 08:20:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JojRJhIq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id AFB108325659; Thu, 19 Oct 2023 08:20:24 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345979AbjJSPUI (ORCPT + 99 others); Thu, 19 Oct 2023 11:20:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345950AbjJSPUH (ORCPT ); Thu, 19 Oct 2023 11:20:07 -0400 Received: from mail-yw1-x112e.google.com (mail-yw1-x112e.google.com [IPv6:2607:f8b0:4864:20::112e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03AF812A for ; Thu, 19 Oct 2023 08:20:05 -0700 (PDT) Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-5a82c2eb50cso80621177b3.2 for ; Thu, 19 Oct 2023 08:20:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697728804; x=1698333604; darn=vger.kernel.org; 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=FRnUGTvblTffidsZLvzhyxUVdZqdHJIWMYZoMmdQD9g=; b=JojRJhIqePzP8nEwaV77iL43Ult2vaCz3uHYjIDX6kiEkCSEHAoqMy0hvLaBx36785 2Fh0ndEfXYxaewcCHSScTlctjy23Bn5YoM5OvZmL4wvfnwxZZVzskqcjZi4OGvNRQIF9 J9FxCjdeC7OQbaidiM+fXo0AvsAAGcmq1RYjHg+iheQ0cpXVloOJ2Z22LpgsxYP8z9hv B3l/jGPFlms+o/1zucpV9BUmVOCXlkxrUECSd7P80NrIEj9gIWY73GJyoD/AoAJxhoLp kLm2JqknSf+JI7uDXRfOPd4Bvq/OPi/tGGUuE66gpjIyFWkC32o9rvYGnP86gBvAqwyX NNfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697728804; x=1698333604; 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=FRnUGTvblTffidsZLvzhyxUVdZqdHJIWMYZoMmdQD9g=; b=fWVVrt3FunYeHHuBOhJ/TcqO38isYl+Q7oYAavCKvkkYl7wMrqUQCmg/H2HcizxKUn gnNggufL19b4rRRJZiH7WLT/mAyiz5ZUndEowGCTagOOwDiJsVRYQuMcm96l1QkJEXcR Y/g94anv1D9VwZKQefu9RzlPDahbVQwXFgYsi9ifx16wN4fcjPEyCe9fitHnAs0gcclS feTMI2nwIMsGPbzmZa0pYRwJsU3BHl/b2B0wgpwuuk/6Feocpa3x/5FdSSxLAxXts6Af kGe02dYG/YK3I2eTGe3askE9QoQnR6yiBtpOnlCvs1/2/Hsdpie9/MOU6XQ4z0xLg+G2 rYXg== X-Gm-Message-State: AOJu0YyS1Q5b1qIHSoyYKd9dHMnLDmkhQc/RhmPf11Q8qWBnD5LQhZSt 2hxKlWele94eVpjazvvfpnMci1s4eRnfsekWV6/WRw== X-Received: by 2002:a81:9255:0:b0:5a7:d9ce:363c with SMTP id j82-20020a819255000000b005a7d9ce363cmr2795218ywg.6.1697728803811; Thu, 19 Oct 2023 08:20:03 -0700 (PDT) MIME-Version: 1.0 References: <20231009064230.2952396-1-surenb@google.com> <20231009064230.2952396-2-surenb@google.com> <7495754c-9267-74af-b943-9b0f86619b5d@redhat.com> In-Reply-To: <7495754c-9267-74af-b943-9b0f86619b5d@redhat.com> From: Suren Baghdasaryan Date: Thu, 19 Oct 2023 08:19:50 -0700 Message-ID: Subject: Re: [PATCH v3 1/3] mm/rmap: support move to different root anon_vma in folio_move_anon_rmap() To: David Hildenbrand Cc: Peter Xu , akpm@linux-foundation.org, viro@zeniv.linux.org.uk, brauner@kernel.org, shuah@kernel.org, aarcange@redhat.com, lokeshgidra@google.com, hughd@google.com, mhocko@suse.com, axelrasmussen@google.com, rppt@kernel.org, willy@infradead.org, Liam.Howlett@oracle.com, jannh@google.com, zhangpeng362@huawei.com, bgeffon@google.com, kaleshsingh@google.com, ngeoffray@google.com, jdduke@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@android.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 19 Oct 2023 08:20:24 -0700 (PDT) On Fri, Oct 13, 2023 at 1:04=E2=80=AFAM David Hildenbrand wrote: > > On 13.10.23 00:01, Peter Xu wrote: > > On Sun, Oct 08, 2023 at 11:42:26PM -0700, Suren Baghdasaryan wrote: > >> From: Andrea Arcangeli > >> > >> For now, folio_move_anon_rmap() was only used to move a folio to a > >> different anon_vma after fork(), whereby the root anon_vma stayed > >> unchanged. For that, it was sufficient to hold the folio lock when > >> calling folio_move_anon_rmap(). > >> > >> However, we want to make use of folio_move_anon_rmap() to move folios > >> between VMAs that have a different root anon_vma. As folio_referenced(= ) > >> performs an RMAP walk without holding the folio lock but only holding = the > >> anon_vma in read mode, holding the folio lock is insufficient. > >> > >> When moving to an anon_vma with a different root anon_vma, we'll have = to > >> hold both, the folio lock and the anon_vma lock in write mode. > >> Consequently, whenever we succeeded in folio_lock_anon_vma_read() to > >> read-lock the anon_vma, we have to re-check if the mapping was changed > >> in the meantime. If that was the case, we have to retry. > >> > >> Note that folio_move_anon_rmap() must only be called if the anon page = is > >> exclusive to a process, and must not be called on KSM folios. > >> > >> This is a preparation for UFFDIO_MOVE, which will hold the folio lock, > >> the anon_vma lock in write mode, and the mmap_lock in read mode. > >> > >> Signed-off-by: Andrea Arcangeli > >> Signed-off-by: Suren Baghdasaryan > >> --- > >> mm/rmap.c | 24 ++++++++++++++++++++++++ > >> 1 file changed, 24 insertions(+) > >> > >> diff --git a/mm/rmap.c b/mm/rmap.c > >> index c1f11c9dbe61..f9ddc50269d2 100644 > >> --- a/mm/rmap.c > >> +++ b/mm/rmap.c > >> @@ -542,7 +542,9 @@ struct anon_vma *folio_lock_anon_vma_read(struct f= olio *folio, > >> struct anon_vma *root_anon_vma; > >> unsigned long anon_mapping; > >> > >> +retry: > >> rcu_read_lock(); > >> +retry_under_rcu: > >> anon_mapping =3D (unsigned long)READ_ONCE(folio->mapping); > >> if ((anon_mapping & PAGE_MAPPING_FLAGS) !=3D PAGE_MAPPING_ANON) > >> goto out; > >> @@ -552,6 +554,16 @@ struct anon_vma *folio_lock_anon_vma_read(struct = folio *folio, > >> anon_vma =3D (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANO= N); > >> root_anon_vma =3D READ_ONCE(anon_vma->root); > >> if (down_read_trylock(&root_anon_vma->rwsem)) { > >> + /* > >> + * folio_move_anon_rmap() might have changed the anon_vma= as we > >> + * might not hold the folio lock here. > >> + */ > >> + if (unlikely((unsigned long)READ_ONCE(folio->mapping) != =3D > >> + anon_mapping)) { > >> + up_read(&root_anon_vma->rwsem); > >> + goto retry_under_rcu; > > > > Is adding this specific label worthwhile? How about rcu unlock and got= o > > retry (then it'll also be clear that we won't hold rcu read lock for > > unpredictable time)? > > +1, sounds good to me Sorry for the delay, I was travelling for a week. I was hesitant about RCU unlocking and then immediately re-locking but your point about holding it for unpredictable time makes sense. Will change. Thanks! > > -- > Cheers, > > David / dhildenb >