Skip to contentSkip to conversation

selftests/mm: Handle implicit read permissions in madv_populate

PATCH Superseded Xionggui Chen 4 participants 15 messages Nov 12 – Nov 18

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/mm: Handle implicit read permissions in madv_populate

From: Xionggui Chen <[email protected]>

The madv_populate selftest assumes that a mapping created with PROT_WRITE cannot be read, and expects MADV_POPULATE_READ to fail with EINVAL.

However, some architectures, including RISC-V, may implicitly grant read permission to PROT_WRITE mappings. On such architectures, MADV_POPULATE_READ succeeds, causing the test to fail incorrectly.

Check the actual VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

---
 tools/testing/selftests/mm/madv_populate.c | 67 ++++++++++++++++++++--
 1 file changed, 63 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/madv_populate.c +63 -4
index 7fce5d0b622b..818f99ee2004 100644
@@ -69,10 +69,38 @@ static void test_prot_read(void)
 	munmap(addr, SIZE);
 }
 
+static int mapping_is_readable(void *addr)
+{
+	FILE *maps;
+	char line[256];
+	unsigned long start, end;
+	char perms[5];
+	int readable = -1;
+
+	maps = fopen("/proc/self/maps", "r");
+	if (!maps)
+		return -1;
+
+	while (fgets(line, sizeof(line), maps)) {
+		if (sscanf(line, "%lx-%lx %4s",
+			   &start, &end, perms) != 3)
+			continue;
+
+		if (start <= (unsigned long)addr &&
+		    (unsigned long)addr < end) {
+			readable = perms[0] == 'r';
+			break;
+		}
+	}
+
+	fclose(maps);
+	return readable;
+}
+
 static void test_prot_write(void)
 {
 	char *addr;
-	int ret;
+	int ret, readable;
 
 	ksft_print_msg("[RUN] %s\n", __func__);
 
@@ -80,9 +108,19 @@ static void test_prot_write(void)
 	if (addr == MAP_FAILED)
 		ksft_exit_fail_msg("mmap failed\n");
 
+	readable = mapping_is_readable(addr);
+	if (readable < 0)
+		ksft_exit_fail_msg("Cannot determine VMA permissions\n");
+
+	errno = 0;
 	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
-	ksft_test_result(ret == -1 && errno == EINVAL,
-			 "MADV_POPULATE_READ with PROT_WRITE\n");
+
+	if (readable)
+		ksft_test_result(ret == 0,
+				 "MADV_POPULATE_READ with readable PROT_WRITE mapping\n");
+	else
+		ksft_test_result(ret == -1 && errno == EINVAL,
+				 "MADV_POPULATE_READ with write-only mapping\n");
 
 	ret = madvise(addr, SIZE, MADV_POPULATE_WRITE);
 	ksft_test_result(!ret, "MADV_POPULATE_WRITE with PROT_WRITE\n");
@@ -90,6 +128,26 @@ static void test_prot_write(void)
 	munmap(addr, SIZE);
 }
 
+static void test_prot_none(void)
+{
+	char *addr;
+	int ret;
+
+	ksft_print_msg("[RUN] %s\n", __func__);
+
+	addr = mmap(0, SIZE, PROT_NONE,
+		    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+	if (addr == MAP_FAILED)
+		ksft_exit_fail_msg("mmap failed\n");
+
+	errno = 0;
+	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
+	ksft_test_result(ret == -1 && errno == EINVAL,
+			 "MADV_POPULATE_READ with PROT_NONE\n");
+
+	munmap(addr, SIZE);
+}
+
 static void test_holes(void)
 {
 	char *addr;
@@ -266,7 +324,7 @@ static void test_softdirty(void)
 
 int main(int argc, char **argv)
 {
-	int nr_tests = 16;
+	int nr_tests = 17;
 	int err;
 
 	pagesize = getpagesize();
@@ -280,6 +338,7 @@ int main(int argc, char **argv)
 	sense_support();
 	test_prot_read();
 	test_prot_write();
+	test_prot_none();
 	test_holes();
 	test_populate_read();
 	test_populate_write();
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 8 lines · On Thu, 12 Nov 2026 03:18:11 +0000 Xionggui Chen <[email protected]> wrote:

The madv_populate selftest assumes that a mapping created with PROT_WRITE cannot be read, and expects MADV_POPULATE_READ to fail with EINVAL.

However, some architectures, including RISC-V, may implicitly grant read permission to PROT_WRITE mappings. On such architectures, MADV_POPULATE_READ succeeds, causing the test to fail incorrectly.

Quoted 8 lines

Check the actual VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

Code looks good, thanks.

The changelog is a little imprecise.

"some architectures" is too broad - this is purely riscv.

The wording suggests that a readable hardware PTE alone determines the result. riscv can have readable writable PTEs without setting VM_READ, and this test still expects EINVAL on those systems.

I altered the second paragraph, please review.

From: Xionggui Chen <[email protected]>
Subject: selftests/mm: handle implicit read permissions in madv_populate
Date: Thu, 12 Nov 2026 03:18:11 +0000

The madv_populate selftest assumes that mmap(PROT_WRITE) creates a VMA without VM_READ and expects MADV_POPULATE_READ to fail with EINVAL.

On RISC-V, commit 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") makes the kernel set VM_READ for mappings requested with PROT_WRITE. MADV_POPULATE_READ checks this VMA permission, so it succeeds and the test fails incorrectly. Whether the hardware can read a writable PTE does not, by itself, determine the madvise() result.

Check the VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

---

 tools/testing/selftests/mm/madv_populate.c |   67 +++++++++++++++++--
 1 file changed, 63 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/madv_populate.c renamed from tools/testing/selftests/mm/madv_populate.c~selftests-mm-handle-implicit-read-permissions-in-madv_populate +63 -4
@@ -69,10 +69,38 @@ static void test_prot_read(void)
 	munmap(addr, SIZE);
 }
 
+static int mapping_is_readable(void *addr)
+{
+	FILE *maps;
+	char line[256];
+	unsigned long start, end;
+	char perms[5];
+	int readable = -1;
+
+	maps = fopen("/proc/self/maps", "r");
+	if (!maps)
+		return -1;
+
+	while (fgets(line, sizeof(line), maps)) {
+		if (sscanf(line, "%lx-%lx %4s",
+			   &start, &end, perms) != 3)
+			continue;
+
+		if (start <= (unsigned long)addr &&
+		    (unsigned long)addr < end) {
+			readable = perms[0] == 'r';
+			break;
+		}
+	}
+
+	fclose(maps);
+	return readable;
+}
+
 static void test_prot_write(void)
 {
 	char *addr;
-	int ret;
+	int ret, readable;
 
 	ksft_print_msg("[RUN] %s\n", __func__);
 
@@ -80,9 +108,19 @@ static void test_prot_write(void)
 	if (addr == MAP_FAILED)
 		ksft_exit_fail_msg("mmap failed\n");
 
+	readable = mapping_is_readable(addr);
+	if (readable < 0)
+		ksft_exit_fail_msg("Cannot determine VMA permissions\n");
+
+	errno = 0;
 	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
-	ksft_test_result(ret == -1 && errno == EINVAL,
-			 "MADV_POPULATE_READ with PROT_WRITE\n");
+
+	if (readable)
+		ksft_test_result(ret == 0,
+				 "MADV_POPULATE_READ with readable PROT_WRITE mapping\n");
+	else
+		ksft_test_result(ret == -1 && errno == EINVAL,
+				 "MADV_POPULATE_READ with write-only mapping\n");
 
 	ret = madvise(addr, SIZE, MADV_POPULATE_WRITE);
 	ksft_test_result(!ret, "MADV_POPULATE_WRITE with PROT_WRITE\n");
@@ -90,6 +128,26 @@ static void test_prot_write(void)
 	munmap(addr, SIZE);
 }
 
+static void test_prot_none(void)
+{
+	char *addr;
+	int ret;
+
+	ksft_print_msg("[RUN] %s\n", __func__);
+
+	addr = mmap(0, SIZE, PROT_NONE,
+		    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+	if (addr == MAP_FAILED)
+		ksft_exit_fail_msg("mmap failed\n");
+
+	errno = 0;
+	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
+	ksft_test_result(ret == -1 && errno == EINVAL,
+			 "MADV_POPULATE_READ with PROT_NONE\n");
+
+	munmap(addr, SIZE);
+}
+
 static void test_holes(void)
 {
 	char *addr;
@@ -266,7 +324,7 @@ static void test_softdirty(void)
 
 int main(int argc, char **argv)
 {
-	int nr_tests = 16;
+	int nr_tests = 17;
 	int err;
 
 	pagesize = getpagesize();
@@ -280,6 +338,7 @@ int main(int argc, char **argv)
 	sense_support();
 	test_prot_read();
 	test_prot_write();
+	test_prot_none();
 	test_holes();
 	test_populate_read();
 	test_populate_write();

_

author
#2 lore raw

Thanks for the review!

You're right. The changelog is too broad, and the distinction between RISC-V PTE permissions and VMA permissions needs to be made clearer.

I'll revisit the wording to describe the RISC-V-specific behavior more precisely, without implying that readable PTE permissions necessarily mean VM_READ is set.

Thanks,
Xionggui

Quoted 175 lines · Andrew Morton
Quoted 8 lines · On Thu, 12 Nov 2026 03:18:11 +0000 Xionggui Chen <[email protected]> wrote:

The madv_populate selftest assumes that a mapping created with PROT_WRITE cannot be read, and expects MADV_POPULATE_READ to fail with EINVAL.

However, some architectures, including RISC-V, may implicitly grant read permission to PROT_WRITE mappings. On such architectures, MADV_POPULATE_READ succeeds, causing the test to fail incorrectly.

Quoted 8 lines

Check the actual VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

Code looks good, thanks.

The changelog is a little imprecise.

"some architectures" is too broad - this is purely riscv.

The wording suggests that a readable hardware PTE alone determines the result. riscv can have readable writable PTEs without setting VM_READ, and this test still expects EINVAL on those systems.

I altered the second paragraph, please review.

From: Xionggui Chen <[email protected]>
Subject: selftests/mm: handle implicit read permissions in madv_populate
Date: Thu, 12 Nov 2026 03:18:11 +0000

The madv_populate selftest assumes that mmap(PROT_WRITE) creates a VMA without VM_READ and expects MADV_POPULATE_READ to fail with EINVAL.

On RISC-V, commit 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") makes the kernel set VM_READ for mappings requested with PROT_WRITE. MADV_POPULATE_READ checks this VMA permission, so it succeeds and the test fails incorrectly. Whether the hardware can read a writable PTE does not, by itself, determine the madvise() result.

Check the VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

---

tools/testing/selftests/mm/madv_populate.c | 67 +++++++++++++++++--

1 file changed, 63 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/madv_populate.c renamed from tools/testing/selftests/mm/madv_populate.c~selftests-mm-handle-implicit-read-permissions-in-madv_populate +63 -4
@@ -69,10 +69,38 @@ static void test_prot_read(void)
 	munmap(addr, SIZE);
 }
 
+static int mapping_is_readable(void *addr)
+{
+	FILE *maps;
+	char line[256];
+	unsigned long start, end;
+	char perms[5];
+	int readable = -1;
+
+	maps = fopen("/proc/self/maps", "r");
+	if (!maps)
+		return -1;
+
+	while (fgets(line, sizeof(line), maps)) {
+		if (sscanf(line, "%lx-%lx %4s",
+			   &start, &end, perms) != 3)
+			continue;
+
+		if (start <= (unsigned long)addr &&
+		    (unsigned long)addr < end) {
+			readable = perms[0] == 'r';
+			break;
+		}
+	}
+
+	fclose(maps);
+	return readable;
+}
+
 static void test_prot_write(void)
 {
 	char *addr;
-	int ret;
+	int ret, readable;
 
 	ksft_print_msg("[RUN] %s\n", __func__);
 
@@ -80,9 +108,19 @@ static void test_prot_write(void)
 	if (addr == MAP_FAILED)
 		ksft_exit_fail_msg("mmap failed\n");
 
+	readable = mapping_is_readable(addr);
+	if (readable < 0)
+		ksft_exit_fail_msg("Cannot determine VMA permissions\n");
+
+	errno = 0;
 	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
-	ksft_test_result(ret == -1 && errno == EINVAL,
-			 "MADV_POPULATE_READ with PROT_WRITE\n");
+
+	if (readable)
+		ksft_test_result(ret == 0,
+				 "MADV_POPULATE_READ with readable PROT_WRITE mapping\n");
+	else
+		ksft_test_result(ret == -1 && errno == EINVAL,
+				 "MADV_POPULATE_READ with write-only mapping\n");
 
 	ret = madvise(addr, SIZE, MADV_POPULATE_WRITE);
 	ksft_test_result(!ret, "MADV_POPULATE_WRITE with PROT_WRITE\n");
@@ -90,6 +128,26 @@ static void test_prot_write(void)
 	munmap(addr, SIZE);
 }
 
+static void test_prot_none(void)
+{
+	char *addr;
+	int ret;
+
+	ksft_print_msg("[RUN] %s\n", __func__);
+
+	addr = mmap(0, SIZE, PROT_NONE,
+		    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+	if (addr == MAP_FAILED)
+		ksft_exit_fail_msg("mmap failed\n");
+
+	errno = 0;
+	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
+	ksft_test_result(ret == -1 && errno == EINVAL,
+			 "MADV_POPULATE_READ with PROT_NONE\n");
+
+	munmap(addr, SIZE);
+}
+
 static void test_holes(void)
 {
 	char *addr;
@@ -266,7 +324,7 @@ static void test_softdirty(void)
 
 int main(int argc, char **argv)
 {
-	int nr_tests = 16;
+	int nr_tests = 17;
 	int err;
 
 	pagesize = getpagesize();
@@ -280,6 +338,7 @@ int main(int argc, char **argv)
 	sense_support();
 	test_prot_read();
 	test_prot_write();
+	test_prot_none();
 	test_holes();
 	test_populate_read();
 	test_populate_write();

_

author
#3 lore raw
In reply to Andrew Morton

Thanks for the review and for improving the changelog.

The revised description is accurate. It correctly explains that the behavior is specific to RISC-V and that MADV_POPULATE_READ depends on the VMA permissions rather than the hardware PTE permissions alone.

The updated changelog looks good to me.

Thanks,
Xionggui

Quoted 174 lines · Andrew Morton
Quoted 8 lines · On Thu, 12 Nov 2026 03:18:11 +0000 Xionggui Chen <[email protected]> wrote:

The madv_populate selftest assumes that a mapping created with PROT_WRITE cannot be read, and expects MADV_POPULATE_READ to fail with EINVAL.

However, some architectures, including RISC-V, may implicitly grant read permission to PROT_WRITE mappings. On such architectures, MADV_POPULATE_READ succeeds, causing the test to fail incorrectly.

Quoted 8 lines

Check the actual VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

Code looks good, thanks.

The changelog is a little imprecise.

"some architectures" is too broad - this is purely riscv.

The wording suggests that a readable hardware PTE alone determines the result. riscv can have readable writable PTEs without setting VM_READ, and this test still expects EINVAL on those systems.

I altered the second paragraph, please review.

From: Xionggui Chen <[email protected]>
Subject: selftests/mm: handle implicit read permissions in madv_populate
Date: Thu, 12 Nov 2026 03:18:11 +0000

The madv_populate selftest assumes that mmap(PROT_WRITE) creates a VMA without VM_READ and expects MADV_POPULATE_READ to fail with EINVAL.

On RISC-V, commit 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") makes the kernel set VM_READ for mappings requested with PROT_WRITE. MADV_POPULATE_READ checks this VMA permission, so it succeeds and the test fails incorrectly. Whether the hardware can read a writable PTE does not, by itself, determine the madvise() result.

Check the VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

---

  tools/testing/selftests/mm/madv_populate.c |   67 +++++++++++++++++--
  1 file changed, 63 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/madv_populate.c renamed from tools/testing/selftests/mm/madv_populate.c~selftests-mm-handle-implicit-read-permissions-in-madv_populate +63 -4
@@ -69,10 +69,38 @@ static void test_prot_read(void)
  	munmap(addr, SIZE);
  }
 
+static int mapping_is_readable(void *addr)
+{
+	FILE *maps;
+	char line[256];
+	unsigned long start, end;
+	char perms[5];
+	int readable = -1;
+
+	maps = fopen("/proc/self/maps", "r");
+	if (!maps)
+		return -1;
+
+	while (fgets(line, sizeof(line), maps)) {
+		if (sscanf(line, "%lx-%lx %4s",
+			   &start, &end, perms) != 3)
+			continue;
+
+		if (start <= (unsigned long)addr &&
+		    (unsigned long)addr < end) {
+			readable = perms[0] == 'r';
+			break;
+		}
+	}
+
+	fclose(maps);
+	return readable;
+}
+
  static void test_prot_write(void)
  {
  	char *addr;
-	int ret;
+	int ret, readable;
 
  	ksft_print_msg("[RUN] %s\n", __func__);
 
@@ -80,9 +108,19 @@ static void test_prot_write(void)
  	if (addr == MAP_FAILED)
  		ksft_exit_fail_msg("mmap failed\n");
 
+	readable = mapping_is_readable(addr);
+	if (readable < 0)
+		ksft_exit_fail_msg("Cannot determine VMA permissions\n");
+
+	errno = 0;
  	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
-	ksft_test_result(ret == -1 && errno == EINVAL,
-			 "MADV_POPULATE_READ with PROT_WRITE\n");
+
+	if (readable)
+		ksft_test_result(ret == 0,
+				 "MADV_POPULATE_READ with readable PROT_WRITE mapping\n");
+	else
+		ksft_test_result(ret == -1 && errno == EINVAL,
+				 "MADV_POPULATE_READ with write-only mapping\n");
 
  	ret = madvise(addr, SIZE, MADV_POPULATE_WRITE);
  	ksft_test_result(!ret, "MADV_POPULATE_WRITE with PROT_WRITE\n");
@@ -90,6 +128,26 @@ static void test_prot_write(void)
  	munmap(addr, SIZE);
  }
 
+static void test_prot_none(void)
+{
+	char *addr;
+	int ret;
+
+	ksft_print_msg("[RUN] %s\n", __func__);
+
+	addr = mmap(0, SIZE, PROT_NONE,
+		    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+	if (addr == MAP_FAILED)
+		ksft_exit_fail_msg("mmap failed\n");
+
+	errno = 0;
+	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
+	ksft_test_result(ret == -1 && errno == EINVAL,
+			 "MADV_POPULATE_READ with PROT_NONE\n");
+
+	munmap(addr, SIZE);
+}
+
  static void test_holes(void)
  {
  	char *addr;
@@ -266,7 +324,7 @@ static void test_softdirty(void)
 
  int main(int argc, char **argv)
  {
-	int nr_tests = 16;
+	int nr_tests = 17;
  	int err;
 
  	pagesize = getpagesize();
@@ -280,6 +338,7 @@ int main(int argc, char **argv)
  	sense_support();
  	test_prot_read();
  	test_prot_write();
+	test_prot_none();
  	test_holes();
  	test_populate_read();
  	test_populate_write();

_

#4 lore raw
In reply to Xionggui Chen
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] selftests/mm: custom /proc/self/maps parser in mapping_is_readable() - [Low] selftests/mm: missing…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] selftests/mm: custom /proc/self/maps parser in mapping_is_readable()
- [Low] selftests/mm: missing Fixes trailer for test_prot_write() failure
--

commit 3592bfa7e9663819c643afaf7d6c337f6b102d15
Author: Xionggui Chen <[email protected]>

Quoted 13 lines

selftests/mm: Handle implicit read permissions in madv_populate

The madv_populate selftest assumes that a mapping created with PROT_WRITE cannot be read, and expects MADV_POPULATE_READ to fail with EINVAL.

However, some architectures, including RISC-V, may implicitly grant read permission to PROT_WRITE mappings. On such architectures, MADV_POPULATE_READ succeeds, causing the test to fail incorrectly.

Check the actual VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Quoted 7 lines

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

[Severity: Low]
Should this commit include a Fixes: tag for the test it fixes?

Fixes e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)")
Quoted 25 lines
tools/testing/selftests/mm/madv_populate.c +17 -0
index 7fce5d0b622be..818f99ee20041 100644
@@ -69,10 +69,38 @@ static void test_prot_read(void)
 	munmap(addr, SIZE);
 }
 
+static int mapping_is_readable(void *addr)
+{
+	FILE *maps;
+	char line[256];
+	unsigned long start, end;
+	char perms[5];
+	int readable = -1;
+
+	maps = fopen("/proc/self/maps", "r");
+	if (!maps)
+		return -1;
+
+	while (fgets(line, sizeof(line), maps)) {
+		if (sscanf(line, "%lx-%lx %4s",
+			   &start, &end, perms) != 3)
+			continue;
+

tools/testing/selftests/mm/madv_populate.c static void test_prot_read(void) · line 98

Quoted 10 lines
tools/testing/selftests/mm/madv_populate.c +10 -0
+		if (start <= (unsigned long)addr &&
+		    (unsigned long)addr < end) {
+			readable = perms[0] == 'r';
+			break;
+		}
+	}
+
+	fclose(maps);
+	return readable;
+}

[Severity: Low]
Could mapping_is_readable() reuse the shared VMA helpers in vm_util.h instead of open-coding a /proc/self/maps parser?

madv_populate.c already includes vm_util.h and links against vm_util.c, which provide open_self_procmap(), find_vma_procmap(), and close_procmap() (exposing PROCMAP_QUERY_VMA_READABLE in procmap.query.vma_flags) as well as check_vmflag(addr, "rd"). Reusing those avoids duplicating VMA lookup logic and avoids line truncation on mappings with paths longer than 256 bytes.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#5 lore raw

Hi Andrew,

Thanks for improving the changelog and picking up the patch.

I noticed that the original madv_populate test was introduced by commit e5bfac53e310. Would it make sense to add the following Fixes tag?

Fixes e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)")

I also tested an alternative implementation using the existing procmap helpers, as suggested by Sashiko. It passes all 17 tests on RISC-V with Linux 7.2.0.

However, since PROCMAP_QUERY requires a relatively recent kernel, keeping the current /proc/self/maps implementation may be preferable for compatibility with older kernels.

Thanks,
Xionggui

Quoted 81 lines · On 11/13/26 11:19, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] selftests/mm: custom /proc/self/maps parser in mapping_is_readable()
- [Low] selftests/mm: missing Fixes trailer for test_prot_write() failure
--

commit 3592bfa7e9663819c643afaf7d6c337f6b102d15
Author: Xionggui Chen <[email protected]>

Quoted 13 lines

selftests/mm: Handle implicit read permissions in madv_populate

The madv_populate selftest assumes that a mapping created with PROT_WRITE cannot be read, and expects MADV_POPULATE_READ to fail with EINVAL.

However, some architectures, including RISC-V, may implicitly grant read permission to PROT_WRITE mappings. On such architectures, MADV_POPULATE_READ succeeds, causing the test to fail incorrectly.

Check the actual VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Quoted 7 lines

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

[Severity: Low]
Should this commit include a Fixes: tag for the test it fixes?

Fixes e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)")
Quoted 25 lines
tools/testing/selftests/mm/madv_populate.c +17 -0
index 7fce5d0b622be..818f99ee20041 100644
@@ -69,10 +69,38 @@ static void test_prot_read(void)
  	munmap(addr, SIZE);
  }
 
+static int mapping_is_readable(void *addr)
+{
+	FILE *maps;
+	char line[256];
+	unsigned long start, end;
+	char perms[5];
+	int readable = -1;
+
+	maps = fopen("/proc/self/maps", "r");
+	if (!maps)
+		return -1;
+
+	while (fgets(line, sizeof(line), maps)) {
+		if (sscanf(line, "%lx-%lx %4s",
+			   &start, &end, perms) != 3)
+			continue;
+

tools/testing/selftests/mm/madv_populate.c static void test_prot_read(void) · line 98

Quoted 10 lines
tools/testing/selftests/mm/madv_populate.c +10 -0
+		if (start <= (unsigned long)addr &&
+		    (unsigned long)addr < end) {
+			readable = perms[0] == 'r';
+			break;
+		}
+	}
+
+	fclose(maps);
+	return readable;
+}

[Severity: Low]
Could mapping_is_readable() reuse the shared VMA helpers in vm_util.h instead of open-coding a /proc/self/maps parser?

madv_populate.c already includes vm_util.h and links against vm_util.c, which provide open_self_procmap(), find_vma_procmap(), and close_procmap() (exposing PROCMAP_QUERY_VMA_READABLE in procmap.query.vma_flags) as well as check_vmflag(addr, "rd"). Reusing those avoids duplicating VMA lookup logic and avoids line truncation on mappings with paths longer than 256 bytes.

#6 lore raw
In reply to Xionggui Chen
Quoted 10 lines · Xionggui Chen

From: Xionggui Chen <[email protected]>

The madv_populate selftest assumes that a mapping created with PROT_WRITE cannot be read, and expects MADV_POPULATE_READ to fail with EINVAL.

However, some architectures, including RISC-V, may implicitly grant read permission to PROT_WRITE mappings. On such architectures, MADV_POPULATE_READ succeeds, causing the test to fail incorrectly.

Quoted 8 lines

Check the actual VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

I would have said, if RISCV is the odd one, special case it and call it a day?

That would keep the changes to test_prot_write() to a minimum.

Signature · 3 lines
Cheers,

David
author
#7 lore raw

Thanks for the suggestion.

Agreed. Since this behavior is specific to RISC-V, a simple architecture-specific check is sufficient.

I've simplified the patch to use |#ifdef __riscv|in |test_prot_write()|, removing the |/proc/self/maps|parsing and the additional PROT_NONE test.

The updated test passes all 16 tests on RISC-V with Linux 7.2.0.

Since the original patch is already in Andrew's mm tree, would you prefer a v2 or an incremental fix?

Thanks,
Xionggui

Quoted 24 lines · David Hildenbrand (Arm)
Quoted 10 lines · Xionggui Chen

From: Xionggui Chen <[email protected]>

The madv_populate selftest assumes that a mapping created with PROT_WRITE cannot be read, and expects MADV_POPULATE_READ to fail with EINVAL.

However, some architectures, including RISC-V, may implicitly grant read permission to PROT_WRITE mappings. On such architectures, MADV_POPULATE_READ succeeds, causing the test to fail incorrectly.

Quoted 8 lines

Check the actual VMA permissions through /proc/self/maps and adjust the expected result accordingly.

Add a PROT_NONE test to verify that MADV_POPULATE_READ still fails with EINVAL for mappings without read permission.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 17 tests.

I would have said, if RISCV is the odd one, special case it and call it a day?

That would keep the changes to test_prot_write() to a minimum.

maintainer
#8 lore raw
Quoted 5 lines · On Mon, 16 Nov 2026 15:00:56 +0800 cxg <[email protected]> wrote:

Thanks for the suggestion.

Agreed. Since this behavior is specific to RISC-V, a simple architecture-specific check is sufficient.

Quoted 7 lines

I've simplified the patch to use |#ifdef __riscv|in |test_prot_write()|, removing the |/proc/self/maps|parsing and the additional PROT_NONE test.

The updated test passes all 16 tests on RISC-V with Linux 7.2.0.

Since the original patch is already in Andrew's mm tree, would you prefer a v2 or an incremental fix?

A v2, please.

#9 lore raw

PATCH v2 1/1 selftests/mm: handle implicit read permissions in madv_populate

In reply to Xionggui Chen

From: Xionggui Chen <[email protected]>

The madv_populate selftest assumes that mmap(PROT_WRITE) creates a VMA without VM_READ and expects MADV_POPULATE_READ to fail with EINVAL.

On RISC-V, commit 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") makes the kernel set VM_READ for mappings requested with PROT_WRITE. MADV_POPULATE_READ checks this VMA permission, so it succeeds and the test fails incorrectly.

Adjust the expected result for RISC-V using an architecture-specific conditional, while preserving the existing behavior on other architectures.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 16 tests.

Fixes e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)")

