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
);
198 for (i
= 0 ; i
< n
; i
++) {
199 unsigned long bit
= BIT(i
);
204 if (!(bit
& BITS(fds
, off
)))
209 mask
= DEFAULT_POLLMASK
;
210 if (file
->f_op
&& file
->f_op
->poll
)
211 mask
= file
->f_op
->poll(file
, wait
);
214 if ((mask
& POLLIN_SET
) && ISSET(bit
, __IN(fds
,off
))) {
215 SET(bit
, __RES_IN(fds
,off
));
219 if ((mask
& POLLOUT_SET
) && ISSET(bit
, __OUT(fds
,off
))) {
220 SET(bit
, __RES_OUT(fds
,off
));
224 if ((mask
& POLLEX_SET
) && ISSET(bit
, __EX(fds
,off
))) {
225 SET(bit
, __RES_EX(fds
,off
));
232 if (retval
|| !__timeout
|| signal_pending(current
))
234 __timeout
= schedule_timeout(__timeout
);
236 current
->state
= TASK_RUNNING
;
238 free_wait(orig_wait
);
241 * Up-to-date the caller timeout.
243 *timeout
= __timeout
;
248 * We can actually return ERESTARTSYS instead of EINTR, but I'd
249 * like to be certain this leads to no problems. So I return
250 * EINTR just for safety.
252 * Update: ERESTARTSYS breaks at least the xview clock binary, so
253 * I'm trying ERESTARTNOHAND which restart only when you want to.
255 #define MAX_SELECT_SECONDS \
256 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
259 sys_select(int n
, fd_set
*inp
, fd_set
*outp
, fd_set
*exp
, struct timeval
*tvp
)
266 timeout
= MAX_SCHEDULE_TIMEOUT
;
270 if ((ret
= verify_area(VERIFY_READ
, tvp
, sizeof(*tvp
)))
271 || (ret
= __get_user(sec
, &tvp
->tv_sec
))
272 || (ret
= __get_user(usec
, &tvp
->tv_usec
)))
276 if (sec
< 0 || usec
< 0)
279 if ((unsigned long) sec
< MAX_SELECT_SECONDS
) {
280 timeout
= ROUND_UP(usec
, 1000000/HZ
);
281 timeout
+= sec
* (unsigned long) HZ
;
289 if (n
> current
->files
->max_fdset
)
290 n
= current
->files
->max_fdset
;
293 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
294 * since we used fdset we need to allocate memory in units of
299 bits
= kmalloc(6 * size
, GFP_KERNEL
);
302 fds
.in
= (unsigned long *) bits
;
303 fds
.out
= (unsigned long *) (bits
+ size
);
304 fds
.ex
= (unsigned long *) (bits
+ 2*size
);
305 fds
.res_in
= (unsigned long *) (bits
+ 3*size
);
306 fds
.res_out
= (unsigned long *) (bits
+ 4*size
);
307 fds
.res_ex
= (unsigned long *) (bits
+ 5*size
);
309 if ((ret
= get_fd_set(n
, inp
, fds
.in
)) ||
310 (ret
= get_fd_set(n
, outp
, fds
.out
)) ||
311 (ret
= get_fd_set(n
, exp
, fds
.ex
)))
313 zero_fd_set(n
, fds
.res_in
);
314 zero_fd_set(n
, fds
.res_out
);
315 zero_fd_set(n
, fds
.res_ex
);
317 ret
= do_select(n
, &fds
, &timeout
);
319 if (tvp
&& !(current
->personality
& STICKY_TIMEOUTS
)) {
320 time_t sec
= 0, usec
= 0;
324 usec
*= (1000000/HZ
);
326 put_user(sec
, &tvp
->tv_sec
);
327 put_user(usec
, &tvp
->tv_usec
);
333 ret
= -ERESTARTNOHAND
;
334 if (signal_pending(current
))
339 set_fd_set(n
, inp
, fds
.res_in
);
340 set_fd_set(n
, outp
, fds
.res_out
);
341 set_fd_set(n
, exp
, fds
.res_ex
);
349 #define POLLFD_PER_PAGE ((PAGE_SIZE) / sizeof(struct pollfd))
351 static void do_pollfd(unsigned int num
, struct pollfd
* fdpage
,
352 poll_table
** pwait
, int *count
)
356 for (i
= 0; i
< num
; i
++) {
365 struct file
* file
= fget(fd
);
368 mask
= DEFAULT_POLLMASK
;
369 if (file
->f_op
&& file
->f_op
->poll
)
370 mask
= file
->f_op
->poll(file
, *pwait
);
371 mask
&= fdp
->events
| POLLERR
| POLLHUP
;
383 static int do_poll(unsigned int nfds
, unsigned int nchunks
, unsigned int nleft
,
384 struct pollfd
*fds
[], poll_table
*wait
, long timeout
)
391 set_current_state(TASK_INTERRUPTIBLE
);
392 for (i
=0; i
< nchunks
; i
++)
393 do_pollfd(POLLFD_PER_PAGE
, fds
[i
], &wait
, &count
);
395 do_pollfd(nleft
, fds
[nchunks
], &wait
, &count
);
397 if (count
|| !timeout
|| signal_pending(current
))
399 timeout
= schedule_timeout(timeout
);
401 current
->state
= TASK_RUNNING
;
405 asmlinkage
long sys_poll(struct pollfd
* ufds
, unsigned int nfds
, long timeout
)
407 int i
, j
, fdcount
, err
;
409 poll_table
*wait
= NULL
;
412 /* Do a sanity check on nfds ... */
413 if (nfds
> current
->files
->max_fds
)
417 /* Careful about overflow in the intermediate values */
418 if ((unsigned long) timeout
< MAX_SCHEDULE_TIMEOUT
/ HZ
)
419 timeout
= (unsigned long)(timeout
*HZ
+999)/1000+1;
420 else /* Negative or overflow */
421 timeout
= MAX_SCHEDULE_TIMEOUT
;
425 wait
= alloc_wait(nfds
);
433 fds
= (struct pollfd
**)kmalloc(
434 (1 + (nfds
- 1) / POLLFD_PER_PAGE
) * sizeof(struct pollfd
*),
442 while (nleft
> POLLFD_PER_PAGE
) { /* allocate complete PAGE_SIZE chunks */
443 fds
[nchunks
] = (struct pollfd
*)__get_free_page(GFP_KERNEL
);
444 if (fds
[nchunks
] == NULL
)
447 nleft
-= POLLFD_PER_PAGE
;
449 if (nleft
) { /* allocate last PAGE_SIZE chunk, only nleft elements used */
450 fds
[nchunks
] = (struct pollfd
*)__get_free_page(GFP_KERNEL
);
451 if (fds
[nchunks
] == NULL
)
456 for (i
=0; i
< nchunks
; i
++)
457 if (copy_from_user(fds
[i
], ufds
+ i
*POLLFD_PER_PAGE
, PAGE_SIZE
))
460 if (copy_from_user(fds
[nchunks
], ufds
+ nchunks
*POLLFD_PER_PAGE
,
461 nleft
* sizeof(struct pollfd
)))
466 fdcount
= do_poll(nfds
, nchunks
, nleft
, fds
, wait
, timeout
);
469 /* OK, now copy the revents fields back to user space. */
470 for(i
=0; i
< nchunks
; i
++)
471 for (j
=0; j
< POLLFD_PER_PAGE
; j
++, ufds
++)
472 __put_user((fds
[i
] + j
)->revents
, &ufds
->revents
);
474 for (j
=0; j
< nleft
; j
++, ufds
++)
475 __put_user((fds
[nchunks
] + j
)->revents
, &ufds
->revents
);
478 if (!fdcount
&& signal_pending(current
))
483 free_page((unsigned long)(fds
[nchunks
]));
485 for (i
=0; i
< nchunks
; i
++)
486 free_page((unsigned long)(fds
[i
]));