select.c 26 KB

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