Skip to content

Commit 5799b25

Browse files
Johannes Thumshirntorvalds
authored andcommitted
include/linux/slab.h: add kmalloc_array_node() and kcalloc_node()
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(). Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Johannes Thumshirn <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Christoph Lameter <[email protected]> Cc: Damien Le Moal <[email protected]> Cc: David Rientjes <[email protected]> Cc: "David S. Miller" <[email protected]> Cc: Doug Ledford <[email protected]> Cc: Hal Rosenstock <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: Mike Marciniszyn <[email protected]> Cc: Pekka Enberg <[email protected]> Cc: Santosh Shilimkar <[email protected]> Cc: Sean Hefty <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 1106638 commit 5799b25

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

include/linux/slab.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,22 @@ extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long);
650650
#define kmalloc_track_caller(size, flags) \
651651
__kmalloc_track_caller(size, flags, _RET_IP_)
652652

653+
static inline void *kmalloc_array_node(size_t n, size_t size, gfp_t flags,
654+
int node)
655+
{
656+
if (size != 0 && n > SIZE_MAX / size)
657+
return NULL;
658+
if (__builtin_constant_p(n) && __builtin_constant_p(size))
659+
return kmalloc_node(n * size, flags, node);
660+
return __kmalloc_node(n * size, flags, node);
661+
}
662+
663+
static inline void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node)
664+
{
665+
return kmalloc_array_node(n, size, flags | __GFP_ZERO, node);
666+
}
667+
668+
653669
#ifdef CONFIG_NUMA
654670
extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long);
655671
#define kmalloc_node_track_caller(size, flags, node) \

0 commit comments

Comments
 (0)