Skip to content

Commit 1395706

Browse files
Petr TesarikChristoph Hellwig
authored andcommitted
swiotlb: search the software IO TLB only if the device makes use of it
Skip searching the software IO TLB if a device has never used it, making sure these devices are not affected by the introduction of multiple IO TLB memory pools. Additional memory barrier is required to ensure that the new value of the flag is visible to other CPUs after mapping a new bounce buffer. For efficiency, the flag check should be inlined, and then the memory barrier must be moved to is_swiotlb_buffer(). However, it can replace the existing barrier in swiotlb_find_pool(), because all callers use is_swiotlb_buffer() first to verify that the buffer address belongs to the software IO TLB. Signed-off-by: Petr Tesarik <petr.tesarik.ext@huawei.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent 1aaa736 commit 1395706

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

include/linux/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ struct device_physical_location {
628628
* @dma_io_tlb_mem: Software IO TLB allocator. Not for driver use.
629629
* @dma_io_tlb_pools: List of transient swiotlb memory pools.
630630
* @dma_io_tlb_lock: Protects changes to the list of active pools.
631+
* @dma_uses_io_tlb: %true if device has used the software IO TLB.
631632
* @archdata: For arch-specific additions.
632633
* @of_node: Associated device tree node.
633634
* @fwnode: Associated device node supplied by platform firmware.
@@ -737,6 +738,7 @@ struct device {
737738
#ifdef CONFIG_SWIOTLB_DYNAMIC
738739
struct list_head dma_io_tlb_pools;
739740
spinlock_t dma_io_tlb_lock;
741+
bool dma_uses_io_tlb;
740742
#endif
741743
/* arch specific additions */
742744
struct dev_archdata archdata;

include/linux/swiotlb.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,13 @@ static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
172172
if (!mem)
173173
return false;
174174

175-
if (IS_ENABLED(CONFIG_SWIOTLB_DYNAMIC))
175+
if (IS_ENABLED(CONFIG_SWIOTLB_DYNAMIC)) {
176+
/* Pairs with smp_wmb() in swiotlb_find_slots() and
177+
* swiotlb_dyn_alloc(), which modify the RCU lists.
178+
*/
179+
smp_rmb();
176180
return swiotlb_find_pool(dev, paddr);
181+
}
177182
return paddr >= mem->defpool.start && paddr < mem->defpool.end;
178183
}
179184

kernel/dma/swiotlb.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ static void swiotlb_dyn_alloc(struct work_struct *work)
730730

731731
add_mem_pool(mem, pool);
732732

733-
/* Pairs with smp_rmb() in swiotlb_find_pool(). */
733+
/* Pairs with smp_rmb() in is_swiotlb_buffer(). */
734734
smp_wmb();
735735
}
736736

@@ -764,11 +764,6 @@ struct io_tlb_pool *swiotlb_find_pool(struct device *dev, phys_addr_t paddr)
764764
struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
765765
struct io_tlb_pool *pool;
766766

767-
/* Pairs with smp_wmb() in swiotlb_find_slots() and
768-
* swiotlb_dyn_alloc(), which modify the RCU lists.
769-
*/
770-
smp_rmb();
771-
772767
rcu_read_lock();
773768
list_for_each_entry_rcu(pool, &mem->pools, node) {
774769
if (paddr >= pool->start && paddr < pool->end)
@@ -813,6 +808,7 @@ void swiotlb_dev_init(struct device *dev)
813808
#ifdef CONFIG_SWIOTLB_DYNAMIC
814809
INIT_LIST_HEAD(&dev->dma_io_tlb_pools);
815810
spin_lock_init(&dev->dma_io_tlb_lock);
811+
dev->dma_uses_io_tlb = false;
816812
#endif
817813
}
818814

@@ -1157,9 +1153,11 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr,
11571153
list_add_rcu(&pool->node, &dev->dma_io_tlb_pools);
11581154
spin_unlock_irqrestore(&dev->dma_io_tlb_lock, flags);
11591155

1160-
/* Pairs with smp_rmb() in swiotlb_find_pool(). */
1161-
smp_wmb();
11621156
found:
1157+
dev->dma_uses_io_tlb = true;
1158+
/* Pairs with smp_rmb() in is_swiotlb_buffer() */
1159+
smp_wmb();
1160+
11631161
*retpool = pool;
11641162
return index;
11651163
}

0 commit comments

Comments
 (0)