Skip to contentSkip to conversation

swiotlb: Add child io tlb mem support

RFC PATCH Superseded Tianyu Lan 4 participants 9 messages May 2, 2022 – May 31, 2022

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tianyu Lan

author
#0 lore raw

v2 cover swiotlb: Add child io tlb mem support

From: Tianyu Lan <[email protected]>

Traditionally swiotlb was not performance critical because it was only used for slow devices. But in some setups, like TDX/SEV confidential guests, all IO has to go through swiotlb. Currently swiotlb only has a single lock. Under high IO load with multiple CPUs this can lead to significant lock contention on the swiotlb lock.

This patch adds child IO TLB mem support to resolve spinlock overhead among device's queues. Each device may allocate IO tlb mem and setup child IO TLB mem according to queue number. The number child IO tlb mem maybe set up equal with device queue number and this helps to resolve swiotlb spinlock overhead among devices and queues.

Patch 2 introduces IO TLB Block concepts and swiotlb_device_allocate() API to allocate per-device swiotlb bounce buffer. The new API Accepts queue number as the number of child IO TLB mem to set up device's IO TLB mem.

Tianyu Lan (2):
swiotlb: Add Child IO TLB mem support
Swiotlb: Add device bounce buffer allocation interface

 include/linux/swiotlb.h |  40 ++++++
 kernel/dma/swiotlb.c    | 290 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 317 insertions(+), 13 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

RFC PATCH v2 1/2 swiotlb: Add Child IO TLB mem support

From: Tianyu Lan <[email protected]>

Traditionally swiotlb was not performance critical because it was only used for slow devices. But in some setups, like TDX/SEV confidential guests, all IO has to go through swiotlb. Currently swiotlb only has a single lock. Under high IO load with multiple CPUs this can lead to significant lock contention on the swiotlb lock.

This patch adds child IO TLB mem support to resolve spinlock overhead among device's queues. Each device may allocate IO tlb mem and setup child IO TLB mem according to queue number. Swiotlb code allocates bounce buffer among child IO tlb mem iterately.

---
 include/linux/swiotlb.h |  7 +++
 kernel/dma/swiotlb.c    | 97 ++++++++++++++++++++++++++++++++++++-----
 2 files changed, 94 insertions(+), 10 deletions(-)
include/linux/swiotlb.h +7 -0
index 7ed35dd3de6e..4a3f6a7b4b7e 100644
@@ -89,6 +89,9 @@ extern enum swiotlb_force swiotlb_force;
  * @late_alloc:	%true if allocated using the page allocator
  * @force_bounce: %true if swiotlb bouncing is forced
  * @for_alloc:  %true if the pool is used for memory allocation
+ * @child_nslot:The number of IO TLB slot in the child IO TLB mem.
+ * @num_child:  The child io tlb mem number in the pool.
+ * @child_start:The child index to start searching in the next round.
  */
 struct io_tlb_mem {
 	phys_addr_t start;
@@ -102,6 +105,10 @@ struct io_tlb_mem {
 	bool late_alloc;
 	bool force_bounce;
 	bool for_alloc;
+	unsigned int num_child;
+	unsigned int child_nslot;
+	unsigned int child_start;
+	struct io_tlb_mem *child;
 	struct io_tlb_slot {
 		phys_addr_t orig_addr;
 		size_t alloc_size;
kernel/dma/swiotlb.c +87 -10

147 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

Re: [RFC PATCH V2 1/2] swiotlb: Add Child IO TLB mem support

I don't really understand how 'childs' fit in here. The code also doesn't seem to be usable without patch 2 and a caller of the new functions added in patch 2, so it is rather impossible to review.

Also:

1) why is SEV/TDX so different from other cases that need bounce buffering to treat it different and we can't work on a general scalability improvement
2) per previous discussions at how swiotlb itself works, it is clear that another option is to just make pages we DMA to shared with the hypervisor. Why don't we try that at least for larger I/O?

author
#3 lore raw

Re: [RFC PATCH V2 1/2] swiotlb: Add Child IO TLB mem support

Quoted 3 lines · Christoph Hellwig

I don't really understand how 'childs' fit in here. The code also doesn't seem to be usable without patch 2 and a caller of the new functions added in patch 2, so it is rather impossible to review.

Hi Christoph:
OK. I will merge two patches and add a caller patch. The motivation
is to avoid global spin lock when devices use swiotlb bounce buffer and
this introduces overhead during high throughput cases. In my test
environment, current code can achieve about 24Gb/s network throughput
with SWIOTLB force enabled and it can achieve about 40Gb/s without
SWIOTLB force. Storage also has the same issue.
Per-device IO TLB mem may resolve global spin lock issue among
devices but device still may have multi queues. Multi queues still need
to share one spin lock. This is why introduce child or IO tlb areas in
the previous patches. Each device queues will have separate child IO TLB
mem and single spin lock to manage their IO TLB buffers.
Otherwise, global spin lock still cost cpu usage during high
throughput even when there is performance regression. Each device queues needs to spin on the different cpus to acquire the global lock. Child IO TLB mem also may resolve the cpu issue.

Quoted 6 lines

Also:

1) why is SEV/TDX so different from other cases that need bounce buffering to treat it different and we can't work on a general scalability improvement

