select.c 35 KB

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