Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5191241imu; Tue, 8 Jan 2019 13:11:33 -0800 (PST) X-Google-Smtp-Source: ALg8bN7l2IGRBG7JOdvuO7wzAlUWGVq6JcsI0r5USvanoP5RSmuXxJiAlUVx3UNsvV/TsdNJ8Ktf X-Received: by 2002:a17:902:a411:: with SMTP id p17mr3304808plq.292.1546981893001; Tue, 08 Jan 2019 13:11:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546981892; cv=none; d=google.com; s=arc-20160816; b=AMNaIcyHNzTS/5vby01kHMcj3JS3KTFpMfYHhHpaN/SCSwcpuwh8d9Z6PopMMXfHcR 5hkhesg9XER/iLYzqjOGuWUS8hV0rhqjPKjDBIB2J/iyhK+Lr6Ne+RKv5+uU+numQKXI KqaDXMqyPhNTEJA9+kxn3hZKEUCMU3CjF+BUPRrmFGdK/u+KYr4tz+9U0etk1KKsN13k 1fEK1KVB14RNR2w7Fqb+skX+amsnWP56TTl7i6sxPSXL5wRENOTibU96S3AhobdcYrgs Q69NH5W4XpJrafXg70Emd1549xIT2iIESB0vZUa/f7YWvTM6mQkPb/ZScIAh5xyl5crG +YYg== 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=gAiodwYIIMyPAVd2eNk6dnNHeX6VuP/4GFCj5LC99yc=; b=nggo4BWciQBy3g7Gzku2ZzR7zTCEyupzI8vebu97vHAHhSP+pY/M2b0oKIybbJ0+CH SqQpL0NIAubDWZnSfZWycOkEO7IoG4Q+6/HbRcXm8ZFJJpSi9+2W7OhZFhHSFQN5xfRt +QJyLg3Le8w4mX9vff+VgmACqZb/O3x5Sl5uUu3LL52MclitmBN3IdDnE5LkuVT8tC7h JAQpBenly6MYjYbR1uF1NvVa4e5AUcoYLLLo/njKzmTXTR0c2/2gq01zLIrCEGI8rVDZ rNxjrXpvyJoiYc9FebQQVBrnrnTDLiXpcYFbuD4xdpwFDeSTl1k61vtRfec+KD6wtEWg qTlw== 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 y15si3593824pgf.321.2019.01.08.13.11.15; Tue, 08 Jan 2019 13:11:32 -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 S1729605AbfAHVJt (ORCPT + 99 others); Tue, 8 Jan 2019 16:09:49 -0500 Received: from gateway22.websitewelcome.com ([192.185.47.100]:32441 "EHLO gateway22.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728794AbfAHVJt (ORCPT ); Tue, 8 Jan 2019 16:09:49 -0500 Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway22.websitewelcome.com (Postfix) with ESMTP id 60FBCB562 for ; Tue, 8 Jan 2019 15:09:48 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id gydAgxcvN2PzOgydAgcGA7; Tue, 08 Jan 2019 15:09:48 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=59602 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1ggyd9-000BII-Mw; Tue, 08 Jan 2019 15:09:47 -0600 Date: Tue, 8 Jan 2019 15:09:46 -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: lpddr: Use struct_size() in kzalloc() Message-ID: <20190108210946.GA4014@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: 1ggyd9-000BII-Mw X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:59602 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 13 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 code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/mtd/lpddr/qinfo_probe.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/mtd/lpddr/qinfo_probe.c b/drivers/mtd/lpddr/qinfo_probe.c index 69f2112340b1..175bdc3b72f4 100644 --- a/drivers/mtd/lpddr/qinfo_probe.c +++ b/drivers/mtd/lpddr/qinfo_probe.c @@ -181,8 +181,8 @@ static struct lpddr_private *lpddr_probe_chip(struct map_info *map) lpddr.numchips = 1; numvirtchips = lpddr.numchips * lpddr.qinfo->HWPartsNum; - retlpddr = kzalloc(sizeof(struct lpddr_private) + - numvirtchips * sizeof(struct flchip), GFP_KERNEL); + retlpddr = kzalloc(struct_size(retlpddr, chips, numvirtchips), + GFP_KERNEL); if (!retlpddr) return NULL; -- 2.20.1