Other cases also have global spin lock issue but it depends on whether hits the bottleneck. The cpu usage issue may be ignored.

Quoted 4 lines

2) per previous discussions at how swiotlb itself works, it is clear that another option is to just make pages we DMA to shared with the hypervisor. Why don't we try that at least for larger I/O?

For confidential VM(Both TDX and SEV), we need to use bounce buffer to copy between private memory that hypervisor can't access directly and shared memory. For security consideration,
confidential VM should not share IO stack DMA pages with
hypervisor directly to avoid attack from hypervisor when IO stack handles the DMA data.

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#4 lore raw

RE: [RFC PATCH V2 1/2] swiotlb: Add Child IO TLB mem support

In reply to Christoph Hellwig

From: Christoph Hellwig <[email protected]> Sent: Monday, May 16, 2022 12:35 AM

Quoted 7 lines

I don't really understand how 'childs' fit in here. The code also doesn't seem to be usable without patch 2 and a caller of the new functions added in patch 2, so it is rather impossible to review.

Also:

Quoted 7 lines

1) why is SEV/TDX so different from other cases that need bounce buffering to treat it different and we can't work on a general scalability improvement
2) per previous discussions at how swiotlb itself works, it is clear that another option is to just make pages we DMA to shared with the hypervisor. Why don't we try that at least for larger I/O?

Tianyu already responded, but let me offer an expanded view. I have better knowledge of AMD's SEV-SNP than of Intel's TDX, so my details might be off for TDX.

Taking your question (2) first, two things must be done when guest memory pages transition between the "shared with the hypervisor" and the "private to the guest" states:

A) Some bookkeeping between the guest and host, which requires a hypercall. Doing a hypercall isn't super-fast, but for large I/Os, it could be a reasonable tradeoff if we could avoid bounce buffer copying.

B) The contents of the memory buffer must transition between encrypted and not encrypted. The hardware doesn't provide any mechanism to do such a transition "in place". The only way to transition is for the CPU to copy the contents between an encrypted area and an unencrypted area of memory.

Because of (B), we're stuck needing bounce buffers. There's no way to avoid them with the current hardware. Tianyu also pointed out not wanting to expose uninitialized guest memory to the host, so, for example, sharing a read buffer with the host requires that it first be initialized to zero.

For your question (1), I think we all would agree that SEV-SNP and TDX usage of bounce buffers isn't fundamentally different from other uses -- they just put a lot more load on the bounce buffering mechanism. If done well, general swiotlb scalability improvements should be sufficient and are much preferred.

You made a recent comment about almost being done removing all knowledge of swiotlb from drivers [1]. I agree with that goal. However, Tianyu's recent patches for improving swiotlb scalability don't align with that goal. A while back, you suggested using per-device swiotlb regions [2], and I think Tianyu's patch sets have taken that approach. But that approach requires going beyond the existing per-device swiotlb regions to get scalability with multi-channel devices, and that's leading us in the wrong direction.

We should reset and make sure we agree on the top-level approach.
1) We want general scalability improvements to swiotlb. These improvements should scale to high CPUs counts (> 100) and for multiple NUMA nodes.
2) Drivers should not require any special knowledge of swiotlb to benefit from the improvements. No new swiotlb APIs should be need to be used by drivers -- the swiotlb scalability improvements should be transparent.
3) The scalability improvements should not be based on device boundaries since a single device may have multiple channels doing bounce buffering on multiple CPUs in parallel.

Anything else?

The patch from Andi Kleen [3] (not submitted upstream, but referenced by Tianyu as the basis for his patches) seems like a good starting point for meeting the top-level approach. Andi and Robin had some back-and-forth about Andi's patch that I haven't delved into yet, but getting that worked out seems like a better overall approach. I had an offline chat with Tianyu, and he would agree as well.

Agree? Disagree?

Michael

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://github.com/intel/tdx/commit/4529b578
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#5 lore raw

Re: [RFC PATCH V2 1/2] swiotlb: Add Child IO TLB mem support

Quoted 11 lines · Michael Kelley (LINUX)

B) The contents of the memory buffer must transition between encrypted and not encrypted. The hardware doesn't provide any mechanism to do such a transition "in place". The only way to transition is for the CPU to copy the contents between an encrypted area and an unencrypted area of memory.

