All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] tools/mm: page-types: flush pending idle bitmap bits after walking
@ 2026-11-25  9:14 Xuewen Wang
  2026-11-25 21:08 ` + tools-mm-page-types-flush-pending-idle-bitmap-bits-after-walking.patch added to mm-new branch Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: Xuewen Wang @ 2026-11-25  9:14 UTC (permalink / raw)
  To: akpm, david, ljs, liam, vbabka, rppt, surenb, mhocko, chansen3,
	sj
  Cc: linux-mm, linux-kernel, Xuewen Wang

From: Xuewen Wang <wangxuewen@kylinos.cn>

mark_page_idle() buffers bits for one 64-page bitmap word and writes
that word when a page in another word is encountered.

walk_addr_ranges() tries to flush the final word by marking PFN 0.
If the last buffered PFN is below 64, this only adds a bit to the same
word and nothing is written. Otherwise it writes the pending word but
leaves an unwanted PFN 0 bit buffered. walk_page_cache() does not flush
at all, so its final pending bits are not written.

Extract the write operation into flush_page_idle() and use it both when
changing words and after either walker returns to main(). Clear the
buffer after a successful write and skip writes when it is empty.
Place the final flush before the no-summary early return and return
EXIT_FAILURE if it fails.

Use 1ULL for the bit mask so shifts by 32 through 63 are also valid
on 32-bit hosts.

Fixes: 59ae96ffc3a67 ("tools/vm/page-types.c: add support for idle page tracking")
Signed-off-by: Xuewen Wang <wangxuewen@kylinos.cn>
---
Changes in v2:
- Return EXIT_FAILURE if the final flush_page_idle() fails, as suggested
  by Andrew Morton.

v1: https://lore.kernel.org/all/20261118021253.1691428-1-wangxuewen_ky@163.com/

Verified on live kernels (v5.10 and v6.6): after "page-types -f <file>
-i" on a 100-page test file, the kernel idle bitmap held 99/100 of the
bits before the fix and 100/100 after it.

 tools/mm/page-types.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/tools/mm/page-types.c b/tools/mm/page-types.c
index 47e4781c5fc3..1695a7b42169 100644
--- a/tools/mm/page-types.c
+++ b/tools/mm/page-types.c
@@ -575,27 +575,39 @@ static int unpoison_page(unsigned long offset)
 	return 0;
 }
 
-static int mark_page_idle(unsigned long offset)
+static unsigned long page_idle_offset;
+static uint64_t page_idle_buf;
+
+static int flush_page_idle(void)
 {
-	static unsigned long off;
-	static uint64_t buf;
 	int len;
 
-	if ((offset / 64 == off / 64) || buf == 0) {
-		buf |= 1UL << (offset % 64);
-		off = offset;
+	if (!page_idle_buf)
 		return 0;
-	}
 
-	len = pwrite(page_idle_fd, &buf, 8, 8 * (off / 64));
+	len = pwrite(page_idle_fd, &page_idle_buf, 8,
+		     8 * (page_idle_offset / 64));
 	if (len < 0) {
 		perror("mark page idle");
 		return len;
 	}
 
-	buf = 1UL << (offset % 64);
-	off = offset;
+	page_idle_buf = 0;
+	return 0;
+}
+
+static int mark_page_idle(unsigned long offset)
+{
+	int ret;
+
+	if (offset / 64 != page_idle_offset / 64) {
+		ret = flush_page_idle();
+		if (ret < 0)
+			return ret;
+	}
 
+	page_idle_buf |= 1ULL << (offset % 64);
+	page_idle_offset = offset;
 	return 0;
 }
 
@@ -798,9 +810,6 @@ static void walk_addr_ranges(void)
 		else
 			walk_task(opt_offset[i], opt_size[i]);
 
-	if (opt_mark_idle)
-		mark_page_idle(0);
-
 	close(kpageflags_fd);
 }
 
@@ -1364,6 +1373,9 @@ int main(int argc, char *argv[])
 	else
 		walk_addr_ranges();
 
+	if (opt_mark_idle && flush_page_idle() < 0)
+		return EXIT_FAILURE;
+
 	if (opt_list == 1)
 		flush_page_range();
 
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25 21:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  9:14 [PATCH v2] tools/mm: page-types: flush pending idle bitmap bits after walking Xuewen Wang
2026-11-25 21:08 ` + tools-mm-page-types-flush-pending-idle-bitmap-bits-after-walking.patch added to mm-new branch Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.