select.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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_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_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 rts64;
  251. struct timespec rts;
  252. struct timeval rtv;
  253. if (!p)
  254. return ret;
  255. if (current->personality & STICKY_TIMEOUTS)
  256. goto sticky;
  257. /* No update for zero timeout */
  258. if (!end_time->tv_sec && !end_time->tv_nsec)
  259. return ret;
  260. ktime_get_ts64(&rts64);
  261. rts64 = timespec64_sub(*end_time, rts64);
  262. if (rts64.tv_sec < 0)
  263. rts64.tv_sec = rts64.tv_nsec = 0;
  264. rts = timespec64_to_timespec(rts64);
  265. if (timeval) {
  266. if (sizeof(rtv) > sizeof(rtv.tv_sec) + sizeof(rtv.tv_usec))
  267. memset(&rtv, 0, sizeof(rtv));
  268. rtv.tv_sec = rts64.tv_sec;
  269. rtv.tv_usec = rts64.tv_nsec / NSEC_PER_USEC;
  270. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  271. return ret;
  272. } else if (!copy_to_user(p, &rts, sizeof(rts)))
  273. return ret;
  274. /*
  275. * If an application puts its timeval in read-only memory, we
  276. * don't want the Linux-specific update to the timeval to
  277. * cause a fault after the select has completed
  278. * successfully. However, because we're not updating the
  279. * timeval, we can't restart the system call.
  280. */
  281. sticky:
  282. if (ret == -ERESTARTNOHAND)
  283. ret = -EINTR;
  284. return ret;
  285. }
  286. /*
  287. * Scalable version of the fd_set.
  288. */
  289. typedef struct {
  290. unsigned long *in, *out, *ex;
  291. unsigned long *res_in, *res_out, *res_ex;
  292. } fd_set_bits;
  293. /*
  294. * How many longwords for "nr" bits?
  295. */
  296. #define FDS_BITPERLONG (8*sizeof(long))
  297. #define FDS_LONGS(nr) (((nr)+FDS_BITPERLONG-1)/FDS_BITPERLONG)
  298. #define FDS_BYTES(nr) (FDS_LONGS(nr)*sizeof(long))
  299. /*
  300. * We do a VERIFY_WRITE here even though we are only reading this time:
  301. * we'll write to it eventually..
  302. *
  303. * Use "unsigned long" accesses to let user-mode fd_set's be long-aligned.
  304. */
  305. static inline
  306. int get_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
  307. {
  308. nr = FDS_BYTES(nr);
  309. if (ufdset)
  310. return copy_from_user(fdset, ufdset, nr) ? -EFAULT : 0;
  311. memset(fdset, 0, nr);
  312. return 0;
  313. }
  314. static inline unsigned long __must_check
  315. set_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
  316. {
  317. if (ufdset)
  318. return __copy_to_user(ufdset, fdset, FDS_BYTES(nr));
  319. return 0;
  320. }
  321. static inline
  322. void zero_fd_set(unsigned long nr, unsigned long *fdset)
  323. {
  324. memset(fdset, 0, FDS_BYTES(nr));
  325. }
  326. #define FDS_IN(fds, n) (fds->in + n)
  327. #define FDS_OUT(fds, n) (fds->out + n)
  328. #define FDS_EX(fds, n) (fds->ex + n)
  329. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  330. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  331. {
  332. unsigned long *open_fds;
  333. unsigned long set;
  334. int max;
  335. struct fdtable *fdt;
  336. /* handle last in-complete long-word first */
  337. set = ~(~0UL << (n & (BITS_PER_LONG-1)));
  338. n /= BITS_PER_LONG;
  339. fdt = files_fdtable(current->files);
  340. open_fds = fdt->open_fds + n;
  341. max = 0;
  342. if (set) {
  343. set &= BITS(fds, n);
  344. if (set) {
  345. if (!(set & ~*open_fds))
  346. goto get_max;
  347. return -EBADF;
  348. }
  349. }
  350. while (n) {
  351. open_fds--;
  352. n--;
  353. set = BITS(fds, n);
  354. if (!set)
  355. continue;
  356. if (set & ~*open_fds)
  357. return -EBADF;
  358. if (max)
  359. continue;
  360. get_max:
  361. do {
  362. max++;
  363. set >>= 1;
  364. } while (set);
  365. max += n * BITS_PER_LONG;
  366. }
  367. return max;
  368. }
  369. #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
  370. #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
  371. #define POLLEX_SET (POLLPRI)
  372. static inline void wait_key_set(poll_table *wait, unsigned long in,
  373. unsigned long out, unsigned long bit,
  374. unsigned int ll_flag)
  375. {
  376. wait->_key = POLLEX_SET | ll_flag;
  377. if (in & bit)
  378. wait->_key |= POLLIN_SET;
  379. if (out & bit)
  380. wait->_key |= POLLOUT_SET;
  381. }
  382. static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
  383. {
  384. ktime_t expire, *to = NULL;
  385. struct poll_wqueues table;
  386. poll_table *wait;
  387. int retval, i, timed_out = 0;
  388. u64 slack = 0;
  389. unsigned int busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  390. unsigned long busy_start = 0;
  391. rcu_read_lock();
  392. retval = max_select_fd(n, fds);
  393. rcu_read_unlock();
  394. if (retval < 0)
  395. return retval;
  396. n = retval;
  397. poll_initwait(&table);
  398. wait = &table.pt;
  399. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  400. wait->_qproc = NULL;
  401. timed_out = 1;
  402. }
  403. if (end_time && !timed_out)
  404. slack = select_estimate_accuracy(end_time);
  405. retval = 0;
  406. for (;;) {
  407. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  408. bool can_busy_loop = false;
  409. inp = fds->in; outp = fds->out; exp = fds->ex;
  410. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  411. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  412. unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  413. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  414. in = *inp++; out = *outp++; ex = *exp++;
  415. all_bits = in | out | ex;
  416. if (all_bits == 0) {
  417. i += BITS_PER_LONG;
  418. continue;
  419. }
  420. for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
  421. struct fd f;
  422. if (i >= n)
  423. break;
  424. if (!(bit & all_bits))
  425. continue;
  426. f = fdget(i);
  427. if (f.file) {
  428. const struct file_operations *f_op;
  429. f_op = f.file->f_op;
  430. mask = DEFAULT_POLLMASK;
  431. if (f_op->poll) {
  432. wait_key_set(wait, in, out,
  433. bit, busy_flag);
  434. mask = (*f_op->poll)(f.file, wait);
  435. }
  436. fdput(f);
  437. if ((mask & POLLIN_SET) && (in & bit)) {
  438. res_in |= bit;
  439. retval++;
  440. wait->_qproc = NULL;
  441. }
  442. if ((mask & POLLOUT_SET) && (out & bit)) {
  443. res_out |= bit;
  444. retval++;
  445. wait->_qproc = NULL;
  446. }
  447. if ((mask & POLLEX_SET) && (ex & bit)) {
  448. res_ex |= bit;
  449. retval++;
  450. wait->_qproc = NULL;
  451. }
  452. /* got something, stop busy polling */
  453. if (retval) {
  454. can_busy_loop = false;
  455. busy_flag = 0;
  456. /*
  457. * only remember a returned
  458. * POLL_BUSY_LOOP if we asked for it
  459. */
  460. } else if (busy_flag & mask)
  461. can_busy_loop = true;
  462. }
  463. }
  464. if (res_in)
  465. *rinp = res_in;
  466. if (res_out)
  467. *routp = res_out;
  468. if (res_ex)
  469. *rexp = res_ex;
  470. cond_resched();
  471. }
  472. wait->_qproc = NULL;
  473. if (retval || timed_out || signal_pending(current))
  474. break;
  475. if (table.error) {
  476. retval = table.error;
  477. break;
  478. }
  479. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  480. if (can_busy_loop && !need_resched()) {
  481. if (!busy_start) {
  482. busy_start = busy_loop_current_time();
  483. continue;
  484. }
  485. if (!busy_loop_timeout(busy_start))
  486. continue;
  487. }
  488. busy_flag = 0;
  489. /*
  490. * If this is the first loop and we have a timeout
  491. * given, then we convert to ktime_t and set the to
  492. * pointer to the expiry value.
  493. */
  494. if (end_time && !to) {
  495. expire = timespec64_to_ktime(*end_time);
  496. to = &expire;
  497. }
  498. if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
  499. to, slack))
  500. timed_out = 1;
  501. }
  502. poll_freewait(&table);
  503. return retval;
  504. }
  505. /*
  506. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  507. * like to be certain this leads to no problems. So I return
  508. * EINTR just for safety.
  509. *
  510. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  511. * I'm trying ERESTARTNOHAND which restart only when you want to.
  512. */
  513. int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  514. fd_set __user *exp, struct timespec64 *end_time)
  515. {
  516. fd_set_bits fds;
  517. void *bits;
  518. int ret, max_fds;
  519. size_t size, alloc_size;
  520. struct fdtable *fdt;
  521. /* Allocate small arguments on the stack to save memory and be faster */
  522. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  523. ret = -EINVAL;
  524. if (n < 0)
  525. goto out_nofds;
  526. /* max_fds can increase, so grab it once to avoid race */
  527. rcu_read_lock();
  528. fdt = files_fdtable(current->files);
  529. max_fds = fdt->max_fds;
  530. rcu_read_unlock();
  531. if (n > max_fds)
  532. n = max_fds;
  533. /*
  534. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  535. * since we used fdset we need to allocate memory in units of
  536. * long-words.
  537. */
  538. size = FDS_BYTES(n);
  539. bits = stack_fds;
  540. if (size > sizeof(stack_fds) / 6) {
  541. /* Not enough space in on-stack array; must use kmalloc */
  542. ret = -ENOMEM;
  543. if (size > (SIZE_MAX / 6))
  544. goto out_nofds;
  545. alloc_size = 6 * size;
  546. bits = kvmalloc(alloc_size, GFP_KERNEL);
  547. if (!bits)
  548. goto out_nofds;
  549. }
  550. fds.in = bits;
  551. fds.out = bits + size;
  552. fds.ex = bits + 2*size;
  553. fds.res_in = bits + 3*size;
  554. fds.res_out = bits + 4*size;
  555. fds.res_ex = bits + 5*size;
  556. if ((ret = get_fd_set(n, inp, fds.in)) ||
  557. (ret = get_fd_set(n, outp, fds.out)) ||
  558. (ret = get_fd_set(n, exp, fds.ex)))
  559. goto out;
  560. zero_fd_set(n, fds.res_in);
  561. zero_fd_set(n, fds.res_out);
  562. zero_fd_set(n, fds.res_ex);
  563. ret = do_select(n, &fds, end_time);
  564. if (ret < 0)
  565. goto out;
  566. if (!ret) {
  567. ret = -ERESTARTNOHAND;
  568. if (signal_pending(current))
  569. goto out;
  570. ret = 0;
  571. }
  572. if (set_fd_set(n, inp, fds.res_in) ||
  573. set_fd_set(n, outp, fds.res_out) ||
  574. set_fd_set(n, exp, fds.res_ex))
  575. ret = -EFAULT;
  576. out:
  577. if (bits != stack_fds)
  578. kvfree(bits);
  579. out_nofds:
  580. return ret;
  581. }
  582. SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
  583. fd_set __user *, exp, struct timeval __user *, tvp)
  584. {
  585. struct timespec64 end_time, *to = NULL;
  586. struct timeval tv;
  587. int ret;
  588. if (tvp) {
  589. if (copy_from_user(&tv, tvp, sizeof(tv)))
  590. return -EFAULT;
  591. to = &end_time;
  592. if (poll_select_set_timeout(to,
  593. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  594. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  595. return -EINVAL;
  596. }
  597. ret = core_sys_select(n, inp, outp, exp, to);
  598. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  599. return ret;
  600. }
  601. static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
  602. fd_set __user *exp, struct timespec __user *tsp,
  603. const sigset_t __user *sigmask, size_t sigsetsize)
  604. {
  605. sigset_t ksigmask, sigsaved;
  606. struct timespec ts;
  607. struct timespec64 ts64, end_time, *to = NULL;
  608. int ret;
  609. if (tsp) {
  610. if (copy_from_user(&ts, tsp, sizeof(ts)))
  611. return -EFAULT;
  612. ts64 = timespec_to_timespec64(ts);
  613. to = &end_time;
  614. if (poll_select_set_timeout(to, ts64.tv_sec, ts64.tv_nsec))
  615. return -EINVAL;
  616. }
  617. if (sigmask) {
  618. /* XXX: Don't preclude handling different sized sigset_t's. */
  619. if (sigsetsize != sizeof(sigset_t))
  620. return -EINVAL;
  621. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  622. return -EFAULT;
  623. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  624. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  625. }
  626. ret = core_sys_select(n, inp, outp, exp, to);
  627. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  628. if (ret == -ERESTARTNOHAND) {
  629. /*
  630. * Don't restore the signal mask yet. Let do_signal() deliver
  631. * the signal on the way back to userspace, before the signal
  632. * mask is restored.
  633. */
  634. if (sigmask) {
  635. memcpy(&current->saved_sigmask, &sigsaved,
  636. sizeof(sigsaved));
  637. set_restore_sigmask();
  638. }
  639. } else if (sigmask)
  640. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  641. return ret;
  642. }
  643. /*
  644. * Most architectures can't handle 7-argument syscalls. So we provide a
  645. * 6-argument version where the sixth argument is a pointer to a structure
  646. * which has a pointer to the sigset_t itself followed by a size_t containing
  647. * the sigset size.
  648. */
  649. SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
  650. fd_set __user *, exp, struct timespec __user *, tsp,
  651. void __user *, sig)
  652. {
  653. size_t sigsetsize = 0;
  654. sigset_t __user *up = NULL;
  655. if (sig) {
  656. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  657. || __get_user(up, (sigset_t __user * __user *)sig)
  658. || __get_user(sigsetsize,
  659. (size_t __user *)(sig+sizeof(void *))))
  660. return -EFAULT;
  661. }
  662. return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
  663. }
  664. #ifdef __ARCH_WANT_SYS_OLD_SELECT
  665. struct sel_arg_struct {
  666. unsigned long n;
  667. fd_set __user *inp, *outp, *exp;
  668. struct timeval __user *tvp;
  669. };
  670. SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
  671. {
  672. struct sel_arg_struct a;
  673. if (copy_from_user(&a, arg, sizeof(a)))
  674. return -EFAULT;
  675. return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  676. }
  677. #endif
  678. struct poll_list {
  679. struct poll_list *next;
  680. int len;
  681. struct pollfd entries[0];
  682. };
  683. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  684. /*
  685. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  686. * interested in events matching the pollfd->events mask, and the result
  687. * matching that mask is both recorded in pollfd->revents and returned. The
  688. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  689. * if pwait->_qproc is non-NULL.
  690. */
  691. static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait,
  692. bool *can_busy_poll,
  693. unsigned int busy_flag)
  694. {
  695. unsigned int mask;
  696. int fd;
  697. mask = 0;
  698. fd = pollfd->fd;
  699. if (fd >= 0) {
  700. struct fd f = fdget(fd);
  701. mask = POLLNVAL;
  702. if (f.file) {
  703. mask = DEFAULT_POLLMASK;
  704. if (f.file->f_op->poll) {
  705. pwait->_key = pollfd->events|POLLERR|POLLHUP;
  706. pwait->_key |= busy_flag;
  707. mask = f.file->f_op->poll(f.file, pwait);
  708. if (mask & busy_flag)
  709. *can_busy_poll = true;
  710. }
  711. /* Mask out unneeded events. */
  712. mask &= pollfd->events | POLLERR | POLLHUP;
  713. fdput(f);
  714. }
  715. }
  716. pollfd->revents = mask;
  717. return mask;
  718. }
  719. static int do_poll(struct poll_list *list, struct poll_wqueues *wait,
  720. struct timespec64 *end_time)
  721. {
  722. poll_table* pt = &wait->pt;
  723. ktime_t expire, *to = NULL;
  724. int timed_out = 0, count = 0;
  725. u64 slack = 0;
  726. unsigned int busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
  727. unsigned long busy_start = 0;
  728. /* Optimise the no-wait case */
  729. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  730. pt->_qproc = NULL;
  731. timed_out = 1;
  732. }
  733. if (end_time && !timed_out)
  734. slack = select_estimate_accuracy(end_time);
  735. for (;;) {
  736. struct poll_list *walk;
  737. bool can_busy_loop = false;
  738. for (walk = list; walk != NULL; walk = walk->next) {
  739. struct pollfd * pfd, * pfd_end;
  740. pfd = walk->entries;
  741. pfd_end = pfd + walk->len;
  742. for (; pfd != pfd_end; pfd++) {
  743. /*
  744. * Fish for events. If we found one, record it
  745. * and kill poll_table->_qproc, so we don't
  746. * needlessly register any other waiters after
  747. * this. They'll get immediately deregistered
  748. * when we break out and return.
  749. */
  750. if (do_pollfd(pfd, pt, &can_busy_loop,
  751. busy_flag)) {
  752. count++;
  753. pt->_qproc = NULL;
  754. /* found something, stop busy polling */
  755. busy_flag = 0;
  756. can_busy_loop = false;
  757. }
  758. }
  759. }
  760. /*
  761. * All waiters have already been registered, so don't provide
  762. * a poll_table->_qproc to them on the next loop iteration.
  763. */
  764. pt->_qproc = NULL;
  765. if (!count) {
  766. count = wait->error;
  767. if (signal_pending(current))
  768. count = -EINTR;
  769. }
  770. if (count || timed_out)
  771. break;
  772. /* only if found POLL_BUSY_LOOP sockets && not out of time */
  773. if (can_busy_loop && !need_resched()) {
  774. if (!busy_start) {
  775. busy_start = busy_loop_current_time();
  776. continue;
  777. }
  778. if (!busy_loop_timeout(busy_start))
  779. continue;
  780. }
  781. busy_flag = 0;
  782. /*
  783. * If this is the first loop and we have a timeout
  784. * given, then we convert to ktime_t and set the to
  785. * pointer to the expiry value.
  786. */
  787. if (end_time && !to) {
  788. expire = timespec64_to_ktime(*end_time);
  789. to = &expire;
  790. }
  791. if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
  792. timed_out = 1;
  793. }
  794. return count;
  795. }
  796. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  797. sizeof(struct pollfd))
  798. static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  799. struct timespec64 *end_time)
  800. {
  801. struct poll_wqueues table;
  802. int err = -EFAULT, fdcount, len, size;
  803. /* Allocate small arguments on the stack to save memory and be
  804. faster - use long to make sure the buffer is aligned properly
  805. on 64 bit archs to avoid unaligned access */
  806. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  807. struct poll_list *const head = (struct poll_list *)stack_pps;
  808. struct poll_list *walk = head;
  809. unsigned long todo = nfds;
  810. if (nfds > rlimit(RLIMIT_NOFILE))
  811. return -EINVAL;
  812. len = min_t(unsigned int, nfds, N_STACK_PPS);
  813. for (;;) {
  814. walk->next = NULL;
  815. walk->len = len;
  816. if (!len)
  817. break;
  818. if (copy_from_user(walk->entries, ufds + nfds-todo,
  819. sizeof(struct pollfd) * walk->len))
  820. goto out_fds;
  821. todo -= walk->len;
  822. if (!todo)
  823. break;
  824. len = min(todo, POLLFD_PER_PAGE);
  825. size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
  826. walk = walk->next = kmalloc(size, GFP_KERNEL);
  827. if (!walk) {
  828. err = -ENOMEM;
  829. goto out_fds;
  830. }
  831. }
  832. poll_initwait(&table);
  833. fdcount = do_poll(head, &table, end_time);
  834. poll_freewait(&table);
  835. for (walk = head; walk; walk = walk->next) {
  836. struct pollfd *fds = walk->entries;
  837. int j;
  838. for (j = 0; j < walk->len; j++, ufds++)
  839. if (__put_user(fds[j].revents, &ufds->revents))
  840. goto out_fds;
  841. }
  842. err = fdcount;
  843. out_fds:
  844. walk = head->next;
  845. while (walk) {
  846. struct poll_list *pos = walk;
  847. walk = walk->next;
  848. kfree(pos);
  849. }
  850. return err;
  851. }
  852. static long do_restart_poll(struct restart_block *restart_block)
  853. {
  854. struct pollfd __user *ufds = restart_block->poll.ufds;
  855. int nfds = restart_block->poll.nfds;
  856. struct timespec64 *to = NULL, end_time;
  857. int ret;
  858. if (restart_block->poll.has_timeout) {
  859. end_time.tv_sec = restart_block->poll.tv_sec;
  860. end_time.tv_nsec = restart_block->poll.tv_nsec;
  861. to = &end_time;
  862. }
  863. ret = do_sys_poll(ufds, nfds, to);
  864. if (ret == -EINTR) {
  865. restart_block->fn = do_restart_poll;
  866. ret = -ERESTART_RESTARTBLOCK;
  867. }
  868. return ret;
  869. }
  870. SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
  871. int, timeout_msecs)
  872. {
  873. struct timespec64 end_time, *to = NULL;
  874. int ret;
  875. if (timeout_msecs >= 0) {
  876. to = &end_time;
  877. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  878. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  879. }
  880. ret = do_sys_poll(ufds, nfds, to);
  881. if (ret == -EINTR) {
  882. struct restart_block *restart_block;
  883. restart_block = &current->restart_block;
  884. restart_block->fn = do_restart_poll;
  885. restart_block->poll.ufds = ufds;
  886. restart_block->poll.nfds = nfds;
  887. if (timeout_msecs >= 0) {
  888. restart_block->poll.tv_sec = end_time.tv_sec;
  889. restart_block->poll.tv_nsec = end_time.tv_nsec;
  890. restart_block->poll.has_timeout = 1;
  891. } else
  892. restart_block->poll.has_timeout = 0;
  893. ret = -ERESTART_RESTARTBLOCK;
  894. }
  895. return ret;
  896. }
  897. SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
  898. struct timespec __user *, tsp, const sigset_t __user *, sigmask,
  899. size_t, sigsetsize)
  900. {
  901. sigset_t ksigmask, sigsaved;
  902. struct timespec ts;
  903. struct timespec64 end_time, *to = NULL;
  904. int ret;
  905. if (tsp) {
  906. if (copy_from_user(&ts, tsp, sizeof(ts)))
  907. return -EFAULT;
  908. to = &end_time;
  909. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  910. return -EINVAL;
  911. }
  912. if (sigmask) {
  913. /* XXX: Don't preclude handling different sized sigset_t's. */
  914. if (sigsetsize != sizeof(sigset_t))
  915. return -EINVAL;
  916. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  917. return -EFAULT;
  918. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  919. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  920. }
  921. ret = do_sys_poll(ufds, nfds, to);
  922. /* We can restart this syscall, usually */
  923. if (ret == -EINTR) {
  924. /*
  925. * Don't restore the signal mask yet. Let do_signal() deliver
  926. * the signal on the way back to userspace, before the signal
  927. * mask is restored.
  928. */
  929. if (sigmask) {
  930. memcpy(&current->saved_sigmask, &sigsaved,
  931. sizeof(sigsaved));
  932. set_restore_sigmask();
  933. }
  934. ret = -ERESTARTNOHAND;
  935. } else if (sigmask)
  936. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  937. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  938. return ret;
  939. }
  940. #ifdef CONFIG_COMPAT
  941. #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
  942. static
  943. int compat_poll_select_copy_remaining(struct timespec *end_time, void __user *p,
  944. int timeval, int ret)
  945. {
  946. struct timespec ts;
  947. if (!p)
  948. return ret;
  949. if (current->personality & STICKY_TIMEOUTS)
  950. goto sticky;
  951. /* No update for zero timeout */
  952. if (!end_time->tv_sec && !end_time->tv_nsec)
  953. return ret;
  954. ktime_get_ts(&ts);
  955. ts = timespec_sub(*end_time, ts);
  956. if (ts.tv_sec < 0)
  957. ts.tv_sec = ts.tv_nsec = 0;
  958. if (timeval) {
  959. struct compat_timeval rtv;
  960. rtv.tv_sec = ts.tv_sec;
  961. rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  962. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  963. return ret;
  964. } else {
  965. struct compat_timespec rts;
  966. rts.tv_sec = ts.tv_sec;
  967. rts.tv_nsec = ts.tv_nsec;
  968. if (!copy_to_user(p, &rts, sizeof(rts)))
  969. return ret;
  970. }
  971. /*
  972. * If an application puts its timeval in read-only memory, we
  973. * don't want the Linux-specific update to the timeval to
  974. * cause a fault after the select has completed
  975. * successfully. However, because we're not updating the
  976. * timeval, we can't restart the system call.
  977. */
  978. sticky:
  979. if (ret == -ERESTARTNOHAND)
  980. ret = -EINTR;
  981. return ret;
  982. }
  983. /*
  984. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  985. * 64-bit unsigned longs.
  986. */
  987. static
  988. int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  989. unsigned long *fdset)
  990. {
  991. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  992. if (ufdset) {
  993. unsigned long odd;
  994. if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
  995. return -EFAULT;
  996. odd = nr & 1UL;
  997. nr &= ~1UL;
  998. while (nr) {
  999. unsigned long h, l;
  1000. if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
  1001. return -EFAULT;
  1002. ufdset += 2;
  1003. *fdset++ = h << 32 | l;
  1004. nr -= 2;
  1005. }
  1006. if (odd && __get_user(*fdset, ufdset))
  1007. return -EFAULT;
  1008. } else {
  1009. /* Tricky, must clear full unsigned long in the
  1010. * kernel fdset at the end, this makes sure that
  1011. * actually happens.
  1012. */
  1013. memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
  1014. }
  1015. return 0;
  1016. }
  1017. static
  1018. int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1019. unsigned long *fdset)
  1020. {
  1021. unsigned long odd;
  1022. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  1023. if (!ufdset)
  1024. return 0;
  1025. odd = nr & 1UL;
  1026. nr &= ~1UL;
  1027. while (nr) {
  1028. unsigned long h, l;
  1029. l = *fdset++;
  1030. h = l >> 32;
  1031. if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
  1032. return -EFAULT;
  1033. ufdset += 2;
  1034. nr -= 2;
  1035. }
  1036. if (odd && __put_user(*fdset, ufdset))
  1037. return -EFAULT;
  1038. return 0;
  1039. }
  1040. /*
  1041. * This is a virtual copy of sys_select from fs/select.c and probably
  1042. * should be compared to it from time to time
  1043. */
  1044. /*
  1045. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  1046. * like to be certain this leads to no problems. So I return
  1047. * EINTR just for safety.
  1048. *
  1049. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  1050. * I'm trying ERESTARTNOHAND which restart only when you want to.
  1051. */
  1052. static int compat_core_sys_select(int n, compat_ulong_t __user *inp,
  1053. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1054. struct timespec *end_time)
  1055. {
  1056. fd_set_bits fds;
  1057. void *bits;
  1058. int size, max_fds, ret = -EINVAL;
  1059. struct fdtable *fdt;
  1060. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  1061. if (n < 0)
  1062. goto out_nofds;
  1063. /* max_fds can increase, so grab it once to avoid race */
  1064. rcu_read_lock();
  1065. fdt = files_fdtable(current->files);
  1066. max_fds = fdt->max_fds;
  1067. rcu_read_unlock();
  1068. if (n > max_fds)
  1069. n = max_fds;
  1070. /*
  1071. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  1072. * since we used fdset we need to allocate memory in units of
  1073. * long-words.
  1074. */
  1075. size = FDS_BYTES(n);
  1076. bits = stack_fds;
  1077. if (size > sizeof(stack_fds) / 6) {
  1078. bits = kmalloc(6 * size, GFP_KERNEL);
  1079. ret = -ENOMEM;
  1080. if (!bits)
  1081. goto out_nofds;
  1082. }
  1083. fds.in = (unsigned long *) bits;
  1084. fds.out = (unsigned long *) (bits + size);
  1085. fds.ex = (unsigned long *) (bits + 2*size);
  1086. fds.res_in = (unsigned long *) (bits + 3*size);
  1087. fds.res_out = (unsigned long *) (bits + 4*size);
  1088. fds.res_ex = (unsigned long *) (bits + 5*size);
  1089. if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
  1090. (ret = compat_get_fd_set(n, outp, fds.out)) ||
  1091. (ret = compat_get_fd_set(n, exp, fds.ex)))
  1092. goto out;
  1093. zero_fd_set(n, fds.res_in);
  1094. zero_fd_set(n, fds.res_out);
  1095. zero_fd_set(n, fds.res_ex);
  1096. ret = do_select(n, &fds, end_time);
  1097. if (ret < 0)
  1098. goto out;
  1099. if (!ret) {
  1100. ret = -ERESTARTNOHAND;
  1101. if (signal_pending(current))
  1102. goto out;
  1103. ret = 0;
  1104. }
  1105. if (compat_set_fd_set(n, inp, fds.res_in) ||
  1106. compat_set_fd_set(n, outp, fds.res_out) ||
  1107. compat_set_fd_set(n, exp, fds.res_ex))
  1108. ret = -EFAULT;
  1109. out:
  1110. if (bits != stack_fds)
  1111. kfree(bits);
  1112. out_nofds:
  1113. return ret;
  1114. }
  1115. COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp,
  1116. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1117. struct compat_timeval __user *, tvp)
  1118. {
  1119. struct timespec end_time, *to = NULL;
  1120. struct compat_timeval tv;
  1121. int ret;
  1122. if (tvp) {
  1123. if (copy_from_user(&tv, tvp, sizeof(tv)))
  1124. return -EFAULT;
  1125. to = &end_time;
  1126. if (poll_select_set_timeout(to,
  1127. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  1128. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  1129. return -EINVAL;
  1130. }
  1131. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1132. ret = compat_poll_select_copy_remaining(&end_time, tvp, 1, ret);
  1133. return ret;
  1134. }
  1135. struct compat_sel_arg_struct {
  1136. compat_ulong_t n;
  1137. compat_uptr_t inp;
  1138. compat_uptr_t outp;
  1139. compat_uptr_t exp;
  1140. compat_uptr_t tvp;
  1141. };
  1142. COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg)
  1143. {
  1144. struct compat_sel_arg_struct a;
  1145. if (copy_from_user(&a, arg, sizeof(a)))
  1146. return -EFAULT;
  1147. return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
  1148. compat_ptr(a.exp), compat_ptr(a.tvp));
  1149. }
  1150. static long do_compat_pselect(int n, compat_ulong_t __user *inp,
  1151. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1152. struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
  1153. compat_size_t sigsetsize)
  1154. {
  1155. compat_sigset_t ss32;
  1156. sigset_t ksigmask, sigsaved;
  1157. struct compat_timespec ts;
  1158. struct timespec end_time, *to = NULL;
  1159. int ret;
  1160. if (tsp) {
  1161. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1162. return -EFAULT;
  1163. to = &end_time;
  1164. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1165. return -EINVAL;
  1166. }
  1167. if (sigmask) {
  1168. if (sigsetsize != sizeof(compat_sigset_t))
  1169. return -EINVAL;
  1170. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1171. return -EFAULT;
  1172. sigset_from_compat(&ksigmask, &ss32);
  1173. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1174. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1175. }
  1176. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1177. ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1178. if (ret == -ERESTARTNOHAND) {
  1179. /*
  1180. * Don't restore the signal mask yet. Let do_signal() deliver
  1181. * the signal on the way back to userspace, before the signal
  1182. * mask is restored.
  1183. */
  1184. if (sigmask) {
  1185. memcpy(&current->saved_sigmask, &sigsaved,
  1186. sizeof(sigsaved));
  1187. set_restore_sigmask();
  1188. }
  1189. } else if (sigmask)
  1190. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1191. return ret;
  1192. }
  1193. COMPAT_SYSCALL_DEFINE6(pselect6, int, n, compat_ulong_t __user *, inp,
  1194. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1195. struct compat_timespec __user *, tsp, void __user *, sig)
  1196. {
  1197. compat_size_t sigsetsize = 0;
  1198. compat_uptr_t up = 0;
  1199. if (sig) {
  1200. if (!access_ok(VERIFY_READ, sig,
  1201. sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
  1202. __get_user(up, (compat_uptr_t __user *)sig) ||
  1203. __get_user(sigsetsize,
  1204. (compat_size_t __user *)(sig+sizeof(up))))
  1205. return -EFAULT;
  1206. }
  1207. return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
  1208. sigsetsize);
  1209. }
  1210. COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds,
  1211. unsigned int, nfds, struct compat_timespec __user *, tsp,
  1212. const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
  1213. {
  1214. compat_sigset_t ss32;
  1215. sigset_t ksigmask, sigsaved;
  1216. struct compat_timespec ts;
  1217. struct timespec end_time, *to = NULL;
  1218. int ret;
  1219. if (tsp) {
  1220. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1221. return -EFAULT;
  1222. to = &end_time;
  1223. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1224. return -EINVAL;
  1225. }
  1226. if (sigmask) {
  1227. if (sigsetsize != sizeof(compat_sigset_t))
  1228. return -EINVAL;
  1229. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1230. return -EFAULT;
  1231. sigset_from_compat(&ksigmask, &ss32);
  1232. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1233. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1234. }
  1235. ret = do_sys_poll(ufds, nfds, to);
  1236. /* We can restart this syscall, usually */
  1237. if (ret == -EINTR) {
  1238. /*
  1239. * Don't restore the signal mask yet. Let do_signal() deliver
  1240. * the signal on the way back to userspace, before the signal
  1241. * mask is restored.
  1242. */
  1243. if (sigmask) {
  1244. memcpy(&current->saved_sigmask, &sigsaved,
  1245. sizeof(sigsaved));
  1246. set_restore_sigmask();
  1247. }
  1248. ret = -ERESTARTNOHAND;
  1249. } else if (sigmask)
  1250. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1251. ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1252. return ret;
  1253. }
  1254. #endif