eventpoll.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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 <linux/device.h>
  36. #include <linux/freezer.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/io.h>
  39. #include <asm/mman.h>
  40. #include <linux/atomic.h>
  41. /*
  42. * LOCKING:
  43. * There are three level of locking required by epoll :
  44. *
  45. * 1) epmutex (mutex)
  46. * 2) ep->mtx (mutex)
  47. * 3) ep->lock (spinlock)
  48. *
  49. * The acquire order is the one listed above, from 1 to 3.
  50. * We need a spinlock (ep->lock) because we manipulate objects
  51. * from inside the poll callback, that might be triggered from
  52. * a wake_up() that in turn might be called from IRQ context.
  53. * So we can't sleep inside the poll callback and hence we need
  54. * a spinlock. During the event transfer loop (from kernel to
  55. * user space) we could end up sleeping due a copy_to_user(), so
  56. * we need a lock that will allow us to sleep. This lock is a
  57. * mutex (ep->mtx). It is acquired during the event transfer loop,
  58. * during epoll_ctl(EPOLL_CTL_DEL) and during eventpoll_release_file().
  59. * Then we also need a global mutex to serialize eventpoll_release_file()
  60. * and ep_free().
  61. * This mutex is acquired by ep_free() during the epoll file
  62. * cleanup path and it is also acquired by eventpoll_release_file()
  63. * if a file has been pushed inside an epoll set and it is then
  64. * close()d without a previous call to epoll_ctl(EPOLL_CTL_DEL).
  65. * It is also acquired when inserting an epoll fd onto another epoll
  66. * fd. We do this so that we walk the epoll tree and ensure that this
  67. * insertion does not create a cycle of epoll file descriptors, which
  68. * could lead to deadlock. We need a global mutex to prevent two
  69. * simultaneous inserts (A into B and B into A) from racing and
  70. * constructing a cycle without either insert observing that it is
  71. * going to.
  72. * It is necessary to acquire multiple "ep->mtx"es at once in the
  73. * case when one epoll fd is added to another. In this case, we
  74. * always acquire the locks in the order of nesting (i.e. after
  75. * epoll_ctl(e1, EPOLL_CTL_ADD, e2), e1->mtx will always be acquired
  76. * before e2->mtx). Since we disallow cycles of epoll file
  77. * descriptors, this ensures that the mutexes are well-ordered. In
  78. * order to communicate this nesting to lockdep, when walking a tree
  79. * of epoll file descriptors, we use the current recursion depth as
  80. * the lockdep subkey.
  81. * It is possible to drop the "ep->mtx" and to use the global
  82. * mutex "epmutex" (together with "ep->lock") to have it working,
  83. * but having "ep->mtx" will make the interface more scalable.
  84. * Events that require holding "epmutex" are very rare, while for
  85. * normal operations the epoll private "ep->mtx" will guarantee
  86. * a better scalability.
  87. */
  88. /* Epoll private bits inside the event mask */
  89. #define EP_PRIVATE_BITS (EPOLLWAKEUP | EPOLLONESHOT | EPOLLET)
  90. /* Maximum number of nesting allowed inside epoll sets */
  91. #define EP_MAX_NESTS 4
  92. #define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
  93. #define EP_UNACTIVE_PTR ((void *) -1L)
  94. #define EP_ITEM_COST (sizeof(struct epitem) + sizeof(struct eppoll_entry))
  95. struct epoll_filefd {
  96. struct file *file;
  97. int fd;
  98. };
  99. /*
  100. * Structure used to track possible nested calls, for too deep recursions
  101. * and loop cycles.
  102. */
  103. struct nested_call_node {
  104. struct list_head llink;
  105. void *cookie;
  106. void *ctx;
  107. };
  108. /*
  109. * This structure is used as collector for nested calls, to check for
  110. * maximum recursion dept and loop cycles.
  111. */
  112. struct nested_calls {
  113. struct list_head tasks_call_list;
  114. spinlock_t lock;
  115. };
  116. /*
  117. * Each file descriptor added to the eventpoll interface will
  118. * have an entry of this type linked to the "rbr" RB tree.
  119. */
  120. struct epitem {
  121. /* RB tree node used to link this structure to the eventpoll RB tree */
  122. struct rb_node rbn;
  123. /* List header used to link this structure to the eventpoll ready list */
  124. struct list_head rdllink;
  125. /*
  126. * Works together "struct eventpoll"->ovflist in keeping the
  127. * single linked chain of items.
  128. */
  129. struct epitem *next;
  130. /* The file descriptor information this item refers to */
  131. struct epoll_filefd ffd;
  132. /* Number of active wait queue attached to poll operations */
  133. int nwait;
  134. /* List containing poll wait queues */
  135. struct list_head pwqlist;
  136. /* The "container" of this item */
  137. struct eventpoll *ep;
  138. /* List header used to link this item to the "struct file" items list */
  139. struct list_head fllink;
  140. /* wakeup_source used when EPOLLWAKEUP is set */
  141. struct wakeup_source *ws;
  142. /* The structure that describe the interested events and the source fd */
  143. struct epoll_event event;
  144. };
  145. /*
  146. * This structure is stored inside the "private_data" member of the file
  147. * structure and represents the main data structure for the eventpoll
  148. * interface.
  149. */
  150. struct eventpoll {
  151. /* Protect the access to this structure */
  152. spinlock_t lock;
  153. /*
  154. * This mutex is used to ensure that files are not removed
  155. * while epoll is using them. This is held during the event
  156. * collection loop, the file cleanup path, the epoll file exit
  157. * code and the ctl operations.
  158. */
  159. struct mutex mtx;
  160. /* Wait queue used by sys_epoll_wait() */
  161. wait_queue_head_t wq;
  162. /* Wait queue used by file->poll() */
  163. wait_queue_head_t poll_wait;
  164. /* List of ready file descriptors */
  165. struct list_head rdllist;
  166. /* RB tree root used to store monitored fd structs */
  167. struct rb_root rbr;
  168. /*
  169. * This is a single linked list that chains all the "struct epitem" that
  170. * happened while transferring ready events to userspace w/out
  171. * holding ->lock.
  172. */
  173. struct epitem *ovflist;
  174. /* wakeup_source used when ep_scan_ready_list is running */
  175. struct wakeup_source *ws;
  176. /* The user that created the eventpoll descriptor */
  177. struct user_struct *user;
  178. struct file *file;
  179. /* used to optimize loop detection check */
  180. int visited;
  181. struct list_head visited_list_link;
  182. };
  183. /* Wait structure used by the poll hooks */
  184. struct eppoll_entry {
  185. /* List header used to link this structure to the "struct epitem" */
  186. struct list_head llink;
  187. /* The "base" pointer is set to the container "struct epitem" */
  188. struct epitem *base;
  189. /*
  190. * Wait queue item that will be linked to the target file wait
  191. * queue head.
  192. */
  193. wait_queue_t wait;
  194. /* The wait queue head that linked the "wait" wait queue item */
  195. wait_queue_head_t *whead;
  196. };
  197. /* Wrapper struct used by poll queueing */
  198. struct ep_pqueue {
  199. poll_table pt;
  200. struct epitem *epi;
  201. };
  202. /* Used by the ep_send_events() function as callback private data */
  203. struct ep_send_events_data {
  204. int maxevents;
  205. struct epoll_event __user *events;
  206. };
  207. /*
  208. * Configuration options available inside /proc/sys/fs/epoll/
  209. */
  210. /* Maximum number of epoll watched descriptors, per user */
  211. static long max_user_watches __read_mostly;
  212. /*
  213. * This mutex is used to serialize ep_free() and eventpoll_release_file().
  214. */
  215. static DEFINE_MUTEX(epmutex);
  216. /* Used to check for epoll file descriptor inclusion loops */
  217. static struct nested_calls poll_loop_ncalls;
  218. /* Used for safe wake up implementation */
  219. static struct nested_calls poll_safewake_ncalls;
  220. /* Used to call file's f_op->poll() under the nested calls boundaries */
  221. static struct nested_calls poll_readywalk_ncalls;
  222. /* Slab cache used to allocate "struct epitem" */
  223. static struct kmem_cache *epi_cache __read_mostly;
  224. /* Slab cache used to allocate "struct eppoll_entry" */
  225. static struct kmem_cache *pwq_cache __read_mostly;
  226. /* Visited nodes during ep_loop_check(), so we can unset them when we finish */
  227. static LIST_HEAD(visited_list);
  228. /*
  229. * List of files with newly added links, where we may need to limit the number
  230. * of emanating paths. Protected by the epmutex.
  231. */
  232. static LIST_HEAD(tfile_check_list);
  233. #ifdef CONFIG_SYSCTL
  234. #include <linux/sysctl.h>
  235. static long zero;
  236. static long long_max = LONG_MAX;
  237. ctl_table epoll_table[] = {
  238. {
  239. .procname = "max_user_watches",
  240. .data = &max_user_watches,
  241. .maxlen = sizeof(max_user_watches),
  242. .mode = 0644,
  243. .proc_handler = proc_doulongvec_minmax,
  244. .extra1 = &zero,
  245. .extra2 = &long_max,
  246. },
  247. { }
  248. };
  249. #endif /* CONFIG_SYSCTL */
  250. static const struct file_operations eventpoll_fops;
  251. static inline int is_file_epoll(struct file *f)
  252. {
  253. return f->f_op == &eventpoll_fops;
  254. }
  255. /* Setup the structure that is used as key for the RB tree */
  256. static inline void ep_set_ffd(struct epoll_filefd *ffd,
  257. struct file *file, int fd)
  258. {
  259. ffd->file = file;
  260. ffd->fd = fd;
  261. }
  262. /* Compare RB tree keys */
  263. static inline int ep_cmp_ffd(struct epoll_filefd *p1,
  264. struct epoll_filefd *p2)
  265. {
  266. return (p1->file > p2->file ? +1:
  267. (p1->file < p2->file ? -1 : p1->fd - p2->fd));
  268. }
  269. /* Tells us if the item is currently linked */
  270. static inline int ep_is_linked(struct list_head *p)
  271. {
  272. return !list_empty(p);
  273. }
  274. static inline struct eppoll_entry *ep_pwq_from_wait(wait_queue_t *p)
  275. {
  276. return container_of(p, struct eppoll_entry, wait);
  277. }
  278. /* Get the "struct epitem" from a wait queue pointer */
  279. static inline struct epitem *ep_item_from_wait(wait_queue_t *p)
  280. {
  281. return container_of(p, struct eppoll_entry, wait)->base;
  282. }
  283. /* Get the "struct epitem" from an epoll queue wrapper */
  284. static inline struct epitem *ep_item_from_epqueue(poll_table *p)
  285. {
  286. return container_of(p, struct ep_pqueue, pt)->epi;
  287. }
  288. /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
  289. static inline int ep_op_has_event(int op)
  290. {
  291. return op != EPOLL_CTL_DEL;
  292. }
  293. /* Initialize the poll safe wake up structure */
  294. static void ep_nested_calls_init(struct nested_calls *ncalls)
  295. {
  296. INIT_LIST_HEAD(&ncalls->tasks_call_list);
  297. spin_lock_init(&ncalls->lock);
  298. }
  299. /**
  300. * ep_events_available - Checks if ready events might be available.
  301. *
  302. * @ep: Pointer to the eventpoll context.
  303. *
  304. * Returns: Returns a value different than zero if ready events are available,
  305. * or zero otherwise.
  306. */
  307. static inline int ep_events_available(struct eventpoll *ep)
  308. {
  309. return !list_empty(&ep->rdllist) || ep->ovflist != EP_UNACTIVE_PTR;
  310. }
  311. /**
  312. * ep_call_nested - Perform a bound (possibly) nested call, by checking
  313. * that the recursion limit is not exceeded, and that
  314. * the same nested call (by the meaning of same cookie) is
  315. * no re-entered.
  316. *
  317. * @ncalls: Pointer to the nested_calls structure to be used for this call.
  318. * @max_nests: Maximum number of allowed nesting calls.
  319. * @nproc: Nested call core function pointer.
  320. * @priv: Opaque data to be passed to the @nproc callback.
  321. * @cookie: Cookie to be used to identify this nested call.
  322. * @ctx: This instance context.
  323. *
  324. * Returns: Returns the code returned by the @nproc callback, or -1 if
  325. * the maximum recursion limit has been exceeded.
  326. */
  327. static int ep_call_nested(struct nested_calls *ncalls, int max_nests,
  328. int (*nproc)(void *, void *, int), void *priv,
  329. void *cookie, void *ctx)
  330. {
  331. int error, call_nests = 0;
  332. unsigned long flags;
  333. struct list_head *lsthead = &ncalls->tasks_call_list;
  334. struct nested_call_node *tncur;
  335. struct nested_call_node tnode;
  336. spin_lock_irqsave(&ncalls->lock, flags);
  337. /*
  338. * Try to see if the current task is already inside this wakeup call.
  339. * We use a list here, since the population inside this set is always
  340. * very much limited.
  341. */
  342. list_for_each_entry(tncur, lsthead, llink) {
  343. if (tncur->ctx == ctx &&
  344. (tncur->cookie == cookie || ++call_nests > max_nests)) {
  345. /*
  346. * Ops ... loop detected or maximum nest level reached.
  347. * We abort this wake by breaking the cycle itself.
  348. */
  349. error = -1;
  350. goto out_unlock;
  351. }
  352. }
  353. /* Add the current task and cookie to the list */
  354. tnode.ctx = ctx;
  355. tnode.cookie = cookie;
  356. list_add(&tnode.llink, lsthead);
  357. spin_unlock_irqrestore(&ncalls->lock, flags);
  358. /* Call the nested function */
  359. error = (*nproc)(priv, cookie, call_nests);
  360. /* Remove the current task from the list */
  361. spin_lock_irqsave(&ncalls->lock, flags);
  362. list_del(&tnode.llink);
  363. out_unlock:
  364. spin_unlock_irqrestore(&ncalls->lock, flags);
  365. return error;
  366. }
  367. /*
  368. * As described in commit 0ccf831cb lockdep: annotate epoll
  369. * the use of wait queues used by epoll is done in a very controlled
  370. * manner. Wake ups can nest inside each other, but are never done
  371. * with the same locking. For example:
  372. *
  373. * dfd = socket(...);
  374. * efd1 = epoll_create();
  375. * efd2 = epoll_create();
  376. * epoll_ctl(efd1, EPOLL_CTL_ADD, dfd, ...);
  377. * epoll_ctl(efd2, EPOLL_CTL_ADD, efd1, ...);
  378. *
  379. * When a packet arrives to the device underneath "dfd", the net code will
  380. * issue a wake_up() on its poll wake list. Epoll (efd1) has installed a
  381. * callback wakeup entry on that queue, and the wake_up() performed by the
  382. * "dfd" net code will end up in ep_poll_callback(). At this point epoll
  383. * (efd1) notices that it may have some event ready, so it needs to wake up
  384. * the waiters on its poll wait list (efd2). So it calls ep_poll_safewake()
  385. * that ends up in another wake_up(), after having checked about the
  386. * recursion constraints. That are, no more than EP_MAX_POLLWAKE_NESTS, to
  387. * avoid stack blasting.
  388. *
  389. * When CONFIG_DEBUG_LOCK_ALLOC is enabled, make sure lockdep can handle
  390. * this special case of epoll.
  391. */
  392. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  393. static inline void ep_wake_up_nested(wait_queue_head_t *wqueue,
  394. unsigned long events, int subclass)
  395. {
  396. unsigned long flags;
  397. spin_lock_irqsave_nested(&wqueue->lock, flags, subclass);
  398. wake_up_locked_poll(wqueue, events);
  399. spin_unlock_irqrestore(&wqueue->lock, flags);
  400. }
  401. #else
  402. static inline void ep_wake_up_nested(wait_queue_head_t *wqueue,
  403. unsigned long events, int subclass)
  404. {
  405. wake_up_poll(wqueue, events);
  406. }
  407. #endif
  408. static int ep_poll_wakeup_proc(void *priv, void *cookie, int call_nests)
  409. {
  410. ep_wake_up_nested((wait_queue_head_t *) cookie, POLLIN,
  411. 1 + call_nests);
  412. return 0;
  413. }
  414. /*
  415. * Perform a safe wake up of the poll wait list. The problem is that
  416. * with the new callback'd wake up system, it is possible that the
  417. * poll callback is reentered from inside the call to wake_up() done
  418. * on the poll wait queue head. The rule is that we cannot reenter the
  419. * wake up code from the same task more than EP_MAX_NESTS times,
  420. * and we cannot reenter the same wait queue head at all. This will
  421. * enable to have a hierarchy of epoll file descriptor of no more than
  422. * EP_MAX_NESTS deep.
  423. */
  424. static void ep_poll_safewake(wait_queue_head_t *wq)
  425. {
  426. int this_cpu = get_cpu();
  427. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  428. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  429. put_cpu();
  430. }
  431. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  432. {
  433. wait_queue_head_t *whead;
  434. rcu_read_lock();
  435. /* If it is cleared by POLLFREE, it should be rcu-safe */
  436. whead = rcu_dereference(pwq->whead);
  437. if (whead)
  438. remove_wait_queue(whead, &pwq->wait);
  439. rcu_read_unlock();
  440. }
  441. /*
  442. * This function unregisters poll callbacks from the associated file
  443. * descriptor. Must be called with "mtx" held (or "epmutex" if called from
  444. * ep_free).
  445. */
  446. static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
  447. {
  448. struct list_head *lsthead = &epi->pwqlist;
  449. struct eppoll_entry *pwq;
  450. while (!list_empty(lsthead)) {
  451. pwq = list_first_entry(lsthead, struct eppoll_entry, llink);
  452. list_del(&pwq->llink);
  453. ep_remove_wait_queue(pwq);
  454. kmem_cache_free(pwq_cache, pwq);
  455. }
  456. }
  457. /**
  458. * ep_scan_ready_list - Scans the ready list in a way that makes possible for
  459. * the scan code, to call f_op->poll(). Also allows for
  460. * O(NumReady) performance.
  461. *
  462. * @ep: Pointer to the epoll private data structure.
  463. * @sproc: Pointer to the scan callback.
  464. * @priv: Private opaque data passed to the @sproc callback.
  465. * @depth: The current depth of recursive f_op->poll calls.
  466. *
  467. * Returns: The same integer error code returned by the @sproc callback.
  468. */
  469. static int ep_scan_ready_list(struct eventpoll *ep,
  470. int (*sproc)(struct eventpoll *,
  471. struct list_head *, void *),
  472. void *priv,
  473. int depth)
  474. {
  475. int error, pwake = 0;
  476. unsigned long flags;
  477. struct epitem *epi, *nepi;
  478. LIST_HEAD(txlist);
  479. /*
  480. * We need to lock this because we could be hit by
  481. * eventpoll_release_file() and epoll_ctl().
  482. */
  483. mutex_lock_nested(&ep->mtx, depth);
  484. /*
  485. * Steal the ready list, and re-init the original one to the
  486. * empty list. Also, set ep->ovflist to NULL so that events
  487. * happening while looping w/out locks, are not lost. We cannot
  488. * have the poll callback to queue directly on ep->rdllist,
  489. * because we want the "sproc" callback to be able to do it
  490. * in a lockless way.
  491. */
  492. spin_lock_irqsave(&ep->lock, flags);
  493. list_splice_init(&ep->rdllist, &txlist);
  494. ep->ovflist = NULL;
  495. spin_unlock_irqrestore(&ep->lock, flags);
  496. /*
  497. * Now call the callback function.
  498. */
  499. error = (*sproc)(ep, &txlist, priv);
  500. spin_lock_irqsave(&ep->lock, flags);
  501. /*
  502. * During the time we spent inside the "sproc" callback, some
  503. * other events might have been queued by the poll callback.
  504. * We re-insert them inside the main ready-list here.
  505. */
  506. for (nepi = ep->ovflist; (epi = nepi) != NULL;
  507. nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
  508. /*
  509. * We need to check if the item is already in the list.
  510. * During the "sproc" callback execution time, items are
  511. * queued into ->ovflist but the "txlist" might already
  512. * contain them, and the list_splice() below takes care of them.
  513. */
  514. if (!ep_is_linked(&epi->rdllink)) {
  515. list_add_tail(&epi->rdllink, &ep->rdllist);
  516. __pm_stay_awake(epi->ws);
  517. }
  518. }
  519. /*
  520. * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
  521. * releasing the lock, events will be queued in the normal way inside
  522. * ep->rdllist.
  523. */
  524. ep->ovflist = EP_UNACTIVE_PTR;
  525. /*
  526. * Quickly re-inject items left on "txlist".
  527. */
  528. list_splice(&txlist, &ep->rdllist);
  529. __pm_relax(ep->ws);
  530. if (!list_empty(&ep->rdllist)) {
  531. /*
  532. * Wake up (if active) both the eventpoll wait list and
  533. * the ->poll() wait list (delayed after we release the lock).
  534. */
  535. if (waitqueue_active(&ep->wq))
  536. wake_up_locked(&ep->wq);
  537. if (waitqueue_active(&ep->poll_wait))
  538. pwake++;
  539. }
  540. spin_unlock_irqrestore(&ep->lock, flags);
  541. mutex_unlock(&ep->mtx);
  542. /* We have to call this outside the lock */
  543. if (pwake)
  544. ep_poll_safewake(&ep->poll_wait);
  545. return error;
  546. }
  547. /*
  548. * Removes a "struct epitem" from the eventpoll RB tree and deallocates
  549. * all the associated resources. Must be called with "mtx" held.
  550. */
  551. static int ep_remove(struct eventpoll *ep, struct epitem *epi)
  552. {
  553. unsigned long flags;
  554. struct file *file = epi->ffd.file;
  555. /*
  556. * Removes poll wait queue hooks. We _have_ to do this without holding
  557. * the "ep->lock" otherwise a deadlock might occur. This because of the
  558. * sequence of the lock acquisition. Here we do "ep->lock" then the wait
  559. * queue head lock when unregistering the wait queue. The wakeup callback
  560. * will run by holding the wait queue head lock and will call our callback
  561. * that will try to get "ep->lock".
  562. */
  563. ep_unregister_pollwait(ep, epi);
  564. /* Remove the current item from the list of epoll hooks */
  565. spin_lock(&file->f_lock);
  566. if (ep_is_linked(&epi->fllink))
  567. list_del_init(&epi->fllink);
  568. spin_unlock(&file->f_lock);
  569. rb_erase(&epi->rbn, &ep->rbr);
  570. spin_lock_irqsave(&ep->lock, flags);
  571. if (ep_is_linked(&epi->rdllink))
  572. list_del_init(&epi->rdllink);
  573. spin_unlock_irqrestore(&ep->lock, flags);
  574. wakeup_source_unregister(epi->ws);
  575. /* At this point it is safe to free the eventpoll item */
  576. kmem_cache_free(epi_cache, epi);
  577. atomic_long_dec(&ep->user->epoll_watches);
  578. return 0;
  579. }
  580. static void ep_free(struct eventpoll *ep)
  581. {
  582. struct rb_node *rbp;
  583. struct epitem *epi;
  584. /* We need to release all tasks waiting for these file */
  585. if (waitqueue_active(&ep->poll_wait))
  586. ep_poll_safewake(&ep->poll_wait);
  587. /*
  588. * We need to lock this because we could be hit by
  589. * eventpoll_release_file() while we're freeing the "struct eventpoll".
  590. * We do not need to hold "ep->mtx" here because the epoll file
  591. * is on the way to be removed and no one has references to it
  592. * anymore. The only hit might come from eventpoll_release_file() but
  593. * holding "epmutex" is sufficient here.
  594. */
  595. mutex_lock(&epmutex);
  596. /*
  597. * Walks through the whole tree by unregistering poll callbacks.
  598. */
  599. for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
  600. epi = rb_entry(rbp, struct epitem, rbn);
  601. ep_unregister_pollwait(ep, epi);
  602. }
  603. /*
  604. * Walks through the whole tree by freeing each "struct epitem". At this
  605. * point we are sure no poll callbacks will be lingering around, and also by
  606. * holding "epmutex" we can be sure that no file cleanup code will hit
  607. * us during this operation. So we can avoid the lock on "ep->lock".
  608. */
  609. while ((rbp = rb_first(&ep->rbr)) != NULL) {
  610. epi = rb_entry(rbp, struct epitem, rbn);
  611. ep_remove(ep, epi);
  612. }
  613. mutex_unlock(&epmutex);
  614. mutex_destroy(&ep->mtx);
  615. free_uid(ep->user);
  616. wakeup_source_unregister(ep->ws);
  617. kfree(ep);
  618. }
  619. static int ep_eventpoll_release(struct inode *inode, struct file *file)
  620. {
  621. struct eventpoll *ep = file->private_data;
  622. if (ep)
  623. ep_free(ep);
  624. return 0;
  625. }
  626. static int ep_read_events_proc(struct eventpoll *ep, struct list_head *head,
  627. void *priv)
  628. {
  629. struct epitem *epi, *tmp;
  630. poll_table pt;
  631. init_poll_funcptr(&pt, NULL);
  632. list_for_each_entry_safe(epi, tmp, head, rdllink) {
  633. pt._key = epi->event.events;
  634. if (epi->ffd.file->f_op->poll(epi->ffd.file, &pt) &
  635. epi->event.events)
  636. return POLLIN | POLLRDNORM;
  637. else {
  638. /*
  639. * Item has been dropped into the ready list by the poll
  640. * callback, but it's not actually ready, as far as
  641. * caller requested events goes. We can remove it here.
  642. */
  643. __pm_relax(epi->ws);
  644. list_del_init(&epi->rdllink);
  645. }
  646. }
  647. return 0;
  648. }
  649. static int ep_poll_readyevents_proc(void *priv, void *cookie, int call_nests)
  650. {
  651. return ep_scan_ready_list(priv, ep_read_events_proc, NULL, call_nests + 1);
  652. }
  653. static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
  654. {
  655. int pollflags;
  656. struct eventpoll *ep = file->private_data;
  657. /* Insert inside our poll wait queue */
  658. poll_wait(file, &ep->poll_wait, wait);
  659. /*
  660. * Proceed to find out if wanted events are really available inside
  661. * the ready list. This need to be done under ep_call_nested()
  662. * supervision, since the call to f_op->poll() done on listed files
  663. * could re-enter here.
  664. */
  665. pollflags = ep_call_nested(&poll_readywalk_ncalls, EP_MAX_NESTS,
  666. ep_poll_readyevents_proc, ep, ep, current);
  667. return pollflags != -1 ? pollflags : 0;
  668. }
  669. /* File callbacks that implement the eventpoll file behaviour */
  670. static const struct file_operations eventpoll_fops = {
  671. .release = ep_eventpoll_release,
  672. .poll = ep_eventpoll_poll,
  673. .llseek = noop_llseek,
  674. };
  675. /*
  676. * This is called from eventpoll_release() to unlink files from the eventpoll
  677. * interface. We need to have this facility to cleanup correctly files that are
  678. * closed without being removed from the eventpoll interface.
  679. */
  680. void eventpoll_release_file(struct file *file)
  681. {
  682. struct list_head *lsthead = &file->f_ep_links;
  683. struct eventpoll *ep;
  684. struct epitem *epi;
  685. /*
  686. * We don't want to get "file->f_lock" because it is not
  687. * necessary. It is not necessary because we're in the "struct file"
  688. * cleanup path, and this means that no one is using this file anymore.
  689. * So, for example, epoll_ctl() cannot hit here since if we reach this
  690. * point, the file counter already went to zero and fget() would fail.
  691. * The only hit might come from ep_free() but by holding the mutex
  692. * will correctly serialize the operation. We do need to acquire
  693. * "ep->mtx" after "epmutex" because ep_remove() requires it when called
  694. * from anywhere but ep_free().
  695. *
  696. * Besides, ep_remove() acquires the lock, so we can't hold it here.
  697. */
  698. mutex_lock(&epmutex);
  699. while (!list_empty(lsthead)) {
  700. epi = list_first_entry(lsthead, struct epitem, fllink);
  701. ep = epi->ep;
  702. list_del_init(&epi->fllink);
  703. mutex_lock_nested(&ep->mtx, 0);
  704. ep_remove(ep, epi);
  705. mutex_unlock(&ep->mtx);
  706. }
  707. mutex_unlock(&epmutex);
  708. }
  709. static int ep_alloc(struct eventpoll **pep)
  710. {
  711. int error;
  712. struct user_struct *user;
  713. struct eventpoll *ep;
  714. user = get_current_user();
  715. error = -ENOMEM;
  716. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  717. if (unlikely(!ep))
  718. goto free_uid;
  719. spin_lock_init(&ep->lock);
  720. mutex_init(&ep->mtx);
  721. init_waitqueue_head(&ep->wq);
  722. init_waitqueue_head(&ep->poll_wait);
  723. INIT_LIST_HEAD(&ep->rdllist);
  724. ep->rbr = RB_ROOT;
  725. ep->ovflist = EP_UNACTIVE_PTR;
  726. ep->user = user;
  727. *pep = ep;
  728. return 0;
  729. free_uid:
  730. free_uid(user);
  731. return error;
  732. }
  733. /*
  734. * Search the file inside the eventpoll tree. The RB tree operations
  735. * are protected by the "mtx" mutex, and ep_find() must be called with
  736. * "mtx" held.
  737. */
  738. static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
  739. {
  740. int kcmp;
  741. struct rb_node *rbp;
  742. struct epitem *epi, *epir = NULL;
  743. struct epoll_filefd ffd;
  744. ep_set_ffd(&ffd, file, fd);
  745. for (rbp = ep->rbr.rb_node; rbp; ) {
  746. epi = rb_entry(rbp, struct epitem, rbn);
  747. kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
  748. if (kcmp > 0)
  749. rbp = rbp->rb_right;
  750. else if (kcmp < 0)
  751. rbp = rbp->rb_left;
  752. else {
  753. epir = epi;
  754. break;
  755. }
  756. }
  757. return epir;
  758. }
  759. /*
  760. * This is the callback that is passed to the wait queue wakeup
  761. * mechanism. It is called by the stored file descriptors when they
  762. * have events to report.
  763. */
  764. static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
  765. {
  766. int pwake = 0;
  767. unsigned long flags;
  768. struct epitem *epi = ep_item_from_wait(wait);
  769. struct eventpoll *ep = epi->ep;
  770. if ((unsigned long)key & POLLFREE) {
  771. ep_pwq_from_wait(wait)->whead = NULL;
  772. /*
  773. * whead = NULL above can race with ep_remove_wait_queue()
  774. * which can do another remove_wait_queue() after us, so we
  775. * can't use __remove_wait_queue(). whead->lock is held by
  776. * the caller.
  777. */
  778. list_del_init(&wait->task_list);
  779. }
  780. spin_lock_irqsave(&ep->lock, flags);
  781. /*
  782. * If the event mask does not contain any poll(2) event, we consider the
  783. * descriptor to be disabled. This condition is likely the effect of the
  784. * EPOLLONESHOT bit that disables the descriptor when an event is received,
  785. * until the next EPOLL_CTL_MOD will be issued.
  786. */
  787. if (!(epi->event.events & ~EP_PRIVATE_BITS))
  788. goto out_unlock;
  789. /*
  790. * Check the events coming with the callback. At this stage, not
  791. * every device reports the events in the "key" parameter of the
  792. * callback. We need to be able to handle both cases here, hence the
  793. * test for "key" != NULL before the event match test.
  794. */
  795. if (key && !((unsigned long) key & epi->event.events))
  796. goto out_unlock;
  797. /*
  798. * If we are transferring events to userspace, we can hold no locks
  799. * (because we're accessing user memory, and because of linux f_op->poll()
  800. * semantics). All the events that happen during that period of time are
  801. * chained in ep->ovflist and requeued later on.
  802. */
  803. if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) {
  804. if (epi->next == EP_UNACTIVE_PTR) {
  805. epi->next = ep->ovflist;
  806. ep->ovflist = epi;
  807. if (epi->ws) {
  808. /*
  809. * Activate ep->ws since epi->ws may get
  810. * deactivated at any time.
  811. */
  812. __pm_stay_awake(ep->ws);
  813. }
  814. }
  815. goto out_unlock;
  816. }
  817. /* If this file is already in the ready list we exit soon */
  818. if (!ep_is_linked(&epi->rdllink)) {
  819. list_add_tail(&epi->rdllink, &ep->rdllist);
  820. __pm_stay_awake(epi->ws);
  821. }
  822. /*
  823. * Wake up ( if active ) both the eventpoll wait list and the ->poll()
  824. * wait list.
  825. */
  826. if (waitqueue_active(&ep->wq))
  827. wake_up_locked(&ep->wq);
  828. if (waitqueue_active(&ep->poll_wait))
  829. pwake++;
  830. out_unlock:
  831. spin_unlock_irqrestore(&ep->lock, flags);
  832. /* We have to call this outside the lock */
  833. if (pwake)
  834. ep_poll_safewake(&ep->poll_wait);
  835. return 1;
  836. }
  837. /*
  838. * This is the callback that is used to add our wait queue to the
  839. * target file wakeup lists.
  840. */
  841. static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
  842. poll_table *pt)
  843. {
  844. struct epitem *epi = ep_item_from_epqueue(pt);
  845. struct eppoll_entry *pwq;
  846. if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
  847. init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
  848. pwq->whead = whead;
  849. pwq->base = epi;
  850. add_wait_queue(whead, &pwq->wait);
  851. list_add_tail(&pwq->llink, &epi->pwqlist);
  852. epi->nwait++;
  853. } else {
  854. /* We have to signal that an error occurred */
  855. epi->nwait = -1;
  856. }
  857. }
  858. static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
  859. {
  860. int kcmp;
  861. struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
  862. struct epitem *epic;
  863. while (*p) {
  864. parent = *p;
  865. epic = rb_entry(parent, struct epitem, rbn);
  866. kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
  867. if (kcmp > 0)
  868. p = &parent->rb_right;
  869. else
  870. p = &parent->rb_left;
  871. }
  872. rb_link_node(&epi->rbn, parent, p);
  873. rb_insert_color(&epi->rbn, &ep->rbr);
  874. }
  875. #define PATH_ARR_SIZE 5
  876. /*
  877. * These are the number paths of length 1 to 5, that we are allowing to emanate
  878. * from a single file of interest. For example, we allow 1000 paths of length
  879. * 1, to emanate from each file of interest. This essentially represents the
  880. * potential wakeup paths, which need to be limited in order to avoid massive
  881. * uncontrolled wakeup storms. The common use case should be a single ep which
  882. * is connected to n file sources. In this case each file source has 1 path
  883. * of length 1. Thus, the numbers below should be more than sufficient. These
  884. * path limits are enforced during an EPOLL_CTL_ADD operation, since a modify
  885. * and delete can't add additional paths. Protected by the epmutex.
  886. */
  887. static const int path_limits[PATH_ARR_SIZE] = { 1000, 500, 100, 50, 10 };
  888. static int path_count[PATH_ARR_SIZE];
  889. static int path_count_inc(int nests)
  890. {
  891. /* Allow an arbitrary number of depth 1 paths */
  892. if (nests == 0)
  893. return 0;
  894. if (++path_count[nests] > path_limits[nests])
  895. return -1;
  896. return 0;
  897. }
  898. static void path_count_init(void)
  899. {
  900. int i;
  901. for (i = 0; i < PATH_ARR_SIZE; i++)
  902. path_count[i] = 0;
  903. }
  904. static int reverse_path_check_proc(void *priv, void *cookie, int call_nests)
  905. {
  906. int error = 0;
  907. struct file *file = priv;
  908. struct file *child_file;
  909. struct epitem *epi;
  910. list_for_each_entry(epi, &file->f_ep_links, fllink) {
  911. child_file = epi->ep->file;
  912. if (is_file_epoll(child_file)) {
  913. if (list_empty(&child_file->f_ep_links)) {
  914. if (path_count_inc(call_nests)) {
  915. error = -1;
  916. break;
  917. }
  918. } else {
  919. error = ep_call_nested(&poll_loop_ncalls,
  920. EP_MAX_NESTS,
  921. reverse_path_check_proc,
  922. child_file, child_file,
  923. current);
  924. }
  925. if (error != 0)
  926. break;
  927. } else {
  928. printk(KERN_ERR "reverse_path_check_proc: "
  929. "file is not an ep!\n");
  930. }
  931. }
  932. return error;
  933. }
  934. /**
  935. * reverse_path_check - The tfile_check_list is list of file *, which have
  936. * links that are proposed to be newly added. We need to
  937. * make sure that those added links don't add too many
  938. * paths such that we will spend all our time waking up
  939. * eventpoll objects.
  940. *
  941. * Returns: Returns zero if the proposed links don't create too many paths,
  942. * -1 otherwise.
  943. */
  944. static int reverse_path_check(void)
  945. {
  946. int error = 0;
  947. struct file *current_file;
  948. /* let's call this for all tfiles */
  949. list_for_each_entry(current_file, &tfile_check_list, f_tfile_llink) {
  950. path_count_init();
  951. error = ep_call_nested(&poll_loop_ncalls, EP_MAX_NESTS,
  952. reverse_path_check_proc, current_file,
  953. current_file, current);
  954. if (error)
  955. break;
  956. }
  957. return error;
  958. }
  959. static int ep_create_wakeup_source(struct epitem *epi)
  960. {
  961. const char *name;
  962. if (!epi->ep->ws) {
  963. epi->ep->ws = wakeup_source_register("eventpoll");
  964. if (!epi->ep->ws)
  965. return -ENOMEM;
  966. }
  967. name = epi->ffd.file->f_path.dentry->d_name.name;
  968. epi->ws = wakeup_source_register(name);
  969. if (!epi->ws)
  970. return -ENOMEM;
  971. return 0;
  972. }
  973. static void ep_destroy_wakeup_source(struct epitem *epi)
  974. {
  975. wakeup_source_unregister(epi->ws);
  976. epi->ws = NULL;
  977. }
  978. /*
  979. * Must be called with "mtx" held.
  980. */
  981. static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
  982. struct file *tfile, int fd)
  983. {
  984. int error, revents, pwake = 0;
  985. unsigned long flags;
  986. long user_watches;
  987. struct epitem *epi;
  988. struct ep_pqueue epq;
  989. user_watches = atomic_long_read(&ep->user->epoll_watches);
  990. if (unlikely(user_watches >= max_user_watches))
  991. return -ENOSPC;
  992. if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
  993. return -ENOMEM;
  994. /* Item initialization follow here ... */
  995. INIT_LIST_HEAD(&epi->rdllink);
  996. INIT_LIST_HEAD(&epi->fllink);
  997. INIT_LIST_HEAD(&epi->pwqlist);
  998. epi->ep = ep;
  999. ep_set_ffd(&epi->ffd, tfile, fd);
  1000. epi->event = *event;
  1001. epi->nwait = 0;
  1002. epi->next = EP_UNACTIVE_PTR;
  1003. if (epi->event.events & EPOLLWAKEUP) {
  1004. error = ep_create_wakeup_source(epi);
  1005. if (error)
  1006. goto error_create_wakeup_source;
  1007. } else {
  1008. epi->ws = NULL;
  1009. }
  1010. /* Initialize the poll table using the queue callback */
  1011. epq.epi = epi;
  1012. init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
  1013. epq.pt._key = event->events;
  1014. /*
  1015. * Attach the item to the poll hooks and get current event bits.
  1016. * We can safely use the file* here because its usage count has
  1017. * been increased by the caller of this function. Note that after
  1018. * this operation completes, the poll callback can start hitting
  1019. * the new item.
  1020. */
  1021. revents = tfile->f_op->poll(tfile, &epq.pt);
  1022. /*
  1023. * We have to check if something went wrong during the poll wait queue
  1024. * install process. Namely an allocation for a wait queue failed due
  1025. * high memory pressure.
  1026. */
  1027. error = -ENOMEM;
  1028. if (epi->nwait < 0)
  1029. goto error_unregister;
  1030. /* Add the current item to the list of active epoll hook for this file */
  1031. spin_lock(&tfile->f_lock);
  1032. list_add_tail(&epi->fllink, &tfile->f_ep_links);
  1033. spin_unlock(&tfile->f_lock);
  1034. /*
  1035. * Add the current item to the RB tree. All RB tree operations are
  1036. * protected by "mtx", and ep_insert() is called with "mtx" held.
  1037. */
  1038. ep_rbtree_insert(ep, epi);
  1039. /* now check if we've created too many backpaths */
  1040. error = -EINVAL;
  1041. if (reverse_path_check())
  1042. goto error_remove_epi;
  1043. /* We have to drop the new item inside our item list to keep track of it */
  1044. spin_lock_irqsave(&ep->lock, flags);
  1045. /* If the file is already "ready" we drop it inside the ready list */
  1046. if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
  1047. list_add_tail(&epi->rdllink, &ep->rdllist);
  1048. __pm_stay_awake(epi->ws);
  1049. /* Notify waiting tasks that events are available */
  1050. if (waitqueue_active(&ep->wq))
  1051. wake_up_locked(&ep->wq);
  1052. if (waitqueue_active(&ep->poll_wait))
  1053. pwake++;
  1054. }
  1055. spin_unlock_irqrestore(&ep->lock, flags);
  1056. atomic_long_inc(&ep->user->epoll_watches);
  1057. /* We have to call this outside the lock */
  1058. if (pwake)
  1059. ep_poll_safewake(&ep->poll_wait);
  1060. return 0;
  1061. error_remove_epi:
  1062. spin_lock(&tfile->f_lock);
  1063. if (ep_is_linked(&epi->fllink))
  1064. list_del_init(&epi->fllink);
  1065. spin_unlock(&tfile->f_lock);
  1066. rb_erase(&epi->rbn, &ep->rbr);
  1067. error_unregister:
  1068. ep_unregister_pollwait(ep, epi);
  1069. /*
  1070. * We need to do this because an event could have been arrived on some
  1071. * allocated wait queue. Note that we don't care about the ep->ovflist
  1072. * list, since that is used/cleaned only inside a section bound by "mtx".
  1073. * And ep_insert() is called with "mtx" held.
  1074. */
  1075. spin_lock_irqsave(&ep->lock, flags);
  1076. if (ep_is_linked(&epi->rdllink))
  1077. list_del_init(&epi->rdllink);
  1078. spin_unlock_irqrestore(&ep->lock, flags);
  1079. wakeup_source_unregister(epi->ws);
  1080. error_create_wakeup_source:
  1081. kmem_cache_free(epi_cache, epi);
  1082. return error;
  1083. }
  1084. /*
  1085. * Modify the interest event mask by dropping an event if the new mask
  1086. * has a match in the current file status. Must be called with "mtx" held.
  1087. */
  1088. static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
  1089. {
  1090. int pwake = 0;
  1091. unsigned int revents;
  1092. poll_table pt;
  1093. init_poll_funcptr(&pt, NULL);
  1094. /*
  1095. * Set the new event interest mask before calling f_op->poll();
  1096. * otherwise we might miss an event that happens between the
  1097. * f_op->poll() call and the new event set registering.
  1098. */
  1099. epi->event.events = event->events; /* need barrier below */
  1100. pt._key = event->events;
  1101. epi->event.data = event->data; /* protected by mtx */
  1102. if (epi->event.events & EPOLLWAKEUP) {
  1103. if (!epi->ws)
  1104. ep_create_wakeup_source(epi);
  1105. } else if (epi->ws) {
  1106. ep_destroy_wakeup_source(epi);
  1107. }
  1108. /*
  1109. * The following barrier has two effects:
  1110. *
  1111. * 1) Flush epi changes above to other CPUs. This ensures
  1112. * we do not miss events from ep_poll_callback if an
  1113. * event occurs immediately after we call f_op->poll().
  1114. * We need this because we did not take ep->lock while
  1115. * changing epi above (but ep_poll_callback does take
  1116. * ep->lock).
  1117. *
  1118. * 2) We also need to ensure we do not miss _past_ events
  1119. * when calling f_op->poll(). This barrier also
  1120. * pairs with the barrier in wq_has_sleeper (see
  1121. * comments for wq_has_sleeper).
  1122. *
  1123. * This barrier will now guarantee ep_poll_callback or f_op->poll
  1124. * (or both) will notice the readiness of an item.
  1125. */
  1126. smp_mb();
  1127. /*
  1128. * Get current event bits. We can safely use the file* here because
  1129. * its usage count has been increased by the caller of this function.
  1130. */
  1131. revents = epi->ffd.file->f_op->poll(epi->ffd.file, &pt);
  1132. /*
  1133. * If the item is "hot" and it is not registered inside the ready
  1134. * list, push it inside.
  1135. */
  1136. if (revents & event->events) {
  1137. spin_lock_irq(&ep->lock);
  1138. if (!ep_is_linked(&epi->rdllink)) {
  1139. list_add_tail(&epi->rdllink, &ep->rdllist);
  1140. __pm_stay_awake(epi->ws);
  1141. /* Notify waiting tasks that events are available */
  1142. if (waitqueue_active(&ep->wq))
  1143. wake_up_locked(&ep->wq);
  1144. if (waitqueue_active(&ep->poll_wait))
  1145. pwake++;
  1146. }
  1147. spin_unlock_irq(&ep->lock);
  1148. }
  1149. /* We have to call this outside the lock */
  1150. if (pwake)
  1151. ep_poll_safewake(&ep->poll_wait);
  1152. return 0;
  1153. }
  1154. static int ep_send_events_proc(struct eventpoll *ep, struct list_head *head,
  1155. void *priv)
  1156. {
  1157. struct ep_send_events_data *esed = priv;
  1158. int eventcnt;
  1159. unsigned int revents;
  1160. struct epitem *epi;
  1161. struct epoll_event __user *uevent;
  1162. poll_table pt;
  1163. init_poll_funcptr(&pt, NULL);
  1164. /*
  1165. * We can loop without lock because we are passed a task private list.
  1166. * Items cannot vanish during the loop because ep_scan_ready_list() is
  1167. * holding "mtx" during this call.
  1168. */
  1169. for (eventcnt = 0, uevent = esed->events;
  1170. !list_empty(head) && eventcnt < esed->maxevents;) {
  1171. epi = list_first_entry(head, struct epitem, rdllink);
  1172. /*
  1173. * Activate ep->ws before deactivating epi->ws to prevent
  1174. * triggering auto-suspend here (in case we reactive epi->ws
  1175. * below).
  1176. *
  1177. * This could be rearranged to delay the deactivation of epi->ws
  1178. * instead, but then epi->ws would temporarily be out of sync
  1179. * with ep_is_linked().
  1180. */
  1181. if (epi->ws && epi->ws->active)
  1182. __pm_stay_awake(ep->ws);
  1183. __pm_relax(epi->ws);
  1184. list_del_init(&epi->rdllink);
  1185. pt._key = epi->event.events;
  1186. revents = epi->ffd.file->f_op->poll(epi->ffd.file, &pt) &
  1187. epi->event.events;
  1188. /*
  1189. * If the event mask intersect the caller-requested one,
  1190. * deliver the event to userspace. Again, ep_scan_ready_list()
  1191. * is holding "mtx", so no operations coming from userspace
  1192. * can change the item.
  1193. */
  1194. if (revents) {
  1195. if (__put_user(revents, &uevent->events) ||
  1196. __put_user(epi->event.data, &uevent->data)) {
  1197. list_add(&epi->rdllink, head);
  1198. __pm_stay_awake(epi->ws);
  1199. return eventcnt ? eventcnt : -EFAULT;
  1200. }
  1201. eventcnt++;
  1202. uevent++;
  1203. if (epi->event.events & EPOLLONESHOT)
  1204. epi->event.events &= EP_PRIVATE_BITS;
  1205. else if (!(epi->event.events & EPOLLET)) {
  1206. /*
  1207. * If this file has been added with Level
  1208. * Trigger mode, we need to insert back inside
  1209. * the ready list, so that the next call to
  1210. * epoll_wait() will check again the events
  1211. * availability. At this point, no one can insert
  1212. * into ep->rdllist besides us. The epoll_ctl()
  1213. * callers are locked out by
  1214. * ep_scan_ready_list() holding "mtx" and the
  1215. * poll callback will queue them in ep->ovflist.
  1216. */
  1217. list_add_tail(&epi->rdllink, &ep->rdllist);
  1218. __pm_stay_awake(epi->ws);
  1219. }
  1220. }
  1221. }
  1222. return eventcnt;
  1223. }
  1224. static int ep_send_events(struct eventpoll *ep,
  1225. struct epoll_event __user *events, int maxevents)
  1226. {
  1227. struct ep_send_events_data esed;
  1228. esed.maxevents = maxevents;
  1229. esed.events = events;
  1230. return ep_scan_ready_list(ep, ep_send_events_proc, &esed, 0);
  1231. }
  1232. static inline struct timespec ep_set_mstimeout(long ms)
  1233. {
  1234. struct timespec now, ts = {
  1235. .tv_sec = ms / MSEC_PER_SEC,
  1236. .tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC),
  1237. };
  1238. ktime_get_ts(&now);
  1239. return timespec_add_safe(now, ts);
  1240. }
  1241. /**
  1242. * ep_poll - Retrieves ready events, and delivers them to the caller supplied
  1243. * event buffer.
  1244. *
  1245. * @ep: Pointer to the eventpoll context.
  1246. * @events: Pointer to the userspace buffer where the ready events should be
  1247. * stored.
  1248. * @maxevents: Size (in terms of number of events) of the caller event buffer.
  1249. * @timeout: Maximum timeout for the ready events fetch operation, in
  1250. * milliseconds. If the @timeout is zero, the function will not block,
  1251. * while if the @timeout is less than zero, the function will block
  1252. * until at least one event has been retrieved (or an error
  1253. * occurred).
  1254. *
  1255. * Returns: Returns the number of ready events which have been fetched, or an
  1256. * error code, in case of error.
  1257. */
  1258. static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
  1259. int maxevents, long timeout)
  1260. {
  1261. int res = 0, eavail, timed_out = 0;
  1262. unsigned long flags;
  1263. long slack = 0;
  1264. wait_queue_t wait;
  1265. ktime_t expires, *to = NULL;
  1266. if (timeout > 0) {
  1267. struct timespec end_time = ep_set_mstimeout(timeout);
  1268. slack = select_estimate_accuracy(&end_time);
  1269. to = &expires;
  1270. *to = timespec_to_ktime(end_time);
  1271. } else if (timeout == 0) {
  1272. /*
  1273. * Avoid the unnecessary trip to the wait queue loop, if the
  1274. * caller specified a non blocking operation.
  1275. */
  1276. timed_out = 1;
  1277. spin_lock_irqsave(&ep->lock, flags);
  1278. goto check_events;
  1279. }
  1280. fetch_events:
  1281. spin_lock_irqsave(&ep->lock, flags);
  1282. if (!ep_events_available(ep)) {
  1283. /*
  1284. * We don't have any available event to return to the caller.
  1285. * We need to sleep here, and we will be wake up by
  1286. * ep_poll_callback() when events will become available.
  1287. */
  1288. init_waitqueue_entry(&wait, current);
  1289. __add_wait_queue_exclusive(&ep->wq, &wait);
  1290. for (;;) {
  1291. /*
  1292. * We don't want to sleep if the ep_poll_callback() sends us
  1293. * a wakeup in between. That's why we set the task state
  1294. * to TASK_INTERRUPTIBLE before doing the checks.
  1295. */
  1296. set_current_state(TASK_INTERRUPTIBLE);
  1297. if (ep_events_available(ep) || timed_out)
  1298. break;
  1299. if (signal_pending(current)) {
  1300. res = -EINTR;
  1301. break;
  1302. }
  1303. spin_unlock_irqrestore(&ep->lock, flags);
  1304. if (!freezable_schedule_hrtimeout_range(to, slack,
  1305. HRTIMER_MODE_ABS))
  1306. timed_out = 1;
  1307. spin_lock_irqsave(&ep->lock, flags);
  1308. }
  1309. __remove_wait_queue(&ep->wq, &wait);
  1310. set_current_state(TASK_RUNNING);
  1311. }
  1312. check_events:
  1313. /* Is it worth to try to dig for events ? */
  1314. eavail = ep_events_available(ep);
  1315. spin_unlock_irqrestore(&ep->lock, flags);
  1316. /*
  1317. * Try to transfer events to user space. In case we get 0 events and
  1318. * there's still timeout left over, we go trying again in search of
  1319. * more luck.
  1320. */
  1321. if (!res && eavail &&
  1322. !(res = ep_send_events(ep, events, maxevents)) && !timed_out)
  1323. goto fetch_events;
  1324. return res;
  1325. }
  1326. /**
  1327. * ep_loop_check_proc - Callback function to be passed to the @ep_call_nested()
  1328. * API, to verify that adding an epoll file inside another
  1329. * epoll structure, does not violate the constraints, in
  1330. * terms of closed loops, or too deep chains (which can
  1331. * result in excessive stack usage).
  1332. *
  1333. * @priv: Pointer to the epoll file to be currently checked.
  1334. * @cookie: Original cookie for this call. This is the top-of-the-chain epoll
  1335. * data structure pointer.
  1336. * @call_nests: Current dept of the @ep_call_nested() call stack.
  1337. *
  1338. * Returns: Returns zero if adding the epoll @file inside current epoll
  1339. * structure @ep does not violate the constraints, or -1 otherwise.
  1340. */
  1341. static int ep_loop_check_proc(void *priv, void *cookie, int call_nests)
  1342. {
  1343. int error = 0;
  1344. struct file *file = priv;
  1345. struct eventpoll *ep = file->private_data;
  1346. struct eventpoll *ep_tovisit;
  1347. struct rb_node *rbp;
  1348. struct epitem *epi;
  1349. mutex_lock_nested(&ep->mtx, call_nests + 1);
  1350. ep->visited = 1;
  1351. list_add(&ep->visited_list_link, &visited_list);
  1352. for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
  1353. epi = rb_entry(rbp, struct epitem, rbn);
  1354. if (unlikely(is_file_epoll(epi->ffd.file))) {
  1355. ep_tovisit = epi->ffd.file->private_data;
  1356. if (ep_tovisit->visited)
  1357. continue;
  1358. error = ep_call_nested(&poll_loop_ncalls, EP_MAX_NESTS,
  1359. ep_loop_check_proc, epi->ffd.file,
  1360. ep_tovisit, current);
  1361. if (error != 0)
  1362. break;
  1363. } else {
  1364. /*
  1365. * If we've reached a file that is not associated with
  1366. * an ep, then we need to check if the newly added
  1367. * links are going to add too many wakeup paths. We do
  1368. * this by adding it to the tfile_check_list, if it's
  1369. * not already there, and calling reverse_path_check()
  1370. * during ep_insert().
  1371. */
  1372. if (list_empty(&epi->ffd.file->f_tfile_llink))
  1373. list_add(&epi->ffd.file->f_tfile_llink,
  1374. &tfile_check_list);
  1375. }
  1376. }
  1377. mutex_unlock(&ep->mtx);
  1378. return error;
  1379. }
  1380. /**
  1381. * ep_loop_check - Performs a check to verify that adding an epoll file (@file)
  1382. * another epoll file (represented by @ep) does not create
  1383. * closed loops or too deep chains.
  1384. *
  1385. * @ep: Pointer to the epoll private data structure.
  1386. * @file: Pointer to the epoll file to be checked.
  1387. *
  1388. * Returns: Returns zero if adding the epoll @file inside current epoll
  1389. * structure @ep does not violate the constraints, or -1 otherwise.
  1390. */
  1391. static int ep_loop_check(struct eventpoll *ep, struct file *file)
  1392. {
  1393. int ret;
  1394. struct eventpoll *ep_cur, *ep_next;
  1395. ret = ep_call_nested(&poll_loop_ncalls, EP_MAX_NESTS,
  1396. ep_loop_check_proc, file, ep, current);
  1397. /* clear visited list */
  1398. list_for_each_entry_safe(ep_cur, ep_next, &visited_list,
  1399. visited_list_link) {
  1400. ep_cur->visited = 0;
  1401. list_del(&ep_cur->visited_list_link);
  1402. }
  1403. return ret;
  1404. }
  1405. static void clear_tfile_check_list(void)
  1406. {
  1407. struct file *file;
  1408. /* first clear the tfile_check_list */
  1409. while (!list_empty(&tfile_check_list)) {
  1410. file = list_first_entry(&tfile_check_list, struct file,
  1411. f_tfile_llink);
  1412. list_del_init(&file->f_tfile_llink);
  1413. }
  1414. INIT_LIST_HEAD(&tfile_check_list);
  1415. }
  1416. /*
  1417. * Open an eventpoll file descriptor.
  1418. */
  1419. SYSCALL_DEFINE1(epoll_create1, int, flags)
  1420. {
  1421. int error, fd;
  1422. struct eventpoll *ep = NULL;
  1423. struct file *file;
  1424. /* Check the EPOLL_* constant for consistency. */
  1425. BUILD_BUG_ON(EPOLL_CLOEXEC != O_CLOEXEC);
  1426. if (flags & ~EPOLL_CLOEXEC)
  1427. return -EINVAL;
  1428. /*
  1429. * Create the internal data structure ("struct eventpoll").
  1430. */
  1431. error = ep_alloc(&ep);
  1432. if (error < 0)
  1433. return error;
  1434. /*
  1435. * Creates all the items needed to setup an eventpoll file. That is,
  1436. * a file structure and a free file descriptor.
  1437. */
  1438. fd = get_unused_fd_flags(O_RDWR | (flags & O_CLOEXEC));
  1439. if (fd < 0) {
  1440. error = fd;
  1441. goto out_free_ep;
  1442. }
  1443. file = anon_inode_getfile("[eventpoll]", &eventpoll_fops, ep,
  1444. O_RDWR | (flags & O_CLOEXEC));
  1445. if (IS_ERR(file)) {
  1446. error = PTR_ERR(file);
  1447. goto out_free_fd;
  1448. }
  1449. fd_install(fd, file);
  1450. ep->file = file;
  1451. return fd;
  1452. out_free_fd:
  1453. put_unused_fd(fd);
  1454. out_free_ep:
  1455. ep_free(ep);
  1456. return error;
  1457. }
  1458. SYSCALL_DEFINE1(epoll_create, int, size)
  1459. {
  1460. if (size <= 0)
  1461. return -EINVAL;
  1462. return sys_epoll_create1(0);
  1463. }
  1464. /*
  1465. * The following function implements the controller interface for
  1466. * the eventpoll file that enables the insertion/removal/change of
  1467. * file descriptors inside the interest set.
  1468. */
  1469. SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
  1470. struct epoll_event __user *, event)
  1471. {
  1472. int error;
  1473. int did_lock_epmutex = 0;
  1474. struct file *file, *tfile;
  1475. struct eventpoll *ep;
  1476. struct epitem *epi;
  1477. struct epoll_event epds;
  1478. error = -EFAULT;
  1479. if (ep_op_has_event(op) &&
  1480. copy_from_user(&epds, event, sizeof(struct epoll_event)))
  1481. goto error_return;
  1482. /* Get the "struct file *" for the eventpoll file */
  1483. error = -EBADF;
  1484. file = fget(epfd);
  1485. if (!file)
  1486. goto error_return;
  1487. /* Get the "struct file *" for the target file */
  1488. tfile = fget(fd);
  1489. if (!tfile)
  1490. goto error_fput;
  1491. /* The target file descriptor must support poll */
  1492. error = -EPERM;
  1493. if (!tfile->f_op || !tfile->f_op->poll)
  1494. goto error_tgt_fput;
  1495. /* Check if EPOLLWAKEUP is allowed */
  1496. if ((epds.events & EPOLLWAKEUP) && !capable(CAP_BLOCK_SUSPEND))
  1497. epds.events &= ~EPOLLWAKEUP;
  1498. /*
  1499. * We have to check that the file structure underneath the file descriptor
  1500. * the user passed to us _is_ an eventpoll file. And also we do not permit
  1501. * adding an epoll file descriptor inside itself.
  1502. */
  1503. error = -EINVAL;
  1504. if (file == tfile || !is_file_epoll(file))
  1505. goto error_tgt_fput;
  1506. /*
  1507. * At this point it is safe to assume that the "private_data" contains
  1508. * our own data structure.
  1509. */
  1510. ep = file->private_data;
  1511. /*
  1512. * When we insert an epoll file descriptor, inside another epoll file
  1513. * descriptor, there is the change of creating closed loops, which are
  1514. * better be handled here, than in more critical paths. While we are
  1515. * checking for loops we also determine the list of files reachable
  1516. * and hang them on the tfile_check_list, so we can check that we
  1517. * haven't created too many possible wakeup paths.
  1518. *
  1519. * We need to hold the epmutex across both ep_insert and ep_remove
  1520. * b/c we want to make sure we are looking at a coherent view of
  1521. * epoll network.
  1522. */
  1523. if (op == EPOLL_CTL_ADD || op == EPOLL_CTL_DEL) {
  1524. mutex_lock(&epmutex);
  1525. did_lock_epmutex = 1;
  1526. }
  1527. if (op == EPOLL_CTL_ADD) {
  1528. if (is_file_epoll(tfile)) {
  1529. error = -ELOOP;
  1530. if (ep_loop_check(ep, tfile) != 0) {
  1531. clear_tfile_check_list();
  1532. goto error_tgt_fput;
  1533. }
  1534. } else
  1535. list_add(&tfile->f_tfile_llink, &tfile_check_list);
  1536. }
  1537. mutex_lock_nested(&ep->mtx, 0);
  1538. /*
  1539. * Try to lookup the file inside our RB tree, Since we grabbed "mtx"
  1540. * above, we can be sure to be able to use the item looked up by
  1541. * ep_find() till we release the mutex.
  1542. */
  1543. epi = ep_find(ep, tfile, fd);
  1544. error = -EINVAL;
  1545. switch (op) {
  1546. case EPOLL_CTL_ADD:
  1547. if (!epi) {
  1548. epds.events |= POLLERR | POLLHUP;
  1549. error = ep_insert(ep, &epds, tfile, fd);
  1550. } else
  1551. error = -EEXIST;
  1552. clear_tfile_check_list();
  1553. break;
  1554. case EPOLL_CTL_DEL:
  1555. if (epi)
  1556. error = ep_remove(ep, epi);
  1557. else
  1558. error = -ENOENT;
  1559. break;
  1560. case EPOLL_CTL_MOD:
  1561. if (epi) {
  1562. epds.events |= POLLERR | POLLHUP;
  1563. error = ep_modify(ep, epi, &epds);
  1564. } else
  1565. error = -ENOENT;
  1566. break;
  1567. }
  1568. mutex_unlock(&ep->mtx);
  1569. error_tgt_fput:
  1570. if (did_lock_epmutex)
  1571. mutex_unlock(&epmutex);
  1572. fput(tfile);
  1573. error_fput:
  1574. fput(file);
  1575. error_return:
  1576. return error;
  1577. }
  1578. /*
  1579. * Implement the event wait interface for the eventpoll file. It is the kernel
  1580. * part of the user space epoll_wait(2).
  1581. */
  1582. SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,
  1583. int, maxevents, int, timeout)
  1584. {
  1585. int error;
  1586. struct file *file;
  1587. struct eventpoll *ep;
  1588. /* The maximum number of event must be greater than zero */
  1589. if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
  1590. return -EINVAL;
  1591. /* Verify that the area passed by the user is writeable */
  1592. if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
  1593. error = -EFAULT;
  1594. goto error_return;
  1595. }
  1596. /* Get the "struct file *" for the eventpoll file */
  1597. error = -EBADF;
  1598. file = fget(epfd);
  1599. if (!file)
  1600. goto error_return;
  1601. /*
  1602. * We have to check that the file structure underneath the fd
  1603. * the user passed to us _is_ an eventpoll file.
  1604. */
  1605. error = -EINVAL;
  1606. if (!is_file_epoll(file))
  1607. goto error_fput;
  1608. /*
  1609. * At this point it is safe to assume that the "private_data" contains
  1610. * our own data structure.
  1611. */
  1612. ep = file->private_data;
  1613. /* Time to fish for events ... */
  1614. error = ep_poll(ep, events, maxevents, timeout);
  1615. error_fput:
  1616. fput(file);
  1617. error_return:
  1618. return error;
  1619. }
  1620. #ifdef HAVE_SET_RESTORE_SIGMASK
  1621. /*
  1622. * Implement the event wait interface for the eventpoll file. It is the kernel
  1623. * part of the user space epoll_pwait(2).
  1624. */
  1625. SYSCALL_DEFINE6(epoll_pwait, int, epfd, struct epoll_event __user *, events,
  1626. int, maxevents, int, timeout, const sigset_t __user *, sigmask,
  1627. size_t, sigsetsize)
  1628. {
  1629. int error;
  1630. sigset_t ksigmask, sigsaved;
  1631. /*
  1632. * If the caller wants a certain signal mask to be set during the wait,
  1633. * we apply it here.
  1634. */
  1635. if (sigmask) {
  1636. if (sigsetsize != sizeof(sigset_t))
  1637. return -EINVAL;
  1638. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  1639. return -EFAULT;
  1640. sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  1641. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1642. }
  1643. error = sys_epoll_wait(epfd, events, maxevents, timeout);
  1644. /*
  1645. * If we changed the signal mask, we need to restore the original one.
  1646. * In case we've got a signal while waiting, we do not restore the
  1647. * signal mask yet, and we allow do_signal() to deliver the signal on
  1648. * the way back to userspace, before the signal mask is restored.
  1649. */
  1650. if (sigmask) {
  1651. if (error == -EINTR) {
  1652. memcpy(&current->saved_sigmask, &sigsaved,
  1653. sizeof(sigsaved));
  1654. set_restore_sigmask();
  1655. } else
  1656. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1657. }
  1658. return error;
  1659. }
  1660. #endif /* HAVE_SET_RESTORE_SIGMASK */
  1661. static int __init eventpoll_init(void)
  1662. {
  1663. struct sysinfo si;
  1664. si_meminfo(&si);
  1665. /*
  1666. * Allows top 4% of lomem to be allocated for epoll watches (per user).
  1667. */
  1668. max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) /
  1669. EP_ITEM_COST;
  1670. BUG_ON(max_user_watches < 0);
  1671. /*
  1672. * Initialize the structure used to perform epoll file descriptor
  1673. * inclusion loops checks.
  1674. */
  1675. ep_nested_calls_init(&poll_loop_ncalls);
  1676. /* Initialize the structure used to perform safe poll wait head wake ups */
  1677. ep_nested_calls_init(&poll_safewake_ncalls);
  1678. /* Initialize the structure used to perform file's f_op->poll() calls */
  1679. ep_nested_calls_init(&poll_readywalk_ncalls);
  1680. /* Allocates slab cache used to allocate "struct epitem" items */
  1681. epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
  1682. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  1683. /* Allocates slab cache used to allocate "struct eppoll_entry" */
  1684. pwq_cache = kmem_cache_create("eventpoll_pwq",
  1685. sizeof(struct eppoll_entry), 0, SLAB_PANIC, NULL);
  1686. return 0;
  1687. }
  1688. fs_initcall(eventpoll_init);