All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mostafa Saleh <smostafa@google.com>
To: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
Cc: kvm@vger.kernel.org, Suzuki K Poulose <Suzuki.Poulose@arm.com>,
	Steven Price <steven.price@arm.com>,
	Will Deacon <will@kernel.org>,
	Julien Thierry <julien.thierry.kdev@gmail.com>
Subject: Re: [RFC PATCH kvmtool 03/10] vfio: Create new file legacy.c
Date: Sun, 27 Jul 2025 18:23:18 +0000	[thread overview]
Message-ID: <aIZulgInZXazv8oY@google.com> (raw)
In-Reply-To: <20250525074917.150332-3-aneesh.kumar@kernel.org>

On Sun, May 25, 2025 at 01:19:09PM +0530, Aneesh Kumar K.V (Arm) wrote:
> Move legacy vfio config methodology to legacy.c. Also add helper
> vfio_map/unmap_mem_range which will be switched to function pointers in
> the later patch.
> 
> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
> ---
>  Makefile           |   1 +
>  include/kvm/vfio.h |  14 ++
>  vfio/core.c        | 342 ++------------------------------------------
>  vfio/legacy.c      | 347 +++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 372 insertions(+), 332 deletions(-)
>  create mode 100644 vfio/legacy.c
> 
> diff --git a/Makefile b/Makefile
> index 60e551fd0c2a..8b2720f73386 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -65,6 +65,7 @@ OBJS	+= pci.o
>  OBJS	+= term.o
>  OBJS	+= vfio/core.o
>  OBJS	+= vfio/pci.o
> +OBJS	+= vfio/legacy.o
>  OBJS	+= virtio/blk.o
>  OBJS	+= virtio/scsi.o
>  OBJS	+= virtio/console.o
> diff --git a/include/kvm/vfio.h b/include/kvm/vfio.h
> index ac7b6226239a..67a528f18d33 100644
> --- a/include/kvm/vfio.h
> +++ b/include/kvm/vfio.h
> @@ -126,4 +126,18 @@ void vfio_unmap_region(struct kvm *kvm, struct vfio_region *region);
>  int vfio_pci_setup_device(struct kvm *kvm, struct vfio_device *device);
>  void vfio_pci_teardown_device(struct kvm *kvm, struct vfio_device *vdev);
>  
> +int vfio_map_mem_range(struct kvm *kvm, __u64 host_addr, __u64 iova, __u64 size);
> +int vfio_unmap_mem_range(struct kvm *kvm, __u64 iova, __u64 size);
> +
> +struct kvm_mem_bank;
> +int vfio_map_mem_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void *data);
> +int vfio_unmap_mem_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void *data);
> +int vfio_configure_reserved_regions(struct kvm *kvm, struct vfio_group *group);
> +int legacy_vfio__init(struct kvm *kvm);
> +int legacy_vfio__exit(struct kvm *kvm);
> +
> +extern int kvm_vfio_device;
> +extern struct list_head vfio_groups;
> +extern struct vfio_device *vfio_devices;
> +
>  #endif /* KVM__VFIO_H */
> diff --git a/vfio/core.c b/vfio/core.c
> index 424dc4ed3aef..2af30df3b2b9 100644
> --- a/vfio/core.c
> +++ b/vfio/core.c
> @@ -4,14 +4,11 @@
>  
>  #include <linux/list.h>
>  
> -#define VFIO_DEV_DIR		"/dev/vfio"
> -#define VFIO_DEV_NODE		VFIO_DEV_DIR "/vfio"
>  #define IOMMU_GROUP_DIR		"/sys/kernel/iommu_groups"
>  
> -static int vfio_container;
> -static int kvm_vfio_device;
> -static LIST_HEAD(vfio_groups);
> -static struct vfio_device *vfio_devices;
> +int kvm_vfio_device;

kvm_vfio_device shouldn’t be VFIO/IOMMUFD specific, so that leads to
duplication in both files, I suggest move it’s management to the vfio/core.c
(and don’t extern the fd) And either export a function to add devices or maybe,
better doing it once from vfio__init()

> +LIST_HEAD(vfio_groups);
“vfio_groups” seems not to be used by the core code, maybe it’s better to have a
static version in each file?
Also, as that is not really used for IOMMUFD, it seems to move group logic into
legacy file. Instead of making iommufd populating groups so the core code handle
the group exit.

