Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3867201imu; Mon, 7 Jan 2019 10:55:48 -0800 (PST) X-Google-Smtp-Source: ALg8bN4MpWh3JXZsn6VtyspMQJhlfSYCV/EQ3mTpWI4YL0vnQEQLISuMczeq6csSrGjteoFYBAlZ X-Received: by 2002:a63:6506:: with SMTP id z6mr11887407pgb.334.1546887348261; Mon, 07 Jan 2019 10:55:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546887348; cv=none; d=google.com; s=arc-20160816; b=xCUP6r2bMd/O7J+DZrst1KDLM85LB6CWzOoxECvLhk3fHfemMS4wsx/FUmaLkO6LGf JS4fcAgzhnVEOjQZ11AP0Vme9QQ3ZAMiG5KWCwTslDcQx0heakJtU2UEnFyPgLT4gML+ TnKmpsWpB1nRsnjGMcqG+jZIcUiIgDg3n2Rb8sEvaNq6Sp/0mLyMjXdS5Mgd53bhRMCs TFhn6bBdymYplR0mj2u91F7IFue4m3JvMfxr55Lb4Bb1JyWyYKzbLEfi0gJVOqQecfUK VAE7xHJrwOmUdMd0jPFSPakwBfRWfL0MZ63d/FyhyoiyvS3fLT4ZbyZm++797jsOut7U 8gpg== 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=GmxANh5aAUve9xC5fe9zGvJ16aO7X3c3+3tnDTChmCk=; b=YrY+tPyJJ3t/Kc8dwSG2UwGUxDRG9oHrx6pqsOTWtcGkNBm0xFnc+BSEzJdav5EhRr dQRlderSMJRVZZPTP81hhNGg3MQSzlHGd1MsWCQyRCcxALLrDouDH7ue4k7jPOpCSB7H tsCPV3ZcLrrWYM6FqBC24wcx8744R5PrF6SWecmZNrEXaVDWOSl4StDUQjpgRyHDBJ/1 TKg4617J2Y6D4nmsNe2wPCBX/OcFrbJPZ+rMWaHXPevXucTUrMu0nqzjb5HFV6q4Z2DP DNOUesvfKUqo3PHiUc0eytuENr1RXp8muHrWQLquzb7hRZMnmRpUMLAA1bnqQKHcciRi HCsw== 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 s123si22846890pgs.93.2019.01.07.10.55.33; Mon, 07 Jan 2019 10:55:48 -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 S1728343AbfAGRpl (ORCPT + 99 others); Mon, 7 Jan 2019 12:45:41 -0500 Received: from gateway24.websitewelcome.com ([192.185.50.71]:34536 "EHLO gateway24.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726942AbfAGRpk (ORCPT ); Mon, 7 Jan 2019 12:45:40 -0500 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway24.websitewelcome.com (Postfix) with ESMTP id 32B84429383 for ; Mon, 7 Jan 2019 11:45:40 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id gYy4gEOAv2qH7gYy4gtWk0; Mon, 07 Jan 2019 11:45:40 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=60502 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1ggYy3-002YJf-KX; Mon, 07 Jan 2019 11:45:39 -0600 Date: Mon, 7 Jan 2019 11:45:38 -0600 From: "Gustavo A. R. Silva" To: Shaohua Li Cc: linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] md-linear: use struct_size() in kzalloc() Message-ID: <20190107174538.GA7255@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: 1ggYy3-002YJf-KX X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:60502 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 = 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/md/md-linear.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/md/md-linear.c b/drivers/md/md-linear.c index d45c697c0ebe..5998d78aa189 100644 --- a/drivers/md/md-linear.c +++ b/drivers/md/md-linear.c @@ -96,8 +96,7 @@ static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks) int i, cnt; bool discard_supported = false; - conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info), - GFP_KERNEL); + conf = kzalloc(struct_size(conf, disks, raid_disks), GFP_KERNEL); if (!conf) return NULL; -- 2.20.1