eventpoll.c 39 KB

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