input: misc: Changed sysfs permissions
[linux-2.6.git] / fs / select.c
index 87df51e..0baa0a3 100644 (file)
@@ -15,8 +15,9 @@
  */
 
 #include <linux/kernel.h>
+#include <linux/sched.h>
 #include <linux/syscalls.h>
-#include <linux/module.h>
+#include <linux/export.h>
 #include <linux/slab.h>
 #include <linux/poll.h>
 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
  * better solutions..
  */
 
+#define MAX_SLACK      (100 * NSEC_PER_MSEC)
+
 static long __estimate_accuracy(struct timespec *tv)
 {
        long slack;
        int divfactor = 1000;
 
+       if (tv->tv_sec < 0)
+               return 0;
+
        if (task_nice(current) > 0)
                divfactor = divfactor / 5;
 
+       if (tv->tv_sec > MAX_SLACK / (NSEC_PER_SEC/divfactor))
+               return MAX_SLACK;
+
        slack = tv->tv_nsec / divfactor;
        slack += tv->tv_sec * (NSEC_PER_SEC/divfactor);
 
-       if (slack > 100 * NSEC_PER_MSEC)
-               slack =  100 * NSEC_PER_MSEC;
+       if (slack > MAX_SLACK)
+               return MAX_SLACK;
 
-       if (slack < 0)
-               slack = 0;
        return slack;
 }
 
-static long estimate_accuracy(struct timespec *tv)
+long select_estimate_accuracy(struct timespec *tv)
 {
        unsigned long ret;
        struct timespec now;
@@ -109,11 +116,12 @@ static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
 void poll_initwait(struct poll_wqueues *pwq)
 {
        init_poll_funcptr(&pwq->pt, __pollwait);
+       pwq->polling_task = current;
+       pwq->triggered = 0;
        pwq->error = 0;
        pwq->table = NULL;
        pwq->inline_index = 0;
 }
-
 EXPORT_SYMBOL(poll_initwait);
 
 static void free_poll_entry(struct poll_table_entry *entry)
@@ -142,12 +150,10 @@ void poll_freewait(struct poll_wqueues *pwq)
                free_page((unsigned long) old);
        }
 }
-
 EXPORT_SYMBOL(poll_freewait);
 
-static struct poll_table_entry *poll_get_entry(poll_table *_p)
+static struct poll_table_entry *poll_get_entry(struct poll_wqueues *p)
 {
-       struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
        struct poll_table_page *table = p->table;
 
        if (p->inline_index < N_INLINE_POLL_ENTRIES)
@@ -159,7 +165,6 @@ static struct poll_table_entry *poll_get_entry(poll_table *_p)
                new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
                if (!new_table) {
                        p->error = -ENOMEM;
-                       __set_current_state(TASK_RUNNING);
                        return NULL;
                }
                new_table->entry = new_table->entries;
@@ -171,20 +176,86 @@ static struct poll_table_entry *poll_get_entry(poll_table *_p)
        return table->entry++;
 }
 
+static int __pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
+{
+       struct poll_wqueues *pwq = wait->private;
+       DECLARE_WAITQUEUE(dummy_wait, pwq->polling_task);
+
+       /*
+        * Although this function is called under waitqueue lock, LOCK
+        * doesn't imply write barrier and the users expect write
+        * barrier semantics on wakeup functions.  The following
+        * smp_wmb() is equivalent to smp_wmb() in try_to_wake_up()
+        * and is paired with set_mb() in poll_schedule_timeout.
+        */
+       smp_wmb();
+       pwq->triggered = 1;
+
+       /*
+        * Perform the default wake up operation using a dummy
+        * waitqueue.
+        *
+        * TODO: This is hacky but there currently is no interface to
+        * pass in @sync.  @sync is scheduled to be removed and once
+        * that happens, wake_up_process() can be used directly.
+        */
+       return default_wake_function(&dummy_wait, mode, sync, key);
+}
+
+static int pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
+{
+       struct poll_table_entry *entry;
+
+       entry = container_of(wait, struct poll_table_entry, wait);
+       if (key && !((unsigned long)key & entry->key))
+               return 0;
+       return __pollwake(wait, mode, sync, key);
+}
+
 /* Add a new entry */
 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
                                poll_table *p)
 {
-       struct poll_table_entry *entry = poll_get_entry(p);
+       struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);
+       struct poll_table_entry *entry = poll_get_entry(pwq);
        if (!entry)
                return;
        get_file(filp);
        entry->filp = filp;
        entry->wait_address = wait_address;
