2 * This file contains the procedures for the handling of select and poll
4 * Created for Linux based loosely upon Mathius Lattner's minix
5 * patches by Peter MacDonald. Heavily edited by Linus.
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.
13 * Changed sys_poll()/do_poll() to use PAGE_SIZE chunk-based allocation
14 * of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian).
17 #include <linux/kernel.h>
18 #include <linux/syscalls.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/poll.h>
22 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
23 #include <linux/file.h>
24 #include <linux/fdtable.h>
26 #include <linux/rcupdate.h>
28 #include <asm/uaccess.h>
30 struct poll_table_page {
31 struct poll_table_page * next;
32 struct poll_table_entry * entry;
33 struct poll_table_entry entries[0];
36 #define POLL_TABLE_FULL(table) \
37 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
40 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
41 * I have rewritten this, taking some shortcuts: This code may not be easy to
42 * follow, but it should be free of race-conditions, and it's practical. If you
43 * understand what I'm doing here, then you understand how the linux
44 * sleep/wakeup mechanism works.
46 * Two very simple procedures, poll_wait() and poll_freewait() make all the
47 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
48 * as all select/poll functions have to call it to add an entry to the
51 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
54 void poll_initwait(struct poll_wqueues *pwq)
56 init_poll_funcptr(&pwq->pt, __pollwait);
59 pwq->inline_index = 0;
62 EXPORT_SYMBOL(poll_initwait);
64 static void free_poll_entry(struct poll_table_entry *entry)
66 remove_wait_queue(entry->wait_address, &entry->wait);
70 void poll_freewait(struct poll_wqueues *pwq)
72 struct poll_table_page * p = pwq->table;
74 for (i = 0; i < pwq->inline_index; i++)
75 free_poll_entry(pwq->inline_entries + i);
77 struct poll_table_entry * entry;
78 struct poll_table_page *old;
83 free_poll_entry(entry);
84 } while (entry > p->entries);
87 free_page((unsigned long) old);
91 EXPORT_SYMBOL(poll_freewait);
93 static struct poll_table_entry *poll_get_entry(poll_table *_p)
95 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
96 struct poll_table_page *table = p->table;
98 if (p->inline_index < N_INLINE_POLL_ENTRIES)
99 return p->inline_entries + p->inline_index++;
101 if (!table || POLL_TABLE_FULL(table)) {
102 struct poll_table_page *new_table;
104 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
107 __set_current_state(TASK_RUNNING);
110 new_table->entry = new_table->entries;
111 new_table->next = table;
112 p->table = new_table;
116 return table->entry++;
119 /* Add a new entry */
120 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
123 struct poll_table_entry *entry = poll_get_entry(p);
128 entry->wait_address = wait_address;
129 init_waitqueue_entry(&entry->wait, current);
130 add_wait_queue(wait_address, &entry->wait);
134 * poll_select_set_timeout - helper function to setup the timeout value
135 * @to: pointer to timespec variable for the final timeout
136 * @sec: seconds (from user space)
137 * @nsec: nanoseconds (from user space)
139 * Note, we do not use a timespec for the user space value here, That
140 * way we can use the function for timeval and compat interfaces as well.
142 * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0.
144 int poll_select_set_timeout(struct timespec *to, long sec, long nsec)
146 struct timespec ts = {.tv_sec = sec, .tv_nsec = nsec};
148 if (!timespec_valid(&ts))
151 /* Optimize for the zero timeout value here */
153 to->tv_sec = to->tv_nsec = 0;
156 *to = timespec_add_safe(*to, ts);
161 static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
162 int timeval, int ret)
170 if (current->personality & STICKY_TIMEOUTS)
173 /* No update for zero timeout */
174 if (!end_time->tv_sec && !end_time->tv_nsec)
178 rts = timespec_sub(*end_time, rts);
180 rts.tv_sec = rts.tv_nsec = 0;
183 rtv.tv_sec = rts.tv_sec;
184 rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
186 if (!copy_to_user(p, &rtv, sizeof(rtv)))
189 } else if (!copy_to_user(p, &rts, sizeof(rts)))
193 * If an application puts its timeval in read-only memory, we
194 * don't want the Linux-specific update to the timeval to
195 * cause a fault after the select has completed
196 * successfully. However, because we're not updating the
197 * timeval, we can't restart the system call.
201 if (ret == -ERESTARTNOHAND)
208 #define FDS_IN(fds, n) (fds->in + n)
209 #define FDS_OUT(fds, n) (fds->out + n)
210 #define FDS_EX(fds, n) (fds->ex + n)
212 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
214 static int max_select_fd(unsigned long n, fd_set_bits *fds)
216 unsigned long *open_fds;
221 /* handle last in-complete long-word first */
222 set = ~(~0UL << (n & (__NFDBITS-1)));
224 fdt = files_fdtable(current->files);
225 open_fds = fdt->open_fds->fds_bits+n;
230 if (!(set & ~*open_fds))
241 if (set & ~*open_fds)
250 max += n * __NFDBITS;
256 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
257 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
258 #define POLLEX_SET (POLLPRI)
260 int do_select(int n, fd_set_bits *fds, s64 *timeout)
262 struct poll_wqueues table;
267 retval = max_select_fd(n, fds);
274 poll_initwait(&table);
280 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
283 set_current_state(TASK_INTERRUPTIBLE);
285 inp = fds->in; outp = fds->out; exp = fds->ex;
286 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
288 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
289 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
290 unsigned long res_in = 0, res_out = 0, res_ex = 0;
291 const struct file_operations *f_op = NULL;
292 struct file *file = NULL;
294 in = *inp++; out = *outp++; ex = *exp++;
295 all_bits = in | out | ex;
301 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
305 if (!(bit & all_bits))
307 file = fget_light(i, &fput_needed);
310 mask = DEFAULT_POLLMASK;
311 if (f_op && f_op->poll)
312 mask = (*f_op->poll)(file, retval ? NULL : wait);
313 fput_light(file, fput_needed);
314 if ((mask & POLLIN_SET) && (in & bit)) {
318 if ((mask & POLLOUT_SET) && (out & bit)) {
322 if ((mask & POLLEX_SET) && (ex & bit)) {
337 if (retval || !*timeout || signal_pending(current))
340 retval = table.error;
345 /* Wait indefinitely */
346 __timeout = MAX_SCHEDULE_TIMEOUT;
347 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
348 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
349 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
350 *timeout -= __timeout;
352 __timeout = *timeout;
355 __timeout = schedule_timeout(__timeout);
357 *timeout += __timeout;
359 __set_current_state(TASK_RUNNING);
361 poll_freewait(&table);
367 * We can actually return ERESTARTSYS instead of EINTR, but I'd
368 * like to be certain this leads to no problems. So I return
369 * EINTR just for safety.
371 * Update: ERESTARTSYS breaks at least the xview clock binary, so
372 * I'm trying ERESTARTNOHAND which restart only when you want to.
374 #define MAX_SELECT_SECONDS \
375 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
377 int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
378 fd_set __user *exp, s64 *timeout)
385 /* Allocate small arguments on the stack to save memory and be faster */
386 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
392 /* max_fds can increase, so grab it once to avoid race */
394 fdt = files_fdtable(current->files);
395 max_fds = fdt->max_fds;
401 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
402 * since we used fdset we need to allocate memory in units of
407 if (size > sizeof(stack_fds) / 6) {
408 /* Not enough space in on-stack array; must use kmalloc */
410 bits = kmalloc(6 * size, GFP_KERNEL);
415 fds.out = bits + size;
416 fds.ex = bits + 2*size;
417 fds.res_in = bits + 3*size;
418 fds.res_out = bits + 4*size;
419 fds.res_ex = bits + 5*size;
421 if ((ret = get_fd_set(n, inp, fds.in)) ||
422 (ret = get_fd_set(n, outp, fds.out)) ||
423 (ret = get_fd_set(n, exp, fds.ex)))
425 zero_fd_set(n, fds.res_in);
426 zero_fd_set(n, fds.res_out);
427 zero_fd_set(n, fds.res_ex);
429 ret = do_select(n, &fds, timeout);
434 ret = -ERESTARTNOHAND;
435 if (signal_pending(current))
440 if (set_fd_set(n, inp, fds.res_in) ||
441 set_fd_set(n, outp, fds.res_out) ||
442 set_fd_set(n, exp, fds.res_ex))
446 if (bits != stack_fds)
452 asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
453 fd_set __user *exp, struct timeval __user *tvp)
460 if (copy_from_user(&tv, tvp, sizeof(tv)))
463 if (tv.tv_sec < 0 || tv.tv_usec < 0)
466 /* Cast to u64 to make GCC stop complaining */
467 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
468 timeout = -1; /* infinite */
470 timeout = DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
471 timeout += tv.tv_sec * HZ;
475 ret = core_sys_select(n, inp, outp, exp, &timeout);
480 if (current->personality & STICKY_TIMEOUTS)
482 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
483 rtv.tv_sec = timeout;
484 if (timeval_compare(&rtv, &tv) >= 0)
486 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
489 * If an application puts its timeval in read-only
490 * memory, we don't want the Linux-specific update to
491 * the timeval to cause a fault after the select has
492 * completed successfully. However, because we're not
493 * updating the timeval, we can't restart the system
496 if (ret == -ERESTARTNOHAND)
504 #ifdef HAVE_SET_RESTORE_SIGMASK
505 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
506 fd_set __user *exp, struct timespec __user *tsp,
507 const sigset_t __user *sigmask, size_t sigsetsize)
509 s64 timeout = MAX_SCHEDULE_TIMEOUT;
510 sigset_t ksigmask, sigsaved;
515 if (copy_from_user(&ts, tsp, sizeof(ts)))
518 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
521 /* Cast to u64 to make GCC stop complaining */
522 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
523 timeout = -1; /* infinite */
525 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
526 timeout += ts.tv_sec * HZ;
531 /* XXX: Don't preclude handling different sized sigset_t's. */
532 if (sigsetsize != sizeof(sigset_t))
534 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
537 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
538 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
541 ret = core_sys_select(n, inp, outp, exp, &timeout);
546 if (current->personality & STICKY_TIMEOUTS)
548 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
550 rts.tv_sec = timeout;
551 if (timespec_compare(&rts, &ts) >= 0)
553 if (copy_to_user(tsp, &rts, sizeof(rts))) {
556 * If an application puts its timeval in read-only
557 * memory, we don't want the Linux-specific update to
558 * the timeval to cause a fault after the select has
559 * completed successfully. However, because we're not
560 * updating the timeval, we can't restart the system
563 if (ret == -ERESTARTNOHAND)
568 if (ret == -ERESTARTNOHAND) {
570 * Don't restore the signal mask yet. Let do_signal() deliver
571 * the signal on the way back to userspace, before the signal
575 memcpy(¤t->saved_sigmask, &sigsaved,
577 set_restore_sigmask();
580 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
586 * Most architectures can't handle 7-argument syscalls. So we provide a
587 * 6-argument version where the sixth argument is a pointer to a structure
588 * which has a pointer to the sigset_t itself followed by a size_t containing
591 asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
592 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
594 size_t sigsetsize = 0;
595 sigset_t __user *up = NULL;
598 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
599 || __get_user(up, (sigset_t __user * __user *)sig)
600 || __get_user(sigsetsize,
601 (size_t __user *)(sig+sizeof(void *))))
605 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
607 #endif /* HAVE_SET_RESTORE_SIGMASK */
610 struct poll_list *next;
612 struct pollfd entries[0];
615 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
618 * Fish for pollable events on the pollfd->fd file descriptor. We're only
619 * interested in events matching the pollfd->events mask, and the result
620 * matching that mask is both recorded in pollfd->revents and returned. The
621 * pwait poll_table will be used by the fd-provided poll handler for waiting,
624 static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
635 file = fget_light(fd, &fput_needed);
638 mask = DEFAULT_POLLMASK;
639 if (file->f_op && file->f_op->poll)
640 mask = file->f_op->poll(file, pwait);
641 /* Mask out unneeded events. */
642 mask &= pollfd->events | POLLERR | POLLHUP;
643 fput_light(file, fput_needed);
646 pollfd->revents = mask;
651 static int do_poll(unsigned int nfds, struct poll_list *list,
652 struct poll_wqueues *wait, s64 *timeout)
655 poll_table* pt = &wait->pt;
657 /* Optimise the no-wait case */
662 struct poll_list *walk;
665 set_current_state(TASK_INTERRUPTIBLE);
666 for (walk = list; walk != NULL; walk = walk->next) {
667 struct pollfd * pfd, * pfd_end;
670 pfd_end = pfd + walk->len;
671 for (; pfd != pfd_end; pfd++) {
673 * Fish for events. If we found one, record it
674 * and kill the poll_table, so we don't
675 * needlessly register any other waiters after
676 * this. They'll get immediately deregistered
677 * when we break out and return.
679 if (do_pollfd(pfd, pt)) {
686 * All waiters have already been registered, so don't provide
687 * a poll_table to them on the next loop iteration.
692 if (signal_pending(current))
695 if (count || !*timeout)
699 /* Wait indefinitely */
700 __timeout = MAX_SCHEDULE_TIMEOUT;
701 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
703 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
706 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
707 *timeout -= __timeout;
709 __timeout = *timeout;
713 __timeout = schedule_timeout(__timeout);
715 *timeout += __timeout;
717 __set_current_state(TASK_RUNNING);
721 #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
722 sizeof(struct pollfd))
724 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
726 struct poll_wqueues table;
727 int err = -EFAULT, fdcount, len, size;
728 /* Allocate small arguments on the stack to save memory and be
729 faster - use long to make sure the buffer is aligned properly
730 on 64 bit archs to avoid unaligned access */
731 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
732 struct poll_list *const head = (struct poll_list *)stack_pps;
733 struct poll_list *walk = head;
734 unsigned long todo = nfds;
736 if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
739 len = min_t(unsigned int, nfds, N_STACK_PPS);
746 if (copy_from_user(walk->entries, ufds + nfds-todo,
747 sizeof(struct pollfd) * walk->len))
754 len = min(todo, POLLFD_PER_PAGE);
755 size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
756 walk = walk->next = kmalloc(size, GFP_KERNEL);
763 poll_initwait(&table);
764 fdcount = do_poll(nfds, head, &table, timeout);
765 poll_freewait(&table);
767 for (walk = head; walk; walk = walk->next) {
768 struct pollfd *fds = walk->entries;
771 for (j = 0; j < walk->len; j++, ufds++)
772 if (__put_user(fds[j].revents, &ufds->revents))
780 struct poll_list *pos = walk;
788 static long do_restart_poll(struct restart_block *restart_block)
790 struct pollfd __user *ufds = (struct pollfd __user*)restart_block->arg0;
791 int nfds = restart_block->arg1;
792 s64 timeout = ((s64)restart_block->arg3<<32) | (s64)restart_block->arg2;
795 ret = do_sys_poll(ufds, nfds, &timeout);
797 restart_block->fn = do_restart_poll;
798 restart_block->arg2 = timeout & 0xFFFFFFFF;
799 restart_block->arg3 = (u64)timeout >> 32;
800 ret = -ERESTART_RESTARTBLOCK;
805 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
811 if (timeout_msecs > 0) {
813 /* We can only overflow if HZ > 1000 */
814 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
815 timeout_jiffies = -1;
818 timeout_jiffies = msecs_to_jiffies(timeout_msecs) + 1;
820 /* Infinite (< 0) or no (0) timeout */
821 timeout_jiffies = timeout_msecs;
824 ret = do_sys_poll(ufds, nfds, &timeout_jiffies);
826 struct restart_block *restart_block;
827 restart_block = ¤t_thread_info()->restart_block;
828 restart_block->fn = do_restart_poll;
829 restart_block->arg0 = (unsigned long)ufds;
830 restart_block->arg1 = nfds;
831 restart_block->arg2 = timeout_jiffies & 0xFFFFFFFF;
832 restart_block->arg3 = (u64)timeout_jiffies >> 32;
833 ret = -ERESTART_RESTARTBLOCK;
838 #ifdef HAVE_SET_RESTORE_SIGMASK
839 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
840 struct timespec __user *tsp, const sigset_t __user *sigmask,
843 sigset_t ksigmask, sigsaved;
849 if (copy_from_user(&ts, tsp, sizeof(ts)))
852 /* Cast to u64 to make GCC stop complaining */
853 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
854 timeout = -1; /* infinite */
856 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
857 timeout += ts.tv_sec * HZ;
862 /* XXX: Don't preclude handling different sized sigset_t's. */
863 if (sigsetsize != sizeof(sigset_t))
865 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
868 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
869 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
872 ret = do_sys_poll(ufds, nfds, &timeout);
874 /* We can restart this syscall, usually */
877 * Don't restore the signal mask yet. Let do_signal() deliver
878 * the signal on the way back to userspace, before the signal
882 memcpy(¤t->saved_sigmask, &sigsaved,
884 set_restore_sigmask();
886 ret = -ERESTARTNOHAND;
888 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
890 if (tsp && timeout >= 0) {
893 if (current->personality & STICKY_TIMEOUTS)
895 /* Yes, we know it's actually an s64, but it's also positive. */
896 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
898 rts.tv_sec = timeout;
899 if (timespec_compare(&rts, &ts) >= 0)
901 if (copy_to_user(tsp, &rts, sizeof(rts))) {
904 * If an application puts its timeval in read-only
905 * memory, we don't want the Linux-specific update to
906 * the timeval to cause a fault after the select has
907 * completed successfully. However, because we're not
908 * updating the timeval, we can't restart the system
911 if (ret == -ERESTARTNOHAND && timeout >= 0)
918 #endif /* HAVE_SET_RESTORE_SIGMASK */