/* * fs/eventpoll.c ( Efficent event polling implementation ) * Copyright (C) 2001,...,2003 Davide Libenzi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Davide Libenzi <davidel@xmailserver.org> * */ #include <linux/module.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/fs.h>#include <linux/file.h>#include <linux/signal.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/poll.h>#include <linux/smp_lock.h>#include <linux/string.h>#include <linux/list.h>#include <linux/hash.h>#include <linux/spinlock.h>#include <linux/syscalls.h>#include <linux/rwsem.h>#include <linux/rbtree.h>#include <linux/wait.h>#include <linux/eventpoll.h>#include <linux/mount.h>#include <linux/bitops.h>#include <linux/mutex.h>#include <asm/uaccess.h>#include <asm/system.h>#include <asm/io.h>#include <asm/mman.h>#include <asm/atomic.h>#include <asm/semaphore.h> /* * LOCKING: * There are three level of locking required by epoll : * * 1) epmutex (mutex) * 2) ep->sem (rw_semaphore) * 3) ep->lock (rw_lock) * * The acquire order is the one listed above, from 1 to 3. * We need a spinlock (ep->lock) because we manipulate objects * from inside the poll callback, that might be triggered from * a wake_up() that in turn might be called from IRQ context. * So we can't sleep inside the poll callback and hence we need * a spinlock. During the event transfer loop (from kernel to * user space) we could end up sleeping due a copy_to_user(), so * we need a lock that will allow us to sleep. This lock is a * read-write semaphore (ep->sem). It is acquired on read during * the event transfer loop and in write during epoll_ctl(EPOLL_CTL_DEL) * and during eventpoll_release_file(). Then we also need a global * semaphore to serialize eventpoll_release_file() and ep_free(). * This semaphore is acquired by ep_free() during the epoll file * cleanup path and it is also acquired by eventpoll_release_file() * if a file has been pushed inside an epoll set and it is then * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL). * It is possible to drop the "ep->sem" and to use the global * semaphore "epmutex" (together with "ep->lock") to have it working, * but having "ep->sem" will make the interface more scalable. * Events that require holding "epmutex" are very rare, while for * normal operations the epoll private "ep->sem" will guarantee * a greater scalability. */ #define EVENTPOLLFS_MAGIC 0x03111965 /* My birthday should work for this :) */ #define DEBUG_EPOLL 0 #if DEBUG_EPOLL > 0#define DPRINTK(x) printk x#define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0)#else /* #if DEBUG_EPOLL > 0 */#define DPRINTK(x) (void) 0#define DNPRINTK(n, x) (void) 0#endif /* #if DEBUG_EPOLL > 0 */ #define DEBUG_EPI 0 #if DEBUG_EPI != 0#define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */)#else /* #if DEBUG_EPI != 0 */#define EPI_SLAB_DEBUG 0#endif /* #if DEBUG_EPI != 0 */ /* Epoll private bits inside the event mask */#define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET) /* Maximum number of poll wake up nests we are allowing */#define EP_MAX_POLLWAKE_NESTS 4 /* Maximum msec timeout value storeable in a long int */#define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ) struct epoll_filefd { struct file *file; int fd;}; /* * Node that is linked into the "wake_task_list" member of the "struct poll_safewake". * It is used to keep track on all tasks that are currently inside the wake_up() code * to 1) short-circuit the one coming from the same task and same wait queue head * ( loop ) 2) allow a maximum number of epoll descriptors inclusion nesting * 3) let go the ones coming from other tasks. */struct wake_task_node { struct list_head llink; task_t *task; wait_queue_head_t *wq;}; /* * This is used to implement the safe poll wake up avoiding to reenter * the poll callback from inside wake_up(). */struct poll_safewake { struct list_head wake_task_list; spinlock_t lock;}; /* * This structure is stored inside the "private_data" member of the file * structure and rapresent the main data sructure for the eventpoll * interface. */struct eventpoll { /* Protect the this structure access */ rwlock_t lock; /* * This semaphore is used to ensure that files are not removed * while epoll is using them. This is read-held during the event * collection loop and it is write-held during the file cleanup * path, the epoll file exit code and the ctl operations. */ struct rw_semaphore sem; /* Wait queue used by sys_epoll_wait() */ wait_queue_head_t wq; /* Wait queue used by file->poll() */ wait_queue_head_t poll_wait; /* List of ready file descriptors */ struct list_head rdllist; /* RB-Tree root used to store monitored fd structs */ struct rb_root rbr;}; /* Wait structure used by the poll hooks */struct eppoll_entry { /* List header used to link this structure to the "struct epitem" */ struct list_head llink; /* The "base" pointer is set to the container "struct epitem" */ void *base; /* * Wait queue item that will be linked to the target file wait * queue head. */ wait_queue_t wait; /* The wait queue head that linked the "wait" wait queue item */ wait_queue_head_t *whead;}; /* * Each file descriptor added to the eventpoll interface will * have an entry of this type linked to the hash. */struct epitem { /* RB-Tree node used to link this structure to the eventpoll rb-tree */ struct rb_node rbn; /* List header used to link this structure to the eventpoll ready list */ struct list_head rdllink; /* The file descriptor information this item refers to */ struct epoll_filefd ffd; /* Number of active wait queue attached to poll operations */ int nwait; /* List containing poll wait queues */ struct list_head pwqlist; /* The "container" of this item */ struct eventpoll *ep; /* The structure that describe the interested events and the source fd */ struct epoll_event event; /* * Used to keep track of the usage count of the structure. This avoids * that the structure will desappear from underneath our processing. */ atomic_t usecnt; /* List header used to link this item to the "struct file" items list */ struct list_head fllink; /* List header used to link the item to the transfer list */ struct list_head txlink; /* * This is used during the collection/transfer of events to userspace * to pin items empty events set. */ unsigned int revents;}; /* Wrapper struct used by poll queueing */struct ep_pqueue { poll_table pt; struct epitem *epi;}; static void ep_poll_safewake_init(struct poll_safewake *psw);static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq);static int ep_getfd(int *efd, struct inode **einode, struct file **efile, struct eventpoll *ep);static int ep_alloc(struct eventpoll **pep);static void ep_free(struct eventpoll *ep);static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd);static void ep_use_epitem(struct epitem *epi);static void ep_release_epitem(struct epitem *epi);static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, poll_table *pt);static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi);static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct file *tfile, int fd);static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event);static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi);static int ep_unlink(struct eventpoll *ep, struct epitem *epi);static int ep_remove(struct eventpoll *ep, struct epitem *epi);static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key);static int ep_eventpoll_close(struct inode *inode, struct file *file);static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait);static int ep_collect_ready_items(struct eventpoll *ep, struct list_head *txlist, int maxevents);static int ep_send_events(struct eventpoll *ep, struct list_head *txlist, struct epoll_event __user *events);static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist);static int ep_events_transfer(struct eventpoll *ep, struct epoll_event __user *events, int maxevents);static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, int maxevents, long timeout);static int eventpollfs_delete_dentry(struct dentry *dentry);static struct inode *ep_eventpoll_inode(void);static struct super_block *eventpollfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data); /* * This semaphore is used to serialize ep_free() and eventpoll_release_file(). */static struct mutex epmutex; /* Safe wake up implementation */static struct poll_safewake psw; /* Slab cache used to allocate "struct epitem" */static kmem_cache_t *epi_cache __read_mostly; /* Slab cache used to allocate "struct eppoll_entry" */static kmem_cache_t *pwq_cache __read_mostly; /* Virtual fs used to allocate inodes for eventpoll files */static struct vfsmount *eventpoll_mnt __read_mostly; /* File callbacks that implement the eventpoll file behaviour */static const struct file_operations eventpoll_fops = { .release = ep_eventpoll_close, .poll = ep_eventpoll_poll}; /* * This is used to register the virtual file system from where * eventpoll inodes are allocated. */static struct file_system_type eventpoll_fs_type = { .name = "eventpollfs", .get_sb = eventpollfs_get_sb, .kill_sb = kill_anon_super,}; /* Very basic directory entry operations for the eventpoll virtual file system */static struct dentry_operations eventpollfs_dentry_operations = { .d_delete = eventpollfs_delete_dentry,}; /* Fast test to see if the file is an evenpoll file */static inline int is_file_epoll(struct file *f){ return f->f_op == &eventpoll_fops;} /* Setup the structure that is used as key for the rb-tree */static inline void ep_set_ffd(struct epoll_filefd *ffd, struct file *file, int fd){ ffd->file = file; ffd->fd = fd;} /* Compare rb-tree keys */static inline int ep_cmp_ffd(struct epoll_filefd *p1, struct epoll_filefd *p2){ return (p1->file > p2->file ? +1: (p1->file < p2->file ? -1 : p1->fd - p2->fd));} /* Special initialization for the rb-tree node to detect linkage */static inline void ep_rb_initnode(struct rb_node *n){ rb_set_parent(n, n);} /* Removes a node from the rb-tree and marks it for a fast is-linked check */static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r){ rb_erase(n, r); rb_set_parent(n, n);} /* Fast check to verify that the item is linked to the main rb-tree */static inline int ep_rb_linked(struct rb_node *n){ return rb_parent(n) != n;} /* * Remove the item from the list and perform its initialization. * This is useful for us because we can test if the item is linked * using "ep_is_linked(p)". */static inline void ep_list_del(struct list_head *p){ list_del(p); INIT_LIST_HEAD(p);} /* Tells us if the item is currently linked */static inline int ep_is_linked(struct list_head *p){ return !list_empty(p);} /* Get the "struct epitem" from a wait queue pointer */static inline struct epitem * ep_item_from_wait(wait_queue_t *p){ return container_of(p, struct eppoll_entry, wait)->base;} /* Get the "struct epitem" from an epoll queue wrapper */static inline struct epitem * ep_item_from_epqueue(poll_table *p){ return container_of(p, struct ep_pqueue, pt)->epi;} /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */static inline int ep_op_hash_event(int op){ return op != EPOLL_CTL_DEL;} /* Initialize the poll safe wake up structure */static void ep_poll_safewake_init(struct poll_safewake *psw){ INIT_LIST_HEAD(&psw->wake_task_list); spin_lock_init(&psw->lock);} /* * Perform a safe wake up of the poll wait list. The problem is that * with the new callback'd wake up system, it is possible that the * poll callback is reentered from inside the call to wake_up() done * on the poll wait queue head. The rule is that we cannot reenter the * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times, * and we cannot reenter the same wait queue head at all. This will * enable to have a hierarchy of epoll file descriptor of no more than * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock * because this one gets called by the poll callback, that in turn is called * from inside a wake_up(), that might be called from irq context. */static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq){ int wake_nests = 0; unsigned long flags; task_t *this_task = current; struct list_head *lsthead = &psw->wake_task_list, *lnk; struct wake_task_node *tncur; struct wake_task_node tnode; spin_lock_irqsave(&psw->lock, flags); /* Try to see if the current task is already inside this wakeup call */ list_for_each(lnk, lsthead) { tncur = list_entry(lnk, struct wake_task_node, llink); if (tncur->wq == wq || (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) { /* * Ops ... loop detected or maximum nest level reached. * We abort this wake by breaking the cycle itself. */ spin_unlock_irqrestore(&psw->lock, flags); return; } } /* Add the current task to the list */ tnode.task = this_task; tnode.wq = wq; list_add(&tnode.llink, lsthead); spin_unlock_irqrestore(&psw->lock, flags); /* Do really wake up now */ wake_up(wq); /* Remove the current task from the list */ spin_lock_irqsave(&psw->lock, flags); list_del(&tnode.llink); spin_unlock_irqrestore(&psw->lock, flags);} /* * This is called from eventpoll_release() to unlink files from the eventpoll * interface. We need to have this facility to cleanup correctly files that are * closed without being removed from the eventpoll interface. */void eventpoll_release_file(struct file *file){ struct list_head *lsthead = &file->f_ep_links; struct eventpoll *ep; struct epitem *epi; /* * We don't want to get "file->f_ep_lock" because it is not * necessary. It is not necessary because we're in the "struct file" * cleanup path, and this means that noone is using this file anymore. * The only hit might come from ep_free() but by holding the semaphore * will correctly serialize the operation. We do need to acquire * "ep->sem" after "epmutex" because ep_remove() requires it when called * from anywhere but ep_free(). */ mutex_lock(&epmutex); while (!list_empty(lsthead)) { epi = list_entry(lsthead->next, struct epitem, fllink); ep = epi->ep; ep_list_del(&epi->fllink); down_write(&ep->sem); ep_remove(ep, epi); up_write(&ep->sem); } mutex_unlock(&epmutex);} /* * It opens an eventpoll file descriptor by suggesting a storage of "size" * file descriptors. The size parameter is just an hint about how to size * data structures. It won't prevent the user to store more than "size" * file descriptors inside the epoll interface. It is the kernel part of * the userspace epoll_create(2). */asmlinkage long sys_epoll_create(int size){ int error, fd;Lines 1–500 of 1660. Load more
/* * fs/eventpoll.c ( Efficent event polling implementation ) * Copyright (C) 2001,...,2003 Davide Libenzi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Davide Libenzi <davidel@xmailserver.org> * */ #include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/sched.h> #include <linux/fs.h> #include <linux/file.h> #include <linux/signal.h> #include <linux/errno.h> #include <linux/mm.h> #include <linux/slab.h> #include <linux/poll.h> #include <linux/smp_lock.h> #include <linux/string.h> #include <linux/list.h> #include <linux/hash.h> #include <linux/spinlock.h> #include <linux/syscalls.h> #include <linux/rwsem.h> #include <linux/rbtree.h> #include <linux/wait.h> #include <linux/eventpoll.h> #include <linux/mount.h> #include <linux/bitops.h> #include <linux/mutex.h> #include <asm/uaccess.h> #include <asm/system.h> #include <asm/io.h> #include <asm/mman.h> #include <asm/atomic.h> #include <asm/semaphore.h> /* * LOCKING: * There are three level of locking required by epoll : * * 1) epmutex (mutex) * 2) ep->sem (rw_semaphore) * 3) ep->lock (rw_lock) * * The acquire order is the one listed above, from 1 to 3. * We need a spinlock (ep->lock) because we manipulate objects * from inside the poll callback, that might be triggered from * a wake_up() that in turn might be called from IRQ context. * So we can't sleep inside the poll callback and hence we need * a spinlock. During the event transfer loop (from kernel to * user space) we could end up sleeping due a copy_to_user(), so * we need a lock that will allow us to sleep. This lock is a * read-write semaphore (ep->sem). It is acquired on read during * the event transfer loop and in write during epoll_ctl(EPOLL_CTL_DEL) * and during eventpoll_release_file(). Then we also need a global * semaphore to serialize eventpoll_release_file() and ep_free(). * This semaphore is acquired by ep_free() during the epoll file * cleanup path and it is also acquired by eventpoll_release_file() * if a file has been pushed inside an epoll set and it is then * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL). * It is possible to drop the "ep->sem" and to use the global * semaphore "epmutex" (together with "ep->lock") to have it working, * but having "ep->sem" will make the interface more scalable. * Events that require holding "epmutex" are very rare, while for * normal operations the epoll private "ep->sem" will guarantee * a greater scalability. */ #define EVENTPOLLFS_MAGIC 0x03111965 /* My birthday should work for this :) */ #define DEBUG_EPOLL 0 #if DEBUG_EPOLL > 0 #define DPRINTK(x) printk x #define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0) #else /* #if DEBUG_EPOLL > 0 */ #define DPRINTK(x) (void) 0 #define DNPRINTK(n, x) (void) 0 #endif /* #if DEBUG_EPOLL > 0 */ #define DEBUG_EPI 0 #if DEBUG_EPI != 0 #define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */) #else /* #if DEBUG_EPI != 0 */ #define EPI_SLAB_DEBUG 0 #endif /* #if DEBUG_EPI != 0 */ /* Epoll private bits inside the event mask */ #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET) /* Maximum number of poll wake up nests we are allowing */ #define EP_MAX_POLLWAKE_NESTS 4 /* Maximum msec timeout value storeable in a long int */ #define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ) struct epoll_filefd { struct file *file; int fd; }; /* * Node that is linked into the "wake_task_list" member of the "struct poll_safewake". * It is used to keep track on all tasks that are currently inside the wake_up() code * to 1) short-circuit the one coming from the same task and same wait queue head * ( loop ) 2) allow a maximum number of epoll descriptors inclusion nesting * 3) let go the ones coming from other tasks. */ struct wake_task_node { struct list_head llink; task_t *task; wait_queue_head_t *wq; }; /* * This is used to implement the safe poll wake up avoiding to reenter * the poll callback from inside wake_up(). */ struct poll_safewake { struct list_head wake_task_list; spinlock_t lock; }; /* * This structure is stored inside the "private_data" member of the file * structure and rapresent the main data sructure for the eventpoll * interface. */ struct eventpoll { /* Protect the this structure access */ rwlock_t lock; /* * This semaphore is used to ensure that files are not removed * while epoll is using them. This is read-held during the event * collection loop and it is write-held during the file cleanup * path, the epoll file exit code and the ctl operations. */ struct rw_semaphore sem; /* Wait queue used by sys_epoll_wait() */ wait_queue_head_t wq; /* Wait queue used by file->poll() */ wait_queue_head_t poll_wait; /* List of ready file descriptors */ struct list_head rdllist; /* RB-Tree root used to store monitored fd structs */ struct rb_root rbr; }; /* Wait structure used by the poll hooks */ struct eppoll_entry { /* List header used to link this structure to the "struct epitem" */ struct list_head llink; /* The "base" pointer is set to the container "struct epitem" */ void *base; /* * Wait queue item that will be linked to the target file wait * queue head. */ wait_queue_t wait; /* The wait queue head that linked the "wait" wait queue item */ wait_queue_head_t *whead; }; /* * Each file descriptor added to the eventpoll interface will * have an entry of this type linked to the hash. */ struct epitem { /* RB-Tree node used to link this structure to the eventpoll rb-tree */ struct rb_node rbn; /* List header used to link this structure to the eventpoll ready list */ struct list_head rdllink; /* The file descriptor information this item refers to */ struct epoll_filefd ffd; /* Number of active wait queue attached to poll operations */ int nwait; /* List containing poll wait queues */ struct list_head pwqlist; /* The "container" of this item */ struct eventpoll *ep; /* The structure that describe the interested events and the source fd */ struct epoll_event event; /* * Used to keep track of the usage count of the structure. This avoids * that the structure will desappear from underneath our processing. */ atomic_t usecnt; /* List header used to link this item to the "struct file" items list */ struct list_head fllink; /* List header used to link the item to the transfer list */ struct list_head txlink; /* * This is used during the collection/transfer of events to userspace * to pin items empty events set. */ unsigned int revents; }; /* Wrapper struct used by poll queueing */ struct ep_pqueue { poll_table pt; struct epitem *epi; }; static void ep_poll_safewake_init(struct poll_safewake *psw); static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq); static int ep_getfd(int *efd, struct inode **einode, struct file **efile, struct eventpoll *ep); static int ep_alloc(struct eventpoll **pep); static void ep_free(struct eventpoll *ep); static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd); static void ep_use_epitem(struct epitem *epi); static void ep_release_epitem(struct epitem *epi); static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, poll_table *pt); static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi); static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct file *tfile, int fd); static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event); static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi); static int ep_unlink(struct eventpoll *ep, struct epitem *epi); static int ep_remove(struct eventpoll *ep, struct epitem *epi); static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key); static int ep_eventpoll_close(struct inode *inode, struct file *file); static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait); static int ep_collect_ready_items(struct eventpoll *ep, struct list_head *txlist, int maxevents); static int ep_send_events(struct eventpoll *ep, struct list_head *txlist, struct epoll_event __user *events); static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist); static int ep_events_transfer(struct eventpoll *ep, struct epoll_event __user *events, int maxevents); static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, int maxevents, long timeout); static int eventpollfs_delete_dentry(struct dentry *dentry); static struct inode *ep_eventpoll_inode(void); static struct super_block *eventpollfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data); /* * This semaphore is used to serialize ep_free() and eventpoll_release_file(). */ static struct mutex epmutex; /* Safe wake up implementation */ static struct poll_safewake psw; /* Slab cache used to allocate "struct epitem" */ static kmem_cache_t *epi_cache __read_mostly; /* Slab cache used to allocate "struct eppoll_entry" */ static kmem_cache_t *pwq_cache __read_mostly; /* Virtual fs used to allocate inodes for eventpoll files */ static struct vfsmount *eventpoll_mnt __read_mostly; /* File callbacks that implement the eventpoll file behaviour */ static const struct file_operations eventpoll_fops = { .release = ep_eventpoll_close, .poll = ep_eventpoll_poll }; /* * This is used to register the virtual file system from where * eventpoll inodes are allocated. */ static struct file_system_type eventpoll_fs_type = { .name = "eventpollfs", .get_sb = eventpollfs_get_sb, .kill_sb = kill_anon_super, }; /* Very basic directory entry operations for the eventpoll virtual file system */ static struct dentry_operations eventpollfs_dentry_operations = { .d_delete = eventpollfs_delete_dentry, }; /* Fast test to see if the file is an evenpoll file */ static inline int is_file_epoll(struct file *f) { return f->f_op == &eventpoll_fops; } /* Setup the structure that is used as key for the rb-tree */ static inline void ep_set_ffd(struct epoll_filefd *ffd, struct file *file, int fd) { ffd->file = file; ffd->fd = fd; } /* Compare rb-tree keys */ static inline int ep_cmp_ffd(struct epoll_filefd *p1, struct epoll_filefd *p2) { return (p1->file > p2->file ? +1: (p1->file < p2->file ? -1 : p1->fd - p2->fd)); } /* Special initialization for the rb-tree node to detect linkage */ static inline void ep_rb_initnode(struct rb_node *n) { rb_set_parent(n, n); } /* Removes a node from the rb-tree and marks it for a fast is-linked check */ static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r) { rb_erase(n, r); rb_set_parent(n, n); } /* Fast check to verify that the item is linked to the main rb-tree */ static inline int ep_rb_linked(struct rb_node *n) { return rb_parent(n) != n; } /* * Remove the item from the list and perform its initialization. * This is useful for us because we can test if the item is linked * using "ep_is_linked(p)". */ static inline void ep_list_del(struct list_head *p) { list_del(p); INIT_LIST_HEAD(p); } /* Tells us if the item is currently linked */ static inline int ep_is_linked(struct list_head *p) { return !list_empty(p); } /* Get the "struct epitem" from a wait queue pointer */ static inline struct epitem * ep_item_from_wait(wait_queue_t *p) { return container_of(p, struct eppoll_entry, wait)->base; } /* Get the "struct epitem" from an epoll queue wrapper */ static inline struct epitem * ep_item_from_epqueue(poll_table *p) { return container_of(p, struct ep_pqueue, pt)->epi; } /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */ static inline int ep_op_hash_event(int op) { return op != EPOLL_CTL_DEL; } /* Initialize the poll safe wake up structure */ static void ep_poll_safewake_init(struct poll_safewake *psw) { INIT_LIST_HEAD(&psw->wake_task_list); spin_lock_init(&psw->lock); } /* * Perform a safe wake up of the poll wait list. The problem is that * with the new callback'd wake up system, it is possible that the * poll callback is reentered from inside the call to wake_up() done * on the poll wait queue head. The rule is that we cannot reenter the * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times, * and we cannot reenter the same wait queue head at all. This will * enable to have a hierarchy of epoll file descriptor of no more than * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock * because this one gets called by the poll callback, that in turn is called * from inside a wake_up(), that might be called from irq context. */ static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq) { int wake_nests = 0; unsigned long flags; task_t *this_task = current; struct list_head *lsthead = &psw->wake_task_list, *lnk; struct wake_task_node *tncur; struct wake_task_node tnode; spin_lock_irqsave(&psw->lock, flags); /* Try to see if the current task is already inside this wakeup call */ list_for_each(lnk, lsthead) { tncur = list_entry(lnk, struct wake_task_node, llink); if (tncur->wq == wq || (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) { /* * Ops ... loop detected or maximum nest level reached. * We abort this wake by breaking the cycle itself. */ spin_unlock_irqrestore(&psw->lock, flags); return; } } /* Add the current task to the list */ tnode.task = this_task; tnode.wq = wq; list_add(&tnode.llink, lsthead); spin_unlock_irqrestore(&psw->lock, flags); /* Do really wake up now */ wake_up(wq); /* Remove the current task from the list */ spin_lock_irqsave(&psw->lock, flags); list_del(&tnode.llink); spin_unlock_irqrestore(&psw->lock, flags); } /* * This is called from eventpoll_release() to unlink files from the eventpoll * interface. We need to have this facility to cleanup correctly files that are * closed without being removed from the eventpoll interface. */ void eventpoll_release_file(struct file *file) { struct list_head *lsthead = &file->f_ep_links; struct eventpoll *ep; struct epitem *epi; /* * We don't want to get "file->f_ep_lock" because it is not * necessary. It is not necessary because we're in the "struct file" * cleanup path, and this means that noone is using this file anymore. * The only hit might come from ep_free() but by holding the semaphore * will correctly serialize the operation. We do need to acquire * "ep->sem" after "epmutex" because ep_remove() requires it when called * from anywhere but ep_free(). */ mutex_lock(&epmutex); while (!list_empty(lsthead)) { epi = list_entry(lsthead->next, struct epitem, fllink); ep = epi->ep; ep_list_del(&epi->fllink); down_write(&ep->sem); ep_remove(ep, epi); up_write(&ep->sem); } mutex_unlock(&epmutex); } /* * It opens an eventpoll file descriptor by suggesting a storage of "size" * file descriptors. The size parameter is just an hint about how to size * data structures. It won't prevent the user to store more than "size" * file descriptors inside the epoll interface. It is the kernel part of * the userspace epoll_create(2). */ asmlinkage long sys_epoll_create(int size) { int error, fd;