select.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * This file contains the procedures for the handling of select and poll
  3. *
  4. * Created for Linux based loosely upon Mathius Lattner's minix
  5. * patches by Peter MacDonald. Heavily edited by Linus.
  6. *
  7. * 4 February 1994
  8. * COFF/ELF binary emulation. If the process has the STICKY_TIMEOUTS
  9. * flag set in its personality we do *not* modify the given timeout
  10. * parameter to reflect time remaining.
  11. *
  12. * 24 January 2000
  13. * Changed sys_poll()/do_poll() to use PAGE_SIZE chunk-based allocation
  14. * of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian).
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/poll.h>
  21. #include <linux/personality.h> /* for STICKY_TIMEOUTS */
  22. #include <linux/file.h>
  23. #include <linux/fdtable.h>
  24. #include <linux/fs.h>
  25. #include <linux/rcupdate.h>
  26. #include <linux/hrtimer.h>
  27. #include <asm/uaccess.h>
  28. /*
  29. * Estimate expected accuracy in ns from a timeval.
  30. *
  31. * After quite a bit of churning around, we've settled on
  32. * a simple thing of taking 0.1% of the timeout as the
  33. * slack, with a cap of 100 msec.
  34. * "nice" tasks get a 0.5% slack instead.
  35. *
  36. * Consider this comment an open invitation to come up with even
  37. * better solutions..
  38. */
  39. static long __estimate_accuracy(struct timespec *tv)
  40. {
  41. long slack;
  42. int divfactor = 1000;
  43. if (task_nice(current) > 0)
  44. divfactor = divfactor / 5;
  45. slack = tv->tv_nsec / divfactor;
  46. slack += tv->tv_sec * (NSEC_PER_SEC/divfactor);
  47. if (slack > 100 * NSEC_PER_MSEC)
  48. slack = 100 * NSEC_PER_MSEC;
  49. if (slack < 0)
  50. slack = 0;
  51. return slack;
  52. }
  53. static long estimate_accuracy(struct timespec *tv)
  54. {
  55. unsigned long ret;
  56. struct timespec now;
  57. /*
  58. * Realtime tasks get a slack of 0 for obvious reasons.
  59. */
  60. if (rt_task(current))
  61. return 0;
  62. ktime_get_ts(&now);
  63. now = timespec_sub(*tv, now);
  64. ret = __estimate_accuracy(&now);
  65. if (ret < current->timer_slack_ns)
  66. return current->timer_slack_ns;
  67. return ret;
  68. }
  69. struct poll_table_page {
  70. struct poll_table_page * next;
  71. struct poll_table_entry * entry;
  72. struct poll_table_entry entries[0];
  73. };
  74. #define POLL_TABLE_FULL(table) \
  75. ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
  76. /*
  77. * Ok, Peter made a complicated, but straightforward multiple_wait() function.
  78. * I have rewritten this, taking some shortcuts: This code may not be easy to
  79. * follow, but it should be free of race-conditions, and it's practical. If you
  80. * understand what I'm doing here, then you understand how the linux
  81. * sleep/wakeup mechanism works.
  82. *
  83. * Two very simple procedures, poll_wait() and poll_freewait() make all the
  84. * work. poll_wait() is an inline-function defined in <linux/poll.h>,
  85. * as all select/poll functions have to call it to add an entry to the
  86. * poll table.
  87. */
  88. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  89. poll_table *p);
  90. void poll_initwait(struct poll_wqueues *pwq)
  91. {
  92. init_poll_funcptr(&pwq->pt, __pollwait);
  93. pwq->error = 0;
  94. pwq->table = NULL;
  95. pwq->inline_index = 0;
  96. }
  97. EXPORT_SYMBOL(poll_initwait);
  98. static void free_poll_entry(struct poll_table_entry *entry)
  99. {
  100. remove_wait_queue(entry->wait_address, &entry->wait);
  101. fput(entry->filp);
  102. }
  103. void poll_freewait(struct poll_wqueues *pwq)
  104. {
  105. struct poll_table_page * p = pwq->table;
  106. int i;
  107. for (i = 0; i < pwq->inline_index; i++)
  108. free_poll_entry(pwq->inline_entries + i);
  109. while (p) {
  110. struct poll_table_entry * entry;
  111. struct poll_table_page *old;
  112. entry = p->entry;
  113. do {
  114. entry--;
  115. free_poll_entry(entry);
  116. } while (entry > p->entries);
  117. old = p;
  118. p = p->next;
  119. free_page((unsigned long) old);
  120. }
  121. }
  122. EXPORT_SYMBOL(poll_freewait);
  123. static struct poll_table_entry *poll_get_entry(poll_table *_p)
  124. {
  125. struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
  126. struct poll_table_page *table = p->table;
  127. if (p->inline_index < N_INLINE_POLL_ENTRIES)
  128. return p->inline_entries + p->inline_index++;
  129. if (!table || POLL_TABLE_FULL(table)) {
  130. struct poll_table_page *new_table;
  131. new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
  132. if (!new_table) {
  133. p->error = -ENOMEM;
  134. __set_current_state(TASK_RUNNING);
  135. return NULL;
  136. }
  137. new_table->entry = new_table->entries;
  138. new_table->next = table;
  139. p->table = new_table;
  140. table = new_table;
  141. }
  142. return table->entry++;
  143. }
  144. /* Add a new entry */
  145. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  146. poll_table *p)
  147. {
  148. struct poll_table_entry *entry = poll_get_entry(p);
  149. if (!entry)
  150. return;
  151. get_file(filp);
  152. entry->filp = filp;
  153. entry->wait_address = wait_address;
  154. init_waitqueue_entry(&entry->wait, current);
  155. add_wait_queue(wait_address, &entry->wait);
  156. }
  157. /**
  158. * poll_select_set_timeout - helper function to setup the timeout value
  159. * @to: pointer to timespec variable for the final timeout
  160. * @sec: seconds (from user space)
  161. * @nsec: nanoseconds (from user space)
  162. *
  163. * Note, we do not use a timespec for the user space value here, That
  164. * way we can use the function for timeval and compat interfaces as well.
  165. *
  166. * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0.
  167. */
  168. int poll_select_set_timeout(struct timespec *to, long sec, long nsec)
  169. {
  170. struct timespec ts = {.tv_sec = sec, .tv_nsec = nsec};
  171. if (!timespec_valid(&ts))
  172. return -EINVAL;
  173. /* Optimize for the zero timeout value here */
  174. if (!sec && !nsec) {
  175. to->tv_sec = to->tv_nsec = 0;
  176. } else {
  177. ktime_get_ts(to);
  178. *to = timespec_add_safe(*to, ts);
  179. }
  180. return 0;
  181. }
  182. static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
  183. int timeval, int ret)
  184. {
  185. struct timespec rts;
  186. struct timeval rtv;
  187. if (!p)
  188. return ret;
  189. if (current->personality & STICKY_TIMEOUTS)
  190. goto sticky;
  191. /* No update for zero timeout */
  192. if (!end_time->tv_sec && !end_time->tv_nsec)
  193. return ret;
  194. ktime_get_ts(&rts);
  195. rts = timespec_sub(*end_time, rts);
  196. if (rts.tv_sec < 0)
  197. rts.tv_sec = rts.tv_nsec = 0;
  198. if (timeval) {
  199. rtv.tv_sec = rts.tv_sec;
  200. rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
  201. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  202. return ret;
  203. } else if (!copy_to_user(p, &rts, sizeof(rts)))
  204. return ret;
  205. /*
  206. * If an application puts its timeval in read-only memory, we
  207. * don't want the Linux-specific update to the timeval to
  208. * cause a fault after the select has completed
  209. * successfully. However, because we're not updating the
  210. * timeval, we can't restart the system call.
  211. */
  212. sticky:
  213. if (ret == -ERESTARTNOHAND)
  214. ret = -EINTR;
  215. return ret;
  216. }
  217. #define FDS_IN(fds, n) (fds->in + n)
  218. #define FDS_OUT(fds, n) (fds->out + n)
  219. #define FDS_EX(fds, n) (fds->ex + n)
  220. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  221. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  222. {
  223. unsigned long *open_fds;
  224. unsigned long set;
  225. int max;
  226. struct fdtable *fdt;
  227. /* handle last in-complete long-word first */
  228. set = ~(~0UL << (n & (__NFDBITS-1)));
  229. n /= __NFDBITS;
  230. fdt = files_fdtable(current->files);
  231. open_fds = fdt->open_fds->fds_bits+n;
  232. max = 0;
  233. if (set) {
  234. set &= BITS(fds, n);
  235. if (set) {
  236. if (!(set & ~*open_fds))
  237. goto get_max;
  238. return -EBADF;
  239. }
  240. }
  241. while (n) {
  242. open_fds--;
  243. n--;
  244. set = BITS(fds, n);
  245. if (!set)
  246. continue;
  247. if (set & ~*open_fds)
  248. return -EBADF;
  249. if (max)
  250. continue;
  251. get_max:
  252. do {
  253. max++;
  254. set >>= 1;
  255. } while (set);
  256. max += n * __NFDBITS;
  257. }
  258. return max;
  259. }
  260. #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
  261. #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
  262. #define POLLEX_SET (POLLPRI)
  263. int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
  264. {
  265. ktime_t expire, *to = NULL;
  266. struct poll_wqueues table;
  267. poll_table *wait;
  268. int retval, i, timed_out = 0;
  269. unsigned long slack = 0;
  270. rcu_read_lock();
  271. retval = max_select_fd(n, fds);
  272. rcu_read_unlock();
  273. if (retval < 0)
  274. return retval;
  275. n = retval;
  276. poll_initwait(&table);
  277. wait = &table.pt;
  278. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  279. wait = NULL;
  280. timed_out = 1;
  281. }
  282. if (end_time && !timed_out)
  283. slack = estimate_accuracy(end_time);
  284. retval = 0;
  285. for (;;) {
  286. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  287. set_current_state(TASK_INTERRUPTIBLE);
  288. inp = fds->in; outp = fds->out; exp = fds->ex;
  289. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  290. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  291. unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  292. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  293. const struct file_operations *f_op = NULL;
  294. struct file *file = NULL;
  295. in = *inp++; out = *outp++; ex = *exp++;
  296. all_bits = in | out | ex;
  297. if (all_bits == 0) {
  298. i += __NFDBITS;
  299. continue;
  300. }
  301. for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
  302. int fput_needed;
  303. if (i >= n)
  304. break;
  305. if (!(bit & all_bits))
  306. continue;
  307. file = fget_light(i, &fput_needed);
  308. if (file) {
  309. f_op = file->f_op;
  310. mask = DEFAULT_POLLMASK;
  311. if (f_op && f_op->poll)
  312. mask = (*f_op->poll)(file, retval ? NULL : wait);
  313. fput_light(file, fput_needed);
  314. if ((mask & POLLIN_SET) && (in & bit)) {
  315. res_in |= bit;
  316. retval++;
  317. }
  318. if ((mask & POLLOUT_SET) && (out & bit)) {
  319. res_out |= bit;
  320. retval++;
  321. }
  322. if ((mask & POLLEX_SET) && (ex & bit)) {
  323. res_ex |= bit;
  324. retval++;
  325. }
  326. }
  327. }
  328. if (res_in)
  329. *rinp = res_in;
  330. if (res_out)
  331. *routp = res_out;
  332. if (res_ex)
  333. *rexp = res_ex;
  334. cond_resched();
  335. }
  336. wait = NULL;
  337. if (retval || timed_out || signal_pending(current))
  338. break;
  339. if (table.error) {
  340. retval = table.error;
  341. break;
  342. }
  343. /*
  344. * If this is the first loop and we have a timeout
  345. * given, then we convert to ktime_t and set the to
  346. * pointer to the expiry value.
  347. */
  348. if (end_time && !to) {
  349. expire = timespec_to_ktime(*end_time);
  350. to = &expire;
  351. }
  352. if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS))
  353. timed_out = 1;
  354. }
  355. __set_current_state(TASK_RUNNING);
  356. poll_freewait(&table);
  357. return retval;
  358. }
  359. /*
  360. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  361. * like to be certain this leads to no problems. So I return
  362. * EINTR just for safety.
  363. *
  364. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  365. * I'm trying ERESTARTNOHAND which restart only when you want to.
  366. */
  367. #define MAX_SELECT_SECONDS \
  368. ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
  369. int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  370. fd_set __user *exp, struct timespec *end_time)
  371. {
  372. fd_set_bits fds;
  373. void *bits;
  374. int ret, max_fds;
  375. unsigned int size;
  376. struct fdtable *fdt;
  377. /* Allocate small arguments on the stack to save memory and be faster */
  378. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  379. ret = -EINVAL;
  380. if (n < 0)
  381. goto out_nofds;
  382. /* max_fds can increase, so grab it once to avoid race */
  383. rcu_read_lock();
  384. fdt = files_fdtable(current->files);
  385. max_fds = fdt->max_fds;
  386. rcu_read_unlock();
  387. if (n > max_fds)
  388. n = max_fds;
  389. /*
  390. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  391. * since we used fdset we need to allocate memory in units of
  392. * long-words.
  393. */
  394. size = FDS_BYTES(n);
  395. bits = stack_fds;
  396. if (size > sizeof(stack_fds) / 6) {
  397. /* Not enough space in on-stack array; must use kmalloc */
  398. ret = -ENOMEM;
  399. bits = kmalloc(6 * size, GFP_KERNEL);
  400. if (!bits)
  401. goto out_nofds;
  402. }
  403. fds.in = bits;
  404. fds.out = bits + size;
  405. fds.ex = bits + 2*size;
  406. fds.res_in = bits + 3*size;
  407. fds.res_out = bits + 4*size;
  408. fds.res_ex = bits + 5*size;
  409. if ((ret = get_fd_set(n, inp, fds.in)) ||
  410. (ret = get_fd_set(n, outp, fds.out)) ||
  411. (ret = get_fd_set(n, exp, fds.ex)))
  412. goto out;
  413. zero_fd_set(n, fds.res_in);
  414. zero_fd_set(n, fds.res_out);
  415. zero_fd_set(n, fds.res_ex);
  416. ret = do_select(n, &fds, end_time);
  417. if (ret < 0)
  418. goto out;
  419. if (!ret) {
  420. ret = -ERESTARTNOHAND;
  421. if (signal_pending(current))
  422. goto out;
  423. ret = 0;
  424. }
  425. if (set_fd_set(n, inp, fds.res_in) ||
  426. set_fd_set(n, outp, fds.res_out) ||
  427. set_fd_set(n, exp, fds.res_ex))
  428. ret = -EFAULT;
  429. out:
  430. if (bits != stack_fds)
  431. kfree(bits);
  432. out_nofds:
  433. return ret;
  434. }
  435. asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  436. fd_set __user *exp, struct timeval __user *tvp)
  437. {
  438. struct timespec end_time, *to = NULL;
  439. struct timeval tv;
  440. int ret;
  441. if (tvp) {
  442. if (copy_from_user(&tv, tvp, sizeof(tv)))
  443. return -EFAULT;
  444. to = &end_time;
  445. if (poll_select_set_timeout(to,
  446. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  447. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  448. return -EINVAL;
  449. }
  450. ret = core_sys_select(n, inp, outp, exp, to);
  451. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  452. return ret;
  453. }
  454. #ifdef HAVE_SET_RESTORE_SIGMASK
  455. asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
  456. fd_set __user *exp, struct timespec __user *tsp,
  457. const sigset_t __user *sigmask, size_t sigsetsize)
  458. {
  459. sigset_t ksigmask, sigsaved;
  460. struct timespec ts, end_time, *to = NULL;
  461. int ret;
  462. if (tsp) {
  463. if (copy_from_user(&ts, tsp, sizeof(ts)))
  464. return -EFAULT;
  465. to = &end_time;
  466. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  467. return -EINVAL;
  468. }
  469. if (sigmask) {
  470. /* XXX: Don't preclude handling different sized sigset_t's. */
  471. if (sigsetsize != sizeof(sigset_t))
  472. return -EINVAL;
  473. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  474. return -EFAULT;
  475. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  476. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  477. }
  478. ret = core_sys_select(n, inp, outp, exp, &end_time);
  479. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  480. if (ret == -ERESTARTNOHAND) {
  481. /*
  482. * Don't restore the signal mask yet. Let do_signal() deliver
  483. * the signal on the way back to userspace, before the signal
  484. * mask is restored.
  485. */
  486. if (sigmask) {
  487. memcpy(&current->saved_sigmask, &sigsaved,
  488. sizeof(sigsaved));
  489. set_restore_sigmask();
  490. }
  491. } else if (sigmask)
  492. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  493. return ret;
  494. }
  495. /*
  496. * Most architectures can't handle 7-argument syscalls. So we provide a
  497. * 6-argument version where the sixth argument is a pointer to a structure
  498. * which has a pointer to the sigset_t itself followed by a size_t containing
  499. * the sigset size.
  500. */
  501. asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
  502. fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
  503. {
  504. size_t sigsetsize = 0;
  505. sigset_t __user *up = NULL;
  506. if (sig) {
  507. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  508. || __get_user(up, (sigset_t __user * __user *)sig)
  509. || __get_user(sigsetsize,
  510. (size_t __user *)(sig+sizeof(void *))))
  511. return -EFAULT;
  512. }
  513. return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
  514. }
  515. #endif /* HAVE_SET_RESTORE_SIGMASK */
  516. struct poll_list {
  517. struct poll_list *next;
  518. int len;
  519. struct pollfd entries[0];
  520. };
  521. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  522. /*
  523. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  524. * interested in events matching the pollfd->events mask, and the result
  525. * matching that mask is both recorded in pollfd->revents and returned. The
  526. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  527. * if non-NULL.
  528. */
  529. static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
  530. {
  531. unsigned int mask;
  532. int fd;
  533. mask = 0;
  534. fd = pollfd->fd;
  535. if (fd >= 0) {
  536. int fput_needed;
  537. struct file * file;
  538. file = fget_light(fd, &fput_needed);
  539. mask = POLLNVAL;
  540. if (file != NULL) {
  541. mask = DEFAULT_POLLMASK;
  542. if (file->f_op && file->f_op->poll)
  543. mask = file->f_op->poll(file, pwait);
  544. /* Mask out unneeded events. */
  545. mask &= pollfd->events | POLLERR | POLLHUP;
  546. fput_light(file, fput_needed);
  547. }
  548. }
  549. pollfd->revents = mask;
  550. return mask;
  551. }
  552. static int do_poll(unsigned int nfds, struct poll_list *list,
  553. struct poll_wqueues *wait, struct timespec *end_time)
  554. {
  555. poll_table* pt = &wait->pt;
  556. ktime_t expire, *to = NULL;
  557. int timed_out = 0, count = 0;
  558. unsigned long slack = 0;
  559. /* Optimise the no-wait case */
  560. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  561. pt = NULL;
  562. timed_out = 1;
  563. }
  564. if (end_time && !timed_out)
  565. slack = estimate_accuracy(end_time);
  566. for (;;) {
  567. struct poll_list *walk;
  568. set_current_state(TASK_INTERRUPTIBLE);
  569. for (walk = list; walk != NULL; walk = walk->next) {
  570. struct pollfd * pfd, * pfd_end;
  571. pfd = walk->entries;
  572. pfd_end = pfd + walk->len;
  573. for (; pfd != pfd_end; pfd++) {
  574. /*
  575. * Fish for events. If we found one, record it
  576. * and kill the poll_table, so we don't
  577. * needlessly register any other waiters after
  578. * this. They'll get immediately deregistered
  579. * when we break out and return.
  580. */
  581. if (do_pollfd(pfd, pt)) {
  582. count++;
  583. pt = NULL;
  584. }
  585. }
  586. }
  587. /*
  588. * All waiters have already been registered, so don't provide
  589. * a poll_table to them on the next loop iteration.
  590. */
  591. pt = NULL;
  592. if (!count) {
  593. count = wait->error;
  594. if (signal_pending(current))
  595. count = -EINTR;
  596. }
  597. if (count || timed_out)
  598. break;
  599. /*
  600. * If this is the first loop and we have a timeout
  601. * given, then we convert to ktime_t and set the to
  602. * pointer to the expiry value.
  603. */
  604. if (end_time && !to) {
  605. expire = timespec_to_ktime(*end_time);
  606. to = &expire;
  607. }
  608. if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS))
  609. timed_out = 1;
  610. }
  611. __set_current_state(TASK_RUNNING);
  612. return count;
  613. }
  614. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  615. sizeof(struct pollfd))
  616. int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  617. struct timespec *end_time)
  618. {
  619. struct poll_wqueues table;
  620. int err = -EFAULT, fdcount, len, size;
  621. /* Allocate small arguments on the stack to save memory and be
  622. faster - use long to make sure the buffer is aligned properly
  623. on 64 bit archs to avoid unaligned access */
  624. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  625. struct poll_list *const head = (struct poll_list *)stack_pps;
  626. struct poll_list *walk = head;
  627. unsigned long todo = nfds;
  628. if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  629. return -EINVAL;
  630. len = min_t(unsigned int, nfds, N_STACK_PPS);
  631. for (;;) {
  632. walk->next = NULL;
  633. walk->len = len;
  634. if (!len)
  635. break;
  636. if (copy_from_user(walk->entries, ufds + nfds-todo,
  637. sizeof(struct pollfd) * walk->len))
  638. goto out_fds;
  639. todo -= walk->len;
  640. if (!todo)
  641. break;
  642. len = min(todo, POLLFD_PER_PAGE);
  643. size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
  644. walk = walk->next = kmalloc(size, GFP_KERNEL);
  645. if (!walk) {
  646. err = -ENOMEM;
  647. goto out_fds;
  648. }
  649. }
  650. poll_initwait(&table);
  651. fdcount = do_poll(nfds, head, &table, end_time);
  652. poll_freewait(&table);
  653. for (walk = head; walk; walk = walk->next) {
  654. struct pollfd *fds = walk->entries;
  655. int j;
  656. for (j = 0; j < walk->len; j++, ufds++)
  657. if (__put_user(fds[j].revents, &ufds->revents))
  658. goto out_fds;
  659. }
  660. err = fdcount;
  661. out_fds:
  662. walk = head->next;
  663. while (walk) {
  664. struct poll_list *pos = walk;
  665. walk = walk->next;
  666. kfree(pos);
  667. }
  668. return err;
  669. }
  670. static long do_restart_poll(struct restart_block *restart_block)
  671. {
  672. struct pollfd __user *ufds = restart_block->poll.ufds;
  673. int nfds = restart_block->poll.nfds;
  674. struct timespec *to = NULL, end_time;
  675. int ret;
  676. if (restart_block->poll.has_timeout) {
  677. end_time.tv_sec = restart_block->poll.tv_sec;
  678. end_time.tv_nsec = restart_block->poll.tv_nsec;
  679. to = &end_time;
  680. }
  681. ret = do_sys_poll(ufds, nfds, to);
  682. if (ret == -EINTR) {
  683. restart_block->fn = do_restart_poll;
  684. ret = -ERESTART_RESTARTBLOCK;
  685. }
  686. return ret;
  687. }
  688. asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  689. long timeout_msecs)
  690. {
  691. struct timespec end_time, *to = NULL;
  692. int ret;
  693. if (timeout_msecs >= 0) {
  694. to = &end_time;
  695. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  696. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  697. }
  698. ret = do_sys_poll(ufds, nfds, to);
  699. if (ret == -EINTR) {
  700. struct restart_block *restart_block;
  701. restart_block = &current_thread_info()->restart_block;
  702. restart_block->fn = do_restart_poll;
  703. restart_block->poll.ufds = ufds;
  704. restart_block->poll.nfds = nfds;
  705. if (timeout_msecs >= 0) {
  706. restart_block->poll.tv_sec = end_time.tv_sec;
  707. restart_block->poll.tv_nsec = end_time.tv_nsec;
  708. restart_block->poll.has_timeout = 1;
  709. } else
  710. restart_block->poll.has_timeout = 0;
  711. ret = -ERESTART_RESTARTBLOCK;
  712. }
  713. return ret;
  714. }
  715. #ifdef HAVE_SET_RESTORE_SIGMASK
  716. asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
  717. struct timespec __user *tsp, const sigset_t __user *sigmask,
  718. size_t sigsetsize)
  719. {
  720. sigset_t ksigmask, sigsaved;
  721. struct timespec ts, end_time, *to = NULL;
  722. int ret;
  723. if (tsp) {
  724. if (copy_from_user(&ts, tsp, sizeof(ts)))
  725. return -EFAULT;
  726. to = &end_time;
  727. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  728. return -EINVAL;
  729. }
  730. if (sigmask) {
  731. /* XXX: Don't preclude handling different sized sigset_t's. */
  732. if (sigsetsize != sizeof(sigset_t))
  733. return -EINVAL;
  734. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  735. return -EFAULT;
  736. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  737. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  738. }
  739. ret = do_sys_poll(ufds, nfds, to);
  740. /* We can restart this syscall, usually */
  741. if (ret == -EINTR) {
  742. /*
  743. * Don't restore the signal mask yet. Let do_signal() deliver
  744. * the signal on the way back to userspace, before the signal
  745. * mask is restored.
  746. */
  747. if (sigmask) {
  748. memcpy(&current->saved_sigmask, &sigsaved,
  749. sizeof(sigsaved));
  750. set_restore_sigmask();
  751. }
  752. ret = -ERESTARTNOHAND;
  753. } else if (sigmask)
  754. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  755. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  756. return ret;
  757. }
  758. #endif /* HAVE_SET_RESTORE_SIGMASK */