select.c 21 KB

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