Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7411709imu; Thu, 31 Jan 2019 09:39:17 -0800 (PST) X-Google-Smtp-Source: ALg8bN75bFIFpC8O6sk/vEzcgESN9ITRUL2orYXymanvQrhzz/HOfhVEKOzn7FwgxPMDn7Veo9bE X-Received: by 2002:a17:902:7402:: with SMTP id g2mr34747908pll.198.1548956357187; Thu, 31 Jan 2019 09:39:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548956357; cv=none; d=google.com; s=arc-20160816; b=TpsGNUSEY6xEN3YaaTMNmQjSQRc6x3o+mniaFNg4GWj2m72t48x2Xbpa7dzoEoFD3c Wcq4qBQ31mltRV1zxpiTt0NbHyoxw2/MVoQMyoA/vSfh/fNVBk7WjPF6ss6ee2WA+/sj 2EqUptNiN0nEmrxrMTkbAa10lpuixbqJSeMJzhms9lfv2bdkHws37zwpR0w1lPaIN0cc OtrRaYl1ZUE5asmXkgwQ3LSBMat6NNv/tVmr46de+AOmTWr+vcOfPKwytMzI97ViVXtP r9luW7Dd0hJFaIJ6l7TnbT9cg+kHok0nGjbDvKJS1P2E+cxCWyEI39rxHrBxjpPDw+9T eYjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=gF0wyOh9QuyKTeDPh7VnS0CPAokIa2SmW9ciznUKYto=; b=GCQyyl2L2VVoH2GhLn8aQ+YYrbbhaicwWCj+6EeVU7T9GfVcIY6+BU9Yxh+WTSVDn0 szeKBXYzcpf42HpTR34VOqERNcre+3yR2e1pyCuounElsrLudUMujaGrOSvqepeeE4B+ LqgYkRTPt3aAe3/3e17ilqYOA2U551nSgiTgAKUme0Oo1/Cz3ZycGSFFUcxyp/7zTec5 fl2i7Tee1w6ABa8foGGVdVy//ctJfPJD2NzcbqezInYVGbhnVNPEFhAcU67FnDwGFast 5RKIWZrlCBsaBXlIlzW3787WvFRrRCzwPDei8pe7FoEf48DFCLqjs95RwCFELXlbdA4L 5/RQ== ARC-Authentication-Results: i=1; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y10si4214503pgp.348.2019.01.31.09.39.01; Thu, 31 Jan 2019 09:39:17 -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 S1728947AbfAaRLS (ORCPT + 99 others); Thu, 31 Jan 2019 12:11:18 -0500 Received: from smtprelay0104.hostedemail.com ([216.40.44.104]:33996 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726481AbfAaRLR (ORCPT ); Thu, 31 Jan 2019 12:11:17 -0500 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay05.hostedemail.com (Postfix) with ESMTP id 0EF431802EC2A; Thu, 31 Jan 2019 17:11:16 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::,RULES_HIT:41:355:379:599:967:973:982:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2393:2525:2559:2563:2682:2685:2828:2859:2914:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3622:3865:3866:3867:3868:3871:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4321:5007:8603:8957:9025:10004:10400:10848:11026:11232:11657:11658:11914:12043:12296:12438:12555:12740:12760:12895:12986:13019:13069:13311:13357:13439:13845:14181:14659:14721:21080:21324:21451:21627:21740:30012:30034:30054:30055:30091,0,RBL:149.142.244.94:@perches.com:.lbl8.mailshell.net-62.8.0.100 64.201.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:31,LUA_SUMMARY:none X-HE-Tag: wind88_623057b075b3a X-Filterd-Recvd-Size: 2456 Received: from XPS-9350 (unknown [149.142.244.94]) (Authenticated sender: joe@perches.com) by omf18.hostedemail.com (Postfix) with ESMTPA; Thu, 31 Jan 2019 17:11:14 +0000 (UTC) Message-ID: Subject: Re: [PATCH net-next] nfp: use struct_size() in kzalloc() From: Joe Perches To: "Gustavo A. R. Silva" , Jakub Kicinski , "David S. Miller" Cc: oss-drivers@netronome.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Date: Thu, 31 Jan 2019 09:11:12 -0800 In-Reply-To: <20190131003859.GA28539@embeddedor> References: <20190131003859.GA28539@embeddedor> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.30.1-1build1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2019-01-30 at 18:38 -0600, Gustavo A. R. Silva wrote: > One of the more common cases of allocation size calculations is finding > the size of a structure that has a zero-sized array at the end, along > with memory for some number of elements for that array. For example: > > struct foo { > int stuff; > struct boo entry[]; > }; > > instance = kzalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL); > > Instead of leaving these open-coded and prone to type mistakes, we can > now use the new struct_size() helper: > > instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL); > > This code was detected with the help of Coccinelle. Might be useful to augment the script to include cases where the computed size is saved to a temporary and that temporary is used ala: https://patchwork.kernel.org/patch/10782453/ On Sat, 2019-01-26 at 20:42 +0800, YueHaibing wrote: > Use kmemdup rather than duplicating its implementation [] > diff --git a/drivers/net/wireless/intel/iwlwifi/iwl-nvm-parse.c b/drivers/net/wireless/intel/iwlwifi/iwl-nvm-parse.c [] > @@ -1196,13 +1196,9 @@ iwl_parse_nvm_mcc_info(struct device *dev, const struct iwl_cfg *cfg, > regd_to_copy = sizeof(struct ieee80211_regdomain) + > valid_rules * sizeof(struct ieee80211_reg_rule); > - copy_rd = kzalloc(regd_to_copy, GFP_KERNEL); > - if (!copy_rd) { > + copy_rd = kmemdup(regd, regd_to_copy, GFP_KERNEL); This should probably be copy_rd = kmemdup(regd, struct_size(regd, reg_rules, valid_rules), GFP_KERNEL);