select.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file contains the procedures for the handling of select and poll
  4. *
  5. * Created for Linux based loosely upon Mathius Lattner's minix
  6. * patches by Peter MacDonald. Heavily edited by Linus.
  7. *
  8. * 4 February 1994
  9. * COFF/ELF binary emulation. If the process has the STICKY_TIMEOUTS
  10. * flag set in its personality we do *not* modify the given timeout
  11. * parameter to reflect time remaining.
  12. *
  13. * 24 January 2000
  14. * Changed sys_poll()/do_poll() to use PAGE_SIZE chunk-based allocation
  15. * of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian).
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched/signal.h>
  19. #include <linux/sched/rt.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/export.h>
  22. #include <linux/slab.h>
  23. #include <linux/poll.h>
  24. #include <linux/personality.h> /* for STICKY_TIMEOUTS */
  25. #include <linux/file.h>
  26. #include <linux/fdtable.h>
  27. #include <linux/fs.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/hrtimer.h>
  30. #include <linux/freezer.h>
  31. #include <net/busy_poll.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/uaccess.h>
  34. /*
  35. * Estimate expected accuracy in ns from a timeval.
  36. *
  37. * After quite a bit of churning around, we've settled on
  38. * a simple thing of taking 0.1% of the timeout as the
  39. * slack, with a cap of 100 msec.
  40. * "nice" tasks get a 0.5% slack instead.
  41. *
  42. * Consider this comment an open invitation to come up with even
  43. * better solutions..
  44. */
  45. #define MAX_SLACK (100 * NSEC_PER_MSEC)
  46. static long __estimate_accuracy(struct timespec64 *tv)
  47. {
  48. long slack;
  49. int divfactor = 1000;
  50. if (tv->tv_sec < 0)
  51. return 0;
  52. if (task_nice(current) > 0)
  53. divfactor = divfactor / 5;
  54. if (tv->tv_sec > MAX_SLACK / (NSEC_PER_SEC/divfactor))
  55. return MAX_SLACK;
  56. slack = tv->tv_nsec / divfactor;
  57. slack += tv->tv_sec * (NSEC_PER_SEC/divfactor);
  58. if (slack > MAX_SLACK)
  59. return MAX_SLACK;
  60. return slack;
  61. }
  62. u64 select_estimate_accuracy(struct timespec64 *tv)
  63. {
  64. u64 ret;
  65. struct timespec64 now;
  66. /*
  67. * Realtime tasks get a slack of 0 for obvious reasons.
  68. */
  69. if (rt_task(current))
  70. return 0;
  71. ktime_get_ts64(&now);
  72. now = timespec64_sub(*tv, now);
  73. ret = __estimate_accuracy(&now);
  74. if (ret < current->timer_slack_ns)
  75. return current->timer_slack_ns;
  76. return ret;
  77. }
  78. struct poll_table_page {
  79. struct poll_table_page * next;
  80. struct poll_table_entry * entry;
  81. struct poll_table_entry entries[0];
  82. };
  83. #define POLL_TABLE_FULL(table) \
  84. ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
  85. /*
  86. * Ok, Peter made a complicated, but straightforward multiple_wait() function.
  87. * I have rewritten this, taking some shortcuts: This code may not be easy to
  88. * follow, but it should be free of race-conditions, and it's practical. If you
  89. * understand what I'm doing here, then you understand how the linux
  90. * sleep/wakeup mechanism works.
  91. *
  92. * Two very simple procedures, poll_wait() and poll_freewait() make all the
  93. * work. poll_wait() is an inline-function defined in <linux/poll.h>,
  94. * as all select/poll functions have to call it to add an entry to the
  95. * poll table.
  96. */
  97. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  98. poll_table *p);
  99. void poll_initwait(struct poll_wqueues *pwq)
  100. {
  101. init_poll_funcptr(&pwq->pt, __pollwait);
  102. pwq->polling_task = current;
  103. pwq->triggered = 0;
  104. pwq->error = 0;
  105. pwq->table = NULL;
  106. pwq->inline_index = 0;
  107. }
  108. EXPORT_SYMBOL(poll_initwait);
  109. static void free_poll_entry(struct poll_table_entry *entry)
  110. {
  111. remove_wait_queue(entry->wait_address, &entry->wait);
  112. fput(entry->filp);
  113. }
  114. void poll_freewait(struct poll_wqueues *pwq)
  115. {
  116. struct poll_table_page * p = pwq->table;
  117. int i;
  118. for (i = 0; i < pwq->inline_index; i++)
  119. free_poll_entry(pwq->inline_entries + i);
  120. while (p) {
  121. struct poll_table_entry * entry;
  122. struct poll_table_page *old;
  123. entry = p->entry;
  124. do {
  125. entry--;
  126. free_poll_entry(entry);
  127. } while (entry > p->entries);
  128. old = p;
  129. p = p->next;
  130. free_page((unsigned long) old);
  131. }
  132. }
  133. EXPORT_SYMBOL(poll_freewait);
  134. static struct poll_table_entry *poll_get_entry(struct poll_wqueues *p)
  135. {
  136. struct poll_table_page *table = p->table;
  137. if (p->inline_index < N_INLINE_POLL_ENTRIES)
  138. return p->inline_entries + p->inline_index++;
  139. if (!table || POLL_TABLE_FULL(table)) {
  140. struct poll_table_page *new_table;
  141. new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
  142. if (!new_table) {
  143. p->error = -ENOMEM;
  144. return NULL;
  145. }
  146. new_table->entry = new_table->entries;
  147. new_table->next = table;
  148. p->table = new_table;
  149. table = new_table;
  150. }
  151. return table->entry++;
  152. }
  153. static int __pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
  154. {
  155. struct poll_wqueues *pwq = wait->private;
  156. DECLARE_WAITQUEUE(dummy_wait, pwq->polling_task);
  157. /*
  158. * Although this function is called under waitqueue lock, LOCK
  159. * doesn't imply write barrier and the users expect write
  160. * barrier semantics on wakeup functions. The following
  161. * smp_wmb() is equivalent to smp_wmb() in try_to_wake_up()
  162. * and is paired with smp_store_mb() in poll_schedule_timeout.
  163. */
  164. smp_wmb();
  165. pwq->triggered = 1;
  166. /*
  167. * Perform the default wake up operation using a dummy
  168. * waitqueue.
  169. *
  170. * TODO: This is hacky but there currently is no interface to
  171. * pass in @sync. @sync is scheduled to be removed and once
  172. * that happens, wake_up_process() can be used directly.
  173. */
  174. return default_wake_function(&dummy_wait, mode, sync, key);
  175. }
  176. static int pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
  177. {
  178. struct poll_table_entry *entry;
  179. entry = container_of(wait, struct poll_table_entry, wait);
  180. if (key && !(key_to_poll(key) & entry->key))
  181. return 0;
  182. return __pollwake(wait, mode, sync, key);
  183. }
  184. /* Add a new entry */
  185. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  186. poll_table *p)
  187. {
  188. struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);
  189. struct poll_table_entry *entry = poll_get_entry(pwq);
  190. if (!entry)
  191. return;
  192. entry->filp = get_file(filp);
  193. entry->wait_address = wait_address;
  194. entry->key = p->_key;
  195. init_waitqueue_func_entry(&entry->wait, pollwake);
  196. entry->wait.private = pwq;
  197. add_wait_queue(wait_address, &entry->wait);
  198. }
  199. static int poll_schedule_timeout(struct poll_wqueues *pwq, int state,
  200. ktime_t *expires, unsigned long slack)
  201. {
  202. int rc = -EINTR;
  203. set_current_state(state);
  204. if (!pwq->triggered)
  205. rc = schedule_hrtimeout_range(expires, slack, HRTIMER_MODE_ABS);
  206. __set_current_state(TASK_RUNNING);
  207. /*
  208. * Prepare for the next iteration.
  209. *
  210. * The following smp_store_mb() serves two purposes. First, it's
  211. * the counterpart rmb of the wmb in pollwake() such that data
  212. * written before wake up is always visible after wake up.
  213. * Second, the full barrier guarantees that triggered clearing
  214. * doesn't pass event check of the next iteration. Note that
  215. * this problem doesn't exist for the first iteration as
  216. * add_wait_queue() has full barrier semantics.
  217. */
  218. smp_store_mb(pwq->triggered, 0);
  219. return rc;
  220. }
  221. /**
  222. * poll_select_set_timeout - helper function to setup the timeout value
  223. * @to: pointer to timespec64 variable for the final timeout
  224. * @sec: seconds (from user space)
  225. * @nsec: nanoseconds (from user space)
  226. *
  227. * Note, we do not use a timespec for the user space value here, That
  228. * way we can use the function for timeval and compat interfaces as well.
  229. *
  230. * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0.
  231. */
  232. int poll_select_set_timeout(struct timespec64 *to, time64_t sec, long nsec)
  233. {
  234. struct timespec64 ts = {.tv_sec = sec, .tv_nsec = nsec};
  235. if (!timespec64_valid(&ts))
  236. return -EINVAL;
  237. /* Optimize for the zero timeout value here */
  238. if (!sec && !nsec) {
  239. to->tv_sec = to->tv_nsec = 0;
  240. } else {
  241. ktime_get_ts64(to);
  242. *to = timespec64_add_safe(*to, ts);
  243. }
  244. return 0;
  245. }
  246. static int poll_select_copy_remaining(struct timespec64 *end_time,
  247. void __user *p,
  248. int timeval, int ret)
  249. {
  250. struct timespec64 rts;
  251. struct timeval rtv;
  252. if (!p)
  253. return ret;
  254. if (current->personality & STICKY_TIMEOUTS)
  255. goto sticky;
  256. /* No update for zero timeout */
  257. if (!end_time->tv_sec && !end_time->tv_nsec)
  258. return ret;
  259. ktime_get_ts64(&rts);
  260. rts = timespec64_sub(*end_time, rts);
  261. if (rts.tv_sec < 0)
  262. rts.tv_sec = rts.tv_nsec = 0;
  263. if (timeval) {
  264. if (sizeof(rtv) > sizeof(rtv.tv_sec) + sizeof(rtv.tv_usec))
  265. memset(&rtv, 0, sizeof(rtv));
  266. rtv.tv_sec = rts.tv_sec;
  267. rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
  268. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  269. return ret;
  270. } else if (!put_timespec64(&rts, p))
  271. return ret;
  272. /*
  273. * If an application puts its timeval in read-only memory, we
  274. * don't want the Linux-specific update to the timeval to
  275. * cause a fault after the select has completed
  276. * successfully. However, because we're not updating the
  277. * timeval, we can't restart the system call.
  278. */
  279. sticky:
  280. if (ret == -ERESTARTNOHAND)
  281. ret = -EINTR;
  282. return ret;
  283. }
  284. /*
  285. * Scalable version of the fd_set.
  286. */
  287. typedef struct {
  288. unsigned long *in, *out, *ex;
  289. unsigned long *res_in, *res_out, *res_ex;
  290. } fd_set_bits;
  291. /*
  292. * How many longwords for "nr" bits?
  293. */
  294. #define FDS_BITPERLONG (8*sizeof(long))
  295. #define FDS_LONGS(nr) (((nr)+FDS_BITPERLONG-1)/FDS_BITPERLONG)
  296. #define FDS_BYTES(nr) (FDS_LONGS(nr)*sizeof(long))
  297. /*
  298. * We do a VERIFY_WRITE here even though we are only reading this time:
  299. * we'll write to it eventually..
  300. *
  301. * Use "unsigned long" accesses to let user-mode fd_set's be long-aligned.
  302. */
  303. static inline
  304. int get_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
  305. {
  306. nr = FDS_BYTES(nr);
  307. if (ufdset)
  308. return copy_from_user(fdset, ufdset, nr) ? -EFAULT : 0;
  309. memset(fdset, 0, nr);
  310. return 0;
  311. }
  312. static inline unsigned long __must_check
  313. set_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
  314. {
  315. if (ufdset)
  316. return __copy_to_user(ufdset, fdset, FDS_BYTES(nr));
  317. return 0;
  318. }
  319. static inline
  320. void zero_fd_set(unsigned long nr, unsigned long *fdset)
  321. {
  322. memset(fdset, 0, FDS_BYTES(nr));
  323. }
  324. #define FDS_IN(fds, n) (fds->in + n)
  325. #define FDS_OUT(fds, n) (fds->out + n)
  326. #define FDS_EX(fds, n) (fds->ex + n)
  327. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  328. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  329. {
  330. unsigned long *open_fds;
  331. unsigned long set;
  332. int max;
  333. struct fdtable *fdt;
  334. /* handle last in-complete long-word first */
  335. set = ~(~0UL << (n & (BITS_PER_LONG-1)));
  336. n /= BITS_PER_LONG;
  337. fdt = files_fdtable(current->files);
  338. open_fds = fdt->open_fds + n;
  339. max = 0;
  340. if (set) {
  341. set &= BITS(fds, n);
  342. if (set) {
  343. if (!(set & ~*open_fds))
  344. goto get_max;
  345. return -EBADF;
  346. }
  347. }
  348. while (n) {
  349. open_fds--;
  350. n--;
  351. set = BITS(fds, n);
  352. if (!set)
  353. continue;
  354. if (set & ~*open_fds)
  355. return -EBADF;
  356. if (max)
  357. continue;
  358. get_max:
  359. do {
  360. max++;
  361. set >>= 1;
  362. } while (set);
  363. max += n * BITS_PER_LONG;
  364. }
  365. return max;
  366. }
  367. #define POLLIN_SET (EPOLLRDNORM | EPOLLRDBAND | EPOLLIN | EPOLLHUP | EPOLLERR)
  368. #define POLLOUT_SET (EPOLLWRBAND | EPOLLWRNORM | EPOLLOUT | EPOLLERR)
  369. #define POLLEX_SET (EPOLLPRI)
  370. static inline void wait_key_set(poll_table *wait, unsigned long in,
  371. unsigned long out, unsigned long bit,
  372. __poll_t ll_flag)
  373. {
  374. wait->_key = POLLEX_SET | ll_flag;
  375. if (in & bit)
  376. wait->_key |= POLLIN_SET;
  377. if (out & bit)
  378. wait->_key |= POLLOUT_SET;
  379. }
  380. static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
  381. {
  382. ktime_t expire, *to = NULL;
  383. struct poll_wqueues table;
  384. poll_table *wait;
  385. int retval, i, timed_out = 0;
  386. u64 slack = 0;
  387. __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  388. unsigned long busy_start = 0;
  389. rcu_read_lock();
  390. retval = max_select_fd(n, fds);
  391. rcu_read_unlock();
  392. if (retval < 0)
  393. return retval;
  394. n = retval;
  395. poll_initwait(&table);
  396. wait = &table.pt;
  397. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  398. wait->_qproc = NULL;
  399. timed_out = 1;
  400. }
  401. if (end_time && !timed_out)
  402. slack = select_estimate_accuracy(end_time);
  403. retval = 0;
  404. for (;;) {
  405. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  406. bool can_busy_loop = false;
  407. inp = fds->in; outp = fds->out; exp = fds->ex;
  408. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  409. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  410. unsigned long in, out, ex, all_bits, bit = 1, j;
  411. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  412. __poll_t mask;
  413. in = *inp++; out = *outp++; ex = *exp++;
  414. all_bits = in | out | ex;
  415. if (all_bits == 0) {
  416. i += BITS_PER_LONG;
  417. continue;
  418. }
  419. for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
  420. struct fd f;
  421. if (i >= n)
  422. break;
  423. if (!(bit & all_bits))
  424. continue;
  425. f = fdget(i);
  426. if (f.file) {
  427. wait_key_set(wait, in, out, bit,
  428. busy_flag);
  429. mask = vfs_poll(f.file, wait);
  430. fdput(f);
  431. if ((mask & POLLIN_SET) && (in & bit)) {
  432. res_in |= bit;
  433. retval++;
  434. wait->_qproc = NULL;
  435. }
  436. if ((mask & POLLOUT_SET) && (out & bit)) {
  437. res_out |= bit;
  438. retval++;
  439. wait->_qproc = NULL;
  440. }
  441. if ((mask & POLLEX_SET) && (ex & bit)) {
  442. res_ex |= bit;
  443. retval++;
  444. wait->_qproc = NULL;
  445. }
  446. /* got something, stop busy polling */
  447. if (retval) {
  448. can_busy_loop = false;
  449. busy_flag = 0;
  450. /*
  451. * only remember a returned
  452. * POLL_BUSY_LOOP if we asked for it
  453. */
  454. } else if (busy_flag & mask)
  455. can_busy_loop = true;
  456. }
  457. }
  458. if (res_in)
  459. *rinp = res_in;
  460. if (res_out)
  461. *routp = res_out;
  462. if (res_ex)
  463. *rexp = res_ex;
  464. cond_resched();
  465. }
  466. wait->_qproc = NULL;
  467. if (retval || timed_out || signal_pending(current))
  468. break;
  469. if (table.error) {
  470. retval = table.error;
  471. break;
  472. }
  473. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  474. if (can_busy_loop && !need_resched()) {
  475. if (!busy_start) {
  476. busy_start = busy_loop_current_time();
  477. continue;
  478. }
  479. if (!busy_loop_timeout(busy_start))
  480. continue;
  481. }
  482. busy_flag = 0;
  483. /*
  484. * If this is the first loop and we have a timeout
  485. * given, then we convert to ktime_t and set the to
  486. * pointer to the expiry value.
  487. */
  488. if (end_time && !to) {
  489. expire = timespec64_to_ktime(*end_time);
  490. to = &expire;
  491. }
  492. if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
  493. to, slack))
  494. timed_out = 1;
  495. }
  496. poll_freewait(&table);
  497. return retval;
  498. }
  499. /*
  500. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  501. * like to be certain this leads to no problems. So I return
  502. * EINTR just for safety.
  503. *
  504. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  505. * I'm trying ERESTARTNOHAND which restart only when you want to.
  506. */
  507. int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  508. fd_set __user *exp, struct timespec64 *end_time)
  509. {
  510. fd_set_bits fds;
  511. void *bits;
  512. int ret, max_fds;
  513. size_t size, alloc_size;
  514. struct fdtable *fdt;
  515. /* Allocate small arguments on the stack to save memory and be faster */
  516. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  517. ret = -EINVAL;
  518. if (n < 0)
  519. goto out_nofds;
  520. /* max_fds can increase, so grab it once to avoid race */
  521. rcu_read_lock();
  522. fdt = files_fdtable(current->files);
  523. max_fds = fdt->max_fds;
  524. rcu_read_unlock();
  525. if (n > max_fds)
  526. n = max_fds;
  527. /*
  528. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  529. * since we used fdset we need to allocate memory in units of
  530. * long-words.
  531. */
  532. size = FDS_BYTES(n);
  533. bits = stack_fds;
  534. if (size > sizeof(stack_fds) / 6) {
  535. /* Not enough space in on-stack array; must use kmalloc */
  536. ret = -ENOMEM;
  537. if (size > (SIZE_MAX / 6))
  538. goto out_nofds;
  539. alloc_size = 6 * size;
  540. bits = kvmalloc(alloc_size, GFP_KERNEL);
  541. if (!bits)
  542. goto out_nofds;
  543. }
  544. fds.in = bits;
  545. fds.out = bits + size;
  546. fds.ex = bits + 2*size;
  547. fds.res_in = bits + 3*size;
  548. fds.res_out = bits + 4*size;
  549. fds.res_ex = bits + 5*size;
  550. if ((ret = get_fd_set(n, inp, fds.in)) ||
  551. (ret = get_fd_set(n, outp, fds.out)) ||
  552. (ret = get_fd_set(n, exp, fds.ex)))
  553. goto out;
  554. zero_fd_set(n, fds.res_in);
  555. zero_fd_set(n, fds.res_out);
  556. zero_fd_set(n, fds.res_ex);
  557. ret = do_select(n, &fds, end_time);
  558. if (ret < 0)
  559. goto out;
  560. if (!ret) {
  561. ret = -ERESTARTNOHAND;
  562. if (signal_pending(current))
  563. goto out;
  564. ret = 0;
  565. }
  566. if (set_fd_set(n, inp, fds.res_in) ||
  567. set_fd_set(n, outp, fds.res_out) ||
  568. set_fd_set(n, exp, fds.res_ex))
  569. ret = -EFAULT;
  570. out:
  571. if (bits != stack_fds)
  572. kvfree(bits);
  573. out_nofds:
  574. return ret;
  575. }
  576. static int kern_select(int n, fd_set __user *inp, fd_set __user *outp,
  577. fd_set __user *exp, struct timeval __user *tvp)
  578. {
  579. struct timespec64 end_time, *to = NULL;
  580. struct timeval tv;
  581. int ret;
  582. if (tvp) {
  583. if (copy_from_user(&tv, tvp, sizeof(tv)))
  584. return -EFAULT;
  585. to = &end_time;
  586. if (poll_select_set_timeout(to,
  587. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  588. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  589. return -EINVAL;
  590. }
  591. ret = core_sys_select(n, inp, outp, exp, to);
  592. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  593. return ret;
  594. }
  595. SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
  596. fd_set __user *, exp, struct timeval __user *, tvp)
  597. {
  598. return kern_select(n, inp, outp, exp, tvp);
  599. }
  600. static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
  601. fd_set __user *exp, struct timespec __user *tsp,
  602. const sigset_t __user *sigmask, size_t sigsetsize)
  603. {
  604. sigset_t ksigmask, sigsaved;
  605. struct timespec64 ts, end_time, *to = NULL;
  606. int ret;
  607. if (tsp) {
  608. if (get_timespec64(&ts, tsp))
  609. return -EFAULT;
  610. to = &end_time;
  611. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  612. return -EINVAL;
  613. }
  614. if (sigmask) {
  615. /* XXX: Don't preclude handling different sized sigset_t's. */
  616. if (sigsetsize != sizeof(sigset_t))
  617. return -EINVAL;
  618. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  619. return -EFAULT;
  620. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  621. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  622. }
  623. ret = core_sys_select(n, inp, outp, exp, to);
  624. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  625. if (ret == -ERESTARTNOHAND) {
  626. /*
  627. * Don't restore the signal mask yet. Let do_signal() deliver
  628. * the signal on the way back to userspace, before the signal
  629. * mask is restored.
  630. */
  631. if (sigmask) {
  632. memcpy(&current->saved_sigmask, &sigsaved,
  633. sizeof(sigsaved));
  634. set_restore_sigmask();
  635. }
  636. } else if (sigmask)
  637. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  638. return ret;
  639. }
  640. /*
  641. * Most architectures can't handle 7-argument syscalls. So we provide a
  642. * 6-argument version where the sixth argument is a pointer to a structure
  643. * which has a pointer to the sigset_t itself followed by a size_t containing
  644. * the sigset size.
  645. */
  646. SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
  647. fd_set __user *, exp, struct timespec __user *, tsp,
  648. void __user *, sig)
  649. {
  650. size_t sigsetsize = 0;
  651. sigset_t __user *up = NULL;
  652. if (sig) {
  653. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  654. || __get_user(up, (sigset_t __user * __user *)sig)
  655. || __get_user(sigsetsize,
  656. (size_t __user *)(sig+sizeof(void *))))
  657. return -EFAULT;
  658. }
  659. return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
  660. }
  661. #ifdef __ARCH_WANT_SYS_OLD_SELECT
  662. struct sel_arg_struct {
  663. unsigned long n;
  664. fd_set __user *inp, *outp, *exp;
  665. struct timeval __user *tvp;
  666. };
  667. SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
  668. {
  669. struct sel_arg_struct a;
  670. if (copy_from_user(&a, arg, sizeof(a)))
  671. return -EFAULT;
  672. return kern_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  673. }
  674. #endif
  675. struct poll_list {
  676. struct poll_list *next;
  677. int len;
  678. struct pollfd entries[0];
  679. };
  680. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  681. /*
  682. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  683. * interested in events matching the pollfd->events mask, and the result
  684. * matching that mask is both recorded in pollfd->revents and returned. The
  685. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  686. * if pwait->_qproc is non-NULL.
  687. */
  688. static inline __poll_t do_pollfd(struct pollfd *pollfd, poll_table *pwait,
  689. bool *can_busy_poll,
  690. __poll_t busy_flag)
  691. {
  692. int fd = pollfd->fd;
  693. __poll_t mask = 0, filter;
  694. struct fd f;
  695. if (fd < 0)
  696. goto out;
  697. mask = EPOLLNVAL;
  698. f = fdget(fd);
  699. if (!f.file)
  700. goto out;
  701. /* userland u16 ->events contains POLL... bitmap */
  702. filter = demangle_poll(pollfd->events) | EPOLLERR | EPOLLHUP;
  703. pwait->_key = filter | busy_flag;
  704. mask = vfs_poll(f.file, pwait);
  705. if (mask & busy_flag)
  706. *can_busy_poll = true;
  707. mask &= filter; /* Mask out unneeded events. */
  708. fdput(f);
  709. out:
  710. /* ... and so does ->revents */
  711. pollfd->revents = mangle_poll(mask);
  712. return mask;
  713. }
  714. static int do_poll(struct poll_list *list, struct poll_wqueues *wait,
  715. struct timespec64 *end_time)
  716. {
  717. poll_table* pt = &wait->pt;
  718. ktime_t expire, *to = NULL;
  719. int timed_out = 0, count = 0;
  720. u64 slack = 0;
  721. __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  722. unsigned long busy_start = 0;
  723. /* Optimise the no-wait case */
  724. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  725. pt->_qproc = NULL;
  726. timed_out = 1;
  727. }
  728. if (end_time && !timed_out)
  729. slack = select_estimate_accuracy(end_time);
  730. for (;;) {
  731. struct poll_list *walk;
  732. bool can_busy_loop = false;
  733. for (walk = list; walk != NULL; walk = walk->next) {
  734. struct pollfd * pfd, * pfd_end;
  735. pfd = walk->entries;
  736. pfd_end = pfd + walk->len;
  737. for (; pfd != pfd_end; pfd++) {
  738. /*
  739. * Fish for events. If we found one, record it
  740. * and kill poll_table->_qproc, so we don't
  741. * needlessly register any other waiters after
  742. * this. They'll get immediately deregistered
  743. * when we break out and return.
  744. */
  745. if (do_pollfd(pfd, pt, &can_busy_loop,
  746. busy_flag)) {
  747. count++;
  748. pt->_qproc = NULL;
  749. /* found something, stop busy polling */
  750. busy_flag = 0;
  751. can_busy_loop = false;
  752. }
  753. }
  754. }
  755. /*
  756. * All waiters have already been registered, so don't provide
  757. * a poll_table->_qproc to them on the next loop iteration.
  758. */
  759. pt->_qproc = NULL;
  760. if (!count) {
  761. count = wait->error;
  762. if (signal_pending(current))
  763. count = -EINTR;
  764. }
  765. if (count || timed_out)
  766. break;
  767. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  768. if (can_busy_loop && !need_resched()) {
  769. if (!busy_start) {
  770. busy_start = busy_loop_current_time();
  771. continue;
  772. }
  773. if (!busy_loop_timeout(busy_start))
  774. continue;
  775. }
  776. busy_flag = 0;
  777. /*
  778. * If this is the first loop and we have a timeout
  779. * given, then we convert to ktime_t and set the to
  780. * pointer to the expiry value.
  781. */
  782. if (end_time && !to) {
  783. expire = timespec64_to_ktime(*end_time);
  784. to = &expire;
  785. }
  786. if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
  787. timed_out = 1;
  788. }
  789. return count;
  790. }
  791. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  792. sizeof(struct pollfd))
  793. static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  794. struct timespec64 *end_time)
  795. {
  796. struct poll_wqueues table;
  797. int err = -EFAULT, fdcount, len, size;
  798. /* Allocate small arguments on the stack to save memory and be
  799. faster - use long to make sure the buffer is aligned properly
  800. on 64 bit archs to avoid unaligned access */
  801. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  802. struct poll_list *const head = (struct poll_list *)stack_pps;
  803. struct poll_list *walk = head;
  804. unsigned long todo = nfds;
  805. if (nfds > rlimit(RLIMIT_NOFILE))
  806. return -EINVAL;
  807. len = min_t(unsigned int, nfds, N_STACK_PPS);
  808. for (;;) {
  809. walk->next = NULL;
  810. walk->len = len;
  811. if (!len)
  812. break;
  813. if (copy_from_user(walk->entries, ufds + nfds-todo,
  814. sizeof(struct pollfd) * walk->len))
  815. goto out_fds;
  816. todo -= walk->len;
  817. if (!todo)
  818. break;
  819. len = min(todo, POLLFD_PER_PAGE);
  820. size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
  821. walk = walk->next = kmalloc(size, GFP_KERNEL);
  822. if (!walk) {
  823. err = -ENOMEM;
  824. goto out_fds;
  825. }
  826. }
  827. poll_initwait(&table);
  828. fdcount = do_poll(head, &table, end_time);
  829. poll_freewait(&table);
  830. for (walk = head; walk; walk = walk->next) {
  831. struct pollfd *fds = walk->entries;
  832. int j;
  833. for (j = 0; j < walk->len; j++, ufds++)
  834. if (__put_user(fds[j].revents, &ufds->revents))
  835. goto out_fds;
  836. }
  837. err = fdcount;
  838. out_fds:
  839. walk = head->next;
  840. while (walk) {
  841. struct poll_list *pos = walk;
  842. walk = walk->next;
  843. kfree(pos);
  844. }
  845. return err;
  846. }
  847. static long do_restart_poll(struct restart_block *restart_block)
  848. {
  849. struct pollfd __user *ufds = restart_block->poll.ufds;
  850. int nfds = restart_block->poll.nfds;
  851. struct timespec64 *to = NULL, end_time;
  852. int ret;
  853. if (restart_block->poll.has_timeout) {
  854. end_time.tv_sec = restart_block->poll.tv_sec;
  855. end_time.tv_nsec = restart_block->poll.tv_nsec;
  856. to = &end_time;
  857. }
  858. ret = do_sys_poll(ufds, nfds, to);
  859. if (ret == -EINTR) {
  860. restart_block->fn = do_restart_poll;
  861. ret = -ERESTART_RESTARTBLOCK;
  862. }
  863. return ret;
  864. }
  865. SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
  866. int, timeout_msecs)
  867. {
  868. struct timespec64 end_time, *to = NULL;
  869. int ret;
  870. if (timeout_msecs >= 0) {
  871. to = &end_time;
  872. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  873. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  874. }
  875. ret = do_sys_poll(ufds, nfds, to);
  876. if (ret == -EINTR) {
  877. struct restart_block *restart_block;
  878. restart_block = &current->restart_block;
  879. restart_block->fn = do_restart_poll;
  880. restart_block->poll.ufds = ufds;
  881. restart_block->poll.nfds = nfds;
  882. if (timeout_msecs >= 0) {
  883. restart_block->poll.tv_sec = end_time.tv_sec;
  884. restart_block->poll.tv_nsec = end_time.tv_nsec;
  885. restart_block->poll.has_timeout = 1;
  886. } else
  887. restart_block->poll.has_timeout = 0;
  888. ret = -ERESTART_RESTARTBLOCK;
  889. }
  890. return ret;
  891. }
  892. SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
  893. struct timespec __user *, tsp, const sigset_t __user *, sigmask,
  894. size_t, sigsetsize)
  895. {
  896. sigset_t ksigmask, sigsaved;
  897. struct timespec64 ts, end_time, *to = NULL;
  898. int ret;
  899. if (tsp) {
  900. if (get_timespec64(&ts, tsp))
  901. return -EFAULT;
  902. to = &end_time;
  903. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  904. return -EINVAL;
  905. }
  906. if (sigmask) {
  907. /* XXX: Don't preclude handling different sized sigset_t's. */
  908. if (sigsetsize != sizeof(sigset_t))
  909. return -EINVAL;
  910. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  911. return -EFAULT;
  912. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  913. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  914. }
  915. ret = do_sys_poll(ufds, nfds, to);
  916. /* We can restart this syscall, usually */
  917. if (ret == -EINTR) {
  918. /*
  919. * Don't restore the signal mask yet. Let do_signal() deliver
  920. * the signal on the way back to userspace, before the signal
  921. * mask is restored.
  922. */
  923. if (sigmask) {
  924. memcpy(&current->saved_sigmask, &sigsaved,
  925. sizeof(sigsaved));
  926. set_restore_sigmask();
  927. }
  928. ret = -ERESTARTNOHAND;
  929. } else if (sigmask)
  930. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  931. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  932. return ret;
  933. }
  934. #ifdef CONFIG_COMPAT
  935. #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
  936. static
  937. int compat_poll_select_copy_remaining(struct timespec64 *end_time, void __user *p,
  938. int timeval, int ret)
  939. {
  940. struct timespec64 ts;
  941. if (!p)
  942. return ret;
  943. if (current->personality & STICKY_TIMEOUTS)
  944. goto sticky;
  945. /* No update for zero timeout */
  946. if (!end_time->tv_sec && !end_time->tv_nsec)
  947. return ret;
  948. ktime_get_ts64(&ts);
  949. ts = timespec64_sub(*end_time, ts);
  950. if (ts.tv_sec < 0)
  951. ts.tv_sec = ts.tv_nsec = 0;
  952. if (timeval) {
  953. struct compat_timeval rtv;
  954. rtv.tv_sec = ts.tv_sec;
  955. rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  956. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  957. return ret;
  958. } else {
  959. if (!compat_put_timespec64(&ts, p))
  960. return ret;
  961. }
  962. /*
  963. * If an application puts its timeval in read-only memory, we
  964. * don't want the Linux-specific update to the timeval to
  965. * cause a fault after the select has completed
  966. * successfully. However, because we're not updating the
  967. * timeval, we can't restart the system call.
  968. */
  969. sticky:
  970. if (ret == -ERESTARTNOHAND)
  971. ret = -EINTR;
  972. return ret;
  973. }
  974. /*
  975. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  976. * 64-bit unsigned longs.
  977. */
  978. static
  979. int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  980. unsigned long *fdset)
  981. {
  982. if (ufdset) {
  983. return compat_get_bitmap(fdset, ufdset, nr);
  984. } else {
  985. zero_fd_set(nr, fdset);
  986. return 0;
  987. }
  988. }
  989. static
  990. int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  991. unsigned long *fdset)
  992. {
  993. if (!ufdset)
  994. return 0;
  995. return compat_put_bitmap(ufdset, fdset, nr);
  996. }
  997. /*
  998. * This is a virtual copy of sys_select from fs/select.c and probably
  999. * should be compared to it from time to time
  1000. */
  1001. /*
  1002. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  1003. * like to be certain this leads to no problems. So I return
  1004. * EINTR just for safety.
  1005. *
  1006. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  1007. * I'm trying ERESTARTNOHAND which restart only when you want to.
  1008. */
  1009. static int compat_core_sys_select(int n, compat_ulong_t __user *inp,
  1010. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1011. struct timespec64 *end_time)
  1012. {
  1013. fd_set_bits fds;
  1014. void *bits;
  1015. int size, max_fds, ret = -EINVAL;
  1016. struct fdtable *fdt;
  1017. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  1018. if (n < 0)
  1019. goto out_nofds;
  1020. /* max_fds can increase, so grab it once to avoid race */
  1021. rcu_read_lock();
  1022. fdt = files_fdtable(current->files);
  1023. max_fds = fdt->max_fds;
  1024. rcu_read_unlock();
  1025. if (n > max_fds)
  1026. n = max_fds;
  1027. /*
  1028. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  1029. * since we used fdset we need to allocate memory in units of
  1030. * long-words.
  1031. */
  1032. size = FDS_BYTES(n);
  1033. bits = stack_fds;
  1034. if (size > sizeof(stack_fds) / 6) {
  1035. bits = kmalloc_array(6, size, GFP_KERNEL);
  1036. ret = -ENOMEM;
  1037. if (!bits)
  1038. goto out_nofds;
  1039. }
  1040. fds.in = (unsigned long *) bits;
  1041. fds.out = (unsigned long *) (bits + size);
  1042. fds.ex = (unsigned long *) (bits + 2*size);
  1043. fds.res_in = (unsigned long *) (bits + 3*size);
  1044. fds.res_out = (unsigned long *) (bits + 4*size);
  1045. fds.res_ex = (unsigned long *) (bits + 5*size);
  1046. if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
  1047. (ret = compat_get_fd_set(n, outp, fds.out)) ||
  1048. (ret = compat_get_fd_set(n, exp, fds.ex)))
  1049. goto out;
  1050. zero_fd_set(n, fds.res_in);
  1051. zero_fd_set(n, fds.res_out);
  1052. zero_fd_set(n, fds.res_ex);
  1053. ret = do_select(n, &fds, end_time);
  1054. if (ret < 0)
  1055. goto out;
  1056. if (!ret) {
  1057. ret = -ERESTARTNOHAND;
  1058. if (signal_pending(current))
  1059. goto out;
  1060. ret = 0;
  1061. }
  1062. if (compat_set_fd_set(n, inp, fds.res_in) ||
  1063. compat_set_fd_set(n, outp, fds.res_out) ||
  1064. compat_set_fd_set(n, exp, fds.res_ex))
  1065. ret = -EFAULT;
  1066. out:
  1067. if (bits != stack_fds)
  1068. kfree(bits);
  1069. out_nofds:
  1070. return ret;
  1071. }
  1072. static int do_compat_select(int n, compat_ulong_t __user *inp,
  1073. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1074. struct compat_timeval __user *tvp)
  1075. {
  1076. struct timespec64 end_time, *to = NULL;
  1077. struct compat_timeval tv;
  1078. int ret;
  1079. if (tvp) {
  1080. if (copy_from_user(&tv, tvp, sizeof(tv)))
  1081. return -EFAULT;
  1082. to = &end_time;
  1083. if (poll_select_set_timeout(to,
  1084. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  1085. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  1086. return -EINVAL;
  1087. }
  1088. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1089. ret = compat_poll_select_copy_remaining(&end_time, tvp, 1, ret);
  1090. return ret;
  1091. }
  1092. COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp,
  1093. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1094. struct compat_timeval __user *, tvp)
  1095. {
  1096. return do_compat_select(n, inp, outp, exp, tvp);
  1097. }
  1098. struct compat_sel_arg_struct {
  1099. compat_ulong_t n;
  1100. compat_uptr_t inp;
  1101. compat_uptr_t outp;
  1102. compat_uptr_t exp;
  1103. compat_uptr_t tvp;
  1104. };
  1105. COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg)
  1106. {
  1107. struct compat_sel_arg_struct a;
  1108. if (copy_from_user(&a, arg, sizeof(a)))
  1109. return -EFAULT;
  1110. return do_compat_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
  1111. compat_ptr(a.exp), compat_ptr(a.tvp));
  1112. }
  1113. static long do_compat_pselect(int n, compat_ulong_t __user *inp,
  1114. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1115. struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
  1116. compat_size_t sigsetsize)
  1117. {
  1118. sigset_t ksigmask, sigsaved;
  1119. struct timespec64 ts, end_time, *to = NULL;
  1120. int ret;
  1121. if (tsp) {
  1122. if (compat_get_timespec64(&ts, tsp))
  1123. return -EFAULT;
  1124. to = &end_time;
  1125. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1126. return -EINVAL;
  1127. }
  1128. if (sigmask) {
  1129. if (sigsetsize != sizeof(compat_sigset_t))
  1130. return -EINVAL;
  1131. if (get_compat_sigset(&ksigmask, sigmask))
  1132. return -EFAULT;
  1133. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1134. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1135. }
  1136. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1137. ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1138. if (ret == -ERESTARTNOHAND) {
  1139. /*
  1140. * Don't restore the signal mask yet. Let do_signal() deliver
  1141. * the signal on the way back to userspace, before the signal
  1142. * mask is restored.
  1143. */
  1144. if (sigmask) {
  1145. memcpy(&current->saved_sigmask, &sigsaved,
  1146. sizeof(sigsaved));
  1147. set_restore_sigmask();
  1148. }
  1149. } else if (sigmask)
  1150. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1151. return ret;
  1152. }
  1153. COMPAT_SYSCALL_DEFINE6(pselect6, int, n, compat_ulong_t __user *, inp,
  1154. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1155. struct compat_timespec __user *, tsp, void __user *, sig)
  1156. {
  1157. compat_size_t sigsetsize = 0;
  1158. compat_uptr_t up = 0;
  1159. if (sig) {
  1160. if (!access_ok(VERIFY_READ, sig,
  1161. sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
  1162. __get_user(up, (compat_uptr_t __user *)sig) ||
  1163. __get_user(sigsetsize,
  1164. (compat_size_t __user *)(sig+sizeof(up))))
  1165. return -EFAULT;
  1166. }
  1167. return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
  1168. sigsetsize);
  1169. }
  1170. COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds,
  1171. unsigned int, nfds, struct compat_timespec __user *, tsp,
  1172. const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
  1173. {
  1174. sigset_t ksigmask, sigsaved;
  1175. struct timespec64 ts, end_time, *to = NULL;
  1176. int ret;
  1177. if (tsp) {
  1178. if (compat_get_timespec64(&ts, tsp))
  1179. return -EFAULT;
  1180. to = &end_time;
  1181. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1182. return -EINVAL;
  1183. }
  1184. if (sigmask) {
  1185. if (sigsetsize != sizeof(compat_sigset_t))
  1186. return -EINVAL;
  1187. if (get_compat_sigset(&ksigmask, sigmask))
  1188. return -EFAULT;
  1189. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1190. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1191. }
  1192. ret = do_sys_poll(ufds, nfds, to);
  1193. /* We can restart this syscall, usually */
  1194. if (ret == -EINTR) {
  1195. /*
  1196. * Don't restore the signal mask yet. Let do_signal() deliver
  1197. * the signal on the way back to userspace, before the signal
  1198. * mask is restored.
  1199. */
  1200. if (sigmask) {
  1201. memcpy(&current->saved_sigmask, &sigsaved,
  1202. sizeof(sigsaved));
  1203. set_restore_sigmask();
  1204. }
  1205. ret = -ERESTARTNOHAND;
  1206. } else if (sigmask)
  1207. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1208. ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1209. return ret;
  1210. }
  1211. #endif