From patchwork Sat Oct 28 00:38:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13439339 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 53EA9C27C48 for ; Sat, 28 Oct 2023 00:38:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2BA86B0400; Fri, 27 Oct 2023 20:38:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD75D6B0401; Fri, 27 Oct 2023 20:38:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ADC346B0402; Fri, 27 Oct 2023 20:38:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A1D9F6B0400 for ; Fri, 27 Oct 2023 20:38:27 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8756FB6373 for ; Sat, 28 Oct 2023 00:38:27 +0000 (UTC) X-FDA: 81393009054.03.E48464C Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf13.hostedemail.com (Postfix) with ESMTP id C2A7620014 for ; Sat, 28 Oct 2023 00:38:25 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=leQPcjQn; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3AFg8ZQYKCNEFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3AFg8ZQYKCNEFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698453505; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Hd9WkLbC3/9tKbDr398vMG9sjKzgB6769/dIszrNtnA=; b=8RULVqt2mjLPIdwTq5qbG+lTAs//SRQpGDd2gqU7iBxM0I2c3tdiZe8HyJKJ8hfgUb7GIB JqHsMVQKpHSXwey3+EHVVn7cUi4buKcX+4y+k5/Dhg58B5Tcp+LGowDqajW8tsgDvEwryM 9UsUqv7ArQzKcVZcOfbcShmTMc3fMZg= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=leQPcjQn; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3AFg8ZQYKCNEFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3AFg8ZQYKCNEFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698453505; a=rsa-sha256; cv=none; b=7OQ2bvalrzFXYK167ox6X4JJoyd2lFbz8cvn6wgoP16RaA6ahOAGfn1eqcg3MtLon5EFah VlpWVXVbByphS5YKmxSHn+bdec5H1uO+McD2FjqtAUroA9ed651PjLPgjL4RLl9C19ThZ8 l5hWPcL3iMlk3miyvt5BRZf49y6YBlU= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59b5a586da6so24802617b3.1 for ; Fri, 27 Oct 2023 17:38:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698453505; x=1699058305; darn=kvack.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=Hd9WkLbC3/9tKbDr398vMG9sjKzgB6769/dIszrNtnA=; b=leQPcjQn951lVjYgpM2JlK2qa/LL5qIQPVIIGpXXdEyiN1X7A59Lz0m8jdz0qi0YkL ylXJvQRUVE+VLC40MVvQdvWggC9B3pgrugNpfOJlzgJbxeLAkSbZKKOZWoJY7k1JuTSf 8EyOXiSk9+ZRPaGctZ51HT4xXJ29LSVCrbyIJZO4ewFRIYLjvOQnrqgqzsUOpAJbPzZv sSdYZP2vb504ejhuwuWGpRl60Dfv6zzuSMItDF0lpWDoSxvwyA4WIxDrm5gN+IBhg4kg wmQ8C+sOuxDqwS/xcBVz5Rnq1cCryZJMOd89OxWLpy9ukQSD1P1viF7ZTg+9myGBPwBG 7nPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698453505; x=1699058305; 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=Hd9WkLbC3/9tKbDr398vMG9sjKzgB6769/dIszrNtnA=; b=Yyq+FZHBFpqX1xIgMB7dnme8Mi3HkfwE/yTKRtEnQ+km0n7MirxRfg91TQF9IdpUpV KtdRKCnFln1UD/kH5Lllh4g9sLiXVVRhLNofqwapzK91ZLgM4SYFpCIKjXXdinDTM0rG cwy4D0ra9XulfPN7Z5UgssVPc+fsWDt7wGmpmfSSLFp6Ypnb4skyCy1ZjW7FosqKv+VM 8zcMzd07bRnLo7pOY+iXq9ed68aGwsaAi7YN1zJseJ4UCvgKKCBfPt44xsei4Zo9HrH9 WOF1/2hqfRzQTeCOvEw4Z4gvLGOaRK2h5kyoCLW2HpE0OMZd4zYLUZR7BxT/ySRF5mU3 gtYw== X-Gm-Message-State: AOJu0YyM5TNplGW1WJ6c8TxNlG2DZ4VFV12Mje8tHbhR5hzHTUKM0bU3 MoRZd1jRTd9dtXxBt733XDnkpzIizHM= X-Google-Smtp-Source: AGHT+IEhFUjmYdEXljORLOc7MoR1TUT63j0H8MtfIU3FFKJvRVJyyGkKkhRak6IgKRfrZGiSU7tVlIjlryE= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:cba3:7e7f:79e4:fa57]) (user=surenb job=sendgmr) by 2002:a81:7648:0:b0:59b:b0b1:d75a with SMTP id j8-20020a817648000000b0059bb0b1d75amr194570ywk.4.1698453504908; Fri, 27 Oct 2023 17:38:24 -0700 (PDT) Date: Fri, 27 Oct 2023 17:38:11 -0700 In-Reply-To: <20231028003819.652322-1-surenb@google.com> Mime-Version: 1.0 References: <20231028003819.652322-1-surenb@google.com> X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231028003819.652322-2-surenb@google.com> Subject: [PATCH v4 1/5] 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 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: C2A7620014 X-Stat-Signature: wgzg8e78fggqfdc6hsdczoh1z56zzj7m X-HE-Tag: 1698453505-437284 X-HE-Meta: U2FsdGVkX1+0e3O1tkQ5V3ucnUaWHYxyAb+IDc2N4TQ/4SoSzFUJgT2fbKXr6MLjM5E7ZYEGXoAnuQTCZDB2FfMUdL3fkxUUJhk+bYOhiiv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OAPthHKOpmFuAmWHTpS0GwEBRkjkwrjbXtzM9mhRRph+xEIiG6CV2v8u2wblAyfblxpoTNK/IJJB1MHPWop/A+YBwUSfu1a3vUMTjOiKq3rOhlgtxEQBPpxBu7CMS9WUHSvGAKU47Gq4EyNGQVrx+Zq1N0OU2qsI7yzCnAanLqRz8pp8DtpguiSsGynhP9Ni29DqDzGVFTEB3Vr0CML3LRxLnK1doAbmxL6ElWjUb3BCzY8ecm/9AekKQ20EyA9VQbdI8m8NIEbatMXymAkswkk7n1oGgopb3bd5Vb7Knz6AFtFEQFtxd0+MSU0GBtb2nQaiY5Bn9ttKTuOTVtT/Y4bHSuzn2VW40h/kufX+FcsFGWPHdaAMmCVD9jES8A32t4LpAxkckfw/wEg3lt26FCZrz+u2RJLP+0VPK2sWDH4VYYyO2pR3KrFhif1qX+YNSlYC5WUZChhcCtySM33VBSTfhvLTmj9nLzBiAbNjVQQBQ== 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: List-Subscribe: List-Unsubscribe: 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 Acked-by: Peter Xu --- mm/rmap.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/mm/rmap.c b/mm/rmap.c index 7a27a2b41802..525c5bc0b0b3 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; +retry: rcu_read_lock(); anon_mapping = (unsigned long)READ_ONCE(folio->mapping); if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) @@ -552,6 +553,17 @@ 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); + rcu_read_unlock(); + goto retry; + } + /* * 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