Skip to contentSkip to conversation

writeback: size foreign flushes by target wb dirty pages

PATCH Applied Xin Yin 3 participants 3 messages Sep 24 – Sep 27 VFS

lore
Share & tools
lore

Xin Yin

author
#0 lore raw

PATCH v2 1/1 writeback: size foreign flushes by target wb dirty pages

Foreign dirty tracking records the bdi and wb memcg IDs of recently dirtied foreign inodes. When the source memcg needs foreign writeback, mem_cgroup_flush_foreign() queues WB_REASON_FOREIGN_FLUSH to the recorded target bdi_writeback.

cgroup_writeback_by_id() currently sizes this best-effort work from the target memcg's NR_FILE_DIRTY counter. The work is scoped to one target wb, so a memcg-wide dirty count can over-size a single-bdi flush when the target memcg has dirty pages on other devices. It can also under-size the flush when the target wb has dirty pages charged to other memcgs.

Over-sizing keeps the target wb busy longer and can delay later writeback work. Under-sizing can finish before enough pages are written back for the source memcg.

Use the target wb's WB_RECLAIMABLE counter and keep the existing 25% headroom. This keeps the budget aligned with the writeback object being queued.

Fixes 97b27821b485 ("writeback, memcg: Implement foreign dirty flushing")

---
Changes in v2:
- Submit the budget fix on its own. Drop the snapshot writeback patch for now, pending further evidence of its benefit in production.
- Add Jan's Reviewed-by and Tejun's Acked-by.
- No code changes from v1 patch 1.

v1: https://lore.kernel.org/linux-fsdevel/[email protected]/

 fs/fs-writeback.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
fs/fs-writeback.c +7 -10
index 7c75ed7e8979..7c2340a5dead 100644
@@ -1131,7 +1131,7 @@ int cgroup_writeback_by_id(u64 bdi_id, int memcg_id,
 	struct cgroup_subsys_state *memcg_css;
 	struct bdi_writeback *wb;
 	struct wb_writeback_work *work;
-	unsigned long dirty;
+	long dirty;
 	int ret;
 
 	/* lookup bdi and memcg */
@@ -1160,16 +1160,13 @@ int cgroup_writeback_by_id(u64 bdi_id, int memcg_id,
 	}
 
 	/*
-	 * The caller is attempting to write out most of
-	 * the currently dirty pages.  Let's take the current dirty page
-	 * count and inflate it by 25% which should be large enough to
-	 * flush out most dirty pages while avoiding getting livelocked by
-	 * concurrent dirtiers.
-	 *
-	 * BTW the memcg stats are flushed periodically and this is best-effort
-	 * estimation, so some potential error is ok.
+	 * The caller is attempting to write out most of the target wb's
+	 * currently dirty pages.  Size the work from the wb's reclaimable pages
+	 * and inflate the count by 25%, which should be large enough to flush
+	 * out most dirty pages while avoiding getting livelocked by concurrent
+	 * dirtiers.
 	 */
-	dirty = memcg_page_state(mem_cgroup_from_css(memcg_css), NR_FILE_DIRTY);
+	dirty = wb_stat_sum(wb, WB_RECLAIMABLE);
 	dirty = dirty * 10 / 8;
 
 	/* issue the writeback work */

base-commit: 46b513250491a7bfc97d98791dbe6a10bcc8129d

Signature · 1 line
2.20.1
maintainer Applied
#1 lore raw
Quoted 1 line · Xin Yin

writeback: size foreign flushes by target wb dirty pages

Applied to the vfs-7.4.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.4.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.4.misc

[1/1] writeback: size foreign flushes by target wb dirty pages

      https://git.kernel.org/vfs/vfs/c/168a8c13159c
Tested-by on v2 1/1
#2 lore raw
In reply to Xin Yin
Quoted 2 lines · Xin Yin

Use the target wb's WB_RECLAIMABLE counter and keep the existing 25% headroom.

At Honeycomb, a container that reads from Kafka and writes columnar files to a host volume stalls for 30-60s after each deploy replaces it (v6.18). This is likely the cause: the stall looks the same as in our reproducer (little CPU use, lag growing linearly and then recovering), though we haven't traced it in production yet.

I tested with a reproducer where one cgroup appends to 1000 files at 250MiB/s and stops, then a sibling keeps appending to the same files under a 4G parent limit. With the old cgroup kept alive, worst lag behind schedule over 120s, three runs each, arm64 KVM guest:

  b53f422491af (parent)  9.4s  3.0s  10.7s
  168a8c13159c           1.0s  1.1s   1.1s

When the old cgroup is removed, which is what a restart does, this patch alone doesn't help: wb_get_lookup() can't find the dying wb, so the flush fails with -ENOENT before the sizing matters. My follow-up fixes the lookup. Worst lag in each run, this patch alone and with the follow-up on top:

                                       this patch         + follow-up
  old cgroup removed, 1000 files       85.3  46.9  52.1s  1.1  1.0  1.1s
  500k files, synced before removal   115.3 115.3s        1.5  1.0s
  500k files, pod cgroup removed too  115.9  47.5s        1.0  1.0s

https://lore.kernel.org/all/[email protected]/

Christian, could this one get Cc: stable as well? Both bugs date back to v5.4.

Debugged with Claude Opus 5.5, which ran the tests; I designed the experiments and reviewed the results.

Thanks,
Liz