select.c 25 KB

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