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>
27 #include <linux/hrtimer.h>
29 #include <asm/uaccess.h>
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);
135 * poll_select_set_timeout - helper function to setup the timeout value
136 * @to: pointer to timespec variable for the final timeout
137 * @sec: seconds (from user space)
138 * @nsec: nanoseconds (from user space)
140 * Note, we do not use a timespec for the user space value here, That
141 * way we can use the function for timeval and compat interfaces as well.
143 * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0.
145 int poll_select_set_timeout(struct timespec *to, long sec, long nsec)
147 struct timespec ts = {.tv_sec = sec, .tv_nsec = nsec};
149 if (!timespec_valid(&ts))
152 /* Optimize for the zero timeout value here */
154 to->tv_sec = to->tv_nsec = 0;
157 *to = timespec_add_safe(*to, ts);
162 static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
163 int timeval, int ret)
171 if (current->personality & STICKY_TIMEOUTS)
174 /* No update for zero timeout */
175 if (!end_time->tv_sec && !end_time->tv_nsec)
179 rts = timespec_sub(*end_time, rts);
181 rts.tv_sec = rts.tv_nsec = 0;
184 rtv.tv_sec = rts.tv_sec;
185 rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
187 if (!copy_to_user(p, &rtv, sizeof(rtv)))
190 } else if (!copy_to_user(p, &rts, sizeof(rts)))
194 * If an application puts its timeval in read-only memory, we
195 * don't want the Linux-specific update to the timeval to
196 * cause a fault after the select has completed
197 * successfully. However, because we're not updating the
198 * timeval, we can't restart the system call.
202 if (ret == -ERESTARTNOHAND)
207 #define FDS_IN(fds, n) (fds->in + n)
208 #define FDS_OUT(fds, n) (fds->out + n)
209 #define FDS_EX(fds, n) (fds->ex + n)
211 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
213 static int max_select_fd(unsigned long n, fd_set_bits *fds)
215 unsigned long *open_fds;
220 /* handle last in-complete long-word first */
221 set = ~(~0UL << (n & (__NFDBITS-1)));
223 fdt = files_fdtable(current->files);
224 open_fds = fdt->open_fds->fds_bits+n;
229 if (!(set & ~*open_fds))
240 if (set & ~*open_fds)
249 max += n * __NFDBITS;
255 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
256 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
257 #define POLLEX_SET (POLLPRI)
259 int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
261 ktime_t expire, *to = NULL;
262 struct poll_wqueues table;
264 int retval, i, timed_out = 0;
267 retval = max_select_fd(n, fds);
274 poll_initwait(&table);
276 if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
283 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
285 set_current_state(TASK_INTERRUPTIBLE);
287 inp = fds->in; outp = fds->out; exp = fds->ex;
288 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
290 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
291 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
292 unsigned long res_in = 0, res_out = 0, res_ex = 0;
293 const struct file_operations *f_op = NULL;
294 struct file *file = NULL;
296 in = *inp++; out = *outp++; ex = *exp++;
297 all_bits = in | out | ex;
303 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
307 if (!(bit & all_bits))
309 file = fget_light(i, &fput_needed);
312 mask = DEFAULT_POLLMASK;
313 if (f_op && f_op->poll)
314 mask = (*f_op->poll)(file, retval ? NULL : wait);
315 fput_light(file, fput_needed);
316 if ((mask & POLLIN_SET) && (in & bit)) {
320 if ((mask & POLLOUT_SET) && (out & bit)) {
324 if ((mask & POLLEX_SET) && (ex & bit)) {
339 if (retval || timed_out || signal_pending(current))
342 retval = table.error;
347 * If this is the first loop and we have a timeout
348 * given, then we convert to ktime_t and set the to
349 * pointer to the expiry value.
351 if (end_time && !to) {
352 expire = timespec_to_ktime(*end_time);
356 if (!schedule_hrtimeout(to, HRTIMER_MODE_ABS))
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, struct timespec *end_time)
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, end_time);
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)
455 struct timespec end_time, *to = NULL;
460 if (copy_from_user(&tv, tvp, sizeof(tv)))
464 if (poll_select_set_timeout(to, tv.tv_sec,
465 tv.tv_usec * NSEC_PER_USEC))
469 ret = core_sys_select(n, inp, outp, exp, to);
470 ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
475 #ifdef HAVE_SET_RESTORE_SIGMASK
476 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
477 fd_set __user *exp, struct timespec __user *tsp,
478 const sigset_t __user *sigmask, size_t sigsetsize)
480 sigset_t ksigmask, sigsaved;
481 struct timespec ts, end_time, *to = NULL;
485 if (copy_from_user(&ts, tsp, sizeof(ts)))
489 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
494 /* XXX: Don't preclude handling different sized sigset_t's. */
495 if (sigsetsize != sizeof(sigset_t))
497 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
500 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
501 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
504 ret = core_sys_select(n, inp, outp, exp, &end_time);
505 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
507 if (ret == -ERESTARTNOHAND) {
509 * Don't restore the signal mask yet. Let do_signal() deliver
510 * the signal on the way back to userspace, before the signal
514 memcpy(¤t->saved_sigmask, &sigsaved,
516 set_restore_sigmask();
519 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
525 * Most architectures can't handle 7-argument syscalls. So we provide a
526 * 6-argument version where the sixth argument is a pointer to a structure
527 * which has a pointer to the sigset_t itself followed by a size_t containing
530 asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
531 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
533 size_t sigsetsize = 0;
534 sigset_t __user *up = NULL;
537 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
538 || __get_user(up, (sigset_t __user * __user *)sig)
539 || __get_user(sigsetsize,
540 (size_t __user *)(sig+sizeof(void *))))
544 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
546 #endif /* HAVE_SET_RESTORE_SIGMASK */
549 struct poll_list *next;
551 struct pollfd entries[0];
554 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
557 * Fish for pollable events on the pollfd->fd file descriptor. We're only
558 * interested in events matching the pollfd->events mask, and the result
559 * matching that mask is both recorded in pollfd->revents and returned. The
560 * pwait poll_table will be used by the fd-provided poll handler for waiting,
563 static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
574 file = fget_light(fd, &fput_needed);
577 mask = DEFAULT_POLLMASK;
578 if (file->f_op && file->f_op->poll)
579 mask = file->f_op->poll(file, pwait);
580 /* Mask out unneeded events. */
581 mask &= pollfd->events | POLLERR | POLLHUP;
582 fput_light(file, fput_needed);
585 pollfd->revents = mask;
590 static int do_poll(unsigned int nfds, struct poll_list *list,
591 struct poll_wqueues *wait, struct timespec *end_time)
593 poll_table* pt = &wait->pt;
594 ktime_t expire, *to = NULL;
595 int timed_out = 0, count = 0;
597 /* Optimise the no-wait case */
598 if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
604 struct poll_list *walk;
606 set_current_state(TASK_INTERRUPTIBLE);
607 for (walk = list; walk != NULL; walk = walk->next) {
608 struct pollfd * pfd, * pfd_end;
611 pfd_end = pfd + walk->len;
612 for (; pfd != pfd_end; pfd++) {
614 * Fish for events. If we found one, record it
615 * and kill the poll_table, so we don't
616 * needlessly register any other waiters after
617 * this. They'll get immediately deregistered
618 * when we break out and return.
620 if (do_pollfd(pfd, pt)) {
627 * All waiters have already been registered, so don't provide
628 * a poll_table to them on the next loop iteration.
633 if (signal_pending(current))
636 if (count || timed_out)
640 * If this is the first loop and we have a timeout
641 * given, then we convert to ktime_t and set the to
642 * pointer to the expiry value.
644 if (end_time && !to) {
645 expire = timespec_to_ktime(*end_time);
649 if (!schedule_hrtimeout(to, HRTIMER_MODE_ABS))
652 __set_current_state(TASK_RUNNING);
656 #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
657 sizeof(struct pollfd))
659 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
660 struct timespec *end_time)
662 struct poll_wqueues table;
663 int err = -EFAULT, fdcount, len, size;
664 /* Allocate small arguments on the stack to save memory and be
665 faster - use long to make sure the buffer is aligned properly
666 on 64 bit archs to avoid unaligned access */
667 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
668 struct poll_list *const head = (struct poll_list *)stack_pps;
669 struct poll_list *walk = head;
670 unsigned long todo = nfds;
672 if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
675 len = min_t(unsigned int, nfds, N_STACK_PPS);
682 if (copy_from_user(walk->entries, ufds + nfds-todo,
683 sizeof(struct pollfd) * walk->len))
690 len = min(todo, POLLFD_PER_PAGE);
691 size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
692 walk = walk->next = kmalloc(size, GFP_KERNEL);
699 poll_initwait(&table);
700 fdcount = do_poll(nfds, head, &table, end_time);
701 poll_freewait(&table);
703 for (walk = head; walk; walk = walk->next) {
704 struct pollfd *fds = walk->entries;
707 for (j = 0; j < walk->len; j++, ufds++)
708 if (__put_user(fds[j].revents, &ufds->revents))
716 struct poll_list *pos = walk;
724 static long do_restart_poll(struct restart_block *restart_block)
726 struct pollfd __user *ufds = restart_block->poll.ufds;
727 int nfds = restart_block->poll.nfds;
728 struct timespec *to = NULL, end_time;
731 if (restart_block->poll.has_timeout) {
732 end_time.tv_sec = restart_block->poll.tv_sec;
733 end_time.tv_nsec = restart_block->poll.tv_nsec;
737 ret = do_sys_poll(ufds, nfds, to);
740 restart_block->fn = do_restart_poll;
741 ret = -ERESTART_RESTARTBLOCK;
746 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
749 struct timespec end_time, *to = NULL;
752 if (timeout_msecs >= 0) {
754 poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
755 NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
758 ret = do_sys_poll(ufds, nfds, to);
761 struct restart_block *restart_block;
763 restart_block = ¤t_thread_info()->restart_block;
764 restart_block->fn = do_restart_poll;
765 restart_block->poll.ufds = ufds;
766 restart_block->poll.nfds = nfds;
768 if (timeout_msecs >= 0) {
769 restart_block->poll.tv_sec = end_time.tv_sec;
770 restart_block->poll.tv_nsec = end_time.tv_nsec;
771 restart_block->poll.has_timeout = 1;
773 restart_block->poll.has_timeout = 0;
775 ret = -ERESTART_RESTARTBLOCK;
780 #ifdef HAVE_SET_RESTORE_SIGMASK
781 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
782 struct timespec __user *tsp, const sigset_t __user *sigmask,
785 sigset_t ksigmask, sigsaved;
786 struct timespec ts, end_time, *to = NULL;
790 if (copy_from_user(&ts, tsp, sizeof(ts)))
794 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
799 /* XXX: Don't preclude handling different sized sigset_t's. */
800 if (sigsetsize != sizeof(sigset_t))
802 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
805 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
806 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
809 ret = do_sys_poll(ufds, nfds, to);
811 /* We can restart this syscall, usually */
814 * Don't restore the signal mask yet. Let do_signal() deliver
815 * the signal on the way back to userspace, before the signal
819 memcpy(¤t->saved_sigmask, &sigsaved,
821 set_restore_sigmask();
823 ret = -ERESTARTNOHAND;
825 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
827 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
831 #endif /* HAVE_SET_RESTORE_SIGMASK */