Skip to contentSkip to conversation

swiotlb performance optimizations

RFC In review Chao Gao 1 participant 4 messages Jul 18, 2022 – Aug 5, 2022 DMA mapping helpers

lore
Share & tools
lore

Chao Gao

author
#0 lore raw

v2 cover swiotlb performance optimizations

Intent of this post:
Seek reviews from Intel reviewers and anyone else in the list interested in IO performance in confidential VMs. Need some acked-by reviewed-by tags before I can add swiotlb maintainers to "to/cc" lists and ask for a review from them.

Changes from v1 to v2:
- rebase to the latest dma-mapping tree.
- drop the duplicate patch for mitigating lock contention
- re-collect perf data

swiotlb is now widely used by confidential VMs. This series optimizes swiotlb to reduce cache misses and lock contention during bounce buffer allocation/free and memory bouncing to improve IO workload performance in confidential VMs.

Here are some FIO tests we did to demonstrate the improvement.

Test setup
----------

A normal VM with 8vCPU and 32G memory, swiotlb is enabled by swiotlb=force. FIO block size is 4K and iodepth is 256. Note that a normal VM is used so that others lack of necessary hardware to host confidential VMs can reproduce results below.

Results
-------

1 FIO job read/write IOPS (k)
vanilla read 216

		write		251 
optimized	read		250 
		write		270 

1-job FIO sequential read/write perf increase by 19% and 8% respectively.

Chao Gao (2):
swiotlb: use bitmap to track free slots
swiotlb: Allocate memory in a cache-friendly way

 include/linux/swiotlb.h |   8 ++-
 kernel/dma/swiotlb.c    | 127 +++++++++++++++++-----------------------
 2 files changed, 60 insertions(+), 75 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

RFC v2 1/2 swiotlb: use bitmap to track free slots

Currently, each slot tracks the number of contiguous free slots starting from itself. It helps to quickly check if there are enough contiguous entries when dealing with an allocation request. But maintaining this information can leads to some overhead. Specifically, if a slot is allocated/freed, preceding slots may need to be updated as the number of contiguous free slots can change. This process may access memory scattering over multiple cachelines.

To reduce the overhead of maintaining the number of contiguous free entries, use a global bitmap to track free slots; each bit represents if a slot is available. The number of contiguous free slots can be calculated by counting the number of consecutive 1s in the bitmap.

Tests show that the average cost of freeing slots drops by 120 cycles while the average cost of allocation increases by 20 cycles. Overall, 100 cycles are saved from a pair of allocation and freeing.

---
Ilpo, I didn't add your Reviewed-by as many changes were made due to conflicts during rebasing.

---
 include/linux/swiotlb.h |  6 ++---
 kernel/dma/swiotlb.c    | 60 +++++++++++++++++++----------------------
 2 files changed, 30 insertions(+), 36 deletions(-)
include/linux/swiotlb.h +3 -3
index d3ae03edbbd2..2c8e6f5df610 100644
@@ -77,8 +77,6 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
  *		@end. For default swiotlb, this is command line adjustable via
  *		setup_io_tlb_npages.
  * @used:	The number of used IO TLB block.
- * @list:	The free list describing the number of free entries available
- *		from each index.
  * @orig_addr:	The original address corresponding to a mapped entry.
  * @alloc_size:	Size of the allocated buffer.
  * @debugfs:	The dentry to debugfs.
@@ -87,6 +85,8 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
  * @for_alloc:  %true if the pool is used for memory allocation
  * @nareas:  The area number in the pool.
  * @area_nslabs: The slot number in the area.
