eventpoll.c 39 KB

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