select.c 24 KB

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