mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sarthak Sharma <sarthak.sharma@arm.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@kernel.org>
Cc: 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>, Shuah Khan <shuah@kernel.org>,
	Zi Yan <ziy@nvidia.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	Nico Pache <nico.pache@linux.dev>,
	Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	Anshuman Khandual <anshuman.khandual@arm.com>,
	Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
	Usama Arif <usama.arif@linux.dev>,
	Kiryl Shutsemau <kas@kernel.org>,
	linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Sarthak Sharma <sarthak.sharma@arm.com>
Subject: [PATCH 2/4] selftests/mm: detect masked PFNs in allocate_transhuge()
Date: Thu, 12 Nov 2026 14:32:48 +0530	[thread overview]
Message-ID: <20261112090250.334344-3-sarthak.sharma@arm.com> (raw)
In-Reply-To: <20261112090250.334344-1-sarthak.sharma@arm.com>

allocate_transhuge() uses pagemap PFNs to determine whether a THP
was allocated. But pagemap PFN returns zero PFNs when the caller
is not allowed to read them (without CAP_SYS_ADMIN). Currently
this looks like a normal THP allocation failure.

When both pagemap entries are present but their PFNs are zero,
return -EACCES since the THP size and alignment cannot be verified.
In case of a base page fallback, the second entry won't be present
and thus it will still return -1 indicating a THP allocation failure.

Update ksm_tests.c to skip the test when pagemap PFNs are
unavailable. While here, use a common cleanup path and
add the missing close(pagemap_fd).

Signed-off-by: Sarthak Sharma <sarthak.sharma@arm.com>
---
 tools/testing/selftests/mm/ksm_tests.c | 26 +++++++++++++++++---------
 tools/testing/selftests/mm/vm_util.c   |  9 +++++++++
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c
index 9e6e1633d0b5..9f97ce28e72a 100644
--- a/tools/testing/selftests/mm/ksm_tests.c
+++ b/tools/testing/selftests/mm/ksm_tests.c
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdint.h>
+#include <errno.h>
 #include <err.h>
 
 #include "kselftest.h"
@@ -521,6 +522,8 @@ static int ksm_merge_hugepages_time(int merge_type, int mapping, int prot,
 	unsigned long scan_time_ns;
 	int pagemap_fd, n_normal_pages, n_huge_pages;
 	const uint64_t hpage_size = pmd_psize();
+	int status = KSFT_FAIL;
+	int64_t pfn;
 
 	if (!thp_is_enabled()) {
 		ksft_print_msg("Transparent Hugepages not available\n");
@@ -551,7 +554,13 @@ static int ksm_merge_hugepages_time(int merge_type, int mapping, int prot,
 	n_normal_pages = 0;
 	n_huge_pages = 0;
 	for (void *p = map_ptr; p < map_ptr + len; p += hpage_size) {
-		if (allocate_transhuge(p, pagemap_fd) < 0)
+		pfn = allocate_transhuge(p, pagemap_fd);
+		if (pfn == -EACCES) {
+			ksft_print_msg("Pagemap PFNs are unavailable\n");
+			status = KSFT_SKIP;
+			goto out;
+		}
+		if (pfn < 0)
 			n_normal_pages++;
 		else
 			n_huge_pages++;
@@ -563,13 +572,13 @@ static int ksm_merge_hugepages_time(int merge_type, int mapping, int prot,
 
 	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
 		ksft_perror("clock_gettime");
-		goto err_out;
+		goto out;
 	}
 	if (ksm_merge_pages(merge_type, map_ptr, map_size, start_time, timeout))
-		goto err_out;
+		goto out;
 	if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
 		ksft_perror("clock_gettime");
-		goto err_out;
+		goto out;
 	}
 
 	scan_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
@@ -581,12 +590,11 @@ static int ksm_merge_hugepages_time(int merge_type, int mapping, int prot,
 	ksft_print_msg("Average speed:  %.3f MiB/s\n", (map_size / MB) /
 					       ((double)scan_time_ns / NSEC_PER_SEC));
 
+	status = KSFT_PASS;
+out:
+	close(pagemap_fd);
 	munmap(map_ptr_orig, len + hpage_size);
-	return KSFT_PASS;
-
-err_out:
-	munmap(map_ptr_orig, len + hpage_size);
-	return KSFT_FAIL;
+	return status;
 }
 
 static int ksm_merge_time(int merge_type, int mapping, int prot, int timeout, size_t map_size)
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 8ef226107d78..9942bbea7837 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -534,6 +534,15 @@ int64_t allocate_transhuge(void *ptr, int pagemap_fd)
 		  (uintptr_t)ptr >> (pshift() - 3)) != sizeof(ent))
 		ksft_exit_fail_msg("read pagemap\n");
 
+	/*
+	 * Both entries present means that a THP has been allocated. But if
+	 * their PFNs are masked, we cannot verify the size and alignment of
+	 * the THP
+	 */
+	if (PAGEMAP_PRESENT(ent[0]) && PAGEMAP_PRESENT(ent[1]) &&
+	    !PAGEMAP_PFN(ent[0]) && !PAGEMAP_PFN(ent[1]))
+		return -EACCES;
+
 	if (PAGEMAP_PRESENT(ent[0]) && PAGEMAP_PRESENT(ent[1]) &&
 	    PAGEMAP_PFN(ent[0]) + 1 == PAGEMAP_PFN(ent[1]) &&
 	    !(PAGEMAP_PFN(ent[0]) & ((1 << (hpage_shift - pshift())) - 1)))
-- 
2.53.0


  parent reply	other threads:[~2026-11-12  9:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  9:02 [PATCH 0/4] selftests/mm: replace transhuge-stress with a THP compaction selftest Sarthak Sharma
2026-11-12  9:02 ` [PATCH 1/4] selftests/mm: remove transhuge-stress Sarthak Sharma
2026-11-12  9:02 ` Sarthak Sharma [this message]
2026-11-12  9:02 ` [PATCH 3/4] selftests/mm: factor out THP split helpers Sarthak Sharma
2026-11-12  9:02 ` [PATCH 4/4] selftests/mm: add a THP compaction selftest Sarthak Sharma
2026-11-14 12:43 ` [PATCH 0/4] selftests/mm: replace transhuge-stress with " Dev Jain

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=20261112090250.334344-3-sarthak.sharma@arm.com \
    --to=sarthak.sharma@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=anshuman.khandual@arm.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.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=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --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®