Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4890956imu; Tue, 8 Jan 2019 08:01:10 -0800 (PST) X-Google-Smtp-Source: ALg8bN4OZdMGQ+kIbpfalE4/8lsMaNsVtNR4/NFnrKYYdo1WeanirajYoH8LoO2JiDKxw40pcZn7 X-Received: by 2002:a17:902:bcc7:: with SMTP id o7mr2328725pls.281.1546963270294; Tue, 08 Jan 2019 08:01:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546963270; cv=none; d=google.com; s=arc-20160816; b=CyzOORvKoCEn0f+Aam7ryjxLC8g6sL1/3cNQrZk538OQk1Bl8+AkQI/al+GNJZ/BdW VtbO8esfGOhJ7FGnwqSRWKVkzcnsGcS18iFpnIbY4zt7XCTDmSwNfLxd7J4HXLAaYBRN S7S8fgCXKFoJYvpldNQJ7mSys9X+rrVEVcNNLOiCUSl5m7kHzKLAzVWuLvqZE5Bd0dL5 tqQs6N7mo9hlNjPG3wy2BPvdAIa9JkFF/CWKuoDNFM2uROBF1YGjnWGexlZuiy8akg0C 2cqrhDBvdfqETJjRNoWQ699uv7hx6YBsgsHvguf1bmcQzuWnIxCASrzi7uletr5rYs1b F+Lw== 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=j90a2jgUWLhp/ewptAU8IJM9uXpuSnxQCLgsi0rdHcE=; b=HIRliiRr4xr6A1lL4ERTyMwwg1aYlibuY2uKUKBvPkTJB6k7rvcErdTUd/kKpW86/B te3jUEoA6zSHKq3Q14LLReb7x5RZc6/jY/btf2BxYEc7HNXNLAtsaN0TkvgK2i1RFgeu /yxJF2n2amLTO5KrUft5WuxfDusbmPEBO5RyfL5AvZXRAwlJd8pAo3uvxVCHQnj9pIxh 6Abi+cyxNvkBzMwKwZ8rbIAmg9adWFWkBVslyabquDsYcvK7peLSHsvIZ5/3jte38gSk scGexKWkh1pLo7oF4/ODmzDBjksPn61b2ESqET8z51tWjIImECvKuS4ys/+DB8K4TiH/ 2gOg== 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 s191si46853326pfs.53.2019.01.08.08.00.54; Tue, 08 Jan 2019 08:01:10 -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 S1729184AbfAHP7T (ORCPT + 99 others); Tue, 8 Jan 2019 10:59:19 -0500 Received: from gateway20.websitewelcome.com ([192.185.4.169]:31398 "EHLO gateway20.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727840AbfAHP7T (ORCPT ); Tue, 8 Jan 2019 10:59:19 -0500 X-Greylist: delayed 1391 seconds by postgrey-1.27 at vger.kernel.org; Tue, 08 Jan 2019 10:59:19 EST Received: from cm16.websitewelcome.com (cm16.websitewelcome.com [100.42.49.19]) by gateway20.websitewelcome.com (Postfix) with ESMTP id 09291400C5D3A for ; Tue, 8 Jan 2019 09:36:08 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id gtQFgCNSW4FKpgtQGgdunH; Tue, 08 Jan 2019 09:36:08 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=52150 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1ggtQE-001MAR-Vt; Tue, 08 Jan 2019 09:36:07 -0600 Date: Tue, 8 Jan 2019 09:36:01 -0600 From: "Gustavo A. R. Silva" To: David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] mtd: gen_probe: Use struct_size() in kmalloc() Message-ID: <20190108153601.GA11557@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: 1ggtQE-001MAR-Vt X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:52150 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 16 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 = kmalloc(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 = kmalloc(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/chips/gen_probe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/mtd/chips/gen_probe.c b/drivers/mtd/chips/gen_probe.c index 837b04ab96a9..839ed40625d6 100644 --- a/drivers/mtd/chips/gen_probe.c +++ b/drivers/mtd/chips/gen_probe.c @@ -135,7 +135,7 @@ static struct cfi_private *genprobe_ident_chips(struct map_info *map, struct chi * our caller, and copy the appropriate data into them. */ - retcfi = kmalloc(sizeof(struct cfi_private) + cfi.numchips * sizeof(struct flchip), GFP_KERNEL); + retcfi = kmalloc(struct_size(retcfi, chips, cfi.numchips), GFP_KERNEL); if (!retcfi) { kfree(cfi.cfiq); -- 2.20.1