---
Changes in v2:
- Use an architecture-specific conditional for RISC-V.
- Remove /proc/self/maps parsing.
- Remove the additional PROT_NONE test.

 tools/testing/selftests/mm/madv_populate.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/mm/madv_populate.c +5 -0
index 7fce5d0b622b..fa26ca2d8cf6 100644
@@ -81,8 +81,13 @@ static void test_prot_write(void)
 		ksft_exit_fail_msg("mmap failed\n");
 
 	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
+#ifdef __riscv
+	ksft_test_result(ret == 0,
+			 "MADV_POPULATE_READ with PROT_WRITE\n");
+#else
 	ksft_test_result(ret == -1 && errno == EINVAL,
 			 "MADV_POPULATE_READ with PROT_WRITE\n");
+#endif
 
 	ret = madvise(addr, SIZE, MADV_POPULATE_WRITE);
 	ksft_test_result(!ret, "MADV_POPULATE_WRITE with PROT_WRITE\n");
maintainer Acked-by on v2 1/1
#10 lore raw

Re: [PATCH v2] selftests/mm: handle implicit read permissions in madv_populate

Quoted 10 lines · Xionggui Chen

From: Xionggui Chen <[email protected]>

The madv_populate selftest assumes that mmap(PROT_WRITE) creates a VMA without VM_READ and expects MADV_POPULATE_READ to fail with EINVAL.

