drm: reorganise drm tree to be more future proof.
[safe/jmp/linux-2.6] / fs / select.c
index dcbc111..da0e882 100644 (file)
  *     of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian).
  */
 
+#include <linux/kernel.h>
 #include <linux/syscalls.h>
 #include <linux/module.h>
 #include <linux/slab.h>
-#include <linux/smp_lock.h>
 #include <linux/poll.h>
 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
 #include <linux/file.h>
+#include <linux/fdtable.h>
 #include <linux/fs.h>
 #include <linux/rcupdate.h>
 
 #include <asm/uaccess.h>
 
-#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
-#define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
-
 struct poll_table_page {
        struct poll_table_page * next;
        struct poll_table_entry * entry;
@@ -65,7 +63,7 @@ EXPORT_SYMBOL(poll_initwait);
 
 static void free_poll_entry(struct poll_table_entry *entry)
 {
-       remove_wait_queue(entry->wait_address,&entry->wait);
+       remove_wait_queue(entry->wait_address, &entry->wait);
        fput(entry->filp);
 }
 
@@ -129,7 +127,7 @@ static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
        entry->filp = filp;
        entry->wait_address = wait_address;
        init_waitqueue_entry(&entry->wait, current);
-       add_wait_queue(wait_address,&entry->wait);
+       add_wait_queue(wait_address, &entry->wait);
 }
 
 #define FDS_IN(fds, n)         (fds->in + n)
@@ -180,11 +178,6 @@ get_max:
        return max;
 }
 
-#define BIT(i)         (1UL << ((i)&(__NFDBITS-1)))
-#define MEM(i,m)       ((m)+(unsigned)(i)/__NFDBITS)
-#define ISSET(i,m)     (((i)&*(m)) != 0)
-#define SET(i,m)       (*(m) |= (i))
-
 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
 #define POLLEX_SET (POLLPRI)
@@ -256,7 +249,6 @@ int do_select(int n, fd_set_bits *fds, s64 *timeout)
                                                retval++;
                                        }
                                }
-                               cond_resched();
                        }
                        if (res_in)
                                *rinp = res_in;
@@ -264,11 +256,12 @@ int do_select(int n, fd_set_bits *fds, s64 *timeout)
                                *routp = res_out;
                        if (res_ex)
                                *rexp = res_ex;
+                       cond_resched();
                }
                wait = NULL;
                if (retval || !*timeout || signal_pending(current))
                        break;
-               if(table.error) {
+               if (table.error) {
                        retval = table.error;
                        break;
                }
@@ -306,12 +299,12 @@ int do_select(int n, fd_set_bits *fds, s64 *timeout)
 #define MAX_SELECT_SECONDS \
        ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
 
-static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
+int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
                           fd_set __user *exp, s64 *timeout)
 {
        fd_set_bits fds;
        void *bits;
-       int ret, max_fdset;
+       int ret, max_fds;
        unsigned int size;
        struct fdtable *fdt;
        /* Allocate small arguments on the stack to save memory and be faster */
@@ -321,13 +314,13 @@ static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
        if (n < 0)
                goto out_nofds;
 
-       /* max_fdset can increase, so grab it once to avoid race */
+       /* max_fds can increase, so grab it once to avoid race */
        rcu_read_lock();
        fdt = files_fdtable(current->files);
-       max_fdset = fdt->max_fdset;
+       max_fds = fdt->max_fds;
        rcu_read_unlock();
-       if (n > max_fdset)
-               n = max_fdset;
+       if (n > max_fds)
+               n = max_fds;
 
        /*
         * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
@@ -399,7 +392,7 @@ asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
                if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
                        timeout = -1;   /* infinite */
                else {
-                       timeout = ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
+                       timeout = DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
                        timeout += tv.tv_sec * HZ;
                }
        }
@@ -433,7 +426,7 @@ sticky:
        return ret;
 }
 
-#ifdef TIF_RESTORE_SIGMASK
+#ifdef HAVE_SET_RESTORE_SIGMASK
 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
                fd_set __user *exp, struct timespec __user *tsp,
                const sigset_t __user *sigmask, size_t sigsetsize)
