select.c 26 KB

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