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/syscalls.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/smp_lock.h>
21 #include <linux/poll.h>
22 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
23 #include <linux/file.h>
25 #include <linux/rcupdate.h>
26 #include <linux/kallsyms.h>
28 #include <asm/uaccess.h>
30 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
31 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
33 struct poll_table_page {
34 struct poll_table_page * next;
35 struct poll_table_entry * entry;
36 struct poll_table_entry entries[0];
39 #define POLL_TABLE_FULL(table) \
40 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
43 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
44 * I have rewritten this, taking some shortcuts: This code may not be easy to
45 * follow, but it should be free of race-conditions, and it's practical. If you
46 * understand what I'm doing here, then you understand how the linux
47 * sleep/wakeup mechanism works.
49 * Two very simple procedures, poll_wait() and poll_freewait() make all the
50 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
51 * as all select/poll functions have to call it to add an entry to the
54 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
57 void poll_initwait(struct poll_wqueues *pwq)
59 init_poll_funcptr(&pwq->pt, __pollwait);
62 pwq->inline_index = 0;
65 EXPORT_SYMBOL(poll_initwait);
67 static void free_poll_entry(struct poll_table_entry *entry)
69 if (remove_wait_queue(entry->wait_address,&entry->wait) < 0)
70 print_symbol("bad poll-entry for %s", (unsigned long) entry->filp->f_op->poll);
74 void poll_freewait(struct poll_wqueues *pwq)
76 struct poll_table_page * p = pwq->table;
78 for (i = 0; i < pwq->inline_index; i++)
79 free_poll_entry(pwq->inline_entries + i);
81 struct poll_table_entry * entry;
82 struct poll_table_page *old;
87 free_poll_entry(entry);
88 } while (entry > p->entries);
91 free_page((unsigned long) old);
95 EXPORT_SYMBOL(poll_freewait);
97 static struct poll_table_entry *poll_get_entry(poll_table *_p)
99 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
100 struct poll_table_page *table = p->table;
102 if (p->inline_index < N_INLINE_POLL_ENTRIES)
103 return p->inline_entries + p->inline_index++;
105 if (!table || POLL_TABLE_FULL(table)) {
106 struct poll_table_page *new_table;
108 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
111 __set_current_state(TASK_RUNNING);
114 new_table->entry = new_table->entries;
115 new_table->next = table;
116 p->table = new_table;
120 return table->entry++;
123 /* Add a new entry */
124 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
127 struct poll_table_entry *entry = poll_get_entry(p);
132 entry->wait_address = wait_address;
133 init_waitqueue_entry(&entry->wait, current);
134 add_wait_queue(wait_address,&entry->wait);
137 #define FDS_IN(fds, n) (fds->in + n)
138 #define FDS_OUT(fds, n) (fds->out + n)
139 #define FDS_EX(fds, n) (fds->ex + n)
141 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
143 static int max_select_fd(unsigned long n, fd_set_bits *fds)
145 unsigned long *open_fds;
150 /* handle last in-complete long-word first */
151 set = ~(~0UL << (n & (__NFDBITS-1)));
153 fdt = files_fdtable(current->files);
154 open_fds = fdt->open_fds->fds_bits+n;
159 if (!(set & ~*open_fds))
170 if (set & ~*open_fds)
179 max += n * __NFDBITS;
185 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
186 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
187 #define ISSET(i,m) (((i)&*(m)) != 0)
188 #define SET(i,m) (*(m) |= (i))
190 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
191 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
192 #define POLLEX_SET (POLLPRI)
194 int do_select(int n, fd_set_bits *fds, s64 *timeout)
196 struct poll_wqueues table;
201 retval = max_select_fd(n, fds);
208 poll_initwait(&table);
214 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
217 set_current_state(TASK_INTERRUPTIBLE);
219 inp = fds->in; outp = fds->out; exp = fds->ex;
220 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
222 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
223 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
224 unsigned long res_in = 0, res_out = 0, res_ex = 0;
225 const struct file_operations *f_op = NULL;
226 struct file *file = NULL;
228 in = *inp++; out = *outp++; ex = *exp++;
229 all_bits = in | out | ex;
235 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
239 if (!(bit & all_bits))
241 file = fget_light(i, &fput_needed);
244 mask = DEFAULT_POLLMASK;
245 if (f_op && f_op->poll)
246 mask = (*f_op->poll)(file, retval ? NULL : wait);
247 fput_light(file, fput_needed);
248 if ((mask & POLLIN_SET) && (in & bit)) {
252 if ((mask & POLLOUT_SET) && (out & bit)) {
256 if ((mask & POLLEX_SET) && (ex & bit)) {
271 if (retval || !*timeout || signal_pending(current))
274 retval = table.error;
279 /* Wait indefinitely */
280 __timeout = MAX_SCHEDULE_TIMEOUT;
281 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
282 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
283 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
284 *timeout -= __timeout;
286 __timeout = *timeout;
289 __timeout = schedule_timeout(__timeout);
291 *timeout += __timeout;
293 __set_current_state(TASK_RUNNING);
295 poll_freewait(&table);
301 * We can actually return ERESTARTSYS instead of EINTR, but I'd
302 * like to be certain this leads to no problems. So I return
303 * EINTR just for safety.
305 * Update: ERESTARTSYS breaks at least the xview clock binary, so
306 * I'm trying ERESTARTNOHAND which restart only when you want to.
308 #define MAX_SELECT_SECONDS \
309 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
311 static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
312 fd_set __user *exp, s64 *timeout)
319 /* Allocate small arguments on the stack to save memory and be faster */
320 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
326 /* max_fdset can increase, so grab it once to avoid race */
328 fdt = files_fdtable(current->files);
329 max_fdset = fdt->max_fdset;
335 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
336 * since we used fdset we need to allocate memory in units of
341 if (size > sizeof(stack_fds) / 6) {
342 /* Not enough space in on-stack array; must use kmalloc */
344 bits = kmalloc(6 * size, GFP_KERNEL);
349 fds.out = bits + size;
350 fds.ex = bits + 2*size;
351 fds.res_in = bits + 3*size;
352 fds.res_out = bits + 4*size;
353 fds.res_ex = bits + 5*size;
355 if ((ret = get_fd_set(n, inp, fds.in)) ||
356 (ret = get_fd_set(n, outp, fds.out)) ||
357 (ret = get_fd_set(n, exp, fds.ex)))
359 zero_fd_set(n, fds.res_in);
360 zero_fd_set(n, fds.res_out);
361 zero_fd_set(n, fds.res_ex);
363 ret = do_select(n, &fds, timeout);
368 ret = -ERESTARTNOHAND;
369 if (signal_pending(current))
374 if (set_fd_set(n, inp, fds.res_in) ||
375 set_fd_set(n, outp, fds.res_out) ||
376 set_fd_set(n, exp, fds.res_ex))
380 if (bits != stack_fds)
386 asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
387 fd_set __user *exp, struct timeval __user *tvp)
394 if (copy_from_user(&tv, tvp, sizeof(tv)))
397 if (tv.tv_sec < 0 || tv.tv_usec < 0)
400 /* Cast to u64 to make GCC stop complaining */
401 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
402 timeout = -1; /* infinite */
404 timeout = ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
405 timeout += tv.tv_sec * HZ;
409 ret = core_sys_select(n, inp, outp, exp, &timeout);
414 if (current->personality & STICKY_TIMEOUTS)
416 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
417 rtv.tv_sec = timeout;
418 if (timeval_compare(&rtv, &tv) >= 0)
420 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
423 * If an application puts its timeval in read-only
424 * memory, we don't want the Linux-specific update to
425 * the timeval to cause a fault after the select has
426 * completed successfully. However, because we're not
427 * updating the timeval, we can't restart the system
430 if (ret == -ERESTARTNOHAND)
438 #ifdef TIF_RESTORE_SIGMASK
439 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
440 fd_set __user *exp, struct timespec __user *tsp,
441 const sigset_t __user *sigmask, size_t sigsetsize)
443 s64 timeout = MAX_SCHEDULE_TIMEOUT;
444 sigset_t ksigmask, sigsaved;
449 if (copy_from_user(&ts, tsp, sizeof(ts)))
452 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
455 /* Cast to u64 to make GCC stop complaining */
456 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
457 timeout = -1; /* infinite */
459 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
460 timeout += ts.tv_sec * HZ;
465 /* XXX: Don't preclude handling different sized sigset_t's. */
466 if (sigsetsize != sizeof(sigset_t))
468 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
471 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
472 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
475 ret = core_sys_select(n, inp, outp, exp, &timeout);
480 if (current->personality & STICKY_TIMEOUTS)
482 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
484 rts.tv_sec = timeout;
485 if (timespec_compare(&rts, &ts) >= 0)
487 if (copy_to_user(tsp, &rts, sizeof(rts))) {
490 * If an application puts its timeval in read-only
491 * memory, we don't want the Linux-specific update to
492 * the timeval to cause a fault after the select has
493 * completed successfully. However, because we're not
494 * updating the timeval, we can't restart the system
497 if (ret == -ERESTARTNOHAND)
502 if (ret == -ERESTARTNOHAND) {
504 * Don't restore the signal mask yet. Let do_signal() deliver
505 * the signal on the way back to userspace, before the signal
509 memcpy(¤t->saved_sigmask, &sigsaved,
511 set_thread_flag(TIF_RESTORE_SIGMASK);
514 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
520 * Most architectures can't handle 7-argument syscalls. So we provide a
521 * 6-argument version where the sixth argument is a pointer to a structure
522 * which has a pointer to the sigset_t itself followed by a size_t containing
525 asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
526 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
528 size_t sigsetsize = 0;
529 sigset_t __user *up = NULL;
532 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
533 || __get_user(up, (sigset_t __user * __user *)sig)
534 || __get_user(sigsetsize,
535 (size_t __user *)(sig+sizeof(void *))))
539 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
541 #endif /* TIF_RESTORE_SIGMASK */
544 struct poll_list *next;
546 struct pollfd entries[0];
549 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
551 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
552 poll_table ** pwait, int *count)
556 for (i = 0; i < num; i++) {
566 struct file * 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 &= fdp->events | POLLERR | POLLHUP;
573 fput_light(file, fput_needed);
584 static int do_poll(unsigned int nfds, struct poll_list *list,
585 struct poll_wqueues *wait, s64 *timeout)
588 poll_table* pt = &wait->pt;
590 /* Optimise the no-wait case */
595 struct poll_list *walk;
598 set_current_state(TASK_INTERRUPTIBLE);
600 while(walk != NULL) {
601 do_pollfd( walk->len, walk->entries, &pt, &count);
605 if (count || !*timeout || signal_pending(current))
612 /* Wait indefinitely */
613 __timeout = MAX_SCHEDULE_TIMEOUT;
614 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
616 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
619 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
620 *timeout -= __timeout;
622 __timeout = *timeout;
626 __timeout = schedule_timeout(__timeout);
628 *timeout += __timeout;
630 __set_current_state(TASK_RUNNING);
634 #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
635 sizeof(struct pollfd))
637 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
639 struct poll_wqueues table;
642 struct poll_list *head;
643 struct poll_list *walk;
646 /* Allocate small arguments on the stack to save memory and be
647 faster - use long to make sure the buffer is aligned properly
648 on 64 bit archs to avoid unaligned access */
649 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
650 struct poll_list *stack_pp = NULL;
652 /* Do a sanity check on nfds ... */
654 fdt = files_fdtable(current->files);
655 max_fdset = fdt->max_fdset;
657 if (nfds > max_fdset && nfds > OPEN_MAX)
660 poll_initwait(&table);
667 struct poll_list *pp;
669 if (stack_pp == NULL)
672 num = POLLFD_PER_PAGE;
675 size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
677 stack_pp = pp = (struct poll_list *)stack_pps;
679 pp = kmalloc(size, GFP_KERNEL);
691 if (copy_from_user(pp->entries, ufds + nfds-i,
692 sizeof(struct pollfd)*num)) {
699 fdcount = do_poll(nfds, head, &table, timeout);
701 /* OK, now copy the revents fields back to user space. */
704 while(walk != NULL) {
705 struct pollfd *fds = walk->entries;
708 for (j=0; j < walk->len; j++, ufds++) {
709 if(__put_user(fds[j].revents, &ufds->revents))
715 if (!fdcount && signal_pending(current))
720 struct poll_list *pp = walk->next;
721 if (walk != stack_pp)
725 poll_freewait(&table);
729 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
732 s64 timeout_jiffies = 0;
736 /* We can only overflow if HZ > 1000 */
737 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
738 timeout_jiffies = -1;
741 timeout_jiffies = msecs_to_jiffies(timeout_msecs);
744 return do_sys_poll(ufds, nfds, &timeout_jiffies);
747 #ifdef TIF_RESTORE_SIGMASK
748 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
749 struct timespec __user *tsp, const sigset_t __user *sigmask,
752 sigset_t ksigmask, sigsaved;
758 if (copy_from_user(&ts, tsp, sizeof(ts)))
761 /* Cast to u64 to make GCC stop complaining */
762 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
763 timeout = -1; /* infinite */
765 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
766 timeout += ts.tv_sec * HZ;
771 /* XXX: Don't preclude handling different sized sigset_t's. */
772 if (sigsetsize != sizeof(sigset_t))
774 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
777 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
778 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
781 ret = do_sys_poll(ufds, nfds, &timeout);
783 /* We can restart this syscall, usually */
786 * Don't restore the signal mask yet. Let do_signal() deliver
787 * the signal on the way back to userspace, before the signal
791 memcpy(¤t->saved_sigmask, &sigsaved,
793 set_thread_flag(TIF_RESTORE_SIGMASK);
795 ret = -ERESTARTNOHAND;
797 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
799 if (tsp && timeout >= 0) {
802 if (current->personality & STICKY_TIMEOUTS)
804 /* Yes, we know it's actually an s64, but it's also positive. */
805 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
807 rts.tv_sec = timeout;
808 if (timespec_compare(&rts, &ts) >= 0)
810 if (copy_to_user(tsp, &rts, sizeof(rts))) {
813 * If an application puts its timeval in read-only
814 * memory, we don't want the Linux-specific update to
815 * the timeval to cause a fault after the select has
816 * completed successfully. However, because we're not
817 * updating the timeval, we can't restart the system
820 if (ret == -ERESTARTNOHAND && timeout >= 0)
827 #endif /* TIF_RESTORE_SIGMASK */