Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp6391028ybe; Wed, 18 Sep 2019 02:42:03 -0700 (PDT) X-Google-Smtp-Source: APXvYqy2OTLiZGiXyNBK11d9HgFh4S2a5PLX4jrd88t4UjOfw//ghlboP2QogBcstHvWyrDX/Gy6 X-Received: by 2002:a50:acc1:: with SMTP id x59mr9325901edc.278.1568799722996; Wed, 18 Sep 2019 02:42:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568799722; cv=none; d=google.com; s=arc-20160816; b=ysy3lyzxxuPIVHhPbqn5L4QHiwULW6G58fHDh4uMDdiXziPgWfhnqLrsMYIyZ6dgPD TCpKoyo99uL6y9IssKBiXvh46vK/7LVFuPg9C0nOkYDtWbHWoxG9UmCXQ+FOZG39CSD5 18jsfx4I2DG56jRcgI2d8oDZCM/jpo2I1lETYmdZgDK3DPFZTiik6OXFFyx1l7krV/fe Zm5qI/8zf9CmXgbUNnVYd1XpojnxeB0c9Fd/rp+PmSVqGdpuj3x11pmbQgwhQtOyKB5G AKtPBiWoUG/JKgVSqeT0AkdMCZQObclxtCTFaanADOTV3LbQEucem+AOhWKeybtUqsxJ GBTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=NJIzQPNPSzGBSt6n9B6CDaU6PZ/+RvBAhr/szwhGzic=; b=a+VsDP5P9IckW6QP9G5B00T9HnV0rwc6Cs6CYi7cLq9FlnGe26y/4kaczqQ2aOVFg9 D91X0pyqsAuCc7kpA5ZBpnzJdlaUo5C2fIz4Y0QkVjpe4htG/5rS6nqklpwRLjSGo89r lvYDhPDX/0BiSUnozDZZDuEk7zY7xH0iMHC8RJgNHhdFV7OimNm1RkMjcpHXjk7mDbkJ lChPpNu+VGJ0R0DvTPWWAbHZ3bUmkk4wV7pvsyqU0+E/XtS2xgbzcR/BWA+vssbDhl28 az8jR318LWgRBsoQbK+tdpsiH3WF3/l1C7So256Un2Cc8LxNORjQMdoMM3t6UoWdyBU4 c8kQ== 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 mj22si2459367ejb.156.2019.09.18.02.41.39; Wed, 18 Sep 2019 02:42:02 -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; 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 S1729111AbfIRJRX (ORCPT + 99 others); Wed, 18 Sep 2019 05:17:23 -0400 Received: from foss.arm.com ([217.140.110.172]:37960 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726268AbfIRJRX (ORCPT ); Wed, 18 Sep 2019 05:17:23 -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 63918337; Wed, 18 Sep 2019 02:17:22 -0700 (PDT) Received: from localhost (unknown [10.37.6.20]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id CE9563F59C; Wed, 18 Sep 2019 02:17:21 -0700 (PDT) Date: Wed, 18 Sep 2019 10:17:20 +0100 From: Andrew Murray To: Denis Efremov Cc: Bjorn Helgaas , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, Cornelia Huck , Alex Williamson Subject: Re: [PATCH v3 17/26] vfio_pci: Loop using PCI_STD_NUM_BARS Message-ID: <20190918091719.GA9720@e119886-lin.cambridge.arm.com> References: <20190916204158.6889-1-efremov@linux.com> <20190916204158.6889-18-efremov@linux.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190916204158.6889-18-efremov@linux.com> User-Agent: Mutt/1.10.1+81 (426a6c1) (2018-08-26) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 16, 2019 at 11:41:49PM +0300, Denis Efremov wrote: > Refactor loops to use idiomatic C style and avoid the fencepost error > of using "i < PCI_STD_RESOURCE_END" when "i <= PCI_STD_RESOURCE_END" > is required, e.g., commit 2f686f1d9bee ("PCI: Correct PCI_STD_RESOURCE_END > usage"). > > To iterate through all possible BARs, loop conditions changed to the > *number* of BARs "i < PCI_STD_NUM_BARS", instead of the index of the last > valid BAR "i <= PCI_STD_RESOURCE_END". > > Cc: Cornelia Huck > Cc: Alex Williamson > Signed-off-by: Denis Efremov > --- > drivers/vfio/pci/vfio_pci.c | 11 ++++++---- > drivers/vfio/pci/vfio_pci_config.c | 32 +++++++++++++++-------------- > drivers/vfio/pci/vfio_pci_private.h | 4 ++-- > 3 files changed, 26 insertions(+), 21 deletions(-) > > diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c > index 703948c9fbe1..cb7d220d3246 100644 > --- a/drivers/vfio/pci/vfio_pci.c > +++ b/drivers/vfio/pci/vfio_pci.c > @@ -110,13 +110,15 @@ static inline bool vfio_pci_is_vga(struct pci_dev *pdev) > static void vfio_pci_probe_mmaps(struct vfio_pci_device *vdev) > { > struct resource *res; > - int bar; > + int i; > struct vfio_pci_dummy_resource *dummy_res; > > INIT_LIST_HEAD(&vdev->dummy_resources_list); > > - for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) { > - res = vdev->pdev->resource + bar; > + for (i = 0; i < PCI_STD_NUM_BARS; i++) { > + int bar = i + PCI_STD_RESOURCES; > + > + res = &vdev->pdev->resource[bar]; Why can't we just drop PCI_STD_RESOURCES and replace it was 0. I understand the abstraction here, but we don't do it elsewhere across the kernel. Is this necessary? Thanks, Andrew Murray > > if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP)) > goto no_mmap; > @@ -399,7 +401,8 @@ static void vfio_pci_disable(struct vfio_pci_device *vdev) > > vfio_config_free(vdev); > > - for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) { > + for (i = 0; i < PCI_STD_NUM_BARS; i++) { > + bar = i + PCI_STD_RESOURCES; > if (!vdev->barmap[bar]) > continue; > pci_iounmap(pdev, vdev->barmap[bar]); > diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c > index f0891bd8444c..90c0b80f8acf 100644 > --- a/drivers/vfio/pci/vfio_pci_config.c > +++ b/drivers/vfio/pci/vfio_pci_config.c > @@ -450,30 +450,32 @@ static void vfio_bar_fixup(struct vfio_pci_device *vdev) > { > struct pci_dev *pdev = vdev->pdev; > int i; > - __le32 *bar; > + __le32 *vbar; > u64 mask; > > - bar = (__le32 *)&vdev->vconfig[PCI_BASE_ADDRESS_0]; > + vbar = (__le32 *)&vdev->vconfig[PCI_BASE_ADDRESS_0]; > > - for (i = PCI_STD_RESOURCES; i <= PCI_STD_RESOURCE_END; i++, bar++) { > - if (!pci_resource_start(pdev, i)) { > - *bar = 0; /* Unmapped by host = unimplemented to user */ > + for (i = 0; i < PCI_STD_NUM_BARS; i++, vbar++) { > + int bar = i + PCI_STD_RESOURCES; > + > + if (!pci_resource_start(pdev, bar)) { > + *vbar = 0; /* Unmapped by host = unimplemented to user */ > continue; > } > > - mask = ~(pci_resource_len(pdev, i) - 1); > + mask = ~(pci_resource_len(pdev, bar) - 1); > > - *bar &= cpu_to_le32((u32)mask); > - *bar |= vfio_generate_bar_flags(pdev, i); > + *vbar &= cpu_to_le32((u32)mask); > + *vbar |= vfio_generate_bar_flags(pdev, bar); > > - if (*bar & cpu_to_le32(PCI_BASE_ADDRESS_MEM_TYPE_64)) { > - bar++; > - *bar &= cpu_to_le32((u32)(mask >> 32)); > + if (*vbar & cpu_to_le32(PCI_BASE_ADDRESS_MEM_TYPE_64)) { > + vbar++; > + *vbar &= cpu_to_le32((u32)(mask >> 32)); > i++; > } > } > > - bar = (__le32 *)&vdev->vconfig[PCI_ROM_ADDRESS]; > + vbar = (__le32 *)&vdev->vconfig[PCI_ROM_ADDRESS]; > > /* > * NB. REGION_INFO will have reported zero size if we weren't able > @@ -483,14 +485,14 @@ static void vfio_bar_fixup(struct vfio_pci_device *vdev) > if (pci_resource_start(pdev, PCI_ROM_RESOURCE)) { > mask = ~(pci_resource_len(pdev, PCI_ROM_RESOURCE) - 1); > mask |= PCI_ROM_ADDRESS_ENABLE; > - *bar &= cpu_to_le32((u32)mask); > + *vbar &= cpu_to_le32((u32)mask); > } else if (pdev->resource[PCI_ROM_RESOURCE].flags & > IORESOURCE_ROM_SHADOW) { > mask = ~(0x20000 - 1); > mask |= PCI_ROM_ADDRESS_ENABLE; > - *bar &= cpu_to_le32((u32)mask); > + *vbar &= cpu_to_le32((u32)mask); > } else > - *bar = 0; > + *vbar = 0; > > vdev->bardirty = false; > } > diff --git a/drivers/vfio/pci/vfio_pci_private.h b/drivers/vfio/pci/vfio_pci_private.h > index ee6ee91718a4..8a2c7607d513 100644 > --- a/drivers/vfio/pci/vfio_pci_private.h > +++ b/drivers/vfio/pci/vfio_pci_private.h > @@ -86,8 +86,8 @@ struct vfio_pci_reflck { > > struct vfio_pci_device { > struct pci_dev *pdev; > - void __iomem *barmap[PCI_STD_RESOURCE_END + 1]; > - bool bar_mmap_supported[PCI_STD_RESOURCE_END + 1]; > + void __iomem *barmap[PCI_STD_NUM_BARS]; > + bool bar_mmap_supported[PCI_STD_NUM_BARS]; > u8 *pci_config_map; > u8 *vconfig; > struct perm_bits *msi_perm; > -- > 2.21.0 >