Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2129892imm; Thu, 23 Aug 2018 14:52:26 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZobEujCA+45km43jMC5KV0UbAyEI2RBTOfp9cmS7/WF0W5ZrI3ScDwf8CyX+u0g9ZsWHcg X-Received: by 2002:a17:902:b68d:: with SMTP id c13-v6mr11608630pls.167.1535061146194; Thu, 23 Aug 2018 14:52:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535061146; cv=none; d=google.com; s=arc-20160816; b=NZisgtjfryDSL0v5WmAYPNjIbpuhbwA+UlZsbJ3F5az9XGUGHr7dL/OeebkyR7ugZA yxqZyns5ugLeDQ1hERwmkrNnAr+rJHRIZbEcXbThE9czbcpvhrtL93LRlMIGI521EO8u W3WNKaeL0Hx4eivqgYkS6M47mSrnemPoJybX9/SAL93tQ8yNAefwQykueCkGBMIDqI5A Upun/EynJNf9xx4ZRwxBnAL3H94Iw+J/JFofOMmrQXGGkJleOybLqfx9yIRLkZLFRrrD gJaK1EBywtm4dn1feo0vV7CjW6sP6ZprhRfIOxIv1tH70/QLy+buhxW8E+VdjEMUEHTO PXBw== 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 :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=VFb6OoLEOQcDG2HrU8ctw0XRRv7XW1+1y1TbsuyGtac=; b=nW0ySNj0it1QrjPd1tgw8NYtF+s/WbLB+iEZLYbeaqowYZbIEKQTL+eBz19iO+HWTF CZDFiRwYo3FWGVuru7i3uWBmzqSRD2H7/F3JF44Mm8DZ43vUQ2vR1x6rBQQO77VVAw/1 7OqjpSaMm/1oLdp1kw24TrS1uryMiCMLyuucmwgqJvYKbpKqy5soMwb3fupLtzNB731I Ys56qkjdNOrc5l0rnem/kGtzfQMfm305lsYPxn/mk0vKkiwtbOI4a2j+FW515vgmOjcV qtmhMcCemvMi2azh6rtQdeApuXtyFFkudH1O5EqMYxqvue+52fJzNtm5He+HNfwvcVos pEpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kq+iZ6U8; 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=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x69-v6si5868861pfe.318.2018.08.23.14.52.07; Thu, 23 Aug 2018 14:52:26 -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; dkim=pass header.i=@chromium.org header.s=google header.b=kq+iZ6U8; 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=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728166AbeHXBWb (ORCPT + 99 others); Thu, 23 Aug 2018 21:22:31 -0400 Received: from mail-yb0-f196.google.com ([209.85.213.196]:35632 "EHLO mail-yb0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726818AbeHXBWb (ORCPT ); Thu, 23 Aug 2018 21:22:31 -0400 Received: by mail-yb0-f196.google.com with SMTP id o17-v6so2773603yba.2 for ; Thu, 23 Aug 2018 14:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=VFb6OoLEOQcDG2HrU8ctw0XRRv7XW1+1y1TbsuyGtac=; b=kq+iZ6U8zkgXhN73N+UWmvJl6mUGqDnWuSVUqYDUpWnH78QUQPSQ9BT1QwyXjZBhj5 Gps9kgPjA4yhLd7AbiRYybiyXnpabpPx4nMhqkuh9IQxzj5FTprFslWpFJNkKUokm7Uv gwoOF4kDDBmB6aLSOSM4zpJsS6aM61fFzKdAc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=VFb6OoLEOQcDG2HrU8ctw0XRRv7XW1+1y1TbsuyGtac=; b=XCkVmHpi0RSGyud0fdIeAUPlv13+bT8MEvLtd74oOru5BGUAhfD7t2F9YCsmJegg8I tjtdZ5LdfzxsbsjhiRzzdAIUGj+H9BM3N6Lpt1Pey6Rt4Cs7Z27c8cojPH/SqmVTYb9+ fbkSwPcex+Q4wa7oP1hmoUimMOC0fXlYb2EIrHY2zVOouMKzg4W9u/fj9p2KyD0oZ/IU cuWmUnRig8hOcHrkZJxHfdEAyNn0LjNkcW9xM3DWwmuLGAGnxmnj/9ylB/JozBbIQzh+ YeurvEMOAYXuH9Y0Axzd8Z9oQPJONLA/sYptdX/HYOL4SHfbqdn+LJoMusk98XCnoey7 7z9w== X-Gm-Message-State: APzg51AU0Y1lQtA9yrShTkya7sxZ8sptUQlwr2gH8+FOJJaU9Vi6ge4J Bk8GV/8/CKl/2FReeebIsH95BwBATIw= X-Received: by 2002:a0d:d945:: with SMTP id b66-v6mr3896745ywe.493.1535061051777; Thu, 23 Aug 2018 14:50:51 -0700 (PDT) Received: from mail-yb0-f171.google.com (mail-yb0-f171.google.com. [209.85.213.171]) by smtp.gmail.com with ESMTPSA id 200-v6sm2576330ywr.102.2018.08.23.14.50.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Aug 2018 14:50:49 -0700 (PDT) Received: by mail-yb0-f171.google.com with SMTP id v13-v6so2757640ybq.12 for ; Thu, 23 Aug 2018 14:50:49 -0700 (PDT) X-Received: by 2002:a81:9fd6:: with SMTP id w205-v6mr4124292ywg.288.1535061049152; Thu, 23 Aug 2018 14:50:49 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:2c11:0:0:0:0:0 with HTTP; Thu, 23 Aug 2018 14:50:48 -0700 (PDT) In-Reply-To: <20180823193332.GA31602@embeddedor.com> References: <20180823193332.GA31602@embeddedor.com> From: Kees Cook Date: Thu, 23 Aug 2018 14:50:48 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] mtd: rawnand: jz4780: use struct_size() in devm_kzalloc() To: "Gustavo A. R. Silva" Cc: Harvey Hunt , Boris Brezillon , Miquel Raynal , Richard Weinberger , David Woodhouse , Brian Norris , Marek Vasut , Linux mtd , LKML 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 Thu, Aug 23, 2018 at 12:33 PM, 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 = 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); > > Signed-off-by: Gustavo A. R. Silva Reviewed-by: Kees Cook -Kees > --- > drivers/mtd/nand/raw/jz4780_nand.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/mtd/nand/raw/jz4780_nand.c b/drivers/mtd/nand/raw/jz4780_nand.c > index db4fa60..ac62395 100644 > --- a/drivers/mtd/nand/raw/jz4780_nand.c > +++ b/drivers/mtd/nand/raw/jz4780_nand.c > @@ -352,7 +352,7 @@ static int jz4780_nand_probe(struct platform_device *pdev) > return -ENODEV; > } > > - nfc = devm_kzalloc(dev, sizeof(*nfc) + (sizeof(nfc->cs[0]) * num_banks), GFP_KERNEL); > + nfc = devm_kzalloc(dev, struct_size(nfc, cs, num_banks), GFP_KERNEL); > if (!nfc) > return -ENOMEM; > > -- > 2.7.4 > -- Kees Cook Pixel Security