-       init_waitqueue_entry(&entry->wait, current);
+       entry->key = p->_key;
+       init_waitqueue_func_entry(&entry->wait, pollwake);
+       entry->wait.private = pwq;
        add_wait_queue(wait_address, &entry->wait);
 }
 
+int poll_schedule_timeout(struct poll_wqueues *pwq, int state,
+                         ktime_t *expires, unsigned long slack)
+{
+       int rc = -EINTR;
+
+       set_current_state(state);
+       if (!pwq->triggered)
+               rc = schedule_hrtimeout_range(expires, slack, HRTIMER_MODE_ABS);
+       __set_current_state(TASK_RUNNING);
+
+       /*
+        * Prepare for the next iteration.
+        *
+        * The following set_mb() serves two purposes.  First, it's
+        * the counterpart rmb of the wmb in pollwake() such that data
+        * written before wake up is always visible after wake up.
+        * Second, the full barrier guarantees that triggered clearing
+        * doesn't pass event check of the next iteration.  Note that
+        * this problem doesn't exist for the first iteration as
+        * add_wait_queue() has full barrier semantics.
+        */
+       set_mb(pwq->triggered, 0);
+
+       return rc;
+}
+EXPORT_SYMBOL(poll_schedule_timeout);
+
 /**
  * poll_select_set_timeout - helper function to setup the timeout value
  * @to:                pointer to timespec variable for the final timeout
@@ -235,6 +306,8 @@ static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
                rts.tv_sec = rts.tv_nsec = 0;
 
        if (timeval) {
+               if (sizeof(rtv) > sizeof(rtv.tv_sec) + sizeof(rtv.tv_usec))
+                       memset(&rtv, 0, sizeof(rtv));
                rtv.tv_sec = rts.tv_sec;
                rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
 
@@ -272,10 +345,10 @@ static int max_select_fd(unsigned long n, fd_set_bits *fds)
        struct fdtable *fdt;
 
        /* handle last in-complete long-word first */
-       set = ~(~0UL << (n & (__NFDBITS-1)));
-       n /= __NFDBITS;
+       set = ~(~0UL << (n & (BITS_PER_LONG-1)));
+       n /= BITS_PER_LONG;
        fdt = files_fdtable(current->files);
-       open_fds = fdt->open_fds->fds_bits+n;
+       open_fds = fdt->open_fds + n;
        max = 0;
        if (set) {
                set &= BITS(fds, n);
@@ -300,7 +373,7 @@ get_max:
                        max++;
                        set >>= 1;
                } while (set);
-               max += n * __NFDBITS;
+               max += n * BITS_PER_LONG;
        }
 
        return max;
@@ -310,6 +383,16 @@ get_max:
 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
 #define POLLEX_SET (POLLPRI)
 
+static inline void wait_key_set(poll_table *wait, unsigned long in,
+                               unsigned long out, unsigned long bit)
+{
+       wait->_key = POLLEX_SET;
+       if (in & bit)
+               wait->_key |= POLLIN_SET;
+       if (out & bit)
+               wait->_key |= POLLOUT_SET;
+}
+
 int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
 {
        ktime_t expire, *to = NULL;
@@ -329,19 +412,17 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
        poll_initwait(&table);
        wait = &table.pt;
        if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
-               wait = NULL;
+               wait->_qproc = NULL;
                timed_out = 1;
        }
 
        if (end_time && !timed_out)
