eventpoll.c 39 KB

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