mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrei Vagin <avagin@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Ryan Roberts <ryan.roberts@arm.com>
Cc: linux-kernel@vger.kernel.org, Andrei Vagin <avagin@google.com>
Subject: [PATCH] selftests/mm: don't fail if pagemap_scan isn't supported
Date: Fri, 17 Nov 2023 10:11:27 -0800	[thread overview]
Message-ID: <20231117181127.2574897-1-avagin@google.com> (raw)

This change allows to run tests on old kernels.

Reported-by: Ryan Roberts <ryan.roberts@arm.com>
Closes: https://lore.kernel.org/lkml/696a0a99-eb42-4e13-be14-58a88c9c33f7@arm.com/
Signed-off-by: Andrei Vagin <avagin@google.com>
---
 tools/testing/selftests/mm/vm_util.c | 51 +++++++++++++++++++++-------
 1 file changed, 39 insertions(+), 12 deletions(-)

diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index ec3478b96e4c..4aeb8d5299ff 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -29,15 +29,13 @@ uint64_t pagemap_get_entry(int fd, char *start)
 	return entry;
 }
 
-static uint64_t pagemap_scan_get_categories(int fd, char *start)
+static uint64_t __pagemap_scan_get_categories(int fd, char *start, struct page_region *r)
 {
 	struct pm_scan_arg arg;
-	struct page_region r;
-	long ret;
 
 	arg.start = (uintptr_t)start;
 	arg.end = (uintptr_t)(start + psize());
-	arg.vec = (uintptr_t)&r;
+	arg.vec = (uintptr_t)r;
 	arg.vec_len = 1;
 	arg.flags = 0;
 	arg.size = sizeof(struct pm_scan_arg);
@@ -49,7 +47,15 @@ static uint64_t pagemap_scan_get_categories(int fd, char *start)
 				  PAGE_IS_HUGE | PAGE_IS_SOFT_DIRTY;
 	arg.return_mask = arg.category_anyof_mask;
 
-	ret = ioctl(fd, PAGEMAP_SCAN, &arg);
+	return ioctl(fd, PAGEMAP_SCAN, &arg);
+}
+
+static uint64_t pagemap_scan_get_categories(int fd, char *start)
+{
+	struct page_region r;
+	long ret;
+
+	ret = __pagemap_scan_get_categories(fd, start, &r);
 	if (ret < 0)
 		ksft_exit_fail_msg("PAGEMAP_SCAN failed: %s\n", strerror(errno));
 	if (ret == 0)
@@ -57,18 +63,39 @@ static uint64_t pagemap_scan_get_categories(int fd, char *start)
 	return r.categories;
 }
 
+/* `start` is any valid address. */
+static bool pagemap_scan_supported(int fd, char *start)
+{
+	static int supported = -1;
+	int ret;
+
+	if (supported != -1)
+		return supported;
+
+	/* Provide an invalid address in order to trigger EFAULT. */
+	ret = __pagemap_scan_get_categories(fd, start, (struct page_region *) ~0UL);
+	if (ret == 0)
+		ksft_exit_fail_msg("PAGEMAP_SCAN succedded unexpectedly\n");
+
+	supported = errno == EFAULT;
+
+	return supported;
+}
+
 static bool page_entry_is(int fd, char *start, char *desc,
 			  uint64_t pagemap_flags, uint64_t pagescan_flags)
 {
-	bool m, s;
+	bool m = pagemap_get_entry(fd, start) & pagemap_flags;
 
-	m = pagemap_get_entry(fd, start) & pagemap_flags;
-	s = pagemap_scan_get_categories(fd, start) & pagescan_flags;
-	if (m == s)
-		return m;
+	if (pagemap_scan_supported(fd, start)) {
+		bool s = pagemap_scan_get_categories(fd, start) & pagescan_flags;
 
-	ksft_exit_fail_msg(
-		"read and ioctl return unmatched results for %s: %d %d", desc, m, s);
+		if (m == s)
+			return m;
+
+		ksft_exit_fail_msg(
+			"read and ioctl return unmatched results for %s: %d %d", desc, m, s);
+	}
 	return m;
 }
 
-- 
2.43.0.rc0.421.g78406f8d94-goog


                 reply	other threads:[~2023-11-17 18:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20231117181127.2574897-1-avagin@google.com \
    --to=avagin@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ryan.roberts@arm.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®