Skip to contentSkip to conversation

Add an option for devices to skip SWIOTLB pre-copy on read.

RFC PATCH In review Jianxiong Gao 5 participants 9 messages Feb 4, 2025 – Feb 5, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Add an option for devices to skip SWIOTLB pre-copy on read.

Removes an extra memory copy that occurs during IO read operations through the SWIOTLB. During high throughput read workloads, this extra copy is causing a lot of stress on the SWIOTLB.

With high performance devices, for example NVMe devices, the device will be overwriting the entire buffer. In such cases the entire pre-copy is redundent, only to slow down the overall bounce buffering.

We propose to add a full_buffer_write flag to the device_dma_parameters flag. When the flag is set the pre-copy can be omitted to boost performance.

Jianxiong Gao (3):
add full_buffer_write flag to struct device_dma_parameters skip swiotlb pre copy if the device does full buffer write. set full_buffer_write for nvme devices.

 drivers/nvme/host/pci.c     |  1 +
 include/linux/device.h      |  1 +
 include/linux/dma-mapping.h | 15 +++++++++++++++
 kernel/dma/swiotlb.c        |  3 ++-
 4 files changed, 19 insertions(+), 1 deletion(-)
Signature · 1 line
2.48.1.362.g079036d154-goog
#1 lore raw

RFC PATCH 1/3 add full_buffer_write flag to struct device_dma_parameters

When devices write to a buffer, some devices are guaranteed to overwrite the entire buffer. These devices may benefit from such behaviors by reducing the need to pre-condition the original buffer. For example when bouncing data through swiotlb, the buffer is forced to be synced before the device writes to it. For devices that we know for sure overwrites the entire buffer, this flag can be utilized to eliminate the extra copy on every IO bounced through the swiotlb.

---
 include/linux/device.h      |  1 +
 include/linux/dma-mapping.h | 15 +++++++++++++++
 2 files changed, 16 insertions(+)
include/linux/device.h +1 -0
index 80a5b3268986..003007ad6ad3 100644
@@ -460,6 +460,7 @@ struct device_dma_parameters {
 	 * a low level driver may set these to teach IOMMU code about
 	 * sg limitations.
 	 */
+	bool full_buffer_write;
 	unsigned int max_segment_size;
 	unsigned int min_align_mask;
 	unsigned long segment_boundary_mask;
include/linux/dma-mapping.h +15 -0
index b79925b1c433..e93b909865d3 100644
@@ -513,6 +513,21 @@ static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
 	return dma_set_mask_and_coherent(dev, mask);
 }
 
+static inline bool dma_is_full_buffer_write(struct device *dev)
+{
+	if (dev->dma_parms)
+		return dev->dma_parms->full_buffer_write;
+	return false;
+}
+
+static inline int dma_set_full_buffer_write(struct device *dev, bool full_buffer_write)
+{
+	if (WARN_ON_ONCE(!dev->dma_parms))
+		return -EIO;
+	dev->dma_parms->full_buffer_write = full_buffer_write;
+	return 0;
+}
+
 static inline unsigned int dma_get_max_seg_size(struct device *dev)
 {
 	if (dev->dma_parms && dev->dma_parms->max_segment_size)
Signature · 1 line
2.48.1.362.g079036d154-goog
#2 lore raw

Re: [RFC PATCH 1/3] add full_buffer_write flag to struct device_dma_parameters

Quoted 8 lines · Jianxiong Gao

When devices write to a buffer, some devices are guaranteed to overwrite the entire buffer. These devices may benefit from such behaviors by reducing the need to pre-condition the original buffer. For example when bouncing data through swiotlb, the buffer is forced to be synced before the device writes to it. For devices that we know for sure overwrites the entire buffer, this flag can be utilized to eliminate the extra copy on every IO bounced through the swiotlb.

...

include/linux/device.h struct device_dma_parameters { · line 466

Quoted 9 lines

struct device_dma_parameters {

include/linux/device.h +1 -0
 	 * a low level driver may set these to teach IOMMU code about
 	 * sg limitations.
 	 */
+	bool full_buffer_write;
 	unsigned int max_segment_size;
 	unsigned int min_align_mask;
 	unsigned long segment_boundary_mask;

Have you run pahole? Please, share the results.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

RFC PATCH 2/3 skip swiotlb pre copy if the device does full buffer write.

In reply to Jianxiong Gao

In cases that the device is known to do a full buffer write, we can skip the pre copy of the swiotlb buffer.

---
 kernel/dma/swiotlb.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/dma/swiotlb.c +2 -1
index abcf3fa63a56..12124d4fd44f 100644
@@ -1436,7 +1436,8 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
 	 * hardware behavior.  Use of swiotlb is supposed to be transparent,
 	 * i.e. swiotlb must not corrupt memory by clobbering unwritten bytes.
 	 */
-	swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE, pool);
+	if ((dir == DMA_TO_DEVICE) || !dma_is_full_buffer_write(dev))
+		swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE, pool);
 	return tlb_addr;
 }
 