Because of (B), we're stuck needing bounce buffers. There's no way to avoid them with the current hardware. Tianyu also pointed out not wanting to expose uninitialized guest memory to the host, so, for example, sharing a read buffer with the host requires that it first be initialized to zero.

Ok, B is a deal breaker. I just brought this in because I've received review comments that state bouncing is just the easiest option for now and we could map it into the hypervisor in the future. But at least for SEV that does not seem like an option without hardware changes.

Quoted 11 lines

We should reset and make sure we agree on the top-level approach.
1) We want general scalability improvements to swiotlb. These improvements should scale to high CPUs counts (> 100) and for multiple NUMA nodes.
2) Drivers should not require any special knowledge of swiotlb to benefit from the improvements. No new swiotlb APIs should be need to be used by drivers -- the swiotlb scalability improvements should be transparent.
3) The scalability improvements should not be based on device boundaries since a single device may have multiple channels doing bounce buffering on multiple CPUs in parallel.

Agreed to all counts.

Quoted 3 lines

The patch from Andi Kleen [3] (not submitted upstream, but referenced by Tianyu as the basis for his patches) seems like a good starting point for meeting the top-level approach.

Yes, I think doing per-cpu and/or per-node scaling sounds like the right general approach. Why was this never sent out?

Quoted 4 lines

Andi and Robin had some
back-and-forth about Andi's patch that I haven't delved into yet, but getting that worked out seems like a better overall approach. I had an offline chat with Tianyu, and he would agree as well.

Where was this discussion?

#6 lore raw

RE: [RFC PATCH V2 1/2] swiotlb: Add Child IO TLB mem support

From: [email protected] <[email protected]> Sent: Tuesday, May 31, 2022 12:17 AM

Quoted 32 lines · Michael Kelley (LINUX)
Quoted 11 lines

B) The contents of the memory buffer must transition between encrypted and not encrypted. The hardware doesn't provide any mechanism to do such a transition "in place". The only way to transition is for the CPU to copy the contents between an encrypted area and an unencrypted area of memory.

Because of (B), we're stuck needing bounce buffers. There's no way to avoid them with the current hardware. Tianyu also pointed out not wanting to expose uninitialized guest memory to the host, so, for example, sharing a read buffer with the host requires that it first be initialized to zero.

Ok, B is a deal breaker. I just brought this in because I've received review comments that state bouncing is just the easiest option for now and we could map it into the hypervisor in the future. But at least for SEV that does not seem like an option without hardware changes.

Quoted 11 lines

We should reset and make sure we agree on the top-level approach.
1) We want general scalability improvements to swiotlb. These improvements should scale to high CPUs counts (> 100) and for multiple NUMA nodes.
2) Drivers should not require any special knowledge of swiotlb to benefit from the improvements. No new swiotlb APIs should be need to be used by drivers -- the swiotlb scalability improvements should be transparent.
3) The scalability improvements should not be based on device boundaries since a single device may have multiple channels doing bounce buffering on multiple CPUs in parallel.

Quoted 8 lines

Agreed to all counts.

Quoted 3 lines

The patch from Andi Kleen [3] (not submitted upstream, but referenced by Tianyu as the basis for his patches) seems like a good starting point for meeting the top-level approach.

Yes, I think doing per-cpu and/or per-node scaling sounds like the right general approach. Why was this never sent out?

I'll defer to Andi on what his thinking/plan is for this patch.

Quoted 7 lines
Quoted 5 lines

Andi and Robin had some
back-and-forth about Andi's patch that I haven't delved into yet, but getting that worked out seems like a better overall approach. I had an offline chat with Tianyu, and he would agree as well.

Where was this discussion?

I was just referring to this thread that you are already on:
https://lore.kernel.org/lkml/[email protected]/

Michael

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#7 lore raw

RFC PATCH v2 2/2 Swiotlb: Add device bounce buffer allocation interface

In reply to Tianyu Lan

From: Tianyu Lan <[email protected]>

In SEV/TDX Confidential VM, device DMA transaction needs use swiotlb bounce buffer to share data with host/hypervisor. The swiotlb spinlock introduces overhead among devices if they share io tlb mem. Avoid such issue, introduce swiotlb_device_allocate() to allocate device bounce buffer from default io tlb pool and set up child IO tlb mem for queue bounce buffer allocaton according input queue number. Device may have multi io queues and setting up the same number of child io tlb mem may help to resolve spinlock overhead among queues.

Introduce IO TLB Block unit(2MB) concepts to allocate big bounce buffer from default pool for devices. IO TLB segment(256k) is too small.

