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

mtd: rawnand: Fix return type of __DIVIDE() when called with 32-bit

The __DIVIDE() macro checks whether it is called with a 32-bit or 64-bit
dividend, to select the appropriate divide-and-round-up routine.
As the check uses the ternary operator, the result will always be
promoted to a type that can hold both results, i.e. unsigned long long.

When using this result in a division on a 32-bit system, this may lead
to link errors like:

ERROR: "__udivdi3" [drivers/mtd/nand/raw/nand.ko] undefined!

Fix this by casting the result of the division to the type of the
dividend.

Fixes: 8878b126df769831 ("mtd: nand: add ->exec_op() implementation")
Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>

authored by

Geert Uytterhoeven and committed by
Boris Brezillon
9f825e74 90d61763

+11 -5
+11 -5
include/linux/mtd/rawnand.h
··· 867 867 * tBERS (during an erase) which all of them are u64 values that cannot be 868 868 * divided by usual kernel macros and must be handled with the special 869 869 * DIV_ROUND_UP_ULL() macro. 870 + * 871 + * Cast to type of dividend is needed here to guarantee that the result won't 872 + * be an unsigned long long when the dividend is an unsigned long (or smaller), 873 + * which is what the compiler does when it sees ternary operator with 2 874 + * different return types (picks the largest type to make sure there's no 875 + * loss). 870 876 */ 871 - #define __DIVIDE(dividend, divisor) ({ \ 872 - sizeof(dividend) == sizeof(u32) ? \ 873 - DIV_ROUND_UP(dividend, divisor) : \ 874 - DIV_ROUND_UP_ULL(dividend, divisor); \ 875 - }) 877 + #define __DIVIDE(dividend, divisor) ({ \ 878 + (__typeof__(dividend))(sizeof(dividend) <= sizeof(unsigned long) ? \ 879 + DIV_ROUND_UP(dividend, divisor) : \ 880 + DIV_ROUND_UP_ULL(dividend, divisor)); \ 881 + }) 876 882 #define PSEC_TO_NSEC(x) __DIVIDE(x, 1000) 877 883 #define PSEC_TO_MSEC(x) __DIVIDE(x, 1000000000) 878 884