select.c 25 KB

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