Skip to content

Commit daa1211

Browse files
committed
Merge tag 'dma-mapping-6.10-2024-05-20' of git://git.infradead.org/users/hch/dma-mapping
Pull dma-mapping updates from Christoph Hellwig: - optimize DMA sync calls when they are no-ops (Alexander Lobakin) - fix swiotlb padding for untrusted devices (Michael Kelley) - add documentation for swiotb (Michael Kelley) * tag 'dma-mapping-6.10-2024-05-20' of git://git.infradead.org/users/hch/dma-mapping: dma: fix DMA sync for drivers not calling dma_set_mask*() xsk: use generic DMA sync shortcut instead of a custom one page_pool: check for DMA sync shortcut earlier page_pool: don't use driver-set flags field directly page_pool: make sure frag API fields don't span between cachelines iommu/dma: avoid expensive indirect calls for sync operations dma: avoid redundant calls for sync operations dma: compile-out DMA sync op calls when not used iommu/dma: fix zeroing of bounce buffer padding used by untrusted devices swiotlb: remove alloc_size argument to swiotlb_tbl_map_single() Documentation/core-api: add swiotlb documentation
2 parents 6e51b4b + a6016aa commit daa1211

File tree

27 files changed

+634
-163
lines changed

27 files changed

+634
-163
lines changed

Documentation/core-api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ more memory-management documentation in Documentation/mm/index.rst.
102102
dma-api-howto
103103
dma-attributes
104104
dma-isa-lpc
105+
swiotlb
105106
mm-api
106107
genalloc
107108
pin_user_pages

Documentation/core-api/swiotlb.rst

Lines changed: 321 additions & 0 deletions
Large diffs are not rendered by default.

drivers/iommu/dma-iommu.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,34 +1152,37 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
11521152
*/
11531153
if (dev_use_swiotlb(dev, size, dir) &&
11541154
iova_offset(iovad, phys | size)) {
1155-
void *padding_start;
1156-
size_t padding_size, aligned_size;
1157-
11581155
if (!is_swiotlb_active(dev)) {
11591156
dev_warn_once(dev, "DMA bounce buffers are inactive, unable to map unaligned transaction.\n");
11601157
return DMA_MAPPING_ERROR;
11611158
}
11621159

11631160
trace_swiotlb_bounced(dev, phys, size);
11641161

1165-
aligned_size = iova_align(iovad, size);
1166-
phys = swiotlb_tbl_map_single(dev, phys, size, aligned_size,
1162+
phys = swiotlb_tbl_map_single(dev, phys, size,
11671163
iova_mask(iovad), dir, attrs);
11681164

11691165
if (phys == DMA_MAPPING_ERROR)
11701166
return DMA_MAPPING_ERROR;
11711167

1172-
/* Cleanup the padding area. */
1173-
padding_start = phys_to_virt(phys);
1174-
padding_size = aligned_size;
1168+
/*
1169+
* Untrusted devices should not see padding areas with random
1170+
* leftover kernel data, so zero the pre- and post-padding.
1171+
* swiotlb_tbl_map_single() has initialized the bounce buffer
1172+
* proper to the contents of the original memory buffer.
1173+
*/
1174+
if (dev_is_untrusted(dev)) {
1175+
size_t start, virt = (size_t)phys_to_virt(phys);
11751176

1176-
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
1177-
(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) {
1178-
padding_start += size;
1179-
padding_size -= size;
1180-
}
1177+
/* Pre-padding */
1178+
start = iova_align_down(iovad, virt);
1179+
memset((void *)start, 0, virt - start);
11811180

1182-
memset(padding_start, 0, padding_size);
1181+
/* Post-padding */
1182+
start = virt + size;
1183+
memset((void *)start, 0,
1184+
iova_align(iovad, start) - start);
1185+
}
11831186
}
11841187

11851188
if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
@@ -1718,7 +1721,8 @@ static size_t iommu_dma_max_mapping_size(struct device *dev)
17181721
}
17191722

