select.c 35 KB

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