select.c 25 KB

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