Skip to contentSkip to conversation

ioeventfd: Introduce KVM_IOEVENTFD_FLAG_PIPE

PATCH In review Sasha Levin 4 participants 20 messages Jul 3, 2011 – Jul 6, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ioeventfd: Introduce KVM_IOEVENTFD_FLAG_PIPE

The new flag allows passing a write side of a pipe instead of an eventfd to be notified of writes to the specified memory region.

Instead of signaling an event, the value written to the memory region is written to the pipe.

Using a pipe instead of an eventfd is usefull when any value can be written to the memory region but we're interested in recieving the actual value instead of just a notification.

A simple example for practical use is the serial port. we are not interested in an exit every time a char is written to the port, but we do need to know what was written so we could handle it on the guest.

Cc Avi Kivity <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Cc Pekka Enberg <[email protected]>
---
 include/linux/kvm.h |    2 +
 virt/kvm/eventfd.c  |   65 +++++++++++++++++++++++++++++++++++---------------
 2 files changed, 47 insertions(+), 20 deletions(-)
include/linux/kvm.h +2 -0
index 55ef181..548f23a 100644
@@ -387,12 +387,14 @@ enum {
 	kvm_ioeventfd_flag_nr_datamatch,
 	kvm_ioeventfd_flag_nr_pio,
 	kvm_ioeventfd_flag_nr_deassign,
+	kvm_ioeventfd_flag_nr_pipe,
 	kvm_ioeventfd_flag_nr_max,
 };
 
 #define KVM_IOEVENTFD_FLAG_DATAMATCH (1 << kvm_ioeventfd_flag_nr_datamatch)
 #define KVM_IOEVENTFD_FLAG_PIO       (1 << kvm_ioeventfd_flag_nr_pio)
 #define KVM_IOEVENTFD_FLAG_DEASSIGN  (1 << kvm_ioeventfd_flag_nr_deassign)
+#define KVM_IOEVENTFD_FLAG_PIPE      (1 << kvm_ioeventfd_flag_nr_pipe)
 
 #define KVM_IOEVENTFD_VALID_FLAG_MASK  ((1 << kvm_ioeventfd_flag_nr_max) - 1)
 
virt/kvm/eventfd.c +45 -20

139 lines not on this page · raw message

Signature · 1 line
1.7.6
#1 lore raw
Quoted 10 lines · Sasha Levin

The new flag allows passing a write side of a pipe instead of an eventfd to be notified of writes to the specified memory region.

Instead of signaling an event, the value written to the memory region is written to the pipe.

Using a pipe instead of an eventfd is usefull when any value can be written to the memory region but we're interested in recieving the actual value instead of just a notification.

Quoted 7 lines

A simple example for practical use is the serial port. we are not interested in an exit every time a char is written to the port, but we do need to know what was written so we could handle it on the guest.

---
  include/linux/kvm.h |    2 +
  virt/kvm/eventfd.c  |   65 +++++++++++++++++++++++++++++++++++---------------

Documentation/virtua/kvm/api.txt +++++++++++++++++

virt/kvm/eventfd.c struct _ioeventfd { · line 429

Quoted 7 lines
virt/kvm/eventfd.c +1 -0
@@ -424,6 +425,7 @@ struct _ioeventfd {
  	struct list_head     list;
  	u64                  addr;
  	int                  length;
+	struct file         *pipe;
  	struct eventfd_ctx  *eventfd;

In a union with eventfd please.

Quoted 9 lines
virt/kvm/eventfd.c +5 -0
@@ -481,6 +487,21 @@ ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
  	return _val == p->datamatch ? true : false;
  }
 
+static ssize_t kernel_write(struct file *file, const char *buf, size_t count,
+			    loff_t pos)
+{
+	mm_segment_t old_fs;
+	ssize_t res;

virt/kvm/eventfd.c ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val) · line 504

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+
+	old_fs = get_fs();
+	set_fs(get_ds());
+	/* The cast to a user pointer is valid due to the set_fs() */
+	res = vfs_write(file, (const char __user *)buf, count,&pos);
+	set_fs(old_fs);
+
+	return res;
+}
+

Is there no generic helper for this? Should there be?

virt/kvm/eventfd.c ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len, · line 516

Quoted 10 lines
virt/kvm/eventfd.c +2 -1
  /* MMIO/PIO writes trigger an event if the addr/val match */
  static int
  ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
@@ -491,7 +512,11 @@ ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
  	if (!ioeventfd_in_range(p, addr, len, val))
  		return -EOPNOTSUPP;
 
-	eventfd_signal(p->eventfd, 1);
+	if (p->pipe)
+		kernel_write(p->pipe, val, len, 0);

You're writing potentially variable length data.

We need a protocol containing address, data, length, and supporting read accesses as well.

Is the write guaranteed atomic? We probably need serialization here.

Quoted 22 lines
virt/kvm/eventfd.c +8 -3
+	else
+		eventfd_signal(p->eventfd, 1);
+
  	return 0;
  }
 
@@ -555,9 +580,11 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  	if (args->flags&  ~KVM_IOEVENTFD_VALID_FLAG_MASK)
  		return -EINVAL;
 