On RISC-V, commit 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") makes the kernel set VM_READ for mappings requested with PROT_WRITE. MADV_POPULATE_READ checks this VMA permission, so it succeeds and the test fails incorrectly.

Quoted 6 lines

Adjust the expected result for RISC-V using an architecture-specific conditional, while preserving the existing behavior on other architectures.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 16 tests.

Thanks, please don't send new revisions in reply to previous revisions next time.

Quoted 22 lines
Fixes e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)")

---
Changes in v2:
- Use an architecture-specific conditional for RISC-V.
- Remove /proc/self/maps parsing.
- Remove the additional PROT_NONE test.

 tools/testing/selftests/mm/madv_populate.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/mm/madv_populate.c +0 -0
index 7fce5d0b622b..fa26ca2d8cf6 100644
@@ -81,8 +81,13 @@ static void test_prot_write(void)
 		ksft_exit_fail_msg("mmap failed\n");
 
 	ret = madvise(addr, SIZE, MADV_POPULATE_READ);

tools/testing/selftests/mm/madv_populate.c static void test_prot_write(void) · line 93

Quoted 10 lines
tools/testing/selftests/mm/madv_populate.c +5 -0
+#ifdef __riscv
+	ksft_test_result(ret == 0,
+			 "MADV_POPULATE_READ with PROT_WRITE\n");
+#else
 	ksft_test_result(ret == -1 && errno == EINVAL,
 			 "MADV_POPULATE_READ with PROT_WRITE\n");
