Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[linux-2.6-microblaze.git] / ipc / util.c
index 465bbd2..0af0575 100644 (file)
@@ -88,16 +88,12 @@ struct ipc_proc_iface {
  */
 static int __init ipc_init(void)
 {
-       int err_sem, err_msg;
-
        proc_mkdir("sysvipc", NULL);
-       err_sem = sem_init();
-       WARN(err_sem, "ipc: sysv sem_init failed: %d\n", err_sem);
-       err_msg = msg_init();
-       WARN(err_msg, "ipc: sysv msg_init failed: %d\n", err_msg);
+       sem_init();
+       msg_init();
        shm_init();
 
-       return err_msg ? err_msg : err_sem;
+       return 0;
 }
 device_initcall(ipc_init);
 
@@ -116,22 +112,17 @@ static const struct rhashtable_params ipc_kht_params = {
  * Set up the sequence range to use for the ipc identifier range (limited
  * below IPCMNI) then initialise the keys hashtable and ids idr.
  */
-int ipc_init_ids(struct ipc_ids *ids)
+void ipc_init_ids(struct ipc_ids *ids)
 {
-       int err;
        ids->in_use = 0;
        ids->seq = 0;
        init_rwsem(&ids->rwsem);
-       err = rhashtable_init(&ids->key_ht, &ipc_kht_params);
-       if (err)
-               return err;
+       rhashtable_init(&ids->key_ht, &ipc_kht_params);
        idr_init(&ids->ipcs_idr);
-       ids->tables_initialized = true;
-       ids->max_id = -1;
+       ids->max_idx = -1;
 #ifdef CONFIG_CHECKPOINT_RESTORE
        ids->next_id = -1;
 #endif
-       return 0;
 }
 
 #ifdef CONFIG_PROC_FS
@@ -179,19 +170,16 @@ void __init ipc_init_proc_interface(const char *path, const char *header,
  */
 static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
 {
-       struct kern_ipc_perm *ipcp = NULL;
+       struct kern_ipc_perm *ipcp;
 
-       if (likely(ids->tables_initialized))
-               ipcp = rhashtable_lookup_fast(&ids->key_ht, &key,
+       ipcp = rhashtable_lookup_fast(&ids->key_ht, &key,
                                              ipc_kht_params);
+       if (!ipcp)
+               return NULL;
 
-       if (ipcp) {
-               rcu_read_lock();
-               ipc_lock_object(ipcp);
-               return ipcp;
-       }
-
-       return NULL;
+       rcu_read_lock();
+       ipc_lock_object(ipcp);
+       return ipcp;
 }
 
 /*
@@ -249,7 +237,7 @@ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new)
  * @limit: limit for the number of used ids
  *
  * Add an entry 'new' to the ipc ids idr. The permissions object is
- * initialised and the first free entry is set up and the id assigned
+ * initialised and the first free entry is set up and the index assigned
  * is returned. The 'new' entry is returned in a locked state on success.
  *
  * On failure the entry is not locked and a negative err-code is returned.
@@ -269,7 +257,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
        if (limit > IPCMNI)
                limit = IPCMNI;
 
-       if (!ids->tables_initialized || ids->in_use >= limit)
+       if (ids->in_use >= limit)
                return -ENOSPC;
 
        idr_preload(GFP_KERNEL);
@@ -303,8 +291,8 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
        }
 
        ids->in_use++;
-       if (idx > ids->max_id)
-               ids->max_id = idx;
+       if (idx > ids->max_idx)
+               ids->max_idx = idx;
        return idx;
 }
 
@@ -443,20 +431,20 @@ static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  */
 void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
 {
-       int lid = ipcid_to_idx(ipcp->id);
+       int idx = ipcid_to_idx(ipcp->id);
 
-       idr_remove(&ids->ipcs_idr, lid);
+       idr_remove(&ids->ipcs_idr, idx);
        ipc_kht_remove(ids, ipcp);
        ids->in_use--;
        ipcp->deleted = true;
 
-       if (unlikely(lid == ids->max_id)) {
+       if (unlikely(idx == ids->max_idx)) {
                do {
-                       lid--;
-                       if (lid == -1)
+                       idx--;
+                       if (idx == -1)
                                break;
-               } while (!idr_find(&ids->ipcs_idr, lid));
-               ids->max_id = lid;
+               } while (!idr_find(&ids->ipcs_idr, idx));
+               ids->max_idx = idx;
        }
 }
 
@@ -474,7 +462,7 @@ void ipc_set_key_private(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
        ipcp->key = IPC_PRIVATE;
 }
 
-int ipc_rcu_getref(struct kern_ipc_perm *ptr)
+bool ipc_rcu_getref(struct kern_ipc_perm *ptr)
 {
        return refcount_inc_not_zero(&ptr->refcount);
 }
@@ -576,61 +564,22 @@ void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
 struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
 {
        struct kern_ipc_perm *out;
-       int lid = ipcid_to_idx(id);
-
-       if (unlikely(!ids->tables_initialized))
-               return ERR_PTR(-EINVAL);
+       int idx = ipcid_to_idx(id);
 
-       out = idr_find(&ids->ipcs_idr, lid);
+       out = idr_find(&ids->ipcs_idr, idx);
        if (!out)
                return ERR_PTR(-EINVAL);
 
        return out;
 }
 
-/**
- * ipc_lock - lock an ipc structure without rwsem held
- * @ids: ipc identifier set
- * @id: ipc id to look for
- *
- * Look for an id in the ipc ids idr and lock the associated ipc object.
- *
- * The ipc object is locked on successful exit.
- */
-struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
-{
-       struct kern_ipc_perm *out;
-
-       rcu_read_lock();
-       out = ipc_obtain_object_idr(ids, id);
-       if (IS_ERR(out))
-               goto err;
-
-       spin_lock(&out->lock);
-
-       /*
-        * ipc_rmid() may have already freed the ID while ipc_lock()
-        * was spinning: here verify that the structure is still valid.
-        * Upon races with RMID, return -EIDRM, thus indicating that
-        * the ID points to a removed identifier.
-        */
-       if (ipc_valid_object(out))
-               return out;
-
-       spin_unlock(&out->lock);
-       out = ERR_PTR(-EIDRM);
-err:
-       rcu_read_unlock();
-       return out;
-}
-
 /**
  * ipc_obtain_object_check
  * @ids: ipc identifier set
  * @id: ipc id to look for
  *
- * Similar to ipc_obtain_object_idr() but also checks
- * the ipc object reference counter.
+ * Similar to ipc_obtain_object_idr() but also checks the ipc object
+ * sequence number.
  *
  * Call inside the RCU critical section.
  * The ipc object is *not* locked on exit.
@@ -688,7 +637,7 @@ int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
 }
 
 /**
- * ipcctl_pre_down_nolock - retrieve an ipc and check permissions for some IPC_XXX cmd
+ * ipcctl_obtain_check - retrieve an ipc object and check permissions
  * @ns:  ipc namespace
  * @ids:  the table of ids where to look for the ipc
  * @id:   the id of the ipc to retrieve
@@ -698,16 +647,16 @@ int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
  *
  * This function does some common audit and permissions check for some IPC_XXX
  * cmd and is called from semctl_down, shmctl_down and msgctl_down.
- * It must be called without any lock held and:
  *
- *   - retrieves the ipc with the given id in the given table.
+ * It:
+ *   - retrieves the ipc object with the given id in the given table.
  *   - performs some audit and permission check, depending on the given cmd
  *   - returns a pointer to the ipc object or otherwise, the corresponding
  *     error.
  *
  * Call holding the both the rwsem and the rcu read lock.
  */
-struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
+struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns,
                                        struct ipc_ids *ids, int id, int cmd,
                                        struct ipc64_perm *perm, int extra_perm)
 {