eventpoll.c 53 KB

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