Skip to content

swiotlb: avoid swiotlb copy on network sockets

Luigi Rizzo driver-core linux-iommu linux-mm lkml llvm netdev oe-kbuild-all syzbot 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest5 patches 6 repliesIn review

Changes in v2 from the cover letter

- split components into separate commits
- simplified allocator, no need for a new page type
- many code cleanups
- also implement the rx side

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5swiotlb: enforce pool nareas and nslabs invariantskernel/dma/swiotlb.c+16 -30 replies
2/5swiotlb/mm: Implement SWIOTLB nocopy page allocatorinclude/linux/swiotlb.h kernel/dma/swiotlb.c mm/page_alloc.c+255 -73 replies
3/5net/swiotlb: Track bounce device per socketdrivers/base/core.c include/linux/netdevice.h include/linux/swiotlb.h +3 more+216 -00 replies
4/5net: Divert socket allocations to SWIOTLB for nocopy TXdrivers/iommu/dma-iommu.c include/linux/skbuff.h include/linux/swiotlb.h +4 more+102 -111 reply
5/5swiotlb: Implement RX nocopy with fast recycling evictioninclude/linux/swiotlb.h kernel/dma/swiotlb.c net/core/page_pool.c+28 -31 reply

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/5] swiotlb: avoid swiotlb copy on network sockets

The use of swiotlb, common in Confidential Computing, causes an extra data copy on each I/O. Focusing on network sockets:
- on tx, the copy has a high chance of happening in the tx softirq handler (especially with greedy senders where the device queue is often full)
- on rx, it is guaranteed to happen in the rx softirq handler. Thus, on top of the copy cost, swiotlb concentrates the overhead on an already constrained resource (CPUs processing network interrupts).

Reduce or remove the extra copy by conditionally allocating socket buffers directly from the swiotlb buffer pool.

The feature is controlled by runtime parameters to set the percentage of swiotlb buffers that can be used for this purpose. This avoids stranding the entire swiotlb pool in socket buffers.

The implementation is made of four main parts:
- introduce a swiotlb page allocator that can be used instead of regular pages, and teach __free_frozen_pages(), free_unref_folio() how to handle them
- dynamically track the leaf device for each tx network socket, so we can tell at copy_from_user() time whether we need to use swiotlb for this socket
- modify skb_page_frag_refill() to allocate from swiotlb if needed. This implements the copy elision for the transmit path
- modify __page_pool_alloc_page_order() to allocate from swiotlb if needed. This implements the copy elision for the receive path.

The savings are especially visible with fewer queues. In synthetic benchmarks, senders with 1-2 queues would cap around 50Gbps with conventional swiotlb, and reach over 170Gbps with the feature enabled.

OPEN ISSUES

Currently the swiotlb allocator looks for free slots using an approximately linear scan of each pool (with some hints to likely candidates) and then does a linear scan of subsequent pools. This works extremely well when the number of pools matches the number of CPUs, and there is plenty of memory available. In fact, it is almost unbeatable by any more complex strategy.

Under high load or buffer fragmentation, a CPU might repeatedly do a full scan of its starting pool before finding a suitable candidate. Even worse, with multiple tx/rx queues, what happens is that multiple CPUs will trail each other on the same sequence of pools. The effect is that some allocations will end up costing O(100us) and more.

I have tried to implement two improvements:
- a buddy allocator on top of each pool, so to make it quicker to find a candidate of the requested size
- make each CPU use a different sequence to explore other pools in case one is full, so they will not end up queueing one after the other
While they are very effective on the tails, for low load scenarios the current linear allocators is better. Thus this will take more investigation.

---
v1 -> v2:

- split components into separate commits
- simplified allocator, no need for a new page type
- many code cleanups
- also implement the rx side

Luigi Rizzo (5):
swiotlb: enforce pool nareas and nslabs invariants swiotlb/mm: Implement SWIOTLB nocopy page allocator net/swiotlb: Track bounce device per socket
net: Divert socket allocations to SWIOTLB for nocopy TX
swiotlb: Implement RX nocopy with fast recycling eviction

 drivers/base/core.c       |   1 +
 drivers/iommu/dma-iommu.c |   9 +-
 include/linux/netdevice.h |  21 +++
 include/linux/skbuff.h    |   7 +-
 include/linux/swiotlb.h   |  63 ++++++++
 include/net/sock.h        |  46 ++++++
 kernel/dma/direct.h       |  11 ++
 kernel/dma/swiotlb.c      | 296 ++++++++++++++++++++++++++++++++++++--
 mm/page_alloc.c           |  61 +++++++-
 net/core/page_pool.c      |  25 +++-
 net/core/sock.c           | 101 +++++++++++--
 11 files changed, 617 insertions(+), 24 deletions(-)
Signature · 1 line
2.55.0.766.g2966f0265a-goog