+#endif
 
 	ret = madvise(addr, SIZE, MADV_POPULATE_WRITE);
 	ksft_test_result(!ret, "MADV_POPULATE_WRITE with PROT_WRITE\n");

No that's a simple fix :)

Signature · 3 lines
Cheers,

David
author
#11 lore raw

Re: [PATCH v2] selftests/mm: handle implicit read permissions in madv_populate

Thanks, David, for the Acked-by.

I'll send future patch revisions as separate threads.

Best regards,
Xionggui

Quoted 54 lines · David Hildenbrand (Arm)
Quoted 10 lines · Xionggui Chen

From: Xionggui Chen <[email protected]>

The madv_populate selftest assumes that mmap(PROT_WRITE) creates a VMA without VM_READ and expects MADV_POPULATE_READ to fail with EINVAL.

On RISC-V, commit 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") makes the kernel set VM_READ for mappings requested with PROT_WRITE. MADV_POPULATE_READ checks this VMA permission, so it succeeds and the test fails incorrectly.

Quoted 6 lines

Adjust the expected result for RISC-V using an architecture-specific conditional, while preserving the existing behavior on other architectures.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 16 tests.

Thanks, please don't send new revisions in reply to previous revisions next time.

Quoted 21 lines
Fixes e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)")

---
Changes in v2:
- Use an architecture-specific conditional for RISC-V.
- Remove /proc/self/maps parsing.
- Remove the additional PROT_NONE test.

  tools/testing/selftests/mm/madv_populate.c | 5 +++++
  1 file changed, 5 insertions(+)