> +struct vfio_device *vfio_devices;
>  

Similarly for “vfio_devices”, it’s only allocated/freed in core code, but never used.
But no strong opinion about that.

Thanks,
Mostafa

>  static int vfio_device_pci_parser(const struct option *opt, char *arg,
>  				  struct vfio_device_params *dev)
> @@ -282,124 +279,17 @@ void vfio_unmap_region(struct kvm *kvm, struct vfio_region *region)
>  	}
>  }
>  
> -static int legacy_vfio_configure_device(struct kvm *kvm, struct vfio_device *vdev)
> +int vfio_map_mem_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void *data)
>  {
> -	int ret;
> -	struct vfio_group *group = vdev->group;
> -
> -	vdev->fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD,
> -			 vdev->params->name);
> -	if (vdev->fd < 0) {
> -		vfio_dev_warn(vdev, "failed to get fd");
> -
> -		/* The device might be a bridge without an fd */
> -		return 0;
> -	}
> -
> -	vdev->info.argsz = sizeof(vdev->info);
> -	if (ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &vdev->info)) {
> -		ret = -errno;
> -		vfio_dev_err(vdev, "failed to get info");
> -		goto err_close_device;
> -	}
> -
> -	if (vdev->info.flags & VFIO_DEVICE_FLAGS_RESET &&
> -	    ioctl(vdev->fd, VFIO_DEVICE_RESET) < 0)
> -		vfio_dev_warn(vdev, "failed to reset device");
> -
> -	vdev->regions = calloc(vdev->info.num_regions, sizeof(*vdev->regions));
> -	if (!vdev->regions) {
> -		ret = -ENOMEM;
> -		goto err_close_device;
> -	}
> -
> -	/* Now for the bus-specific initialization... */
> -	switch (vdev->params->type) {
> -	case VFIO_DEVICE_PCI:
> -		BUG_ON(!(vdev->info.flags & VFIO_DEVICE_FLAGS_PCI));
> -		ret = vfio_pci_setup_device(kvm, vdev);
> -		break;
> -	default:
> -		BUG_ON(1);
> -		ret = -EINVAL;
> -	}
> -
> -	if (ret)
> -		goto err_free_regions;
> -
> -	vfio_dev_info(vdev, "assigned to device number 0x%x in group %lu",
> -		      vdev->dev_hdr.dev_num, group->id);
> -
> -	return 0;
> -
> -err_free_regions:
> -	free(vdev->regions);
> -err_close_device:
> -	close(vdev->fd);
> -
> -	return ret;
> -}
> -
> -static int legacy_vfio_configure_devices(struct kvm *kvm)
> -{
> -	int i, ret;
> -
> -	for (i = 0; i < kvm->cfg.num_vfio_devices; ++i) {
> -		ret = legacy_vfio_configure_device(kvm, &vfio_devices[i]);
> -		if (ret)
> -			return ret;
> -	}
> -
> -	return 0;
> +	return vfio_map_mem_range(kvm, (u64)bank->host_addr, bank->guest_phys_addr, bank->size);
>  }
>  
> -static int vfio_get_iommu_type(void)
> +int vfio_unmap_mem_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void *data)
>  {
> -	if (ioctl(vfio_container, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU))
> -		return VFIO_TYPE1v2_IOMMU;
> -
> -	if (ioctl(vfio_container, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU))
> -		return VFIO_TYPE1_IOMMU;
> -
> -	return -ENODEV;
> -}
> -
> -static int vfio_map_mem_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void *data)
> -{
> -	int ret = 0;
> -	struct vfio_iommu_type1_dma_map dma_map = {
> -		.argsz	= sizeof(dma_map),
> -		.flags	= VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
> -		.vaddr	= (unsigned long)bank->host_addr,
> -		.iova	= (u64)bank->guest_phys_addr,
> -		.size	= bank->size,
> -	};
> -
> -	/* Map the guest memory for DMA (i.e. provide isolation) */
> -	if (ioctl(vfio_container, VFIO_IOMMU_MAP_DMA, &dma_map)) {
> -		ret = -errno;
> -		pr_err("Failed to map 0x%llx -> 0x%llx (%llu) for DMA",
> -		       dma_map.iova, dma_map.vaddr, dma_map.size);
> -	}
> -
> -	return ret;
> -}
> -
> -static int vfio_unmap_mem_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void *data)
> -{
> -	struct vfio_iommu_type1_dma_unmap dma_unmap = {
> -		.argsz = sizeof(dma_unmap),
> -		.size = bank->size,
> -		.iova = bank->guest_phys_addr,
> -	};
> -
> -	ioctl(vfio_container, VFIO_IOMMU_UNMAP_DMA, &dma_unmap);
> -
> -	return 0;
> +	return vfio_unmap_mem_range(kvm, bank->guest_phys_addr, bank->size);
>  }
>  
> -static int vfio_configure_reserved_regions(struct kvm *kvm,
> -					   struct vfio_group *group)
> +int vfio_configure_reserved_regions(struct kvm *kvm, struct vfio_group *group)
>  {
>  	FILE *file;
>  	int ret = 0;
> @@ -429,84 +319,6 @@ static int vfio_configure_reserved_regions(struct kvm *kvm,
>  	return ret;
>  }
>  
> -static int legacy_vfio_configure_groups(struct kvm *kvm)
> -{
> -	int ret;
> -	struct vfio_group *group;
> -
> -	list_for_each_entry(group, &vfio_groups, list) {
> -		ret = vfio_configure_reserved_regions(kvm, group);
> -		if (ret)
> -			return ret;
> -
> -		struct kvm_device_attr attr = {
> -			.group = KVM_DEV_VFIO_FILE,
> -			.attr = KVM_DEV_VFIO_FILE_ADD,
> -			.addr = (__u64)&group->fd,
> -		};
> -
> -		if (ioctl(kvm_vfio_device, KVM_SET_DEVICE_ATTR, &attr)) {
> -			pr_err("Failed KVM_SET_DEVICE_ATTR for KVM_DEV_VFIO_FILE");
> -			return -ENODEV;
> -		}
> -
> -	}
> -	return 0;
> -}
> -
> -static struct vfio_group *legacy_vfio_group_create(struct kvm *kvm, unsigned long id)
> -{
> -	int ret;
> -	struct vfio_group *group;
> -	char group_node[PATH_MAX];
> -	struct vfio_group_status group_status = {
> -		.argsz = sizeof(group_status),
> -	};
> -
> -	group = calloc(1, sizeof(*group));
> -	if (!group)
> -		return NULL;
> -
> -	group->id	= id;
> -	group->refs	= 1;
> -
> -	ret = snprintf(group_node, PATH_MAX, VFIO_DEV_DIR "/%lu", id);
> -	if (ret < 0 || ret == PATH_MAX)
> -		return NULL;
> -
> -	group->fd = open(group_node, O_RDWR);
> -	if (group->fd < 0) {
> -		pr_err("Failed to open IOMMU group %s", group_node);
> -		goto err_free_group;
> -	}
> -
> -	if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &group_status)) {
> -		pr_err("Failed to determine status of IOMMU group %lu", id);
> -		goto err_close_group;
> -	}
> -
> -	if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
> -		pr_err("IOMMU group %lu is not viable", id);
> -		goto err_close_group;
> -	}
> -
> -	if (ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &vfio_container)) {
> -		pr_err("Failed to add IOMMU group %lu to VFIO container", id);
> -		goto err_close_group;
> -	}
> -
> -	list_add(&group->list, &vfio_groups);
> -
> -	return group;
> -
> -err_close_group:
> -	close(group->fd);
> -err_free_group:
> -	free(group);
> -
> -	return NULL;
> -}
> -
>  static void vfio_group_exit(struct kvm *kvm, struct vfio_group *group)
>  {
>  	if (--group->refs != 0)
> @@ -520,78 +332,6 @@ static void vfio_group_exit(struct kvm *kvm, struct vfio_group *group)
>  	free(group);
>  }
>  
> -static struct vfio_group *
> -vfio_group_get_for_dev(struct kvm *kvm, struct vfio_device *vdev)
> -{
> -	int dirfd;
> -	ssize_t ret;
> -	char *group_name;
> -	unsigned long group_id;
> -	char group_path[PATH_MAX];
> -	struct vfio_group *group = NULL;
> -
> -	/* Find IOMMU group for this device */
> -	dirfd = open(vdev->sysfs_path, O_DIRECTORY | O_PATH | O_RDONLY);
> -	if (dirfd < 0) {
> -		vfio_dev_err(vdev, "failed to open '%s'", vdev->sysfs_path);
> -		return NULL;
> -	}
> -
> -	ret = readlinkat(dirfd, "iommu_group", group_path, PATH_MAX);
> -	if (ret < 0) {
> -		vfio_dev_err(vdev, "no iommu_group");
> -		goto out_close;
> -	}
> -	if (ret == PATH_MAX)
> -		goto out_close;
> -
> -	group_path[ret] = '\0';
> -
> -	group_name = basename(group_path);
> -	errno = 0;
> -	group_id = strtoul(group_name, NULL, 10);
> -	if (errno)
> -		goto out_close;
> -
> -	list_for_each_entry(group, &vfio_groups, list) {
> -		if (group->id == group_id) {
> -			group->refs++;
> -			return group;
> -		}
> -	}
> -
> -	group = legacy_vfio_group_create(kvm, group_id);
> -
> -out_close:
> -	close(dirfd);
> -	return group;
> -}
> -
> -static int legacy_vfio_device_init(struct kvm *kvm, struct vfio_device *vdev)
> -{
> -	int ret;
> -	char dev_path[PATH_MAX];
> -	struct vfio_group *group;
> -
> -	ret = snprintf(dev_path, PATH_MAX, "/sys/bus/%s/devices/%s",
> -		       vdev->params->bus, vdev->params->name);
> -	if (ret < 0 || ret == PATH_MAX)
> -		return -EINVAL;
> -
> -	vdev->sysfs_path = strndup(dev_path, PATH_MAX);
> -	if (!vdev->sysfs_path)
> -		return -errno;
> -
> -	group = vfio_group_get_for_dev(kvm, vdev);
> -	if (!group) {
> -		free(vdev->sysfs_path);
> -		return -EINVAL;
> -	}
> -
> -	vdev->group = group;
> -
> -	return 0;
> -}
>  
>  static void vfio_device_exit(struct kvm *kvm, struct vfio_device *vdev)
>  {
> @@ -611,57 +351,8 @@ static void vfio_device_exit(struct kvm *kvm, struct vfio_device *vdev)
>  	free(vdev->sysfs_path);
>  }
>  
> -static int legacy_vfio_container_init(struct kvm *kvm)
> -{
> -	int api, i, ret, iommu_type;;
> -
> -	/* Create a container for our IOMMU groups */
> -	vfio_container = open(VFIO_DEV_NODE, O_RDWR);
> -	if (vfio_container == -1) {
> -		ret = errno;
> -		pr_err("Failed to open %s", VFIO_DEV_NODE);
> -		return ret;
> -	}
> -
> -	api = ioctl(vfio_container, VFIO_GET_API_VERSION);
> -	if (api != VFIO_API_VERSION) {
> -		pr_err("Unknown VFIO API version %d", api);
> -		return -ENODEV;
> -	}
> -
> -	iommu_type = vfio_get_iommu_type();
> -	if (iommu_type < 0) {
> -		pr_err("VFIO type-1 IOMMU not supported on this platform");
> -		return iommu_type;
> -	}
> -
> -	/* Create groups for our devices and add them to the container */
> -	for (i = 0; i < kvm->cfg.num_vfio_devices; ++i) {
> -		vfio_devices[i].params = &kvm->cfg.vfio_devices[i];
> -
> -		ret = legacy_vfio_device_init(kvm, &vfio_devices[i]);
> -		if (ret)
> -			return ret;
> -	}
> -
> -	/* Finalise the container */
> -	if (ioctl(vfio_container, VFIO_SET_IOMMU, iommu_type)) {
> -		ret = -errno;
> -		pr_err("Failed to set IOMMU type %d for VFIO container",
> -		       iommu_type);
> -		return ret;
> -	} else {
> -		pr_info("Using IOMMU type %d for VFIO container", iommu_type);
> -	}
> -
> -	return kvm__for_each_mem_bank(kvm, KVM_MEM_TYPE_RAM, vfio_map_mem_bank,
> -				      NULL);
> -}
> -
>  static int vfio__init(struct kvm *kvm)
>  {
> -	int ret;
> -
>  	if (!kvm->cfg.num_vfio_devices)
>  		return 0;
>  
> @@ -679,19 +370,7 @@ static int vfio__init(struct kvm *kvm)
>  	}
>  	kvm_vfio_device = device.fd;
>  
> -	ret = legacy_vfio_container_init(kvm);
> -	if (ret)
> -		return ret;
> -
> -	ret = legacy_vfio_configure_groups(kvm);
> -	if (ret)
> -		return ret;
> -
> -	ret = legacy_vfio_configure_devices(kvm);
> -	if (ret)
> -		return ret;
> -
> -	return 0;
> +	return legacy_vfio__init(kvm);
>  }
>  dev_base_init(vfio__init);
>  
> @@ -708,10 +387,9 @@ static int vfio__exit(struct kvm *kvm)
>  	free(vfio_devices);
>  
>  	kvm__for_each_mem_bank(kvm, KVM_MEM_TYPE_RAM, vfio_unmap_mem_bank, NULL);
> -	close(vfio_container);
>  
>  	free(kvm->cfg.vfio_devices);
>  
> -	return 0;
> +	return legacy_vfio__exit(kvm);
>  }
>  dev_base_exit(vfio__exit);
> diff --git a/vfio/legacy.c b/vfio/legacy.c
> new file mode 100644
> index 000000000000..92d6d0bd5c80
> --- /dev/null
> +++ b/vfio/legacy.c
> @@ -0,0 +1,347 @@
> +#include "kvm/kvm.h"
> +#include "kvm/vfio.h"
> +
> +#include <linux/list.h>
> +
> +#define VFIO_DEV_DIR		"/dev/vfio"
> +#define VFIO_DEV_NODE		VFIO_DEV_DIR "/vfio"
> +static int vfio_container;
> +
> +static int legacy_vfio_configure_device(struct kvm *kvm, struct vfio_device *vdev)
> +{
> +	int ret;
> +	struct vfio_group *group = vdev->group;
> +
> +	vdev->fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD,
> +			 vdev->params->name);
> +	if (vdev->fd < 0) {
> +		vfio_dev_warn(vdev, "failed to get fd");
> +
> +		/* The device might be a bridge without an fd */
> +		return 0;
> +	}
> +
> +	vdev->info.argsz = sizeof(vdev->info);
> +	if (ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &vdev->info)) {
> +		ret = -errno;
> +		vfio_dev_err(vdev, "failed to get info");
> +		goto err_close_device;
> +	}
> +
> +	if (vdev->info.flags & VFIO_DEVICE_FLAGS_RESET &&
> +	    ioctl(vdev->fd, VFIO_DEVICE_RESET) < 0)
> +		vfio_dev_warn(vdev, "failed to reset device");
> +
> +	vdev->regions = calloc(vdev->info.num_regions, sizeof(*vdev->regions));
> +	if (!vdev->regions) {
> +		ret = -ENOMEM;
> +		goto err_close_device;
> +	}
> +
> +	/* Now for the bus-specific initialization... */
> +	switch (vdev->params->type) {
> +	case VFIO_DEVICE_PCI:
> +		BUG_ON(!(vdev->info.flags & VFIO_DEVICE_FLAGS_PCI));
> +		ret = vfio_pci_setup_device(kvm, vdev);
> +		break;
> +	default:
> +		BUG_ON(1);
> +		ret = -EINVAL;
> +	}
> +
> +	if (ret)
> +		goto err_free_regions;
> +
> +	vfio_dev_info(vdev, "assigned to device number 0x%x in group %lu",
> +		      vdev->dev_hdr.dev_num, group->id);
> +
> +	return 0;
> +
> +err_free_regions:
> +	free(vdev->regions);
> +err_close_device:
> +	close(vdev->fd);
> +
> +	return ret;
> +}
> +
> +static int legacy_vfio_configure_devices(struct kvm *kvm)
> +{
> +	int i, ret;
> +
> +	for (i = 0; i < kvm->cfg.num_vfio_devices; ++i) {
> +		ret = legacy_vfio_configure_device(kvm, &vfio_devices[i]);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static int vfio_get_iommu_type(void)
> +{
> +	if (ioctl(vfio_container, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU))
> +		return VFIO_TYPE1v2_IOMMU;
> +
> +	if (ioctl(vfio_container, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU))
> +		return VFIO_TYPE1_IOMMU;
> +
> +	return -ENODEV;
> +}
> +
> +int vfio_map_mem_range(struct kvm *kvm, __u64 host_addr, __u64 iova, __u64 size)
> +{
> +	int ret = 0;
> +	struct vfio_iommu_type1_dma_map dma_map = {
> +		.argsz	= sizeof(dma_map),
> +		.flags	= VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
> +		.vaddr	= host_addr,
> +		.iova	= iova,
> +		.size	= size,
> +	};
> +
> +	/* Map the guest memory for DMA (i.e. provide isolation) */
> +	if (ioctl(vfio_container, VFIO_IOMMU_MAP_DMA, &dma_map)) {
> +		ret = -errno;
> +		pr_err("Failed to map 0x%llx -> 0x%llx (%llu) for DMA",
> +		       dma_map.iova, dma_map.vaddr, dma_map.size);
> +	}
> +
> +	return ret;
> +}
> +
> +int vfio_unmap_mem_range(struct kvm *kvm, __u64 iova, __u64 size)
> +{
> +	struct vfio_iommu_type1_dma_unmap dma_unmap = {
> +		.argsz = sizeof(dma_unmap),
> +		.size = size,
> +		.iova = iova,
> +	};
> +
> +	ioctl(vfio_container, VFIO_IOMMU_UNMAP_DMA, &dma_unmap);
> +
> +	return 0;
> +}
> +
> +static int legacy_vfio_configure_groups(struct kvm *kvm)
> +{
> +	int ret;
> +	struct vfio_group *group;
> +
> +	list_for_each_entry(group, &vfio_groups, list) {
> +		ret = vfio_configure_reserved_regions(kvm, group);
> +		if (ret)
> +			return ret;
> +
> +		struct kvm_device_attr attr = {
> +			.group = KVM_DEV_VFIO_FILE,
> +			.attr = KVM_DEV_VFIO_FILE_ADD,
> +			.addr = (__u64)&group->fd,
> +		};
> +
> +		if (ioctl(kvm_vfio_device, KVM_SET_DEVICE_ATTR, &attr)) {
> +			pr_err("Failed KVM_SET_DEVICE_ATTR for KVM_DEV_VFIO_FILE");
> +			return -ENODEV;
> +		}
> +
> +	}
> +	return 0;
> +}
> +
> +static struct vfio_group *legacy_vfio_group_create(struct kvm *kvm, unsigned long id)
> +{
> +	int ret;
> +	struct vfio_group *group;
> +	char group_node[PATH_MAX];
> +	struct vfio_group_status group_status = {
> +		.argsz = sizeof(group_status),
> +	};
> +
> +	group = calloc(1, sizeof(*group));
> +	if (!group)
> +		return NULL;
> +
> +	group->id	= id;
> +	group->refs	= 1;
> +
> +	ret = snprintf(group_node, PATH_MAX, VFIO_DEV_DIR "/%lu", id);
> +	if (ret < 0 || ret == PATH_MAX)
> +		return NULL;
> +
> +	group->fd = open(group_node, O_RDWR);
> +	if (group->fd < 0) {
> +		pr_err("Failed to open IOMMU group %s", group_node);
> +		goto err_free_group;
> +	}
> +
> +	if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &group_status)) {
> +		pr_err("Failed to determine status of IOMMU group %lu", id);
> +		goto err_close_group;
> +	}
> +
> +	if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
> +		pr_err("IOMMU group %lu is not viable", id);
> +		goto err_close_group;
> +	}
> +
> +	if (ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &vfio_container)) {
> +		pr_err("Failed to add IOMMU group %lu to VFIO container", id);
> +		goto err_close_group;
> +	}
> +
> +	list_add(&group->list, &vfio_groups);
> +
> +	return group;
> +
> +err_close_group:
> +	close(group->fd);
> +err_free_group:
> +	free(group);
> +
> +	return NULL;
> +}
> +
> +static struct vfio_group *
> +vfio_group_get_for_dev(struct kvm *kvm, struct vfio_device *vdev)
> +{
> +	int dirfd;
> +	ssize_t ret;
> +	char *group_name;
> +	unsigned long group_id;
> +	char group_path[PATH_MAX];
> +	struct vfio_group *group = NULL;
> +
> +	/* Find IOMMU group for this device */
> +	dirfd = open(vdev->sysfs_path, O_DIRECTORY | O_PATH | O_RDONLY);
> +	if (dirfd < 0) {
> +		vfio_dev_err(vdev, "failed to open '%s'", vdev->sysfs_path);
> +		return NULL;
> +	}
> +
> +	ret = readlinkat(dirfd, "iommu_group", group_path, PATH_MAX);
> +	if (ret < 0) {
> +		vfio_dev_err(vdev, "no iommu_group");
> +		goto out_close;
> +	}
> +	if (ret == PATH_MAX)
> +		goto out_close;
> +
> +	group_path[ret] = '\0';
> +
> +	group_name = basename(group_path);
> +	errno = 0;
> +	group_id = strtoul(group_name, NULL, 10);
> +	if (errno)
> +		goto out_close;
> +
> +	list_for_each_entry(group, &vfio_groups, list) {
> +		if (group->id == group_id) {
> +			group->refs++;
> +			return group;
> +		}
> +	}
> +
> +	group = legacy_vfio_group_create(kvm, group_id);
> +
> +out_close:
> +	close(dirfd);
> +	return group;
> +}
> +
> +static int legacy_vfio_device_init(struct kvm *kvm, struct vfio_device *vdev)
> +{
> +	int ret;
> +	char dev_path[PATH_MAX];
> +	struct vfio_group *group;
> +
> +	ret = snprintf(dev_path, PATH_MAX, "/sys/bus/%s/devices/%s",
> +		       vdev->params->bus, vdev->params->name);
> +	if (ret < 0 || ret == PATH_MAX)
> +		return -EINVAL;
> +
> +	vdev->sysfs_path = strndup(dev_path, PATH_MAX);
> +	if (!vdev->sysfs_path)
> +		return -errno;
> +
> +	group = vfio_group_get_for_dev(kvm, vdev);
> +	if (!group) {
> +		free(vdev->sysfs_path);
> +		return -EINVAL;
> +	}
> +
> +	vdev->group = group;
> +
> +	return 0;
> +}
> +
> +static int legacy_vfio_container_init(struct kvm *kvm)
> +{
> +	int api, i, ret, iommu_type;;
> +
> +	/* Create a container for our IOMMU groups */
> +	vfio_container = open(VFIO_DEV_NODE, O_RDWR);
> +	if (vfio_container == -1) {
> +		ret = errno;
> +		pr_err("Failed to open %s", VFIO_DEV_NODE);
> +		return ret;
> +	}
> +
> +	api = ioctl(vfio_container, VFIO_GET_API_VERSION);
> +	if (api != VFIO_API_VERSION) {
> +		pr_err("Unknown VFIO API version %d", api);
> +		return -ENODEV;
> +	}
> +
> +	iommu_type = vfio_get_iommu_type();
> +	if (iommu_type < 0) {
> +		pr_err("VFIO type-1 IOMMU not supported on this platform");
> +		return iommu_type;
> +	}
> +
> +	/* Create groups for our devices and add them to the container */
> +	for (i = 0; i < kvm->cfg.num_vfio_devices; ++i) {
> +		vfio_devices[i].params = &kvm->cfg.vfio_devices[i];
> +
> +		ret = legacy_vfio_device_init(kvm, &vfio_devices[i]);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	/* Finalise the container */
> +	if (ioctl(vfio_container, VFIO_SET_IOMMU, iommu_type)) {
> +		ret = -errno;
> +		pr_err("Failed to set IOMMU type %d for VFIO container",
> +		       iommu_type);
> +		return ret;
> +	} else {
> +		pr_info("Using IOMMU type %d for VFIO container", iommu_type);
> +	}
> +
> +	return kvm__for_each_mem_bank(kvm, KVM_MEM_TYPE_RAM, vfio_map_mem_bank,
> +				      NULL);
> +}
> +
> +int legacy_vfio__init(struct kvm *kvm)
> +{
> +	int ret;
> +
> +	ret = legacy_vfio_container_init(kvm);
> +	if (ret)
> +		return ret;
> +
> +	ret = legacy_vfio_configure_groups(kvm);
> +	if (ret)
> +		return ret;
> +
> +	ret = legacy_vfio_configure_devices(kvm);
> +	if (ret)
> +		return ret;
> +
> +	return 0;
> +}
> +
> +int legacy_vfio__exit(struct kvm *kvm)
> +{
> +	close(vfio_container);
> +	return 0;
> +}
> -- 
> 2.43.0
> 

  reply	other threads:[~2025-07-27 18:23 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-25  7:49 [RFC PATCH kvmtool 01/10] vfio: Associate vm instance with vfio fd Aneesh Kumar K.V (Arm)
2025-05-25  7:49 ` [RFC PATCH kvmtool 02/10] vfio: Rename some functions Aneesh Kumar K.V (Arm)
2025-07-27 18:20   ` Mostafa Saleh
2025-07-29  4:53     ` Aneesh Kumar K.V
2025-05-25  7:49 ` [RFC PATCH kvmtool 03/10] vfio: Create new file legacy.c Aneesh Kumar K.V (Arm)
2025-07-27 18:23   ` Mostafa Saleh [this message]
2025-07-29  4:59     ` Aneesh Kumar K.V
2025-05-25  7:49 ` [RFC PATCH kvmtool 04/10] vfio: Update vfio header from linux kernel Aneesh Kumar K.V (Arm)
2025-07-27 18:23   ` Mostafa Saleh
2025-05-25  7:49 ` [RFC PATCH kvmtool 05/10] vfio: Add dma map/unmap handlers Aneesh Kumar K.V (Arm)
2025-07-27 18:25   ` Mostafa Saleh
2025-07-29  5:03     ` Aneesh Kumar K.V
2025-05-25  7:49 ` [RFC PATCH kvmtool 06/10] vfio/iommufd: Import iommufd header from kernel Aneesh Kumar K.V (Arm)
2025-07-27 18:25   ` Mostafa Saleh
2025-05-25  7:49 ` [RFC PATCH kvmtool 07/10] vfio/iommufd: Add basic iommufd support Aneesh Kumar K.V (Arm)
2025-07-27 18:31   ` Mostafa Saleh
2025-07-29  5:12     ` Aneesh Kumar K.V
2025-07-29  9:38       ` Mostafa Saleh
2025-05-25  7:49 ` [RFC PATCH kvmtool 08/10] vfio/iommufd: Move the hwpt allocation to helper Aneesh Kumar K.V (Arm)
2025-07-27 18:32   ` Mostafa Saleh
2025-07-29  5:14     ` Aneesh Kumar K.V
2025-07-29  9:43       ` Mostafa Saleh
2025-05-25  7:49 ` [RFC PATCH kvmtool 09/10] vfio/iommufd: Add viommu and vdevice objects Aneesh Kumar K.V (Arm)
2025-07-21 12:27   ` Will Deacon
2025-07-24 14:09     ` Aneesh Kumar K.V
2025-08-04 22:33       ` Suzuki K Poulose
2025-08-08 13:00         ` Will Deacon
2025-08-11  6:16           ` Aneesh Kumar K.V
2025-07-27 18:35   ` Mostafa Saleh
2025-07-29  5:19     ` Aneesh Kumar K.V
2025-07-29  9:41       ` Mostafa Saleh
2025-07-30  8:13         ` Aneesh Kumar K.V
2025-07-30 14:15           ` Mostafa Saleh
2025-07-31  4:39             ` Aneesh Kumar K.V
2025-08-04 15:07               ` Mostafa Saleh
2025-05-25  7:49 ` [RFC PATCH kvmtool 10/10] util/update_headers: Add vfio related header files to update list Aneesh Kumar K.V (Arm)
2025-07-27 18:35   ` Mostafa Saleh
2025-07-27 18:19 ` [RFC PATCH kvmtool 01/10] vfio: Associate vm instance with vfio fd Mostafa Saleh

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=aIZulgInZXazv8oY@google.com \
    --to=smostafa@google.com \
    --cc=Suzuki.Poulose@arm.com \
    --cc=aneesh.kumar@kernel.org \
    --cc=julien.thierry.kdev@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=steven.price@arm.com \
    --cc=will@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.