eventpoll.c 36 KB

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