[LTP] [PATCH v2 1/2] msync04: Check smaps when the page frame number is not exposed

Wake Liu wakel@google.com
Mon Nov 16 15:22:39 CET 2026


The test looks up the dirty flag of the mapped page in /proc/kpageflags,
using the PFN it reads from /proc/self/pagemap. If the PFN is reported
as zero, get_dirty_bit() returns 0 and the test falls back to checking
the file content, which can end only with TCONF or TFAIL.

The PFN of a present page is reported as zero with the 16K page size
emulation that Android uses on x86_64, where each entry of the emulated
pagemap covers four 4K kernel pages and therefore carries no single PFN.
The test then reports:

  TCONF: Write was too fast, couldn't test msync()

When the page is present but its PFN is not exposed, sum up the
Shared_Dirty and Private_Dirty counters of the mapping in
/proc/self/smaps instead. They account for both the dirty bit in the
page table and the dirty flag of the page, and drop to zero once
msync() has written the page back. Nothing changes on kernels that
expose the PFN to root.

Signed-off-by: Wake Liu <wakel@google.com>
---
 testcases/kernel/syscalls/msync/msync04.c | 46 ++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/msync/msync04.c b/testcases/kernel/syscalls/msync/msync04.c
index 1251388b6..1ed56fec6 100644
--- a/testcases/kernel/syscalls/msync/msync04.c
+++ b/testcases/kernel/syscalls/msync/msync04.c
@@ -17,6 +17,7 @@
 #define _GNU_SOURCE
 #include <errno.h>
 #include "tst_test.h"
+#include "tst_safe_stdio.h"
 
 static int test_fd;
 static char *mmaped_area;
@@ -24,6 +25,39 @@ static long pagesize;
 
 #define STRING_TO_WRITE	"AAAAAAAAAA"
 
+#define PM_PFRAME_MASK	((1ULL << 55) - 1)
+#define PM_PRESENT	(1ULL << 63)
+
+/*
+ * Returns the amount of dirty memory of the mapping starting at addr, as
+ * accounted in /proc/self/smaps.
+ */
+static long get_smaps_dirty_kb(unsigned long addr)
+{
+	FILE *fp = SAFE_FOPEN("/proc/self/smaps", "r");
+	unsigned long start, end;
+	long dirty_kb = 0, kb;
+	char line[1024];
+	int found = 0;
+
+	while (fgets(line, sizeof(line), fp)) {
+		if (sscanf(line, "%lx-%lx ", &start, &end) == 2) {
+			found = start == addr;
+			continue;
+		}
+
+		if (!found)
+			continue;
+
+		if (sscanf(line, "Shared_Dirty: %ld kB", &kb) == 1 ||
+		    sscanf(line, "Private_Dirty: %ld kB", &kb) == 1)
+			dirty_kb += kb;
+	}
+
+	SAFE_FCLOSE(fp);
+	return dirty_kb;
+}
+
 static uint64_t get_dirty_bit(void *data)
 {
 	int pagemap_fd, pageflags_fd;
@@ -37,9 +71,17 @@ static uint64_t get_dirty_bit(void *data)
 	SAFE_READ(SAFE_READ_ALL, pagemap_fd, &pagemap_entry,
 		  sizeof(pagemap_entry));
 	SAFE_CLOSE(pagemap_fd);
-	pfn = pagemap_entry & ((1ULL << 55) - 1);
-	if (!pfn)
+	pfn = pagemap_entry & PM_PFRAME_MASK;
+	if (!pfn) {
+		/*
+		 * The PFN of a present page may not be exposed, check the
+		 * dirty accounting of the mapping in smaps instead.
+		 */
+		if (pagemap_entry & PM_PRESENT)
+			return get_smaps_dirty_kb(addr) > 0;
+
 		return 0;
+	}
 	pageflags_fd = SAFE_OPEN("/proc/kpageflags", O_RDONLY);
 	index = pfn * sizeof(uint64_t);
 	SAFE_LSEEK(pageflags_fd, index, SEEK_SET);
-- 
2.56.0.611.g1030f0b940-goog



More information about the ltp mailing list