eventpoll.c 38 KB

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