select.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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. 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. EXPORT_SYMBOL(poll_schedule_timeout);
  222. /**
  223. * poll_select_set_timeout - helper function to setup the timeout value
  224. * @to: pointer to timespec64 variable for the final timeout
  225. * @sec: seconds (from user space)
  226. * @nsec: nanoseconds (from user space)
  227. *
  228. * Note, we do not use a timespec for the user space value here, That
  229. * way we can use the function for timeval and compat interfaces as well.
  230. *
  231. * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0.
  232. */
  233. int poll_select_set_timeout(struct timespec64 *to, time64_t sec, long nsec)
  234. {
  235. struct timespec64 ts = {.tv_sec = sec, .tv_nsec = nsec};
  236. if (!timespec64_valid(&ts))
  237. return -EINVAL;
  238. /* Optimize for the zero timeout value here */
  239. if (!sec && !nsec) {
  240. to->tv_sec = to->tv_nsec = 0;
  241. } else {
  242. ktime_get_ts64(to);
  243. *to = timespec64_add_safe(*to, ts);
  244. }
  245. return 0;
  246. }
  247. static int poll_select_copy_remaining(struct timespec64 *end_time,
  248. void __user *p,
  249. int timeval, int ret)
  250. {
  251. struct timespec64 rts;
  252. struct timeval rtv;
  253. if (!p)
  254. return ret;
  255. if (current->personality & STICKY_TIMEOUTS)
  256. goto sticky;
  257. /* No update for zero timeout */
  258. if (!end_time->tv_sec && !end_time->tv_nsec)
  259. return ret;
  260. ktime_get_ts64(&rts);
  261. rts = timespec64_sub(*end_time, rts);
  262. if (rts.tv_sec < 0)
  263. rts.tv_sec = rts.tv_nsec = 0;
  264. if (timeval) {
  265. if (sizeof(rtv) > sizeof(rtv.tv_sec) + sizeof(rtv.tv_usec))
  266. memset(&rtv, 0, sizeof(rtv));
  267. rtv.tv_sec = rts.tv_sec;
  268. rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
  269. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  270. return ret;
  271. } else if (!put_timespec64(&rts, p))
  272. return ret;
  273. /*
  274. * If an application puts its timeval in read-only memory, we
  275. * don't want the Linux-specific update to the timeval to
  276. * cause a fault after the select has completed
  277. * successfully. However, because we're not updating the
  278. * timeval, we can't restart the system call.
  279. */
  280. sticky:
  281. if (ret == -ERESTARTNOHAND)
  282. ret = -EINTR;
  283. return ret;
  284. }
  285. /*
  286. * Scalable version of the fd_set.
  287. */
  288. typedef struct {
  289. unsigned long *in, *out, *ex;
  290. unsigned long *res_in, *res_out, *res_ex;
  291. } fd_set_bits;
  292. /*
  293. * How many longwords for "nr" bits?
  294. */
  295. #define FDS_BITPERLONG (8*sizeof(long))
  296. #define FDS_LONGS(nr) (((nr)+FDS_BITPERLONG-1)/FDS_BITPERLONG)
  297. #define FDS_BYTES(nr) (FDS_LONGS(nr)*sizeof(long))
  298. /*
  299. * We do a VERIFY_WRITE here even though we are only reading this time:
  300. * we'll write to it eventually..
  301. *
  302. * Use "unsigned long" accesses to let user-mode fd_set's be long-aligned.
  303. */
  304. static inline
  305. int get_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
  306. {
  307. nr = FDS_BYTES(nr);
  308. if (ufdset)
  309. return copy_from_user(fdset, ufdset, nr) ? -EFAULT : 0;
  310. memset(fdset, 0, nr);
  311. return 0;
  312. }
  313. static inline unsigned long __must_check
  314. set_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
  315. {
  316. if (ufdset)
  317. return __copy_to_user(ufdset, fdset, FDS_BYTES(nr));
  318. return 0;
  319. }
  320. static inline
  321. void zero_fd_set(unsigned long nr, unsigned long *fdset)
  322. {
  323. memset(fdset, 0, FDS_BYTES(nr));
  324. }
  325. #define FDS_IN(fds, n) (fds->in + n)
  326. #define FDS_OUT(fds, n) (fds->out + n)
  327. #define FDS_EX(fds, n) (fds->ex + n)
  328. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  329. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  330. {
  331. unsigned long *open_fds;
  332. unsigned long set;
  333. int max;
  334. struct fdtable *fdt;
  335. /* handle last in-complete long-word first */
  336. set = ~(~0UL << (n & (BITS_PER_LONG-1)));
  337. n /= BITS_PER_LONG;
  338. fdt = files_fdtable(current->files);
  339. open_fds = fdt->open_fds + n;
  340. max = 0;
  341. if (set) {
  342. set &= BITS(fds, n);
  343. if (set) {
  344. if (!(set & ~*open_fds))
  345. goto get_max;
  346. return -EBADF;
  347. }
  348. }
  349. while (n) {
  350. open_fds--;
  351. n--;
  352. set = BITS(fds, n);
  353. if (!set)
  354. continue;
  355. if (set & ~*open_fds)
  356. return -EBADF;
  357. if (max)
  358. continue;
  359. get_max:
  360. do {
  361. max++;
  362. set >>= 1;
  363. } while (set);
  364. max += n * BITS_PER_LONG;
  365. }
  366. return max;
  367. }
  368. #define POLLIN_SET (EPOLLRDNORM | EPOLLRDBAND | EPOLLIN | EPOLLHUP | EPOLLERR)
  369. #define POLLOUT_SET (EPOLLWRBAND | EPOLLWRNORM | EPOLLOUT | EPOLLERR)
  370. #define POLLEX_SET (EPOLLPRI)
  371. static inline void wait_key_set(poll_table *wait, unsigned long in,
  372. unsigned long out, unsigned long bit,
  373. __poll_t ll_flag)
  374. {
  375. wait->_key = POLLEX_SET | ll_flag;
  376. if (in & bit)
  377. wait->_key |= POLLIN_SET;
  378. if (out & bit)
  379. wait->_key |= POLLOUT_SET;
  380. }
  381. static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
  382. {
  383. ktime_t expire, *to = NULL;
  384. struct poll_wqueues table;
  385. poll_table *wait;
  386. int retval, i, timed_out = 0;
  387. u64 slack = 0;
  388. __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  389. unsigned long busy_start = 0;
  390. rcu_read_lock();
  391. retval = max_select_fd(n, fds);
  392. rcu_read_unlock();
  393. if (retval < 0)
  394. return retval;
  395. n = retval;
  396. poll_initwait(&table);
  397. wait = &table.pt;
  398. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  399. wait->_qproc = NULL;
  400. timed_out = 1;
  401. }
  402. if (end_time && !timed_out)
  403. slack = select_estimate_accuracy(end_time);
  404. retval = 0;
  405. for (;;) {
  406. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  407. bool can_busy_loop = false;
  408. inp = fds->in; outp = fds->out; exp = fds->ex;
  409. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  410. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  411. unsigned long in, out, ex, all_bits, bit = 1, j;
  412. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  413. __poll_t mask;
  414. in = *inp++; out = *outp++; ex = *exp++;
  415. all_bits = in | out | ex;
  416. if (all_bits == 0) {
  417. i += BITS_PER_LONG;
  418. continue;
  419. }
  420. for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
  421. struct fd f;
  422. if (i >= n)
  423. break;
  424. if (!(bit & all_bits))
  425. continue;
  426. f = fdget(i);
  427. if (f.file) {
  428. const struct file_operations *f_op;
  429. f_op = f.file->f_op;
  430. mask = DEFAULT_POLLMASK;
  431. if (f_op->poll) {
  432. wait_key_set(wait, in, out,
  433. bit, busy_flag);
  434. mask = (*f_op->poll)(f.file, wait);
  435. }
  436. fdput(f);
  437. if ((mask & POLLIN_SET) && (in & bit)) {
  438. res_in |= bit;
  439. retval++;
  440. wait->_qproc = NULL;
  441. }
  442. if ((mask & POLLOUT_SET) && (out & bit)) {
  443. res_out |= bit;
  444. retval++;
  445. wait->_qproc = NULL;
  446. }
  447. if ((mask & POLLEX_SET) && (ex & bit)) {
  448. res_ex |= bit;
  449. retval++;
  450. wait->_qproc = NULL;
  451. }
  452. /* got something, stop busy polling */
  453. if (retval) {
  454. can_busy_loop = false;
  455. busy_flag = 0;
  456. /*
  457. * only remember a returned
  458. * POLL_BUSY_LOOP if we asked for it
  459. */
  460. } else if (busy_flag & mask)
  461. can_busy_loop = true;
  462. }
  463. }
  464. if (res_in)
  465. *rinp = res_in;
  466. if (res_out)
  467. *routp = res_out;
  468. if (res_ex)
  469. *rexp = res_ex;
  470. cond_resched();
  471. }
  472. wait->_qproc = NULL;
  473. if (retval || timed_out || signal_pending(current))
  474. break;
  475. if (table.error) {
  476. retval = table.error;
  477. break;
  478. }
  479. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  480. if (can_busy_loop && !need_resched()) {
  481. if (!busy_start) {
  482. busy_start = busy_loop_current_time();
  483. continue;
  484. }
  485. if (!busy_loop_timeout(busy_start))
  486. continue;
  487. }
  488. busy_flag = 0;
  489. /*
  490. * If this is the first loop and we have a timeout
  491. * given, then we convert to ktime_t and set the to
  492. * pointer to the expiry value.
  493. */
  494. if (end_time && !to) {
  495. expire = timespec64_to_ktime(*end_time);
  496. to = &expire;
  497. }
  498. if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
  499. to, slack))
  500. timed_out = 1;
  501. }
  502. poll_freewait(&table);
  503. return retval;
  504. }
  505. /*
  506. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  507. * like to be certain this leads to no problems. So I return
  508. * EINTR just for safety.
  509. *
  510. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  511. * I'm trying ERESTARTNOHAND which restart only when you want to.
  512. */
  513. int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  514. fd_set __user *exp, struct timespec64 *end_time)
  515. {
  516. fd_set_bits fds;
  517. void *bits;
  518. int ret, max_fds;
  519. size_t size, alloc_size;
  520. struct fdtable *fdt;
  521. /* Allocate small arguments on the stack to save memory and be faster */
  522. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  523. ret = -EINVAL;
  524. if (n < 0)
  525. goto out_nofds;
  526. /* max_fds can increase, so grab it once to avoid race */
  527. rcu_read_lock();
  528. fdt = files_fdtable(current->files);
  529. max_fds = fdt->max_fds;
  530. rcu_read_unlock();
  531. if (n > max_fds)
  532. n = max_fds;
  533. /*
  534. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  535. * since we used fdset we need to allocate memory in units of
  536. * long-words.
  537. */
  538. size = FDS_BYTES(n);
  539. bits = stack_fds;
  540. if (size > sizeof(stack_fds) / 6) {
  541. /* Not enough space in on-stack array; must use kmalloc */
  542. ret = -ENOMEM;
  543. if (size > (SIZE_MAX / 6))
  544. goto out_nofds;
  545. alloc_size = 6 * size;
  546. bits = kvmalloc(alloc_size, GFP_KERNEL);
  547. if (!bits)
  548. goto out_nofds;
  549. }
  550. fds.in = bits;
  551. fds.out = bits + size;
  552. fds.ex = bits + 2*size;
  553. fds.res_in = bits + 3*size;
  554. fds.res_out = bits + 4*size;
  555. fds.res_ex = bits + 5*size;
  556. if ((ret = get_fd_set(n, inp, fds.in)) ||
  557. (ret = get_fd_set(n, outp, fds.out)) ||
  558. (ret = get_fd_set(n, exp, fds.ex)))
  559. goto out;
  560. zero_fd_set(n, fds.res_in);
  561. zero_fd_set(n, fds.res_out);
  562. zero_fd_set(n, fds.res_ex);
  563. ret = do_select(n, &fds, end_time);
  564. if (ret < 0)
  565. goto out;
  566. if (!ret) {
  567. ret = -ERESTARTNOHAND;
  568. if (signal_pending(current))
  569. goto out;
  570. ret = 0;
  571. }
  572. if (set_fd_set(n, inp, fds.res_in) ||
  573. set_fd_set(n, outp, fds.res_out) ||
  574. set_fd_set(n, exp, fds.res_ex))
  575. ret = -EFAULT;
  576. out:
  577. if (bits != stack_fds)
  578. kvfree(bits);
  579. out_nofds:
  580. return ret;
  581. }
  582. static int kern_select(int n, fd_set __user *inp, fd_set __user *outp,
  583. fd_set __user *exp, struct timeval __user *tvp)
  584. {
  585. struct timespec64 end_time, *to = NULL;
  586. struct timeval tv;
  587. int ret;
  588. if (tvp) {
  589. if (copy_from_user(&tv, tvp, sizeof(tv)))
  590. return -EFAULT;
  591. to = &end_time;
  592. if (poll_select_set_timeout(to,
  593. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  594. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  595. return -EINVAL;
  596. }
  597. ret = core_sys_select(n, inp, outp, exp, to);
  598. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  599. return ret;
  600. }
  601. SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
  602. fd_set __user *, exp, struct timeval __user *, tvp)
  603. {
  604. return kern_select(n, inp, outp, exp, tvp);
  605. }
  606. static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
  607. fd_set __user *exp, struct timespec __user *tsp,
  608. const sigset_t __user *sigmask, size_t sigsetsize)
  609. {
  610. sigset_t ksigmask, sigsaved;
  611. struct timespec64 ts, end_time, *to = NULL;
  612. int ret;
  613. if (tsp) {
  614. if (get_timespec64(&ts, tsp))
  615. return -EFAULT;
  616. to = &end_time;
  617. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  618. return -EINVAL;
  619. }
  620. if (sigmask) {
  621. /* XXX: Don't preclude handling different sized sigset_t's. */
  622. if (sigsetsize != sizeof(sigset_t))
  623. return -EINVAL;
  624. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  625. return -EFAULT;
  626. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  627. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  628. }
  629. ret = core_sys_select(n, inp, outp, exp, to);
  630. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  631. if (ret == -ERESTARTNOHAND) {
  632. /*
  633. * Don't restore the signal mask yet. Let do_signal() deliver
  634. * the signal on the way back to userspace, before the signal
  635. * mask is restored.
  636. */
  637. if (sigmask) {
  638. memcpy(&current->saved_sigmask, &sigsaved,
  639. sizeof(sigsaved));
  640. set_restore_sigmask();
  641. }
  642. } else if (sigmask)
  643. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  644. return ret;
  645. }
  646. /*
  647. * Most architectures can't handle 7-argument syscalls. So we provide a
  648. * 6-argument version where the sixth argument is a pointer to a structure
  649. * which has a pointer to the sigset_t itself followed by a size_t containing
  650. * the sigset size.
  651. */
  652. SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
  653. fd_set __user *, exp, struct timespec __user *, tsp,
  654. void __user *, sig)
  655. {
  656. size_t sigsetsize = 0;
  657. sigset_t __user *up = NULL;
  658. if (sig) {
  659. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  660. || __get_user(up, (sigset_t __user * __user *)sig)
  661. || __get_user(sigsetsize,
  662. (size_t __user *)(sig+sizeof(void *))))
  663. return -EFAULT;
  664. }
  665. return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
  666. }
  667. #ifdef __ARCH_WANT_SYS_OLD_SELECT
  668. struct sel_arg_struct {
  669. unsigned long n;
  670. fd_set __user *inp, *outp, *exp;
  671. struct timeval __user *tvp;
  672. };
  673. SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
  674. {
  675. struct sel_arg_struct a;
  676. if (copy_from_user(&a, arg, sizeof(a)))
  677. return -EFAULT;
  678. return kern_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  679. }
  680. #endif
  681. struct poll_list {
  682. struct poll_list *next;
  683. int len;
  684. struct pollfd entries[0];
  685. };
  686. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  687. /*
  688. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  689. * interested in events matching the pollfd->events mask, and the result
  690. * matching that mask is both recorded in pollfd->revents and returned. The
  691. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  692. * if pwait->_qproc is non-NULL.
  693. */
  694. static inline __poll_t do_pollfd(struct pollfd *pollfd, poll_table *pwait,
  695. bool *can_busy_poll,
  696. __poll_t busy_flag)
  697. {
  698. __poll_t mask;
  699. int fd;
  700. mask = 0;
  701. fd = pollfd->fd;
  702. if (fd >= 0) {
  703. struct fd f = fdget(fd);
  704. mask = EPOLLNVAL;
  705. if (f.file) {
  706. /* userland u16 ->events contains POLL... bitmap */
  707. __poll_t filter = demangle_poll(pollfd->events) |
  708. EPOLLERR | EPOLLHUP;
  709. mask = DEFAULT_POLLMASK;
  710. if (f.file->f_op->poll) {
  711. pwait->_key = filter;
  712. pwait->_key |= busy_flag;
  713. mask = f.file->f_op->poll(f.file, pwait);
  714. if (mask & busy_flag)
  715. *can_busy_poll = true;
  716. }
  717. /* Mask out unneeded events. */
  718. mask &= filter;
  719. fdput(f);
  720. }
  721. }
  722. /* ... and so does ->revents */
  723. pollfd->revents = mangle_poll(mask);
  724. return mask;
  725. }
  726. static int do_poll(struct poll_list *list, struct poll_wqueues *wait,
  727. struct timespec64 *end_time)
  728. {
  729. poll_table* pt = &wait->pt;
  730. ktime_t expire, *to = NULL;
  731. int timed_out = 0, count = 0;
  732. u64 slack = 0;
  733. __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  734. unsigned long busy_start = 0;
  735. /* Optimise the no-wait case */
  736. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  737. pt->_qproc = NULL;
  738. timed_out = 1;
  739. }
  740. if (end_time && !timed_out)
  741. slack = select_estimate_accuracy(end_time);
  742. for (;;) {
  743. struct poll_list *walk;
  744. bool can_busy_loop = false;
  745. for (walk = list; walk != NULL; walk = walk->next) {
  746. struct pollfd * pfd, * pfd_end;
  747. pfd = walk->entries;
  748. pfd_end = pfd + walk->len;
  749. for (; pfd != pfd_end; pfd++) {
  750. /*
  751. * Fish for events. If we found one, record it
  752. * and kill poll_table->_qproc, so we don't
  753. * needlessly register any other waiters after
  754. * this. They'll get immediately deregistered
  755. * when we break out and return.
  756. */
  757. if (do_pollfd(pfd, pt, &can_busy_loop,
  758. busy_flag)) {
  759. count++;
  760. pt->_qproc = NULL;
  761. /* found something, stop busy polling */
  762. busy_flag = 0;
  763. can_busy_loop = false;
  764. }
  765. }
  766. }
  767. /*
  768. * All waiters have already been registered, so don't provide
  769. * a poll_table->_qproc to them on the next loop iteration.
  770. */
  771. pt->_qproc = NULL;
  772. if (!count) {
  773. count = wait->error;
  774. if (signal_pending(current))
  775. count = -EINTR;
  776. }
  777. if (count || timed_out)
  778. break;
  779. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  780. if (can_busy_loop && !need_resched()) {
  781. if (!busy_start) {
  782. busy_start = busy_loop_current_time();
  783. continue;
  784. }
  785. if (!busy_loop_timeout(busy_start))
  786. continue;
  787. }
  788. busy_flag = 0;
  789. /*
  790. * If this is the first loop and we have a timeout
  791. * given, then we convert to ktime_t and set the to
  792. * pointer to the expiry value.
  793. */
  794. if (end_time && !to) {
  795. expire = timespec64_to_ktime(*end_time);
  796. to = &expire;
  797. }
  798. if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
  799. timed_out = 1;
  800. }
  801. return count;
  802. }
  803. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  804. sizeof(struct pollfd))
  805. static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  806. struct timespec64 *end_time)
  807. {
  808. struct poll_wqueues table;
  809. int err = -EFAULT, fdcount, len, size;
  810. /* Allocate small arguments on the stack to save memory and be
  811. faster - use long to make sure the buffer is aligned properly
  812. on 64 bit archs to avoid unaligned access */
  813. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  814. struct poll_list *const head = (struct poll_list *)stack_pps;
  815. struct poll_list *walk = head;
  816. unsigned long todo = nfds;
  817. if (nfds > rlimit(RLIMIT_NOFILE))
  818. return -EINVAL;
  819. len = min_t(unsigned int, nfds, N_STACK_PPS);
  820. for (;;) {
  821. walk->next = NULL;
  822. walk->len = len;
  823. if (!len)
  824. break;
  825. if (copy_from_user(walk->entries, ufds + nfds-todo,
  826. sizeof(struct pollfd) * walk->len))
  827. goto out_fds;
  828. todo -= walk->len;
  829. if (!todo)
  830. break;
  831. len = min(todo, POLLFD_PER_PAGE);
  832. size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
  833. walk = walk->next = kmalloc(size, GFP_KERNEL);
  834. if (!walk) {
  835. err = -ENOMEM;
  836. goto out_fds;
  837. }
  838. }
  839. poll_initwait(&table);
  840. fdcount = do_poll(head, &table, end_time);
  841. poll_freewait(&table);
  842. for (walk = head; walk; walk = walk->next) {
  843. struct pollfd *fds = walk->entries;
  844. int j;
  845. for (j = 0; j < walk->len; j++, ufds++)
  846. if (__put_user(fds[j].revents, &ufds->revents))
  847. goto out_fds;
  848. }
  849. err = fdcount;
  850. out_fds:
  851. walk = head->next;
  852. while (walk) {
  853. struct poll_list *pos = walk;
  854. walk = walk->next;
  855. kfree(pos);
  856. }
  857. return err;
  858. }
  859. static long do_restart_poll(struct restart_block *restart_block)
  860. {
  861. struct pollfd __user *ufds = restart_block->poll.ufds;
  862. int nfds = restart_block->poll.nfds;
  863. struct timespec64 *to = NULL, end_time;
  864. int ret;
  865. if (restart_block->poll.has_timeout) {
  866. end_time.tv_sec = restart_block->poll.tv_sec;
  867. end_time.tv_nsec = restart_block->poll.tv_nsec;
  868. to = &end_time;
  869. }
  870. ret = do_sys_poll(ufds, nfds, to);
  871. if (ret == -EINTR) {
  872. restart_block->fn = do_restart_poll;
  873. ret = -ERESTART_RESTARTBLOCK;
  874. }
  875. return ret;
  876. }
  877. SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
  878. int, timeout_msecs)
  879. {
  880. struct timespec64 end_time, *to = NULL;
  881. int ret;
  882. if (timeout_msecs >= 0) {
  883. to = &end_time;
  884. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  885. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  886. }
  887. ret = do_sys_poll(ufds, nfds, to);
  888. if (ret == -EINTR) {
  889. struct restart_block *restart_block;
  890. restart_block = &current->restart_block;
  891. restart_block->fn = do_restart_poll;
  892. restart_block->poll.ufds = ufds;
  893. restart_block->poll.nfds = nfds;
  894. if (timeout_msecs >= 0) {
  895. restart_block->poll.tv_sec = end_time.tv_sec;
  896. restart_block->poll.tv_nsec = end_time.tv_nsec;
  897. restart_block->poll.has_timeout = 1;
  898. } else
  899. restart_block->poll.has_timeout = 0;
  900. ret = -ERESTART_RESTARTBLOCK;
  901. }
  902. return ret;
  903. }
  904. SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
  905. struct timespec __user *, tsp, const sigset_t __user *, sigmask,
  906. size_t, sigsetsize)
  907. {
  908. sigset_t ksigmask, sigsaved;
  909. struct timespec64 ts, end_time, *to = NULL;
  910. int ret;
  911. if (tsp) {
  912. if (get_timespec64(&ts, tsp))
  913. return -EFAULT;
  914. to = &end_time;
  915. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  916. return -EINVAL;
  917. }
  918. if (sigmask) {
  919. /* XXX: Don't preclude handling different sized sigset_t's. */
  920. if (sigsetsize != sizeof(sigset_t))
  921. return -EINVAL;
  922. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  923. return -EFAULT;
  924. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  925. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  926. }
  927. ret = do_sys_poll(ufds, nfds, to);
  928. /* We can restart this syscall, usually */
  929. if (ret == -EINTR) {
  930. /*
  931. * Don't restore the signal mask yet. Let do_signal() deliver
  932. * the signal on the way back to userspace, before the signal
  933. * mask is restored.
  934. */
  935. if (sigmask) {
  936. memcpy(&current->saved_sigmask, &sigsaved,
  937. sizeof(sigsaved));
  938. set_restore_sigmask();
  939. }
  940. ret = -ERESTARTNOHAND;
  941. } else if (sigmask)
  942. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  943. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  944. return ret;
  945. }
  946. #ifdef CONFIG_COMPAT
  947. #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
  948. static
  949. int compat_poll_select_copy_remaining(struct timespec64 *end_time, void __user *p,
  950. int timeval, int ret)
  951. {
  952. struct timespec64 ts;
  953. if (!p)
  954. return ret;
  955. if (current->personality & STICKY_TIMEOUTS)
  956. goto sticky;
  957. /* No update for zero timeout */
  958. if (!end_time->tv_sec && !end_time->tv_nsec)
  959. return ret;
  960. ktime_get_ts64(&ts);
  961. ts = timespec64_sub(*end_time, ts);
  962. if (ts.tv_sec < 0)
  963. ts.tv_sec = ts.tv_nsec = 0;
  964. if (timeval) {
  965. struct compat_timeval rtv;
  966. rtv.tv_sec = ts.tv_sec;
  967. rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  968. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  969. return ret;
  970. } else {
  971. if (!compat_put_timespec64(&ts, p))
  972. return ret;
  973. }
  974. /*
  975. * If an application puts its timeval in read-only memory, we
  976. * don't want the Linux-specific update to the timeval to
  977. * cause a fault after the select has completed
  978. * successfully. However, because we're not updating the
  979. * timeval, we can't restart the system call.
  980. */
  981. sticky:
  982. if (ret == -ERESTARTNOHAND)
  983. ret = -EINTR;
  984. return ret;
  985. }
  986. /*
  987. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  988. * 64-bit unsigned longs.
  989. */
  990. static
  991. int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  992. unsigned long *fdset)
  993. {
  994. if (ufdset) {
  995. return compat_get_bitmap(fdset, ufdset, nr);
  996. } else {
  997. zero_fd_set(nr, fdset);
  998. return 0;
  999. }
  1000. }
  1001. static
  1002. int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1003. unsigned long *fdset)
  1004. {
  1005. if (!ufdset)
  1006. return 0;
  1007. return compat_put_bitmap(ufdset, fdset, nr);
  1008. }
  1009. /*
  1010. * This is a virtual copy of sys_select from fs/select.c and probably
  1011. * should be compared to it from time to time
  1012. */
  1013. /*
  1014. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  1015. * like to be certain this leads to no problems. So I return
  1016. * EINTR just for safety.
  1017. *
  1018. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  1019. * I'm trying ERESTARTNOHAND which restart only when you want to.
  1020. */
  1021. static int compat_core_sys_select(int n, compat_ulong_t __user *inp,
  1022. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1023. struct timespec64 *end_time)
  1024. {
  1025. fd_set_bits fds;
  1026. void *bits;
  1027. int size, max_fds, ret = -EINVAL;
  1028. struct fdtable *fdt;
  1029. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  1030. if (n < 0)
  1031. goto out_nofds;
  1032. /* max_fds can increase, so grab it once to avoid race */
  1033. rcu_read_lock();
  1034. fdt = files_fdtable(current->files);
  1035. max_fds = fdt->max_fds;
  1036. rcu_read_unlock();
  1037. if (n > max_fds)
  1038. n = max_fds;
  1039. /*
  1040. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  1041. * since we used fdset we need to allocate memory in units of
  1042. * long-words.
  1043. */
  1044. size = FDS_BYTES(n);
  1045. bits = stack_fds;
  1046. if (size > sizeof(stack_fds) / 6) {
  1047. bits = kmalloc(6 * size, GFP_KERNEL);
  1048. ret = -ENOMEM;
  1049. if (!bits)
  1050. goto out_nofds;
  1051. }
  1052. fds.in = (unsigned long *) bits;
  1053. fds.out = (unsigned long *) (bits + size);
  1054. fds.ex = (unsigned long *) (bits + 2*size);
  1055. fds.res_in = (unsigned long *) (bits + 3*size);
  1056. fds.res_out = (unsigned long *) (bits + 4*size);
  1057. fds.res_ex = (unsigned long *) (bits + 5*size);
  1058. if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
  1059. (ret = compat_get_fd_set(n, outp, fds.out)) ||
  1060. (ret = compat_get_fd_set(n, exp, fds.ex)))
  1061. goto out;
  1062. zero_fd_set(n, fds.res_in);
  1063. zero_fd_set(n, fds.res_out);
  1064. zero_fd_set(n, fds.res_ex);
  1065. ret = do_select(n, &fds, end_time);
  1066. if (ret < 0)
  1067. goto out;
  1068. if (!ret) {
  1069. ret = -ERESTARTNOHAND;
  1070. if (signal_pending(current))
  1071. goto out;
  1072. ret = 0;
  1073. }
  1074. if (compat_set_fd_set(n, inp, fds.res_in) ||
  1075. compat_set_fd_set(n, outp, fds.res_out) ||
  1076. compat_set_fd_set(n, exp, fds.res_ex))
  1077. ret = -EFAULT;
  1078. out:
  1079. if (bits != stack_fds)
  1080. kfree(bits);
  1081. out_nofds:
  1082. return ret;
  1083. }
  1084. static int do_compat_select(int n, compat_ulong_t __user *inp,
  1085. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1086. struct compat_timeval __user *tvp)
  1087. {
  1088. struct timespec64 end_time, *to = NULL;
  1089. struct compat_timeval tv;
  1090. int ret;
  1091. if (tvp) {
  1092. if (copy_from_user(&tv, tvp, sizeof(tv)))
  1093. return -EFAULT;
  1094. to = &end_time;
  1095. if (poll_select_set_timeout(to,
  1096. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  1097. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  1098. return -EINVAL;
  1099. }
  1100. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1101. ret = compat_poll_select_copy_remaining(&end_time, tvp, 1, ret);
  1102. return ret;
  1103. }
  1104. COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp,
  1105. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1106. struct compat_timeval __user *, tvp)
  1107. {
  1108. return do_compat_select(n, inp, outp, exp, tvp);
  1109. }
  1110. struct compat_sel_arg_struct {
  1111. compat_ulong_t n;
  1112. compat_uptr_t inp;
  1113. compat_uptr_t outp;
  1114. compat_uptr_t exp;
  1115. compat_uptr_t tvp;
  1116. };
  1117. COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg)
  1118. {
  1119. struct compat_sel_arg_struct a;
  1120. if (copy_from_user(&a, arg, sizeof(a)))
  1121. return -EFAULT;
  1122. return do_compat_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
  1123. compat_ptr(a.exp), compat_ptr(a.tvp));
  1124. }
  1125. static long do_compat_pselect(int n, compat_ulong_t __user *inp,
  1126. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1127. struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
  1128. compat_size_t sigsetsize)
  1129. {
  1130. sigset_t ksigmask, sigsaved;
  1131. struct timespec64 ts, end_time, *to = NULL;
  1132. int ret;
  1133. if (tsp) {
  1134. if (compat_get_timespec64(&ts, tsp))
  1135. return -EFAULT;
  1136. to = &end_time;
  1137. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1138. return -EINVAL;
  1139. }
  1140. if (sigmask) {
  1141. if (sigsetsize != sizeof(compat_sigset_t))
  1142. return -EINVAL;
  1143. if (get_compat_sigset(&ksigmask, sigmask))
  1144. return -EFAULT;
  1145. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1146. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1147. }
  1148. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1149. ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1150. if (ret == -ERESTARTNOHAND) {
  1151. /*
  1152. * Don't restore the signal mask yet. Let do_signal() deliver
  1153. * the signal on the way back to userspace, before the signal
  1154. * mask is restored.
  1155. */
  1156. if (sigmask) {
  1157. memcpy(&current->saved_sigmask, &sigsaved,
  1158. sizeof(sigsaved));
  1159. set_restore_sigmask();
  1160. }
  1161. } else if (sigmask)
  1162. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1163. return ret;
  1164. }
  1165. COMPAT_SYSCALL_DEFINE6(pselect6, int, n, compat_ulong_t __user *, inp,
  1166. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1167. struct compat_timespec __user *, tsp, void __user *, sig)
  1168. {
  1169. compat_size_t sigsetsize = 0;
  1170. compat_uptr_t up = 0;
  1171. if (sig) {
  1172. if (!access_ok(VERIFY_READ, sig,
  1173. sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
  1174. __get_user(up, (compat_uptr_t __user *)sig) ||
  1175. __get_user(sigsetsize,
  1176. (compat_size_t __user *)(sig+sizeof(up))))
  1177. return -EFAULT;
  1178. }
  1179. return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
  1180. sigsetsize);
  1181. }
  1182. COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds,
  1183. unsigned int, nfds, struct compat_timespec __user *, tsp,
  1184. const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
  1185. {
  1186. sigset_t ksigmask, sigsaved;
  1187. struct timespec64 ts, end_time, *to = NULL;
  1188. int ret;
  1189. if (tsp) {
  1190. if (compat_get_timespec64(&ts, tsp))
  1191. return -EFAULT;
  1192. to = &end_time;
  1193. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1194. return -EINVAL;
  1195. }
  1196. if (sigmask) {
  1197. if (sigsetsize != sizeof(compat_sigset_t))
  1198. return -EINVAL;
  1199. if (get_compat_sigset(&ksigmask, sigmask))
  1200. return -EFAULT;
  1201. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1202. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1203. }
  1204. ret = do_sys_poll(ufds, nfds, to);
  1205. /* We can restart this syscall, usually */
  1206. if (ret == -EINTR) {
  1207. /*
  1208. * Don't restore the signal mask yet. Let do_signal() deliver
  1209. * the signal on the way back to userspace, before the signal
  1210. * mask is restored.
  1211. */
  1212. if (sigmask) {
  1213. memcpy(&current->saved_sigmask, &sigsaved,
  1214. sizeof(sigsaved));
  1215. set_restore_sigmask();
  1216. }
  1217. ret = -ERESTARTNOHAND;
  1218. } else if (sigmask)
  1219. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1220. ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1221. return ret;
  1222. }
  1223. #endif