17201723
static const struct dma_map_ops iommu_dma_ops = {
1721-
.flags = DMA_F_PCI_P2PDMA_SUPPORTED,
1724+
.flags = DMA_F_PCI_P2PDMA_SUPPORTED |
1725+
DMA_F_CAN_SKIP_SYNC,
17221726
.alloc = iommu_dma_alloc,
17231727
.free = iommu_dma_free,
17241728
.alloc_pages_op = dma_common_alloc_pages,

drivers/net/ethernet/engleder/tsnep_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ static int tsnep_rx_poll_zc(struct tsnep_rx *rx, struct napi_struct *napi,
15871587
length = __le32_to_cpu(entry->desc_wb->properties) &
15881588
TSNEP_DESC_LENGTH_MASK;
15891589
xsk_buff_set_size(entry->xdp, length - ETH_FCS_LEN);
1590-
xsk_buff_dma_sync_for_cpu(entry->xdp, rx->xsk_pool);
1590+
xsk_buff_dma_sync_for_cpu(entry->xdp);
15911591

15921592
/* RX metadata with timestamps is in front of actual data,
15931593
* subtract metadata size to get length of actual data and

drivers/net/ethernet/freescale/dpaa2/dpaa2-xsk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static u32 dpaa2_xsk_run_xdp(struct dpaa2_eth_priv *priv,
5555
xdp_set_data_meta_invalid(xdp_buff);
5656
xdp_buff->rxq = &ch->xdp_rxq;
5757

58-
xsk_buff_dma_sync_for_cpu(xdp_buff, ch->xsk_pool);
58+
xsk_buff_dma_sync_for_cpu(xdp_buff);
5959
xdp_act = bpf_prog_run_xdp(xdp_prog, xdp_buff);
6060

6161
/* xdp.data pointer may have changed */

drivers/net/ethernet/intel/i40e/i40e_xsk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
482482

483483
bi = *i40e_rx_bi(rx_ring, next_to_process);
484484
xsk_buff_set_size(bi, size);
485-
xsk_buff_dma_sync_for_cpu(bi, rx_ring->xsk_pool);
485+
xsk_buff_dma_sync_for_cpu(bi);
486486

487487
if (!first)
488488
first = bi;

drivers/net/ethernet/intel/ice/ice_xsk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ int ice_clean_rx_irq_zc(struct ice_rx_ring *rx_ring, int budget)
878878
ICE_RX_FLX_DESC_PKT_LEN_M;
879879

880880
xsk_buff_set_size(xdp, size);
881-
xsk_buff_dma_sync_for_cpu(xdp, xsk_pool);
881+
xsk_buff_dma_sync_for_cpu(xdp);
882882

883883
if (!first) {
884884
first = xdp;

drivers/net/ethernet/intel/igc/igc_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2812,7 +2812,7 @@ static int igc_clean_rx_irq_zc(struct igc_q_vector *q_vector, const int budget)
28122812
}
28132813

28142814
bi->xdp->data_end = bi->xdp->data + size;
2815-
xsk_buff_dma_sync_for_cpu(bi->xdp, ring->xsk_pool);
2815+
xsk_buff_dma_sync_for_cpu(bi->xdp);
28162816

28172817
res = __igc_xdp_run_prog(adapter, prog, bi->xdp);
28182818
switch (res) {

drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ int ixgbe_clean_rx_irq_zc(struct ixgbe_q_vector *q_vector,
303303
}
304304

305305
bi->xdp->data_end = bi->xdp->data + size;
306-
xsk_buff_dma_sync_for_cpu(bi->xdp, rx_ring->xsk_pool);
306+
xsk_buff_dma_sync_for_cpu(bi->xdp);
307307
xdp_res = ixgbe_run_xdp_zc(adapter, rx_ring, bi->xdp);
308308

309309
if (likely(xdp_res & (IXGBE_XDP_TX | IXGBE_XDP_REDIR))) {

drivers/net/ethernet/mellanox/mlx5/core/en/xsk/rx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq,
270270
/* mxbuf->rq is set on allocation, but cqe is per-packet so set it here */
271271
mxbuf->cqe = cqe;
272272
xsk_buff_set_size(&mxbuf->xdp, cqe_bcnt);
273-
xsk_buff_dma_sync_for_cpu(&mxbuf->xdp, rq->xsk_pool);
273+
xsk_buff_dma_sync_for_cpu(&mxbuf->xdp);
274274
net_prefetch(mxbuf->xdp.data);
275275

276276
/* Possible flows:
@@ -319,7 +319,7 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_linear(struct mlx5e_rq *rq,
319319
/* mxbuf->rq is set on allocation, but cqe is per-packet so set it here */
320320
mxbuf->cqe = cqe;
321321
xsk_buff_set_size(&mxbuf->xdp, cqe_bcnt);
322-
xsk_buff_dma_sync_for_cpu(&mxbuf->xdp, rq->xsk_pool);
322+
xsk_buff_dma_sync_for_cpu(&mxbuf->xdp);
323323
net_prefetch(mxbuf->xdp.data);
324324

325325
prog = rcu_dereference(rq->xdp_prog);

0 commit comments

Comments
 (0)