select.c 34 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/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. WRITE_ONCE(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 (!READ_ONCE(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 __poll_t select_poll_one(int fd, poll_table *wait, unsigned long in,
  398. unsigned long out, unsigned long bit,
  399. __poll_t ll_flag)
  400. {
  401. CLASS(fd, f)(fd);
  402. if (fd_empty(f))
  403. return EPOLLNVAL;
  404. wait->_key = POLLEX_SET | ll_flag;
  405. if (in & bit)
  406. wait->_key |= POLLIN_SET;
  407. if (out & bit)
  408. wait->_key |= POLLOUT_SET;
  409. return vfs_poll(fd_file(f), wait);
  410. }
  411. static noinline_for_stack int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
  412. {
  413. ktime_t expire, *to = NULL;
  414. struct poll_wqueues table;
  415. poll_table *wait;
  416. int retval, i, timed_out = 0;
  417. u64 slack = 0;
  418. __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  419. unsigned long busy_start = 0;
  420. rcu_read_lock();
  421. retval = max_select_fd(n, fds);
  422. rcu_read_unlock();
  423. if (retval < 0)
  424. return retval;
  425. n = retval;
  426. poll_initwait(&table);
  427. wait = &table.pt;
  428. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  429. wait->_qproc = NULL;
  430. timed_out = 1;
  431. }
  432. if (end_time && !timed_out)
  433. slack = select_estimate_accuracy(end_time);
  434. retval = 0;
  435. for (;;) {
  436. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  437. bool can_busy_loop = false;
  438. inp = fds->in; outp = fds->out; exp = fds->ex;
  439. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  440. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  441. unsigned long in, out, ex, all_bits, bit = 1, j;
  442. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  443. __poll_t mask;
  444. in = *inp++; out = *outp++; ex = *exp++;
  445. all_bits = in | out | ex;
  446. if (all_bits == 0) {
  447. i += BITS_PER_LONG;
  448. continue;
  449. }
  450. for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
  451. if (i >= n)
  452. break;
  453. if (!(bit & all_bits))
  454. continue;
  455. mask = select_poll_one(i, wait, in, out, bit,
  456. busy_flag);
  457. if ((mask & POLLIN_SET) && (in & bit)) {
  458. res_in |= bit;
  459. retval++;
  460. wait->_qproc = NULL;
  461. }
  462. if ((mask & POLLOUT_SET) && (out & bit)) {
  463. res_out |= bit;
  464. retval++;
  465. wait->_qproc = NULL;
  466. }
  467. if ((mask & POLLEX_SET) && (ex & bit)) {
  468. res_ex |= bit;
  469. retval++;
  470. wait->_qproc = NULL;
  471. }
  472. /* got something, stop busy polling */
  473. if (retval) {
  474. can_busy_loop = false;
  475. busy_flag = 0;
  476. /*
  477. * only remember a returned
  478. * POLL_BUSY_LOOP if we asked for it
  479. */
  480. } else if (busy_flag & mask)
  481. can_busy_loop = true;
  482. }
  483. if (res_in)
  484. *rinp = res_in;
  485. if (res_out)
  486. *routp = res_out;
  487. if (res_ex)
  488. *rexp = res_ex;
  489. cond_resched();
  490. }
  491. wait->_qproc = NULL;
  492. if (retval || timed_out || signal_pending(current))
  493. break;
  494. if (table.error) {
  495. retval = table.error;
  496. break;
  497. }
  498. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  499. if (can_busy_loop && !need_resched()) {
  500. if (!busy_start) {
  501. busy_start = busy_loop_current_time();
  502. continue;
  503. }
  504. if (!busy_loop_timeout(busy_start))
  505. continue;
  506. }
  507. busy_flag = 0;
  508. /*
  509. * If this is the first loop and we have a timeout
  510. * given, then we convert to ktime_t and set the to
  511. * pointer to the expiry value.
  512. */
  513. if (end_time && !to) {
  514. expire = timespec64_to_ktime(*end_time);
  515. to = &expire;
  516. }
  517. if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
  518. to, slack))
  519. timed_out = 1;
  520. }
  521. poll_freewait(&table);
  522. return retval;
  523. }
  524. /*
  525. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  526. * like to be certain this leads to no problems. So I return
  527. * EINTR just for safety.
  528. *
  529. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  530. * I'm trying ERESTARTNOHAND which restart only when you want to.
  531. */
  532. int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  533. fd_set __user *exp, struct timespec64 *end_time)
  534. {
  535. fd_set_bits fds;
  536. void *bits;
  537. int ret, max_fds;
  538. size_t size, alloc_size;
  539. struct fdtable *fdt;
  540. /* Allocate small arguments on the stack to save memory and be faster */
  541. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  542. ret = -EINVAL;
  543. if (unlikely(n < 0))
  544. goto out_nofds;
  545. /* max_fds can increase, so grab it once to avoid race */
  546. rcu_read_lock();
  547. fdt = files_fdtable(current->files);
  548. max_fds = fdt->max_fds;
  549. rcu_read_unlock();
  550. if (n > max_fds)
  551. n = max_fds;
  552. /*
  553. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  554. * since we used fdset we need to allocate memory in units of
  555. * long-words.
  556. */
  557. size = FDS_BYTES(n);
  558. bits = stack_fds;
  559. if (size > sizeof(stack_fds) / 6) {
  560. /* Not enough space in on-stack array; must use kmalloc */
  561. ret = -ENOMEM;
  562. if (size > (SIZE_MAX / 6))
  563. goto out_nofds;
  564. alloc_size = 6 * size;
  565. bits = kvmalloc(alloc_size, GFP_KERNEL);
  566. if (!bits)
  567. goto out_nofds;
  568. }
  569. fds.in = bits;
  570. fds.out = bits + size;
  571. fds.ex = bits + 2*size;
  572. fds.res_in = bits + 3*size;
  573. fds.res_out = bits + 4*size;
  574. fds.res_ex = bits + 5*size;
  575. if ((ret = get_fd_set(n, inp, fds.in)) ||
  576. (ret = get_fd_set(n, outp, fds.out)) ||
  577. (ret = get_fd_set(n, exp, fds.ex)))
  578. goto out;
  579. zero_fd_set(n, fds.res_in);
  580. zero_fd_set(n, fds.res_out);
  581. zero_fd_set(n, fds.res_ex);
  582. ret = do_select(n, &fds, end_time);
  583. if (ret < 0)
  584. goto out;
  585. if (!ret) {
  586. ret = -ERESTARTNOHAND;
  587. if (signal_pending(current))
  588. goto out;
  589. ret = 0;
  590. }
  591. if (set_fd_set(n, inp, fds.res_in) ||
  592. set_fd_set(n, outp, fds.res_out) ||
  593. set_fd_set(n, exp, fds.res_ex))
  594. ret = -EFAULT;
  595. out:
  596. if (bits != stack_fds)
  597. kvfree(bits);
  598. out_nofds:
  599. return ret;
  600. }
  601. static int kern_select(int n, fd_set __user *inp, fd_set __user *outp,
  602. fd_set __user *exp, struct __kernel_old_timeval __user *tvp)
  603. {
  604. struct timespec64 end_time, *to = NULL;
  605. struct __kernel_old_timeval tv;
  606. int ret;
  607. if (tvp) {
  608. if (copy_from_user(&tv, tvp, sizeof(tv)))
  609. return -EFAULT;
  610. to = &end_time;
  611. if (poll_select_set_timeout(to,
  612. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  613. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  614. return -EINVAL;
  615. }
  616. ret = core_sys_select(n, inp, outp, exp, to);
  617. return poll_select_finish(&end_time, tvp, PT_TIMEVAL, ret);
  618. }
  619. SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
  620. fd_set __user *, exp, struct __kernel_old_timeval __user *, tvp)
  621. {
  622. return kern_select(n, inp, outp, exp, tvp);
  623. }
  624. static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
  625. fd_set __user *exp, void __user *tsp,
  626. const sigset_t __user *sigmask, size_t sigsetsize,
  627. enum poll_time_type type)
  628. {
  629. struct timespec64 ts, end_time, *to = NULL;
  630. int ret;
  631. if (tsp) {
  632. switch (type) {
  633. case PT_TIMESPEC:
  634. if (get_timespec64(&ts, tsp))
  635. return -EFAULT;
  636. break;
  637. case PT_OLD_TIMESPEC:
  638. if (get_old_timespec32(&ts, tsp))
  639. return -EFAULT;
  640. break;
  641. default:
  642. BUG();
  643. }
  644. to = &end_time;
  645. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  646. return -EINVAL;
  647. }
  648. ret = set_user_sigmask(sigmask, sigsetsize);
  649. if (ret)
  650. return ret;
  651. ret = core_sys_select(n, inp, outp, exp, to);
  652. return poll_select_finish(&end_time, tsp, type, ret);
  653. }
  654. /*
  655. * Most architectures can't handle 7-argument syscalls. So we provide a
  656. * 6-argument version where the sixth argument is a pointer to a structure
  657. * which has a pointer to the sigset_t itself followed by a size_t containing
  658. * the sigset size.
  659. */
  660. struct sigset_argpack {
  661. sigset_t __user *p;
  662. size_t size;
  663. };
  664. static inline int get_sigset_argpack(struct sigset_argpack *to,
  665. struct sigset_argpack __user *from)
  666. {
  667. // the path is hot enough for overhead of copy_from_user() to matter
  668. if (from) {
  669. scoped_user_read_access(from, Efault) {
  670. unsafe_get_user(to->p, &from->p, Efault);
  671. unsafe_get_user(to->size, &from->size, Efault);
  672. }
  673. }
  674. return 0;
  675. Efault:
  676. return -EFAULT;
  677. }
  678. SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
  679. fd_set __user *, exp, struct __kernel_timespec __user *, tsp,
  680. void __user *, sig)
  681. {
  682. struct sigset_argpack x = {NULL, 0};
  683. if (get_sigset_argpack(&x, sig))
  684. return -EFAULT;
  685. return do_pselect(n, inp, outp, exp, tsp, x.p, x.size, PT_TIMESPEC);
  686. }
  687. #if defined(CONFIG_COMPAT_32BIT_TIME) && !defined(CONFIG_64BIT)
  688. SYSCALL_DEFINE6(pselect6_time32, int, n, fd_set __user *, inp, fd_set __user *, outp,
  689. fd_set __user *, exp, struct old_timespec32 __user *, tsp,
  690. void __user *, sig)
  691. {
  692. struct sigset_argpack x = {NULL, 0};
  693. if (get_sigset_argpack(&x, sig))
  694. return -EFAULT;
  695. return do_pselect(n, inp, outp, exp, tsp, x.p, x.size, PT_OLD_TIMESPEC);
  696. }
  697. #endif
  698. #ifdef __ARCH_WANT_SYS_OLD_SELECT
  699. struct sel_arg_struct {
  700. unsigned long n;
  701. fd_set __user *inp, *outp, *exp;
  702. struct __kernel_old_timeval __user *tvp;
  703. };
  704. SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
  705. {
  706. struct sel_arg_struct a;
  707. if (copy_from_user(&a, arg, sizeof(a)))
  708. return -EFAULT;
  709. return kern_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  710. }
  711. #endif
  712. struct poll_list {
  713. struct poll_list *next;
  714. unsigned int len;
  715. struct pollfd entries[] __counted_by(len);
  716. };
  717. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  718. /*
  719. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  720. * interested in events matching the pollfd->events mask, and the result
  721. * matching that mask is both recorded in pollfd->revents and returned. The
  722. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  723. * if pwait->_qproc is non-NULL.
  724. */
  725. static inline __poll_t do_pollfd(struct pollfd *pollfd, poll_table *pwait,
  726. bool *can_busy_poll,
  727. __poll_t busy_flag)
  728. {
  729. int fd = pollfd->fd;
  730. __poll_t mask, filter;
  731. if (unlikely(fd < 0))
  732. return 0;
  733. CLASS(fd, f)(fd);
  734. if (fd_empty(f))
  735. return EPOLLNVAL;
  736. /* userland u16 ->events contains POLL... bitmap */
  737. filter = demangle_poll(pollfd->events) | EPOLLERR | EPOLLHUP;
  738. pwait->_key = filter | busy_flag;
  739. mask = vfs_poll(fd_file(f), pwait);
  740. if (mask & busy_flag)
  741. *can_busy_poll = true;
  742. return mask & filter; /* Mask out unneeded events. */
  743. }
  744. static int do_poll(struct poll_list *list, struct poll_wqueues *wait,
  745. struct timespec64 *end_time)
  746. {
  747. poll_table* pt = &wait->pt;
  748. ktime_t expire, *to = NULL;
  749. int timed_out = 0, count = 0;
  750. u64 slack = 0;
  751. __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  752. unsigned long busy_start = 0;
  753. /* Optimise the no-wait case */
  754. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  755. pt->_qproc = NULL;
  756. timed_out = 1;
  757. }
  758. if (end_time && !timed_out)
  759. slack = select_estimate_accuracy(end_time);
  760. for (;;) {
  761. struct poll_list *walk;
  762. bool can_busy_loop = false;
  763. for (walk = list; walk != NULL; walk = walk->next) {
  764. struct pollfd * pfd, * pfd_end;
  765. pfd = walk->entries;
  766. pfd_end = pfd + walk->len;
  767. for (; pfd != pfd_end; pfd++) {
  768. __poll_t mask;
  769. /*
  770. * Fish for events. If we found one, record it
  771. * and kill poll_table->_qproc, so we don't
  772. * needlessly register any other waiters after
  773. * this. They'll get immediately deregistered
  774. * when we break out and return.
  775. */
  776. mask = do_pollfd(pfd, pt, &can_busy_loop, busy_flag);
  777. pfd->revents = mangle_poll(mask);
  778. if (mask) {
  779. count++;
  780. pt->_qproc = NULL;
  781. /* found something, stop busy polling */
  782. busy_flag = 0;
  783. can_busy_loop = false;
  784. }
  785. }
  786. }
  787. /*
  788. * All waiters have already been registered, so don't provide
  789. * a poll_table->_qproc to them on the next loop iteration.
  790. */
  791. pt->_qproc = NULL;
  792. if (!count) {
  793. count = wait->error;
  794. if (signal_pending(current))
  795. count = -ERESTARTNOHAND;
  796. }
  797. if (count || timed_out)
  798. break;
  799. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  800. if (can_busy_loop && !need_resched()) {
  801. if (!busy_start) {
  802. busy_start = busy_loop_current_time();
  803. continue;
  804. }
  805. if (!busy_loop_timeout(busy_start))
  806. continue;
  807. }
  808. busy_flag = 0;
  809. /*
  810. * If this is the first loop and we have a timeout
  811. * given, then we convert to ktime_t and set the to
  812. * pointer to the expiry value.
  813. */
  814. if (end_time && !to) {
  815. expire = timespec64_to_ktime(*end_time);
  816. to = &expire;
  817. }
  818. if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
  819. timed_out = 1;
  820. }
  821. return count;
  822. }
  823. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  824. sizeof(struct pollfd))
  825. static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  826. struct timespec64 *end_time)
  827. {
  828. struct poll_wqueues table;
  829. int err = -EFAULT, fdcount;
  830. /* Allocate small arguments on the stack to save memory and be
  831. faster - use long to make sure the buffer is aligned properly
  832. on 64 bit archs to avoid unaligned access */
  833. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  834. struct poll_list *const head = (struct poll_list *)stack_pps;
  835. struct poll_list *walk = head;
  836. unsigned int todo = nfds;
  837. unsigned int len;
  838. if (nfds > rlimit(RLIMIT_NOFILE))
  839. return -EINVAL;
  840. len = min_t(unsigned int, nfds, N_STACK_PPS);
  841. for (;;) {
  842. walk->next = NULL;
  843. walk->len = len;
  844. if (!len)
  845. break;
  846. if (copy_from_user(walk->entries, ufds + nfds-todo,
  847. sizeof(struct pollfd) * walk->len))
  848. goto out_fds;
  849. if (walk->len >= todo)
  850. break;
  851. todo -= walk->len;
  852. len = min(todo, POLLFD_PER_PAGE);
  853. walk = walk->next = kmalloc_flex(*walk, entries, len);
  854. if (!walk) {
  855. err = -ENOMEM;
  856. goto out_fds;
  857. }
  858. }
  859. poll_initwait(&table);
  860. fdcount = do_poll(head, &table, end_time);
  861. poll_freewait(&table);
  862. if (!user_write_access_begin(ufds, nfds * sizeof(*ufds)))
  863. goto out_fds;
  864. for (walk = head; walk; walk = walk->next) {
  865. struct pollfd *fds = walk->entries;
  866. unsigned int j;
  867. for (j = walk->len; j; fds++, ufds++, j--)
  868. unsafe_put_user(fds->revents, &ufds->revents, Efault);
  869. }
  870. user_write_access_end();
  871. err = fdcount;
  872. out_fds:
  873. walk = head->next;
  874. while (walk) {
  875. struct poll_list *pos = walk;
  876. walk = walk->next;
  877. kfree(pos);
  878. }
  879. return err;
  880. Efault:
  881. user_write_access_end();
  882. err = -EFAULT;
  883. goto out_fds;
  884. }
  885. static long do_restart_poll(struct restart_block *restart_block)
  886. {
  887. struct pollfd __user *ufds = restart_block->poll.ufds;
  888. int nfds = restart_block->poll.nfds;
  889. struct timespec64 *to = NULL;
  890. int ret;
  891. if (restart_block->poll.has_timeout)
  892. to = &restart_block->poll.end_time;
  893. ret = do_sys_poll(ufds, nfds, to);
  894. if (ret == -ERESTARTNOHAND)
  895. ret = set_restart_fn(restart_block, do_restart_poll);
  896. return ret;
  897. }
  898. SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
  899. int, timeout_msecs)
  900. {
  901. struct timespec64 end_time, *to = NULL;
  902. int ret;
  903. if (timeout_msecs >= 0) {
  904. to = &end_time;
  905. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  906. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  907. }
  908. ret = do_sys_poll(ufds, nfds, to);
  909. if (ret == -ERESTARTNOHAND) {
  910. struct restart_block *restart_block;
  911. restart_block = &current->restart_block;
  912. restart_block->poll.ufds = ufds;
  913. restart_block->poll.nfds = nfds;
  914. if (timeout_msecs >= 0) {
  915. restart_block->poll.end_time = end_time;
  916. restart_block->poll.has_timeout = 1;
  917. } else
  918. restart_block->poll.has_timeout = 0;
  919. ret = set_restart_fn(restart_block, do_restart_poll);
  920. }
  921. return ret;
  922. }
  923. SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
  924. struct __kernel_timespec __user *, tsp, const sigset_t __user *, sigmask,
  925. size_t, sigsetsize)
  926. {
  927. struct timespec64 ts, end_time, *to = NULL;
  928. int ret;
  929. if (tsp) {
  930. if (get_timespec64(&ts, tsp))
  931. return -EFAULT;
  932. to = &end_time;
  933. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  934. return -EINVAL;
  935. }
  936. ret = set_user_sigmask(sigmask, sigsetsize);
  937. if (ret)
  938. return ret;
  939. ret = do_sys_poll(ufds, nfds, to);
  940. return poll_select_finish(&end_time, tsp, PT_TIMESPEC, ret);
  941. }
  942. #if defined(CONFIG_COMPAT_32BIT_TIME) && !defined(CONFIG_64BIT)
  943. SYSCALL_DEFINE5(ppoll_time32, struct pollfd __user *, ufds, unsigned int, nfds,
  944. struct old_timespec32 __user *, tsp, const sigset_t __user *, sigmask,
  945. size_t, sigsetsize)
  946. {
  947. struct timespec64 ts, end_time, *to = NULL;
  948. int ret;
  949. if (tsp) {
  950. if (get_old_timespec32(&ts, tsp))
  951. return -EFAULT;
  952. to = &end_time;
  953. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  954. return -EINVAL;
  955. }
  956. ret = set_user_sigmask(sigmask, sigsetsize);
  957. if (ret)
  958. return ret;
  959. ret = do_sys_poll(ufds, nfds, to);
  960. return poll_select_finish(&end_time, tsp, PT_OLD_TIMESPEC, ret);
  961. }
  962. #endif
  963. #ifdef CONFIG_COMPAT
  964. #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
  965. /*
  966. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  967. * 64-bit unsigned longs.
  968. */
  969. static
  970. int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  971. unsigned long *fdset)
  972. {
  973. if (ufdset) {
  974. return compat_get_bitmap(fdset, ufdset, nr);
  975. } else {
  976. zero_fd_set(nr, fdset);
  977. return 0;
  978. }
  979. }
  980. static
  981. int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  982. unsigned long *fdset)
  983. {
  984. if (!ufdset)
  985. return 0;
  986. return compat_put_bitmap(ufdset, fdset, nr);
  987. }
  988. /*
  989. * This is a virtual copy of sys_select from fs/select.c and probably
  990. * should be compared to it from time to time
  991. */
  992. /*
  993. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  994. * like to be certain this leads to no problems. So I return
  995. * EINTR just for safety.
  996. *
  997. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  998. * I'm trying ERESTARTNOHAND which restart only when you want to.
  999. */
  1000. static int compat_core_sys_select(int n, compat_ulong_t __user *inp,
  1001. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1002. struct timespec64 *end_time)
  1003. {
  1004. fd_set_bits fds;
  1005. void *bits;
  1006. int size, max_fds, ret = -EINVAL;
  1007. struct fdtable *fdt;
  1008. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  1009. if (n < 0)
  1010. goto out_nofds;
  1011. /* max_fds can increase, so grab it once to avoid race */
  1012. rcu_read_lock();
  1013. fdt = files_fdtable(current->files);
  1014. max_fds = fdt->max_fds;
  1015. rcu_read_unlock();
  1016. if (n > max_fds)
  1017. n = max_fds;
  1018. /*
  1019. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  1020. * since we used fdset we need to allocate memory in units of
  1021. * long-words.
  1022. */
  1023. size = FDS_BYTES(n);
  1024. bits = stack_fds;
  1025. if (size > sizeof(stack_fds) / 6) {
  1026. bits = kmalloc_array(6, size, GFP_KERNEL);
  1027. ret = -ENOMEM;
  1028. if (!bits)
  1029. goto out_nofds;
  1030. }
  1031. fds.in = (unsigned long *) bits;
  1032. fds.out = (unsigned long *) (bits + size);
  1033. fds.ex = (unsigned long *) (bits + 2*size);
  1034. fds.res_in = (unsigned long *) (bits + 3*size);
  1035. fds.res_out = (unsigned long *) (bits + 4*size);
  1036. fds.res_ex = (unsigned long *) (bits + 5*size);
  1037. if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
  1038. (ret = compat_get_fd_set(n, outp, fds.out)) ||
  1039. (ret = compat_get_fd_set(n, exp, fds.ex)))
  1040. goto out;
  1041. zero_fd_set(n, fds.res_in);
  1042. zero_fd_set(n, fds.res_out);
  1043. zero_fd_set(n, fds.res_ex);
  1044. ret = do_select(n, &fds, end_time);
  1045. if (ret < 0)
  1046. goto out;
  1047. if (!ret) {
  1048. ret = -ERESTARTNOHAND;
  1049. if (signal_pending(current))
  1050. goto out;
  1051. ret = 0;
  1052. }
  1053. if (compat_set_fd_set(n, inp, fds.res_in) ||
  1054. compat_set_fd_set(n, outp, fds.res_out) ||
  1055. compat_set_fd_set(n, exp, fds.res_ex))
  1056. ret = -EFAULT;
  1057. out:
  1058. if (bits != stack_fds)
  1059. kfree(bits);
  1060. out_nofds:
  1061. return ret;
  1062. }
  1063. static int do_compat_select(int n, compat_ulong_t __user *inp,
  1064. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1065. struct old_timeval32 __user *tvp)
  1066. {
  1067. struct timespec64 end_time, *to = NULL;
  1068. struct old_timeval32 tv;
  1069. int ret;
  1070. if (tvp) {
  1071. if (copy_from_user(&tv, tvp, sizeof(tv)))
  1072. return -EFAULT;
  1073. to = &end_time;
  1074. if (poll_select_set_timeout(to,
  1075. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  1076. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  1077. return -EINVAL;
  1078. }
  1079. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1080. return poll_select_finish(&end_time, tvp, PT_OLD_TIMEVAL, ret);
  1081. }
  1082. COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp,
  1083. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1084. struct old_timeval32 __user *, tvp)
  1085. {
  1086. return do_compat_select(n, inp, outp, exp, tvp);
  1087. }
  1088. struct compat_sel_arg_struct {
  1089. compat_ulong_t n;
  1090. compat_uptr_t inp;
  1091. compat_uptr_t outp;
  1092. compat_uptr_t exp;
  1093. compat_uptr_t tvp;
  1094. };
  1095. COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg)
  1096. {
  1097. struct compat_sel_arg_struct a;
  1098. if (copy_from_user(&a, arg, sizeof(a)))
  1099. return -EFAULT;
  1100. return do_compat_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
  1101. compat_ptr(a.exp), compat_ptr(a.tvp));
  1102. }
  1103. static long do_compat_pselect(int n, compat_ulong_t __user *inp,
  1104. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1105. void __user *tsp, compat_sigset_t __user *sigmask,
  1106. compat_size_t sigsetsize, enum poll_time_type type)
  1107. {
  1108. struct timespec64 ts, end_time, *to = NULL;
  1109. int ret;
  1110. if (tsp) {
  1111. switch (type) {
  1112. case PT_OLD_TIMESPEC:
  1113. if (get_old_timespec32(&ts, tsp))
  1114. return -EFAULT;
  1115. break;
  1116. case PT_TIMESPEC:
  1117. if (get_timespec64(&ts, tsp))
  1118. return -EFAULT;
  1119. break;
  1120. default:
  1121. BUG();
  1122. }
  1123. to = &end_time;
  1124. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1125. return -EINVAL;
  1126. }
  1127. ret = set_compat_user_sigmask(sigmask, sigsetsize);
  1128. if (ret)
  1129. return ret;
  1130. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1131. return poll_select_finish(&end_time, tsp, type, ret);
  1132. }
  1133. struct compat_sigset_argpack {
  1134. compat_uptr_t p;
  1135. compat_size_t size;
  1136. };
  1137. static inline int get_compat_sigset_argpack(struct compat_sigset_argpack *to,
  1138. struct compat_sigset_argpack __user *from)
  1139. {
  1140. if (from) {
  1141. if (!user_read_access_begin(from, sizeof(*from)))
  1142. return -EFAULT;
  1143. unsafe_get_user(to->p, &from->p, Efault);
  1144. unsafe_get_user(to->size, &from->size, Efault);
  1145. user_read_access_end();
  1146. }
  1147. return 0;
  1148. Efault:
  1149. user_read_access_end();
  1150. return -EFAULT;
  1151. }
  1152. COMPAT_SYSCALL_DEFINE6(pselect6_time64, int, n, compat_ulong_t __user *, inp,
  1153. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1154. struct __kernel_timespec __user *, tsp, void __user *, sig)
  1155. {
  1156. struct compat_sigset_argpack x = {0, 0};
  1157. if (get_compat_sigset_argpack(&x, sig))
  1158. return -EFAULT;
  1159. return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(x.p),
  1160. x.size, PT_TIMESPEC);
  1161. }
  1162. #if defined(CONFIG_COMPAT_32BIT_TIME)
  1163. COMPAT_SYSCALL_DEFINE6(pselect6_time32, int, n, compat_ulong_t __user *, inp,
  1164. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1165. struct old_timespec32 __user *, tsp, void __user *, sig)
  1166. {
  1167. struct compat_sigset_argpack x = {0, 0};
  1168. if (get_compat_sigset_argpack(&x, sig))
  1169. return -EFAULT;
  1170. return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(x.p),
  1171. x.size, PT_OLD_TIMESPEC);
  1172. }
  1173. #endif
  1174. #if defined(CONFIG_COMPAT_32BIT_TIME)
  1175. COMPAT_SYSCALL_DEFINE5(ppoll_time32, struct pollfd __user *, ufds,
  1176. unsigned int, nfds, struct old_timespec32 __user *, tsp,
  1177. const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
  1178. {
  1179. struct timespec64 ts, end_time, *to = NULL;
  1180. int ret;
  1181. if (tsp) {
  1182. if (get_old_timespec32(&ts, tsp))
  1183. return -EFAULT;
  1184. to = &end_time;
  1185. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1186. return -EINVAL;
  1187. }
  1188. ret = set_compat_user_sigmask(sigmask, sigsetsize);
  1189. if (ret)
  1190. return ret;
  1191. ret = do_sys_poll(ufds, nfds, to);
  1192. return poll_select_finish(&end_time, tsp, PT_OLD_TIMESPEC, ret);
  1193. }
  1194. #endif
  1195. /* New compat syscall for 64 bit time_t*/
  1196. COMPAT_SYSCALL_DEFINE5(ppoll_time64, struct pollfd __user *, ufds,
  1197. unsigned int, nfds, struct __kernel_timespec __user *, tsp,
  1198. const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
  1199. {
  1200. struct timespec64 ts, end_time, *to = NULL;
  1201. int ret;
  1202. if (tsp) {
  1203. if (get_timespec64(&ts, tsp))
  1204. return -EFAULT;
  1205. to = &end_time;
  1206. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1207. return -EINVAL;
  1208. }
  1209. ret = set_compat_user_sigmask(sigmask, sigsetsize);
  1210. if (ret)
  1211. return ret;
  1212. ret = do_sys_poll(ufds, nfds, to);
  1213. return poll_select_finish(&end_time, tsp, PT_TIMESPEC, ret);
  1214. }
  1215. #endif