@@ -454,7 +447,7 @@ asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
                if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
                        timeout = -1;   /* infinite */
                else {
-                       timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
+                       timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
                        timeout += ts.tv_sec * HZ;
                }
        }
@@ -506,7 +499,7 @@ sticky:
                if (sigmask) {
                        memcpy(&current->saved_sigmask, &sigsaved,
                                        sizeof(sigsaved));
-                       set_thread_flag(TIF_RESTORE_SIGMASK);
+                       set_restore_sigmask();
                }
        } else if (sigmask)
                sigprocmask(SIG_SETMASK, &sigsaved, NULL);
@@ -536,7 +529,7 @@ asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
 
        return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
 }
-#endif /* TIF_RESTORE_SIGMASK */
+#endif /* HAVE_SET_RESTORE_SIGMASK */
 
 struct poll_list {
        struct poll_list *next;
@@ -589,7 +582,7 @@ static int do_poll(unsigned int nfds,  struct poll_list *list,
        /* Optimise the no-wait case */
        if (!(*timeout))
                pt = NULL;
+
        for (;;) {
                struct poll_list *walk;
                long __timeout;
@@ -619,10 +612,12 @@ static int do_poll(unsigned int nfds,  struct poll_list *list,
                 * a poll_table to them on the next loop iteration.
                 */
                pt = NULL;
-               if (count || !*timeout || signal_pending(current))
-                       break;
-               count = wait->error;
-               if (count)
+               if (!count) {
+                       count = wait->error;
+                       if (signal_pending(current))
+                               count = -EINTR;
+               }
+               if (count || !*timeout)
                        break;
 
                if (*timeout < 0) {
@@ -654,93 +649,89 @@ static int do_poll(unsigned int nfds,  struct poll_list *list,
 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
 {
        struct poll_wqueues table;
-       int fdcount, err;
-       unsigned int i;
-       struct poll_list *head;
-       struct poll_list *walk;
+       int err = -EFAULT, fdcount, len, size;
        /* Allocate small arguments on the stack to save memory and be
           faster - use long to make sure the buffer is aligned properly
           on 64 bit archs to avoid unaligned access */
        long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
-       struct poll_list *stack_pp = NULL;
+       struct poll_list *const head = (struct poll_list *)stack_pps;
+       struct poll_list *walk = head;
+       unsigned long todo = nfds;
 
-       /* Do a sanity check on nfds ... */
        if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
                return -EINVAL;
 
-       poll_initwait(&table);
+       len = min_t(unsigned int, nfds, N_STACK_PPS);
+       for (;;) {
+               walk->next = NULL;
+               walk->len = len;
+               if (!len)
+                       break;
 
-       head = NULL;
-       walk = NULL;
-       i = nfds;
-       err = -ENOMEM;
-       while(i!=0) {
-               struct poll_list *pp;
-               int num, size;
-               if (stack_pp == NULL)
-                       num = N_STACK_PPS;
-               else
-                       num = POLLFD_PER_PAGE;
-               if (num > i)
-                       num = i;
-               size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
-               if (!stack_pp)
-                       stack_pp = pp = (struct poll_list *)stack_pps;
-               else {
-                       pp = kmalloc(size, GFP_KERNEL);
-                       if (!pp)
-                               goto out_fds;
-               }
-               pp->next=NULL;
-               pp->len = num;
-               if (head == NULL)
-                       head = pp;
-               else
-                       walk->next = pp;
+               if (copy_from_user(walk->entries, ufds + nfds-todo,
+                                       sizeof(struct pollfd) * walk->len))
+                       goto out_fds;
 
-               walk = pp;
-               if (copy_from_user(pp->entries, ufds + nfds-i, 
-                               sizeof(struct pollfd)*num)) {
-                       err = -EFAULT;
+               todo -= walk->len;
+               if (!todo)
+                       break;
+
+               len = min(todo, POLLFD_PER_PAGE);
+               size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
+               walk = walk->next = kmalloc(size, GFP_KERNEL);
+               if (!walk) {
+                       err = -ENOMEM;
                        goto out_fds;
                }
-               i -= pp->len;
        }
 
+       poll_initwait(&table);
        fdcount = do_poll(nfds, head, &table, timeout);
+       poll_freewait(&table);
 
-       /* OK, now copy the revents fields back to user space. */
-       walk = head;
-       err = -EFAULT;
-       while(walk != NULL) {
+       for (walk = head; walk; walk = walk->next) {
                struct pollfd *fds = walk->entries;
                int j;
 
-               for (j=0; j < walk->len; j++, ufds++) {
-                       if(__put_user(fds[j].revents, &ufds->revents))
+               for (j = 0; j < walk->len; j++, ufds++)
+                       if (__put_user(fds[j].revents, &ufds->revents))
                                goto out_fds;
-               }
-               walk = walk->next;
        }
+
        err = fdcount;
-       if (!fdcount && signal_pending(current))
-               err = -EINTR;
 out_fds:
-       walk = head;
-       while(walk!=NULL) {
-               struct poll_list *pp = walk->next;
-               if (walk != stack_pp)
-                       kfree(walk);
-               walk = pp;
+       walk = head->next;
+       while (walk) {
+               struct poll_list *pos = walk;
+               walk = walk->next;
+               kfree(pos);
        }
-       poll_freewait(&table);
+
        return err;
 }
 
+static long do_restart_poll(struct restart_block *restart_block)
+{
+       struct pollfd __user *ufds = (struct pollfd __user*)restart_block->arg0;
+       int nfds = restart_block->arg1;
+       s64 timeout = ((s64)restart_block->arg3<<32) | (s64)restart_block->arg2;
+       int ret;
+
+       ret = do_sys_poll(ufds, nfds, &timeout);
+       if (ret == -EINTR) {
+               restart_block->fn = do_restart_poll;
+               restart_block->arg2 = timeout & 0xFFFFFFFF;
+               restart_block->arg3 = (u64)timeout >> 32;
+               ret = -ERESTART_RESTARTBLOCK;
+       }
+       return ret;
+}
+
 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
                        long timeout_msecs)
 {
        s64 timeout_jiffies;
+       int ret;
 
        if (timeout_msecs > 0) {
 #if HZ > 1000
@@ -749,16 +740,27 @@ asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
                        timeout_jiffies = -1;
                else
 #endif
-                       timeout_jiffies = msecs_to_jiffies(timeout_msecs);
+                       timeout_jiffies = msecs_to_jiffies(timeout_msecs) + 1;
        } else {
                /* Infinite (< 0) or no (0) timeout */
                timeout_jiffies = timeout_msecs;
        }
 
-       return do_sys_poll(ufds, nfds, &timeout_jiffies);
+       ret = do_sys_poll(ufds, nfds, &timeout_jiffies);
+       if (ret == -EINTR) {
+               struct restart_block *restart_block;
+               restart_block = &current_thread_info()->restart_block;
+               restart_block->fn = do_restart_poll;
+               restart_block->arg0 = (unsigned long)ufds;
+               restart_block->arg1 = nfds;
+               restart_block->arg2 = timeout_jiffies & 0xFFFFFFFF;
+               restart_block->arg3 = (u64)timeout_jiffies >> 32;
+               ret = -ERESTART_RESTARTBLOCK;
+       }
+       return ret;
 }
 
-#ifdef TIF_RESTORE_SIGMASK
+#ifdef HAVE_SET_RESTORE_SIGMASK
 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
        struct timespec __user *tsp, const sigset_t __user *sigmask,
        size_t sigsetsize)
@@ -776,7 +778,7 @@ asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
                if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
                        timeout = -1;   /* infinite */
                else {
-                       timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
+                       timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
                        timeout += ts.tv_sec * HZ;
                }
        }
@@ -804,7 +806,7 @@ asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
                if (sigmask) {
                        memcpy(&current->saved_sigmask, &sigsaved,
                                        sizeof(sigsaved));
-                       set_thread_flag(TIF_RESTORE_SIGMASK);
+                       set_restore_sigmask();
                }
                ret = -ERESTARTNOHAND;
        } else if (sigmask)
@@ -838,4 +840,4 @@ asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
 
        return ret;
 }
-#endif /* TIF_RESTORE_SIGMASK */
+#endif /* HAVE_SET_RESTORE_SIGMASK */