Skip to contentSkip to conversation

compiler-gcc: hide COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW from sparse

PATCH Superseded Johannes Berg 1 participant 3 messages Nov 9, 2018 Slab allocator

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 compiler-gcc: hide COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW from sparse

From: Johannes Berg <[email protected]>

Sparse doesn't support all the overflow builtins, so just hide them from it to avoid lots of warnings/errors reported by it.

We could try to teach them to sparse, but the passed types are variable, and sparse doesn't seem to handle that well.

---
 include/linux/compiler-gcc.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler-gcc.h +1 -1
index 2010493e1040..3154f2a84571 100644
@@ -143,7 +143,7 @@
 #define KASAN_ABI_VERSION 3
 #endif
 
-#if GCC_VERSION >= 50100
+#if GCC_VERSION >= 50100 && !defined(__CHECKER__)
 #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
 #endif
 
Signature · 1 line
2.17.2
#1 lore raw

PATCH 2/3 kernel.h: hide __is_constexpr() from sparse

From: Johannes Berg <[email protected]>

__is_constexpr() is a work of art, understandable only to the most respected wizards of C. Even sparse doesn't seem to belong to that group and warns that there's an "expression using sizeof(void)".

Just hide the definition from sparse and pretend it's always true.

---
 include/linux/kernel.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/linux/kernel.h +8 -0
index d6aac75b51ba..d4d2233f95c9 100644
@@ -844,6 +844,7 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
 #define __typecheck(x, y) \
 		(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
 
+#ifndef __CHECKER__
 /*
  * This returns a constant expression while determining if an argument is
  * a constant expression, most importantly without evaluating the argument.
@@ -851,6 +852,13 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  */
 #define __is_constexpr(x) \
 	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+#else
+/*
+ * We don't really care about the check when running sparse and the
+ * above expression causes a warning due to sizeof(void).
+ */
+#define __is_constexpr(x) 1
+#endif
 
 #define __no_side_effects(x, y) \
 		(__is_constexpr(x) && __is_constexpr(y))
Signature · 1 line
2.17.2
#2 lore raw

PATCH 3/3 slab: use logical instead of bitwise operation in kmalloc_type()

In reply to Johannes Berg

From: Johannes Berg <[email protected]>

The operation here really is more logical than bitwise, even if due to the setup the bitwise operation works fine. However, this causes a complaint from sparse that the operation doesn't really make sense due to the not.

Use a logical and instead of bitwise.

In my (somewhat unscientific) test this caused no differences in the generated code.

---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +1 -1
index 918f374e7156..d395c7366312 100644
@@ -329,7 +329,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + (is_reclaimable && !is_dma) * KMALLOC_RECLAIM;
 }
 
 /*
Signature · 1 line
2.17.2