eventpoll.c 40 KB

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