-	eventfd = eventfd_ctx_fdget(args->fd);
-	if (IS_ERR(eventfd))
-		return PTR_ERR(eventfd);
+	if (!(args->flags&  KVM_IOEVENTFD_FLAG_PIPE)) {
+		eventfd = eventfd_ctx_fdget(args->fd);
+		if (IS_ERR(eventfd))
+			return PTR_ERR(eventfd);
+	}
 
  	p = kzalloc(sizeof(*p), GFP_KERNEL);
  	if (!p) {

virt/kvm/eventfd.c kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 602

Quoted 9 lines
virt/kvm/eventfd.c +5 -1
@@ -568,7 +595,11 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  	INIT_LIST_HEAD(&p->list);
  	p->addr    = args->addr;
  	p->length  = args->len;
-	p->eventfd = eventfd;
+
+	if (args->flags&  KVM_IOEVENTFD_FLAG_PIPE)
+		p->pipe = fget(args->fd);
+	else
+		p->eventfd = eventfd;

The split logic with the previous hunk isn't nice. Suggest moving the 'else' there, and assigning the whole union here.

virt/kvm/eventfd.c kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 658

Quoted 9 lines
virt/kvm/eventfd.c +1 -2
  	list_for_each_entry_safe(p, tmp,&kvm->ioeventfds, list) {
  		bool wildcard = !(args->flags&  KVM_IOEVENTFD_FLAG_DATAMATCH);
 
-		if (p->eventfd != eventfd  ||
-		    p->addr != args->addr  ||
+		if (p->addr != args->addr  ||
  		    p->length != args->len ||
  		    p->wildcard != wildcard)
  			continue;

Why?

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 15 lines · Avi Kivity
Quoted 13 lines · Sasha Levin

The new flag allows passing a write side of a pipe instead of an eventfd to be notified of writes to the specified memory region.

Instead of signaling an event, the value written to the memory region is written to the pipe.

Using a pipe instead of an eventfd is usefull when any value can be written to the memory region but we're interested in recieving the actual value instead of just a notification.

A simple example for practical use is the serial port. we are not interested in an exit every time a char is written to the port, but we do need to know what was written so we could handle it on the guest.

Quoted 5 lines
Quoted 3 lines
---
  include/linux/kvm.h |    2 +
  virt/kvm/eventfd.c  |   65 +++++++++++++++++++++++++++++++++++---------------

Documentation/virtua/kvm/api.txt +++++++++++++++++

I couldn't find the ioeventfd docs in there and forgot that it's not in the mainline yet, I'll rebase on kvm git tree :)

Quoted 23 lines

virt/kvm/eventfd.c struct _ioeventfd { · line 429

Quoted 7 lines
virt/kvm/eventfd.c +1 -0
@@ -424,6 +425,7 @@ struct _ioeventfd {
  	struct list_head     list;
  	u64                  addr;
  	int                  length;
+	struct file         *pipe;
  	struct eventfd_ctx  *eventfd;

In a union with eventfd please.

Quoted 13 lines
virt/kvm/eventfd.c +9 -0
@@ -481,6 +487,21 @@ ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
  	return _val == p->datamatch ? true : false;
  }
 
+static ssize_t kernel_write(struct file *file, const char *buf, size_t count,
+			    loff_t pos)
+{
+	mm_segment_t old_fs;
+	ssize_t res;
+
+	old_fs = get_fs();
+	set_fs(get_ds());
+	/* The cast to a user pointer is valid due to the set_fs() */
Quoted 9 lines

virt/kvm/eventfd.c ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val) · line 504

Quoted 6 lines
virt/kvm/eventfd.c +6 -0
+	res = vfs_write(file, (const char __user *)buf, count,&pos);
+	set_fs(old_fs);
+
+	return res;
+}
+

Is there no generic helper for this? Should there be?

I couldn't find one, I took the code above from fs/splice.c.

There should probably be a generic version of it as this snippet repeats itself in several locations throughout the kernel.

Quoted 7 lines
Quoted 7 lines
virt/kvm/eventfd.c +0 -0
  /* MMIO/PIO writes trigger an event if the addr/val match */
  static int
  ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
@@ -491,7 +512,11 @@ ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
  	if (!ioeventfd_in_range(p, addr, len, val))
  		return -EOPNOTSUPP;
 
Quoted 9 lines

virt/kvm/eventfd.c ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len, · line 516

Quoted 3 lines
virt/kvm/eventfd.c +2 -1
-	eventfd_signal(p->eventfd, 1);
+	if (p->pipe)
+		kernel_write(p->pipe, val, len, 0);

You're writing potentially variable length data.

We need a protocol containing address, data, length, and supporting read accesses as well.

This can't be variable length.

The user defines an ioeventfd as an address+length (with length being up to 8 bytes). The only time an ioeventfd is signaled is when the write to the guest memory is exactly at the specified address with exactly the specified length.

ioeventfds can be extended to handle more than 8 bytes, variable address offset and reads now that pipe support is added, but I'd rather do it in follow-up patches once basic pipe support is in.

Quoted 1 line

Is the write guaranteed atomic? We probably need serialization here.

afaik vfs_write is just a wrapper to the write() function of the underlying fs so it should be atomic, no?

Quoted 39 lines
Quoted 23 lines
virt/kvm/eventfd.c +8 -3
+	else
+		eventfd_signal(p->eventfd, 1);
+
  	return 0;
  }
 
@@ -555,9 +580,11 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  	if (args->flags&  ~KVM_IOEVENTFD_VALID_FLAG_MASK)
  		return -EINVAL;
 
-	eventfd = eventfd_ctx_fdget(args->fd);
-	if (IS_ERR(eventfd))
-		return PTR_ERR(eventfd);
+	if (!(args->flags&  KVM_IOEVENTFD_FLAG_PIPE)) {
+		eventfd = eventfd_ctx_fdget(args->fd);
+		if (IS_ERR(eventfd))
+			return PTR_ERR(eventfd);
+	}
 
  	p = kzalloc(sizeof(*p), GFP_KERNEL);
  	if (!p) {

virt/kvm/eventfd.c kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 602

Quoted 9 lines
virt/kvm/eventfd.c +5 -1
@@ -568,7 +595,11 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  	INIT_LIST_HEAD(&p->list);
  	p->addr    = args->addr;
  	p->length  = args->len;
-	p->eventfd = eventfd;
+
+	if (args->flags&  KVM_IOEVENTFD_FLAG_PIPE)
+		p->pipe = fget(args->fd);
+	else
+		p->eventfd = eventfd;

The split logic with the previous hunk isn't nice. Suggest moving the 'else' there, and assigning the whole union here.

Quoted 3 lines
(quoted diff) +0 -0
  	list_for_each_entry_safe(p, tmp,&kvm->ioeventfds, list) {
  		bool wildcard = !(args->flags&  KVM_IOEVENTFD_FLAG_DATAMATCH);
 
Quoted 8 lines

virt/kvm/eventfd.c kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 658

Quoted 6 lines
virt/kvm/eventfd.c +1 -2
-		if (p->eventfd != eventfd  ||
-		    p->addr != args->addr  ||
+		if (p->addr != args->addr  ||
  		    p->length != args->len ||
  		    p->wildcard != wildcard)
  			continue;

Why?

I didn't think that assigning 2 different events with exactly the same address, length and data can happen. Why would it?

Quoted 2 lines
Signature · 1 line
Sasha.
#3 lore raw
Quoted 19 lines · Sasha Levin
Quoted 9 lines
Quoted 9 lines
virt/kvm/eventfd.c +9 -0
+static ssize_t kernel_write(struct file *file, const char *buf, size_t count,
+                       loff_t pos)
+{
+   mm_segment_t old_fs;
+   ssize_t res;
+
+   old_fs = get_fs();
+   set_fs(get_ds());
+   /* The cast to a user pointer is valid due to the set_fs() */
Quoted 9 lines

virt/kvm/eventfd.c ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val) · line 504

Quoted 6 lines

+   res = vfs_write(file, (const char __user *)buf, count,&pos);
+   set_fs(old_fs);

virt/kvm/eventfd.c +4 -0
+
+   return res;
+}
+

Is there no generic helper for this?  Should there be?

Quoted 4 lines

I couldn't find one, I took the code above from fs/splice.c.

There should probably be a generic version of it as this snippet repeats itself in several locations throughout the kernel.

But not all of them do just vfs_write() after set_fs(). I don't think a generic kernel_wrapper() is worth it to be honest.

Pekka

#4 lore raw
In reply to Sasha Levin
Quoted 16 lines · Sasha Levin
Quoted 15 lines · Avi Kivity
Quoted 13 lines · Sasha Levin

The new flag allows passing a write side of a pipe instead of an eventfd to be notified of writes to the specified memory region.

Instead of signaling an event, the value written to the memory region is written to the pipe.

Using a pipe instead of an eventfd is usefull when any value can be written to the memory region but we're interested in recieving the actual value instead of just a notification.

A simple example for practical use is the serial port. we are not interested in an exit every time a char is written to the port, but we do need to know what was written so we could handle it on the guest.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

---

   include/linux/kvm.h |    2 +
   virt/kvm/eventfd.c  |   65 +++++++++++++++++++++++++++++++++++---------------

Documentation/virtua/kvm/api.txt +++++++++++++++++

I couldn't find the ioeventfd docs in there and forgot that it's not in the mainline yet, I'll rebase on kvm git tree :)

Please do that forall kvm patches in the future.

Quoted 8 lines
Quoted 3 lines

Is there no generic helper for this? Should there be?

I couldn't find one, I took the code above from fs/splice.c.

There should probably be a generic version of it as this snippet repeats itself in several locations throughout the kernel.

Yes. Suggest sending a patchset that consolidates this to akpm.

Quoted 11 lines
Quoted 10 lines
Quoted 4 lines

- eventfd_signal(p->eventfd, 1);
+ if (p->pipe)
+ kernel_write(p->pipe, val, len, 0);

You're writing potentially variable length data.

We need a protocol containing address, data, length, and supporting read accesses as well.

Quoted 7 lines

This can't be variable length.

The user defines an ioeventfd as an address+length (with length being up to 8 bytes). The only time an ioeventfd is signaled is when the write to the guest memory is exactly at the specified address with exactly the specified length.

It can be variable length if multiple ioeventfds reference the same pipe.

Quoted 3 lines

ioeventfds can be extended to handle more than 8 bytes, variable address offset and reads now that pipe support is added, but I'd rather do it in follow-up patches once basic pipe support is in.

In general incremental development is great, but I don't want to fragment the ABI. I'd like to be able to forward an entire PCI BAR over a pipe. That means sending the address/data/length tuple, and both read and write support.

Quoted 4 lines
Quoted 1 line

Is the write guaranteed atomic? We probably need serialization here.

afaik vfs_write is just a wrapper to the write() function of the underlying fs so it should be atomic, no?

write() isn't atomic in general. It is for pipes under certain circumstances, but there is no guarantee that the circumstances apply, or that the fd is in fact a pipe.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
   	list_for_each_entry_safe(p, tmp,&kvm->ioeventfds, list) {
   		bool wildcard = !(args->flags&   KVM_IOEVENTFD_FLAG_DATAMATCH);
 -		if (p->eventfd != eventfd  ||
 -		    p->addr != args->addr  ||
 +		if (p->addr != args->addr  ||

p->length != args->len ||
p->wildcard != wildcard)
continue;

Quoted 5 lines
Quoted 1 line

Why?

I didn't think that assigning 2 different events with exactly the same address, length and data can happen. Why would it?

No reason, but this is a user interface. You can't assume anything about the user except that he is an evil genius intent on breaking the kernel in various ways.

Signature · 1 line
error compiling committee.c: too many arguments to function
#13 lore raw
In reply to Sasha Levin
Quoted 6 lines · Sasha Levin

The new flag allows passing a write side of a pipe instead of an eventfd to be notified of writes to the specified memory region.

Instead of signaling an event, the value written to the memory region is written to the pipe.

Quoted 7 lines

Using a pipe instead of an eventfd is usefull when any value can be written to the memory region but we're interested in recieving the actual value instead of just a notification.

A simple example for practical use is the serial port. we are not interested in an exit every time a char is written to the port, but we do need to know what was written so we could handle it on the guest.

Looking at this example, how would you handle a pipe full condition? We can't buffer unlimited amount of data in the host.

Quoted 73 lines
Cc Avi Kivity <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Cc Pekka Enberg <[email protected]>
---
 include/linux/kvm.h |    2 +
 virt/kvm/eventfd.c  |   65 +++++++++++++++++++++++++++++++++++---------------
 2 files changed, 47 insertions(+), 20 deletions(-)
include/linux/kvm.h +2 -0
index 55ef181..548f23a 100644
@@ -387,12 +387,14 @@ enum {
 	kvm_ioeventfd_flag_nr_datamatch,
 	kvm_ioeventfd_flag_nr_pio,
 	kvm_ioeventfd_flag_nr_deassign,
+	kvm_ioeventfd_flag_nr_pipe,
 	kvm_ioeventfd_flag_nr_max,
 };
 
 #define KVM_IOEVENTFD_FLAG_DATAMATCH (1 << kvm_ioeventfd_flag_nr_datamatch)
 #define KVM_IOEVENTFD_FLAG_PIO       (1 << kvm_ioeventfd_flag_nr_pio)
 #define KVM_IOEVENTFD_FLAG_DEASSIGN  (1 << kvm_ioeventfd_flag_nr_deassign)
+#define KVM_IOEVENTFD_FLAG_PIPE      (1 << kvm_ioeventfd_flag_nr_pipe)
 
 #define KVM_IOEVENTFD_VALID_FLAG_MASK  ((1 << kvm_ioeventfd_flag_nr_max) - 1)
 
virt/kvm/eventfd.c +10 -4
index 73358d2..434293e 100644
@@ -413,10 +413,11 @@ module_exit(irqfd_module_exit);
 
 /*
  * --------------------------------------------------------------------
- * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
+ * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal or
+ *            a pipe write.
  *
- * userspace can register a PIO/MMIO address with an eventfd for receiving
- * notification when the memory has been touched.
+ * userspace can register a PIO/MMIO address with an eventfd or a 
+ * pipe for receiving notification when the memory has been touched.
  * --------------------------------------------------------------------
  */
 
@@ -424,6 +425,7 @@ struct _ioeventfd {
 	struct list_head     list;
 	u64                  addr;
 	int                  length;
+	struct file         *pipe;
 	struct eventfd_ctx  *eventfd;
 	u64                  datamatch;
 	struct kvm_io_device dev;
@@ -439,7 +441,11 @@ to_ioeventfd(struct kvm_io_device *dev)
 static void
 ioeventfd_release(struct _ioeventfd *p)
 {
-	eventfd_ctx_put(p->eventfd);
+	if (p->eventfd)
+		eventfd_ctx_put(p->eventfd);
+	else
+		fput(p->pipe);
+
 	list_del(&p->list);
 	kfree(p);
 }
@@ -481,6 +487,21 @@ ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
 	return _val == p->datamatch ? true : false;
 }

virt/kvm/eventfd.c ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val) · line 498

Quoted 10 lines
virt/kvm/eventfd.c +9 -0
 
+static ssize_t kernel_write(struct file *file, const char *buf, size_t count,
+			    loff_t pos)
+{
+	mm_segment_t old_fs;
+	ssize_t res;
+
+	old_fs = get_fs();
+	set_fs(get_ds());
+	/* The cast to a user pointer is valid due to the set_fs() */

Interesting. Is buf really always a user pointer?
Why don't we tag it __user then?

virt/kvm/eventfd.c ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val) · line 499

Quoted 1 line
virt/kvm/eventfd.c +1 -0
+	res = vfs_write(file, (const char __user *)buf, count, &pos);

If pipe is non-blocking, or if we get a signal,
this might fail or return a value < len.
Data will be lost then, won't it?

Quoted 46 lines
virt/kvm/eventfd.c +16 -5
+	set_fs(old_fs);
+
+	return res;
+}
+
 /* MMIO/PIO writes trigger an event if the addr/val match */
 static int
 ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
@@ -491,7 +512,11 @@ ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
 	if (!ioeventfd_in_range(p, addr, len, val))
 		return -EOPNOTSUPP;
 
-	eventfd_signal(p->eventfd, 1);
+	if (p->pipe)
+		kernel_write(p->pipe, val, len, 0);
+	else
+		eventfd_signal(p->eventfd, 1);
+
 	return 0;
 }
 
@@ -533,7 +558,7 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 	int                       pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
 	enum kvm_bus              bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
 	struct _ioeventfd        *p;
-	struct eventfd_ctx       *eventfd;
+	struct eventfd_ctx       *eventfd = NULL;
 	int                       ret;
 
 	/* must be natural-word sized */
@@ -555,9 +580,11 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 	if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
 		return -EINVAL;
 
-	eventfd = eventfd_ctx_fdget(args->fd);
-	if (IS_ERR(eventfd))
-		return PTR_ERR(eventfd);
+	if (!(args->flags & KVM_IOEVENTFD_FLAG_PIPE)) {
+		eventfd = eventfd_ctx_fdget(args->fd);
+		if (IS_ERR(eventfd))
+			return PTR_ERR(eventfd);
+	}
 
 	p = kzalloc(sizeof(*p), GFP_KERNEL);
 	if (!p) {

virt/kvm/eventfd.c kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 600

Quoted 7 lines
virt/kvm/eventfd.c +3 -1
@@ -568,7 +595,11 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 	INIT_LIST_HEAD(&p->list);
 	p->addr    = args->addr;
 	p->length  = args->len;
-	p->eventfd = eventfd;
+
+	if (args->flags & KVM_IOEVENTFD_FLAG_PIPE)
+		p->pipe = fget(args->fd);

This really needs to check that the fd is a pipe.
Otherwise you can do weird things like pass in
the kvm device fd itself.

Quoted 39 lines
virt/kvm/eventfd.c +6 -8
+	else
+		p->eventfd = eventfd;
 
 	/* The datamatch feature is optional, otherwise this is a wildcard */
 	if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
@@ -601,7 +632,9 @@ unlock_fail:
 
 fail:
 	kfree(p);
-	eventfd_ctx_put(eventfd);
+
+	if (!(args->flags & KVM_IOEVENTFD_FLAG_PIPE))
+		eventfd_ctx_put(eventfd);
 
 	return ret;
 }
@@ -612,20 +645,14 @@ kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 	int                       pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
 	enum kvm_bus              bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
 	struct _ioeventfd        *p, *tmp;
-	struct eventfd_ctx       *eventfd;
 	int                       ret = -ENOENT;
 
-	eventfd = eventfd_ctx_fdget(args->fd);
-	if (IS_ERR(eventfd))
-		return PTR_ERR(eventfd);
-
 	mutex_lock(&kvm->slots_lock);
 
 	list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
 		bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
 
-		if (p->eventfd != eventfd  ||
-		    p->addr != args->addr  ||
+		if (p->addr != args->addr  ||
 		    p->length != args->len ||
 		    p->wildcard != wildcard)
 			continue;

virt/kvm/eventfd.c kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 645

Quoted 5 lines
virt/kvm/eventfd.c +0 -2
@@ -641,8 +668,6 @@ kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 
 	mutex_unlock(&kvm->slots_lock);
 
-	eventfd_ctx_put(eventfd);
-

Don't we need to put on deassign?

Quoted 5 lines
virt/kvm/eventfd.c +0 -0
 	return ret;
 }
 
Signature · 1 line
1.7.6
#14 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Sasha Levin

The new flag allows passing a write side of a pipe instead of an eventfd to be notified of writes to the specified memory region.

Instead of signaling an event, the value written to the memory region is written to the pipe.

Using a pipe instead of an eventfd is usefull when any value can be written to the memory region but we're interested in recieving the actual value instead of just a notification.

Quoted 6 lines
Quoted 3 lines

A simple example for practical use is the serial port. we are not interested in an exit every time a char is written to the port, but we do need to know what was written so we could handle it on the guest.

Looking at this example, how would you handle a pipe full condition? We can't buffer unlimited amount of data in the host.

Stall.

Quoted 10 lines
Quoted 9 lines

+static ssize_t kernel_write(struct file *file, const char *buf, size_t count,
+ loff_t pos)
+{
+ mm_segment_t old_fs;
+ ssize_t res;
+
+ old_fs = get_fs();
+ set_fs(get_ds());
+ /* The cast to a user pointer is valid due to the set_fs() */

Quoted 2 lines

Interesting. Is buf really always a user pointer?
Why don't we tag it __user then?

It's not a user pointer, this is just to get the tools happy. set_fs() makes it safe.

Quoted 5 lines
Quoted 1 line

+ res = vfs_write(file, (const char __user *)buf, count,&pos);

If pipe is non-blocking, or if we get a signal,
this might fail or return a value< len.
Data will be lost then, won't it?

Yes. Need a loop-until-buffer-exhausted-or-error.

Error reporting is an interesting question. Typically we have KVM_RUN return the error, but if we use this facility to run something in a separate process, this can cause the device process crash to cause the guest to crash.

Quoted 8 lines
Quoted 4 lines

- p->eventfd = eventfd;
+
+ if (args->flags& KVM_IOEVENTFD_FLAG_PIPE)
+ p->pipe = fget(args->fd);

This really needs to check that the fd is a pipe.
Otherwise you can do weird things like pass in
the kvm device fd itself.

Eww, reference loop. Good catch.

We should allow unix domain sockets as well. In fact, for read/write support, we need this to be a unix domain socket.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
In reply to Avi Kivity
Quoted 4 lines · Avi Kivity

I'd like to be able to forward an entire PCI BAR
over a pipe. That means sending the address/data/length tuple, and both read and write support.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Is the write guaranteed atomic? We probably need serialization here.

afaik vfs_write is just a wrapper to the write() function of the underlying fs so it should be atomic, no?

write() isn't atomic in general. It is for pipes under certain circumstances, but there is no guarantee that the circumstances apply, or that the fd is in fact a pipe.

So the above makes it tricky to pass structured data
which is > 1 byte in size over a pipe. We could build an in-kernel API (not sure how useful it would be for userspace) to do
an atomic write or fail (instead of a partial write),
but we'll still need to handle failures.

Signature · 1 line
MST
#6 lore raw
Quoted 14 lines · Michael S. Tsirkin
Quoted 4 lines · Avi Kivity

I'd like to be able to forward an entire PCI BAR
over a pipe. That means sending the address/data/length tuple, and both read and write support.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Is the write guaranteed atomic? We probably need serialization here.

afaik vfs_write is just a wrapper to the write() function of the underlying fs so it should be atomic, no?

write() isn't atomic in general. It is for pipes under certain circumstances, but there is no guarantee that the circumstances apply, or that the fd is in fact a pipe.

Quoted 5 lines

So the above makes it tricky to pass structured data
which is> 1 byte in size over a pipe. We could build an in-kernel API (not sure how useful it would be for userspace) to do
an atomic write or fail (instead of a partial write),
but we'll still need to handle failures.

We don't need atomicity after a failure, just under normal operation, and that can be provided by a mutex.

It's not trivial since a single fd might be associated with several region.

Signature · 1 line
error compiling committee.c: too many arguments to function
#15 lore raw
In reply to Avi Kivity
Quoted 12 lines · Avi Kivity
Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Sasha Levin

The new flag allows passing a write side of a pipe instead of an eventfd to be notified of writes to the specified memory region.

Instead of signaling an event, the value written to the memory region is written to the pipe.

Using a pipe instead of an eventfd is usefull when any value can be written to the memory region but we're interested in recieving the actual value instead of just a notification.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

A simple example for practical use is the serial port. we are not interested in an exit every time a char is written to the port, but we do need to know what was written so we could handle it on the guest.

Looking at this example, how would you handle a pipe full condition? We can't buffer unlimited amount of data in the host.

Stall.

Right, but the guest gets no indication that the pipe is full. Something like virtio would let the guest do something useful instead of stalling the vcpu.

Also noting that the fd can be set not to block, or that a signal can interrupt the write. Both cases are not errors.

Quoted 16 lines
Quoted 10 lines
Quoted 9 lines

+static ssize_t kernel_write(struct file *file, const char *buf, size_t count,
+ loff_t pos)
+{
+ mm_segment_t old_fs;
+ ssize_t res;
+
+ old_fs = get_fs();
+ set_fs(get_ds());
+ /* The cast to a user pointer is valid due to the set_fs() */

Quoted 2 lines

Interesting. Is buf really always a user pointer?
Why don't we tag it __user then?

It's not a user pointer, this is just to get the tools happy. set_fs() makes it safe.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

+ res = vfs_write(file, (const char __user *)buf, count,&pos);

If pipe is non-blocking, or if we get a signal,
this might fail or return a value< len.
Data will be lost then, won't it?

Yes. Need a loop-until-buffer-exhausted-or-error.

Signal handling becomes a problem. You don't want a full pipe to prevent qemu from getting killed or getting a timer alert.

Quoted 10 lines

Error reporting is an interesting question. Typically we have KVM_RUN return the error, but if we use this facility to run something in a separate process, this can cause the device process crash to cause the guest to crash.

Quoted 5 lines
Quoted 4 lines

- p->eventfd = eventfd;
+
+ if (args->flags& KVM_IOEVENTFD_FLAG_PIPE)
+ p->pipe = fget(args->fd);

Quoted 8 lines
Quoted 3 lines

This really needs to check that the fd is a pipe.
Otherwise you can do weird things like pass in
the kvm device fd itself.

Eww, reference loop. Good catch.

We should allow unix domain sockets as well. In fact, for read/write support, we need this to be a unix domain socket.

Sockets are actually better at this than pipes as you can at least make the writes non-blocking by passing in a message flag. If we support sockets, do we really need to support pipes at all?

Signature · 1 line
MST
#16 lore raw
Quoted 17 lines · Michael S. Tsirkin
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Michael S. Tsirkin
Quoted 13 lines · Sasha Levin

The new flag allows passing a write side of a pipe instead of an eventfd to be notified of writes to the specified memory region.

Instead of signaling an event, the value written to the memory region is written to the pipe.

Using a pipe instead of an eventfd is usefull when any value can be written to the memory region but we're interested in recieving the actual value instead of just a notification.

A simple example for practical use is the serial port. we are not interested in an exit every time a char is written to the port, but we do need to know what was written so we could handle it on the guest.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Looking at this example, how would you handle a pipe full condition? We can't buffer unlimited amount of data in the host.

Stall.

Right, but the guest gets no indication that the pipe is full. Something like virtio would let the guest do something useful instead of stalling the vcpu.

That's not a problem. The vcpu blocks, which lets the other process get the cpu and run with it. If there are not enough cpu resources, we'll indeed stall the vcpu, but that happens whenever you're overcommitted anyway.

Quoted 2 lines

Also noting that the fd can be set not to block, or that a signal can interrupt the write. Both cases are not errors.

One thing we can do is return via the normal KVM_EXIT_MMIO method and hope userspace knows how to handle this. Otherwise I don't see what we can do.

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

If pipe is non-blocking, or if we get a signal,
this might fail or return a value< len.
Data will be lost then, won't it?

Yes. Need a loop-until-buffer-exhausted-or-error.

Signal handling becomes a problem. You don't want a full pipe to prevent qemu from getting killed or getting a timer alert.

Maybe we should require AF_UNIX SOCK_SEQPACKET connection. That gives us atomicity, and drops the need for a mutex.

Quoted 7 lines
Quoted 3 lines

We should allow unix domain sockets as well. In fact, for read/write support, we need this to be a unix domain socket.

Sockets are actually better at this than pipes
as you can at least make the writes
non-blocking by passing in a message flag.

I'm not sure we want that. How do we handle it?

If the socket buffers get filled up, it's time for the vcpu to wait for the mmio server process. Let the scheduler sort things out.

btw, like vhost-net and other thread offloads, this sort of trick is dangerous. When you have excess cpu resources throughput improves, but once the system is loaded, the workload is needlessly spread across more cores than strictly necessary and communication is done by context switches instead of user/system transitions.

Quoted 2 lines

If we support sockets, do we really need to support pipes at all

I think not.

Signature · 1 line
error compiling committee.c: too many arguments to function
#17 lore raw
Quoted 5 lines · Avi Kivity
Quoted 2 lines

Also noting that the fd can be set not to block, or that a signal can interrupt the write. Both cases are not errors.

One thing we can do is return via the normal KVM_EXIT_MMIO method and hope userspace knows how to handle this.

Agree. We'll need some way to tell userspace that write has partially succeeded, presumably, so that userspace can write out the missing part.

Signature · 1 line
MST
#18 lore raw
Quoted 10 lines · Michael S. Tsirkin
Quoted 5 lines · Avi Kivity
Quoted 2 lines

Also noting that the fd can be set not to block, or that a signal can interrupt the write. Both cases are not errors.

One thing we can do is return via the normal KVM_EXIT_MMIO method and hope userspace knows how to handle this.

Agree. We'll need some way to tell userspace that write has partially succeeded, presumably, so that userspace can write out the missing part.

With SOCK_SEQPACKET, there will be no partial writes.

Signature · 1 line
error compiling committee.c: too many arguments to function
#19 lore raw
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Avi Kivity
Quoted 2 lines

Also noting that the fd can be set not to block, or that a signal can interrupt the write. Both cases are not errors.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

One thing we can do is return via the normal KVM_EXIT_MMIO method and hope userspace knows how to handle this.

Agree. We'll need some way to tell userspace that write has partially succeeded, presumably, so that userspace can write out the missing part.

With SOCK_SEQPACKET, there will be no partial writes.

Good idea. I think the same applies to netlink sockets.

Quoted 2 lines
Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw
In reply to Avi Kivity
Quoted 24 lines · Avi Kivity
Quoted 13 lines · Sasha Levin
Quoted 11 lines · Avi Kivity
Quoted 4 lines · Sasha Levin

- eventfd_signal(p->eventfd, 1);
+ if (p->pipe)
+ kernel_write(p->pipe, val, len, 0);

You're writing potentially variable length data.

We need a protocol containing address, data, length, and supporting read accesses as well.

Quoted 7 lines

This can't be variable length.

The user defines an ioeventfd as an address+length (with length being up to 8 bytes). The only time an ioeventfd is signaled is when the write to the guest memory is exactly at the specified address with exactly the specified length.

It can be variable length if multiple ioeventfds reference the same pipe.

Quoted 8 lines
Quoted 3 lines

ioeventfds can be extended to handle more than 8 bytes, variable address offset and reads now that pipe support is added, but I'd rather do it in follow-up patches once basic pipe support is in.

In general incremental development is great, but I don't want to fragment the ABI. I'd like to be able to forward an entire PCI BAR over a pipe. That means sending the address/data/length tuple, and both read and write support.

Would this mean that for sockets we want to remove the 8 byte limit?

What about eventfds? We can remove the limit there and assume that if the user asked for more than 8 bytes he knows what he's doing?

Signature · 1 line
Sasha.
#8 lore raw
Quoted 7 lines · Sasha Levin
Quoted 5 lines

In general incremental development is great, but I don't want to fragment the ABI. I'd like to be able to forward an entire PCI BAR over a pipe. That means sending the address/data/length tuple, and both read and write support.

Would this mean that for sockets we want to remove the 8 byte limit?

Yes. Register a range and support all sizes.

Perhaps it merits a separate ioctl.

Quoted 2 lines

What about eventfds? We can remove the limit there and assume that if the user asked for more than 8 bytes he knows what he's doing?

I can't really see that as useful. eventfds destroy information; without datamatch, you have no idea what value was written. Even with datamatch, you have no idea how many times it was written. With a range, you also have no idea which address was written. It's pretty meaningless.

Signature · 1 line
error compiling committee.c: too many arguments to function
#9 lore raw
Quoted 13 lines · Avi Kivity
Quoted 7 lines · Sasha Levin
Quoted 5 lines

In general incremental development is great, but I don't want to fragment the ABI. I'd like to be able to forward an entire PCI BAR over a pipe. That means sending the address/data/length tuple, and both read and write support.

Would this mean that for sockets we want to remove the 8 byte limit?

Yes. Register a range and support all sizes.

Perhaps it merits a separate ioctl.

Quoted 9 lines
Quoted 2 lines

What about eventfds? We can remove the limit there and assume that if the user asked for more than 8 bytes he knows what he's doing?

I can't really see that as useful. eventfds destroy information; without datamatch, you have no idea what value was written. Even with datamatch, you have no idea how many times it was written. With a range, you also have no idea which address was written. It's pretty meaningless.

It is pretty useless, but I didn't want the ioctl to behave differently when passing a socket or an eventfd.

If we do go for a new ioctl as you suggested then yes, problem solved.

Signature · 1 line
Sasha.
#10 lore raw
Quoted 8 lines · Sasha Levin
Quoted 7 lines

I can't really see that as useful. eventfds destroy information; without datamatch, you have no idea what value was written. Even with datamatch, you have no idea how many times it was written. With a range, you also have no idea which address was written. It's pretty meaningless.

Quoted 5 lines

It is pretty useless, but I didn't want the ioctl to behave differently when passing a socket or an eventfd.

If we do go for a new ioctl as you suggested then yes, problem solved.

Yes. I guess it depends on the numbers of if () s introduced into the code. If it starts to feel dirty, split it into a separate ioctl (they can both call common helpers).

It's fine to allow size > 8 for eventfds. Yes it's meaningless, but it's not harmful and I can't see it breaking anything. Note that we may need to change the way we do matches - currently 'size' means the access size, with an exact match on the address, but the new meaning is 'any address from start to start+size-1'.

Signature · 1 line
error compiling committee.c: too many arguments to function
#11 lore raw
Quoted 19 lines · Avi Kivity
Quoted 8 lines · Sasha Levin
Quoted 7 lines

I can't really see that as useful. eventfds destroy information; without datamatch, you have no idea what value was written. Even with datamatch, you have no idea how many times it was written. With a range, you also have no idea which address was written. It's pretty meaningless.

Quoted 5 lines

It is pretty useless, but I didn't want the ioctl to behave differently when passing a socket or an eventfd.

If we do go for a new ioctl as you suggested then yes, problem solved.

Yes. I guess it depends on the numbers of if () s introduced into the code. If it starts to feel dirty, split it into a separate ioctl (they can both call common helpers).

Quoted 6 lines

It's fine to allow size > 8 for eventfds. Yes it's meaningless, but it's not harmful and I can't see it breaking anything. Note that we may need to change the way we do matches - currently 'size' means the access size, with an exact match on the address, but the new meaning is 'any address from start to start+size-1'.

Looks like there was no need for a new ioctl. I've tried going further with adding 'meaningless' features to the existing implementation so that the sockets could be added without needing a new mechanism.

I've added a user configurable 'read' flag which is supposed to trigger an event when the memory is read and a 'nowrite' flag which won't trigger an event on a write. This makes it possible to add sockets while keeping the code pretty much the same.

Instead of making these flags user configurable, they could be implicitly defined according to the choice of eventfd or socket, I couldn't decide which method was better so just went with the first one.

Signature · 1 line
Sasha.
#12 lore raw
Quoted 4 lines · Sasha Levin

Instead of making these flags user configurable, they could be implicitly defined according to the choice of eventfd or socket, I couldn't decide which method was better so just went with the first one.

Explicit is better than implicit, so I agree with your choice here.

Signature · 1 line
error compiling committee.c: too many arguments to function