Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp1192416imj; Thu, 7 Feb 2019 19:47:58 -0800 (PST) X-Google-Smtp-Source: AHgI3IatYRClcuuplobyLzu421GIO8xAlk6khDMhvCXQzBqcCVTnku+x0Pr2UB6m9aZ3iEhzsL9l X-Received: by 2002:a63:ff60:: with SMTP id s32mr17674211pgk.266.1549597678148; Thu, 07 Feb 2019 19:47:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549597678; cv=none; d=google.com; s=arc-20160816; b=NIdWDCPm8F1Ej+jEjHeJiLayM0IBDyGYq+EDKgujw0oJTjvaBzJqBMIhOe/dDdTddv A2FI0vN3MAa0A9vOMzbPsuoTFNHMGoof8d7IWxfgwUk5M0HYHBxEplRHeT9XSvfL6LcQ q4W48j6RiplhF23VLcCKBC0xzTBxo49orYDRPTQS2veRFymruWE1y6jFyFAg8qohWRfY DM/tygG0Kct4WXpQyqqJW77LKh7YqpSw6ysKQrgSUM+e2703r3pNZXLEZBV48RGHCkKY 3yb1zT5DNlnoEeZZU6ZM2Emo0dAk8EXj3RGWFZozST8Mo89ZPIwF87uEgva1R9WlneZZ o0Ug== 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=nq303aZX7HFPi+SidpAaD7/aDWe47yiR9Db9vyciA0I=; b=e0wE54D0dM0SiPbFrkM5ymld6VvoHIunYbysOrn5hJvt5W1wV4lYrvu8Jvo4rd1BNF uo8pWCA550DtFVoTacwPMy8T+2GEC49UKl/PgrqDXo899Tz52mPnmaSgfhZY15GzXV7K YDQUmuM3nMb0Mwlu7sv7yX4CvxwgjAnR271BVRGqSTfToXxhR7Es0vGpC9d/G2yr7qZ6 LaNiQ29wBzjJtooUWE4Fr3I6OWv/KW5XirlIzXW7EDPqYjirkqJ4fHHYpwDGNtz2h7xU z80KQzqK3FVmPYP8rO2xmYnRBVEC1Lw/4EUo3c3vgVvZ6DDKT62Hs3lvow4BANvGPf+7 GhaA== 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 n59si1041721plb.388.2019.02.07.19.47.41; Thu, 07 Feb 2019 19:47:58 -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 S1726836AbfBHDr2 (ORCPT + 99 others); Thu, 7 Feb 2019 22:47:28 -0500 Received: from gateway32.websitewelcome.com ([192.185.145.187]:49376 "EHLO gateway32.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726728AbfBHDr1 (ORCPT ); Thu, 7 Feb 2019 22:47:27 -0500 Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway32.websitewelcome.com (Postfix) with ESMTP id E09AD1D620 for ; Thu, 7 Feb 2019 21:47:26 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id rx8QgVx7X2PzOrx8Qg21sW; Thu, 07 Feb 2019 21:47:26 -0600 X-Authority-Reason: nr=8 Received: from [189.250.49.191] (port=49434 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1grx8Q-001puo-3Z; Thu, 07 Feb 2019 21:47:26 -0600 Date: Thu, 7 Feb 2019 21:47:25 -0600 From: "Gustavo A. R. Silva" To: Jakub Kicinski , "David S. Miller" Cc: oss-drivers@netronome.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH net-next] nfp: flower: cmsg: use struct_size() helper Message-ID: <20190208034725.GA12043@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.49.191 X-Source-L: No X-Exim-ID: 1grx8Q-001puo-3Z X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.49.191]:49434 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 35 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; void *entry[]; }; size = sizeof(struct foo) + count * sizeof(void *); instance = alloc(size, GFP_KERNEL); Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper: instance = alloc(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/netronome/nfp/flower/cmsg.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/net/ethernet/netronome/nfp/flower/cmsg.c b/drivers/net/ethernet/netronome/nfp/flower/cmsg.c index 56b22ea32474..cf9e1118ee8f 100644 --- a/drivers/net/ethernet/netronome/nfp/flower/cmsg.c +++ b/drivers/net/ethernet/netronome/nfp/flower/cmsg.c @@ -45,11 +45,9 @@ nfp_flower_cmsg_mac_repr_start(struct nfp_app *app, unsigned int num_ports) { struct nfp_flower_cmsg_mac_repr *msg; struct sk_buff *skb; - unsigned int size; - size = sizeof(*msg) + num_ports * sizeof(msg->ports[0]); - skb = nfp_flower_cmsg_alloc(app, size, NFP_FLOWER_CMSG_TYPE_MAC_REPR, - GFP_KERNEL); + skb = nfp_flower_cmsg_alloc(app, struct_size(msg, ports, num_ports), + NFP_FLOWER_CMSG_TYPE_MAC_REPR, GFP_KERNEL); if (!skb) return NULL; -- 2.20.1