Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3307723imu; Sun, 23 Dec 2018 22:25:38 -0800 (PST) X-Google-Smtp-Source: ALg8bN5rY/x8IOjUT64H+tbhS8oiJMvxBiRs+SfH/IVJAb9Yzcr0NOBLgTOat/7yubjY7JmrBQk3 X-Received: by 2002:a17:902:9006:: with SMTP id a6mr11776465plp.334.1545632738701; Sun, 23 Dec 2018 22:25:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545632738; cv=none; d=google.com; s=arc-20160816; b=NuQVU1xtGe1PdJN9yirsvQXKEELLKi42Jq2my/p1yw9d2e0aQMf9pGtt2MwxF69CxX SGOt4bzmIR2QwgYpiyUY8vOPm9ptlHfuGmhjvXanUeQ9cNtkeW7kuMwEivGZ/dahjnYX pQBNIqqAFeqstynKdlpcxtbjTb5qM4/oLNBA9BnSRnTv/COl6/Fg6gG43Ioakxj4TlQ5 /0/5D9IeHfz/EcL6iP9ixsX0n8tqNoGMWBVKRdK2bmt4SQjZbLOlK6JSANhMwcJXA79e YHTapNuTlTPI9aSERhiRqV3Im1O4w6UZA9yOoLpqGyZjmNZKc7RvSAJAUs6M4Huq8U6R Scrg== 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=Ek7Wg3SnwFWphAe0sQ3idPbdyLQbFQh3NVInV2hqYAc=; b=XI8E3PVh5/Ktf69SQTVIZnLbBM1oqCCz8xqTlDnhjGj0gg1aOp7j7VaurAjD6SkivM jxtUsAhETkgGLxDqcFg7o3q31LUogGiJ4+ZgZ7VRa4eJmp73kPWM4gpZDVP+9xkIqfPV sj2D5V/LI8tNVMJQBPsxUnEAUNze7pRAcRSgNuiGlyU8tvlkKsfmL2ga4tZ9FxrOtE/B ihdeo1aZPF4fjctpZFwCL568PQ/rVQuUrS5p2hefpVOCkBlzucEIH8tiwvhULdytlDLj B2uHV7cgHRHjqixnLrbTOBD9dzfgYldIBZk8boIBn8bLnTNUKsIXzTL/kzy1kljVbP+C 6bTQ== 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 90si25270748plb.17.2018.12.23.22.24.54; Sun, 23 Dec 2018 22:25:38 -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 S1725946AbeLXGWh (ORCPT + 99 others); Mon, 24 Dec 2018 01:22:37 -0500 Received: from gateway34.websitewelcome.com ([192.185.150.114]:38190 "EHLO gateway34.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725896AbeLXGWh (ORCPT ); Mon, 24 Dec 2018 01:22:37 -0500 Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway34.websitewelcome.com (Postfix) with ESMTP id A547F1F260F for ; Mon, 24 Dec 2018 00:22:36 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id bJdMg8vkCiQerbJdMgDZPv; Mon, 24 Dec 2018 00:22:36 -0600 X-Authority-Reason: nr=8 Received: from [189.250.106.44] (port=49928 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gbJdM-00049X-1E; Mon, 24 Dec 2018 00:22:36 -0600 Date: Mon, 24 Dec 2018 00:22:35 -0600 From: "Gustavo A. R. Silva" To: Andy Gross , David Brown Cc: linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] drivers: qcom: rpmh: use struct_size() in kzalloc() Message-ID: <20181224062235.GA27815@embeddedor.com> 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.106.44 X-Source-L: No X-Exim-ID: 1gbJdM-00049X-1E X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.106.44]:49928 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 4 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[]; }; instance = kzalloc(sizeof(struct foo) + sizeof(void *) * count, 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/soc/qcom/rpmh.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/soc/qcom/rpmh.c b/drivers/soc/qcom/rpmh.c index c7beb6841289..12c057a0b325 100644 --- a/drivers/soc/qcom/rpmh.c +++ b/drivers/soc/qcom/rpmh.c @@ -362,8 +362,7 @@ int rpmh_write_batch(const struct device *dev, enum rpmh_state state, if (!count) return -EINVAL; - req = kzalloc(sizeof(*req) + count * sizeof(req->rpm_msgs[0]), - GFP_ATOMIC); + req = kzalloc(struct_size(req, rpm_msgs, count), GFP_ATOMIC); if (!req) return -ENOMEM; req->count = count; -- 2.17.0