Skip to contentSkip to conversation

signal/timer/event fds v9 - KAIO eventfd support example ...

PATCH In review Davide Libenzi 3 participants 9 messages Mar 31, 2007 – Apr 1, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 13/13 signal/timer/event fds v9 - KAIO eventfd support example ...

This is an example about how to add eventfd support to the current KAIO code, in order to enable KAIO to post readiness events to a pollable fd (hence compatible with POSIX select/poll). The KAIO code simply signals the eventfd fd when events are ready, and this triggers a POLLIN in the fd. This patch uses a reserved for future use member of the struct iocb to pass an eventfd file descriptor, that KAIO will use to post events every time a request completes. At that point, an aio_getevents() will return the completed result to a struct io_event.
I made a quick test program to verify the patch, and it runs fine here:

http://www.xmailserver.org/eventfd-aio-test.c

The test program uses poll(2), but it'd, of course, work with select and epoll too.
This can allow to schedule both block I/O and other poll-able devices requests, and wait for results using select/poll/epoll.
In a typical scenario, an application would submit KAIO request using aio_submit(), and will also use epoll_ctl() on the whole other class of devices (that with the addition of signals, timers and user events, now it's pretty much complete), and then would:

	epoll_wait(...);
	for_each_event {
		if (curr_event_is_kaiofd) {
			aio_getevents();
			dispatch_aio_events();
		} else {
			dispatch_epoll_event();
		}
	}

- Davide

fs/aio.c +25 -0
Index: linux-2.6.21-rc5.fds/fs/aio.c
===================================================================
@@ -30,6 +30,7 @@
 #include <linux/highmem.h>
 #include <linux/workqueue.h>
 #include <linux/security.h>
+#include <linux/eventfd.h>
 
 #include <asm/kmap_types.h>
 #include <asm/uaccess.h>
@@ -421,6 +422,7 @@
 	req->private = NULL;
 	req->ki_iovec = NULL;
 	INIT_LIST_HEAD(&req->ki_run_list);
+	req->ki_eventfd = ERR_PTR(-EINVAL);
 
 	/* Check if the completion queue has enough free space to
 	 * accept an event from this io.
@@ -462,6 +464,8 @@
 {
 	assert_spin_locked(&ctx->ctx_lock);
 
+	if (!IS_ERR(req->ki_eventfd))
+		fput(req->ki_eventfd);
 	if (req->ki_dtor)
 		req->ki_dtor(req);
 	if (req->ki_iovec != &req->ki_inline_vec)
@@ -946,6 +950,14 @@
 		return 1;
 	}
 
+	/*
+	 * Check if the user asked us to deliver the result through an
+	 * eventfd. The eventfd_signal() function is safe to be called
+	 * from IRQ context.
+	 */
+	if (!IS_ERR(iocb->ki_eventfd))
+		eventfd_signal(iocb->ki_eventfd, 1);
+
 	info = &ctx->ring_info;
 
 	/* add a completion event to the ring buffer.
@@ -1555,6 +1567,19 @@
 		fput(file);
 		return -EAGAIN;
 	}
+	if (iocb->aio_resfd != 0) {
+		/*
+		 * If the aio_resfd field of the iocb is not zero, get an
+		 * instance of the file* now. The file descriptor must be
+		 * an eventfd() fd, and will be signaled for each completed
+		 * event using the eventfd_signal() function.
+		 */
+		req->ki_eventfd = eventfd_fget((int) iocb->aio_resfd);
+		if (IS_ERR(req->ki_eventfd)) {
+			ret = PTR_ERR(req->ki_eventfd);
+			goto out_put_req;
+		}
+	}
 
 	req->ki_filp = file;
 	ret = put_user(req->ki_key, &user_iocb->aio_key);
include/linux/aio.h +6 -0
Index: linux-2.6.21-rc5.fds/include/linux/aio.h
===================================================================
@@ -119,6 +119,12 @@
 
 	struct list_head	ki_list;	/* the aio core uses this
 						 * for cancellation */
