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