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>
26 #include <asm/uaccess.h>
28 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
29 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
31 struct poll_table_entry {
34 wait_queue_head_t * wait_address;
37 struct poll_table_page {
38 struct poll_table_page * next;
39 struct poll_table_entry * entry;
40 struct poll_table_entry entries[0];
43 #define POLL_TABLE_FULL(table) \
44 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
47 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
48 * I have rewritten this, taking some shortcuts: This code may not be easy to
49 * follow, but it should be free of race-conditions, and it's practical. If you
50 * understand what I'm doing here, then you understand how the linux
51 * sleep/wakeup mechanism works.
53 * Two very simple procedures, poll_wait() and poll_freewait() make all the
54 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
55 * as all select/poll functions have to call it to add an entry to the
58 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
61 void poll_initwait(struct poll_wqueues *pwq)
63 init_poll_funcptr(&pwq->pt, __pollwait);
68 EXPORT_SYMBOL(poll_initwait);
70 void poll_freewait(struct poll_wqueues *pwq)
72 struct poll_table_page * p = pwq->table;
74 struct poll_table_entry * entry;
75 struct poll_table_page *old;
80 remove_wait_queue(entry->wait_address,&entry->wait);
82 } while (entry > p->entries);
85 free_page((unsigned long) old);
89 EXPORT_SYMBOL(poll_freewait);
91 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
94 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
95 struct poll_table_page *table = p->table;
97 if (!table || POLL_TABLE_FULL(table)) {
98 struct poll_table_page *new_table;
100 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
103 __set_current_state(TASK_RUNNING);
106 new_table->entry = new_table->entries;
107 new_table->next = table;
108 p->table = new_table;
112 /* Add a new entry */
114 struct poll_table_entry * entry = table->entry;
115 table->entry = entry+1;
118 entry->wait_address = wait_address;
119 init_waitqueue_entry(&entry->wait, current);
120 add_wait_queue(wait_address,&entry->wait);
124 #define FDS_IN(fds, n) (fds->in + n)
125 #define FDS_OUT(fds, n) (fds->out + n)
126 #define FDS_EX(fds, n) (fds->ex + n)
128 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
130 static int max_select_fd(unsigned long n, fd_set_bits *fds)
132 unsigned long *open_fds;
136 /* handle last in-complete long-word first */
137 set = ~(~0UL << (n & (__NFDBITS-1)));
139 open_fds = current->files->open_fds->fds_bits+n;
144 if (!(set & ~*open_fds))
155 if (set & ~*open_fds)
164 max += n * __NFDBITS;
170 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
171 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
172 #define ISSET(i,m) (((i)&*(m)) != 0)
173 #define SET(i,m) (*(m) |= (i))
175 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
176 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
177 #define POLLEX_SET (POLLPRI)
179 int do_select(int n, fd_set_bits *fds, long *timeout)
181 struct poll_wqueues table;
184 long __timeout = *timeout;
186 spin_lock(¤t->files->file_lock);
187 retval = max_select_fd(n, fds);
188 spin_unlock(¤t->files->file_lock);
194 poll_initwait(&table);
200 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
202 set_current_state(TASK_INTERRUPTIBLE);
204 inp = fds->in; outp = fds->out; exp = fds->ex;
205 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
207 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
208 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
209 unsigned long res_in = 0, res_out = 0, res_ex = 0;
210 struct file_operations *f_op = NULL;
211 struct file *file = NULL;
213 in = *inp++; out = *outp++; ex = *exp++;
214 all_bits = in | out | ex;
220 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
223 if (!(bit & all_bits))
228 mask = DEFAULT_POLLMASK;
229 if (f_op && f_op->poll)
230 mask = (*f_op->poll)(file, retval ? NULL : wait);
232 if ((mask & POLLIN_SET) && (in & bit)) {
236 if ((mask & POLLOUT_SET) && (out & bit)) {
240 if ((mask & POLLEX_SET) && (ex & bit)) {
255 if (retval || !__timeout || signal_pending(current))
258 retval = table.error;
261 __timeout = schedule_timeout(__timeout);
263 __set_current_state(TASK_RUNNING);
265 poll_freewait(&table);
268 * Up-to-date the caller timeout.
270 *timeout = __timeout;
274 static void *select_bits_alloc(int size)
276 return kmalloc(6 * size, GFP_KERNEL);
279 static void select_bits_free(void *bits, int size)
285 * We can actually return ERESTARTSYS instead of EINTR, but I'd
286 * like to be certain this leads to no problems. So I return
287 * EINTR just for safety.
289 * Update: ERESTARTSYS breaks at least the xview clock binary, so
290 * I'm trying ERESTARTNOHAND which restart only when you want to.
292 #define MAX_SELECT_SECONDS \
293 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
296 sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
301 int ret, size, max_fdset;
303 timeout = MAX_SCHEDULE_TIMEOUT;
307 if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
308 || __get_user(sec, &tvp->tv_sec)
309 || __get_user(usec, &tvp->tv_usec)) {
315 if (sec < 0 || usec < 0)
318 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
319 timeout = ROUND_UP(usec, 1000000/HZ);
320 timeout += sec * (unsigned long) HZ;
328 /* max_fdset can increase, so grab it once to avoid race */
329 max_fdset = current->files->max_fdset;
334 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
335 * since we used fdset we need to allocate memory in units of
340 bits = select_bits_alloc(size);
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);
360 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
361 time_t sec = 0, usec = 0;
365 usec *= (1000000/HZ);
367 put_user(sec, &tvp->tv_sec);
368 put_user(usec, &tvp->tv_usec);
374 ret = -ERESTARTNOHAND;
375 if (signal_pending(current))
380 if (set_fd_set(n, inp, fds.res_in) ||
381 set_fd_set(n, outp, fds.res_out) ||
382 set_fd_set(n, exp, fds.res_ex))
386 select_bits_free(bits, size);
392 struct poll_list *next;
394 struct pollfd entries[0];
397 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
399 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
400 poll_table ** pwait, int *count)
404 for (i = 0; i < num; i++) {
413 struct file * file = fget(fd);
416 mask = DEFAULT_POLLMASK;
417 if (file->f_op && file->f_op->poll)
418 mask = file->f_op->poll(file, *pwait);
419 mask &= fdp->events | POLLERR | POLLHUP;
431 static int do_poll(unsigned int nfds, struct poll_list *list,
432 struct poll_wqueues *wait, long timeout)
435 poll_table* pt = &wait->pt;
441 struct poll_list *walk;
442 set_current_state(TASK_INTERRUPTIBLE);
444 while(walk != NULL) {
445 do_pollfd( walk->len, walk->entries, &pt, &count);
449 if (count || !timeout || signal_pending(current))
454 timeout = schedule_timeout(timeout);
456 __set_current_state(TASK_RUNNING);
460 asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long timeout)
462 struct poll_wqueues table;
465 struct poll_list *head;
466 struct poll_list *walk;
468 /* Do a sanity check on nfds ... */
469 if (nfds > current->files->max_fdset && nfds > OPEN_MAX)
473 /* Careful about overflow in the intermediate values */
474 if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
475 timeout = (unsigned long)(timeout*HZ+999)/1000+1;
476 else /* Negative or overflow */
477 timeout = MAX_SCHEDULE_TIMEOUT;
480 poll_initwait(&table);
487 struct poll_list *pp;
488 pp = kmalloc(sizeof(struct poll_list)+
489 sizeof(struct pollfd)*
490 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
495 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
502 if (copy_from_user(pp->entries, ufds + nfds-i,
503 sizeof(struct pollfd)*pp->len)) {
509 fdcount = do_poll(nfds, head, &table, timeout);
511 /* OK, now copy the revents fields back to user space. */
514 while(walk != NULL) {
515 struct pollfd *fds = walk->entries;
518 for (j=0; j < walk->len; j++, ufds++) {
519 if(__put_user(fds[j].revents, &ufds->revents))
525 if (!fdcount && signal_pending(current))
530 struct poll_list *pp = walk->next;
534 poll_freewait(&table);