17 #include <linux/kernel.h>
18 #include <linux/sched.h>
20 #include <linux/export.h>
21 #include <linux/slab.h>
22 #include <linux/poll.h>
23 #include <linux/personality.h>
28 #include <linux/hrtimer.h>
30 #include <asm/uaccess.h>
45 #define MAX_SLACK (100 * NSEC_PER_MSEC)
47 static long __estimate_accuracy(
struct timespec *tv)
56 divfactor = divfactor / 5;
61 slack = tv->
tv_nsec / divfactor;
83 now = timespec_sub(*tv, now);
84 ret = __estimate_accuracy(&now);
85 if (ret < current->timer_slack_ns)
98 #define POLL_TABLE_FULL(table) \
99 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
118 init_poll_funcptr(&pwq->
pt, __pollwait);
146 free_poll_entry(entry);
172 p->
table = new_table;
176 return table->
entry++;
210 if (key && !((
unsigned long)key & entry->
key))
212 return __pollwake(wait, mode, sync, key);
223 entry->
filp = get_file(filp);
226 init_waitqueue_func_entry(&entry->
wait, pollwake);
232 ktime_t *expires,
unsigned long slack)
273 if (!timespec_valid(&ts))
286 static int poll_select_copy_remaining(
struct timespec *end_time,
void __user *p,
303 rts = timespec_sub(*end_time,
rts);
305 rts.tv_sec =
rts.tv_nsec = 0;
308 if (
sizeof(rtv) >
sizeof(rtv.tv_sec) +
sizeof(rtv.tv_usec))
309 memset(&rtv, 0,
sizeof(rtv));
310 rtv.tv_sec =
rts.tv_sec;
333 #define FDS_IN(fds, n) (fds->in + n)
334 #define FDS_OUT(fds, n) (fds->out + n)
335 #define FDS_EX(fds, n) (fds->ex + n)
337 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
339 static int max_select_fd(
unsigned long n,
fd_set_bits *fds)
341 unsigned long *open_fds;
355 if (!(
set & ~*open_fds))
366 if (
set & ~*open_fds)
381 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
382 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
383 #define POLLEX_SET (POLLPRI)
385 static inline void wait_key_set(
poll_table *wait,
unsigned long in,
386 unsigned long out,
unsigned long bit)
401 unsigned long slack = 0;
404 retval = max_select_fd(n, fds);
418 if (end_time && !timed_out)
423 unsigned long *rinp, *routp, *rexp, *
inp, *
outp, *
exp;
425 inp = fds->
in; outp = fds->
out; exp = fds->
ex;
428 for (i = 0; i <
n; ++rinp, ++routp, ++rexp) {
430 unsigned long res_in = 0, res_out = 0, res_ex = 0;
432 in = *inp++; out = *outp++; ex = *exp++;
433 all_bits = in | out | ex;
443 if (!(bit & all_bits))
450 if (f_op && f_op->
poll) {
451 wait_key_set(wait, in, out, bit);
481 if (retval || timed_out || signal_pending(
current))
484 retval = table.
error;
493 if (end_time && !to) {
494 expire = timespec_to_ktime(*end_time);
546 if (size >
sizeof(stack_fds) / 6) {
560 if ((ret = get_fd_set(n, inp, fds.
in)) ||
561 (ret = get_fd_set(n, outp, fds.
out)) ||
562 (ret = get_fd_set(n, exp, fds.
ex)))
564 zero_fd_set(n, fds.
res_in);
566 zero_fd_set(n, fds.
res_ex);
579 if (set_fd_set(n, inp, fds.
res_in) ||
580 set_fd_set(n, outp, fds.
res_out) ||
581 set_fd_set(n, exp, fds.
res_ex))
585 if (bits != stack_fds)
592 fd_set __user *,
exp,
struct timeval __user *, tvp)
610 ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
644 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
655 set_restore_sigmask();
673 size_t sigsetsize = 0;
680 (
size_t __user *)(
sig+
sizeof(
void *))))
684 return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
687 #ifdef __ARCH_WANT_SYS_OLD_SELECT
688 struct sel_arg_struct {
691 struct timeval
__user *tvp;
696 struct sel_arg_struct
a;
710 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
727 struct fd f = fdget(fd);
731 if (f.
file->f_op && f.
file->f_op->poll) {
733 mask = f.
file->f_op->poll(f.
file, pwait);
745 static int do_poll(
unsigned int nfds,
struct poll_list *
list,
750 int timed_out = 0,
count = 0;
751 unsigned long slack = 0;
759 if (end_time && !timed_out)
765 for (walk = list; walk !=
NULL; walk = walk->
next) {
766 struct pollfd * pfd, * pfd_end;
769 pfd_end = pfd + walk->
len;
770 for (; pfd != pfd_end; pfd++) {
778 if (do_pollfd(pfd, pt)) {
794 if (
count || timed_out)
802 if (end_time && !to) {
803 expire = timespec_to_ktime(*end_time);
813 #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
814 sizeof(struct pollfd))
827 unsigned long todo = nfds;
848 size =
sizeof(
struct poll_list) + sizeof(struct pollfd) *
len;
857 fdcount = do_poll(nfds, head, &table, end_time);
860 for (walk = head; walk; walk = walk->
next) {
864 for (j = 0; j < walk->
len; j++, ufds++)
883 struct pollfd __user *ufds = restart_block->
poll.ufds;
884 int nfds = restart_block->
poll.nfds;
888 if (restart_block->
poll.has_timeout) {
889 end_time.tv_sec = restart_block->
poll.tv_sec;
890 end_time.tv_nsec = restart_block->
poll.tv_nsec;
897 restart_block->
fn = do_restart_poll;
909 if (timeout_msecs >= 0) {
918 struct restart_block *restart_block;
921 restart_block->
fn = do_restart_poll;
925 if (timeout_msecs >= 0) {
928 restart_block->
poll.has_timeout = 1;
930 restart_block->
poll.has_timeout = 0;
977 set_restore_sigmask();
983 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);