Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp1717603rdb; Sun, 8 Oct 2023 23:43:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEJ1ExtyigAxSyi0bI0dTsl2BcasTo+uIa6wffbu3OlEIETFoDYXCKTy9h/Zr7h5hyPEEjh X-Received: by 2002:a05:6a00:1ac6:b0:690:2818:a4ce with SMTP id f6-20020a056a001ac600b006902818a4cemr19748953pfv.7.1696833790753; Sun, 08 Oct 2023 23:43:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696833790; cv=none; d=google.com; s=arc-20160816; b=uE2w+iQunZR0Zo+Jp3CyHHu0l/6PvER5PP0VX3uo6gRcZ+C+j+hKvkcJV3WNSd7B7G 6ybx/Kkc9MdkyRrM+5nc9rfL3eBdPMKnCMLMq9nWx8TafzyNFlLZthIgYtpsimaj+tbT Xn9vgqVslU2Ao8DbxA5ie3O6dgHUeQuewpBKCsZlzij/QQ1mruj0PamrKiBFcdkoxY/6 jt85LvYdBk4UIX6EPadgK0qBKrNDyh64z1sXI76ihxDrkD9ffXG9OpN/DV08cAWczmxR sC5kJF9HJMyQL96gimolWu5toY8zibjWYs9XUDDoxCJ7ya4IUkOqTFTWlLypYTFCErDV Xxqg== 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=GMzA+bY5iQ55icqv7/c58xVjlf8IiONnVGODeN5A6AA=; fh=vBLx0KH+vftfPuCKolEKY+MKbAkCpwC5WBbaBlSuTXE=; b=ihAB5zMKVAZFGqYv54zRdH2NOveAdwGKCc6Q75zfFDLt7pNWfeLlQlEExHoQyTwATo ed0eTVx4DYuDn0DPJK4QilB6oY34xBbO0UYPfn8oQcTFRXdKpfonXSnYJOxVd/OLuFUm XDHN4m74LmYiHkfDjGo13KqgDqmbHFK8Nr96RY7x57d4kv3DL2AUUQsKV98Ls0/fLT/8 uWgpBoDLzrxSpbE1gk3dwz55tViFvAiBUQQFDDyWfUgjArQve5EKVtnwQOXWuj7LSLw4 c3en2FNLqIR+A7VHYQABZOA1VAZ23evmPc9V4yNITayra2tGtLCXKu/w1vJVt4Xt2/hN k8KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bVDwtzWt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id k14-20020a6568ce000000b00578e9a29ee4si2477916pgt.240.2023.10.08.23.43.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 23:43:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bVDwtzWt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 00001805EB0F; Sun, 8 Oct 2023 23:42:58 -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 S1345322AbjJIGmn (ORCPT + 99 others); Mon, 9 Oct 2023 02:42:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345304AbjJIGmj (ORCPT ); Mon, 9 Oct 2023 02:42:39 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D01AEC6 for ; Sun, 8 Oct 2023 23:42:37 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-59f4f2a9ef0so66483267b3.2 for ; Sun, 08 Oct 2023 23:42:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696833757; x=1697438557; 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=GMzA+bY5iQ55icqv7/c58xVjlf8IiONnVGODeN5A6AA=; b=bVDwtzWtwc1VM4vQGrACZvNw57uyX10S4ICovNdmKLlGBwAwXVt37DKrZ8fYdL+i6s hg/S5stmKoEql8LybZzVlqpG+Pz9UQvORdFX+wTv3OyhcHlW9NQOhkjkHl/iS0EDhb6H SZ0UO1RKmg//m24u1crbacIuBCwM3kvDIs2NR73ct6bSeVuTPW1YmQhujjjHuBBWRbXp ozrIQXnECsYK0I2maQFKV7syjcTk9GXMjfKYsNNe4Se2e9YdbXQ21H9zkxX7UpKjYAa/ JKzmQAjBHM13eetlwt1Dz5TWT7ccI94TsKSumlFFdfTLpWG6eQpJqd+QonllWgCYVoOB 7Jtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696833757; x=1697438557; 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=GMzA+bY5iQ55icqv7/c58xVjlf8IiONnVGODeN5A6AA=; b=DjtjsIoEuYctDUgbSHDnMPVxm7bumQk67013rm4RCO878MIgvhkixn3sxvCoSLA0if mT1BjRz74gByqP/yYgtIztYPP77jzGXadJ+C7yn9KIQWCHU1JZFGboCnylv2RKorTiSI UY6iiJWpiyzp3bDf/kfqb75xyJ5w+ftMfQ2eVNBHBIJ3jti1GDs9M56ZBRXyRoS21Lx8 4fBbGKZC/MPVMYA6SGD26WVSuWmZ6wrg38/0H+KR2QB7LeEScP/oUvmHrxpxdZl+zXXB Ic4s++s+OXtw47BT90De6pRpCcKNtz6T5HxtQ5vPinv4bxiwQONzi6Isx8HKcbFswhDg P6ng== X-Gm-Message-State: AOJu0YyH9kV2EovA2rrO3hRoGiQVOeubpyTqdnUz7TutCUhXJz2TfkQ/ QhtFXDrtq0Xg7pZjmWFoEvQg8JYl9N8= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3e83:601e:dc42:a455]) (user=surenb job=sendgmr) by 2002:a81:a809:0:b0:59b:ebe0:9fce with SMTP id f9-20020a81a809000000b0059bebe09fcemr261242ywh.5.1696833757037; Sun, 08 Oct 2023 23:42:37 -0700 (PDT) Date: Sun, 8 Oct 2023 23:42:26 -0700 In-Reply-To: <20231009064230.2952396-1-surenb@google.com> Mime-Version: 1.0 References: <20231009064230.2952396-1-surenb@google.com> X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Message-ID: <20231009064230.2952396-2-surenb@google.com> Subject: [PATCH v3 1/3] mm/rmap: support move to different root anon_vma in folio_move_anon_rmap() 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 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]); Sun, 08 Oct 2023 23:42:59 -0700 (PDT) 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; + } + /* * If the folio is still mapped, then this anon_vma is still * its anon_vma, and holding the mutex ensures that it will @@ -586,6 +598,18 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, rcu_read_unlock(); anon_vma_lock_read(anon_vma); + /* + * 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)) { + anon_vma_unlock_read(anon_vma); + put_anon_vma(anon_vma); + anon_vma = NULL; + goto retry; + } + if (atomic_dec_and_test(&anon_vma->refcount)) { /* * Oops, we held the last refcount, release the lock -- 2.42.0.609.gbb76f46606-goog