KVM
Classes | Functions | Variables
eventfd.c File Reference
#include <linux/kvm_host.h>
#include <linux/kvm.h>
#include <linux/kvm_irqfd.h>
#include <linux/workqueue.h>
#include <linux/syscalls.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/file.h>
#include <linux/list.h>
#include <linux/eventfd.h>
#include <linux/kernel.h>
#include <linux/srcu.h>
#include <linux/slab.h>
#include <linux/seqlock.h>
#include <linux/irqbypass.h>
#include <trace/events/kvm.h>
#include <kvm/iodev.h>
Include dependency graph for eventfd.c:

Go to the source code of this file.

Classes

struct  _ioeventfd
 

Functions

static struct _ioeventfdto_ioeventfd (struct kvm_io_device *dev)
 
static void ioeventfd_release (struct _ioeventfd *p)
 
static bool ioeventfd_in_range (struct _ioeventfd *p, gpa_t addr, int len, const void *val)
 
static int ioeventfd_write (struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t addr, int len, const void *val)
 
static void ioeventfd_destructor (struct kvm_io_device *this)
 
static bool ioeventfd_check_collision (struct kvm *kvm, struct _ioeventfd *p)
 
static enum kvm_bus ioeventfd_bus_from_flags (__u32 flags)
 
static int kvm_assign_ioeventfd_idx (struct kvm *kvm, enum kvm_bus bus_idx, struct kvm_ioeventfd *args)
 
static int kvm_deassign_ioeventfd_idx (struct kvm *kvm, enum kvm_bus bus_idx, struct kvm_ioeventfd *args)
 
static int kvm_deassign_ioeventfd (struct kvm *kvm, struct kvm_ioeventfd *args)
 
static int kvm_assign_ioeventfd (struct kvm *kvm, struct kvm_ioeventfd *args)
 
int kvm_ioeventfd (struct kvm *kvm, struct kvm_ioeventfd *args)
 
void kvm_eventfd_init (struct kvm *kvm)
 

Variables

static const struct kvm_io_device_ops ioeventfd_ops
 

Function Documentation

◆ ioeventfd_bus_from_flags()

static enum kvm_bus ioeventfd_bus_from_flags ( __u32  flags)
static

Definition at line 798 of file eventfd.c.

815 {
816  if (flags & KVM_IOEVENTFD_FLAG_PIO)
817  return KVM_PIO_BUS;
818  if (flags & KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY)
819  return KVM_VIRTIO_CCW_NOTIFY_BUS;
820  return KVM_MMIO_BUS;
821 }
Here is the caller graph for this function:

◆ ioeventfd_check_collision()

static bool ioeventfd_check_collision ( struct kvm *  kvm,
struct _ioeventfd p 
)
static

Definition at line 798 of file eventfd.c.

799 {
800  struct _ioeventfd *_p;
801 
802  list_for_each_entry(_p, &kvm->ioeventfds, list)
803  if (_p->bus_idx == p->bus_idx &&
804  _p->addr == p->addr &&
805  (!_p->length || !p->length ||
806  (_p->length == p->length &&
807  (_p->wildcard || p->wildcard ||
808  _p->datamatch == p->datamatch))))
809  return true;
810 
811  return false;
812 }
bool wildcard
Definition: eventfd.c:703
u64 addr
Definition: eventfd.c:697
int length
Definition: eventfd.c:698
u8 bus_idx
Definition: eventfd.c:702
u64 datamatch
Definition: eventfd.c:700
struct list_head list
Definition: eventfd.c:696
Here is the caller graph for this function:

◆ ioeventfd_destructor()

static void ioeventfd_destructor ( struct kvm_io_device this)
static

Definition at line 784 of file eventfd.c.

785 {
786  struct _ioeventfd *p = to_ioeventfd(this);
787 
789 }
static struct _ioeventfd * to_ioeventfd(struct kvm_io_device *dev)
Definition: eventfd.c:707
static void ioeventfd_release(struct _ioeventfd *p)
Definition: eventfd.c:713
Here is the call graph for this function:

◆ ioeventfd_in_range()

static bool ioeventfd_in_range ( struct _ioeventfd p,
gpa_t  addr,
int  len,
const void *  val 
)
static

Definition at line 721 of file eventfd.c.

722 {
723  u64 _val;
724 
725  if (addr != p->addr)
726  /* address must be precise for a hit */
727  return false;
728 
729  if (!p->length)
730  /* length = 0 means only look at the address, so always a hit */
731  return true;
732 
733  if (len != p->length)
734  /* address-range must be precise for a hit */
735  return false;
736 
737  if (p->wildcard)
738  /* all else equal, wildcard is always a hit */
739  return true;
740 
741  /* otherwise, we have to actually compare the data */
742 
743  BUG_ON(!IS_ALIGNED((unsigned long)val, len));
744 
745  switch (len) {
746  case 1:
747  _val = *(u8 *)val;
748  break;
749  case 2:
750  _val = *(u16 *)val;
751  break;
752  case 4:
753  _val = *(u32 *)val;
754  break;
755  case 8:
756  _val = *(u64 *)val;
757  break;
758  default:
759  return false;
760  }
761 
762  return _val == p->datamatch;
763 }
Here is the caller graph for this function:

