Slab allocators: Consistent ZERO_SIZE_PTR support Make ZERO_SIZE_PTR work for all slab allocators and get rid of the WARN_ON_ONCE(size == 0) that is still remaining in SLAB. Signed-off-by: Christoph Lameter --- include/linux/slab.h | 10 ++++++++++ include/linux/slab_def.h | 12 ++++++++++++ include/linux/slub_def.h | 11 ----------- mm/slab.c | 13 ++++++++----- mm/slob.c | 9 ++++++--- mm/util.c | 2 +- 6 files changed, 37 insertions(+), 20 deletions(-) Index: linux-2.6.22-rc4-mm2/include/linux/slab.h =================================================================== --- linux-2.6.22-rc4-mm2.orig/include/linux/slab.h 2007-06-16 18:36:40.000000000 -0700 +++ linux-2.6.22-rc4-mm2/include/linux/slab.h 2007-06-16 18:57:13.000000000 -0700 @@ -33,6 +33,16 @@ #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */ #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT /* Objects are short-lived */ /* + * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. + * + * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault. + * + * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can. + * Both make kfree a no-op. + */ +#define ZERO_SIZE_PTR ((void *)16) + +/* * struct kmem_cache related prototypes */ void __init kmem_cache_init(void); Index: linux-2.6.22-rc4-mm2/include/linux/slab_def.h =================================================================== --- linux-2.6.22-rc4-mm2.orig/include/linux/slab_def.h 2007-06-04 17:57:25.000000000 -0700 +++ linux-2.6.22-rc4-mm2/include/linux/slab_def.h 2007-06-16 18:37:28.000000000 -0700 @@ -29,6 +29,10 @@ static inline void *kmalloc(size_t size, { if (__builtin_constant_p(size)) { int i = 0; + + if (!size) + return ZERO_SIZE_PTR; + #define CACHE(x) \ if (size <= x) \ goto found; \ @@ -55,6 +59,10 @@ static inline void *kzalloc(size_t size, { if (__builtin_constant_p(size)) { int i = 0; + + if (!size) + return ZERO_SIZE_PTR; + #define CACHE(x) \ if (size <= x) \ goto found; \ @@ -84,6 +92,10 @@ static inline void *kmalloc_node(size_t { if (__builtin_constant_p(size)) { int i = 0; + + if (!size) + return ZERO_SIZE_PTR; + #define CACHE(x) \ if (size <= x) \ goto found; \ Index: linux-2.6.22-rc4-mm2/include/linux/slub_def.h =================================================================== --- linux-2.6.22-rc4-mm2.orig/include/linux/slub_def.h 2007-06-16 18:36:40.000000000 -0700 +++ linux-2.6.22-rc4-mm2/include/linux/slub_def.h 2007-06-16 18:57:13.000000000 -0700 @@ -160,17 +160,6 @@ static inline struct kmem_cache *kmalloc #endif -/* - * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. - * - * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault. - * - * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can. - * Both make kfree a no-op. - */ -#define ZERO_SIZE_PTR ((void *)16) - - static inline void *kmalloc(size_t size, gfp_t flags) { if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) { Index: linux-2.6.22-rc4-mm2/mm/slab.c =================================================================== --- linux-2.6.22-rc4-mm2.orig/mm/slab.c 2007-06-16 18:36:54.000000000 -0700 +++ linux-2.6.22-rc4-mm2/mm/slab.c 2007-06-16 18:57:13.000000000 -0700 @@ -774,7 +774,9 @@ static inline struct kmem_cache *__find_ */ BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL); #endif - WARN_ON_ONCE(size == 0); + if (!size) + return ZERO_SIZE_PTR; + while (size > csizep->cs_size) csizep++; @@ -2340,7 +2342,8 @@ kmem_cache_create (const char *name, siz * this should not happen at all. * But leave a BUG_ON for some lucky dude. */ - BUG_ON(!cachep->slabp_cache); + BUG_ON((unsigned long)cachep->slabp_cache <= + (unsigned long)ZERO_SIZE_PTR); } cachep->ctor = ctor; cachep->name = name; @@ -3642,8 +3645,8 @@ __do_kmalloc_node(size_t size, gfp_t fla struct kmem_cache *cachep; cachep = kmem_find_general_cachep(size, flags); - if (unlikely(cachep == NULL)) - return NULL; + if (unlikely((unsigned long)cachep <= (unsigned long)ZERO_SIZE_PTR)) + return cachep; return kmem_cache_alloc_node(cachep, flags, node); } @@ -3749,7 +3752,7 @@ void kfree(const void *objp) struct kmem_cache *c; unsigned long flags; - if (unlikely(!objp)) + if (unlikely((unsigned long)objp <= (unsigned long)ZERO_SIZE_PTR)) return; local_irq_save(flags); kfree_debugcheck(objp); Index: linux-2.6.22-rc4-mm2/mm/slob.c =================================================================== --- linux-2.6.22-rc4-mm2.orig/mm/slob.c 2007-06-16 18:36:54.000000000 -0700 +++ linux-2.6.22-rc4-mm2/mm/slob.c 2007-06-16 18:57:18.000000000 -0700 @@ -306,7 +306,7 @@ static void slob_free(void *block, int s slobidx_t units; unsigned long flags; - if (!block) + if ((unsigned long)block <= (unsigned long)ZERO_SIZE_PTR) return; BUG_ON(!size); @@ -384,11 +384,14 @@ out: void *__kmalloc(size_t size, gfp_t gfp) { + unsigned int *m; int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); if (size < PAGE_SIZE - align) { - unsigned int *m; - m = slob_alloc(size + align, gfp, align); + if (!size) + return ZERO_SIZE_PTR; + + m = slob_alloc(size + align, gfp, align); if (m) *m = size; return (void *)m + align; Index: linux-2.6.22-rc4-mm2/mm/util.c =================================================================== --- linux-2.6.22-rc4-mm2.orig/mm/util.c 2007-06-16 18:36:54.000000000 -0700 +++ linux-2.6.22-rc4-mm2/mm/util.c 2007-06-16 18:37:28.000000000 -0700 @@ -99,7 +99,7 @@ void *krealloc(const void *p, size_t new if (unlikely(!new_size)) { kfree(p); - return NULL; + return ZERO_SIZE_PTR; } ks = ksize(p);