mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shivank Garg <shivankg@amd.com>
To: "Andrew Morton" <akpm@linux-foundation.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Michal Hocko" <mhocko@suse.com>, "Dev Jain" <dev.jain@arm.com>,
	"Zi Yan" <ziy@nvidia.com>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Nico Pache" <nico.pache@linux.dev>,
	"Ryan Roberts" <ryan.roberts@arm.com>,
	"Barry Song" <baohua@kernel.org>,
	"Lance Yang" <lance.yang@linux.dev>,
	"Usama Arif" <usama.arif@linux.dev>,
	"Kiryl Shutsemau" <kas@kernel.org>,
	"Peter Xu" <peterx@redhat.com>, "Shuah Khan" <shuah@kernel.org>,
	"Michał Mirosław" <emmir@google.com>,
	"Andrei Vagin" <avagin@gmail.com>,
	"Paul Gofman" <pgofman@codeweavers.com>,
	"Bharata B Rao" <bharata@amd.com>,
	"Daniel Wade" <danjwade95@gmail.com>,
	"Yin Tirui" <yintirui@gmail.com>,
	"Muhammad Usama Anjum" <usama.anjum@arm.com>
Cc: <linux-mm@kvack.org>, <linux-kselftest@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, Shivank Garg <shivankg@amd.com>
Subject: [PATCH 3/3] selftests/mm: soft-dirty: check that mprotect() keeps PTE-mapped THPs clean
Date: Wed, 25 Nov 2026 13:32:29 +0000	[thread overview]
Message-ID: <20261125-b4-mm-soft-dirty-fixes-v1-3-538e3cdb618c@amd.com> (raw)
In-Reply-To: <20261125-b4-mm-soft-dirty-fixes-v1-0-538e3cdb618c@amd.com>

Add a test that clears the soft-dirty bits of a dirty, PTE-mapped THP,
mprotect()s it without writing to it, and checks that pagemap reports no
page as soft-dirty.  mprotect() batches the PTEs of the large folio, so
this covers modify_prot_start_ptes().

On x86-64, the test fails without the previous patch.  It is skipped
without THP.

Signed-off-by: Shivank Garg <shivankg@amd.com>
---
 tools/testing/selftests/mm/soft-dirty.c | 48 ++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/soft-dirty.c b/tools/testing/selftests/mm/soft-dirty.c
index 7f649b673355..1764e285f125 100644
--- a/tools/testing/selftests/mm/soft-dirty.c
+++ b/tools/testing/selftests/mm/soft-dirty.c
@@ -310,6 +310,51 @@ static void test_merge(int pagemap_fd, int pagesize)
 	munmap(map, 2 * pagesize);
 }
 
+/*
+ * Clear the soft-dirty bits of a dirty PTE-mapped THP, then mprotect() it
+ * without writing to it. mprotect() batches the PTEs of the large folio
+ * and must not mark them soft-dirty.
+ */
+static void test_thp_mprotect(int pagemap_fd, int pagesize)
+{
+	size_t hpage_len = read_pmd_pagesize();
+	size_t len = hpage_len - pagesize;
+	unsigned long nr = 0;
+	size_t offs;
+	char *map;
+
+	if (!thp_is_enabled() || !hpage_len) {
+		ksft_test_result_skip("Test %s no THP\n", __func__);
+		return;
+	}
+
+	map = memalign(hpage_len, hpage_len);
+	if (!map)
+		ksft_exit_fail_msg("memalign failed\n");
+	if (madvise(map, hpage_len, MADV_HUGEPAGE))
+		ksft_exit_fail_msg("madvise failed\n");
+	memset(map, 1, hpage_len);
+
+	if (!check_huge_anon(map, hpage_len, 1, hpage_len)) {
+		ksft_test_result_skip("Test %s no THP\n", __func__);
+		goto out;
+	}
+
+	/* Zapping the last page PTE-maps the rest of the THP. */
+	madvise(map + len, pagesize, MADV_DONTNEED);
+	clear_softdirty();
+	mprotect(map, len, PROT_READ);
+
+	for (offs = 0; offs < len; offs += pagesize)
+		nr += pagemap_is_softdirty(pagemap_fd, map + offs);
+	ksft_test_result(!nr, "Test %s soft-dirty clear after mprotect\n",
+			 __func__);
+
+	mprotect(map, len, PROT_READ | PROT_WRITE);
+out:
+	free(map);
+}
+
 static void test_mprotect_anon(int pagemap_fd, int pagesize)
 {
 	test_mprotect(pagemap_fd, pagesize, true);
@@ -330,7 +375,7 @@ int main(int argc, char **argv)
 	if (!softdirty_supported())
 		ksft_exit_skip("soft-dirty is not support\n");
 
-	ksft_set_plan(19);
+	ksft_set_plan(20);
 	pagemap_fd = open(PAGEMAP_FILE_PATH, O_RDONLY);
 	if (pagemap_fd < 0)
 		ksft_exit_fail_msg("Failed to open %s\n", PAGEMAP_FILE_PATH);
@@ -343,6 +388,7 @@ int main(int argc, char **argv)
 	test_mprotect_anon(pagemap_fd, pagesize);
 	test_mprotect_file(pagemap_fd, pagesize);
 	test_merge(pagemap_fd, pagesize);
+	test_thp_mprotect(pagemap_fd, pagesize);
 
 	close(pagemap_fd);
 

-- 
2.53.0


  parent reply	other threads:[~2026-11-25 13:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 13:32 [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios Shivank Garg
2026-11-25 13:32 ` [PATCH 1/3] mm: move the soft-dirty fallbacks up in pgtable.h Shivank Garg
2026-11-25 16:44   ` kernel test robot
2026-11-25 16:44   ` kernel test robot
2026-11-25 13:32 ` [PATCH 2/3] mm: don't let modify_prot_start_ptes() set soft-dirty on clean PTEs Shivank Garg
2026-11-25 13:32 ` Shivank Garg [this message]
2026-11-25 13:43 ` [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios David Hildenbrand (Arm)
2026-11-25 14:16   ` Garg, Shivank
2026-11-25 15:42   ` Paul Gofman
2026-11-25 16:11     ` Garg, Shivank

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125-b4-mm-soft-dirty-fixes-v1-3-538e3cdb618c@amd.com \
    --to=shivankg@amd.com \
    --cc=akpm@linux-foundation.org \
    --cc=avagin@gmail.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=bharata@amd.com \
    --cc=danjwade95@gmail.com \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=emmir@google.com \
    --cc=kas@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=nico.pache@linux.dev \
    --cc=peterx@redhat.com \
    --cc=pgofman@codeweavers.com \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=usama.anjum@arm.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=yintirui@gmail.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®