eventfd.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  fs/eventfd.c
 *
 *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
 *
 */

#include <linux/file.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/anon_inodes.h>
#include <linux/eventfd.h>
18
#include <linux/syscalls.h>
19 20 21 22 23 24 25 26 27 28 29 30

struct eventfd_ctx {
	wait_queue_head_t wqh;
	/*
	 * Every time that a write(2) is performed on an eventfd, the
	 * value of the __u64 being written is added to "count" and a
	 * wakeup is performed on "wqh". A read(2) will return the "count"
	 * value to userspace, and will reset "count" to zero. The kernel
	 * size eventfd_signal() also, adds to the "count" counter and
	 * issue a wakeup.
	 */
	__u64 count;
31
	unsigned int flags;
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
};

/*
 * Adds "n" to the eventfd counter "count". Returns "n" in case of
 * success, or a value lower then "n" in case of coutner overflow.
 * This function is supposed to be called by the kernel in paths
 * that do not allow sleeping. In this function we allow the counter
 * to reach the ULLONG_MAX value, and we signal this as overflow
 * condition by returining a POLLERR to poll(2).
 */
int eventfd_signal(struct file *file, int n)
{
	struct eventfd_ctx *ctx = file->private_data;
	unsigned long flags;

	if (n < 0)
		return -EINVAL;
D
Davide Libenzi 已提交
49
	spin_lock_irqsave(&ctx->wqh.lock, flags);
50 51 52 53
	if (ULLONG_MAX - ctx->count < n)
		n = (int) (ULLONG_MAX - ctx->count);
	ctx->count += n;
	if (waitqueue_active(&ctx->wqh))
54
		wake_up_locked_poll(&ctx->wqh, POLLIN);
D
Davide Libenzi 已提交
55
	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

	return n;
}

static int eventfd_release(struct inode *inode, struct file *file)
{
	kfree(file->private_data);
	return 0;
}

static unsigned int eventfd_poll(struct file *file, poll_table *wait)
{
	struct eventfd_ctx *ctx = file->private_data;
	unsigned int events = 0;
	unsigned long flags;

	poll_wait(file, &ctx->wqh, wait);

D
Davide Libenzi 已提交
74
	spin_lock_irqsave(&ctx->wqh.lock, flags);
75 76 77 78 79 80
	if (ctx->count > 0)
		events |= POLLIN;
	if (ctx->count == ULLONG_MAX)
		events |= POLLERR;
	if (ULLONG_MAX - 1 > ctx->count)
		events |= POLLOUT;
D
Davide Libenzi 已提交
81
	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
82 83 84 85 86 87 88 89 90

	return events;
}

static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
			    loff_t *ppos)
{
	struct eventfd_ctx *ctx = file->private_data;
	ssize_t res;
91
	__u64 ucnt = 0;
92 93 94 95
	DECLARE_WAITQUEUE(wait, current);

	if (count < sizeof(ucnt))
		return -EINVAL;
D
Davide Libenzi 已提交
96
	spin_lock_irq(&ctx->wqh.lock);
97
	res = -EAGAIN;
98
	if (ctx->count > 0)
99 100 101 102 103 104 105 106 107 108 109 110 111
		res = sizeof(ucnt);
	else if (!(file->f_flags & O_NONBLOCK)) {
		__add_wait_queue(&ctx->wqh, &wait);
		for (res = 0;;) {
			set_current_state(TASK_INTERRUPTIBLE);
			if (ctx->count > 0) {
				res = sizeof(ucnt);
				break;
			}
			if (signal_pending(current)) {
				res = -ERESTARTSYS;
				break;
			}
D
Davide Libenzi 已提交
112
			spin_unlock_irq(&ctx->wqh.lock);
113
			schedule();
D
Davide Libenzi 已提交
114
			spin_lock_irq(&ctx->wqh.lock);
115 116 117 118
		}
		__remove_wait_queue(&ctx->wqh, &wait);
		__set_current_state(TASK_RUNNING);
	}
119 120 121
	if (likely(res > 0)) {
		ucnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
		ctx->count -= ucnt;
122
		if (waitqueue_active(&ctx->wqh))
123
			wake_up_locked_poll(&ctx->wqh, POLLOUT);
124
	}
D
Davide Libenzi 已提交
125
	spin_unlock_irq(&ctx->wqh.lock);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	if (res > 0 && put_user(ucnt, (__u64 __user *) buf))
		return -EFAULT;

	return res;
}

