Linux 7.2 Improves Anonymous/Unnamed Pipe Performance For Shell Pipelines & More

Linux 7.2 Improves Anonymous/Unnamed Pipe Performance For Shell Pipelines & More

Linux 7.2 提升了 Shell 管道及其他场景下匿名管道的性能

Written by Michael Larabel in Linux Kernel on 16 June 2026 at 01:05 PM EDT. 本文由 Michael Larabel 撰写,发布于 Linux 内核专栏,时间为 2026 年 6 月 16 日下午 01:05(东部夏令时间)。

Yet another performance optimization merged for the in-development Linux 7.2 kernel is improving the speed of anon_pipe_write, the kernel function used for writing data into anonymous/unnamed pipes such as when using shell pipelines or standard streams from applications. 在开发中的 Linux 7.2 内核中,又一项性能优化被合并,旨在提升 anon_pipe_write 的速度。该内核函数用于向匿名管道(unnamed pipes)写入数据,例如在使用 Shell 管道或应用程序的标准流时。

Breno Leitao of Meta was profiling some of their caching code and found pipe to mutex contention in a hot path, which is now resolved by the newly-merged code to pre-allocate outside the lock for avoiding contention. 来自 Meta 的 Breno Leitao 在分析其缓存代码时,发现热点路径中存在管道互斥锁(mutex)竞争问题。新合并的代码通过在锁外进行预分配来避免竞争,从而解决了这一问题。

In the VFS misc pull request that situation is described as: “anon_pipe_write() called alloc_page() once per page while holding pipe->mutex. The allocation can sleep doing direct reclaim and runs memcg charging, which extends the critical section and stalls any concurrent reader on the same mutex. Now up to 8 pages are pre-allocated before the mutex is taken, leftovers are recycled into the per-pipe tmp_page[] cache before unlock, and any remainder is released after unlock, keeping the allocator out of the critical section on both sides. 在 VFS 杂项合并请求(pull request)中,该情况描述如下:“anon_pipe_write() 在持有 pipe->mutex 时,每页调用一次 alloc_page()。分配过程可能会因直接内存回收(direct reclaim)而进入睡眠,并执行 memcg 计费,这延长了临界区并阻塞了同一互斥锁上的任何并发读取器。现在,在获取互斥锁之前会预先分配最多 8 个页面,剩余部分在解锁前被回收至每个管道的 tmp_page[] 缓存中,其余部分在解锁后释放,从而使分配器在临界区两侧均保持在外部。”

On a writers x readers sweep with 64KB writes against a 1 MB pipe throughput improves 6-28% and average write latency drops 5-22%; under memory pressure - when the cost of holding the mutex across reclaim is highest - throughput improves 21-48% and latency drops 17-33%. The microbenchmark is added to selftests.” “在针对 1MB 管道进行 64KB 写入的读写测试中,吞吐量提升了 6-28%,平均写入延迟降低了 5-22%;在内存压力下(即在回收过程中持有互斥锁成本最高时),吞吐量提升了 21-48%,延迟降低了 17-33%。该微基准测试已添加到自测(selftests)中。”

Very nice gains. More of the numbers in detail can be found via this patch cover letter by Breno Leitao. That work is now merged for Linux 7.2. 性能提升非常可观。更多详细数据可以通过 Breno Leitao 提供的补丁说明信查看。该工作现已合并至 Linux 7.2。