Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp3135038rdb; Fri, 22 Sep 2023 21:40:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IES+EEBQ0GJPiDCPz9ilOPEf2kynC/pWbZAOmqPJ74CMHu18PjVQnC5JSTTRuO/mvBic0l7 X-Received: by 2002:a05:6358:912:b0:13a:6748:927d with SMTP id r18-20020a056358091200b0013a6748927dmr1474633rwi.0.1695444026105; Fri, 22 Sep 2023 21:40:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695444026; cv=none; d=google.com; s=arc-20160816; b=Bp0d9ZfWBdQBfJbHTByPlRUHSwcAUbcnmagyImi1zqxfAVAGh9DJ3Hn09mAZCkmkME Vnsr3bNudq1t7PU8ScEwpYo75JlXB5AgJjGEPrtqs9OJDc++GQz7Qu0v0US3vJfJ6Jn/ HRZLQdiyBp8o86BvTm/UYVJ+YlYe3KOUIeRV48GkTDAdNZcemC4ba7MII3dyvSUhWIy0 ZSlerJHLjRyjzOpO+iZRyO3cIpV93JlcRUc13OBDihJoxoQVYyD+FTQoWQiRyeIglVOH Y9TPq9UsUK/QyKowXmypDftn4seyDhj5DoM2LT4ki2gXDsxC6zKIPxpGlaA5SZsIMgxK lBwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=CmlRLzvRmqHtTQ7XfVaULJQdpUmF9g0LEd2VGVtMlKs=; fh=vBLx0KH+vftfPuCKolEKY+MKbAkCpwC5WBbaBlSuTXE=; b=XQZ5tAvtb2M0fO40bU5xIFysccYtaPKjJg9lLHfH+Yt9jJ7U+omDkgk6DIzamgdufl 4phZlfNJAGvHHpixHZvt5dBN3IQhQz2x2EavG6/xYZVB7yxOAMUVt+O8saTRqw0EzbWz Pt+khOExqnL88QjIad14qEUs5lvftWeKxjlFUi9QBV2nOLc/tqepVJSramBO3mgLPSXb xduXF40DOhMPQ3h182u8IRM2Qi/7u6ijQI7LhJReL/Srr3KZnN9y1m0rozdmCxXyLJmB coIaDB3COmJmkIsoRSuWrIDWrqBd6LE3YJsNUkg+0gp7aIHf/+xN/jmLvWfvZbrWJdIg A9iA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=G8owUdPz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id f14-20020a17090aa78e00b00274abe06b0asi5041838pjq.127.2023.09.22.21.40.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 21:40:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=G8owUdPz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id D134C82600BE; Fri, 22 Sep 2023 18:32:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231167AbjIWBcM (ORCPT + 99 others); Fri, 22 Sep 2023 21:32:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231133AbjIWBcB (ORCPT ); Fri, 22 Sep 2023 21:32:01 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 559FAF1 for ; Fri, 22 Sep 2023 18:31:55 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d81c39acfd9so4088851276.0 for ; Fri, 22 Sep 2023 18:31:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695432714; x=1696037514; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CmlRLzvRmqHtTQ7XfVaULJQdpUmF9g0LEd2VGVtMlKs=; b=G8owUdPznKVfLY45n4FQwwFNCPmcO1hv0oASRh5iwzDGrKQUSW0/GCJ665iIBhlwKK D8lQHxHOLRlGSHKaQgVPxw1U+crg7KdZ4UhMJR5n8G9zTuPet62f3XnykrnpPZe3wIj+ aD077xa/f0N1AxbxBY00jXiOB6mADwPkCBaF1csm95Gi9eeLwQSg5k7oRN8ujBSy7IWP TAYyeMwI1Xw20t9oXEJ7zm1y5PpVNwknrcepH+ZZpWtrUn0IQWNzjc+y50vheI/imNtn zqe1waeQAanWmFpFCkZrOplL65HipkM8KgGrRkOZKyAxE/hUFVyuQmtCP1LV5fQSwkCN O88A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695432714; x=1696037514; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CmlRLzvRmqHtTQ7XfVaULJQdpUmF9g0LEd2VGVtMlKs=; b=mZJZEKu4NlKKGi2snhw27nt4WBsVcmZdaAoIAAbA/Lh45hi2VrSdVCNg1J/Bmgpg0n 9Vy4i15AhM6GwCiWE3jaLH1CeZYfvwC2Li9/cNxaqVwTW1qCUfy9C/gj5Ehjog+Qjq9l 96wzGp+4ggQxrv24R8laefKzxBIkmpdw5sUqTkgqcQa5JgwtLcPcm9IC3FoWuofX4ncd Zv+/bwkTqHLav8YtcuMxIzt9o6od6+qcSFlGjQuiMEJ1DoQm34GXmSuROfIz95kM6cDu tok60TIDh7vuIEGdc5wepOPF3+UHUeJRjvb3KssCvPiTJRryrYceGzff/0m+DJV4ljC4 oJlg== X-Gm-Message-State: AOJu0Yw5gWyDs8UU5IylB/78HDLGcfFCJbw+5OG/mJnI2VhPZuK0hJ2u 4m3sKlRIeCrTXXvffNsTpClAPnu4kVU= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3398:6644:ed7f:bec9]) (user=surenb job=sendgmr) by 2002:a25:cb0f:0:b0:d7f:2cb6:7d8c with SMTP id b15-20020a25cb0f000000b00d7f2cb67d8cmr11540ybg.13.1695432714541; Fri, 22 Sep 2023 18:31:54 -0700 (PDT) Date: Fri, 22 Sep 2023 18:31:44 -0700 In-Reply-To: <20230923013148.1390521-1-surenb@google.com> Mime-Version: 1.0 References: <20230923013148.1390521-1-surenb@google.com> X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230923013148.1390521-2-surenb@google.com> Subject: [PATCH v2 1/3] userfaultfd: UFFDIO_REMAP: rmap preparation From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: viro@zeniv.linux.org.uk, brauner@kernel.org, shuah@kernel.org, aarcange@redhat.com, lokeshgidra@google.com, peterx@redhat.com, david@redhat.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, surenb@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" X-Spam-Status: No, score=-4.8 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Fri, 22 Sep 2023 18:32:22 -0700 (PDT) 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. UFFDIO_REMAP takes the anon_vma lock for writing before altering the folio->mapping, so if the folio->mapping is still the same after obtaining the anon_vma read lock (without the folio lock), the rmap walks can go ahead safely (and UFFDIO_REMAP will wait the rmap walk to complete before proceeding). UFFDIO_REMAP serializes against itself with the folio lock. All other places taking the anon_vma lock while holding the mmap_lock for writing, don't need to check if the folio->mapping has changed after taking the anon_vma lock, regardless of the folio lock, because UFFDIO_REMAP holds the mmap_lock for reading. There's one constraint enforced to allow this simplification: the source pages passed to UFFDIO_REMAP must be mapped only in one vma, but this constraint is an acceptable tradeoff for UFFDIO_REMAP users. The source addresses passed to UFFDIO_REMAP can be set as VM_DONTCOPY with MADV_DONTFORK to avoid any risk of the mapcount of the pages increasing if some thread of the process forks() before UFFDIO_REMAP run. Signed-off-by: Andrea Arcangeli Signed-off-by: Suren Baghdasaryan --- mm/rmap.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mm/rmap.c b/mm/rmap.c index ec7f8e6c9e48..c1ebbd23fa61 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -542,6 +542,7 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, struct anon_vma *root_anon_vma; unsigned long anon_mapping; +repeat: rcu_read_lock(); anon_mapping = (unsigned long)READ_ONCE(folio->mapping); if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) @@ -586,6 +587,18 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, rcu_read_unlock(); anon_vma_lock_read(anon_vma); + /* + * Check if UFFDIO_REMAP changed the anon_vma. This is needed + * because we don't assume the folio was locked. + */ + if (unlikely((unsigned long) READ_ONCE(folio->mapping) != + anon_mapping)) { + anon_vma_unlock_read(anon_vma); + put_anon_vma(anon_vma); + anon_vma = NULL; + goto repeat; + } + if (atomic_dec_and_test(&anon_vma->refcount)) { /* * Oops, we held the last refcount, release the lock -- 2.42.0.515.g380fc7ccd1-goog