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: 9460139 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3A0896071C for ; Sun, 4 Dec 2016 22:57:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 171C1209CF for ; Sun, 4 Dec 2016 22:57:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EE38B25D9E; Sun, 4 Dec 2016 22:57:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_TVD_MIME_EPI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5EBED209CF for ; Sun, 4 Dec 2016 22:57:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751373AbcLDW5D (ORCPT ); Sun, 4 Dec 2016 17:57:03 -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-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-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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);