Received: by 2002:a25:5b86:0:0:0:0:0 with SMTP id p128csp723399ybb; Thu, 28 Mar 2019 10:53:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqzC7QLdh3wrjcCmFgjy1739cYVwMJDAXlrP308nMx4vpkonr24iTyaNYE5iAjcXptHi82/P X-Received: by 2002:a17:902:523:: with SMTP id 32mr43094534plf.249.1553795609418; Thu, 28 Mar 2019 10:53:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553795609; cv=none; d=google.com; s=arc-20160816; b=TQiM+PvODjiloR8ocmUWxMyejwhKTffED9ZblDTvfmJLQhWCSBjC5u3cg4hegSOxau WcW4gJk5dxggnaQvEQLGAu0PtbtieImXu+bgw0cYJ1cfCUqbyj/HZC/+5g+jHMT3TeHy JjPcjFAjEvB+SvG6Owq6wxeGq/8D9Cw2JMnVbig+3DA3y4FCCCp0fx9cOnjtQE6tCD3w A2xyfDYGmhPTM267mjc+k8brU0eDsIuSJ24u4RsShCxw2kEBwyOzpMRiG76Ph3cYEL6M FdPNmzYoGZ7ySCEh3IWYysmw3H2BLCY38sfJmKCv8dofK9Lg6ql+lAcnQVe19KiIDzGs 66fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date; bh=fsCAg9kulweooIWxkI2CeBNeThnbyDLr15Ia9BLCbQ4=; b=rnuka6s06P9yWRZqVnKTaY6GRcsEzAn/la1Wv358cUwVkCRKprp8+M9xDrY830uS0W 1AKE+6CEyjyfF++mKKS2tRaH+HITBhn+t8wN2AbA9mIW6c2LlWYfEFvorPSxuAAhrfxl 4Goli3VoPW/KRFSQ3eTfU+Gnq7M/iDIZWm5rf8kxbPvTBhyh6YRSm/UM+NkcrSYynpp+ icsw+Hswk41tK8D5vv2pRrSdxUOkWRIFUycKwDsbMlqMDWuvSBkuq4bqu/Jkr+9m+jdB qrJMfsaS/VvcDgcvQBKVGJO+x2onXKkbTphBXNJgEa7SCTFqpp968qUWV4E2qCgLg0Fw bn5g== 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 k12si3508051pgi.107.2019.03.28.10.53.13; Thu, 28 Mar 2019 10:53:29 -0700 (PDT) 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 S1727181AbfC1RwZ (ORCPT + 99 others); Thu, 28 Mar 2019 13:52:25 -0400 Received: from gateway30.websitewelcome.com ([192.185.149.4]:15627 "EHLO gateway30.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725816AbfC1RwZ (ORCPT ); Thu, 28 Mar 2019 13:52:25 -0400 X-Greylist: delayed 411 seconds by postgrey-1.27 at vger.kernel.org; Thu, 28 Mar 2019 13:52:25 EDT Received: from cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway30.websitewelcome.com (Postfix) with ESMTP id D49C22693C for ; Thu, 28 Mar 2019 12:52:24 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id 9ZCSh4cDYdnCe9ZCShv5oW; Thu, 28 Mar 2019 12:52:24 -0500 X-Authority-Reason: nr=8 Received: from [189.250.117.246] (port=37584 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1h9ZCS-003FY3-0e; Thu, 28 Mar 2019 12:52:24 -0500 Date: Thu, 28 Mar 2019 12:52:23 -0500 From: "Gustavo A. R. Silva" To: Jeff Kirsher , "David S. Miller" Cc: intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH][next] i40e: Use struct_size() in kzalloc() Message-ID: <20190328175223.GA18532@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 189.250.117.246 X-Source-L: No X-Exim-ID: 1h9ZCS-003FY3-0e X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.117.246]:37584 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 16 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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[]; }; size = sizeof(struct foo) + count * sizeof(struct boo); instance = kzalloc(size, 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) Notice that, in this case, variable size is not necessary, hence it is removed. This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/net/ethernet/intel/i40e/i40e_client.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/net/ethernet/intel/i40e/i40e_client.c b/drivers/net/ethernet/intel/i40e/i40e_client.c index 5f3b8b9ff511..e81530ca08d0 100644 --- a/drivers/net/ethernet/intel/i40e/i40e_client.c +++ b/drivers/net/ethernet/intel/i40e/i40e_client.c @@ -578,11 +578,9 @@ static int i40e_client_setup_qvlist(struct i40e_info *ldev, struct i40e_hw *hw = &pf->hw; struct i40e_qv_info *qv_info; u32 v_idx, i, reg_idx, reg; - u32 size; - size = sizeof(struct i40e_qvlist_info) + - (sizeof(struct i40e_qv_info) * (qvlist_info->num_vectors - 1)); - ldev->qvlist_info = kzalloc(size, GFP_KERNEL); + ldev->qvlist_info = kzalloc(struct_size(ldev->qvlist_info, qv_info, + qvlist_info->num_vectors - 1), GFP_KERNEL); if (!ldev->qvlist_info) return -ENOMEM; ldev->qvlist_info->num_vectors = qvlist_info->num_vectors; -- 2.21.0