Lines Matching refs:group
26 static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group, in vfio_device_get_from_name() argument
31 mutex_lock(&group->device_lock); in vfio_device_get_from_name()
32 list_for_each_entry(it, &group->device_list, group_next) { in vfio_device_get_from_name()
50 mutex_unlock(&group->device_lock); in vfio_device_get_from_name()
58 static bool vfio_group_has_iommu(struct vfio_group *group) in vfio_group_has_iommu() argument
60 lockdep_assert_held(&group->group_lock); in vfio_group_has_iommu()
65 WARN_ON(!group->container != !group->container_users); in vfio_group_has_iommu()
67 return group->container || group->iommufd; in vfio_group_has_iommu()
76 static int vfio_group_ioctl_unset_container(struct vfio_group *group) in vfio_group_ioctl_unset_container() argument
80 mutex_lock(&group->group_lock); in vfio_group_ioctl_unset_container()
81 if (!vfio_group_has_iommu(group)) { in vfio_group_ioctl_unset_container()
85 if (group->container) { in vfio_group_ioctl_unset_container()
86 if (group->container_users != 1) { in vfio_group_ioctl_unset_container()
90 vfio_group_detach_container(group); in vfio_group_ioctl_unset_container()
92 if (group->iommufd) { in vfio_group_ioctl_unset_container()
93 iommufd_ctx_put(group->iommufd); in vfio_group_ioctl_unset_container()
94 group->iommufd = NULL; in vfio_group_ioctl_unset_container()
98 mutex_unlock(&group->group_lock); in vfio_group_ioctl_unset_container()
102 static int vfio_group_ioctl_set_container(struct vfio_group *group, in vfio_group_ioctl_set_container() argument
118 mutex_lock(&group->group_lock); in vfio_group_ioctl_set_container()
119 if (vfio_group_has_iommu(group)) { in vfio_group_ioctl_set_container()
123 if (!group->iommu_group) { in vfio_group_ioctl_set_container()
130 ret = vfio_container_attach_group(container, group); in vfio_group_ioctl_set_container()
137 group->type == VFIO_NO_IOMMU) in vfio_group_ioctl_set_container()
147 group->iommufd = iommufd; in vfio_group_ioctl_set_container()
155 mutex_unlock(&group->group_lock); in vfio_group_ioctl_set_container()
162 spin_lock(&device->group->kvm_ref_lock); in vfio_device_group_get_kvm_safe()
163 vfio_device_get_kvm_safe(device, device->group->kvm); in vfio_device_group_get_kvm_safe()
164 spin_unlock(&device->group->kvm_ref_lock); in vfio_device_group_get_kvm_safe()
172 mutex_lock(&device->group->group_lock); in vfio_df_group_open()
173 if (!vfio_group_has_iommu(device->group)) { in vfio_df_group_open()
189 df->iommufd = device->group->iommufd; in vfio_df_group_open()
221 mutex_unlock(&device->group->group_lock); in vfio_df_group_open()
232 mutex_unlock(&device->group->group_lock); in vfio_df_group_open()
240 mutex_lock(&device->group->group_lock); in vfio_df_group_close()
250 mutex_unlock(&device->group->group_lock); in vfio_df_group_close()
265 df->group = device->group; in vfio_device_open_file()
296 if (device->group->type == VFIO_NO_IOMMU) in vfio_device_open_file()
313 static int vfio_group_ioctl_get_device_fd(struct vfio_group *group, in vfio_group_ioctl_get_device_fd() argument
326 device = vfio_device_get_from_name(group, buf); in vfio_group_ioctl_get_device_fd()
353 static int vfio_group_ioctl_get_status(struct vfio_group *group, in vfio_group_ioctl_get_status() argument
367 mutex_lock(&group->group_lock); in vfio_group_ioctl_get_status()
368 if (!group->iommu_group) { in vfio_group_ioctl_get_status()
369 mutex_unlock(&group->group_lock); in vfio_group_ioctl_get_status()
380 if (vfio_group_has_iommu(group)) in vfio_group_ioctl_get_status()
383 else if (!iommu_group_dma_owner_claimed(group->iommu_group)) in vfio_group_ioctl_get_status()
385 mutex_unlock(&group->group_lock); in vfio_group_ioctl_get_status()
395 struct vfio_group *group = filep->private_data; in vfio_group_fops_unl_ioctl() local
400 return vfio_group_ioctl_get_device_fd(group, uarg); in vfio_group_fops_unl_ioctl()
402 return vfio_group_ioctl_get_status(group, uarg); in vfio_group_fops_unl_ioctl()
404 return vfio_group_ioctl_set_container(group, uarg); in vfio_group_fops_unl_ioctl()
406 return vfio_group_ioctl_unset_container(group); in vfio_group_fops_unl_ioctl()
414 struct vfio_group *group = device->group; in vfio_device_block_group() local
417 mutex_lock(&group->group_lock); in vfio_device_block_group()
418 if (group->opened_file) { in vfio_device_block_group()
423 group->cdev_device_open_cnt++; in vfio_device_block_group()
426 mutex_unlock(&group->group_lock); in vfio_device_block_group()
432 struct vfio_group *group = device->group; in vfio_device_unblock_group() local
434 mutex_lock(&group->group_lock); in vfio_device_unblock_group()
435 group->cdev_device_open_cnt--; in vfio_device_unblock_group()
436 mutex_unlock(&group->group_lock); in vfio_device_unblock_group()
441 struct vfio_group *group = in vfio_group_fops_open() local
445 mutex_lock(&group->group_lock); in vfio_group_fops_open()
451 if (refcount_read(&group->drivers) == 0) { in vfio_group_fops_open()
456 if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) { in vfio_group_fops_open()
461 if (group->cdev_device_open_cnt) { in vfio_group_fops_open()
469 if (group->opened_file) { in vfio_group_fops_open()
473 group->opened_file = filep; in vfio_group_fops_open()
474 filep->private_data = group; in vfio_group_fops_open()
477 mutex_unlock(&group->group_lock); in vfio_group_fops_open()
483 struct vfio_group *group = filep->private_data; in vfio_group_fops_release() local
487 mutex_lock(&group->group_lock); in vfio_group_fops_release()
492 WARN_ON(group->notifier.head); in vfio_group_fops_release()
493 if (group->container) in vfio_group_fops_release()
494 vfio_group_detach_container(group); in vfio_group_fops_release()
495 if (group->iommufd) { in vfio_group_fops_release()
496 iommufd_ctx_put(group->iommufd); in vfio_group_fops_release()
497 group->iommufd = NULL; in vfio_group_fops_release()
499 group->opened_file = NULL; in vfio_group_fops_release()
500 mutex_unlock(&group->group_lock); in vfio_group_fops_release()
518 struct vfio_group *group; in vfio_group_find_from_iommu() local
526 list_for_each_entry(group, &vfio.group_list, vfio_next) { in vfio_group_find_from_iommu()
527 if (group->iommu_group == iommu_group) in vfio_group_find_from_iommu()
528 return group; in vfio_group_find_from_iommu()
535 struct vfio_group *group = container_of(dev, struct vfio_group, dev); in vfio_group_release() local
537 mutex_destroy(&group->device_lock); in vfio_group_release()
538 mutex_destroy(&group->group_lock); in vfio_group_release()
539 WARN_ON(group->iommu_group); in vfio_group_release()
540 WARN_ON(group->cdev_device_open_cnt); in vfio_group_release()
541 ida_free(&vfio.group_ida, MINOR(group->dev.devt)); in vfio_group_release()
542 kfree(group); in vfio_group_release()
548 struct vfio_group *group; in vfio_group_alloc() local
551 group = kzalloc(sizeof(*group), GFP_KERNEL); in vfio_group_alloc()
552 if (!group) in vfio_group_alloc()
557 kfree(group); in vfio_group_alloc()
561 device_initialize(&group->dev); in vfio_group_alloc()
562 group->dev.devt = MKDEV(MAJOR(vfio.group_devt), minor); in vfio_group_alloc()
563 group->dev.class = vfio.class; in vfio_group_alloc()
564 group->dev.release = vfio_group_release; in vfio_group_alloc()
565 cdev_init(&group->cdev, &vfio_group_fops); in vfio_group_alloc()
566 group->cdev.owner = THIS_MODULE; in vfio_group_alloc()
568 refcount_set(&group->drivers, 1); in vfio_group_alloc()
569 mutex_init(&group->group_lock); in vfio_group_alloc()
570 spin_lock_init(&group->kvm_ref_lock); in vfio_group_alloc()
571 INIT_LIST_HEAD(&group->device_list); in vfio_group_alloc()
572 mutex_init(&group->device_lock); in vfio_group_alloc()
573 group->iommu_group = iommu_group; in vfio_group_alloc()
576 group->type = type; in vfio_group_alloc()
577 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); in vfio_group_alloc()
579 return group; in vfio_group_alloc()
585 struct vfio_group *group; in vfio_create_group() local
591 group = vfio_group_alloc(iommu_group, type); in vfio_create_group()
592 if (IS_ERR(group)) in vfio_create_group()
593 return group; in vfio_create_group()
595 err = dev_set_name(&group->dev, "%s%d", in vfio_create_group()
596 group->type == VFIO_NO_IOMMU ? "noiommu-" : "", in vfio_create_group()
603 err = cdev_device_add(&group->cdev, &group->dev); in vfio_create_group()
609 list_add(&group->vfio_next, &vfio.group_list); in vfio_create_group()
611 return group; in vfio_create_group()
614 put_device(&group->dev); in vfio_create_group()
622 struct vfio_group *group; in vfio_noiommu_group_alloc() local
637 group = vfio_create_group(iommu_group, type); in vfio_noiommu_group_alloc()
639 if (IS_ERR(group)) { in vfio_noiommu_group_alloc()
640 ret = PTR_ERR(group); in vfio_noiommu_group_alloc()
644 return group; in vfio_noiommu_group_alloc()
653 static bool vfio_group_has_device(struct vfio_group *group, struct device *dev) in vfio_group_has_device() argument
657 mutex_lock(&group->device_lock); in vfio_group_has_device()
658 list_for_each_entry(device, &group->device_list, group_next) { in vfio_group_has_device()
660 mutex_unlock(&group->device_lock); in vfio_group_has_device()
664 mutex_unlock(&group->device_lock); in vfio_group_has_device()
671 struct vfio_group *group; in vfio_group_find_or_alloc() local
681 group = vfio_noiommu_group_alloc(dev, VFIO_NO_IOMMU); in vfio_group_find_or_alloc()
682 if (!IS_ERR(group)) { in vfio_group_find_or_alloc()
686 return group; in vfio_group_find_or_alloc()
693 group = vfio_group_find_from_iommu(iommu_group); in vfio_group_find_or_alloc()
694 if (group) { in vfio_group_find_or_alloc()
695 if (WARN_ON(vfio_group_has_device(group, dev))) in vfio_group_find_or_alloc()
696 group = ERR_PTR(-EINVAL); in vfio_group_find_or_alloc()
698 refcount_inc(&group->drivers); in vfio_group_find_or_alloc()
700 group = vfio_create_group(iommu_group, VFIO_IOMMU); in vfio_group_find_or_alloc()
706 return group; in vfio_group_find_or_alloc()
712 struct vfio_group *group; in vfio_device_set_group() local
715 group = vfio_group_find_or_alloc(device->dev); in vfio_device_set_group()
717 group = vfio_noiommu_group_alloc(device->dev, type); in vfio_device_set_group()
719 if (IS_ERR(group)) in vfio_device_set_group()
720 return PTR_ERR(group); in vfio_device_set_group()
723 device->group = group; in vfio_device_set_group()
729 struct vfio_group *group = device->group; in vfio_device_remove_group() local
732 if (group->type == VFIO_NO_IOMMU || group->type == VFIO_EMULATED_IOMMU) in vfio_device_remove_group()
736 if (!refcount_dec_and_mutex_lock(&group->drivers, &vfio.group_lock)) in vfio_device_remove_group()
738 list_del(&group->vfio_next); in vfio_device_remove_group()
746 cdev_device_del(&group->cdev, &group->dev); in vfio_device_remove_group()
748 mutex_lock(&group->group_lock); in vfio_device_remove_group()
755 WARN_ON(!list_empty(&group->device_list)); in vfio_device_remove_group()
756 WARN_ON(group->notifier.head); in vfio_device_remove_group()
763 if (group->container) in vfio_device_remove_group()
764 vfio_group_detach_container(group); in vfio_device_remove_group()
765 iommu_group = group->iommu_group; in vfio_device_remove_group()
766 group->iommu_group = NULL; in vfio_device_remove_group()
767 mutex_unlock(&group->group_lock); in vfio_device_remove_group()
771 put_device(&group->dev); in vfio_device_remove_group()
776 mutex_lock(&device->group->device_lock); in vfio_device_group_register()
777 list_add(&device->group_next, &device->group->device_list); in vfio_device_group_register()
778 mutex_unlock(&device->group->device_lock); in vfio_device_group_register()
783 mutex_lock(&device->group->device_lock); in vfio_device_group_unregister()
785 mutex_unlock(&device->group->device_lock); in vfio_device_group_unregister()
790 struct vfio_group *group = device->group; in vfio_device_group_use_iommu() local
793 lockdep_assert_held(&group->group_lock); in vfio_device_group_use_iommu()
795 if (WARN_ON(!group->container)) in vfio_device_group_use_iommu()
798 ret = vfio_group_use_container(group); in vfio_device_group_use_iommu()
807 struct vfio_group *group = device->group; in vfio_device_group_unuse_iommu() local
809 lockdep_assert_held(&group->group_lock); in vfio_device_group_unuse_iommu()
811 if (WARN_ON(!group->container)) in vfio_device_group_unuse_iommu()
815 vfio_group_unuse_container(group); in vfio_device_group_unuse_iommu()
820 return device->group->container; in vfio_device_has_container()
825 struct vfio_group *group = file->private_data; in vfio_group_from_file() local
829 return group; in vfio_group_from_file()
842 struct vfio_group *group = vfio_group_from_file(file); in vfio_file_iommu_group() local
848 if (!group) in vfio_file_iommu_group()
851 mutex_lock(&group->group_lock); in vfio_file_iommu_group()
852 if (group->iommu_group) { in vfio_file_iommu_group()
853 iommu_group = group->iommu_group; in vfio_file_iommu_group()
856 mutex_unlock(&group->group_lock); in vfio_file_iommu_group()
871 bool vfio_group_enforced_coherent(struct vfio_group *group) in vfio_group_enforced_coherent() argument
882 mutex_lock(&group->device_lock); in vfio_group_enforced_coherent()
883 list_for_each_entry(device, &group->device_list, group_next) { in vfio_group_enforced_coherent()
890 mutex_unlock(&group->device_lock); in vfio_group_enforced_coherent()
894 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) in vfio_group_set_kvm() argument
896 spin_lock(&group->kvm_ref_lock); in vfio_group_set_kvm()
897 group->kvm = kvm; in vfio_group_set_kvm()
898 spin_unlock(&group->kvm_ref_lock); in vfio_group_set_kvm()
910 struct vfio_group *group = vfio_group_from_file(file); in vfio_file_has_dev() local
912 if (!group) in vfio_file_has_dev()
915 return group == device->group; in vfio_file_has_dev()