Skip to contentSkip to conversation

selftests/mm: skip soft-dirty tests when CONFIG_MEM_SOFT_DIRTY is disabled

PATCH RESEND Superseded Lance Yang 2 participants 3 messages Sep 17, 2025

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Lance Yang

author
#0 lore raw

PATCH RESEND 1/1 selftests/mm: skip soft-dirty tests when CONFIG_MEM_SOFT_DIRTY is disabled

From: Lance Yang <[email protected]>

The madv_populate and soft-dirty kselftests currently fail on systems where CONFIG_MEM_SOFT_DIRTY is disabled.

Introduce a new helper softdirty_is_supported() into vm_util.c/h to ensure tests are properly skipped when the feature is not enabled.

---
 tools/testing/selftests/mm/madv_populate.c | 21 ++--------------
 tools/testing/selftests/mm/soft-dirty.c    |  5 +++-
 tools/testing/selftests/mm/vm_util.c       | 28 ++++++++++++++++++++++
 tools/testing/selftests/mm/vm_util.h       |  1 +
 4 files changed, 35 insertions(+), 20 deletions(-)
tools/testing/selftests/mm/madv_populate.c +2 -19
index b6fabd5c27ed..43dac7783004 100644
@@ -264,23 +264,6 @@ static void test_softdirty(void)
 	munmap(addr, SIZE);
 }
 
-static int system_has_softdirty(void)
-{
-	/*
-	 * There is no way to check if the kernel supports soft-dirty, other
-	 * than by writing to a page and seeing if the bit was set. But the
-	 * tests are intended to check that the bit gets set when it should, so
-	 * doing that check would turn a potentially legitimate fail into a
-	 * skip. Fortunately, we know for sure that arm64 does not support
-	 * soft-dirty. So for now, let's just use the arch as a corse guide.
-	 */
-#if defined(__aarch64__)
-	return 0;
-#else
-	return 1;
-#endif
-}
-
 int main(int argc, char **argv)
 {
 	int nr_tests = 16;
@@ -288,7 +271,7 @@ int main(int argc, char **argv)
 
 	pagesize = getpagesize();
 
-	if (system_has_softdirty())
+	if (softdirty_is_supported())
 		nr_tests += 5;
 
 	ksft_print_header();
@@ -300,7 +283,7 @@ int main(int argc, char **argv)
 	test_holes();
 	test_populate_read();
 	test_populate_write();
-	if (system_has_softdirty())
+	if (softdirty_is_supported())
 		test_softdirty();
 
 	err = ksft_get_fail_cnt();
tools/testing/selftests/mm/soft-dirty.c +4 -1
index 8a3f2b4b2186..98e42d2ac32a 100644
@@ -200,8 +200,11 @@ int main(int argc, char **argv)
 	int pagesize;
 
 	ksft_print_header();
-	ksft_set_plan(15);
 
+	if (!softdirty_is_supported())
+		ksft_exit_skip("soft-dirty is not support\n");
+
+	ksft_set_plan(15);
 	pagemap_fd = open(PAGEMAP_FILE_PATH, O_RDONLY);
 	if (pagemap_fd < 0)
 		ksft_exit_fail_msg("Failed to open %s\n", PAGEMAP_FILE_PATH);
tools/testing/selftests/mm/vm_util.c +28 -0
index 56e9bd541edd..3173335df775 100644
@@ -449,6 +449,34 @@ bool check_vmflag_pfnmap(void *addr)
 	return check_vmflag(addr, "pf");
 }
 
