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_page
{
33 struct poll_table_page
* next
;
34 struct poll_table_entry
* entry
;
35 struct poll_table_entry entries
[0];
38 #define POLL_TABLE_FULL(table) \
39 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
42 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
43 * I have rewritten this, taking some shortcuts: This code may not be easy to
44 * follow, but it should be free of race-conditions, and it's practical. If you
45 * understand what I'm doing here, then you understand how the linux
46 * sleep/wakeup mechanism works.
48 * Two very simple procedures, poll_wait() and poll_freewait() make all the
49 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
50 * as all select/poll functions have to call it to add an entry to the
53 static void __pollwait(struct file
*filp
, wait_queue_head_t
*wait_address
,
56 void poll_initwait(struct poll_wqueues
*pwq
)
58 init_poll_funcptr(&pwq
->pt
, __pollwait
);
61 pwq
->inline_index
= 0;
64 EXPORT_SYMBOL(poll_initwait
);
66 static void free_poll_entry(struct poll_table_entry
*entry
)
68 remove_wait_queue(entry
->wait_address
,&entry
->wait
);
72 void poll_freewait(struct poll_wqueues
*pwq
)
74 struct poll_table_page
* p
= pwq
->table
;
76 for (i
= 0; i
< pwq
->inline_index
; i
++)
77 free_poll_entry(pwq
->inline_entries
+ i
);
79 struct poll_table_entry
* entry
;
80 struct poll_table_page
*old
;
85 free_poll_entry(entry
);
86 } while (entry
> p
->entries
);
89 free_page((unsigned long) old
);
93 EXPORT_SYMBOL(poll_freewait
);
95 static struct poll_table_entry
*poll_get_entry(poll_table
*_p
)
97 struct poll_wqueues
*p
= container_of(_p
, struct poll_wqueues
, pt
);
98 struct poll_table_page
*table
= p
->table
;
100 if (p
->inline_index
< N_INLINE_POLL_ENTRIES
)
101 return p
->inline_entries
+ p
->inline_index
++;
103 if (!table
|| POLL_TABLE_FULL(table
)) {
104 struct poll_table_page
*new_table
;
106 new_table
= (struct poll_table_page
*) __get_free_page(GFP_KERNEL
);
109 __set_current_state(TASK_RUNNING
);
112 new_table
->entry
= new_table
->entries
;
113 new_table
->next
= table
;
114 p
->table
= new_table
;
118 return table
->entry
++;
121 /* Add a new entry */
122 static void __pollwait(struct file
*filp
, wait_queue_head_t
*wait_address
,
125 struct poll_table_entry
*entry
= poll_get_entry(p
);
130 entry
->wait_address
= wait_address
;
131 init_waitqueue_entry(&entry
->wait
, current
);
132 add_wait_queue(wait_address
,&entry
->wait
);
135 #define FDS_IN(fds, n) (fds->in + n)
136 #define FDS_OUT(fds, n) (fds->out + n)
137 #define FDS_EX(fds, n) (fds->ex + n)
139 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
141 static int max_select_fd(unsigned long n
, fd_set_bits
*fds
)
143 unsigned long *open_fds
;
148 /* handle last in-complete long-word first */
149 set
= ~(~0UL << (n
& (__NFDBITS
-1)));
151 fdt
= files_fdtable(current
->files
);
152 open_fds
= fdt
->open_fds
->fds_bits
+n
;
157 if (!(set
& ~*open_fds
))
168 if (set
& ~*open_fds
)
177 max
+= n
* __NFDBITS
;
183 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
184 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
185 #define ISSET(i,m) (((i)&*(m)) != 0)
186 #define SET(i,m) (*(m) |= (i))
188 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
189 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
190 #define POLLEX_SET (POLLPRI)
192 int do_select(int n
, fd_set_bits
*fds
, s64
*timeout
)
194 struct poll_wqueues table
;
199 retval
= max_select_fd(n
, fds
);
206 poll_initwait(&table
);
212 unsigned long *rinp
, *routp
, *rexp
, *inp
, *outp
, *exp
;
215 set_current_state(TASK_INTERRUPTIBLE
);
217 inp
= fds
->in
; outp
= fds
->out
; exp
= fds
->ex
;
218 rinp
= fds
->res_in
; routp
= fds
->res_out
; rexp
= fds
->res_ex
;
220 for (i
= 0; i
< n
; ++rinp
, ++routp
, ++rexp
) {
221 unsigned long in
, out
, ex
, all_bits
, bit
= 1, mask
, j
;
222 unsigned long res_in
= 0, res_out
= 0, res_ex
= 0;
223 struct file_operations
*f_op
= NULL
;
224 struct file
*file
= NULL
;
226 in
= *inp
++; out
= *outp
++; ex
= *exp
++;
227 all_bits
= in
| out
| ex
;
233 for (j
= 0; j
< __NFDBITS
; ++j
, ++i
, bit
<<= 1) {
236 if (!(bit
& all_bits
))
241 mask
= DEFAULT_POLLMASK
;
242 if (f_op
&& f_op
->poll
)
243 mask
= (*f_op
->poll
)(file
, retval
? NULL
: wait
);
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
)
313 int ret
, size
, max_fdset
;
315 /* Allocate small arguments on the stack to save memory and be faster */
316 char stack_fds
[SELECT_STACK_ALLOC
];
322 /* max_fdset can increase, so grab it once to avoid race */
324 fdt
= files_fdtable(current
->files
);
325 max_fdset
= fdt
->max_fdset
;
331 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
332 * since we used fdset we need to allocate memory in units of
337 if (6*size
< SELECT_STACK_ALLOC
)
340 bits
= kmalloc(6 * size
, GFP_KERNEL
);
343 fds
.in
= (unsigned long *) bits
;
344 fds
.out
= (unsigned long *) (bits
+ size
);
345 fds
.ex
= (unsigned long *) (bits
+ 2*size
);
346 fds
.res_in
= (unsigned long *) (bits
+ 3*size
);
347 fds
.res_out
= (unsigned long *) (bits
+ 4*size
);
348 fds
.res_ex
= (unsigned long *) (bits
+ 5*size
);
350 if ((ret
= get_fd_set(n
, inp
, fds
.in
)) ||
351 (ret
= get_fd_set(n
, outp
, fds
.out
)) ||
352 (ret
= get_fd_set(n
, exp
, fds
.ex
)))
354 zero_fd_set(n
, fds
.res_in
);
355 zero_fd_set(n
, fds
.res_out
);
356 zero_fd_set(n
, fds
.res_ex
);
358 ret
= do_select(n
, &fds
, timeout
);
363 ret
= -ERESTARTNOHAND
;
364 if (signal_pending(current
))
369 if (set_fd_set(n
, inp
, fds
.res_in
) ||
370 set_fd_set(n
, outp
, fds
.res_out
) ||
371 set_fd_set(n
, exp
, fds
.res_ex
))
375 if (bits
!= stack_fds
)
381 asmlinkage
long sys_select(int n
, fd_set __user
*inp
, fd_set __user
*outp
,
382 fd_set __user
*exp
, struct timeval __user
*tvp
)
389 if (copy_from_user(&tv
, tvp
, sizeof(tv
)))
392 if (tv
.tv_sec
< 0 || tv
.tv_usec
< 0)
395 /* Cast to u64 to make GCC stop complaining */
396 if ((u64
)tv
.tv_sec
>= (u64
)MAX_INT64_SECONDS
)
397 timeout
= -1; /* infinite */
399 timeout
= ROUND_UP(tv
.tv_usec
, USEC_PER_SEC
/HZ
);
400 timeout
+= tv
.tv_sec
* HZ
;
404 ret
= core_sys_select(n
, inp
, outp
, exp
, &timeout
);
409 if (current
->personality
& STICKY_TIMEOUTS
)
411 rtv
.tv_usec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
));
412 rtv
.tv_sec
= timeout
;
413 if (timeval_compare(&rtv
, &tv
) >= 0)
415 if (copy_to_user(tvp
, &rtv
, sizeof(rtv
))) {
418 * If an application puts its timeval in read-only
419 * memory, we don't want the Linux-specific update to
420 * the timeval to cause a fault after the select has
421 * completed successfully. However, because we're not
422 * updating the timeval, we can't restart the system
425 if (ret
== -ERESTARTNOHAND
)
433 #ifdef TIF_RESTORE_SIGMASK
434 asmlinkage
long sys_pselect7(int n
, fd_set __user
*inp
, fd_set __user
*outp
,
435 fd_set __user
*exp
, struct timespec __user
*tsp
,
436 const sigset_t __user
*sigmask
, size_t sigsetsize
)
438 s64 timeout
= MAX_SCHEDULE_TIMEOUT
;
439 sigset_t ksigmask
, sigsaved
;
444 if (copy_from_user(&ts
, tsp
, sizeof(ts
)))
447 if (ts
.tv_sec
< 0 || ts
.tv_nsec
< 0)
450 /* Cast to u64 to make GCC stop complaining */
451 if ((u64
)ts
.tv_sec
>= (u64
)MAX_INT64_SECONDS
)
452 timeout
= -1; /* infinite */
454 timeout
= ROUND_UP(ts
.tv_nsec
, NSEC_PER_SEC
/HZ
);
455 timeout
+= ts
.tv_sec
* HZ
;
460 /* XXX: Don't preclude handling different sized sigset_t's. */
461 if (sigsetsize
!= sizeof(sigset_t
))
463 if (copy_from_user(&ksigmask
, sigmask
, sizeof(ksigmask
)))
466 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
467 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
470 ret
= core_sys_select(n
, inp
, outp
, exp
, &timeout
);
475 if (current
->personality
& STICKY_TIMEOUTS
)
477 rts
.tv_nsec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
)) *
479 rts
.tv_sec
= timeout
;
480 if (timespec_compare(&rts
, &ts
) >= 0)
482 if (copy_to_user(tsp
, &rts
, sizeof(rts
))) {
485 * If an application puts its timeval in read-only
486 * memory, we don't want the Linux-specific update to
487 * the timeval to cause a fault after the select has
488 * completed successfully. However, because we're not
489 * updating the timeval, we can't restart the system
492 if (ret
== -ERESTARTNOHAND
)
497 if (ret
== -ERESTARTNOHAND
) {
499 * Don't restore the signal mask yet. Let do_signal() deliver
500 * the signal on the way back to userspace, before the signal
504 memcpy(¤t
->saved_sigmask
, &sigsaved
,
506 set_thread_flag(TIF_RESTORE_SIGMASK
);
509 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
515 * Most architectures can't handle 7-argument syscalls. So we provide a
516 * 6-argument version where the sixth argument is a pointer to a structure
517 * which has a pointer to the sigset_t itself followed by a size_t containing
520 asmlinkage
long sys_pselect6(int n
, fd_set __user
*inp
, fd_set __user
*outp
,
521 fd_set __user
*exp
, struct timespec __user
*tsp
, void __user
*sig
)
523 size_t sigsetsize
= 0;
524 sigset_t __user
*up
= NULL
;
527 if (!access_ok(VERIFY_READ
, sig
, sizeof(void *)+sizeof(size_t))
528 || __get_user(up
, (sigset_t __user
* __user
*)sig
)
529 || __get_user(sigsetsize
,
530 (size_t __user
*)(sig
+sizeof(void *))))
534 return sys_pselect7(n
, inp
, outp
, exp
, tsp
, up
, sigsetsize
);
536 #endif /* TIF_RESTORE_SIGMASK */
539 struct poll_list
*next
;
541 struct pollfd entries
[0];
544 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
546 static void do_pollfd(unsigned int num
, struct pollfd
* fdpage
,
547 poll_table
** pwait
, int *count
)
551 for (i
= 0; i
< num
; i
++) {
560 struct file
* file
= fget(fd
);
563 mask
= DEFAULT_POLLMASK
;
564 if (file
->f_op
&& file
->f_op
->poll
)
565 mask
= file
->f_op
->poll(file
, *pwait
);
566 mask
&= fdp
->events
| POLLERR
| POLLHUP
;
578 static int do_poll(unsigned int nfds
, struct poll_list
*list
,
579 struct poll_wqueues
*wait
, s64
*timeout
)
582 poll_table
* pt
= &wait
->pt
;
584 /* Optimise the no-wait case */
589 struct poll_list
*walk
;
592 set_current_state(TASK_INTERRUPTIBLE
);
594 while(walk
!= NULL
) {
595 do_pollfd( walk
->len
, walk
->entries
, &pt
, &count
);
599 if (count
|| !*timeout
|| signal_pending(current
))
606 /* Wait indefinitely */
607 __timeout
= MAX_SCHEDULE_TIMEOUT
;
608 } else if (unlikely(*timeout
>= (s64
)MAX_SCHEDULE_TIMEOUT
-1)) {
610 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
613 __timeout
= MAX_SCHEDULE_TIMEOUT
- 1;
614 *timeout
-= __timeout
;
616 __timeout
= *timeout
;
620 __timeout
= schedule_timeout(__timeout
);
622 *timeout
+= __timeout
;
624 __set_current_state(TASK_RUNNING
);
628 #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
629 sizeof(struct pollfd))
631 int do_sys_poll(struct pollfd __user
*ufds
, unsigned int nfds
, s64
*timeout
)
633 struct poll_wqueues table
;
636 struct poll_list
*head
;
637 struct poll_list
*walk
;
640 /* Allocate small arguments on the stack to save memory and be faster */
641 char stack_pps
[POLL_STACK_ALLOC
];
642 struct poll_list
*stack_pp
= NULL
;
644 /* Do a sanity check on nfds ... */
646 fdt
= files_fdtable(current
->files
);
647 max_fdset
= fdt
->max_fdset
;
649 if (nfds
> max_fdset
&& nfds
> OPEN_MAX
)
652 poll_initwait(&table
);
659 struct poll_list
*pp
;
661 if (stack_pp
== NULL
)
664 num
= POLLFD_PER_PAGE
;
667 size
= sizeof(struct poll_list
) + sizeof(struct pollfd
)*num
;
669 stack_pp
= pp
= (struct poll_list
*)stack_pps
;
671 pp
= kmalloc(size
, GFP_KERNEL
);
683 if (copy_from_user(pp
->entries
, ufds
+ nfds
-i
,
684 sizeof(struct pollfd
)*num
)) {
691 fdcount
= do_poll(nfds
, head
, &table
, timeout
);
693 /* OK, now copy the revents fields back to user space. */
696 while(walk
!= NULL
) {
697 struct pollfd
*fds
= walk
->entries
;
700 for (j
=0; j
< walk
->len
; j
++, ufds
++) {
701 if(__put_user(fds
[j
].revents
, &ufds
->revents
))
707 if (!fdcount
&& signal_pending(current
))
712 struct poll_list
*pp
= walk
->next
;
713 if (walk
!= stack_pp
)
717 poll_freewait(&table
);
721 asmlinkage
long sys_poll(struct pollfd __user
*ufds
, unsigned int nfds
,
724 s64 timeout_jiffies
= 0;
728 /* We can only overflow if HZ > 1000 */
729 if (timeout_msecs
/ 1000 > (s64
)0x7fffffffffffffffULL
/ (s64
)HZ
)
730 timeout_jiffies
= -1;
733 timeout_jiffies
= msecs_to_jiffies(timeout_msecs
);
736 return do_sys_poll(ufds
, nfds
, &timeout_jiffies
);
739 #ifdef TIF_RESTORE_SIGMASK
740 asmlinkage
long sys_ppoll(struct pollfd __user
*ufds
, unsigned int nfds
,
741 struct timespec __user
*tsp
, const sigset_t __user
*sigmask
,
744 sigset_t ksigmask
, sigsaved
;
750 if (copy_from_user(&ts
, tsp
, sizeof(ts
)))
753 /* Cast to u64 to make GCC stop complaining */
754 if ((u64
)ts
.tv_sec
>= (u64
)MAX_INT64_SECONDS
)
755 timeout
= -1; /* infinite */
757 timeout
= ROUND_UP(ts
.tv_nsec
, NSEC_PER_SEC
/HZ
);
758 timeout
+= ts
.tv_sec
* HZ
;
763 /* XXX: Don't preclude handling different sized sigset_t's. */
764 if (sigsetsize
!= sizeof(sigset_t
))
766 if (copy_from_user(&ksigmask
, sigmask
, sizeof(ksigmask
)))
769 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
770 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
773 ret
= do_sys_poll(ufds
, nfds
, &timeout
);
775 /* We can restart this syscall, usually */
778 * Don't restore the signal mask yet. Let do_signal() deliver
779 * the signal on the way back to userspace, before the signal
783 memcpy(¤t
->saved_sigmask
, &sigsaved
,
785 set_thread_flag(TIF_RESTORE_SIGMASK
);
787 ret
= -ERESTARTNOHAND
;
789 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
791 if (tsp
&& timeout
>= 0) {
794 if (current
->personality
& STICKY_TIMEOUTS
)
796 /* Yes, we know it's actually an s64, but it's also positive. */
797 rts
.tv_nsec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
)) *
799 rts
.tv_sec
= timeout
;
800 if (timespec_compare(&rts
, &ts
) >= 0)
802 if (copy_to_user(tsp
, &rts
, sizeof(rts
))) {
805 * If an application puts its timeval in read-only
806 * memory, we don't want the Linux-specific update to
807 * the timeval to cause a fault after the select has
808 * completed successfully. However, because we're not
809 * updating the timeval, we can't restart the system
812 if (ret
== -ERESTARTNOHAND
&& timeout
>= 0)
819 #endif /* TIF_RESTORE_SIGMASK */
This page took 0.072271 seconds and 6 git commands to generate.