From mboxrd@z Thu Jan 1 00:00:00 1970 From: Heiko Stuebner Subject: Re: [PATCH] drm/rockchip: Use struct_size() in devm_kzalloc() Date: Wed, 29 Aug 2018 13:41:50 +0200 Message-ID: <2639880.ggikvLCEDc@phil> References: <20180826184712.GA9330@embeddedor.com> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: base64 Return-path: In-Reply-To: <20180826184712.GA9330@embeddedor.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" To: "Gustavo A. R. Silva" Cc: Kees Cook , David Airlie , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-rockchip@lists.infradead.org, linux-arm-kernel@lists.infradead.org List-Id: linux-rockchip.vger.kernel.org QW0gU29ubnRhZywgMjYuIEF1Z3VzdCAyMDE4LCAyMDo0NzoxMiBDRVNUIHNjaHJpZWIgR3VzdGF2 byBBLiBSLiBTaWx2YToKPiBPbmUgb2YgdGhlIG1vcmUgY29tbW9uIGNhc2VzIG9mIGFsbG9jYXRp b24gc2l6ZSBjYWxjdWxhdGlvbnMgaXMgZmluZGluZwo+IHRoZSBzaXplIG9mIGEgc3RydWN0dXJl IHRoYXQgaGFzIGEgemVyby1zaXplZCBhcnJheSBhdCB0aGUgZW5kLCBhbG9uZwo+IHdpdGggbWVt b3J5IGZvciBzb21lIG51bWJlciBvZiBlbGVtZW50cyBmb3IgdGhhdCBhcnJheS4gRm9yIGV4YW1w bGU6Cj4gCj4gc3RydWN0IGZvbyB7Cj4gCWludCBzdHVmZjsKPiAgICAgICAgIHZvaWQgKmVudHJ5 W107Cj4gfTsKPiAKPiBpbnN0YW5jZSA9IGRldm1fa3phbGxvYyhkZXYsIHNpemVvZihzdHJ1Y3Qg Zm9vKSArIHNpemVvZih2b2lkICopICogY291bnQsIEdGUF9LRVJORUwpOwo+IAo+IG9yLCBsaWtl IGluIHRoaXMgcGFydGljdWxhciBjYXNlOgo+IAo+IHNpemUgPSBzaXplb2Yoc3RydWN0IGZvbykg KyBzaXplb2Yodm9pZCAqKSAqIGNvdW50Owo+IGluc3RhbmNlID0gZGV2bV9remFsbG9jKGRldiwg c2l6ZSwgR0ZQX0tFUk5FTCk7Cj4gCj4gSW5zdGVhZCBvZiBsZWF2aW5nIHRoZXNlIG9wZW4tY29k ZWQgYW5kIHByb25lIHRvIHR5cGUgbWlzdGFrZXMsIHdlIGNhbgo+IG5vdyB1c2UgdGhlIG5ldyBz dHJ1Y3Rfc2l6ZSgpIGhlbHBlcjoKPiAKPiBpbnN0YW5jZSA9IGRldm1fa3phbGxvYyhkZXYsIHN0 cnVjdF9zaXplKGluc3RhbmNlLCBlbnRyeSwgY291bnQpLAo+IEdGUF9LRVJORUwpOwo+IAo+IFRo aXMgaXNzdWUgd2FzIGRldGVjdGVkIHdpdGggdGhlIGhlbHAgb2YgQ29jY2luZWxsZS4KPiAKPiBT aWduZWQtb2ZmLWJ5OiBHdXN0YXZvIEEuIFIuIFNpbHZhIDxndXN0YXZvQGVtYmVkZGVkb3IuY29t PgoKYXBwbGllZCB0byBkcm0tbWlzYyB3aXRoIEtlZXMnIFJldmlldwoKVGhhbmtzCkhlaWtvCgoK X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KZHJpLWRldmVs IG1haWxpbmcgbGlzdApkcmktZGV2ZWxAbGlzdHMuZnJlZWRlc2t0b3Aub3JnCmh0dHBzOi8vbGlz dHMuZnJlZWRlc2t0b3Aub3JnL21haWxtYW4vbGlzdGluZm8vZHJpLWRldmVsCg== From mboxrd@z Thu Jan 1 00:00:00 1970 From: heiko@sntech.de (Heiko Stuebner) Date: Wed, 29 Aug 2018 13:41:50 +0200 Subject: [PATCH] drm/rockchip: Use struct_size() in devm_kzalloc() In-Reply-To: <20180826184712.GA9330@embeddedor.com> References: <20180826184712.GA9330@embeddedor.com> Message-ID: <2639880.ggikvLCEDc@phil> To: linux-arm-kernel@lists.infradead.org List-Id: linux-arm-kernel.lists.infradead.org Am Sonntag, 26. August 2018, 20:47:12 CEST schrieb Gustavo A. R. Silva: > 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); > > or, like in this particular case: > > size = sizeof(struct foo) + sizeof(void *) * count; > instance = devm_kzalloc(dev, size, 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); > > This issue was detected with the help of Coccinelle. > > Signed-off-by: Gustavo A. R. Silva applied to drm-misc with Kees' Review Thanks Heiko From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93442C433F5 for ; Wed, 29 Aug 2018 11:42:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4552B20858 for ; Wed, 29 Aug 2018 11:42:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4552B20858 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sntech.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728445AbeH2Pig (ORCPT ); Wed, 29 Aug 2018 11:38:36 -0400 Received: from gloria.sntech.de ([185.11.138.130]:43610 "EHLO gloria.sntech.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727514AbeH2Pig (ORCPT ); Wed, 29 Aug 2018 11:38:36 -0400 Received: from wd0180.dip.tu-dresden.de ([141.76.108.180] helo=phil.localnet) by gloria.sntech.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1fuyr9-0001Q0-64; Wed, 29 Aug 2018 13:41:51 +0200 From: Heiko Stuebner To: "Gustavo A. R. Silva" Cc: Sandy Huang , David Airlie , dri-devel@lists.freedesktop.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, Kees Cook Subject: Re: [PATCH] drm/rockchip: Use struct_size() in devm_kzalloc() Date: Wed, 29 Aug 2018 13:41:50 +0200 Message-ID: <2639880.ggikvLCEDc@phil> In-Reply-To: <20180826184712.GA9330@embeddedor.com> References: <20180826184712.GA9330@embeddedor.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Am Sonntag, 26. August 2018, 20:47:12 CEST schrieb Gustavo A. R. Silva: > 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); > > or, like in this particular case: > > size = sizeof(struct foo) + sizeof(void *) * count; > instance = devm_kzalloc(dev, size, 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); > > This issue was detected with the help of Coccinelle. > > Signed-off-by: Gustavo A. R. Silva applied to drm-misc with Kees' Review Thanks Heiko