+bool softdirty_is_supported(void)
+{
+	char *addr;
+	int ret = 0;
+	size_t pagesize;
+
+	/* We know for sure that arm64 does not support soft-dirty. */
+#if defined(__aarch64__)
+	return ret;
+#endif
+	pagesize = getpagesize();
+	/*
+	 * __mmap_complete() always sets VM_SOFTDIRTY for new VMAs, so we
+	 * just mmap a small region and check its VmFlags in /proc/self/smaps
+	 * for the "sd" flag.
+	 */
+	addr = mmap(0, pagesize, PROT_READ | PROT_WRITE,
+		    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+	if (!addr)
+		ksft_exit_fail_msg("mmap failed\n");
+
+	if (check_vmflag(addr, "sd"))
+		ret = 1;
+
+	munmap(addr, pagesize);
+	return ret;
+}
+
 /*
  * Open an fd at /proc/$pid/maps and configure procmap_out ready for
  * PROCMAP_QUERY query. Returns 0 on success, or an error code otherwise.
tools/testing/selftests/mm/vm_util.h +1 -0
index 07c4acfd84b6..87ad8e0d92c0 100644
@@ -104,6 +104,7 @@ bool find_vma_procmap(struct procmap_fd *procmap, void *address);
 int close_procmap(struct procmap_fd *procmap);
 int write_sysfs(const char *file_path, unsigned long val);
 int read_sysfs(const char *file_path, unsigned long *val);
+bool softdirty_is_supported(void);
 
 static inline int open_self_procmap(struct procmap_fd *procmap_out)
 {
Signature · 1 line
2.49.0
#1 lore raw
Quoted 7 lines · Lance Yang

From: Lance Yang <[email protected]>

The madv_populate and soft-dirty kselftests currently fail on systems where CONFIG_MEM_SOFT_DIRTY is disabled.

Introduce a new helper softdirty_is_supported() into vm_util.c/h to ensure tests are properly skipped when the feature is not enabled.

I'll note that tools/testing/selftests/mm/config contains

CONFIG_MEM_SOFT_DIRTY=y

But yes, the current arm64 handling is nasty, because some other archs (e.g., riscv) also don't support it yet.

LGTM, some nits below:

Quoted 78 lines
---
  tools/testing/selftests/mm/madv_populate.c | 21 ++--------------
  tools/testing/selftests/mm/soft-dirty.c    |  5 +++-
  tools/testing/selftests/mm/vm_util.c       | 28 ++++++++++++++++++++++
  tools/testing/selftests/mm/vm_util.h       |  1 +
  4 files changed, 35 insertions(+), 20 deletions(-)
tools/testing/selftests/mm/madv_populate.c +2 -19
index b6fabd5c27ed..43dac7783004 100644
@@ -264,23 +264,6 @@ static void test_softdirty(void)
  	munmap(addr, SIZE);
  }
 
-static int system_has_softdirty(void)
-{
-	/*
-	 * There is no way to check if the kernel supports soft-dirty, other
-	 * than by writing to a page and seeing if the bit was set. But the
-	 * tests are intended to check that the bit gets set when it should, so
-	 * doing that check would turn a potentially legitimate fail into a
-	 * skip. Fortunately, we know for sure that arm64 does not support
-	 * soft-dirty. So for now, let's just use the arch as a corse guide.
-	 */
-#if defined(__aarch64__)
-	return 0;
-#else
-	return 1;
-#endif
-}
-
  int main(int argc, char **argv)
  {
  	int nr_tests = 16;
@@ -288,7 +271,7 @@ int main(int argc, char **argv)
 
  	pagesize = getpagesize();
 
-	if (system_has_softdirty())
+	if (softdirty_is_supported())
  		nr_tests += 5;
 
  	ksft_print_header();
@@ -300,7 +283,7 @@ int main(int argc, char **argv)
  	test_holes();
  	test_populate_read();
  	test_populate_write();
-	if (system_has_softdirty())
+	if (softdirty_is_supported())
  		test_softdirty();
 
  	err = ksft_get_fail_cnt();
tools/testing/selftests/mm/soft-dirty.c +4 -1
index 8a3f2b4b2186..98e42d2ac32a 100644
@@ -200,8 +200,11 @@ int main(int argc, char **argv)
  	int pagesize;
 
  	ksft_print_header();
-	ksft_set_plan(15);
 
+	if (!softdirty_is_supported())
+		ksft_exit_skip("soft-dirty is not support\n");
+
+	ksft_set_plan(15);
  	pagemap_fd = open(PAGEMAP_FILE_PATH, O_RDONLY);
  	if (pagemap_fd < 0)
  		ksft_exit_fail_msg("Failed to open %s\n", PAGEMAP_FILE_PATH);
tools/testing/selftests/mm/vm_util.c +0 -0
index 56e9bd541edd..3173335df775 100644

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 452

Quoted 4 lines
tools/testing/selftests/mm/vm_util.c +1 -0
@@ -449,6 +449,34 @@ bool check_vmflag_pfnmap(void *addr)
  	return check_vmflag(addr, "pf");
  }
 
