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
);
401 if (current
->personality
& STICKY_TIMEOUTS
)
403 tv
.tv_usec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
));
405 if (copy_to_user(tvp
, &tv
, sizeof(tv
))) {
408 * If an application puts its timeval in read-only
409 * memory, we don't want the Linux-specific update to
410 * the timeval to cause a fault after the select has
411 * completed successfully. However, because we're not
412 * updating the timeval, we can't restart the system
415 if (ret
== -ERESTARTNOHAND
)
423 #ifdef TIF_RESTORE_SIGMASK
424 asmlinkage
long sys_pselect7(int n
, fd_set __user
*inp
, fd_set __user
*outp
,
425 fd_set __user
*exp
, struct timespec __user
*tsp
,
426 const sigset_t __user
*sigmask
, size_t sigsetsize
)
428 s64 timeout
= MAX_SCHEDULE_TIMEOUT
;
429 sigset_t ksigmask
, sigsaved
;
434 if (copy_from_user(&ts
, tsp
, sizeof(ts
)))
437 if (ts
.tv_sec
< 0 || ts
.tv_nsec
< 0)
440 /* Cast to u64 to make GCC stop complaining */
441 if ((u64
)ts
.tv_sec
>= (u64
)MAX_INT64_SECONDS
)
442 timeout
= -1; /* infinite */
444 timeout
= ROUND_UP(ts
.tv_nsec
, NSEC_PER_SEC
/HZ
);
445 timeout
+= ts
.tv_sec
* HZ
;
450 /* XXX: Don't preclude handling different sized sigset_t's. */
451 if (sigsetsize
!= sizeof(sigset_t
))
453 if (copy_from_user(&ksigmask
, sigmask
, sizeof(ksigmask
)))
456 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
457 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
460 ret
= core_sys_select(n
, inp
, outp
, exp
, &timeout
);
463 if (current
->personality
& STICKY_TIMEOUTS
)
465 ts
.tv_nsec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
)) * 1000;
467 if (copy_to_user(tsp
, &ts
, sizeof(ts
))) {
470 * If an application puts its timeval in read-only
471 * memory, we don't want the Linux-specific update to
472 * the timeval to cause a fault after the select has
473 * completed successfully. However, because we're not
474 * updating the timeval, we can't restart the system
477 if (ret
== -ERESTARTNOHAND
)
482 if (ret
== -ERESTARTNOHAND
) {
484 * Don't restore the signal mask yet. Let do_signal() deliver
485 * the signal on the way back to userspace, before the signal
489 memcpy(¤t
->saved_sigmask
, &sigsaved
,
491 set_thread_flag(TIF_RESTORE_SIGMASK
);
494 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
500 * Most architectures can't handle 7-argument syscalls. So we provide a
501 * 6-argument version where the sixth argument is a pointer to a structure
502 * which has a pointer to the sigset_t itself followed by a size_t containing
505 asmlinkage
long sys_pselect6(int n
, fd_set __user
*inp
, fd_set __user
*outp
,
506 fd_set __user
*exp
, struct timespec __user
*tsp
, void __user
*sig
)
508 size_t sigsetsize
= 0;
509 sigset_t __user
*up
= NULL
;
512 if (!access_ok(VERIFY_READ
, sig
, sizeof(void *)+sizeof(size_t))
513 || __get_user(up
, (sigset_t __user
* __user
*)sig
)
514 || __get_user(sigsetsize
,
515 (size_t __user
*)(sig
+sizeof(void *))))
519 return sys_pselect7(n
, inp
, outp
, exp
, tsp
, up
, sigsetsize
);
521 #endif /* TIF_RESTORE_SIGMASK */
524 struct poll_list
*next
;
526 struct pollfd entries
[0];
529 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
531 static void do_pollfd(unsigned int num
, struct pollfd
* fdpage
,
532 poll_table
** pwait
, int *count
)
536 for (i
= 0; i
< num
; i
++) {
545 struct file
* file
= fget(fd
);
548 mask
= DEFAULT_POLLMASK
;
549 if (file
->f_op
&& file
->f_op
->poll
)
550 mask
= file
->f_op
->poll(file
, *pwait
);
551 mask
&= fdp
->events
| POLLERR
| POLLHUP
;
563 static int do_poll(unsigned int nfds
, struct poll_list
*list
,
564 struct poll_wqueues
*wait
, s64
*timeout
)
567 poll_table
* pt
= &wait
->pt
;
569 /* Optimise the no-wait case */
574 struct poll_list
*walk
;
577 set_current_state(TASK_INTERRUPTIBLE
);
579 while(walk
!= NULL
) {
580 do_pollfd( walk
->len
, walk
->entries
, &pt
, &count
);
584 if (count
|| !*timeout
|| signal_pending(current
))
591 /* Wait indefinitely */
592 __timeout
= MAX_SCHEDULE_TIMEOUT
;
593 } else if (unlikely(*timeout
>= (s64
)MAX_SCHEDULE_TIMEOUT
-1)) {
595 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
598 __timeout
= MAX_SCHEDULE_TIMEOUT
- 1;
599 *timeout
-= __timeout
;
601 __timeout
= *timeout
;
605 __timeout
= schedule_timeout(__timeout
);
607 *timeout
+= __timeout
;
609 __set_current_state(TASK_RUNNING
);
613 int do_sys_poll(struct pollfd __user
*ufds
, unsigned int nfds
, s64
*timeout
)
615 struct poll_wqueues table
;
618 struct poll_list
*head
;
619 struct poll_list
*walk
;
623 /* Do a sanity check on nfds ... */
625 fdt
= files_fdtable(current
->files
);
626 max_fdset
= fdt
->max_fdset
;
628 if (nfds
> max_fdset
&& nfds
> OPEN_MAX
)
631 poll_initwait(&table
);
638 struct poll_list
*pp
;
639 pp
= kmalloc(sizeof(struct poll_list
)+
640 sizeof(struct pollfd
)*
641 (i
>POLLFD_PER_PAGE
?POLLFD_PER_PAGE
:i
),
646 pp
->len
= (i
>POLLFD_PER_PAGE
?POLLFD_PER_PAGE
:i
);
653 if (copy_from_user(pp
->entries
, ufds
+ nfds
-i
,
654 sizeof(struct pollfd
)*pp
->len
)) {
661 fdcount
= do_poll(nfds
, head
, &table
, timeout
);
663 /* OK, now copy the revents fields back to user space. */
666 while(walk
!= NULL
) {
667 struct pollfd
*fds
= walk
->entries
;
670 for (j
=0; j
< walk
->len
; j
++, ufds
++) {
671 if(__put_user(fds
[j
].revents
, &ufds
->revents
))
677 if (!fdcount
&& signal_pending(current
))
682 struct poll_list
*pp
= walk
->next
;
686 poll_freewait(&table
);
690 asmlinkage
long sys_poll(struct pollfd __user
*ufds
, unsigned int nfds
,
693 s64 timeout_jiffies
= 0;
697 /* We can only overflow if HZ > 1000 */
698 if (timeout_msecs
/ 1000 > (s64
)0x7fffffffffffffffULL
/ (s64
)HZ
)
699 timeout_jiffies
= -1;
702 timeout_jiffies
= msecs_to_jiffies(timeout_msecs
);
705 return do_sys_poll(ufds
, nfds
, &timeout_jiffies
);
708 #ifdef TIF_RESTORE_SIGMASK
709 asmlinkage
long sys_ppoll(struct pollfd __user
*ufds
, unsigned int nfds
,
710 struct timespec __user
*tsp
, const sigset_t __user
*sigmask
,
713 sigset_t ksigmask
, sigsaved
;
719 if (copy_from_user(&ts
, tsp
, sizeof(ts
)))
722 /* Cast to u64 to make GCC stop complaining */
723 if ((u64
)ts
.tv_sec
>= (u64
)MAX_INT64_SECONDS
)
724 timeout
= -1; /* infinite */
726 timeout
= ROUND_UP(ts
.tv_nsec
, NSEC_PER_SEC
/HZ
);
727 timeout
+= ts
.tv_sec
* HZ
;
732 /* XXX: Don't preclude handling different sized sigset_t's. */
733 if (sigsetsize
!= sizeof(sigset_t
))
735 if (copy_from_user(&ksigmask
, sigmask
, sizeof(ksigmask
)))
738 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
739 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
742 ret
= do_sys_poll(ufds
, nfds
, &timeout
);
744 /* We can restart this syscall, usually */
747 * Don't restore the signal mask yet. Let do_signal() deliver
748 * the signal on the way back to userspace, before the signal
752 memcpy(¤t
->saved_sigmask
, &sigsaved
,
754 set_thread_flag(TIF_RESTORE_SIGMASK
);
756 ret
= -ERESTARTNOHAND
;
758 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
760 if (tsp
&& timeout
>= 0) {
761 if (current
->personality
& STICKY_TIMEOUTS
)
763 /* Yes, we know it's actually an s64, but it's also positive. */
764 ts
.tv_nsec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
)) * 1000;
766 if (copy_to_user(tsp
, &ts
, sizeof(ts
))) {
769 * If an application puts its timeval in read-only
770 * memory, we don't want the Linux-specific update to
771 * the timeval to cause a fault after the select has
772 * completed successfully. However, because we're not
773 * updating the timeval, we can't restart the system
776 if (ret
== -ERESTARTNOHAND
&& timeout
>= 0)
783 #endif /* TIF_RESTORE_SIGMASK */