Skip to content

Commit e8068f2

Browse files
mhklinuxChristoph Hellwig
authored andcommitted
swiotlb: fix swiotlb_bounce() to do partial sync's correctly
In current code, swiotlb_bounce() may do partial sync's correctly in some circumstances, but may incorrectly fail in other circumstances. The failure cases require both of these to be true: 1) swiotlb_align_offset() returns a non-zero "offset" value 2) the tlb_addr of the partial sync area points into the first "offset" bytes of the _second_ or subsequent swiotlb slot allocated for the mapping Code added in commit 868c9dd ("swiotlb: add overflow checks to swiotlb_bounce") attempts to WARN on the invalid case where tlb_addr points into the first "offset" bytes of the _first_ allocated slot. But there's no way for swiotlb_bounce() to distinguish the first slot from the second and subsequent slots, so the WARN can be triggered incorrectly when #2 above is true. Related, current code calculates an adjustment to the orig_addr stored in the swiotlb slot. The adjustment compensates for the difference in the tlb_addr used for the partial sync vs. the tlb_addr for the full mapping. The adjustment is stored in the local variable tlb_offset. But when #1 and #2 above are true, it's valid for this adjustment to be negative. In such case the arithmetic to adjust orig_addr produces the wrong result due to tlb_offset being declared as unsigned. Fix these problems by removing the over-constraining validations added in 868c9dd. Change the declaration of tlb_offset to be signed instead of unsigned so the adjustment arithmetic works correctly. Tested with a test-only hack to how swiotlb_tbl_map_single() calls swiotlb_bounce(). Instead of calling swiotlb_bounce() just once for the entire mapped area, do a loop with each iteration doing only a 128 byte partial sync until the entire mapped area is sync'ed. Then with swiotlb=force on the kernel boot line, run a variety of raw disk writes followed by read and verification of all bytes of the written data. The storage device has DMA min_align_mask set, and the writes are done with a variety of original buffer memory address alignments and overall buffer sizes. For many of the combinations, current code triggers the WARN statements, or the data verification fails. With the fixes, no WARNs occur and all verifications pass. Fixes: 5f89468 ("swiotlb: manipulate orig_addr when tlb_addr has offset") Fixes: 868c9dd ("swiotlb: add overflow checks to swiotlb_bounce") Signed-off-by: Michael Kelley <[email protected]> Dominique Martinet <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]>
1 parent af13356 commit e8068f2

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

kernel/dma/swiotlb.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -863,27 +863,23 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size
863863
size_t alloc_size = mem->slots[index].alloc_size;
864864
unsigned long pfn = PFN_DOWN(orig_addr);
865865
unsigned char *vaddr = mem->vaddr + tlb_addr - mem->start;
866-
unsigned int tlb_offset, orig_addr_offset;
866+
int tlb_offset;
867867

868868
if (orig_addr == INVALID_PHYS_ADDR)
869869
return;
870870

871-
tlb_offset = tlb_addr & (IO_TLB_SIZE - 1);
872-
orig_addr_offset = swiotlb_align_offset(dev, 0, orig_addr);
873-
if (tlb_offset < orig_addr_offset) {
874-
dev_WARN_ONCE(dev, 1,
875-
"Access before mapping start detected. orig offset %u, requested offset %u.\n",
876-
orig_addr_offset, tlb_offset);
877-
return;
878-
}
879-
880-
tlb_offset -= orig_addr_offset;
881-
if (tlb_offset > alloc_size) {
882-
dev_WARN_ONCE(dev, 1,
883-
"Buffer overflow detected. Allocation size: %zu. Mapping size: %zu+%u.\n",
884-
alloc_size, size, tlb_offset);
885-
return;
886-
}
871+
/*
872+
* It's valid for tlb_offset to be negative. This can happen when the
873+
* "offset" returned by swiotlb_align_offset() is non-zero, and the
874+
* tlb_addr is pointing within the first "offset" bytes of the second
875+
* or subsequent slots of the allocated swiotlb area. While it's not
876+
* valid for tlb_addr to be pointing within the first "offset" bytes
877+
* of the first slot, there's no way to check for such an error since
878+
* this function can't distinguish the first slot from the second and
879+
* subsequent slots.
880+
*/
881+
tlb_offset = (tlb_addr & (IO_TLB_SIZE - 1)) -
882+
swiotlb_align_offset(dev, 0, orig_addr);
887883

888884
orig_addr += tlb_offset;
889885
alloc_size -= tlb_offset;

0 commit comments

Comments
 (0)