select.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 timespec *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. long select_estimate_accuracy(struct timespec *tv)
  62. {
  63. unsigned long ret;
  64. struct timespec 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_ts(&now);
  71. now = timespec_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 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->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. size_t size, alloc_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. if (size > (SIZE_MAX / 6))
  501. goto out_nofds;
  502. alloc_size = 6 * size;
  503. bits = kmalloc(alloc_size, GFP_KERNEL|__GFP_NOWARN);
  504. if (!bits && alloc_size > PAGE_SIZE)
  505. bits = vmalloc(alloc_size);
  506. if (!bits)
  507. goto out_nofds;
  508. }
  509. fds.in = bits;
  510. fds.out = bits + size;
  511. fds.ex = bits + 2*size;
  512. fds.res_in = bits + 3*size;
  513. fds.res_out = bits + 4*size;
  514. fds.res_ex = bits + 5*size;
  515. if ((ret = get_fd_set(n, inp, fds.in)) ||
  516. (ret = get_fd_set(n, outp, fds.out)) ||
  517. (ret = get_fd_set(n, exp, fds.ex)))
  518. goto out;
  519. zero_fd_set(n, fds.res_in);
  520. zero_fd_set(n, fds.res_out);
  521. zero_fd_set(n, fds.res_ex);
  522. ret = do_select(n, &fds, end_time);
  523. if (ret < 0)
  524. goto out;
  525. if (!ret) {
  526. ret = -ERESTARTNOHAND;
  527. if (signal_pending(current))
  528. goto out;
  529. ret = 0;
  530. }
  531. if (set_fd_set(n, inp, fds.res_in) ||
  532. set_fd_set(n, outp, fds.res_out) ||
  533. set_fd_set(n, exp, fds.res_ex))
  534. ret = -EFAULT;
  535. out:
  536. if (bits != stack_fds)
  537. kvfree(bits);
  538. out_nofds:
  539. return ret;
  540. }
  541. SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
  542. fd_set __user *, exp, struct timeval __user *, tvp)
  543. {
  544. struct timespec end_time, *to = NULL;
  545. struct timeval tv;
  546. int ret;
  547. if (tvp) {
  548. if (copy_from_user(&tv, tvp, sizeof(tv)))
  549. return -EFAULT;
  550. to = &end_time;
  551. if (poll_select_set_timeout(to,
  552. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  553. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  554. return -EINVAL;
  555. }
  556. ret = core_sys_select(n, inp, outp, exp, to);
  557. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  558. return ret;
  559. }
  560. static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
  561. fd_set __user *exp, struct timespec __user *tsp,
  562. const sigset_t __user *sigmask, size_t sigsetsize)
  563. {
  564. sigset_t ksigmask, sigsaved;
  565. struct timespec ts, end_time, *to = NULL;
  566. int ret;
  567. if (tsp) {
  568. if (copy_from_user(&ts, tsp, sizeof(ts)))
  569. return -EFAULT;
  570. to = &end_time;
  571. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  572. return -EINVAL;
  573. }
  574. if (sigmask) {
  575. /* XXX: Don't preclude handling different sized sigset_t's. */
  576. if (sigsetsize != sizeof(sigset_t))
  577. return -EINVAL;
  578. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  579. return -EFAULT;
  580. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  581. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  582. }
  583. ret = core_sys_select(n, inp, outp, exp, to);
  584. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  585. if (ret == -ERESTARTNOHAND) {
  586. /*
  587. * Don't restore the signal mask yet. Let do_signal() deliver
  588. * the signal on the way back to userspace, before the signal
  589. * mask is restored.
  590. */
  591. if (sigmask) {
  592. memcpy(&current->saved_sigmask, &sigsaved,
  593. sizeof(sigsaved));
  594. set_restore_sigmask();
  595. }
  596. } else if (sigmask)
  597. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  598. return ret;
  599. }
  600. /*
  601. * Most architectures can't handle 7-argument syscalls. So we provide a
  602. * 6-argument version where the sixth argument is a pointer to a structure
  603. * which has a pointer to the sigset_t itself followed by a size_t containing
  604. * the sigset size.
  605. */
  606. SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
  607. fd_set __user *, exp, struct timespec __user *, tsp,
  608. void __user *, sig)
  609. {
  610. size_t sigsetsize = 0;
  611. sigset_t __user *up = NULL;
  612. if (sig) {
  613. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  614. || __get_user(up, (sigset_t __user * __user *)sig)
  615. || __get_user(sigsetsize,
  616. (size_t __user *)(sig+sizeof(void *))))
  617. return -EFAULT;
  618. }
  619. return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
  620. }
  621. #ifdef __ARCH_WANT_SYS_OLD_SELECT
  622. struct sel_arg_struct {
  623. unsigned long n;
  624. fd_set __user *inp, *outp, *exp;
  625. struct timeval __user *tvp;
  626. };
  627. SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
  628. {
  629. struct sel_arg_struct a;
  630. if (copy_from_user(&a, arg, sizeof(a)))
  631. return -EFAULT;
  632. return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  633. }
  634. #endif
  635. struct poll_list {
  636. struct poll_list *next;
  637. int len;
  638. struct pollfd entries[0];
  639. };
  640. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  641. /*
  642. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  643. * interested in events matching the pollfd->events mask, and the result
  644. * matching that mask is both recorded in pollfd->revents and returned. The
  645. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  646. * if pwait->_qproc is non-NULL.
  647. */
  648. static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait,
  649. bool *can_busy_poll,
  650. unsigned int busy_flag)
  651. {
  652. unsigned int mask;
  653. int fd;
  654. mask = 0;
  655. fd = pollfd->fd;
  656. if (fd >= 0) {
  657. struct fd f = fdget(fd);
  658. mask = POLLNVAL;
  659. if (f.file) {
  660. mask = DEFAULT_POLLMASK;
  661. if (f.file->f_op->poll) {
  662. pwait->_key = pollfd->events|POLLERR|POLLHUP;
  663. pwait->_key |= busy_flag;
  664. mask = f.file->f_op->poll(f.file, pwait);
  665. if (mask & busy_flag)
  666. *can_busy_poll = true;
  667. }
  668. /* Mask out unneeded events. */
  669. mask &= pollfd->events | POLLERR | POLLHUP;
  670. fdput(f);
  671. }
  672. }
  673. pollfd->revents = mask;
  674. return mask;
  675. }
  676. static int do_poll(unsigned int nfds, struct poll_list *list,
  677. struct poll_wqueues *wait, struct timespec *end_time)
  678. {
  679. poll_table* pt = &wait->pt;
  680. ktime_t expire, *to = NULL;
  681. int timed_out = 0, count = 0;
  682. unsigned long slack = 0;
  683. unsigned int busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  684. unsigned long busy_end = 0;
  685. /* Optimise the no-wait case */
  686. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  687. pt->_qproc = NULL;
  688. timed_out = 1;
  689. }
  690. if (end_time && !timed_out)
  691. slack = select_estimate_accuracy(end_time);
  692. for (;;) {
  693. struct poll_list *walk;
  694. bool can_busy_loop = false;
  695. for (walk = list; walk != NULL; walk = walk->next) {
  696. struct pollfd * pfd, * pfd_end;
  697. pfd = walk->entries;
  698. pfd_end = pfd + walk->len;
  699. for (; pfd != pfd_end; pfd++) {
  700. /*
  701. * Fish for events. If we found one, record it
  702. * and kill poll_table->_qproc, so we don't
  703. * needlessly register any other waiters after
  704. * this. They'll get immediately deregistered
  705. * when we break out and return.
  706. */
  707. if (do_pollfd(pfd, pt, &can_busy_loop,
  708. busy_flag)) {
  709. count++;
  710. pt->_qproc = NULL;
  711. /* found something, stop busy polling */
  712. busy_flag = 0;
  713. can_busy_loop = false;
  714. }
  715. }
  716. }
  717. /*
  718. * All waiters have already been registered, so don't provide
  719. * a poll_table->_qproc to them on the next loop iteration.
  720. */
  721. pt->_qproc = NULL;
  722. if (!count) {
  723. count = wait->error;
  724. if (signal_pending(current))
  725. count = -EINTR;
  726. }
  727. if (count || timed_out)
  728. break;
  729. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  730. if (can_busy_loop && !need_resched()) {
  731. if (!busy_end) {
  732. busy_end = busy_loop_end_time();
  733. continue;
  734. }
  735. if (!busy_loop_timeout(busy_end))
  736. continue;
  737. }
  738. busy_flag = 0;
  739. /*
  740. * If this is the first loop and we have a timeout
  741. * given, then we convert to ktime_t and set the to
  742. * pointer to the expiry value.
  743. */
  744. if (end_time && !to) {
  745. expire = timespec_to_ktime(*end_time);
  746. to = &expire;
  747. }
  748. if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
  749. timed_out = 1;
  750. }
  751. return count;
  752. }
  753. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  754. sizeof(struct pollfd))
  755. int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  756. struct timespec *end_time)
  757. {
  758. struct poll_wqueues table;
  759. int err = -EFAULT, fdcount, len, size;
  760. /* Allocate small arguments on the stack to save memory and be
  761. faster - use long to make sure the buffer is aligned properly
  762. on 64 bit archs to avoid unaligned access */
  763. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  764. struct poll_list *const head = (struct poll_list *)stack_pps;
  765. struct poll_list *walk = head;
  766. unsigned long todo = nfds;
  767. if (nfds > rlimit(RLIMIT_NOFILE))
  768. return -EINVAL;
  769. len = min_t(unsigned int, nfds, N_STACK_PPS);
  770. for (;;) {
  771. walk->next = NULL;
  772. walk->len = len;
  773. if (!len)
  774. break;
  775. if (copy_from_user(walk->entries, ufds + nfds-todo,
  776. sizeof(struct pollfd) * walk->len))
  777. goto out_fds;
  778. todo -= walk->len;
  779. if (!todo)
  780. break;
  781. len = min(todo, POLLFD_PER_PAGE);
  782. size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
  783. walk = walk->next = kmalloc(size, GFP_KERNEL);
  784. if (!walk) {
  785. err = -ENOMEM;
  786. goto out_fds;
  787. }
  788. }
  789. poll_initwait(&table);
  790. fdcount = do_poll(nfds, head, &table, end_time);
  791. poll_freewait(&table);
  792. for (walk = head; walk; walk = walk->next) {
  793. struct pollfd *fds = walk->entries;
  794. int j;
  795. for (j = 0; j < walk->len; j++, ufds++)
  796. if (__put_user(fds[j].revents, &ufds->revents))
  797. goto out_fds;
  798. }
  799. err = fdcount;
  800. out_fds:
  801. walk = head->next;
  802. while (walk) {
  803. struct poll_list *pos = walk;
  804. walk = walk->next;
  805. kfree(pos);
  806. }
  807. return err;
  808. }
  809. static long do_restart_poll(struct restart_block *restart_block)
  810. {
  811. struct pollfd __user *ufds = restart_block->poll.ufds;
  812. int nfds = restart_block->poll.nfds;
  813. struct timespec *to = NULL, end_time;
  814. int ret;
  815. if (restart_block->poll.has_timeout) {
  816. end_time.tv_sec = restart_block->poll.tv_sec;
  817. end_time.tv_nsec = restart_block->poll.tv_nsec;
  818. to = &end_time;
  819. }
  820. ret = do_sys_poll(ufds, nfds, to);
  821. if (ret == -EINTR) {
  822. restart_block->fn = do_restart_poll;
  823. ret = -ERESTART_RESTARTBLOCK;
  824. }
  825. return ret;
  826. }
  827. SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
  828. int, timeout_msecs)
  829. {
  830. struct timespec end_time, *to = NULL;
  831. int ret;
  832. if (timeout_msecs >= 0) {
  833. to = &end_time;
  834. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  835. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  836. }
  837. ret = do_sys_poll(ufds, nfds, to);
  838. if (ret == -EINTR) {
  839. struct restart_block *restart_block;
  840. restart_block = &current->restart_block;
  841. restart_block->fn = do_restart_poll;
  842. restart_block->poll.ufds = ufds;
  843. restart_block->poll.nfds = nfds;
  844. if (timeout_msecs >= 0) {
  845. restart_block->poll.tv_sec = end_time.tv_sec;
  846. restart_block->poll.tv_nsec = end_time.tv_nsec;
  847. restart_block->poll.has_timeout = 1;
  848. } else
  849. restart_block->poll.has_timeout = 0;
  850. ret = -ERESTART_RESTARTBLOCK;
  851. }
  852. return ret;
  853. }
  854. SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
  855. struct timespec __user *, tsp, const sigset_t __user *, sigmask,
  856. size_t, sigsetsize)
  857. {
  858. sigset_t ksigmask, sigsaved;
  859. struct timespec ts, 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. }