Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 2 | * fs/eventpoll.c (Efficent event polling implementation) |
| 3 | * Copyright (C) 2001,...,2007 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/file.h> |
| 19 | #include <linux/signal.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/poll.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/string.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/hash.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | #include <linux/syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/rbtree.h> |
| 30 | #include <linux/wait.h> |
| 31 | #include <linux/eventpoll.h> |
| 32 | #include <linux/mount.h> |
| 33 | #include <linux/bitops.h> |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Davide Libenzi | da66f7c | 2007-05-10 22:23:21 -0700 | [diff] [blame] | 35 | #include <linux/anon_inodes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/uaccess.h> |
| 37 | #include <asm/system.h> |
| 38 | #include <asm/io.h> |
| 39 | #include <asm/mman.h> |
| 40 | #include <asm/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* |
| 43 | * LOCKING: |
| 44 | * There are three level of locking required by epoll : |
| 45 | * |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 46 | * 1) epmutex (mutex) |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 47 | * 2) ep->mtx (mutex) |
| 48 | * 3) ep->lock (spinlock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | * |
| 50 | * The acquire order is the one listed above, from 1 to 3. |
| 51 | * We need a spinlock (ep->lock) because we manipulate objects |
| 52 | * from inside the poll callback, that might be triggered from |
| 53 | * a wake_up() that in turn might be called from IRQ context. |
| 54 | * So we can't sleep inside the poll callback and hence we need |
| 55 | * a spinlock. During the event transfer loop (from kernel to |
| 56 | * user space) we could end up sleeping due a copy_to_user(), so |
| 57 | * we need a lock that will allow us to sleep. This lock is a |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 58 | * mutex (ep->mtx). It is acquired during the event transfer loop, |
| 59 | * during epoll_ctl(EPOLL_CTL_DEL) and during eventpoll_release_file(). |
| 60 | * Then we also need a global mutex to serialize eventpoll_release_file() |
| 61 | * and ep_free(). |
| 62 | * This mutex is acquired by ep_free() during the epoll file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * cleanup path and it is also acquired by eventpoll_release_file() |
| 64 | * if a file has been pushed inside an epoll set and it is then |
| 65 | * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL). |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 66 | * It is possible to drop the "ep->mtx" and to use the global |
| 67 | * mutex "epmutex" (together with "ep->lock") to have it working, |
| 68 | * but having "ep->mtx" will make the interface more scalable. |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 69 | * Events that require holding "epmutex" are very rare, while for |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 70 | * normal operations the epoll private "ep->mtx" will guarantee |
| 71 | * a better scalability. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | */ |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #define DEBUG_EPOLL 0 |
| 75 | |
| 76 | #if DEBUG_EPOLL > 0 |
| 77 | #define DPRINTK(x) printk x |
| 78 | #define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0) |
| 79 | #else /* #if DEBUG_EPOLL > 0 */ |
| 80 | #define DPRINTK(x) (void) 0 |
| 81 | #define DNPRINTK(n, x) (void) 0 |
| 82 | #endif /* #if DEBUG_EPOLL > 0 */ |
| 83 | |
| 84 | #define DEBUG_EPI 0 |
| 85 | |
| 86 | #if DEBUG_EPI != 0 |
| 87 | #define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */) |
| 88 | #else /* #if DEBUG_EPI != 0 */ |
| 89 | #define EPI_SLAB_DEBUG 0 |
| 90 | #endif /* #if DEBUG_EPI != 0 */ |
| 91 | |
| 92 | /* Epoll private bits inside the event mask */ |
| 93 | #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET) |
| 94 | |
| 95 | /* Maximum number of poll wake up nests we are allowing */ |
| 96 | #define EP_MAX_POLLWAKE_NESTS 4 |
| 97 | |
Davide Libenzi | e3306dd | 2005-09-27 21:45:33 -0700 | [diff] [blame] | 98 | /* Maximum msec timeout value storeable in a long int */ |
| 99 | #define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ) |
| 100 | |
Davide Libenzi | b611967 | 2006-10-11 01:21:44 -0700 | [diff] [blame] | 101 | #define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event)) |
| 102 | |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 103 | #define EP_UNACTIVE_PTR ((void *) -1L) |
| 104 | |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 105 | #define EP_ITEM_COST (sizeof(struct epitem) + sizeof(struct eppoll_entry)) |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | struct epoll_filefd { |
| 108 | struct file *file; |
| 109 | int fd; |
| 110 | }; |
| 111 | |
| 112 | /* |
| 113 | * Node that is linked into the "wake_task_list" member of the "struct poll_safewake". |
| 114 | * It is used to keep track on all tasks that are currently inside the wake_up() code |
| 115 | * to 1) short-circuit the one coming from the same task and same wait queue head |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 116 | * (loop) 2) allow a maximum number of epoll descriptors inclusion nesting |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * 3) let go the ones coming from other tasks. |
| 118 | */ |
| 119 | struct wake_task_node { |
| 120 | struct list_head llink; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 121 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | wait_queue_head_t *wq; |
| 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * This is used to implement the safe poll wake up avoiding to reenter |
| 127 | * the poll callback from inside wake_up(). |
| 128 | */ |
| 129 | struct poll_safewake { |
| 130 | struct list_head wake_task_list; |
| 131 | spinlock_t lock; |
| 132 | }; |
| 133 | |
| 134 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | * Each file descriptor added to the eventpoll interface will |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 136 | * have an entry of this type linked to the "rbr" RB tree. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | */ |
| 138 | struct epitem { |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 139 | /* RB tree node used to link this structure to the eventpoll RB tree */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | struct rb_node rbn; |
| 141 | |
| 142 | /* List header used to link this structure to the eventpoll ready list */ |
| 143 | struct list_head rdllink; |
| 144 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Works together "struct eventpoll"->ovflist in keeping the |
| 147 | * single linked chain of items. |
| 148 | */ |
| 149 | struct epitem *next; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* The file descriptor information this item refers to */ |
| 152 | struct epoll_filefd ffd; |
| 153 | |
| 154 | /* Number of active wait queue attached to poll operations */ |
| 155 | int nwait; |
| 156 | |
| 157 | /* List containing poll wait queues */ |
| 158 | struct list_head pwqlist; |
| 159 | |
| 160 | /* The "container" of this item */ |
| 161 | struct eventpoll *ep; |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* List header used to link this item to the "struct file" items list */ |
| 164 | struct list_head fllink; |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 165 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 166 | /* The structure that describe the interested events and the source fd */ |
| 167 | struct epoll_event event; |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | /* |
| 171 | * This structure is stored inside the "private_data" member of the file |
| 172 | * structure and rapresent the main data sructure for the eventpoll |
| 173 | * interface. |
| 174 | */ |
| 175 | struct eventpoll { |
| 176 | /* Protect the this structure access */ |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 177 | spinlock_t lock; |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * This mutex is used to ensure that files are not removed |
| 181 | * while epoll is using them. This is held during the event |
| 182 | * collection loop, the file cleanup path, the epoll file exit |
| 183 | * code and the ctl operations. |
| 184 | */ |
| 185 | struct mutex mtx; |
| 186 | |
| 187 | /* Wait queue used by sys_epoll_wait() */ |
| 188 | wait_queue_head_t wq; |
| 189 | |
| 190 | /* Wait queue used by file->poll() */ |
| 191 | wait_queue_head_t poll_wait; |
| 192 | |
| 193 | /* List of ready file descriptors */ |
| 194 | struct list_head rdllist; |
| 195 | |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 196 | /* RB tree root used to store monitored fd structs */ |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 197 | struct rb_root rbr; |
| 198 | |
| 199 | /* |
| 200 | * This is a single linked list that chains all the "struct epitem" that |
| 201 | * happened while transfering ready events to userspace w/out |
| 202 | * holding ->lock. |
| 203 | */ |
| 204 | struct epitem *ovflist; |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 205 | |
| 206 | /* The user that created the eventpoll descriptor */ |
| 207 | struct user_struct *user; |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | /* Wait structure used by the poll hooks */ |
| 211 | struct eppoll_entry { |
| 212 | /* List header used to link this structure to the "struct epitem" */ |
| 213 | struct list_head llink; |
| 214 | |
| 215 | /* The "base" pointer is set to the container "struct epitem" */ |
| 216 | void *base; |
| 217 | |
| 218 | /* |
| 219 | * Wait queue item that will be linked to the target file wait |
| 220 | * queue head. |
| 221 | */ |
| 222 | wait_queue_t wait; |
| 223 | |
| 224 | /* The wait queue head that linked the "wait" wait queue item */ |
| 225 | wait_queue_head_t *whead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | /* Wrapper struct used by poll queueing */ |
| 229 | struct ep_pqueue { |
| 230 | poll_table pt; |
| 231 | struct epitem *epi; |
| 232 | }; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 235 | * Configuration options available inside /proc/sys/fs/epoll/ |
| 236 | */ |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 237 | /* Maximum number of epoll watched descriptors, per user */ |
| 238 | static int max_user_watches __read_mostly; |
| 239 | |
| 240 | /* |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 241 | * This mutex is used to serialize ep_free() and eventpoll_release_file(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | */ |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 243 | static DEFINE_MUTEX(epmutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | /* Safe wake up implementation */ |
| 246 | static struct poll_safewake psw; |
| 247 | |
| 248 | /* Slab cache used to allocate "struct epitem" */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 249 | static struct kmem_cache *epi_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | /* Slab cache used to allocate "struct eppoll_entry" */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 252 | static struct kmem_cache *pwq_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 254 | #ifdef CONFIG_SYSCTL |
| 255 | |
| 256 | #include <linux/sysctl.h> |
| 257 | |
| 258 | static int zero; |
| 259 | |
| 260 | ctl_table epoll_table[] = { |
| 261 | { |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 262 | .procname = "max_user_watches", |
| 263 | .data = &max_user_watches, |
| 264 | .maxlen = sizeof(int), |
| 265 | .mode = 0644, |
| 266 | .proc_handler = &proc_dointvec_minmax, |
| 267 | .extra1 = &zero, |
| 268 | }, |
| 269 | { .ctl_name = 0 } |
| 270 | }; |
| 271 | #endif /* CONFIG_SYSCTL */ |
| 272 | |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 273 | |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 274 | /* Setup the structure that is used as key for the RB tree */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 275 | static inline void ep_set_ffd(struct epoll_filefd *ffd, |
| 276 | struct file *file, int fd) |
| 277 | { |
| 278 | ffd->file = file; |
| 279 | ffd->fd = fd; |
| 280 | } |
| 281 | |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 282 | /* Compare RB tree keys */ |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 283 | static inline int ep_cmp_ffd(struct epoll_filefd *p1, |
| 284 | struct epoll_filefd *p2) |
| 285 | { |
| 286 | return (p1->file > p2->file ? +1: |
| 287 | (p1->file < p2->file ? -1 : p1->fd - p2->fd)); |
| 288 | } |
| 289 | |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 290 | /* Tells us if the item is currently linked */ |
| 291 | static inline int ep_is_linked(struct list_head *p) |
| 292 | { |
| 293 | return !list_empty(p); |
| 294 | } |
| 295 | |
| 296 | /* Get the "struct epitem" from a wait queue pointer */ |
Davide Libenzi | cdac75e | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 297 | static inline struct epitem *ep_item_from_wait(wait_queue_t *p) |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 298 | { |
| 299 | return container_of(p, struct eppoll_entry, wait)->base; |
| 300 | } |
| 301 | |
| 302 | /* Get the "struct epitem" from an epoll queue wrapper */ |
Davide Libenzi | cdac75e | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 303 | static inline struct epitem *ep_item_from_epqueue(poll_table *p) |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 304 | { |
| 305 | return container_of(p, struct ep_pqueue, pt)->epi; |
| 306 | } |
| 307 | |
| 308 | /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */ |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 309 | static inline int ep_op_has_event(int op) |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 310 | { |
| 311 | return op != EPOLL_CTL_DEL; |
| 312 | } |
| 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | /* Initialize the poll safe wake up structure */ |
| 315 | static void ep_poll_safewake_init(struct poll_safewake *psw) |
| 316 | { |
| 317 | |
| 318 | INIT_LIST_HEAD(&psw->wake_task_list); |
| 319 | spin_lock_init(&psw->lock); |
| 320 | } |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Perform a safe wake up of the poll wait list. The problem is that |
| 324 | * with the new callback'd wake up system, it is possible that the |
| 325 | * poll callback is reentered from inside the call to wake_up() done |
| 326 | * on the poll wait queue head. The rule is that we cannot reenter the |
| 327 | * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times, |
| 328 | * and we cannot reenter the same wait queue head at all. This will |
| 329 | * enable to have a hierarchy of epoll file descriptor of no more than |
| 330 | * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock |
| 331 | * because this one gets called by the poll callback, that in turn is called |
| 332 | * from inside a wake_up(), that might be called from irq context. |
| 333 | */ |
| 334 | static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq) |
| 335 | { |
| 336 | int wake_nests = 0; |
| 337 | unsigned long flags; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 338 | struct task_struct *this_task = current; |
Matthias Kaehlcke | b70c394 | 2007-10-18 23:39:56 -0700 | [diff] [blame] | 339 | struct list_head *lsthead = &psw->wake_task_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | struct wake_task_node *tncur; |
| 341 | struct wake_task_node tnode; |
| 342 | |
| 343 | spin_lock_irqsave(&psw->lock, flags); |
| 344 | |
| 345 | /* Try to see if the current task is already inside this wakeup call */ |
Matthias Kaehlcke | b70c394 | 2007-10-18 23:39:56 -0700 | [diff] [blame] | 346 | list_for_each_entry(tncur, lsthead, llink) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | if (tncur->wq == wq || |
| 349 | (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) { |
| 350 | /* |
| 351 | * Ops ... loop detected or maximum nest level reached. |
| 352 | * We abort this wake by breaking the cycle itself. |
| 353 | */ |
| 354 | spin_unlock_irqrestore(&psw->lock, flags); |
| 355 | return; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* Add the current task to the list */ |
| 360 | tnode.task = this_task; |
| 361 | tnode.wq = wq; |
| 362 | list_add(&tnode.llink, lsthead); |
| 363 | |
| 364 | spin_unlock_irqrestore(&psw->lock, flags); |
| 365 | |
| 366 | /* Do really wake up now */ |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 367 | wake_up_nested(wq, 1 + wake_nests); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | /* Remove the current task from the list */ |
| 370 | spin_lock_irqsave(&psw->lock, flags); |
| 371 | list_del(&tnode.llink); |
| 372 | spin_unlock_irqrestore(&psw->lock, flags); |
| 373 | } |
| 374 | |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 375 | /* |
| 376 | * This function unregister poll callbacks from the associated file descriptor. |
| 377 | * Since this must be called without holding "ep->lock" the atomic exchange trick |
| 378 | * will protect us from multiple unregister. |
| 379 | */ |
| 380 | static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi) |
| 381 | { |
| 382 | int nwait; |
| 383 | struct list_head *lsthead = &epi->pwqlist; |
| 384 | struct eppoll_entry *pwq; |
| 385 | |
| 386 | /* This is called without locks, so we need the atomic exchange */ |
| 387 | nwait = xchg(&epi->nwait, 0); |
| 388 | |
| 389 | if (nwait) { |
| 390 | while (!list_empty(lsthead)) { |
| 391 | pwq = list_first_entry(lsthead, struct eppoll_entry, llink); |
| 392 | |
| 393 | list_del_init(&pwq->llink); |
| 394 | remove_wait_queue(pwq->whead, &pwq->wait); |
| 395 | kmem_cache_free(pwq_cache, pwq); |
| 396 | } |
| 397 | } |
| 398 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | /* |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 401 | * Removes a "struct epitem" from the eventpoll RB tree and deallocates |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 402 | * all the associated resources. Must be called with "mtx" held. |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 403 | */ |
| 404 | static int ep_remove(struct eventpoll *ep, struct epitem *epi) |
| 405 | { |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 406 | unsigned long flags; |
| 407 | struct file *file = epi->ffd.file; |
| 408 | |
| 409 | /* |
| 410 | * Removes poll wait queue hooks. We _have_ to do this without holding |
| 411 | * the "ep->lock" otherwise a deadlock might occur. This because of the |
| 412 | * sequence of the lock acquisition. Here we do "ep->lock" then the wait |
| 413 | * queue head lock when unregistering the wait queue. The wakeup callback |
| 414 | * will run by holding the wait queue head lock and will call our callback |
| 415 | * that will try to get "ep->lock". |
| 416 | */ |
| 417 | ep_unregister_pollwait(ep, epi); |
| 418 | |
| 419 | /* Remove the current item from the list of epoll hooks */ |
Jonathan Corbet | 6849991 | 2009-02-06 13:52:43 -0700 | [diff] [blame] | 420 | spin_lock(&file->f_lock); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 421 | if (ep_is_linked(&epi->fllink)) |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 422 | list_del_init(&epi->fllink); |
Jonathan Corbet | 6849991 | 2009-02-06 13:52:43 -0700 | [diff] [blame] | 423 | spin_unlock(&file->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Davide Libenzi | cdac75e | 2008-04-29 00:58:34 -0700 | [diff] [blame] | 425 | rb_erase(&epi->rbn, &ep->rbr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 427 | spin_lock_irqsave(&ep->lock, flags); |
| 428 | if (ep_is_linked(&epi->rdllink)) |
| 429 | list_del_init(&epi->rdllink); |
| 430 | spin_unlock_irqrestore(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 432 | /* At this point it is safe to free the eventpoll item */ |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 433 | kmem_cache_free(epi_cache, epi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 435 | atomic_dec(&ep->user->epoll_watches); |
| 436 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 437 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p)\n", |
| 438 | current, ep, file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 440 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | static void ep_free(struct eventpoll *ep) |
| 444 | { |
| 445 | struct rb_node *rbp; |
| 446 | struct epitem *epi; |
| 447 | |
| 448 | /* We need to release all tasks waiting for these file */ |
| 449 | if (waitqueue_active(&ep->poll_wait)) |
| 450 | ep_poll_safewake(&psw, &ep->poll_wait); |
| 451 | |
| 452 | /* |
| 453 | * We need to lock this because we could be hit by |
| 454 | * eventpoll_release_file() while we're freeing the "struct eventpoll". |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 455 | * We do not need to hold "ep->mtx" here because the epoll file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | * is on the way to be removed and no one has references to it |
| 457 | * 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] | 458 | * holding "epmutex" is sufficent here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | */ |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 460 | mutex_lock(&epmutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * Walks through the whole tree by unregistering poll callbacks. |
| 464 | */ |
| 465 | for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) { |
| 466 | epi = rb_entry(rbp, struct epitem, rbn); |
| 467 | |
| 468 | ep_unregister_pollwait(ep, epi); |
| 469 | } |
| 470 | |
| 471 | /* |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 472 | * Walks through the whole tree by freeing each "struct epitem". At this |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | * point we are sure no poll callbacks will be lingering around, and also by |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 474 | * holding "epmutex" we can be sure that no file cleanup code will hit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | * us during this operation. So we can avoid the lock on "ep->lock". |
| 476 | */ |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 477 | while ((rbp = rb_first(&ep->rbr)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | epi = rb_entry(rbp, struct epitem, rbn); |
| 479 | ep_remove(ep, epi); |
| 480 | } |
| 481 | |
Arjan van de Ven | 144efe3 | 2006-03-23 03:00:32 -0800 | [diff] [blame] | 482 | mutex_unlock(&epmutex); |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 483 | mutex_destroy(&ep->mtx); |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 484 | free_uid(ep->user); |
Davide Libenzi | f0ee9aa | 2007-05-15 01:40:57 -0700 | [diff] [blame] | 485 | kfree(ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 488 | static int ep_eventpoll_release(struct inode *inode, struct file *file) |
| 489 | { |
| 490 | struct eventpoll *ep = file->private_data; |
| 491 | |
Davide Libenzi | f0ee9aa | 2007-05-15 01:40:57 -0700 | [diff] [blame] | 492 | if (ep) |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 493 | ep_free(ep); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 494 | |
| 495 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep)); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait) |
| 500 | { |
| 501 | unsigned int pollflags = 0; |
| 502 | unsigned long flags; |
| 503 | struct eventpoll *ep = file->private_data; |
| 504 | |
| 505 | /* Insert inside our poll wait queue */ |
| 506 | poll_wait(file, &ep->poll_wait, wait); |
| 507 | |
| 508 | /* Check our condition */ |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 509 | spin_lock_irqsave(&ep->lock, flags); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 510 | if (!list_empty(&ep->rdllist)) |
| 511 | pollflags = POLLIN | POLLRDNORM; |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 512 | spin_unlock_irqrestore(&ep->lock, flags); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 513 | |
| 514 | return pollflags; |
| 515 | } |
| 516 | |
| 517 | /* File callbacks that implement the eventpoll file behaviour */ |
| 518 | static const struct file_operations eventpoll_fops = { |
| 519 | .release = ep_eventpoll_release, |
| 520 | .poll = ep_eventpoll_poll |
| 521 | }; |
| 522 | |
| 523 | /* Fast test to see if the file is an evenpoll file */ |
| 524 | static inline int is_file_epoll(struct file *f) |
| 525 | { |
| 526 | return f->f_op == &eventpoll_fops; |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * This is called from eventpoll_release() to unlink files from the eventpoll |
| 531 | * interface. We need to have this facility to cleanup correctly files that are |
| 532 | * closed without being removed from the eventpoll interface. |
| 533 | */ |
| 534 | void eventpoll_release_file(struct file *file) |
| 535 | { |
| 536 | struct list_head *lsthead = &file->f_ep_links; |
| 537 | struct eventpoll *ep; |
| 538 | struct epitem *epi; |
| 539 | |
| 540 | /* |
Jonathan Corbet | 6849991 | 2009-02-06 13:52:43 -0700 | [diff] [blame] | 541 | * We don't want to get "file->f_lock" because it is not |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 542 | * necessary. It is not necessary because we're in the "struct file" |
| 543 | * cleanup path, and this means that noone is using this file anymore. |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 544 | * So, for example, epoll_ctl() cannot hit here sicne if we reach this |
| 545 | * point, the file counter already went to zero and fget() would fail. |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 546 | * The only hit might come from ep_free() but by holding the mutex |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 547 | * will correctly serialize the operation. We do need to acquire |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 548 | * "ep->mtx" after "epmutex" because ep_remove() requires it when called |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 549 | * from anywhere but ep_free(). |
Jonathan Corbet | 6849991 | 2009-02-06 13:52:43 -0700 | [diff] [blame] | 550 | * |
| 551 | * Besides, ep_remove() acquires the lock, so we can't hold it here. |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 552 | */ |
| 553 | mutex_lock(&epmutex); |
| 554 | |
| 555 | while (!list_empty(lsthead)) { |
| 556 | epi = list_first_entry(lsthead, struct epitem, fllink); |
| 557 | |
| 558 | ep = epi->ep; |
| 559 | list_del_init(&epi->fllink); |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 560 | mutex_lock(&ep->mtx); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 561 | ep_remove(ep, epi); |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 562 | mutex_unlock(&ep->mtx); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | mutex_unlock(&epmutex); |
| 566 | } |
| 567 | |
| 568 | static int ep_alloc(struct eventpoll **pep) |
| 569 | { |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 570 | int error; |
| 571 | struct user_struct *user; |
| 572 | struct eventpoll *ep; |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 573 | |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 574 | user = get_current_user(); |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 575 | error = -ENOMEM; |
| 576 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
| 577 | if (unlikely(!ep)) |
| 578 | goto free_uid; |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 579 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 580 | spin_lock_init(&ep->lock); |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 581 | mutex_init(&ep->mtx); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 582 | init_waitqueue_head(&ep->wq); |
| 583 | init_waitqueue_head(&ep->poll_wait); |
| 584 | INIT_LIST_HEAD(&ep->rdllist); |
| 585 | ep->rbr = RB_ROOT; |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 586 | ep->ovflist = EP_UNACTIVE_PTR; |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 587 | ep->user = user; |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 588 | |
| 589 | *pep = ep; |
| 590 | |
| 591 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n", |
| 592 | current, ep)); |
| 593 | return 0; |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 594 | |
| 595 | free_uid: |
| 596 | free_uid(user); |
| 597 | return error; |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | /* |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 601 | * Search the file inside the eventpoll tree. The RB tree operations |
| 602 | * are protected by the "mtx" mutex, and ep_find() must be called with |
| 603 | * "mtx" held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | */ |
| 605 | static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd) |
| 606 | { |
| 607 | int kcmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | struct rb_node *rbp; |
| 609 | struct epitem *epi, *epir = NULL; |
| 610 | struct epoll_filefd ffd; |
| 611 | |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 612 | ep_set_ffd(&ffd, file, fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | for (rbp = ep->rbr.rb_node; rbp; ) { |
| 614 | epi = rb_entry(rbp, struct epitem, rbn); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 615 | kcmp = ep_cmp_ffd(&ffd, &epi->ffd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | if (kcmp > 0) |
| 617 | rbp = rbp->rb_right; |
| 618 | else if (kcmp < 0) |
| 619 | rbp = rbp->rb_left; |
| 620 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | epir = epi; |
| 622 | break; |
| 623 | } |
| 624 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n", |
| 627 | current, file, epir)); |
| 628 | |
| 629 | return epir; |
| 630 | } |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | /* |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 633 | * This is the callback that is passed to the wait queue wakeup |
| 634 | * machanism. It is called by the stored file descriptors when they |
| 635 | * have events to report. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | */ |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 637 | static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 639 | int pwake = 0; |
| 640 | unsigned long flags; |
| 641 | struct epitem *epi = ep_item_from_wait(wait); |
| 642 | struct eventpoll *ep = epi->ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 644 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n", |
| 645 | current, epi->ffd.file, epi, ep)); |
| 646 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 647 | spin_lock_irqsave(&ep->lock, flags); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 648 | |
| 649 | /* |
| 650 | * If the event mask does not contain any poll(2) event, we consider the |
| 651 | * descriptor to be disabled. This condition is likely the effect of the |
| 652 | * EPOLLONESHOT bit that disables the descriptor when an event is received, |
| 653 | * until the next EPOLL_CTL_MOD will be issued. |
| 654 | */ |
| 655 | if (!(epi->event.events & ~EP_PRIVATE_BITS)) |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 656 | goto out_unlock; |
| 657 | |
| 658 | /* |
| 659 | * If we are trasfering events to userspace, we can hold no locks |
| 660 | * (because we're accessing user memory, and because of linux f_op->poll() |
| 661 | * semantics). All the events that happens during that period of time are |
| 662 | * chained in ep->ovflist and requeued later on. |
| 663 | */ |
| 664 | if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) { |
| 665 | if (epi->next == EP_UNACTIVE_PTR) { |
| 666 | epi->next = ep->ovflist; |
| 667 | ep->ovflist = epi; |
| 668 | } |
| 669 | goto out_unlock; |
| 670 | } |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 671 | |
| 672 | /* If this file is already in the ready list we exit soon */ |
| 673 | if (ep_is_linked(&epi->rdllink)) |
| 674 | goto is_linked; |
| 675 | |
| 676 | list_add_tail(&epi->rdllink, &ep->rdllist); |
| 677 | |
| 678 | is_linked: |
| 679 | /* |
| 680 | * Wake up ( if active ) both the eventpoll wait list and the ->poll() |
| 681 | * wait list. |
| 682 | */ |
| 683 | if (waitqueue_active(&ep->wq)) |
Matthew Wilcox | 4a6e9e2 | 2007-08-30 16:10:22 -0400 | [diff] [blame] | 684 | wake_up_locked(&ep->wq); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 685 | if (waitqueue_active(&ep->poll_wait)) |
| 686 | pwake++; |
| 687 | |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 688 | out_unlock: |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 689 | spin_unlock_irqrestore(&ep->lock, flags); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 690 | |
| 691 | /* We have to call this outside the lock */ |
| 692 | if (pwake) |
| 693 | ep_poll_safewake(&psw, &ep->poll_wait); |
| 694 | |
| 695 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | /* |
| 699 | * This is the callback that is used to add our wait queue to the |
| 700 | * target file wakeup lists. |
| 701 | */ |
| 702 | static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, |
| 703 | poll_table *pt) |
| 704 | { |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 705 | struct epitem *epi = ep_item_from_epqueue(pt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | struct eppoll_entry *pwq; |
| 707 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 708 | if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | init_waitqueue_func_entry(&pwq->wait, ep_poll_callback); |
| 710 | pwq->whead = whead; |
| 711 | pwq->base = epi; |
| 712 | add_wait_queue(whead, &pwq->wait); |
| 713 | list_add_tail(&pwq->llink, &epi->pwqlist); |
| 714 | epi->nwait++; |
| 715 | } else { |
| 716 | /* We have to signal that an error occurred */ |
| 717 | epi->nwait = -1; |
| 718 | } |
| 719 | } |
| 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi) |
| 722 | { |
| 723 | int kcmp; |
| 724 | struct rb_node **p = &ep->rbr.rb_node, *parent = NULL; |
| 725 | struct epitem *epic; |
| 726 | |
| 727 | while (*p) { |
| 728 | parent = *p; |
| 729 | epic = rb_entry(parent, struct epitem, rbn); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 730 | kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if (kcmp > 0) |
| 732 | p = &parent->rb_right; |
| 733 | else |
| 734 | p = &parent->rb_left; |
| 735 | } |
| 736 | rb_link_node(&epi->rbn, parent, p); |
| 737 | rb_insert_color(&epi->rbn, &ep->rbr); |
| 738 | } |
| 739 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 740 | /* |
| 741 | * Must be called with "mtx" held. |
| 742 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | static int ep_insert(struct eventpoll *ep, struct epoll_event *event, |
| 744 | struct file *tfile, int fd) |
| 745 | { |
| 746 | int error, revents, pwake = 0; |
| 747 | unsigned long flags; |
| 748 | struct epitem *epi; |
| 749 | struct ep_pqueue epq; |
| 750 | |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 751 | if (unlikely(atomic_read(&ep->user->epoll_watches) >= |
| 752 | max_user_watches)) |
| 753 | return -ENOSPC; |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 754 | if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL))) |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 755 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
| 757 | /* Item initialization follow here ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | INIT_LIST_HEAD(&epi->rdllink); |
| 759 | INIT_LIST_HEAD(&epi->fllink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | INIT_LIST_HEAD(&epi->pwqlist); |
| 761 | epi->ep = ep; |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 762 | ep_set_ffd(&epi->ffd, tfile, fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | epi->event = *event; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | epi->nwait = 0; |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 765 | epi->next = EP_UNACTIVE_PTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
| 767 | /* Initialize the poll table using the queue callback */ |
| 768 | epq.epi = epi; |
| 769 | init_poll_funcptr(&epq.pt, ep_ptable_queue_proc); |
| 770 | |
| 771 | /* |
| 772 | * Attach the item to the poll hooks and get current event bits. |
| 773 | * We can safely use the file* here because its usage count has |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 774 | * been increased by the caller of this function. Note that after |
| 775 | * this operation completes, the poll callback can start hitting |
| 776 | * the new item. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | */ |
| 778 | revents = tfile->f_op->poll(tfile, &epq.pt); |
| 779 | |
| 780 | /* |
| 781 | * We have to check if something went wrong during the poll wait queue |
| 782 | * install process. Namely an allocation for a wait queue failed due |
| 783 | * high memory pressure. |
| 784 | */ |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 785 | error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | if (epi->nwait < 0) |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 787 | goto error_unregister; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
| 789 | /* Add the current item to the list of active epoll hook for this file */ |
Jonathan Corbet | 6849991 | 2009-02-06 13:52:43 -0700 | [diff] [blame] | 790 | spin_lock(&tfile->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | list_add_tail(&epi->fllink, &tfile->f_ep_links); |
Jonathan Corbet | 6849991 | 2009-02-06 13:52:43 -0700 | [diff] [blame] | 792 | spin_unlock(&tfile->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 794 | /* |
| 795 | * Add the current item to the RB tree. All RB tree operations are |
| 796 | * protected by "mtx", and ep_insert() is called with "mtx" held. |
| 797 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | ep_rbtree_insert(ep, epi); |
| 799 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 800 | /* We have to drop the new item inside our item list to keep track of it */ |
| 801 | spin_lock_irqsave(&ep->lock, flags); |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | /* 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] | 804 | if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | list_add_tail(&epi->rdllink, &ep->rdllist); |
| 806 | |
| 807 | /* Notify waiting tasks that events are available */ |
| 808 | if (waitqueue_active(&ep->wq)) |
Matthew Wilcox | 4a6e9e2 | 2007-08-30 16:10:22 -0400 | [diff] [blame] | 809 | wake_up_locked(&ep->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | if (waitqueue_active(&ep->poll_wait)) |
| 811 | pwake++; |
| 812 | } |
| 813 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 814 | spin_unlock_irqrestore(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 816 | atomic_inc(&ep->user->epoll_watches); |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | /* We have to call this outside the lock */ |
| 819 | if (pwake) |
| 820 | ep_poll_safewake(&psw, &ep->poll_wait); |
| 821 | |
| 822 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n", |
| 823 | current, ep, tfile, fd)); |
| 824 | |
| 825 | return 0; |
| 826 | |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 827 | error_unregister: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | ep_unregister_pollwait(ep, epi); |
| 829 | |
| 830 | /* |
| 831 | * We need to do this because an event could have been arrived on some |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 832 | * allocated wait queue. Note that we don't care about the ep->ovflist |
| 833 | * list, since that is used/cleaned only inside a section bound by "mtx". |
| 834 | * And ep_insert() is called with "mtx" held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | */ |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 836 | spin_lock_irqsave(&ep->lock, flags); |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 837 | if (ep_is_linked(&epi->rdllink)) |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 838 | list_del_init(&epi->rdllink); |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 839 | spin_unlock_irqrestore(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
Pekka Enberg | b030a4d | 2005-06-23 00:10:03 -0700 | [diff] [blame] | 841 | kmem_cache_free(epi_cache, epi); |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | return error; |
| 844 | } |
| 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | /* |
| 847 | * Modify the interest event mask by dropping an event if the new mask |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 848 | * has a match in the current file status. Must be called with "mtx" held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | */ |
| 850 | static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event) |
| 851 | { |
| 852 | int pwake = 0; |
| 853 | unsigned int revents; |
| 854 | unsigned long flags; |
| 855 | |
| 856 | /* |
| 857 | * Set the new event interest mask before calling f_op->poll(), otherwise |
| 858 | * a potential race might occur. In fact if we do this operation inside |
| 859 | * the lock, an event might happen between the f_op->poll() call and the |
| 860 | * new event set registering. |
| 861 | */ |
| 862 | epi->event.events = event->events; |
| 863 | |
| 864 | /* |
| 865 | * Get current event bits. We can safely use the file* here because |
| 866 | * its usage count has been increased by the caller of this function. |
| 867 | */ |
| 868 | revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL); |
| 869 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 870 | spin_lock_irqsave(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
| 872 | /* Copy the data member from inside the lock */ |
| 873 | epi->event.data = event->data; |
| 874 | |
| 875 | /* |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 876 | * If the item is "hot" and it is not registered inside the ready |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 877 | * list, push it inside. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | */ |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 879 | if (revents & event->events) { |
| 880 | if (!ep_is_linked(&epi->rdllink)) { |
| 881 | list_add_tail(&epi->rdllink, &ep->rdllist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 883 | /* Notify waiting tasks that events are available */ |
| 884 | if (waitqueue_active(&ep->wq)) |
Matthew Wilcox | 4a6e9e2 | 2007-08-30 16:10:22 -0400 | [diff] [blame] | 885 | wake_up_locked(&ep->wq); |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 886 | if (waitqueue_active(&ep->poll_wait)) |
| 887 | pwake++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | } |
| 889 | } |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 890 | spin_unlock_irqrestore(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
| 892 | /* We have to call this outside the lock */ |
| 893 | if (pwake) |
| 894 | ep_poll_safewake(&psw, &ep->poll_wait); |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 899 | static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *events, |
| 900 | int maxevents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | { |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 902 | int eventcnt, error = -EFAULT, pwake = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | unsigned int revents; |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 904 | unsigned long flags; |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 905 | struct epitem *epi, *nepi; |
| 906 | struct list_head txlist; |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 907 | |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 908 | INIT_LIST_HEAD(&txlist); |
| 909 | |
| 910 | /* |
| 911 | * We need to lock this because we could be hit by |
| 912 | * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL). |
| 913 | */ |
| 914 | mutex_lock(&ep->mtx); |
| 915 | |
| 916 | /* |
| 917 | * Steal the ready list, and re-init the original one to the |
| 918 | * empty list. Also, set ep->ovflist to NULL so that events |
| 919 | * happening while looping w/out locks, are not lost. We cannot |
| 920 | * have the poll callback to queue directly on ep->rdllist, |
| 921 | * because we are doing it in the loop below, in a lockless way. |
| 922 | */ |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 923 | spin_lock_irqsave(&ep->lock, flags); |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 924 | list_splice(&ep->rdllist, &txlist); |
| 925 | INIT_LIST_HEAD(&ep->rdllist); |
| 926 | ep->ovflist = NULL; |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 927 | spin_unlock_irqrestore(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
| 929 | /* |
| 930 | * We can loop without lock because this is a task private list. |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 931 | * We just splice'd out the ep->rdllist in ep_collect_ready_items(). |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 932 | * Items cannot vanish during the loop because we are holding "mtx". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | */ |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 934 | for (eventcnt = 0; !list_empty(&txlist) && eventcnt < maxevents;) { |
| 935 | epi = list_first_entry(&txlist, struct epitem, rdllink); |
| 936 | |
| 937 | list_del_init(&epi->rdllink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
| 939 | /* |
| 940 | * Get the ready file event set. We can safely use the file |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 941 | * because we are holding the "mtx" and this will guarantee |
| 942 | * that both the file and the item will not vanish. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | */ |
| 944 | revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL); |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 945 | revents &= epi->event.events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
| 947 | /* |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 948 | * Is the event mask intersect the caller-requested one, |
| 949 | * deliver the event to userspace. Again, we are holding |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 950 | * "mtx", so no operations coming from userspace can change |
| 951 | * the item. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | */ |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 953 | if (revents) { |
| 954 | if (__put_user(revents, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | &events[eventcnt].events) || |
| 956 | __put_user(epi->event.data, |
| 957 | &events[eventcnt].data)) |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 958 | goto errxit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | if (epi->event.events & EPOLLONESHOT) |
| 960 | epi->event.events &= EP_PRIVATE_BITS; |
| 961 | eventcnt++; |
| 962 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | /* |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 964 | * At this point, noone can insert into ep->rdllist besides |
| 965 | * us. The epoll_ctl() callers are locked out by us holding |
| 966 | * "mtx" and the poll callback will queue them in ep->ovflist. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | */ |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 968 | if (!(epi->event.events & EPOLLET) && |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 969 | (revents & epi->event.events)) |
| 970 | list_add_tail(&epi->rdllink, &ep->rdllist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 972 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 974 | errxit: |
| 975 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 976 | spin_lock_irqsave(&ep->lock, flags); |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 977 | /* |
| 978 | * During the time we spent in the loop above, some other events |
| 979 | * might have been queued by the poll callback. We re-insert them |
Davide Libenzi | f337b9c | 2008-10-15 22:01:56 -0700 | [diff] [blame] | 980 | * inside the main ready-list here. |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 981 | */ |
| 982 | for (nepi = ep->ovflist; (epi = nepi) != NULL; |
Davide Libenzi | 9ce209d | 2008-10-17 16:17:40 -0700 | [diff] [blame] | 983 | nepi = epi->next, epi->next = EP_UNACTIVE_PTR) { |
| 984 | /* |
| 985 | * If the above loop quit with errors, the epoll item might still |
| 986 | * be linked to "txlist", and the list_splice() done below will |
| 987 | * take care of those cases. |
| 988 | */ |
| 989 | if (!ep_is_linked(&epi->rdllink)) |
| 990 | list_add_tail(&epi->rdllink, &ep->rdllist); |
| 991 | } |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 992 | /* |
| 993 | * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after |
| 994 | * releasing the lock, events will be queued in the normal way inside |
| 995 | * ep->rdllist. |
| 996 | */ |
| 997 | ep->ovflist = EP_UNACTIVE_PTR; |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 998 | |
| 999 | /* |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 1000 | * In case of error in the event-send loop, or in case the number of |
| 1001 | * ready events exceeds the userspace limit, we need to splice the |
| 1002 | * "txlist" back inside ep->rdllist. |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 1003 | */ |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 1004 | list_splice(&txlist, &ep->rdllist); |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 1005 | |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 1006 | if (!list_empty(&ep->rdllist)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | /* |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 1008 | * Wake up (if active) both the eventpoll wait list and the ->poll() |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 1009 | * wait list (delayed after we release the lock). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | */ |
| 1011 | if (waitqueue_active(&ep->wq)) |
Matthew Wilcox | 4a6e9e2 | 2007-08-30 16:10:22 -0400 | [diff] [blame] | 1012 | wake_up_locked(&ep->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | if (waitqueue_active(&ep->poll_wait)) |
| 1014 | pwake++; |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 1015 | } |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 1016 | spin_unlock_irqrestore(&ep->lock, flags); |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 1017 | |
| 1018 | mutex_unlock(&ep->mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
| 1020 | /* We have to call this outside the lock */ |
| 1021 | if (pwake) |
| 1022 | ep_poll_safewake(&psw, &ep->poll_wait); |
Davide Libenzi | 6192bd5 | 2007-05-08 00:25:41 -0700 | [diff] [blame] | 1023 | |
| 1024 | return eventcnt == 0 ? error: eventcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, |
| 1028 | int maxevents, long timeout) |
| 1029 | { |
| 1030 | int res, eavail; |
| 1031 | unsigned long flags; |
| 1032 | long jtimeout; |
| 1033 | wait_queue_t wait; |
| 1034 | |
| 1035 | /* |
| 1036 | * Calculate the timeout by checking for the "infinite" value ( -1 ) |
| 1037 | * and the overflow condition. The passed timeout is in milliseconds, |
| 1038 | * that why (t * HZ) / 1000. |
| 1039 | */ |
Davide Libenzi | e3306dd | 2005-09-27 21:45:33 -0700 | [diff] [blame] | 1040 | jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ? |
| 1041 | MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | retry: |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 1044 | spin_lock_irqsave(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
| 1046 | res = 0; |
| 1047 | if (list_empty(&ep->rdllist)) { |
| 1048 | /* |
| 1049 | * We don't have any available event to return to the caller. |
| 1050 | * We need to sleep here, and we will be wake up by |
| 1051 | * ep_poll_callback() when events will become available. |
| 1052 | */ |
| 1053 | init_waitqueue_entry(&wait, current); |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 1054 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
Davide Libenzi | 3419b23 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 1055 | __add_wait_queue(&ep->wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
| 1057 | for (;;) { |
| 1058 | /* |
| 1059 | * We don't want to sleep if the ep_poll_callback() sends us |
| 1060 | * a wakeup in between. That's why we set the task state |
| 1061 | * to TASK_INTERRUPTIBLE before doing the checks. |
| 1062 | */ |
| 1063 | set_current_state(TASK_INTERRUPTIBLE); |
| 1064 | if (!list_empty(&ep->rdllist) || !jtimeout) |
| 1065 | break; |
| 1066 | if (signal_pending(current)) { |
| 1067 | res = -EINTR; |
| 1068 | break; |
| 1069 | } |
| 1070 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 1071 | spin_unlock_irqrestore(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | jtimeout = schedule_timeout(jtimeout); |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 1073 | spin_lock_irqsave(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | } |
Davide Libenzi | 3419b23 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 1075 | __remove_wait_queue(&ep->wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
| 1077 | set_current_state(TASK_RUNNING); |
| 1078 | } |
| 1079 | |
| 1080 | /* Is it worth to try to dig for events ? */ |
| 1081 | eavail = !list_empty(&ep->rdllist); |
| 1082 | |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 1083 | spin_unlock_irqrestore(&ep->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
| 1085 | /* |
| 1086 | * Try to transfer events to user space. In case we get 0 events and |
| 1087 | * there's still timeout left over, we go trying again in search of |
| 1088 | * more luck. |
| 1089 | */ |
| 1090 | if (!res && eavail && |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 1091 | !(res = ep_send_events(ep, events, maxevents)) && jtimeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | goto retry; |
| 1093 | |
| 1094 | return res; |
| 1095 | } |
| 1096 | |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1097 | /* |
Andrew Morton | 523723b | 2008-08-12 15:09:01 -0700 | [diff] [blame] | 1098 | * Open an eventpoll file descriptor. |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1099 | */ |
Heiko Carstens | 5a8a82b | 2009-01-14 14:14:25 +0100 | [diff] [blame] | 1100 | SYSCALL_DEFINE1(epoll_create1, int, flags) |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1101 | { |
| 1102 | int error, fd = -1; |
| 1103 | struct eventpoll *ep; |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1104 | |
Ulrich Drepper | e38b36f | 2008-07-23 21:29:42 -0700 | [diff] [blame] | 1105 | /* Check the EPOLL_* constant for consistency. */ |
| 1106 | BUILD_BUG_ON(EPOLL_CLOEXEC != O_CLOEXEC); |
| 1107 | |
Ulrich Drepper | a0998b5 | 2008-07-23 21:29:27 -0700 | [diff] [blame] | 1108 | if (flags & ~EPOLL_CLOEXEC) |
| 1109 | return -EINVAL; |
| 1110 | |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1111 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n", |
Ulrich Drepper | 9fe5ad9 | 2008-07-23 21:29:43 -0700 | [diff] [blame] | 1112 | current, flags)); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1113 | |
| 1114 | /* |
Ulrich Drepper | 9fe5ad9 | 2008-07-23 21:29:43 -0700 | [diff] [blame] | 1115 | * Create the internal data structure ( "struct eventpoll" ). |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1116 | */ |
Ulrich Drepper | 9fe5ad9 | 2008-07-23 21:29:43 -0700 | [diff] [blame] | 1117 | error = ep_alloc(&ep); |
| 1118 | if (error < 0) { |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1119 | fd = error; |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1120 | goto error_return; |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1121 | } |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1122 | |
| 1123 | /* |
| 1124 | * Creates all the items needed to setup an eventpoll file. That is, |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1125 | * a file structure and a free file descriptor. |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1126 | */ |
Ulrich Drepper | a0998b5 | 2008-07-23 21:29:27 -0700 | [diff] [blame] | 1127 | fd = anon_inode_getfd("[eventpoll]", &eventpoll_fops, ep, |
| 1128 | flags & O_CLOEXEC); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1129 | if (fd < 0) |
| 1130 | ep_free(ep); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1131 | |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1132 | error_return: |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1133 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n", |
Ulrich Drepper | 9fe5ad9 | 2008-07-23 21:29:43 -0700 | [diff] [blame] | 1134 | current, flags, fd)); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1135 | |
| 1136 | return fd; |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Heiko Carstens | 5a8a82b | 2009-01-14 14:14:25 +0100 | [diff] [blame] | 1139 | SYSCALL_DEFINE1(epoll_create, int, size) |
Ulrich Drepper | a0998b5 | 2008-07-23 21:29:27 -0700 | [diff] [blame] | 1140 | { |
Ulrich Drepper | 9fe5ad9 | 2008-07-23 21:29:43 -0700 | [diff] [blame] | 1141 | if (size < 0) |
| 1142 | return -EINVAL; |
| 1143 | |
| 1144 | return sys_epoll_create1(0); |
Ulrich Drepper | a0998b5 | 2008-07-23 21:29:27 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1147 | /* |
| 1148 | * The following function implements the controller interface for |
| 1149 | * the eventpoll file that enables the insertion/removal/change of |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 1150 | * file descriptors inside the interest set. |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1151 | */ |
Heiko Carstens | 5a8a82b | 2009-01-14 14:14:25 +0100 | [diff] [blame] | 1152 | SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, |
| 1153 | struct epoll_event __user *, event) |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1154 | { |
| 1155 | int error; |
| 1156 | struct file *file, *tfile; |
| 1157 | struct eventpoll *ep; |
| 1158 | struct epitem *epi; |
| 1159 | struct epoll_event epds; |
| 1160 | |
| 1161 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n", |
| 1162 | current, epfd, op, fd, event)); |
| 1163 | |
| 1164 | error = -EFAULT; |
| 1165 | if (ep_op_has_event(op) && |
| 1166 | copy_from_user(&epds, event, sizeof(struct epoll_event))) |
| 1167 | goto error_return; |
| 1168 | |
| 1169 | /* Get the "struct file *" for the eventpoll file */ |
| 1170 | error = -EBADF; |
| 1171 | file = fget(epfd); |
| 1172 | if (!file) |
| 1173 | goto error_return; |
| 1174 | |
| 1175 | /* Get the "struct file *" for the target file */ |
| 1176 | tfile = fget(fd); |
| 1177 | if (!tfile) |
| 1178 | goto error_fput; |
| 1179 | |
| 1180 | /* The target file descriptor must support poll */ |
| 1181 | error = -EPERM; |
| 1182 | if (!tfile->f_op || !tfile->f_op->poll) |
| 1183 | goto error_tgt_fput; |
| 1184 | |
| 1185 | /* |
| 1186 | * We have to check that the file structure underneath the file descriptor |
| 1187 | * the user passed to us _is_ an eventpoll file. And also we do not permit |
| 1188 | * adding an epoll file descriptor inside itself. |
| 1189 | */ |
| 1190 | error = -EINVAL; |
| 1191 | if (file == tfile || !is_file_epoll(file)) |
| 1192 | goto error_tgt_fput; |
| 1193 | |
| 1194 | /* |
| 1195 | * At this point it is safe to assume that the "private_data" contains |
| 1196 | * our own data structure. |
| 1197 | */ |
| 1198 | ep = file->private_data; |
| 1199 | |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 1200 | mutex_lock(&ep->mtx); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1201 | |
Davide Libenzi | 67647d0 | 2007-05-15 01:40:52 -0700 | [diff] [blame] | 1202 | /* |
| 1203 | * Try to lookup the file inside our RB tree, Since we grabbed "mtx" |
| 1204 | * above, we can be sure to be able to use the item looked up by |
| 1205 | * ep_find() till we release the mutex. |
| 1206 | */ |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1207 | epi = ep_find(ep, tfile, fd); |
| 1208 | |
| 1209 | error = -EINVAL; |
| 1210 | switch (op) { |
| 1211 | case EPOLL_CTL_ADD: |
| 1212 | if (!epi) { |
| 1213 | epds.events |= POLLERR | POLLHUP; |
| 1214 | |
| 1215 | error = ep_insert(ep, &epds, tfile, fd); |
| 1216 | } else |
| 1217 | error = -EEXIST; |
| 1218 | break; |
| 1219 | case EPOLL_CTL_DEL: |
| 1220 | if (epi) |
| 1221 | error = ep_remove(ep, epi); |
| 1222 | else |
| 1223 | error = -ENOENT; |
| 1224 | break; |
| 1225 | case EPOLL_CTL_MOD: |
| 1226 | if (epi) { |
| 1227 | epds.events |= POLLERR | POLLHUP; |
| 1228 | error = ep_modify(ep, epi, &epds); |
| 1229 | } else |
| 1230 | error = -ENOENT; |
| 1231 | break; |
| 1232 | } |
Davide Libenzi | d47de16 | 2007-05-15 01:40:41 -0700 | [diff] [blame] | 1233 | mutex_unlock(&ep->mtx); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1234 | |
| 1235 | error_tgt_fput: |
| 1236 | fput(tfile); |
| 1237 | error_fput: |
| 1238 | fput(file); |
| 1239 | error_return: |
| 1240 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n", |
| 1241 | current, epfd, op, fd, event, error)); |
| 1242 | |
| 1243 | return error; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * Implement the event wait interface for the eventpoll file. It is the kernel |
| 1248 | * part of the user space epoll_wait(2). |
| 1249 | */ |
Heiko Carstens | 5a8a82b | 2009-01-14 14:14:25 +0100 | [diff] [blame] | 1250 | SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events, |
| 1251 | int, maxevents, int, timeout) |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1252 | { |
| 1253 | int error; |
| 1254 | struct file *file; |
| 1255 | struct eventpoll *ep; |
| 1256 | |
| 1257 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n", |
| 1258 | current, epfd, events, maxevents, timeout)); |
| 1259 | |
| 1260 | /* The maximum number of event must be greater than zero */ |
| 1261 | if (maxevents <= 0 || maxevents > EP_MAX_EVENTS) |
| 1262 | return -EINVAL; |
| 1263 | |
| 1264 | /* Verify that the area passed by the user is writeable */ |
| 1265 | if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) { |
| 1266 | error = -EFAULT; |
| 1267 | goto error_return; |
| 1268 | } |
| 1269 | |
| 1270 | /* Get the "struct file *" for the eventpoll file */ |
| 1271 | error = -EBADF; |
| 1272 | file = fget(epfd); |
| 1273 | if (!file) |
| 1274 | goto error_return; |
| 1275 | |
| 1276 | /* |
| 1277 | * We have to check that the file structure underneath the fd |
| 1278 | * the user passed to us _is_ an eventpoll file. |
| 1279 | */ |
| 1280 | error = -EINVAL; |
| 1281 | if (!is_file_epoll(file)) |
| 1282 | goto error_fput; |
| 1283 | |
| 1284 | /* |
| 1285 | * At this point it is safe to assume that the "private_data" contains |
| 1286 | * our own data structure. |
| 1287 | */ |
| 1288 | ep = file->private_data; |
| 1289 | |
| 1290 | /* Time to fish for events ... */ |
| 1291 | error = ep_poll(ep, events, maxevents, timeout); |
| 1292 | |
| 1293 | error_fput: |
| 1294 | fput(file); |
| 1295 | error_return: |
| 1296 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n", |
| 1297 | current, epfd, events, maxevents, timeout, error)); |
| 1298 | |
| 1299 | return error; |
| 1300 | } |
| 1301 | |
Roland McGrath | f3de272 | 2008-04-30 00:53:09 -0700 | [diff] [blame] | 1302 | #ifdef HAVE_SET_RESTORE_SIGMASK |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1303 | |
| 1304 | /* |
| 1305 | * Implement the event wait interface for the eventpoll file. It is the kernel |
| 1306 | * part of the user space epoll_pwait(2). |
| 1307 | */ |
Heiko Carstens | 5a8a82b | 2009-01-14 14:14:25 +0100 | [diff] [blame] | 1308 | SYSCALL_DEFINE6(epoll_pwait, int, epfd, struct epoll_event __user *, events, |
| 1309 | int, maxevents, int, timeout, const sigset_t __user *, sigmask, |
| 1310 | size_t, sigsetsize) |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1311 | { |
| 1312 | int error; |
| 1313 | sigset_t ksigmask, sigsaved; |
| 1314 | |
| 1315 | /* |
| 1316 | * If the caller wants a certain signal mask to be set during the wait, |
| 1317 | * we apply it here. |
| 1318 | */ |
| 1319 | if (sigmask) { |
| 1320 | if (sigsetsize != sizeof(sigset_t)) |
| 1321 | return -EINVAL; |
| 1322 | if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask))) |
| 1323 | return -EFAULT; |
| 1324 | sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP)); |
| 1325 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); |
| 1326 | } |
| 1327 | |
| 1328 | error = sys_epoll_wait(epfd, events, maxevents, timeout); |
| 1329 | |
| 1330 | /* |
| 1331 | * If we changed the signal mask, we need to restore the original one. |
| 1332 | * In case we've got a signal while waiting, we do not restore the |
| 1333 | * signal mask yet, and we allow do_signal() to deliver the signal on |
| 1334 | * the way back to userspace, before the signal mask is restored. |
| 1335 | */ |
| 1336 | if (sigmask) { |
| 1337 | if (error == -EINTR) { |
| 1338 | memcpy(¤t->saved_sigmask, &sigsaved, |
Davide Libenzi | c7ea763 | 2007-05-15 01:40:47 -0700 | [diff] [blame] | 1339 | sizeof(sigsaved)); |
Roland McGrath | 4e4c22c | 2008-04-30 00:53:06 -0700 | [diff] [blame] | 1340 | set_restore_sigmask(); |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1341 | } else |
| 1342 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 1343 | } |
| 1344 | |
| 1345 | return error; |
| 1346 | } |
| 1347 | |
Roland McGrath | f3de272 | 2008-04-30 00:53:09 -0700 | [diff] [blame] | 1348 | #endif /* HAVE_SET_RESTORE_SIGMASK */ |
Davide Libenzi | 7699acd | 2007-05-10 22:23:23 -0700 | [diff] [blame] | 1349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | static int __init eventpoll_init(void) |
| 1351 | { |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 1352 | struct sysinfo si; |
| 1353 | |
| 1354 | si_meminfo(&si); |
Davide Libenzi | 9df04e1 | 2009-01-29 14:25:26 -0800 | [diff] [blame] | 1355 | /* |
| 1356 | * Allows top 4% of lomem to be allocated for epoll watches (per user). |
| 1357 | */ |
| 1358 | max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) / |
Davide Libenzi | 7ef9964 | 2008-12-01 13:13:55 -0800 | [diff] [blame] | 1359 | EP_ITEM_COST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
| 1361 | /* Initialize the structure used to perform safe poll wait head wake ups */ |
| 1362 | ep_poll_safewake_init(&psw); |
| 1363 | |
| 1364 | /* Allocates slab cache used to allocate "struct epitem" items */ |
| 1365 | epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem), |
| 1366 | 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1367 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
| 1369 | /* Allocates slab cache used to allocate "struct eppoll_entry" */ |
| 1370 | pwq_cache = kmem_cache_create("eventpoll_pwq", |
| 1371 | sizeof(struct eppoll_entry), 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1372 | EPI_SLAB_DEBUG|SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | } |
Davide Libenzi | cea6924 | 2007-05-10 22:23:22 -0700 | [diff] [blame] | 1376 | fs_initcall(eventpoll_init); |