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 --- 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