◆ ioeventfd_release()

static void ioeventfd_release ( struct _ioeventfd p)
static

Definition at line 713 of file eventfd.c.

714 {
715  eventfd_ctx_put(p->eventfd);
716  list_del(&p->list);
717  kfree(p);
718 }
struct eventfd_ctx * eventfd
Definition: eventfd.c:699
Here is the caller graph for this function:

◆ ioeventfd_write()

static int ioeventfd_write ( struct kvm_vcpu *  vcpu,
struct kvm_io_device this,
gpa_t  addr,
int  len,
const void *  val 
)
static

Definition at line 767 of file eventfd.c.

769 {
770  struct _ioeventfd *p = to_ioeventfd(this);
771 
772  if (!ioeventfd_in_range(p, addr, len, val))
773  return -EOPNOTSUPP;
774 
775  eventfd_signal(p->eventfd);
776  return 0;
777 }
static bool ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
Definition: eventfd.c:721
Here is the call graph for this function:

◆ kvm_assign_ioeventfd()

static int kvm_assign_ioeventfd ( struct kvm *  kvm,
struct kvm_ioeventfd args 
)
static

Definition at line 942 of file eventfd.c.

943 {
944  enum kvm_bus bus_idx;
945  int ret;
946 
947  bus_idx = ioeventfd_bus_from_flags(args->flags);
948  /* must be natural-word sized, or 0 to ignore length */
949  switch (args->len) {
950  case 0:
951  case 1:
952  case 2:
953  case 4:
954  case 8:
955  break;
956  default:
957  return -EINVAL;
958  }
959 
960  /* check for range overflow */
961  if (args->addr + args->len < args->addr)
962  return -EINVAL;
963 
964  /* check for extra flags that we don't understand */
965  if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
966  return -EINVAL;
967 
968  /* ioeventfd with no length can't be combined with DATAMATCH */
969  if (!args->len && (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH))
970  return -EINVAL;
971 
972  ret = kvm_assign_ioeventfd_idx(kvm, bus_idx, args);
973  if (ret)
974  goto fail;
975 
976  /* When length is ignored, MMIO is also put on a separate bus, for
977  * faster lookups.
978  */
979  if (!args->len && bus_idx == KVM_MMIO_BUS) {
980  ret = kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args);
981  if (ret < 0)
982  goto fast_fail;
983  }
984 
985  return 0;
986 
987 fast_fail:
988  kvm_deassign_ioeventfd_idx(kvm, bus_idx, args);
989 fail:
990  return ret;
991 }
static enum kvm_bus ioeventfd_bus_from_flags(__u32 flags)
Definition: eventfd.c:814
static int kvm_assign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx, struct kvm_ioeventfd *args)
Definition: eventfd.c:823
static int kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx, struct kvm_ioeventfd *args)
Definition: eventfd.c:887
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_assign_ioeventfd_idx()

static int kvm_assign_ioeventfd_idx ( struct kvm *  kvm,
enum kvm_bus  bus_idx,
struct kvm_ioeventfd args 
)
static

Definition at line 823 of file eventfd.c.

826 {
827 
828  struct eventfd_ctx *eventfd;
829  struct _ioeventfd *p;
830  int ret;
831 
832  eventfd = eventfd_ctx_fdget(args->fd);
833  if (IS_ERR(eventfd))
834  return PTR_ERR(eventfd);
835 
836  p = kzalloc(sizeof(*p), GFP_KERNEL_ACCOUNT);
837  if (!p) {
838  ret = -ENOMEM;
839  goto fail;
840  }
841 
842  INIT_LIST_HEAD(&p->list);
843  p->addr = args->addr;
844  p->bus_idx = bus_idx;
845  p->length = args->len;
846  p->eventfd = eventfd;
847 
848  /* The datamatch feature is optional, otherwise this is a wildcard */
849  if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
850  p->datamatch = args->datamatch;
851  else
852  p->wildcard = true;
853 
854  mutex_lock(&kvm->slots_lock);
855 
856  /* Verify that there isn't a match already */
857  if (ioeventfd_check_collision(kvm, p)) {
858  ret = -EEXIST;
859  goto unlock_fail;
860  }
861 
863 
864  ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
865  &p->dev);
866  if (ret < 0)
867  goto unlock_fail;
868 
869  kvm_get_bus(kvm, bus_idx)->ioeventfd_count++;
870  list_add_tail(&p->list, &kvm->ioeventfds);
871 
872  mutex_unlock(&kvm->slots_lock);
873 
874  return 0;
875 
876 unlock_fail:
877  mutex_unlock(&kvm->slots_lock);
878  kfree(p);
879 
880 fail:
881  eventfd_ctx_put(eventfd);
882 
883  return ret;
884 }
static bool ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)
Definition: eventfd.c:798
static const struct kvm_io_device_ops ioeventfd_ops
Definition: eventfd.c:791
static void kvm_iodevice_init(struct kvm_io_device *dev, const struct kvm_io_device_ops *ops)
Definition: iodev.h:36
int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len, struct kvm_io_device *dev)
Definition: kvm_main.c:5897
struct kvm_io_device dev
Definition: eventfd.c:701
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_deassign_ioeventfd()