+
+	/*
+	 * If the aio_resfd field of the userspace iocb is not zero,
+	 * this is the underlying file* to deliver event to.
+	 */
+	struct file		*ki_eventfd;
 };
 
 #define is_sync_kiocb(iocb)	((iocb)->ki_key == KIOCB_SYNC_KEY)
include/linux/aio_abi.h +5 -1
Index: linux-2.6.21-rc5.fds/include/linux/aio_abi.h
===================================================================
@@ -84,7 +84,11 @@
 
 	/* extra parameters */
 	__u64	aio_reserved2;	/* TODO: use this for a (struct sigevent *) */
-	__u64	aio_reserved3;
+	__u32	aio_reserved3;
+	/*
+	 * If different from 0, this is an eventfd to deliver AIO results to
+	 */
+	__u32	aio_resfd;
 }; /* 64 bytes */
 
 #undef IFBIG
#1 lore raw
Quoted 16 lines · Davide Libenzi

This is an example about how to add eventfd support to the current KAIO code, in order to enable KAIO to post readiness events to a pollable fd (hence compatible with POSIX select/poll). The KAIO code simply signals the eventfd fd when events are ready, and this triggers a POLLIN in the fd. This patch uses a reserved for future use member of the struct iocb to pass an eventfd file descriptor, that KAIO will use to post events every time a request completes. At that point, an aio_getevents() will return the completed result to a struct io_event.
I made a quick test program to verify the patch, and it runs fine here:

Index: linux-2.6.21-rc5.fds/include/linux/aio_abi.h
===================================================================

include/linux/aio_abi.h +0 -0
@@ -84,7 +84,11 @@
 

include/linux/aio_abi.h line 93

Quoted 10 lines
include/linux/aio_abi.h +5 -1
 	/* extra parameters */
 	__u64	aio_reserved2;	/* TODO: use this for a (struct sigevent *) */
-	__u64	aio_reserved3;
+	__u32	aio_reserved3;
+	/*
+	 * If different from 0, this is an eventfd to deliver AIO results to
+	 */
+	__u32	aio_resfd;
 }; /* 64 bytes */
 

What is the motivation for adding aio_resfd to an individual iocb instead of the entire io context? It seems redundant, as you can already create multiple io contexts to wait on.

