select.c 34 KB

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