Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp799960rdg; Fri, 13 Oct 2023 01:05:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH+fzO6uCfIaRYvgz+ISdA1kTIg3NLrQasg88cTErnweBT7QS+hZrOyJDPRJWOfA7N7y6fw X-Received: by 2002:a05:6358:7e14:b0:166:a6e3:cba5 with SMTP id o20-20020a0563587e1400b00166a6e3cba5mr3402rwm.2.1697184348282; Fri, 13 Oct 2023 01:05:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697184348; cv=none; d=google.com; s=arc-20160816; b=t4VuCb2yaXPjJHKN7edVNROz4czenaIppd29w00T0BgnhZ4OOpdgfoEzpPPcMJ1V5D 8f1AsB5YxoYpbAUTlQV1n0Izqp7sTp03ov3nrWtcWmZ4RJhO56Xf2F5guGBLCTlFYEkm HphFqTxTrCdUP+FdRUKW8ORjvOcZjBPoQpXZFG7F2Uv46qlSlAttacYATDRikW11dfn7 gooq2TWpj/C0DiXLqPGR18hhzsqzd16KS30k5W8l/jleqqcG7Uy1lJqzg0WFm3pg20ID 8ITXsqkWrGyuh+flVT0bjD2Ae0hDXH8YkZky9EvtEagLfHTlo3tJe9iSggmBHzGZRfhj Y99Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=Jnw6CV1in12hPMuWpKYAfFREANvgubRLaHRIuqtezJI=; fh=7TbVHvfFZRX4sKvAy+cCSPnfAdXJ509CNdWYhkE4ui4=; b=fJ/6QPAThHMlGRDS5Itcjg3XmPr8qW3BXRXHwF8O/dVRm73NzfFW9FJHEDrQ97D7Co EmrJoMWm/R+QU5eNKMmjRo78ABxO6LH5DxOecFBn30LF5UfH7gedc67xfTxNIPxkOlN2 orf9HFdFThHDvXuc6/pdky97WUCZAh0CT9izaRWqQZtYjOum96JsVXsZbLEUW0HAl041 cWGlaQjgXvbqjnU9cIlaa266dg4F0yncOfxyMUofn83IKIQudJgZgAelgHQM5l269PtJ 2Bc8/9p0N0R/BFEY1AmhXj0G9Z4CpVf60CyJCAQQ85vq9Xykq2jCe0jonurp05MfdZEr 3k4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="MMAb/LPY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id cb7-20020a056a00430700b0068ff659af62si16072926pfb.106.2023.10.13.01.05.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 01:05:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="MMAb/LPY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id A1A148078D6F; Fri, 13 Oct 2023 01:05:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230005AbjJMIFE (ORCPT + 99 others); Fri, 13 Oct 2023 04:05:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229989AbjJMIFD (ORCPT ); Fri, 13 Oct 2023 04:05:03 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C77B6BE for ; Fri, 13 Oct 2023 01:04:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697184258; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jnw6CV1in12hPMuWpKYAfFREANvgubRLaHRIuqtezJI=; b=MMAb/LPYjNSLIkM2RDgXyaGsae9bF2uIZ9WmTojalLO8CFb6SHmcbqiP5/C094IATsmO8+ D3wqUDApM8JiM1bio/9ude+YIA3lxXrvs9IJtU0aOqgA9jnUlbAOzIBlWRSPLVPGJwc6ko ISs8PHrEKMiG4IOo6loqRJPCiqv+IZU= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-625-xZTlsuG6PXKc7rHTi42WPg-1; Fri, 13 Oct 2023 04:04:16 -0400 X-MC-Unique: xZTlsuG6PXKc7rHTi42WPg-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-32d9602824dso688055f8f.2 for ; Fri, 13 Oct 2023 01:04:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697184255; x=1697789055; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Jnw6CV1in12hPMuWpKYAfFREANvgubRLaHRIuqtezJI=; b=YdEcRGxkjFdfdIjSX7tpafzneTlegTNltlFbiGd/3o1IDsuDrxhnun6LatBuQKLHHk T231Hup9SzLtQ/bxILvfd+8Kaw9NBWYzZd5TKz+j2kPPcTCJHgJsQ5AShrQMEE/5EyNw pmHSn1o3gb+WwulsWYbJZXWz8M8OWimC90eJZv6lElplXKsy8HMTpFz2eamj29sRdt93 YDcd2edGGbl5zQKQFUmGGNuGbdGJaGJi0PfxycRFkT4Z/NtFrAKR9/2Jfr/NaqbB4yPH BKJrlNWYOojzhpoVqEeKkxbHch5l4cLrMnSGdy2q92BQo8RBgOoGWSZlT4AvqFoeQtFZ CjdQ== X-Gm-Message-State: AOJu0YwpHCzq33fLFc7VWApQwI6csWOsQGpKr8pPaX4+0vNMGRCRhmy1 MAAG9S1W77LDIOxViW/tT8Up8YugeABc8eBcWuKgZnmNE3WGx8qRYFaLyAym0NRxAF1A4PVW+b4 NK+g4MUdm6kIw1Zv+BFXle/SR X-Received: by 2002:a05:6000:68d:b0:32d:88fd:5c65 with SMTP id bo13-20020a056000068d00b0032d88fd5c65mr7476698wrb.1.1697184255432; Fri, 13 Oct 2023 01:04:15 -0700 (PDT) X-Received: by 2002:a05:6000:68d:b0:32d:88fd:5c65 with SMTP id bo13-20020a056000068d00b0032d88fd5c65mr7476648wrb.1.1697184254961; Fri, 13 Oct 2023 01:04:14 -0700 (PDT) Received: from [192.168.3.108] (p5b0c6028.dip0.t-ipconnect.de. [91.12.96.40]) by smtp.gmail.com with ESMTPSA id g7-20020adfe407000000b003232d122dbfsm20377154wrm.66.2023.10.13.01.04.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 13 Oct 2023 01:04:14 -0700 (PDT) Message-ID: <7495754c-9267-74af-b943-9b0f86619b5d@redhat.com> Date: Fri, 13 Oct 2023 10:04:12 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH v3 1/3] mm/rmap: support move to different root anon_vma in folio_move_anon_rmap() Content-Language: en-US To: Peter Xu , Suren Baghdasaryan Cc: 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 References: <20231009064230.2952396-1-surenb@google.com> <20231009064230.2952396-2-surenb@google.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Fri, 13 Oct 2023 01:05:21 -0700 (PDT) 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 folio *folio, >> struct anon_vma *root_anon_vma; >> unsigned long anon_mapping; >> >> +retry: >> rcu_read_lock(); >> +retry_under_rcu: >> anon_mapping = (unsigned long)READ_ONCE(folio->mapping); >> if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) >> goto out; >> @@ -552,6 +554,16 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, >> anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); >> root_anon_vma = 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) != >> + anon_mapping)) { >> + up_read(&root_anon_vma->rwsem); >> + goto retry_under_rcu; > > Is adding this specific label worthwhile? How about rcu unlock and goto > retry (then it'll also be clear that we won't hold rcu read lock for > unpredictable time)? +1, sounds good to me -- Cheers, David / dhildenb