tools/testing/selftests/mm/madv_populate.c +0 -0
index 7fce5d0b622b..fa26ca2d8cf6 100644
@@ -81,8 +81,13 @@ static void test_prot_write(void)
  		ksft_exit_fail_msg("mmap failed\n");
 
  	ret = madvise(addr, SIZE, MADV_POPULATE_READ);

tools/testing/selftests/mm/madv_populate.c static void test_prot_write(void) · line 93

Quoted 10 lines
tools/testing/selftests/mm/madv_populate.c +5 -0
+#ifdef __riscv
+	ksft_test_result(ret == 0,
+			 "MADV_POPULATE_READ with PROT_WRITE\n");
+#else
  	ksft_test_result(ret == -1 && errno == EINVAL,
  			 "MADV_POPULATE_READ with PROT_WRITE\n");
+#endif
 
  	ret = madvise(addr, SIZE, MADV_POPULATE_WRITE);
  	ksft_test_result(!ret, "MADV_POPULATE_WRITE with PROT_WRITE\n");

No that's a simple fix :)

#12 lore raw

Re: [PATCH v2] selftests/mm: handle implicit read permissions in madv_populate

In reply to Xionggui Chen
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/mm: wrong Fixes trailer in madv_populate test_prot_write() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/mm: wrong Fixes trailer in madv_populate test_prot_write()
--

