BUILD_BUG_ON does not work
From: Christoph Lameter
Date: Wed Apr 25 2007 - 15:25:59 EST
Here is a simple patch attempting to use BUILD_ON to fail if a too
high slab size is requested. Currently we generate a link failure which
makes it difficult to detect bad kmallocs.
However the BUILD_BUG_ON never triggers. kmalloc(64 *1024*1024,
GFP_KERNEL) returns 0 and we get an alloc from the 8 byte kmalloc cache.
kmalloc_index() returns a constant result and thus index is
constant. What do I need to do to make BUILD_BUG_ON trigger?
SLUB: Use BUILD_BUG_ON to flag too big slab caches
Do not generate a link failure instead abort compilation
using BUILD_BUG_ON
Signed-off-by: Christoph Lameter <clameter@xxxxxxx>
Index: linux-2.6.21-rc7-mm1/include/linux/slub_def.h
===================================================================
--- linux-2.6.21-rc7-mm1.orig/include/linux/slub_def.h 2007-04-25 11:38:00.000000000 -0700
+++ linux-2.6.21-rc7-mm1/include/linux/slub_def.h 2007-04-25 11:38:14.000000000 -0700
@@ -140,17 +140,11 @@ static inline struct kmem_cache *kmalloc
{
int index = kmalloc_index(size);
+ BUILD_BUG_ON(index < 0);
+
if (index == 0)
return NULL;
- if (index < 0) {
- /*
- * Generate a link failure. Would be great if we could
- * do something to stop the compile here.
- */
- extern void __kmalloc_size_too_large(void);
- __kmalloc_size_too_large();
- }
return &kmalloc_caches[index];
}
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/