KVM
Classes | Functions | Variables
vfio.c File Reference
#include <linux/errno.h>
#include <linux/file.h>
#include <linux/kvm_host.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/vfio.h>
#include "vfio.h"
Include dependency graph for vfio.c:

Go to the source code of this file.

Classes

struct  kvm_vfio_file
 
struct  kvm_vfio
 

Functions

static void kvm_vfio_file_set_kvm (struct file *file, struct kvm *kvm)
 
static bool kvm_vfio_file_enforced_coherent (struct file *file)
 
static bool kvm_vfio_file_is_valid (struct file *file)
 
static void kvm_vfio_update_coherency (struct kvm_device *dev)
 
static int kvm_vfio_file_add (struct kvm_device *dev, unsigned int fd)
 
static int kvm_vfio_file_del (struct kvm_device *dev, unsigned int fd)
 
static int kvm_vfio_set_file (struct kvm_device *dev, long attr, void __user *arg)
 
static int kvm_vfio_set_attr (struct kvm_device *dev, struct kvm_device_attr *attr)
 
static int kvm_vfio_has_attr (struct kvm_device *dev, struct kvm_device_attr *attr)
 
static void kvm_vfio_release (struct kvm_device *dev)
 
static int kvm_vfio_create (struct kvm_device *dev, u32 type)
 
int kvm_vfio_ops_init (void)
 
void kvm_vfio_ops_exit (void)
 

Variables

static struct kvm_device_ops kvm_vfio_ops
 

Function Documentation

◆ kvm_vfio_create()

static int kvm_vfio_create ( struct kvm_device *  dev,
u32  type 
)
static

Definition at line 364 of file vfio.c.

365 {
366  struct kvm_device *tmp;
367  struct kvm_vfio *kv;
368 
369  /* Only one VFIO "device" per VM */
370  list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
371  if (tmp->ops == &kvm_vfio_ops)
372  return -EBUSY;
373 
374  kv = kzalloc(sizeof(*kv), GFP_KERNEL_ACCOUNT);
375  if (!kv)
376  return -ENOMEM;
377 
378  INIT_LIST_HEAD(&kv->file_list);
379  mutex_init(&kv->lock);
380 
381  dev->private = kv;
382 
383  return 0;
384 }
Definition: vfio.c:32
struct mutex lock
Definition: vfio.c:34
struct list_head file_list
Definition: vfio.c:33
static struct kvm_device_ops kvm_vfio_ops
Definition: vfio.c:356

◆ kvm_vfio_file_add()

static int kvm_vfio_file_add ( struct kvm_device *  dev,
unsigned int  fd 
)
static

Definition at line 143 of file vfio.c.

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);
181 
182 out_unlock:
183  mutex_unlock(&kv->lock);
184 out_fput:
185  fput(filp);
186  return ret;
187 }
struct list_head node
Definition: vfio.c:25
struct file * file
Definition: vfio.c:26
static void kvm_vfio_update_coherency(struct kvm_device *dev)
Definition: vfio.c:120
static bool kvm_vfio_file_is_valid(struct file *file)
Definition: vfio.c:67
static void kvm_vfio_file_set_kvm(struct file *file, struct kvm *kvm)
Definition: vfio.c:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_vfio_file_del()

static int kvm_vfio_file_del ( struct kvm_device *  dev,
unsigned int  fd 
)
static

Definition at line 189 of file vfio.c.

190 {
191  struct kvm_vfio *kv = dev->private;
192  struct kvm_vfio_file *kvf;
193  struct fd f;
194  int ret;
195 
196  f = fdget(fd);
197  if (!f.file)
198  return -EBADF;
199 
200  ret = -ENOENT;
201 
202  mutex_lock(&kv->lock);
203 
204  list_for_each_entry(kvf, &kv->file_list, node) {
205  if (kvf->file != f.file)
206  continue;
207 
208  list_del(&kvf->node);
209  kvm_arch_end_assignment(dev->kvm);
210 #ifdef CONFIG_SPAPR_TCE_IOMMU
211  kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
212 #endif
213  kvm_vfio_file_set_kvm(kvf->file, NULL);
214  fput(kvf->file);
215  kfree(kvf);
216  ret = 0;
217  break;
218  }
219 
221 
222  mutex_unlock(&kv->lock);
223 
224  fdput(f);
225 
226  return ret;
227 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_vfio_file_enforced_coherent()

static bool kvm_vfio_file_enforced_coherent ( struct file *  file)
static

Definition at line 51 of file vfio.c.

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 }
Here is the caller graph for this function:

◆ kvm_vfio_file_is_valid()

static bool kvm_vfio_file_is_valid ( struct file *  file)
static

Definition at line 67 of file vfio.c.

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 }
Here is the caller graph for this function:

◆ kvm_vfio_file_set_kvm()