commit a72c4c7dc3b5c0ebca1f3529ca19931b9c980959
Author: Xionggui Chen <[email protected]>

Quoted 14 lines

selftests/mm: handle implicit read permissions in madv_populate

The madv_populate selftest assumes that mmap(PROT_WRITE) creates a VMA without VM_READ and expects MADV_POPULATE_READ to fail with EINVAL.

On RISC-V, commit 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") makes the kernel set VM_READ for mappings requested with PROT_WRITE. MADV_POPULATE_READ checks this VMA permission, so it succeeds and the test fails incorrectly.

Adjust the expected result for RISC-V using an architecture-specific conditional, while preserving the existing behavior on other architectures.

Quoted 6 lines

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 16 tests.

Fixes e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)")

[Severity: Low]
Should the Fixes: tag point to 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") instead?

When e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)") added test_prot_write(), mmap(PROT_WRITE) on RISC-V did not set VM_READ and MADV_POPULATE_READ returned -EINVAL as expected. Backporting this change to stable trees older than 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") will cause test_prot_write() to fail on RISC-V.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#13 lore raw

Re: [PATCH v2] selftests/mm: handle implicit read permissions in madv_populate

Hi Andrew,

I noticed that the Fixes tag in the patch currently in mm-new points to the original selftest commit.