+bool softdirty_is_supported(void)

I'd just call it "softdirty_supported" similar to "pagemap_scan_supported()".

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 455

Quoted 3 lines
tools/testing/selftests/mm/vm_util.c +3 -0
+{
+	char *addr;
+	int ret = 0;

bool supported = false;

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 461

Quoted 6 lines
tools/testing/selftests/mm/vm_util.c +6 -0
+	size_t pagesize;
+
+	/* We know for sure that arm64 does not support soft-dirty. */
+#if defined(__aarch64__)
+	return ret;
+#endif

Just drop this arm special case now.

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 462

Quoted 1 line
tools/testing/selftests/mm/vm_util.c +1 -0
+	pagesize = getpagesize();

const size_t pagesize = getpagesize();

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 467

Quoted 5 lines
tools/testing/selftests/mm/vm_util.c +5 -0
+	/*
+	 * __mmap_complete() always sets VM_SOFTDIRTY for new VMAs, so we
+	 * just mmap a small region and check its VmFlags in /proc/self/smaps
+	 * for the "sd" flag.
+	 */

/* New mappings are expected to be marked with VM_SOFTDIRTY (sd). */

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 474

Quoted 7 lines
tools/testing/selftests/mm/vm_util.c +7 -0
+	addr = mmap(0, pagesize, PROT_READ | PROT_WRITE,
+		    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+	if (!addr)
+		ksft_exit_fail_msg("mmap failed\n");
+
+	if (check_vmflag(addr, "sd"))
+		ret = 1;

supported = true;

Quoted 5 lines
tools/testing/selftests/mm/vm_util.c +5 -0
+
+	munmap(addr, pagesize);
+	return ret;
+}
+
Signature · 3 lines
Cheers

David / dhildenb
author
#2 lore raw
Quoted 6 lines · David Hildenbrand
Quoted 5 lines · Lance Yang

From: Lance Yang <[email protected]>

The madv_populate and soft-dirty kselftests currently fail on systems where CONFIG_MEM_SOFT_DIRTY is disabled.

Quoted 8 lines
Quoted 2 lines

Introduce a new helper softdirty_is_supported() into vm_util.c/h to ensure tests are properly skipped when the feature is not enabled.

I'll note that tools/testing/selftests/mm/config contains

    CONFIG_MEM_SOFT_DIRTY=y

But yes, the current arm64 handling is nasty, because some other archs (e.g., riscv) also don't support it yet.

Yep.

Quoted 2 lines

LGTM, some nits below:

Thanks for taking time to review!

Quoted 70 lines
Quoted 70 lines

---
  tools/testing/selftests/mm/madv_populate.c | 21 ++-------------- tools/testing/selftests/mm/soft-dirty.c    |  5 +++- tools/testing/selftests/mm/vm_util.c       | 28 ++++++++++++++++++++++ tools/testing/selftests/mm/vm_util.h       |  1 +

  4 files changed, 35 insertions(+), 20 deletions(-)
tools/testing/selftests/mm/madv_populate.c +0 -6
index b6fabd5c27ed..43dac7783004 100644
@@ -264,23 +264,6 @@ static void test_softdirty(void)
       munmap(addr, SIZE);
   }