-               slack = estimate_accuracy(end_time);
+               slack = select_estimate_accuracy(end_time);
 
        retval = 0;
        for (;;) {
                unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
 
-               set_current_state(TASK_INTERRUPTIBLE);
-
                inp = fds->in; outp = fds->out; exp = fds->ex;
                rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
 
@@ -354,11 +435,11 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
                        in = *inp++; out = *outp++; ex = *exp++;
                        all_bits = in | out | ex;
                        if (all_bits == 0) {
-                               i += __NFDBITS;
+                               i += BITS_PER_LONG;
                                continue;
                        }
 
-                       for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
+                       for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
                                int fput_needed;
                                if (i >= n)
                                        break;
@@ -368,20 +449,25 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
                                if (file) {
                                        f_op = file->f_op;
                                        mask = DEFAULT_POLLMASK;
-                                       if (f_op && f_op->poll)
-                                               mask = (*f_op->poll)(file, retval ? NULL : wait);
+                                       if (f_op && f_op->poll) {
+                                               wait_key_set(wait, in, out, bit);
+                                               mask = (*f_op->poll)(file, wait);
+                                       }
                                        fput_light(file, fput_needed);
                                        if ((mask & POLLIN_SET) && (in & bit)) {
                                                res_in |= bit;
                                                retval++;
+                                               wait->_qproc = NULL;
                                        }
                                        if ((mask & POLLOUT_SET) && (out & bit)) {
                                                res_out |= bit;
                                                retval++;
+                                               wait->_qproc = NULL;
                                        }
                                        if ((mask & POLLEX_SET) && (ex & bit)) {
                                                res_ex |= bit;
                                                retval++;
+                                               wait->_qproc = NULL;
                                        }
                                }
                        }
@@ -393,7 +479,7 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
                                *rexp = res_ex;
                        cond_resched();
                }
-               wait = NULL;
+               wait->_qproc = NULL;
                if (retval || timed_out || signal_pending(current))
                        break;
                if (table.error) {
@@ -411,10 +497,10 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
                        to = &expire;
                }
 
-               if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS))
+               if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
+                                          to, slack))
                        timed_out = 1;
        }
-       __set_current_state(TASK_RUNNING);
 
        poll_freewait(&table);
 
@@ -429,9 +515,6 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
  * Update: ERESTARTSYS breaks at least the xview clock binary, so
  * I'm trying ERESTARTNOHAND which restart only when you want to.
  */
-#define MAX_SELECT_SECONDS \
-       ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
-
 int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
                           fd_set __user *exp, struct timespec *end_time)
 {
@@ -507,8 +590,8 @@ out_nofds:
        return ret;
 }
 
-asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
-                       fd_set __user *exp, struct timeval __user *tvp)
+SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
+               fd_set __user *, exp, struct timeval __user *, tvp)
 {
        struct timespec end_time, *to = NULL;
        struct timeval tv;
@@ -532,9 +615,9 @@ asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
 }
 
 #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)
+static long do_pselect(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)
 {
        sigset_t ksigmask, sigsaved;
        struct timespec ts, end_time, *to = NULL;
@@ -560,7 +643,7 @@ asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
                sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
        }
 
-       ret = core_sys_select(n, inp, outp, exp, &end_time);
+       ret = core_sys_select(n, inp, outp, exp, to);
        ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
 
        if (ret == -ERESTARTNOHAND) {
@@ -586,8 +669,9 @@ asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
  * which has a pointer to the sigset_t itself followed by a size_t containing
  * the sigset size.
  */
-asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
-       fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
+SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
+               fd_set __user *, exp, struct timespec __user *, tsp,
+               void __user *, sig)
 {
        size_t sigsetsize = 0;
        sigset_t __user *up = NULL;
@@ -600,10 +684,27 @@ asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
                        return -EFAULT;
        }
 
-       return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
+       return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
 }
 #endif /* HAVE_SET_RESTORE_SIGMASK */
 
