mm: faster kmalloc_array(), kcalloc()
authorAlexey Dobriyan <adobriyan@gmail.com>
Tue, 26 Jul 2016 22:22:08 +0000 (15:22 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 26 Jul 2016 23:19:19 +0000 (16:19 -0700)
When both arguments to kmalloc_array() or kcalloc() are known at compile
time then their product is known at compile time but search for kmalloc
cache happens at runtime not at compile time.

Link: http://lkml.kernel.org/r/20160627213454.GA2440@p183.telecom.by
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/slab.h

index aeb3e6d00a66be64ef522bfa63b6d2dd1c03df68..1a4ea551aae51c08fd70e4dd4693e97ca56f01de 100644 (file)
@@ -565,6 +565,8 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
 {
        if (size != 0 && n > SIZE_MAX / size)
                return NULL;
+       if (__builtin_constant_p(n) && __builtin_constant_p(size))
+               return kmalloc(n * size, flags);
        return __kmalloc(n * size, flags);
 }