eventpoll.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * fs/eventpoll.c (Efficient event retrieval implementation)
  3. * Copyright (C) 2001,...,2009 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 (mutex)
  46. * 3) ep->lock (spinlock)
  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. /* Epoll private bits inside the event mask */
  72. #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)
  73. /* Maximum number of nesting allowed inside epoll sets */
  74. #define EP_MAX_NESTS 4
  75. #define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
  76. #define EP_UNACTIVE_PTR ((void *) -1L)
  77. #define EP_ITEM_COST (sizeof(struct epitem) + sizeof(struct eppoll_entry))
  78. struct epoll_filefd {
  79. struct file *file;
  80. int fd;
  81. };
  82. /*
  83. * Structure used to track possible nested calls, for too deep recursions
  84. * and loop cycles.
  85. */
  86. struct nested_call_node {
  87. struct list_head llink;
  88. void *cookie;
  89. void *ctx;
  90. };
  91. /*
  92. * This structure is used as collector for nested calls, to check for
  93. * maximum recursion dept and loop cycles.
  94. */
  95. struct nested_calls {
  96. struct list_head tasks_call_list;
  97. spinlock_t lock;
  98. };
  99. /*
  100. * Each file descriptor added to the eventpoll interface will
  101. * have an entry of this type linked to the "rbr" RB tree.
  102. */
  103. struct epitem {
  104. /* RB tree node used to link this structure to the eventpoll RB tree */
  105. struct rb_node rbn;
  106. /* List header used to link this structure to the eventpoll ready list */
  107. struct list_head rdllink;
  108. /*
  109. * Works together "struct eventpoll"->ovflist in keeping the
  110. * single linked chain of items.
  111. */
  112. struct epitem *next;
  113. /* The file descriptor information this item refers to */
  114. struct epoll_filefd ffd;
  115. /* Number of active wait queue attached to poll operations */
  116. int nwait;
  117. /* List containing poll wait queues */
  118. struct list_head pwqlist;
  119. /* The "container" of this item */
  120. struct eventpoll *ep;
  121. /* List header used to link this item to the "struct file" items list */
  122. struct list_head fllink;
  123. /* The structure that describe the interested events and the source fd */
  124. struct epoll_event event;
  125. };
  126. /*
  127. * This structure is stored inside the "private_data" member of the file
  128. * structure and rapresent the main data sructure for the eventpoll
  129. * interface.
  130. */
  131. struct eventpoll {
  132. /* Protect the this structure access */
  133. spinlock_t lock;
  134. /*
  135. * This mutex is used to ensure that files are not removed
  136. * while epoll is using them. This is held during the event
  137. * collection loop, the file cleanup path, the epoll file exit
  138. * code and the ctl operations.
  139. */
  140. struct mutex mtx;
  141. /* Wait queue used by sys_epoll_wait() */
  142. wait_queue_head_t wq;
  143. /* Wait queue used by file->poll() */
  144. wait_queue_head_t poll_wait;
  145. /* List of ready file descriptors */
  146. struct list_head rdllist;
  147. /* RB tree root used to store monitored fd structs */
  148. struct rb_root rbr;
  149. /*
  150. * This is a single linked list that chains all the "struct epitem" that
  151. * happened while transfering ready events to userspace w/out
  152. * holding ->lock.
  153. */
  154. struct epitem *ovflist;
  155. /* The user that created the eventpoll descriptor */
  156. struct user_struct *user;
  157. };
  158. /* Wait structure used by the poll hooks */
  159. struct eppoll_entry {
  160. /* List header used to link this structure to the "struct epitem" */
  161. struct list_head llink;
  162. /* The "base" pointer is set to the container "struct epitem" */
  163. struct epitem *base;
  164. /*
  165. * Wait queue item that will be linked to the target file wait
  166. * queue head.
  167. */
  168. wait_queue_t wait;
  169. /* The wait queue head that linked the "wait" wait queue item */
  170. wait_queue_head_t *whead;
  171. };
  172. /* Wrapper struct used by poll queueing */
  173. struct ep_pqueue {
  174. poll_table pt;
  175. struct epitem *epi;
  176. };
  177. /* Used by the ep_send_events() function as callback private data */
  178. struct ep_send_events_data {
  179. int maxevents;
  180. struct epoll_event __user *events;
  181. };
  182. /*
  183. * Configuration options available inside /proc/sys/fs/epoll/
  184. */
  185. /* Maximum number of epoll watched descriptors, per user */
  186. static int max_user_watches __read_mostly;
  187. /*
  188. * This mutex is used to serialize ep_free() and eventpoll_release_file().
  189. */
  190. static DEFINE_MUTEX(epmutex);
  191. /* Used for safe wake up implementation */
  192. static struct nested_calls poll_safewake_ncalls;
  193. /* Used to call file's f_op->poll() under the nested calls boundaries */
  194. static struct nested_calls poll_readywalk_ncalls;
  195. /* Slab cache used to allocate "struct epitem" */
  196. static struct kmem_cache *epi_cache __read_mostly;
  197. /* Slab cache used to allocate "struct eppoll_entry" */
  198. static struct kmem_cache *pwq_cache __read_mostly;
  199. #ifdef CONFIG_SYSCTL
  200. #include <linux/sysctl.h>
  201. static int zero;
  202. ctl_table epoll_table[] = {
  203. {
  204. .procname = "max_user_watches",
  205. .data = &max_user_watches,
  206. .maxlen = sizeof(int),
  207. .mode = 0644,
  208. .proc_handler = proc_dointvec_minmax,
  209. .extra1 = &zero,
  210. },
  211. { }
  212. };
  213. #endif /* CONFIG_SYSCTL */
  214. /* Setup the structure that is used as key for the RB tree */
  215. static inline void ep_set_ffd(struct epoll_filefd *ffd,
  216. struct file *file, int fd)
  217. {
  218. ffd->file = file;
  219. ffd->fd = fd;
  220. }
  221. /* Compare RB tree keys */
  222. static inline int ep_cmp_ffd(struct epoll_filefd *p1,
  223. struct epoll_filefd *p2)
  224. {
  225. return (p1->file > p2->file ? +1:
  226. (p1->file < p2->file ? -1 : p1->fd - p2->fd));
  227. }
  228. /* Tells us if the item is currently linked */
  229. static inline int ep_is_linked(struct list_head *p)
  230. {
  231. return !list_empty(p);
  232. }
  233. /* Get the "struct epitem" from a wait queue pointer */
  234. static inline struct epitem *ep_item_from_wait(wait_queue_t *p)
  235. {
  236. return container_of(p, struct eppoll_entry, wait)->base;
  237. }
  238. /* Get the "struct epitem" from an epoll queue wrapper */
  239. static inline struct epitem *ep_item_from_epqueue(poll_table *p)
  240. {
  241. return container_of(p, struct ep_pqueue, pt)->epi;
  242. }
  243. /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
  244. static inline int ep_op_has_event(int op)
  245. {
  246. return op != EPOLL_CTL_DEL;
  247. }
  248. /* Initialize the poll safe wake up structure */
  249. static void ep_nested_calls_init(struct nested_calls *ncalls)
  250. {
  251. INIT_LIST_HEAD(&ncalls->tasks_call_list);
  252. spin_lock_init(&ncalls->lock);
  253. }
  254. /**
  255. * ep_call_nested - Perform a bound (possibly) nested call, by checking
  256. * that the recursion limit is not exceeded, and that
  257. * the same nested call (by the meaning of same cookie) is
  258. * no re-entered.
  259. *
  260. * @ncalls: Pointer to the nested_calls structure to be used for this call.
  261. * @max_nests: Maximum number of allowed nesting calls.
  262. * @nproc: Nested call core function pointer.
  263. * @priv: Opaque data to be passed to the @nproc callback.
  264. * @cookie: Cookie to be used to identify this nested call.
  265. * @ctx: This instance context.
  266. *
  267. * Returns: Returns the code returned by the @nproc callback, or -1 if
  268. * the maximum recursion limit has been exceeded.
  269. */
  270. static int ep_call_nested(struct nested_calls *ncalls, int max_nests,
  271. int (*nproc)(void *, void *, int), void *priv,
  272. void *cookie, void *ctx)
  273. {
  274. int error, call_nests = 0;
  275. unsigned long flags;
  276. struct list_head *lsthead = &ncalls->tasks_call_list;
  277. struct nested_call_node *tncur;
  278. struct nested_call_node tnode;
  279. spin_lock_irqsave(&ncalls->lock, flags);
  280. /*
  281. * Try to see if the current task is already inside this wakeup call.
  282. * We use a list here, since the population inside this set is always
  283. * very much limited.
  284. */
  285. list_for_each_entry(tncur, lsthead, llink) {
  286. if (tncur->ctx == ctx &&
  287. (tncur->cookie == cookie || ++call_nests > max_nests)) {
  288. /*
  289. * Ops ... loop detected or maximum nest level reached.
  290. * We abort this wake by breaking the cycle itself.
  291. */
  292. error = -1;
  293. goto out_unlock;
  294. }
  295. }
  296. /* Add the current task and cookie to the list */
  297. tnode.ctx = ctx;
  298. tnode.cookie = cookie;
  299. list_add(&tnode.llink, lsthead);
  300. spin_unlock_irqrestore(&ncalls->lock, flags);
  301. /* Call the nested function */
  302. error = (*nproc)(priv, cookie, call_nests);
  303. /* Remove the current task from the list */
  304. spin_lock_irqsave(&ncalls->lock, flags);
  305. list_del(&tnode.llink);
  306. out_unlock:
  307. spin_unlock_irqrestore(&ncalls->lock, flags);
  308. return error;
  309. }
  310. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  311. static inline void ep_wake_up_nested(wait_queue_head_t *wqueue,
  312. unsigned long events, int subclass)
  313. {
  314. unsigned long flags;
  315. spin_lock_irqsave_nested(&wqueue->lock, flags, subclass);
  316. wake_up_locked_poll(wqueue, events);
  317. spin_unlock_irqrestore(&wqueue->lock, flags);
  318. }
  319. #else
  320. static inline void ep_wake_up_nested(wait_queue_head_t *wqueue,
  321. unsigned long events, int subclass)
  322. {
  323. wake_up_poll(wqueue, events);
  324. }
  325. #endif
  326. static int ep_poll_wakeup_proc(void *priv, void *cookie, int call_nests)
  327. {
  328. ep_wake_up_nested((wait_queue_head_t *) cookie, POLLIN,
  329. 1 + call_nests);
  330. return 0;
  331. }
  332. /*
  333. * Perform a safe wake up of the poll wait list. The problem is that
  334. * with the new callback'd wake up system, it is possible that the
  335. * poll callback is reentered from inside the call to wake_up() done
  336. * on the poll wait queue head. The rule is that we cannot reenter the
  337. * wake up code from the same task more than EP_MAX_NESTS times,
  338. * and we cannot reenter the same wait queue head at all. This will
  339. * enable to have a hierarchy of epoll file descriptor of no more than
  340. * EP_MAX_NESTS deep.
  341. */
  342. static void ep_poll_safewake(wait_queue_head_t *wq)
  343. {
  344. int this_cpu = get_cpu();
  345. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  346. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  347. put_cpu();
  348. }
  349. /*
  350. * This function unregisters poll callbacks from the associated file
  351. * descriptor. Must be called with "mtx" held (or "epmutex" if called from
  352. * ep_free).
  353. */
  354. static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
  355. {
  356. struct list_head *lsthead = &epi->pwqlist;
  357. struct eppoll_entry *pwq;
  358. while (!list_empty(lsthead)) {
  359. pwq = list_first_entry(lsthead, struct eppoll_entry, llink);
  360. list_del(&pwq->llink);
  361. remove_wait_queue(pwq->whead, &pwq->wait);
  362. kmem_cache_free(pwq_cache, pwq);
  363. }
  364. }
  365. /**
  366. * ep_scan_ready_list - Scans the ready list in a way that makes possible for
  367. * the scan code, to call f_op->poll(). Also allows for
  368. * O(NumReady) performance.
  369. *
  370. * @ep: Pointer to the epoll private data structure.
  371. * @sproc: Pointer to the scan callback.
  372. * @priv: Private opaque data passed to the @sproc callback.
  373. *
  374. * Returns: The same integer error code returned by the @sproc callback.
  375. */
  376. static int ep_scan_ready_list(struct eventpoll *ep,
  377. int (*sproc)(struct eventpoll *,
  378. struct list_head *, void *),
  379. void *priv)
  380. {
  381. int error, pwake = 0;
  382. unsigned long flags;
  383. struct epitem *epi, *nepi;
  384. LIST_HEAD(txlist);
  385. /*
  386. * We need to lock this because we could be hit by
  387. * eventpoll_release_file() and epoll_ctl().
  388. */
  389. mutex_lock(&ep->mtx);
  390. /*
  391. * Steal the ready list, and re-init the original one to the
  392. * empty list. Also, set ep->ovflist to NULL so that events
  393. * happening while looping w/out locks, are not lost. We cannot
  394. * have the poll callback to queue directly on ep->rdllist,
  395. * because we want the "sproc" callback to be able to do it
  396. * in a lockless way.
  397. */
  398. spin_lock_irqsave(&ep->lock, flags);
  399. list_splice_init(&ep->rdllist, &txlist);
  400. ep->ovflist = NULL;
  401. spin_unlock_irqrestore(&ep->lock, flags);
  402. /*
  403. * Now call the callback function.
  404. */
  405. error = (*sproc)(ep, &txlist, priv);
  406. spin_lock_irqsave(&ep->lock, flags);
  407. /*
  408. * During the time we spent inside the "sproc" callback, some
  409. * other events might have been queued by the poll callback.
  410. * We re-insert them inside the main ready-list here.
  411. */
  412. for (nepi = ep->ovflist; (epi = nepi) != NULL;
  413. nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
  414. /*
  415. * We need to check if the item is already in the list.
  416. * During the "sproc" callback execution time, items are
  417. * queued into ->ovflist but the "txlist" might already
  418. * contain them, and the list_splice() below takes care of them.
  419. */
  420. if (!ep_is_linked(&epi->rdllink))
  421. list_add_tail(&epi->rdllink, &ep->rdllist);
  422. }
  423. /*
  424. * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
  425. * releasing the lock, events will be queued in the normal way inside
  426. * ep->rdllist.
  427. */
  428. ep->ovflist = EP_UNACTIVE_PTR;
  429. /*
  430. * Quickly re-inject items left on "txlist".
  431. */
  432. list_splice(&txlist, &ep->rdllist);
  433. if (!list_empty(&ep->rdllist)) {
  434. /*
  435. * Wake up (if active) both the eventpoll wait list and
  436. * the ->poll() wait list (delayed after we release the lock).
  437. */
  438. if (waitqueue_active(&ep->wq))
  439. wake_up_locked(&ep->wq);
  440. if (waitqueue_active(&ep->poll_wait))
  441. pwake++;
  442. }
  443. spin_unlock_irqrestore(&ep->lock, flags);
  444. mutex_unlock(&ep->mtx);
  445. /* We have to call this outside the lock */
  446. if (pwake)
  447. ep_poll_safewake(&ep->poll_wait);
  448. return error;
  449. }
  450. /*
  451. * Removes a "struct epitem" from the eventpoll RB tree and deallocates
  452. * all the associated resources. Must be called with "mtx" held.
  453. */
  454. static int ep_remove(struct eventpoll *ep, struct epitem *epi)
  455. {
  456. unsigned long flags;
  457. struct file *file = epi->ffd.file;
  458. /*
  459. * Removes poll wait queue hooks. We _have_ to do this without holding
  460. * the "ep->lock" otherwise a deadlock might occur. This because of the
  461. * sequence of the lock acquisition. Here we do "ep->lock" then the wait
  462. * queue head lock when unregistering the wait queue. The wakeup callback
  463. * will run by holding the wait queue head lock and will call our callback
  464. * that will try to get "ep->lock".
  465. */
  466. ep_unregister_pollwait(ep, epi);
  467. /* Remove the current item from the list of epoll hooks */
  468. spin_lock(&file->f_lock);
  469. if (ep_is_linked(&epi->fllink))
  470. list_del_init(&epi->fllink);
  471. spin_unlock(&file->f_lock);
  472. rb_erase(&epi->rbn, &ep->rbr);
  473. spin_lock_irqsave(&ep->lock, flags);
  474. if (ep_is_linked(&epi->rdllink))
  475. list_del_init(&epi->rdllink);
  476. spin_unlock_irqrestore(&ep->lock, flags);
  477. /* At this point it is safe to free the eventpoll item */
  478. kmem_cache_free(epi_cache, epi);
  479. atomic_dec(&ep->user->epoll_watches);
  480. return 0;
  481. }
  482. static void ep_free(struct eventpoll *ep)
  483. {
  484. struct rb_node *rbp;
  485. struct epitem *epi;
  486. /* We need to release all tasks waiting for these file */
  487. if (waitqueue_active(&ep->poll_wait))
  488. ep_poll_safewake(&ep->poll_wait);
  489. /*
  490. * We need to lock this because we could be hit by
  491. * eventpoll_release_file() while we're freeing the "struct eventpoll".
  492. * We do not need to hold "ep->mtx" here because the epoll file
  493. * is on the way to be removed and no one has references to it
  494. * anymore. The only hit might come from eventpoll_release_file() but
  495. * holding "epmutex" is sufficent here.
  496. */
  497. mutex_lock(&epmutex);
  498. /*
  499. * Walks through the whole tree by unregistering poll callbacks.
  500. */
  501. for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
  502. epi = rb_entry(rbp, struct epitem, rbn);
  503. ep_unregister_pollwait(ep, epi);
  504. }
  505. /*
  506. * Walks through the whole tree by freeing each "struct epitem". At this
  507. * point we are sure no poll callbacks will be lingering around, and also by
  508. * holding "epmutex" we can be sure that no file cleanup code will hit
  509. * us during this operation. So we can avoid the lock on "ep->lock".
  510. */
  511. while ((rbp = rb_first(&ep->rbr)) != NULL) {
  512. epi = rb_entry(rbp, struct epitem, rbn);
  513. ep_remove(ep, epi);
  514. }
  515. mutex_unlock(&epmutex);
  516. mutex_destroy(&ep->mtx);
  517. free_uid(ep->user);
  518. kfree(ep);
  519. }
  520. static int ep_eventpoll_release(struct inode *inode, struct file *file)
  521. {
  522. struct eventpoll *ep = file->private_data;
  523. if (ep)
  524. ep_free(ep);
  525. return 0;
  526. }
  527. static int ep_read_events_proc(struct eventpoll *ep, struct list_head *head,
  528. void *priv)
  529. {
  530. struct epitem *epi, *tmp;
  531. list_for_each_entry_safe(epi, tmp, head, rdllink) {
  532. if (epi->ffd.file->f_op->poll(epi->ffd.file, NULL) &
  533. epi->event.events)
  534. return POLLIN | POLLRDNORM;
  535. else {
  536. /*
  537. * Item has been dropped into the ready list by the poll
  538. * callback, but it's not actually ready, as far as
  539. * caller requested events goes. We can remove it here.
  540. */
  541. list_del_init(&epi->rdllink);
  542. }
  543. }
  544. return 0;
  545. }
  546. static int ep_poll_readyevents_proc(void *priv, void *cookie, int call_nests)
  547. {
  548. return ep_scan_ready_list(priv, ep_read_events_proc, NULL);
  549. }
  550. static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
  551. {
  552. int pollflags;
  553. struct eventpoll *ep = file->private_data;
  554. /* Insert inside our poll wait queue */
  555. poll_wait(file, &ep->poll_wait, wait);
  556. /*
  557. * Proceed to find out if wanted events are really available inside
  558. * the ready list. This need to be done under ep_call_nested()
  559. * supervision, since the call to f_op->poll() done on listed files
  560. * could re-enter here.
  561. */
  562. pollflags = ep_call_nested(&poll_readywalk_ncalls, EP_MAX_NESTS,
  563. ep_poll_readyevents_proc, ep, ep, current);
  564. return pollflags != -1 ? pollflags : 0;
  565. }
  566. /* File callbacks that implement the eventpoll file behaviour */
  567. static const struct file_operations eventpoll_fops = {
  568. .release = ep_eventpoll_release,
  569. .poll = ep_eventpoll_poll,
  570. .llseek = noop_llseek,
  571. };
  572. /* Fast test to see if the file is an evenpoll file */
  573. static inline int is_file_epoll(struct file *f)
  574. {
  575. return f->f_op == &eventpoll_fops;
  576. }
  577. /*
  578. * This is called from eventpoll_release() to unlink files from the eventpoll
  579. * interface. We need to have this facility to cleanup correctly files that are
  580. * closed without being removed from the eventpoll interface.
  581. */
  582. void eventpoll_release_file(struct file *file)
  583. {
  584. struct list_head *lsthead = &file->f_ep_links;
  585. struct eventpoll *ep;
  586. struct epitem *epi;
  587. /*
  588. * We don't want to get "file->f_lock" because it is not
  589. * necessary. It is not necessary because we're in the "struct file"
  590. * cleanup path, and this means that noone is using this file anymore.
  591. * So, for example, epoll_ctl() cannot hit here since if we reach this
  592. * point, the file counter already went to zero and fget() would fail.
  593. * The only hit might come from ep_free() but by holding the mutex
  594. * will correctly serialize the operation. We do need to acquire
  595. * "ep->mtx" after "epmutex" because ep_remove() requires it when called
  596. * from anywhere but ep_free().
  597. *
  598. * Besides, ep_remove() acquires the lock, so we can't hold it here.
  599. */
  600. mutex_lock(&epmutex);
  601. while (!list_empty(lsthead)) {
  602. epi = list_first_entry(lsthead, struct epitem, fllink);
  603. ep = epi->ep;
  604. list_del_init(&epi->fllink);
  605. mutex_lock(&ep->mtx);
  606. ep_remove(ep, epi);
  607. mutex_unlock(&ep->mtx);
  608. }
  609. mutex_unlock(&epmutex);
  610. }
  611. static int ep_alloc(struct eventpoll **pep)
  612. {
  613. int error;
  614. struct user_struct *user;
  615. struct eventpoll *ep;
  616. user = get_current_user();
  617. error = -ENOMEM;
  618. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  619. if (unlikely(!ep))
  620. goto free_uid;
  621. spin_lock_init(&ep->lock);
  622. mutex_init(&ep->mtx);
  623. init_waitqueue_head(&ep->wq);
  624. init_waitqueue_head(&ep->poll_wait);
  625. INIT_LIST_HEAD(&ep->rdllist);
  626. ep->rbr = RB_ROOT;
  627. ep->ovflist = EP_UNACTIVE_PTR;
  628. ep->user = user;
  629. *pep = ep;
  630. return 0;
  631. free_uid:
  632. free_uid(user);
  633. return error;
  634. }
  635. /*
  636. * Search the file inside the eventpoll tree. The RB tree operations
  637. * are protected by the "mtx" mutex, and ep_find() must be called with
  638. * "mtx" held.
  639. */
  640. static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
  641. {
  642. int kcmp;
  643. struct rb_node *rbp;
  644. struct epitem *epi, *epir = NULL;
  645. struct epoll_filefd ffd;
  646. ep_set_ffd(&ffd, file, fd);
  647. for (rbp = ep->rbr.rb_node; rbp; ) {
  648. epi = rb_entry(rbp, struct epitem, rbn);
  649. kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
  650. if (kcmp > 0)
  651. rbp = rbp->rb_right;
  652. else if (kcmp < 0)
  653. rbp = rbp->rb_left;
  654. else {
  655. epir = epi;
  656. break;
  657. }
  658. }
  659. return epir;
  660. }
  661. /*
  662. * This is the callback that is passed to the wait queue wakeup
  663. * machanism. It is called by the stored file descriptors when they
  664. * have events to report.
  665. */
  666. static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
  667. {
  668. int pwake = 0;
  669. unsigned long flags;
  670. struct epitem *epi = ep_item_from_wait(wait);
  671. struct eventpoll *ep = epi->ep;
  672. spin_lock_irqsave(&ep->lock, flags);
  673. /*
  674. * If the event mask does not contain any poll(2) event, we consider the
  675. * descriptor to be disabled. This condition is likely the effect of the
  676. * EPOLLONESHOT bit that disables the descriptor when an event is received,
  677. * until the next EPOLL_CTL_MOD will be issued.
  678. */
  679. if (!(epi->event.events & ~EP_PRIVATE_BITS))
  680. goto out_unlock;
  681. /*
  682. * Check the events coming with the callback. At this stage, not
  683. * every device reports the events in the "key" parameter of the
  684. * callback. We need to be able to handle both cases here, hence the
  685. * test for "key" != NULL before the event match test.
  686. */
  687. if (key && !((unsigned long) key & epi->event.events))
  688. goto out_unlock;
  689. /*
  690. * If we are trasfering events to userspace, we can hold no locks
  691. * (because we're accessing user memory, and because of linux f_op->poll()
  692. * semantics). All the events that happens during that period of time are
  693. * chained in ep->ovflist and requeued later on.
  694. */
  695. if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) {
  696. if (epi->next == EP_UNACTIVE_PTR) {
  697. epi->next = ep->ovflist;
  698. ep->ovflist = epi;
  699. }
  700. goto out_unlock;
  701. }
  702. /* If this file is already in the ready list we exit soon */
  703. if (!ep_is_linked(&epi->rdllink))
  704. list_add_tail(&epi->rdllink, &ep->rdllist);
  705. /*
  706. * Wake up ( if active ) both the eventpoll wait list and the ->poll()
  707. * wait list.
  708. */
  709. if (waitqueue_active(&ep->wq))
  710. wake_up_locked(&ep->wq);
  711. if (waitqueue_active(&ep->poll_wait))
  712. pwake++;
  713. out_unlock:
  714. spin_unlock_irqrestore(&ep->lock, flags);
  715. /* We have to call this outside the lock */
  716. if (pwake)
  717. ep_poll_safewake(&ep->poll_wait);
  718. return 1;
  719. }
  720. /*
  721. * This is the callback that is used to add our wait queue to the
  722. * target file wakeup lists.
  723. */
  724. static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
  725. poll_table *pt)
  726. {
  727. struct epitem *epi = ep_item_from_epqueue(pt);
  728. struct eppoll_entry *pwq;
  729. if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
  730. init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
  731. pwq->whead = whead;
  732. pwq->base = epi;
  733. add_wait_queue(whead, &pwq->wait);
  734. list_add_tail(&pwq->llink, &epi->pwqlist);
  735. epi->nwait++;
  736. } else {
  737. /* We have to signal that an error occurred */
  738. epi->nwait = -1;
  739. }
  740. }
  741. static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
  742. {
  743. int kcmp;
  744. struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
  745. struct epitem *epic;
  746. while (*p) {
  747. parent = *p;
  748. epic = rb_entry(parent, struct epitem, rbn);
  749. kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
  750. if (kcmp > 0)
  751. p = &parent->rb_right;
  752. else
  753. p = &parent->rb_left;
  754. }
  755. rb_link_node(&epi->rbn, parent, p);
  756. rb_insert_color(&epi->rbn, &ep->rbr);
  757. }
  758. /*
  759. * Must be called with "mtx" held.
  760. */
  761. static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
  762. struct file *tfile, int fd)
  763. {
  764. int error, revents, pwake = 0;
  765. unsigned long flags;
  766. struct epitem *epi;
  767. struct ep_pqueue epq;
  768. if (unlikely(atomic_read(&ep->user->epoll_watches) >=
  769. max_user_watches))
  770. return -ENOSPC;
  771. if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
  772. return -ENOMEM;
  773. /* Item initialization follow here ... */
  774. INIT_LIST_HEAD(&epi->rdllink);
  775. INIT_LIST_HEAD(&epi->fllink);
  776. INIT_LIST_HEAD(&epi->pwqlist);
  777. epi->ep = ep;
  778. ep_set_ffd(&epi->ffd, tfile, fd);
  779. epi->event = *event;
  780. epi->nwait = 0;
  781. epi->next = EP_UNACTIVE_PTR;
  782. /* Initialize the poll table using the queue callback */
  783. epq.epi = epi;
  784. init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
  785. /*
  786. * Attach the item to the poll hooks and get current event bits.
  787. * We can safely use the file* here because its usage count has
  788. * been increased by the caller of this function. Note that after
  789. * this operation completes, the poll callback can start hitting
  790. * the new item.
  791. */
  792. revents = tfile->f_op->poll(tfile, &epq.pt);
  793. /*
  794. * We have to check if something went wrong during the poll wait queue
  795. * install process. Namely an allocation for a wait queue failed due
  796. * high memory pressure.
  797. */
  798. error = -ENOMEM;
  799. if (epi->nwait < 0)
  800. goto error_unregister;
  801. /* Add the current item to the list of active epoll hook for this file */
  802. spin_lock(&tfile->f_lock);
  803. list_add_tail(&epi->fllink, &tfile->f_ep_links);
  804. spin_unlock(&tfile->f_lock);
  805. /*
  806. * Add the current item to the RB tree. All RB tree operations are
  807. * protected by "mtx", and ep_insert() is called with "mtx" held.
  808. */
  809. ep_rbtree_insert(ep, epi);
  810. /* We have to drop the new item inside our item list to keep track of it */
  811. spin_lock_irqsave(&ep->lock, flags);
  812. /* If the file is already "ready" we drop it inside the ready list */
  813. if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
  814. list_add_tail(&epi->rdllink, &ep->rdllist);
  815. /* Notify waiting tasks that events are available */
  816. if (waitqueue_active(&ep->wq))
  817. wake_up_locked(&ep->wq);
  818. if (waitqueue_active(&ep->poll_wait))
  819. pwake++;
  820. }
  821. spin_unlock_irqrestore(&ep->lock, flags);
  822. atomic_inc(&ep->user->epoll_watches);
  823. /* We have to call this outside the lock */
  824. if (pwake)
  825. ep_poll_safewake(&ep->poll_wait);
  826. return 0;
  827. error_unregister:
  828. ep_unregister_pollwait(ep, epi);
  829. /*
  830. * We need to do this because an event could have been arrived on some
  831. * allocated wait queue. Note that we don't care about the ep->ovflist
  832. * list, since that is used/cleaned only inside a section bound by "mtx".
  833. * And ep_insert() is called with "mtx" held.
  834. */
  835. spin_lock_irqsave(&ep->lock, flags);
  836. if (ep_is_linked(&epi->rdllink))
  837. list_del_init(&epi->rdllink);
  838. spin_unlock_irqrestore(&ep->lock, flags);
  839. kmem_cache_free(epi_cache, epi);
  840. return error;
  841. }
  842. /*
  843. * Modify the interest event mask by dropping an event if the new mask
  844. * has a match in the current file status. Must be called with "mtx" held.
  845. */
  846. static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
  847. {
  848. int pwake = 0;
  849. unsigned int revents;
  850. /*
  851. * Set the new event interest mask before calling f_op->poll();
  852. * otherwise we might miss an event that happens between the
  853. * f_op->poll() call and the new event set registering.
  854. */
  855. epi->event.events = event->events;
  856. epi->event.data = event->data; /* protected by mtx */
  857. /*
  858. * Get current event bits. We can safely use the file* here because
  859. * its usage count has been increased by the caller of this function.
  860. */
  861. revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
  862. /*
  863. * If the item is "hot" and it is not registered inside the ready
  864. * list, push it inside.
  865. */
  866. if (revents & event->events) {
  867. spin_lock_irq(&ep->lock);
  868. if (!ep_is_linked(&epi->rdllink)) {
  869. list_add_tail(&epi->rdllink, &ep->rdllist);
  870. /* Notify waiting tasks that events are available */
  871. if (waitqueue_active(&ep->wq))
  872. wake_up_locked(&ep->wq);
  873. if (waitqueue_active(&ep->poll_wait))
  874. pwake++;
  875. }
  876. spin_unlock_irq(&ep->lock);
  877. }
  878. /* We have to call this outside the lock */
  879. if (pwake)
  880. ep_poll_safewake(&ep->poll_wait);
  881. return 0;
  882. }
  883. static int ep_send_events_proc(struct eventpoll *ep, struct list_head *head,
  884. void *priv)
  885. {
  886. struct ep_send_events_data *esed = priv;
  887. int eventcnt;
  888. unsigned int revents;
  889. struct epitem *epi;
  890. struct epoll_event __user *uevent;
  891. /*
  892. * We can loop without lock because we are passed a task private list.
  893. * Items cannot vanish during the loop because ep_scan_ready_list() is
  894. * holding "mtx" during this call.
  895. */
  896. for (eventcnt = 0, uevent = esed->events;
  897. !list_empty(head) && eventcnt < esed->maxevents;) {
  898. epi = list_first_entry(head, struct epitem, rdllink);
  899. list_del_init(&epi->rdllink);
  900. revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL) &
  901. epi->event.events;
  902. /*
  903. * If the event mask intersect the caller-requested one,
  904. * deliver the event to userspace. Again, ep_scan_ready_list()
  905. * is holding "mtx", so no operations coming from userspace
  906. * can change the item.
  907. */
  908. if (revents) {
  909. if (__put_user(revents, &uevent->events) ||
  910. __put_user(epi->event.data, &uevent->data)) {
  911. list_add(&epi->rdllink, head);
  912. return eventcnt ? eventcnt : -EFAULT;
  913. }
  914. eventcnt++;
  915. uevent++;
  916. if (epi->event.events & EPOLLONESHOT)
  917. epi->event.events &= EP_PRIVATE_BITS;
  918. else if (!(epi->event.events & EPOLLET)) {
  919. /*
  920. * If this file has been added with Level
  921. * Trigger mode, we need to insert back inside
  922. * the ready list, so that the next call to
  923. * epoll_wait() will check again the events
  924. * availability. At this point, noone can insert
  925. * into ep->rdllist besides us. The epoll_ctl()
  926. * callers are locked out by
  927. * ep_scan_ready_list() holding "mtx" and the
  928. * poll callback will queue them in ep->ovflist.
  929. */
  930. list_add_tail(&epi->rdllink, &ep->rdllist);
  931. }
  932. }
  933. }
  934. return eventcnt;
  935. }
  936. static int ep_send_events(struct eventpoll *ep,
  937. struct epoll_event __user *events, int maxevents)
  938. {
  939. struct ep_send_events_data esed;
  940. esed.maxevents = maxevents;
  941. esed.events = events;
  942. return ep_scan_ready_list(ep, ep_send_events_proc, &esed);
  943. }
  944. static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
  945. int maxevents, long timeout)
  946. {
  947. int res, eavail, timed_out = 0;
  948. unsigned long flags;
  949. long slack;
  950. wait_queue_t wait;
  951. struct timespec end_time;
  952. ktime_t expires, *to = NULL;
  953. if (timeout > 0) {
  954. ktime_get_ts(&end_time);
  955. timespec_add_ns(&end_time, (u64)timeout * NSEC_PER_MSEC);
  956. slack = select_estimate_accuracy(&end_time);
  957. to = &expires;
  958. *to = timespec_to_ktime(end_time);
  959. } else if (timeout == 0) {
  960. timed_out = 1;
  961. }
  962. retry:
  963. spin_lock_irqsave(&ep->lock, flags);
  964. res = 0;
  965. if (list_empty(&ep->rdllist)) {
  966. /*
  967. * We don't have any available event to return to the caller.
  968. * We need to sleep here, and we will be wake up by
  969. * ep_poll_callback() when events will become available.
  970. */
  971. init_waitqueue_entry(&wait, current);
  972. __add_wait_queue_exclusive(&ep->wq, &wait);
  973. for (;;) {
  974. /*
  975. * We don't want to sleep if the ep_poll_callback() sends us
  976. * a wakeup in between. That's why we set the task state
  977. * to TASK_INTERRUPTIBLE before doing the checks.
  978. */
  979. set_current_state(TASK_INTERRUPTIBLE);
  980. if (!list_empty(&ep->rdllist) || timed_out)
  981. break;
  982. if (signal_pending(current)) {
  983. res = -EINTR;
  984. break;
  985. }
  986. spin_unlock_irqrestore(&ep->lock, flags);
  987. if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS))
  988. timed_out = 1;
  989. spin_lock_irqsave(&ep->lock, flags);
  990. }
  991. __remove_wait_queue(&ep->wq, &wait);
  992. set_current_state(TASK_RUNNING);
  993. }
  994. /* Is it worth to try to dig for events ? */
  995. eavail = !list_empty(&ep->rdllist) || ep->ovflist != EP_UNACTIVE_PTR;
  996. spin_unlock_irqrestore(&ep->lock, flags);
  997. /*
  998. * Try to transfer events to user space. In case we get 0 events and
  999. * there's still timeout left over, we go trying again in search of
  1000. * more luck.
  1001. */
  1002. if (!res && eavail &&
  1003. !(res = ep_send_events(ep, events, maxevents)) && !timed_out)
  1004. goto retry;
  1005. return res;
  1006. }
  1007. /*
  1008. * Open an eventpoll file descriptor.
  1009. */
  1010. SYSCALL_DEFINE1(epoll_create1, int, flags)
  1011. {
  1012. int error;
  1013. struct eventpoll *ep = NULL;
  1014. /* Check the EPOLL_* constant for consistency. */
  1015. BUILD_BUG_ON(EPOLL_CLOEXEC != O_CLOEXEC);
  1016. if (flags & ~EPOLL_CLOEXEC)
  1017. return -EINVAL;
  1018. /*
  1019. * Create the internal data structure ("struct eventpoll").
  1020. */
  1021. error = ep_alloc(&ep);
  1022. if (error < 0)
  1023. return error;
  1024. /*
  1025. * Creates all the items needed to setup an eventpoll file. That is,
  1026. * a file structure and a free file descriptor.
  1027. */
  1028. error = anon_inode_getfd("[eventpoll]", &eventpoll_fops, ep,
  1029. O_RDWR | (flags & O_CLOEXEC));
  1030. if (error < 0)
  1031. ep_free(ep);
  1032. return error;
  1033. }
  1034. SYSCALL_DEFINE1(epoll_create, int, size)
  1035. {
  1036. if (size <= 0)
  1037. return -EINVAL;
  1038. return sys_epoll_create1(0);
  1039. }
  1040. /*
  1041. * The following function implements the controller interface for
  1042. * the eventpoll file that enables the insertion/removal/change of
  1043. * file descriptors inside the interest set.
  1044. */
  1045. SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
  1046. struct epoll_event __user *, event)
  1047. {
  1048. int error;
  1049. struct file *file, *tfile;
  1050. struct eventpoll *ep;
  1051. struct epitem *epi;
  1052. struct epoll_event epds;
  1053. error = -EFAULT;
  1054. if (ep_op_has_event(op) &&
  1055. copy_from_user(&epds, event, sizeof(struct epoll_event)))
  1056. goto error_return;
  1057. /* Get the "struct file *" for the eventpoll file */
  1058. error = -EBADF;
  1059. file = fget(epfd);
  1060. if (!file)
  1061. goto error_return;
  1062. /* Get the "struct file *" for the target file */
  1063. tfile = fget(fd);
  1064. if (!tfile)
  1065. goto error_fput;
  1066. /* The target file descriptor must support poll */
  1067. error = -EPERM;
  1068. if (!tfile->f_op || !tfile->f_op->poll)
  1069. goto error_tgt_fput;
  1070. /*
  1071. * We have to check that the file structure underneath the file descriptor
  1072. * the user passed to us _is_ an eventpoll file. And also we do not permit
  1073. * adding an epoll file descriptor inside itself.
  1074. */
  1075. error = -EINVAL;
  1076. if (file == tfile || !is_file_epoll(file))
  1077. goto error_tgt_fput;
  1078. /*
  1079. * At this point it is safe to assume that the "private_data" contains
  1080. * our own data structure.
  1081. */
  1082. ep = file->private_data;
  1083. mutex_lock(&ep->mtx);
  1084. /*
  1085. * Try to lookup the file inside our RB tree, Since we grabbed "mtx"
  1086. * above, we can be sure to be able to use the item looked up by
  1087. * ep_find() till we release the mutex.
  1088. */
  1089. epi = ep_find(ep, tfile, fd);
  1090. error = -EINVAL;
  1091. switch (op) {
  1092. case EPOLL_CTL_ADD:
  1093. if (!epi) {
  1094. epds.events |= POLLERR | POLLHUP;
  1095. error = ep_insert(ep, &epds, tfile, fd);
  1096. } else
  1097. error = -EEXIST;
  1098. break;
  1099. case EPOLL_CTL_DEL:
  1100. if (epi)
  1101. error = ep_remove(ep, epi);
  1102. else
  1103. error = -ENOENT;
  1104. break;
  1105. case EPOLL_CTL_MOD:
  1106. if (epi) {
  1107. epds.events |= POLLERR | POLLHUP;
  1108. error = ep_modify(ep, epi, &epds);
  1109. } else
  1110. error = -ENOENT;
  1111. break;
  1112. }
  1113. mutex_unlock(&ep->mtx);
  1114. error_tgt_fput:
  1115. fput(tfile);
  1116. error_fput:
  1117. fput(file);
  1118. error_return:
  1119. return error;
  1120. }
  1121. /*
  1122. * Implement the event wait interface for the eventpoll file. It is the kernel
  1123. * part of the user space epoll_wait(2).
  1124. */
  1125. SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,
  1126. int, maxevents, int, timeout)
  1127. {
  1128. int error;
  1129. struct file *file;
  1130. struct eventpoll *ep;
  1131. /* The maximum number of event must be greater than zero */
  1132. if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
  1133. return -EINVAL;
  1134. /* Verify that the area passed by the user is writeable */
  1135. if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
  1136. error = -EFAULT;
  1137. goto error_return;
  1138. }
  1139. /* Get the "struct file *" for the eventpoll file */
  1140. error = -EBADF;
  1141. file = fget(epfd);
  1142. if (!file)
  1143. goto error_return;
  1144. /*
  1145. * We have to check that the file structure underneath the fd
  1146. * the user passed to us _is_ an eventpoll file.
  1147. */
  1148. error = -EINVAL;
  1149. if (!is_file_epoll(file))
  1150. goto error_fput;
  1151. /*
  1152. * At this point it is safe to assume that the "private_data" contains
  1153. * our own data structure.
  1154. */
  1155. ep = file->private_data;
  1156. /* Time to fish for events ... */
  1157. error = ep_poll(ep, events, maxevents, timeout);
  1158. error_fput:
  1159. fput(file);
  1160. error_return:
  1161. return error;
  1162. }
  1163. #ifdef HAVE_SET_RESTORE_SIGMASK
  1164. /*
  1165. * Implement the event wait interface for the eventpoll file. It is the kernel
  1166. * part of the user space epoll_pwait(2).
  1167. */
  1168. SYSCALL_DEFINE6(epoll_pwait, int, epfd, struct epoll_event __user *, events,
  1169. int, maxevents, int, timeout, const sigset_t __user *, sigmask,
  1170. size_t, sigsetsize)
  1171. {
  1172. int error;
  1173. sigset_t ksigmask, sigsaved;
  1174. /*
  1175. * If the caller wants a certain signal mask to be set during the wait,
  1176. * we apply it here.
  1177. */
  1178. if (sigmask) {
  1179. if (sigsetsize != sizeof(sigset_t))
  1180. return -EINVAL;
  1181. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  1182. return -EFAULT;
  1183. sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  1184. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1185. }
  1186. error = sys_epoll_wait(epfd, events, maxevents, timeout);
  1187. /*
  1188. * If we changed the signal mask, we need to restore the original one.
  1189. * In case we've got a signal while waiting, we do not restore the
  1190. * signal mask yet, and we allow do_signal() to deliver the signal on
  1191. * the way back to userspace, before the signal mask is restored.
  1192. */
  1193. if (sigmask) {
  1194. if (error == -EINTR) {
  1195. memcpy(&current->saved_sigmask, &sigsaved,
  1196. sizeof(sigsaved));
  1197. set_restore_sigmask();
  1198. } else
  1199. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1200. }
  1201. return error;
  1202. }
  1203. #endif /* HAVE_SET_RESTORE_SIGMASK */
  1204. static int __init eventpoll_init(void)
  1205. {
  1206. struct sysinfo si;
  1207. si_meminfo(&si);
  1208. /*
  1209. * Allows top 4% of lomem to be allocated for epoll watches (per user).
  1210. */
  1211. max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) /
  1212. EP_ITEM_COST;
  1213. /* Initialize the structure used to perform safe poll wait head wake ups */
  1214. ep_nested_calls_init(&poll_safewake_ncalls);
  1215. /* Initialize the structure used to perform file's f_op->poll() calls */
  1216. ep_nested_calls_init(&poll_readywalk_ncalls);
  1217. /* Allocates slab cache used to allocate "struct epitem" items */
  1218. epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
  1219. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  1220. /* Allocates slab cache used to allocate "struct eppoll_entry" */
  1221. pwq_cache = kmem_cache_create("eventpoll_pwq",
  1222. sizeof(struct eppoll_entry), 0, SLAB_PANIC, NULL);
  1223. return 0;
  1224. }
  1225. fs_initcall(eventpoll_init);