[also, minor nit: sys_eventfd() can legitimately return 0, but you're banning its use in aio. userspace could easily dup() and close(), but...]

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

What is the motivation for adding aio_resfd to an individual iocb instead of the entire io context? It seems redundant, as you can already create multiple io contexts to wait on.

To add it to the context, you need to either change the context create API (I think no-go here), or add a new syscall just to handle that. Doing it in the iocb gives finer grained setup, but can be more work for the user that wants to use it for all the iocbs.

Quoted 2 lines

[also, minor nit: sys_eventfd() can legitimately return 0, but you're banning its use in aio. userspace could easily dup() and close(), but...]

Noone said that binary compatibility comes free ;)
I'm truly open to other ways of integration, but I thought that binary compatibility is the key here.

- Davide

#3 lore raw
Quoted 7 lines · Davide Libenzi
Quoted 3 lines

[also, minor nit: sys_eventfd() can legitimately return 0, but you're banning its use in aio. userspace could easily dup() and close(), but...]

Noone said that binary compatibility comes free ;)
I'm truly open to other ways of integration, but I thought that binary compatibility is the key here.

I would suggest taking "aio_reserved3" and turning it into

	__u32 aio_flags;
	__u32 aio_resfd;

if so. And then some bit in the "flags" field has to be set (say, "IOCB_FLAG_RESFB") to enable this codepath, instead of expecting that aio_resfd == 0 is special.

It's generally always a good thing to have a "flags" field anyway, exactly because it allows for future expansion.

Linus

#4 lore raw
Quoted 14 lines · Linus Torvalds
Quoted 7 lines · Davide Libenzi
Quoted 3 lines

[also, minor nit: sys_eventfd() can legitimately return 0, but you're banning its use in aio. userspace could easily dup() and close(), but...]

Noone said that binary compatibility comes free ;)
I'm truly open to other ways of integration, but I thought that binary compatibility is the key here.

I would suggest taking "aio_reserved3" and turning it into

	__u32 aio_flags;
	__u32 aio_resfd;
Quoted 6 lines

if so. And then some bit in the "flags" field has to be set (say, "IOCB_FLAG_RESFB") to enable this codepath, instead of expecting that aio_resfd == 0 is special.

It's generally always a good thing to have a "flags" field anyway, exactly because it allows for future expansion.

Makes sense. I was actually thinking of using the higher bit to signal that, but a flags field makes more sense.

- Davide

#5 lore raw
In reply to Davide Libenzi
Quoted 11 lines · Davide Libenzi
Quoted 4 lines · Avi Kivity

What is the motivation for adding aio_resfd to an individual iocb instead of the entire io context? It seems redundant, as you can already create multiple io contexts to wait on.

To add it to the context, you need to either change the context create API (I think no-go here), or add a new syscall just to handle that. Doing it in the iocb gives finer grained setup, but can be more work for the user that wants to use it for all the iocbs.

I think it's a bit too fine grained, and a new system call (io_bindfd()?) would be easier to use. In addition, you would move the eventfd_fget() out of the submission path.

For the users that want fine grained control, they can already call io_setup() multiple times and submit iocbs to different completion rings.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#6 lore raw
Quoted 17 lines · Avi Kivity
Quoted 10 lines · Davide Libenzi
Quoted 6 lines · Avi Kivity

What is the motivation for adding aio_resfd to an individual iocb instead of
the entire io context? It seems redundant, as you can already create multiple
io contexts to wait on.

Quoted 5 lines

To add it to the context, you need to either change the context create API (I think no-go here), or add a new syscall just to handle that. Doing it in the iocb gives finer grained setup, but can be more work for the user that wants to use it for all the iocbs.

Quoted 3 lines

I think it's a bit too fine grained, and a new system call (io_bindfd()?) would be easier to use. In addition, you would move the eventfd_fget() out of the submission path.

IMO the cost of the eventfd_fget() (have you seen it?) is not worth adding a new syscall.
Actually, the flags field that Linus suggested may be given an extra meaning of "bind to ctx", that'd solve the problem w/out new syscalls.

- Davide

#7 lore raw
Quoted 7 lines · Davide Libenzi
Quoted 4 lines

I think it's a bit too fine grained, and a new system call (io_bindfd()?) would be easier to use. In addition, you would move the eventfd_fget() out of the submission path.

IMO the cost of the eventfd_fget() (have you seen it?) is not worth adding a new syscall.

There's an atomic op there (and another on the way out). Probably on a busy cacheline. Still it's probably lost in the noise.

Regardless of that, I think that specifying the fd per submission is wrong. It feels like a setup thing that needs to be done once. We shouldn't skimp on syscalls, especially on something as important as unifying the async event model.

Quoted 3 lines

Actually, the flags field that Linus suggested may be given an extra meaning of "bind to ctx", that'd solve the problem w/out new syscalls.

I don't see how. It's still per submission. You could do it on the first iocb, but that's just adding warts to the API.

You could add an IO_CMD_BIND_EVENTFD, but that feels wrong too, as it isn't really an I/O command.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#8 lore raw
Quoted 13 lines · Avi Kivity
Quoted 9 lines · Davide Libenzi
Quoted 5 lines

I think it's a bit too fine grained, and a new system call (io_bindfd()?) would be easier to use. In addition, you would move the eventfd_fget() out of
the submission path.

IMO the cost of the eventfd_fget() (have you seen it?) is not worth adding a new syscall.

Quoted 7 lines

There's an atomic op there (and another on the way out). Probably on a busy cacheline. Still it's probably lost in the noise.

Regardless of that, I think that specifying the fd per submission is wrong. It feels like a setup thing that needs to be done once. We shouldn't skimp on syscalls, especially on something as important as unifying the async event model.

For userspace that wants to do that (that is, having iocbs signaled to an eventfd), do not really matter much. They have their own iocb setup function and they'd simply set a flag more and set the resfd.
The global setting may be good for some users, with others that may complain in having to create another ctx just to set an fd. Again, IMO is not worth a new API. What do other folks think?

- Davide