+#ifdef __ARCH_WANT_SYS_OLD_SELECT
+struct sel_arg_struct {
+       unsigned long n;
+       fd_set __user *inp, *outp, *exp;
+       struct timeval __user *tvp;
+};
+
+SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
+{
+       struct sel_arg_struct a;
+
+       if (copy_from_user(&a, arg, sizeof(a)))
+               return -EFAULT;
+       return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
+}
+#endif
+
 struct poll_list {
        struct poll_list *next;
        int len;
@@ -617,7 +718,7 @@ struct poll_list {
  * interested in events matching the pollfd->events mask, and the result
  * matching that mask is both recorded in pollfd->revents and returned. The
  * pwait poll_table will be used by the fd-provided poll handler for waiting,
- * if non-NULL.
+ * if pwait->_qproc is non-NULL.
  */
 static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
 {
@@ -634,8 +735,10 @@ static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
                mask = POLLNVAL;
                if (file != NULL) {
                        mask = DEFAULT_POLLMASK;
-                       if (file->f_op && file->f_op->poll)
+                       if (file->f_op && file->f_op->poll) {
+                               pwait->_key = pollfd->events|POLLERR|POLLHUP;
                                mask = file->f_op->poll(file, pwait);
+                       }
                        /* Mask out unneeded events. */
                        mask &= pollfd->events | POLLERR | POLLHUP;
                        fput_light(file, fput_needed);
@@ -656,17 +759,16 @@ static int do_poll(unsigned int nfds,  struct poll_list *list,
 
        /* Optimise the no-wait case */
        if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
-               pt = NULL;
+               pt->_qproc = NULL;
                timed_out = 1;
        }
 
        if (end_time && !timed_out)
-               slack = estimate_accuracy(end_time);
+               slack = select_estimate_accuracy(end_time);
 
        for (;;) {
                struct poll_list *walk;
 
-               set_current_state(TASK_INTERRUPTIBLE);
                for (walk = list; walk != NULL; walk = walk->next) {
                        struct pollfd * pfd, * pfd_end;
 
@@ -675,22 +777,22 @@ static int do_poll(unsigned int nfds,  struct poll_list *list,
                        for (; pfd != pfd_end; pfd++) {
                                /*
                                 * Fish for events. If we found one, record it
-                                * and kill the poll_table, so we don't
+                                * and kill poll_table->_qproc, so we don't
                                 * needlessly register any other waiters after
                                 * this. They'll get immediately deregistered
                                 * when we break out and return.
                                 */
                                if (do_pollfd(pfd, pt)) {
                                        count++;
-                                       pt = NULL;
+                                       pt->_qproc = NULL;
                                }
                        }
                }
                /*
                 * All waiters have already been registered, so don't provide
-                * a poll_table to them on the next loop iteration.
+                * a poll_table->_qproc to them on the next loop iteration.
                 */
-               pt = NULL;
+               pt->_qproc = NULL;
                if (!count) {
                        count = wait->error;
                        if (signal_pending(current))
@@ -709,10 +811,9 @@ static int do_poll(unsigned int nfds,  struct poll_list *list,
                        to = &expire;
                }
 
-               if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS))
+               if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
                        timed_out = 1;
        }
-       __set_current_state(TASK_RUNNING);
        return count;
 }
 
@@ -732,7 +833,7 @@ int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
        struct poll_list *walk = head;
        unsigned long todo = nfds;
 
-       if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
+       if (nfds > rlimit(RLIMIT_NOFILE))
                return -EINVAL;
 
        len = min_t(unsigned int, nfds, N_STACK_PPS);
@@ -806,8 +907,8 @@ static long do_restart_poll(struct restart_block *restart_block)
        return ret;
 }
 
-asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
-                       long timeout_msecs)
+SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
+               int, timeout_msecs)
 {
        struct timespec end_time, *to = NULL;
        int ret;
@@ -841,9 +942,9 @@ asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 }
 
 #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)
+SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
+               struct timespec __user *, tsp, const sigset_t __user *, sigmask,
+               size_t, sigsetsize)
 {
        sigset_t ksigmask, sigsaved;
        struct timespec ts, end_time, *to = NULL;