ipc,sem: do not hold ipc lock more than necessary
[linux-2.6-microblaze.git] / ipc / sem.c
index 58d31f1..cd1093c 100644 (file)
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -204,13 +204,34 @@ static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
        return container_of(ipcp, struct sem_array, sem_perm);
 }
 
+static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
+{
+       struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
+
+       if (IS_ERR(ipcp))
+               return ERR_CAST(ipcp);
+
+       return container_of(ipcp, struct sem_array, sem_perm);
+}
+
 static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
                                                int id)
 {
        struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
 
        if (IS_ERR(ipcp))
-               return (struct sem_array *)ipcp;
+               return ERR_CAST(ipcp);
+
+       return container_of(ipcp, struct sem_array, sem_perm);
+}
+
+static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
+                                                       int id)
+{
+       struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
+
+       if (IS_ERR(ipcp))
+               return ERR_CAST(ipcp);
 
        return container_of(ipcp, struct sem_array, sem_perm);
 }
@@ -234,6 +255,16 @@ static inline void sem_putref(struct sem_array *sma)
        ipc_unlock(&(sma)->sem_perm);
 }
 
+/*
+ * Call inside the rcu read section.
+ */
+static inline void sem_getref(struct sem_array *sma)
+{
+       spin_lock(&(sma)->sem_perm.lock);
+       ipc_rcu_getref(sma);
+       ipc_unlock(&(sma)->sem_perm);
+}
+
 static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
 {
        ipc_rmid(&sem_ids(ns), &s->sem_perm);
@@ -799,7 +830,7 @@ static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in,
 }
 
 static int semctl_nolock(struct ipc_namespace *ns, int semid,
-                        int cmd, int version, union semun arg)
+                        int cmd, int version, void __user *p)
 {
        int err;
        struct sem_array *sma;
@@ -834,7 +865,7 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid,
                }
                max_id = ipc_get_maxid(&sem_ids(ns));
                up_read(&sem_ids(ns).rw_mutex);
-               if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo))) 
+               if (copy_to_user(p, &seminfo, sizeof(struct seminfo))) 
                        return -EFAULT;
                return (max_id < 0) ? 0: max_id;
        }
@@ -842,18 +873,25 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid,
        case SEM_STAT:
        {
                struct semid64_ds tbuf;
-               int id;
+               int id = 0;
+
+               memset(&tbuf, 0, sizeof(tbuf));
 
                if (cmd == SEM_STAT) {
-                       sma = sem_lock(ns, semid);
-                       if (IS_ERR(sma))
-                               return PTR_ERR(sma);
+                       rcu_read_lock();
+                       sma = sem_obtain_object(ns, semid);
+                       if (IS_ERR(sma)) {
+                               err = PTR_ERR(sma);
+                               goto out_unlock;
+                       }
                        id = sma->sem_perm.id;
                } else {
-                       sma = sem_lock_check(ns, semid);
-                       if (IS_ERR(sma))
-                               return PTR_ERR(sma);
-                       id = 0;
+                       rcu_read_lock();
+                       sma = sem_obtain_object_check(ns, semid);
+                       if (IS_ERR(sma)) {
+                               err = PTR_ERR(sma);
+                               goto out_unlock;
+                       }
                }
 
                err = -EACCES;
@@ -864,14 +902,12 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid,
                if (err)
                        goto out_unlock;
 
-               memset(&tbuf, 0, sizeof(tbuf));
-
                kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
                tbuf.sem_otime  = sma->sem_otime;
                tbuf.sem_ctime  = sma->sem_ctime;
                tbuf.sem_nsems  = sma->sem_nsems;
-               sem_unlock(sma);
-               if (copy_semid_to_user (arg.buf, &tbuf, version))
+               rcu_read_unlock();
+               if (copy_semid_to_user(p, &tbuf, version))
                        return -EFAULT;
                return id;
        }
@@ -879,20 +915,27 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid,
                return -EINVAL;
        }
 out_unlock:
-       sem_unlock(sma);
+       rcu_read_unlock();
        return err;
 }
 
