Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4994667imu; Tue, 8 Jan 2019 09:39:30 -0800 (PST) X-Google-Smtp-Source: ALg8bN7nveIQVbGXlKC/GhZlH7h5nypda5TWOiBa9o8OIjU4XpRU/w3kNuzSUbUegdZiKOXCWZke X-Received: by 2002:a63:f94c:: with SMTP id q12mr2281837pgk.91.1546969170038; Tue, 08 Jan 2019 09:39:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546969170; cv=none; d=google.com; s=arc-20160816; b=Bgv0Y6u9AAlDjpF3WJknWd3f0sIL9WIH8ecjNpTix0HRUlIUJe3nw98coHY0O/qKXU 99/OQBSYfWDXX9oypBNaMExUL2s20nC2CF9Xg92n/uLHil6c9fJ/ITbSCOP2NlT7LU1W dFnrNAsvFhc3HP3M/i2jYTi5ON6KW1Ut0O3ZWQ5rRcDrYLJjtqphPy4DG5nJCIx3VKZ9 ugj/902LrelVTX4a17CUFQOAHqezPoZnvt6cxKJ+Havpmi4EiRFbZaVxSXGEG15SSjT4 ibgHHQDQ6CQ/6p8XzMovov4jUCUXSbw1EfYfLPdZbHzAOg6R/lMVP5sHwhXRHBiDPd49 1t5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:in-reply-to :subject:cc:to:from:date; bh=zV8JPWwEmUT8rtvSf/1L1NCW2TIKaofAR/Komf6c6RA=; b=ETJc/4EOz17HRvIhN5ZB4GGvLba/NVRLiCc9LBgKBR0gsma2SLGS88OE17Kwep1Gm0 Xqc+mM8J3DjbVXKpXi1urXPPdiYqWazW06xmm0y4N6i2MYUaBU6aPVGhwOmwrMwnbqj8 a7HCqtrDWqPu/vXR6N6BIibQeF7JNiOIhB7+8x6aGmfNsbbIclO8WUfNs7jZhnjPr5nk zdXolzapjuJb5O0d7v5h7k+ocoGN49mtAcRX/zDJhl1M8p+Fjdk3fsmca6ltsQPpBUAc dOphxEo/NZcSB6QAJ8p+lOt0NNDpr4BN0iKk2qNmXIzUhSM0vCjZ+FqawyyzXTZ8onwg NUpQ== 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 c139si3944095pfb.281.2019.01.08.09.39.13; Tue, 08 Jan 2019 09:39:30 -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 S1729148AbfAHRgm (ORCPT + 99 others); Tue, 8 Jan 2019 12:36:42 -0500 Received: from iolanthe.rowland.org ([192.131.102.54]:53928 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1727484AbfAHRgl (ORCPT ); Tue, 8 Jan 2019 12:36:41 -0500 Received: (qmail 3960 invoked by uid 2102); 8 Jan 2019 12:36:40 -0500 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 8 Jan 2019 12:36:40 -0500 Date: Tue, 8 Jan 2019 12:36:40 -0500 (EST) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: "Gustavo A. R. Silva" cc: Greg Kroah-Hartman , , Subject: Re: [PATCH] USB: core: urb: Use struct_size() in kmalloc() In-Reply-To: <20190108152225.GA5721@embeddedor> Message-ID: MIME-Version: 1.0 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 On Tue, 8 Jan 2019, 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 = kmalloc(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 = kmalloc(struct_size(instance, entry, count), GFP_KERNEL); > > This code was detected with the help of Coccinelle. > > Signed-off-by: Gustavo A. R. Silva > --- > drivers/usb/core/urb.c | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > > diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c > index f51750bcd152..0eab79f82ce4 100644 > --- a/drivers/usb/core/urb.c > +++ b/drivers/usb/core/urb.c > @@ -70,9 +70,8 @@ struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags) > { > struct urb *urb; > > - urb = kmalloc(sizeof(struct urb) + > - iso_packets * sizeof(struct usb_iso_packet_descriptor), > - mem_flags); > + urb = kmalloc(struct_size(urb, iso_frame_desc, iso_packets), > + mem_flags); > if (!urb) > return NULL; > usb_init_urb(urb); Acked-by: Alan Stern