From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753958AbcHZVC1 (ORCPT ); Fri, 26 Aug 2016 17:02:27 -0400 Received: from smtprelay0175.hostedemail.com ([216.40.44.175]:55910 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752158AbcHZVC0 (ORCPT ); Fri, 26 Aug 2016 17:02:26 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 50,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::,RULES_HIT:41:355:379:541:599:960:967:973:988:989:1042:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1542:1593:1594:1711:1730:1747:1777:1792:2194:2199:2393:2525:2553:2560:2563:2682:2685:2828:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3354:3622:3865:3866:3867:3868:3870:3871:3872:3873:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4321:5007:6119:7576:7809:7903:8603:9025:10004:10400:10848:10967:11026:11232:11473:11657:11658:11783:11914:12043:12296:12679:12740:13439:13845:13894:14181:14659:14721:14819:21063:21080:21433:21434:30012:30054:30064:30070:30090: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:0:0,LFtime:2,LUA_SUMMARY:none X-HE-Tag: hour17_31d9bca47f332 X-Filterd-Recvd-Size: 3758 Message-ID: <1472245341.4914.79.camel@perches.com> Subject: Re: [PATCH 1/5] IA64-IRQ: Use kmalloc_array() in sn_irq_lh_init() From: Joe Perches To: Julia Lawall , SF Markus Elfring Cc: linux-ia64@vger.kernel.org, Fenghua Yu , Tony Luck , LKML , kernel-janitors@vger.kernel.org, Paolo Bonzini Date: Fri, 26 Aug 2016 14:02:21 -0700 In-Reply-To: References: <349bbfb4-bada-628e-2981-ca2a315299fc@users.sourceforge.net> <2e046b40-1c8e-717f-68b1-534c3125724c@users.sourceforge.net> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.18.5.2-0ubuntu3 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2016-08-26 at 15:57 -0400, Julia Lawall wrote: > On Fri, 26 Aug 2016, SF Markus Elfring wrote: > > From: Markus Elfring > > Date: Fri, 26 Aug 2016 18:32:53 +0200 > > > > * A multiplication for the size determination of a memory allocation > >   indicated that an array data structure should be processed. > >   Thus use the corresponding function "kmalloc_array". > > > >   This issue was detected by using the Coccinelle software. > > > > * Replace the specification of data structures by pointer dereferences > >   to make the corresponding size determination a bit safer according to > >   the Linux coding style convention. > > > > Signed-off-by: Markus Elfring > > --- > >  arch/ia64/sn/kernel/irq.c | 4 ++-- > >  1 file changed, 2 insertions(+), 2 deletions(-) > > > > diff --git a/arch/ia64/sn/kernel/irq.c b/arch/ia64/sn/kernel/irq.c [] > > @@ -474,12 +474,12 @@ void __init sn_irq_lh_init(void) > >  { > >       int i; > > > > -     sn_irq_lh = kmalloc(sizeof(struct list_head *) * NR_IRQS, GFP_KERNEL); > > +     sn_irq_lh = kmalloc_array(NR_IRQS, sizeof(*sn_irq_lh), GFP_KERNEL); > >       if (!sn_irq_lh) > >               panic("SN PCI INIT: Failed to allocate memory for PCI init\n"); > > > >       for (i = 0; i < NR_IRQS; i++) { > > -             sn_irq_lh[i] = kmalloc(sizeof(struct list_head), GFP_KERNEL); > > +             sn_irq_lh[i] = kmalloc(*sn_irq_lh[i], GFP_KERNEL); > > Did a sizeof get lost here? Yes, thanks Julia. This is why adding the generating spatch code is always good. And Markus, please always compile test your code using the appropriate cross-compilers available here: https://www.kernel.org/pub/tools/crosstool/ And btw: using sizeof(*pp[i]) or sizeof(**pp) is not always clearer or better than using sizeof(type) If you _really wanted to clear up this code and make it more robust/better, it'd probably be nicer to convert the struct list_head **sn_irq_lh to a single struct list_head * and do a single struct list_head *sn_irq_la = malloc_array(nr_irqs, sizeof(struct list_head); instead of an malloc_array of the *sn_irq_lh and the multiple individual struct list_head malloc entries and change the INIT_LIST_HEAD and indexing code. That would be less data space overall given the alignment waste of the individual allocs. It also appears that sn_irq_lh is extern in arch/ia64/include/asm/sn/intr.h but could be made static to arch/ia64/sn/kernel/irq.c. But really, the conversion isn't worthwhile as NR_IRQS is limited to much less than the maximum allocation / sizeof(ptr).