From patchwork Tue Nov 21 17:16:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13463393 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 3375AC61D92 for ; Tue, 21 Nov 2023 17:16:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 952986B04C3; Tue, 21 Nov 2023 12:16:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 901606B04C4; Tue, 21 Nov 2023 12:16:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7328D6B04C5; Tue, 21 Nov 2023 12:16:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5F0516B04C3 for ; Tue, 21 Nov 2023 12:16:52 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2DE0F1A051D for ; Tue, 21 Nov 2023 17:16:52 +0000 (UTC) X-FDA: 81482616264.19.033D17D Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf15.hostedemail.com (Postfix) with ESMTP id 4D5DAA0023 for ; Tue, 21 Nov 2023 17:16:50 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZFIBBeWo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3AeZcZQYKCHIikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3AeZcZQYKCHIikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700587010; 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=He74YIcSIwOjGCEm7bMpO/TfFvRAcUHKo6jbQtwLQrs=; b=tJFBhmNNhugquOxgvkzVpgtlyRfCnW2MZigEKys0xHbF/1HpjsIYPtg40J2GnfBqTxYq5D Eyg5GjcIDm85Z9xMTOOIrjVaw/W8cv9p5W+4TvZiTgzIMPkrkr3EYELaN6ZZhRXB9iCdns 998Gw0hTYVbCvrSa30tOGBF1xIwcqig= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZFIBBeWo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3AeZcZQYKCHIikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3AeZcZQYKCHIikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700587010; a=rsa-sha256; cv=none; b=q9GCR6kJm5voSMRpEyWptgISJttGHViwZxhdjc3z/tq+jm2v0U2p23qPFSvge6S42SOBpK 4jB0zKomvSR5VyNsCnFvsRXtY7P2t+aciQSyV/soeG47Vun8e8rsKCjKH2hTf0v0OGE0mg WDxYAatDJszNpkQRKIyxZHL8ZoAC2RY= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5c87663a873so62597987b3.2 for ; Tue, 21 Nov 2023 09:16:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700587009; x=1701191809; 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=He74YIcSIwOjGCEm7bMpO/TfFvRAcUHKo6jbQtwLQrs=; b=ZFIBBeWoIu/4KUmyf3dXBwDV1j/AwzL7Mysee1o3Ha8Zhliv4/vLqNnLuX4PaxJraI svPDEfiDrXes8BZNU+tjP9buvPnZbZ+6Tiz6+F8Sy0PQojyX0RU1aS7nZ5bUX88+E5B7 HgvoKOLZVLTwDplsxxb5hJnfXhjgp1UYnhRhtXbIporNzISMhLkp9ookzF1PUkm/yXAe ODyawm0e3ADD/I6CdzXvbD7EFqE9gJpK0RM0HZDJrggCZgcMXimcERRcB/3pUhbaIdYB i9sf+rMZjLY5CKqgXw7rZc0/VZQhl+D/26QiV7FV2O88GjGip/ytJ+kowCDmMXMQOXve b5nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700587009; x=1701191809; 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=He74YIcSIwOjGCEm7bMpO/TfFvRAcUHKo6jbQtwLQrs=; b=Yk57jaDVsEunYTn+tY+hPfBnjxTcaKLnqH99TSBLkvBx5xAwDM+wTxfcGueQD/Ld1r uAJJRks3Hfp5mmNeAJaHGNkmvnuuiuBp6LkAJGATH5txe+oSZp5kW2sXIBRTB2OLcWKw +9pccugCGqNJ1ZFJnXV3sK5IWTDFr2RAFXZQ7sdnEBPFdqpSrdrAAqPGVY/Rap2uTjBI gfqX38NyOh19+SfrDz7mzq135spwBfJz1yzc+ITj4jRS4CUBaO6u10EsGIBfTfWuSrDT zi39A4DcJXXwgz87ROUSPkGMq0fjGquOvkC4CPR2NPkvLUX/pjPvAP3auXlzpbvKKx8o g8qA== X-Gm-Message-State: AOJu0Yy58RQl1YL5wdp9kKPfgWbj8BMzhS2ZMenntEiynS7KzvesXtZ0 uq2jQVXC7tHxAvglN/WABMDEWe7ALEQ= X-Google-Smtp-Source: AGHT+IGyUrMT5Mj/g7lBAQcYKLbTTgQRTtXQcXK7X2hl+2vA5HYqDMy63skEG09MWnl+1v4a8CZ5k0TbKFY= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:2045:f6d2:f01d:3fff]) (user=surenb job=sendgmr) by 2002:a0d:fb03:0:b0:5c8:b756:f3af with SMTP id l3-20020a0dfb03000000b005c8b756f3afmr330699ywf.4.1700587009501; Tue, 21 Nov 2023 09:16:49 -0800 (PST) Date: Tue, 21 Nov 2023 09:16:34 -0800 In-Reply-To: <20231121171643.3719880-1-surenb@google.com> Mime-Version: 1.0 References: <20231121171643.3719880-1-surenb@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Message-ID: <20231121171643.3719880-2-surenb@google.com> Subject: [PATCH v5 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-Rspamd-Queue-Id: 4D5DAA0023 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: df53x61yre399wawahjzwd1h4xabhy3b X-HE-Tag: 1700587010-151162 X-HE-Meta: U2FsdGVkX1+RLbU+GM1z9WpqjrWyFg7HHA7k6yVMKa5kEBGAXzF1Woj83/LL8uSf7ZD1I8jnk1MCs1qbhYpkdPkudbRU09vWQml4PhPLixm2X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C4yCmSjUEGUgl0ToiSQicfn3IaBaRbmUdQ1waK1urgGjJFPILp56wk90tbijT6Z4KQl5Esn//q803iHdJpqc9JYCZ20s6YK5FVddiN9/3GAPOHaZeyfpXtrLANHX2QpdPCjYY9ZxUW9iM+6tHhAXI1F+D8SVSdSQpUBh21V6a3MePTGtW8APnWBwMJq37RyWtVQRzo5YbLLawq/7hPRdEyq5AydvSdbJmACyjue9tGYZIAm7GX5Zzf92VhMenSCRpZvncc/JzYVIwPJeLyNyOQ1PTWDYBlLLN8N9l4hWf2woA+Zu9FgGd+xHmWkCqUqFcgVNtIepNQR2/3VkCRfPNK6xXI07O6u+6NiD6+hhB3Uuatyv+3s1yxR98MWGjTIXMLy8PV2EZqnKiXVKcNJZD/19SXQCgpsV4JS+XpN+aUAh/dLptP3n8ZZPfQuSgGOaL92pap4aWn46rYlaHQFKIPOiDKHLBjIFzTfEXDTlaFjDV/XYvSg== 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