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 C34A6E748FE for ; Mon, 2 Oct 2023 17:30:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2456C8D0049; Mon, 2 Oct 2023 13:30:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CF7B8D000E; Mon, 2 Oct 2023 13:30:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 047788D0049; Mon, 2 Oct 2023 13:30:43 -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 E3BCF8D000E for ; Mon, 2 Oct 2023 13:30:43 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E0238B3CB6 for ; Mon, 2 Oct 2023 17:30:42 +0000 (UTC) X-FDA: 81301211124.16.7931CB9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 82BF140005 for ; Mon, 2 Oct 2023 17:30:40 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="e/f2yQYt"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf12.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696267840; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1Zv0IPJYzYkiErG2s6ydEpQAC8u1zC0W3lAo3suRrzY=; b=g5VASB8j9OLkZ3/nex2JL/QgL6Yd0S8k8Ac8Xa3pVCe3ZniNO7WTtAh9eAC/1/Hg6Rz4tH HDWQqhLgzJ575DXeBUDX2ubOBqC9yVHqxwCx9JIZRdDfWVeh3v3j+Yi+Tl3c2ix0ppg0Mh r51Cmxr62ae9+bIvfPVyRR2o0gQPGiw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="e/f2yQYt"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf12.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696267840; a=rsa-sha256; cv=none; b=vzeJ3Zf3ycP6ks+6dTkFKoJ4THgJADcyF6on/s8WJD4qySCAfcG76byiosIXA6xWKEZECn ZhtW3HVihUDqqmx52HRNq7ylxEK4yiZ9Fnnuy4bbVEGesNXGLavotM+KpOZ/R9jFC78dqZ BfV8LR/xqU/j1D0JewWp1mmyqxDg5Wk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696267839; 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=1Zv0IPJYzYkiErG2s6ydEpQAC8u1zC0W3lAo3suRrzY=; b=e/f2yQYtzBIREkqYknpBkj+1B0brnbtdb9ZH3LL+mpUiVNkTYw4OxdibMhdL7PD4NzVQSA ZKIxO4gvdwxpLIUqWA2qalgofbE08RBtlahCv7XNVpioy3zmk/s8gKJNuRSTiEm3KvUlIU uKoOwmRwMehEgkR05Cs9C1qxy7ElqdA= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-cRzxFcpCNTKmloNNsXbYYA-1; Mon, 02 Oct 2023 13:30:38 -0400 X-MC-Unique: cRzxFcpCNTKmloNNsXbYYA-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-321726b0238so26769f8f.2 for ; Mon, 02 Oct 2023 10:30:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696267837; x=1696872637; h=content-transfer-encoding:in-reply-to:subject:organization:from :content-language:references:cc:to:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1Zv0IPJYzYkiErG2s6ydEpQAC8u1zC0W3lAo3suRrzY=; b=F88gZjo2wnF+Qh8VIhbBug6MqYZa1vkA5igTITocyeI0t3B8sHx1Yn2Ag36eUt5Epy z6mJjhaCcuxkMn3+dxE07KNKBMm+m2bkdw9D/l7DXElQITr16zoFVIdF3Z8uPE42Mccf DJsyLT9MAvaMAnpCrWTeL32Oclw0+BUHTrAEKS/OI/y8npU1MxEWvtM4mI4FEZYgwD4v MBu5PYGtVv34CeMhRCY7HG7o6EKr9tv2rmIxVrgkUoIAVl/Rh8c/UKmpu0umwxcU1/RL D7jTZDtNlbcyocCSConsVaZ2WXoV8YDb81u0kfd23Td44zWRRu9EZAhu8vKU3uJ862g6 sR5w== X-Gm-Message-State: AOJu0Yw2+kVsz8MFC/b363isaao0qty0fS82si2SM/ZQoR4Sk2CSM+xz 02556L5fUOINvfOZX243O5YbK+o+E4LiAjZIx0MFC7Lq96WhSG5y+4kKZ+ADwwneEm0rQ8pUUX0 QkXPUYdFN/L0= X-Received: by 2002:a5d:448c:0:b0:321:4d1c:425f with SMTP id j12-20020a5d448c000000b003214d1c425fmr10461724wrq.47.1696267837287; Mon, 02 Oct 2023 10:30:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH1s/Oh/sK26P/qhtJnkQ9pABgb0+Kto2RYoGCs5kChT5kKPuRRCO3dybkxiXNrHhCDhqfXMQ== X-Received: by 2002:a5d:448c:0:b0:321:4d1c:425f with SMTP id j12-20020a5d448c000000b003214d1c425fmr10461690wrq.47.1696267836848; Mon, 02 Oct 2023 10:30:36 -0700 (PDT) Received: from ?IPV6:2003:cb:c735:f200:cb49:cb8f:88fc:9446? (p200300cbc735f200cb49cb8f88fc9446.dip0.t-ipconnect.de. [2003:cb:c735:f200:cb49:cb8f:88fc:9446]) by smtp.gmail.com with ESMTPSA id r17-20020adfda51000000b00327c79ca7c6sm3612731wrl.80.2023.10.02.10.30.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Oct 2023 10:30:36 -0700 (PDT) Message-ID: Date: Mon, 2 Oct 2023 19:30:35 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 To: Peter Xu Cc: Suren Baghdasaryan , 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: <20230923013148.1390521-1-surenb@google.com> <20230923013148.1390521-2-surenb@google.com> <27f177c9-1035-3277-cd62-dc81c12acec4@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v2 1/3] userfaultfd: UFFDIO_REMAP: rmap preparation In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 82BF140005 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: nfjd3ousub8541oubap6azqyasmw387g X-HE-Tag: 1696267840-827411 X-HE-Meta: U2FsdGVkX19LxKThQ7Xa9CYMnFseW+ZoQLzgZe1bOv4V8YlxktYDzCoA37Vm4hx6j1RIrZZAvw5NzV38DU4JrCr0VTfRPidIsTqciSM9QQQR6evFNYJSWj2HS75yuqc937Y9m3j+SRpi8b92iBQ9mwODMQH+z1CR90eovUWxNdOdtAMkNZEAHUsSUL/yzaTVM4P5SEgoziVZu0TJ4eJ5Fq6AmFdd1bjAAOFAQc1EKPqzBgpAB1FPTM8f3EWMiO3TL3MIG6sDNPM+GcF5iexbi/Qk2HV2anldLO267O+M69AjX93aoKr9RJbjbtUSOSd1WCZ+tqLF7x8yhN2t+YDdLBlpbJ5jZeEUVGAgJ+LSv2UKInMVKBB8oXvgurYekP/L5AyTDnsEb933QGC2xmUicyGcqsKJrom7YTwksLQFGhE7+pxonp2yJgsPtIKMdMlZhlqKzRl8Uivn1n9pTbVOxVrME2aL/+ZyfpdA9jsmcSri/hR7G3doq1wd62zhQQflK0lNM+7eBVhjTe2lbqodrJobsRCXw3bAewbVxwNSo6kq3UK1buFW0SMhkGeTENDnEsCSfT2ZzrU+pzsmeq10/Gpi2adlorHOgg55Qm4LhRt7cv6T3bIRDiS9p2WPo/UWJYV3oKQrrB9P/96aaCXDZMRMBOtT74RmpDSGyDLrzsSfKMKDf7n5fJ6F+drjrxmCMx4wKadVlFoAFSlBFDc024CYJxm4GRiJT1fABJDKst6qotFc8MqepgXulLnFLVwZPNK57+cEDUDtDQ3KGZ+ed76tBVMF7XAhs7IMN1BeUygO3LzzUJny8Gu8gInFZr7Kch+ePGC4raKlK0/rQDp4YzYOOpxO8QesePxOCRR+HYKc3vs6/0c50KJ4cmlK0DvlVxvRqDuAHWf0+6zLnAovxacjGcz988Mcz1JW/4CK7MeMqn6GcmVDGDuopts2s9wuSjH4B1XiGubqOt+ZnzG 3/hDB5gV HlH/vXlxb0A1TZvjet7vBuArjSJRH/x1z1Baqvtl/SfkVwBJCyGEMSC7JcoYlW320LvPmHHiq6UGB8Y83+d2Q9vTbyX9gG2JxjP2obFopNyMhr7/BhmCYJHSjJWul0upMVhvTafm+4Qocush6QCwLFHR8VuLqQYufdgo862ru5xLGn/ps8Gh2do9YqQrMPDZG7y617Nzfy/KdcM5TSvw/7YC5EeIEMHcYcDy4/gW17F1vz/V9yL3vZ0aYNtb3Q87qkE+OLBrNZqEDzsO+kw9uUVY6aiZbxeoExpwnvzzoR4zpob5vWAeqgB72aTNP93dYP8jVAjc6BKg7NLCp98pC6w1asKhaE1jHdr2J5/1jGCf5eJfQ9Cejj0KkVMQSOW57eJ6D2na5e44CqN6QKrxE6rtGn4vtyf7URH0vz8MKCfADpw7557VW5CG27xvBPVVUjLRPJj6wzu8rLXEbI9Vs3i+EfhMhNcdn4oAfRS5tth/R/j7MAytBD97k4oFUsT6pOvH5BQ/IvtjoYjY= 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 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 the >>> 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). "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. " 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." [1] https://lkml.kernel.org/r/20231002142949.235104-3-david@redhat.com -- Cheers, David / dhildenb