-static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
-               int cmd, int version, union semun arg)
+static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
+               unsigned long arg)
 {
+       struct sem_undo *un;
        struct sem_array *sma;
        struct sem* curr;
        int err;
-       ushort fast_sem_io[SEMMSL_FAST];
-       ushort* sem_io = fast_sem_io;
        int nsems;
        struct list_head tasks;
+       int val;
+#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
+       /* big-endian 64bit */
+       val = arg >> 32;
+#else
+       /* 32bit or little-endian 64bit */
+       val = arg;
+#endif
 
        sma = sem_lock_check(ns, semid);
        if (IS_ERR(sma))
@@ -902,23 +945,82 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
        nsems = sma->sem_nsems;
 
        err = -EACCES;
-       if (ipcperms(ns, &sma->sem_perm,
-                       (cmd == SETVAL || cmd == SETALL) ? S_IWUGO : S_IRUGO))
+       if (ipcperms(ns, &sma->sem_perm, S_IWUGO))
                goto out_unlock;
 
-       err = security_sem_semctl(sma, cmd);
+       err = security_sem_semctl(sma, SETVAL);
        if (err)
                goto out_unlock;
 
+       err = -EINVAL;
+       if(semnum < 0 || semnum >= nsems)
+               goto out_unlock;
+
+       curr = &sma->sem_base[semnum];
+
+       err = -ERANGE;
+       if (val > SEMVMX || val < 0)
+               goto out_unlock;
+
+       assert_spin_locked(&sma->sem_perm.lock);
+       list_for_each_entry(un, &sma->list_id, list_id)
+               un->semadj[semnum] = 0;
+
+       curr->semval = val;
+       curr->sempid = task_tgid_vnr(current);
+       sma->sem_ctime = get_seconds();
+       /* maybe some queued-up processes were waiting for this */
+       do_smart_update(sma, NULL, 0, 0, &tasks);
+       err = 0;
+out_unlock:
+       sem_unlock(sma);
+       wake_up_sem_queue_do(&tasks);
+       return err;
+}
+
+static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
+               int cmd, void __user *p)
+{
+       struct sem_array *sma;
+       struct sem* curr;
+       int err, nsems;
+       ushort fast_sem_io[SEMMSL_FAST];
+       ushort* sem_io = fast_sem_io;
+       struct list_head tasks;
+
+       INIT_LIST_HEAD(&tasks);
+
+       rcu_read_lock();
+       sma = sem_obtain_object_check(ns, semid);
+       if (IS_ERR(sma)) {
+               rcu_read_unlock();
+               return PTR_ERR(sma);
+       }
+
+       nsems = sma->sem_nsems;
+
+       err = -EACCES;
+       if (ipcperms(ns, &sma->sem_perm,
+                       cmd == SETALL ? S_IWUGO : S_IRUGO)) {
+               rcu_read_unlock();
+               goto out_wakeup;
+       }
+
+       err = security_sem_semctl(sma, cmd);
+       if (err) {
+               rcu_read_unlock();
+               goto out_wakeup;
+       }
+
        err = -EACCES;
        switch (cmd) {
        case GETALL:
        {
-               ushort __user *array = arg.array;
+               ushort __user *array = p;
                int i;
 
                if(nsems > SEMMSL_FAST) {
-                       sem_getref_and_unlock(sma);
+                       sem_getref(sma);
 
                        sem_io = ipc_alloc(sizeof(ushort)*nsems);
                        if(sem_io == NULL) {
@@ -934,6 +1036,7 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
                        }
                }
 
+               spin_lock(&sma->sem_perm.lock);
                for (i = 0; i < sma->sem_nsems; i++)
                        sem_io[i] = sma->sem_base[i].semval;
                sem_unlock(sma);
@@ -947,7 +1050,8 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
                int i;
                struct sem_undo *un;
 
-               sem_getref_and_unlock(sma);
+               ipc_rcu_getref(sma);
+               rcu_read_unlock();
 
                if(nsems > SEMMSL_FAST) {
                        sem_io = ipc_alloc(sizeof(ushort)*nsems);
@@ -957,7 +1061,7 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
                        }
                }
 
-               if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
+               if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
                        sem_putref(sma);
                        err = -EFAULT;
                        goto out_free;
@@ -991,12 +1095,15 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
                err = 0;
                goto out_unlock;
        }
-       /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
+       /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
        }
        err = -EINVAL;