---
 include/linux/swiotlb.h |  35 +++++++-
 kernel/dma/swiotlb.c    | 195 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 225 insertions(+), 5 deletions(-)
include/linux/swiotlb.h +34 -1
index 4a3f6a7b4b7e..efd29e884fd7 100644
@@ -31,6 +31,14 @@ struct scatterlist;
 #define IO_TLB_SHIFT 11
 #define IO_TLB_SIZE (1 << IO_TLB_SHIFT)
 
+/*
+ * IO TLB BLOCK UNIT as device bounce buffer allocation unit.
+ * This allows device allocates bounce buffer from default io
+ * tlb pool.
+ */
+#define IO_TLB_BLOCKSIZE   (8 * IO_TLB_SEGSIZE)
+#define IO_TLB_BLOCK_UNIT  (IO_TLB_BLOCKSIZE << IO_TLB_SHIFT)
+
 /* default to 64MB */
 #define IO_TLB_DEFAULT_SIZE (64UL<<20)
 
@@ -89,9 +97,11 @@ extern enum swiotlb_force swiotlb_force;
  * @late_alloc:	%true if allocated using the page allocator
  * @force_bounce: %true if swiotlb bouncing is forced
  * @for_alloc:  %true if the pool is used for memory allocation
- * @child_nslot:The number of IO TLB slot in the child IO TLB mem.
  * @num_child:  The child io tlb mem number in the pool.
+ * @child_nslot:The number of IO TLB slot in the child IO TLB mem.
+ * @child_nblock:The number of IO TLB block in the child IO TLB mem.
  * @child_start:The child index to start searching in the next round.
+ * @block_start:The block index to start searching in the next round.
  */
 struct io_tlb_mem {
 	phys_addr_t start;
@@ -107,8 +117,16 @@ struct io_tlb_mem {
 	bool for_alloc;
 	unsigned int num_child;
 	unsigned int child_nslot;
+	unsigned int child_nblock;
 	unsigned int child_start;
+	unsigned int block_index;
 	struct io_tlb_mem *child;
+	struct io_tlb_mem *parent;
+	struct io_tlb_block {
+		size_t alloc_size;
+		unsigned long start_slot;
+		unsigned int list;
+	} *block;
 	struct io_tlb_slot {
 		phys_addr_t orig_addr;
 		size_t alloc_size;
@@ -137,6 +155,10 @@ unsigned int swiotlb_max_segment(void);
 size_t swiotlb_max_mapping_size(struct device *dev);
 bool is_swiotlb_active(struct device *dev);
 void __init swiotlb_adjust_size(unsigned long size);
+int swiotlb_device_allocate(struct device *dev,
+			    unsigned int area_num,
+			    unsigned long size);
+void swiotlb_device_free(struct device *dev);
 #else
 static inline void swiotlb_init(bool addressing_limited, unsigned int flags)
 {
@@ -169,6 +191,17 @@ static inline bool is_swiotlb_active(struct device *dev)
 static inline void swiotlb_adjust_size(unsigned long size)
 {
 }
+
+void swiotlb_device_free(struct device *dev)
+{
+}
+
+int swiotlb_device_allocate(struct device *dev,
+			    unsigned int area_num,
+			    unsigned long size)
+{
+	return -ENOMEM;
+}
 #endif /* CONFIG_SWIOTLB */
 
 extern void swiotlb_print_info(void);
kernel/dma/swiotlb.c +191 -4

268 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#8 lore raw
In reply to Tianyu Lan
Quoted 14 lines · Tianyu Lan

From: Tianyu Lan <[email protected]>

Traditionally swiotlb was not performance critical because it was only used for slow devices. But in some setups, like TDX/SEV confidential guests, all IO has to go through swiotlb. Currently swiotlb only has a single lock. Under high IO load with multiple CPUs this can lead to significant lock contention on the swiotlb lock.

This patch adds child IO TLB mem support to resolve spinlock overhead among device's queues. Each device may allocate IO tlb mem and setup child IO TLB mem according to queue number. The number child IO tlb mem maybe set up equal with device queue number and this helps to resolve swiotlb spinlock overhead among devices and queues.

Quoted 4 lines

Patch 2 introduces IO TLB Block concepts and swiotlb_device_allocate() API to allocate per-device swiotlb bounce buffer. The new API Accepts queue number as the number of child IO TLB mem to set up device's IO TLB mem.

Gentile ping...

Thanks.

Quoted 9 lines

Tianyu Lan (2):
swiotlb: Add Child IO TLB mem support
Swiotlb: Add device bounce buffer allocation interface

  include/linux/swiotlb.h |  40 ++++++
  kernel/dma/swiotlb.c    | 290 ++++++++++++++++++++++++++++++++++++++--
  2 files changed, 317 insertions(+), 13 deletions(-)