linux-bk/fs/select.c
<<
>>
Prefs
   1/*
   2 * This file contains the procedures for the handling of select and poll
   3 *
   4 * Created for Linux based loosely upon Mathius Lattner's minix
   5 * patches by Peter MacDonald. Heavily edited by Linus.
   6 *
   7 *  4 February 1994
   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.
  11 *
  12 *  24 January 2000
  13 *     Changed sys_poll()/do_poll() to use PAGE_SIZE chunk-based allocation 
  14 *     of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian).
  15 */
  16
  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>
  22#include <linux/fs.h>
  23
  24#include <asm/uaccess.h>
  25
  26#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
  27#define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
  28
  29struct poll_table_entry {
  30        struct file * filp;
  31        wait_queue_t wait;
  32        wait_queue_head_t * wait_address;
  33};
  34
  35struct poll_table_page {
  36        struct poll_table_page * next;
  37        struct poll_table_entry * entry;
  38        struct poll_table_entry entries[0];
  39};
  40
  41#define POLL_TABLE_FULL(table) \
  42        ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
  43
  44/*
  45 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
  46 * I have rewritten this, taking some shortcuts: This code may not be easy to
  47 * follow, but it should be free of race-conditions, and it's practical. If you
  48 * understand what I'm doing here, then you understand how the linux
  49 * sleep/wakeup mechanism works.
  50 *
  51 * Two very simple procedures, poll_wait() and poll_freewait() make all the
  52 * work.  poll_wait() is an inline-function defined in <linux/poll.h>,
  53 * as all select/poll functions have to call it to add an entry to the
  54 * poll table.
  55 */
  56
  57void poll_freewait(poll_table* pt)
  58{
  59        struct poll_table_page * p = pt->table;
  60        while (p) {
  61                struct poll_table_entry * entry;
  62                struct poll_table_page *old;
  63
  64                entry = p->entry;
  65                do {
  66                        entry--;
  67                        remove_wait_queue(entry->wait_address,&entry->wait);
  68                        fput(entry->filp);
  69                } while (entry > p->entries);
  70                old = p;
  71                p = p->next;
  72                free_page((unsigned long) old);
  73        }
  74}
  75
  76void __pollwait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
  77{
  78        struct poll_table_page *table = p->table;
  79
  80        if (!table || POLL_TABLE_FULL(table)) {
  81                struct poll_table_page *new_table;
  82
  83                new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
  84                if (!new_table) {
  85                        p->error = -ENOMEM;
  86                        __set_current_state(TASK_RUNNING);
  87                        return;
  88                }
  89                new_table->entry = new_table->entries;
  90                new_table->next = table;
  91                p->table = new_table;
  92                table = new_table;
  93        }
  94
  95        /* Add a new entry */
  96        {
  97                struct poll_table_entry * entry = table->entry;
  98                table->entry = entry+1;
  99                get_file(filp);
 100                entry->filp = filp;
 101                entry->wait_address = wait_address;
 102                init_waitqueue_entry(&entry->wait, current);
 103                add_wait_queue(wait_address,&entry->wait);
 104        }
 105}
 106
 107#define __IN(fds, n)            (fds->in + n)
 108#define __OUT(fds, n)           (fds->out + n)
 109#define __EX(fds, n)            (fds->ex + n)
 110#define __RES_IN(fds, n)        (fds->res_in + n)
 111#define __RES_OUT(fds, n)       (fds->res_out + n)
 112#define __RES_EX(fds, n)        (fds->res_ex + n)
 113
 114#define BITS(fds, n)            (*__IN(fds, n)|*__OUT(fds, n)|*__EX(fds, n))
 115
 116static int max_select_fd(unsigned long n, fd_set_bits *fds)
 117{
 118        unsigned long *open_fds;
 119        unsigned long set;
 120        int max;
 121
 122        /* handle last in-complete long-word first */
 123        set = ~(~0UL << (n & (__NFDBITS-1)));
 124        n /= __NFDBITS;
 125        open_fds = current->files->open_fds->fds_bits+n;
 126        max = 0;
 127        if (set) {
 128                set &= BITS(fds, n);
 129                if (set) {
 130                        if (!(set & ~*open_fds))
 131                                goto get_max;
 132                        return -EBADF;
 133                }
 134        }
 135        while (n) {
 136                open_fds--;
 137                n--;
 138                set = BITS(fds, n);
 139                if (!set)
 140                        continue;
 141                if (set & ~*open_fds)
 142                        return -EBADF;
 143                if (max)
 144                        continue;
 145get_max:
 146                do {
 147                        max++;
 148                        set >>= 1;
 149                } while (set);
 150                max += n * __NFDBITS;
 151        }
 152
 153        return max;
 154}
 155
 156#define BIT(i)          (1UL << ((i)&(__NFDBITS-1)))
 157#define MEM(i,m)        ((m)+(unsigned)(i)/__NFDBITS)
 158#define ISSET(i,m)      (((i)&*(m)) != 0)
 159#define SET(i,m)        (*(m) |= (i))
 160
 161#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
 162#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
 163#define POLLEX_SET (POLLPRI)
 164
 165int do_select(int n, fd_set_bits *fds, long *timeout)
 166{
 167        poll_table table, *wait;
 168        int retval, i, off;
 169        long __timeout = *timeout;
 170
 171        read_lock(&current->files->file_lock);
 172        retval = max_select_fd(n, fds);
 173        read_unlock(&current->files->file_lock);
 174
 175        if (retval < 0)
 176                return retval;
 177        n = retval;
 178
 179        poll_initwait(&table);
 180        wait = &table;
 181        if (!__timeout)
 182                wait = NULL;
 183        retval = 0;
 184        for (;;) {
 185                set_current_state(TASK_INTERRUPTIBLE);
 186                for (i = 0 ; i < n; i++) {
 187                        unsigned long bit = BIT(i);
 188                        unsigned long mask;
 189                        struct file *file;
 190
 191                        off = i / __NFDBITS;
 192                        if (!(bit & BITS(fds, off)))
 193                                continue;
 194                        file = fget(i);
 195                        mask = POLLNVAL;
 196                        if (file) {
 197                                mask = DEFAULT_POLLMASK;
 198                                if (file->f_op && file->f_op->poll)
 199                                        mask = file->f_op->poll(file, wait);
 200                                fput(file);
 201                        }
 202                        if ((mask & POLLIN_SET) && ISSET(bit, __IN(fds,off))) {
 203                                SET(bit, __RES_IN(fds,off));
 204                                retval++;
 205                                wait = NULL;
 206                        }
 207                        if ((mask & POLLOUT_SET) && ISSET(bit, __OUT(fds,off))) {
 208                                SET(bit, __RES_OUT(fds,off));
 209                                retval++;
 210                                wait = NULL;
 211                        }
 212                        if ((mask & POLLEX_SET) && ISSET(bit, __EX(fds,off))) {
 213                                SET(bit, __RES_EX(fds,off));
 214                                retval++;
 215                                wait = NULL;
 216                        }
 217                }
 218                wait = NULL;
 219                if (retval || !__timeout || signal_pending(current))
 220                        break;
 221                if(table.error) {
 222                        retval = table.error;
 223                        break;
 224                }
 225                __timeout = schedule_timeout(__timeout);
 226        }
 227        current->state = TASK_RUNNING;
 228
 229        poll_freewait(&table);
 230
 231        /*
 232         * Up-to-date the caller timeout.
 233         */
 234        *timeout = __timeout;
 235        return retval;
 236}
 237
 238static void *select_bits_alloc(int size)
 239{
 240        return kmalloc(6 * size, GFP_KERNEL);
 241}
 242
 243static void select_bits_free(void *bits, int size)
 244{
 245        kfree(bits);
 246}
 247
 248/*
 249 * We can actually return ERESTARTSYS instead of EINTR, but I'd
 250 * like to be certain this leads to no problems. So I return
 251 * EINTR just for safety.
 252 *
 253 * Update: ERESTARTSYS breaks at least the xview clock binary, so
 254 * I'm trying ERESTARTNOHAND which restart only when you want to.
 255 */
 256#define MAX_SELECT_SECONDS \
 257        ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
 258
 259asmlinkage long
 260sys_select(int n, fd_set *inp, fd_set *outp, fd_set *exp, struct timeval *tvp)
 261{
 262        fd_set_bits fds;
 263        char *bits;
 264        long timeout;
 265        int ret, size, max_fdset;
 266
 267        timeout = MAX_SCHEDULE_TIMEOUT;
 268        if (tvp) {
 269                time_t sec, usec;
 270
 271                if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp)))
 272                    || (ret = __get_user(sec, &tvp->tv_sec))
 273                    || (ret = __get_user(usec, &tvp->tv_usec)))
 274                        goto out_nofds;
 275
 276                ret = -EINVAL;
 277                if (sec < 0 || usec < 0)
 278                        goto out_nofds;
 279
 280                if ((unsigned long) sec < MAX_SELECT_SECONDS) {
 281                        timeout = ROUND_UP(usec, 1000000/HZ);
 282                        timeout += sec * (unsigned long) HZ;
 283                }
 284        }
 285
 286        ret = -EINVAL;
 287        if (n < 0)
 288                goto out_nofds;
 289
 290        /* max_fdset can increase, so grab it once to avoid race */
 291        max_fdset = current->files->max_fdset;
 292        if (n > max_fdset)
 293                n = max_fdset;
 294
 295        /*
 296         * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
 297         * since we used fdset we need to allocate memory in units of
 298         * long-words. 
 299         */
 300        ret = -ENOMEM;
 301        size = FDS_BYTES(n);
 302        bits = select_bits_alloc(size);
 303        if (!bits)
 304                goto out_nofds;
 305        fds.in      = (unsigned long *)  bits;
 306        fds.out     = (unsigned long *) (bits +   size);
 307        fds.ex      = (unsigned long *) (bits + 2*size);
 308        fds.res_in  = (unsigned long *) (bits + 3*size);
 309        fds.res_out = (unsigned long *) (bits + 4*size);
 310        fds.res_ex  = (unsigned long *) (bits + 5*size);
 311
 312        if ((ret = get_fd_set(n, inp, fds.in)) ||
 313            (ret = get_fd_set(n, outp, fds.out)) ||
 314            (ret = get_fd_set(n, exp, fds.ex)))
 315                goto out;
 316        zero_fd_set(n, fds.res_in);
 317        zero_fd_set(n, fds.res_out);
 318        zero_fd_set(n, fds.res_ex);
 319
 320        ret = do_select(n, &fds, &timeout);
 321
 322        if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
 323                time_t sec = 0, usec = 0;
 324                if (timeout) {
 325                        sec = timeout / HZ;
 326                        usec = timeout % HZ;
 327                        usec *= (1000000/HZ);
 328                }
 329                put_user(sec, &tvp->tv_sec);
 330                put_user(usec, &tvp->tv_usec);
 331        }
 332
 333        if (ret < 0)
 334                goto out;
 335        if (!ret) {
 336                ret = -ERESTARTNOHAND;
 337                if (signal_pending(current))
 338                        goto out;
 339                ret = 0;
 340        }
 341
 342        set_fd_set(n, inp, fds.res_in);
 343        set_fd_set(n, outp, fds.res_out);
 344        set_fd_set(n, exp, fds.res_ex);
 345
 346out:
 347        select_bits_free(bits, size);
 348out_nofds:
 349        return ret;
 350}
 351
 352#define POLLFD_PER_PAGE  ((PAGE_SIZE) / sizeof(struct pollfd))
 353
 354static void do_pollfd(unsigned int num, struct pollfd * fdpage,
 355        poll_table ** pwait, int *count)
 356{
 357        int i;
 358
 359        for (i = 0; i < num; i++) {
 360                int fd;
 361                unsigned int mask;
 362                struct pollfd *fdp;
 363
 364                mask = 0;
 365                fdp = fdpage+i;
 366                fd = fdp->fd;
 367                if (fd >= 0) {
 368                        struct file * file = fget(fd);
 369                        mask = POLLNVAL;
 370                        if (file != NULL) {
 371                                mask = DEFAULT_POLLMASK;
 372                                if (file->f_op && file->f_op->poll)
 373                                        mask = file->f_op->poll(file, *pwait);
 374                                mask &= fdp->events | POLLERR | POLLHUP;
 375                                fput(file);
 376                        }
 377                        if (mask) {
 378                                *pwait = NULL;
 379                                (*count)++;
 380                        }
 381                }
 382                fdp->revents = mask;
 383        }
 384}
 385
 386static int do_poll(unsigned int nfds, unsigned int nchunks, unsigned int nleft, 
 387        struct pollfd *fds[], poll_table *wait, long timeout)
 388{
 389        int count;
 390        poll_table* pt = wait;
 391
 392        for (;;) {
 393                unsigned int i;
 394
 395                set_current_state(TASK_INTERRUPTIBLE);
 396                count = 0;
 397                for (i=0; i < nchunks; i++)
 398                        do_pollfd(POLLFD_PER_PAGE, fds[i], &pt, &count);
 399                if (nleft)
 400                        do_pollfd(nleft, fds[nchunks], &pt, &count);
 401                pt = NULL;
 402                if (count || !timeout || signal_pending(current))
 403                        break;
 404                count = wait->error;
 405                if (count)
 406                        break;
 407                timeout = schedule_timeout(timeout);
 408        }
 409        current->state = TASK_RUNNING;
 410        return count;
 411}
 412
 413asmlinkage long sys_poll(struct pollfd * ufds, unsigned int nfds, long timeout)
 414{
 415        int i, j, fdcount, err;
 416        struct pollfd **fds;
 417        poll_table table, *wait;
 418        int nchunks, nleft;
 419
 420        /* Do a sanity check on nfds ... */
 421        if (nfds > NR_OPEN)
 422                return -EINVAL;
 423
 424        if (timeout) {
 425                /* Careful about overflow in the intermediate values */
 426                if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
 427                        timeout = (unsigned long)(timeout*HZ+999)/1000+1;
 428                else /* Negative or overflow */
 429                        timeout = MAX_SCHEDULE_TIMEOUT;
 430        }
 431
 432        poll_initwait(&table);
 433        wait = &table;
 434        if (!timeout)
 435                wait = NULL;
 436
 437        err = -ENOMEM;
 438        fds = NULL;
 439        if (nfds != 0) {
 440                fds = (struct pollfd **)kmalloc(
 441                        (1 + (nfds - 1) / POLLFD_PER_PAGE) * sizeof(struct pollfd *),
 442                        GFP_KERNEL);
 443                if (fds == NULL)
 444                        goto out;
 445        }
 446
 447        nchunks = 0;
 448        nleft = nfds;
 449        while (nleft > POLLFD_PER_PAGE) { /* allocate complete PAGE_SIZE chunks */
 450                fds[nchunks] = (struct pollfd *)__get_free_page(GFP_KERNEL);
 451                if (fds[nchunks] == NULL)
 452                        goto out_fds;
 453                nchunks++;
 454                nleft -= POLLFD_PER_PAGE;
 455        }
 456        if (nleft) { /* allocate last PAGE_SIZE chunk, only nleft elements used */
 457                fds[nchunks] = (struct pollfd *)__get_free_page(GFP_KERNEL);
 458                if (fds[nchunks] == NULL)
 459                        goto out_fds;
 460        }
 461
 462        err = -EFAULT;
 463        for (i=0; i < nchunks; i++)
 464                if (copy_from_user(fds[i], ufds + i*POLLFD_PER_PAGE, PAGE_SIZE))
 465                        goto out_fds1;
 466        if (nleft) {
 467                if (copy_from_user(fds[nchunks], ufds + nchunks*POLLFD_PER_PAGE, 
 468                                nleft * sizeof(struct pollfd)))
 469                        goto out_fds1;
 470        }
 471
 472        fdcount = do_poll(nfds, nchunks, nleft, fds, wait, timeout);
 473
 474        /* OK, now copy the revents fields back to user space. */
 475        for(i=0; i < nchunks; i++)
 476                for (j=0; j < POLLFD_PER_PAGE; j++, ufds++)
 477                        __put_user((fds[i] + j)->revents, &ufds->revents);
 478        if (nleft)
 479                for (j=0; j < nleft; j++, ufds++)
 480                        __put_user((fds[nchunks] + j)->revents, &ufds->revents);
 481
 482        err = fdcount;
 483        if (!fdcount && signal_pending(current))
 484                err = -EINTR;
 485
 486out_fds1:
 487        if (nleft)
 488                free_page((unsigned long)(fds[nchunks]));
 489out_fds:
 490        for (i=0; i < nchunks; i++)
 491                free_page((unsigned long)(fds[i]));
 492        if (nfds != 0)
 493                kfree(fds);
 494out:
 495        poll_freewait(&table);
 496        return err;
 497}
 498
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.