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

Linux-MTD Mailing List linux-mtd at lists.infradead.org
Tue May 15 05:59:01 PDT 2018


Gitweb:     http://git.infradead.org/?p=mtd-2.6.git;a=commit;h=9f825e74d761c13b0cfaa5f65344d64ff970e252
Commit:     9f825e74d761c13b0cfaa5f65344d64ff970e252
Parent:     90d617633368ab97a2c7571c6e66dad54f39228d
Author:     Geert Uytterhoeven <geert at linux-m68k.org>
AuthorDate: Mon May 14 12:49:37 2018 +0200
Committer:  Boris Brezillon <boris.brezillon at bootlin.com>
CommitDate: Tue May 15 14:40:34 2018 +0200

    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 at linux-m68k.org>
    Signed-off-by: Boris Brezillon <boris.brezillon at bootlin.com>
---
 include/linux/mtd/rawnand.h | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h
index 5dad59b31244..17c919436f48 100644
--- a/include/linux/mtd/rawnand.h
+++ b/include/linux/mtd/rawnand.h
@@ -867,12 +867,18 @@ struct nand_op_instr {
  * tBERS (during an erase) which all of them are u64 values that cannot be
  * divided by usual kernel macros and must be handled with the special
  * DIV_ROUND_UP_ULL() macro.
+ *
+ * Cast to type of dividend is needed here to guarantee that the result won't
+ * be an unsigned long long when the dividend is an unsigned long (or smaller),
+ * which is what the compiler does when it sees ternary operator with 2
+ * different return types (picks the largest type to make sure there's no
+ * loss).
  */
-#define __DIVIDE(dividend, divisor) ({					\
-	sizeof(dividend) == sizeof(u32) ?				\
-		DIV_ROUND_UP(dividend, divisor) :			\
-		DIV_ROUND_UP_ULL(dividend, divisor);			\
-		})
+#define __DIVIDE(dividend, divisor) ({						\
+	(__typeof__(dividend))(sizeof(dividend) <= sizeof(unsigned long) ?	\
+			       DIV_ROUND_UP(dividend, divisor) :		\
+			       DIV_ROUND_UP_ULL(dividend, divisor)); 		\
+	})
 #define PSEC_TO_NSEC(x) __DIVIDE(x, 1000)
 #define PSEC_TO_MSEC(x) __DIVIDE(x, 1000000000)
 



More information about the linux-mtd-cvs mailing list