Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp998123imu; Fri, 4 Jan 2019 11:00:40 -0800 (PST) X-Google-Smtp-Source: ALg8bN57dHjR83M2vCT4Zvm91t2r6AK35n40QsEZ6d69YBFeuZlPqHEdhnjZqbhZ/+VwhvXLtdJS X-Received: by 2002:a17:902:f20d:: with SMTP id gn13mr50014154plb.11.1546628440463; Fri, 04 Jan 2019 11:00:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546628440; cv=none; d=google.com; s=arc-20160816; b=oXl+boA9Zt0akKAC7eyqtbJ6WyLI1HdnkwQ3LPraPdvoA5KghEHgB9s9TTzCV/k8on 0Eol9lWYWew+VyD6ikBO2b1zQmn8ZHIwu9viCDa+FuLvZ7PxkNJrou/e3muOKcdqBm8U WcHu9KIep7ZdHMIget+iGVh8ptEFiY+fufXqXcerT70AZpdbIyuG+k3+Omdde4H9/t5F jChl1JVEu+kCbSt36ZbmIiFGruM11tUPFN/NMciHIP5fi0Bi0Jc3vLfJtkjksFEHZkD1 Er+A7nMpk7woTLHUvJWcBVBIKLfFNqdZ9G9btVC6XKNLwYBCeCzBq1ai27h5kHfmj3Ap U5HQ== 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=0XW1+CfUeCVt3fUDu1U98rmPterJ5a9KwvTe0LCnq28=; b=bxNZA5TFOI5qPdZGmq9+8UimDyKLzopNvGTSabG5XCRGAh5IitPeXeH6mLEs2d4kE/ 3DOxLUEbhGFi3SCD5xX/u2XdqytkTwYPtancJbVzgIavYgGXK12wnlrstnXiLDjno+B8 OeYG9Hfrazt6oS3NWrFtXujH2by/ZBD7BMjuVaWpn1HQlbvkYAhQWjHBYUQo2gpAU1Aw vxdz02Nb7OvE06uBqtPAhxaJUfhlEJK3GXlCxW6T0amcdhOwlbwP8ycSSi804PW3rpp4 cyUOSfDmTkuDEEsH228u6RSBYTxeDEJtbRSKSPKKIieKKGjrSfDSJMbG2pdqJDCgzznh hYFg== 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 n1si55013944pgh.172.2019.01.04.11.00.25; Fri, 04 Jan 2019 11:00:40 -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 S1728140AbfADR0n (ORCPT + 99 others); Fri, 4 Jan 2019 12:26:43 -0500 Received: from gateway24.websitewelcome.com ([192.185.50.84]:49573 "EHLO gateway24.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726760AbfADR0m (ORCPT ); Fri, 4 Jan 2019 12:26:42 -0500 Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway24.websitewelcome.com (Postfix) with ESMTP id 25C43ED6E for ; Fri, 4 Jan 2019 11:26:42 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id fTF4gdZlQiQerfTF4gfxlk; Fri, 04 Jan 2019 11:26:42 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=35220 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gfTF2-000LQY-LV; Fri, 04 Jan 2019 11:26:41 -0600 Date: Fri, 4 Jan 2019 11:26:37 -0600 From: "Gustavo A. R. Silva" To: Boris Brezillon , Miquel Raynal , Richard Weinberger , David Woodhouse , Brian Norris , Marek Vasut , Maxime Ripard , Chen-Yu Tsai Cc: linux-mtd@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] mtd: rawnand: sunxi: Use struct_size() in devm_kzalloc() Message-ID: <20190104172637.GA28084@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: 1gfTF2-000LQY-LV X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:35220 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 23 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 = devm_kzalloc(dev, 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 = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL); This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/mtd/nand/raw/sunxi_nand.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c index e828ee50a201..f66fffe663bf 100644 --- a/drivers/mtd/nand/raw/sunxi_nand.c +++ b/drivers/mtd/nand/raw/sunxi_nand.c @@ -1870,10 +1870,7 @@ static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc, return -EINVAL; } - chip = devm_kzalloc(dev, - sizeof(*chip) + - (nsels * sizeof(struct sunxi_nand_chip_sel)), - GFP_KERNEL); + chip = devm_kzalloc(dev, struct_size(chip, sels, nsels), GFP_KERNEL); if (!chip) { dev_err(dev, "could not allocate chip\n"); return -ENOMEM; -- 2.20.1