blob: b5c7ca584939acb628cd3f7644a99c3c53d0681d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/eventpoll.c ( Efficent event polling implementation )
Davide Libenzi3419b232006-06-25 05:48:14 -07003 * Copyright (C) 2001,...,2006 Davide Libenzi
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Davide Libenzi <davidel@xmailserver.org>
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/sched.h>
18#include <linux/fs.h>
19#include <linux/file.h>
20#include <linux/signal.h>
21#include <linux/errno.h>
22#include <linux/mm.h>
23#include <linux/slab.h>
24#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
26#include <linux/list.h>
27#include <linux/hash.h>
28#include <linux/spinlock.h>
29#include <linux/syscalls.h>
30#include <linux/rwsem.h>
31#include <linux/rbtree.h>
32#include <linux/wait.h>
33#include <linux/eventpoll.h>
34#include <linux/mount.h>
35#include <linux/bitops.h>
Arjan van de Ven144efe32006-03-23 03:00:32 -080036#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38#include <asm/system.h>
39#include <asm/io.h>
40#include <asm/mman.h>
41#include <asm/atomic.h>
42#include <asm/semaphore.h>
43
44
45/*
46 * LOCKING:
47 * There are three level of locking required by epoll :
48 *
Arjan van de Ven144efe32006-03-23 03:00:32 -080049 * 1) epmutex (mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * 2) ep->sem (rw_semaphore)
51 * 3) ep->lock (rw_lock)
52 *
53 * The acquire order is the one listed above, from 1 to 3.
54 * We need a spinlock (ep->lock) because we manipulate objects
55 * from inside the poll callback, that might be triggered from
56 * a wake_up() that in turn might be called from IRQ context.
57 * So we can't sleep inside the poll callback and hence we need
58 * a spinlock. During the event transfer loop (from kernel to
59 * user space) we could end up sleeping due a copy_to_user(), so
60 * we need a lock that will allow us to sleep. This lock is a
61 * read-write semaphore (ep->sem). It is acquired on read during
62 * the event transfer loop and in write during epoll_ctl(EPOLL_CTL_DEL)
63 * and during eventpoll_release_file(). Then we also need a global
64 * semaphore to serialize eventpoll_release_file() and ep_free().
65 * This semaphore is acquired by ep_free() during the epoll file
66 * cleanup path and it is also acquired by eventpoll_release_file()
67 * if a file has been pushed inside an epoll set and it is then
68 * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL).
69 * It is possible to drop the "ep->sem" and to use the global
Arjan van de Ven144efe32006-03-23 03:00:32 -080070 * semaphore "epmutex" (together with "ep->lock") to have it working,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * but having "ep->sem" will make the interface more scalable.
Arjan van de Ven144efe32006-03-23 03:00:32 -080072 * Events that require holding "epmutex" are very rare, while for
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * normal operations the epoll private "ep->sem" will guarantee
74 * a greater scalability.
75 */
76
77
78#define EVENTPOLLFS_MAGIC 0x03111965 /* My birthday should work for this :) */
79
80#define DEBUG_EPOLL 0
81
82#if DEBUG_EPOLL > 0
83#define DPRINTK(x) printk x
84#define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0)
85#else /* #if DEBUG_EPOLL > 0 */
86#define DPRINTK(x) (void) 0
87#define DNPRINTK(n, x) (void) 0
88#endif /* #if DEBUG_EPOLL > 0 */
89
90#define DEBUG_EPI 0
91
92#if DEBUG_EPI != 0
93#define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */)
94#else /* #if DEBUG_EPI != 0 */
95#define EPI_SLAB_DEBUG 0
96#endif /* #if DEBUG_EPI != 0 */
97
98/* Epoll private bits inside the event mask */
99#define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)
100
101/* Maximum number of poll wake up nests we are allowing */
102#define EP_MAX_POLLWAKE_NESTS 4
103
Davide Libenzie3306dd2005-09-27 21:45:33 -0700104/* Maximum msec timeout value storeable in a long int */
105#define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ)
106
Davide Libenzib6119672006-10-11 01:21:44 -0700107#define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
108
Davide Libenzie3306dd2005-09-27 21:45:33 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct epoll_filefd {
111 struct file *file;
112 int fd;
113};
114
115/*
116 * Node that is linked into the "wake_task_list" member of the "struct poll_safewake".
117 * It is used to keep track on all tasks that are currently inside the wake_up() code
118 * to 1) short-circuit the one coming from the same task and same wait queue head
119 * ( loop ) 2) allow a maximum number of epoll descriptors inclusion nesting
120 * 3) let go the ones coming from other tasks.
121 */
122struct wake_task_node {
123 struct list_head llink;
Ingo Molnar36c8b582006-07-03 00:25:41 -0700124 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 wait_queue_head_t *wq;
126};
127
128/*
129 * This is used to implement the safe poll wake up avoiding to reenter
130 * the poll callback from inside wake_up().
131 */
132struct poll_safewake {
133 struct list_head wake_task_list;
134 spinlock_t lock;
135};
136
137/*
138 * This structure is stored inside the "private_data" member of the file
139 * structure and rapresent the main data sructure for the eventpoll
140 * interface.
141 */
142struct eventpoll {
143 /* Protect the this structure access */
144 rwlock_t lock;
145
146 /*
147 * This semaphore is used to ensure that files are not removed
148 * while epoll is using them. This is read-held during the event
149 * collection loop and it is write-held during the file cleanup
150 * path, the epoll file exit code and the ctl operations.
151 */
152 struct rw_semaphore sem;
153
154 /* Wait queue used by sys_epoll_wait() */
155 wait_queue_head_t wq;
156
157 /* Wait queue used by file->poll() */
158 wait_queue_head_t poll_wait;
159
160 /* List of ready file descriptors */
161 struct list_head rdllist;
162
163 /* RB-Tree root used to store monitored fd structs */
164 struct rb_root rbr;
165};
166
167/* Wait structure used by the poll hooks */
168struct eppoll_entry {
169 /* List header used to link this structure to the "struct epitem" */
170 struct list_head llink;
171
172 /* The "base" pointer is set to the container "struct epitem" */
173 void *base;
174
175 /*
176 * Wait queue item that will be linked to the target file wait
177 * queue head.
178 */
179 wait_queue_t wait;
180
181 /* The wait queue head that linked the "wait" wait queue item */
182 wait_queue_head_t *whead;
183};
184
185/*
186 * Each file descriptor added to the eventpoll interface will
Davide Libenzi6192bd52007-05-08 00:25:41 -0700187 * have an entry of this type linked to the "rbr" RB tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189struct epitem {
190 /* RB-Tree node used to link this structure to the eventpoll rb-tree */
191 struct rb_node rbn;
192
193 /* List header used to link this structure to the eventpoll ready list */
194 struct list_head rdllink;
195
196 /* The file descriptor information this item refers to */
197 struct epoll_filefd ffd;
198
199 /* Number of active wait queue attached to poll operations */
200 int nwait;
201
202 /* List containing poll wait queues */
203 struct list_head pwqlist;
204
205 /* The "container" of this item */
206 struct eventpoll *ep;
207
208 /* The structure that describe the interested events and the source fd */
209 struct epoll_event event;
210
211 /*
212 * Used to keep track of the usage count of the structure. This avoids
213 * that the structure will desappear from underneath our processing.
214 */
215 atomic_t usecnt;
216
217 /* List header used to link this item to the "struct file" items list */
218 struct list_head fllink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
221/* Wrapper struct used by poll queueing */
222struct ep_pqueue {
223 poll_table pt;
224 struct epitem *epi;
225};
226
227
228
229static void ep_poll_safewake_init(struct poll_safewake *psw);
230static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq);
Davide Libenzi53d2be72005-09-16 19:28:06 -0700231static int ep_getfd(int *efd, struct inode **einode, struct file **efile,
232 struct eventpoll *ep);
233static int ep_alloc(struct eventpoll **pep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static void ep_free(struct eventpoll *ep);
235static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd);
236static void ep_use_epitem(struct epitem *epi);
237static void ep_release_epitem(struct epitem *epi);
238static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
239 poll_table *pt);
240static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi);
241static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
242 struct file *tfile, int fd);
243static int ep_modify(struct eventpoll *ep, struct epitem *epi,
244 struct epoll_event *event);
245static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi);
246static int ep_unlink(struct eventpoll *ep, struct epitem *epi);
247static int ep_remove(struct eventpoll *ep, struct epitem *epi);
248static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key);
249static int ep_eventpoll_close(struct inode *inode, struct file *file);
250static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
Davide Libenzi6192bd52007-05-08 00:25:41 -0700252 struct epoll_event __user *events, int maxevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int ep_events_transfer(struct eventpoll *ep,
254 struct epoll_event __user *events,
255 int maxevents);
256static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
257 int maxevents, long timeout);
258static int eventpollfs_delete_dentry(struct dentry *dentry);
259static struct inode *ep_eventpoll_inode(void);
David Howells454e2392006-06-23 02:02:57 -0700260static int eventpollfs_get_sb(struct file_system_type *fs_type,
261 int flags, const char *dev_name,
262 void *data, struct vfsmount *mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/*
265 * This semaphore is used to serialize ep_free() and eventpoll_release_file().
266 */
Arjan van de Ven144efe32006-03-23 03:00:32 -0800267static struct mutex epmutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269/* Safe wake up implementation */
270static struct poll_safewake psw;
271
272/* Slab cache used to allocate "struct epitem" */
Christoph Lametere18b8902006-12-06 20:33:20 -0800273static struct kmem_cache *epi_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275/* Slab cache used to allocate "struct eppoll_entry" */
Christoph Lametere18b8902006-12-06 20:33:20 -0800276static struct kmem_cache *pwq_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/* Virtual fs used to allocate inodes for eventpoll files */
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800279static struct vfsmount *eventpoll_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281/* File callbacks that implement the eventpoll file behaviour */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800282static const struct file_operations eventpoll_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 .release = ep_eventpoll_close,
284 .poll = ep_eventpoll_poll
285};
286
287/*
288 * This is used to register the virtual file system from where
289 * eventpoll inodes are allocated.
290 */
291static struct file_system_type eventpoll_fs_type = {
292 .name = "eventpollfs",
293 .get_sb = eventpollfs_get_sb,
294 .kill_sb = kill_anon_super,
295};
296
297/* Very basic directory entry operations for the eventpoll virtual file system */
298static struct dentry_operations eventpollfs_dentry_operations = {
299 .d_delete = eventpollfs_delete_dentry,
300};
301
302
303
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700304/* Fast test to see if the file is an evenpoll file */
305static inline int is_file_epoll(struct file *f)
306{
307 return f->f_op == &eventpoll_fops;
308}
309
310/* Setup the structure that is used as key for the rb-tree */
311static inline void ep_set_ffd(struct epoll_filefd *ffd,
312 struct file *file, int fd)
313{
314 ffd->file = file;
315 ffd->fd = fd;
316}
317
318/* Compare rb-tree keys */
319static inline int ep_cmp_ffd(struct epoll_filefd *p1,
320 struct epoll_filefd *p2)
321{
322 return (p1->file > p2->file ? +1:
323 (p1->file < p2->file ? -1 : p1->fd - p2->fd));
324}
325
326/* Special initialization for the rb-tree node to detect linkage */
327static inline void ep_rb_initnode(struct rb_node *n)
328{
David Woodhousec5698822006-04-21 13:17:24 +0100329 rb_set_parent(n, n);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700330}
331
332/* Removes a node from the rb-tree and marks it for a fast is-linked check */
333static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r)
334{
335 rb_erase(n, r);
David Woodhousec5698822006-04-21 13:17:24 +0100336 rb_set_parent(n, n);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700337}
338
339/* Fast check to verify that the item is linked to the main rb-tree */
340static inline int ep_rb_linked(struct rb_node *n)
341{
David Woodhousec5698822006-04-21 13:17:24 +0100342 return rb_parent(n) != n;
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700343}
344
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700345/* Tells us if the item is currently linked */
346static inline int ep_is_linked(struct list_head *p)
347{
348 return !list_empty(p);
349}
350
351/* Get the "struct epitem" from a wait queue pointer */
352static inline struct epitem * ep_item_from_wait(wait_queue_t *p)
353{
354 return container_of(p, struct eppoll_entry, wait)->base;
355}
356
357/* Get the "struct epitem" from an epoll queue wrapper */
358static inline struct epitem * ep_item_from_epqueue(poll_table *p)
359{
360 return container_of(p, struct ep_pqueue, pt)->epi;
361}
362
363/* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
Davide Libenzi6192bd52007-05-08 00:25:41 -0700364static inline int ep_op_has_event(int op)
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700365{
366 return op != EPOLL_CTL_DEL;
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/* Initialize the poll safe wake up structure */
370static void ep_poll_safewake_init(struct poll_safewake *psw)
371{
372
373 INIT_LIST_HEAD(&psw->wake_task_list);
374 spin_lock_init(&psw->lock);
375}
376
377
378/*
379 * Perform a safe wake up of the poll wait list. The problem is that
380 * with the new callback'd wake up system, it is possible that the
381 * poll callback is reentered from inside the call to wake_up() done
382 * on the poll wait queue head. The rule is that we cannot reenter the
383 * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times,
384 * and we cannot reenter the same wait queue head at all. This will
385 * enable to have a hierarchy of epoll file descriptor of no more than
386 * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock
387 * because this one gets called by the poll callback, that in turn is called
388 * from inside a wake_up(), that might be called from irq context.
389 */
390static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
391{
392 int wake_nests = 0;
393 unsigned long flags;
Ingo Molnar36c8b582006-07-03 00:25:41 -0700394 struct task_struct *this_task = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct list_head *lsthead = &psw->wake_task_list, *lnk;
396 struct wake_task_node *tncur;
397 struct wake_task_node tnode;
398
399 spin_lock_irqsave(&psw->lock, flags);
400
401 /* Try to see if the current task is already inside this wakeup call */
402 list_for_each(lnk, lsthead) {
403 tncur = list_entry(lnk, struct wake_task_node, llink);
404
405 if (tncur->wq == wq ||
406 (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) {
407 /*
408 * Ops ... loop detected or maximum nest level reached.
409 * We abort this wake by breaking the cycle itself.
410 */
411 spin_unlock_irqrestore(&psw->lock, flags);
412 return;
413 }
414 }
415
416 /* Add the current task to the list */
417 tnode.task = this_task;
418 tnode.wq = wq;
419 list_add(&tnode.llink, lsthead);
420
421 spin_unlock_irqrestore(&psw->lock, flags);
422
423 /* Do really wake up now */
424 wake_up(wq);
425
426 /* Remove the current task from the list */
427 spin_lock_irqsave(&psw->lock, flags);
428 list_del(&tnode.llink);
429 spin_unlock_irqrestore(&psw->lock, flags);
430}
431
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/*
434 * This is called from eventpoll_release() to unlink files from the eventpoll
435 * interface. We need to have this facility to cleanup correctly files that are
436 * closed without being removed from the eventpoll interface.
437 */
438void eventpoll_release_file(struct file *file)
439{
440 struct list_head *lsthead = &file->f_ep_links;
441 struct eventpoll *ep;
442 struct epitem *epi;
443
444 /*
445 * We don't want to get "file->f_ep_lock" because it is not
446 * necessary. It is not necessary because we're in the "struct file"
447 * cleanup path, and this means that noone is using this file anymore.
448 * The only hit might come from ep_free() but by holding the semaphore
449 * will correctly serialize the operation. We do need to acquire
Arjan van de Ven144efe32006-03-23 03:00:32 -0800450 * "ep->sem" after "epmutex" because ep_remove() requires it when called
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * from anywhere but ep_free().
452 */
Arjan van de Ven144efe32006-03-23 03:00:32 -0800453 mutex_lock(&epmutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 while (!list_empty(lsthead)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700456 epi = list_first_entry(lsthead, struct epitem, fllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 ep = epi->ep;
Davide Libenzi6192bd52007-05-08 00:25:41 -0700459 list_del_init(&epi->fllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 down_write(&ep->sem);
461 ep_remove(ep, epi);
462 up_write(&ep->sem);
463 }
464
Arjan van de Ven144efe32006-03-23 03:00:32 -0800465 mutex_unlock(&epmutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468
469/*
470 * It opens an eventpoll file descriptor by suggesting a storage of "size"
471 * file descriptors. The size parameter is just an hint about how to size
472 * data structures. It won't prevent the user to store more than "size"
473 * file descriptors inside the epoll interface. It is the kernel part of
474 * the userspace epoll_create(2).
475 */
476asmlinkage long sys_epoll_create(int size)
477{
Davide Libenzib6119672006-10-11 01:21:44 -0700478 int error, fd = -1;
Davide Libenzi53d2be72005-09-16 19:28:06 -0700479 struct eventpoll *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct inode *inode;
481 struct file *file;
482
483 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
484 current, size));
485
Davide Libenzi53d2be72005-09-16 19:28:06 -0700486 /*
487 * Sanity check on the size parameter, and create the internal data
488 * structure ( "struct eventpoll" ).
489 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 error = -EINVAL;
Davide Libenzi53d2be72005-09-16 19:28:06 -0700491 if (size <= 0 || (error = ep_alloc(&ep)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto eexit_1;
493
494 /*
495 * Creates all the items needed to setup an eventpoll file. That is,
496 * a file structure, and inode and a free file descriptor.
497 */
Davide Libenzi53d2be72005-09-16 19:28:06 -0700498 error = ep_getfd(&fd, &inode, &file, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (error)
500 goto eexit_2;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
503 current, size, fd));
504
505 return fd;
506
507eexit_2:
Davide Libenzi53d2be72005-09-16 19:28:06 -0700508 ep_free(ep);
509 kfree(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510eexit_1:
511 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
512 current, size, error));
513 return error;
514}
515
516
517/*
518 * The following function implements the controller interface for
519 * the eventpoll file that enables the insertion/removal/change of
520 * file descriptors inside the interest set. It represents
521 * the kernel part of the user space epoll_ctl(2).
522 */
523asmlinkage long
524sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event)
525{
526 int error;
527 struct file *file, *tfile;
528 struct eventpoll *ep;
529 struct epitem *epi;
530 struct epoll_event epds;
531
532 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n",
533 current, epfd, op, fd, event));
534
535 error = -EFAULT;
Davide Libenzi6192bd52007-05-08 00:25:41 -0700536 if (ep_op_has_event(op) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 copy_from_user(&epds, event, sizeof(struct epoll_event)))
538 goto eexit_1;
539
540 /* Get the "struct file *" for the eventpoll file */
541 error = -EBADF;
542 file = fget(epfd);
543 if (!file)
544 goto eexit_1;
545
546 /* Get the "struct file *" for the target file */
547 tfile = fget(fd);
548 if (!tfile)
549 goto eexit_2;
550
551 /* The target file descriptor must support poll */
552 error = -EPERM;
553 if (!tfile->f_op || !tfile->f_op->poll)
554 goto eexit_3;
555
556 /*
557 * We have to check that the file structure underneath the file descriptor
558 * the user passed to us _is_ an eventpoll file. And also we do not permit
559 * adding an epoll file descriptor inside itself.
560 */
561 error = -EINVAL;
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700562 if (file == tfile || !is_file_epoll(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto eexit_3;
564
565 /*
566 * At this point it is safe to assume that the "private_data" contains
567 * our own data structure.
568 */
569 ep = file->private_data;
570
571 down_write(&ep->sem);
572
Davide Libenzi6192bd52007-05-08 00:25:41 -0700573 /* Try to lookup the file inside our RB tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 epi = ep_find(ep, tfile, fd);
575
576 error = -EINVAL;
577 switch (op) {
578 case EPOLL_CTL_ADD:
579 if (!epi) {
Davide Libenzi23951402006-04-10 22:54:12 -0700580 epds.events |= POLLERR | POLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 error = ep_insert(ep, &epds, tfile, fd);
583 } else
584 error = -EEXIST;
585 break;
586 case EPOLL_CTL_DEL:
587 if (epi)
588 error = ep_remove(ep, epi);
589 else
590 error = -ENOENT;
591 break;
592 case EPOLL_CTL_MOD:
593 if (epi) {
Davide Libenzi23951402006-04-10 22:54:12 -0700594 epds.events |= POLLERR | POLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 error = ep_modify(ep, epi, &epds);
596 } else
597 error = -ENOENT;
598 break;
599 }
600
601 /*
602 * The function ep_find() increments the usage count of the structure
603 * so, if this is not NULL, we need to release it.
604 */
605 if (epi)
606 ep_release_epitem(epi);
607
608 up_write(&ep->sem);
609
610eexit_3:
611 fput(tfile);
612eexit_2:
613 fput(file);
614eexit_1:
615 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n",
616 current, epfd, op, fd, event, error));
617
618 return error;
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/*
623 * Implement the event wait interface for the eventpoll file. It is the kernel
624 * part of the user space epoll_wait(2).
625 */
626asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
627 int maxevents, int timeout)
628{
629 int error;
630 struct file *file;
631 struct eventpoll *ep;
632
633 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n",
634 current, epfd, events, maxevents, timeout));
635
636 /* The maximum number of event must be greater than zero */
Davide Libenzib6119672006-10-11 01:21:44 -0700637 if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -EINVAL;
639
640 /* Verify that the area passed by the user is writeable */
641 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
642 error = -EFAULT;
643 goto eexit_1;
644 }
645
646 /* Get the "struct file *" for the eventpoll file */
647 error = -EBADF;
648 file = fget(epfd);
649 if (!file)
650 goto eexit_1;
651
652 /*
653 * We have to check that the file structure underneath the fd
654 * the user passed to us _is_ an eventpoll file.
655 */
656 error = -EINVAL;
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700657 if (!is_file_epoll(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto eexit_2;
659
660 /*
661 * At this point it is safe to assume that the "private_data" contains
662 * our own data structure.
663 */
664 ep = file->private_data;
665
666 /* Time to fish for events ... */
667 error = ep_poll(ep, events, maxevents, timeout);
668
669eexit_2:
670 fput(file);
671eexit_1:
672 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n",
673 current, epfd, events, maxevents, timeout, error));
674
675 return error;
676}
677
678
Davide Libenzib6119672006-10-11 01:21:44 -0700679#ifdef TIF_RESTORE_SIGMASK
680
681/*
682 * Implement the event wait interface for the eventpoll file. It is the kernel
683 * part of the user space epoll_pwait(2).
684 */
685asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events,
686 int maxevents, int timeout, const sigset_t __user *sigmask,
687 size_t sigsetsize)
688{
689 int error;
690 sigset_t ksigmask, sigsaved;
691
692 /*
693 * If the caller wants a certain signal mask to be set during the wait,
694 * we apply it here.
695 */
696 if (sigmask) {
697 if (sigsetsize != sizeof(sigset_t))
698 return -EINVAL;
699 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
700 return -EFAULT;
701 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
702 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
703 }
704
705 error = sys_epoll_wait(epfd, events, maxevents, timeout);
706
707 /*
708 * If we changed the signal mask, we need to restore the original one.
709 * In case we've got a signal while waiting, we do not restore the
710 * signal mask yet, and we allow do_signal() to deliver the signal on
711 * the way back to userspace, before the signal mask is restored.
712 */
713 if (sigmask) {
714 if (error == -EINTR) {
715 memcpy(&current->saved_sigmask, &sigsaved,
716 sizeof(sigsaved));
717 set_thread_flag(TIF_RESTORE_SIGMASK);
718 } else
719 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
720 }
721
722 return error;
723}
724
725#endif /* #ifdef TIF_RESTORE_SIGMASK */
726
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/*
729 * Creates the file descriptor to be used by the epoll interface.
730 */
Davide Libenzi53d2be72005-09-16 19:28:06 -0700731static int ep_getfd(int *efd, struct inode **einode, struct file **efile,
732 struct eventpoll *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct qstr this;
735 char name[32];
736 struct dentry *dentry;
737 struct inode *inode;
738 struct file *file;
739 int error, fd;
740
741 /* Get an ready to use file */
742 error = -ENFILE;
743 file = get_empty_filp();
744 if (!file)
745 goto eexit_1;
746
747 /* Allocates an inode from the eventpoll file system */
748 inode = ep_eventpoll_inode();
Jeff Garzikc3b65712006-10-03 01:13:52 -0700749 if (IS_ERR(inode)) {
750 error = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto eexit_2;
Jeff Garzikc3b65712006-10-03 01:13:52 -0700752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Allocates a free descriptor to plug the file onto */
755 error = get_unused_fd();
756 if (error < 0)
757 goto eexit_3;
758 fd = error;
759
760 /*
761 * Link the inode to a directory entry by creating a unique name
762 * using the inode number.
763 */
764 error = -ENOMEM;
765 sprintf(name, "[%lu]", inode->i_ino);
766 this.name = name;
767 this.len = strlen(name);
768 this.hash = inode->i_ino;
769 dentry = d_alloc(eventpoll_mnt->mnt_sb->s_root, &this);
770 if (!dentry)
771 goto eexit_4;
772 dentry->d_op = &eventpollfs_dentry_operations;
773 d_add(dentry, inode);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800774 file->f_path.mnt = mntget(eventpoll_mnt);
775 file->f_path.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 file->f_mapping = inode->i_mapping;
777
778 file->f_pos = 0;
779 file->f_flags = O_RDONLY;
780 file->f_op = &eventpoll_fops;
781 file->f_mode = FMODE_READ;
782 file->f_version = 0;
Davide Libenzi53d2be72005-09-16 19:28:06 -0700783 file->private_data = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* Install the new setup file into the allocated fd. */
786 fd_install(fd, file);
787
788 *efd = fd;
789 *einode = inode;
790 *efile = file;
791 return 0;
792
793eexit_4:
794 put_unused_fd(fd);
795eexit_3:
796 iput(inode);
797eexit_2:
798 put_filp(file);
799eexit_1:
800 return error;
801}
802
803
Davide Libenzi53d2be72005-09-16 19:28:06 -0700804static int ep_alloc(struct eventpoll **pep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Davide Libenzi53d2be72005-09-16 19:28:06 -0700806 struct eventpoll *ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Davide Libenzi53d2be72005-09-16 19:28:06 -0700808 if (!ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return -ENOMEM;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 rwlock_init(&ep->lock);
812 init_rwsem(&ep->sem);
813 init_waitqueue_head(&ep->wq);
814 init_waitqueue_head(&ep->poll_wait);
815 INIT_LIST_HEAD(&ep->rdllist);
816 ep->rbr = RB_ROOT;
817
Davide Libenzi53d2be72005-09-16 19:28:06 -0700818 *pep = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Davide Libenzi53d2be72005-09-16 19:28:06 -0700820 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 current, ep));
822 return 0;
823}
824
825
826static void ep_free(struct eventpoll *ep)
827{
828 struct rb_node *rbp;
829 struct epitem *epi;
830
831 /* We need to release all tasks waiting for these file */
832 if (waitqueue_active(&ep->poll_wait))
833 ep_poll_safewake(&psw, &ep->poll_wait);
834
835 /*
836 * We need to lock this because we could be hit by
837 * eventpoll_release_file() while we're freeing the "struct eventpoll".
838 * We do not need to hold "ep->sem" here because the epoll file
839 * is on the way to be removed and no one has references to it
840 * anymore. The only hit might come from eventpoll_release_file() but
Arjan van de Ven144efe32006-03-23 03:00:32 -0800841 * holding "epmutex" is sufficent here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
Arjan van de Ven144efe32006-03-23 03:00:32 -0800843 mutex_lock(&epmutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /*
846 * Walks through the whole tree by unregistering poll callbacks.
847 */
848 for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
849 epi = rb_entry(rbp, struct epitem, rbn);
850
851 ep_unregister_pollwait(ep, epi);
852 }
853
854 /*
Davide Libenzi6192bd52007-05-08 00:25:41 -0700855 * Walks through the whole tree by freeing each "struct epitem". At this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * point we are sure no poll callbacks will be lingering around, and also by
857 * write-holding "sem" we can be sure that no file cleanup code will hit
858 * us during this operation. So we can avoid the lock on "ep->lock".
859 */
860 while ((rbp = rb_first(&ep->rbr)) != 0) {
861 epi = rb_entry(rbp, struct epitem, rbn);
862 ep_remove(ep, epi);
863 }
864
Arjan van de Ven144efe32006-03-23 03:00:32 -0800865 mutex_unlock(&epmutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868
869/*
Davide Libenzi6192bd52007-05-08 00:25:41 -0700870 * Search the file inside the eventpoll tree. It add usage count to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 * the returned item, so the caller must call ep_release_epitem()
872 * after finished using the "struct epitem".
873 */
874static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
875{
876 int kcmp;
877 unsigned long flags;
878 struct rb_node *rbp;
879 struct epitem *epi, *epir = NULL;
880 struct epoll_filefd ffd;
881
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700882 ep_set_ffd(&ffd, file, fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 read_lock_irqsave(&ep->lock, flags);
884 for (rbp = ep->rbr.rb_node; rbp; ) {
885 epi = rb_entry(rbp, struct epitem, rbn);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700886 kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (kcmp > 0)
888 rbp = rbp->rb_right;
889 else if (kcmp < 0)
890 rbp = rbp->rb_left;
891 else {
892 ep_use_epitem(epi);
893 epir = epi;
894 break;
895 }
896 }
897 read_unlock_irqrestore(&ep->lock, flags);
898
899 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
900 current, file, epir));
901
902 return epir;
903}
904
905
906/*
907 * Increment the usage count of the "struct epitem" making it sure
908 * that the user will have a valid pointer to reference.
909 */
910static void ep_use_epitem(struct epitem *epi)
911{
912
913 atomic_inc(&epi->usecnt);
914}
915
916
917/*
918 * Decrement ( release ) the usage count by signaling that the user
919 * has finished using the structure. It might lead to freeing the
920 * structure itself if the count goes to zero.
921 */
922static void ep_release_epitem(struct epitem *epi)
923{
924
925 if (atomic_dec_and_test(&epi->usecnt))
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700926 kmem_cache_free(epi_cache, epi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929
930/*
931 * This is the callback that is used to add our wait queue to the
932 * target file wakeup lists.
933 */
934static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
935 poll_table *pt)
936{
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700937 struct epitem *epi = ep_item_from_epqueue(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct eppoll_entry *pwq;
939
Christoph Lametere94b1762006-12-06 20:33:17 -0800940 if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
942 pwq->whead = whead;
943 pwq->base = epi;
944 add_wait_queue(whead, &pwq->wait);
945 list_add_tail(&pwq->llink, &epi->pwqlist);
946 epi->nwait++;
947 } else {
948 /* We have to signal that an error occurred */
949 epi->nwait = -1;
950 }
951}
952
953
954static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
955{
956 int kcmp;
957 struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
958 struct epitem *epic;
959
960 while (*p) {
961 parent = *p;
962 epic = rb_entry(parent, struct epitem, rbn);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700963 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (kcmp > 0)
965 p = &parent->rb_right;
966 else
967 p = &parent->rb_left;
968 }
969 rb_link_node(&epi->rbn, parent, p);
970 rb_insert_color(&epi->rbn, &ep->rbr);
971}
972
973
974static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
975 struct file *tfile, int fd)
976{
977 int error, revents, pwake = 0;
978 unsigned long flags;
979 struct epitem *epi;
980 struct ep_pqueue epq;
981
982 error = -ENOMEM;
Christoph Lametere94b1762006-12-06 20:33:17 -0800983 if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto eexit_1;
985
986 /* Item initialization follow here ... */
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700987 ep_rb_initnode(&epi->rbn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 INIT_LIST_HEAD(&epi->rdllink);
989 INIT_LIST_HEAD(&epi->fllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 INIT_LIST_HEAD(&epi->pwqlist);
991 epi->ep = ep;
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700992 ep_set_ffd(&epi->ffd, tfile, fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 epi->event = *event;
994 atomic_set(&epi->usecnt, 1);
995 epi->nwait = 0;
996
997 /* Initialize the poll table using the queue callback */
998 epq.epi = epi;
999 init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
1000
1001 /*
1002 * Attach the item to the poll hooks and get current event bits.
1003 * We can safely use the file* here because its usage count has
1004 * been increased by the caller of this function.
1005 */
1006 revents = tfile->f_op->poll(tfile, &epq.pt);
1007
1008 /*
1009 * We have to check if something went wrong during the poll wait queue
1010 * install process. Namely an allocation for a wait queue failed due
1011 * high memory pressure.
1012 */
1013 if (epi->nwait < 0)
1014 goto eexit_2;
1015
1016 /* Add the current item to the list of active epoll hook for this file */
1017 spin_lock(&tfile->f_ep_lock);
1018 list_add_tail(&epi->fllink, &tfile->f_ep_links);
1019 spin_unlock(&tfile->f_ep_lock);
1020
1021 /* We have to drop the new item inside our item list to keep track of it */
1022 write_lock_irqsave(&ep->lock, flags);
1023
1024 /* Add the current item to the rb-tree */
1025 ep_rbtree_insert(ep, epi);
1026
1027 /* If the file is already "ready" we drop it inside the ready list */
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001028 if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 list_add_tail(&epi->rdllink, &ep->rdllist);
1030
1031 /* Notify waiting tasks that events are available */
1032 if (waitqueue_active(&ep->wq))
Davide Libenzi3419b232006-06-25 05:48:14 -07001033 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (waitqueue_active(&ep->poll_wait))
1035 pwake++;
1036 }
1037
1038 write_unlock_irqrestore(&ep->lock, flags);
1039
1040 /* We have to call this outside the lock */
1041 if (pwake)
1042 ep_poll_safewake(&psw, &ep->poll_wait);
1043
1044 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n",
1045 current, ep, tfile, fd));
1046
1047 return 0;
1048
1049eexit_2:
1050 ep_unregister_pollwait(ep, epi);
1051
1052 /*
1053 * We need to do this because an event could have been arrived on some
1054 * allocated wait queue.
1055 */
1056 write_lock_irqsave(&ep->lock, flags);
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001057 if (ep_is_linked(&epi->rdllink))
Davide Libenzi6192bd52007-05-08 00:25:41 -07001058 list_del_init(&epi->rdllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 write_unlock_irqrestore(&ep->lock, flags);
1060
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001061 kmem_cache_free(epi_cache, epi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062eexit_1:
1063 return error;
1064}
1065
1066
1067/*
1068 * Modify the interest event mask by dropping an event if the new mask
1069 * has a match in the current file status.
1070 */
1071static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
1072{
1073 int pwake = 0;
1074 unsigned int revents;
1075 unsigned long flags;
1076
1077 /*
1078 * Set the new event interest mask before calling f_op->poll(), otherwise
1079 * a potential race might occur. In fact if we do this operation inside
1080 * the lock, an event might happen between the f_op->poll() call and the
1081 * new event set registering.
1082 */
1083 epi->event.events = event->events;
1084
1085 /*
1086 * Get current event bits. We can safely use the file* here because
1087 * its usage count has been increased by the caller of this function.
1088 */
1089 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
1090
1091 write_lock_irqsave(&ep->lock, flags);
1092
1093 /* Copy the data member from inside the lock */
1094 epi->event.data = event->data;
1095
1096 /*
Davide Libenzi6192bd52007-05-08 00:25:41 -07001097 * If the item is not linked to the RB tree it means that it's on its
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * way toward the removal. Do nothing in this case.
1099 */
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001100 if (ep_rb_linked(&epi->rbn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * If the item is "hot" and it is not registered inside the ready
1103 * list, push it inside. If the item is not "hot" and it is currently
1104 * registered inside the ready list, unlink it.
1105 */
1106 if (revents & event->events) {
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001107 if (!ep_is_linked(&epi->rdllink)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 list_add_tail(&epi->rdllink, &ep->rdllist);
1109
1110 /* Notify waiting tasks that events are available */
1111 if (waitqueue_active(&ep->wq))
Davide Libenzi3419b232006-06-25 05:48:14 -07001112 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1113 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (waitqueue_active(&ep->poll_wait))
1115 pwake++;
1116 }
1117 }
1118 }
1119
1120 write_unlock_irqrestore(&ep->lock, flags);
1121
1122 /* We have to call this outside the lock */
1123 if (pwake)
1124 ep_poll_safewake(&psw, &ep->poll_wait);
1125
1126 return 0;
1127}
1128
1129
1130/*
1131 * This function unregister poll callbacks from the associated file descriptor.
1132 * Since this must be called without holding "ep->lock" the atomic exchange trick
1133 * will protect us from multiple unregister.
1134 */
1135static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
1136{
1137 int nwait;
1138 struct list_head *lsthead = &epi->pwqlist;
1139 struct eppoll_entry *pwq;
1140
1141 /* This is called without locks, so we need the atomic exchange */
1142 nwait = xchg(&epi->nwait, 0);
1143
1144 if (nwait) {
1145 while (!list_empty(lsthead)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001146 pwq = list_first_entry(lsthead, struct eppoll_entry, llink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Davide Libenzi6192bd52007-05-08 00:25:41 -07001148 list_del_init(&pwq->llink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 remove_wait_queue(pwq->whead, &pwq->wait);
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001150 kmem_cache_free(pwq_cache, pwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 }
1153}
1154
1155
1156/*
1157 * Unlink the "struct epitem" from all places it might have been hooked up.
1158 * This function must be called with write IRQ lock on "ep->lock".
1159 */
1160static int ep_unlink(struct eventpoll *ep, struct epitem *epi)
1161{
1162 int error;
1163
1164 /*
1165 * It can happen that this one is called for an item already unlinked.
1166 * The check protect us from doing a double unlink ( crash ).
1167 */
1168 error = -ENOENT;
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001169 if (!ep_rb_linked(&epi->rbn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 goto eexit_1;
1171
1172 /*
1173 * Clear the event mask for the unlinked item. This will avoid item
1174 * notifications to be sent after the unlink operation from inside
1175 * the kernel->userspace event transfer loop.
1176 */
1177 epi->event.events = 0;
1178
1179 /*
1180 * At this point is safe to do the job, unlink the item from our rb-tree.
1181 * This operation togheter with the above check closes the door to
1182 * double unlinks.
1183 */
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001184 ep_rb_erase(&epi->rbn, &ep->rbr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 /*
1187 * If the item we are going to remove is inside the ready file descriptors
1188 * we want to remove it from this list to avoid stale events.
1189 */
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001190 if (ep_is_linked(&epi->rdllink))
Davide Libenzi6192bd52007-05-08 00:25:41 -07001191 list_del_init(&epi->rdllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 error = 0;
1194eexit_1:
1195
1196 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n",
Masoud Asgharifard Sharbiani45f17e02006-08-27 01:23:48 -07001197 current, ep, epi->ffd.file, error));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 return error;
1200}
1201
1202
1203/*
Davide Libenzi6192bd52007-05-08 00:25:41 -07001204 * Removes a "struct epitem" from the eventpoll RB tree and deallocates
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * all the associated resources.
1206 */
1207static int ep_remove(struct eventpoll *ep, struct epitem *epi)
1208{
1209 int error;
1210 unsigned long flags;
1211 struct file *file = epi->ffd.file;
1212
1213 /*
1214 * Removes poll wait queue hooks. We _have_ to do this without holding
1215 * the "ep->lock" otherwise a deadlock might occur. This because of the
1216 * sequence of the lock acquisition. Here we do "ep->lock" then the wait
1217 * queue head lock when unregistering the wait queue. The wakeup callback
1218 * will run by holding the wait queue head lock and will call our callback
1219 * that will try to get "ep->lock".
1220 */
1221 ep_unregister_pollwait(ep, epi);
1222
1223 /* Remove the current item from the list of epoll hooks */
1224 spin_lock(&file->f_ep_lock);
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001225 if (ep_is_linked(&epi->fllink))
Davide Libenzi6192bd52007-05-08 00:25:41 -07001226 list_del_init(&epi->fllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 spin_unlock(&file->f_ep_lock);
1228
1229 /* We need to acquire the write IRQ lock before calling ep_unlink() */
1230 write_lock_irqsave(&ep->lock, flags);
1231
Davide Libenzi6192bd52007-05-08 00:25:41 -07001232 /* Really unlink the item from the RB tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 error = ep_unlink(ep, epi);
1234
1235 write_unlock_irqrestore(&ep->lock, flags);
1236
1237 if (error)
1238 goto eexit_1;
1239
1240 /* At this point it is safe to free the eventpoll item */
1241 ep_release_epitem(epi);
1242
1243 error = 0;
1244eexit_1:
1245 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n",
1246 current, ep, file, error));
1247
1248 return error;
1249}
1250
1251
1252/*
1253 * This is the callback that is passed to the wait queue wakeup
1254 * machanism. It is called by the stored file descriptors when they
1255 * have events to report.
1256 */
1257static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
1258{
1259 int pwake = 0;
1260 unsigned long flags;
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001261 struct epitem *epi = ep_item_from_wait(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 struct eventpoll *ep = epi->ep;
1263
1264 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
Masoud Asgharifard Sharbiani45f17e02006-08-27 01:23:48 -07001265 current, epi->ffd.file, epi, ep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 write_lock_irqsave(&ep->lock, flags);
1268
1269 /*
1270 * If the event mask does not contain any poll(2) event, we consider the
1271 * descriptor to be disabled. This condition is likely the effect of the
1272 * EPOLLONESHOT bit that disables the descriptor when an event is received,
1273 * until the next EPOLL_CTL_MOD will be issued.
1274 */
1275 if (!(epi->event.events & ~EP_PRIVATE_BITS))
1276 goto is_disabled;
1277
1278 /* If this file is already in the ready list we exit soon */
Pekka Enbergb030a4d2005-06-23 00:10:03 -07001279 if (ep_is_linked(&epi->rdllink))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto is_linked;
1281
1282 list_add_tail(&epi->rdllink, &ep->rdllist);
1283
1284is_linked:
1285 /*
1286 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
1287 * wait list.
1288 */
1289 if (waitqueue_active(&ep->wq))
Davide Libenzi3419b232006-06-25 05:48:14 -07001290 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1291 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (waitqueue_active(&ep->poll_wait))
1293 pwake++;
1294
1295is_disabled:
1296 write_unlock_irqrestore(&ep->lock, flags);
1297
1298 /* We have to call this outside the lock */
1299 if (pwake)
1300 ep_poll_safewake(&psw, &ep->poll_wait);
1301
1302 return 1;
1303}
1304
1305
1306static int ep_eventpoll_close(struct inode *inode, struct file *file)
1307{
1308 struct eventpoll *ep = file->private_data;
1309
1310 if (ep) {
1311 ep_free(ep);
1312 kfree(ep);
1313 }
1314
1315 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep));
1316 return 0;
1317}
1318
1319
1320static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
1321{
1322 unsigned int pollflags = 0;
1323 unsigned long flags;
1324 struct eventpoll *ep = file->private_data;
1325
1326 /* Insert inside our poll wait queue */
1327 poll_wait(file, &ep->poll_wait, wait);
1328
1329 /* Check our condition */
1330 read_lock_irqsave(&ep->lock, flags);
1331 if (!list_empty(&ep->rdllist))
1332 pollflags = POLLIN | POLLRDNORM;
1333 read_unlock_irqrestore(&ep->lock, flags);
1334
1335 return pollflags;
1336}
1337
1338
1339/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 * This function is called without holding the "ep->lock" since the call to
1341 * __copy_to_user() might sleep, and also f_op->poll() might reenable the IRQ
1342 * because of the way poll() is traditionally implemented in Linux.
1343 */
1344static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
Davide Libenzi6192bd52007-05-08 00:25:41 -07001345 struct epoll_event __user *events, int maxevents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Davide Libenzi6192bd52007-05-08 00:25:41 -07001347 int eventcnt, error = -EFAULT, pwake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 unsigned int revents;
Davide Libenzi6192bd52007-05-08 00:25:41 -07001349 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct epitem *epi;
Davide Libenzi6192bd52007-05-08 00:25:41 -07001351 struct list_head injlist;
1352
1353 INIT_LIST_HEAD(&injlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /*
1356 * We can loop without lock because this is a task private list.
Davide Libenzi6192bd52007-05-08 00:25:41 -07001357 * We just splice'd out the ep->rdllist in ep_collect_ready_items().
1358 * Items cannot vanish during the loop because we are holding "sem" in
1359 * read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
Davide Libenzi6192bd52007-05-08 00:25:41 -07001361 for (eventcnt = 0; !list_empty(txlist) && eventcnt < maxevents;) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001362 epi = list_first_entry(txlist, struct epitem, rdllink);
Davide Libenzi6192bd52007-05-08 00:25:41 -07001363 prefetch(epi->rdllink.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /*
1366 * Get the ready file event set. We can safely use the file
1367 * because we are holding the "sem" in read and this will
1368 * guarantee that both the file and the item will not vanish.
1369 */
1370 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
Davide Libenzi6192bd52007-05-08 00:25:41 -07001371 revents &= epi->event.events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /*
Davide Libenzi6192bd52007-05-08 00:25:41 -07001374 * Is the event mask intersect the caller-requested one,
1375 * deliver the event to userspace. Again, we are holding
1376 * "sem" in read, so no operations coming from userspace
1377 * can change the item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
Davide Libenzi6192bd52007-05-08 00:25:41 -07001379 if (revents) {
1380 if (__put_user(revents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 &events[eventcnt].events) ||
1382 __put_user(epi->event.data,
1383 &events[eventcnt].data))
Davide Libenzi6192bd52007-05-08 00:25:41 -07001384 goto errxit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (epi->event.events & EPOLLONESHOT)
1386 epi->event.events &= EP_PRIVATE_BITS;
1387 eventcnt++;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 /*
Davide Libenzi6192bd52007-05-08 00:25:41 -07001391 * This is tricky. We are holding the "sem" in read, and this
1392 * means that the operations that can change the "linked" status
1393 * of the epoll item (epi->rbn and epi->rdllink), cannot touch
1394 * them. Also, since we are "linked" from a epi->rdllink POV
1395 * (the item is linked to our transmission list we just
1396 * spliced), the ep_poll_callback() cannot touch us either,
1397 * because of the check present in there. Another parallel
1398 * epoll_wait() will not get the same result set, since we
1399 * spliced the ready list before. Note that list_del() still
1400 * shows the item as linked to the test in ep_poll_callback().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
Davide Libenzi6192bd52007-05-08 00:25:41 -07001402 list_del(&epi->rdllink);
1403 if (!(epi->event.events & EPOLLET) &&
1404 (revents & epi->event.events))
1405 list_add_tail(&epi->rdllink, &injlist);
1406 else {
1407 /*
1408 * Be sure the item is totally detached before re-init
1409 * the list_head. After INIT_LIST_HEAD() is committed,
1410 * the ep_poll_callback() can requeue the item again,
1411 * but we don't care since we are already past it.
1412 */
1413 smp_mb();
1414 INIT_LIST_HEAD(&epi->rdllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416 }
Davide Libenzi6192bd52007-05-08 00:25:41 -07001417 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Davide Libenzi6192bd52007-05-08 00:25:41 -07001419 errxit:
1420
1421 /*
1422 * If the re-injection list or the txlist are not empty, re-splice
1423 * them to the ready list and do proper wakeups.
1424 */
1425 if (!list_empty(&injlist) || !list_empty(txlist)) {
1426 write_lock_irqsave(&ep->lock, flags);
1427
1428 list_splice(txlist, &ep->rdllist);
1429 list_splice(&injlist, &ep->rdllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /*
1431 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
1432 * wait list.
1433 */
1434 if (waitqueue_active(&ep->wq))
Davide Libenzi3419b232006-06-25 05:48:14 -07001435 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1436 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (waitqueue_active(&ep->poll_wait))
1438 pwake++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Davide Libenzi6192bd52007-05-08 00:25:41 -07001440 write_unlock_irqrestore(&ep->lock, flags);
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /* We have to call this outside the lock */
1444 if (pwake)
1445 ep_poll_safewake(&psw, &ep->poll_wait);
Davide Libenzi6192bd52007-05-08 00:25:41 -07001446
1447 return eventcnt == 0 ? error: eventcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
1450
1451/*
1452 * Perform the transfer of events to user space.
1453 */
1454static int ep_events_transfer(struct eventpoll *ep,
1455 struct epoll_event __user *events, int maxevents)
1456{
Davide Libenzi6192bd52007-05-08 00:25:41 -07001457 int eventcnt;
1458 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 struct list_head txlist;
1460
1461 INIT_LIST_HEAD(&txlist);
1462
1463 /*
1464 * We need to lock this because we could be hit by
1465 * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
1466 */
1467 down_read(&ep->sem);
1468
Davide Libenzi6192bd52007-05-08 00:25:41 -07001469 /*
1470 * Steal the ready list, and re-init the original one to the
1471 * empty list.
1472 */
1473 write_lock_irqsave(&ep->lock, flags);
1474 list_splice(&ep->rdllist, &txlist);
1475 INIT_LIST_HEAD(&ep->rdllist);
1476 write_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Davide Libenzi6192bd52007-05-08 00:25:41 -07001478 /* Build result set in userspace */
1479 eventcnt = ep_send_events(ep, &txlist, events, maxevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 up_read(&ep->sem);
1482
1483 return eventcnt;
1484}
1485
1486
1487static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
1488 int maxevents, long timeout)
1489{
1490 int res, eavail;
1491 unsigned long flags;
1492 long jtimeout;
1493 wait_queue_t wait;
1494
1495 /*
1496 * Calculate the timeout by checking for the "infinite" value ( -1 )
1497 * and the overflow condition. The passed timeout is in milliseconds,
1498 * that why (t * HZ) / 1000.
1499 */
Davide Libenzie3306dd2005-09-27 21:45:33 -07001500 jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?
1501 MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503retry:
1504 write_lock_irqsave(&ep->lock, flags);
1505
1506 res = 0;
1507 if (list_empty(&ep->rdllist)) {
1508 /*
1509 * We don't have any available event to return to the caller.
1510 * We need to sleep here, and we will be wake up by
1511 * ep_poll_callback() when events will become available.
1512 */
1513 init_waitqueue_entry(&wait, current);
Davide Libenzi3419b232006-06-25 05:48:14 -07001514 __add_wait_queue(&ep->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 for (;;) {
1517 /*
1518 * We don't want to sleep if the ep_poll_callback() sends us
1519 * a wakeup in between. That's why we set the task state
1520 * to TASK_INTERRUPTIBLE before doing the checks.
1521 */
1522 set_current_state(TASK_INTERRUPTIBLE);
1523 if (!list_empty(&ep->rdllist) || !jtimeout)
1524 break;
1525 if (signal_pending(current)) {
1526 res = -EINTR;
1527 break;
1528 }
1529
1530 write_unlock_irqrestore(&ep->lock, flags);
1531 jtimeout = schedule_timeout(jtimeout);
1532 write_lock_irqsave(&ep->lock, flags);
1533 }
Davide Libenzi3419b232006-06-25 05:48:14 -07001534 __remove_wait_queue(&ep->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 set_current_state(TASK_RUNNING);
1537 }
1538
1539 /* Is it worth to try to dig for events ? */
1540 eavail = !list_empty(&ep->rdllist);
1541
1542 write_unlock_irqrestore(&ep->lock, flags);
1543
1544 /*
1545 * Try to transfer events to user space. In case we get 0 events and
1546 * there's still timeout left over, we go trying again in search of
1547 * more luck.
1548 */
1549 if (!res && eavail &&
1550 !(res = ep_events_transfer(ep, events, maxevents)) && jtimeout)
1551 goto retry;
1552
1553 return res;
1554}
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556static int eventpollfs_delete_dentry(struct dentry *dentry)
1557{
1558
1559 return 1;
1560}
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562static struct inode *ep_eventpoll_inode(void)
1563{
1564 int error = -ENOMEM;
1565 struct inode *inode = new_inode(eventpoll_mnt->mnt_sb);
1566
1567 if (!inode)
1568 goto eexit_1;
1569
1570 inode->i_fop = &eventpoll_fops;
1571
1572 /*
1573 * Mark the inode dirty from the very beginning,
1574 * that way it will never be moved to the dirty
1575 * list because mark_inode_dirty() will think
1576 * that it already _is_ on the dirty list.
1577 */
1578 inode->i_state = I_DIRTY;
1579 inode->i_mode = S_IRUSR | S_IWUSR;
1580 inode->i_uid = current->fsuid;
1581 inode->i_gid = current->fsgid;
1582 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return inode;
1584
1585eexit_1:
1586 return ERR_PTR(error);
1587}
1588
David Howells454e2392006-06-23 02:02:57 -07001589static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590eventpollfs_get_sb(struct file_system_type *fs_type, int flags,
David Howells454e2392006-06-23 02:02:57 -07001591 const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
David Howells454e2392006-06-23 02:02:57 -07001593 return get_sb_pseudo(fs_type, "eventpoll:", NULL, EVENTPOLLFS_MAGIC,
1594 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597
1598static int __init eventpoll_init(void)
1599{
1600 int error;
1601
Arjan van de Ven144efe32006-03-23 03:00:32 -08001602 mutex_init(&epmutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 /* Initialize the structure used to perform safe poll wait head wake ups */
1605 ep_poll_safewake_init(&psw);
1606
1607 /* Allocates slab cache used to allocate "struct epitem" items */
1608 epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
1609 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC,
1610 NULL, NULL);
1611
1612 /* Allocates slab cache used to allocate "struct eppoll_entry" */
1613 pwq_cache = kmem_cache_create("eventpoll_pwq",
1614 sizeof(struct eppoll_entry), 0,
1615 EPI_SLAB_DEBUG|SLAB_PANIC, NULL, NULL);
1616
1617 /*
1618 * Register the virtual file system that will be the source of inodes
1619 * for the eventpoll files
1620 */
1621 error = register_filesystem(&eventpoll_fs_type);
1622 if (error)
1623 goto epanic;
1624
1625 /* Mount the above commented virtual file system */
1626 eventpoll_mnt = kern_mount(&eventpoll_fs_type);
1627 error = PTR_ERR(eventpoll_mnt);
1628 if (IS_ERR(eventpoll_mnt))
1629 goto epanic;
1630
1631 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: successfully initialized.\n",
1632 current));
1633 return 0;
1634
1635epanic:
1636 panic("eventpoll_init() failed\n");
1637}
1638
1639
1640static void __exit eventpoll_exit(void)
1641{
1642 /* Undo all operations done inside eventpoll_init() */
1643 unregister_filesystem(&eventpoll_fs_type);
1644 mntput(eventpoll_mnt);
1645 kmem_cache_destroy(pwq_cache);
1646 kmem_cache_destroy(epi_cache);
1647}
1648
1649module_init(eventpoll_init);
1650module_exit(eventpoll_exit);
1651
1652MODULE_LICENSE("GPL");