blob: 64e368ba7d47e600c39818f7524413726395b0f6 [file] [log] [blame]
From cd32c0ac9de45f5317ca81b77aed8021f85b8dd7 Mon Sep 17 00:00:00 2001
From: Johannes Thumshirn <>
Date: Wed, 15 Nov 2017 17:32:29 -0800
Subject: [PATCH 0402/1795] include/linux/slab.h: add kmalloc_array_node() and
Patch series "Add kmalloc_array_node() and kcalloc_node()".
Our current memeory allocation routines suffer form an API imbalance,
for one we have kmalloc_array() and kcalloc() which check for overflows
in size multiplication and we have kmalloc_node() and kzalloc_node()
which allow for memory allocation on a certain NUMA node but don't check
for eventual overflows.
This patch (of 6):
We have kmalloc_array() and kcalloc() wrappers on top of kmalloc() which
ensure us overflow free multiplication for the size of a memory
allocation but these implementations are not NUMA-aware.
Likewise we have kmalloc_node() which is a NUMA-aware version of
kmalloc() but the implementation is not aware of any possible overflows
in eventual size calculations.
Introduce a combination of the two above cases to have a NUMA-node aware
version of kmalloc_array() and kcalloc().
Signed-off-by: Johannes Thumshirn <>
Acked-by: Vlastimil Babka <>
Cc: Christoph Hellwig <>
Cc: Christoph Lameter <>
Cc: Damien Le Moal <>
Cc: David Rientjes <>
Cc: "David S. Miller" <>
Cc: Doug Ledford <>
Cc: Hal Rosenstock <>
Cc: Jens Axboe <>
Cc: Joonsoo Kim <>
Cc: Mike Marciniszyn <>
Cc: Pekka Enberg <>
Cc: Santosh Shilimkar <>
Cc: Sean Hefty <>
Signed-off-by: Andrew Morton <>
Signed-off-by: Linus Torvalds <>
(cherry picked from commit 5799b255c491d853b73fb9e0e1760210315d06cd)
Signed-off-by: Simon Horman <>
Signed-off-by: Geert Uytterhoeven <>
include/linux/slab.h | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/include/linux/slab.h b/include/linux/slab.h
index ae5ed6492d54..28687c296ece 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -630,6 +630,22 @@ extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long);
#define kmalloc_track_caller(size, flags) \
__kmalloc_track_caller(size, flags, _RET_IP_)
+static inline void *kmalloc_array_node(size_t n, size_t size, gfp_t flags,
+ int node)
+ if (size != 0 && n > SIZE_MAX / size)
+ return NULL;
+ if (__builtin_constant_p(n) && __builtin_constant_p(size))
+ return kmalloc_node(n * size, flags, node);
+ return __kmalloc_node(n * size, flags, node);
+static inline void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node)
+ return kmalloc_array_node(n, size, flags | __GFP_ZERO, node);
extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long);
#define kmalloc_node_track_caller(size, flags, node) \