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>
27 #include <asm/uaccess.h>
29 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
30 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
32 struct poll_table_entry {
35 wait_queue_head_t * wait_address;
38 struct poll_table_page {
39 struct poll_table_page * next;
40 struct poll_table_entry * entry;
41 struct poll_table_entry entries[0];
44 #define POLL_TABLE_FULL(table) \
45 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
48 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
49 * I have rewritten this, taking some shortcuts: This code may not be easy to
50 * follow, but it should be free of race-conditions, and it's practical. If you
51 * understand what I'm doing here, then you understand how the linux
52 * sleep/wakeup mechanism works.
54 * Two very simple procedures, poll_wait() and poll_freewait() make all the
55 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
56 * as all select/poll functions have to call it to add an entry to the
59 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
62 void poll_initwait(struct poll_wqueues *pwq)
64 init_poll_funcptr(&pwq->pt, __pollwait);
69 EXPORT_SYMBOL(poll_initwait);
71 void poll_freewait(struct poll_wqueues *pwq)
73 struct poll_table_page * p = pwq->table;
75 struct poll_table_entry * entry;
76 struct poll_table_page *old;
81 remove_wait_queue(entry->wait_address,&entry->wait);
83 } while (entry > p->entries);
86 free_page((unsigned long) old);
90 EXPORT_SYMBOL(poll_freewait);
92 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
95 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
96 struct poll_table_page *table = p->table;
98 if (!table || POLL_TABLE_FULL(table)) {
99 struct poll_table_page *new_table;
101 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
104 __set_current_state(TASK_RUNNING);
107 new_table->entry = new_table->entries;
108 new_table->next = table;
109 p->table = new_table;
113 /* Add a new entry */
115 struct poll_table_entry * entry = table->entry;
116 table->entry = entry+1;
119 entry->wait_address = wait_address;
120 init_waitqueue_entry(&entry->wait, current);
121 add_wait_queue(wait_address,&entry->wait);
125 #define FDS_IN(fds, n) (fds->in + n)
126 #define FDS_OUT(fds, n) (fds->out + n)
127 #define FDS_EX(fds, n) (fds->ex + n)
129 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
131 static int max_select_fd(unsigned long n, fd_set_bits *fds)
133 unsigned long *open_fds;
138 /* handle last in-complete long-word first */
139 set = ~(~0UL << (n & (__NFDBITS-1)));
141 fdt = files_fdtable(current->files);
142 open_fds = fdt->open_fds->fds_bits+n;
147 if (!(set & ~*open_fds))
158 if (set & ~*open_fds)
167 max += n * __NFDBITS;
173 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
174 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
175 #define ISSET(i,m) (((i)&*(m)) != 0)
176 #define SET(i,m) (*(m) |= (i))
178 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
179 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
180 #define POLLEX_SET (POLLPRI)
182 int do_select(int n, fd_set_bits *fds, s64 *timeout)
184 struct poll_wqueues table;
189 retval = max_select_fd(n, fds);
196 poll_initwait(&table);
202 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
205 set_current_state(TASK_INTERRUPTIBLE);
207 inp = fds->in; outp = fds->out; exp = fds->ex;
208 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
210 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
211 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
212 unsigned long res_in = 0, res_out = 0, res_ex = 0;
213 struct file_operations *f_op = NULL;
214 struct file *file = NULL;
216 in = *inp++; out = *outp++; ex = *exp++;
217 all_bits = in | out | ex;
223 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
226 if (!(bit & all_bits))
231 mask = DEFAULT_POLLMASK;
232 if (f_op && f_op->poll)
233 mask = (*f_op->poll)(file, retval ? NULL : wait);
235 if ((mask & POLLIN_SET) && (in & bit)) {
239 if ((mask & POLLOUT_SET) && (out & bit)) {
243 if ((mask & POLLEX_SET) && (ex & bit)) {
258 if (retval || !*timeout || signal_pending(current))
261 retval = table.error;
266 /* Wait indefinitely */
267 __timeout = MAX_SCHEDULE_TIMEOUT;
268 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
269 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
270 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
271 *timeout -= __timeout;
273 __timeout = *timeout;
276 __timeout = schedule_timeout(__timeout);
278 *timeout += __timeout;
280 __set_current_state(TASK_RUNNING);
282 poll_freewait(&table);
287 static void *select_bits_alloc(int size)
289 return kmalloc(6 * size, GFP_KERNEL);
292 static void select_bits_free(void *bits, int size)
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)
313 int ret, size, max_fdset;
320 /* max_fdset can increase, so grab it once to avoid race */
322 fdt = files_fdtable(current->files);
323 max_fdset = fdt->max_fdset;
329 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
330 * since we used fdset we need to allocate memory in units of
335 bits = select_bits_alloc(size);
338 fds.in = (unsigned long *) bits;
339 fds.out = (unsigned long *) (bits + size);
340 fds.ex = (unsigned long *) (bits + 2*size);
341 fds.res_in = (unsigned long *) (bits + 3*size);
342 fds.res_out = (unsigned long *) (bits + 4*size);
343 fds.res_ex = (unsigned long *) (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 select_bits_free(bits, size);
375 asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
376 fd_set __user *exp, struct timeval __user *tvp)
383 if (copy_from_user(&tv, tvp, sizeof(tv)))
386 if (tv.tv_sec < 0 || tv.tv_usec < 0)
389 /* Cast to u64 to make GCC stop complaining */
390 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
391 timeout = -1; /* infinite */
393 timeout = ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
394 timeout += tv.tv_sec * HZ;
398 ret = core_sys_select(n, inp, outp, exp, &timeout);
403 if (current->personality & STICKY_TIMEOUTS)
405 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
406 rtv.tv_sec = timeout;
407 if (timeval_compare(&rtv, &tv) < 0)
409 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
412 * If an application puts its timeval in read-only
413 * memory, we don't want the Linux-specific update to
414 * the timeval to cause a fault after the select has
415 * completed successfully. However, because we're not
416 * updating the timeval, we can't restart the system
419 if (ret == -ERESTARTNOHAND)
427 #ifdef TIF_RESTORE_SIGMASK
428 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
429 fd_set __user *exp, struct timespec __user *tsp,
430 const sigset_t __user *sigmask, size_t sigsetsize)
432 s64 timeout = MAX_SCHEDULE_TIMEOUT;
433 sigset_t ksigmask, sigsaved;
438 if (copy_from_user(&ts, tsp, sizeof(ts)))
441 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
444 /* Cast to u64 to make GCC stop complaining */
445 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
446 timeout = -1; /* infinite */
448 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
449 timeout += ts.tv_sec * HZ;
454 /* XXX: Don't preclude handling different sized sigset_t's. */
455 if (sigsetsize != sizeof(sigset_t))
457 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
460 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
461 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
464 ret = core_sys_select(n, inp, outp, exp, &timeout);
469 if (current->personality & STICKY_TIMEOUTS)
471 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
473 rts.tv_sec = timeout;
474 if (timespec_compare(&rts, &ts) < 0)
476 if (copy_to_user(tsp, &rts, sizeof(rts))) {
479 * If an application puts its timeval in read-only
480 * memory, we don't want the Linux-specific update to
481 * the timeval to cause a fault after the select has
482 * completed successfully. However, because we're not
483 * updating the timeval, we can't restart the system
486 if (ret == -ERESTARTNOHAND)
491 if (ret == -ERESTARTNOHAND) {
493 * Don't restore the signal mask yet. Let do_signal() deliver
494 * the signal on the way back to userspace, before the signal
498 memcpy(¤t->saved_sigmask, &sigsaved,
500 set_thread_flag(TIF_RESTORE_SIGMASK);
503 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
509 * Most architectures can't handle 7-argument syscalls. So we provide a
510 * 6-argument version where the sixth argument is a pointer to a structure
511 * which has a pointer to the sigset_t itself followed by a size_t containing
514 asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
515 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
517 size_t sigsetsize = 0;
518 sigset_t __user *up = NULL;
521 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
522 || __get_user(up, (sigset_t __user * __user *)sig)
523 || __get_user(sigsetsize,
524 (size_t __user *)(sig+sizeof(void *))))
528 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
530 #endif /* TIF_RESTORE_SIGMASK */
533 struct poll_list *next;
535 struct pollfd entries[0];
538 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
540 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
541 poll_table ** pwait, int *count)
545 for (i = 0; i < num; i++) {
554 struct file * file = fget(fd);
557 mask = DEFAULT_POLLMASK;
558 if (file->f_op && file->f_op->poll)
559 mask = file->f_op->poll(file, *pwait);
560 mask &= fdp->events | POLLERR | POLLHUP;
572 static int do_poll(unsigned int nfds, struct poll_list *list,
573 struct poll_wqueues *wait, s64 *timeout)
576 poll_table* pt = &wait->pt;
578 /* Optimise the no-wait case */
583 struct poll_list *walk;
586 set_current_state(TASK_INTERRUPTIBLE);
588 while(walk != NULL) {
589 do_pollfd( walk->len, walk->entries, &pt, &count);
593 if (count || !*timeout || signal_pending(current))
600 /* Wait indefinitely */
601 __timeout = MAX_SCHEDULE_TIMEOUT;
602 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
604 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
607 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
608 *timeout -= __timeout;
610 __timeout = *timeout;
614 __timeout = schedule_timeout(__timeout);
616 *timeout += __timeout;
618 __set_current_state(TASK_RUNNING);
622 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
624 struct poll_wqueues table;
627 struct poll_list *head;
628 struct poll_list *walk;
632 /* Do a sanity check on nfds ... */
634 fdt = files_fdtable(current->files);
635 max_fdset = fdt->max_fdset;
637 if (nfds > max_fdset && nfds > OPEN_MAX)
640 poll_initwait(&table);
647 struct poll_list *pp;
648 pp = kmalloc(sizeof(struct poll_list)+
649 sizeof(struct pollfd)*
650 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
655 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
662 if (copy_from_user(pp->entries, ufds + nfds-i,
663 sizeof(struct pollfd)*pp->len)) {
670 fdcount = do_poll(nfds, head, &table, timeout);
672 /* OK, now copy the revents fields back to user space. */
675 while(walk != NULL) {
676 struct pollfd *fds = walk->entries;
679 for (j=0; j < walk->len; j++, ufds++) {
680 if(__put_user(fds[j].revents, &ufds->revents))
686 if (!fdcount && signal_pending(current))
691 struct poll_list *pp = walk->next;
695 poll_freewait(&table);
699 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
702 s64 timeout_jiffies = 0;
706 /* We can only overflow if HZ > 1000 */
707 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
708 timeout_jiffies = -1;
711 timeout_jiffies = msecs_to_jiffies(timeout_msecs);
714 return do_sys_poll(ufds, nfds, &timeout_jiffies);
717 #ifdef TIF_RESTORE_SIGMASK
718 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
719 struct timespec __user *tsp, const sigset_t __user *sigmask,
722 sigset_t ksigmask, sigsaved;
728 if (copy_from_user(&ts, tsp, sizeof(ts)))
731 /* Cast to u64 to make GCC stop complaining */
732 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
733 timeout = -1; /* infinite */
735 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
736 timeout += ts.tv_sec * HZ;
741 /* XXX: Don't preclude handling different sized sigset_t's. */
742 if (sigsetsize != sizeof(sigset_t))
744 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
747 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
748 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
751 ret = do_sys_poll(ufds, nfds, &timeout);
753 /* We can restart this syscall, usually */
756 * Don't restore the signal mask yet. Let do_signal() deliver
757 * the signal on the way back to userspace, before the signal
761 memcpy(¤t->saved_sigmask, &sigsaved,
763 set_thread_flag(TIF_RESTORE_SIGMASK);
765 ret = -ERESTARTNOHAND;
767 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
769 if (tsp && timeout >= 0) {
772 if (current->personality & STICKY_TIMEOUTS)
774 /* Yes, we know it's actually an s64, but it's also positive. */
775 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
777 rts.tv_sec = timeout;
778 if (timespec_compare(&rts, &ts) < 0)
780 if (copy_to_user(tsp, &rts, sizeof(rts))) {
783 * If an application puts its timeval in read-only
784 * memory, we don't want the Linux-specific update to
785 * the timeval to cause a fault after the select has
786 * completed successfully. However, because we're not
787 * updating the timeval, we can't restart the system
790 if (ret == -ERESTARTNOHAND && timeout >= 0)
797 #endif /* TIF_RESTORE_SIGMASK */