eventpoll.c 37 KB

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