xref: /linux/virt/kvm/vfio.c (revision 156615264fd3430e7f90b36f46076b2f6ec4a24a)
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 
24 struct 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 
32 struct kvm_vfio {
33 	struct list_head file_list;
34 	struct mutex lock;
35 	bool noncoherent;
36 };
37 
38 static 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 
51 static 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 
67 static 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
84 static 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 
100 static 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  */
120 static 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 
143 static 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 __free(fput) = NULL;
148 
149 	filp = fget(fd);
150 	if (!filp)
151 		return -EBADF;
152 
153 	/* Ensure the FD is a vfio FD. */
154 	if (!kvm_vfio_file_is_valid(filp))
155 		return -EINVAL;
156 
157 	guard(mutex)(&kv->lock);
158 
159 	list_for_each_entry(kvf, &kv->file_list, node) {
160 		if (kvf->file == filp)
161 			return -EEXIST;
162 	}
163 
164 	kvf = kzalloc_obj(*kvf, GFP_KERNEL_ACCOUNT);
165 	if (!kvf)
166 		return -ENOMEM;
167 
168 	kvf->file = get_file(filp);
169 	list_add_tail(&kvf->node, &kv->file_list);
170 
171 	kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
172 	kvm_vfio_update_coherency(dev);
173 
174 	return 0;
175 }
176 
177 static void kvm_vfio_file_free(struct kvm_device *dev, struct kvm_vfio_file *kvf)
178 {
179 #ifdef CONFIG_SPAPR_TCE_IOMMU
180 	kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
181 #endif
182 	kvm_vfio_file_set_kvm(kvf->file, NULL);
183 	fput(kvf->file);
184 	list_del(&kvf->node);
185 	kfree(kvf);
186 }
187 
188 static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
189 {
190 	struct kvm_vfio *kv = dev->private;
191 	struct kvm_vfio_file *kvf;
192 	CLASS(fd, f)(fd);
193 
194 	if (fd_empty(f))
195 		return -EBADF;
196 
197 	guard(mutex)(&kv->lock);
198 
199 	list_for_each_entry(kvf, &kv->file_list, node) {
200 		if (kvf->file == fd_file(f)) {
201 			kvm_vfio_file_free(dev, kvf);
202 			kvm_vfio_update_coherency(dev);
203 			return 0;
204 		}
205 	}
206 
207 	return -ENOENT;
208 }
209 
210 #ifdef CONFIG_SPAPR_TCE_IOMMU
211 static int kvm_vfio_file_set_spapr_tce(struct kvm_device *dev,
212 				       void __user *arg)
213 {
214 	struct kvm_vfio_spapr_tce param;
215 	struct kvm_vfio *kv = dev->private;
216 	struct kvm_vfio_file *kvf;
217 
218 	if (copy_from_user(&param, arg, sizeof(struct kvm_vfio_spapr_tce)))
219 		return -EFAULT;
220 
221 	CLASS(fd, f)(param.groupfd);
222 	if (fd_empty(f))
223 		return -EBADF;
224 
225 	guard(mutex)(&kv->lock);
226 
227 	list_for_each_entry(kvf, &kv->file_list, node) {
228 		if (kvf->file != fd_file(f))
229 			continue;
230 
231 		if (!kvf->iommu_group) {
232 			kvf->iommu_group = kvm_vfio_file_iommu_group(kvf->file);
233 			if (WARN_ON_ONCE(!kvf->iommu_group))
234 				return -EIO;
235 		}
236 
237 		return kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
238 							kvf->iommu_group);
239 	}
240 
241 	return -ENOENT;
242 }
243 #endif
244 
245 static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
246 			     void __user *arg)
247 {
248 	int32_t __user *argp = arg;
249 	int32_t fd;
250 
251 	switch (attr) {
252 	case KVM_DEV_VFIO_FILE_ADD:
253 		if (get_user(fd, argp))
254 			return -EFAULT;
255 		return kvm_vfio_file_add(dev, fd);
256 
257 	case KVM_DEV_VFIO_FILE_DEL:
258 		if (get_user(fd, argp))
259 			return -EFAULT;
260 		return kvm_vfio_file_del(dev, fd);
261 
262 #ifdef CONFIG_SPAPR_TCE_IOMMU
263 	case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
264 		return kvm_vfio_file_set_spapr_tce(dev, arg);
265 #endif
266 	}
267 
268 	return -ENXIO;
269 }
270 
271 static int kvm_vfio_set_attr(struct kvm_device *dev,
272 			     struct kvm_device_attr *attr)
273 {
274 	switch (attr->group) {
275 	case KVM_DEV_VFIO_FILE:
276 		return kvm_vfio_set_file(dev, attr->attr,
277 					 u64_to_user_ptr(attr->addr));
278 	}
279 
280 	return -ENXIO;
281 }
282 
283 static int kvm_vfio_has_attr(struct kvm_device *dev,
284 			     struct kvm_device_attr *attr)
285 {
286 	switch (attr->group) {
287 	case KVM_DEV_VFIO_FILE:
288 		switch (attr->attr) {
289 		case KVM_DEV_VFIO_FILE_ADD:
290 		case KVM_DEV_VFIO_FILE_DEL:
291 #ifdef CONFIG_SPAPR_TCE_IOMMU
292 		case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
293 #endif
294 			return 0;
295 		}
296 
297 		break;
298 	}
299 
300 	return -ENXIO;
301 }
302 
303 static void kvm_vfio_release(struct kvm_device *dev)
304 {
305 	struct kvm_vfio *kv = dev->private;
306 	struct kvm_vfio_file *kvf, *tmp;
307 
308 	list_for_each_entry_safe(kvf, tmp, &kv->file_list, node)
309 		kvm_vfio_file_free(dev, kvf);
310 
311 	kvm_vfio_update_coherency(dev);
312 
313 	kfree(kv);
314 	kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */
315 }
316 
317 static int kvm_vfio_create(struct kvm_device *dev, u32 type);
318 
319 static const struct kvm_device_ops kvm_vfio_ops = {
320 	.name = "kvm-vfio",
321 	.create = kvm_vfio_create,
322 	.release = kvm_vfio_release,
323 	.set_attr = kvm_vfio_set_attr,
324 	.has_attr = kvm_vfio_has_attr,
325 };
326 
327 static int kvm_vfio_create(struct kvm_device *dev, u32 type)
328 {
329 	struct kvm_device *tmp;
330 	struct kvm_vfio *kv;
331 
332 	lockdep_assert_held(&dev->kvm->lock);
333 
334 	/* Only one VFIO "device" per VM */
335 	list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
336 		if (tmp->ops == &kvm_vfio_ops)
337 			return -EBUSY;
338 
339 	kv = kzalloc_obj(*kv, GFP_KERNEL_ACCOUNT);
340 	if (!kv)
341 		return -ENOMEM;
342 
343 	INIT_LIST_HEAD(&kv->file_list);
344 	mutex_init(&kv->lock);
345 
346 	dev->private = kv;
347 
348 	return 0;
349 }
350 
351 int kvm_vfio_ops_init(void)
352 {
353 	return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
354 }
355 
356 void kvm_vfio_ops_exit(void)
357 {
358 	kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
359 }
360