Skip to content

Commit 3e5449d

Browse files
jgunthorpeawilliam
authored andcommitted
kvm/vfio: Remove vfio_group from kvm
None of the VFIO APIs take in the vfio_group anymore, so we can remove it completely. This has a subtle side effect on the enforced coherency tracking. The vfio_group_get_external_user() was holding on to the container_users which would prevent the iommu_domain and thus the enforced coherency value from changing while the group is registered with kvm. It changes the security proof slightly into 'user must hold a group FD that has a device that cannot enforce DMA coherence'. As opening the group FD, not attaching the container, is the privileged operation this doesn't change the security properties much. On the flip side it paves the way to changing the iommu_domain/container attached to a group at runtime which is something that will be required to support nested translation. Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]>i Signed-off-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent ba70a89 commit 3e5449d

File tree

1 file changed

+8
-43
lines changed

1 file changed

+8
-43
lines changed

virt/kvm/vfio.c

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
struct kvm_vfio_group {
2525
struct list_head node;
2626
struct file *file;
27-
struct vfio_group *vfio_group;
2827
};
2928

3029
struct kvm_vfio {
@@ -33,35 +32,6 @@ struct kvm_vfio {
3332
bool noncoherent;
3433
};
3534

36-
static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
37-
{
38-
struct vfio_group *vfio_group;
39-
struct vfio_group *(*fn)(struct file *);
40-
41-
fn = symbol_get(vfio_group_get_external_user);
42-
if (!fn)
43-
return ERR_PTR(-EINVAL);
44-
45-
vfio_group = fn(filep);
46-
47-
symbol_put(vfio_group_get_external_user);
48-
49-
return vfio_group;
50-
}
51-
52-
static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
53-
{
54-
void (*fn)(struct vfio_group *);
55-
56-
fn = symbol_get(vfio_group_put_external_user);
57-
if (!fn)
58-
return;
59-
60-
fn(vfio_group);
61-
62-
symbol_put(vfio_group_put_external_user);
63-
}
64-
6535
static void kvm_vfio_file_set_kvm(struct file *file, struct kvm *kvm)
6636
{
6737
void (*fn)(struct file *file, struct kvm *kvm);
@@ -91,7 +61,6 @@ static bool kvm_vfio_file_enforced_coherent(struct file *file)
9161
return ret;
9262
}
9363

94-
#ifdef CONFIG_SPAPR_TCE_IOMMU
9564
static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
9665
{
9766
struct iommu_group *(*fn)(struct file *file);
@@ -108,6 +77,7 @@ static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
10877
return ret;
10978
}
11079

80+
#ifdef CONFIG_SPAPR_TCE_IOMMU
11181
static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
11282
struct kvm_vfio_group *kvg)
11383
{
@@ -157,7 +127,6 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev)
157127
static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
158128
{
159129
struct kvm_vfio *kv = dev->private;
160-
struct vfio_group *vfio_group;
161130
struct kvm_vfio_group *kvg;
162131
struct file *filp;
163132
int ret;
@@ -166,6 +135,12 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
166135
if (!filp)
167136
return -EBADF;
168137

138+
/* Ensure the FD is a vfio group FD.*/
139+
if (!kvm_vfio_file_iommu_group(filp)) {
140+
ret = -EINVAL;
141+
goto err_fput;
142+
}
143+
169144
mutex_lock(&kv->lock);
170145

171146
list_for_each_entry(kvg, &kv->group_list, node) {
@@ -181,15 +156,8 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
181156
goto err_unlock;
182157
}
183158

184-
vfio_group = kvm_vfio_group_get_external_user(filp);
185-
if (IS_ERR(vfio_group)) {
186-
ret = PTR_ERR(vfio_group);
187-
goto err_free;
188-
}
189-
190159
kvg->file = filp;
191160
list_add_tail(&kvg->node, &kv->group_list);
192-
kvg->vfio_group = vfio_group;
193161

194162
kvm_arch_start_assignment(dev->kvm);
195163

@@ -199,10 +167,9 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
199167
kvm_vfio_update_coherency(dev);
200168

201169
return 0;
202-
err_free:
203-
kfree(kvg);
204170
err_unlock:
205171
mutex_unlock(&kv->lock);
172+
err_fput:
206173
fput(filp);
207174
return ret;
208175
}
@@ -232,7 +199,6 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
232199
kvm_spapr_tce_release_vfio_group(dev->kvm, kvg);
233200
#endif
234201
kvm_vfio_file_set_kvm(kvg->file, NULL);
235-
kvm_vfio_group_put_external_user(kvg->vfio_group);
236202
fput(kvg->file);
237203
kfree(kvg);
238204
ret = 0;
@@ -361,7 +327,6 @@ static void kvm_vfio_destroy(struct kvm_device *dev)
361327
kvm_spapr_tce_release_vfio_group(dev->kvm, kvg);
362328
#endif
363329
kvm_vfio_file_set_kvm(kvg->file, NULL);
364-
kvm_vfio_group_put_external_user(kvg->vfio_group);
365330
fput(kvg->file);
366331
list_del(&kvg->node);
367332
kfree(kvg);

0 commit comments

Comments
 (0)