2022-06-09 01:04:37

by Dongli Zhang

[permalink] [raw]
Subject: [PATCH RFC v1 7/7] swiotlb: fix the slot_addr() overflow

Since the type of swiotlb slot index is a signed integer, the
"((idx) << IO_TLB_SHIFT)" will returns incorrect value. As a result, the
slot_addr() returns a value which is smaller than the expected one.

E.g., the 'tlb_addr' generated in swiotlb_tbl_map_single() may return a
value smaller than the expected one. As a result, the swiotlb_bounce()
will access a wrong swiotlb slot.

Cc: Konrad Wilk <[email protected]>
Cc: Joe Jin <[email protected]>
Signed-off-by: Dongli Zhang <[email protected]>
---
kernel/dma/swiotlb.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index 0dcdd25ea95d..c64e557de55c 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -531,7 +531,8 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size
}
}

-#define slot_addr(start, idx) ((start) + ((idx) << IO_TLB_SHIFT))
+#define slot_addr(start, idx) ((start) + \
+ (((unsigned long)idx) << IO_TLB_SHIFT))

/*
* Carefully handle integer overflow which can occur when boundary_mask == ~0UL.
--
2.17.1


2022-06-09 05:48:58

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH RFC v1 7/7] swiotlb: fix the slot_addr() overflow

On Wed, Jun 08, 2022 at 05:55:53PM -0700, Dongli Zhang wrote:
> +#define slot_addr(start, idx) ((start) + \
> + (((unsigned long)idx) << IO_TLB_SHIFT))

Please just convert it to an inline function.