Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6519076imu; Wed, 30 Jan 2019 16:40:49 -0800 (PST) X-Google-Smtp-Source: ALg8bN6jW5vLj01UnEs7frOzpM+nJos96dtISQIZoaejc6GQN+yZI8ZcRwTF9pOPGIsTQWrdsXGf X-Received: by 2002:a63:955a:: with SMTP id t26mr29764251pgn.449.1548895249572; Wed, 30 Jan 2019 16:40:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548895249; cv=none; d=google.com; s=arc-20160816; b=ITvy1CdIIoYlLnT9TS5LMvVJoywZCSM7xwzdXoEIGTD/4zf6UEJ9qk+eMXDJR+K1Q6 Gu3XKV791qMct4vt2ZSmCtu9vXGsOng740RuJu8LX5D+WWalRL5Ny4EhUDeNP7X9am2F qfJJCWuhWpCfcA6TxT/7ts+J9v65wAe58RycDvxWO+OQ1hEaYHxPXgRcFHi3UDozc4ck GlT0nmSMS6m6cpYkA2sZaLIcrBQKYYawfAtovXI9+AsUfJMxcw3XJjh6p44JrSDdfBKJ TVyZvzxy9gNd72JPNbDjAwZnU9LOxraQ9ee+DjZS0J8Vj39MQpFHY9fJ1FQJZLrko/rH 14Xg== 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=Wr8ripkoA0n+pldg+tdOJGsZPpez4hYbuE1ouCKMGxA=; b=vZnUKyk/+ryegVsy6bLkGo0cjwxHtZS/Qhk+wwfqZh5Sz+1Cxx8T2U8S8puGioSHdr LRBohpSv2Vr3hJn3u2NOcyTs+D+EPTFppWhMomrCi2OT8lYxXbrWS5w9kZpO4HAx3KLb 4SNoq3rtjW7KfPcLA+qOxVzNNjGqkZX5fWSPVpbji1xHUcT/BeBa2PyKkV+he8I5FuRM PRs4LeEoB1tfRxNOVHKmAYndrwRdI+vEXfSKvdlWIY3qq6bUYbeV+KLhZ86eFYn5+L8H 6JViH/rriUgbmhAHscBQYhVMgPGE3HCqtzrqGVVqXaduo8Snv+P1qGLrhC+ACKec3Gmi tAXA== 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 z38si2627557pga.193.2019.01.30.16.40.34; Wed, 30 Jan 2019 16:40:49 -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 S1727923AbfAaAjC (ORCPT + 99 others); Wed, 30 Jan 2019 19:39:02 -0500 Received: from gateway21.websitewelcome.com ([192.185.45.43]:17470 "EHLO gateway21.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725768AbfAaAjC (ORCPT ); Wed, 30 Jan 2019 19:39:02 -0500 Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway21.websitewelcome.com (Postfix) with ESMTP id EB9A9400C4539 for ; Wed, 30 Jan 2019 18:39:00 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id p0Ngg1c1D2PzOp0Ngga4d5; Wed, 30 Jan 2019 18:39:00 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=38504 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gp0Ng-001kaH-2w; Wed, 30 Jan 2019 18:39:00 -0600 Date: Wed, 30 Jan 2019 18:38:59 -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: use struct_size() in kzalloc() Message-ID: <20190131003859.GA28539@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.130.205 X-Source-L: No X-Exim-ID: 1gp0Ng-001kaH-2w X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:38504 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 21 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[]; }; 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. Signed-off-by: Gustavo A. R. Silva --- drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c index 802c9224bb32..f6f028fa5db9 100644 --- a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c +++ b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c @@ -269,8 +269,7 @@ __nfp_eth_read_ports(struct nfp_cpp *cpp, struct nfp_nsp *nsp) goto err; } - table = kzalloc(sizeof(*table) + - sizeof(struct nfp_eth_table_port) * cnt, GFP_KERNEL); + table = kzalloc(struct_size(table, ports, cnt), GFP_KERNEL); if (!table) goto err; -- 2.20.1