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 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
31 struct poll_table_page {
32 struct poll_table_page * next;
33 struct poll_table_entry * entry;
34 struct poll_table_entry entries[0];
37 #define POLL_TABLE_FULL(table) \
38 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
41 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
42 * I have rewritten this, taking some shortcuts: This code may not be easy to
43 * follow, but it should be free of race-conditions, and it's practical. If you
44 * understand what I'm doing here, then you understand how the linux
45 * sleep/wakeup mechanism works.
47 * Two very simple procedures, poll_wait() and poll_freewait() make all the
48 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
49 * as all select/poll functions have to call it to add an entry to the
52 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
55 void poll_initwait(struct poll_wqueues *pwq)
57 init_poll_funcptr(&pwq->pt, __pollwait);
60 pwq->inline_index = 0;
63 EXPORT_SYMBOL(poll_initwait);
65 static void free_poll_entry(struct poll_table_entry *entry)
67 remove_wait_queue(entry->wait_address, &entry->wait);
71 void poll_freewait(struct poll_wqueues *pwq)
73 struct poll_table_page * p = pwq->table;
75 for (i = 0; i < pwq->inline_index; i++)
76 free_poll_entry(pwq->inline_entries + i);
78 struct poll_table_entry * entry;
79 struct poll_table_page *old;
84 free_poll_entry(entry);
85 } while (entry > p->entries);
88 free_page((unsigned long) old);
92 EXPORT_SYMBOL(poll_freewait);
94 static struct poll_table_entry *poll_get_entry(poll_table *_p)
96 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
97 struct poll_table_page *table = p->table;
99 if (p->inline_index < N_INLINE_POLL_ENTRIES)
100 return p->inline_entries + p->inline_index++;
102 if (!table || POLL_TABLE_FULL(table)) {
103 struct poll_table_page *new_table;
105 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
108 __set_current_state(TASK_RUNNING);
111 new_table->entry = new_table->entries;
112 new_table->next = table;
113 p->table = new_table;
117 return table->entry++;
120 /* Add a new entry */
121 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
124 struct poll_table_entry *entry = poll_get_entry(p);
129 entry->wait_address = wait_address;
130 init_waitqueue_entry(&entry->wait, current);
131 add_wait_queue(wait_address, &entry->wait);
134 #define FDS_IN(fds, n) (fds->in + n)
135 #define FDS_OUT(fds, n) (fds->out + n)
136 #define FDS_EX(fds, n) (fds->ex + n)
138 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
140 static int max_select_fd(unsigned long n, fd_set_bits *fds)
142 unsigned long *open_fds;
147 /* handle last in-complete long-word first */
148 set = ~(~0UL << (n & (__NFDBITS-1)));
150 fdt = files_fdtable(current->files);
151 open_fds = fdt->open_fds->fds_bits+n;
156 if (!(set & ~*open_fds))
167 if (set & ~*open_fds)
176 max += n * __NFDBITS;
182 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
183 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
184 #define ISSET(i,m) (((i)&*(m)) != 0)
185 #define SET(i,m) (*(m) |= (i))
187 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
188 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
189 #define POLLEX_SET (POLLPRI)
191 int do_select(int n, fd_set_bits *fds, s64 *timeout)
193 struct poll_wqueues table;
198 retval = max_select_fd(n, fds);
205 poll_initwait(&table);
211 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
214 set_current_state(TASK_INTERRUPTIBLE);
216 inp = fds->in; outp = fds->out; exp = fds->ex;
217 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
219 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
220 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
221 unsigned long res_in = 0, res_out = 0, res_ex = 0;
222 const struct file_operations *f_op = NULL;
223 struct file *file = NULL;
225 in = *inp++; out = *outp++; ex = *exp++;
226 all_bits = in | out | ex;
232 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
236 if (!(bit & all_bits))
238 file = fget_light(i, &fput_needed);
241 mask = DEFAULT_POLLMASK;
242 if (f_op && f_op->poll)
243 mask = (*f_op->poll)(file, retval ? NULL : wait);
244 fput_light(file, fput_needed);
245 if ((mask & POLLIN_SET) && (in & bit)) {
249 if ((mask & POLLOUT_SET) && (out & bit)) {
253 if ((mask & POLLEX_SET) && (ex & bit)) {
268 if (retval || !*timeout || signal_pending(current))
271 retval = table.error;
276 /* Wait indefinitely */
277 __timeout = MAX_SCHEDULE_TIMEOUT;
278 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
279 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
280 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
281 *timeout -= __timeout;
283 __timeout = *timeout;
286 __timeout = schedule_timeout(__timeout);
288 *timeout += __timeout;
290 __set_current_state(TASK_RUNNING);
292 poll_freewait(&table);
298 * We can actually return ERESTARTSYS instead of EINTR, but I'd
299 * like to be certain this leads to no problems. So I return
300 * EINTR just for safety.
302 * Update: ERESTARTSYS breaks at least the xview clock binary, so
303 * I'm trying ERESTARTNOHAND which restart only when you want to.
305 #define MAX_SELECT_SECONDS \
306 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
308 static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
309 fd_set __user *exp, s64 *timeout)
316 /* Allocate small arguments on the stack to save memory and be faster */
317 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
323 /* max_fds can increase, so grab it once to avoid race */
325 fdt = files_fdtable(current->files);
326 max_fds = fdt->max_fds;
332 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
333 * since we used fdset we need to allocate memory in units of
338 if (size > sizeof(stack_fds) / 6) {
339 /* Not enough space in on-stack array; must use kmalloc */
341 bits = kmalloc(6 * size, GFP_KERNEL);
346 fds.out = bits + size;
347 fds.ex = bits + 2*size;
348 fds.res_in = bits + 3*size;
349 fds.res_out = bits + 4*size;
350 fds.res_ex = bits + 5*size;
352 if ((ret = get_fd_set(n, inp, fds.in)) ||
353 (ret = get_fd_set(n, outp, fds.out)) ||
354 (ret = get_fd_set(n, exp, fds.ex)))
356 zero_fd_set(n, fds.res_in);
357 zero_fd_set(n, fds.res_out);
358 zero_fd_set(n, fds.res_ex);
360 ret = do_select(n, &fds, timeout);
365 ret = -ERESTARTNOHAND;
366 if (signal_pending(current))
371 if (set_fd_set(n, inp, fds.res_in) ||
372 set_fd_set(n, outp, fds.res_out) ||
373 set_fd_set(n, exp, fds.res_ex))
377 if (bits != stack_fds)
383 asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
384 fd_set __user *exp, struct timeval __user *tvp)
391 if (copy_from_user(&tv, tvp, sizeof(tv)))
394 if (tv.tv_sec < 0 || tv.tv_usec < 0)
397 /* Cast to u64 to make GCC stop complaining */
398 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
399 timeout = -1; /* infinite */
401 timeout = DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
402 timeout += tv.tv_sec * HZ;
406 ret = core_sys_select(n, inp, outp, exp, &timeout);
411 if (current->personality & STICKY_TIMEOUTS)
413 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
414 rtv.tv_sec = timeout;
415 if (timeval_compare(&rtv, &tv) >= 0)
417 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
420 * If an application puts its timeval in read-only
421 * memory, we don't want the Linux-specific update to
422 * the timeval to cause a fault after the select has
423 * completed successfully. However, because we're not
424 * updating the timeval, we can't restart the system
427 if (ret == -ERESTARTNOHAND)
435 #ifdef TIF_RESTORE_SIGMASK
436 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
437 fd_set __user *exp, struct timespec __user *tsp,
438 const sigset_t __user *sigmask, size_t sigsetsize)
440 s64 timeout = MAX_SCHEDULE_TIMEOUT;
441 sigset_t ksigmask, sigsaved;
446 if (copy_from_user(&ts, tsp, sizeof(ts)))
449 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
452 /* Cast to u64 to make GCC stop complaining */
453 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
454 timeout = -1; /* infinite */
456 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
457 timeout += ts.tv_sec * HZ;
462 /* XXX: Don't preclude handling different sized sigset_t's. */
463 if (sigsetsize != sizeof(sigset_t))
465 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
468 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
469 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
472 ret = core_sys_select(n, inp, outp, exp, &timeout);
477 if (current->personality & STICKY_TIMEOUTS)
479 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
481 rts.tv_sec = timeout;
482 if (timespec_compare(&rts, &ts) >= 0)
484 if (copy_to_user(tsp, &rts, sizeof(rts))) {
487 * If an application puts its timeval in read-only
488 * memory, we don't want the Linux-specific update to
489 * the timeval to cause a fault after the select has
490 * completed successfully. However, because we're not
491 * updating the timeval, we can't restart the system
494 if (ret == -ERESTARTNOHAND)
499 if (ret == -ERESTARTNOHAND) {
501 * Don't restore the signal mask yet. Let do_signal() deliver
502 * the signal on the way back to userspace, before the signal
506 memcpy(¤t->saved_sigmask, &sigsaved,
508 set_thread_flag(TIF_RESTORE_SIGMASK);
511 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
517 * Most architectures can't handle 7-argument syscalls. So we provide a
518 * 6-argument version where the sixth argument is a pointer to a structure
519 * which has a pointer to the sigset_t itself followed by a size_t containing
522 asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
523 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
525 size_t sigsetsize = 0;
526 sigset_t __user *up = NULL;
529 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
530 || __get_user(up, (sigset_t __user * __user *)sig)
531 || __get_user(sigsetsize,
532 (size_t __user *)(sig+sizeof(void *))))
536 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
538 #endif /* TIF_RESTORE_SIGMASK */
541 struct poll_list *next;
543 struct pollfd entries[0];
546 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
549 * Fish for pollable events on the pollfd->fd file descriptor. We're only
550 * interested in events matching the pollfd->events mask, and the result
551 * matching that mask is both recorded in pollfd->revents and returned. The
552 * pwait poll_table will be used by the fd-provided poll handler for waiting,
555 static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
566 file = fget_light(fd, &fput_needed);
569 mask = DEFAULT_POLLMASK;
570 if (file->f_op && file->f_op->poll)
571 mask = file->f_op->poll(file, pwait);
572 /* Mask out unneeded events. */
573 mask &= pollfd->events | POLLERR | POLLHUP;
574 fput_light(file, fput_needed);
577 pollfd->revents = mask;
582 static int do_poll(unsigned int nfds, struct poll_list *list,
583 struct poll_wqueues *wait, s64 *timeout)
586 poll_table* pt = &wait->pt;
588 /* Optimise the no-wait case */
593 struct poll_list *walk;
596 set_current_state(TASK_INTERRUPTIBLE);
597 for (walk = list; walk != NULL; walk = walk->next) {
598 struct pollfd * pfd, * pfd_end;
601 pfd_end = pfd + walk->len;
602 for (; pfd != pfd_end; pfd++) {
604 * Fish for events. If we found one, record it
605 * and kill the poll_table, so we don't
606 * needlessly register any other waiters after
607 * this. They'll get immediately deregistered
608 * when we break out and return.
610 if (do_pollfd(pfd, pt)) {
617 * All waiters have already been registered, so don't provide
618 * a poll_table to them on the next loop iteration.
621 if (count || !*timeout || signal_pending(current))
628 /* Wait indefinitely */
629 __timeout = MAX_SCHEDULE_TIMEOUT;
630 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
632 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
635 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
636 *timeout -= __timeout;
638 __timeout = *timeout;
642 __timeout = schedule_timeout(__timeout);
644 *timeout += __timeout;
646 __set_current_state(TASK_RUNNING);
650 #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
651 sizeof(struct pollfd))
653 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
655 struct poll_wqueues table;
658 struct poll_list *head;
659 struct poll_list *walk;
660 /* Allocate small arguments on the stack to save memory and be
661 faster - use long to make sure the buffer is aligned properly
662 on 64 bit archs to avoid unaligned access */
663 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
664 struct poll_list *stack_pp = NULL;
666 /* Do a sanity check on nfds ... */
667 if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
670 poll_initwait(&table);
677 struct poll_list *pp;
679 if (stack_pp == NULL)
682 num = POLLFD_PER_PAGE;
685 size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
687 stack_pp = pp = (struct poll_list *)stack_pps;
689 pp = kmalloc(size, GFP_KERNEL);
701 if (copy_from_user(pp->entries, ufds + nfds-i,
702 sizeof(struct pollfd)*num)) {
709 fdcount = do_poll(nfds, head, &table, timeout);
711 /* OK, now copy the revents fields back to user space. */
714 while(walk != NULL) {
715 struct pollfd *fds = walk->entries;
718 for (j=0; j < walk->len; j++, ufds++) {
719 if(__put_user(fds[j].revents, &ufds->revents))
725 if (!fdcount && signal_pending(current))
730 struct poll_list *pp = walk->next;
731 if (walk != stack_pp)
735 poll_freewait(&table);
739 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
744 if (timeout_msecs > 0) {
746 /* We can only overflow if HZ > 1000 */
747 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
748 timeout_jiffies = -1;
751 timeout_jiffies = msecs_to_jiffies(timeout_msecs);
753 /* Infinite (< 0) or no (0) timeout */
754 timeout_jiffies = timeout_msecs;
757 return do_sys_poll(ufds, nfds, &timeout_jiffies);
760 #ifdef TIF_RESTORE_SIGMASK
761 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
762 struct timespec __user *tsp, const sigset_t __user *sigmask,
765 sigset_t ksigmask, sigsaved;
771 if (copy_from_user(&ts, tsp, sizeof(ts)))
774 /* Cast to u64 to make GCC stop complaining */
775 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
776 timeout = -1; /* infinite */
778 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
779 timeout += ts.tv_sec * HZ;
784 /* XXX: Don't preclude handling different sized sigset_t's. */
785 if (sigsetsize != sizeof(sigset_t))
787 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
790 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
791 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
794 ret = do_sys_poll(ufds, nfds, &timeout);
796 /* We can restart this syscall, usually */
799 * Don't restore the signal mask yet. Let do_signal() deliver
800 * the signal on the way back to userspace, before the signal
804 memcpy(¤t->saved_sigmask, &sigsaved,
806 set_thread_flag(TIF_RESTORE_SIGMASK);
808 ret = -ERESTARTNOHAND;
810 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
812 if (tsp && timeout >= 0) {
815 if (current->personality & STICKY_TIMEOUTS)
817 /* Yes, we know it's actually an s64, but it's also positive. */
818 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
820 rts.tv_sec = timeout;
821 if (timespec_compare(&rts, &ts) >= 0)
823 if (copy_to_user(tsp, &rts, sizeof(rts))) {
826 * If an application puts its timeval in read-only
827 * memory, we don't want the Linux-specific update to
828 * the timeval to cause a fault after the select has
829 * completed successfully. However, because we're not
830 * updating the timeval, we can't restart the system
833 if (ret == -ERESTARTNOHAND && timeout >= 0)
840 #endif /* TIF_RESTORE_SIGMASK */