static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
			     loff_t *ppos)
{
	struct eventfd_ctx *ctx = file->private_data;
	ssize_t res;
	__u64 ucnt;
	DECLARE_WAITQUEUE(wait, current);

	if (count < sizeof(ucnt))
		return -EINVAL;
	if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
		return -EFAULT;
	if (ucnt == ULLONG_MAX)
		return -EINVAL;
D
Davide Libenzi 已提交
146
	spin_lock_irq(&ctx->wqh.lock);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	res = -EAGAIN;
	if (ULLONG_MAX - ctx->count > ucnt)
		res = sizeof(ucnt);
	else if (!(file->f_flags & O_NONBLOCK)) {
		__add_wait_queue(&ctx->wqh, &wait);
		for (res = 0;;) {
			set_current_state(TASK_INTERRUPTIBLE);
			if (ULLONG_MAX - ctx->count > ucnt) {
				res = sizeof(ucnt);
				break;
			}
			if (signal_pending(current)) {
				res = -ERESTARTSYS;
				break;
			}
D
Davide Libenzi 已提交
162
			spin_unlock_irq(&ctx->wqh.lock);
163
			schedule();
D
Davide Libenzi 已提交
164
			spin_lock_irq(&ctx->wqh.lock);
165 166 167 168
		}
		__remove_wait_queue(&ctx->wqh, &wait);
		__set_current_state(TASK_RUNNING);
	}
169
	if (likely(res > 0)) {
170 171
		ctx->count += ucnt;
		if (waitqueue_active(&ctx->wqh))
172
			wake_up_locked_poll(&ctx->wqh, POLLIN);
173
	}
D
Davide Libenzi 已提交
174
	spin_unlock_irq(&ctx->wqh.lock);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

	return res;
}

static const struct file_operations eventfd_fops = {
	.release	= eventfd_release,
	.poll		= eventfd_poll,
	.read		= eventfd_read,
	.write		= eventfd_write,
};

struct file *eventfd_fget(int fd)
{
	struct file *file;

	file = fget(fd);
	if (!file)
		return ERR_PTR(-EBADF);
	if (file->f_op != &eventfd_fops) {
		fput(file);
		return ERR_PTR(-EINVAL);
	}

	return file;
}

201
SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
202
{
A
Al Viro 已提交
203
	int fd;
204 205
	struct eventfd_ctx *ctx;

206 207 208 209
	/* Check the EFD_* constants for consistency.  */
	BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
	BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);

210
	if (flags & ~EFD_FLAGS_SET)
U
Ulrich Drepper 已提交
211 212
		return -EINVAL;

213 214 215 216 217 218
	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;

	init_waitqueue_head(&ctx->wqh);
	ctx->count = count;
219
	ctx->flags = flags;
220 221 222 223 224

	/*
	 * When we call this, the initialization must be complete, since
	 * anon_inode_getfd() will install the fd.
	 */
U
Ulrich Drepper 已提交
225
	fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
226
			      flags & EFD_SHARED_FCNTL_FLAGS);
A
Al Viro 已提交
227 228 229
	if (fd < 0)
		kfree(ctx);
	return fd;
230 231
}

232
SYSCALL_DEFINE1(eventfd, unsigned int, count)
U
Ulrich Drepper 已提交
233 234 235
{
	return sys_eventfd2(count, 0);
}
236