eventpoll.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. /*
  2. * fs/eventpoll.c (Efficent event polling implementation)
  3. * Copyright (C) 2001,...,2007 Davide Libenzi
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Davide Libenzi <davidel@xmailserver.org>
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/fs.h>
  17. #include <linux/file.h>
  18. #include <linux/signal.h>
  19. #include <linux/errno.h>
  20. #include <linux/mm.h>
  21. #include <linux/slab.h>
  22. #include <linux/poll.h>
  23. #include <linux/string.h>
  24. #include <linux/list.h>
  25. #include <linux/hash.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/syscalls.h>
  28. #include <linux/rbtree.h>
  29. #include <linux/wait.h>
  30. #include <linux/eventpoll.h>
  31. #include <linux/mount.h>
  32. #include <linux/bitops.h>
  33. #include <linux/mutex.h>
  34. #include <linux/anon_inodes.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/system.h>
  37. #include <asm/io.h>
  38. #include <asm/mman.h>
  39. #include <asm/atomic.h>
  40. /*
  41. * LOCKING:
  42. * There are three level of locking required by epoll :
  43. *
  44. * 1) epmutex (mutex)
  45. * 2) ep->mtx (mutex)
  46. * 3) ep->lock (spinlock)
  47. *
  48. * The acquire order is the one listed above, from 1 to 3.
  49. * We need a spinlock (ep->lock) because we manipulate objects
  50. * from inside the poll callback, that might be triggered from
  51. * a wake_up() that in turn might be called from IRQ context.
  52. * So we can't sleep inside the poll callback and hence we need
  53. * a spinlock. During the event transfer loop (from kernel to
  54. * user space) we could end up sleeping due a copy_to_user(), so
  55. * we need a lock that will allow us to sleep. This lock is a
  56. * mutex (ep->mtx). It is acquired during the event transfer loop,
  57. * during epoll_ctl(EPOLL_CTL_DEL) and during eventpoll_release_file().
  58. * Then we also need a global mutex to serialize eventpoll_release_file()
  59. * and ep_free().
  60. * This mutex is acquired by ep_free() during the epoll file
  61. * cleanup path and it is also acquired by eventpoll_release_file()
  62. * if a file has been pushed inside an epoll set and it is then
  63. * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL).
  64. * It is possible to drop the "ep->mtx" and to use the global
  65. * mutex "epmutex" (together with "ep->lock") to have it working,
  66. * but having "ep->mtx" will make the interface more scalable.
  67. * Events that require holding "epmutex" are very rare, while for
  68. * normal operations the epoll private "ep->mtx" will guarantee
  69. * a better scalability.
  70. */
  71. #define DEBUG_EPOLL 0
  72. #if DEBUG_EPOLL > 0
  73. #define DPRINTK(x) printk x
  74. #define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0)
  75. #else /* #if DEBUG_EPOLL > 0 */
  76. #define DPRINTK(x) (void) 0
  77. #define DNPRINTK(n, x) (void) 0
  78. #endif /* #if DEBUG_EPOLL > 0 */
  79. #define DEBUG_EPI 0
  80. #if DEBUG_EPI != 0
  81. #define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */)
  82. #else /* #if DEBUG_EPI != 0 */
  83. #define EPI_SLAB_DEBUG 0
  84. #endif /* #if DEBUG_EPI != 0 */
  85. /* Epoll private bits inside the event mask */
  86. #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)
  87. /* Maximum number of poll wake up nests we are allowing */
  88. #define EP_MAX_POLLWAKE_NESTS 4
  89. /* Maximum msec timeout value storeable in a long int */
  90. #define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ)
  91. #define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
  92. #define EP_UNACTIVE_PTR ((void *) -1L)
  93. #define EP_ITEM_COST (sizeof(struct epitem) + sizeof(struct eppoll_entry))
  94. struct epoll_filefd {
  95. struct file *file;
  96. int fd;
  97. };
  98. /*
  99. * Node that is linked into the "wake_task_list" member of the "struct poll_safewake".
  100. * It is used to keep track on all tasks that are currently inside the wake_up() code
  101. * to 1) short-circuit the one coming from the same task and same wait queue head
  102. * (loop) 2) allow a maximum number of epoll descriptors inclusion nesting
  103. * 3) let go the ones coming from other tasks.
  104. */
  105. struct wake_task_node {
  106. struct list_head llink;
  107. struct task_struct *task;
  108. wait_queue_head_t *wq;
  109. };
  110. /*
  111. * This is used to implement the safe poll wake up avoiding to reenter
  112. * the poll callback from inside wake_up().
  113. */
  114. struct poll_safewake {
  115. struct list_head wake_task_list;
  116. spinlock_t lock;
  117. };
  118. /*
  119. * Each file descriptor added to the eventpoll interface will
  120. * have an entry of this type linked to the "rbr" RB tree.
  121. */
  122. struct epitem {
  123. /* RB tree node used to link this structure to the eventpoll RB tree */
  124. struct rb_node rbn;
  125. /* List header used to link this structure to the eventpoll ready list */
  126. struct list_head rdllink;
  127. /*
  128. * Works together "struct eventpoll"->ovflist in keeping the
  129. * single linked chain of items.
  130. */
  131. struct epitem *next;
  132. /* The file descriptor information this item refers to */
  133. struct epoll_filefd ffd;
  134. /* Number of active wait queue attached to poll operations */
  135. int nwait;
  136. /* List containing poll wait queues */
  137. struct list_head pwqlist;
  138. /* The "container" of this item */
  139. struct eventpoll *ep;
  140. /* List header used to link this item to the "struct file" items list */
  141. struct list_head fllink;
  142. /* The structure that describe the interested events and the source fd */
  143. struct epoll_event event;
  144. };
  145. /*
  146. * This structure is stored inside the "private_data" member of the file
  147. * structure and rapresent the main data sructure for the eventpoll
  148. * interface.
  149. */
  150. struct eventpoll {
  151. /* Protect the this structure access */
  152. spinlock_t lock;
  153. /*
  154. * This mutex is used to ensure that files are not removed
  155. * while epoll is using them. This is held during the event
  156. * collection loop, the file cleanup path, the epoll file exit
  157. * code and the ctl operations.
  158. */
  159. struct mutex mtx;
  160. /* Wait queue used by sys_epoll_wait() */
  161. wait_queue_head_t wq;
  162. /* Wait queue used by file->poll() */
  163. wait_queue_head_t poll_wait;
  164. /* List of ready file descriptors */
  165. struct list_head rdllist;
  166. /* RB tree root used to store monitored fd structs */
  167. struct rb_root rbr;
  168. /*
  169. * This is a single linked list that chains all the "struct epitem" that
  170. * happened while transfering ready events to userspace w/out
  171. * holding ->lock.
  172. */
  173. struct epitem *ovflist;
  174. /* The user that created the eventpoll descriptor */
  175. struct user_struct *user;
  176. };
  177. /* Wait structure used by the poll hooks */
  178. struct eppoll_entry {
  179. /* List header used to link this structure to the "struct epitem" */
  180. struct list_head llink;
  181. /* The "base" pointer is set to the container "struct epitem" */
  182. void *base;
  183. /*
  184. * Wait queue item that will be linked to the target file wait
  185. * queue head.
  186. */
  187. wait_queue_t wait;
  188. /* The wait queue head that linked the "wait" wait queue item */
  189. wait_queue_head_t *whead;
  190. };
  191. /* Wrapper struct used by poll queueing */
  192. struct ep_pqueue {
  193. poll_table pt;
  194. struct epitem *epi;
  195. };
  196. /*
  197. * Configuration options available inside /proc/sys/fs/epoll/
  198. */
  199. /* Maximum number of epoll devices, per user */
  200. static int max_user_instances __read_mostly;
  201. /* Maximum number of epoll watched descriptors, per user */
  202. static int max_user_watches __read_mostly;
  203. /*
  204. * This mutex is used to serialize ep_free() and eventpoll_release_file().
  205. */
  206. static DEFINE_MUTEX(epmutex);
  207. /* Safe wake up implementation */
  208. static struct poll_safewake psw;
  209. /* Slab cache used to allocate "struct epitem" */
  210. static struct kmem_cache *epi_cache __read_mostly;
  211. /* Slab cache used to allocate "struct eppoll_entry" */
  212. static struct kmem_cache *pwq_cache __read_mostly;
  213. #ifdef CONFIG_SYSCTL
  214. #include <linux/sysctl.h>
  215. static int zero;
  216. ctl_table epoll_table[] = {
  217. {
  218. .procname = "max_user_instances",
  219. .data = &max_user_instances,
  220. .maxlen = sizeof(int),
  221. .mode = 0644,
  222. .proc_handler = &proc_dointvec_minmax,
  223. .extra1 = &zero,
  224. },
  225. {
  226. .procname = "max_user_watches",
  227. .data = &max_user_watches,
  228. .maxlen = sizeof(int),
  229. .mode = 0644,
  230. .proc_handler = &proc_dointvec_minmax,
  231. .extra1 = &zero,
  232. },
  233. { .ctl_name = 0 }
  234. };
  235. #endif /* CONFIG_SYSCTL */
  236. /* Setup the structure that is used as key for the RB tree */
  237. static inline void ep_set_ffd(struct epoll_filefd *ffd,
  238. struct file *file, int fd)
  239. {
  240. ffd->file = file;
  241. ffd->fd = fd;
  242. }
  243. /* Compare RB tree keys */
  244. static inline int ep_cmp_ffd(struct epoll_filefd *p1,
  245. struct epoll_filefd *p2)
  246. {
  247. return (p1->file > p2->file ? +1:
  248. (p1->file < p2->file ? -1 : p1->fd - p2->fd));
  249. }
  250. /* Tells us if the item is currently linked */
  251. static inline int ep_is_linked(struct list_head *p)
  252. {
  253. return !list_empty(p);
  254. }
  255. /* Get the "struct epitem" from a wait queue pointer */
  256. static inline struct epitem *ep_item_from_wait(wait_queue_t *p)
  257. {
  258. return container_of(p, struct eppoll_entry, wait)->base;
  259. }
  260. /* Get the "struct epitem" from an epoll queue wrapper */
  261. static inline struct epitem *ep_item_from_epqueue(poll_table *p)
  262. {
  263. return container_of(p, struct ep_pqueue, pt)->epi;
  264. }
  265. /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
  266. static inline int ep_op_has_event(int op)
  267. {
  268. return op != EPOLL_CTL_DEL;
  269. }
  270. /* Initialize the poll safe wake up structure */
  271. static void ep_poll_safewake_init(struct poll_safewake *psw)
  272. {
  273. INIT_LIST_HEAD(&psw->wake_task_list);
  274. spin_lock_init(&psw->lock);
  275. }
  276. /*
  277. * Perform a safe wake up of the poll wait list. The problem is that
  278. * with the new callback'd wake up system, it is possible that the
  279. * poll callback is reentered from inside the call to wake_up() done
  280. * on the poll wait queue head. The rule is that we cannot reenter the
  281. * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times,
  282. * and we cannot reenter the same wait queue head at all. This will
  283. * enable to have a hierarchy of epoll file descriptor of no more than
  284. * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock
  285. * because this one gets called by the poll callback, that in turn is called
  286. * from inside a wake_up(), that might be called from irq context.
  287. */
  288. static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
  289. {
  290. int wake_nests = 0;
  291. unsigned long flags;
  292. struct task_struct *this_task = current;
  293. struct list_head *lsthead = &psw->wake_task_list;
  294. struct wake_task_node *tncur;
  295. struct wake_task_node tnode;
  296. spin_lock_irqsave(&psw->lock, flags);
  297. /* Try to see if the current task is already inside this wakeup call */
  298. list_for_each_entry(tncur, lsthead, llink) {
  299. if (tncur->wq == wq ||
  300. (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) {
  301. /*
  302. * Ops ... loop detected or maximum nest level reached.
  303. * We abort this wake by breaking the cycle itself.
  304. */
  305. spin_unlock_irqrestore(&psw->lock, flags);
  306. return;
  307. }
  308. }
  309. /* Add the current task to the list */
  310. tnode.task = this_task;
  311. tnode.wq = wq;
  312. list_add(&tnode.llink, lsthead);
  313. spin_unlock_irqrestore(&psw->lock, flags);
  314. /* Do really wake up now */
  315. wake_up_nested(wq, 1 + wake_nests);
  316. /* Remove the current task from the list */
  317. spin_lock_irqsave(&psw->lock, flags);
  318. list_del(&tnode.llink);
  319. spin_unlock_irqrestore(&psw->lock, flags);
  320. }
  321. /*
  322. * This function unregister poll callbacks from the associated file descriptor.
  323. * Since this must be called without holding "ep->lock" the atomic exchange trick
  324. * will protect us from multiple unregister.
  325. */
  326. static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
  327. {
  328. int nwait;
  329. struct list_head *lsthead = &epi->pwqlist;
  330. struct eppoll_entry *pwq;
  331. /* This is called without locks, so we need the atomic exchange */
  332. nwait = xchg(&epi->nwait, 0);
  333. if (nwait) {
  334. while (!list_empty(lsthead)) {
  335. pwq = list_first_entry(lsthead, struct eppoll_entry, llink);
  336. list_del_init(&pwq->llink);
  337. remove_wait_queue(pwq->whead, &pwq->wait);
  338. kmem_cache_free(pwq_cache, pwq);
  339. }
  340. }
  341. }
  342. /*
  343. * Removes a "struct epitem" from the eventpoll RB tree and deallocates
  344. * all the associated resources. Must be called with "mtx" held.
  345. */
  346. static int ep_remove(struct eventpoll *ep, struct epitem *epi)
  347. {
  348. unsigned long flags;
  349. struct file *file = epi->ffd.file;
  350. /*
  351. * Removes poll wait queue hooks. We _have_ to do this without holding
  352. * the "ep->lock" otherwise a deadlock might occur. This because of the
  353. * sequence of the lock acquisition. Here we do "ep->lock" then the wait
  354. * queue head lock when unregistering the wait queue. The wakeup callback
  355. * will run by holding the wait queue head lock and will call our callback
  356. * that will try to get "ep->lock".
  357. */
  358. ep_unregister_pollwait(ep, epi);
  359. /* Remove the current item from the list of epoll hooks */
  360. spin_lock(&file->f_ep_lock);
  361. if (ep_is_linked(&epi->fllink))
  362. list_del_init(&epi->fllink);
  363. spin_unlock(&file->f_ep_lock);
  364. rb_erase(&epi->rbn, &ep->rbr);
  365. spin_lock_irqsave(&ep->lock, flags);
  366. if (ep_is_linked(&epi->rdllink))
  367. list_del_init(&epi->rdllink);
  368. spin_unlock_irqrestore(&ep->lock, flags);
  369. /* At this point it is safe to free the eventpoll item */
  370. kmem_cache_free(epi_cache, epi);
  371. atomic_dec(&ep->user->epoll_watches);
  372. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p)\n",
  373. current, ep, file));
  374. return 0;
  375. }
  376. static void ep_free(struct eventpoll *ep)
  377. {
  378. struct rb_node *rbp;
  379. struct epitem *epi;
  380. /* We need to release all tasks waiting for these file */
  381. if (waitqueue_active(&ep->poll_wait))
  382. ep_poll_safewake(&psw, &ep->poll_wait);
  383. /*
  384. * We need to lock this because we could be hit by
  385. * eventpoll_release_file() while we're freeing the "struct eventpoll".
  386. * We do not need to hold "ep->mtx" here because the epoll file
  387. * is on the way to be removed and no one has references to it
  388. * anymore. The only hit might come from eventpoll_release_file() but
  389. * holding "epmutex" is sufficent here.
  390. */
  391. mutex_lock(&epmutex);
  392. /*
  393. * Walks through the whole tree by unregistering poll callbacks.
  394. */
  395. for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
  396. epi = rb_entry(rbp, struct epitem, rbn);
  397. ep_unregister_pollwait(ep, epi);
  398. }
  399. /*
  400. * Walks through the whole tree by freeing each "struct epitem". At this
  401. * point we are sure no poll callbacks will be lingering around, and also by
  402. * holding "epmutex" we can be sure that no file cleanup code will hit
  403. * us during this operation. So we can avoid the lock on "ep->lock".
  404. */
  405. while ((rbp = rb_first(&ep->rbr)) != NULL) {
  406. epi = rb_entry(rbp, struct epitem, rbn);
  407. ep_remove(ep, epi);
  408. }
  409. mutex_unlock(&epmutex);
  410. mutex_destroy(&ep->mtx);
  411. atomic_dec(&ep->user->epoll_devs);
  412. free_uid(ep->user);
  413. kfree(ep);
  414. }
  415. static int ep_eventpoll_release(struct inode *inode, struct file *file)
  416. {
  417. struct eventpoll *ep = file->private_data;
  418. if (ep)
  419. ep_free(ep);
  420. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep));
  421. return 0;
  422. }
  423. static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
  424. {
  425. unsigned int pollflags = 0;
  426. unsigned long flags;
  427. struct eventpoll *ep = file->private_data;
  428. /* Insert inside our poll wait queue */
  429. poll_wait(file, &ep->poll_wait, wait);
  430. /* Check our condition */
  431. spin_lock_irqsave(&ep->lock, flags);
  432. if (!list_empty(&ep->rdllist))
  433. pollflags = POLLIN | POLLRDNORM;
  434. spin_unlock_irqrestore(&ep->lock, flags);
  435. return pollflags;
  436. }
  437. /* File callbacks that implement the eventpoll file behaviour */
  438. static const struct file_operations eventpoll_fops = {
  439. .release = ep_eventpoll_release,
  440. .poll = ep_eventpoll_poll
  441. };
  442. /* Fast test to see if the file is an evenpoll file */
  443. static inline int is_file_epoll(struct file *f)
  444. {
  445. return f->f_op == &eventpoll_fops;
  446. }
  447. /*
  448. * This is called from eventpoll_release() to unlink files from the eventpoll
  449. * interface. We need to have this facility to cleanup correctly files that are
  450. * closed without being removed from the eventpoll interface.
  451. */
  452. void eventpoll_release_file(struct file *file)
  453. {
  454. struct list_head *lsthead = &file->f_ep_links;
  455. struct eventpoll *ep;
  456. struct epitem *epi;
  457. /*
  458. * We don't want to get "file->f_ep_lock" because it is not
  459. * necessary. It is not necessary because we're in the "struct file"
  460. * cleanup path, and this means that noone is using this file anymore.
  461. * So, for example, epoll_ctl() cannot hit here sicne if we reach this
  462. * point, the file counter already went to zero and fget() would fail.
  463. * The only hit might come from ep_free() but by holding the mutex
  464. * will correctly serialize the operation. We do need to acquire
  465. * "ep->mtx" after "epmutex" because ep_remove() requires it when called
  466. * from anywhere but ep_free().
  467. */
  468. mutex_lock(&epmutex);
  469. while (!list_empty(lsthead)) {
  470. epi = list_first_entry(lsthead, struct epitem, fllink);
  471. ep = epi->ep;
  472. list_del_init(&epi->fllink);
  473. mutex_lock(&ep->mtx);
  474. ep_remove(ep, epi);
  475. mutex_unlock(&ep->mtx);
  476. }
  477. mutex_unlock(&epmutex);
  478. }
  479. static int ep_alloc(struct eventpoll **pep)
  480. {
  481. int error;
  482. struct user_struct *user;
  483. struct eventpoll *ep;
  484. user = get_current_user();
  485. error = -EMFILE;
  486. if (unlikely(atomic_read(&user->epoll_devs) >=
  487. max_user_instances))
  488. goto free_uid;
  489. error = -ENOMEM;
  490. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  491. if (unlikely(!ep))
  492. goto free_uid;
  493. spin_lock_init(&ep->lock);
  494. mutex_init(&ep->mtx);
  495. init_waitqueue_head(&ep->wq);
  496. init_waitqueue_head(&ep->poll_wait);
  497. INIT_LIST_HEAD(&ep->rdllist);
  498. ep->rbr = RB_ROOT;
  499. ep->ovflist = EP_UNACTIVE_PTR;
  500. ep->user = user;
  501. *pep = ep;
  502. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
  503. current, ep));
  504. return 0;
  505. free_uid:
  506. free_uid(user);
  507. return error;
  508. }
  509. /*
  510. * Search the file inside the eventpoll tree. The RB tree operations
  511. * are protected by the "mtx" mutex, and ep_find() must be called with
  512. * "mtx" held.
  513. */
  514. static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
  515. {
  516. int kcmp;
  517. struct rb_node *rbp;
  518. struct epitem *epi, *epir = NULL;
  519. struct epoll_filefd ffd;
  520. ep_set_ffd(&ffd, file, fd);
  521. for (rbp = ep->rbr.rb_node; rbp; ) {
  522. epi = rb_entry(rbp, struct epitem, rbn);
  523. kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
  524. if (kcmp > 0)
  525. rbp = rbp->rb_right;
  526. else if (kcmp < 0)
  527. rbp = rbp->rb_left;
  528. else {
  529. epir = epi;
  530. break;
  531. }
  532. }
  533. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
  534. current, file, epir));
  535. return epir;
  536. }
  537. /*
  538. * This is the callback that is passed to the wait queue wakeup
  539. * machanism. It is called by the stored file descriptors when they
  540. * have events to report.
  541. */
  542. static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
  543. {
  544. int pwake = 0;
  545. unsigned long flags;
  546. struct epitem *epi = ep_item_from_wait(wait);
  547. struct eventpoll *ep = epi->ep;
  548. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
  549. current, epi->ffd.file, epi, ep));
  550. spin_lock_irqsave(&ep->lock, flags);
  551. /*
  552. * If the event mask does not contain any poll(2) event, we consider the
  553. * descriptor to be disabled. This condition is likely the effect of the
  554. * EPOLLONESHOT bit that disables the descriptor when an event is received,
  555. * until the next EPOLL_CTL_MOD will be issued.
  556. */
  557. if (!(epi->event.events & ~EP_PRIVATE_BITS))
  558. goto out_unlock;
  559. /*
  560. * If we are trasfering events to userspace, we can hold no locks
  561. * (because we're accessing user memory, and because of linux f_op->poll()
  562. * semantics). All the events that happens during that period of time are
  563. * chained in ep->ovflist and requeued later on.
  564. */
  565. if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) {
  566. if (epi->next == EP_UNACTIVE_PTR) {
  567. epi->next = ep->ovflist;
  568. ep->ovflist = epi;
  569. }
  570. goto out_unlock;
  571. }
  572. /* If this file is already in the ready list we exit soon */
  573. if (ep_is_linked(&epi->rdllink))
  574. goto is_linked;
  575. list_add_tail(&epi->rdllink, &ep->rdllist);
  576. is_linked:
  577. /*
  578. * Wake up ( if active ) both the eventpoll wait list and the ->poll()
  579. * wait list.
  580. */
  581. if (waitqueue_active(&ep->wq))
  582. wake_up_locked(&ep->wq);
  583. if (waitqueue_active(&ep->poll_wait))
  584. pwake++;
  585. out_unlock:
  586. spin_unlock_irqrestore(&ep->lock, flags);
  587. /* We have to call this outside the lock */
  588. if (pwake)
  589. ep_poll_safewake(&psw, &ep->poll_wait);
  590. return 1;
  591. }
  592. /*
  593. * This is the callback that is used to add our wait queue to the
  594. * target file wakeup lists.
  595. */
  596. static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
  597. poll_table *pt)
  598. {
  599. struct epitem *epi = ep_item_from_epqueue(pt);
  600. struct eppoll_entry *pwq;
  601. if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
  602. init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
  603. pwq->whead = whead;
  604. pwq->base = epi;
  605. add_wait_queue(whead, &pwq->wait);
  606. list_add_tail(&pwq->llink, &epi->pwqlist);
  607. epi->nwait++;
  608. } else {
  609. /* We have to signal that an error occurred */
  610. epi->nwait = -1;
  611. }
  612. }
  613. static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
  614. {
  615. int kcmp;
  616. struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
  617. struct epitem *epic;
  618. while (*p) {
  619. parent = *p;
  620. epic = rb_entry(parent, struct epitem, rbn);
  621. kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
  622. if (kcmp > 0)
  623. p = &parent->rb_right;
  624. else
  625. p = &parent->rb_left;
  626. }
  627. rb_link_node(&epi->rbn, parent, p);
  628. rb_insert_color(&epi->rbn, &ep->rbr);
  629. }
  630. /*
  631. * Must be called with "mtx" held.
  632. */
  633. static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
  634. struct file *tfile, int fd)
  635. {
  636. int error, revents, pwake = 0;
  637. unsigned long flags;
  638. struct epitem *epi;
  639. struct ep_pqueue epq;
  640. if (unlikely(atomic_read(&ep->user->epoll_watches) >=
  641. max_user_watches))
  642. return -ENOSPC;
  643. if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
  644. return -ENOMEM;
  645. /* Item initialization follow here ... */
  646. INIT_LIST_HEAD(&epi->rdllink);
  647. INIT_LIST_HEAD(&epi->fllink);
  648. INIT_LIST_HEAD(&epi->pwqlist);
  649. epi->ep = ep;
  650. ep_set_ffd(&epi->ffd, tfile, fd);
  651. epi->event = *event;
  652. epi->nwait = 0;
  653. epi->next = EP_UNACTIVE_PTR;
  654. /* Initialize the poll table using the queue callback */
  655. epq.epi = epi;
  656. init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
  657. /*
  658. * Attach the item to the poll hooks and get current event bits.
  659. * We can safely use the file* here because its usage count has
  660. * been increased by the caller of this function. Note that after
  661. * this operation completes, the poll callback can start hitting
  662. * the new item.
  663. */
  664. revents = tfile->f_op->poll(tfile, &epq.pt);
  665. /*
  666. * We have to check if something went wrong during the poll wait queue
  667. * install process. Namely an allocation for a wait queue failed due
  668. * high memory pressure.
  669. */
  670. error = -ENOMEM;
  671. if (epi->nwait < 0)
  672. goto error_unregister;
  673. /* Add the current item to the list of active epoll hook for this file */
  674. spin_lock(&tfile->f_ep_lock);
  675. list_add_tail(&epi->fllink, &tfile->f_ep_links);
  676. spin_unlock(&tfile->f_ep_lock);
  677. /*
  678. * Add the current item to the RB tree. All RB tree operations are
  679. * protected by "mtx", and ep_insert() is called with "mtx" held.
  680. */
  681. ep_rbtree_insert(ep, epi);
  682. /* We have to drop the new item inside our item list to keep track of it */
  683. spin_lock_irqsave(&ep->lock, flags);
  684. /* If the file is already "ready" we drop it inside the ready list */
  685. if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
  686. list_add_tail(&epi->rdllink, &ep->rdllist);
  687. /* Notify waiting tasks that events are available */
  688. if (waitqueue_active(&ep->wq))
  689. wake_up_locked(&ep->wq);
  690. if (waitqueue_active(&ep->poll_wait))
  691. pwake++;
  692. }
  693. spin_unlock_irqrestore(&ep->lock, flags);
  694. atomic_inc(&ep->user->epoll_watches);
  695. /* We have to call this outside the lock */
  696. if (pwake)
  697. ep_poll_safewake(&psw, &ep->poll_wait);
  698. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n",
  699. current, ep, tfile, fd));
  700. return 0;
  701. error_unregister:
  702. ep_unregister_pollwait(ep, epi);
  703. /*
  704. * We need to do this because an event could have been arrived on some
  705. * allocated wait queue. Note that we don't care about the ep->ovflist
  706. * list, since that is used/cleaned only inside a section bound by "mtx".
  707. * And ep_insert() is called with "mtx" held.
  708. */
  709. spin_lock_irqsave(&ep->lock, flags);
  710. if (ep_is_linked(&epi->rdllink))
  711. list_del_init(&epi->rdllink);
  712. spin_unlock_irqrestore(&ep->lock, flags);
  713. kmem_cache_free(epi_cache, epi);
  714. return error;
  715. }
  716. /*
  717. * Modify the interest event mask by dropping an event if the new mask
  718. * has a match in the current file status. Must be called with "mtx" held.
  719. */
  720. static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
  721. {
  722. int pwake = 0;
  723. unsigned int revents;
  724. unsigned long flags;
  725. /*
  726. * Set the new event interest mask before calling f_op->poll(), otherwise
  727. * a potential race might occur. In fact if we do this operation inside
  728. * the lock, an event might happen between the f_op->poll() call and the
  729. * new event set registering.
  730. */
  731. epi->event.events = event->events;
  732. /*
  733. * Get current event bits. We can safely use the file* here because
  734. * its usage count has been increased by the caller of this function.
  735. */
  736. revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
  737. spin_lock_irqsave(&ep->lock, flags);
  738. /* Copy the data member from inside the lock */
  739. epi->event.data = event->data;
  740. /*
  741. * If the item is "hot" and it is not registered inside the ready
  742. * list, push it inside.
  743. */
  744. if (revents & event->events) {
  745. if (!ep_is_linked(&epi->rdllink)) {
  746. list_add_tail(&epi->rdllink, &ep->rdllist);
  747. /* Notify waiting tasks that events are available */
  748. if (waitqueue_active(&ep->wq))
  749. wake_up_locked(&ep->wq);
  750. if (waitqueue_active(&ep->poll_wait))
  751. pwake++;
  752. }
  753. }
  754. spin_unlock_irqrestore(&ep->lock, flags);
  755. /* We have to call this outside the lock */
  756. if (pwake)
  757. ep_poll_safewake(&psw, &ep->poll_wait);
  758. return 0;
  759. }
  760. static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *events,
  761. int maxevents)
  762. {
  763. int eventcnt, error = -EFAULT, pwake = 0;
  764. unsigned int revents;
  765. unsigned long flags;
  766. struct epitem *epi, *nepi;
  767. struct list_head txlist;
  768. INIT_LIST_HEAD(&txlist);
  769. /*
  770. * We need to lock this because we could be hit by
  771. * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
  772. */
  773. mutex_lock(&ep->mtx);
  774. /*
  775. * Steal the ready list, and re-init the original one to the
  776. * empty list. Also, set ep->ovflist to NULL so that events
  777. * happening while looping w/out locks, are not lost. We cannot
  778. * have the poll callback to queue directly on ep->rdllist,
  779. * because we are doing it in the loop below, in a lockless way.
  780. */
  781. spin_lock_irqsave(&ep->lock, flags);
  782. list_splice(&ep->rdllist, &txlist);
  783. INIT_LIST_HEAD(&ep->rdllist);
  784. ep->ovflist = NULL;
  785. spin_unlock_irqrestore(&ep->lock, flags);
  786. /*
  787. * We can loop without lock because this is a task private list.
  788. * We just splice'd out the ep->rdllist in ep_collect_ready_items().
  789. * Items cannot vanish during the loop because we are holding "mtx".
  790. */
  791. for (eventcnt = 0; !list_empty(&txlist) && eventcnt < maxevents;) {
  792. epi = list_first_entry(&txlist, struct epitem, rdllink);
  793. list_del_init(&epi->rdllink);
  794. /*
  795. * Get the ready file event set. We can safely use the file
  796. * because we are holding the "mtx" and this will guarantee
  797. * that both the file and the item will not vanish.
  798. */
  799. revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
  800. revents &= epi->event.events;
  801. /*
  802. * Is the event mask intersect the caller-requested one,
  803. * deliver the event to userspace. Again, we are holding
  804. * "mtx", so no operations coming from userspace can change
  805. * the item.
  806. */
  807. if (revents) {
  808. if (__put_user(revents,
  809. &events[eventcnt].events) ||
  810. __put_user(epi->event.data,
  811. &events[eventcnt].data))
  812. goto errxit;
  813. if (epi->event.events & EPOLLONESHOT)
  814. epi->event.events &= EP_PRIVATE_BITS;
  815. eventcnt++;
  816. }
  817. /*
  818. * At this point, noone can insert into ep->rdllist besides
  819. * us. The epoll_ctl() callers are locked out by us holding
  820. * "mtx" and the poll callback will queue them in ep->ovflist.
  821. */
  822. if (!(epi->event.events & EPOLLET) &&
  823. (revents & epi->event.events))
  824. list_add_tail(&epi->rdllink, &ep->rdllist);
  825. }
  826. error = 0;
  827. errxit:
  828. spin_lock_irqsave(&ep->lock, flags);
  829. /*
  830. * During the time we spent in the loop above, some other events
  831. * might have been queued by the poll callback. We re-insert them
  832. * inside the main ready-list here.
  833. */
  834. for (nepi = ep->ovflist; (epi = nepi) != NULL;
  835. nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
  836. /*
  837. * If the above loop quit with errors, the epoll item might still
  838. * be linked to "txlist", and the list_splice() done below will
  839. * take care of those cases.
  840. */
  841. if (!ep_is_linked(&epi->rdllink))
  842. list_add_tail(&epi->rdllink, &ep->rdllist);
  843. }
  844. /*
  845. * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
  846. * releasing the lock, events will be queued in the normal way inside
  847. * ep->rdllist.
  848. */
  849. ep->ovflist = EP_UNACTIVE_PTR;
  850. /*
  851. * In case of error in the event-send loop, or in case the number of
  852. * ready events exceeds the userspace limit, we need to splice the
  853. * "txlist" back inside ep->rdllist.
  854. */
  855. list_splice(&txlist, &ep->rdllist);
  856. if (!list_empty(&ep->rdllist)) {
  857. /*
  858. * Wake up (if active) both the eventpoll wait list and the ->poll()
  859. * wait list (delayed after we release the lock).
  860. */
  861. if (waitqueue_active(&ep->wq))
  862. wake_up_locked(&ep->wq);
  863. if (waitqueue_active(&ep->poll_wait))
  864. pwake++;
  865. }
  866. spin_unlock_irqrestore(&ep->lock, flags);
  867. mutex_unlock(&ep->mtx);
  868. /* We have to call this outside the lock */
  869. if (pwake)
  870. ep_poll_safewake(&psw, &ep->poll_wait);
  871. return eventcnt == 0 ? error: eventcnt;
  872. }
  873. static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
  874. int maxevents, long timeout)
  875. {
  876. int res, eavail;
  877. unsigned long flags;
  878. long jtimeout;
  879. wait_queue_t wait;
  880. /*
  881. * Calculate the timeout by checking for the "infinite" value ( -1 )
  882. * and the overflow condition. The passed timeout is in milliseconds,
  883. * that why (t * HZ) / 1000.
  884. */
  885. jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?
  886. MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
  887. retry:
  888. spin_lock_irqsave(&ep->lock, flags);
  889. res = 0;
  890. if (list_empty(&ep->rdllist)) {
  891. /*
  892. * We don't have any available event to return to the caller.
  893. * We need to sleep here, and we will be wake up by
  894. * ep_poll_callback() when events will become available.
  895. */
  896. init_waitqueue_entry(&wait, current);
  897. wait.flags |= WQ_FLAG_EXCLUSIVE;
  898. __add_wait_queue(&ep->wq, &wait);
  899. for (;;) {
  900. /*
  901. * We don't want to sleep if the ep_poll_callback() sends us
  902. * a wakeup in between. That's why we set the task state
  903. * to TASK_INTERRUPTIBLE before doing the checks.
  904. */
  905. set_current_state(TASK_INTERRUPTIBLE);
  906. if (!list_empty(&ep->rdllist) || !jtimeout)
  907. break;
  908. if (signal_pending(current)) {
  909. res = -EINTR;
  910. break;
  911. }
  912. spin_unlock_irqrestore(&ep->lock, flags);
  913. jtimeout = schedule_timeout(jtimeout);
  914. spin_lock_irqsave(&ep->lock, flags);
  915. }
  916. __remove_wait_queue(&ep->wq, &wait);
  917. set_current_state(TASK_RUNNING);
  918. }
  919. /* Is it worth to try to dig for events ? */
  920. eavail = !list_empty(&ep->rdllist);
  921. spin_unlock_irqrestore(&ep->lock, flags);
  922. /*
  923. * Try to transfer events to user space. In case we get 0 events and
  924. * there's still timeout left over, we go trying again in search of
  925. * more luck.
  926. */
  927. if (!res && eavail &&
  928. !(res = ep_send_events(ep, events, maxevents)) && jtimeout)
  929. goto retry;
  930. return res;
  931. }
  932. /*
  933. * Open an eventpoll file descriptor.
  934. */
  935. asmlinkage long sys_epoll_create1(int flags)
  936. {
  937. int error, fd = -1;
  938. struct eventpoll *ep;
  939. /* Check the EPOLL_* constant for consistency. */
  940. BUILD_BUG_ON(EPOLL_CLOEXEC != O_CLOEXEC);
  941. if (flags & ~EPOLL_CLOEXEC)
  942. return -EINVAL;
  943. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
  944. current, flags));
  945. /*
  946. * Create the internal data structure ( "struct eventpoll" ).
  947. */
  948. error = ep_alloc(&ep);
  949. if (error < 0) {
  950. fd = error;
  951. goto error_return;
  952. }
  953. /*
  954. * Creates all the items needed to setup an eventpoll file. That is,
  955. * a file structure and a free file descriptor.
  956. */
  957. fd = anon_inode_getfd("[eventpoll]", &eventpoll_fops, ep,
  958. flags & O_CLOEXEC);
  959. if (fd < 0)
  960. ep_free(ep);
  961. atomic_inc(&ep->user->epoll_devs);
  962. error_return:
  963. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
  964. current, flags, fd));
  965. return fd;
  966. }
  967. asmlinkage long sys_epoll_create(int size)
  968. {
  969. if (size < 0)
  970. return -EINVAL;
  971. return sys_epoll_create1(0);
  972. }
  973. /*
  974. * The following function implements the controller interface for
  975. * the eventpoll file that enables the insertion/removal/change of
  976. * file descriptors inside the interest set.
  977. */
  978. asmlinkage long sys_epoll_ctl(int epfd, int op, int fd,
  979. struct epoll_event __user *event)
  980. {
  981. int error;
  982. struct file *file, *tfile;
  983. struct eventpoll *ep;
  984. struct epitem *epi;
  985. struct epoll_event epds;
  986. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n",
  987. current, epfd, op, fd, event));
  988. error = -EFAULT;
  989. if (ep_op_has_event(op) &&
  990. copy_from_user(&epds, event, sizeof(struct epoll_event)))
  991. goto error_return;
  992. /* Get the "struct file *" for the eventpoll file */
  993. error = -EBADF;
  994. file = fget(epfd);
  995. if (!file)
  996. goto error_return;
  997. /* Get the "struct file *" for the target file */
  998. tfile = fget(fd);
  999. if (!tfile)
  1000. goto error_fput;
  1001. /* The target file descriptor must support poll */
  1002. error = -EPERM;
  1003. if (!tfile->f_op || !tfile->f_op->poll)
  1004. goto error_tgt_fput;
  1005. /*
  1006. * We have to check that the file structure underneath the file descriptor
  1007. * the user passed to us _is_ an eventpoll file. And also we do not permit
  1008. * adding an epoll file descriptor inside itself.
  1009. */
  1010. error = -EINVAL;
  1011. if (file == tfile || !is_file_epoll(file))
  1012. goto error_tgt_fput;
  1013. /*
  1014. * At this point it is safe to assume that the "private_data" contains
  1015. * our own data structure.
  1016. */
  1017. ep = file->private_data;
  1018. mutex_lock(&ep->mtx);
  1019. /*
  1020. * Try to lookup the file inside our RB tree, Since we grabbed "mtx"
  1021. * above, we can be sure to be able to use the item looked up by
  1022. * ep_find() till we release the mutex.
  1023. */
  1024. epi = ep_find(ep, tfile, fd);
  1025. error = -EINVAL;
  1026. switch (op) {
  1027. case EPOLL_CTL_ADD:
  1028. if (!epi) {
  1029. epds.events |= POLLERR | POLLHUP;
  1030. error = ep_insert(ep, &epds, tfile, fd);
  1031. } else
  1032. error = -EEXIST;
  1033. break;
  1034. case EPOLL_CTL_DEL:
  1035. if (epi)
  1036. error = ep_remove(ep, epi);
  1037. else
  1038. error = -ENOENT;
  1039. break;
  1040. case EPOLL_CTL_MOD:
  1041. if (epi) {
  1042. epds.events |= POLLERR | POLLHUP;
  1043. error = ep_modify(ep, epi, &epds);
  1044. } else
  1045. error = -ENOENT;
  1046. break;
  1047. }
  1048. mutex_unlock(&ep->mtx);
  1049. error_tgt_fput:
  1050. fput(tfile);
  1051. error_fput:
  1052. fput(file);
  1053. error_return:
  1054. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n",
  1055. current, epfd, op, fd, event, error));
  1056. return error;
  1057. }
  1058. /*
  1059. * Implement the event wait interface for the eventpoll file. It is the kernel
  1060. * part of the user space epoll_wait(2).
  1061. */
  1062. asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
  1063. int maxevents, int timeout)
  1064. {
  1065. int error;
  1066. struct file *file;
  1067. struct eventpoll *ep;
  1068. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n",
  1069. current, epfd, events, maxevents, timeout));
  1070. /* The maximum number of event must be greater than zero */
  1071. if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
  1072. return -EINVAL;
  1073. /* Verify that the area passed by the user is writeable */
  1074. if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
  1075. error = -EFAULT;
  1076. goto error_return;
  1077. }
  1078. /* Get the "struct file *" for the eventpoll file */
  1079. error = -EBADF;
  1080. file = fget(epfd);
  1081. if (!file)
  1082. goto error_return;
  1083. /*
  1084. * We have to check that the file structure underneath the fd
  1085. * the user passed to us _is_ an eventpoll file.
  1086. */
  1087. error = -EINVAL;
  1088. if (!is_file_epoll(file))
  1089. goto error_fput;
  1090. /*
  1091. * At this point it is safe to assume that the "private_data" contains
  1092. * our own data structure.
  1093. */
  1094. ep = file->private_data;
  1095. /* Time to fish for events ... */
  1096. error = ep_poll(ep, events, maxevents, timeout);
  1097. error_fput:
  1098. fput(file);
  1099. error_return:
  1100. DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n",
  1101. current, epfd, events, maxevents, timeout, error));
  1102. return error;
  1103. }
  1104. #ifdef HAVE_SET_RESTORE_SIGMASK
  1105. /*
  1106. * Implement the event wait interface for the eventpoll file. It is the kernel
  1107. * part of the user space epoll_pwait(2).
  1108. */
  1109. asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events,
  1110. int maxevents, int timeout, const sigset_t __user *sigmask,
  1111. size_t sigsetsize)
  1112. {
  1113. int error;
  1114. sigset_t ksigmask, sigsaved;
  1115. /*
  1116. * If the caller wants a certain signal mask to be set during the wait,
  1117. * we apply it here.
  1118. */
  1119. if (sigmask) {
  1120. if (sigsetsize != sizeof(sigset_t))
  1121. return -EINVAL;
  1122. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  1123. return -EFAULT;
  1124. sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  1125. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1126. }
  1127. error = sys_epoll_wait(epfd, events, maxevents, timeout);
  1128. /*
  1129. * If we changed the signal mask, we need to restore the original one.
  1130. * In case we've got a signal while waiting, we do not restore the
  1131. * signal mask yet, and we allow do_signal() to deliver the signal on
  1132. * the way back to userspace, before the signal mask is restored.
  1133. */
  1134. if (sigmask) {
  1135. if (error == -EINTR) {
  1136. memcpy(&current->saved_sigmask, &sigsaved,
  1137. sizeof(sigsaved));
  1138. set_restore_sigmask();
  1139. } else
  1140. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1141. }
  1142. return error;
  1143. }
  1144. #endif /* HAVE_SET_RESTORE_SIGMASK */
  1145. static int __init eventpoll_init(void)
  1146. {
  1147. struct sysinfo si;
  1148. si_meminfo(&si);
  1149. max_user_instances = 128;
  1150. max_user_watches = (((si.totalram - si.totalhigh) / 32) << PAGE_SHIFT) /
  1151. EP_ITEM_COST;
  1152. /* Initialize the structure used to perform safe poll wait head wake ups */
  1153. ep_poll_safewake_init(&psw);
  1154. /* Allocates slab cache used to allocate "struct epitem" items */
  1155. epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
  1156. 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC,
  1157. NULL);
  1158. /* Allocates slab cache used to allocate "struct eppoll_entry" */
  1159. pwq_cache = kmem_cache_create("eventpoll_pwq",
  1160. sizeof(struct eppoll_entry), 0,
  1161. EPI_SLAB_DEBUG|SLAB_PANIC, NULL);
  1162. return 0;
  1163. }
  1164. fs_initcall(eventpoll_init);