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/malloc.h>
18 #include <linux/smp_lock.h>
19 #include <linux/poll.h>
20 #include <linux/file.h>
22 #include <asm/uaccess.h>
24 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
25 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
28 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
29 * I have rewritten this, taking some shortcuts: This code may not be easy to
30 * follow, but it should be free of race-conditions, and it's practical. If you
31 * understand what I'm doing here, then you understand how the linux
32 * sleep/wakeup mechanism works.
34 * Two very simple procedures, poll_wait() and free_wait() make all the
35 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
36 * as all select/poll functions have to call it to add an entry to the
41 * I rewrote this again to make the poll_table size variable, take some
42 * more shortcuts, improve responsiveness, and remove another race that
43 * Linus noticed. -- jrs
46 static poll_table
* alloc_wait(int nfds
)
51 out
= (poll_table
*) __get_free_page(GFP_KERNEL
);
55 out
->entry
= (struct poll_table_entry
*)(out
+ 1);
57 nfds
-=__MAX_POLL_TABLE_ENTRIES
;
60 poll_table
*tmp
= (poll_table
*) __get_free_page(GFP_KERNEL
);
64 free_page((unsigned long)out
);
70 tmp
->entry
= (struct poll_table_entry
*)(tmp
+ 1);
74 nfds
-=__MAX_POLL_TABLE_ENTRIES
;
79 static void free_wait(poll_table
* p
)
81 struct poll_table_entry
* entry
;
85 entry
= p
->entry
+ p
->nr
;
89 remove_wait_queue(entry
->wait_address
,&entry
->wait
);
94 free_page((unsigned long) old
);
98 void __pollwait(struct file
* filp
, wait_queue_head_t
* wait_address
, poll_table
*p
)
101 if (p
->nr
< __MAX_POLL_TABLE_ENTRIES
) {
102 struct poll_table_entry
* entry
;
103 entry
= p
->entry
+ p
->nr
;
106 entry
->wait_address
= wait_address
;
107 init_waitqueue_entry(&entry
->wait
, current
);
108 add_wait_queue(wait_address
,&entry
->wait
);
116 #define __IN(fds, n) (fds->in + n)
117 #define __OUT(fds, n) (fds->out + n)
118 #define __EX(fds, n) (fds->ex + n)
119 #define __RES_IN(fds, n) (fds->res_in + n)
120 #define __RES_OUT(fds, n) (fds->res_out + n)
121 #define __RES_EX(fds, n) (fds->res_ex + n)
123 #define BITS(fds, n) (*__IN(fds, n)|*__OUT(fds, n)|*__EX(fds, n))
125 static int max_select_fd(unsigned long n
, fd_set_bits
*fds
)
127 unsigned long *open_fds
;
131 /* handle last in-complete long-word first */
132 set
= ~(~0UL << (n
& (__NFDBITS
-1)));
134 open_fds
= current
->files
->open_fds
->fds_bits
+n
;
139 if (!(set
& ~*open_fds
))
150 if (set
& ~*open_fds
)
159 max
+= n
* __NFDBITS
;
165 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
166 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
167 #define ISSET(i,m) (((i)&*(m)) != 0)
168 #define SET(i,m) (*(m) |= (i))
170 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
171 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
172 #define POLLEX_SET (POLLPRI)
174 int do_select(int n
, fd_set_bits
*fds
, long *timeout
)
176 poll_table
*wait
, *orig_wait
;
178 long __timeout
= *timeout
;
180 orig_wait
= wait
= NULL
;
182 read_lock(¤t
->files
->file_lock
);
183 retval
= max_select_fd(n
, fds
);
184 read_unlock(¤t
->files
->file_lock
);
190 orig_wait
= wait
= alloc_wait(n
);
196 set_current_state(TASK_INTERRUPTIBLE
);
197 for (i
= 0 ; i
< n
; i
++) {
198 unsigned long bit
= BIT(i
);
203 if (!(bit
& BITS(fds
, off
)))
208 mask
= DEFAULT_POLLMASK
;
209 if (file
->f_op
&& file
->f_op
->poll
)
210 mask
= file
->f_op
->poll(file
, wait
);
213 if ((mask
& POLLIN_SET
) && ISSET(bit
, __IN(fds
,off
))) {
214 SET(bit
, __RES_IN(fds
,off
));
218 if ((mask
& POLLOUT_SET
) && ISSET(bit
, __OUT(fds
,off
))) {
219 SET(bit
, __RES_OUT(fds
,off
));
223 if ((mask
& POLLEX_SET
) && ISSET(bit
, __EX(fds
,off
))) {
224 SET(bit
, __RES_EX(fds
,off
));
230 if (retval
|| !__timeout
|| signal_pending(current
))
232 __timeout
= schedule_timeout(__timeout
);
234 current
->state
= TASK_RUNNING
;
236 free_wait(orig_wait
);
239 * Up-to-date the caller timeout.
241 *timeout
= __timeout
;
246 * We can actually return ERESTARTSYS instead of EINTR, but I'd
247 * like to be certain this leads to no problems. So I return
248 * EINTR just for safety.
250 * Update: ERESTARTSYS breaks at least the xview clock binary, so
251 * I'm trying ERESTARTNOHAND which restart only when you want to.
253 #define MAX_SELECT_SECONDS \
254 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
257 sys_select(int n
, fd_set
*inp
, fd_set
*outp
, fd_set
*exp
, struct timeval
*tvp
)
264 timeout
= MAX_SCHEDULE_TIMEOUT
;
268 if ((ret
= verify_area(VERIFY_READ
, tvp
, sizeof(*tvp
)))
269 || (ret
= __get_user(sec
, &tvp
->tv_sec
))
270 || (ret
= __get_user(usec
, &tvp
->tv_usec
)))
274 if (sec
< 0 || usec
< 0)
277 if ((unsigned long) sec
< MAX_SELECT_SECONDS
) {
278 timeout
= ROUND_UP(usec
, 1000000/HZ
);
279 timeout
+= sec
* (unsigned long) HZ
;
287 if (n
> current
->files
->max_fdset
)
288 n
= current
->files
->max_fdset
;
291 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
292 * since we used fdset we need to allocate memory in units of
297 bits
= kmalloc(6 * size
, GFP_KERNEL
);
300 fds
.in
= (unsigned long *) bits
;
301 fds
.out
= (unsigned long *) (bits
+ size
);
302 fds
.ex
= (unsigned long *) (bits
+ 2*size
);
303 fds
.res_in
= (unsigned long *) (bits
+ 3*size
);
304 fds
.res_out
= (unsigned long *) (bits
+ 4*size
);
305 fds
.res_ex
= (unsigned long *) (bits
+ 5*size
);
307 if ((ret
= get_fd_set(n
, inp
, fds
.in
)) ||
308 (ret
= get_fd_set(n
, outp
, fds
.out
)) ||
309 (ret
= get_fd_set(n
, exp
, fds
.ex
)))
311 zero_fd_set(n
, fds
.res_in
);
312 zero_fd_set(n
, fds
.res_out
);
313 zero_fd_set(n
, fds
.res_ex
);
315 ret
= do_select(n
, &fds
, &timeout
);
317 if (tvp
&& !(current
->personality
& STICKY_TIMEOUTS
)) {
318 time_t sec
= 0, usec
= 0;
322 usec
*= (1000000/HZ
);
324 put_user(sec
, &tvp
->tv_sec
);
325 put_user(usec
, &tvp
->tv_usec
);
331 ret
= -ERESTARTNOHAND
;
332 if (signal_pending(current
))
337 set_fd_set(n
, inp
, fds
.res_in
);
338 set_fd_set(n
, outp
, fds
.res_out
);
339 set_fd_set(n
, exp
, fds
.res_ex
);
347 #define POLLFD_PER_PAGE ((PAGE_SIZE) / sizeof(struct pollfd))
349 static void do_pollfd(unsigned int num
, struct pollfd
* fdpage
,
350 poll_table
** pwait
, int *count
)
354 for (i
= 0; i
< num
; i
++) {
363 struct file
* file
= fget(fd
);
366 mask
= DEFAULT_POLLMASK
;
367 if (file
->f_op
&& file
->f_op
->poll
)
368 mask
= file
->f_op
->poll(file
, *pwait
);
369 mask
&= fdp
->events
| POLLERR
| POLLHUP
;
381 static int do_poll(unsigned int nfds
, unsigned int nchunks
, unsigned int nleft
,
382 struct pollfd
*fds
[], poll_table
*wait
, long timeout
)
389 set_current_state(TASK_INTERRUPTIBLE
);
390 for (i
=0; i
< nchunks
; i
++)
391 do_pollfd(POLLFD_PER_PAGE
, fds
[i
], &wait
, &count
);
393 do_pollfd(nleft
, fds
[nchunks
], &wait
, &count
);
395 if (count
|| !timeout
|| signal_pending(current
))
397 timeout
= schedule_timeout(timeout
);
399 current
->state
= TASK_RUNNING
;
403 asmlinkage
long sys_poll(struct pollfd
* ufds
, unsigned int nfds
, long timeout
)
405 int i
, j
, fdcount
, err
;
407 poll_table
*wait
= NULL
;
410 /* Do a sanity check on nfds ... */
411 if (nfds
> current
->files
->max_fds
)
415 /* Careful about overflow in the intermediate values */
416 if ((unsigned long) timeout
< MAX_SCHEDULE_TIMEOUT
/ HZ
)
417 timeout
= (unsigned long)(timeout
*HZ
+999)/1000+1;
418 else /* Negative or overflow */
419 timeout
= MAX_SCHEDULE_TIMEOUT
;
423 wait
= alloc_wait(nfds
);
431 fds
= (struct pollfd
**)kmalloc(
432 (1 + (nfds
- 1) / POLLFD_PER_PAGE
) * sizeof(struct pollfd
*),
440 while (nleft
> POLLFD_PER_PAGE
) { /* allocate complete PAGE_SIZE chunks */
441 fds
[nchunks
] = (struct pollfd
*)__get_free_page(GFP_KERNEL
);
442 if (fds
[nchunks
] == NULL
)
445 nleft
-= POLLFD_PER_PAGE
;
447 if (nleft
) { /* allocate last PAGE_SIZE chunk, only nleft elements used */
448 fds
[nchunks
] = (struct pollfd
*)__get_free_page(GFP_KERNEL
);
449 if (fds
[nchunks
] == NULL
)
454 for (i
=0; i
< nchunks
; i
++)
455 if (copy_from_user(fds
[i
], ufds
+ i
*POLLFD_PER_PAGE
, PAGE_SIZE
))
458 if (copy_from_user(fds
[nchunks
], ufds
+ nchunks
*POLLFD_PER_PAGE
,
459 nleft
* sizeof(struct pollfd
)))
463 fdcount
= do_poll(nfds
, nchunks
, nleft
, fds
, wait
, timeout
);
465 /* OK, now copy the revents fields back to user space. */
466 for(i
=0; i
< nchunks
; i
++)
467 for (j
=0; j
< POLLFD_PER_PAGE
; j
++, ufds
++)
468 __put_user((fds
[i
] + j
)->revents
, &ufds
->revents
);
470 for (j
=0; j
< nleft
; j
++, ufds
++)
471 __put_user((fds
[nchunks
] + j
)->revents
, &ufds
->revents
);
474 if (!fdcount
&& signal_pending(current
))
479 free_page((unsigned long)(fds
[nchunks
]));
481 for (i
=0; i
< nchunks
; i
++)
482 free_page((unsigned long)(fds
[i
]));