select.c 21 KB

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