select.c 23 KB

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