Signature · 1 line
2.48.1.362.g079036d154-goog
#4 lore raw

RFC PATCH 3/3 set full_buffer_write for nvme devices.

In reply to Jianxiong Gao

NVMe dma operations always overwrite the whole dma buffer passed to the device. Making use the newly introduced full_buffer_write flag so in cases swiotlb is used to bounce the buffer, we no longer need to do the extra copy on preparing the buffer.

---
 drivers/nvme/host/pci.c | 1 +
 1 file changed, 1 insertion(+)
drivers/nvme/host/pci.c +1 -0
index 278bed4e35bb..8fb6a0a87202 100644
@@ -3209,6 +3209,7 @@ static struct nvme_dev *nvme_pci_alloc_dev(struct pci_dev *pdev,
 		dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
 	else
 		dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
+	dma_set_full_buffer_write(&pdev->dev, true);
 	dma_set_min_align_mask(&pdev->dev, NVME_CTRL_PAGE_SIZE - 1);
 	dma_set_max_seg_size(&pdev->dev, 0xffffffff);
 
Signature · 1 line
2.48.1.362.g079036d154-goog
#5 lore raw
In reply to Jianxiong Gao
Quoted 7 lines · Jianxiong Gao

Removes an extra memory copy that occurs during IO read operations through the SWIOTLB. During high throughput read workloads, this extra copy is causing a lot of stress on the SWIOTLB.

With high performance devices, for example NVMe devices, the device will be overwriting the entire buffer.

Is this really guaranteed? I can imagine surprise power cut or hotplug event, for example, just in the middle of the transfer.

Quoted 6 lines

In such cases the entire pre-copy is redundent, only to slow down the overall bounce buffering.

We propose to add a full_buffer_write flag to the device_dma_parameters flag. When the flag is set the pre-copy can be omitted to boost performance.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 7 lines · Jianxiong Gao

Removes an extra memory copy that occurs during IO read operations through the SWIOTLB. During high throughput read workloads, this extra copy is causing a lot of stress on the SWIOTLB.

With high performance devices, for example NVMe devices, the device will be overwriting the entire buffer.

Is this really guaranteed? I can imagine surprise power cut or hotplug event, for example, just in the middle of the transfer.

Many command can return less data than originally mapped. Get Log Page is an example that comes to mind that is heavily used that way.

maintainer
#7 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Jianxiong Gao

Removes an extra memory copy that occurs during IO read operations through the SWIOTLB. During high throughput read workloads, this extra copy is causing a lot of stress on the SWIOTLB.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

With high performance devices, for example NVMe devices, the device will be overwriting the entire buffer.

Is this really guaranteed? I can imagine surprise power cut or hotplug event, for example, just in the middle of the transfer.

Many command can return less data than originally mapped. Get Log Page is an example that comes to mind that is heavily used that way.

It's also easy enough for a user to "mistakenly" request more memory than the device will transfer. The safest thing is to clear any contents that may get copied back to the user.

reviewer
#8 lore raw
In reply to Jianxiong Gao
Quoted 5 lines · Jianxiong Gao

Removes an extra memory copy that occurs during IO read operations through the SWIOTLB. During high throughput read workloads, this extra copy is causing a lot of stress on the SWIOTLB.

Quoted 8 lines

With high performance devices, for example NVMe devices, the device will be overwriting the entire buffer. In such cases the entire pre-copy is redundent, only to slow down the overall bounce buffering.

We propose to add a full_buffer_write flag to the device_dma_parameters flag. When the flag is set the pre-copy can be omitted to boost performance.

No. We already went through this a couple of weeks ago[1]. It's not about what the driver intends the device to do, it's about what happens if for whatever reason it then *doesn't* do that.

Thanks,
Robin.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 12 lines

Jianxiong Gao (3):
add full_buffer_write flag to struct device_dma_parameters skip swiotlb pre copy if the device does full buffer write. set full_buffer_write for nvme devices.

  drivers/nvme/host/pci.c     |  1 +
  include/linux/device.h      |  1 +
  include/linux/dma-mapping.h | 15 +++++++++++++++
  kernel/dma/swiotlb.c        |  3 ++-
  4 files changed, 19 insertions(+), 1 deletion(-)