Since the test failure was introduced by the RISC-V VMA permission change, the Fixes tag should instead be:

Fixes 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE")

Could you please update the Fixes tag in mm-new?

The code itself does not need any changes.

Thanks,
Xionggui

Quoted 39 lines · On 11/18/26 15:07, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/mm: wrong Fixes trailer in madv_populate test_prot_write()
--

commit a72c4c7dc3b5c0ebca1f3529ca19931b9c980959
Author: Xionggui Chen <[email protected]>

Quoted 14 lines

selftests/mm: handle implicit read permissions in madv_populate

The madv_populate selftest assumes that mmap(PROT_WRITE) creates a VMA without VM_READ and expects MADV_POPULATE_READ to fail with EINVAL.

On RISC-V, commit 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") makes the kernel set VM_READ for mappings requested with PROT_WRITE. MADV_POPULATE_READ checks this VMA permission, so it succeeds and the test fails incorrectly.

Adjust the expected result for RISC-V using an architecture-specific conditional, while preserving the existing behavior on other architectures.

Quoted 6 lines

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 16 tests.

Fixes e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)")

[Severity: Low]
Should the Fixes: tag point to 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") instead?

When e5bfac53e310 ("selftests/vm: add test for MADV_POPULATE_(READ|WRITE)") added test_prot_write(), mmap(PROT_WRITE) on RISC-V did not set VM_READ and MADV_POPULATE_READ returned -EINVAL as expected. Backporting this change to stable trees older than 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") will cause test_prot_write() to fail on RISC-V.

maintainer Acked-by David Hildenbrand (Arm)
#14 lore raw

Re: [PATCH v2] selftests/mm: handle implicit read permissions in madv_populate

Quoted 10 lines · On Wed, 18 Nov 2026 16:03:38 +0800 cxg <[email protected]> wrote:

I noticed that the Fixes tag in the patch currently in mm-new points to the original selftest commit.

Since the test failure was introduced by the RISC-V VMA permission change, the Fixes tag should instead be:

Fixes 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ |

VM_WRITE")

Could you please update the Fixes tag in mm-new?

No probs.

From: Xionggui Chen <[email protected]>
Subject: selftests/mm: handle implicit read permissions in madv_populate
Date: Tue, 17 Nov 2026 07:06:06 +0000

The madv_populate selftest assumes that mmap(PROT_WRITE) creates a VMA without VM_READ and expects MADV_POPULATE_READ to fail with EINVAL.

On RISC-V, commit 6c7559f22b6f ("riscv/mm: ensure PROT_WRITE leads to VM_READ | VM_WRITE") makes the kernel set VM_READ for mappings requested with PROT_WRITE. MADV_POPULATE_READ checks this VMA permission, so it succeeds and the test fails incorrectly.

Adjust the expected result for RISC-V using an architecture-specific conditional, while preserving the existing behavior on other architectures.

On RISC-V with Linux 7.2.0, the original test fails 1 out of 16 tests, while the updated test passes all 16 tests.

 tools/testing/selftests/mm/madv_populate.c |    5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/mm/madv_populate.c renamed from tools/testing/selftests/mm/madv_populate.c~selftests-mm-handle-implicit-read-permissions-in-madv_populate +5 -0
@@ -81,8 +81,13 @@ static void test_prot_write(void)
 		ksft_exit_fail_msg("mmap failed\n");
 
 	ret = madvise(addr, SIZE, MADV_POPULATE_READ);
+#ifdef __riscv
+	ksft_test_result(ret == 0,
+			 "MADV_POPULATE_READ with PROT_WRITE\n");
+#else
 	ksft_test_result(ret == -1 && errno == EINVAL,
 			 "MADV_POPULATE_READ with PROT_WRITE\n");
+#endif
 
 	ret = madvise(addr, SIZE, MADV_POPULATE_WRITE);
 	ksft_test_result(!ret, "MADV_POPULATE_WRITE with PROT_WRITE\n");

_