eventpoll.c 55 KB

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