1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/kernel.h>
19#include <linux/sched/signal.h>
20#include <linux/sched/rt.h>
21#include <linux/syscalls.h>
22#include <linux/export.h>
23#include <linux/slab.h>
24#include <linux/poll.h>
25#include <linux/personality.h>
26#include <linux/file.h>
27#include <linux/fdtable.h>
28#include <linux/fs.h>
29#include <linux/rcupdate.h>
30#include <linux/hrtimer.h>
31#include <linux/freezer.h>
32#include <net/busy_poll.h>
33#include <linux/vmalloc.h>
34
35#include <linux/uaccess.h>
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#define MAX_SLACK (100 * NSEC_PER_MSEC)
51
52static long __estimate_accuracy(struct timespec64 *tv)
53{
54 long slack;
55 int divfactor = 1000;
56
57 if (tv->tv_sec < 0)
58 return 0;
59
60 if (task_nice(current) > 0)
61 divfactor = divfactor / 5;
62
63 if (tv->tv_sec > MAX_SLACK / (NSEC_PER_SEC/divfactor))
64 return MAX_SLACK;
65
66 slack = tv->tv_nsec / divfactor;
67 slack += tv->tv_sec * (NSEC_PER_SEC/divfactor);
68
69 if (slack > MAX_SLACK)
70 return MAX_SLACK;
71
72 return slack;
73}
74
75u64 select_estimate_accuracy(struct timespec64 *tv)
76{
77 u64 ret;
78 struct timespec64 now;
79
80
81
82
83
84 if (rt_task(current))
85 return 0;
86
87 ktime_get_ts64(&now);
88 now = timespec64_sub(*tv, now);
89 ret = __estimate_accuracy(&now);
90 if (ret < current->timer_slack_ns)
91 return current->timer_slack_ns;
92 return ret;
93}
94
95
96
97struct poll_table_page {
98 struct poll_table_page * next;
99 struct poll_table_entry * entry;
100 struct poll_table_entry entries[0];
101};
102
103#define POLL_TABLE_FULL(table) \
104 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
105
106
107
108
109
110
111
112
113
114
115
116
117
118static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
119 poll_table *p);
120
121void poll_initwait(struct poll_wqueues *pwq)
122{
123 init_poll_funcptr(&pwq->pt, __pollwait);
124 pwq->polling_task = current;
125 pwq->triggered = 0;
126 pwq->error = 0;
127 pwq->table = NULL;
128 pwq->inline_index = 0;
129}
130EXPORT_SYMBOL(poll_initwait);
131
132static void free_poll_entry(struct poll_table_entry *entry)
133{
134 remove_wait_queue(entry->wait_address, &entry->wait);
135 fput(entry->filp);
136}
137
138void poll_freewait(struct poll_wqueues *pwq)
139{
140 struct poll_table_page * p = pwq->table;
141 int i;
142 for (i = 0; i < pwq->inline_index; i++)
143 free_poll_entry(pwq->inline_entries + i);
144 while (p) {
145 struct poll_table_entry * entry;
146 struct poll_table_page *old;
147
148 entry = p->entry;
149 do {
150 entry--;
151 free_poll_entry(entry);
152 } while (entry > p->entries);
153 old = p;
154 p = p->next;
155 free_page((unsigned long) old);
156 }
157}
158EXPORT_SYMBOL(poll_freewait);
159
160static struct poll_table_entry *poll_get_entry(struct poll_wqueues *p)
161{
162 struct poll_table_page *table = p->table;
163
164 if (p->inline_index < N_INLINE_POLL_ENTRIES)
165 return p->inline_entries + p->inline_index++;
166
167 if (!table || POLL_TABLE_FULL(table)) {
168 struct poll_table_page *new_table;
169
170 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
171 if (!new_table) {
172 p->error = -ENOMEM;
173 return NULL;
174 }
175 new_table->entry = new_table->entries;
176 new_table->next = table;
177 p->table = new_table;
178 table = new_table;
179 }
180
181 return table->entry++;
182}
183
184static int __pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
185{
186 struct poll_wqueues *pwq = wait->private;
187 DECLARE_WAITQUEUE(dummy_wait, pwq->polling_task);
188
189
190
191
192
193
194
195
196 smp_wmb();
197 pwq->triggered = 1;
198
199
200
201
202
203
204
205
206
207 return default_wake_function(&dummy_wait, mode, sync, key);
208}
209
210static int pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
211{
212 struct poll_table_entry *entry;
213
214 entry = container_of(wait, struct poll_table_entry, wait);
215 if (key && !(key_to_poll(key) & entry->key))
216 return 0;
217 return __pollwake(wait, mode, sync, key);
218}
219
220
221static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
222 poll_table *p)
223{
224 struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);
225 struct poll_table_entry *entry = poll_get_entry(pwq);
226 if (!entry)
227 return;
228 entry->filp = get_file(filp);
229 entry->wait_address = wait_address;
230 entry->key = p->_key;
231 init_waitqueue_func_entry(&entry->wait, pollwake);
232 entry->wait.private = pwq;
233 add_wait_queue(wait_address, &entry->wait);
234}
235
236static int poll_schedule_timeout(struct poll_wqueues *pwq, int state,
237 ktime_t *expires, unsigned long slack)
238{
239 int rc = -EINTR;
240
241 set_current_state(state);
242 if (!pwq->triggered)
243 rc = schedule_hrtimeout_range(expires, slack, HRTIMER_MODE_ABS);
244 __set_current_state(TASK_RUNNING);
245
246
247
248
249
250
251
252
253
254
255
256
257 smp_store_mb(pwq->triggered, 0);
258
259 return rc;
260}
261
262
263
264
265
266
267
268
269
270
271
272
273int poll_select_set_timeout(struct timespec64 *to, time64_t sec, long nsec)
274{
275 struct timespec64 ts = {.tv_sec = sec, .tv_nsec = nsec};
276
277 if (!timespec64_valid(&ts))
278 return -EINVAL;
279
280
281 if (!sec && !nsec) {
282 to->tv_sec = to->tv_nsec = 0;
283 } else {
284 ktime_get_ts64(to);
285 *to = timespec64_add_safe(*to, ts);
286 }
287 return 0;
288}
289
290static int poll_select_copy_remaining(struct timespec64 *end_time,
291 void __user *p,
292 int timeval, int ret)
293{
294 struct timespec64 rts;
295 struct timeval rtv;
296
297 if (!p)
298 return ret;
299
300 if (current->personality & STICKY_TIMEOUTS)
301 goto sticky;
302
303
304 if (!end_time->tv_sec && !end_time->tv_nsec)
305 return ret;
306
307 ktime_get_ts64(&rts);
308 rts = timespec64_sub(*end_time, rts);
309 if (rts.tv_sec < 0)
310 rts.tv_sec = rts.tv_nsec = 0;
311
312
313 if (timeval) {
314 if (sizeof(rtv) > sizeof(rtv.tv_sec) + sizeof(rtv.tv_usec))
315 memset(&rtv, 0, sizeof(rtv));
316 rtv.tv_sec = rts.tv_sec;
317 rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
318
319 if (!copy_to_user(p, &rtv, sizeof(rtv)))
320 return ret;
321
322 } else if (!put_timespec64(&rts, p))
323 return ret;
324
325
326
327
328
329
330
331
332
333sticky:
334 if (ret == -ERESTARTNOHAND)
335 ret = -EINTR;
336 return ret;
337}
338
339
340
341
342
343typedef struct {
344 unsigned long *in, *out, *ex;
345 unsigned long *res_in, *res_out, *res_ex;
346} fd_set_bits;
347
348
349
350
351#define FDS_BITPERLONG (8*sizeof(long))
352#define FDS_LONGS(nr) (((nr)+FDS_BITPERLONG-1)/FDS_BITPERLONG)
353#define FDS_BYTES(nr) (FDS_LONGS(nr)*sizeof(long))
354
355
356
357
358static inline
359int get_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
360{
361 nr = FDS_BYTES(nr);
362 if (ufdset)
363 return copy_from_user(fdset, ufdset, nr) ? -EFAULT : 0;
364
365 memset(fdset, 0, nr);
366 return 0;
367}
368
369static inline unsigned long __must_check
370set_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
371{
372 if (ufdset)
373 return __copy_to_user(ufdset, fdset, FDS_BYTES(nr));
374 return 0;
375}
376
377static inline
378void zero_fd_set(unsigned long nr, unsigned long *fdset)
379{
380 memset(fdset, 0, FDS_BYTES(nr));
381}
382
383#define FDS_IN(fds, n) (fds->in + n)
384#define FDS_OUT(fds, n) (fds->out + n)
385#define FDS_EX(fds, n) (fds->ex + n)
386
387#define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
388
389static int max_select_fd(unsigned long n, fd_set_bits *fds)
390{
391 unsigned long *open_fds;
392 unsigned long set;
393 int max;
394 struct fdtable *fdt;
395
396
397 set = ~(~0UL << (n & (BITS_PER_LONG-1)));
398 n /= BITS_PER_LONG;
399 fdt = files_fdtable(current->files);
400 open_fds = fdt->open_fds + n;
401 max = 0;
402 if (set) {
403 set &= BITS(fds, n);
404 if (set) {
405 if (!(set & ~*open_fds))
406 goto get_max;
407 return -EBADF;
408 }
409 }
410 while (n) {
411 open_fds--;
412 n--;
413 set = BITS(fds, n);
414 if (!set)
415 continue;
416 if (set & ~*open_fds)
417 return -EBADF;
418 if (max)
419 continue;
420get_max:
421 do {
422 max++;
423 set >>= 1;
424 } while (set);
425 max += n * BITS_PER_LONG;
426 }
427
428 return max;
429}
430
431#define POLLIN_SET (EPOLLRDNORM | EPOLLRDBAND | EPOLLIN | EPOLLHUP | EPOLLERR)
432#define POLLOUT_SET (EPOLLWRBAND | EPOLLWRNORM | EPOLLOUT | EPOLLERR)
433#define POLLEX_SET (EPOLLPRI)
434
435static inline void wait_key_set(poll_table *wait, unsigned long in,
436 unsigned long out, unsigned long bit,
437 __poll_t ll_flag)
438{
439 wait->_key = POLLEX_SET | ll_flag;
440 if (in & bit)
441 wait->_key |= POLLIN_SET;
442 if (out & bit)
443 wait->_key |= POLLOUT_SET;
444}
445
446static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
447{
448 ktime_t expire, *to = NULL;
449 struct poll_wqueues table;
450 poll_table *wait;
451 int retval, i, timed_out = 0;
452 u64 slack = 0;
453 __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
454 unsigned long busy_start = 0;
455
456 rcu_read_lock();
457 retval = max_select_fd(n, fds);
458 rcu_read_unlock();
459
460 if (retval < 0)
461 return retval;
462 n = retval;
463
464 poll_initwait(&table);
465 wait = &table.pt;
466 if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
467 wait->_qproc = NULL;
468 timed_out = 1;
469 }
470
471 if (end_time && !timed_out)
472 slack = select_estimate_accuracy(end_time);
473
474 retval = 0;
475 for (;;) {
476 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
477 bool can_busy_loop = false;
478
479 inp = fds->in; outp = fds->out; exp = fds->ex;
480 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
481
482 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
483 unsigned long in, out, ex, all_bits, bit = 1, j;
484 unsigned long res_in = 0, res_out = 0, res_ex = 0;
485 __poll_t mask;
486
487 in = *inp++; out = *outp++; ex = *exp++;
488 all_bits = in | out | ex;
489 if (all_bits == 0) {
490 i += BITS_PER_LONG;
491 continue;
492 }
493
494 for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
495 struct fd f;
496 if (i >= n)
497 break;
498 if (!(bit & all_bits))
499 continue;
500 f = fdget(i);
501 if (f.file) {
502 wait_key_set(wait, in, out, bit,
503 busy_flag);
504 mask = vfs_poll(f.file, wait);
505
506 fdput(f);
507 if ((mask & POLLIN_SET) && (in & bit)) {
508 res_in |= bit;
509 retval++;
510 wait->_qproc = NULL;
511 }
512 if ((mask & POLLOUT_SET) && (out & bit)) {
513 res_out |= bit;
514 retval++;
515 wait->_qproc = NULL;
516 }
517 if ((mask & POLLEX_SET) && (ex & bit)) {
518 res_ex |= bit;
519 retval++;
520 wait->_qproc = NULL;
521 }
522
523 if (retval) {
524 can_busy_loop = false;
525 busy_flag = 0;
526
527
528
529
530
531 } else if (busy_flag & mask)
532 can_busy_loop = true;
533
534 }
535 }
536 if (res_in)
537 *rinp = res_in;
538 if (res_out)
539 *routp = res_out;
540 if (res_ex)
541 *rexp = res_ex;
542 cond_resched();
543 }
544 wait->_qproc = NULL;
545 if (retval || timed_out || signal_pending(current))
546 break;
547 if (table.error) {
548 retval = table.error;
549 break;
550 }
551
552
553 if (can_busy_loop && !need_resched()) {
554 if (!busy_start) {
555 busy_start = busy_loop_current_time();
556 continue;
557 }
558 if (!busy_loop_timeout(busy_start))
559 continue;
560 }
561 busy_flag = 0;
562
563
564
565
566
567
568 if (end_time && !to) {
569 expire = timespec64_to_ktime(*end_time);
570 to = &expire;
571 }
572
573 if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
574 to, slack))
575 timed_out = 1;
576 }
577
578 poll_freewait(&table);
579
580 return retval;
581}
582
583
584
585
586
587
588
589
590
591int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
592 fd_set __user *exp, struct timespec64 *end_time)
593{
594 fd_set_bits fds;
595 void *bits;
596 int ret, max_fds;
597 size_t size, alloc_size;
598 struct fdtable *fdt;
599
600 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
601
602 ret = -EINVAL;
603 if (n < 0)
604 goto out_nofds;
605
606
607 rcu_read_lock();
608 fdt = files_fdtable(current->files);
609 max_fds = fdt->max_fds;
610 rcu_read_unlock();
611 if (n > max_fds)
612 n = max_fds;
613
614
615
616
617
618
619 size = FDS_BYTES(n);
620 bits = stack_fds;
621 if (size > sizeof(stack_fds) / 6) {
622
623 ret = -ENOMEM;
624 if (size > (SIZE_MAX / 6))
625 goto out_nofds;
626
627 alloc_size = 6 * size;
628 bits = kvmalloc(alloc_size, GFP_KERNEL);
629 if (!bits)
630 goto out_nofds;
631 }
632 fds.in = bits;
633 fds.out = bits + size;
634 fds.ex = bits + 2*size;
635 fds.res_in = bits + 3*size;
636 fds.res_out = bits + 4*size;
637 fds.res_ex = bits + 5*size;
638
639 if ((ret = get_fd_set(n, inp, fds.in)) ||
640 (ret = get_fd_set(n, outp, fds.out)) ||
641 (ret = get_fd_set(n, exp, fds.ex)))
642 goto out;
643 zero_fd_set(n, fds.res_in);
644 zero_fd_set(n, fds.res_out);
645 zero_fd_set(n, fds.res_ex);
646
647 ret = do_select(n, &fds, end_time);
648
649 if (ret < 0)
650 goto out;
651 if (!ret) {
652 ret = -ERESTARTNOHAND;
653 if (signal_pending(current))
654 goto out;
655 ret = 0;
656 }
657
658 if (set_fd_set(n, inp, fds.res_in) ||
659 set_fd_set(n, outp, fds.res_out) ||
660 set_fd_set(n, exp, fds.res_ex))
661 ret = -EFAULT;
662
663out:
664 if (bits != stack_fds)
665 kvfree(bits);
666out_nofds:
667 return ret;
668}
669
670static int kern_select(int n, fd_set __user *inp, fd_set __user *outp,
671 fd_set __user *exp, struct timeval __user *tvp)
672{
673 struct timespec64 end_time, *to = NULL;
674 struct timeval tv;
675 int ret;
676
677 if (tvp) {
678 if (copy_from_user(&tv, tvp, sizeof(tv)))
679 return -EFAULT;
680
681 to = &end_time;
682 if (poll_select_set_timeout(to,
683 tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
684 (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
685 return -EINVAL;
686 }
687
688 ret = core_sys_select(n, inp, outp, exp, to);
689 ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
690
691 return ret;
692}
693
694SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
695 fd_set __user *, exp, struct timeval __user *, tvp)
696{
697 return kern_select(n, inp, outp, exp, tvp);
698}
699
700static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
701 fd_set __user *exp, struct timespec __user *tsp,
702 const sigset_t __user *sigmask, size_t sigsetsize)
703{
704 sigset_t ksigmask, sigsaved;
705 struct timespec64 ts, end_time, *to = NULL;
706 int ret;
707
708 if (tsp) {
709 if (get_timespec64(&ts, tsp))
710 return -EFAULT;
711
712 to = &end_time;
713 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
714 return -EINVAL;
715 }
716
717 ret = set_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
718 if (ret)
719 return ret;
720
721 ret = core_sys_select(n, inp, outp, exp, to);
722 restore_user_sigmask(sigmask, &sigsaved, ret == -ERESTARTNOHAND);
723 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
724
725 return ret;
726}
727
728
729
730
731
732
733
734SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
735 fd_set __user *, exp, struct timespec __user *, tsp,
736 void __user *, sig)
737{
738 size_t sigsetsize = 0;
739 sigset_t __user *up = NULL;
740
741 if (sig) {
742 if (!access_ok(sig, sizeof(void *)+sizeof(size_t))
743 || __get_user(up, (sigset_t __user * __user *)sig)
744 || __get_user(sigsetsize,
745 (size_t __user *)(sig+sizeof(void *))))
746 return -EFAULT;
747 }
748
749 return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
750}
751
752#ifdef __ARCH_WANT_SYS_OLD_SELECT
753struct sel_arg_struct {
754 unsigned long n;
755 fd_set __user *inp, *outp, *exp;
756 struct timeval __user *tvp;
757};
758
759SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
760{
761 struct sel_arg_struct a;
762
763 if (copy_from_user(&a, arg, sizeof(a)))
764 return -EFAULT;
765 return kern_select(a.n, a.inp, a.outp, a.exp, a.tvp);
766}
767#endif
768
769struct poll_list {
770 struct poll_list *next;
771 int len;
772 struct pollfd entries[0];
773};
774
775#define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
776
777
778
779
780
781
782
783
784static inline __poll_t do_pollfd(struct pollfd *pollfd, poll_table *pwait,
785 bool *can_busy_poll,
786 __poll_t busy_flag)
787{
788 int fd = pollfd->fd;
789 __poll_t mask = 0, filter;
790 struct fd f;
791
792 if (fd < 0)
793 goto out;
794 mask = EPOLLNVAL;
795 f = fdget(fd);
796 if (!f.file)
797 goto out;
798
799
800 filter = demangle_poll(pollfd->events) | EPOLLERR | EPOLLHUP;
801 pwait->_key = filter | busy_flag;
802 mask = vfs_poll(f.file, pwait);
803 if (mask & busy_flag)
804 *can_busy_poll = true;
805 mask &= filter;
806 fdput(f);
807
808out:
809
810 pollfd->revents = mangle_poll(mask);
811 return mask;
812}
813
814static int do_poll(struct poll_list *list, struct poll_wqueues *wait,
815 struct timespec64 *end_time)
816{
817 poll_table* pt = &wait->pt;
818 ktime_t expire, *to = NULL;
819 int timed_out = 0, count = 0;
820 u64 slack = 0;
821 __poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
822 unsigned long busy_start = 0;
823
824
825 if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
826 pt->_qproc = NULL;
827 timed_out = 1;
828 }
829
830 if (end_time && !timed_out)
831 slack = select_estimate_accuracy(end_time);
832
833 for (;;) {
834 struct poll_list *walk;
835 bool can_busy_loop = false;
836
837 for (walk = list; walk != NULL; walk = walk->next) {
838 struct pollfd * pfd, * pfd_end;
839
840 pfd = walk->entries;
841 pfd_end = pfd + walk->len;
842 for (; pfd != pfd_end; pfd++) {
843
844
845
846
847
848
849
850 if (do_pollfd(pfd, pt, &can_busy_loop,
851 busy_flag)) {
852 count++;
853 pt->_qproc = NULL;
854
855 busy_flag = 0;
856 can_busy_loop = false;
857 }
858 }
859 }
860
861
862
863
864 pt->_qproc = NULL;
865 if (!count) {
866 count = wait->error;
867 if (signal_pending(current))
868 count = -EINTR;
869 }
870 if (count || timed_out)
871 break;
872
873
874 if (can_busy_loop && !need_resched()) {
875 if (!busy_start) {
876 busy_start = busy_loop_current_time();
877 continue;
878 }
879 if (!busy_loop_timeout(busy_start))
880 continue;
881 }
882 busy_flag = 0;
883
884
885
886
887
888
889 if (end_time && !to) {
890 expire = timespec64_to_ktime(*end_time);
891 to = &expire;
892 }
893
894 if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
895 timed_out = 1;
896 }
897 return count;
898}
899
900#define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
901 sizeof(struct pollfd))
902
903static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
904 struct timespec64 *end_time)
905{
906 struct poll_wqueues table;
907 int err = -EFAULT, fdcount, len, size;
908
909
910
911 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
912 struct poll_list *const head = (struct poll_list *)stack_pps;
913 struct poll_list *walk = head;
914 unsigned long todo = nfds;
915
916 if (nfds > rlimit(RLIMIT_NOFILE))
917 return -EINVAL;
918
919 len = min_t(unsigned int, nfds, N_STACK_PPS);
920 for (;;) {
921 walk->next = NULL;
922 walk->len = len;
923 if (!len)
924 break;
925
926 if (copy_from_user(walk->entries, ufds + nfds-todo,
927 sizeof(struct pollfd) * walk->len))
928 goto out_fds;
929
930 todo -= walk->len;
931 if (!todo)
932 break;
933
934 len = min(todo, POLLFD_PER_PAGE);
935 size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
936 walk = walk->next = kmalloc(size, GFP_KERNEL);
937 if (!walk) {
938 err = -ENOMEM;
939 goto out_fds;
940 }
941 }
942
943 poll_initwait(&table);
944 fdcount = do_poll(head, &table, end_time);
945 poll_freewait(&table);
946
947 for (walk = head; walk; walk = walk->next) {
948 struct pollfd *fds = walk->entries;
949 int j;
950
951 for (j = 0; j < walk->len; j++, ufds++)
952 if (__put_user(fds[j].revents, &ufds->revents))
953 goto out_fds;
954 }
955
956 err = fdcount;
957out_fds:
958 walk = head->next;
959 while (walk) {
960 struct poll_list *pos = walk;
961 walk = walk->next;
962 kfree(pos);
963 }
964
965 return err;
966}
967
968static long do_restart_poll(struct restart_block *restart_block)
969{
970 struct pollfd __user *ufds = restart_block->poll.ufds;
971 int nfds = restart_block->poll.nfds;
972 struct timespec64 *to = NULL, end_time;
973 int ret;
974
975 if (restart_block->poll.has_timeout) {
976 end_time.tv_sec = restart_block->poll.tv_sec;
977 end_time.tv_nsec = restart_block->poll.tv_nsec;
978 to = &end_time;
979 }
980
981 ret = do_sys_poll(ufds, nfds, to);
982
983 if (ret == -EINTR) {
984 restart_block->fn = do_restart_poll;
985 ret = -ERESTART_RESTARTBLOCK;
986 }
987 return ret;
988}
989
990SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
991 int, timeout_msecs)
992{
993 struct timespec64 end_time, *to = NULL;
994 int ret;
995
996 if (timeout_msecs >= 0) {
997 to = &end_time;
998 poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
999 NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
1000 }
1001
1002 ret = do_sys_poll(ufds, nfds, to);
1003
1004 if (ret == -EINTR) {
1005 struct restart_block *restart_block;
1006
1007 restart_block = ¤t->restart_block;
1008 restart_block->fn = do_restart_poll;
1009 restart_block->poll.ufds = ufds;
1010 restart_block->poll.nfds = nfds;
1011
1012 if (timeout_msecs >= 0) {
1013 restart_block->poll.tv_sec = end_time.tv_sec;
1014 restart_block->poll.tv_nsec = end_time.tv_nsec;
1015 restart_block->poll.has_timeout = 1;
1016 } else
1017 restart_block->poll.has_timeout = 0;
1018
1019 ret = -ERESTART_RESTARTBLOCK;
1020 }
1021 return ret;
1022}
1023
1024SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
1025 struct timespec __user *, tsp, const sigset_t __user *, sigmask,
1026 size_t, sigsetsize)
1027{
1028 sigset_t ksigmask, sigsaved;
1029 struct timespec64 ts, end_time, *to = NULL;
1030 int ret;
1031
1032 if (tsp) {
1033 if (get_timespec64(&ts, tsp))
1034 return -EFAULT;
1035
1036 to = &end_time;
1037 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1038 return -EINVAL;
1039 }
1040
1041 ret = set_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
1042 if (ret)
1043 return ret;
1044
1045 ret = do_sys_poll(ufds, nfds, to);
1046
1047 restore_user_sigmask(sigmask, &sigsaved, ret == -EINTR);
1048
1049 if (ret == -EINTR)
1050 ret = -ERESTARTNOHAND;
1051
1052 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
1053
1054 return ret;
1055}
1056
1057#ifdef CONFIG_COMPAT
1058#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1059
1060static
1061int compat_poll_select_copy_remaining(struct timespec64 *end_time, void __user *p,
1062 int timeval, int ret)
1063{
1064 struct timespec64 ts;
1065
1066 if (!p)
1067 return ret;
1068
1069 if (current->personality & STICKY_TIMEOUTS)
1070 goto sticky;
1071
1072
1073 if (!end_time->tv_sec && !end_time->tv_nsec)
1074 return ret;
1075
1076 ktime_get_ts64(&ts);
1077 ts = timespec64_sub(*end_time, ts);
1078 if (ts.tv_sec < 0)
1079 ts.tv_sec = ts.tv_nsec = 0;
1080
1081 if (timeval) {
1082 struct compat_timeval rtv;
1083
1084 rtv.tv_sec = ts.tv_sec;
1085 rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
1086
1087 if (!copy_to_user(p, &rtv, sizeof(rtv)))
1088 return ret;
1089 } else {
1090 if (!compat_put_timespec64(&ts, p))
1091 return ret;
1092 }
1093
1094
1095
1096
1097
1098
1099
1100
1101sticky:
1102 if (ret == -ERESTARTNOHAND)
1103 ret = -EINTR;
1104 return ret;
1105}
1106
1107
1108
1109
1110
1111static
1112int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1113 unsigned long *fdset)
1114{
1115 if (ufdset) {
1116 return compat_get_bitmap(fdset, ufdset, nr);
1117 } else {
1118 zero_fd_set(nr, fdset);
1119 return 0;
1120 }
1121}
1122
1123static
1124int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1125 unsigned long *fdset)
1126{
1127 if (!ufdset)
1128 return 0;
1129 return compat_put_bitmap(ufdset, fdset, nr);
1130}
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146static int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1147 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1148 struct timespec64 *end_time)
1149{
1150 fd_set_bits fds;
1151 void *bits;
1152 int size, max_fds, ret = -EINVAL;
1153 struct fdtable *fdt;
1154 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
1155
1156 if (n < 0)
1157 goto out_nofds;
1158
1159
1160 rcu_read_lock();
1161 fdt = files_fdtable(current->files);
1162 max_fds = fdt->max_fds;
1163 rcu_read_unlock();
1164 if (n > max_fds)
1165 n = max_fds;
1166
1167
1168
1169
1170
1171
1172 size = FDS_BYTES(n);
1173 bits = stack_fds;
1174 if (size > sizeof(stack_fds) / 6) {
1175 bits = kmalloc_array(6, size, GFP_KERNEL);
1176 ret = -ENOMEM;
1177 if (!bits)
1178 goto out_nofds;
1179 }
1180 fds.in = (unsigned long *) bits;
1181 fds.out = (unsigned long *) (bits + size);
1182 fds.ex = (unsigned long *) (bits + 2*size);
1183 fds.res_in = (unsigned long *) (bits + 3*size);
1184 fds.res_out = (unsigned long *) (bits + 4*size);
1185 fds.res_ex = (unsigned long *) (bits + 5*size);
1186
1187 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1188 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1189 (ret = compat_get_fd_set(n, exp, fds.ex)))
1190 goto out;
1191 zero_fd_set(n, fds.res_in);
1192 zero_fd_set(n, fds.res_out);
1193 zero_fd_set(n, fds.res_ex);
1194
1195 ret = do_select(n, &fds, end_time);
1196
1197 if (ret < 0)
1198 goto out;
1199 if (!ret) {
1200 ret = -ERESTARTNOHAND;
1201 if (signal_pending(current))
1202 goto out;
1203 ret = 0;
1204 }
1205
1206 if (compat_set_fd_set(n, inp, fds.res_in) ||
1207 compat_set_fd_set(n, outp, fds.res_out) ||
1208 compat_set_fd_set(n, exp, fds.res_ex))
1209 ret = -EFAULT;
1210out:
1211 if (bits != stack_fds)
1212 kfree(bits);
1213out_nofds:
1214 return ret;
1215}
1216
1217static int do_compat_select(int n, compat_ulong_t __user *inp,
1218 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1219 struct compat_timeval __user *tvp)
1220{
1221 struct timespec64 end_time, *to = NULL;
1222 struct compat_timeval tv;
1223 int ret;
1224
1225 if (tvp) {
1226 if (copy_from_user(&tv, tvp, sizeof(tv)))
1227 return -EFAULT;
1228
1229 to = &end_time;
1230 if (poll_select_set_timeout(to,
1231 tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
1232 (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
1233 return -EINVAL;
1234 }
1235
1236 ret = compat_core_sys_select(n, inp, outp, exp, to);
1237 ret = compat_poll_select_copy_remaining(&end_time, tvp, 1, ret);
1238
1239 return ret;
1240}
1241
1242COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp,
1243 compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
1244 struct compat_timeval __user *, tvp)
1245{
1246 return do_compat_select(n, inp, outp, exp, tvp);
1247}
1248
1249struct compat_sel_arg_struct {
1250 compat_ulong_t n;
1251 compat_uptr_t inp;
1252 compat_uptr_t outp;
1253 compat_uptr_t exp;
1254 compat_uptr_t tvp;
1255};
1256
1257COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg)
1258{
1259 struct compat_sel_arg_struct a;
1260
1261 if (copy_from_user(&a, arg, sizeof(a)))
1262 return -EFAULT;
1263 return do_compat_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1264 compat_ptr(a.exp), compat_ptr(a.tvp));
1265}
1266
1267static long do_compat_pselect(int n, compat_ulong_t __user *inp,
1268 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1269 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1270 compat_size_t sigsetsize)
1271{
1272 sigset_t ksigmask, sigsaved;
1273 struct timespec64 ts, end_time, *to = NULL;
1274 int ret;
1275
1276 if (tsp) {
1277 if (compat_get_timespec64(&ts, tsp))
1278 return -EFAULT;
1279
1280 to = &end_time;
1281 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1282 return -EINVAL;
1283 }
1284
1285 ret = set_compat_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
1286 if (ret)
1287 return ret;
1288
1289 ret = compat_core_sys_select(n, inp, outp, exp, to);
1290 restore_user_sigmask(sigmask, &sigsaved, ret == -ERESTARTNOHAND);
1291 ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret);
1292
1293 return ret;
1294}
1295
1296COMPAT_SYSCALL_DEFINE6(pselect6, int, n, compat_ulong_t __user *, inp,
1297 compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
1298 struct compat_timespec __user *, tsp, void __user *, sig)
1299{
1300 compat_size_t sigsetsize = 0;
1301 compat_uptr_t up = 0;
1302
1303 if (sig) {
1304 if (!access_ok(sig,
1305 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1306 __get_user(up, (compat_uptr_t __user *)sig) ||
1307 __get_user(sigsetsize,
1308 (compat_size_t __user *)(sig+sizeof(up))))
1309 return -EFAULT;
1310 }
1311 return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
1312 sigsetsize);
1313}
1314
1315COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds,
1316 unsigned int, nfds, struct compat_timespec __user *, tsp,
1317 const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
1318{
1319 sigset_t ksigmask, sigsaved;
1320 struct timespec64 ts, end_time, *to = NULL;
1321 int ret;
1322
1323 if (tsp) {
1324 if (compat_get_timespec64(&ts, tsp))
1325 return -EFAULT;
1326
1327 to = &end_time;
1328 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1329 return -EINVAL;
1330 }
1331
1332 ret = set_compat_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
1333 if (ret)
1334 return ret;
1335
1336 ret = do_sys_poll(ufds, nfds, to);
1337
1338 restore_user_sigmask(sigmask, &sigsaved, ret == -EINTR);
1339
1340 if (ret == -EINTR)
1341 ret = -ERESTARTNOHAND;
1342
1343 ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret);
1344
1345 return ret;
1346}
1347#endif
1348