| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
#include <linux/module.h> |
|---|
| 15 |
#include <linux/init.h> |
|---|
| 16 |
#include <linux/kernel.h> |
|---|
| 17 |
#include <linux/sched.h> |
|---|
| 18 |
#include <linux/fs.h> |
|---|
| 19 |
#include <linux/file.h> |
|---|
| 20 |
#include <linux/signal.h> |
|---|
| 21 |
#include <linux/errno.h> |
|---|
| 22 |
#include <linux/mm.h> |
|---|
| 23 |
#include <linux/slab.h> |
|---|
| 24 |
#include <linux/poll.h> |
|---|
| 25 |
#include <linux/smp_lock.h> |
|---|
| 26 |
#include <linux/string.h> |
|---|
| 27 |
#include <linux/list.h> |
|---|
| 28 |
#include <linux/hash.h> |
|---|
| 29 |
#include <linux/spinlock.h> |
|---|
| 30 |
#include <linux/syscalls.h> |
|---|
| 31 |
#include <linux/rwsem.h> |
|---|
| 32 |
#include <linux/wait.h> |
|---|
| 33 |
#include <linux/eventpoll.h> |
|---|
| 34 |
#include <linux/mount.h> |
|---|
| 35 |
#include <asm/bitops.h> |
|---|
| 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> |
|---|
| 41 |
#include <asm/semaphore.h> |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
#define EVENTPOLLFS_MAGIC 0x03111965 |
|---|
| 78 |
|
|---|
| 79 |
#define DEBUG_EPOLL 0 |
|---|
| 80 |
|
|---|
| 81 |
#if DEBUG_EPOLL > 0 |
|---|
| 82 |
#define DPRINTK(x) printk x |
|---|
| 83 |
#define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0) |
|---|
| 84 |
#else |
|---|
| 85 |
#define DPRINTK(x) (void) 0 |
|---|
| 86 |
#define DNPRINTK(n, x) (void) 0 |
|---|
| 87 |
#endif |
|---|
| 88 |
|
|---|
| 89 |
#define DEBUG_EPI 0 |
|---|
| 90 |
|
|---|
| 91 |
#if DEBUG_EPI != 0 |
|---|
| 92 |
#define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE ) |
|---|
| 93 |
#else |
|---|
| 94 |
#define EPI_SLAB_DEBUG 0 |
|---|
| 95 |
#endif |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
#define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET) |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
#define EP_MAX_POLLWAKE_NESTS 4 |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
#define EP_MAX_HASH_BITS 17 |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
#define EP_MIN_HASH_BITS 9 |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
#define EP_HENTRY_X_PAGE (PAGE_SIZE / sizeof(struct list_head)) |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
#define EP_MAX_HPAGES ((1 << EP_MAX_HASH_BITS) / EP_HENTRY_X_PAGE + 1) |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
#define EP_HASH_PAGES(hbits) ((int) ((1 << (hbits)) / EP_HENTRY_X_PAGE + \ |
|---|
| 117 |
((1 << (hbits)) % EP_HENTRY_X_PAGE ? 1: 0))) |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
#define EPI_MEM_ALLOC() (struct epitem *) kmem_cache_alloc(epi_cache, SLAB_KERNEL) |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
#define EPI_MEM_FREE(p) kmem_cache_free(epi_cache, p) |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
#define PWQ_MEM_ALLOC() (struct eppoll_entry *) kmem_cache_alloc(pwq_cache, SLAB_KERNEL) |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
#define PWQ_MEM_FREE(p) kmem_cache_free(pwq_cache, p) |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
#define IS_FILE_EPOLL(f) ((f)->f_op == &eventpoll_fops) |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
#define EP_LIST_DEL(p) do { list_del(p); INIT_LIST_HEAD(p); } while (0) |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
#define EP_IS_LINKED(p) (!list_empty(p)) |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
#define EP_ITEM_FROM_WAIT(p) ((struct epitem *) container_of(p, struct eppoll_entry, wait)->base) |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
#define EP_ITEM_FROM_EPQUEUE(p) (container_of(p, struct ep_pqueue, pt)->epi) |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
struct wake_task_node { |
|---|
| 158 |
struct list_head llink; |
|---|
| 159 |
task_t *task; |
|---|
| 160 |
wait_queue_head_t *wq; |
|---|
| 161 |
}; |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
struct poll_safewake { |
|---|
| 168 |
struct list_head wake_task_list; |
|---|
| 169 |
spinlock_t lock; |
|---|
| 170 |
}; |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
struct eventpoll { |
|---|
| 178 |
|
|---|
| 179 |
rwlock_t lock; |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
struct rw_semaphore sem; |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
wait_queue_head_t wq; |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
wait_queue_head_t poll_wait; |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
struct list_head rdllist; |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
unsigned int hashbits; |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
char *hpages[EP_MAX_HPAGES]; |
|---|
| 203 |
}; |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
struct eppoll_entry { |
|---|
| 207 |
|
|---|
| 208 |
struct list_head llink; |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
void *base; |
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
wait_queue_t wait; |
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
wait_queue_head_t *whead; |
|---|
| 221 |
}; |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
struct epitem { |
|---|
| 228 |
|
|---|
| 229 |
struct list_head llink; |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
struct list_head rdllink; |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
int fd; |
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
int nwait; |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
struct list_head pwqlist; |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
struct eventpoll *ep; |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
struct file *file; |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
struct epoll_event event; |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
atomic_t usecnt; |
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
struct list_head fllink; |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
struct list_head txlink; |
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
unsigned int revents; |
|---|
| 269 |
}; |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
struct ep_pqueue { |
|---|
| 273 |
poll_table pt; |
|---|
| 274 |
struct epitem *epi; |
|---|
| 275 |
}; |
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
static void ep_poll_safewake_init(struct poll_safewake *psw); |
|---|
| 280 |
static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq); |
|---|
| 281 |
static unsigned int ep_get_hash_bits(unsigned int hintsize); |
|---|
| 282 |
static int ep_getfd(int *efd, struct inode **einode, struct file **efile); |
|---|
| 283 |
static int ep_alloc_pages(char **pages, int numpages); |
|---|
| 284 |
static int ep_free_pages(char **pages, int numpages); |
|---|
| 285 |
static int ep_file_init(struct file *file, unsigned int hashbits); |
|---|
| 286 |
static unsigned int ep_hash_index(struct eventpoll *ep, struct file *file, |
|---|
| 287 |
int fd); |
|---|
| 288 |
static struct list_head *ep_hash_entry(struct eventpoll *ep, |
|---|
| 289 |
unsigned int index); |
|---|
| 290 |
static int ep_init(struct eventpoll *ep, unsigned int hashbits); |
|---|
| 291 |
static void ep_free(struct eventpoll *ep); |
|---|
| 292 |
static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd); |
|---|
| 293 |
static void ep_use_epitem(struct epitem *epi); |
|---|
| 294 |
static void ep_release_epitem(struct epitem *epi); |
|---|
| 295 |
static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, |
|---|
| 296 |
poll_table *pt); |
|---|
| 297 |
static int ep_insert(struct eventpoll *ep, struct epoll_event *event, |
|---|
| 298 |
struct file *tfile, int fd); |
|---|
| 299 |
static int ep_modify(struct eventpoll *ep, struct epitem *epi, |
|---|
| 300 |
struct epoll_event *event); |
|---|
| 301 |
static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi); |
|---|
| 302 |
static int ep_unlink(struct eventpoll *ep, struct epitem *epi); |
|---|
| 303 |
static int ep_remove(struct eventpoll *ep, struct epitem *epi); |
|---|
| 304 |
static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key); |
|---|
| 305 |
static int ep_eventpoll_close(struct inode *inode, struct file *file); |
|---|
| 306 |
static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait); |
|---|
| 307 |
static int ep_collect_ready_items(struct eventpoll *ep, |
|---|
| 308 |
struct list_head *txlist, int maxevents); |
|---|
| 309 |
static int ep_send_events(struct eventpoll *ep, struct list_head *txlist, |
|---|
| 310 |
struct epoll_event __user *events); |
|---|
| 311 |
static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist); |
|---|
| 312 |
static int ep_events_transfer(struct eventpoll *ep, |
|---|
| 313 |
struct epoll_event __user *events, |
|---|
| 314 |
int maxevents); |
|---|
| 315 |
static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, |
|---|
| 316 |
int maxevents, long timeout); |
|---|
| 317 |
static int eventpollfs_delete_dentry(struct dentry *dentry); |
|---|
| 318 |
static struct inode *ep_eventpoll_inode(void); |
|---|
| 319 |
static struct super_block *eventpollfs_get_sb(struct file_system_type *fs_type, |
|---|
| 320 |
int flags, const char *dev_name, |
|---|
| 321 |
void *data); |
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
struct semaphore epsem; |
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
static struct poll_safewake psw; |
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
static kmem_cache_t *epi_cache; |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
static kmem_cache_t *pwq_cache; |
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
static struct vfsmount *eventpoll_mnt; |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
static struct file_operations eventpoll_fops = { |
|---|
| 342 |
.release = ep_eventpoll_close, |
|---|
| 343 |
.poll = ep_eventpoll_poll |
|---|
| 344 |
}; |
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
static struct file_system_type eventpoll_fs_type = { |
|---|
| 351 |
.name = "eventpollfs", |
|---|
| 352 |
.get_sb = eventpollfs_get_sb, |
|---|
| 353 |
.kill_sb = kill_anon_super, |
|---|
| 354 |
}; |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
static struct dentry_operations eventpollfs_dentry_operations = { |
|---|
| 358 |
.d_delete = eventpollfs_delete_dentry, |
|---|
| 359 |
}; |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
static void ep_poll_safewake_init(struct poll_safewake *psw) |
|---|
| 365 |
{ |
|---|
| 366 |
|
|---|
| 367 |
INIT_LIST_HEAD(&psw->wake_task_list); |
|---|
| 368 |
spin_lock_init(&psw->lock); |
|---|
| 369 |
} |
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq) |
|---|
| 385 |
{ |
|---|
| 386 |
int wake_nests = 0; |
|---|
| 387 |
unsigned long flags; |
|---|
| 388 |
task_t *this_task = current; |
|---|
| 389 |
struct list_head *lsthead = &psw->wake_task_list, *lnk; |
|---|
| 390 |
struct wake_task_node *tncur; |
|---|
| 391 |
struct wake_task_node tnode; |
|---|
| 392 |
|
|---|
| 393 |
spin_lock_irqsave(&psw->lock, flags); |
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
list_for_each(lnk, lsthead) { |
|---|
| 397 |
tncur = list_entry(lnk, struct wake_task_node, llink); |
|---|
| 398 |
|
|---|
| 399 |
if (tncur->wq == wq || |
|---|
| 400 |
(tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) { |
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
spin_unlock_irqrestore(&psw->lock, flags); |
|---|
| 406 |
return; |
|---|
| 407 |
} |
|---|
| 408 |
} |
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
tnode.task = this_task; |
|---|
| 412 |
tnode.wq = wq; |
|---|
| 413 |
list_add(&tnode.llink, lsthead); |
|---|
| 414 |
|
|---|
| 415 |
spin_unlock_irqrestore(&psw->lock, flags); |
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
wake_up(wq); |
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
spin_lock_irqsave(&psw->lock, flags); |
|---|
| 422 |
list_del(&tnode.llink); |
|---|
| 423 |
spin_unlock_irqrestore(&psw->lock, flags); |
|---|
| 424 |
} |
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
static unsigned int ep_get_hash_bits(unsigned int hintsize) |
|---|
| 432 |
{ |
|---|
| 433 |
unsigned int i, val; |
|---|
| 434 |
|
|---|
| 435 |
for (i = 0, val = 1; val < hintsize && i < EP_MAX_HASH_BITS; i++, val <<= 1); |
|---|
| 436 |
return i < EP_MIN_HASH_BITS ? EP_MIN_HASH_BITS: i; |
|---|
| 437 |
} |
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
void eventpoll_init_file(struct file *file) |
|---|
| 442 |
{ |
|---|
| 443 |
|
|---|
| 444 |
INIT_LIST_HEAD(&file->f_ep_links); |
|---|
| 445 |
spin_lock_init(&file->f_ep_lock); |
|---|
| 446 |
} |
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
void eventpoll_release_file(struct file *file) |
|---|
| 455 |
{ |
|---|
| 456 |
struct list_head *lsthead = &file->f_ep_links; |
|---|
| 457 |
struct eventpoll *ep; |
|---|
| 458 |
struct epitem *epi; |
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
down(&epsem); |
|---|
| 470 |
|
|---|
| 471 |
while (!list_empty(lsthead)) { |
|---|
| 472 |
epi = list_entry(lsthead->next, struct epitem, fllink); |
|---|
| 473 |
|
|---|
| 474 |
ep = epi->ep; |
|---|
| 475 |
EP_LIST_DEL(&epi->fllink); |
|---|
| 476 |
down_write(&ep->sem); |
|---|
| 477 |
ep_remove(ep, epi); |
|---|
| 478 |
up_write(&ep->sem); |
|---|
| 479 |
} |
|---|
| 480 |
|
|---|
| 481 |
up(&epsem); |
|---|
| 482 |
} |
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
asmlinkage long sys_epoll_create(int size) |
|---|
| 493 |
{ |
|---|
| 494 |
int error, fd; |
|---|
| 495 |
unsigned int hashbits; |
|---|
| 496 |
struct inode *inode; |
|---|
| 497 |
struct file *file; |
|---|
| 498 |
|
|---|
| 499 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n", |
|---|
| 500 |
current, size)); |
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
error = -EINVAL; |
|---|
| 504 |
if (size <= 0) |
|---|
| 505 |
goto eexit_1; |
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 508 |
hashbits = ep_get_hash_bits((unsigned int) size); |
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
error = ep_getfd(&fd, &inode, &file); |
|---|
| 515 |
if (error) |
|---|
| 516 |
goto eexit_1; |
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
error = ep_file_init(file, hashbits); |
|---|
| 520 |
if (error) |
|---|
| 521 |
goto eexit_2; |
|---|
| 522 |
|
|---|
| 523 |
|
|---|
| 524 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n", |
|---|
| 525 |
current, size, fd)); |
|---|
| 526 |
|
|---|
| 527 |
return fd; |
|---|
| 528 |
|
|---|
| 529 |
eexit_2: |
|---|
| 530 |
sys_close(fd); |
|---|
| 531 |
eexit_1: |
|---|
| 532 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n", |
|---|
| 533 |
current, size, error)); |
|---|
| 534 |
return error; |
|---|
| 535 |
} |
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
asmlinkage long |
|---|
| 545 |
sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event) |
|---|
| 546 |
{ |
|---|
| 547 |
int error; |
|---|
| 548 |
struct file *file, *tfile; |
|---|
| 549 |
struct eventpoll *ep; |
|---|
| 550 |
struct epitem *epi; |
|---|
| 551 |
struct epoll_event epds; |
|---|
| 552 |
|
|---|
| 553 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n", |
|---|
| 554 |
current, epfd, op, fd, event)); |
|---|
| 555 |
|
|---|
| 556 |
error = -EFAULT; |
|---|
| 557 |
if (copy_from_user(&epds, event, sizeof(struct epoll_event))) |
|---|
| 558 |
goto eexit_1; |
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
error = -EBADF; |
|---|
| 562 |
file = fget(epfd); |
|---|
| 563 |
if (!file) |
|---|
| 564 |
goto eexit_1; |
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
tfile = fget(fd); |
|---|
| 568 |
if (!tfile) |
|---|
| 569 |
goto eexit_2; |
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
error = -EPERM; |
|---|
| 573 |
if (!tfile->f_op || !tfile->f_op->poll) |
|---|
| 574 |
goto eexit_3; |
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 |
error = -EINVAL; |
|---|
| 582 |
if (file == tfile || !IS_FILE_EPOLL(file)) |
|---|
| 583 |
goto eexit_3; |
|---|
| 584 |
|
|---|
| 585 |
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
ep = file->private_data; |
|---|
| 590 |
|
|---|
| 591 |
down_write(&ep->sem); |
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
epi = ep_find(ep, tfile, fd); |
|---|
| 595 |
|
|---|
| 596 |
error = -EINVAL; |
|---|
| 597 |
switch (op) { |
|---|
| 598 |
case EPOLL_CTL_ADD: |
|---|
| 599 |
if (!epi) { |
|---|
| 600 |
epds.events |= POLLERR | POLLHUP; |
|---|
| 601 |
|
|---|
| 602 |
error = ep_insert(ep, &epds, tfile, fd); |
|---|
| 603 |
} else |
|---|
| 604 |
error = -EEXIST; |
|---|
| 605 |
break; |
|---|
| 606 |
case EPOLL_CTL_DEL: |
|---|
| 607 |
if (epi) |
|---|
| 608 |
error = ep_remove(ep, epi); |
|---|
| 609 |
else |
|---|
| 610 |
error = -ENOENT; |
|---|
| 611 |
break; |
|---|
| 612 |
case EPOLL_CTL_MOD: |
|---|
| 613 |
if (epi) { |
|---|
| 614 |
epds.events |= POLLERR | POLLHUP; |
|---|
| 615 |
error = ep_modify(ep, epi, &epds); |
|---|
| 616 |
} else |
|---|
| 617 |
error = -ENOENT; |
|---|
| 618 |
break; |
|---|
| 619 |
} |
|---|
| 620 |
|
|---|
| 621 |
|
|---|
| 622 |
|
|---|
| 623 |
|
|---|
| 624 |
|
|---|
| 625 |
if (epi) |
|---|
| 626 |
ep_release_epitem(epi); |
|---|
| 627 |
|
|---|
| 628 |
up_write(&ep->sem); |
|---|
| 629 |
|
|---|
| 630 |
eexit_3: |
|---|
| 631 |
fput(tfile); |
|---|
| 632 |
eexit_2: |
|---|
| 633 |
fput(file); |
|---|
| 634 |
eexit_1: |
|---|
| 635 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n", |
|---|
| 636 |
current, epfd, op, fd, event, error)); |
|---|
| 637 |
|
|---|
| 638 |
return error; |
|---|
| 639 |
} |
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events, |
|---|
| 647 |
int maxevents, int timeout) |
|---|
| 648 |
{ |
|---|
| 649 |
int error; |
|---|
| 650 |
struct file *file; |
|---|
| 651 |
struct eventpoll *ep; |
|---|
| 652 |
|
|---|
| 653 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n", |
|---|
| 654 |
current, epfd, events, maxevents, timeout)); |
|---|
| 655 |
|
|---|
| 656 |
|
|---|
| 657 |
if (maxevents <= 0) |
|---|
| 658 |
return -EINVAL; |
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
if ((error = verify_area(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event)))) |
|---|
| 662 |
goto eexit_1; |
|---|
| 663 |
|
|---|
| 664 |
|
|---|
| 665 |
error = -EBADF; |
|---|
| 666 |
file = fget(epfd); |
|---|
| 667 |
if (!file) |
|---|
| 668 |
goto eexit_1; |
|---|
| 669 |
|
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
error = -EINVAL; |
|---|
| 675 |
if (!IS_FILE_EPOLL(file)) |
|---|
| 676 |
goto eexit_2; |
|---|
| 677 |
|
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
ep = file->private_data; |
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
error = ep_poll(ep, events, maxevents, timeout); |
|---|
| 686 |
|
|---|
| 687 |
eexit_2: |
|---|
| 688 |
fput(file); |
|---|
| 689 |
eexit_1: |
|---|
| 690 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n", |
|---|
| 691 |
current, epfd, events, maxevents, timeout, error)); |
|---|
| 692 |
|
|---|
| 693 |
return error; |
|---|
| 694 |
} |
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 |
|
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 |
static int ep_getfd(int *efd, struct inode **einode, struct file **efile) |
|---|
| 701 |
{ |
|---|
| 702 |
struct qstr this; |
|---|
| 703 |
char name[32]; |
|---|
| 704 |
struct dentry *dentry; |
|---|
| 705 |
struct inode *inode; |
|---|
| 706 |
struct file *file; |
|---|
| 707 |
int error, fd; |
|---|
| 708 |
|
|---|
| 709 |
|
|---|
| 710 |
error = -ENFILE; |
|---|
| 711 |
file = get_empty_filp(); |
|---|
| 712 |
if (!file) |
|---|
| 713 |
goto eexit_1; |
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
inode = ep_eventpoll_inode(); |
|---|
| 717 |
error = PTR_ERR(inode); |
|---|
| 718 |
if (IS_ERR(inode)) |
|---|
| 719 |
goto eexit_2; |
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
error = get_unused_fd(); |
|---|
| 723 |
if (error < 0) |
|---|
| 724 |
goto eexit_3; |
|---|
| 725 |
fd = error; |
|---|
| 726 |
|
|---|
| 727 |
|
|---|
| 728 |
|
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
error = -ENOMEM; |
|---|
| 732 |
sprintf(name, "[%lu]", inode->i_ino); |
|---|
| 733 |
this.name = name; |
|---|
| 734 |
this.len = strlen(name); |
|---|
| 735 |
this.hash = inode->i_ino; |
|---|
| 736 |
dentry = d_alloc(eventpoll_mnt->mnt_sb->s_root, &this); |
|---|
| 737 |
if (!dentry) |
|---|
| 738 |
goto eexit_4; |
|---|
| 739 |
dentry->d_op = &eventpollfs_dentry_operations; |
|---|
| 740 |
d_add(dentry, inode); |
|---|
| 741 |
file->f_vfsmnt = mntget(eventpoll_mnt); |
|---|
| 742 |
file->f_dentry = dget(dentry); |
|---|
| 743 |
file->f_mapping = inode->i_mapping; |
|---|
| 744 |
|
|---|
| 745 |
file->f_pos = 0; |
|---|
| 746 |
file->f_flags = O_RDONLY; |
|---|
| 747 |
file->f_op = &eventpoll_fops; |
|---|
| 748 |
file->f_mode = FMODE_READ; |
|---|
| 749 |
file->f_version = 0; |
|---|
| 750 |
file->private_data = NULL; |
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
fd_install(fd, file); |
|---|
| 754 |
|
|---|
| 755 |
*efd = fd; |
|---|
| 756 |
*einode = inode; |
|---|
| 757 |
*efile = file; |
|---|
| 758 |
return 0; |
|---|
| 759 |
|
|---|
| 760 |
eexit_4: |
|---|
| 761 |
put_unused_fd(fd); |
|---|
| 762 |
eexit_3: |
|---|
| 763 |
iput(inode); |
|---|
| 764 |
eexit_2: |
|---|
| 765 |
put_filp(file); |
|---|
| 766 |
eexit_1: |
|---|
| 767 |
return error; |
|---|
| 768 |
} |
|---|
| 769 |
|
|---|
| 770 |
|
|---|
| 771 |
static int ep_alloc_pages(char **pages, int numpages) |
|---|
| 772 |
{ |
|---|
| 773 |
int i; |
|---|
| 774 |
|
|---|
| 775 |
for (i = 0; i < numpages; i++) { |
|---|
| 776 |
pages[i] = (char *) __get_free_pages(GFP_KERNEL, 0); |
|---|
| 777 |
if (!pages[i]) { |
|---|
| 778 |
for (--i; i >= 0; i--) { |
|---|
| 779 |
ClearPageReserved(virt_to_page(pages[i])); |
|---|
| 780 |
free_pages((unsigned long) pages[i], 0); |
|---|
| 781 |
} |
|---|
| 782 |
return -ENOMEM; |
|---|
| 783 |
} |
|---|
| 784 |
SetPageReserved(virt_to_page(pages[i])); |
|---|
| 785 |
} |
|---|
| 786 |
return 0; |
|---|
| 787 |
} |
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
static int ep_free_pages(char **pages, int numpages) |
|---|
| 791 |
{ |
|---|
| 792 |
int i; |
|---|
| 793 |
|
|---|
| 794 |
for (i = 0; i < numpages; i++) { |
|---|
| 795 |
ClearPageReserved(virt_to_page(pages[i])); |
|---|
| 796 |
free_pages((unsigned long) pages[i], 0); |
|---|
| 797 |
} |
|---|
| 798 |
return 0; |
|---|
| 799 |
} |
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 |
static int ep_file_init(struct file *file, unsigned int hashbits) |
|---|
| 803 |
{ |
|---|
| 804 |
int error; |
|---|
| 805 |
struct eventpoll *ep; |
|---|
| 806 |
|
|---|
| 807 |
if (!(ep = kmalloc(sizeof(struct eventpoll), GFP_KERNEL))) |
|---|
| 808 |
return -ENOMEM; |
|---|
| 809 |
|
|---|
| 810 |
memset(ep, 0, sizeof(*ep)); |
|---|
| 811 |
|
|---|
| 812 |
error = ep_init(ep, hashbits); |
|---|
| 813 |
if (error) { |
|---|
| 814 |
kfree(ep); |
|---|
| 815 |
return error; |
|---|
| 816 |
} |
|---|
| 817 |
|
|---|
| 818 |
file->private_data = ep; |
|---|
| 819 |
|
|---|
| 820 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_file_init() ep=%p\n", |
|---|
| 821 |
current, ep)); |
|---|
| 822 |
return 0; |
|---|
| 823 |
} |
|---|
| 824 |
|
|---|
| 825 |
|
|---|
| 826 |
|
|---|
| 827 |
|
|---|
| 828 |
|
|---|
| 829 |
static unsigned int ep_hash_index(struct eventpoll *ep, struct file *file, int fd) |
|---|
| 830 |
{ |
|---|
| 831 |
unsigned long ptr = (unsigned long) file ^ (fd << ep->hashbits); |
|---|
| 832 |
|
|---|
| 833 |
return (unsigned int) hash_ptr((void *) ptr, ep->hashbits); |
|---|
| 834 |
} |
|---|
| 835 |
|
|---|
| 836 |
|
|---|
| 837 |
|
|---|
| 838 |
|
|---|
| 839 |
|
|---|
| 840 |
static struct list_head *ep_hash_entry(struct eventpoll *ep, unsigned int index) |
|---|
| 841 |
{ |
|---|
| 842 |
|
|---|
| 843 |
return (struct list_head *) (ep->hpages[index / EP_HENTRY_X_PAGE] + |
|---|
| 844 |
(index % EP_HENTRY_X_PAGE) * sizeof(struct list_head)); |
|---|
| 845 |
} |
|---|
| 846 |
|
|---|
| 847 |
|
|---|
| 848 |
static int ep_init(struct eventpoll *ep, unsigned int hashbits) |
|---|
| 849 |
{ |
|---|
| 850 |
int error; |
|---|
| 851 |
unsigned int i, hsize; |
|---|
| 852 |
|
|---|
| 853 |
rwlock_init(&ep->lock); |
|---|
| 854 |
init_rwsem(&ep->sem); |
|---|
| 855 |
init_waitqueue_head(&ep->wq); |
|---|
| 856 |
init_waitqueue_head(&ep->poll_wait); |
|---|
| 857 |
INIT_LIST_HEAD(&ep->rdllist); |
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
ep->hashbits = hashbits; |
|---|
| 861 |
error = ep_alloc_pages(ep->hpages, EP_HASH_PAGES(ep->hashbits)); |
|---|
| 862 |
if (error) |
|---|
| 863 |
goto eexit_1; |
|---|
| 864 |
|
|---|
| 865 |
|
|---|
| 866 |
for (i = 0, hsize = 1 << hashbits; i < hsize; i++) |
|---|
| 867 |
INIT_LIST_HEAD(ep_hash_entry(ep, i)); |
|---|
| 868 |
|
|---|
| 869 |
return 0; |
|---|
| 870 |
eexit_1: |
|---|
| 871 |
return error; |
|---|
| 872 |
} |
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
static void ep_free(struct eventpoll *ep) |
|---|
| 876 |
{ |
|---|
| 877 |
unsigned int i, hsize; |
|---|
| 878 |
struct list_head *lsthead, *lnk; |
|---|
| 879 |
struct epitem *epi; |
|---|
| 880 |
|
|---|
| 881 |
|
|---|
| 882 |
if (waitqueue_active(&ep->poll_wait)) |
|---|
| 883 |
ep_poll_safewake(&psw, &ep->poll_wait); |
|---|
| 884 |
|
|---|
| 885 |
|
|---|
| 886 |
|
|---|
| 887 |
|
|---|
| 888 |
|
|---|
| 889 |
|
|---|
| 890 |
|
|---|
| 891 |
|
|---|
| 892 |
|
|---|
| 893 |
down(&epsem); |
|---|
| 894 |
|
|---|
| 895 |
|
|---|
| 896 |
|
|---|
| 897 |
|
|---|
| 898 |
for (i = 0, hsize = 1 << ep->hashbits; i < hsize; i++) { |
|---|
| 899 |
lsthead = ep_hash_entry(ep, i); |
|---|
| 900 |
|
|---|
| 901 |
list_for_each(lnk, lsthead) { |
|---|
| 902 |
epi = list_entry(lnk, struct epitem, llink); |
|---|
| 903 |
|
|---|
| 904 |
ep_unregister_pollwait(ep, epi); |
|---|
| 905 |
} |
|---|
| 906 |
} |
|---|
| 907 |
|
|---|
| 908 |
|
|---|
| 909 |
|
|---|
| 910 |
|
|---|
| 911 |
|
|---|
| 912 |
|
|---|
| 913 |
|
|---|
| 914 |
for (i = 0, hsize = 1 << ep->hashbits; i < hsize; i++) { |
|---|
| 915 |
lsthead = ep_hash_entry(ep, i); |
|---|
| 916 |
|
|---|
| 917 |
while (!list_empty(lsthead)) { |
|---|
| 918 |
epi = list_entry(lsthead->next, struct epitem, llink); |
|---|
| 919 |
|
|---|
| 920 |
ep_remove(ep, epi); |
|---|
| 921 |
} |
|---|
| 922 |
} |
|---|
| 923 |
|
|---|
| 924 |
up(&epsem); |
|---|
| 925 |
|
|---|
| 926 |
|
|---|
| 927 |
ep_free_pages(ep->hpages, EP_HASH_PAGES(ep->hashbits)); |
|---|
| 928 |
} |
|---|
| 929 |
|
|---|
| 930 |
|
|---|
| 931 |
|
|---|
| 932 |
|
|---|
| 933 |
|
|---|
| 934 |
|
|---|
| 935 |
|
|---|
| 936 |
static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd) |
|---|
| 937 |
{ |
|---|
| 938 |
unsigned long flags; |
|---|
| 939 |
struct list_head *lsthead, *lnk; |
|---|
| 940 |
struct epitem *epi = NULL; |
|---|
| 941 |
|
|---|
| 942 |
read_lock_irqsave(&ep->lock, flags); |
|---|
| 943 |
|
|---|
| 944 |
lsthead = ep_hash_entry(ep, ep_hash_index(ep, file, fd)); |
|---|
| 945 |
list_for_each(lnk, lsthead) { |
|---|
| 946 |
epi = list_entry(lnk, struct epitem, llink); |
|---|
| 947 |
|
|---|
| 948 |
if (epi->file == file && epi->fd == fd) { |
|---|
| 949 |
ep_use_epitem(epi); |
|---|
| 950 |
break; |
|---|
| 951 |
} |
|---|
| 952 |
epi = NULL; |
|---|
| 953 |
} |
|---|
| 954 |
|
|---|
| 955 |
read_unlock_irqrestore(&ep->lock, flags); |
|---|
| 956 |
|
|---|
| 957 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n", |
|---|
| 958 |
current, file, epi)); |
|---|
| 959 |
|
|---|
| 960 |
return epi; |
|---|
| 961 |
} |
|---|
| 962 |
|
|---|
| 963 |
|
|---|
| 964 |
|
|---|
| 965 |
|
|---|
| 966 |
|
|---|
| 967 |
|
|---|
| 968 |
static void ep_use_epitem(struct epitem *epi) |
|---|
| 969 |
{ |
|---|
| 970 |
|
|---|
| 971 |
atomic_inc(&epi->usecnt); |
|---|
| 972 |
} |
|---|
| 973 |
|
|---|
| 974 |
|
|---|
| 975 |
|
|---|
| 976 |
|
|---|
| 977 |
|
|---|
| 978 |
|
|---|
| 979 |
|
|---|
| 980 |
static void ep_release_epitem(struct epitem *epi) |
|---|
| 981 |
{ |
|---|
| 982 |
|
|---|
| 983 |
if (atomic_dec_and_test(&epi->usecnt)) |
|---|
| 984 |
EPI_MEM_FREE(epi); |
|---|
| 985 |
} |
|---|
| 986 |
|
|---|
| 987 |
|
|---|
| 988 |
|
|---|
| 989 |
|
|---|
| 990 |
|
|---|
| 991 |
|
|---|
| 992 |
static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, |
|---|
| 993 |
poll_table *pt) |
|---|
| 994 |
{ |
|---|
| 995 |
struct epitem *epi = EP_ITEM_FROM_EPQUEUE(pt); |
|---|
| 996 |
struct eppoll_entry *pwq; |
|---|
| 997 |
|
|---|
| 998 |
if (epi->nwait >= 0 && (pwq = PWQ_MEM_ALLOC())) { |
|---|
| 999 |
init_waitqueue_func_entry(&pwq->wait, ep_poll_callback); |
|---|
| 1000 |
pwq->whead = whead; |
|---|
| 1001 |
pwq->base = epi; |
|---|
| 1002 |
add_wait_queue(whead, &pwq->wait); |
|---|
| 1003 |
list_add_tail(&pwq->llink, &epi->pwqlist); |
|---|
| 1004 |
epi->nwait++; |
|---|
| 1005 |
} else { |
|---|
| 1006 |
|
|---|
| 1007 |
epi->nwait = -1; |
|---|
| 1008 |
} |
|---|
| 1009 |
} |
|---|
| 1010 |
|
|---|
| 1011 |
|
|---|
| 1012 |
static int ep_insert(struct eventpoll *ep, struct epoll_event *event, |
|---|
| 1013 |
struct file *tfile, int fd) |
|---|
| 1014 |
{ |
|---|
| 1015 |
int error, revents, pwake = 0; |
|---|
| 1016 |
unsigned long flags; |
|---|
| 1017 |
struct epitem *epi; |
|---|
| 1018 |
struct ep_pqueue epq; |
|---|
| 1019 |
|
|---|
| 1020 |
error = -ENOMEM; |
|---|
| 1021 |
if (!(epi = EPI_MEM_ALLOC())) |
|---|
| 1022 |
goto eexit_1; |
|---|
| 1023 |
|
|---|
| 1024 |
|
|---|
| 1025 |
INIT_LIST_HEAD(&epi->llink); |
|---|
| 1026 |
INIT_LIST_HEAD(&epi->rdllink); |
|---|
| 1027 |
INIT_LIST_HEAD(&epi->fllink); |
|---|
| 1028 |
INIT_LIST_HEAD(&epi->txlink); |
|---|
| 1029 |
INIT_LIST_HEAD(&epi->pwqlist); |
|---|
| 1030 |
epi->ep = ep; |
|---|
| 1031 |
epi->file = tfile; |
|---|
| 1032 |
epi->fd = fd; |
|---|
| 1033 |
epi->event = *event; |
|---|
| 1034 |
atomic_set(&epi->usecnt, 1); |
|---|
| 1035 |
epi->nwait = 0; |
|---|
| 1036 |
|
|---|
| 1037 |
|
|---|
| 1038 |
epq.epi = epi; |
|---|
| 1039 |
init_poll_funcptr(&epq.pt, ep_ptable_queue_proc); |
|---|
| 1040 |
|
|---|
| 1041 |
|
|---|
| 1042 |
|
|---|
| 1043 |
|
|---|
| 1044 |
|
|---|
| 1045 |
|
|---|
| 1046 |
revents = tfile->f_op->poll(tfile, &epq.pt); |
|---|
| 1047 |
|
|---|
| 1048 |
|
|---|
| 1049 |
|
|---|
| 1050 |
|
|---|
| 1051 |
|
|---|
| 1052 |
|
|---|
| 1053 |
if (epi->nwait < 0) |
|---|
| 1054 |
goto eexit_2; |
|---|
| 1055 |
|
|---|
| 1056 |
|
|---|
| 1057 |
spin_lock(&tfile->f_ep_lock); |
|---|
| 1058 |
list_add_tail(&epi->fllink, &tfile->f_ep_links); |
|---|
| 1059 |
spin_unlock(&tfile->f_ep_lock); |
|---|
| 1060 |
|
|---|
| 1061 |
|
|---|
| 1062 |
write_lock_irqsave(&ep->lock, flags); |
|---|
| 1063 |
|
|---|
| 1064 |
|
|---|
| 1065 |
list_add(&epi->llink, ep_hash_entry(ep, ep_hash_index(ep, tfile, fd))); |
|---|
| 1066 |
|
|---|
| 1067 |
|
|---|
| 1068 |
if ((revents & event->events) && !EP_IS_LINKED(&epi->rdllink)) { |
|---|
| 1069 |
list_add_tail(&epi->rdllink, &ep->rdllist); |
|---|
| 1070 |
|
|---|
| 1071 |
|
|---|
| 1072 |
if (waitqueue_active(&ep->wq)) |
|---|
| 1073 |
wake_up(&ep->wq); |
|---|
| 1074 |
if (waitqueue_active(&ep->poll_wait)) |
|---|
| 1075 |
pwake++; |
|---|
| 1076 |
} |
|---|
| 1077 |
|
|---|
| 1078 |
write_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1079 |
|
|---|
| 1080 |
|
|---|
| 1081 |
if (pwake) |
|---|
| 1082 |
ep_poll_safewake(&psw, &ep->poll_wait); |
|---|
| 1083 |
|
|---|
| 1084 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n", |
|---|
| 1085 |
current, ep, tfile, fd)); |
|---|
| 1086 |
|
|---|
| 1087 |
return 0; |
|---|
| 1088 |
|
|---|
| 1089 |
eexit_2: |
|---|
| 1090 |
ep_unregister_pollwait(ep, epi); |
|---|
| 1091 |
|
|---|
| 1092 |
|
|---|
| 1093 |
|
|---|
| 1094 |
|
|---|
| 1095 |
|
|---|
| 1096 |
write_lock_irqsave(&ep->lock, flags); |
|---|
| 1097 |
if (EP_IS_LINKED(&epi->rdllink)) |
|---|
| 1098 |
EP_LIST_DEL(&epi->rdllink); |
|---|
| 1099 |
write_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1100 |
|
|---|
| 1101 |
EPI_MEM_FREE(epi); |
|---|
| 1102 |
eexit_1: |
|---|
| 1103 |
return error; |
|---|
| 1104 |
} |
|---|
| 1105 |
|
|---|
| 1106 |
|
|---|
| 1107 |
|
|---|
| 1108 |
|
|---|
| 1109 |
|
|---|
| 1110 |
|
|---|
| 1111 |
static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event) |
|---|
| 1112 |
{ |
|---|
| 1113 |
int pwake = 0; |
|---|
| 1114 |
unsigned int revents; |
|---|
| 1115 |
unsigned long flags; |
|---|
| 1116 |
|
|---|
| 1117 |
|
|---|
| 1118 |
|
|---|
| 1119 |
|
|---|
| 1120 |
|
|---|
| 1121 |
|
|---|
| 1122 |
|
|---|
| 1123 |
epi->event.events = event->events; |
|---|
| 1124 |
|
|---|
| 1125 |
|
|---|
| 1126 |
|
|---|
| 1127 |
|
|---|
| 1128 |
|
|---|
| 1129 |
revents = epi->file->f_op->poll(epi->file, NULL); |
|---|
| 1130 |
|
|---|
| 1131 |
write_lock_irqsave(&ep->lock, flags); |
|---|
| 1132 |
|
|---|
| 1133 |
|
|---|
| 1134 |
epi->event.data = event->data; |
|---|
| 1135 |
|
|---|
| 1136 |
|
|---|
| 1137 |
|
|---|
| 1138 |
|
|---|
| 1139 |
|
|---|
| 1140 |
if (EP_IS_LINKED(&epi->llink)) { |
|---|
| 1141 |
|
|---|
| 1142 |
|
|---|
| 1143 |
|
|---|
| 1144 |
|
|---|
| 1145 |
|
|---|
| 1146 |
if (revents & event->events) { |
|---|
| 1147 |
if (!EP_IS_LINKED(&epi->rdllink)) { |
|---|
| 1148 |
list_add_tail(&epi->rdllink, &ep->rdllist); |
|---|
| 1149 |
|
|---|
| 1150 |
|
|---|
| 1151 |
if (waitqueue_active(&ep->wq)) |
|---|
| 1152 |
wake_up(&ep->wq); |
|---|
| 1153 |
if (waitqueue_active(&ep->poll_wait)) |
|---|
| 1154 |
pwake++; |
|---|
| 1155 |
} |
|---|
| 1156 |
} |
|---|
| 1157 |
} |
|---|
| 1158 |
|
|---|
| 1159 |
write_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1160 |
|
|---|
| 1161 |
|
|---|
| 1162 |
if (pwake) |
|---|
| 1163 |
ep_poll_safewake(&psw, &ep->poll_wait); |
|---|
| 1164 |
|
|---|
| 1165 |
return 0; |
|---|
| 1166 |
} |
|---|
| 1167 |
|
|---|
| 1168 |
|
|---|
| 1169 |
|
|---|
| 1170 |
|
|---|
| 1171 |
|
|---|
| 1172 |
|
|---|
| 1173 |
|
|---|
| 1174 |
static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi) |
|---|
| 1175 |
{ |
|---|
| 1176 |
int nwait; |
|---|
| 1177 |
struct list_head *lsthead = &epi->pwqlist; |
|---|
| 1178 |
struct eppoll_entry *pwq; |
|---|
| 1179 |
|
|---|
| 1180 |
|
|---|
| 1181 |
nwait = xchg(&epi->nwait, 0); |
|---|
| 1182 |
|
|---|
| 1183 |
if (nwait) { |
|---|
| 1184 |
while (!list_empty(lsthead)) { |
|---|
| 1185 |
pwq = list_entry(lsthead->next, struct eppoll_entry, llink); |
|---|
| 1186 |
|
|---|
| 1187 |
EP_LIST_DEL(&pwq->llink); |
|---|
| 1188 |
remove_wait_queue(pwq->whead, &pwq->wait); |
|---|
| 1189 |
PWQ_MEM_FREE(pwq); |
|---|
| 1190 |
} |
|---|
| 1191 |
} |
|---|
| 1192 |
} |
|---|
| 1193 |
|
|---|
| 1194 |
|
|---|
| 1195 |
|
|---|
| 1196 |
|
|---|
| 1197 |
|
|---|
| 1198 |
|
|---|
| 1199 |
static int ep_unlink(struct eventpoll *ep, struct epitem *epi) |
|---|
| 1200 |
{ |
|---|
| 1201 |
int error; |
|---|
| 1202 |
|
|---|
| 1203 |
|
|---|
| 1204 |
|
|---|
| 1205 |
|
|---|
| 1206 |
|
|---|
| 1207 |
error = -ENOENT; |
|---|
| 1208 |
if (!EP_IS_LINKED(&epi->llink)) |
|---|
| 1209 |
goto eexit_1; |
|---|
| 1210 |
|
|---|
| 1211 |
|
|---|
| 1212 |
|
|---|
| 1213 |
|
|---|
| 1214 |
|
|---|
| 1215 |
|
|---|
| 1216 |
epi->event.events = 0; |
|---|
| 1217 |
|
|---|
| 1218 |
|
|---|
| 1219 |
|
|---|
| 1220 |
|
|---|
| 1221 |
|
|---|
| 1222 |
|
|---|
| 1223 |
EP_LIST_DEL(&epi->llink); |
|---|
| 1224 |
|
|---|
| 1225 |
|
|---|
| 1226 |
|
|---|
| 1227 |
|
|---|
| 1228 |
|
|---|
| 1229 |
if (EP_IS_LINKED(&epi->rdllink)) |
|---|
| 1230 |
EP_LIST_DEL(&epi->rdllink); |
|---|
| 1231 |
|
|---|
| 1232 |
error = 0; |
|---|
| 1233 |
eexit_1: |
|---|
| 1234 |
|
|---|
| 1235 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n", |
|---|
| 1236 |
current, ep, epi->file, error)); |
|---|
| 1237 |
|
|---|
| 1238 |
return error; |
|---|
| 1239 |
} |
|---|
| 1240 |
|
|---|
| 1241 |
|
|---|
| 1242 |
|
|---|
| 1243 |
|
|---|
| 1244 |
|
|---|
| 1245 |
|
|---|
| 1246 |
static int ep_remove(struct eventpoll *ep, struct epitem *epi) |
|---|
| 1247 |
{ |
|---|
| 1248 |
int error; |
|---|
| 1249 |
unsigned long flags; |
|---|
| 1250 |
struct file *file = epi->file; |
|---|
| 1251 |
|
|---|
| 1252 |
|
|---|
| 1253 |
|
|---|
| 1254 |
|
|---|
| 1255 |
|
|---|
| 1256 |
|
|---|
| 1257 |
|
|---|
| 1258 |
|
|---|
| 1259 |
|
|---|
| 1260 |
ep_unregister_pollwait(ep, epi); |
|---|
| 1261 |
|
|---|
| 1262 |
|
|---|
| 1263 |
spin_lock(&file->f_ep_lock); |
|---|
| 1264 |
if (EP_IS_LINKED(&epi->fllink)) |
|---|
| 1265 |
EP_LIST_DEL(&epi->fllink); |
|---|
| 1266 |
spin_unlock(&file->f_ep_lock); |
|---|
| 1267 |
|
|---|
| 1268 |
|
|---|
| 1269 |
write_lock_irqsave(&ep->lock, flags); |
|---|
| 1270 |
|
|---|
| 1271 |
|
|---|
| 1272 |
error = ep_unlink(ep, epi); |
|---|
| 1273 |
|
|---|
| 1274 |
write_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1275 |
|
|---|
| 1276 |
if (error) |
|---|
| 1277 |
goto eexit_1; |
|---|
| 1278 |
|
|---|
| 1279 |
|
|---|
| 1280 |
ep_release_epitem(epi); |
|---|
| 1281 |
|
|---|
| 1282 |
error = 0; |
|---|
| 1283 |
eexit_1: |
|---|
| 1284 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n", |
|---|
| 1285 |
current, ep, file, error)); |
|---|
| 1286 |
|
|---|
| 1287 |
return error; |
|---|
| 1288 |
} |
|---|
| 1289 |
|
|---|
| 1290 |
|
|---|
| 1291 |
|
|---|
| 1292 |
|
|---|
| 1293 |
|
|---|
| 1294 |
|
|---|
| 1295 |
|
|---|
| 1296 |
static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key) |
|---|
| 1297 |
{ |
|---|
| 1298 |
int pwake = 0; |
|---|
| 1299 |
unsigned long flags; |
|---|
| 1300 |
struct epitem *epi = EP_ITEM_FROM_WAIT(wait); |
|---|
| 1301 |
struct eventpoll *ep = epi->ep; |
|---|
| 1302 |
|
|---|
| 1303 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n", |
|---|
| 1304 |
current, epi->file, epi, ep)); |
|---|
| 1305 |
|
|---|
| 1306 |
write_lock_irqsave(&ep->lock, flags); |
|---|
| 1307 |
|
|---|
| 1308 |
|
|---|
| 1309 |
|
|---|
| 1310 |
|
|---|
| 1311 |
|
|---|
| 1312 |
|
|---|
| 1313 |
|
|---|
| 1314 |
if (!(epi->event.events & ~EP_PRIVATE_BITS)) |
|---|
| 1315 |
goto is_disabled; |
|---|
| 1316 |
|
|---|
| 1317 |
|
|---|
| 1318 |
if (EP_IS_LINKED(&epi->rdllink)) |
|---|
| 1319 |
goto is_linked; |
|---|
| 1320 |
|
|---|
| 1321 |
list_add_tail(&epi->rdllink, &ep->rdllist); |
|---|
| 1322 |
|
|---|
| 1323 |
is_linked: |
|---|
| 1324 |
|
|---|
| 1325 |
|
|---|
| 1326 |
|
|---|
| 1327 |
|
|---|
| 1328 |
if (waitqueue_active(&ep->wq)) |
|---|
| 1329 |
wake_up(&ep->wq); |
|---|
| 1330 |
if (waitqueue_active(&ep->poll_wait)) |
|---|
| 1331 |
pwake++; |
|---|
| 1332 |
|
|---|
| 1333 |
is_disabled: |
|---|
| 1334 |
write_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1335 |
|
|---|
| 1336 |
|
|---|
| 1337 |
if (pwake) |
|---|
| 1338 |
ep_poll_safewake(&psw, &ep->poll_wait); |
|---|
| 1339 |
|
|---|
| 1340 |
return 1; |
|---|
| 1341 |
} |
|---|
| 1342 |
|
|---|
| 1343 |
|
|---|
| 1344 |
static int ep_eventpoll_close(struct inode *inode, struct file *file) |
|---|
| 1345 |
{ |
|---|
| 1346 |
struct eventpoll *ep = file->private_data; |
|---|
| 1347 |
|
|---|
| 1348 |
if (ep) { |
|---|
| 1349 |
ep_free(ep); |
|---|
| 1350 |
kfree(ep); |
|---|
| 1351 |
} |
|---|
| 1352 |
|
|---|
| 1353 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep)); |
|---|
| 1354 |
return 0; |
|---|
| 1355 |
} |
|---|
| 1356 |
|
|---|
| 1357 |
|
|---|
| 1358 |
static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait) |
|---|
| 1359 |
{ |
|---|
| 1360 |
unsigned int pollflags = 0; |
|---|
| 1361 |
unsigned long flags; |
|---|
| 1362 |
struct eventpoll *ep = file->private_data; |
|---|
| 1363 |
|
|---|
| 1364 |
|
|---|
| 1365 |
poll_wait(file, &ep->poll_wait, wait); |
|---|
| 1366 |
|
|---|
| 1367 |
|
|---|
| 1368 |
read_lock_irqsave(&ep->lock, flags); |
|---|
| 1369 |
if (!list_empty(&ep->rdllist)) |
|---|
| 1370 |
pollflags = POLLIN | POLLRDNORM; |
|---|
| 1371 |
read_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1372 |
|
|---|
| 1373 |
return pollflags; |
|---|
| 1374 |
} |
|---|
| 1375 |
|
|---|
| 1376 |
|
|---|
| 1377 |
|
|---|
| 1378 |
|
|---|
| 1379 |
|
|---|
| 1380 |
|
|---|
| 1381 |
|
|---|
| 1382 |
static int ep_collect_ready_items(struct eventpoll *ep, struct list_head *txlist, int maxevents) |
|---|
| 1383 |
{ |
|---|
| 1384 |
int nepi; |
|---|
| 1385 |
unsigned long flags; |
|---|
| 1386 |
struct list_head *lsthead = &ep->rdllist, *lnk; |
|---|
| 1387 |
struct epitem *epi; |
|---|
| 1388 |
|
|---|
| 1389 |
write_lock_irqsave(&ep->lock, flags); |
|---|
| 1390 |
|
|---|
| 1391 |
for (nepi = 0, lnk = lsthead->next; lnk != lsthead && nepi < maxevents;) { |
|---|
| 1392 |
epi = list_entry(lnk, struct epitem, rdllink); |
|---|
| 1393 |
|
|---|
| 1394 |
lnk = lnk->next; |
|---|
| 1395 |
|
|---|
| 1396 |
|
|---|
| 1397 |
if (!EP_IS_LINKED(&epi->txlink)) { |
|---|
| 1398 |
|
|---|
| 1399 |
|
|---|
| 1400 |
|
|---|
| 1401 |
|
|---|
| 1402 |
|
|---|
| 1403 |
epi->revents = epi->event.events; |
|---|
| 1404 |
|
|---|
| 1405 |
|
|---|
| 1406 |
list_add(&epi->txlink, txlist); |
|---|
| 1407 |
nepi++; |
|---|
| 1408 |
|
|---|
| 1409 |
|
|---|
| 1410 |
|
|---|
| 1411 |
|
|---|
| 1412 |
EP_LIST_DEL(&epi->rdllink); |
|---|
| 1413 |
} |
|---|
| 1414 |
} |
|---|
| 1415 |
|
|---|
| 1416 |
write_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1417 |
|
|---|
| 1418 |
return nepi; |
|---|
| 1419 |
} |
|---|
| 1420 |
|
|---|
| 1421 |
|
|---|
| 1422 |
|
|---|
| 1423 |
|
|---|
| 1424 |
|
|---|
| 1425 |
|
|---|
| 1426 |
|
|---|
| 1427 |
static int ep_send_events(struct eventpoll *ep, struct list_head *txlist, |
|---|
| 1428 |
struct epoll_event __user *events) |
|---|
| 1429 |
{ |
|---|
| 1430 |
int eventcnt = 0; |
|---|
| 1431 |
unsigned int revents; |
|---|
| 1432 |
struct list_head *lnk; |
|---|
| 1433 |
struct epitem *epi; |
|---|
| 1434 |
|
|---|
| 1435 |
|
|---|
| 1436 |
|
|---|
| 1437 |
|
|---|
| 1438 |
|
|---|
| 1439 |
|
|---|
| 1440 |
|
|---|
| 1441 |
list_for_each(lnk, txlist) { |
|---|
| 1442 |
epi = list_entry(lnk, struct epitem, txlink); |
|---|
| 1443 |
|
|---|
| 1444 |
|
|---|
| 1445 |
|
|---|
| 1446 |
|
|---|
| 1447 |
|
|---|
| 1448 |
|
|---|
| 1449 |
revents = epi->file->f_op->poll(epi->file, NULL); |
|---|
| 1450 |
|
|---|
| 1451 |
|
|---|
| 1452 |
|
|---|
| 1453 |
|
|---|
| 1454 |
|
|---|
| 1455 |
|
|---|
| 1456 |
epi->revents = revents & epi->event.events; |
|---|
| 1457 |
|
|---|
| 1458 |
if (epi->revents) { |
|---|
| 1459 |
if (__put_user(epi->revents, |
|---|
| 1460 |
&events[eventcnt].events) || |
|---|
| 1461 |
__put_user(epi->event.data, |
|---|
| 1462 |
&events[eventcnt].data)) |
|---|
| 1463 |
return -EFAULT; |
|---|
| 1464 |
if (epi->event.events & EPOLLONESHOT) |
|---|
| 1465 |
epi->event.events &= EP_PRIVATE_BITS; |
|---|
| 1466 |
eventcnt++; |
|---|
| 1467 |
} |
|---|
| 1468 |
} |
|---|
| 1469 |
return eventcnt; |
|---|
| 1470 |
} |
|---|
| 1471 |
|
|---|
| 1472 |
|
|---|
| 1473 |
|
|---|
| 1474 |
|
|---|
| 1475 |
|
|---|
| 1476 |
|
|---|
| 1477 |
|
|---|
| 1478 |
|
|---|
| 1479 |
static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist) |
|---|
| 1480 |
{ |
|---|
| 1481 |
int ricnt = 0, pwake = 0; |
|---|
| 1482 |
unsigned long flags; |
|---|
| 1483 |
struct epitem *epi; |
|---|
| 1484 |
|
|---|
| 1485 |
write_lock_irqsave(&ep->lock, flags); |
|---|
| 1486 |
|
|---|
| 1487 |
while (!list_empty(txlist)) { |
|---|
| 1488 |
epi = list_entry(txlist->next, struct epitem, txlink); |
|---|
| 1489 |
|
|---|
| 1490 |
|
|---|
| 1491 |
EP_LIST_DEL(&epi->txlink); |
|---|
| 1492 |
|
|---|
| 1493 |
|
|---|
| 1494 |
|
|---|
| 1495 |
|
|---|
| 1496 |
|
|---|
| 1497 |
|
|---|
| 1498 |
|
|---|
| 1499 |
|
|---|
| 1500 |
if (EP_IS_LINKED(&epi->llink) && !(epi->event.events & EPOLLET) && |
|---|
| 1501 |
(epi->revents & epi->event.events) && !EP_IS_LINKED(&epi->rdllink)) { |
|---|
| 1502 |
list_add_tail(&epi->rdllink, &ep->rdllist); |
|---|
| 1503 |
ricnt++; |
|---|
| 1504 |
} |
|---|
| 1505 |
} |
|---|
| 1506 |
|
|---|
| 1507 |
if (ricnt) { |
|---|
| 1508 |
|
|---|
| 1509 |
|
|---|
| 1510 |
|
|---|
| 1511 |
|
|---|
| 1512 |
if (waitqueue_active(&ep->wq)) |
|---|
| 1513 |
wake_up(&ep->wq); |
|---|
| 1514 |
if (waitqueue_active(&ep->poll_wait)) |
|---|
| 1515 |
pwake++; |
|---|
| 1516 |
} |
|---|
| 1517 |
|
|---|
| 1518 |
write_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1519 |
|
|---|
| 1520 |
|
|---|
| 1521 |
if (pwake) |
|---|
| 1522 |
ep_poll_safewake(&psw, &ep->poll_wait); |
|---|
| 1523 |
} |
|---|
| 1524 |
|
|---|
| 1525 |
|
|---|
| 1526 |
|
|---|
| 1527 |
|
|---|
| 1528 |
|
|---|
| 1529 |
static int ep_events_transfer(struct eventpoll *ep, |
|---|
| 1530 |
struct epoll_event __user *events, int maxevents) |
|---|
| 1531 |
{ |
|---|
| 1532 |
int eventcnt = 0; |
|---|
| 1533 |
struct list_head txlist; |
|---|
| 1534 |
|
|---|
| 1535 |
INIT_LIST_HEAD(&txlist); |
|---|
| 1536 |
|
|---|
| 1537 |
|
|---|
| 1538 |
|
|---|
| 1539 |
|
|---|
| 1540 |
|
|---|
| 1541 |
down_read(&ep->sem); |
|---|
| 1542 |
|
|---|
| 1543 |
|
|---|
| 1544 |
if (ep_collect_ready_items(ep, &txlist, maxevents) > 0) { |
|---|
| 1545 |
|
|---|
| 1546 |
eventcnt = ep_send_events(ep, &txlist, events); |
|---|
| 1547 |
|
|---|
| 1548 |
|
|---|
| 1549 |
ep_reinject_items(ep, &txlist); |
|---|
| 1550 |
} |
|---|
| 1551 |
|
|---|
| 1552 |
up_read(&ep->sem); |
|---|
| 1553 |
|
|---|
| 1554 |
return eventcnt; |
|---|
| 1555 |
} |
|---|
| 1556 |
|
|---|
| 1557 |
|
|---|
| 1558 |
static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, |
|---|
| 1559 |
int maxevents, long timeout) |
|---|
| 1560 |
{ |
|---|
| 1561 |
int res, eavail; |
|---|
| 1562 |
unsigned long flags; |
|---|
| 1563 |
long jtimeout; |
|---|
| 1564 |
wait_queue_t wait; |
|---|
| 1565 |
|
|---|
| 1566 |
|
|---|
| 1567 |
|
|---|
| 1568 |
|
|---|
| 1569 |
|
|---|
| 1570 |
|
|---|
| 1571 |
jtimeout = timeout == -1 || timeout > (MAX_SCHEDULE_TIMEOUT - 1000) / HZ ? |
|---|
| 1572 |
MAX_SCHEDULE_TIMEOUT: (timeout * HZ + 999) / 1000; |
|---|
| 1573 |
|
|---|
| 1574 |
retry: |
|---|
| 1575 |
write_lock_irqsave(&ep->lock, flags); |
|---|
| 1576 |
|
|---|
| 1577 |
res = 0; |
|---|
| 1578 |
if (list_empty(&ep->rdllist)) { |
|---|
| 1579 |
|
|---|
| 1580 |
|
|---|
| 1581 |
|
|---|
| 1582 |
|
|---|
| 1583 |
|
|---|
| 1584 |
init_waitqueue_entry(&wait, current); |
|---|
| 1585 |
add_wait_queue(&ep->wq, &wait); |
|---|
| 1586 |
|
|---|
| 1587 |
for (;;) { |
|---|
| 1588 |
|
|---|
| 1589 |
|
|---|
| 1590 |
|
|---|
| 1591 |
|
|---|
| 1592 |
|
|---|
| 1593 |
set_current_state(TASK_INTERRUPTIBLE); |
|---|
| 1594 |
if (!list_empty(&ep->rdllist) || !jtimeout) |
|---|
| 1595 |
break; |
|---|
| 1596 |
if (signal_pending(current)) { |
|---|
| 1597 |
res = -EINTR; |
|---|
| 1598 |
break; |
|---|
| 1599 |
} |
|---|
| 1600 |
|
|---|
| 1601 |
write_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1602 |
jtimeout = schedule_timeout(jtimeout); |
|---|
| 1603 |
write_lock_irqsave(&ep->lock, flags); |
|---|
| 1604 |
} |
|---|
| 1605 |
remove_wait_queue(&ep->wq, &wait); |
|---|
| 1606 |
|
|---|
| 1607 |
set_current_state(TASK_RUNNING); |
|---|
| 1608 |
} |
|---|
| 1609 |
|
|---|
| 1610 |
|
|---|
| 1611 |
eavail = !list_empty(&ep->rdllist); |
|---|
| 1612 |
|
|---|
| 1613 |
write_unlock_irqrestore(&ep->lock, flags); |
|---|
| 1614 |
|
|---|
| 1615 |
|
|---|
| 1616 |
|
|---|
| 1617 |
|
|---|
| 1618 |
|
|---|
| 1619 |
|
|---|
| 1620 |
if (!res && eavail && |
|---|
| 1621 |
!(res = ep_events_transfer(ep, events, maxevents)) && jtimeout) |
|---|
| 1622 |
goto retry; |
|---|
| 1623 |
|
|---|
| 1624 |
return res; |
|---|
| 1625 |
} |
|---|
| 1626 |
|
|---|
| 1627 |
|
|---|
| 1628 |
static int eventpollfs_delete_dentry(struct dentry *dentry) |
|---|
| 1629 |
{ |
|---|
| 1630 |
|
|---|
| 1631 |
return 1; |
|---|
| 1632 |
} |
|---|
| 1633 |
|
|---|
| 1634 |
|
|---|
| 1635 |
static struct inode *ep_eventpoll_inode(void) |
|---|
| 1636 |
{ |
|---|
| 1637 |
int error = -ENOMEM; |
|---|
| 1638 |
struct inode *inode = new_inode(eventpoll_mnt->mnt_sb); |
|---|
| 1639 |
|
|---|
| 1640 |
if (!inode) |
|---|
| 1641 |
goto eexit_1; |
|---|
| 1642 |
|
|---|
| 1643 |
inode->i_fop = &eventpoll_fops; |
|---|
| 1644 |
|
|---|
| 1645 |
|
|---|
| 1646 |
|
|---|
| 1647 |
|
|---|
| 1648 |
|
|---|
| 1649 |
|
|---|
| 1650 |
|
|---|
| 1651 |
inode->i_state = I_DIRTY; |
|---|
| 1652 |
inode->i_mode = S_IRUSR | S_IWUSR; |
|---|
| 1653 |
inode->i_uid = current->fsuid; |
|---|
| 1654 |
inode->i_gid = current->fsgid; |
|---|
| 1655 |
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
|---|
| 1656 |
inode->i_blksize = PAGE_SIZE; |
|---|
| 1657 |
return inode; |
|---|
| 1658 |
|
|---|
| 1659 |
eexit_1: |
|---|
| 1660 |
return ERR_PTR(error); |
|---|
| 1661 |
} |
|---|
| 1662 |
|
|---|
| 1663 |
|
|---|
| 1664 |
static struct super_block * |
|---|
| 1665 |
eventpollfs_get_sb(struct file_system_type *fs_type, int flags, |
|---|
| 1666 |
const char *dev_name, void *data) |
|---|
| 1667 |
{ |
|---|
| 1668 |
return get_sb_pseudo(fs_type, "eventpoll:", NULL, EVENTPOLLFS_MAGIC); |
|---|
| 1669 |
} |
|---|
| 1670 |
|
|---|
| 1671 |
|
|---|
| 1672 |
static int __init eventpoll_init(void) |
|---|
| 1673 |
{ |
|---|
| 1674 |
int error; |
|---|
| 1675 |
|
|---|
| 1676 |
init_MUTEX(&epsem); |
|---|
| 1677 |
|
|---|
| 1678 |
|
|---|
| 1679 |
ep_poll_safewake_init(&psw); |
|---|
| 1680 |
|
|---|
| 1681 |
|
|---|
| 1682 |
epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem), |
|---|
| 1683 |
0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC, |
|---|
| 1684 |
NULL, NULL); |
|---|
| 1685 |
|
|---|
| 1686 |
|
|---|
| 1687 |
pwq_cache = kmem_cache_create("eventpoll_pwq", |
|---|
| 1688 |
sizeof(struct eppoll_entry), 0, |
|---|
| 1689 |
EPI_SLAB_DEBUG|SLAB_PANIC, NULL, NULL); |
|---|
| 1690 |
|
|---|
| 1691 |
|
|---|
| 1692 |
|
|---|
| 1693 |
|
|---|
| 1694 |
|
|---|
| 1695 |
error = register_filesystem(&eventpoll_fs_type); |
|---|
| 1696 |
if (error) |
|---|
| 1697 |
goto epanic; |
|---|
| 1698 |
|
|---|
| 1699 |
|
|---|
| 1700 |
eventpoll_mnt = kern_mount(&eventpoll_fs_type); |
|---|
| 1701 |
error = PTR_ERR(eventpoll_mnt); |
|---|
| 1702 |
if (IS_ERR(eventpoll_mnt)) |
|---|
| 1703 |
goto epanic; |
|---|
| 1704 |
|
|---|
| 1705 |
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: successfully initialized.\n", |
|---|
| 1706 |
current)); |
|---|
| 1707 |
return 0; |
|---|
| 1708 |
|
|---|
| 1709 |
epanic: |
|---|
| 1710 |
panic("eventpoll_init() failed\n"); |
|---|
| 1711 |
} |
|---|
| 1712 |
|
|---|
| 1713 |
|
|---|
| 1714 |
static void __exit eventpoll_exit(void) |
|---|
| 1715 |
{ |
|---|
| 1716 |
|
|---|
| 1717 |
unregister_filesystem(&eventpoll_fs_type); |
|---|
| 1718 |
mntput(eventpoll_mnt); |
|---|
| 1719 |
kmem_cache_destroy(pwq_cache); |
|---|
| 1720 |
kmem_cache_destroy(epi_cache); |
|---|
| 1721 |
} |
|---|
| 1722 |
|
|---|
| 1723 |
module_init(eventpoll_init); |
|---|
| 1724 |
module_exit(eventpoll_exit); |
|---|
| 1725 |
|
|---|
| 1726 |
MODULE_LICENSE("GPL"); |
|---|