Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2276870imm; Thu, 23 Aug 2018 18:12:38 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxniwsouYDbp6ZYcYPeYYQxTYRkjJuNoo61Ua18oWSUdF47uiPW+liYTfhxXNQo+t+9bD7t X-Received: by 2002:aa7:8087:: with SMTP id v7-v6mr65116329pff.38.1535073158625; Thu, 23 Aug 2018 18:12:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535073158; cv=none; d=google.com; s=arc-20160816; b=etQBHg6/0W4EN+/Ycf6JkZqcmlKFeR12oODboX7ZXB3NhSEUUOKPUwJUnOffV/qFZ4 x1842NS+y6eJjJB0NV85Uv5s4WWtpTuvYz3DVogvBiCJCbzM1lRtDaiT0VquYGSul3xI 6nbvZxlJv47BE/dE3qcR8WzhidJnTIwgJ4LKMd0BRHVD0mB16Mp6DjekqboHE0iKHrNJ S+3M1egOuC8Kr9snINP2JMP8BHT84T+3gxqiZ00LmUN6WMMqRMTFVt/tmtAC2RQppfUw 7SH7DFwEijOwemLGEPSeZ0pCXwHYzG/ydWwe3sK0b1EuGyh7UXjiowrNNYyILAbY7HYy IDfg== 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 :arc-authentication-results; bh=iXftqjYi8jFT08UnvXifKHsMeA33SXCtbzjjLFpgP2E=; b=wpJt/RfrfcCb3uXAdWPKaZF0bC/1v1KV0qgvclG3FjIeiGdfMOX+LswsWvkQUDXGN1 uuyNqutOSowe2hhilHpIZulOn8ZAoCTZdH4wf7QGsdQOOCUNC2uFFyCpfFOf847v01cM RhWmaOx4abAp6ZlZb/GbzFbBb9Dm2t7jpA+m3yfMkoEyj7VZcpwpxAP/vIB66oGoUOVh DkTBVuiF4fGpkpPTTvWw3zWr+Y26aYYyPPV97s0gH2Sy+UXlQQEKguWIp+AiqQmOPNS6 si9GQfFTm9ztqjP1HxcXvg6YLbWMPqRxIQ0uwCVwPbHXZuK9alrYlQUDhbMDaKaT97Do o4vA== 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 r17-v6si5649134pgo.606.2018.08.23.18.12.22; Thu, 23 Aug 2018 18:12:38 -0700 (PDT) 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 S1726519AbeHXEnd (ORCPT + 99 others); Fri, 24 Aug 2018 00:43:33 -0400 Received: from gateway34.websitewelcome.com ([192.185.149.13]:35241 "EHLO gateway34.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725945AbeHXEnd (ORCPT ); Fri, 24 Aug 2018 00:43:33 -0400 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway34.websitewelcome.com (Postfix) with ESMTP id E9351168920 for ; Thu, 23 Aug 2018 20:09:39 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id t0bbfITdVkBj6t0bbfXHvT; Thu, 23 Aug 2018 20:09:39 -0500 X-Authority-Reason: nr=8 Received: from [189.250.73.28] (port=44124 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1ft0ba-003Sfb-V3; Thu, 23 Aug 2018 20:09:39 -0500 Date: Thu, 23 Aug 2018 20:09:38 -0500 From: "Gustavo A. R. Silva" To: Josh Wu , Boris Brezillon , Miquel Raynal , Richard Weinberger , David Woodhouse , Brian Norris , Marek Vasut , Nicolas Ferre , Alexandre Belloni Cc: linux-mtd@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Kees Cook , "Gustavo A. R. Silva" Subject: [PATCH] mtd: rawnand: atmel: use struct_size() in devm_kzalloc() Message-ID: <20180824010938.GA25581@embeddedor.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) 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.73.28 X-Source-L: No X-Exim-ID: 1ft0ba-003Sfb-V3 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.73.28]:44124 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 24 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 issue was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/mtd/nand/raw/atmel/nand-controller.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/mtd/nand/raw/atmel/nand-controller.c b/drivers/mtd/nand/raw/atmel/nand-controller.c index a068b21..b17cfd0 100644 --- a/drivers/mtd/nand/raw/atmel/nand-controller.c +++ b/drivers/mtd/nand/raw/atmel/nand-controller.c @@ -1586,9 +1586,7 @@ static struct atmel_nand *atmel_nand_create(struct atmel_nand_controller *nc, return ERR_PTR(-EINVAL); } - nand = devm_kzalloc(nc->dev, - sizeof(*nand) + (numcs * sizeof(*nand->cs)), - GFP_KERNEL); + nand = devm_kzalloc(nc->dev, struct_size(nand, cs, numcs), GFP_KERNEL); if (!nand) { dev_err(nc->dev, "Failed to allocate NAND object\n"); return ERR_PTR(-ENOMEM); -- 2.7.4