Skip to contentSkip to conversation

Avoid a bitmap write per page in exclude_zero_pages_cyclic()

PATCH New Yenda Li 1 participant 1 message Nov 9

Subsystems (2)
lore
Share & tools
lore

Yenda Li

author
#0 lore raw

PATCH 1/1 Avoid a bitmap write per page in exclude_zero_pages_cyclic()

When the bitmaps are file-backed (non-cyclic mode, e.g. with --work-dir), exclude_zero_pages_cyclic() calls sync_2nd_bitmap() for every pfn in a PT_LOAD segment before is_dumpable(). Each call does an lseek() and a 4 KiB write() of the current bitmap block, even when the block has not changed. Creating an ELF dump with zero-page exclusion therefore makes one 4 KiB write() per page of memory: about 268 million write() calls, 1 TiB in total, on a 1 TiB machine.

The sync is only needed when is_dumpable_file() is about to load a different block into the shared buffer, which would otherwise drop the bits cleared by clear_bit_on_2nd_bitmap() in the current block. Use the condition is_dumpable_file() itself uses to decide to reload, and sync only then. The last dirty block is still written by the sync_2nd_bitmap() call that follows exclude_zero_pages_cyclic() in create_2nd_bitmap().

Measured on a 2 GiB x86_64 guest image with
"-E --message-level 31 -d 31 --work-dir=DIR" (bitmap file on btrfs, warm page cache), median of 10 runs each:

                          before        after
  wall time               552 ms        100 ms
  write() to bitmap file  524,353       81
  kernel instructions     3.71G         0.27G

The dump file and the final bitmap were byte-identical before and after.

On a 256 GiB AMD EPYC server, in the kdump kernel with 4 CPUs and the same options, makedumpfile took 22.7 s instead of 99.5 s, and the zero-page pass took 2.1 s instead of 75.9 s. These were two different crashes of the same machine, with makedumpfile 1.7.9 before and after applying this change; the function is identical in 1.8.0.

Dumps that keep the bitmaps in memory (cyclic mode) and the kdump-compressed formats are not affected.

The problem was found, and this change drafted, with the help of an AI coding assistant (Claude Code), which profiled makedumpfile with perf and strace and proposed the fix. I reviewed the change and tested it as described above.

Fixes 679a818f7716 ("[PATCH 07/13] Integrate filtering process for ELF path.")
---
 makedumpfile.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
makedumpfile.c +8 -1
index b21917b..c017eca 100644
@@ -6317,7 +6317,14 @@ exclude_zero_pages_cyclic(struct cycle *cycle)
 		if (!is_in_segs(paddr))
 			continue;
 
-		if (!sync_2nd_bitmap())
+		/*
+		 * is_dumpable() rereads the bitmap block from the bitmap file
+		 * when pfn is in another block, which would drop the bits
+		 * cleared below.  Write the block back only then: syncing for
+		 * every pfn costs one lseek() and one 4 KiB write() per page.
+		 */
+		if ((pfn == 0 || info->bitmap2->no_block != pfn / PFN_BUFBITMAP)
+		    && !sync_2nd_bitmap())
 			goto out_error;
 
 		if (!is_dumpable(info->bitmap2, pfn, cycle))
Signature · 1 line
2.53.0-Meta