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 2/7] vfio_pci_core: split krealloc to allow use RCU & return index
Date: Wed, 24 Sep 2025 16:09:53 +0200	[thread overview]
Message-ID: <20250924141018.80202-3-mngyadam@amazon.de> (raw)
In-Reply-To: <20250924141018.80202-1-mngyadam@amazon.de>

Unwrap the allocation, copying, assignation & freeing part of the
krealloc. This enables using RCU for picking the reference in the
following patches, and synchronize before writing back to region.

Use the return value for returning the region index that was
created. This is helpful for the caller to know the index of the
region that was created.

Signed-off-by: Mahmoud Adam <mngyadam@amazon.de>
---
 drivers/vfio/pci/vfio_pci_core.c | 34 ++++++++++++++++++++------------
 drivers/vfio/pci/vfio_pci_igd.c  |  6 +++---
 2 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index ea04c1291af68..6629490c0e46f 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -881,30 +881,38 @@ static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
 	return vfio_info_add_capability(caps, &header, sizeof(header));
 }
 
+/*
+ * Registers a new region to vfio_pci_core_device.
+ * Returns region index on success or a negative errno.
+ */
 int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev,
 				      unsigned int type, unsigned int subtype,
 				      const struct vfio_pci_regops *ops,
 				      size_t size, u32 flags, void *data)
 {
-	struct vfio_pci_region *region;
+	int num_regions = vdev->num_regions;
+	struct vfio_pci_region *region, *old_region;
 
-	region = krealloc(vdev->region,
-			  (vdev->num_regions + 1) * sizeof(*region),
-			  GFP_KERNEL_ACCOUNT);
+	region = kmalloc((num_regions + 1) * sizeof(*region),
+			 GFP_KERNEL_ACCOUNT);
 	if (!region)
 		return -ENOMEM;
 
-	vdev->region = region;
-	vdev->region[vdev->num_regions].type = type;
-	vdev->region[vdev->num_regions].subtype = subtype;
-	vdev->region[vdev->num_regions].ops = ops;
-	vdev->region[vdev->num_regions].size = size;
-	vdev->region[vdev->num_regions].flags = flags;
-	vdev->region[vdev->num_regions].data = data;
+	old_region = vdev->region;
+	if (old_region)
+		memcpy(region, old_region, num_regions * sizeof(*region));
 
-	vdev->num_regions++;
+	region[num_regions].type = type;
+	region[num_regions].subtype = subtype;
+	region[num_regions].ops = ops;
+	region[num_regions].size = size;
+	region[num_regions].flags = flags;
+	region[num_regions].data = data;
 
-	return 0;
+	vdev->region = region;
+	vdev->num_regions++;
+	kfree(old_region);
+	return num_regions;
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_register_dev_region);
 
diff --git a/drivers/vfio/pci/vfio_pci_igd.c b/drivers/vfio/pci/vfio_pci_igd.c
index ac0921fdc62da..93ddef48e4e4c 100644
--- a/drivers/vfio/pci/vfio_pci_igd.c
+++ b/drivers/vfio/pci/vfio_pci_igd.c
@@ -265,7 +265,7 @@ static int vfio_pci_igd_opregion_init(struct vfio_pci_core_device *vdev)
 		PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE,
 		VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &vfio_pci_igd_regops,
 		size, VFIO_REGION_INFO_FLAG_READ, opregionvbt);
-	if (ret) {
+	if (ret < 0) {
 		if (opregionvbt->vbt_ex)
 			memunmap(opregionvbt->vbt_ex);
 
@@ -415,7 +415,7 @@ static int vfio_pci_igd_cfg_init(struct vfio_pci_core_device *vdev)
 		VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG,
 		&vfio_pci_igd_cfg_regops, host_bridge->cfg_size,
 		VFIO_REGION_INFO_FLAG_READ, host_bridge);
-	if (ret) {
+	if (ret < 0) {
 		pci_dev_put(host_bridge);
 		return ret;
 	}
@@ -435,7 +435,7 @@ static int vfio_pci_igd_cfg_init(struct vfio_pci_core_device *vdev)
 		VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG,
 		&vfio_pci_igd_cfg_regops, lpc_bridge->cfg_size,
 		VFIO_REGION_INFO_FLAG_READ, lpc_bridge);
-	if (ret) {
+	if (ret < 0) {
 		pci_dev_put(lpc_bridge);
 		return ret;
 	}
-- 
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


  parent 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 ` [RFC PATCH 1/7] vfio/pci: refactor region dereferences for RCU Mahmoud Adam
2025-09-24 14:09 ` Mahmoud Adam [this message]
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-3-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