Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

+14 -3
+14 -3
drivers/dma/tegra210-adma.c
··· 887 887 const struct tegra_adma_chip_data *cdata; 888 888 struct tegra_adma *tdma; 889 889 struct resource *res_page, *res_base; 890 - int ret, i, page_no; 890 + int ret, i; 891 891 892 892 cdata = of_device_get_match_data(&pdev->dev); 893 893 if (!cdata) { ··· 914 914 915 915 res_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "global"); 916 916 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) 919 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) 925 + 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 + 920 931 tdma->ch_page_no = page_no - 1; 921 932 tdma->base_addr = devm_ioremap_resource(&pdev->dev, res_base); 922 933 if (IS_ERR(tdma->base_addr))