-       if(semnum < 0 || semnum >= nsems)
-               goto out_unlock;
+       if (semnum < 0 || semnum >= nsems) {
+               rcu_read_unlock();
+               goto out_wakeup;
+       }
 
+       spin_lock(&sma->sem_perm.lock);
        curr = &sma->sem_base[semnum];
 
        switch (cmd) {
@@ -1012,32 +1119,12 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
        case GETZCNT:
                err = count_semzcnt(sma,semnum);
                goto out_unlock;
-       case SETVAL:
-       {
-               int val = arg.val;
-               struct sem_undo *un;
-
-               err = -ERANGE;
-               if (val > SEMVMX || val < 0)
-                       goto out_unlock;
-
-               assert_spin_locked(&sma->sem_perm.lock);
-               list_for_each_entry(un, &sma->list_id, list_id)
-                       un->semadj[semnum] = 0;
-
-               curr->semval = val;
-               curr->sempid = task_tgid_vnr(current);
-               sma->sem_ctime = get_seconds();
-               /* maybe some queued-up processes were waiting for this */
-               do_smart_update(sma, NULL, 0, 0, &tasks);
-               err = 0;
-               goto out_unlock;
-       }
        }
+
 out_unlock:
        sem_unlock(sma);
+out_wakeup:
        wake_up_sem_queue_do(&tasks);
-
 out_free:
        if(sem_io != fast_sem_io)
                ipc_free(sem_io, sizeof(ushort)*nsems);
@@ -1076,7 +1163,7 @@ copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
  * NOTE: no locks must be held, the rw_mutex is taken inside this function.
  */
 static int semctl_down(struct ipc_namespace *ns, int semid,
-                      int cmd, int version, union semun arg)
+                      int cmd, int version, void __user *p)
 {
        struct sem_array *sma;
        int err;
@@ -1084,33 +1171,39 @@ static int semctl_down(struct ipc_namespace *ns, int semid,
        struct kern_ipc_perm *ipcp;
 
        if(cmd == IPC_SET) {
-               if (copy_semid_from_user(&semid64, arg.buf, version))
+               if (copy_semid_from_user(&semid64, p, version))
                        return -EFAULT;
        }
 
-       ipcp = ipcctl_pre_down(ns, &sem_ids(ns), semid, cmd,
-                              &semid64.sem_perm, 0);
+       ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
+                                     &semid64.sem_perm, 0);
        if (IS_ERR(ipcp))
                return PTR_ERR(ipcp);
 
        sma = container_of(ipcp, struct sem_array, sem_perm);
 
        err = security_sem_semctl(sma, cmd);
-       if (err)
+       if (err) {
+               rcu_read_unlock();
                goto out_unlock;
+       }
 
        switch(cmd){
        case IPC_RMID:
+               ipc_lock_object(&sma->sem_perm);
                freeary(ns, ipcp);
                goto out_up;
        case IPC_SET:
+               ipc_lock_object(&sma->sem_perm);
                err = ipc_update_perm(&semid64.sem_perm, ipcp);
                if (err)
                        goto out_unlock;
                sma->sem_ctime = get_seconds();
                break;
        default:
+               rcu_read_unlock();
                err = -EINVAL;
+               goto out_up;
        }
 
 out_unlock:
@@ -1120,11 +1213,11 @@ out_up:
        return err;
 }
 
