Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/eventpoll.c ( Efficent event polling implementation ) |
Davide Libenzi | 3419b23 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 3 | * Copyright (C) 2001,...,2006 Davide Libenzi |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 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> |
| 25 | #include <linux/smp_lock.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/list.h> |
| 28 | #include <linux/hash.h> |
| 29 | #include <linux/spinlock.h> |
| 30 | #include <linux/syscalls.h> |
| 31 | #include <linux/rwsem.h> |
| 32 | #include <linux/rbtree.h> |
| 33 | #include <linux/wait.h> |
| 34 | #include <linux/eventpoll.h> |
| 35 | #include <linux/mount.h> |
| 36 | #include <linux/bitops.h> |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/uaccess.h> |
| 39 | #include <asm/system.h> |
| 40 | #include <asm/io.h> |
| 41 | #include <asm/mman.h> |
| 42 | #include <asm/atomic.h> |
| 43 | #include <asm/semaphore.h> |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * LOCKING: |
| 48 | * There are three level of locking required by epoll : |
| 49 | * |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 50 | * 1) epmutex (mutex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | * 2) ep->sem (rw_semaphore) |
| 52 | * 3) ep->lock (rw_lock) |
| 53 | * |
| 54 | * The acquire order is the one listed above, from 1 to 3. |
| 55 | * We need a spinlock (ep->lock) because we manipulate objects |
| 56 | * from inside the poll callback, that might be triggered from |
| 57 | * a wake_up() that in turn might be called from IRQ context. |
| 58 | * So we can't sleep inside the poll callback and hence we need |
| 59 | * a spinlock. During the event transfer loop (from kernel to |
| 60 | * user space) we could end up sleeping due a copy_to_user(), so |
| 61 | * we need a lock that will allow us to sleep. This lock is a |
| 62 | * read-write semaphore (ep->sem). It is acquired on read during |
| 63 | * the event transfer loop and in write during epoll_ctl(EPOLL_CTL_DEL) |
| 64 | * and during eventpoll_release_file(). Then we also need a global |
| 65 | * semaphore to serialize eventpoll_release_file() and ep_free(). |
| 66 | * This semaphore is acquired by ep_free() during the epoll file |
| 67 | * cleanup path and it is also acquired by eventpoll_release_file() |
| 68 | * if a file has been pushed inside an epoll set and it is then |
| 69 | * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL). |
| 70 | * It is possible to drop the "ep->sem" and to use the global |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 71 | * semaphore "epmutex" (together with "ep->lock") to have it working, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * but having "ep->sem" will make the interface more scalable. |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 73 | * Events that require holding "epmutex" are very rare, while for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * normal operations the epoll private "ep->sem" will guarantee |
| 75 | * a greater scalability. |
| 76 | */ |
| 77 | |
| 78 | |
| 79 | #define EVENTPOLLFS_MAGIC 0x03111965 /* My birthday should work for this :) */ |
| 80 | |
| 81 | #define DEBUG_EPOLL 0 |
| 82 | |
| 83 | #if DEBUG_EPOLL > 0 |
| 84 | #define DPRINTK(x) printk x |
| 85 | #define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0) |
| 86 | #else /* #if DEBUG_EPOLL > 0 */ |
| 87 | #define DPRINTK(x) (void) 0 |
| 88 | #define DNPRINTK(n, x) (void) 0 |
| 89 | #endif /* #if DEBUG_EPOLL > 0 */ |
| 90 | |
| 91 | #define DEBUG_EPI 0 |
| 92 | |
| 93 | #if DEBUG_EPI != 0 |
| 94 | #define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */) |
| 95 | #else /* #if DEBUG_EPI != 0 */ |
| 96 | #define EPI_SLAB_DEBUG 0 |
| 97 | #endif /* #if DEBUG_EPI != 0 */ |
| 98 | |
| 99 | /* Epoll private bits inside the event mask */ |
| 100 | #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET) |
| 101 | |
| 102 | /* Maximum number of poll wake up nests we are allowing */ |
| 103 | #define EP_MAX_POLLWAKE_NESTS 4 |
| 104 | |
Davide Libenzi | e3306dd | 2005-09-27 21:45:33 -0700 | [diff] [blame] | 105 | /* Maximum msec timeout value storeable in a long int */ |
| 106 | #define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ) |
| 107 | |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | struct epoll_filefd { |
| 110 | struct file *file; |
| 111 | int fd; |
| 112 | }; |
| 113 | |
| 114 | /* |
| 115 | * Node that is linked into the "wake_task_list" member of the "struct poll_safewake". |
| 116 | * It is used to keep track on all tasks that are currently inside the wake_up() code |
| 117 | * to 1) short-circuit the one coming from the same task and same wait queue head |
| 118 | * ( loop ) 2) allow a maximum number of epoll descriptors inclusion nesting |
| 119 | * 3) let go the ones coming from other tasks. |
| 120 | */ |
| 121 | struct wake_task_node { |
| 122 | struct list_head llink; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 123 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | wait_queue_head_t *wq; |
| 125 | }; |
| 126 | |
| 127 | /* |
| 128 | * This is used to implement the safe poll wake up avoiding to reenter |
| 129 | * the poll callback from inside wake_up(). |
| 130 | */ |
| 131 | struct poll_safewake { |
| 132 | struct list_head wake_task_list; |
| 133 | spinlock_t lock; |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * This structure is stored inside the "private_data" member of the file |
| 138 | * structure and rapresent the main data sructure for the eventpoll |
| 139 | * interface. |
| 140 | */ |
| 141 | struct eventpoll { |
| 142 | /* Protect the this structure access */ |
| 143 | rwlock_t lock; |
| 144 | |
| 145 | /* |
| 146 | * This semaphore is used to ensure that files are not removed |
| 147 | * while epoll is using them. This is read-held during the event |
| 148 | * collection loop and it is write-held during the file cleanup |
| 149 | * path, the epoll file exit code and the ctl operations. |
| 150 | */ |
| 151 | struct rw_semaphore sem; |
| 152 | |
| 153 | /* Wait queue used by sys_epoll_wait() */ |
| 154 | wait_queue_head_t wq; |
| 155 | |
| 156 | /* Wait queue used by file->poll() */ |
| 157 | wait_queue_head_t poll_wait; |
| 158 | |
| 159 | /* List of ready file descriptors */ |
| 160 | struct list_head rdllist; |
| 161 | |
| 162 | /* RB-Tree root used to store monitored fd structs */ |
| 163 | struct rb_root rbr; |
| 164 | }; |
| 165 | |
| 166 | /* Wait structure used by the poll hooks */ |
| 167 | struct eppoll_entry { |
| 168 | /* List header used to link this structure to the "struct epitem" */ |
| 169 | struct list_head llink; |
| 170 | |
| 171 | /* The "base" pointer is set to the container "struct epitem" */ |
| 172 | void *base; |
| 173 | |
| 174 | /* |
| 175 | * Wait queue item that will be linked to the target file wait |
| 176 | * queue head. |
| 177 | */ |
| 178 | wait_queue_t wait; |
| 179 | |
| 180 | /* The wait queue head that linked the "wait" wait queue item */ |
| 181 | wait_queue_head_t *whead; |
| 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * Each file descriptor added to the eventpoll interface will |
| 186 | * have an entry of this type linked to the hash. |
| 187 | */ |
| 188 | struct epitem { |
| 189 | /* RB-Tree node used to link this structure to the eventpoll rb-tree */ |
| 190 | struct rb_node rbn; |
| 191 | |
| 192 | /* List header used to link this structure to the eventpoll ready list */ |
| 193 | struct list_head rdllink; |
| 194 | |
| 195 | /* The file descriptor information this item refers to */ |
| 196 | struct epoll_filefd ffd; |
| 197 | |
| 198 | /* Number of active wait queue attached to poll operations */ |
| 199 | int nwait; |
| 200 | |
| 201 | /* List containing poll wait queues */ |
| 202 | struct list_head pwqlist; |
| 203 | |
| 204 | /* The "container" of this item */ |
| 205 | struct eventpoll *ep; |
| 206 | |
| 207 | /* The structure that describe the interested events and the source fd */ |
| 208 | struct epoll_event event; |
| 209 | |
| 210 | /* |
| 211 | * Used to keep track of the usage count of the structure. This avoids |
| 212 | * that the structure will desappear from underneath our processing. |
| 213 | */ |
| 214 | atomic_t usecnt; |
| 215 | |
| 216 | /* List header used to link this item to the "struct file" items list */ |
| 217 | struct list_head fllink; |
| 218 | |
| 219 | /* List header used to link the item to the transfer list */ |
| 220 | struct list_head txlink; |
| 221 | |
| 222 | /* |
| 223 | * This is used during the collection/transfer of events to userspace |
| 224 | * to pin items empty events set. |
| 225 | */ |
| 226 | unsigned int revents; |
| 227 | }; |
| 228 | |
| 229 | /* Wrapper struct used by poll queueing */ |
| 230 | struct ep_pqueue { |
| 231 | poll_table pt; |
| 232 | struct epitem *epi; |
| 233 | }; |
| 234 | |
| 235 | |
| 236 | |
| 237 | static void ep_poll_safewake_init(struct poll_safewake *psw); |
| 238 | static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq); |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 239 | static int ep_getfd(int *efd, struct inode **einode, struct file **efile, |
| 240 | struct eventpoll *ep); |
| 241 | static int ep_alloc(struct eventpoll **pep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | static void ep_free(struct eventpoll *ep); |
| 243 | static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd); |
| 244 | static void ep_use_epitem(struct epitem *epi); |
| 245 | static void ep_release_epitem(struct epitem *epi); |
| 246 | static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, |
| 247 | poll_table *pt); |
| 248 | static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi); |
| 249 | static int ep_insert(struct eventpoll *ep, struct epoll_event *event, |
| 250 | struct file *tfile, int fd); |
| 251 | static int ep_modify(struct eventpoll *ep, struct epitem *epi, |
| 252 | struct epoll_event *event); |
| 253 | static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi); |
| 254 | static int ep_unlink(struct eventpoll *ep, struct epitem *epi); |
| 255 | static int ep_remove(struct eventpoll *ep, struct epitem *epi); |
| 256 | static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key); |
| 257 | static int ep_eventpoll_close(struct inode *inode, struct file *file); |
| 258 | static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait); |
| 259 | static int ep_collect_ready_items(struct eventpoll *ep, |
| 260 | struct list_head *txlist, int maxevents); |
| 261 | static int ep_send_events(struct eventpoll *ep, struct list_head *txlist, |
| 262 | struct epoll_event __user *events); |
| 263 | static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist); |
| 264 | static int ep_events_transfer(struct eventpoll *ep, |
| 265 | struct epoll_event __user *events, |
| 266 | int maxevents); |
| 267 | static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, |
| 268 | int maxevents, long timeout); |
| 269 | static int eventpollfs_delete_dentry(struct dentry *dentry); |
| 270 | static struct inode *ep_eventpoll_inode(void); |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 271 | static int eventpollfs_get_sb(struct file_system_type *fs_type, |
| 272 | int flags, const char *dev_name, |
| 273 | void *data, struct vfsmount *mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * This semaphore is used to serialize ep_free() and eventpoll_release_file(). |
| 277 | */ |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 278 | static struct mutex epmutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | /* Safe wake up implementation */ |
| 281 | static struct poll_safewake psw; |
| 282 | |
| 283 | /* Slab cache used to allocate "struct epitem" */ |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 284 | static kmem_cache_t *epi_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | /* Slab cache used to allocate "struct eppoll_entry" */ |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 287 | static kmem_cache_t *pwq_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | /* Virtual fs used to allocate inodes for eventpoll files */ |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 290 | static struct vfsmount *eventpoll_mnt __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | /* File callbacks that implement the eventpoll file behaviour */ |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 293 | static const struct file_operations eventpoll_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | .release = ep_eventpoll_close, |
| 295 | .poll = ep_eventpoll_poll |
| 296 | }; |
| 297 | |
| 298 | /* |
| 299 | * This is used to register the virtual file system from where |
| 300 | * eventpoll inodes are allocated. |
| 301 | */ |
| 302 | static struct file_system_type eventpoll_fs_type = { |
| 303 | .name = "eventpollfs", |
| 304 | .get_sb = eventpollfs_get_sb, |
| 305 | .kill_sb = kill_anon_super, |
| 306 | }; |
| 307 | |
| 308 | /* Very basic directory entry operations for the eventpoll virtual file system */ |
| 309 | static struct dentry_operations eventpollfs_dentry_operations = { |
| 310 | .d_delete = eventpollfs_delete_dentry, |
| 311 | }; |
| 312 | |
| 313 | |
| 314 | |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 315 | /* Fast test to see if the file is an evenpoll file */ |
| 316 | static inline int is_file_epoll(struct file *f) |
| 317 | { |
| 318 | return f->f_op == &eventpoll_fops; |
| 319 | } |
| 320 | |
| 321 | /* Setup the structure that is used as key for the rb-tree */ |
| 322 | static inline void ep_set_ffd(struct epoll_filefd *ffd, |
| 323 | struct file *file, int fd) |
| 324 | { |
| 325 | ffd->file = file; |
| 326 | ffd->fd = fd; |
| 327 | } |
| 328 | |
| 329 | /* Compare rb-tree keys */ |
| 330 | static inline int ep_cmp_ffd(struct epoll_filefd *p1, |
| 331 | struct epoll_filefd *p2) |
| 332 | { |
| 333 | return (p1->file > p2->file ? +1: |
| 334 | (p1->file < p2->file ? -1 : p1->fd - p2->fd)); |
| 335 | } |
| 336 | |
| 337 | /* Special initialization for the rb-tree node to detect linkage */ |
| 338 | static inline void ep_rb_initnode(struct rb_node *n) |
| 339 | { |
David Woodhouse | c569882 | 2006-04-21 13:17:24 +0100 | [diff] [blame] | 340 | rb_set_parent(n, n); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* Removes a node from the rb-tree and marks it for a fast is-linked check */ |
| 344 | static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r) |
| 345 | { |
| 346 | rb_erase(n, r); |
David Woodhouse | c569882 | 2006-04-21 13:17:24 +0100 | [diff] [blame] | 347 | rb_set_parent(n, n); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* Fast check to verify that the item is linked to the main rb-tree */ |
| 351 | static inline int ep_rb_linked(struct rb_node *n) |
| 352 | { |
David Woodhouse | c569882 | 2006-04-21 13:17:24 +0100 | [diff] [blame] | 353 | return rb_parent(n) != n; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Remove the item from the list and perform its initialization. |
| 358 | * This is useful for us because we can test if the item is linked |
| 359 | * using "ep_is_linked(p)". |
| 360 | */ |
| 361 | static inline void ep_list_del(struct list_head *p) |
| 362 | { |
| 363 | list_del(p); |
| 364 | INIT_LIST_HEAD(p); |
| 365 | } |
| 366 | |
| 367 | /* Tells us if the item is currently linked */ |
| 368 | static inline int ep_is_linked(struct list_head *p) |
| 369 | { |
| 370 | return !list_empty(p); |
| 371 | } |
| 372 | |
| 373 | /* Get the "struct epitem" from a wait queue pointer */ |
| 374 | static inline struct epitem * ep_item_from_wait(wait_queue_t *p) |
| 375 | { |
| 376 | return container_of(p, struct eppoll_entry, wait)->base; |
| 377 | } |
| 378 | |
| 379 | /* Get the "struct epitem" from an epoll queue wrapper */ |
| 380 | static inline struct epitem * ep_item_from_epqueue(poll_table *p) |
| 381 | { |
| 382 | return container_of(p, struct ep_pqueue, pt)->epi; |
| 383 | } |
| 384 | |
| 385 | /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */ |
| 386 | static inline int ep_op_hash_event(int op) |
| 387 | { |
| 388 | return op != EPOLL_CTL_DEL; |
| 389 | } |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | /* Initialize the poll safe wake up structure */ |
| 392 | static void ep_poll_safewake_init(struct poll_safewake *psw) |
| 393 | { |
| 394 | |
| 395 | INIT_LIST_HEAD(&psw->wake_task_list); |
| 396 | spin_lock_init(&psw->lock); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /* |
| 401 | * Perform a safe wake up of the poll wait list. The problem is that |
| 402 | * with the new callback'd wake up system, it is possible that the |
| 403 | * poll callback is reentered from inside the call to wake_up() done |
| 404 | * on the poll wait queue head. The rule is that we cannot reenter the |
| 405 | * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times, |
| 406 | * and we cannot reenter the same wait queue head at all. This will |
| 407 | * enable to have a hierarchy of epoll file descriptor of no more than |
| 408 | * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock |
| 409 | * because this one gets called by the poll callback, that in turn is called |
| 410 | * from inside a wake_up(), that might be called from irq context. |
| 411 | */ |
| 412 | static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq) |
| 413 | { |
| 414 | int wake_nests = 0; |
| 415 | unsigned long flags; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 416 | struct task_struct *this_task = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | struct list_head *lsthead = &psw->wake_task_list, *lnk; |
| 418 | struct wake_task_node *tncur; |
| 419 | struct wake_task_node tnode; |
| 420 | |
| 421 | spin_lock_irqsave(&psw->lock, flags); |
| 422 | |
| 423 | /* Try to see if the current task is already inside this wakeup call */ |
| 424 | list_for_each(lnk, lsthead) { |
| 425 | tncur = list_entry(lnk, struct wake_task_node, llink); |
| 426 | |
| 427 | if (tncur->wq == wq || |
| 428 | (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) { |
| 429 | /* |
| 430 | * Ops ... loop detected or maximum nest level reached. |
| 431 | * We abort this wake by breaking the cycle itself. |
| 432 | */ |
| 433 | spin_unlock_irqrestore(&psw->lock, flags); |
| 434 | return; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* Add the current task to the list */ |
| 439 | tnode.task = this_task; |
| 440 | tnode.wq = wq; |
| 441 | list_add(&tnode.llink, lsthead); |
| 442 | |
| 443 | spin_unlock_irqrestore(&psw->lock, flags); |
| 444 | |
| 445 | /* Do really wake up now */ |
| 446 | wake_up(wq); |
| 447 | |
| 448 | /* Remove the current task from the list */ |
| 449 | spin_lock_irqsave(&psw->lock, flags); |
| 450 | list_del(&tnode.llink); |
| 451 | spin_unlock_irqrestore(&psw->lock, flags); |
| 452 | } |
| 453 | |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* |
| 456 | * This is called from eventpoll_release() to unlink files from the eventpoll |
| 457 | * interface. We need to have this facility to cleanup correctly files that are |
| 458 | * closed without being removed from the eventpoll interface. |
| 459 | */ |
| 460 | void eventpoll_release_file(struct file *file) |
| 461 | { |
| 462 | struct list_head *lsthead = &file->f_ep_links; |
| 463 | struct eventpoll *ep; |
| 464 | struct epitem *epi; |
| 465 | |
| 466 | /* |
| 467 | * We don't want to get "file->f_ep_lock" because it is not |
| 468 | * necessary. It is not necessary because we're in the "struct file" |
| 469 | * cleanup path, and this means that noone is using this file anymore. |
| 470 | * The only hit might come from ep_free() but by holding the semaphore |
| 471 | * will correctly serialize the operation. We do need to acquire |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 472 | * "ep->sem" after "epmutex" because ep_remove() requires it when called |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | * from anywhere but ep_free(). |
| 474 | */ |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 475 | mutex_lock(&epmutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | while (!list_empty(lsthead)) { |
| 478 | epi = list_entry(lsthead->next, struct epitem, fllink); |
| 479 | |
| 480 | ep = epi->ep; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 481 | ep_list_del(&epi->fllink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | down_write(&ep->sem); |
| 483 | ep_remove(ep, epi); |
| 484 | up_write(&ep->sem); |
| 485 | } |
| 486 | |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 487 | mutex_unlock(&epmutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | |
| 491 | /* |
| 492 | * It opens an eventpoll file descriptor by suggesting a storage of "size" |
| 493 | * file descriptors. The size parameter is just an hint about how to size |
| 494 | * data structures. It won't prevent the user to store more than "size" |
| 495 | * file descriptors inside the epoll interface. It is the kernel part of |
| 496 | * the userspace epoll_create(2). |
| 497 | */ |
| 498 | asmlinkage long sys_epoll_create(int size) |
| 499 | { |
| 500 | int error, fd; |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 501 | struct eventpoll *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | struct inode *inode; |
| 503 | struct file *file; |
| 504 | |
| 505 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n", |
| 506 | current, size)); |
| 507 | |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 508 | /* |
| 509 | * Sanity check on the size parameter, and create the internal data |
| 510 | * structure ( "struct eventpoll" ). |
| 511 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | error = -EINVAL; |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 513 | if (size <= 0 || (error = ep_alloc(&ep)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | goto eexit_1; |
| 515 | |
| 516 | /* |
| 517 | * Creates all the items needed to setup an eventpoll file. That is, |
| 518 | * a file structure, and inode and a free file descriptor. |
| 519 | */ |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 520 | error = ep_getfd(&fd, &inode, &file, ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | if (error) |
| 522 | goto eexit_2; |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n", |
| 525 | current, size, fd)); |
| 526 | |
| 527 | return fd; |
| 528 | |
| 529 | eexit_2: |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 530 | ep_free(ep); |
| 531 | kfree(ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | eexit_1: |
| 533 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n", |
| 534 | current, size, error)); |
| 535 | return error; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | /* |
| 540 | * The following function implements the controller interface for |
| 541 | * the eventpoll file that enables the insertion/removal/change of |
| 542 | * file descriptors inside the interest set. It represents |
| 543 | * the kernel part of the user space epoll_ctl(2). |
| 544 | */ |
| 545 | asmlinkage long |
| 546 | sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event) |
| 547 | { |
| 548 | int error; |
| 549 | struct file *file, *tfile; |
| 550 | struct eventpoll *ep; |
| 551 | struct epitem *epi; |
| 552 | struct epoll_event epds; |
| 553 | |
| 554 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n", |
| 555 | current, epfd, op, fd, event)); |
| 556 | |
| 557 | error = -EFAULT; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 558 | if (ep_op_hash_event(op) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | copy_from_user(&epds, event, sizeof(struct epoll_event))) |
| 560 | goto eexit_1; |
| 561 | |
| 562 | /* Get the "struct file *" for the eventpoll file */ |
| 563 | error = -EBADF; |
| 564 | file = fget(epfd); |
| 565 | if (!file) |
| 566 | goto eexit_1; |
| 567 | |
| 568 | /* Get the "struct file *" for the target file */ |
| 569 | tfile = fget(fd); |
| 570 | if (!tfile) |
| 571 | goto eexit_2; |
| 572 | |
| 573 | /* The target file descriptor must support poll */ |
| 574 | error = -EPERM; |
| 575 | if (!tfile->f_op || !tfile->f_op->poll) |
| 576 | goto eexit_3; |
| 577 | |
| 578 | /* |
| 579 | * We have to check that the file structure underneath the file descriptor |
| 580 | * the user passed to us _is_ an eventpoll file. And also we do not permit |
| 581 | * adding an epoll file descriptor inside itself. |
| 582 | */ |
| 583 | error = -EINVAL; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 584 | if (file == tfile || !is_file_epoll(file)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | goto eexit_3; |
| 586 | |
| 587 | /* |
| 588 | * At this point it is safe to assume that the "private_data" contains |
| 589 | * our own data structure. |
| 590 | */ |
| 591 | ep = file->private_data; |
| 592 | |
| 593 | down_write(&ep->sem); |
| 594 | |
| 595 | /* Try to lookup the file inside our hash table */ |
| 596 | epi = ep_find(ep, tfile, fd); |
| 597 | |
| 598 | error = -EINVAL; |
| 599 | switch (op) { |
| 600 | case EPOLL_CTL_ADD: |
| 601 | if (!epi) { |
Davide Libenzi | 2395140 | 2006-04-10 22:54:12 -0700 | [diff] [blame] | 602 | epds.events |= POLLERR | POLLHUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
| 604 | error = ep_insert(ep, &epds, tfile, fd); |
| 605 | } else |
| 606 | error = -EEXIST; |
| 607 | break; |
| 608 | case EPOLL_CTL_DEL: |
| 609 | if (epi) |
| 610 | error = ep_remove(ep, epi); |
| 611 | else |
| 612 | error = -ENOENT; |
| 613 | break; |
| 614 | case EPOLL_CTL_MOD: |
| 615 | if (epi) { |
Davide Libenzi | 2395140 | 2006-04-10 22:54:12 -0700 | [diff] [blame] | 616 | epds.events |= POLLERR | POLLHUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | error = ep_modify(ep, epi, &epds); |
| 618 | } else |
| 619 | error = -ENOENT; |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * The function ep_find() increments the usage count of the structure |
| 625 | * so, if this is not NULL, we need to release it. |
| 626 | */ |
| 627 | if (epi) |
| 628 | ep_release_epitem(epi); |
| 629 | |
| 630 | up_write(&ep->sem); |
| 631 | |
| 632 | eexit_3: |
| 633 | fput(tfile); |
| 634 | eexit_2: |
| 635 | fput(file); |
| 636 | eexit_1: |
| 637 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n", |
| 638 | current, epfd, op, fd, event, error)); |
| 639 | |
| 640 | return error; |
| 641 | } |
| 642 | |
| 643 | #define MAX_EVENTS (INT_MAX / sizeof(struct epoll_event)) |
| 644 | |
| 645 | /* |
| 646 | * Implement the event wait interface for the eventpoll file. It is the kernel |
| 647 | * part of the user space epoll_wait(2). |
| 648 | */ |
| 649 | asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events, |
| 650 | int maxevents, int timeout) |
| 651 | { |
| 652 | int error; |
| 653 | struct file *file; |
| 654 | struct eventpoll *ep; |
| 655 | |
| 656 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n", |
| 657 | current, epfd, events, maxevents, timeout)); |
| 658 | |
| 659 | /* The maximum number of event must be greater than zero */ |
| 660 | if (maxevents <= 0 || maxevents > MAX_EVENTS) |
| 661 | return -EINVAL; |
| 662 | |
| 663 | /* Verify that the area passed by the user is writeable */ |
| 664 | if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) { |
| 665 | error = -EFAULT; |
| 666 | goto eexit_1; |
| 667 | } |
| 668 | |
| 669 | /* Get the "struct file *" for the eventpoll file */ |
| 670 | error = -EBADF; |
| 671 | file = fget(epfd); |
| 672 | if (!file) |
| 673 | goto eexit_1; |
| 674 | |
| 675 | /* |
| 676 | * We have to check that the file structure underneath the fd |
| 677 | * the user passed to us _is_ an eventpoll file. |
| 678 | */ |
| 679 | error = -EINVAL; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 680 | if (!is_file_epoll(file)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | goto eexit_2; |
| 682 | |
| 683 | /* |
| 684 | * At this point it is safe to assume that the "private_data" contains |
| 685 | * our own data structure. |
| 686 | */ |
| 687 | ep = file->private_data; |
| 688 | |
| 689 | /* Time to fish for events ... */ |
| 690 | error = ep_poll(ep, events, maxevents, timeout); |
| 691 | |
| 692 | eexit_2: |
| 693 | fput(file); |
| 694 | eexit_1: |
| 695 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n", |
| 696 | current, epfd, events, maxevents, timeout, error)); |
| 697 | |
| 698 | return error; |
| 699 | } |
| 700 | |
| 701 | |
| 702 | /* |
| 703 | * Creates the file descriptor to be used by the epoll interface. |
| 704 | */ |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 705 | static int ep_getfd(int *efd, struct inode **einode, struct file **efile, |
| 706 | struct eventpoll *ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
| 708 | struct qstr this; |
| 709 | char name[32]; |
| 710 | struct dentry *dentry; |
| 711 | struct inode *inode; |
| 712 | struct file *file; |
| 713 | int error, fd; |
| 714 | |
| 715 | /* Get an ready to use file */ |
| 716 | error = -ENFILE; |
| 717 | file = get_empty_filp(); |
| 718 | if (!file) |
| 719 | goto eexit_1; |
| 720 | |
| 721 | /* Allocates an inode from the eventpoll file system */ |
| 722 | inode = ep_eventpoll_inode(); |
| 723 | error = PTR_ERR(inode); |
| 724 | if (IS_ERR(inode)) |
| 725 | goto eexit_2; |
| 726 | |
| 727 | /* Allocates a free descriptor to plug the file onto */ |
| 728 | error = get_unused_fd(); |
| 729 | if (error < 0) |
| 730 | goto eexit_3; |
| 731 | fd = error; |
| 732 | |
| 733 | /* |
| 734 | * Link the inode to a directory entry by creating a unique name |
| 735 | * using the inode number. |
| 736 | */ |
| 737 | error = -ENOMEM; |
| 738 | sprintf(name, "[%lu]", inode->i_ino); |
| 739 | this.name = name; |
| 740 | this.len = strlen(name); |
| 741 | this.hash = inode->i_ino; |
| 742 | dentry = d_alloc(eventpoll_mnt->mnt_sb->s_root, &this); |
| 743 | if (!dentry) |
| 744 | goto eexit_4; |
| 745 | dentry->d_op = &eventpollfs_dentry_operations; |
| 746 | d_add(dentry, inode); |
| 747 | file->f_vfsmnt = mntget(eventpoll_mnt); |
| 748 | file->f_dentry = dentry; |
| 749 | file->f_mapping = inode->i_mapping; |
| 750 | |
| 751 | file->f_pos = 0; |
| 752 | file->f_flags = O_RDONLY; |
| 753 | file->f_op = &eventpoll_fops; |
| 754 | file->f_mode = FMODE_READ; |
| 755 | file->f_version = 0; |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 756 | file->private_data = ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | /* Install the new setup file into the allocated fd. */ |
| 759 | fd_install(fd, file); |
| 760 | |
| 761 | *efd = fd; |
| 762 | *einode = inode; |
| 763 | *efile = file; |
| 764 | return 0; |
| 765 | |
| 766 | eexit_4: |
| 767 | put_unused_fd(fd); |
| 768 | eexit_3: |
| 769 | iput(inode); |
| 770 | eexit_2: |
| 771 | put_filp(file); |
| 772 | eexit_1: |
| 773 | return error; |
| 774 | } |
| 775 | |
| 776 | |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 777 | static int ep_alloc(struct eventpoll **pep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 779 | struct eventpoll *ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 781 | if (!ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | return -ENOMEM; |
| 783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | rwlock_init(&ep->lock); |
| 785 | init_rwsem(&ep->sem); |
| 786 | init_waitqueue_head(&ep->wq); |
| 787 | init_waitqueue_head(&ep->poll_wait); |
| 788 | INIT_LIST_HEAD(&ep->rdllist); |
| 789 | ep->rbr = RB_ROOT; |
| 790 | |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 791 | *pep = ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Davide Libenzi | 53d2be7 | 2005-09-16 19:28:06 -0700 | [diff] [blame] | 793 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | current, ep)); |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | |
| 799 | static void ep_free(struct eventpoll *ep) |
| 800 | { |
| 801 | struct rb_node *rbp; |
| 802 | struct epitem *epi; |
| 803 | |
| 804 | /* We need to release all tasks waiting for these file */ |
| 805 | if (waitqueue_active(&ep->poll_wait)) |
| 806 | ep_poll_safewake(&psw, &ep->poll_wait); |
| 807 | |
| 808 | /* |
| 809 | * We need to lock this because we could be hit by |
| 810 | * eventpoll_release_file() while we're freeing the "struct eventpoll". |
| 811 | * We do not need to hold "ep->sem" here because the epoll file |
| 812 | * is on the way to be removed and no one has references to it |
| 813 | * anymore. The only hit might come from eventpoll_release_file() but |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 814 | * holding "epmutex" is sufficent here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | */ |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 816 | mutex_lock(&epmutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | /* |
| 819 | * Walks through the whole tree by unregistering poll callbacks. |
| 820 | */ |
| 821 | for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) { |
| 822 | epi = rb_entry(rbp, struct epitem, rbn); |
| 823 | |
| 824 | ep_unregister_pollwait(ep, epi); |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Walks through the whole hash by freeing each "struct epitem". At this |
| 829 | * point we are sure no poll callbacks will be lingering around, and also by |
| 830 | * write-holding "sem" we can be sure that no file cleanup code will hit |
| 831 | * us during this operation. So we can avoid the lock on "ep->lock". |
| 832 | */ |
| 833 | while ((rbp = rb_first(&ep->rbr)) != 0) { |
| 834 | epi = rb_entry(rbp, struct epitem, rbn); |
| 835 | ep_remove(ep, epi); |
| 836 | } |
| 837 | |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 838 | mutex_unlock(&epmutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | |
| 842 | /* |
| 843 | * Search the file inside the eventpoll hash. It add usage count to |
| 844 | * the returned item, so the caller must call ep_release_epitem() |
| 845 | * after finished using the "struct epitem". |
| 846 | */ |
| 847 | static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd) |
| 848 | { |
| 849 | int kcmp; |
| 850 | unsigned long flags; |
| 851 | struct rb_node *rbp; |
| 852 | struct epitem *epi, *epir = NULL; |
| 853 | struct epoll_filefd ffd; |
| 854 | |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 855 | ep_set_ffd(&ffd, file, fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | read_lock_irqsave(&ep->lock, flags); |
| 857 | for (rbp = ep->rbr.rb_node; rbp; ) { |
| 858 | epi = rb_entry(rbp, struct epitem, rbn); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 859 | kcmp = ep_cmp_ffd(&ffd, &epi->ffd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | if (kcmp > 0) |
| 861 | rbp = rbp->rb_right; |
| 862 | else if (kcmp < 0) |
| 863 | rbp = rbp->rb_left; |
| 864 | else { |
| 865 | ep_use_epitem(epi); |
| 866 | epir = epi; |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | read_unlock_irqrestore(&ep->lock, flags); |
| 871 | |
| 872 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n", |
| 873 | current, file, epir)); |
| 874 | |
| 875 | return epir; |
| 876 | } |
| 877 | |
| 878 | |
| 879 | /* |
| 880 | * Increment the usage count of the "struct epitem" making it sure |
| 881 | * that the user will have a valid pointer to reference. |
| 882 | */ |
| 883 | static void ep_use_epitem(struct epitem *epi) |
| 884 | { |
| 885 | |
| 886 | atomic_inc(&epi->usecnt); |
| 887 | } |
| 888 | |
| 889 | |
| 890 | /* |
| 891 | * Decrement ( release ) the usage count by signaling that the user |
| 892 | * has finished using the structure. It might lead to freeing the |
| 893 | * structure itself if the count goes to zero. |
| 894 | */ |
| 895 | static void ep_release_epitem(struct epitem *epi) |
| 896 | { |
| 897 | |
| 898 | if (atomic_dec_and_test(&epi->usecnt)) |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 899 | kmem_cache_free(epi_cache, epi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | |
| 903 | /* |
| 904 | * This is the callback that is used to add our wait queue to the |
| 905 | * target file wakeup lists. |
| 906 | */ |
| 907 | static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, |
| 908 | poll_table *pt) |
| 909 | { |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 910 | struct epitem *epi = ep_item_from_epqueue(pt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | struct eppoll_entry *pwq; |
| 912 | |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 913 | if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, SLAB_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | init_waitqueue_func_entry(&pwq->wait, ep_poll_callback); |
| 915 | pwq->whead = whead; |
| 916 | pwq->base = epi; |
| 917 | add_wait_queue(whead, &pwq->wait); |
| 918 | list_add_tail(&pwq->llink, &epi->pwqlist); |
| 919 | epi->nwait++; |
| 920 | } else { |
| 921 | /* We have to signal that an error occurred */ |
| 922 | epi->nwait = -1; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | |
| 927 | static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi) |
| 928 | { |
| 929 | int kcmp; |
| 930 | struct rb_node **p = &ep->rbr.rb_node, *parent = NULL; |
| 931 | struct epitem *epic; |
| 932 | |
| 933 | while (*p) { |
| 934 | parent = *p; |
| 935 | epic = rb_entry(parent, struct epitem, rbn); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 936 | kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | if (kcmp > 0) |
| 938 | p = &parent->rb_right; |
| 939 | else |
| 940 | p = &parent->rb_left; |
| 941 | } |
| 942 | rb_link_node(&epi->rbn, parent, p); |
| 943 | rb_insert_color(&epi->rbn, &ep->rbr); |
| 944 | } |
| 945 | |
| 946 | |
| 947 | static int ep_insert(struct eventpoll *ep, struct epoll_event *event, |
| 948 | struct file *tfile, int fd) |
| 949 | { |
| 950 | int error, revents, pwake = 0; |
| 951 | unsigned long flags; |
| 952 | struct epitem *epi; |
| 953 | struct ep_pqueue epq; |
| 954 | |
| 955 | error = -ENOMEM; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 956 | if (!(epi = kmem_cache_alloc(epi_cache, SLAB_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | goto eexit_1; |
| 958 | |
| 959 | /* Item initialization follow here ... */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 960 | ep_rb_initnode(&epi->rbn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | INIT_LIST_HEAD(&epi->rdllink); |
| 962 | INIT_LIST_HEAD(&epi->fllink); |
| 963 | INIT_LIST_HEAD(&epi->txlink); |
| 964 | INIT_LIST_HEAD(&epi->pwqlist); |
| 965 | epi->ep = ep; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 966 | ep_set_ffd(&epi->ffd, tfile, fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | epi->event = *event; |
| 968 | atomic_set(&epi->usecnt, 1); |
| 969 | epi->nwait = 0; |
| 970 | |
| 971 | /* Initialize the poll table using the queue callback */ |
| 972 | epq.epi = epi; |
| 973 | init_poll_funcptr(&epq.pt, ep_ptable_queue_proc); |
| 974 | |
| 975 | /* |
| 976 | * Attach the item to the poll hooks and get current event bits. |
| 977 | * We can safely use the file* here because its usage count has |
| 978 | * been increased by the caller of this function. |
| 979 | */ |
| 980 | revents = tfile->f_op->poll(tfile, &epq.pt); |
| 981 | |
| 982 | /* |
| 983 | * We have to check if something went wrong during the poll wait queue |
| 984 | * install process. Namely an allocation for a wait queue failed due |
| 985 | * high memory pressure. |
| 986 | */ |
| 987 | if (epi->nwait < 0) |
| 988 | goto eexit_2; |
| 989 | |
| 990 | /* Add the current item to the list of active epoll hook for this file */ |
| 991 | spin_lock(&tfile->f_ep_lock); |
| 992 | list_add_tail(&epi->fllink, &tfile->f_ep_links); |
| 993 | spin_unlock(&tfile->f_ep_lock); |
| 994 | |
| 995 | /* We have to drop the new item inside our item list to keep track of it */ |
| 996 | write_lock_irqsave(&ep->lock, flags); |
| 997 | |
| 998 | /* Add the current item to the rb-tree */ |
| 999 | ep_rbtree_insert(ep, epi); |
| 1000 | |
| 1001 | /* If the file is already "ready" we drop it inside the ready list */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1002 | if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | list_add_tail(&epi->rdllink, &ep->rdllist); |
| 1004 | |
| 1005 | /* Notify waiting tasks that events are available */ |
| 1006 | if (waitqueue_active(&ep->wq)) |
Davide Libenzi | 3419b23 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 1007 | __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (waitqueue_active(&ep->poll_wait)) |
| 1009 | pwake++; |
| 1010 | } |
| 1011 | |
| 1012 | write_unlock_irqrestore(&ep->lock, flags); |
| 1013 | |
| 1014 | /* We have to call this outside the lock */ |
| 1015 | if (pwake) |
| 1016 | ep_poll_safewake(&psw, &ep->poll_wait); |
| 1017 | |
| 1018 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n", |
| 1019 | current, ep, tfile, fd)); |
| 1020 | |
| 1021 | return 0; |
| 1022 | |
| 1023 | eexit_2: |
| 1024 | ep_unregister_pollwait(ep, epi); |
| 1025 | |
| 1026 | /* |
| 1027 | * We need to do this because an event could have been arrived on some |
| 1028 | * allocated wait queue. |
| 1029 | */ |
| 1030 | write_lock_irqsave(&ep->lock, flags); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1031 | if (ep_is_linked(&epi->rdllink)) |
| 1032 | ep_list_del(&epi->rdllink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | write_unlock_irqrestore(&ep->lock, flags); |
| 1034 | |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1035 | kmem_cache_free(epi_cache, epi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | eexit_1: |
| 1037 | return error; |
| 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | /* |
| 1042 | * Modify the interest event mask by dropping an event if the new mask |
| 1043 | * has a match in the current file status. |
| 1044 | */ |
| 1045 | static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event) |
| 1046 | { |
| 1047 | int pwake = 0; |
| 1048 | unsigned int revents; |
| 1049 | unsigned long flags; |
| 1050 | |
| 1051 | /* |
| 1052 | * Set the new event interest mask before calling f_op->poll(), otherwise |
| 1053 | * a potential race might occur. In fact if we do this operation inside |
| 1054 | * the lock, an event might happen between the f_op->poll() call and the |
| 1055 | * new event set registering. |
| 1056 | */ |
| 1057 | epi->event.events = event->events; |
| 1058 | |
| 1059 | /* |
| 1060 | * Get current event bits. We can safely use the file* here because |
| 1061 | * its usage count has been increased by the caller of this function. |
| 1062 | */ |
| 1063 | revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL); |
| 1064 | |
| 1065 | write_lock_irqsave(&ep->lock, flags); |
| 1066 | |
| 1067 | /* Copy the data member from inside the lock */ |
| 1068 | epi->event.data = event->data; |
| 1069 | |
| 1070 | /* |
| 1071 | * If the item is not linked to the hash it means that it's on its |
| 1072 | * way toward the removal. Do nothing in this case. |
| 1073 | */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1074 | if (ep_rb_linked(&epi->rbn)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | /* |
| 1076 | * If the item is "hot" and it is not registered inside the ready |
| 1077 | * list, push it inside. If the item is not "hot" and it is currently |
| 1078 | * registered inside the ready list, unlink it. |
| 1079 | */ |
| 1080 | if (revents & event->events) { |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1081 | if (!ep_is_linked(&epi->rdllink)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | list_add_tail(&epi->rdllink, &ep->rdllist); |
| 1083 | |
| 1084 | /* Notify waiting tasks that events are available */ |
| 1085 | if (waitqueue_active(&ep->wq)) |
Davide Libenzi | 3419b23 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 1086 | __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | |
| 1087 | TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | if (waitqueue_active(&ep->poll_wait)) |
| 1089 | pwake++; |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | write_unlock_irqrestore(&ep->lock, flags); |
| 1095 | |
| 1096 | /* We have to call this outside the lock */ |
| 1097 | if (pwake) |
| 1098 | ep_poll_safewake(&psw, &ep->poll_wait); |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | /* |
| 1105 | * This function unregister poll callbacks from the associated file descriptor. |
| 1106 | * Since this must be called without holding "ep->lock" the atomic exchange trick |
| 1107 | * will protect us from multiple unregister. |
| 1108 | */ |
| 1109 | static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi) |
| 1110 | { |
| 1111 | int nwait; |
| 1112 | struct list_head *lsthead = &epi->pwqlist; |
| 1113 | struct eppoll_entry *pwq; |
| 1114 | |
| 1115 | /* This is called without locks, so we need the atomic exchange */ |
| 1116 | nwait = xchg(&epi->nwait, 0); |
| 1117 | |
| 1118 | if (nwait) { |
| 1119 | while (!list_empty(lsthead)) { |
| 1120 | pwq = list_entry(lsthead->next, struct eppoll_entry, llink); |
| 1121 | |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1122 | ep_list_del(&pwq->llink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | remove_wait_queue(pwq->whead, &pwq->wait); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1124 | kmem_cache_free(pwq_cache, pwq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | /* |
| 1131 | * Unlink the "struct epitem" from all places it might have been hooked up. |
| 1132 | * This function must be called with write IRQ lock on "ep->lock". |
| 1133 | */ |
| 1134 | static int ep_unlink(struct eventpoll *ep, struct epitem *epi) |
| 1135 | { |
| 1136 | int error; |
| 1137 | |
| 1138 | /* |
| 1139 | * It can happen that this one is called for an item already unlinked. |
| 1140 | * The check protect us from doing a double unlink ( crash ). |
| 1141 | */ |
| 1142 | error = -ENOENT; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1143 | if (!ep_rb_linked(&epi->rbn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | goto eexit_1; |
| 1145 | |
| 1146 | /* |
| 1147 | * Clear the event mask for the unlinked item. This will avoid item |
| 1148 | * notifications to be sent after the unlink operation from inside |
| 1149 | * the kernel->userspace event transfer loop. |
| 1150 | */ |
| 1151 | epi->event.events = 0; |
| 1152 | |
| 1153 | /* |
| 1154 | * At this point is safe to do the job, unlink the item from our rb-tree. |
| 1155 | * This operation togheter with the above check closes the door to |
| 1156 | * double unlinks. |
| 1157 | */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1158 | ep_rb_erase(&epi->rbn, &ep->rbr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
| 1160 | /* |
| 1161 | * If the item we are going to remove is inside the ready file descriptors |
| 1162 | * we want to remove it from this list to avoid stale events. |
| 1163 | */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1164 | if (ep_is_linked(&epi->rdllink)) |
| 1165 | ep_list_del(&epi->rdllink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
| 1167 | error = 0; |
| 1168 | eexit_1: |
| 1169 | |
| 1170 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n", |
Masoud Asgharifard Sharbiani | 45f17e0 | 2006-08-27 01:23:48 -0700 | [diff] [blame] | 1171 | current, ep, epi->ffd.file, error)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
| 1173 | return error; |
| 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | /* |
| 1178 | * Removes a "struct epitem" from the eventpoll hash and deallocates |
| 1179 | * all the associated resources. |
| 1180 | */ |
| 1181 | static int ep_remove(struct eventpoll *ep, struct epitem *epi) |
| 1182 | { |
| 1183 | int error; |
| 1184 | unsigned long flags; |
| 1185 | struct file *file = epi->ffd.file; |
| 1186 | |
| 1187 | /* |
| 1188 | * Removes poll wait queue hooks. We _have_ to do this without holding |
| 1189 | * the "ep->lock" otherwise a deadlock might occur. This because of the |
| 1190 | * sequence of the lock acquisition. Here we do "ep->lock" then the wait |
| 1191 | * queue head lock when unregistering the wait queue. The wakeup callback |
| 1192 | * will run by holding the wait queue head lock and will call our callback |
| 1193 | * that will try to get "ep->lock". |
| 1194 | */ |
| 1195 | ep_unregister_pollwait(ep, epi); |
| 1196 | |
| 1197 | /* Remove the current item from the list of epoll hooks */ |
| 1198 | spin_lock(&file->f_ep_lock); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1199 | if (ep_is_linked(&epi->fllink)) |
| 1200 | ep_list_del(&epi->fllink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | spin_unlock(&file->f_ep_lock); |
| 1202 | |
| 1203 | /* We need to acquire the write IRQ lock before calling ep_unlink() */ |
| 1204 | write_lock_irqsave(&ep->lock, flags); |
| 1205 | |
| 1206 | /* Really unlink the item from the hash */ |
| 1207 | error = ep_unlink(ep, epi); |
| 1208 | |
| 1209 | write_unlock_irqrestore(&ep->lock, flags); |
| 1210 | |
| 1211 | if (error) |
| 1212 | goto eexit_1; |
| 1213 | |
| 1214 | /* At this point it is safe to free the eventpoll item */ |
| 1215 | ep_release_epitem(epi); |
| 1216 | |
| 1217 | error = 0; |
| 1218 | eexit_1: |
| 1219 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n", |
| 1220 | current, ep, file, error)); |
| 1221 | |
| 1222 | return error; |
| 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | /* |
| 1227 | * This is the callback that is passed to the wait queue wakeup |
| 1228 | * machanism. It is called by the stored file descriptors when they |
| 1229 | * have events to report. |
| 1230 | */ |
| 1231 | static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key) |
| 1232 | { |
| 1233 | int pwake = 0; |
| 1234 | unsigned long flags; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1235 | struct epitem *epi = ep_item_from_wait(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | struct eventpoll *ep = epi->ep; |
| 1237 | |
| 1238 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n", |
Masoud Asgharifard Sharbiani | 45f17e0 | 2006-08-27 01:23:48 -0700 | [diff] [blame] | 1239 | current, epi->ffd.file, epi, ep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
| 1241 | write_lock_irqsave(&ep->lock, flags); |
| 1242 | |
| 1243 | /* |
| 1244 | * If the event mask does not contain any poll(2) event, we consider the |
| 1245 | * descriptor to be disabled. This condition is likely the effect of the |
| 1246 | * EPOLLONESHOT bit that disables the descriptor when an event is received, |
| 1247 | * until the next EPOLL_CTL_MOD will be issued. |
| 1248 | */ |
| 1249 | if (!(epi->event.events & ~EP_PRIVATE_BITS)) |
| 1250 | goto is_disabled; |
| 1251 | |
| 1252 | /* If this file is already in the ready list we exit soon */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1253 | if (ep_is_linked(&epi->rdllink)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | goto is_linked; |
| 1255 | |
| 1256 | list_add_tail(&epi->rdllink, &ep->rdllist); |
| 1257 | |
| 1258 | is_linked: |
| 1259 | /* |
| 1260 | * Wake up ( if active ) both the eventpoll wait list and the ->poll() |
| 1261 | * wait list. |
| 1262 | */ |
| 1263 | if (waitqueue_active(&ep->wq)) |
Davide Libenzi | 3419b23 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 1264 | __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | |
| 1265 | TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | if (waitqueue_active(&ep->poll_wait)) |
| 1267 | pwake++; |
| 1268 | |
| 1269 | is_disabled: |
| 1270 | write_unlock_irqrestore(&ep->lock, flags); |
| 1271 | |
| 1272 | /* We have to call this outside the lock */ |
| 1273 | if (pwake) |
| 1274 | ep_poll_safewake(&psw, &ep->poll_wait); |
| 1275 | |
| 1276 | return 1; |
| 1277 | } |
| 1278 | |
| 1279 | |
| 1280 | static int ep_eventpoll_close(struct inode *inode, struct file *file) |
| 1281 | { |
| 1282 | struct eventpoll *ep = file->private_data; |
| 1283 | |
| 1284 | if (ep) { |
| 1285 | ep_free(ep); |
| 1286 | kfree(ep); |
| 1287 | } |
| 1288 | |
| 1289 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep)); |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait) |
| 1295 | { |
| 1296 | unsigned int pollflags = 0; |
| 1297 | unsigned long flags; |
| 1298 | struct eventpoll *ep = file->private_data; |
| 1299 | |
| 1300 | /* Insert inside our poll wait queue */ |
| 1301 | poll_wait(file, &ep->poll_wait, wait); |
| 1302 | |
| 1303 | /* Check our condition */ |
| 1304 | read_lock_irqsave(&ep->lock, flags); |
| 1305 | if (!list_empty(&ep->rdllist)) |
| 1306 | pollflags = POLLIN | POLLRDNORM; |
| 1307 | read_unlock_irqrestore(&ep->lock, flags); |
| 1308 | |
| 1309 | return pollflags; |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | /* |
| 1314 | * Since we have to release the lock during the __copy_to_user() operation and |
| 1315 | * during the f_op->poll() call, we try to collect the maximum number of items |
| 1316 | * by reducing the irqlock/irqunlock switching rate. |
| 1317 | */ |
| 1318 | static int ep_collect_ready_items(struct eventpoll *ep, struct list_head *txlist, int maxevents) |
| 1319 | { |
| 1320 | int nepi; |
| 1321 | unsigned long flags; |
| 1322 | struct list_head *lsthead = &ep->rdllist, *lnk; |
| 1323 | struct epitem *epi; |
| 1324 | |
| 1325 | write_lock_irqsave(&ep->lock, flags); |
| 1326 | |
| 1327 | for (nepi = 0, lnk = lsthead->next; lnk != lsthead && nepi < maxevents;) { |
| 1328 | epi = list_entry(lnk, struct epitem, rdllink); |
| 1329 | |
| 1330 | lnk = lnk->next; |
| 1331 | |
| 1332 | /* If this file is already in the ready list we exit soon */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1333 | if (!ep_is_linked(&epi->txlink)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | /* |
| 1335 | * This is initialized in this way so that the default |
| 1336 | * behaviour of the reinjecting code will be to push back |
| 1337 | * the item inside the ready list. |
| 1338 | */ |
| 1339 | epi->revents = epi->event.events; |
| 1340 | |
| 1341 | /* Link the ready item into the transfer list */ |
| 1342 | list_add(&epi->txlink, txlist); |
| 1343 | nepi++; |
| 1344 | |
| 1345 | /* |
| 1346 | * Unlink the item from the ready list. |
| 1347 | */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1348 | ep_list_del(&epi->rdllink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | write_unlock_irqrestore(&ep->lock, flags); |
| 1353 | |
| 1354 | return nepi; |
| 1355 | } |
| 1356 | |
| 1357 | |
| 1358 | /* |
| 1359 | * This function is called without holding the "ep->lock" since the call to |
| 1360 | * __copy_to_user() might sleep, and also f_op->poll() might reenable the IRQ |
| 1361 | * because of the way poll() is traditionally implemented in Linux. |
| 1362 | */ |
| 1363 | static int ep_send_events(struct eventpoll *ep, struct list_head *txlist, |
| 1364 | struct epoll_event __user *events) |
| 1365 | { |
| 1366 | int eventcnt = 0; |
| 1367 | unsigned int revents; |
| 1368 | struct list_head *lnk; |
| 1369 | struct epitem *epi; |
| 1370 | |
| 1371 | /* |
| 1372 | * We can loop without lock because this is a task private list. |
| 1373 | * The test done during the collection loop will guarantee us that |
| 1374 | * another task will not try to collect this file. Also, items |
| 1375 | * cannot vanish during the loop because we are holding "sem". |
| 1376 | */ |
| 1377 | list_for_each(lnk, txlist) { |
| 1378 | epi = list_entry(lnk, struct epitem, txlink); |
| 1379 | |
| 1380 | /* |
| 1381 | * Get the ready file event set. We can safely use the file |
| 1382 | * because we are holding the "sem" in read and this will |
| 1383 | * guarantee that both the file and the item will not vanish. |
| 1384 | */ |
| 1385 | revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL); |
| 1386 | |
| 1387 | /* |
| 1388 | * Set the return event set for the current file descriptor. |
| 1389 | * Note that only the task task was successfully able to link |
| 1390 | * the item to its "txlist" will write this field. |
| 1391 | */ |
| 1392 | epi->revents = revents & epi->event.events; |
| 1393 | |
| 1394 | if (epi->revents) { |
| 1395 | if (__put_user(epi->revents, |
| 1396 | &events[eventcnt].events) || |
| 1397 | __put_user(epi->event.data, |
| 1398 | &events[eventcnt].data)) |
| 1399 | return -EFAULT; |
| 1400 | if (epi->event.events & EPOLLONESHOT) |
| 1401 | epi->event.events &= EP_PRIVATE_BITS; |
| 1402 | eventcnt++; |
| 1403 | } |
| 1404 | } |
| 1405 | return eventcnt; |
| 1406 | } |
| 1407 | |
| 1408 | |
| 1409 | /* |
| 1410 | * Walk through the transfer list we collected with ep_collect_ready_items() |
| 1411 | * and, if 1) the item is still "alive" 2) its event set is not empty 3) it's |
| 1412 | * not already linked, links it to the ready list. Same as above, we are holding |
| 1413 | * "sem" so items cannot vanish underneath our nose. |
| 1414 | */ |
| 1415 | static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist) |
| 1416 | { |
| 1417 | int ricnt = 0, pwake = 0; |
| 1418 | unsigned long flags; |
| 1419 | struct epitem *epi; |
| 1420 | |
| 1421 | write_lock_irqsave(&ep->lock, flags); |
| 1422 | |
| 1423 | while (!list_empty(txlist)) { |
| 1424 | epi = list_entry(txlist->next, struct epitem, txlink); |
| 1425 | |
| 1426 | /* Unlink the current item from the transfer list */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1427 | ep_list_del(&epi->txlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | |
| 1429 | /* |
| 1430 | * If the item is no more linked to the interest set, we don't |
| 1431 | * have to push it inside the ready list because the following |
| 1432 | * ep_release_epitem() is going to drop it. Also, if the current |
| 1433 | * item is set to have an Edge Triggered behaviour, we don't have |
| 1434 | * to push it back either. |
| 1435 | */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 1436 | if (ep_rb_linked(&epi->rbn) && !(epi->event.events & EPOLLET) && |
| 1437 | (epi->revents & epi->event.events) && !ep_is_linked(&epi->rdllink)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | list_add_tail(&epi->rdllink, &ep->rdllist); |
| 1439 | ricnt++; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | if (ricnt) { |
| 1444 | /* |
| 1445 | * Wake up ( if active ) both the eventpoll wait list and the ->poll() |
| 1446 | * wait list. |
| 1447 | */ |
| 1448 | if (waitqueue_active(&ep->wq)) |
Davide Libenzi | 3419b23 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 1449 | __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | |
| 1450 | TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | if (waitqueue_active(&ep->poll_wait)) |
| 1452 | pwake++; |
| 1453 | } |
| 1454 | |
| 1455 | write_unlock_irqrestore(&ep->lock, flags); |
| 1456 | |
| 1457 | /* We have to call this outside the lock */ |
| 1458 | if (pwake) |
| 1459 | ep_poll_safewake(&psw, &ep->poll_wait); |
| 1460 | } |
| 1461 | |
| 1462 | |
| 1463 | /* |
| 1464 | * Perform the transfer of events to user space. |
| 1465 | */ |
| 1466 | static int ep_events_transfer(struct eventpoll *ep, |
| 1467 | struct epoll_event __user *events, int maxevents) |
| 1468 | { |
| 1469 | int eventcnt = 0; |
| 1470 | struct list_head txlist; |
| 1471 | |
| 1472 | INIT_LIST_HEAD(&txlist); |
| 1473 | |
| 1474 | /* |
| 1475 | * We need to lock this because we could be hit by |
| 1476 | * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL). |
| 1477 | */ |
| 1478 | down_read(&ep->sem); |
| 1479 | |
| 1480 | /* Collect/extract ready items */ |
| 1481 | if (ep_collect_ready_items(ep, &txlist, maxevents) > 0) { |
| 1482 | /* Build result set in userspace */ |
| 1483 | eventcnt = ep_send_events(ep, &txlist, events); |
| 1484 | |
| 1485 | /* Reinject ready items into the ready list */ |
| 1486 | ep_reinject_items(ep, &txlist); |
| 1487 | } |
| 1488 | |
| 1489 | up_read(&ep->sem); |
| 1490 | |
| 1491 | return eventcnt; |
| 1492 | } |
| 1493 | |
| 1494 | |
| 1495 | static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, |
| 1496 | int maxevents, long timeout) |
| 1497 | { |
| 1498 | int res, eavail; |
| 1499 | unsigned long flags; |
| 1500 | long jtimeout; |
| 1501 | wait_queue_t wait; |
| 1502 | |
| 1503 | /* |
| 1504 | * Calculate the timeout by checking for the "infinite" value ( -1 ) |
| 1505 | * and the overflow condition. The passed timeout is in milliseconds, |
| 1506 | * that why (t * HZ) / 1000. |
| 1507 | */ |
Davide Libenzi | e3306dd | 2005-09-27 21:45:33 -0700 | [diff] [blame] | 1508 | jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ? |
| 1509 | MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | |
| 1511 | retry: |
| 1512 | write_lock_irqsave(&ep->lock, flags); |
| 1513 | |
| 1514 | res = 0; |
| 1515 | if (list_empty(&ep->rdllist)) { |
| 1516 | /* |
| 1517 | * We don't have any available event to return to the caller. |
| 1518 | * We need to sleep here, and we will be wake up by |
| 1519 | * ep_poll_callback() when events will become available. |
| 1520 | */ |
| 1521 | init_waitqueue_entry(&wait, current); |
Davide Libenzi | 3419b23 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 1522 | __add_wait_queue(&ep->wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | |
| 1524 | for (;;) { |
| 1525 | /* |
| 1526 | * We don't want to sleep if the ep_poll_callback() sends us |
| 1527 | * a wakeup in between. That's why we set the task state |
| 1528 | * to TASK_INTERRUPTIBLE before doing the checks. |
| 1529 | */ |
| 1530 | set_current_state(TASK_INTERRUPTIBLE); |
| 1531 | if (!list_empty(&ep->rdllist) || !jtimeout) |
| 1532 | break; |
| 1533 | if (signal_pending(current)) { |
| 1534 | res = -EINTR; |
| 1535 | break; |
| 1536 | } |
| 1537 | |
| 1538 | write_unlock_irqrestore(&ep->lock, flags); |
| 1539 | jtimeout = schedule_timeout(jtimeout); |
| 1540 | write_lock_irqsave(&ep->lock, flags); |
| 1541 | } |
Davide Libenzi | 3419b23 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 1542 | __remove_wait_queue(&ep->wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | |
| 1544 | set_current_state(TASK_RUNNING); |
| 1545 | } |
| 1546 | |
| 1547 | /* Is it worth to try to dig for events ? */ |
| 1548 | eavail = !list_empty(&ep->rdllist); |
| 1549 | |
| 1550 | write_unlock_irqrestore(&ep->lock, flags); |
| 1551 | |
| 1552 | /* |
| 1553 | * Try to transfer events to user space. In case we get 0 events and |
| 1554 | * there's still timeout left over, we go trying again in search of |
| 1555 | * more luck. |
| 1556 | */ |
| 1557 | if (!res && eavail && |
| 1558 | !(res = ep_events_transfer(ep, events, maxevents)) && jtimeout) |
| 1559 | goto retry; |
| 1560 | |
| 1561 | return res; |
| 1562 | } |
| 1563 | |
| 1564 | |
| 1565 | static int eventpollfs_delete_dentry(struct dentry *dentry) |
| 1566 | { |
| 1567 | |
| 1568 | return 1; |
| 1569 | } |
| 1570 | |
| 1571 | |
| 1572 | static struct inode *ep_eventpoll_inode(void) |
| 1573 | { |
| 1574 | int error = -ENOMEM; |
| 1575 | struct inode *inode = new_inode(eventpoll_mnt->mnt_sb); |
| 1576 | |
| 1577 | if (!inode) |
| 1578 | goto eexit_1; |
| 1579 | |
| 1580 | inode->i_fop = &eventpoll_fops; |
| 1581 | |
| 1582 | /* |
| 1583 | * Mark the inode dirty from the very beginning, |
| 1584 | * that way it will never be moved to the dirty |
| 1585 | * list because mark_inode_dirty() will think |
| 1586 | * that it already _is_ on the dirty list. |
| 1587 | */ |
| 1588 | inode->i_state = I_DIRTY; |
| 1589 | inode->i_mode = S_IRUSR | S_IWUSR; |
| 1590 | inode->i_uid = current->fsuid; |
| 1591 | inode->i_gid = current->fsgid; |
| 1592 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 1593 | inode->i_blksize = PAGE_SIZE; |
| 1594 | return inode; |
| 1595 | |
| 1596 | eexit_1: |
| 1597 | return ERR_PTR(error); |
| 1598 | } |
| 1599 | |
| 1600 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1601 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | eventpollfs_get_sb(struct file_system_type *fs_type, int flags, |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1603 | const char *dev_name, void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1605 | return get_sb_pseudo(fs_type, "eventpoll:", NULL, EVENTPOLLFS_MAGIC, |
| 1606 | mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | |
| 1610 | static int __init eventpoll_init(void) |
| 1611 | { |
| 1612 | int error; |
| 1613 | |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 1614 | mutex_init(&epmutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
| 1616 | /* Initialize the structure used to perform safe poll wait head wake ups */ |
| 1617 | ep_poll_safewake_init(&psw); |
| 1618 | |
| 1619 | /* Allocates slab cache used to allocate "struct epitem" items */ |
| 1620 | epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem), |
| 1621 | 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC, |
| 1622 | NULL, NULL); |
| 1623 | |
| 1624 | /* Allocates slab cache used to allocate "struct eppoll_entry" */ |
| 1625 | pwq_cache = kmem_cache_create("eventpoll_pwq", |
| 1626 | sizeof(struct eppoll_entry), 0, |
| 1627 | EPI_SLAB_DEBUG|SLAB_PANIC, NULL, NULL); |
| 1628 | |
| 1629 | /* |
| 1630 | * Register the virtual file system that will be the source of inodes |
| 1631 | * for the eventpoll files |
| 1632 | */ |
| 1633 | error = register_filesystem(&eventpoll_fs_type); |
| 1634 | if (error) |
| 1635 | goto epanic; |
| 1636 | |
| 1637 | /* Mount the above commented virtual file system */ |
| 1638 | eventpoll_mnt = kern_mount(&eventpoll_fs_type); |
| 1639 | error = PTR_ERR(eventpoll_mnt); |
| 1640 | if (IS_ERR(eventpoll_mnt)) |
| 1641 | goto epanic; |
| 1642 | |
| 1643 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: successfully initialized.\n", |
| 1644 | current)); |
| 1645 | return 0; |
| 1646 | |
| 1647 | epanic: |
| 1648 | panic("eventpoll_init() failed\n"); |
| 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | static void __exit eventpoll_exit(void) |
| 1653 | { |
| 1654 | /* Undo all operations done inside eventpoll_init() */ |
| 1655 | unregister_filesystem(&eventpoll_fs_type); |
| 1656 | mntput(eventpoll_mnt); |
| 1657 | kmem_cache_destroy(pwq_cache); |
| 1658 | kmem_cache_destroy(epi_cache); |
| 1659 | } |
| 1660 | |
| 1661 | module_init(eventpoll_init); |
| 1662 | module_exit(eventpoll_exit); |
| 1663 | |
| 1664 | MODULE_LICENSE("GPL"); |