From patchwork Sun Dec 4 22:56:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Perches X-Patchwork-Id: 86468 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1223262qgi; Sun, 4 Dec 2016 14:57:16 -0800 (PST) X-Received: by 10.84.172.131 with SMTP id n3mr116592492plb.5.1480892236378; Sun, 04 Dec 2016 14:57:16 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z19si12409001pgj.173.2016.12.04.14.57.15; Sun, 04 Dec 2016 14:57:16 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751613AbcLDW5G (ORCPT + 25 others); Sun, 4 Dec 2016 17:57:06 -0500 Received: from smtprelay0160.hostedemail.com ([216.40.44.160]:55012 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750979AbcLDW5D (ORCPT ); Sun, 4 Dec 2016 17:57:03 -0500 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay05.hostedemail.com (Postfix) with ESMTP id DF8FB2691B4; Sun, 4 Dec 2016 22:56:34 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2, 0, 0, , d41d8cd98f00b204, joe@perches.com, :::::::::::, RULES_HIT:41:355:379:541:599:800:871:960:973:988:989:1000:1260:1313:1314:1345:1359:1373:1437:1516:1518:1534:1541:1575:1594:1711:1730:1747:1764:1777:1792:2393:2559:2562:2691:3138:3139:3140:3141:3142:3352:3622:3865:3866:3867:3868:3870:3871:3872:3873:3874:4250:4321:5007:6119:6226:6506:6747:6748:7281:10004:10394:10400:10848:11026:11232:11604:11658:11914:12050:12296:12740:13439:14181:14659:14721:21080:21324:21433:30003:30034:30054:30083:30091, 0, RBL:none, CacheIP:none, Bayesian:0.5, 0.5, 0.5, Netcheck:none, DomainCache:0, MSF:not bulk, SPF:fn, MSBL:0, DNSBL:none, Custom_rules:0:1:0, LFtime:2, LUA_SUMMARY:none X-HE-Tag: grain57_1c35263c5b510 X-Filterd-Recvd-Size: 4376 Received: from XPS-9350 (unknown [47.151.132.55]) (Authenticated sender: joe@perches.com) by omf04.hostedemail.com (Postfix) with ESMTPA; Sun, 4 Dec 2016 22:56:33 +0000 (UTC) Message-ID: <1480892192.4534.11.camel@perches.com> Subject: Re: [PATCH] mlx4: Use kernel sizeof and alloc styles From: Joe Perches To: Eric Dumazet Cc: Yishai Hadas , Tariq Toukan , netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Date: Sun, 04 Dec 2016 14:56:32 -0800 In-Reply-To: <1480885139.18162.484.camel@edumazet-glaptop3.roam.corp.google.com> References: <8b20668cb08cce8b4af872863440e149bd25fa94.1480882180.git.joe@perches.com> <1480885139.18162.484.camel@edumazet-glaptop3.roam.corp.google.com> X-Mailer: Evolution 3.22.1-0ubuntu2 Mime-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 2016-12-04 at 12:58 -0800, Eric Dumazet wrote: > On Sun, 2016-12-04 at 12:11 -0800, Joe Perches wrote: > > Convert sizeof foo to sizeof(foo) and allocations with multiplications > > to the appropriate kcalloc/kmalloc_array styles. > > > > Signed-off-by: Joe Perches > > --- > > Gah. > > This is one of the hotest NIC driver on linux at this moment, > with XDP and other efforts going on. > > Some kmalloc() are becoming kmalloc_node() in some dev branches, and > there is no kmalloc_array_node() yet. Well that kmalloc_array_node, like this patch, is pretty trivial to add. Something like the attached for kmalloc_array_node and kcalloc_node. > This kind of patch is making rebases/backports very painful. That's really not an issue for me. > Could we wait ~6 months before doing such cleanup/changes please ? This is certainly a trivial patch that could be done at almost any time. > If you believe a bug needs a fix, please send a patch to address it. > > Thanks. No worries. include/linux/slab.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/linux/slab.h b/include/linux/slab.h index 084b12bad198..d98c07713c03 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -647,6 +647,37 @@ static inline void *kzalloc_node(size_t size, gfp_t flags, int node) return kmalloc_node(size, flags | __GFP_ZERO, node); } +/** + * kmalloc_array_node - allocate memory for an array + * from a particular memory node. + * @n: number of elements. + * @size: element size. + * @flags: the type of memory to allocate (see kmalloc). + * @node: memory node from which to allocate + */ +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); +} + +/** + * kcalloc_node - allocate memory for an array from a particular memory node. + * The memory is set to zero. + * @n: number of elements. + * @size: element size. + * @flags: the type of memory to allocate (see kmalloc). + * @node: memory node from which to allocate + */ +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); +} + unsigned int kmem_cache_size(struct kmem_cache *s); void __init kmem_cache_init_late(void);