static int kvm_deassign_ioeventfd ( struct kvm *  kvm,
struct kvm_ioeventfd args 
)
static

Definition at line 930 of file eventfd.c.

931 {
932  enum kvm_bus bus_idx = ioeventfd_bus_from_flags(args->flags);
933  int ret = kvm_deassign_ioeventfd_idx(kvm, bus_idx, args);
934 
935  if (!args->len && bus_idx == KVM_MMIO_BUS)
936  kvm_deassign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args);
937 
938  return ret;
939 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_deassign_ioeventfd_idx()

static int kvm_deassign_ioeventfd_idx ( struct kvm *  kvm,
enum kvm_bus  bus_idx,
struct kvm_ioeventfd args 
)
static

Definition at line 887 of file eventfd.c.

889 {
890  struct _ioeventfd *p;
891  struct eventfd_ctx *eventfd;
892  struct kvm_io_bus *bus;
893  int ret = -ENOENT;
894  bool wildcard;
895 
896  eventfd = eventfd_ctx_fdget(args->fd);
897  if (IS_ERR(eventfd))
898  return PTR_ERR(eventfd);
899 
900  wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
901 
902  mutex_lock(&kvm->slots_lock);
903 
904  list_for_each_entry(p, &kvm->ioeventfds, list) {
905  if (p->bus_idx != bus_idx ||
906  p->eventfd != eventfd ||
907  p->addr != args->addr ||
908  p->length != args->len ||
909  p->wildcard != wildcard)
910  continue;
911 
912  if (!p->wildcard && p->datamatch != args->datamatch)
913  continue;
914 
915  kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
916  bus = kvm_get_bus(kvm, bus_idx);
917  if (bus)
918  bus->ioeventfd_count--;
919  ret = 0;
920  break;
921  }
922 
923  mutex_unlock(&kvm->slots_lock);
924 
925  eventfd_ctx_put(eventfd);
926 
927  return ret;
928 }
int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, struct kvm_io_device *dev)
Definition: kvm_main.c:5941
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_eventfd_init()

void kvm_eventfd_init ( struct kvm *  kvm)

Definition at line 1003 of file eventfd.c.

1004 {
1005 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1006  spin_lock_init(&kvm->irqfds.lock);
1007  INIT_LIST_HEAD(&kvm->irqfds.items);
1008  INIT_LIST_HEAD(&kvm->irqfds.resampler_list);
1009  mutex_init(&kvm->irqfds.resampler_lock);
1010 #endif
1011  INIT_LIST_HEAD(&kvm->ioeventfds);
1012 }
Here is the caller graph for this function:

◆ kvm_ioeventfd()

int kvm_ioeventfd ( struct kvm *  kvm,
struct kvm_ioeventfd *  args 
)

Definition at line 994 of file eventfd.c.

995 {
996  if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN)
997  return kvm_deassign_ioeventfd(kvm, args);
998 
999  return kvm_assign_ioeventfd(kvm, args);
1000 }
static int kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
Definition: eventfd.c:942
static int kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
Definition: eventfd.c:930
Here is the call graph for this function:

◆ to_ioeventfd()

static struct _ioeventfd* to_ioeventfd ( struct kvm_io_device dev)
inlinestatic

Definition at line 707 of file eventfd.c.

708 {
709  return container_of(dev, struct _ioeventfd, dev);
710 }
Here is the caller graph for this function:

Variable Documentation

◆ ioeventfd_ops

const struct kvm_io_device_ops ioeventfd_ops
static
Initial value:
= {
.write = ioeventfd_write,
.destructor = ioeventfd_destructor,
}
static void ioeventfd_destructor(struct kvm_io_device *this)
Definition: eventfd.c:784
static int ioeventfd_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t addr, int len, const void *val)
Definition: eventfd.c:767

Definition at line 784 of file eventfd.c.