Skip to content

Commit c1cce6d

Browse files
yiliu1765awilliam
authored andcommitted
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 <[email protected]> Tested-by: Nicolin Chen <[email protected]> Tested-by: Matthew Rosato <[email protected]> Tested-by: Yanting Jiang <[email protected]> Tested-by: Shameer Kolothum <[email protected]> Tested-by: Terrence Xu <[email protected]> Tested-by: Zhenzhong Duan <[email protected]> Signed-off-by: Yi Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent 5398be2 commit c1cce6d

File tree

5 files changed

+123
-12
lines changed

5 files changed

+123
-12
lines changed

drivers/iommu/iommufd/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ config IOMMUFD
1414
if IOMMUFD
1515
config IOMMUFD_VFIO_CONTAINER
1616
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
1919
help
2020
IOMMUFD will provide /dev/vfio/vfio instead of VFIO. This relies on
2121
IOMMUFD providing compatibility emulation to give the same ioctls.

drivers/vfio/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ menuconfig VFIO
44
select IOMMU_API
55
depends on IOMMUFD || !IOMMUFD
66
select INTERVAL_TREE
7+
select VFIO_GROUP if SPAPR_TCE_IOMMU || IOMMUFD=n
8+
select VFIO_DEVICE_CDEV if !VFIO_GROUP
79
select VFIO_CONTAINER if IOMMUFD=n
810
help
911
VFIO provides a framework for secure userspace device drivers.
@@ -15,6 +17,7 @@ if VFIO
1517
config VFIO_DEVICE_CDEV
1618
bool "Support for the VFIO cdev /dev/vfio/devices/vfioX"
1719
depends on IOMMUFD && !SPAPR_TCE_IOMMU
20+
default !VFIO_GROUP
1821
help
1922
The VFIO device cdev is another way for userspace to get device
2023
access. Userspace gets device fd by opening device cdev under
@@ -24,9 +27,20 @@ config VFIO_DEVICE_CDEV
2427

2528
If you don't know what to do here, say N.
2629

30+
config VFIO_GROUP
31+
bool "Support for the VFIO group /dev/vfio/$group_id"
32+
default y
33+
help
34+
VFIO group support provides the traditional model for accessing
35+
devices through VFIO and is used by the majority of userspace
36+
applications and drivers making use of VFIO.
37+
38+
If you don't know what to do here, say Y.
39+
2740
config VFIO_CONTAINER
2841
bool "Support for the VFIO container /dev/vfio/vfio"
2942
select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64)
43+
depends on VFIO_GROUP
3044
default y
3145
help
3246
The VFIO container is the classic interface to VFIO for establishing
@@ -48,6 +62,7 @@ endif
4862

4963
config VFIO_NOIOMMU
5064
bool "VFIO No-IOMMU support"
65+
depends on VFIO_GROUP
5166
help
5267
VFIO is built on the ability to isolate devices using the IOMMU.
5368
Only with an IOMMU can userspace access to DMA capable devices be

drivers/vfio/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
obj-$(CONFIG_VFIO) += vfio.o
33

44
vfio-y += vfio_main.o \
5-
group.o \
65
iova_bitmap.o
76
vfio-$(CONFIG_VFIO_DEVICE_CDEV) += device_cdev.o
7+
vfio-$(CONFIG_VFIO_GROUP) += group.o
88
vfio-$(CONFIG_IOMMUFD) += iommufd.o
99
vfio-$(CONFIG_VFIO_CONTAINER) += container.o
1010
vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o

drivers/vfio/vfio.h

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ vfio_allocate_device_file(struct vfio_device *device);
3636

3737
extern const struct file_operations vfio_device_fops;
3838

