public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Mahmoud Adam <mngyadam@amazon.de>
To: <kvm@vger.kernel.org>
Cc: <alex.williamson@redhat.com>, <jgg@ziepe.ca>, <kbusch@kernel.org>,
	<benh@kernel.crashing.org>, David Woodhouse <dwmw@amazon.co.uk>,
	<pravkmr@amazon.de>, <nagy@khwaternagy.com>,
	<linux-kernel@vger.kernel.org>
Subject: [RFC PATCH 1/7] vfio/pci: refactor region dereferences for RCU.
Date: Wed, 24 Sep 2025 16:09:52 +0200	[thread overview]
Message-ID: <20250924141018.80202-2-mngyadam@amazon.de> (raw)
In-Reply-To: <20250924141018.80202-1-mngyadam@amazon.de>

No functional changes. These refactors multiple region array accessing
into one place. This prepares for the RCU locking in the following
patches.

Signed-off-by: Mahmoud Adam <mngyadam@amazon.de>
---
 drivers/vfio/pci/vfio_pci_core.c | 21 ++++++++++++---------
 drivers/vfio/pci/vfio_pci_igd.c  | 20 ++++++++++++++------
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 7dcf5439dedc9..ea04c1291af68 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1003,6 +1003,7 @@ static int vfio_pci_ioctl_get_region_info(struct vfio_pci_core_device *vdev,
 	struct pci_dev *pdev = vdev->pdev;
 	struct vfio_region_info info;
 	struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
+	struct vfio_pci_region *region;
 	int i, ret;
 
 	if (copy_from_user(&info, arg, minsz))
@@ -1091,22 +1092,23 @@ static int vfio_pci_ioctl_get_region_info(struct vfio_pci_core_device *vdev,
 			info.index, VFIO_PCI_NUM_REGIONS + vdev->num_regions);
 
 		i = info.index - VFIO_PCI_NUM_REGIONS;
+		region = &vdev->region[i];
 
 		info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
-		info.size = vdev->region[i].size;
-		info.flags = vdev->region[i].flags;
+		info.size = region->size;
+		info.flags = region->flags;
 
-		cap_type.type = vdev->region[i].type;
-		cap_type.subtype = vdev->region[i].subtype;
+		cap_type.type = region->type;
+		cap_type.subtype = region->subtype;
 
 		ret = vfio_info_add_capability(&caps, &cap_type.header,
 					       sizeof(cap_type));
 		if (ret)
 			return ret;
 
-		if (vdev->region[i].ops->add_capability) {
-			ret = vdev->region[i].ops->add_capability(
-				vdev, &vdev->region[i], &caps);
+		if (region->ops->add_capability) {
+			ret = region->ops->add_capability(
+				vdev, region, &caps);
 			if (ret)
 				return ret;
 		}
@@ -1726,10 +1728,11 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
 		int regnum = index - VFIO_PCI_NUM_REGIONS;
 		struct vfio_pci_region *region = vdev->region + regnum;
 
+		ret = -EINVAL;
 		if (region->ops && region->ops->mmap &&
 		    (region->flags & VFIO_REGION_INFO_FLAG_MMAP))
-			return region->ops->mmap(vdev, region, vma);
-		return -EINVAL;
+			ret = region->ops->mmap(vdev, region, vma);
+		return ret;
 	}
 	if (index >= VFIO_PCI_ROM_REGION_INDEX)
 		return -EINVAL;
diff --git a/drivers/vfio/pci/vfio_pci_igd.c b/drivers/vfio/pci/vfio_pci_igd.c
index 988b6919c2c31..ac0921fdc62da 100644
--- a/drivers/vfio/pci/vfio_pci_igd.c
+++ b/drivers/vfio/pci/vfio_pci_igd.c
@@ -66,14 +66,18 @@ static ssize_t vfio_pci_igd_rw(struct vfio_pci_core_device *vdev,
 			       bool iswrite)
 {
 	unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) - VFIO_PCI_NUM_REGIONS;
-	struct igd_opregion_vbt *opregionvbt = vdev->region[i].data;
 	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK, off = 0;
 	size_t remaining;
+	struct vfio_pci_region *region;
+	struct igd_opregion_vbt *opregionvbt;
+
+	region = &vdev->region[i];
+	opregionvbt = region->data;
 
-	if (pos >= vdev->region[i].size || iswrite)
+	if (pos >= region->size || iswrite)
 		return -EINVAL;
 
-	count = min_t(size_t, count, vdev->region[i].size - pos);
+	count = min_t(size_t, count, region->size - pos);
 	remaining = count;
 
 	/* Copy until OpRegion version */
@@ -283,15 +287,19 @@ static ssize_t vfio_pci_igd_cfg_rw(struct vfio_pci_core_device *vdev,
 				   bool iswrite)
 {
 	unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) - VFIO_PCI_NUM_REGIONS;
-	struct pci_dev *pdev = vdev->region[i].data;
 	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
 	size_t size;
 	int ret;
+	struct vfio_pci_region *region;
+	struct pci_dev *pdev;
+
+	region = &vdev->region[i];
+	pdev = region->data;
 
-	if (pos >= vdev->region[i].size || iswrite)
+	if (pos >= region->size || iswrite)
 		return -EINVAL;
 
-	size = count = min(count, (size_t)(vdev->region[i].size - pos));
+	size = count = min(count, (size_t)(region->size - pos));
 
 	if ((pos & 1) && size) {
 		u8 val;
-- 
2.47.3




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christian Schlaeger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597


  reply	other threads:[~2025-09-24 14:11 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-24 14:09 [RFC PATCH 0/7] vfio: Add alias region uapi for device feature Mahmoud Adam
2025-09-24 14:09 ` Mahmoud Adam [this message]
2025-09-24 14:09 ` [RFC PATCH 2/7] vfio_pci_core: split krealloc to allow use RCU & return index Mahmoud Adam
2025-09-24 14:09 ` [RFC PATCH 3/7] vfio/pci: add RCU locking for regions access Mahmoud Adam
2025-09-24 16:15   ` Mahmoud Nagy Adam
2025-09-24 14:09 ` [RFC PATCH 4/7] vfio: add FEATURE_ALIAS_REGION uapi Mahmoud Adam
2025-09-24 14:09 ` [RFC PATCH 5/7] vfio_pci_core: allow regions with no release op Mahmoud Adam
2025-09-24 14:09 ` [RFC PATCH 6/7] vfio-pci: add alias_region mmap ops Mahmoud Adam
2025-09-24 14:09 ` [RFC PATCH 7/7] vfio-pci-core: implement FEATURE_ALIAS_REGION uapi Mahmoud Adam
2025-10-03 21:58 ` [RFC PATCH 0/7] vfio: Add alias region uapi for device feature David Matlack
2025-10-05 10:16   ` Mahmoud Nagy Adam
2025-10-15 19:36 ` Alex Williamson
2025-10-27 16:32 ` David Matlack
2025-10-27 18:19   ` Mahmoud Nagy Adam

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250924141018.80202-2-mngyadam@amazon.de \
    --to=mngyadam@amazon.de \
    --cc=alex.williamson@redhat.com \
    --cc=benh@kernel.crashing.org \
    --cc=dwmw@amazon.co.uk \
    --cc=jgg@ziepe.ca \
    --cc=kbusch@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nagy@khwaternagy.com \
    --cc=pravkmr@amazon.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox