public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* div64: Rework 64-bit type safety checks in do_div().
@ 2007-12-17  1:48 Paul Mundt
  2007-12-17  3:04 ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Mundt @ 2007-12-17  1:48 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

The current do_div() implementation has a bogus pointer compare to
generate build warnings on mismatch on 32-bit, unfortunately this not
only triggers for size mismatch, but also _any_ type mismatch, even on
reasonable 64-bit values:

In file included from kernel/sched.c:869:
kernel/sched_debug.c: In function 'nsec_high':
kernel/sched_debug.c:38: warning: comparison of distinct pointer types lacks a cast
kernel/sched_debug.c:41: warning: comparison of distinct pointer types lacks a cast
kernel/sched_debug.c: In function 'nsec_low':
kernel/sched_debug.c:51: warning: comparison of distinct pointer types lacks a cast
...

The options are to either 'fix' all callers of do_div() to make sure
they're using a uint64_t explicitly, or to update do_div() to make sure
that the value is 64-bits, regardless of specific type. Currently
everything that uses the generic do_div() causes a warning when using one
of 'u64', 'long long', etc. instead of 'uint64_t'.

Half-assed empirical testing indicates that the number of false positives
far outweighs any benefits of this type of checking:

$ git grep uint64_t | wc -l
947
$ git grep u64 | wc -l
13942

In short, screw uint64_t and its fan club.

Signed-off-by: Paul Mundt <lethal@linux-sh.org>

---

 include/asm-generic/div64.h |    7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/include/asm-generic/div64.h b/include/asm-generic/div64.h
index a4a4937..63e7768 100644
--- a/include/asm-generic/div64.h
+++ b/include/asm-generic/div64.h
@@ -19,6 +19,7 @@
 
 #include <linux/types.h>
 #include <linux/compiler.h>
+#include <linux/kernel.h>
 
 #if BITS_PER_LONG == 64
 
@@ -39,13 +40,11 @@ static inline uint64_t div64_64(uint64_t dividend, uint64_t divisor)
 
 extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
 
-/* The unnecessary pointer compare is there
- * to check for type safety (n must be 64bit)
- */
+/* The BUILD_BUG_ON() is there to check for type safety (n must be 64bit) */
 # define do_div(n,base) ({				\
 	uint32_t __base = (base);			\
 	uint32_t __rem;					\
-	(void)(((typeof((n)) *)0) == ((uint64_t *)0));	\
+	BUILD_BUG_ON(sizeof(n) != sizeof(uint64_t));	\
 	if (likely(((n) >> 32) == 0)) {			\
 		__rem = (uint32_t)(n) % __base;		\
 		(n) = (uint32_t)(n) / __base;		\

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-12-17  5:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-17  1:48 div64: Rework 64-bit type safety checks in do_div() Paul Mundt
2007-12-17  3:04 ` Andrew Morton
2007-12-17  3:20   ` Paul Mundt
2007-12-17  5:05     ` Al Viro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox