Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp4621731ybe; Mon, 16 Sep 2019 15:41:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqyU5OdT/ecwjI8WDbpikmKkDMnUbbkLTOX2lgDeGkvj+ycFWR0wIoibQLQn32O98oVhTIpa X-Received: by 2002:a17:906:5606:: with SMTP id f6mr2179398ejq.110.1568673670184; Mon, 16 Sep 2019 15:41:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568673670; cv=none; d=google.com; s=arc-20160816; b=afsIFJzcDM7/eZw3ted64AlFuJKB2D/DnZdKKRi0bfeUkEI1HYFo9zyW2Ml+sA/SC+ d8/ud6ofB35QTqdwuKudMn1fVg6m4qmv2U74S5XIR0Wobe56VFQ61UvxnEBAjhEjB2+t M4KzOmtq0I8Y6kY3Ul8B8Cmb1QG/vePslhctH976nAbKJq19nNoTh7XvM/doSEhvYCf7 ZJ2F5qiYmqjX0lGPHmRIc0ZGeoAaWiiZNBCrnlmB/KrN4c7c61PefVYlD4vw8hiJtarz jEXV/72H6TZKjS1UicHgBInSWknj9oDlLVyHNpy0FGZHkFU6dvjntrajS+gEwp4hiLq4 zTRA== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=QLQPAYHUACHzoMzXgGl4aMEibp9KP46d8BzQG7n7Uds=; b=tdjcnx0JmQT/9IpFEcl+fQ3uM5XnmngFTy+jbYeEH9gFcY7J1UlNituMe14glcAjoK be0sX1K7lFD5htZbcuEtY6ZF84ThWxejAO+bJvjzTCTCZmygO0JqsOAWdWM2SEycqYPZ Q0YaJPWsYN6bm31+eraTtmXbKXYpDjybPa+KGj41AQ885P33BPc4jHFEQvG1juvxxC0K XHZt9+Z6V0hmX8rup18Z3yRUdwMW6x2OosC4fUShveS7RYByR6KTd+8VdfRjCuNbBNVB cPoR9ZKlBxUzmOtAcuMvSurnLKyEwlq/Kh53Vnf05IKHAVrs6H4uauwCscArcp7Y5vWu FYbw== 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 o46si272185edc.124.2019.09.16.15.40.43; Mon, 16 Sep 2019 15:41:10 -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 S2388653AbfIPUrb (ORCPT + 99 others); Mon, 16 Sep 2019 16:47:31 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:52034 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727971AbfIPUrb (ORCPT ); Mon, 16 Sep 2019 16:47:31 -0400 Received: by mail-wm1-f65.google.com with SMTP id 7so797937wme.1; Mon, 16 Sep 2019 13:47:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QLQPAYHUACHzoMzXgGl4aMEibp9KP46d8BzQG7n7Uds=; b=hm760oHm3uMSkpxZuRLuBvLrNOeyALAfldCHV1fv5zO0kMAXr9Vg6hYrsMdJdxoRRl iP6pAOxGvvtlypfAf2m7olv+nkopteVT9K+4DmxuEk9fiqTvK3nxkyJUgDkftAhnsp+7 MTdVWhQDxmHCErYb4N8ZLbjNgLmUMnFb4rt64Z91GLefd9s29iKGvWOzuJHVv44bHpdX GxD+ae5EtGMO2r3hDnkwAd1oFNhWqPBRR6tDgtxGT1tdXJFTmyC9WzFVFiE+fpxvIb33 GkXCRUwxzk2+GGBSw8/O3BwWqTFYB0r5Y0B1yQEo8jrk3kJPt6VzHyCjGiD+po34nnbM UJrA== X-Gm-Message-State: APjAAAWxnD8kmzyWvIZZ/C27SIeqs74z95hA9Y6o8Gai+YdUJxuqIikx ukCGvDPrzT03bbDL++sAUaw= X-Received: by 2002:a1c:dd0a:: with SMTP id u10mr674534wmg.100.1568666847741; Mon, 16 Sep 2019 13:47:27 -0700 (PDT) Received: from black.home (broadband-188-32-48-208.ip.moscow.rt.ru. [188.32.48.208]) by smtp.googlemail.com with ESMTPSA id x6sm231437wmf.38.2019.09.16.13.47.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2019 13:47:27 -0700 (PDT) From: Denis Efremov To: Bjorn Helgaas Cc: Denis Efremov , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Andrew Murray , kvm@vger.kernel.org, Cornelia Huck , Alex Williamson Subject: [PATCH v3 17/26] vfio_pci: Loop using PCI_STD_NUM_BARS Date: Mon, 16 Sep 2019 23:41:49 +0300 Message-Id: <20190916204158.6889-18-efremov@linux.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190916204158.6889-1-efremov@linux.com> References: <20190916204158.6889-1-efremov@linux.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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]; 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