select.c 21 KB

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