eventpoll.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  1. /*
  2. * fs/eventpoll.c ( Efficent event polling implementation )
  3. * Copyright (C) 2001,...,2003 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/module.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/fs.h>
  18. #include <linux/file.h>
  19. #include <linux/signal.h>
  20. #include <linux/errno.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <linux/poll.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/string.h>
  26. #include <linux/list.h>
  27. #include <linux/hash.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/rwsem.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/wait.h>
  33. #include <linux/eventpoll.h>
  34. #include <linux/mount.h>
  35. #include <linux/bitops.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/system.h>
  38. #include <asm/io.h>
  39. #include <asm/mman.h>
  40. #include <asm/atomic.h>
  41. #include <asm/semaphore.h>
  42. /*
  43. * LOCKING:
  44. * There are three level of locking required by epoll :
  45. *
  46. * 1) epsem (semaphore)
  47. * 2) ep->sem (rw_semaphore)
  48. * 3) ep->lock (rw_lock)
  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. * read-write semaphore (ep->sem). It is acquired on read during
  59. * the event transfer loop and in write during epoll_ctl(EPOLL_CTL_DEL)
  60. * and during eventpoll_release_file(). Then we also need a global
  61. * semaphore to serialize eventpoll_release_file() and ep_free().
  62. * This semaphore 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 toepoll_ctl(EPOLL_CTL_DEL).
  66. * It is possible to drop the "ep->sem" and to use the global
  67. * semaphore "epsem" (together with "ep->lock") to have it working,
  68. * but having "ep->sem" will make the interface more scalable.
  69. * Events that require holding "epsem" are very rare, while for
  70. * normal operations the epoll private "ep->sem" will guarantee
  71. * a greater scalability.
  72. */
  73. #define EVENTPOLLFS_MAGIC 0x03111965 /* My birthday should work for this :) */
  74. #define DEBUG_EPOLL 0
  75. #if DEBUG_EPOLL > 0
  76. #define DPRINTK(x) printk x
  77. #define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0)
  78. #else /* #if DEBUG_EPOLL > 0 */
  79. #define DPRINTK(x) (void) 0
  80. #define DNPRINTK(n, x) (void) 0
  81. #endif /* #if DEBUG_EPOLL > 0 */
  82. #define DEBUG_EPI 0
  83. #if DEBUG_EPI != 0
  84. #define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */)
  85. #else /* #if DEBUG_EPI != 0 */
  86. #define EPI_SLAB_DEBUG 0
  87. #endif /* #if DEBUG_EPI != 0 */
  88. /* Epoll private bits inside the event mask */
  89. #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)
  90. /* Maximum number of poll wake up nests we are allowing */
  91. #define EP_MAX_POLLWAKE_NESTS 4
  92. /* Maximum msec timeout value storeable in a long int */
  93. #define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ)
  94. struct epoll_filefd {
  95. struct file *file;
  96. int fd;
  97. };
  98. /*
  99. * Node that is linked into the "wake_task_list" member of the "struct poll_safewake".
  100. * It is used to keep track on all tasks that are currently inside the wake_up() code
  101. * to 1) short-circuit the one coming from the same task and same wait queue head
  102. * ( loop ) 2) allow a maximum number of epoll descriptors inclusion nesting
  103. * 3) let go the ones coming from other tasks.
  104. */
  105. struct wake_task_node {
  106. struct list_head llink;
  107. task_t *task;
  108. wait_queue_head_t *wq;
  109. };
  110. /*
  111. * This is used to implement the safe poll wake up avoiding to reenter
  112. * the poll callback from inside wake_up().
  113. */
  114. struct poll_safewake {
  115. struct list_head wake_task_list;
  116. spinlock_t lock;
  117. };
  118. /*
  119. * This structure is stored inside the "private_data" member of the file
  120. * structure and rapresent the main data sructure for the eventpoll
  121. * interface.
  122. */
  123. struct eventpoll {
  124. /* Protect the this structure access */
  125. rwlock_t lock;
  126. /*
  127. * This semaphore is used to ensure that files are not removed
  128. * while epoll is using them. This is read-held during the event
  129. * collection loop and it is write-held during the file cleanup
  130. * path, the epoll file exit code and the ctl operations.
  131. */
  132. struct rw_semaphore sem;
  133. /* Wait queue used by sys_epoll_wait() */
  134. wait_queue_head_t wq;
  135. /* Wait queue used by file->poll() */
  136. wait_queue_head_t poll_wait;
  137. /* List of ready file descriptors */
  138. struct list_head rdllist;
  139. /* RB-Tree root used to store monitored fd structs */
  140. struct rb_root rbr;
  141. };
  142. /* Wait structure used by the poll hooks */
  143. struct eppoll_entry {
  144. /* List header used to link this structure to the "struct epitem" */
  145. struct list_head llink;
  146. /* The "base" pointer is set to the container "struct epitem" */
  147. void *base;
  148. /*
  149. * Wait queue item that will be linked to the target file wait
  150. * queue head.
  151. */
  152. wait_queue_t wait;
  153. /* The wait queue head that linked the "wait" wait queue item */
  154. wait_queue_head_t *whead;
  155. };
  156. /*
  157. * Each file descriptor added to the eventpoll interface will
  158. * have an entry of this type linked to the hash.
  159. */
  160. struct epitem {
  161. /* RB-Tree node used to link this structure to the eventpoll rb-tree */
  162. struct rb_node rbn;
  163. /* List header used to link this structure to the eventpoll ready list */
  164. struct list_head rdllink;
  165. /* The file descriptor information this item refers to */
  166. struct epoll_filefd ffd;
  167. /* Number of active wait queue attached to poll operations */
  168. int nwait;
  169. /* List containing poll wait queues */
  170. struct list_head pwqlist;
  171. /* The "container" of this item */
  172. struct eventpoll *ep;
  173. /* The structure that describe the interested events and the source fd */
  174. struct epoll_event event;
  175. /*
  176. * Used to keep track of the usage count of the structure. This avoids
  177. * that the structure will desappear from underneath our processing.
  178. */
  179. atomic_t usecnt;
  180. /* List header used to link this item to the "struct file" items list */
  181. struct list_head fllink;
  182. /* List header used to link the item to the transfer list */
  183. struct list_head txlink;
  184. /*
  185. * This is used during the collection/transfer of events to userspace
  186. * to pin items empty events set.
  187. */
  188. unsigned int revents;
  189. };
  190. /* Wrapper struct used by poll queueing */
  191. struct ep_pqueue {
  192. poll_table pt;
  193. struct epitem *epi;
  194. };
  195. static void ep_poll_safewake_init(struct poll_safewake *psw);
  196. static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq);
  197. static int ep_getfd(int *efd, struct inode **einode, struct file **efile,
  198. struct eventpoll *ep);
  199. static int ep_alloc(struct eventpoll **pep);
  200. static void ep_free(struct eventpoll *ep);
  201. static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd);
  202. static void ep_use_epitem(struct epitem *epi);
  203. static void ep_release_epitem(struct epitem *epi);
  204. static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
  205. poll_table *pt);
  206. static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi);
  207. static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
  208. struct file *tfile, int fd);
  209. static int ep_modify(struct eventpoll *ep, struct epitem *epi,
  210. struct epoll_event *event);
  211. static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi);
  212. static int ep_unlink(struct eventpoll *ep, struct epitem *epi);
  213. static int ep_remove(struct eventpoll *ep, struct epitem *epi);
  214. static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key);
  215. static int ep_eventpoll_close(struct inode *inode, struct file *file);
  216. static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait);
  217. static int ep_collect_ready_items(struct eventpoll *ep,
  218. struct list_head *txlist, int maxevents);
  219. static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
  220. struct epoll_event __user *events);
  221. static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist);
  222. static int ep_events_transfer(struct eventpoll *ep,
  223. struct epoll_event __user *events,
  224. int maxevents);
  225. static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
  226. int maxevents, long timeout);
  227. static int eventpollfs_delete_dentry(struct dentry *dentry);
  228. static struct inode *ep_eventpoll_inode(void);
  229. static struct super_block *eventpollfs_get_sb(struct file_system_type *fs_type,
  230. int flags, const char *dev_name,
  231. void *data);
  232. /*
  233. * This semaphore is used to serialize ep_free() and eventpoll_release_file().
  234. */
  235. static struct semaphore epsem;
  236. /* Safe wake up implementation */
  237. static struct poll_safewake psw;
  238. /* Slab cache used to allocate "struct epitem" */
  239. static kmem_cache_t *epi_cache;
  240. /* Slab cache used to allocate "struct eppoll_entry" */
  241. static kmem_cache_t *pwq_cache;
  242. /* Virtual fs used to allocate inodes for eventpoll files */
  243. static struct vfsmount *eventpoll_mnt;
  244. /* File callbacks that implement the eventpoll file behaviour */
  245. static struct file_operations eventpoll_fops = {
  246. .release = ep_eventpoll_close,
  247. .poll = ep_eventpoll_poll
  248. };
  249. /*
  250. * This is used to register the virtual file system from where
  251. * eventpoll inodes are allocated.
  252. */
  253. static struct file_system_type eventpoll_fs_type = {
  254. .name = "eventpollfs",
  255. .get_sb = eventpollfs_get_sb,
  256. .kill_sb = kill_anon_super,
  257. };
  258. /* Very basic directory entry operations for the eventpoll virtual file system */
  259. static struct dentry_operations eventpollfs_dentry_operations = {
  260. .d_delete = eventpollfs_delete_dentry,
  261. };
  262. /* Fast test to see if the file is an evenpoll file */
  263. static inline int is_file_epoll(struct file *f)
  264. {
  265. return f->f_op == &eventpoll_fops;
  266. }
  267. /* Setup the structure that is used as key for the rb-tree */
  268. static inline void ep_set_ffd(struct epoll_filefd *ffd,
  269. struct file *file, int fd)
  270. {
  271. ffd->file = file;
  272. ffd->fd = fd;
  273. }
  274. /* Compare rb-tree keys */
  275. static inline int ep_cmp_ffd(struct epoll_filefd *p1,
  276. struct epoll_filefd *p2)
  277. {
  278. return (p1->file > p2->file ? +1:
  279. (p1->file < p2->file ? -1 : p1->fd - p2->fd));
  280. }
  281. /* Special initialization for the rb-tree node to detect linkage */
  282. static inline void ep_rb_initnode(struct rb_node *n)
  283. {
  284. n->rb_parent = n;
  285. }
  286. /* Removes a node from the rb-tree and marks it for a fast is-linked check */
  287. static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r)
  288. {
  289. rb_erase(n, r);
  290. n->rb_parent = n;
  291. }
  292. /* Fast check to verify that the item is linked to the main rb-tree */
  293. static inline int ep_rb_linked(struct rb_node *n)
  294. {
  295. return n->rb_parent != n;
  296. }
  297. /*
  298. * Remove the item from the list and perform its initialization.
  299. * This is useful for us because we can test if the item is linked
  300. * using "ep_is_linked(p)".
  301. */
  302. static inline void ep_list_del(struct list_head *p)
  303. {
  304. list_del(p);
  305. INIT_LIST_HEAD(p);
  306. }
  307. /* Tells us if the item is currently linked */
  308. static inline int ep_is_linked(struct list_head *p)
  309. {
  310. return !list_empty(p);
  311. }
  312. /* Get the "struct epitem" from a wait queue pointer */
  313. static inline struct epitem * ep_item_from_wait(wait_queue_t *p)
  314. {
  315. return container_of(p, struct eppoll_entry, wait)->base;
  316. }
  317. /* Get the "struct epitem" from an epoll queue wrapper */
  318. static inline struct epitem * ep_item_from_epqueue(poll_table *p)
  319. {
  320. return container_of(p, struct ep_pqueue, pt)->epi;
  321. }
  322. /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
  323. static inline int ep_op_hash_event(int op)
  324. {
  325. return op != EPOLL_CTL_DEL;
  326. }
  327. /* Initialize the poll safe wake up structure */
  328. static void ep_poll_safewake_init(struct poll_safewake *psw)
  329. {
  330. INIT_LIST_HEAD(&psw->wake_task_list);
  331. spin_lock_init(&psw->lock);
  332. }
  333. /*
  334. * Perform a safe wake up of the poll wait list. The problem is that
  335. * with the new callback'd wake up system, it is possible that the
  336. * poll callback is reentered from inside the call to wake_up() done
  337. * on the poll wait queue head. The rule is that we cannot reenter the
  338. * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times,
  339. * and we cannot reenter the same wait queue head at all. This will
  340. * enable to have a hierarchy of epoll file descriptor of no more than
  341. * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock
  342. * because this one gets called by the poll callback, that in turn is called
  343. * from inside a wake_up(), that might be called from irq context.
  344. */
  345. static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
  346. {
  347. int wake_nests = 0;
  348. unsigned long flags;
  349. task_t *this_task = current;
  350. struct list_head *lsthead = &psw->wake_task_list, *lnk;
  351. struct wake_task_node *tncur;
  352. struct wake_task_node tnode;
  353. spin_lock_irqsave(&psw->lock, flags);
  354. /* Try to see if the current task is already inside this wakeup call */
  355. list_for_each(lnk, lsthead) {
  356. tncur = list_entry(lnk, struct wake_task_node, llink);
  357. if (tncur->wq == wq ||
  358. (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) {
  359. /*
  360. * Ops ... loop detected or maximum nest level reached.
  361. * We abort this wake by breaking the cycle itself.
  362. */
  363. spin_unlock_irqrestore(&psw->lock, flags);
  364. return;
  365. }
  366. }
  367. /* Add the current task to the list */
  368. tnode.task = this_task;
  369. tnode.wq = wq;
  370. list_add(&tnode.llink, lsthead);
  371. spin_unlock_irqrestore(&psw->lock, flags);
  372. /* Do really wake up now */
  373. wake_up(wq);
  374. /* Remove the current task from the list */
  375. spin_lock_irqsave(&psw->lock, flags);
  376. list_del(&tnode.llink);
  377. spin_unlock_irqrestore(&psw->lock, flags);
  378. }
  379. /* Used to initialize the epoll bits inside the "struct file" */
  380. void eventpoll_init_file(struct file *file)
  381. {
  382. INIT_LIST_HEAD(&file->f_ep_links);
  383. spin_lock_init(&file->f_ep_lock);
  384. }
  385. /*
  386. * This is called from eventpoll_release() to unlink files from the eventpoll
  387. * interface. We need to have this facility to cleanup correctly files that are
  388. * closed without being removed from the eventpoll interface.
  389. */
  390. void eventpoll_release_file(struct file *file)
  391. {
  392. struct list_head *lsthead = &file->f_ep_links;
  393. struct eventpoll *ep;
  394. struct epitem *epi;
  395. /*
  396. * We don't want to get "file->f_ep_lock" because it is not
  397. * necessary. It is not necessary because we're in the "struct file"
  398. * cleanup path, and this means that noone is using this file anymore.
  399. * The only hit might come from ep_free() but by holding the semaphore
  400. * will correctly serialize the operation. We do need to acquire
  401. * "ep->sem" after "epsem" because ep_remove() requires it when called
  402. * from anywhere but ep_free().
  403. */
  404. down(&epsem);
  405. while (!list_empty(lsthead)) {
  406. epi = list_entry(lsthead->next, struct epitem, fllink);
  407. ep = epi->ep;
  408. ep_list_del(&epi->fllink);
  409. down_write(&ep->sem);
  410. ep_remove(ep, epi);
  411. up_write(&ep->sem);
  412. }
  413. up(&epsem);
  414. }
  415. /*
  416. * It opens an eventpoll file descriptor by suggesting a storage of "size"
  417. * file descriptors. The size parameter is just an hint about how to size
  418. * data structures. It won't prevent the user to store more than "size"
  419. * file descriptors inside the epoll interface. It is the kernel part of
  420. * the userspace epoll_create(2).
  421. */
  422. asmlinkage long sys_epoll_create(int size)
  423. {
  424. int error, fd;
  425. struct eventpoll *ep;
  426. struct inode *inode;
  427. struct file *file;
  428. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
  429. current, size));
  430. /*
  431. * Sanity check on the size parameter, and create the internal data
  432. * structure ( "struct eventpoll" ).
  433. */
  434. error = -EINVAL;
  435. if (size <= 0 || (error = ep_alloc(&ep)) != 0)
  436. goto eexit_1;
  437. /*
  438. * Creates all the items needed to setup an eventpoll file. That is,
  439. * a file structure, and inode and a free file descriptor.
  440. */
  441. error = ep_getfd(&fd, &inode, &file, ep);
  442. if (error)
  443. goto eexit_2;
  444. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
  445. current, size, fd));
  446. return fd;
  447. eexit_2:
  448. ep_free(ep);
  449. kfree(ep);
  450. eexit_1:
  451. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
  452. current, size, error));
  453. return error;
  454. }
  455. /*
  456. * The following function implements the controller interface for
  457. * the eventpoll file that enables the insertion/removal/change of
  458. * file descriptors inside the interest set. It represents
  459. * the kernel part of the user space epoll_ctl(2).
  460. */
  461. asmlinkage long
  462. sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event)
  463. {
  464. int error;
  465. struct file *file, *tfile;
  466. struct eventpoll *ep;
  467. struct epitem *epi;
  468. struct epoll_event epds;
  469. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n",
  470. current, epfd, op, fd, event));
  471. error = -EFAULT;
  472. if (ep_op_hash_event(op) &&
  473. copy_from_user(&epds, event, sizeof(struct epoll_event)))
  474. goto eexit_1;
  475. /* Get the "struct file *" for the eventpoll file */
  476. error = -EBADF;
  477. file = fget(epfd);
  478. if (!file)
  479. goto eexit_1;
  480. /* Get the "struct file *" for the target file */
  481. tfile = fget(fd);
  482. if (!tfile)
  483. goto eexit_2;
  484. /* The target file descriptor must support poll */
  485. error = -EPERM;
  486. if (!tfile->f_op || !tfile->f_op->poll)
  487. goto eexit_3;
  488. /*
  489. * We have to check that the file structure underneath the file descriptor
  490. * the user passed to us _is_ an eventpoll file. And also we do not permit
  491. * adding an epoll file descriptor inside itself.
  492. */
  493. error = -EINVAL;
  494. if (file == tfile || !is_file_epoll(file))
  495. goto eexit_3;
  496. /*
  497. * At this point it is safe to assume that the "private_data" contains
  498. * our own data structure.
  499. */
  500. ep = file->private_data;
  501. down_write(&ep->sem);
  502. /* Try to lookup the file inside our hash table */
  503. epi = ep_find(ep, tfile, fd);
  504. error = -EINVAL;
  505. switch (op) {
  506. case EPOLL_CTL_ADD:
  507. if (!epi) {
  508. epds.events |= POLLERR | POLLHUP;
  509. error = ep_insert(ep, &epds, tfile, fd);
  510. } else
  511. error = -EEXIST;
  512. break;
  513. case EPOLL_CTL_DEL:
  514. if (epi)
  515. error = ep_remove(ep, epi);
  516. else
  517. error = -ENOENT;
  518. break;
  519. case EPOLL_CTL_MOD:
  520. if (epi) {
  521. epds.events |= POLLERR | POLLHUP;
  522. error = ep_modify(ep, epi, &epds);
  523. } else
  524. error = -ENOENT;
  525. break;
  526. }
  527. /*
  528. * The function ep_find() increments the usage count of the structure
  529. * so, if this is not NULL, we need to release it.
  530. */
  531. if (epi)
  532. ep_release_epitem(epi);
  533. up_write(&ep->sem);
  534. eexit_3:
  535. fput(tfile);
  536. eexit_2:
  537. fput(file);
  538. eexit_1:
  539. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n",
  540. current, epfd, op, fd, event, error));
  541. return error;
  542. }
  543. #define MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
  544. /*
  545. * Implement the event wait interface for the eventpoll file. It is the kernel
  546. * part of the user space epoll_wait(2).
  547. */
  548. asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
  549. int maxevents, int timeout)
  550. {
  551. int error;
  552. struct file *file;
  553. struct eventpoll *ep;
  554. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n",
  555. current, epfd, events, maxevents, timeout));
  556. /* The maximum number of event must be greater than zero */
  557. if (maxevents <= 0 || maxevents > MAX_EVENTS)
  558. return -EINVAL;
  559. /* Verify that the area passed by the user is writeable */
  560. if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
  561. error = -EFAULT;
  562. goto eexit_1;
  563. }
  564. /* Get the "struct file *" for the eventpoll file */
  565. error = -EBADF;
  566. file = fget(epfd);
  567. if (!file)
  568. goto eexit_1;
  569. /*
  570. * We have to check that the file structure underneath the fd
  571. * the user passed to us _is_ an eventpoll file.
  572. */
  573. error = -EINVAL;
  574. if (!is_file_epoll(file))
  575. goto eexit_2;
  576. /*
  577. * At this point it is safe to assume that the "private_data" contains
  578. * our own data structure.
  579. */
  580. ep = file->private_data;
  581. /* Time to fish for events ... */
  582. error = ep_poll(ep, events, maxevents, timeout);
  583. eexit_2:
  584. fput(file);
  585. eexit_1:
  586. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n",
  587. current, epfd, events, maxevents, timeout, error));
  588. return error;
  589. }
  590. /*
  591. * Creates the file descriptor to be used by the epoll interface.
  592. */
  593. static int ep_getfd(int *efd, struct inode **einode, struct file **efile,
  594. struct eventpoll *ep)
  595. {
  596. struct qstr this;
  597. char name[32];
  598. struct dentry *dentry;
  599. struct inode *inode;
  600. struct file *file;
  601. int error, fd;
  602. /* Get an ready to use file */
  603. error = -ENFILE;
  604. file = get_empty_filp();
  605. if (!file)
  606. goto eexit_1;
  607. /* Allocates an inode from the eventpoll file system */
  608. inode = ep_eventpoll_inode();
  609. error = PTR_ERR(inode);
  610. if (IS_ERR(inode))
  611. goto eexit_2;
  612. /* Allocates a free descriptor to plug the file onto */
  613. error = get_unused_fd();
  614. if (error < 0)
  615. goto eexit_3;
  616. fd = error;
  617. /*
  618. * Link the inode to a directory entry by creating a unique name
  619. * using the inode number.
  620. */
  621. error = -ENOMEM;
  622. sprintf(name, "[%lu]", inode->i_ino);
  623. this.name = name;
  624. this.len = strlen(name);
  625. this.hash = inode->i_ino;
  626. dentry = d_alloc(eventpoll_mnt->mnt_sb->s_root, &this);
  627. if (!dentry)
  628. goto eexit_4;
  629. dentry->d_op = &eventpollfs_dentry_operations;
  630. d_add(dentry, inode);
  631. file->f_vfsmnt = mntget(eventpoll_mnt);
  632. file->f_dentry = dentry;
  633. file->f_mapping = inode->i_mapping;
  634. file->f_pos = 0;
  635. file->f_flags = O_RDONLY;
  636. file->f_op = &eventpoll_fops;
  637. file->f_mode = FMODE_READ;
  638. file->f_version = 0;
  639. file->private_data = ep;
  640. /* Install the new setup file into the allocated fd. */
  641. fd_install(fd, file);
  642. *efd = fd;
  643. *einode = inode;
  644. *efile = file;
  645. return 0;
  646. eexit_4:
  647. put_unused_fd(fd);
  648. eexit_3:
  649. iput(inode);
  650. eexit_2:
  651. put_filp(file);
  652. eexit_1:
  653. return error;
  654. }
  655. static int ep_alloc(struct eventpoll **pep)
  656. {
  657. struct eventpoll *ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  658. if (!ep)
  659. return -ENOMEM;
  660. rwlock_init(&ep->lock);
  661. init_rwsem(&ep->sem);
  662. init_waitqueue_head(&ep->wq);
  663. init_waitqueue_head(&ep->poll_wait);
  664. INIT_LIST_HEAD(&ep->rdllist);
  665. ep->rbr = RB_ROOT;
  666. *pep = ep;
  667. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
  668. current, ep));
  669. return 0;
  670. }
  671. static void ep_free(struct eventpoll *ep)
  672. {
  673. struct rb_node *rbp;
  674. struct epitem *epi;
  675. /* We need to release all tasks waiting for these file */
  676. if (waitqueue_active(&ep->poll_wait))
  677. ep_poll_safewake(&psw, &ep->poll_wait);
  678. /*
  679. * We need to lock this because we could be hit by
  680. * eventpoll_release_file() while we're freeing the "struct eventpoll".
  681. * We do not need to hold "ep->sem" here because the epoll file
  682. * is on the way to be removed and no one has references to it
  683. * anymore. The only hit might come from eventpoll_release_file() but
  684. * holding "epsem" is sufficent here.
  685. */
  686. down(&epsem);
  687. /*
  688. * Walks through the whole tree by unregistering poll callbacks.
  689. */
  690. for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
  691. epi = rb_entry(rbp, struct epitem, rbn);
  692. ep_unregister_pollwait(ep, epi);
  693. }
  694. /*
  695. * Walks through the whole hash by freeing each "struct epitem". At this
  696. * point we are sure no poll callbacks will be lingering around, and also by
  697. * write-holding "sem" we can be sure that no file cleanup code will hit
  698. * us during this operation. So we can avoid the lock on "ep->lock".
  699. */
  700. while ((rbp = rb_first(&ep->rbr)) != 0) {
  701. epi = rb_entry(rbp, struct epitem, rbn);
  702. ep_remove(ep, epi);
  703. }
  704. up(&epsem);
  705. }
  706. /*
  707. * Search the file inside the eventpoll hash. It add usage count to
  708. * the returned item, so the caller must call ep_release_epitem()
  709. * after finished using the "struct epitem".
  710. */
  711. static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
  712. {
  713. int kcmp;
  714. unsigned long flags;
  715. struct rb_node *rbp;
  716. struct epitem *epi, *epir = NULL;
  717. struct epoll_filefd ffd;
  718. ep_set_ffd(&ffd, file, fd);
  719. read_lock_irqsave(&ep->lock, flags);
  720. for (rbp = ep->rbr.rb_node; rbp; ) {
  721. epi = rb_entry(rbp, struct epitem, rbn);
  722. kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
  723. if (kcmp > 0)
  724. rbp = rbp->rb_right;
  725. else if (kcmp < 0)
  726. rbp = rbp->rb_left;
  727. else {
  728. ep_use_epitem(epi);
  729. epir = epi;
  730. break;
  731. }
  732. }
  733. read_unlock_irqrestore(&ep->lock, flags);
  734. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
  735. current, file, epir));
  736. return epir;
  737. }
  738. /*
  739. * Increment the usage count of the "struct epitem" making it sure
  740. * that the user will have a valid pointer to reference.
  741. */
  742. static void ep_use_epitem(struct epitem *epi)
  743. {
  744. atomic_inc(&epi->usecnt);
  745. }
  746. /*
  747. * Decrement ( release ) the usage count by signaling that the user
  748. * has finished using the structure. It might lead to freeing the
  749. * structure itself if the count goes to zero.
  750. */
  751. static void ep_release_epitem(struct epitem *epi)
  752. {
  753. if (atomic_dec_and_test(&epi->usecnt))
  754. kmem_cache_free(epi_cache, epi);
  755. }
  756. /*
  757. * This is the callback that is used to add our wait queue to the
  758. * target file wakeup lists.
  759. */
  760. static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
  761. poll_table *pt)
  762. {
  763. struct epitem *epi = ep_item_from_epqueue(pt);
  764. struct eppoll_entry *pwq;
  765. if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, SLAB_KERNEL))) {
  766. init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
  767. pwq->whead = whead;
  768. pwq->base = epi;
  769. add_wait_queue(whead, &pwq->wait);
  770. list_add_tail(&pwq->llink, &epi->pwqlist);
  771. epi->nwait++;
  772. } else {
  773. /* We have to signal that an error occurred */
  774. epi->nwait = -1;
  775. }
  776. }
  777. static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
  778. {
  779. int kcmp;
  780. struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
  781. struct epitem *epic;
  782. while (*p) {
  783. parent = *p;
  784. epic = rb_entry(parent, struct epitem, rbn);
  785. kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
  786. if (kcmp > 0)
  787. p = &parent->rb_right;
  788. else
  789. p = &parent->rb_left;
  790. }
  791. rb_link_node(&epi->rbn, parent, p);
  792. rb_insert_color(&epi->rbn, &ep->rbr);
  793. }
  794. static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
  795. struct file *tfile, int fd)
  796. {
  797. int error, revents, pwake = 0;
  798. unsigned long flags;
  799. struct epitem *epi;
  800. struct ep_pqueue epq;
  801. error = -ENOMEM;
  802. if (!(epi = kmem_cache_alloc(epi_cache, SLAB_KERNEL)))
  803. goto eexit_1;
  804. /* Item initialization follow here ... */
  805. ep_rb_initnode(&epi->rbn);
  806. INIT_LIST_HEAD(&epi->rdllink);
  807. INIT_LIST_HEAD(&epi->fllink);
  808. INIT_LIST_HEAD(&epi->txlink);
  809. INIT_LIST_HEAD(&epi->pwqlist);
  810. epi->ep = ep;
  811. ep_set_ffd(&epi->ffd, tfile, fd);
  812. epi->event = *event;
  813. atomic_set(&epi->usecnt, 1);
  814. epi->nwait = 0;
  815. /* Initialize the poll table using the queue callback */
  816. epq.epi = epi;
  817. init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
  818. /*
  819. * Attach the item to the poll hooks and get current event bits.
  820. * We can safely use the file* here because its usage count has
  821. * been increased by the caller of this function.
  822. */
  823. revents = tfile->f_op->poll(tfile, &epq.pt);
  824. /*
  825. * We have to check if something went wrong during the poll wait queue
  826. * install process. Namely an allocation for a wait queue failed due
  827. * high memory pressure.
  828. */
  829. if (epi->nwait < 0)
  830. goto eexit_2;
  831. /* Add the current item to the list of active epoll hook for this file */
  832. spin_lock(&tfile->f_ep_lock);
  833. list_add_tail(&epi->fllink, &tfile->f_ep_links);
  834. spin_unlock(&tfile->f_ep_lock);
  835. /* We have to drop the new item inside our item list to keep track of it */
  836. write_lock_irqsave(&ep->lock, flags);
  837. /* Add the current item to the rb-tree */
  838. ep_rbtree_insert(ep, epi);
  839. /* If the file is already "ready" we drop it inside the ready list */
  840. if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
  841. list_add_tail(&epi->rdllink, &ep->rdllist);
  842. /* Notify waiting tasks that events are available */
  843. if (waitqueue_active(&ep->wq))
  844. wake_up(&ep->wq);
  845. if (waitqueue_active(&ep->poll_wait))
  846. pwake++;
  847. }
  848. write_unlock_irqrestore(&ep->lock, flags);
  849. /* We have to call this outside the lock */
  850. if (pwake)
  851. ep_poll_safewake(&psw, &ep->poll_wait);
  852. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n",
  853. current, ep, tfile, fd));
  854. return 0;
  855. eexit_2:
  856. ep_unregister_pollwait(ep, epi);
  857. /*
  858. * We need to do this because an event could have been arrived on some
  859. * allocated wait queue.
  860. */
  861. write_lock_irqsave(&ep->lock, flags);
  862. if (ep_is_linked(&epi->rdllink))
  863. ep_list_del(&epi->rdllink);
  864. write_unlock_irqrestore(&ep->lock, flags);
  865. kmem_cache_free(epi_cache, epi);
  866. eexit_1:
  867. return error;
  868. }
  869. /*
  870. * Modify the interest event mask by dropping an event if the new mask
  871. * has a match in the current file status.
  872. */
  873. static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
  874. {
  875. int pwake = 0;
  876. unsigned int revents;
  877. unsigned long flags;
  878. /*
  879. * Set the new event interest mask before calling f_op->poll(), otherwise
  880. * a potential race might occur. In fact if we do this operation inside
  881. * the lock, an event might happen between the f_op->poll() call and the
  882. * new event set registering.
  883. */
  884. epi->event.events = event->events;
  885. /*
  886. * Get current event bits. We can safely use the file* here because
  887. * its usage count has been increased by the caller of this function.
  888. */
  889. revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
  890. write_lock_irqsave(&ep->lock, flags);
  891. /* Copy the data member from inside the lock */
  892. epi->event.data = event->data;
  893. /*
  894. * If the item is not linked to the hash it means that it's on its
  895. * way toward the removal. Do nothing in this case.
  896. */
  897. if (ep_rb_linked(&epi->rbn)) {
  898. /*
  899. * If the item is "hot" and it is not registered inside the ready
  900. * list, push it inside. If the item is not "hot" and it is currently
  901. * registered inside the ready list, unlink it.
  902. */
  903. if (revents & event->events) {
  904. if (!ep_is_linked(&epi->rdllink)) {
  905. list_add_tail(&epi->rdllink, &ep->rdllist);
  906. /* Notify waiting tasks that events are available */
  907. if (waitqueue_active(&ep->wq))
  908. wake_up(&ep->wq);
  909. if (waitqueue_active(&ep->poll_wait))
  910. pwake++;
  911. }
  912. }
  913. }
  914. write_unlock_irqrestore(&ep->lock, flags);
  915. /* We have to call this outside the lock */
  916. if (pwake)
  917. ep_poll_safewake(&psw, &ep->poll_wait);
  918. return 0;
  919. }
  920. /*
  921. * This function unregister poll callbacks from the associated file descriptor.
  922. * Since this must be called without holding "ep->lock" the atomic exchange trick
  923. * will protect us from multiple unregister.
  924. */
  925. static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
  926. {
  927. int nwait;
  928. struct list_head *lsthead = &epi->pwqlist;
  929. struct eppoll_entry *pwq;
  930. /* This is called without locks, so we need the atomic exchange */
  931. nwait = xchg(&epi->nwait, 0);
  932. if (nwait) {
  933. while (!list_empty(lsthead)) {
  934. pwq = list_entry(lsthead->next, struct eppoll_entry, llink);
  935. ep_list_del(&pwq->llink);
  936. remove_wait_queue(pwq->whead, &pwq->wait);
  937. kmem_cache_free(pwq_cache, pwq);
  938. }
  939. }
  940. }
  941. /*
  942. * Unlink the "struct epitem" from all places it might have been hooked up.
  943. * This function must be called with write IRQ lock on "ep->lock".
  944. */
  945. static int ep_unlink(struct eventpoll *ep, struct epitem *epi)
  946. {
  947. int error;
  948. /*
  949. * It can happen that this one is called for an item already unlinked.
  950. * The check protect us from doing a double unlink ( crash ).
  951. */
  952. error = -ENOENT;
  953. if (!ep_rb_linked(&epi->rbn))
  954. goto eexit_1;
  955. /*
  956. * Clear the event mask for the unlinked item. This will avoid item
  957. * notifications to be sent after the unlink operation from inside
  958. * the kernel->userspace event transfer loop.
  959. */
  960. epi->event.events = 0;
  961. /*
  962. * At this point is safe to do the job, unlink the item from our rb-tree.
  963. * This operation togheter with the above check closes the door to
  964. * double unlinks.
  965. */
  966. ep_rb_erase(&epi->rbn, &ep->rbr);
  967. /*
  968. * If the item we are going to remove is inside the ready file descriptors
  969. * we want to remove it from this list to avoid stale events.
  970. */
  971. if (ep_is_linked(&epi->rdllink))
  972. ep_list_del(&epi->rdllink);
  973. error = 0;
  974. eexit_1:
  975. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n",
  976. current, ep, epi->file, error));
  977. return error;
  978. }
  979. /*
  980. * Removes a "struct epitem" from the eventpoll hash and deallocates
  981. * all the associated resources.
  982. */
  983. static int ep_remove(struct eventpoll *ep, struct epitem *epi)
  984. {
  985. int error;
  986. unsigned long flags;
  987. struct file *file = epi->ffd.file;
  988. /*
  989. * Removes poll wait queue hooks. We _have_ to do this without holding
  990. * the "ep->lock" otherwise a deadlock might occur. This because of the
  991. * sequence of the lock acquisition. Here we do "ep->lock" then the wait
  992. * queue head lock when unregistering the wait queue. The wakeup callback
  993. * will run by holding the wait queue head lock and will call our callback
  994. * that will try to get "ep->lock".
  995. */
  996. ep_unregister_pollwait(ep, epi);
  997. /* Remove the current item from the list of epoll hooks */
  998. spin_lock(&file->f_ep_lock);
  999. if (ep_is_linked(&epi->fllink))
  1000. ep_list_del(&epi->fllink);
  1001. spin_unlock(&file->f_ep_lock);
  1002. /* We need to acquire the write IRQ lock before calling ep_unlink() */
  1003. write_lock_irqsave(&ep->lock, flags);
  1004. /* Really unlink the item from the hash */
  1005. error = ep_unlink(ep, epi);
  1006. write_unlock_irqrestore(&ep->lock, flags);
  1007. if (error)
  1008. goto eexit_1;
  1009. /* At this point it is safe to free the eventpoll item */
  1010. ep_release_epitem(epi);
  1011. error = 0;
  1012. eexit_1:
  1013. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n",
  1014. current, ep, file, error));
  1015. return error;
  1016. }
  1017. /*
  1018. * This is the callback that is passed to the wait queue wakeup
  1019. * machanism. It is called by the stored file descriptors when they
  1020. * have events to report.
  1021. */
  1022. static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
  1023. {
  1024. int pwake = 0;
  1025. unsigned long flags;
  1026. struct epitem *epi = ep_item_from_wait(wait);
  1027. struct eventpoll *ep = epi->ep;
  1028. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
  1029. current, epi->file, epi, ep));
  1030. write_lock_irqsave(&ep->lock, flags);
  1031. /*
  1032. * If the event mask does not contain any poll(2) event, we consider the
  1033. * descriptor to be disabled. This condition is likely the effect of the
  1034. * EPOLLONESHOT bit that disables the descriptor when an event is received,
  1035. * until the next EPOLL_CTL_MOD will be issued.
  1036. */
  1037. if (!(epi->event.events & ~EP_PRIVATE_BITS))
  1038. goto is_disabled;
  1039. /* If this file is already in the ready list we exit soon */
  1040. if (ep_is_linked(&epi->rdllink))
  1041. goto is_linked;
  1042. list_add_tail(&epi->rdllink, &ep->rdllist);
  1043. is_linked:
  1044. /*
  1045. * Wake up ( if active ) both the eventpoll wait list and the ->poll()
  1046. * wait list.
  1047. */
  1048. if (waitqueue_active(&ep->wq))
  1049. wake_up(&ep->wq);
  1050. if (waitqueue_active(&ep->poll_wait))
  1051. pwake++;
  1052. is_disabled:
  1053. write_unlock_irqrestore(&ep->lock, flags);
  1054. /* We have to call this outside the lock */
  1055. if (pwake)
  1056. ep_poll_safewake(&psw, &ep->poll_wait);
  1057. return 1;
  1058. }
  1059. static int ep_eventpoll_close(struct inode *inode, struct file *file)
  1060. {
  1061. struct eventpoll *ep = file->private_data;
  1062. if (ep) {
  1063. ep_free(ep);
  1064. kfree(ep);
  1065. }
  1066. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep));
  1067. return 0;
  1068. }
  1069. static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
  1070. {
  1071. unsigned int pollflags = 0;
  1072. unsigned long flags;
  1073. struct eventpoll *ep = file->private_data;
  1074. /* Insert inside our poll wait queue */
  1075. poll_wait(file, &ep->poll_wait, wait);
  1076. /* Check our condition */
  1077. read_lock_irqsave(&ep->lock, flags);
  1078. if (!list_empty(&ep->rdllist))
  1079. pollflags = POLLIN | POLLRDNORM;
  1080. read_unlock_irqrestore(&ep->lock, flags);
  1081. return pollflags;
  1082. }
  1083. /*
  1084. * Since we have to release the lock during the __copy_to_user() operation and
  1085. * during the f_op->poll() call, we try to collect the maximum number of items
  1086. * by reducing the irqlock/irqunlock switching rate.
  1087. */
  1088. static int ep_collect_ready_items(struct eventpoll *ep, struct list_head *txlist, int maxevents)
  1089. {
  1090. int nepi;
  1091. unsigned long flags;
  1092. struct list_head *lsthead = &ep->rdllist, *lnk;
  1093. struct epitem *epi;
  1094. write_lock_irqsave(&ep->lock, flags);
  1095. for (nepi = 0, lnk = lsthead->next; lnk != lsthead && nepi < maxevents;) {
  1096. epi = list_entry(lnk, struct epitem, rdllink);
  1097. lnk = lnk->next;
  1098. /* If this file is already in the ready list we exit soon */
  1099. if (!ep_is_linked(&epi->txlink)) {
  1100. /*
  1101. * This is initialized in this way so that the default
  1102. * behaviour of the reinjecting code will be to push back
  1103. * the item inside the ready list.
  1104. */
  1105. epi->revents = epi->event.events;
  1106. /* Link the ready item into the transfer list */
  1107. list_add(&epi->txlink, txlist);
  1108. nepi++;
  1109. /*
  1110. * Unlink the item from the ready list.
  1111. */
  1112. ep_list_del(&epi->rdllink);
  1113. }
  1114. }
  1115. write_unlock_irqrestore(&ep->lock, flags);
  1116. return nepi;
  1117. }
  1118. /*
  1119. * This function is called without holding the "ep->lock" since the call to
  1120. * __copy_to_user() might sleep, and also f_op->poll() might reenable the IRQ
  1121. * because of the way poll() is traditionally implemented in Linux.
  1122. */
  1123. static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
  1124. struct epoll_event __user *events)
  1125. {
  1126. int eventcnt = 0;
  1127. unsigned int revents;
  1128. struct list_head *lnk;
  1129. struct epitem *epi;
  1130. /*
  1131. * We can loop without lock because this is a task private list.
  1132. * The test done during the collection loop will guarantee us that
  1133. * another task will not try to collect this file. Also, items
  1134. * cannot vanish during the loop because we are holding "sem".
  1135. */
  1136. list_for_each(lnk, txlist) {
  1137. epi = list_entry(lnk, struct epitem, txlink);
  1138. /*
  1139. * Get the ready file event set. We can safely use the file
  1140. * because we are holding the "sem" in read and this will
  1141. * guarantee that both the file and the item will not vanish.
  1142. */
  1143. revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
  1144. /*
  1145. * Set the return event set for the current file descriptor.
  1146. * Note that only the task task was successfully able to link
  1147. * the item to its "txlist" will write this field.
  1148. */
  1149. epi->revents = revents & epi->event.events;
  1150. if (epi->revents) {
  1151. if (__put_user(epi->revents,
  1152. &events[eventcnt].events) ||
  1153. __put_user(epi->event.data,
  1154. &events[eventcnt].data))
  1155. return -EFAULT;
  1156. if (epi->event.events & EPOLLONESHOT)
  1157. epi->event.events &= EP_PRIVATE_BITS;
  1158. eventcnt++;
  1159. }
  1160. }
  1161. return eventcnt;
  1162. }
  1163. /*
  1164. * Walk through the transfer list we collected with ep_collect_ready_items()
  1165. * and, if 1) the item is still "alive" 2) its event set is not empty 3) it's
  1166. * not already linked, links it to the ready list. Same as above, we are holding
  1167. * "sem" so items cannot vanish underneath our nose.
  1168. */
  1169. static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist)
  1170. {
  1171. int ricnt = 0, pwake = 0;
  1172. unsigned long flags;
  1173. struct epitem *epi;
  1174. write_lock_irqsave(&ep->lock, flags);
  1175. while (!list_empty(txlist)) {
  1176. epi = list_entry(txlist->next, struct epitem, txlink);
  1177. /* Unlink the current item from the transfer list */
  1178. ep_list_del(&epi->txlink);
  1179. /*
  1180. * If the item is no more linked to the interest set, we don't
  1181. * have to push it inside the ready list because the following
  1182. * ep_release_epitem() is going to drop it. Also, if the current
  1183. * item is set to have an Edge Triggered behaviour, we don't have
  1184. * to push it back either.
  1185. */
  1186. if (ep_rb_linked(&epi->rbn) && !(epi->event.events & EPOLLET) &&
  1187. (epi->revents & epi->event.events) && !ep_is_linked(&epi->rdllink)) {
  1188. list_add_tail(&epi->rdllink, &ep->rdllist);
  1189. ricnt++;
  1190. }
  1191. }
  1192. if (ricnt) {
  1193. /*
  1194. * Wake up ( if active ) both the eventpoll wait list and the ->poll()
  1195. * wait list.
  1196. */
  1197. if (waitqueue_active(&ep->wq))
  1198. wake_up(&ep->wq);
  1199. if (waitqueue_active(&ep->poll_wait))
  1200. pwake++;
  1201. }
  1202. write_unlock_irqrestore(&ep->lock, flags);
  1203. /* We have to call this outside the lock */
  1204. if (pwake)
  1205. ep_poll_safewake(&psw, &ep->poll_wait);
  1206. }
  1207. /*
  1208. * Perform the transfer of events to user space.
  1209. */
  1210. static int ep_events_transfer(struct eventpoll *ep,
  1211. struct epoll_event __user *events, int maxevents)
  1212. {
  1213. int eventcnt = 0;
  1214. struct list_head txlist;
  1215. INIT_LIST_HEAD(&txlist);
  1216. /*
  1217. * We need to lock this because we could be hit by
  1218. * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
  1219. */
  1220. down_read(&ep->sem);
  1221. /* Collect/extract ready items */
  1222. if (ep_collect_ready_items(ep, &txlist, maxevents) > 0) {
  1223. /* Build result set in userspace */
  1224. eventcnt = ep_send_events(ep, &txlist, events);
  1225. /* Reinject ready items into the ready list */
  1226. ep_reinject_items(ep, &txlist);
  1227. }
  1228. up_read(&ep->sem);
  1229. return eventcnt;
  1230. }
  1231. static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
  1232. int maxevents, long timeout)
  1233. {
  1234. int res, eavail;
  1235. unsigned long flags;
  1236. long jtimeout;
  1237. wait_queue_t wait;
  1238. /*
  1239. * Calculate the timeout by checking for the "infinite" value ( -1 )
  1240. * and the overflow condition. The passed timeout is in milliseconds,
  1241. * that why (t * HZ) / 1000.
  1242. */
  1243. jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?
  1244. MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
  1245. retry:
  1246. write_lock_irqsave(&ep->lock, flags);
  1247. res = 0;
  1248. if (list_empty(&ep->rdllist)) {
  1249. /*
  1250. * We don't have any available event to return to the caller.
  1251. * We need to sleep here, and we will be wake up by
  1252. * ep_poll_callback() when events will become available.
  1253. */
  1254. init_waitqueue_entry(&wait, current);
  1255. add_wait_queue(&ep->wq, &wait);
  1256. for (;;) {
  1257. /*
  1258. * We don't want to sleep if the ep_poll_callback() sends us
  1259. * a wakeup in between. That's why we set the task state
  1260. * to TASK_INTERRUPTIBLE before doing the checks.
  1261. */
  1262. set_current_state(TASK_INTERRUPTIBLE);
  1263. if (!list_empty(&ep->rdllist) || !jtimeout)
  1264. break;
  1265. if (signal_pending(current)) {
  1266. res = -EINTR;
  1267. break;
  1268. }
  1269. write_unlock_irqrestore(&ep->lock, flags);
  1270. jtimeout = schedule_timeout(jtimeout);
  1271. write_lock_irqsave(&ep->lock, flags);
  1272. }
  1273. remove_wait_queue(&ep->wq, &wait);
  1274. set_current_state(TASK_RUNNING);
  1275. }
  1276. /* Is it worth to try to dig for events ? */
  1277. eavail = !list_empty(&ep->rdllist);
  1278. write_unlock_irqrestore(&ep->lock, flags);
  1279. /*
  1280. * Try to transfer events to user space. In case we get 0 events and
  1281. * there's still timeout left over, we go trying again in search of
  1282. * more luck.
  1283. */
  1284. if (!res && eavail &&
  1285. !(res = ep_events_transfer(ep, events, maxevents)) && jtimeout)
  1286. goto retry;
  1287. return res;
  1288. }
  1289. static int eventpollfs_delete_dentry(struct dentry *dentry)
  1290. {
  1291. return 1;
  1292. }
  1293. static struct inode *ep_eventpoll_inode(void)
  1294. {
  1295. int error = -ENOMEM;
  1296. struct inode *inode = new_inode(eventpoll_mnt->mnt_sb);
  1297. if (!inode)
  1298. goto eexit_1;
  1299. inode->i_fop = &eventpoll_fops;
  1300. /*
  1301. * Mark the inode dirty from the very beginning,
  1302. * that way it will never be moved to the dirty
  1303. * list because mark_inode_dirty() will think
  1304. * that it already _is_ on the dirty list.
  1305. */
  1306. inode->i_state = I_DIRTY;
  1307. inode->i_mode = S_IRUSR | S_IWUSR;
  1308. inode->i_uid = current->fsuid;
  1309. inode->i_gid = current->fsgid;
  1310. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1311. inode->i_blksize = PAGE_SIZE;
  1312. return inode;
  1313. eexit_1:
  1314. return ERR_PTR(error);
  1315. }
  1316. static struct super_block *
  1317. eventpollfs_get_sb(struct file_system_type *fs_type, int flags,
  1318. const char *dev_name, void *data)
  1319. {
  1320. return get_sb_pseudo(fs_type, "eventpoll:", NULL, EVENTPOLLFS_MAGIC);
  1321. }
  1322. static int __init eventpoll_init(void)
  1323. {
  1324. int error;
  1325. init_MUTEX(&epsem);
  1326. /* Initialize the structure used to perform safe poll wait head wake ups */
  1327. ep_poll_safewake_init(&psw);
  1328. /* Allocates slab cache used to allocate "struct epitem" items */
  1329. epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
  1330. 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC,
  1331. NULL, NULL);
  1332. /* Allocates slab cache used to allocate "struct eppoll_entry" */
  1333. pwq_cache = kmem_cache_create("eventpoll_pwq",
  1334. sizeof(struct eppoll_entry), 0,
  1335. EPI_SLAB_DEBUG|SLAB_PANIC, NULL, NULL);
  1336. /*
  1337. * Register the virtual file system that will be the source of inodes
  1338. * for the eventpoll files
  1339. */
  1340. error = register_filesystem(&eventpoll_fs_type);
  1341. if (error)
  1342. goto epanic;
  1343. /* Mount the above commented virtual file system */
  1344. eventpoll_mnt = kern_mount(&eventpoll_fs_type);
  1345. error = PTR_ERR(eventpoll_mnt);
  1346. if (IS_ERR(eventpoll_mnt))
  1347. goto epanic;
  1348. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: successfully initialized.\n",
  1349. current));
  1350. return 0;
  1351. epanic:
  1352. panic("eventpoll_init() failed\n");
  1353. }
  1354. static void __exit eventpoll_exit(void)
  1355. {
  1356. /* Undo all operations done inside eventpoll_init() */
  1357. unregister_filesystem(&eventpoll_fs_type);
  1358. mntput(eventpoll_mnt);
  1359. kmem_cache_destroy(pwq_cache);
  1360. kmem_cache_destroy(epi_cache);
  1361. }
  1362. module_init(eventpoll_init);
  1363. module_exit(eventpoll_exit);
  1364. MODULE_LICENSE("GPL");