Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp346648ybt; Fri, 26 Jun 2020 00:32:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzMBX6Ki7c5QR46U5PJ8h6bXO3mXdG0ngCfsJf9Y+DJztckcBF6mDbSr0T0mjpXLfXZa0H6 X-Received: by 2002:a17:906:6d4b:: with SMTP id a11mr1512040ejt.108.1593156777774; Fri, 26 Jun 2020 00:32:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593156777; cv=none; d=google.com; s=arc-20160816; b=ng1tChgp5wIXEgs1m7Bcrm232DL8fEmQQ3huXahbayZtb0OpKeY2vUbo61+zqLgVoq YPfmTWkbE2lbOjJL6yUETgIIBhEi3tR/Rr/5U28IA1ucLEEID7wphXcdcR5fh9CT0pSV i64jjkdLj/C2nW0Ht7jDDcDfxs9l0hJU2eSm/L81T8Ccv698bdtuoP5ldOGphnurkOT7 iSAjK1OsicKDaa9CmQOKuWlaUBG7/y9zBqXTDJ6ugmc3qOzi5Y3sDif7MfarUi99L2NL ADzx5QcIlrYaaowLox0zxjT5vS2CRxHQmn3lSD0/dZ3Fqc1Stc/2+3IwnabzZMZ+aYFp y3jA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=H7Sye2F6KYJGyt86hcv78Gg9uUrflv9GgwpNcNmMWqA=; b=uY5qQQZBNJQvtXVHZwbtY9i8aamsg8qZsOcDQWsK2r9WuP2+pNl8OSyu2JwmrYqr1E v1K28ZDfkIxgWbZOkhRvmxDkTUB3OJPYUHTQwTjpWCURG7K3NMVkVY0MaRHIIePOaMEo WHXS1Q9eDPEzIhswf5ijKW5qnmycleFHVUNoCGjOwRFTv7mvWlRLyeXNUpiYSPfQnkt4 AfG+DjIKe2cis4MggHIQmpqHjXkO2clUb/H8C90JFxXaPikuD+Xfyu5rgzl68QqIzGya sRYntdZkxoRKwKvh9uK0ngTJuxT4yj5j7l96FIPNG3Se/kCJ4+sBstpFOcQyxlm0Q0kq MsFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=qmi8KBoS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p5si12003706edq.110.2020.06.26.00.32.34; Fri, 26 Jun 2020 00:32:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=qmi8KBoS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728633AbgFZHaA (ORCPT + 99 others); Fri, 26 Jun 2020 03:30:00 -0400 Received: from lelv0143.ext.ti.com ([198.47.23.248]:35844 "EHLO lelv0143.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725855AbgFZHaA (ORCPT ); Fri, 26 Jun 2020 03:30:00 -0400 Received: from fllv0034.itg.ti.com ([10.64.40.246]) by lelv0143.ext.ti.com (8.15.2/8.15.2) with ESMTP id 05Q7TiKO110026; Fri, 26 Jun 2020 02:29:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1593156584; bh=H7Sye2F6KYJGyt86hcv78Gg9uUrflv9GgwpNcNmMWqA=; h=Subject:To:CC:References:From:Date:In-Reply-To; b=qmi8KBoS3LJBml2lCLL+R4h2DzChe0sIGuMZoPXycfI9jeo8yaJtuxe5BVmJYgjh1 PZbDJ06D6FPBdVDAXOWcdrx7wGJdEzrQNZIv6mRUkyzyhxSvVxawabrhynKB4K8yya 9kmXrg5m5Jp1qmBMC9DbdyU2WsdK0LxNWSUUBbjU= Received: from DLEE114.ent.ti.com (dlee114.ent.ti.com [157.170.170.25]) by fllv0034.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 05Q7TimS099997 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 26 Jun 2020 02:29:44 -0500 Received: from DLEE109.ent.ti.com (157.170.170.41) by DLEE114.ent.ti.com (157.170.170.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1979.3; Fri, 26 Jun 2020 02:29:43 -0500 Received: from fllv0040.itg.ti.com (10.64.41.20) by DLEE109.ent.ti.com (157.170.170.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1979.3 via Frontend Transport; Fri, 26 Jun 2020 02:29:43 -0500 Received: from [192.168.2.10] (ileax41-snat.itg.ti.com [10.172.224.153]) by fllv0040.itg.ti.com (8.15.2/8.15.2) with ESMTP id 05Q7Tgwm111765; Fri, 26 Jun 2020 02:29:42 -0500 Subject: Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc() To: "Gustavo A. R. Silva" , Vinod Koul , "Gustavo A. R. Silva" CC: Dan Williams , , References: <20200619224334.GA7857@embeddedor> <20200624055535.GX2324254@vkoul-mobl> <3a5514c9-d966-c332-84ba-f418c26fa74c@embeddedor.com> From: Peter Ujfalusi X-Pep-Version: 2.0 Message-ID: <98426221-8bff-25df-a062-9ec1ca4e8f26@ti.com> Date: Fri, 26 Jun 2020 10:30:37 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <3a5514c9-d966-c332-84ba-f418c26fa74c@embeddedor.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 24/06/2020 20.12, Gustavo A. R. Silva wrote: > Hi Vinod, >=20 > On 6/24/20 00:55, Vinod Koul wrote: >> On 19-06-20, 17:43, Gustavo A. R. Silva wrote: >>> Make use of the struct_size() helper instead of an open-coded version= >>> in order to avoid any potential type mistakes. >>> >>> This code was detected with the help of Coccinelle and, audited and >>> fixed manually. >>> >>> Addresses-KSPP-ID: https://github.com/KSPP/linux/issues/83 >>> Signed-off-by: Gustavo A. R. Silva >>> --- >>> drivers/dma/ti/k3-udma.c | 4 ++-- >>> 1 file changed, 2 insertions(+), 2 deletions(-) >>> >>> diff --git a/drivers/dma/ti/k3-udma.c b/drivers/dma/ti/k3-udma.c >>> index 0d5fb154b8e2..411c54b86ba8 100644 >>> --- a/drivers/dma/ti/k3-udma.c >>> +++ b/drivers/dma/ti/k3-udma.c >>> @@ -2209,7 +2209,7 @@ udma_prep_slave_sg_pkt(struct udma_chan *uc, st= ruct scatterlist *sgl, >>> u32 ring_id; >>> unsigned int i; >>> =20 >>> - d =3D kzalloc(sizeof(*d) + sglen * sizeof(d->hwdesc[0]), GFP_NOWAIT= ); >>> + d =3D kzalloc(struct_size(d, hwdesc, sglen), GFP_NOWAIT); >> >> struct_size() is a * b + c but here we need, a + b * c.. the trailing >> struct is N times here.. >> >=20 > struct_size() works exactly as expected in this case. :) > Please, see: >=20 > include/linux/overflow.h:314: > 314 #define struct_size(p, member, count) = \ > 315 __ab_c_size(count, = \ > 316 sizeof(*(p)->member) + __must_be_array((p)->mem= ber),\ > 317 sizeof(*(p))) True, struct_size is for this sort of things. Acked-by: Peter Ujfalusi While looking it up in include/linux/overflow.h I have noticed your commit in linux-next, which adds flex_array_size() The example in the commit message contradicts with what the helper does imho. To be correct it should have been: struct something { size_t count; struct foo items[]; }; - struct something *instance; + struct something instance; - instance =3D kmalloc(struct_size(instance, items, count), GFP_KERNEL); + instance.items =3D kmalloc(struct_size(instance, items, count), GFP_KER= NEL); instance->count =3D count; memcpy(instance->items, src, flex_array_size(instance, items, instance->c= ount)); If I'm not mistaken.. >=20 > Thanks > -- > Gustavo >=20 >>> if (!d) >>> return NULL; >>> =20 >>> @@ -2525,7 +2525,7 @@ udma_prep_dma_cyclic_pkt(struct udma_chan *uc, = dma_addr_t buf_addr, >>> if (period_len >=3D SZ_4M) >>> return NULL; >>> =20 >>> - d =3D kzalloc(sizeof(*d) + periods * sizeof(d->hwdesc[0]), GFP_NOWA= IT); >>> + d =3D kzalloc(struct_size(d, hwdesc, periods), GFP_NOWAIT); >>> if (!d) >>> return NULL; >>> =20 >>> --=20 >>> 2.27.0 >> - P=C3=A9ter Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/= Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki