Loading...
1/*
2 * VFIO-KVM bridge pseudo device
3 *
4 * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/errno.h>
13#include <linux/file.h>
14#include <linux/kvm_host.h>
15#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/uaccess.h>
20#include <linux/vfio.h>
21
22struct kvm_vfio_group {
23 struct list_head node;
24 struct vfio_group *vfio_group;
25};
26
27struct kvm_vfio {
28 struct list_head group_list;
29 struct mutex lock;
30 bool noncoherent;
31};
32
33static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
34{
35 struct vfio_group *vfio_group;
36 struct vfio_group *(*fn)(struct file *);
37
38 fn = symbol_get(vfio_group_get_external_user);
39 if (!fn)
40 return ERR_PTR(-EINVAL);
41
42 vfio_group = fn(filep);
43
44 symbol_put(vfio_group_get_external_user);
45
46 return vfio_group;
47}
48
49static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
50{
51 void (*fn)(struct vfio_group *);
52
53 fn = symbol_get(vfio_group_put_external_user);
54 if (!fn)
55 return;
56
57 fn(vfio_group);
58
59 symbol_put(vfio_group_put_external_user);
60}
61
62static bool kvm_vfio_group_is_coherent(struct vfio_group *vfio_group)
63{
64 long (*fn)(struct vfio_group *, unsigned long);
65 long ret;
66
67 fn = symbol_get(vfio_external_check_extension);
68 if (!fn)
69 return false;
70
71 ret = fn(vfio_group, VFIO_DMA_CC_IOMMU);
72
73 symbol_put(vfio_external_check_extension);
74
75 return ret > 0;
76}
77
78/*
79 * Groups can use the same or different IOMMU domains. If the same then
80 * adding a new group may change the coherency of groups we've previously
81 * been told about. We don't want to care about any of that so we retest
82 * each group and bail as soon as we find one that's noncoherent. This
83 * means we only ever [un]register_noncoherent_dma once for the whole device.
84 */
85static void kvm_vfio_update_coherency(struct kvm_device *dev)
86{
87 struct kvm_vfio *kv = dev->private;
88 bool noncoherent = false;
89 struct kvm_vfio_group *kvg;
90
91 mutex_lock(&kv->lock);
92
93 list_for_each_entry(kvg, &kv->group_list, node) {
94 if (!kvm_vfio_group_is_coherent(kvg->vfio_group)) {
95 noncoherent = true;
96 break;
97 }
98 }
99
100 if (noncoherent != kv->noncoherent) {
101 kv->noncoherent = noncoherent;
102
103 if (kv->noncoherent)
104 kvm_arch_register_noncoherent_dma(dev->kvm);
105 else
106 kvm_arch_unregister_noncoherent_dma(dev->kvm);
107 }
108
109 mutex_unlock(&kv->lock);
110}
111
112static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)
113{
114 struct kvm_vfio *kv = dev->private;
115 struct vfio_group *vfio_group;
116 struct kvm_vfio_group *kvg;
117 int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
118 struct fd f;
119 int32_t fd;
120 int ret;
121
122 switch (attr) {
123 case KVM_DEV_VFIO_GROUP_ADD:
124 if (get_user(fd, argp))
125 return -EFAULT;
126
127 f = fdget(fd);
128 if (!f.file)
129 return -EBADF;
130
131 vfio_group = kvm_vfio_group_get_external_user(f.file);
132 fdput(f);
133
134 if (IS_ERR(vfio_group))
135 return PTR_ERR(vfio_group);
136
137 mutex_lock(&kv->lock);
138
139 list_for_each_entry(kvg, &kv->group_list, node) {
140 if (kvg->vfio_group == vfio_group) {
141 mutex_unlock(&kv->lock);
142 kvm_vfio_group_put_external_user(vfio_group);
143 return -EEXIST;
144 }
145 }
146
147 kvg = kzalloc(sizeof(*kvg), GFP_KERNEL);
148 if (!kvg) {
149 mutex_unlock(&kv->lock);
150 kvm_vfio_group_put_external_user(vfio_group);
151 return -ENOMEM;
152 }
153
154 list_add_tail(&kvg->node, &kv->group_list);
155 kvg->vfio_group = vfio_group;
156
157 mutex_unlock(&kv->lock);
158
159 kvm_vfio_update_coherency(dev);
160
161 return 0;
162
163 case KVM_DEV_VFIO_GROUP_DEL:
164 if (get_user(fd, argp))
165 return -EFAULT;
166
167 f = fdget(fd);
168 if (!f.file)
169 return -EBADF;
170
171 vfio_group = kvm_vfio_group_get_external_user(f.file);
172 fdput(f);
173
174 if (IS_ERR(vfio_group))
175 return PTR_ERR(vfio_group);
176
177 ret = -ENOENT;
178
179 mutex_lock(&kv->lock);
180
181 list_for_each_entry(kvg, &kv->group_list, node) {
182 if (kvg->vfio_group != vfio_group)
183 continue;
184
185 list_del(&kvg->node);
186 kvm_vfio_group_put_external_user(kvg->vfio_group);
187 kfree(kvg);
188 ret = 0;
189 break;
190 }
191
192 mutex_unlock(&kv->lock);
193
194 kvm_vfio_group_put_external_user(vfio_group);
195
196 kvm_vfio_update_coherency(dev);
197
198 return ret;
199 }
200
201 return -ENXIO;
202}
203
204static int kvm_vfio_set_attr(struct kvm_device *dev,
205 struct kvm_device_attr *attr)
206{
207 switch (attr->group) {
208 case KVM_DEV_VFIO_GROUP:
209 return kvm_vfio_set_group(dev, attr->attr, attr->addr);
210 }
211
212 return -ENXIO;
213}
214
215static int kvm_vfio_has_attr(struct kvm_device *dev,
216 struct kvm_device_attr *attr)
217{
218 switch (attr->group) {
219 case KVM_DEV_VFIO_GROUP:
220 switch (attr->attr) {
221 case KVM_DEV_VFIO_GROUP_ADD:
222 case KVM_DEV_VFIO_GROUP_DEL:
223 return 0;
224 }
225
226 break;
227 }
228
229 return -ENXIO;
230}
231
232static void kvm_vfio_destroy(struct kvm_device *dev)
233{
234 struct kvm_vfio *kv = dev->private;
235 struct kvm_vfio_group *kvg, *tmp;
236
237 list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
238 kvm_vfio_group_put_external_user(kvg->vfio_group);
239 list_del(&kvg->node);
240 kfree(kvg);
241 }
242
243 kvm_vfio_update_coherency(dev);
244
245 kfree(kv);
246 kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */
247}
248
249static int kvm_vfio_create(struct kvm_device *dev, u32 type)
250{
251 struct kvm_device *tmp;
252 struct kvm_vfio *kv;
253
254 /* Only one VFIO "device" per VM */
255 list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
256 if (tmp->ops == &kvm_vfio_ops)
257 return -EBUSY;
258
259 kv = kzalloc(sizeof(*kv), GFP_KERNEL);
260 if (!kv)
261 return -ENOMEM;
262
263 INIT_LIST_HEAD(&kv->group_list);
264 mutex_init(&kv->lock);
265
266 dev->private = kv;
267
268 return 0;
269}
270
271struct kvm_device_ops kvm_vfio_ops = {
272 .name = "kvm-vfio",
273 .create = kvm_vfio_create,
274 .destroy = kvm_vfio_destroy,
275 .set_attr = kvm_vfio_set_attr,
276 .has_attr = kvm_vfio_has_attr,
277};
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * VFIO-KVM bridge pseudo device
4 *
5 * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
7 */
8
9#include <linux/errno.h>
10#include <linux/file.h>
11#include <linux/kvm_host.h>
12#include <linux/list.h>
13#include <linux/module.h>
14#include <linux/mutex.h>
15#include <linux/slab.h>
16#include <linux/uaccess.h>
17#include <linux/vfio.h>
18#include "vfio.h"
19
20#ifdef CONFIG_SPAPR_TCE_IOMMU
21#include <asm/kvm_ppc.h>
22#endif
23
24struct kvm_vfio_file {
25 struct list_head node;
26 struct file *file;
27#ifdef CONFIG_SPAPR_TCE_IOMMU
28 struct iommu_group *iommu_group;
29#endif
30};
31
32struct kvm_vfio {
33 struct list_head file_list;
34 struct mutex lock;
35 bool noncoherent;
36};
37
38static void kvm_vfio_file_set_kvm(struct file *file, struct kvm *kvm)
39{
40 void (*fn)(struct file *file, struct kvm *kvm);
41
42 fn = symbol_get(vfio_file_set_kvm);
43 if (!fn)
44 return;
45
46 fn(file, kvm);
47
48 symbol_put(vfio_file_set_kvm);
49}
50
51static bool kvm_vfio_file_enforced_coherent(struct file *file)
52{
53 bool (*fn)(struct file *file);
54 bool ret;
55
56 fn = symbol_get(vfio_file_enforced_coherent);
57 if (!fn)
58 return false;
59
60 ret = fn(file);
61
62 symbol_put(vfio_file_enforced_coherent);
63
64 return ret;
65}
66
67static bool kvm_vfio_file_is_valid(struct file *file)
68{
69 bool (*fn)(struct file *file);
70 bool ret;
71
72 fn = symbol_get(vfio_file_is_valid);
73 if (!fn)
74 return false;
75
76 ret = fn(file);
77
78 symbol_put(vfio_file_is_valid);
79
80 return ret;
81}
82
83#ifdef CONFIG_SPAPR_TCE_IOMMU
84static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
85{
86 struct iommu_group *(*fn)(struct file *file);
87 struct iommu_group *ret;
88
89 fn = symbol_get(vfio_file_iommu_group);
90 if (!fn)
91 return NULL;
92
93 ret = fn(file);
94
95 symbol_put(vfio_file_iommu_group);
96
97 return ret;
98}
99
100static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
101 struct kvm_vfio_file *kvf)
102{
103 if (WARN_ON_ONCE(!kvf->iommu_group))
104 return;
105
106 kvm_spapr_tce_release_iommu_group(kvm, kvf->iommu_group);
107 iommu_group_put(kvf->iommu_group);
108 kvf->iommu_group = NULL;
109}
110#endif
111
112/*
113 * Groups/devices can use the same or different IOMMU domains. If the same
114 * then adding a new group/device may change the coherency of groups/devices
115 * we've previously been told about. We don't want to care about any of
116 * that so we retest each group/device and bail as soon as we find one that's
117 * noncoherent. This means we only ever [un]register_noncoherent_dma once
118 * for the whole device.
119 */
120static void kvm_vfio_update_coherency(struct kvm_device *dev)
121{
122 struct kvm_vfio *kv = dev->private;
123 bool noncoherent = false;
124 struct kvm_vfio_file *kvf;
125
126 list_for_each_entry(kvf, &kv->file_list, node) {
127 if (!kvm_vfio_file_enforced_coherent(kvf->file)) {
128 noncoherent = true;
129 break;
130 }
131 }
132
133 if (noncoherent != kv->noncoherent) {
134 kv->noncoherent = noncoherent;
135
136 if (kv->noncoherent)
137 kvm_arch_register_noncoherent_dma(dev->kvm);
138 else
139 kvm_arch_unregister_noncoherent_dma(dev->kvm);
140 }
141}
142
143static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
144{
145 struct kvm_vfio *kv = dev->private;
146 struct kvm_vfio_file *kvf;
147 struct file *filp;
148 int ret = 0;
149
150 filp = fget(fd);
151 if (!filp)
152 return -EBADF;
153
154 /* Ensure the FD is a vfio FD. */
155 if (!kvm_vfio_file_is_valid(filp)) {
156 ret = -EINVAL;
157 goto out_fput;
158 }
159
160 mutex_lock(&kv->lock);
161
162 list_for_each_entry(kvf, &kv->file_list, node) {
163 if (kvf->file == filp) {
164 ret = -EEXIST;
165 goto out_unlock;
166 }
167 }
168
169 kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT);
170 if (!kvf) {
171 ret = -ENOMEM;
172 goto out_unlock;
173 }
174
175 kvf->file = get_file(filp);
176 list_add_tail(&kvf->node, &kv->file_list);
177
178 kvm_arch_start_assignment(dev->kvm);
179 kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
180 kvm_vfio_update_coherency(dev);
181
182out_unlock:
183 mutex_unlock(&kv->lock);
184out_fput:
185 fput(filp);
186 return ret;
187}
188
189static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
190{
191 struct kvm_vfio *kv = dev->private;
192 struct kvm_vfio_file *kvf;
193 CLASS(fd, f)(fd);
194 int ret;
195
196 if (fd_empty(f))
197 return -EBADF;
198
199 ret = -ENOENT;
200
201 mutex_lock(&kv->lock);
202
203 list_for_each_entry(kvf, &kv->file_list, node) {
204 if (kvf->file != fd_file(f))
205 continue;
206
207 list_del(&kvf->node);
208 kvm_arch_end_assignment(dev->kvm);
209#ifdef CONFIG_SPAPR_TCE_IOMMU
210 kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
211#endif
212 kvm_vfio_file_set_kvm(kvf->file, NULL);
213 fput(kvf->file);
214 kfree(kvf);
215 ret = 0;
216 break;
217 }
218
219 kvm_vfio_update_coherency(dev);
220
221 mutex_unlock(&kv->lock);
222 return ret;
223}
224
225#ifdef CONFIG_SPAPR_TCE_IOMMU
226static int kvm_vfio_file_set_spapr_tce(struct kvm_device *dev,
227 void __user *arg)
228{
229 struct kvm_vfio_spapr_tce param;
230 struct kvm_vfio *kv = dev->private;
231 struct kvm_vfio_file *kvf;
232 int ret;
233
234 if (copy_from_user(¶m, arg, sizeof(struct kvm_vfio_spapr_tce)))
235 return -EFAULT;
236
237 CLASS(fd, f)(param.groupfd);
238 if (fd_empty(f))
239 return -EBADF;
240
241 ret = -ENOENT;
242
243 mutex_lock(&kv->lock);
244
245 list_for_each_entry(kvf, &kv->file_list, node) {
246 if (kvf->file != fd_file(f))
247 continue;
248
249 if (!kvf->iommu_group) {
250 kvf->iommu_group = kvm_vfio_file_iommu_group(kvf->file);
251 if (WARN_ON_ONCE(!kvf->iommu_group)) {
252 ret = -EIO;
253 goto err_fdput;
254 }
255 }
256
257 ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
258 kvf->iommu_group);
259 break;
260 }
261
262err_fdput:
263 mutex_unlock(&kv->lock);
264 return ret;
265}
266#endif
267
268static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
269 void __user *arg)
270{
271 int32_t __user *argp = arg;
272 int32_t fd;
273
274 switch (attr) {
275 case KVM_DEV_VFIO_FILE_ADD:
276 if (get_user(fd, argp))
277 return -EFAULT;
278 return kvm_vfio_file_add(dev, fd);
279
280 case KVM_DEV_VFIO_FILE_DEL:
281 if (get_user(fd, argp))
282 return -EFAULT;
283 return kvm_vfio_file_del(dev, fd);
284
285#ifdef CONFIG_SPAPR_TCE_IOMMU
286 case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
287 return kvm_vfio_file_set_spapr_tce(dev, arg);
288#endif
289 }
290
291 return -ENXIO;
292}
293
294static int kvm_vfio_set_attr(struct kvm_device *dev,
295 struct kvm_device_attr *attr)
296{
297 switch (attr->group) {
298 case KVM_DEV_VFIO_FILE:
299 return kvm_vfio_set_file(dev, attr->attr,
300 u64_to_user_ptr(attr->addr));
301 }
302
303 return -ENXIO;
304}
305
306static int kvm_vfio_has_attr(struct kvm_device *dev,
307 struct kvm_device_attr *attr)
308{
309 switch (attr->group) {
310 case KVM_DEV_VFIO_FILE:
311 switch (attr->attr) {
312 case KVM_DEV_VFIO_FILE_ADD:
313 case KVM_DEV_VFIO_FILE_DEL:
314#ifdef CONFIG_SPAPR_TCE_IOMMU
315 case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
316#endif
317 return 0;
318 }
319
320 break;
321 }
322
323 return -ENXIO;
324}
325
326static void kvm_vfio_release(struct kvm_device *dev)
327{
328 struct kvm_vfio *kv = dev->private;
329 struct kvm_vfio_file *kvf, *tmp;
330
331 list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) {
332#ifdef CONFIG_SPAPR_TCE_IOMMU
333 kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
334#endif
335 kvm_vfio_file_set_kvm(kvf->file, NULL);
336 fput(kvf->file);
337 list_del(&kvf->node);
338 kfree(kvf);
339 kvm_arch_end_assignment(dev->kvm);
340 }
341
342 kvm_vfio_update_coherency(dev);
343
344 kfree(kv);
345 kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */
346}
347
348static int kvm_vfio_create(struct kvm_device *dev, u32 type);
349
350static const struct kvm_device_ops kvm_vfio_ops = {
351 .name = "kvm-vfio",
352 .create = kvm_vfio_create,
353 .release = kvm_vfio_release,
354 .set_attr = kvm_vfio_set_attr,
355 .has_attr = kvm_vfio_has_attr,
356};
357
358static int kvm_vfio_create(struct kvm_device *dev, u32 type)
359{
360 struct kvm_device *tmp;
361 struct kvm_vfio *kv;
362
363 lockdep_assert_held(&dev->kvm->lock);
364
365 /* Only one VFIO "device" per VM */
366 list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
367 if (tmp->ops == &kvm_vfio_ops)
368 return -EBUSY;
369
370 kv = kzalloc(sizeof(*kv), GFP_KERNEL_ACCOUNT);
371 if (!kv)
372 return -ENOMEM;
373
374 INIT_LIST_HEAD(&kv->file_list);
375 mutex_init(&kv->lock);
376
377 dev->private = kv;
378
379 return 0;
380}
381
382int kvm_vfio_ops_init(void)
383{
384 return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
385}
386
387void kvm_vfio_ops_exit(void)
388{
389 kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
390}