Skip to content

Commit d440148

Browse files
committed
tegra210-adma: fix 32-bit x86 build
The Tegra210 Audio DMA controller driver did a plain divide: page_no = (res_page->start - res_base->start) / cdata->ch_base_offset; which causes problems on 32-bit x86 configurations that have 64-bit resource sizes: x86_64-linux-ld: drivers/dma/tegra210-adma.o: in function `tegra_adma_probe': tegra210-adma.c:(.text+0x1322): undefined reference to `__udivdi3' because gcc doesn't generate the trivial code for a 64-by-32 divide, turning it into a function call to do a full 64-by-64 divide. And the kernel intentionally doesn't provide that helper function, because 99% of the time all you want is the narrower version. Of course, tegra210 is a 64-bit architecture and the 32-bit x86 build is purely for build testing, so this really is just about build coverage failure. But build coverage is good. Side note: div_u64() would be suboptimal if you actually have a 32-bit resource_t, so our "helper" for divides are admittedly making it harder than it should be to generate good code for all the possible cases. At some point, I'll consider 32-bit x86 so entirely legacy that I can't find it in myself to care any more, and we'll just add the __udivdi3 library function. But for now, the right thing to do is to use "div_u64()" to show that you know that you are doing the simpler divide with a 32-bit number. And the build error enforces that. While fixing the build issue, also check for division-by-zero, and for overflow. Which hopefully cannot happen on real production hardware, but the value of 'ch_base_offset' can definitely be zero in other places. Reported-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 6452fea commit d440148

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

drivers/dma/tegra210-adma.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ static int tegra_adma_probe(struct platform_device *pdev)
887887
const struct tegra_adma_chip_data *cdata;
888888
struct tegra_adma *tdma;
889889
struct resource *res_page, *res_base;
890-
int ret, i, page_no;
890+
int ret, i;
891891

892892
cdata = of_device_get_match_data(&pdev->dev);
893893
if (!cdata) {
@@ -914,9 +914,20 @@ static int tegra_adma_probe(struct platform_device *pdev)
914914

915915
res_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "global");
916916
if (res_base) {
917-
page_no = (res_page->start - res_base->start) / cdata->ch_base_offset;
918-
if (page_no <= 0)
917+
resource_size_t page_offset, page_no;
918+
unsigned int ch_base_offset;
919+
920+
if (res_page->start < res_base->start)
921+
return -EINVAL;
922+
page_offset = res_page->start - res_base->start;
923+
ch_base_offset = cdata->ch_base_offset;
924+
if (!ch_base_offset)
919925
return -EINVAL;
926+
927+
page_no = div_u64(page_offset, ch_base_offset);
928+
if (!page_no || page_no > INT_MAX)
929+
return -EINVAL;
930+
920931
tdma->ch_page_no = page_no - 1;
921932
tdma->base_addr = devm_ioremap_resource(&pdev->dev, res_base);
922933
if (IS_ERR(tdma->base_addr))

0 commit comments

Comments
 (0)