Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

vfio: Compile vfio_group infrastructure optionally

vfio_group is not needed for vfio device cdev, so with vfio device cdev
introduced, the vfio_group infrastructures can be compiled out if only
cdev is needed.

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>
Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
Tested-by: Yanting Jiang <yanting.jiang@intel.com>
Tested-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
Tested-by: Terrence Xu <terrence.xu@intel.com>
Tested-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Link: https://lore.kernel.org/r/20230718135551.6592-26-yi.l.liu@intel.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>

authored by

Yi Liu and committed by
Alex Williamson
c1cce6d0 5398be25

+123 -12
+2 -2
drivers/iommu/iommufd/Kconfig
··· 14 14 if IOMMUFD 15 15 config IOMMUFD_VFIO_CONTAINER 16 16 bool "IOMMUFD provides the VFIO container /dev/vfio/vfio" 17 - depends on VFIO && !VFIO_CONTAINER 18 - default VFIO && !VFIO_CONTAINER 17 + depends on VFIO_GROUP && !VFIO_CONTAINER 18 + default VFIO_GROUP && !VFIO_CONTAINER 19 19 help 20 20 IOMMUFD will provide /dev/vfio/vfio instead of VFIO. This relies on 21 21 IOMMUFD providing compatibility emulation to give the same ioctls.
+15
drivers/vfio/Kconfig
··· 4 4 select IOMMU_API 5 5 depends on IOMMUFD || !IOMMUFD 6 6 select INTERVAL_TREE 7 + select VFIO_GROUP if SPAPR_TCE_IOMMU || IOMMUFD=n 8 + select VFIO_DEVICE_CDEV if !VFIO_GROUP 7 9 select VFIO_CONTAINER if IOMMUFD=n 8 10 help 9 11 VFIO provides a framework for secure userspace device drivers. ··· 17 15 config VFIO_DEVICE_CDEV 18 16 bool "Support for the VFIO cdev /dev/vfio/devices/vfioX" 19 17 depends on IOMMUFD && !SPAPR_TCE_IOMMU 18 + default !VFIO_GROUP 20 19 help 21 20 The VFIO device cdev is another way for userspace to get device 22 21 access. Userspace gets device fd by opening device cdev under ··· 27 24 28 25 If you don't know what to do here, say N. 29 26 27 + config VFIO_GROUP 28 + bool "Support for the VFIO group /dev/vfio/$group_id" 29 + default y 30 + help 31 + VFIO group support provides the traditional model for accessing 32 + devices through VFIO and is used by the majority of userspace 33 + applications and drivers making use of VFIO. 34 + 35 + If you don't know what to do here, say Y. 36 + 30 37 config VFIO_CONTAINER 31 38 bool "Support for the VFIO container /dev/vfio/vfio" 32 39 select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64) 40 + depends on VFIO_GROUP 33 41 default y 34 42 help 35 43 The VFIO container is the classic interface to VFIO for establishing ··· 62 48 63 49 config VFIO_NOIOMMU 64 50 bool "VFIO No-IOMMU support" 51 + depends on VFIO_GROUP 65 52 help 66 53 VFIO is built on the ability to isolate devices using the IOMMU. 67 54 Only with an IOMMU can userspace access to DMA capable devices be
+1 -1
drivers/vfio/Makefile
··· 2 2 obj-$(CONFIG_VFIO) += vfio.o 3 3 4 4 vfio-y += vfio_main.o \ 5 - group.o \ 6 5 iova_bitmap.o 7 6 vfio-$(CONFIG_VFIO_DEVICE_CDEV) += device_cdev.o 7 + vfio-$(CONFIG_VFIO_GROUP) += group.o 8 8 vfio-$(CONFIG_IOMMUFD) += iommufd.o 9 9 vfio-$(CONFIG_VFIO_CONTAINER) += container.o 10 10 vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o
+83 -6
drivers/vfio/vfio.h
··· 36 36 37 37 extern const struct file_operations vfio_device_fops; 38 38 39 + #ifdef CONFIG_VFIO_NOIOMMU 40 + extern bool vfio_noiommu __read_mostly; 41 + #else 42 + enum { vfio_noiommu = false }; 43 + #endif 44 + 39 45 enum vfio_group_type { 40 46 /* 41 47 * Physical device with IOMMU backing. ··· 66 60 VFIO_NO_IOMMU, 67 61 }; 68 62 63 + #if IS_ENABLED(CONFIG_VFIO_GROUP) 69 64 struct vfio_group { 70 65 struct device dev; 71 66 struct cdev cdev; ··· 118 111 return IS_ENABLED(CONFIG_VFIO_NOIOMMU) && 119 112 vdev->group->type == VFIO_NO_IOMMU; 120 113 } 114 + #else 115 + struct vfio_group; 116 + 117 + static inline int vfio_device_block_group(struct vfio_device *device) 118 + { 119 + return 0; 120 + } 121 + 122 + static inline void vfio_device_unblock_group(struct vfio_device *device) 123 + { 124 + } 125 + 126 + static inline int vfio_device_set_group(struct vfio_device *device, 127 + enum vfio_group_type type) 128 + { 129 + return 0; 130 + } 131 + 132 + static inline void vfio_device_remove_group(struct vfio_device *device) 133 + { 134 + } 135 + 136 + static inline void vfio_device_group_register(struct vfio_device *device) 137 + { 138 + } 139 + 140 + static inline void vfio_device_group_unregister(struct vfio_device *device) 141 + { 142 + } 143 + 144 + static inline int vfio_device_group_use_iommu(struct vfio_device *device) 145 + { 146 + return -EOPNOTSUPP; 147 + } 148 + 149 + static inline void vfio_device_group_unuse_iommu(struct vfio_device *device) 150 + { 151 + } 152 + 153 + static inline void vfio_df_group_close(struct vfio_device_file *df) 154 + { 155 + } 156 + 157 + static inline struct vfio_group *vfio_group_from_file(struct file *file) 158 + { 159 + return NULL; 160 + } 161 + 162 + static inline bool vfio_group_enforced_coherent(struct vfio_group *group) 163 + { 164 + return true; 165 + } 166 + 167 + static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) 168 + { 169 + } 170 + 171 + static inline bool vfio_device_has_container(struct vfio_device *device) 172 + { 173 + return false; 174 + } 175 + 176 + static inline int __init vfio_group_init(void) 177 + { 178 + return 0; 179 + } 180 + 181 + static inline void vfio_group_cleanup(void) 182 + { 183 + } 184 + 185 + static inline bool vfio_device_is_noiommu(struct vfio_device *vdev) 186 + { 187 + return false; 188 + } 189 + #endif /* CONFIG_VFIO_GROUP */ 121 190 122 191 #if IS_ENABLED(CONFIG_VFIO_CONTAINER) 123 192 /** ··· 432 349 static inline void vfio_virqfd_exit(void) 433 350 { 434 351 } 435 - #endif 436 - 437 - #ifdef CONFIG_VFIO_NOIOMMU 438 - extern bool vfio_noiommu __read_mostly; 439 - #else 440 - enum { vfio_noiommu = false }; 441 352 #endif 442 353 443 354 #ifdef CONFIG_HAVE_KVM
+22 -3
include/linux/vfio.h
··· 43 43 */ 44 44 const struct vfio_migration_ops *mig_ops; 45 45 const struct vfio_log_ops *log_ops; 46 + #if IS_ENABLED(CONFIG_VFIO_GROUP) 46 47 struct vfio_group *group; 48 + struct list_head group_next; 49 + struct list_head iommu_entry; 50 + #endif 47 51 struct vfio_device_set *dev_set; 48 52 struct list_head dev_set_list; 49 53 unsigned int migration_flags; ··· 62 58 refcount_t refcount; /* user count on registered device*/ 63 59 unsigned int open_count; 64 60 struct completion comp; 65 - struct list_head group_next; 66 - struct list_head iommu_entry; 67 61 struct iommufd_access *iommufd_access; 68 62 void (*put_kvm)(struct kvm *kvm); 69 63 #if IS_ENABLED(CONFIG_IOMMUFD) ··· 286 284 /* 287 285 * External user API 288 286 */ 287 + #if IS_ENABLED(CONFIG_VFIO_GROUP) 289 288 struct iommu_group *vfio_file_iommu_group(struct file *file); 290 289 bool vfio_file_is_group(struct file *file); 290 + bool vfio_file_has_dev(struct file *file, struct vfio_device *device); 291 + #else 292 + static inline struct iommu_group *vfio_file_iommu_group(struct file *file) 293 + { 294 + return NULL; 295 + } 296 + 297 + static inline bool vfio_file_is_group(struct file *file) 298 + { 299 + return false; 300 + } 301 + 302 + static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *device) 303 + { 304 + return false; 305 + } 306 + #endif 291 307 bool vfio_file_is_valid(struct file *file); 292 308 bool vfio_file_enforced_coherent(struct file *file); 293 309 void vfio_file_set_kvm(struct file *file, struct kvm *kvm); 294 - bool vfio_file_has_dev(struct file *file, struct vfio_device *device); 295 310 296 311 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) 297 312