static void kvm_vfio_file_set_kvm ( struct file *  file,
struct kvm *  kvm 
)
static

Definition at line 38 of file vfio.c.

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 }
Here is the caller graph for this function:

◆ kvm_vfio_has_attr()

static int kvm_vfio_has_attr ( struct kvm_device *  dev,
struct kvm_device_attr *  attr 
)
static

Definition at line 312 of file vfio.c.

314 {
315  switch (attr->group) {
316  case KVM_DEV_VFIO_FILE:
317  switch (attr->attr) {
318  case KVM_DEV_VFIO_FILE_ADD:
319  case KVM_DEV_VFIO_FILE_DEL:
320 #ifdef CONFIG_SPAPR_TCE_IOMMU
321  case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
322 #endif
323  return 0;
324  }
325 
326  break;
327  }
328 
329  return -ENXIO;
330 }

◆ kvm_vfio_ops_exit()

void kvm_vfio_ops_exit ( void  )

Definition at line 391 of file vfio.c.

392 {
393  kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
394 }
void kvm_unregister_device_ops(u32 type)
Definition: kvm_main.c:4754
Here is the caller graph for this function:

◆ kvm_vfio_ops_init()

int kvm_vfio_ops_init ( void  )

Definition at line 386 of file vfio.c.

387 {
388  return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
389 }
int kvm_register_device_ops(const struct kvm_device_ops *ops, u32 type)
Definition: kvm_main.c:4742
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_vfio_release()

static void kvm_vfio_release ( struct kvm_device *  dev)
static

Definition at line 332 of file vfio.c.

333 {
334  struct kvm_vfio *kv = dev->private;
335  struct kvm_vfio_file *kvf, *tmp;
336 
337  list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) {
338 #ifdef CONFIG_SPAPR_TCE_IOMMU
339  kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
340 #endif
341  kvm_vfio_file_set_kvm(kvf->file, NULL);
342  fput(kvf->file);
343  list_del(&kvf->node);
344  kfree(kvf);
345  kvm_arch_end_assignment(dev->kvm);
346  }
347 
349 
350  kfree(kv);
351  kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */
352 }
Here is the call graph for this function:

◆ kvm_vfio_set_attr()

static int kvm_vfio_set_attr ( struct kvm_device *  dev,
struct kvm_device_attr *  attr 
)
static

Definition at line 300 of file vfio.c.

302 {
303  switch (attr->group) {
304  case KVM_DEV_VFIO_FILE:
305  return kvm_vfio_set_file(dev, attr->attr,
306  u64_to_user_ptr(attr->addr));
307  }
308 
309  return -ENXIO;
310 }
static int kvm_vfio_set_file(struct kvm_device *dev, long attr, void __user *arg)
Definition: vfio.c:274
Here is the call graph for this function:

◆ kvm_vfio_set_file()

static int kvm_vfio_set_file ( struct kvm_device *  dev,
long  attr,
void __user *  arg 
)
static

Definition at line 274 of file vfio.c.

276 {
277  int32_t __user *argp = arg;
278  int32_t fd;
279 
280  switch (attr) {
281  case KVM_DEV_VFIO_FILE_ADD:
282  if (get_user(fd, argp))
283  return -EFAULT;
284  return kvm_vfio_file_add(dev, fd);
285 
286  case KVM_DEV_VFIO_FILE_DEL:
287  if (get_user(fd, argp))
288  return -EFAULT;
289  return kvm_vfio_file_del(dev, fd);
290 
291 #ifdef CONFIG_SPAPR_TCE_IOMMU
292  case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
293  return kvm_vfio_file_set_spapr_tce(dev, arg);
294 #endif
295  }
296 
297  return -ENXIO;
298 }
static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
Definition: vfio.c:189
static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
Definition: vfio.c:143
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_vfio_update_coherency()

static void kvm_vfio_update_coherency ( struct kvm_device *  dev)
static

Definition at line 120 of file vfio.c.

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) {
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 }
bool noncoherent
Definition: vfio.c:35
static bool kvm_vfio_file_enforced_coherent(struct file *file)
Definition: vfio.c:51
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ kvm_vfio_ops

struct kvm_device_ops kvm_vfio_ops
static
Initial value:
= {
.name = "kvm-vfio",
.create = kvm_vfio_create,
.release = kvm_vfio_release,
.set_attr = kvm_vfio_set_attr,
.has_attr = kvm_vfio_has_attr,
}
static void kvm_vfio_release(struct kvm_device *dev)
Definition: vfio.c:332
static int kvm_vfio_create(struct kvm_device *dev, u32 type)
Definition: vfio.c:364
static int kvm_vfio_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Definition: vfio.c:312
static int kvm_vfio_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
Definition: vfio.c:300

Definition at line 354 of file vfio.c.