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/slab.h>
18 #include <linux/smp_lock.h>
19 #include <linux/poll.h>
20 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
21 #include <linux/file.h>
23 #include <asm/uaccess.h>
25 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
26 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
28 struct poll_table_entry {
31 wait_queue_head_t * wait_address;
34 struct poll_table_page {
35 struct poll_table_page * next;
36 struct poll_table_entry * entry;
37 struct poll_table_entry entries[0];
40 #define POLL_TABLE_FULL(table) \
41 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
44 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
45 * I have rewritten this, taking some shortcuts: This code may not be easy to
46 * follow, but it should be free of race-conditions, and it's practical. If you
47 * understand what I'm doing here, then you understand how the linux
48 * sleep/wakeup mechanism works.
50 * Two very simple procedures, poll_wait() and poll_freewait() make all the
51 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
52 * as all select/poll functions have to call it to add an entry to the
56 void poll_freewait(poll_table* pt)
58 struct poll_table_page * p = pt->table;
60 struct poll_table_entry * entry;
61 struct poll_table_page *old;
66 remove_wait_queue(entry->wait_address,&entry->wait);
68 } while (entry > p->entries);
71 free_page((unsigned long) old);
75 void __pollwait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
77 struct poll_table_page *table = p->table;
79 if (!table || POLL_TABLE_FULL(table)) {
80 struct poll_table_page *new_table;
82 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
85 __set_current_state(TASK_RUNNING);
88 new_table->entry = new_table->entries;
89 new_table->next = table;
96 struct poll_table_entry * entry = table->entry;
97 table->entry = entry+1;
100 entry->wait_address = wait_address;
101 init_waitqueue_entry(&entry->wait, current);
102 add_wait_queue(wait_address,&entry->wait);
106 #define __IN(fds, n) (fds->in + n)
107 #define __OUT(fds, n) (fds->out + n)
108 #define __EX(fds, n) (fds->ex + n)
109 #define __RES_IN(fds, n) (fds->res_in + n)
110 #define __RES_OUT(fds, n) (fds->res_out + n)
111 #define __RES_EX(fds, n) (fds->res_ex + n)
113 #define BITS(fds, n) (*__IN(fds, n)|*__OUT(fds, n)|*__EX(fds, n))
115 static int max_select_fd(unsigned long n, fd_set_bits *fds)
117 unsigned long *open_fds;
121 /* handle last in-complete long-word first */
122 set = ~(~0UL << (n & (__NFDBITS-1)));
124 open_fds = current->files->open_fds->fds_bits+n;
129 if (!(set & ~*open_fds))
140 if (set & ~*open_fds)
149 max += n * __NFDBITS;
155 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
156 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
157 #define ISSET(i,m) (((i)&*(m)) != 0)
158 #define SET(i,m) (*(m) |= (i))
160 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
161 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
162 #define POLLEX_SET (POLLPRI)
164 int do_select(int n, fd_set_bits *fds, long *timeout)
166 poll_table table, *wait;
168 long __timeout = *timeout;
170 read_lock(¤t->files->file_lock);
171 retval = max_select_fd(n, fds);
172 read_unlock(¤t->files->file_lock);
178 poll_initwait(&table);
184 set_current_state(TASK_INTERRUPTIBLE);
185 for (i = 0 ; i < n; i++) {
186 unsigned long bit = BIT(i);
191 if (!(bit & BITS(fds, off)))
196 mask = DEFAULT_POLLMASK;
197 if (file->f_op && file->f_op->poll)
198 mask = file->f_op->poll(file, wait);
201 if ((mask & POLLIN_SET) && ISSET(bit, __IN(fds,off))) {
202 SET(bit, __RES_IN(fds,off));
206 if ((mask & POLLOUT_SET) && ISSET(bit, __OUT(fds,off))) {
207 SET(bit, __RES_OUT(fds,off));
211 if ((mask & POLLEX_SET) && ISSET(bit, __EX(fds,off))) {
212 SET(bit, __RES_EX(fds,off));
218 if (retval || !__timeout || signal_pending(current))
221 retval = table.error;
224 __timeout = schedule_timeout(__timeout);
226 current->state = TASK_RUNNING;
228 poll_freewait(&table);
231 * Up-to-date the caller timeout.
233 *timeout = __timeout;
237 static void *select_bits_alloc(int size)
239 return kmalloc(6 * size, GFP_KERNEL);
242 static void select_bits_free(void *bits, int size)
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)
264 int ret, size, max_fdset;
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 /* max_fdset can increase, so grab it once to avoid race */
290 max_fdset = current->files->max_fdset;
295 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
296 * since we used fdset we need to allocate memory in units of
301 bits = select_bits_alloc(size);
304 fds.in = (unsigned long *) bits;
305 fds.out = (unsigned long *) (bits + size);
306 fds.ex = (unsigned long *) (bits + 2*size);
307 fds.res_in = (unsigned long *) (bits + 3*size);
308 fds.res_out = (unsigned long *) (bits + 4*size);
309 fds.res_ex = (unsigned long *) (bits + 5*size);
311 if ((ret = get_fd_set(n, inp, fds.in)) ||
312 (ret = get_fd_set(n, outp, fds.out)) ||
313 (ret = get_fd_set(n, exp, fds.ex)))
315 zero_fd_set(n, fds.res_in);
316 zero_fd_set(n, fds.res_out);
317 zero_fd_set(n, fds.res_ex);
319 ret = do_select(n, &fds, &timeout);
321 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
322 time_t sec = 0, usec = 0;
326 usec *= (1000000/HZ);
328 put_user(sec, &tvp->tv_sec);
329 put_user(usec, &tvp->tv_usec);
335 ret = -ERESTARTNOHAND;
336 if (signal_pending(current))
341 set_fd_set(n, inp, fds.res_in);
342 set_fd_set(n, outp, fds.res_out);
343 set_fd_set(n, exp, fds.res_ex);
346 select_bits_free(bits, size);
351 #define POLLFD_PER_PAGE ((PAGE_SIZE) / sizeof(struct pollfd))
353 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
354 poll_table ** pwait, int *count)
358 for (i = 0; i < num; i++) {
367 struct file * file = fget(fd);
370 mask = DEFAULT_POLLMASK;
371 if (file->f_op && file->f_op->poll)
372 mask = file->f_op->poll(file, *pwait);
373 mask &= fdp->events | POLLERR | POLLHUP;
385 static int do_poll(unsigned int nfds, unsigned int nchunks, unsigned int nleft,
386 struct pollfd *fds[], poll_table *wait, long timeout)
389 poll_table* pt = wait;
394 set_current_state(TASK_INTERRUPTIBLE);
396 for (i=0; i < nchunks; i++)
397 do_pollfd(POLLFD_PER_PAGE, fds[i], &pt, &count);
399 do_pollfd(nleft, fds[nchunks], &pt, &count);
401 if (count || !timeout || signal_pending(current))
406 timeout = schedule_timeout(timeout);
408 current->state = TASK_RUNNING;
412 asmlinkage long sys_poll(struct pollfd * ufds, unsigned int nfds, long timeout)
414 int i, j, fdcount, err;
416 poll_table table, *wait;
419 /* Do a sanity check on nfds ... */
420 if (nfds > current->files->max_fdset && nfds > OPEN_MAX)
424 /* Careful about overflow in the intermediate values */
425 if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
426 timeout = (unsigned long)(timeout*HZ+999)/1000+1;
427 else /* Negative or overflow */
428 timeout = MAX_SCHEDULE_TIMEOUT;
431 poll_initwait(&table);
439 fds = (struct pollfd **)kmalloc(
440 (1 + (nfds - 1) / POLLFD_PER_PAGE) * sizeof(struct pollfd *),
448 while (nleft > POLLFD_PER_PAGE) { /* allocate complete PAGE_SIZE chunks */
449 fds[nchunks] = (struct pollfd *)__get_free_page(GFP_KERNEL);
450 if (fds[nchunks] == NULL)
453 nleft -= POLLFD_PER_PAGE;
455 if (nleft) { /* allocate last PAGE_SIZE chunk, only nleft elements used */
456 fds[nchunks] = (struct pollfd *)__get_free_page(GFP_KERNEL);
457 if (fds[nchunks] == NULL)
462 for (i=0; i < nchunks; i++)
463 if (copy_from_user(fds[i], ufds + i*POLLFD_PER_PAGE, PAGE_SIZE))
466 if (copy_from_user(fds[nchunks], ufds + nchunks*POLLFD_PER_PAGE,
467 nleft * sizeof(struct pollfd)))
471 fdcount = do_poll(nfds, nchunks, nleft, fds, wait, timeout);
473 /* OK, now copy the revents fields back to user space. */
474 for(i=0; i < nchunks; i++)
475 for (j=0; j < POLLFD_PER_PAGE; j++, ufds++)
476 __put_user((fds[i] + j)->revents, &ufds->revents);
478 for (j=0; j < nleft; j++, ufds++)
479 __put_user((fds[nchunks] + j)->revents, &ufds->revents);
482 if (!fdcount && signal_pending(current))
487 free_page((unsigned long)(fds[nchunks]));
489 for (i=0; i < nchunks; i++)
490 free_page((unsigned long)(fds[i]));
494 poll_freewait(&table);