Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4068347imu; Mon, 7 Jan 2019 15:02:56 -0800 (PST) X-Google-Smtp-Source: AFSGD/Uula2UIqYtWWmLIkY/C9psjwiUQbg43rjTlo6nujIaZT9xbUKv2VpLr9Vb9XRXdWUdMhOA X-Received: by 2002:a62:d448:: with SMTP id u8mr64813865pfl.105.1546902176774; Mon, 07 Jan 2019 15:02:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546902176; cv=none; d=google.com; s=arc-20160816; b=mNV+pb6XtiLulkUO9Ei1r30/iXhuTdcBHOFCsy/zsIkBAEXd19mmGCx3LYMd+8oro3 tcXQGRkdbNKrmmjEYYIbAkG9xnjAkj8IS5v0i5t+QpTRNsg9dBchcIHFsP71ud6GjtRW puQybkZ3E35BKiXE63sx2a19bMAjaqrmHl6EpnW4Df8dkbX47QxYEO6VCvXJsnp1OyMb X0KttMit/vkREgH5Ag/WMSguiCWtOcKEqs0Q65z6uyxNqRdWK23iZfLl9gZNLjN+P5IA hYjNqhRP1lz2s+mUt+Vnqti2a7tWZueKfYzQS6mvddn8VgmRLr35DkBhnqO3xmRUHOOG WdJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=0h0Ha7r0BaRhtlNIOmdmovDFXMe2CkC1Z2XLUJXCAeg=; b=p5nEKVyZUuNs4+pRPgIlz7qCrXfNeTHX+Zi0/SyBGjPGa00sorHeT2FDRL+nA03DMo APb7qCeL0IbMGeqW2Tc+/1WhEhi4awedUXaANdZoV02IP9hiHOwdpmEX6ezFzN0CzTwY zz9dhoH1Yufiu78ibOmNc0Ud/KnYEgBv7w1KeDaD2UDC7wV9ekB1kxshgFNe/PtmF2yi 7MJUJnZhP7hfG6+r1UdS6bJaE5zceWY8Z1H2bZu1HMfkq5ratvdXijVTWQjxbtimmB7f mD+4Fg/0L8tZ7W4FZiwkqWtDQdCyE8iczM4d76bg/TdHJCSVxhVfu6Y6m7hkbc/YqgR+ D9Pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=d7AsFt5c; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 35si18514196plf.177.2019.01.07.15.02.41; Mon, 07 Jan 2019 15:02:56 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=d7AsFt5c; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727052AbfAGXBe (ORCPT + 99 others); Mon, 7 Jan 2019 18:01:34 -0500 Received: from mail-qt1-f194.google.com ([209.85.160.194]:35875 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726667AbfAGXBd (ORCPT ); Mon, 7 Jan 2019 18:01:33 -0500 Received: by mail-qt1-f194.google.com with SMTP id t13so2445675qtn.3; Mon, 07 Jan 2019 15:01:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0h0Ha7r0BaRhtlNIOmdmovDFXMe2CkC1Z2XLUJXCAeg=; b=d7AsFt5cYvoY3mCX2evgEfoR630LCPgXzriCrDL6hMyEpQX8kLLAb1l8jKuATzKQwg 61mk5ZFyih3jObZHTjFFN9jQZq1mTMWzRxq0UnT8rgBWj+YhACwBAWQkzFXU+f41cTur dcMhRq41G49xrVUt2RmNsZI95X/Bqa3KvUJcSfcl6c8ZLnmysC4OyYdmAjSBVKVPiAI+ uwumJkjqRIEOUi59FoORYRrgRJtE1pUfZW53SYrlXajWNg99QXXV67aOTU83PRzBf0D5 PuVU8R9STYfeJWDQpXtiAUwO+mqKCSEvfkPrYFmRcYoqOa/+z25B38XeYiumlKBViEqy mtKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0h0Ha7r0BaRhtlNIOmdmovDFXMe2CkC1Z2XLUJXCAeg=; b=ufwfEfBSwAHi7bvSRtHTWVcwecjYBTqNUP1WTqLuodZeaFvmOht208lb1EWnyMDrSC x+xiENPPE8LnCdoRlearw42o6NHWznDNOhFadEbBoG1jLvvIipk2E6bXptKnBi/UHd97 qkTecAn+ivrGtrbGtqP5J/ot8FB+hBtKgnXO/j0+bKBtz1b/0dJkCeCFgqbLUHoQ1vXY RnXi2yxBBcAXeBhsue/H7XwKjNnhAPu+lBl27IqufXwGjslW+7xH54vfnki1uv6rdCMq VfyzWXS9aLpNFNSZUgjCf5aMkT6blm9Uh2hGECsS7fJO+jMJYGBHT9Ld9Qb5viHNNDpp CDFA== X-Gm-Message-State: AA+aEWblDyG7zjP/EOH5stO7bnPATd6o4XM0Th+nqCCdpm7mnSn/KFrr V9dhKVPKYpC5BA6HRqhyp3yLkpvd7pYCXA6Vsqma4joL X-Received: by 2002:aed:2798:: with SMTP id a24mr61551842qtd.138.1546902092691; Mon, 07 Jan 2019 15:01:32 -0800 (PST) MIME-Version: 1.0 References: <20190107174538.GA7255@embeddedor> In-Reply-To: <20190107174538.GA7255@embeddedor> From: Song Liu Date: Mon, 7 Jan 2019 15:01:21 -0800 Message-ID: Subject: Re: [PATCH] md-linear: use struct_size() in kzalloc() To: "Gustavo A. R. Silva" Cc: Shaohua Li , linux-raid@vger.kernel.org, open list Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 7, 2019 at 10:35 AM Gustavo A. R. Silva wrote: > > 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 Acked-by: Song Liu > --- > 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 >