From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f69.google.com (mail-wm0-f69.google.com [74.125.82.69]) by kanga.kvack.org (Postfix) with ESMTP id CA60C6B0069 for ; Wed, 27 Sep 2017 04:20:58 -0400 (EDT) Received: by mail-wm0-f69.google.com with SMTP id e64so13648130wmi.0 for ; Wed, 27 Sep 2017 01:20:58 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id i33si23388wra.310.2017.09.27.01.20.57 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Sep 2017 01:20:57 -0700 (PDT) From: Johannes Thumshirn Subject: [PATCH 0/6] Add kmalloc_array_node() and kcalloc_node() Date: Wed, 27 Sep 2017 10:20:32 +0200 Message-Id: <20170927082038.3782-1-jthumshirn@suse.de> Sender: owner-linux-mm@kvack.org List-ID: To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig , Johannes Thumshirn 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. Johannes Thumshirn (6): mm: add kmalloc_array_node and kcalloc_node block: use kmalloc_array_node IB/qib: use kmalloc_array_node IB/rdmavt: use kmalloc_array_node mm, mempool: use kmalloc_array_node rds: ib: use kmalloc_array_node block/blk-mq.c | 2 +- drivers/infiniband/hw/qib/qib_init.c | 5 +++-- drivers/infiniband/sw/rdmavt/qp.c | 2 +- include/linux/slab.h | 16 ++++++++++++++++ mm/mempool.c | 2 +- net/rds/ib_fmr.c | 4 ++-- 6 files changed, 24 insertions(+), 7 deletions(-) -- 2.13.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f69.google.com (mail-wm0-f69.google.com [74.125.82.69]) by kanga.kvack.org (Postfix) with ESMTP id 2DE536B025F for ; Wed, 27 Sep 2017 04:20:59 -0400 (EDT) Received: by mail-wm0-f69.google.com with SMTP id f4so13616022wmh.7 for ; Wed, 27 Sep 2017 01:20:59 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id p1si8602982wrp.443.2017.09.27.01.20.57 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Sep 2017 01:20:58 -0700 (PDT) From: Johannes Thumshirn Subject: [PATCH 1/6] mm: add kmalloc_array_node and kcalloc_node Date: Wed, 27 Sep 2017 10:20:33 +0200 Message-Id: <20170927082038.3782-2-jthumshirn@suse.de> In-Reply-To: <20170927082038.3782-1-jthumshirn@suse.de> References: <20170927082038.3782-1-jthumshirn@suse.de> Sender: owner-linux-mm@kvack.org List-ID: To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig , Johannes Thumshirn 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 --- include/linux/slab.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/linux/slab.h b/include/linux/slab.h index 41473df6dfb0..aaf4723e41b3 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -635,6 +635,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); +} + + #ifdef CONFIG_NUMA extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long); #define kmalloc_node_track_caller(size, flags, node) \ -- 2.13.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f198.google.com (mail-wr0-f198.google.com [209.85.128.198]) by kanga.kvack.org (Postfix) with ESMTP id EBB646B025F for ; Wed, 27 Sep 2017 04:21:00 -0400 (EDT) Received: by mail-wr0-f198.google.com with SMTP id b9so14760140wra.3 for ; Wed, 27 Sep 2017 01:21:00 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id r1si8859615wre.56.2017.09.27.01.20.59 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Sep 2017 01:20:59 -0700 (PDT) From: Johannes Thumshirn Subject: [PATCH 2/6] block: use kmalloc_array_node Date: Wed, 27 Sep 2017 10:20:34 +0200 Message-Id: <20170927082038.3782-3-jthumshirn@suse.de> In-Reply-To: <20170927082038.3782-1-jthumshirn@suse.de> References: <20170927082038.3782-1-jthumshirn@suse.de> Sender: owner-linux-mm@kvack.org List-ID: To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig , Johannes Thumshirn Now that we have a NUMA-aware version of kmalloc_array() we can use it instead of kmalloc_node() without an overflow check in the size calculation. Signed-off-by: Johannes Thumshirn --- block/blk-mq.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 98a18609755e..49f9dc0eb47c 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -1979,7 +1979,7 @@ static int blk_mq_init_hctx(struct request_queue *q, * Allocate space for all possible cpus to avoid allocation at * runtime */ - hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), + hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *), GFP_KERNEL, node); if (!hctx->ctxs) goto unregister_cpu_notifier; -- 2.13.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f71.google.com (mail-wm0-f71.google.com [74.125.82.71]) by kanga.kvack.org (Postfix) with ESMTP id 5BF3C6B0260 for ; Wed, 27 Sep 2017 04:21:06 -0400 (EDT) Received: by mail-wm0-f71.google.com with SMTP id b195so13639848wmb.6 for ; Wed, 27 Sep 2017 01:21:06 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id f81si3200766wmh.41.2017.09.27.01.21.05 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Sep 2017 01:21:05 -0700 (PDT) From: Johannes Thumshirn Subject: [PATCH 3/6] IB/qib: use kmalloc_array_node Date: Wed, 27 Sep 2017 10:20:35 +0200 Message-Id: <20170927082038.3782-4-jthumshirn@suse.de> In-Reply-To: <20170927082038.3782-1-jthumshirn@suse.de> References: <20170927082038.3782-1-jthumshirn@suse.de> Sender: owner-linux-mm@kvack.org List-ID: To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig , Johannes Thumshirn Now that we have a NUMA-aware version of kmalloc_array() we can use it instead of kmalloc_node() without an overflow check in the size calculation. Signed-off-by: Johannes Thumshirn --- drivers/infiniband/hw/qib/qib_init.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/infiniband/hw/qib/qib_init.c b/drivers/infiniband/hw/qib/qib_init.c index c5a4c65636d6..6aebf4e707b9 100644 --- a/drivers/infiniband/hw/qib/qib_init.c +++ b/drivers/infiniband/hw/qib/qib_init.c @@ -1674,8 +1674,9 @@ int qib_setup_eagerbufs(struct qib_ctxtdata *rcd) } if (!rcd->rcvegrbuf_phys) { rcd->rcvegrbuf_phys = - kmalloc_node(chunk * sizeof(rcd->rcvegrbuf_phys[0]), - GFP_KERNEL, rcd->node_id); + kmalloc_array_node(chunk, + sizeof(rcd->rcvegrbuf_phys[0]), + GFP_KERNEL, rcd->node_id); if (!rcd->rcvegrbuf_phys) goto bail_rcvegrbuf; } -- 2.13.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f199.google.com (mail-wr0-f199.google.com [209.85.128.199]) by kanga.kvack.org (Postfix) with ESMTP id 645A06B0261 for ; Wed, 27 Sep 2017 04:21:08 -0400 (EDT) Received: by mail-wr0-f199.google.com with SMTP id j50so1169595wra.4 for ; Wed, 27 Sep 2017 01:21:08 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id i26si3076456wmb.174.2017.09.27.01.21.07 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Sep 2017 01:21:07 -0700 (PDT) From: Johannes Thumshirn Subject: [PATCH 4/6] IB/rdmavt: use kmalloc_array_node Date: Wed, 27 Sep 2017 10:20:36 +0200 Message-Id: <20170927082038.3782-5-jthumshirn@suse.de> In-Reply-To: <20170927082038.3782-1-jthumshirn@suse.de> References: <20170927082038.3782-1-jthumshirn@suse.de> Sender: owner-linux-mm@kvack.org List-ID: To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig , Johannes Thumshirn Now that we have a NUMA-aware version of kmalloc_array() we can use it instead of kmalloc_node() without an overflow check in the size calculation. Signed-off-by: Johannes Thumshirn --- drivers/infiniband/sw/rdmavt/qp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/infiniband/sw/rdmavt/qp.c b/drivers/infiniband/sw/rdmavt/qp.c index 22df09ae809e..b8e904905f47 100644 --- a/drivers/infiniband/sw/rdmavt/qp.c +++ b/drivers/infiniband/sw/rdmavt/qp.c @@ -238,7 +238,7 @@ int rvt_driver_qp_init(struct rvt_dev_info *rdi) rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size; rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size); rdi->qp_dev->qp_table = - kmalloc_node(rdi->qp_dev->qp_table_size * + kmalloc_array_node(rdi->qp_dev->qp_table_size, sizeof(*rdi->qp_dev->qp_table), GFP_KERNEL, rdi->dparms.node); if (!rdi->qp_dev->qp_table) -- 2.13.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f200.google.com (mail-wr0-f200.google.com [209.85.128.200]) by kanga.kvack.org (Postfix) with ESMTP id 9F42E6B0266 for ; Wed, 27 Sep 2017 04:21:10 -0400 (EDT) Received: by mail-wr0-f200.google.com with SMTP id v109so14765342wrc.5 for ; Wed, 27 Sep 2017 01:21:10 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id 6si8749256wrm.70.2017.09.27.01.21.09 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Sep 2017 01:21:09 -0700 (PDT) From: Johannes Thumshirn Subject: [PATCH 5/6] mm, mempool: use kmalloc_array_node Date: Wed, 27 Sep 2017 10:20:37 +0200 Message-Id: <20170927082038.3782-6-jthumshirn@suse.de> In-Reply-To: <20170927082038.3782-1-jthumshirn@suse.de> References: <20170927082038.3782-1-jthumshirn@suse.de> Sender: owner-linux-mm@kvack.org List-ID: To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig , Johannes Thumshirn Now that we have a NUMA-aware version of kmalloc_array() we can use it instead of kmalloc_node() without an overflow check in the size calculation. Signed-off-by: Johannes Thumshirn --- mm/mempool.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/mempool.c b/mm/mempool.c index 1c0294858527..26f1b70c4a4e 100644 --- a/mm/mempool.c +++ b/mm/mempool.c @@ -188,7 +188,7 @@ mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn, pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id); if (!pool) return NULL; - pool->elements = kmalloc_node(min_nr * sizeof(void *), + pool->elements = kmalloc_array_node(min_nr, sizeof(void *), gfp_mask, node_id); if (!pool->elements) { kfree(pool); -- 2.13.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f70.google.com (mail-wm0-f70.google.com [74.125.82.70]) by kanga.kvack.org (Postfix) with ESMTP id 19F146B026A for ; Wed, 27 Sep 2017 04:21:13 -0400 (EDT) Received: by mail-wm0-f70.google.com with SMTP id i131so13648473wma.1 for ; Wed, 27 Sep 2017 01:21:13 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id h189si3012137wmf.89.2017.09.27.01.21.11 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Sep 2017 01:21:12 -0700 (PDT) From: Johannes Thumshirn Subject: [PATCH 6/6] rds: ib: use kmalloc_array_node Date: Wed, 27 Sep 2017 10:20:38 +0200 Message-Id: <20170927082038.3782-7-jthumshirn@suse.de> In-Reply-To: <20170927082038.3782-1-jthumshirn@suse.de> References: <20170927082038.3782-1-jthumshirn@suse.de> Sender: owner-linux-mm@kvack.org List-ID: To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig , Johannes Thumshirn Now that we have a NUMA-aware version of kmalloc_array() we can use it instead of kmalloc_node() without an overflow check in the size calculation. Signed-off-by: Johannes Thumshirn --- net/rds/ib_fmr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/rds/ib_fmr.c b/net/rds/ib_fmr.c index 86ef907067bb..e0f70c4051b6 100644 --- a/net/rds/ib_fmr.c +++ b/net/rds/ib_fmr.c @@ -139,8 +139,8 @@ static int rds_ib_map_fmr(struct rds_ib_device *rds_ibdev, return -EINVAL; } - dma_pages = kmalloc_node(sizeof(u64) * page_cnt, GFP_ATOMIC, - rdsibdev_to_node(rds_ibdev)); + dma_pages = kmalloc_array_node(sizeof(u64), page_cnt, GFP_ATOMIC, + rdsibdev_to_node(rds_ibdev)); if (!dma_pages) { ib_dma_unmap_sg(dev, sg, nents, DMA_BIDIRECTIONAL); return -ENOMEM; -- 2.13.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f197.google.com (mail-wr0-f197.google.com [209.85.128.197]) by kanga.kvack.org (Postfix) with ESMTP id A48636B0069 for ; Wed, 27 Sep 2017 04:42:54 -0400 (EDT) Received: by mail-wr0-f197.google.com with SMTP id h16so14872367wrf.0 for ; Wed, 27 Sep 2017 01:42:54 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id x73si1203345wme.29.2017.09.27.01.42.53 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Sep 2017 01:42:53 -0700 (PDT) Date: Wed, 27 Sep 2017 10:42:51 +0200 From: Michal Hocko Subject: Re: [PATCH 1/6] mm: add kmalloc_array_node and kcalloc_node Message-ID: <20170927084251.kxves5ce76jz5skr@dhcp22.suse.cz> References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-2-jthumshirn@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170927082038.3782-2-jthumshirn@suse.de> Sender: owner-linux-mm@kvack.org List-ID: To: Johannes Thumshirn Cc: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig On Wed 27-09-17 10:20:33, Johannes Thumshirn wrote: > 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(). Yes, this is helpful. I am just wondering why we cannot have kmalloc_array to call kmalloc_array_node with the local node as a parameter. Maybe some sort of an optimization? > Signed-off-by: Johannes Thumshirn Anyway Acked-by: Michal Hocko > --- > include/linux/slab.h | 16 ++++++++++++++++ > 1 file changed, 16 insertions(+) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 41473df6dfb0..aaf4723e41b3 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -635,6 +635,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); > +} > + > + > #ifdef CONFIG_NUMA > extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long); > #define kmalloc_node_track_caller(size, flags, node) \ > -- > 2.13.5 > > -- > To unsubscribe, send a message with 'unsubscribe linux-mm' in > the body to majordomo@kvack.org. For more info on Linux MM, > see: http://www.linux-mm.org/ . > Don't email: email@kvack.org -- Michal Hocko SUSE Labs -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f198.google.com (mail-wr0-f198.google.com [209.85.128.198]) by kanga.kvack.org (Postfix) with ESMTP id 160D46B0069 for ; Wed, 27 Sep 2017 05:16:25 -0400 (EDT) Received: by mail-wr0-f198.google.com with SMTP id v109so15016069wrc.5 for ; Wed, 27 Sep 2017 02:16:25 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id r7si3138382wmg.191.2017.09.27.02.16.23 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Sep 2017 02:16:23 -0700 (PDT) Date: Wed, 27 Sep 2017 11:16:19 +0200 From: Michal Hocko Subject: Re: [PATCH 1/6] mm: add kmalloc_array_node and kcalloc_node Message-ID: <20170927091619.lkhfhwv3uu3km3sv@dhcp22.suse.cz> References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-2-jthumshirn@suse.de> <20170927084251.kxves5ce76jz5skr@dhcp22.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: owner-linux-mm@kvack.org List-ID: To: Christopher Lameter Cc: Johannes Thumshirn , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig On Wed 27-09-17 04:03:01, Cristopher Lameter wrote: > On Wed, 27 Sep 2017, Michal Hocko wrote: > > > > Introduce a combination of the two above cases to have a NUMA-node aware > > > version of kmalloc_array() and kcalloc(). > > > > Yes, this is helpful. I am just wondering why we cannot have > > kmalloc_array to call kmalloc_array_node with the local node as a > > parameter. Maybe some sort of an optimization? > > Well the regular kmalloc without node is supposed to follow memory > policies. An explicit mentioning of a node requires allocation from that > node and will override memory allocation policies. I see. Thanks for the clarification -- Michal Hocko SUSE Labs -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f72.google.com (mail-wm0-f72.google.com [74.125.82.72]) by kanga.kvack.org (Postfix) with ESMTP id C02CD6B025E for ; Fri, 29 Sep 2017 08:00:41 -0400 (EDT) Received: by mail-wm0-f72.google.com with SMTP id n64so1265274wma.0 for ; Fri, 29 Sep 2017 05:00:41 -0700 (PDT) Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id z134si2838313wmd.154.2017.09.29.05.00.39 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 29 Sep 2017 05:00:39 -0700 (PDT) Subject: Re: [PATCH 1/6] mm: add kmalloc_array_node and kcalloc_node References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-2-jthumshirn@suse.de> From: Vlastimil Babka Message-ID: Date: Fri, 29 Sep 2017 14:00:37 +0200 MIME-Version: 1.0 In-Reply-To: <20170927082038.3782-2-jthumshirn@suse.de> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: owner-linux-mm@kvack.org List-ID: To: Johannes Thumshirn , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig On 09/27/2017 10:20 AM, Johannes Thumshirn wrote: > 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 Sounds better than custom open-coded stuff indeed. Acked-by: Vlastimil Babka > --- > include/linux/slab.h | 16 ++++++++++++++++ > 1 file changed, 16 insertions(+) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 41473df6dfb0..aaf4723e41b3 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -635,6 +635,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); > +} > + > + > #ifdef CONFIG_NUMA > extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long); > #define kmalloc_node_track_caller(size, flags, node) \ > -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christopher Lameter Subject: Re: [PATCH 1/6] mm: add kmalloc_array_node and kcalloc_node Date: Wed, 27 Sep 2017 03:56:40 -0500 (CDT) Message-ID: References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-2-jthumshirn@suse.de> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <20170927082038.3782-2-jthumshirn@suse.de> Sender: linux-kernel-owner@vger.kernel.org To: Johannes Thumshirn Cc: Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig List-Id: linux-mm.kvack.org On Wed, 27 Sep 2017, Johannes Thumshirn wrote: > +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); Isnt the same check done by kmalloc_node already? The result of multiplying two constants is a constant after all. From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christopher Lameter Subject: Re: [PATCH 2/6] block: use kmalloc_array_node Date: Wed, 27 Sep 2017 03:57:31 -0500 (CDT) Message-ID: References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-3-jthumshirn@suse.de> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <20170927082038.3782-3-jthumshirn@suse.de> Sender: linux-kernel-owner@vger.kernel.org To: Johannes Thumshirn Cc: Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig List-Id: linux-mm.kvack.org Reviewed-by: Christoph Lameter From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christopher Lameter Subject: Re: [PATCH 3/6] IB/qib: use kmalloc_array_node Date: Wed, 27 Sep 2017 03:58:08 -0500 (CDT) Message-ID: References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-4-jthumshirn@suse.de> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <20170927082038.3782-4-jthumshirn@suse.de> Sender: linux-kernel-owner@vger.kernel.org To: Johannes Thumshirn Cc: Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig List-Id: linux-mm.kvack.org Reviewed-by: Christoph Lameter From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christopher Lameter Subject: Re: [PATCH 1/6] mm: add kmalloc_array_node and kcalloc_node Date: Wed, 27 Sep 2017 04:03:01 -0500 (CDT) Message-ID: References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-2-jthumshirn@suse.de> <20170927084251.kxves5ce76jz5skr@dhcp22.suse.cz> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <20170927084251.kxves5ce76jz5skr@dhcp22.suse.cz> Sender: linux-kernel-owner@vger.kernel.org To: Michal Hocko Cc: Johannes Thumshirn , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig List-Id: linux-mm.kvack.org On Wed, 27 Sep 2017, Michal Hocko wrote: > > Introduce a combination of the two above cases to have a NUMA-node aware > > version of kmalloc_array() and kcalloc(). > > Yes, this is helpful. I am just wondering why we cannot have > kmalloc_array to call kmalloc_array_node with the local node as a > parameter. Maybe some sort of an optimization? Well the regular kmalloc without node is supposed to follow memory policies. An explicit mentioning of a node requires allocation from that node and will override memory allocation policies. Note that node local policy is the default for allocations but that can be overridden by the application or at the command line level. Assumptions that this is always the case come up frequently but if we do that we will loose the ability to control memory locality for user space. From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christopher Lameter Subject: Re: [PATCH 6/6] rds: ib: use kmalloc_array_node Date: Wed, 27 Sep 2017 04:03:37 -0500 (CDT) Message-ID: References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-7-jthumshirn@suse.de> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <20170927082038.3782-7-jthumshirn@suse.de> Sender: linux-kernel-owner@vger.kernel.org To: Johannes Thumshirn Cc: Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig List-Id: linux-mm.kvack.org Reviewed-by: Christoph Lameter From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christopher Lameter Subject: Re: [PATCH 5/6] mm, mempool: use kmalloc_array_node Date: Wed, 27 Sep 2017 04:04:08 -0500 (CDT) Message-ID: References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-6-jthumshirn@suse.de> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <20170927082038.3782-6-jthumshirn@suse.de> Sender: linux-kernel-owner@vger.kernel.org To: Johannes Thumshirn Cc: Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig List-Id: linux-mm.kvack.org Reviewed-by: Christoph Lameter From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christopher Lameter Subject: Re: [PATCH 4/6] IB/rdmavt: use kmalloc_array_node Date: Wed, 27 Sep 2017 04:04:35 -0500 (CDT) Message-ID: References: <20170927082038.3782-1-jthumshirn@suse.de> <20170927082038.3782-5-jthumshirn@suse.de> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <20170927082038.3782-5-jthumshirn@suse.de> Sender: linux-kernel-owner@vger.kernel.org To: Johannes Thumshirn Cc: Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Damien Le Moal , Christoph Hellwig List-Id: linux-mm.kvack.org Reviewed-by: Christoph Lameter