eventpoll.c 44 KB

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