select.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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. /*
  68. * Realtime tasks get a slack of 0 for obvious reasons.
  69. */
  70. if (rt_task(current))
  71. return 0;
  72. ktime_get_ts64(&now);
  73. now = timespec64_sub(*tv, now);
  74. ret = __estimate_accuracy(&now);
  75. if (ret < current->timer_slack_ns)
  76. return current->timer_slack_ns;
  77. return ret;
  78. }
  79. struct poll_table_page {
  80. struct poll_table_page * next;
  81. struct poll_table_entry * entry;
  82. struct poll_table_entry entries[];
  83. };
  84. #define POLL_TABLE_FULL(table) \
  85. ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
  86. /*
  87. * Ok, Peter made a complicated, but straightforward multiple_wait() function.
  88. * I have rewritten this, taking some shortcuts: This code may not be easy to
  89. * follow, but it should be free of race-conditions, and it's practical. If you
  90. * understand what I'm doing here, then you understand how the linux
  91. * sleep/wakeup mechanism works.
  92. *
  93. * Two very simple procedures, poll_wait() and poll_freewait() make all the
  94. * work. poll_wait() is an inline-function defined in <linux/poll.h>,
  95. * as all select/poll functions have to call it to add an entry to the
  96. * poll table.
  97. */
  98. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  99. poll_table *p);
  100. void poll_initwait(struct poll_wqueues *pwq)
  101. {
  102. init_poll_funcptr(&pwq->pt, __pollwait);
  103. pwq->polling_task = current;
  104. pwq->triggered = 0;
  105. pwq->error = 0;
  106. pwq->table = NULL;
  107. pwq->inline_index = 0;
  108. }
  109. EXPORT_SYMBOL(poll_initwait);
  110. static void free_poll_entry(struct poll_table_entry *entry)
  111. {
  112. remove_wait_queue(entry->wait_address, &entry->wait);
  113. fput(entry->filp);
  114. }
  115. void poll_freewait(struct poll_wqueues *pwq)
  116. {
  117. struct poll_table_page * p = pwq->table;
  118. int i;
  119. for (i = 0; i < pwq->inline_index; i++)
  120. free_poll_entry(pwq->inline_entries + i);
  121. while (p) {
  122. struct poll_table_entry * entry;
  123. struct poll_table_page *old;
  124. entry = p->entry;
  125. do {
  126. entry--;
  127. free_poll_entry(entry);
  128. } while (entry > p->entries);
  129. old = p;
  130. p = p->next;
  131. free_page((unsigned long) old);
  132. }
  133. }
  134. EXPORT_SYMBOL(poll_freewait);
  135. static struct poll_table_entry *poll_get_entry(struct poll_wqueues *p)
  136. {
  137. struct poll_table_page *table = p->table;
  138. if (p->inline_index < N_INLINE_POLL_ENTRIES)
  139. return p->inline_entries + p->inline_index++;
  140. if (!table || POLL_TABLE_FULL(table)) {
  141. struct poll_table_page *new_table;
  142. new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
  143. if (!new_table) {
  144. p->error = -ENOMEM;
  145. return NULL;
  146. }
  147. new_table->entry = new_table->entries;
  148. new_table->next = table;
  149. p->table = new_table;
  150. table = new_table;
  151. }
  152. return table->entry++;
  153. }
  154. static int __pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
  155. {
  156. struct poll_wqueues *pwq = wait->private;
  157. DECLARE_WAITQUEUE(dummy_wait, pwq->polling_task);
  158. /*
  159. * Although this function is called under waitqueue lock, LOCK
  160. * doesn't imply write barrier and the users expect write
  161. * barrier semantics on wakeup functions. The following
  162. * smp_wmb() is equivalent to smp_wmb() in try_to_wake_up()
  163. * and is paired with smp_store_mb() in poll_schedule_timeout.
  164. */
  165. smp_wmb();
  166. pwq->triggered = 1;
  167. /*
  168. * Perform the default wake up operation using a dummy
  169. * waitqueue.
  170. *
  171. * TODO: This is hacky but there currently is no interface to
  172. * pass in @sync. @sync is scheduled to be removed and once
  173. * that happens, wake_up_process() can be used directly.
  174. */
  175. return default_wake_function(&dummy_wait, mode, sync, key);
  176. }
  177. static int pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
  178. {
  179. struct poll_table_entry *entry;
  180. entry = container_of(wait, struct poll_table_entry, wait);
  181. if (key && !(key_to_poll(key) & entry->key))
  182. return 0;
  183. return __pollwake(wait, mode, sync, key);
  184. }
  185. /* Add a new entry */
  186. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  187. poll_table *p)
  188. {
  189. struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);
  190. struct poll_table_entry *entry = poll_get_entry(pwq);
  191. if (!entry)
  192. return;
  193. entry->filp = get_file(filp);
  194. entry->wait_address = wait_address;
  195. entry->key = p->_key;
  196. init_waitqueue_func_entry(&entry->wait, pollwake);
  197. entry->wait.private = pwq;
  198. add_wait_queue(wait_address, &entry->wait);
  199. }
  200. static int poll_schedule_timeout(struct poll_wqueues *pwq, int state,
  201. ktime_t *expires, unsigned long slack)
  202. {
  203. int rc = -EINTR;
  204. set_current_state(state);
  205. if (!pwq->triggered)
  206. rc = schedule_hrtimeout_range(expires, slack, HRTIMER_MODE_ABS);
  207. __set_current_state(TASK_RUNNING);
  208. /*
  209. * Prepare for the next iteration.
  210. *
  211. * The following smp_store_mb() serves two purposes. First, it's
  212. * the counterpart rmb of the wmb in pollwake() such that data
  213. * written before wake up is always visible after wake up.
  214. * Second, the full barrier guarantees that triggered clearing
  215. * doesn't pass event check of the next iteration. Note that
  216. * this problem doesn't exist for the first iteration as
  217. * add_wait_queue() has full barrier semantics.
  218. */
  219. smp_store_mb(pwq->triggered, 0);
  220. return rc;
  221. }
  222. /**
  223. * poll_select_set_timeout - helper function to setup the timeout value
  224. * @to: pointer to timespec64 variable for the final timeout
  225. * @sec: seconds (from user space)
  226. * @nsec: nanoseconds (from user space)
  227. *
  228. * Note, we do not use a timespec for the user space value here, That
  229. * way we can use the function for timeval and compat interfaces as well.
  230. *
  231. * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0.
  232. */
  233. int poll_select_set_timeout(struct timespec64 *to, time64_t sec, long nsec)
  234. {
  235. struct timespec64 ts = {.tv_sec = sec, .tv_nsec = nsec};
  236. if (!timespec64_valid(&ts))
  237. return -EINVAL;
  238. /* Optimize for the zero timeout value here */
  239. if (!sec && !nsec) {
  240. to->tv_sec = to->tv_nsec = 0;
  241. } else {
  242. ktime_get_ts64(to);
  243. *to = timespec64_add_safe(*to, ts);
  244. }
  245. return 0;
  246. }
  247. enum poll_time_type {
  248. PT_TIMEVAL = 0,
  249. PT_OLD_TIMEVAL = 1,
  250. PT_TIMESPEC = 2,
  251. PT_OLD_TIMESPEC = 3,
  252. };
  253. static int poll_select_finish(struct timespec64 *end_time,
  254. void __user *p,
  255. enum poll_time_type pt_type, int ret)
  256. {
  257. struct timespec64 rts;
  258. restore_saved_sigmask_unless(ret == -ERESTARTNOHAND);
  259. if (!p)
  260. return ret;
  261. if (current->personality & STICKY_TIMEOUTS)
  262. goto sticky;
  263. /* No update for zero timeout */
  264. if (!end_time->tv_sec && !end_time->tv_nsec)
  265. return ret;
  266. ktime_get_ts64(&rts);
  267. rts = timespec64_sub(*end_time, rts);
  268. if (rts.tv_sec < 0)
  269. rts.tv_sec = rts.tv_nsec = 0;
  270. switch (pt_type) {
  271. case PT_TIMEVAL:
  272. {
  273. struct __kernel_old_timeval rtv;
  274. if (sizeof(rtv) > sizeof(rtv.tv_sec) + sizeof(rtv.tv_usec))
  275. memset(&rtv, 0, sizeof(rtv));
  276. rtv.tv_sec = rts.tv_sec;
  277. rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
  278. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  279. return ret;
  280. }
  281. break;
  282. case PT_OLD_TIMEVAL:
  283. {
  284. struct old_timeval32 rtv;
  285. rtv.tv_sec = rts.tv_sec;
  286. rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
  287. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  288. return ret;
  289. }
  290. break;
  291. case PT_TIMESPEC:
  292. if (!put_timespec64(&rts, p))
  293. return ret;
  294. break;
  295. case PT_OLD_TIMESPEC:
  296. if (!put_old_timespec32(&rts, p))
  297. return ret;
  298. break;
  299. default:
  300. BUG();
  301. }
  302. /*
  303. * If an application puts its timeval in read-only memory, we
  304. * don't want the Linux-specific update to the timeval to
  305. * cause a fault after the select has completed
  306. * successfully. However, because we're not updating the
  307. * timeval, we can't restart the system call.
  308. */
  309. sticky:
  310. if (ret == -ERESTARTNOHAND)
  311. ret = -EINTR;
  312. return ret;
  313. }
  314. /*
  315. * Scalable version of the fd_set.
  316. */
  317. typedef struct {
  318. unsigned long *in, *out, *ex;
  319. unsigned long *res_in, *res_out, *res_ex;
  320. } fd_set_bits;
  321. /*
  322. * How many longwords for "nr" bits?
  323. */
  324. #define FDS_BITPERLONG (8*sizeof(long))
  325. #define FDS_LONGS(nr) (((nr)+FDS_BITPERLONG-1)/FDS_BITPERLONG)
  326. #define FDS_BYTES(nr) (FDS_LONGS(nr)*sizeof(long))
  327. /*
  328. * Use "unsigned long" accesses to let user-mode fd_set's be long-aligned.
  329. */
  330. static inline
  331. int get_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
  332. {
  333. nr = FDS_BYTES(nr);
  334. if (ufdset)
  335. return copy_from_user(fdset, ufdset, nr) ? -EFAULT : 0;
  336. memset(fdset, 0, nr);
  337. return 0;
  338. }
  339. static inline unsigned long __must_check
  340. set_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
  341. {
  342. if (ufdset)
  343. return __copy_to_user(ufdset, fdset, FDS_BYTES(nr));
  344. return 0;
  345. }
  346. static inline
  347. void zero_fd_set(unsigned long nr, unsigned long *fdset)
  348. {
  349. memset(fdset, 0, FDS_BYTES(nr));
  350. }
  351. #define FDS_IN(fds, n) (fds->in + n)
  352. #define FDS_OUT(fds, n) (fds->out + n)
  353. #define FDS_EX(fds, n) (fds->ex + n)
  354. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  355. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  356. {
  357. unsigned long *open_fds;
  358. unsigned long set;
  359. int max;
  360. struct fdtable *fdt;
  361. /* handle last in-complete long-word first */
  362. set = ~(~0UL << (n & (BITS_PER_LONG-1)));
  363. n /= BITS_PER_LONG;
  364. fdt = files_fdtable(current->files);
  365. open_fds = fdt->open_fds + n;
  366. max = 0;
  367. if (set) {
  368. set &= BITS(fds, n);
  369. if (set) {
  370. if (!(set & ~*open_fds))
  371. goto get_max;
  372. return -EBADF;
  373. }
  374. }
  375. while (n) {
  376. open_fds--;
  377. n--;
  378. set = BITS(fds, n);
  379. if (!set)
  380. continue;
  381. if (set & ~*open_fds)
  382. return -EBADF;
  383. if (max)
  384. continue;
  385. get_max:
  386. do {
  387. max++;
  388. set >>= 1;
  389. } while (set);
  390. max += n * BITS_PER_LONG;
  391. }
  392. return max;
  393. }
  394. #define POLLIN_SET (EPOLLRDNORM | EPOLLRDBAND | EPOLLIN | EPOLLHUP | EPOLLERR |\
  395. EPOLLNVAL)
  396. #define POLLOUT_SET (EPOLLWRBAND | EPOLLWRNORM | EPOLLOUT | EPOLLERR |\
  397. EPOLLNVAL)
  398. #define POLLEX_SET (EPOLLPRI | EPOLLNVAL)
  399. static inline void wait_key_set(poll_table *wait, unsigned long in,
  400. unsigned long out, unsigned long bit,
  401. __poll_t ll_flag)
  402. {
  403. wait->_key = POLLEX_SET | ll_flag;
  404. if (in & bit)
  405. wait->_key |= POLLIN_SET;
  406. if (out & bit)
  407. wait->_key |= POLLOUT_SET;
  408. }
  409. noinline_for_stack
  410. static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
  411. {
  412. ktime_t expire, *to = NULL;
  413. struct poll_wqueues table;
  414. poll_table *wait;
  415. int retval, i, timed_out = 0;
  416. u64 slack = 0;
  417. __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  418. unsigned long busy_start = 0;
  419. rcu_read_lock();
  420. retval = max_select_fd(n, fds);
  421. rcu_read_unlock();
  422. if (retval < 0)
  423. return retval;
  424. n = retval;
  425. poll_initwait(&table);
  426. wait = &table.pt;
  427. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  428. wait->_qproc = NULL;
  429. timed_out = 1;
  430. }
  431. if (end_time && !timed_out)
  432. slack = select_estimate_accuracy(end_time);
  433. retval = 0;
  434. for (;;) {
  435. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  436. bool can_busy_loop = false;
  437. inp = fds->in; outp = fds->out; exp = fds->ex;
  438. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  439. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  440. unsigned long in, out, ex, all_bits, bit = 1, j;
  441. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  442. __poll_t mask;
  443. in = *inp++; out = *outp++; ex = *exp++;
  444. all_bits = in | out | ex;
  445. if (all_bits == 0) {
  446. i += BITS_PER_LONG;
  447. continue;
  448. }
  449. for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
  450. struct fd f;
  451. if (i >= n)
  452. break;
  453. if (!(bit & all_bits))
  454. continue;
  455. mask = EPOLLNVAL;
  456. f = fdget(i);
  457. if (f.file) {
  458. wait_key_set(wait, in, out, bit,
  459. busy_flag);
  460. mask = vfs_poll(f.file, wait);
  461. fdput(f);
  462. }
  463. if ((mask & POLLIN_SET) && (in & bit)) {
  464. res_in |= bit;
  465. retval++;
  466. wait->_qproc = NULL;
  467. }
  468. if ((mask & POLLOUT_SET) && (out & bit)) {
  469. res_out |= bit;
  470. retval++;
  471. wait->_qproc = NULL;
  472. }
  473. if ((mask & POLLEX_SET) && (ex & bit)) {
  474. res_ex |= bit;
  475. retval++;
  476. wait->_qproc = NULL;
  477. }
  478. /* got something, stop busy polling */
  479. if (retval) {
  480. can_busy_loop = false;
  481. busy_flag = 0;
  482. /*
  483. * only remember a returned
  484. * POLL_BUSY_LOOP if we asked for it
  485. */
  486. } else if (busy_flag & mask)
  487. can_busy_loop = true;
  488. }
  489. if (res_in)
  490. *rinp = res_in;
  491. if (res_out)
  492. *routp = res_out;
  493. if (res_ex)
  494. *rexp = res_ex;
  495. cond_resched();
  496. }
  497. wait->_qproc = NULL;
  498. if (retval || timed_out || signal_pending(current))
  499. break;
  500. if (table.error) {
  501. retval = table.error;
  502. break;
  503. }
  504. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  505. if (can_busy_loop && !need_resched()) {
  506. if (!busy_start) {
  507. busy_start = busy_loop_current_time();
  508. continue;
  509. }
  510. if (!busy_loop_timeout(busy_start))
  511. continue;
  512. }
  513. busy_flag = 0;
  514. /*
  515. * If this is the first loop and we have a timeout
  516. * given, then we convert to ktime_t and set the to
  517. * pointer to the expiry value.
  518. */
  519. if (end_time && !to) {
  520. expire = timespec64_to_ktime(*end_time);
  521. to = &expire;
  522. }
  523. if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
  524. to, slack))
  525. timed_out = 1;
  526. }
  527. poll_freewait(&table);
  528. return retval;
  529. }
  530. /*
  531. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  532. * like to be certain this leads to no problems. So I return
  533. * EINTR just for safety.
  534. *
  535. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  536. * I'm trying ERESTARTNOHAND which restart only when you want to.
  537. */
  538. int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  539. fd_set __user *exp, struct timespec64 *end_time)
  540. {
  541. fd_set_bits fds;
  542. void *bits;
  543. int ret, max_fds;
  544. size_t size, alloc_size;
  545. struct fdtable *fdt;
  546. /* Allocate small arguments on the stack to save memory and be faster */
  547. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  548. ret = -EINVAL;
  549. if (n < 0)
  550. goto out_nofds;
  551. /* max_fds can increase, so grab it once to avoid race */
  552. rcu_read_lock();
  553. fdt = files_fdtable(current->files);
  554. max_fds = fdt->max_fds;
  555. rcu_read_unlock();
  556. if (n > max_fds)
  557. n = max_fds;
  558. /*
  559. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  560. * since we used fdset we need to allocate memory in units of
  561. * long-words.
  562. */
  563. size = FDS_BYTES(n);
  564. bits = stack_fds;
  565. if (size > sizeof(stack_fds) / 6) {
  566. /* Not enough space in on-stack array; must use kmalloc */
  567. ret = -ENOMEM;
  568. if (size > (SIZE_MAX / 6))
  569. goto out_nofds;
  570. alloc_size = 6 * size;
  571. bits = kvmalloc(alloc_size, GFP_KERNEL);
  572. if (!bits)
  573. goto out_nofds;
  574. }
  575. fds.in = bits;
  576. fds.out = bits + size;
  577. fds.ex = bits + 2*size;
  578. fds.res_in = bits + 3*size;
  579. fds.res_out = bits + 4*size;
  580. fds.res_ex = bits + 5*size;
  581. if ((ret = get_fd_set(n, inp, fds.in)) ||
  582. (ret = get_fd_set(n, outp, fds.out)) ||
  583. (ret = get_fd_set(n, exp, fds.ex)))
  584. goto out;
  585. zero_fd_set(n, fds.res_in);
  586. zero_fd_set(n, fds.res_out);
  587. zero_fd_set(n, fds.res_ex);
  588. ret = do_select(n, &fds, end_time);
  589. if (ret < 0)
  590. goto out;
  591. if (!ret) {
  592. ret = -ERESTARTNOHAND;
  593. if (signal_pending(current))
  594. goto out;
  595. ret = 0;
  596. }
  597. if (set_fd_set(n, inp, fds.res_in) ||
  598. set_fd_set(n, outp, fds.res_out) ||
  599. set_fd_set(n, exp, fds.res_ex))
  600. ret = -EFAULT;
  601. out:
  602. if (bits != stack_fds)
  603. kvfree(bits);
  604. out_nofds:
  605. return ret;
  606. }
  607. static int kern_select(int n, fd_set __user *inp, fd_set __user *outp,
  608. fd_set __user *exp, struct __kernel_old_timeval __user *tvp)
  609. {
  610. struct timespec64 end_time, *to = NULL;
  611. struct __kernel_old_timeval tv;
  612. int ret;
  613. if (tvp) {
  614. if (copy_from_user(&tv, tvp, sizeof(tv)))
  615. return -EFAULT;
  616. to = &end_time;
  617. if (poll_select_set_timeout(to,
  618. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  619. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  620. return -EINVAL;
  621. }
  622. ret = core_sys_select(n, inp, outp, exp, to);
  623. return poll_select_finish(&end_time, tvp, PT_TIMEVAL, ret);
  624. }
  625. SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
  626. fd_set __user *, exp, struct __kernel_old_timeval __user *, tvp)
  627. {
  628. return kern_select(n, inp, outp, exp, tvp);
  629. }
  630. static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
  631. fd_set __user *exp, void __user *tsp,
  632. const sigset_t __user *sigmask, size_t sigsetsize,
  633. enum poll_time_type type)
  634. {
  635. struct timespec64 ts, end_time, *to = NULL;
  636. int ret;
  637. if (tsp) {
  638. switch (type) {
  639. case PT_TIMESPEC:
  640. if (get_timespec64(&ts, tsp))
  641. return -EFAULT;
  642. break;
  643. case PT_OLD_TIMESPEC:
  644. if (get_old_timespec32(&ts, tsp))
  645. return -EFAULT;
  646. break;
  647. default:
  648. BUG();
  649. }
  650. to = &end_time;
  651. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  652. return -EINVAL;
  653. }
  654. ret = set_user_sigmask(sigmask, sigsetsize);
  655. if (ret)
  656. return ret;
  657. ret = core_sys_select(n, inp, outp, exp, to);
  658. return poll_select_finish(&end_time, tsp, type, ret);
  659. }
  660. /*
  661. * Most architectures can't handle 7-argument syscalls. So we provide a
  662. * 6-argument version where the sixth argument is a pointer to a structure
  663. * which has a pointer to the sigset_t itself followed by a size_t containing
  664. * the sigset size.
  665. */
  666. struct sigset_argpack {
  667. sigset_t __user *p;
  668. size_t size;
  669. };
  670. static inline int get_sigset_argpack(struct sigset_argpack *to,
  671. struct sigset_argpack __user *from)
  672. {
  673. // the path is hot enough for overhead of copy_from_user() to matter
  674. if (from) {
  675. if (!user_read_access_begin(from, sizeof(*from)))
  676. return -EFAULT;
  677. unsafe_get_user(to->p, &from->p, Efault);
  678. unsafe_get_user(to->size, &from->size, Efault);
  679. user_read_access_end();
  680. }
  681. return 0;
  682. Efault:
  683. user_access_end();
  684. return -EFAULT;
  685. }
  686. SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
  687. fd_set __user *, exp, struct __kernel_timespec __user *, tsp,
  688. void __user *, sig)
  689. {
  690. struct sigset_argpack x = {NULL, 0};
  691. if (get_sigset_argpack(&x, sig))
  692. return -EFAULT;
  693. return do_pselect(n, inp, outp, exp, tsp, x.p, x.size, PT_TIMESPEC);
  694. }
  695. #if defined(CONFIG_COMPAT_32BIT_TIME) && !defined(CONFIG_64BIT)
  696. SYSCALL_DEFINE6(pselect6_time32, int, n, fd_set __user *, inp, fd_set __user *, outp,
  697. fd_set __user *, exp, struct old_timespec32 __user *, tsp,
  698. void __user *, sig)
  699. {
  700. struct sigset_argpack x = {NULL, 0};
  701. if (get_sigset_argpack(&x, sig))
  702. return -EFAULT;
  703. return do_pselect(n, inp, outp, exp, tsp, x.p, x.size, PT_OLD_TIMESPEC);
  704. }
  705. #endif
  706. #ifdef __ARCH_WANT_SYS_OLD_SELECT
  707. struct sel_arg_struct {
  708. unsigned long n;
  709. fd_set __user *inp, *outp, *exp;
  710. struct __kernel_old_timeval __user *tvp;
  711. };
  712. SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
  713. {
  714. struct sel_arg_struct a;
  715. if (copy_from_user(&a, arg, sizeof(a)))
  716. return -EFAULT;
  717. return kern_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  718. }
  719. #endif
  720. struct poll_list {
  721. struct poll_list *next;
  722. int len;
  723. struct pollfd entries[];
  724. };
  725. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  726. /*
  727. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  728. * interested in events matching the pollfd->events mask, and the result
  729. * matching that mask is both recorded in pollfd->revents and returned. The
  730. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  731. * if pwait->_qproc is non-NULL.
  732. */
  733. static inline __poll_t do_pollfd(struct pollfd *pollfd, poll_table *pwait,
  734. bool *can_busy_poll,
  735. __poll_t busy_flag)
  736. {
  737. int fd = pollfd->fd;
  738. __poll_t mask = 0, filter;
  739. struct fd f;
  740. if (fd < 0)
  741. goto out;
  742. mask = EPOLLNVAL;
  743. f = fdget(fd);
  744. if (!f.file)
  745. goto out;
  746. /* userland u16 ->events contains POLL... bitmap */
  747. filter = demangle_poll(pollfd->events) | EPOLLERR | EPOLLHUP;
  748. pwait->_key = filter | busy_flag;
  749. mask = vfs_poll(f.file, pwait);
  750. if (mask & busy_flag)
  751. *can_busy_poll = true;
  752. mask &= filter; /* Mask out unneeded events. */
  753. fdput(f);
  754. out:
  755. /* ... and so does ->revents */
  756. pollfd->revents = mangle_poll(mask);
  757. return mask;
  758. }
  759. static int do_poll(struct poll_list *list, struct poll_wqueues *wait,
  760. struct timespec64 *end_time)
  761. {
  762. poll_table* pt = &wait->pt;
  763. ktime_t expire, *to = NULL;
  764. int timed_out = 0, count = 0;
  765. u64 slack = 0;
  766. __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  767. unsigned long busy_start = 0;
  768. /* Optimise the no-wait case */
  769. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  770. pt->_qproc = NULL;
  771. timed_out = 1;
  772. }
  773. if (end_time && !timed_out)
  774. slack = select_estimate_accuracy(end_time);
  775. for (;;) {
  776. struct poll_list *walk;
  777. bool can_busy_loop = false;
  778. for (walk = list; walk != NULL; walk = walk->next) {
  779. struct pollfd * pfd, * pfd_end;
  780. pfd = walk->entries;
  781. pfd_end = pfd + walk->len;
  782. for (; pfd != pfd_end; pfd++) {
  783. /*
  784. * Fish for events. If we found one, record it
  785. * and kill poll_table->_qproc, so we don't
  786. * needlessly register any other waiters after
  787. * this. They'll get immediately deregistered
  788. * when we break out and return.
  789. */
  790. if (do_pollfd(pfd, pt, &can_busy_loop,
  791. busy_flag)) {
  792. count++;
  793. pt->_qproc = NULL;
  794. /* found something, stop busy polling */
  795. busy_flag = 0;
  796. can_busy_loop = false;
  797. }
  798. }
  799. }
  800. /*
  801. * All waiters have already been registered, so don't provide
  802. * a poll_table->_qproc to them on the next loop iteration.
  803. */
  804. pt->_qproc = NULL;
  805. if (!count) {
  806. count = wait->error;
  807. if (signal_pending(current))
  808. count = -ERESTARTNOHAND;
  809. }
  810. if (count || timed_out)
  811. break;
  812. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  813. if (can_busy_loop && !need_resched()) {
  814. if (!busy_start) {
  815. busy_start = busy_loop_current_time();
  816. continue;
  817. }
  818. if (!busy_loop_timeout(busy_start))
  819. continue;
  820. }
  821. busy_flag = 0;
  822. /*
  823. * If this is the first loop and we have a timeout
  824. * given, then we convert to ktime_t and set the to
  825. * pointer to the expiry value.
  826. */
  827. if (end_time && !to) {
  828. expire = timespec64_to_ktime(*end_time);
  829. to = &expire;
  830. }
  831. if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
  832. timed_out = 1;
  833. }
  834. return count;
  835. }
  836. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  837. sizeof(struct pollfd))
  838. static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  839. struct timespec64 *end_time)
  840. {
  841. struct poll_wqueues table;
  842. int err = -EFAULT, fdcount, len;
  843. /* Allocate small arguments on the stack to save memory and be
  844. faster - use long to make sure the buffer is aligned properly
  845. on 64 bit archs to avoid unaligned access */
  846. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  847. struct poll_list *const head = (struct poll_list *)stack_pps;
  848. struct poll_list *walk = head;
  849. unsigned long todo = nfds;
  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. todo -= walk->len;
  862. if (!todo)
  863. break;
  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. 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_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