Skip to contentSkip to conversation

signalfd/timerfd/asyncfd v5 - asyncfd core ...

PATCH In review Davide Libenzi 1 participant 1 message Mar 14, 2007

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

PATCH 10/13 signalfd/timerfd/asyncfd v5 - asyncfd core ...

This is the asyncfd implementation. It's a thin and generic file descriptor that can be used to deliver results to userspace and, at the same time, supports the Linux f_op->poll subsystem making it able to be used with POSIX select(2)/poll(2) (and, as a consequence, epoll(2)).
The use for such result delivery fd is to enable KAIO and syslets/threadlets to work with POSIX select(2)/poll(2) and epoll, hence bridging the wide range of devices supporting f_op->poll with the ones that are typically not (like block I/O).
Coupled with syslets/threadlets, this enable results coming from them to be "pollable" inside an epoll fd, that is a complementary way to the one of hosting an epoll_wait(2) over a syslet/threadlet.
I have two version of this patch actually. One that uses kernel memory to buffer results, and exposes a read(2) interface to userspace, and this one, that uses a buffer (ring) supplied by the user during the asyncfd creation. Both have Pros&Cons, but I ended up posting this one because of the kernel memory usage limiting that the former had.
I have no personal preferences between the two, so to me it does not matter. The asyncfd API at the moment is defined as:

struct asyncfd_uring {
	__u64 ptr;	/* Pointer to a buffer of struct asyncfd_result */
	__u32 iuser;	/* User index */
	__u32 size;	/* Number of struct asyncfd_result in the buffer */
	__u32 ikernel;	/* Kernel index */
};

int asyncfd(int ufd, struct asyncfd_uring *urng);

Like the signalfd and timerfd APIs, it is possible to change the userspace ring used by the fd, by passing an axisting fd in "ufd". If "ufd" is -1, a new asyncfd file will be created.
It is possible in that way to replace the existing ring with a larger one, once the userspace code realizes that the potential number of submitted requests may not be able to be stored in the previous one. Just:

asyncfd(asfd, &newring);
flush_process(&oldring);

The kernel side asyncfd API is very simple:

struct asyncfd_result {
	__u64 cookie;
	__u64 obj;
	__s64 res;
	__s64 res2;
};

struct file *asyncfd_fget(int fd);
int asyncfd_add_results(struct file *file, const struct asyncfd_result *results,

			int n);

When the user passes an asyncfd to the kernel, by hence telling it to deliver result to such fd, the kernel should call asyncfd_fget() to get an instance of the asyncfd file*. The asyncfd_fget() function simply does an fget() and verifies that this is really an asyncfd (compare f_op pointer). Then, when results are available to be delivered, the kernel should call the asyncfd_add_results() function with one or more results to be stored in the userspace ring. The asyncfd_add_results() function returns the number of stored results, or an error if the userspace ring is invalid (-EFAULT basically). It can also return a number lower than "n", if the userspace ring is full. The asyncfd_add_results() may sleep in this implementation, since it uses {put,get}_user on non mlocked userspace memory. It should, in theory, not be a problem, but if it is, we need to either mlock the ring (me no-liky) or use the buffered results version of the patch.
The struct asyncfd_result tries to include all the members currently necessary to ship a KAIO result to userspace, but even this, is not set in stone. As I see it, the asyncfd should be an optional delivery machanism to be included in the struct async_head_user used by syslets/threadlets, and hence offer a result transport to them.

- Davide

fs/Makefile +1 -1
Index: linux-2.6.20.ep2/fs/Makefile
===================================================================
@@ -11,7 +11,7 @@
 		attr.o bad_inode.o file.o filesystems.o namespace.o aio.o \
 		seq_file.o xattr.o libfs.o fs-writeback.o \
 		pnode.o drop_caches.o splice.o sync.o utimes.o \
-		stack.o anon_inodes.o signalfd.o timerfd.o
+		stack.o anon_inodes.o signalfd.o timerfd.o asyncfd.o
 
 ifeq ($(CONFIG_BLOCK),y)
 obj-y +=	buffer.o bio.o block_dev.o direct-io.o mpage.o ioprio.o
fs/asyncfd.c new file +233 -0

233 lines not on this page · raw message

include/linux/asyncfd.h new file +34 -0
Index: linux-2.6.20.ep2/include/linux/asyncfd.h
===================================================================
@@ -0,0 +1,34 @@
+/*
+ *  include/linux/asyncfd.h
+ *
+ *  Copyright (C) 2007  Davide Libenzi <[email protected]>
+ *
+ */
+
+#ifndef _LINUX_ASYNCFD_H
+#define _LINUX_ASYNCFD_H
+
+
+
+struct asyncfd_uring {
+	__u64 ptr;
+	__u32 iuser;
+	__u32 size;
+	__u32 ikernel;
+};
+
+struct asyncfd_result {
+	__u64 cookie;
+	__u64 obj;
+	__s64 res;
+	__s64 res2;
+};
+
+
+struct file *asyncfd_fget(int fd);
+int asyncfd_add_results(struct file *file, const struct asyncfd_result *results,
+			int n);
+
+
+#endif /* _LINUX_ASYNCFD_H */
+
include/linux/syscalls.h +2 -0
Index: linux-2.6.20.ep2/include/linux/syscalls.h
===================================================================
@@ -54,6 +54,7 @@
 struct compat_timeval;
 struct robust_list_head;
 struct getcpu_cache;
+struct asyncfd_uring;
 
 #include <linux/types.h>
 #include <linux/aio_abi.h>
@@ -605,6 +606,7 @@
 asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask);
 asmlinkage long sys_timerfd(int ufd, int clockid, int flags,
 			    const struct itimerspec __user *utmr);
+asmlinkage long sys_asyncfd(int ufd, struct asyncfd_uring __user *urng);
 
 int kernel_execve(const char *filename, char *const argv[], char *const envp[]);