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
, long *timeout
)
184 struct poll_wqueues table
;
187 long __timeout
= *timeout
;
190 retval
= max_select_fd(n
, fds
);
197 poll_initwait(&table
);
203 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
;
264 __timeout
= schedule_timeout(__timeout
);
266 __set_current_state(TASK_RUNNING
);
268 poll_freewait(&table
);
271 * Up-to-date the caller timeout.
273 *timeout
= __timeout
;
277 static void *select_bits_alloc(int size
)
279 return kmalloc(6 * size
, GFP_KERNEL
);
282 static void select_bits_free(void *bits
, int size
)
288 * We can actually return ERESTARTSYS instead of EINTR, but I'd
289 * like to be certain this leads to no problems. So I return
290 * EINTR just for safety.
292 * Update: ERESTARTSYS breaks at least the xview clock binary, so
293 * I'm trying ERESTARTNOHAND which restart only when you want to.
295 #define MAX_SELECT_SECONDS \
296 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
299 sys_select(int n
, fd_set __user
*inp
, fd_set __user
*outp
, fd_set __user
*exp
, struct timeval __user
*tvp
)
304 int ret
, size
, max_fdset
;
307 timeout
= MAX_SCHEDULE_TIMEOUT
;
311 if (!access_ok(VERIFY_READ
, tvp
, sizeof(*tvp
))
312 || __get_user(sec
, &tvp
->tv_sec
)
313 || __get_user(usec
, &tvp
->tv_usec
)) {
319 if (sec
< 0 || usec
< 0)
322 if ((unsigned long) sec
< MAX_SELECT_SECONDS
) {
323 timeout
= ROUND_UP(usec
, 1000000/HZ
);
324 timeout
+= sec
* (unsigned long) HZ
;
332 /* max_fdset can increase, so grab it once to avoid race */
334 fdt
= files_fdtable(current
->files
);
335 max_fdset
= fdt
->max_fdset
;
341 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
342 * since we used fdset we need to allocate memory in units of
347 bits
= select_bits_alloc(size
);
350 fds
.in
= (unsigned long *) bits
;
351 fds
.out
= (unsigned long *) (bits
+ size
);
352 fds
.ex
= (unsigned long *) (bits
+ 2*size
);
353 fds
.res_in
= (unsigned long *) (bits
+ 3*size
);
354 fds
.res_out
= (unsigned long *) (bits
+ 4*size
);
355 fds
.res_ex
= (unsigned long *) (bits
+ 5*size
);
357 if ((ret
= get_fd_set(n
, inp
, fds
.in
)) ||
358 (ret
= get_fd_set(n
, outp
, fds
.out
)) ||
359 (ret
= get_fd_set(n
, exp
, fds
.ex
)))
361 zero_fd_set(n
, fds
.res_in
);
362 zero_fd_set(n
, fds
.res_out
);
363 zero_fd_set(n
, fds
.res_ex
);
365 ret
= do_select(n
, &fds
, &timeout
);
367 if (tvp
&& !(current
->personality
& STICKY_TIMEOUTS
)) {
368 time_t sec
= 0, usec
= 0;
372 usec
*= (1000000/HZ
);
374 put_user(sec
, &tvp
->tv_sec
);
375 put_user(usec
, &tvp
->tv_usec
);
381 ret
= -ERESTARTNOHAND
;
382 if (signal_pending(current
))
387 if (set_fd_set(n
, inp
, fds
.res_in
) ||
388 set_fd_set(n
, outp
, fds
.res_out
) ||
389 set_fd_set(n
, exp
, fds
.res_ex
))
393 select_bits_free(bits
, size
);
399 struct poll_list
*next
;
401 struct pollfd entries
[0];
404 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
406 static void do_pollfd(unsigned int num
, struct pollfd
* fdpage
,
407 poll_table
** pwait
, int *count
)
411 for (i
= 0; i
< num
; i
++) {
420 struct file
* file
= fget(fd
);
423 mask
= DEFAULT_POLLMASK
;
424 if (file
->f_op
&& file
->f_op
->poll
)
425 mask
= file
->f_op
->poll(file
, *pwait
);
426 mask
&= fdp
->events
| POLLERR
| POLLHUP
;
438 static int do_poll(unsigned int nfds
, struct poll_list
*list
,
439 struct poll_wqueues
*wait
, long timeout
)
442 poll_table
* pt
= &wait
->pt
;
448 struct poll_list
*walk
;
449 set_current_state(TASK_INTERRUPTIBLE
);
451 while(walk
!= NULL
) {
452 do_pollfd( walk
->len
, walk
->entries
, &pt
, &count
);
456 if (count
|| !timeout
|| signal_pending(current
))
461 timeout
= schedule_timeout(timeout
);
463 __set_current_state(TASK_RUNNING
);
467 asmlinkage
long sys_poll(struct pollfd __user
* ufds
, unsigned int nfds
, long timeout
)
469 struct poll_wqueues table
;
472 struct poll_list
*head
;
473 struct poll_list
*walk
;
477 /* Do a sanity check on nfds ... */
479 fdt
= files_fdtable(current
->files
);
480 max_fdset
= fdt
->max_fdset
;
482 if (nfds
> max_fdset
&& nfds
> OPEN_MAX
)
486 /* Careful about overflow in the intermediate values */
487 if ((unsigned long) timeout
< MAX_SCHEDULE_TIMEOUT
/ HZ
)
488 timeout
= (unsigned long)(timeout
*HZ
+999)/1000+1;
489 else /* Negative or overflow */
490 timeout
= MAX_SCHEDULE_TIMEOUT
;
493 poll_initwait(&table
);
500 struct poll_list
*pp
;
501 pp
= kmalloc(sizeof(struct poll_list
)+
502 sizeof(struct pollfd
)*
503 (i
>POLLFD_PER_PAGE
?POLLFD_PER_PAGE
:i
),
508 pp
->len
= (i
>POLLFD_PER_PAGE
?POLLFD_PER_PAGE
:i
);
515 if (copy_from_user(pp
->entries
, ufds
+ nfds
-i
,
516 sizeof(struct pollfd
)*pp
->len
)) {
522 fdcount
= do_poll(nfds
, head
, &table
, timeout
);
524 /* OK, now copy the revents fields back to user space. */
527 while(walk
!= NULL
) {
528 struct pollfd
*fds
= walk
->entries
;
531 for (j
=0; j
< walk
->len
; j
++, ufds
++) {
532 if(__put_user(fds
[j
].revents
, &ufds
->revents
))
538 if (!fdcount
&& signal_pending(current
))
543 struct poll_list
*pp
= walk
->next
;
547 poll_freewait(&table
);