Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2125013rdb; Tue, 3 Oct 2023 10:56:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH5CrsKGtmsB9OPE2jt3P9CyyURfdjoqSNRB2RvjkwvhY4tBTJKPjU8cYmjJS9+UHy76sw2 X-Received: by 2002:a05:6870:160d:b0:1d6:e8bd:5b48 with SMTP id b13-20020a056870160d00b001d6e8bd5b48mr255725oae.56.1696355804505; Tue, 03 Oct 2023 10:56:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696355804; cv=none; d=google.com; s=arc-20160816; b=YsPRX9hD249nu14VYNzYD69ecCJuFzsKAbW9rDno2CRiGIQkLL4K5ZjpaXAEYH8NZt fgDnVezuvW+qqE25g4s1WJI9+iFziAMtcd69LyYM7ho1xUwVhf9YnkJjD1GuU4WPZvhp 6YxuKo2XLhLf91cKT6yP2SOcdNuxQw7CBMeBCBVnuNIAJhF0YL8MQ/ZTnTOpXXkc647z DqBbHshtTdLzEoHpIxZAtkr0Q8C6UunSIGxAzItH9WEVzh/C2HZmiraW/hg0ZwfRjWkl efKcLyWzMFE8l/Xj/wyEw3FcnrDxo1zVb+AhL8Kj58u1wma537VQ3W453EU9gXOYcXNe D3NA== 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=4vl0fsz4J68Cnn2M5AWsY3O6OVBkAbz573ZVyP6v8RU=; fh=/yWRANWYd0dgFr7ria3T2K+/VbrYH6Dj/HpxAYu019I=; b=jKtTo/48/BhPY2OQY5IhwuQIOtfy4e0N2QmLnXMPoBq6C1CRWgbWFCy8S32kBW67V5 IHWaVLRWtKcNo84R8r1wPKn5z3GLkCRWT+0wRsLB7gkON24MdfSDI8faC1FF7827rFOh gjPnNdzT1LYi1zZFlmvaitEssr6RxCWG7MnF2jnlaU6FASqZz71bYJxkP/aj4H5/BknE Jd4g3jqUXZQk+1cCCXYW1RGq2O2t27LWKOlDAESrlRr5wD+tFmsQeKe/YCI3AJb/yA91 +ZDRLv7i3RIaGRNmGl9vW73qtyRARzBKMOLuWcn3eXu4lrcqksJSTPV86lnZ/keWJR0Z PIfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=byIujj7L; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id c5-20020a6566c5000000b00584a7863503si1938833pgw.372.2023.10.03.10.56.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 10:56:44 -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=@google.com header.s=20230601 header.b=byIujj7L; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 58681804279E; Tue, 3 Oct 2023 10:56:35 -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 S240723AbjJCR4W (ORCPT + 99 others); Tue, 3 Oct 2023 13:56:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240714AbjJCR4U (ORCPT ); Tue, 3 Oct 2023 13:56:20 -0400 Received: from mail-yw1-x1133.google.com (mail-yw1-x1133.google.com [IPv6:2607:f8b0:4864:20::1133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FAADBF for ; Tue, 3 Oct 2023 10:56:17 -0700 (PDT) Received: by mail-yw1-x1133.google.com with SMTP id 00721157ae682-5a22f9e2f40so14303107b3.1 for ; Tue, 03 Oct 2023 10:56:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696355776; x=1696960576; 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=4vl0fsz4J68Cnn2M5AWsY3O6OVBkAbz573ZVyP6v8RU=; b=byIujj7LX5PdeWfGJNc840K2lhqEXxX9Prx44oyQiYxprBc+c79C24Yfsuy/14+W5i 3mLVJHJoBLxL3OHxgR0SzsZV37HwJ6PCKYVRLSYWkt/EcEIMYhXGm8vgNCqO9S99VBH0 RzAINzChqyw3qR0lUi1Hkp/hx+EERiZLtQr6hiGefnz/ySUbSHxK+BOC7rkEIpxvY+2D smBTwwQLcQnnLubr3WJ5i3hp1wjn8p/PODzy5ynz4lZhyEwoPMHQBpVVw7wKVogXFB+Y uw50S/J9pZG/pejizAewVKuznLAg/ueFWjPhZ/Hjw47icvKe4ddL5t+U46U8BFgPN0g1 Ho9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696355776; x=1696960576; 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=4vl0fsz4J68Cnn2M5AWsY3O6OVBkAbz573ZVyP6v8RU=; b=euhXD7GJsWyd75nOIXmUGVNdvmrdQjNnb0xoJYn6XZklZOgesFngvrccyeUlhRLb9Z j/0BNx/U5ox2t8wK5FjhRtfaAYlyQ3U9XKGyjVgQJOLpzHMhdUl9qbMZnCY343EHu85V MLDxOnUj77i54wqXrlm6FYrtbQmRbzkyTl8tMC09SD7iFsiMZrS5KspotnmzJgwm3QE6 8f8zLBsVzzt8Wcq5Ha8MxoyoV0vO/pXzUL1wgUHo4DnUneSjVNAW6TrAi/F8YK9hDB/t 2I5MPb3VrWrABHr6CtorefkVurh2PEjXR7VTxfeO1ze7BwLHmECSaW1FQ8qASY/2tSyQ RCdw== X-Gm-Message-State: AOJu0YxdRvtZRjEXHWnP8bqG5MvCFyC/30vJzTUNY4PkiHfh3qjB4IaI n8oRZ7Y/fPTElYHBmfw8ME0syYCvBOtemVKtYN8BRw== X-Received: by 2002:a0d:df46:0:b0:5a0:c0d0:1c48 with SMTP id i67-20020a0ddf46000000b005a0c0d01c48mr331306ywe.6.1696355776271; Tue, 03 Oct 2023 10:56:16 -0700 (PDT) MIME-Version: 1.0 References: <20230923013148.1390521-1-surenb@google.com> <20230923013148.1390521-2-surenb@google.com> <27f177c9-1035-3277-cd62-dc81c12acec4@redhat.com> In-Reply-To: From: Suren Baghdasaryan Date: Tue, 3 Oct 2023 10:56:01 -0700 Message-ID: Subject: Re: [PATCH v2 1/3] userfaultfd: UFFDIO_REMAP: rmap preparation 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 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]); Tue, 03 Oct 2023 10:56:35 -0700 (PDT) On Mon, Oct 2, 2023 at 10:30=E2=80=AFAM David Hildenbrand wrote: > > On 02.10.23 17:23, Peter Xu wrote: > > On Mon, Oct 02, 2023 at 04:42:50PM +0200, David Hildenbrand wrote: > >> On 23.09.23 03:31, Suren Baghdasaryan wrote: > >>> From: Andrea Arcangeli > >>> > >>> As far as the rmap code is concerned, UFFDIO_REMAP only alters the > >>> page->mapping and page->index. It does it while holding the page > >>> lock. However folio_referenced() is doing rmap walks without taking t= he > >>> folio lock first, so folio_lock_anon_vma_read() must be updated to > >>> re-check that the folio->mapping didn't change after we obtained the > >>> anon_vma read lock. > >> > >> I'm curious: why don't we need this for existing users of > >> page_move_anon_rmap()? What's special about UFFDIO_REMAP? > > > > Totally no expert on anon vma so I'm prone to errors, but IIUC the > > difference here is root anon vma cannot change in page_move_anon_rmap()= , > > while UFFDIO_REMAP can. > > That does sound reasonable, thanks. > > Probably we can do better with the patch description (once [1] is used > to move the folio to the other anon_vma). I'll develop the next version with your patches in the baseline. Hopefully by the time of my posting your patches will be in the mm-unstable. > > "mm/rmap: support move to different root anon_vma in folio_move_anon_rmap= () > > 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 page 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 page lock but only holding the > anon_vma in read mode, holding the page lock is insufficient. > > When moving to an anon_vma with a different root anon_vma, we'll have to > hold both, the page 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_REMAP, which will hold the page lock, > the anon_vma lock in write mode, and the mmap_lock in read mode. > " Thanks for taking time to write this up! Looks really clear to me. I'll reuse it. > > In addition, we should document these locking details for > folio_move_anon_rmap() and probably not mention UFFDIO_REMAP in the > comment in folio_lock_anon_vma_read(), but instead say > "folio_move_anon_rmap() might have changed the anon_vma as we might not > hold the page lock here." Sounds good. Will add. > > > [1] https://lkml.kernel.org/r/20231002142949.235104-3-david@redhat.com > > -- > Cheers, > > David / dhildenb >