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=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 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 39EEDC433E0 for ; Thu, 28 May 2020 14:41:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 258B5207D3 for ; Thu, 28 May 2020 14:41:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728479AbgE1OlN (ORCPT ); Thu, 28 May 2020 10:41:13 -0400 Received: from foss.arm.com ([217.140.110.172]:53730 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725928AbgE1OlM (ORCPT ); Thu, 28 May 2020 10:41:12 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AEF3BD6E; Thu, 28 May 2020 07:41:11 -0700 (PDT) Received: from e121166-lin.cambridge.arm.com (e121166-lin.cambridge.arm.com [10.1.196.255]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F11383F52E; Thu, 28 May 2020 07:41:09 -0700 (PDT) Date: Thu, 28 May 2020 15:41:07 +0100 From: Lorenzo Pieralisi To: "Gustavo A. R. Silva" Cc: "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Rob Herring , Bjorn Helgaas , linux-hyperv@vger.kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , Kees Cook Subject: Re: [PATCH] PCI: hv: Use struct_size() helper Message-ID: <20200528144107.GB28290@e121166-lin.cambridge.arm.com> References: <20200525164319.GA13596@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200525164319.GA13596@embeddedor> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-hyperv-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hyperv@vger.kernel.org On Mon, May 25, 2020 at 11:43:19AM -0500, 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 hv_dr_state { > ... > struct hv_pcidev_description func[]; > }; > > struct pci_bus_relations { > ... > struct pci_function_description func[]; > } __packed; > > Make use of the struct_size() helper instead of an open-coded version > in order to avoid any potential type mistakes. > > So, replace the following forms: > > offsetof(struct hv_dr_state, func) + > (sizeof(struct hv_pcidev_description) * > (relations->device_count)) > > offsetof(struct pci_bus_relations, func) + > (sizeof(struct pci_function_description) * > (bus_rel->device_count)) > > with: > > struct_size(dr, func, relations->device_count) > > and > > struct_size(bus_rel, func, bus_rel->device_count) > > respectively. > > Signed-off-by: Gustavo A. R. Silva > --- > drivers/pci/controller/pci-hyperv.c | 22 ++++++++-------------- > 1 file changed, 8 insertions(+), 14 deletions(-) Applied to pci/hv, thanks. Lorenzo > diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c > index 892f3a742117a..bf40ff09c99d6 100644 > --- a/drivers/pci/controller/pci-hyperv.c > +++ b/drivers/pci/controller/pci-hyperv.c > @@ -2213,10 +2213,8 @@ static void hv_pci_devices_present(struct hv_pcibus_device *hbus, > struct hv_dr_state *dr; > int i; > > - dr = kzalloc(offsetof(struct hv_dr_state, func) + > - (sizeof(struct hv_pcidev_description) * > - (relations->device_count)), GFP_NOWAIT); > - > + dr = kzalloc(struct_size(dr, func, relations->device_count), > + GFP_NOWAIT); > if (!dr) > return; > > @@ -2250,10 +2248,8 @@ static void hv_pci_devices_present2(struct hv_pcibus_device *hbus, > struct hv_dr_state *dr; > int i; > > - dr = kzalloc(offsetof(struct hv_dr_state, func) + > - (sizeof(struct hv_pcidev_description) * > - (relations->device_count)), GFP_NOWAIT); > - > + dr = kzalloc(struct_size(dr, func, relations->device_count), > + GFP_NOWAIT); > if (!dr) > return; > > @@ -2447,9 +2443,8 @@ static void hv_pci_onchannelcallback(void *context) > > bus_rel = (struct pci_bus_relations *)buffer; > if (bytes_recvd < > - offsetof(struct pci_bus_relations, func) + > - (sizeof(struct pci_function_description) * > - (bus_rel->device_count))) { > + struct_size(bus_rel, func, > + bus_rel->device_count)) { > dev_err(&hbus->hdev->device, > "bus relations too small\n"); > break; > @@ -2462,9 +2457,8 @@ static void hv_pci_onchannelcallback(void *context) > > bus_rel2 = (struct pci_bus_relations2 *)buffer; > if (bytes_recvd < > - offsetof(struct pci_bus_relations2, func) + > - (sizeof(struct pci_function_description2) * > - (bus_rel2->device_count))) { > + struct_size(bus_rel2, func, > + bus_rel2->device_count)) { > dev_err(&hbus->hdev->device, > "bus relations v2 too small\n"); > break; > -- > 2.26.2 >