-static int system_has_softdirty(void)
-{
-    /*
-     * There is no way to check if the kernel supports soft-dirty, other
-     * than by writing to a page and seeing if the bit was set. But the
-     * tests are intended to check that the bit gets set when it 

should, so
-     * doing that check would turn a potentially legitimate fail into a -     * skip. Fortunately, we know for sure that arm64 does not support -     * soft-dirty. So for now, let's just use the arch as a corse guide. -     */

tools/testing/selftests/mm/madv_populate.c +0 -7
-#if defined(__aarch64__)
-    return 0;
-#else
-    return 1;
-#endif
-}
-

  int main(int argc, char **argv)
  {
      int nr_tests = 16;

tools/testing/selftests/mm/madv_populate.c +2 -2
@@ -288,7 +271,7 @@ int main(int argc, char **argv)
       pagesize = getpagesize();
-    if (system_has_softdirty())
+    if (softdirty_is_supported())
           nr_tests += 5;
       ksft_print_header();
@@ -300,7 +283,7 @@ int main(int argc, char **argv)
       test_holes();
       test_populate_read();
       test_populate_write();
-    if (system_has_softdirty())
+    if (softdirty_is_supported())
           test_softdirty();
       err = ksft_get_fail_cnt();
tools/testing/selftests/mm/soft-dirty.c +4 -1
index 8a3f2b4b2186..98e42d2ac32a 100644
@@ -200,8 +200,11 @@ int main(int argc, char **argv)
       int pagesize;
       ksft_print_header();
-    ksft_set_plan(15);
+    if (!softdirty_is_supported())
+        ksft_exit_skip("soft-dirty is not support\n");
+
+    ksft_set_plan(15);
       pagemap_fd = open(PAGEMAP_FILE_PATH, O_RDONLY);
       if (pagemap_fd < 0)
           ksft_exit_fail_msg("Failed to open %s\n", PAGEMAP_FILE_PATH);
tools/testing/ selftests/mm/vm_util.c renamed from tools/testing/selftests/mm/vm_util.c +0 -0
index 56e9bd541edd..3173335df775 100644
Quoted 8 lines

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 451

Quoted 6 lines
tools/testing/selftests/mm/vm_util.c +1 -0
@@ -449,6 +449,34 @@ bool check_vmflag_pfnmap(void *addr)
       return check_vmflag(addr, "pf");
   }
+bool softdirty_is_supported(void)

I'd just call it "softdirty_supported" similar to "pagemap_scan_supported()".

Got it.

Quoted 7 lines

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 455

Quoted 4 lines
tools/testing/selftests/mm/vm_util.c +3 -0
+{
+    char *addr;
+    int ret = 0;

bool supported = false;

Quoted 8 lines

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 461

Quoted 6 lines
tools/testing/selftests/mm/vm_util.c +6 -0
+    size_t pagesize;
+
+    /* We know for sure that arm64 does not support soft-dirty. */
+#if defined(__aarch64__)
+    return ret;
+#endif

Just drop this arm special case now.

OK

Quoted 5 lines

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 462

Quoted 2 lines
tools/testing/selftests/mm/vm_util.c +1 -0
+    pagesize = getpagesize();

const size_t pagesize = getpagesize();

Quoted 8 lines
Quoted 6 lines
tools/testing/selftests/mm/vm_util.c +3 -0
+    /*
+     * __mmap_complete() always sets VM_SOFTDIRTY for new VMAs, so we
+     * just mmap a small region and check its VmFlags in /proc/self/ 

smaps
+     * for the "sd" flag.
+     */

/* New mappings are expected to be marked with VM_SOFTDIRTY (sd). */

Cool. Much better!

Quoted 10 lines
Quoted 8 lines

+    addr = mmap(0, pagesize, PROT_READ | PROT_WRITE, +            MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); +    if (!addr)
+        ksft_exit_fail_msg("mmap failed\n"); +
+    if (check_vmflag(addr, "sd"))
+        ret = 1;

supported = true;

I'll adjust as you suggested ;)

Quoted 7 lines

tools/testing/selftests/mm/vm_util.c bool check_vmflag_pfnmap(void *addr) · line 479

Quoted 6 lines
tools/testing/selftests/mm/vm_util.c +5 -0
+
+    munmap(addr, pagesize);
+    return ret;
+}
+

Cheers,
Lance