Skip to content

Commit f8e71c6

Browse files
Jianxiong Gaogregkh
authored andcommitted
swiotlb: respect min_align_mask
commit: 1f221a0 swiotlb: respect min_align_mask Respect the min_align_mask in struct device_dma_parameters in swiotlb. There are two parts to it: 1) for the lower bits of the alignment inside the io tlb slot, just extent the size of the allocation and leave the start of the slot empty 2) for the high bits ensure we find a slot that matches the high bits of the alignment to avoid wasting too much memory Based on an earlier patch from Jianxiong Gao <jxgao@google.com>. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Jianxiong Gao <jxgao@google.com> Tested-by: Jianxiong Gao <jxgao@google.com> Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Signed-off-by: Jianxiong Gao <jxgao@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 85a5a68 commit f8e71c6

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

‎kernel/dma/swiotlb.c‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,14 @@ static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
454454

455455
#define slot_addr(start, idx) ((start) + ((idx) << IO_TLB_SHIFT))
456456

457+
/*
458+
* Return the offset into a iotlb slot required to keep the device happy.
459+
*/
460+
static unsigned int swiotlb_align_offset(struct device *dev, u64 addr)
461+
{
462+
return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1);
463+
}
464+
457465
/*
458466
* Carefully handle integer overflow which can occur when boundary_mask == ~0UL.
459467
*/
@@ -475,31 +483,42 @@ static unsigned int wrap_index(unsigned int index)
475483
* Find a suitable number of IO TLB entries size that will fit this request and
476484
* allocate a buffer from that IO TLB pool.
477485
*/
478-
static int find_slots(struct device *dev, size_t alloc_size)
486+
static int find_slots(struct device *dev, phys_addr_t orig_addr,
487+
size_t alloc_size)
479488
{
480489
unsigned long boundary_mask = dma_get_seg_boundary(dev);
481490
dma_addr_t tbl_dma_addr =
482491
phys_to_dma_unencrypted(dev, io_tlb_start) & boundary_mask;
483492
unsigned long max_slots = get_max_slots(boundary_mask);
484-
unsigned int nslots = nr_slots(alloc_size), stride = 1;
493+
unsigned int iotlb_align_mask =
494+
dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1);
495+
unsigned int nslots = nr_slots(alloc_size), stride;
485496
unsigned int index, wrap, count = 0, i;
486497
unsigned long flags;
487498

488499
BUG_ON(!nslots);
489500

490501
/*
491-
* For mappings greater than or equal to a page, we limit the stride
492-
* (and hence alignment) to a page size.
502+
* For mappings with an alignment requirement don't bother looping to
503+
* unaligned slots once we found an aligned one. For allocations of
504+
* PAGE_SIZE or larger only look for page aligned allocations.
493505
*/
506+
stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1;
494507
if (alloc_size >= PAGE_SIZE)
495-
stride <<= (PAGE_SHIFT - IO_TLB_SHIFT);
508+
stride = max(stride, stride << (PAGE_SHIFT - IO_TLB_SHIFT));
496509

497510
spin_lock_irqsave(&io_tlb_lock, flags);
498511
if (unlikely(nslots > io_tlb_nslabs - io_tlb_used))
499512
goto not_found;
500513

501514
index = wrap = wrap_index(ALIGN(io_tlb_index, stride));
502515
do {
516+
if ((slot_addr(tbl_dma_addr, index) & iotlb_align_mask) !=
517+
(orig_addr & iotlb_align_mask)) {
518+
index = wrap_index(index + 1);
519+
continue;
520+
}
521+
503522
/*
504523
* If we find a slot that indicates we have 'nslots' number of
505524
* contiguous buffers, we allocate the buffers from that slot
@@ -543,6 +562,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
543562
size_t mapping_size, size_t alloc_size,
544563
enum dma_data_direction dir, unsigned long attrs)
545564
{
565+
unsigned int offset = swiotlb_align_offset(dev, orig_addr);
546566
unsigned int index, i;
547567
phys_addr_t tlb_addr;
548568

@@ -558,7 +578,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
558578
return (phys_addr_t)DMA_MAPPING_ERROR;
559579
}
560580

561-
index = find_slots(dev, alloc_size);
581+
index = find_slots(dev, orig_addr, alloc_size + offset);
562582
if (index == -1) {
563583
if (!(attrs & DMA_ATTR_NO_WARN))
564584
dev_warn_ratelimited(dev,
@@ -572,10 +592,10 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
572592
* This is needed when we sync the memory. Then we sync the buffer if
573593
* needed.
574594
*/
575-
for (i = 0; i < nr_slots(alloc_size); i++)
595+
for (i = 0; i < nr_slots(alloc_size + offset); i++)
576596
io_tlb_orig_addr[index + i] = slot_addr(orig_addr, i);
577597

578-
tlb_addr = slot_addr(io_tlb_start, index);
598+
tlb_addr = slot_addr(io_tlb_start, index) + offset;
579599
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
580600
(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
581601
swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_TO_DEVICE);
@@ -590,8 +610,9 @@ void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
590610
enum dma_data_direction dir, unsigned long attrs)
591611
{
592612
unsigned long flags;
593-
int i, count, nslots = nr_slots(alloc_size);
594-
int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
613+
unsigned int offset = swiotlb_align_offset(hwdev, tlb_addr);
614+
int i, count, nslots = nr_slots(alloc_size + offset);
615+
int index = (tlb_addr - offset - io_tlb_start) >> IO_TLB_SHIFT;
595616
phys_addr_t orig_addr = io_tlb_orig_addr[index];
596617

597618
/*

0 commit comments

Comments
 (0)