-SYSCALL_DEFINE(semctl)(int semid, int semnum, int cmd, union semun arg)
+SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
 {
-       int err = -EINVAL;
        int version;
        struct ipc_namespace *ns;
+       void __user *p = (void __user *)arg;
 
        if (semid < 0)
                return -EINVAL;
@@ -1137,32 +1230,23 @@ SYSCALL_DEFINE(semctl)(int semid, int semnum, int cmd, union semun arg)
        case SEM_INFO:
        case IPC_STAT:
        case SEM_STAT:
-               err = semctl_nolock(ns, semid, cmd, version, arg);
-               return err;
+               return semctl_nolock(ns, semid, cmd, version, p);
        case GETALL:
        case GETVAL:
        case GETPID:
        case GETNCNT:
        case GETZCNT:
-       case SETVAL:
        case SETALL:
-               err = semctl_main(ns,semid,semnum,cmd,version,arg);
-               return err;
+               return semctl_main(ns, semid, semnum, cmd, p);
+       case SETVAL:
+               return semctl_setval(ns, semid, semnum, arg);
        case IPC_RMID:
        case IPC_SET:
-               err = semctl_down(ns, semid, cmd, version, arg);
-               return err;
+               return semctl_down(ns, semid, cmd, version, p);
        default:
                return -EINVAL;
        }
 }
-#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
-asmlinkage long SyS_semctl(int semid, int semnum, int cmd, union semun arg)
-{
-       return SYSC_semctl((int) semid, (int) semnum, (int) cmd, arg);
-}
-SYSCALL_ALIAS(sys_semctl, SyS_semctl);
-#endif
 
 /* If the task doesn't already have a undo_list, then allocate one
  * here.  We guarantee there is only one thread using this undo list,
@@ -1248,16 +1332,18 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
        spin_unlock(&ulp->lock);
        if (likely(un!=NULL))
                goto out;
-       rcu_read_unlock();
 
        /* no undo structure around - allocate one. */
        /* step 1: figure out the size of the semaphore array */
-       sma = sem_lock_check(ns, semid);
-       if (IS_ERR(sma))
+       sma = sem_obtain_object_check(ns, semid);
+       if (IS_ERR(sma)) {
+               rcu_read_unlock();
                return ERR_CAST(sma);
+       }
 
        nsems = sma->sem_nsems;
-       sem_getref_and_unlock(sma);
+       ipc_rcu_getref(sma);
+       rcu_read_unlock();
 
        /* step 2: allocate new undo structure */
        new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
@@ -1392,7 +1478,8 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
 
        INIT_LIST_HEAD(&tasks);
 
-       sma = sem_lock_check(ns, semid);
+       rcu_read_lock();
+       sma = sem_obtain_object_check(ns, semid);
        if (IS_ERR(sma)) {
                if (un)
                        rcu_read_unlock();
@@ -1400,6 +1487,24 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
                goto out_free;
        }
 
+       error = -EFBIG;
+       if (max >= sma->sem_nsems) {
+               rcu_read_unlock();
+               goto out_wakeup;
+       }
+
+       error = -EACCES;
+       if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
+               rcu_read_unlock();
+               goto out_wakeup;
+       }
+
+       error = security_sem_semop(sma, sops, nsops, alter);
+       if (error) {
+               rcu_read_unlock();
+               goto out_wakeup;
+       }
+
        /*
         * semid identifiers are not unique - find_alloc_undo may have
         * allocated an undo structure, it was invalidated by an RMID
@@ -1408,6 +1513,7 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
         * "un" itself is guaranteed by rcu.
         */
        error = -EIDRM;
+       ipc_lock_object(&sma->sem_perm);
        if (un) {
                if (un->semid == -1) {
                        rcu_read_unlock();
@@ -1425,18 +1531,6 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
                }
        }
 
-       error = -EFBIG;
-       if (max >= sma->sem_nsems)
-               goto out_unlock_free;
-
-       error = -EACCES;
-       if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
-               goto out_unlock_free;
-
-       error = security_sem_semop(sma, sops, nsops, alter);
-       if (error)
-               goto out_unlock_free;
-
        error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
        if (error <= 0) {
                if (alter && error == 0)
@@ -1539,7 +1633,7 @@ sleep_again:
 
 out_unlock_free:
        sem_unlock(sma);
-
+out_wakeup:
        wake_up_sem_queue_do(&tasks);
 out_free:
        if(sops != fast_sops)