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>
25 #include <linux/rcupdate.h>
27 #include <asm/uaccess.h>
29 struct poll_table_page {
30 struct poll_table_page * next;
31 struct poll_table_entry * entry;
32 struct poll_table_entry entries[0];
35 #define POLL_TABLE_FULL(table) \
36 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
39 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
40 * I have rewritten this, taking some shortcuts: This code may not be easy to
41 * follow, but it should be free of race-conditions, and it's practical. If you
42 * understand what I'm doing here, then you understand how the linux
43 * sleep/wakeup mechanism works.
45 * Two very simple procedures, poll_wait() and poll_freewait() make all the
46 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
47 * as all select/poll functions have to call it to add an entry to the
50 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
53 void poll_initwait(struct poll_wqueues *pwq)
55 init_poll_funcptr(&pwq->pt, __pollwait);
58 pwq->inline_index = 0;
61 EXPORT_SYMBOL(poll_initwait);
63 static void free_poll_entry(struct poll_table_entry *entry)
65 remove_wait_queue(entry->wait_address, &entry->wait);
69 void poll_freewait(struct poll_wqueues *pwq)
71 struct poll_table_page * p = pwq->table;
73 for (i = 0; i < pwq->inline_index; i++)
74 free_poll_entry(pwq->inline_entries + i);
76 struct poll_table_entry * entry;
77 struct poll_table_page *old;
82 free_poll_entry(entry);
83 } while (entry > p->entries);
86 free_page((unsigned long) old);
90 EXPORT_SYMBOL(poll_freewait);
92 static struct poll_table_entry *poll_get_entry(poll_table *_p)
94 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
95 struct poll_table_page *table = p->table;
97 if (p->inline_index < N_INLINE_POLL_ENTRIES)
98 return p->inline_entries + p->inline_index++;
100 if (!table || POLL_TABLE_FULL(table)) {
101 struct poll_table_page *new_table;
103 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
106 __set_current_state(TASK_RUNNING);
109 new_table->entry = new_table->entries;
110 new_table->next = table;
111 p->table = new_table;
115 return table->entry++;
118 /* Add a new entry */
119 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
122 struct poll_table_entry *entry = poll_get_entry(p);
127 entry->wait_address = wait_address;
128 init_waitqueue_entry(&entry->wait, current);
129 add_wait_queue(wait_address, &entry->wait);
132 #define FDS_IN(fds, n) (fds->in + n)
133 #define FDS_OUT(fds, n) (fds->out + n)
134 #define FDS_EX(fds, n) (fds->ex + n)
136 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
138 static int max_select_fd(unsigned long n, fd_set_bits *fds)
140 unsigned long *open_fds;
145 /* handle last in-complete long-word first */
146 set = ~(~0UL << (n & (__NFDBITS-1)));
148 fdt = files_fdtable(current->files);
149 open_fds = fdt->open_fds->fds_bits+n;
154 if (!(set & ~*open_fds))
165 if (set & ~*open_fds)
174 max += n * __NFDBITS;
180 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
181 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
182 #define POLLEX_SET (POLLPRI)
184 int do_select(int n, fd_set_bits *fds, s64 *timeout)
186 struct poll_wqueues table;
191 retval = max_select_fd(n, fds);
198 poll_initwait(&table);
204 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
207 set_current_state(TASK_INTERRUPTIBLE);
209 inp = fds->in; outp = fds->out; exp = fds->ex;
210 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
212 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
213 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
214 unsigned long res_in = 0, res_out = 0, res_ex = 0;
215 const struct file_operations *f_op = NULL;
216 struct file *file = NULL;
218 in = *inp++; out = *outp++; ex = *exp++;
219 all_bits = in | out | ex;
225 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
229 if (!(bit & all_bits))
231 file = fget_light(i, &fput_needed);
234 mask = DEFAULT_POLLMASK;
235 if (f_op && f_op->poll)
236 mask = (*f_op->poll)(file, retval ? NULL : wait);
237 fput_light(file, fput_needed);
238 if ((mask & POLLIN_SET) && (in & bit)) {
242 if ((mask & POLLOUT_SET) && (out & bit)) {
246 if ((mask & POLLEX_SET) && (ex & bit)) {
261 if (retval || !*timeout || signal_pending(current))
264 retval = table.error;
269 /* Wait indefinitely */
270 __timeout = MAX_SCHEDULE_TIMEOUT;
271 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
272 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
273 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
274 *timeout -= __timeout;
276 __timeout = *timeout;
279 __timeout = schedule_timeout(__timeout);
281 *timeout += __timeout;
283 __set_current_state(TASK_RUNNING);
285 poll_freewait(&table);
291 * We can actually return ERESTARTSYS instead of EINTR, but I'd
292 * like to be certain this leads to no problems. So I return
293 * EINTR just for safety.
295 * Update: ERESTARTSYS breaks at least the xview clock binary, so
296 * I'm trying ERESTARTNOHAND which restart only when you want to.
298 #define MAX_SELECT_SECONDS \
299 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
301 static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
302 fd_set __user *exp, s64 *timeout)
309 /* Allocate small arguments on the stack to save memory and be faster */
310 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
316 /* max_fds can increase, so grab it once to avoid race */
318 fdt = files_fdtable(current->files);
319 max_fds = fdt->max_fds;
325 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
326 * since we used fdset we need to allocate memory in units of
331 if (size > sizeof(stack_fds) / 6) {
332 /* Not enough space in on-stack array; must use kmalloc */
334 bits = kmalloc(6 * size, GFP_KERNEL);
339 fds.out = bits + size;
340 fds.ex = bits + 2*size;
341 fds.res_in = bits + 3*size;
342 fds.res_out = bits + 4*size;
343 fds.res_ex = bits + 5*size;
345 if ((ret = get_fd_set(n, inp, fds.in)) ||
346 (ret = get_fd_set(n, outp, fds.out)) ||
347 (ret = get_fd_set(n, exp, fds.ex)))
349 zero_fd_set(n, fds.res_in);
350 zero_fd_set(n, fds.res_out);
351 zero_fd_set(n, fds.res_ex);
353 ret = do_select(n, &fds, timeout);
358 ret = -ERESTARTNOHAND;
359 if (signal_pending(current))
364 if (set_fd_set(n, inp, fds.res_in) ||
365 set_fd_set(n, outp, fds.res_out) ||
366 set_fd_set(n, exp, fds.res_ex))
370 if (bits != stack_fds)
376 asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
377 fd_set __user *exp, struct timeval __user *tvp)
384 if (copy_from_user(&tv, tvp, sizeof(tv)))
387 if (tv.tv_sec < 0 || tv.tv_usec < 0)
390 /* Cast to u64 to make GCC stop complaining */
391 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
392 timeout = -1; /* infinite */
394 timeout = DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
395 timeout += tv.tv_sec * HZ;
399 ret = core_sys_select(n, inp, outp, exp, &timeout);
404 if (current->personality & STICKY_TIMEOUTS)
406 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
407 rtv.tv_sec = timeout;
408 if (timeval_compare(&rtv, &tv) >= 0)
410 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
413 * If an application puts its timeval in read-only
414 * memory, we don't want the Linux-specific update to
415 * the timeval to cause a fault after the select has
416 * completed successfully. However, because we're not
417 * updating the timeval, we can't restart the system
420 if (ret == -ERESTARTNOHAND)
428 #ifdef TIF_RESTORE_SIGMASK
429 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
430 fd_set __user *exp, struct timespec __user *tsp,
431 const sigset_t __user *sigmask, size_t sigsetsize)
433 s64 timeout = MAX_SCHEDULE_TIMEOUT;
434 sigset_t ksigmask, sigsaved;
439 if (copy_from_user(&ts, tsp, sizeof(ts)))
442 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
445 /* Cast to u64 to make GCC stop complaining */
446 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
447 timeout = -1; /* infinite */
449 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
450 timeout += ts.tv_sec * HZ;
455 /* XXX: Don't preclude handling different sized sigset_t's. */
456 if (sigsetsize != sizeof(sigset_t))
458 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
461 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
462 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
465 ret = core_sys_select(n, inp, outp, exp, &timeout);
470 if (current->personality & STICKY_TIMEOUTS)
472 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
474 rts.tv_sec = timeout;
475 if (timespec_compare(&rts, &ts) >= 0)
477 if (copy_to_user(tsp, &rts, sizeof(rts))) {
480 * If an application puts its timeval in read-only
481 * memory, we don't want the Linux-specific update to
482 * the timeval to cause a fault after the select has
483 * completed successfully. However, because we're not
484 * updating the timeval, we can't restart the system
487 if (ret == -ERESTARTNOHAND)
492 if (ret == -ERESTARTNOHAND) {
494 * Don't restore the signal mask yet. Let do_signal() deliver
495 * the signal on the way back to userspace, before the signal
499 memcpy(¤t->saved_sigmask, &sigsaved,
501 set_thread_flag(TIF_RESTORE_SIGMASK);
504 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
510 * Most architectures can't handle 7-argument syscalls. So we provide a
511 * 6-argument version where the sixth argument is a pointer to a structure
512 * which has a pointer to the sigset_t itself followed by a size_t containing
515 asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
516 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
518 size_t sigsetsize = 0;
519 sigset_t __user *up = NULL;
522 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
523 || __get_user(up, (sigset_t __user * __user *)sig)
524 || __get_user(sigsetsize,
525 (size_t __user *)(sig+sizeof(void *))))
529 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
531 #endif /* TIF_RESTORE_SIGMASK */
534 struct poll_list *next;
536 struct pollfd entries[0];
539 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
542 * Fish for pollable events on the pollfd->fd file descriptor. We're only
543 * interested in events matching the pollfd->events mask, and the result
544 * matching that mask is both recorded in pollfd->revents and returned. The
545 * pwait poll_table will be used by the fd-provided poll handler for waiting,
548 static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
559 file = fget_light(fd, &fput_needed);
562 mask = DEFAULT_POLLMASK;
563 if (file->f_op && file->f_op->poll)
564 mask = file->f_op->poll(file, pwait);
565 /* Mask out unneeded events. */
566 mask &= pollfd->events | POLLERR | POLLHUP;
567 fput_light(file, fput_needed);
570 pollfd->revents = mask;
575 static int do_poll(unsigned int nfds, struct poll_list *list,
576 struct poll_wqueues *wait, s64 *timeout)
579 poll_table* pt = &wait->pt;
581 /* Optimise the no-wait case */
586 struct poll_list *walk;
589 set_current_state(TASK_INTERRUPTIBLE);
590 for (walk = list; walk != NULL; walk = walk->next) {
591 struct pollfd * pfd, * pfd_end;
594 pfd_end = pfd + walk->len;
595 for (; pfd != pfd_end; pfd++) {
597 * Fish for events. If we found one, record it
598 * and kill the poll_table, so we don't
599 * needlessly register any other waiters after
600 * this. They'll get immediately deregistered
601 * when we break out and return.
603 if (do_pollfd(pfd, pt)) {
610 * All waiters have already been registered, so don't provide
611 * a poll_table to them on the next loop iteration.
616 if (signal_pending(current))
619 if (count || !*timeout)
623 /* Wait indefinitely */
624 __timeout = MAX_SCHEDULE_TIMEOUT;
625 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
627 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
630 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
631 *timeout -= __timeout;
633 __timeout = *timeout;
637 __timeout = schedule_timeout(__timeout);
639 *timeout += __timeout;
641 __set_current_state(TASK_RUNNING);
645 #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
646 sizeof(struct pollfd))
648 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
650 struct poll_wqueues table;
651 int err = -EFAULT, fdcount, len, size;
652 /* Allocate small arguments on the stack to save memory and be
653 faster - use long to make sure the buffer is aligned properly
654 on 64 bit archs to avoid unaligned access */
655 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
656 struct poll_list *const head = (struct poll_list *)stack_pps;
657 struct poll_list *walk = head;
658 unsigned long todo = nfds;
660 if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
663 len = min_t(unsigned int, nfds, N_STACK_PPS);
670 if (copy_from_user(walk->entries, ufds + nfds-todo,
671 sizeof(struct pollfd) * walk->len))
678 len = min(todo, POLLFD_PER_PAGE);
679 size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
680 walk = walk->next = kmalloc(size, GFP_KERNEL);
687 poll_initwait(&table);
688 fdcount = do_poll(nfds, head, &table, timeout);
689 poll_freewait(&table);
691 for (walk = head; walk; walk = walk->next) {
692 struct pollfd *fds = walk->entries;
695 for (j = 0; j < walk->len; j++, ufds++)
696 if (__put_user(fds[j].revents, &ufds->revents))
704 struct poll_list *pos = walk;
712 static long do_restart_poll(struct restart_block *restart_block)
714 struct pollfd __user *ufds = (struct pollfd __user*)restart_block->arg0;
715 int nfds = restart_block->arg1;
716 s64 timeout = ((s64)restart_block->arg3<<32) | (s64)restart_block->arg2;
719 ret = do_sys_poll(ufds, nfds, &timeout);
721 restart_block->fn = do_restart_poll;
722 restart_block->arg2 = timeout & 0xFFFFFFFF;
723 restart_block->arg3 = (u64)timeout >> 32;
724 ret = -ERESTART_RESTARTBLOCK;
729 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
735 if (timeout_msecs > 0) {
737 /* We can only overflow if HZ > 1000 */
738 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
739 timeout_jiffies = -1;
742 timeout_jiffies = msecs_to_jiffies(timeout_msecs) + 1;
744 /* Infinite (< 0) or no (0) timeout */
745 timeout_jiffies = timeout_msecs;
748 ret = do_sys_poll(ufds, nfds, &timeout_jiffies);
750 struct restart_block *restart_block;
751 restart_block = ¤t_thread_info()->restart_block;
752 restart_block->fn = do_restart_poll;
753 restart_block->arg0 = (unsigned long)ufds;
754 restart_block->arg1 = nfds;
755 restart_block->arg2 = timeout_jiffies & 0xFFFFFFFF;
756 restart_block->arg3 = (u64)timeout_jiffies >> 32;
757 ret = -ERESTART_RESTARTBLOCK;
762 #ifdef TIF_RESTORE_SIGMASK
763 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
764 struct timespec __user *tsp, const sigset_t __user *sigmask,
767 sigset_t ksigmask, sigsaved;
773 if (copy_from_user(&ts, tsp, sizeof(ts)))
776 /* Cast to u64 to make GCC stop complaining */
777 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
778 timeout = -1; /* infinite */
780 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
781 timeout += ts.tv_sec * HZ;
786 /* XXX: Don't preclude handling different sized sigset_t's. */
787 if (sigsetsize != sizeof(sigset_t))
789 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
792 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
793 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
796 ret = do_sys_poll(ufds, nfds, &timeout);
798 /* We can restart this syscall, usually */
801 * Don't restore the signal mask yet. Let do_signal() deliver
802 * the signal on the way back to userspace, before the signal
806 memcpy(¤t->saved_sigmask, &sigsaved,
808 set_thread_flag(TIF_RESTORE_SIGMASK);
810 ret = -ERESTARTNOHAND;
812 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
814 if (tsp && timeout >= 0) {
817 if (current->personality & STICKY_TIMEOUTS)
819 /* Yes, we know it's actually an s64, but it's also positive. */
820 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
822 rts.tv_sec = timeout;
823 if (timespec_compare(&rts, &ts) >= 0)
825 if (copy_to_user(tsp, &rts, sizeof(rts))) {
828 * If an application puts its timeval in read-only
829 * memory, we don't want the Linux-specific update to
830 * the timeval to cause a fault after the select has
831 * completed successfully. However, because we're not
832 * updating the timeval, we can't restart the system
835 if (ret == -ERESTARTNOHAND && timeout >= 0)
842 #endif /* TIF_RESTORE_SIGMASK */