eventpoll.c 44 KB

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