39+
#ifdef CONFIG_VFIO_NOIOMMU
40+
extern bool vfio_noiommu __read_mostly;
41+
#else
42+
enum { vfio_noiommu = false };
43+
#endif
44+
3945
enum vfio_group_type {
4046
/*
4147
* Physical device with IOMMU backing.
@@ -60,6 +66,7 @@ enum vfio_group_type {
6066
VFIO_NO_IOMMU,
6167
};
6268

69+
#if IS_ENABLED(CONFIG_VFIO_GROUP)
6370
struct vfio_group {
6471
struct device dev;
6572
struct cdev cdev;
@@ -111,6 +118,82 @@ static inline bool vfio_device_is_noiommu(struct vfio_device *vdev)
111118
return IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&
112119
vdev->group->type == VFIO_NO_IOMMU;
113120
}
121+
#else
122+
struct vfio_group;
123+
124+
static inline int vfio_device_block_group(struct vfio_device *device)
125+
{
126+
return 0;
127+
}
128+
129+
static inline void vfio_device_unblock_group(struct vfio_device *device)
130+
{
131+
}
132+
133+
static inline int vfio_device_set_group(struct vfio_device *device,
134+
enum vfio_group_type type)
135+
{
136+
return 0;
137+
}
138+
139+
static inline void vfio_device_remove_group(struct vfio_device *device)
140+
{
141+
}
142+
143+
static inline void vfio_device_group_register(struct vfio_device *device)
144+
{
145+
}
146+
147+
static inline void vfio_device_group_unregister(struct vfio_device *device)
148+
{
149+
}
150+
151+
static inline int vfio_device_group_use_iommu(struct vfio_device *device)
152+
{
153+
return -EOPNOTSUPP;
154+
}
155+
156+
static inline void vfio_device_group_unuse_iommu(struct vfio_device *device)
157+
{
158+
}
159+
160+
static inline void vfio_df_group_close(struct vfio_device_file *df)
161+
{
162+
}
163+
164+
static inline struct vfio_group *vfio_group_from_file(struct file *file)
165+
{
166+
return NULL;
167+
}
168+
169+
static inline bool vfio_group_enforced_coherent(struct vfio_group *group)
170+
{
171+
return true;
172+
}
173+
174+
static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
175+
{
176+
}
177+
178+
static inline bool vfio_device_has_container(struct vfio_device *device)
179+
{
180+
return false;
181+
}
182+
183+
static inline int __init vfio_group_init(void)
184+
{
185+
return 0;
186+
}
187+
188+
static inline void vfio_group_cleanup(void)
189+
{
190+
}
191+
192+
static inline bool vfio_device_is_noiommu(struct vfio_device *vdev)
193+
{
194+
return false;
195+
}
196+
#endif /* CONFIG_VFIO_GROUP */
114197

115198
#if IS_ENABLED(CONFIG_VFIO_CONTAINER)
116199
/**
@@ -351,12 +434,6 @@ static inline void vfio_virqfd_exit(void)
351434
}
352435
#endif
353436

354-
#ifdef CONFIG_VFIO_NOIOMMU
355-
extern bool vfio_noiommu __read_mostly;
356-
#else
357-
enum { vfio_noiommu = false };
358-
#endif
359-
360437
#ifdef CONFIG_HAVE_KVM
361438
void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm);
362439
void vfio_device_put_kvm(struct vfio_device *device);

include/linux/vfio.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ struct vfio_device {
4343
*/
4444
const struct vfio_migration_ops *mig_ops;
4545
const struct vfio_log_ops *log_ops;
46+
#if IS_ENABLED(CONFIG_VFIO_GROUP)
4647
struct vfio_group *group;
48+
struct list_head group_next;
49+
struct list_head iommu_entry;
50+
#endif
4751
struct vfio_device_set *dev_set;
4852
struct list_head dev_set_list;
4953
unsigned int migration_flags;
@@ -58,8 +62,6 @@ struct vfio_device {
5862
refcount_t refcount; /* user count on registered device*/
5963
unsigned int open_count;
6064
struct completion comp;
61-
struct list_head group_next;
62-
struct list_head iommu_entry;
6365
struct iommufd_access *iommufd_access;
6466
void (*put_kvm)(struct kvm *kvm);
6567
#if IS_ENABLED(CONFIG_IOMMUFD)
@@ -284,12 +286,29 @@ int vfio_mig_get_next_state(struct vfio_device *device,
284286
/*
285287
* External user API
286288
*/
289+
#if IS_ENABLED(CONFIG_VFIO_GROUP)
287290
struct iommu_group *vfio_file_iommu_group(struct file *file);
288291
bool vfio_file_is_group(struct file *file);
292+
bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
293+
#else
294+
static inline struct iommu_group *vfio_file_iommu_group(struct file *file)
295+
{
296+
return NULL;
297+
}
298+
299+
static inline bool vfio_file_is_group(struct file *file)
300+
{
301+
return false;
302+
}
303+
304+
static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
305+
{
306+
return false;
307+
}
308+
#endif
289309
bool vfio_file_is_valid(struct file *file);
290310
bool vfio_file_enforced_coherent(struct file *file);
291311
void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
292-
bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
293312

294313
#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
295314

0 commit comments

Comments
 (0)