+ * @bitmap:	The bitmap used to track free entries. 1 in bit X means the slot
+ *		indexed by X is free.
  */
 struct io_tlb_mem {
 	phys_addr_t start;
@@ -104,8 +104,8 @@ struct io_tlb_mem {
 	struct io_tlb_slot {
 		phys_addr_t orig_addr;
 		size_t alloc_size;
-		unsigned int list;
 	} *slots;
+	unsigned long *bitmap;
 };
 extern struct io_tlb_mem io_tlb_default_mem;
 
kernel/dma/swiotlb.c +27 -33

133 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#2 lore raw

RFC v2 2/2 swiotlb: Allocate memory in a cache-friendly way

In reply to Chao Gao

Currently, swiotlb uses an index to indicate the starting point of next search. The index increases from 0 to the number of slots - 1 and then wraps around. It is straightforward but not cache-friendly because the "oldest" slot in a swiotlb area is used first.

Freed slots are probably accessed right before being freed, especially in VM's case (device backends access them in DMA_TO_DEVICE mode; guest accesses them in other DMA modes). Thus those just freed slots may reside in cache. Then reusing those just freed slots can reduce cache misses.

To that end, maintain a free list for free slots and insert freed slots from the head and searching for free slots always starts from the head.

With this optimization, network throughput of sending data from host to guest, measured by iperf3, increases by 7%.

A bad side effect of this patch is we cannot use a large stride to skip unaligned slots when there is an alignment requirement. Currently, a large stride is used when a) device has an alignment requirement, stride is calculated according to the requirement; b) the requested size is larger than PAGE_SIZE. For x86 with 4KB page size, stride is set to 2.

For case a), few devices have an alignment requirement; the impact is limited. For case b) this patch probably leads to one (or more if page size is larger than 4K) additional lookup; but as the "io_tlb_slot" struct of free slots are also accessed when freeing slots, they probably resides in CPU cache as well and then the overhead is almost negligible.

---
 include/linux/swiotlb.h |  2 ++
 kernel/dma/swiotlb.c    | 71 +++++++++++++++++------------------------
 2 files changed, 32 insertions(+), 41 deletions(-)
include/linux/swiotlb.h +2 -0
index 2c8e6f5df610..335a550aeda5 100644
@@ -79,6 +79,7 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
  * @used:	The number of used IO TLB block.
  * @orig_addr:	The original address corresponding to a mapped entry.
  * @alloc_size:	Size of the allocated buffer.
+ * @node:	Representation of an io_tlb_slot in the per-area free list.
  * @debugfs:	The dentry to debugfs.
  * @late_alloc:	%true if allocated using the page allocator
  * @force_bounce: %true if swiotlb bouncing is forced
@@ -104,6 +105,7 @@ struct io_tlb_mem {
 	struct io_tlb_slot {
 		phys_addr_t orig_addr;
 		size_t alloc_size;
+		struct list_head node;
 	} *slots;
 	unsigned long *bitmap;
 };
kernel/dma/swiotlb.c +30 -41

152 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#3 lore raw
In reply to Chao Gao

Ping. Intel reviewers, comments and suggestions are welcome.

Quoted 48 lines · Chao Gao

Intent of this post:
Seek reviews from Intel reviewers and anyone else in the list interested in IO performance in confidential VMs. Need some acked-by reviewed-by tags before I can add swiotlb maintainers to "to/cc" lists and ask for a review from them.

Changes from v1 to v2:
- rebase to the latest dma-mapping tree.
- drop the duplicate patch for mitigating lock contention
- re-collect perf data

swiotlb is now widely used by confidential VMs. This series optimizes swiotlb to reduce cache misses and lock contention during bounce buffer allocation/free and memory bouncing to improve IO workload performance in confidential VMs.

Here are some FIO tests we did to demonstrate the improvement.

Test setup
----------

A normal VM with 8vCPU and 32G memory, swiotlb is enabled by swiotlb=force. FIO block size is 4K and iodepth is 256. Note that a normal VM is used so that others lack of necessary hardware to host confidential VMs can reproduce results below.

Results
-------

1 FIO job read/write IOPS (k)
vanilla read 216

		write		251 
optimized	read		250 
		write		270 

1-job FIO sequential read/write perf increase by 19% and 8% respectively.

Chao Gao (2):
swiotlb: use bitmap to track free slots
swiotlb: Allocate memory in a cache-friendly way

include/linux/swiotlb.h | 8 ++-
kernel/dma/swiotlb.c | 127 +++++++++++++++++-----------------------

2 files changed, 60 insertions(+), 75 deletions(-)
Signature · 1 line
2.25.1