Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp879126imu; Wed, 16 Jan 2019 09:02:24 -0800 (PST) X-Google-Smtp-Source: ALg8bN5VlxFsgscpPNmTRw7C0Biu4A8gORHgq4wXNV3MyBaxS008SS4j5tsmx7+YEw5i4/LXfIte X-Received: by 2002:a17:902:e085:: with SMTP id cb5mr10765424plb.24.1547658144369; Wed, 16 Jan 2019 09:02:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547658144; cv=none; d=google.com; s=arc-20160816; b=Mr7XPsDadCBO5LGB7M5ZDiG1S4IFW7MDiVfuq7oWfOhqtujuViiBKziUo5NjbHiuSB NnwOve7wrWhaAcqrlCwP0KRzFRytBAoV/dd4+9H968f5cpeK1IAfH4m64zsoB1fF1JHQ wCnf4O92+zujXj0m3M7wtjs/na8jURXXrsuvGzhpe1JnUGleEKwiBbFUe5uRLds9xkVG 8R0XxGRzZiLS1KSHYXgH4QWd4JoaIZdFAzfWVig8vOOoOBGGSFJvGmMtkQwxJvUpurq9 HQSWdAnTCYPyTNx22mITw/3qjVzvkmYKX2adIEwRruCZEHapP4A7xjhAJdKesibhC/Qo uCzQ== 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=k3W3NkF/ZmxBRdvWSmOWwNx3C13qpSxrB5g4GwOUT4k=; b=qakJJBaiIQlBvUeG8hermkS/vME1aM8ZMYbygTsGWPycEkdaLXtKtK2cFVybCwG1Wq aGyb6GYrf6LqT0LSVHOigVz4+Jd2yNzR+3/wo9tThi+pOT/h7hU4FmQdWC+odH3B264u uSeUf46SQ+/JcL5jAAPEpj7peMaaW/IfcY0W0lZvOSkOCuIJF+EyMa3CDb2f+N3JW81l z7HVUkK2LuQOB/I+ljV2/y6+KP54DVPXKRaqJNl+TSrrp47yMtACZotesW0YJNC9g+eb xS4pvRnyNghZvs9JmJc3g1z6sDCcoCxa52B3+i9gRApZbj/6gVId0rgsgnIKpBSkreuM XBgg== 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 f4si6777038plf.370.2019.01.16.09.01.56; Wed, 16 Jan 2019 09:02:24 -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 S2391104AbfAOXOd (ORCPT + 99 others); Tue, 15 Jan 2019 18:14:33 -0500 Received: from gateway31.websitewelcome.com ([192.185.144.218]:30030 "EHLO gateway31.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728858AbfAOXOd (ORCPT ); Tue, 15 Jan 2019 18:14:33 -0500 Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway31.websitewelcome.com (Postfix) with ESMTP id EB90D13BE0 for ; Tue, 15 Jan 2019 17:14:32 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id jXuigklkq90onjXuigDaUl; Tue, 15 Jan 2019 17:14:32 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=56030 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gjXui-002jld-3n; Tue, 15 Jan 2019 17:14:32 -0600 Date: Tue, 15 Jan 2019 17:14:29 -0600 From: "Gustavo A. R. Silva" To: Jiri Pirko , Ido Schimmel , "David S. Miller" Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH net-next] mlxsw: spectrum_nve: Use struct_size() in kzalloc() Message-ID: <20190115231429.GA25525@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: 1gjXui-002jld-3n X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:56030 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 15 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 issue was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c index fb1c48c698f2..9019548d731b 100644 --- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c +++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c @@ -267,8 +267,8 @@ mlxsw_sp_nve_mc_record_create(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_nve_mc_record *mc_record; int err; - mc_record = kzalloc(sizeof(*mc_record) + num_max_entries * - sizeof(struct mlxsw_sp_nve_mc_entry), GFP_KERNEL); + mc_record = kzalloc(struct_size(mc_record, entries, num_max_entries), + GFP_KERNEL); if (!mc_record) return ERR_PTR(-ENOMEM); -- 2.20.1