Skip to content

fs/pipe: reduce pipe->mutex contention by pre-allocating outside the lock#1445

Open
vfsci-bot[bot] wants to merge 2 commits into
vfs.base.cifrom
pw/1099509/vfs.base.ci
Open

fs/pipe: reduce pipe->mutex contention by pre-allocating outside the lock#1445
vfsci-bot[bot] wants to merge 2 commits into
vfs.base.cifrom
pw/1099509/vfs.base.ci

Conversation

@vfsci-bot
Copy link
Copy Markdown

@vfsci-bot vfsci-bot Bot commented May 22, 2026

Series: https://patchwork.kernel.org/project/linux-fsdevel/list/?series=1099509
Submitter: Breno Leitao
Version: 2
Patches: 2/2
Message-ID: <20260522-fix_pipe-v2-0-a8b35a78244e@debian.org>
Base: vfs.base.ci
Lore: https://lore.kernel.org/linux-fsdevel/20260522-fix_pipe-v2-0-a8b35a78244e@debian.org


Automated by ml2pr

leitao added 2 commits May 22, 2026 17:51
anon_pipe_write() takes pipe->mutex (aka "mutex protecting the whole
thing") and then, from the per-iteration anon_pipe_get_page() helper,
used to call alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT) once per page
while still holding it.

That allocation can sleep doing direct reclaim and/or runs memcg
charging, which extends the critical section and stalls a concurrent
reader on the very same mutex.

Just pre-alloc the required pages before the lock in an array and just pop
them inside the lock.

This can improve the pipe throughput up to 48% and reduce the
latency in 33%, easily seen when there is memory pressure and direct
reclaim.

Signed-off-by: Breno Leitao <leitao@debian.org>
Add a small selftest that stresses pipe->mutex contention by spawning N
writer threads that hammer a single pipe with multi-page writes, plus M
reader threads that drain. Each writer records its own write() latency
samples into a log2-bucketed histogram; main aggregates and prints
total writes, throughput, average and percentile (p50/p99) latencies,
and the maximum observed latency.

Pass --memory-pressure to fork stress-ng (--vm 4 --vm-bytes 80%
--vm-method all) for the duration of the run, so alloc_page() in
anon_pipe_write() routinely hits direct reclaim. The flag fails
fast if stress-ng is not on $PATH.

Program print something like the following, for different writes,
readers, msgsizes and memory pressure:

	config: writers=X readers=Y msgsize=Z duration=3 pipe_size=1048576
	memory_pressure=[no|yes]
	writes: total=54451 rate=18150/s
	throughput_MBps: 1134.40
	lat_avg_ns: 275355
	lat_p50_ns_upper: 262143
	lat_p99_ns_upper: 1048575
	lat_max_ns: 2145633

Signed-off-by: Breno Leitao <leitao@debian.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant