linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / drivers / infiniband / core / uverbs_cmd.c
index 30923eb..407b628 100644 (file)
@@ -1,8 +1,7 @@
 /*
  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
- * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
+ * Copyright (c) 2005 Cisco Systems.  All rights reserved.
  * Copyright (c) 2005 PathScale, Inc.  All rights reserved.
- * Copyright (c) 2006 Mellanox Technologies.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
 
 #include "uverbs.h"
 
-static struct lock_class_key pd_lock_key;
-static struct lock_class_key mr_lock_key;
-static struct lock_class_key cq_lock_key;
-static struct lock_class_key qp_lock_key;
-static struct lock_class_key ah_lock_key;
-static struct lock_class_key srq_lock_key;
-
 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen)                      \
        do {                                                            \
                (udata)->inbuf  = (void __user *) (ibuf);               \
@@ -57,197 +49,6 @@ static struct lock_class_key srq_lock_key;
                (udata)->outlen = (olen);                               \
        } while (0)
 
-/*
- * The ib_uobject locking scheme is as follows:
- *
- * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
- *   needs to be held during all idr operations.  When an object is
- *   looked up, a reference must be taken on the object's kref before
- *   dropping this lock.
- *
- * - Each object also has an rwsem.  This rwsem must be held for
- *   reading while an operation that uses the object is performed.
- *   For example, while registering an MR, the associated PD's
- *   uobject.mutex must be held for reading.  The rwsem must be held
- *   for writing while initializing or destroying an object.
- *
- * - In addition, each object has a "live" flag.  If this flag is not
- *   set, then lookups of the object will fail even if it is found in
- *   the idr.  This handles a reader that blocks and does not acquire
- *   the rwsem until after the object is destroyed.  The destroy
- *   operation will set the live flag to 0 and then drop the rwsem;
- *   this will allow the reader to acquire the rwsem, see that the
- *   live flag is 0, and then drop the rwsem and its reference to
- *   object.  The underlying storage will not be freed until the last
- *   reference to the object is dropped.
- */
-
-static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
-                     struct ib_ucontext *context, struct lock_class_key *key)
-{
-       uobj->user_handle = user_handle;
-       uobj->context     = context;
-       kref_init(&uobj->ref);
-       init_rwsem(&uobj->mutex);
-       lockdep_set_class(&uobj->mutex, key);
-       uobj->live        = 0;
-}
-
-static void release_uobj(struct kref *kref)
-{
-       kfree(container_of(kref, struct ib_uobject, ref));
-}
-
-static void put_uobj(struct ib_uobject *uobj)
-{
-       kref_put(&uobj->ref, release_uobj);
-}
-
-static void put_uobj_read(struct ib_uobject *uobj)
-{
-       up_read(&uobj->mutex);
-       put_uobj(uobj);
-}
-
-static void put_uobj_write(struct ib_uobject *uobj)
-{
-       up_write(&uobj->mutex);
-       put_uobj(uobj);
-}
-
-static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
-{
-       int ret;
-
-retry:
-       if (!idr_pre_get(idr, GFP_KERNEL))
-               return -ENOMEM;
-
-       spin_lock(&ib_uverbs_idr_lock);
-       ret = idr_get_new(idr, uobj, &uobj->id);
-       spin_unlock(&ib_uverbs_idr_lock);
-
-       if (ret == -EAGAIN)
-               goto retry;
-
-       return ret;
-}
-
-void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
-{
-       spin_lock(&ib_uverbs_idr_lock);
-       idr_remove(idr, uobj->id);
-       spin_unlock(&ib_uverbs_idr_lock);
-}
-
-static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
-                                        struct ib_ucontext *context)
-{
-       struct ib_uobject *uobj;
-
-       spin_lock(&ib_uverbs_idr_lock);
-       uobj = idr_find(idr, id);
-       if (uobj)
-               kref_get(&uobj->ref);
-       spin_unlock(&ib_uverbs_idr_lock);
-
-       return uobj;
-}
-
-static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
-                                       struct ib_ucontext *context)
-{
-       struct ib_uobject *uobj;
-
-       uobj = __idr_get_uobj(idr, id, context);
-       if (!uobj)
-               return NULL;
-
-       down_read(&uobj->mutex);
-       if (!uobj->live) {
-               put_uobj_read(uobj);
-               return NULL;
-       }
-
-       return uobj;
-}
-
-static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
-                                        struct ib_ucontext *context)
-{
-       struct ib_uobject *uobj;
-
-       uobj = __idr_get_uobj(idr, id, context);
-       if (!uobj)
-               return NULL;
-
-       down_write(&uobj->mutex);
-       if (!uobj->live) {
-               put_uobj_write(uobj);
-               return NULL;
-       }
-
-       return uobj;
-}
-
-static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context)
-{
-       struct ib_uobject *uobj;
-
-       uobj = idr_read_uobj(idr, id, context);
-       return uobj ? uobj->object : NULL;
-}
-
-static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
-{
-       return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context);
-}
-
-static void put_pd_read(struct ib_pd *pd)
-{
-       put_uobj_read(pd->uobject);
-}
-
-static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context)
-{
-       return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context);
-}
-
-static void put_cq_read(struct ib_cq *cq)
-{
-       put_uobj_read(cq->uobject);
-}
-
-static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
-{
-       return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context);
-}
-
-static void put_ah_read(struct ib_ah *ah)
-{
-       put_uobj_read(ah->uobject);
-}
-
-static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
-{
-       return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context);
-}
-
-static void put_qp_read(struct ib_qp *qp)
-{
-       put_uobj_read(qp->uobject);
-}
-
-static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
-{
-       return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context);
-}
-
-static void put_srq_read(struct ib_srq *srq)
-{
-       put_uobj_read(srq->uobject);
-}
-
 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
                              const char __user *buf,
                              int in_len, int out_len)
@@ -278,10 +79,8 @@ ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
                   in_len - sizeof cmd, out_len - sizeof resp);
 
        ucontext = ibdev->alloc_ucontext(ibdev, &udata);
-       if (IS_ERR(ucontext)) {
-               ret = PTR_ERR(file->ucontext);
-               goto err;
-       }
+       if (IS_ERR(ucontext))
+               return PTR_ERR(file->ucontext);
 
        ucontext->device = ibdev;
        INIT_LIST_HEAD(&ucontext->pd_list);
@@ -478,8 +277,7 @@ ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
        if (!uobj)
                return -ENOMEM;
 
-       init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
-       down_write(&uobj->mutex);
+       uobj->context = file->ucontext;
 
        pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
                                            file->ucontext, &udata);
@@ -492,10 +290,20 @@ ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
        pd->uobject = uobj;
        atomic_set(&pd->usecnt, 0);
 
-       uobj->object = pd;
-       ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+retry:
+       if (!idr_pre_get(&ib_uverbs_pd_idr, GFP_KERNEL)) {
+               ret = -ENOMEM;
+               goto err_up;
+       }
+
+       ret = idr_get_new(&ib_uverbs_pd_idr, pd, &uobj->id);
+
+       if (ret == -EAGAIN)
+               goto retry;
        if (ret)
-               goto err_idr;
+               goto err_up;
 
        memset(&resp, 0, sizeof resp);
        resp.pd_handle = uobj->id;
@@ -503,27 +311,26 @@ ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_copy;
+               goto err_idr;
        }
 
        mutex_lock(&file->mutex);
        list_add_tail(&uobj->list, &file->ucontext->pd_list);
        mutex_unlock(&file->mutex);
 
-       uobj->live = 1;
-
-       up_write(&uobj->mutex);
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        return in_len;
 
-err_copy:
-       idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
-
 err_idr:
+       idr_remove(&ib_uverbs_pd_idr, uobj->id);
+
+err_up:
+       mutex_unlock(&ib_uverbs_idr_mutex);
        ib_dealloc_pd(pd);
 
 err:
-       put_uobj_write(uobj);
+       kfree(uobj);
        return ret;
 }
 
@@ -532,34 +339,37 @@ ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
                             int in_len, int out_len)
 {
        struct ib_uverbs_dealloc_pd cmd;
+       struct ib_pd               *pd;
        struct ib_uobject          *uobj;
-       int                         ret;
+       int                         ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
-       if (!uobj)
-               return -EINVAL;
+       mutex_lock(&ib_uverbs_idr_mutex);
 
-       ret = ib_dealloc_pd(uobj->object);
-       if (!ret)
-               uobj->live = 0;
+       pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
+       if (!pd || pd->uobject->context != file->ucontext)
+               goto out;
 
-       put_uobj_write(uobj);
+       uobj = pd->uobject;
 
+       ret = ib_dealloc_pd(pd);
        if (ret)
-               return ret;
+               goto out;
 
-       idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
+       idr_remove(&ib_uverbs_pd_idr, cmd.pd_handle);
 
        mutex_lock(&file->mutex);
        list_del(&uobj->list);
        mutex_unlock(&file->mutex);
 
-       put_uobj(uobj);
+       kfree(uobj);
 
-       return in_len;
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
+
+       return ret ? ret : in_len;
 }
 
 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
@@ -599,8 +409,7 @@ ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
        if (!obj)
                return -ENOMEM;
 
-       init_uobj(&obj->uobject, 0, file->ucontext, &mr_lock_key);
-       down_write(&obj->uobject.mutex);
+       obj->uobject.context = file->ucontext;
 
        /*
         * We ask for writable memory if any access flags other than
@@ -617,14 +426,23 @@ ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
 
        obj->umem.virt_base = cmd.hca_va;
 
-       pd = idr_read_pd(cmd.pd_handle, file->ucontext);
-       if (!pd)
-               goto err_release;
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+       pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
+       if (!pd || pd->uobject->context != file->ucontext) {
+               ret = -EINVAL;
+               goto err_up;
+       }
+
+       if (!pd->device->reg_user_mr) {
+               ret = -ENOSYS;
+               goto err_up;
+       }
 
        mr = pd->device->reg_user_mr(pd, &obj->umem, cmd.access_flags, &udata);
        if (IS_ERR(mr)) {
                ret = PTR_ERR(mr);
-               goto err_put;
+               goto err_up;
        }
 
        mr->device  = pd->device;
@@ -633,48 +451,53 @@ ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
        atomic_inc(&pd->usecnt);
        atomic_set(&mr->usecnt, 0);
 
-       obj->uobject.object = mr;
-       ret = idr_add_uobj(&ib_uverbs_mr_idr, &obj->uobject);
+       memset(&resp, 0, sizeof resp);
+       resp.lkey = mr->lkey;
+       resp.rkey = mr->rkey;
+
+retry:
+       if (!idr_pre_get(&ib_uverbs_mr_idr, GFP_KERNEL)) {
+               ret = -ENOMEM;
+               goto err_unreg;
+       }
+
+       ret = idr_get_new(&ib_uverbs_mr_idr, mr, &obj->uobject.id);
+
+       if (ret == -EAGAIN)
+               goto retry;
        if (ret)
                goto err_unreg;
 
-       memset(&resp, 0, sizeof resp);
-       resp.lkey      = mr->lkey;
-       resp.rkey      = mr->rkey;
        resp.mr_handle = obj->uobject.id;
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_copy;
+               goto err_idr;
        }
 
-       put_pd_read(pd);
-
        mutex_lock(&file->mutex);
        list_add_tail(&obj->uobject.list, &file->ucontext->mr_list);
        mutex_unlock(&file->mutex);
 
-       obj->uobject.live = 1;
-
-       up_write(&obj->uobject.mutex);
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        return in_len;
 
-err_copy:
-       idr_remove_uobj(&ib_uverbs_mr_idr, &obj->uobject);
+err_idr:
+       idr_remove(&ib_uverbs_mr_idr, obj->uobject.id);
 
 err_unreg:
        ib_dereg_mr(mr);
+       atomic_dec(&pd->usecnt);
 
-err_put:
-       put_pd_read(pd);
+err_up:
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
-err_release:
        ib_umem_release(file->device->ib_dev, &obj->umem);
 
 err_free:
-       put_uobj_write(&obj->uobject);
+       kfree(obj);
        return ret;
 }
 
@@ -684,40 +507,37 @@ ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
 {
        struct ib_uverbs_dereg_mr cmd;
        struct ib_mr             *mr;
-       struct ib_uobject        *uobj;
        struct ib_umem_object    *memobj;
        int                       ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
-       if (!uobj)
-               return -EINVAL;
-
-       memobj = container_of(uobj, struct ib_umem_object, uobject);
-       mr     = uobj->object;
+       mutex_lock(&ib_uverbs_idr_mutex);
 
-       ret = ib_dereg_mr(mr);
-       if (!ret)
-               uobj->live = 0;
+       mr = idr_find(&ib_uverbs_mr_idr, cmd.mr_handle);
+       if (!mr || mr->uobject->context != file->ucontext)
+               goto out;
 
-       put_uobj_write(uobj);
+       memobj = container_of(mr->uobject, struct ib_umem_object, uobject);
 
+       ret = ib_dereg_mr(mr);
        if (ret)
-               return ret;
+               goto out;
 
-       idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
+       idr_remove(&ib_uverbs_mr_idr, cmd.mr_handle);
 
        mutex_lock(&file->mutex);
-       list_del(&uobj->list);
+       list_del(&memobj->uobject.list);
        mutex_unlock(&file->mutex);
 
        ib_umem_release(file->device->ib_dev, &memobj->umem);
+       kfree(memobj);
 
-       put_uobj(uobj);
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
-       return in_len;
+       return ret ? ret : in_len;
 }
 
 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
@@ -756,7 +576,7 @@ ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
        struct ib_uverbs_create_cq      cmd;
        struct ib_uverbs_create_cq_resp resp;
        struct ib_udata                 udata;
-       struct ib_ucq_object           *obj;
+       struct ib_ucq_object           *uobj;
        struct ib_uverbs_event_file    *ev_file = NULL;
        struct ib_cq                   *cq;
        int                             ret;
@@ -774,13 +594,10 @@ ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
        if (cmd.comp_vector >= file->device->num_comp_vectors)
                return -EINVAL;
 
-       obj = kmalloc(sizeof *obj, GFP_KERNEL);
-       if (!obj)
+       uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
+       if (!uobj)
                return -ENOMEM;
 
-       init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);
-       down_write(&obj->uobject.mutex);
-
        if (cmd.comp_channel >= 0) {
                ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
                if (!ev_file) {
@@ -789,112 +606,81 @@ ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
                }
        }
 
-       obj->uverbs_file           = file;
-       obj->comp_events_reported  = 0;
-       obj->async_events_reported = 0;
-       INIT_LIST_HEAD(&obj->comp_list);
-       INIT_LIST_HEAD(&obj->async_list);
+       uobj->uobject.user_handle   = cmd.user_handle;
+       uobj->uobject.context       = file->ucontext;
+       uobj->uverbs_file           = file;
+       uobj->comp_events_reported  = 0;
+       uobj->async_events_reported = 0;
+       INIT_LIST_HEAD(&uobj->comp_list);
+       INIT_LIST_HEAD(&uobj->async_list);
 
        cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
                                             file->ucontext, &udata);
        if (IS_ERR(cq)) {
                ret = PTR_ERR(cq);
-               goto err_file;
+               goto err;
        }
 
        cq->device        = file->device->ib_dev;
-       cq->uobject       = &obj->uobject;
+       cq->uobject       = &uobj->uobject;
        cq->comp_handler  = ib_uverbs_comp_handler;
        cq->event_handler = ib_uverbs_cq_event_handler;
        cq->cq_context    = ev_file;
        atomic_set(&cq->usecnt, 0);
 
-       obj->uobject.object = cq;
-       ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+retry:
+       if (!idr_pre_get(&ib_uverbs_cq_idr, GFP_KERNEL)) {
+               ret = -ENOMEM;
+               goto err_up;
+       }
+
+       ret = idr_get_new(&ib_uverbs_cq_idr, cq, &uobj->uobject.id);
+
+       if (ret == -EAGAIN)
+               goto retry;
        if (ret)
-               goto err_free;
+               goto err_up;
 
        memset(&resp, 0, sizeof resp);
-       resp.cq_handle = obj->uobject.id;
+       resp.cq_handle = uobj->uobject.id;
        resp.cqe       = cq->cqe;
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_copy;
+               goto err_idr;
        }
 
        mutex_lock(&file->mutex);
-       list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
+       list_add_tail(&uobj->uobject.list, &file->ucontext->cq_list);
        mutex_unlock(&file->mutex);
 
-       obj->uobject.live = 1;
-
-       up_write(&obj->uobject.mutex);
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        return in_len;
 
-err_copy:
-       idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
-
+err_idr:
+       idr_remove(&ib_uverbs_cq_idr, uobj->uobject.id);
 
-err_free:
+err_up:
+       mutex_unlock(&ib_uverbs_idr_mutex);
        ib_destroy_cq(cq);
 
-err_file:
-       if (ev_file)
-               ib_uverbs_release_ucq(file, ev_file, obj);
-
 err:
-       put_uobj_write(&obj->uobject);
+       if (ev_file)
+               ib_uverbs_release_ucq(file, ev_file, uobj);
+       kfree(uobj);
        return ret;
 }
 
-ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
-                           const char __user *buf, int in_len,
-                           int out_len)
-{
-       struct ib_uverbs_resize_cq      cmd;
-       struct ib_uverbs_resize_cq_resp resp;
-       struct ib_udata                 udata;
-       struct ib_cq                    *cq;
-       int                             ret = -EINVAL;
-
-       if (copy_from_user(&cmd, buf, sizeof cmd))
-               return -EFAULT;
-
-       INIT_UDATA(&udata, buf + sizeof cmd,
-                  (unsigned long) cmd.response + sizeof resp,
-                  in_len - sizeof cmd, out_len - sizeof resp);
-
-       cq = idr_read_cq(cmd.cq_handle, file->ucontext);
-       if (!cq)
-               return -EINVAL;
-
-       ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
-       if (ret)
-               goto out;
-
-       memset(&resp, 0, sizeof resp);
-       resp.cqe = cq->cqe;
-
-       if (copy_to_user((void __user *) (unsigned long) cmd.response,
-                        &resp, sizeof resp))
-               ret = -EFAULT;
-
-out:
-       put_cq_read(cq);
-
-       return ret ? ret : in_len;
-}
-
 ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
                          const char __user *buf, int in_len,
                          int out_len)
 {
        struct ib_uverbs_poll_cq       cmd;
        struct ib_uverbs_poll_cq_resp *resp;
-       struct ib_uobject             *uobj;
        struct ib_cq                  *cq;
        struct ib_wc                  *wc;
        int                            ret = 0;
@@ -915,17 +701,15 @@ ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
                goto out_wc;
        }
 
-       uobj = idr_read_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
-       if (!uobj) {
+       mutex_lock(&ib_uverbs_idr_mutex);
+       cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
+       if (!cq || cq->uobject->context != file->ucontext) {
                ret = -EINVAL;
                goto out;
        }
-       cq = uobj->object;
 
        resp->count = ib_poll_cq(cq, cmd.ne, wc);
 
-       put_uobj_read(uobj);
-
        for (i = 0; i < resp->count; i++) {
                resp->wc[i].wr_id          = wc[i].wr_id;
                resp->wc[i].status         = wc[i].status;
@@ -947,6 +731,7 @@ ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
                ret = -EFAULT;
 
 out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
        kfree(resp);
 
 out_wc:
@@ -959,23 +744,22 @@ ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
                                int out_len)
 {
        struct ib_uverbs_req_notify_cq cmd;
-       struct ib_uobject             *uobj;
        struct ib_cq                  *cq;
+       int                            ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       uobj = idr_read_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
-       if (!uobj)
-               return -EINVAL;
-       cq = uobj->object;
-
-       ib_req_notify_cq(cq, cmd.solicited_only ?
-                        IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
-
-       put_uobj_read(uobj);
+       mutex_lock(&ib_uverbs_idr_mutex);
+       cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
+       if (cq && cq->uobject->context == file->ucontext) {
+               ib_req_notify_cq(cq, cmd.solicited_only ?
+                                       IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
+               ret = in_len;
+       }
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
-       return in_len;
+       return ret;
 }
 
 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
@@ -984,50 +768,52 @@ ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
 {
        struct ib_uverbs_destroy_cq      cmd;
        struct ib_uverbs_destroy_cq_resp resp;
-       struct ib_uobject               *uobj;
        struct ib_cq                    *cq;
-       struct ib_ucq_object            *obj;
+       struct ib_ucq_object            *uobj;
        struct ib_uverbs_event_file     *ev_file;
+       u64                              user_handle;
        int                              ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
-       if (!uobj)
-               return -EINVAL;
-       cq      = uobj->object;
-       ev_file = cq->cq_context;
-       obj     = container_of(cq->uobject, struct ib_ucq_object, uobject);
+       memset(&resp, 0, sizeof resp);
 
-       ret = ib_destroy_cq(cq);
-       if (!ret)
-               uobj->live = 0;
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+       cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
+       if (!cq || cq->uobject->context != file->ucontext)
+               goto out;
 
-       put_uobj_write(uobj);
+       user_handle = cq->uobject->user_handle;
+       uobj        = container_of(cq->uobject, struct ib_ucq_object, uobject);
+       ev_file     = cq->cq_context;
 
+       ret = ib_destroy_cq(cq);
        if (ret)
-               return ret;
+               goto out;
 
-       idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
+       idr_remove(&ib_uverbs_cq_idr, cmd.cq_handle);
 
        mutex_lock(&file->mutex);
-       list_del(&uobj->list);
+       list_del(&uobj->uobject.list);
        mutex_unlock(&file->mutex);
 
-       ib_uverbs_release_ucq(file, ev_file, obj);
+       ib_uverbs_release_ucq(file, ev_file, uobj);
 
-       memset(&resp, 0, sizeof resp);
-       resp.comp_events_reported  = obj->comp_events_reported;
-       resp.async_events_reported = obj->async_events_reported;
+       resp.comp_events_reported  = uobj->comp_events_reported;
+       resp.async_events_reported = uobj->async_events_reported;
 
-       put_uobj(uobj);
+       kfree(uobj);
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp))
-               return -EFAULT;
+               ret = -EFAULT;
 
-       return in_len;
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
+
+       return ret ? ret : in_len;
 }
 
 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
@@ -1037,7 +823,7 @@ ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
        struct ib_uverbs_create_qp      cmd;
        struct ib_uverbs_create_qp_resp resp;
        struct ib_udata                 udata;
-       struct ib_uqp_object           *obj;
+       struct ib_uqp_object           *uobj;
        struct ib_pd                   *pd;
        struct ib_cq                   *scq, *rcq;
        struct ib_srq                  *srq;
@@ -1055,22 +841,23 @@ ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
                   (unsigned long) cmd.response + sizeof resp,
                   in_len - sizeof cmd, out_len - sizeof resp);
 
-       obj = kmalloc(sizeof *obj, GFP_KERNEL);
-       if (!obj)
+       uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
+       if (!uobj)
                return -ENOMEM;
 
-       init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
-       down_write(&obj->uevent.uobject.mutex);
+       mutex_lock(&ib_uverbs_idr_mutex);
 
-       srq = cmd.is_srq ? idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
-       pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
-       scq = idr_read_cq(cmd.send_cq_handle, file->ucontext);
-       rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
-               scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext);
+       pd  = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
+       scq = idr_find(&ib_uverbs_cq_idr, cmd.send_cq_handle);
+       rcq = idr_find(&ib_uverbs_cq_idr, cmd.recv_cq_handle);
+       srq = cmd.is_srq ? idr_find(&ib_uverbs_srq_idr, cmd.srq_handle) : NULL;
 
-       if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {
+       if (!pd  || pd->uobject->context  != file->ucontext ||
+           !scq || scq->uobject->context != file->ucontext ||
+           !rcq || rcq->uobject->context != file->ucontext ||
+           (cmd.is_srq && (!srq || srq->uobject->context != file->ucontext))) {
                ret = -EINVAL;
-               goto err_put;
+               goto err_up;
        }
 
        attr.event_handler = ib_uverbs_qp_event_handler;
@@ -1087,14 +874,16 @@ ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
        attr.cap.max_recv_sge    = cmd.max_recv_sge;
        attr.cap.max_inline_data = cmd.max_inline_data;
 
-       obj->uevent.events_reported     = 0;
-       INIT_LIST_HEAD(&obj->uevent.event_list);
-       INIT_LIST_HEAD(&obj->mcast_list);
+       uobj->uevent.uobject.user_handle = cmd.user_handle;
+       uobj->uevent.uobject.context     = file->ucontext;
+       uobj->uevent.events_reported     = 0;
+       INIT_LIST_HEAD(&uobj->uevent.event_list);
+       INIT_LIST_HEAD(&uobj->mcast_list);
 
        qp = pd->device->create_qp(pd, &attr, &udata);
        if (IS_ERR(qp)) {
                ret = PTR_ERR(qp);
-               goto err_put;
+               goto err_up;
        }
 
        qp->device        = pd->device;
@@ -1102,7 +891,7 @@ ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
        qp->send_cq       = attr.send_cq;
        qp->recv_cq       = attr.recv_cq;
        qp->srq           = attr.srq;
-       qp->uobject       = &obj->uevent.uobject;
+       qp->uobject       = &uobj->uevent.uobject;
        qp->event_handler = attr.event_handler;
        qp->qp_context    = attr.qp_context;
        qp->qp_type       = attr.qp_type;
@@ -1112,14 +901,23 @@ ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
        if (attr.srq)
                atomic_inc(&attr.srq->usecnt);
 
-       obj->uevent.uobject.object = qp;
-       ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
+       memset(&resp, 0, sizeof resp);
+       resp.qpn = qp->qp_num;
+
+retry:
+       if (!idr_pre_get(&ib_uverbs_qp_idr, GFP_KERNEL)) {
+               ret = -ENOMEM;
+               goto err_destroy;
+       }
+
+       ret = idr_get_new(&ib_uverbs_qp_idr, qp, &uobj->uevent.uobject.id);
+
+       if (ret == -EAGAIN)
+               goto retry;
        if (ret)
                goto err_destroy;
 
-       memset(&resp, 0, sizeof resp);
-       resp.qpn             = qp->qp_num;
-       resp.qp_handle       = obj->uevent.uobject.id;
+       resp.qp_handle       = uobj->uevent.uobject.id;
        resp.max_recv_sge    = attr.cap.max_recv_sge;
        resp.max_send_sge    = attr.cap.max_send_sge;
        resp.max_recv_wr     = attr.cap.max_recv_wr;
@@ -1129,144 +927,33 @@ ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_copy;
+               goto err_idr;
        }
 
-       put_pd_read(pd);
-       put_cq_read(scq);
-       if (rcq != scq)
-               put_cq_read(rcq);
-       if (srq)
-               put_srq_read(srq);
-
        mutex_lock(&file->mutex);
-       list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
+       list_add_tail(&uobj->uevent.uobject.list, &file->ucontext->qp_list);
        mutex_unlock(&file->mutex);
 
-       obj->uevent.uobject.live = 1;
-
-       up_write(&obj->uevent.uobject.mutex);
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        return in_len;
 
-err_copy:
-       idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
+err_idr:
+       idr_remove(&ib_uverbs_qp_idr, uobj->uevent.uobject.id);
 
 err_destroy:
        ib_destroy_qp(qp);
+       atomic_dec(&pd->usecnt);
+       atomic_dec(&attr.send_cq->usecnt);
+       atomic_dec(&attr.recv_cq->usecnt);
+       if (attr.srq)
+               atomic_dec(&attr.srq->usecnt);
 
-err_put:
-       if (pd)
-               put_pd_read(pd);
-       if (scq)
-               put_cq_read(scq);
-       if (rcq && rcq != scq)
-               put_cq_read(rcq);
-       if (srq)
-               put_srq_read(srq);
-
-       put_uobj_write(&obj->uevent.uobject);
-       return ret;
-}
-
-ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
-                          const char __user *buf, int in_len,
-                          int out_len)
-{
-       struct ib_uverbs_query_qp      cmd;
-       struct ib_uverbs_query_qp_resp resp;
-       struct ib_qp                   *qp;
-       struct ib_qp_attr              *attr;
-       struct ib_qp_init_attr         *init_attr;
-       int                            ret;
-
-       if (copy_from_user(&cmd, buf, sizeof cmd))
-               return -EFAULT;
-
-       attr      = kmalloc(sizeof *attr, GFP_KERNEL);
-       init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
-       if (!attr || !init_attr) {
-               ret = -ENOMEM;
-               goto out;
-       }
-
-       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
-       if (!qp) {
-               ret = -EINVAL;
-               goto out;
-       }
-
-       ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
-
-       put_qp_read(qp);
-
-       if (ret)
-               goto out;
-
-       memset(&resp, 0, sizeof resp);
-
-       resp.qp_state               = attr->qp_state;
-       resp.cur_qp_state           = attr->cur_qp_state;
-       resp.path_mtu               = attr->path_mtu;
-       resp.path_mig_state         = attr->path_mig_state;
-       resp.qkey                   = attr->qkey;
-       resp.rq_psn                 = attr->rq_psn;
-       resp.sq_psn                 = attr->sq_psn;
-       resp.dest_qp_num            = attr->dest_qp_num;
-       resp.qp_access_flags        = attr->qp_access_flags;
-       resp.pkey_index             = attr->pkey_index;
-       resp.alt_pkey_index         = attr->alt_pkey_index;
-       resp.en_sqd_async_notify    = attr->en_sqd_async_notify;
-       resp.max_rd_atomic          = attr->max_rd_atomic;
-       resp.max_dest_rd_atomic     = attr->max_dest_rd_atomic;
-       resp.min_rnr_timer          = attr->min_rnr_timer;
-       resp.port_num               = attr->port_num;
-       resp.timeout                = attr->timeout;
-       resp.retry_cnt              = attr->retry_cnt;
-       resp.rnr_retry              = attr->rnr_retry;
-       resp.alt_port_num           = attr->alt_port_num;
-       resp.alt_timeout            = attr->alt_timeout;
-
-       memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
-       resp.dest.flow_label        = attr->ah_attr.grh.flow_label;
-       resp.dest.sgid_index        = attr->ah_attr.grh.sgid_index;
-       resp.dest.hop_limit         = attr->ah_attr.grh.hop_limit;
-       resp.dest.traffic_class     = attr->ah_attr.grh.traffic_class;
-       resp.dest.dlid              = attr->ah_attr.dlid;
-       resp.dest.sl                = attr->ah_attr.sl;
-       resp.dest.src_path_bits     = attr->ah_attr.src_path_bits;
-       resp.dest.static_rate       = attr->ah_attr.static_rate;
-       resp.dest.is_global         = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
-       resp.dest.port_num          = attr->ah_attr.port_num;
-
-       memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
-       resp.alt_dest.flow_label    = attr->alt_ah_attr.grh.flow_label;
-       resp.alt_dest.sgid_index    = attr->alt_ah_attr.grh.sgid_index;
-       resp.alt_dest.hop_limit     = attr->alt_ah_attr.grh.hop_limit;
-       resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
-       resp.alt_dest.dlid          = attr->alt_ah_attr.dlid;
-       resp.alt_dest.sl            = attr->alt_ah_attr.sl;
-       resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
-       resp.alt_dest.static_rate   = attr->alt_ah_attr.static_rate;
-       resp.alt_dest.is_global     = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
-       resp.alt_dest.port_num      = attr->alt_ah_attr.port_num;
-
-       resp.max_send_wr            = init_attr->cap.max_send_wr;
-       resp.max_recv_wr            = init_attr->cap.max_recv_wr;
-       resp.max_send_sge           = init_attr->cap.max_send_sge;
-       resp.max_recv_sge           = init_attr->cap.max_recv_sge;
-       resp.max_inline_data        = init_attr->cap.max_inline_data;
-       resp.sq_sig_all             = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
+err_up:
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
-       if (copy_to_user((void __user *) (unsigned long) cmd.response,
-                        &resp, sizeof resp))
-               ret = -EFAULT;
-
-out:
-       kfree(attr);
-       kfree(init_attr);
-
-       return ret ? ret : in_len;
+       kfree(uobj);
+       return ret;
 }
 
 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
@@ -1285,8 +972,10 @@ ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
        if (!attr)
                return -ENOMEM;
 
-       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
-       if (!qp) {
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
+       if (!qp || qp->uobject->context != file->ucontext) {
                ret = -EINVAL;
                goto out;
        }
@@ -1301,7 +990,7 @@ ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
        attr->dest_qp_num         = cmd.dest_qp_num;
        attr->qp_access_flags     = cmd.qp_access_flags;
        attr->pkey_index          = cmd.pkey_index;
-       attr->alt_pkey_index      = cmd.alt_pkey_index;
+       attr->alt_pkey_index      = cmd.pkey_index;
        attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
        attr->max_rd_atomic       = cmd.max_rd_atomic;
        attr->max_dest_rd_atomic  = cmd.max_dest_rd_atomic;
@@ -1338,15 +1027,13 @@ ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
        attr->alt_ah_attr.port_num          = cmd.alt_dest.port_num;
 
        ret = ib_modify_qp(qp, attr, cmd.attr_mask);
-
-       put_qp_read(qp);
-
        if (ret)
                goto out;
 
        ret = in_len;
 
 out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
        kfree(attr);
 
        return ret;
@@ -1358,9 +1045,8 @@ ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
 {
        struct ib_uverbs_destroy_qp      cmd;
        struct ib_uverbs_destroy_qp_resp resp;
-       struct ib_uobject               *uobj;
        struct ib_qp                    *qp;
-       struct ib_uqp_object            *obj;
+       struct ib_uqp_object            *uobj;
        int                              ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
@@ -1368,48 +1054,48 @@ ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
 
        memset(&resp, 0, sizeof resp);
 
-       uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
-       if (!uobj)
-               return -EINVAL;
-       qp  = uobj->object;
-       obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
+       mutex_lock(&ib_uverbs_idr_mutex);
 
-       if (!list_empty(&obj->mcast_list)) {
-               put_uobj_write(uobj);
-               return -EBUSY;
-       }
+       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
+       if (!qp || qp->uobject->context != file->ucontext)
+               goto out;
 
-       ret = ib_destroy_qp(qp);
-       if (!ret)
-               uobj->live = 0;
+       uobj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
 
-       put_uobj_write(uobj);
+       if (!list_empty(&uobj->mcast_list)) {
+               ret = -EBUSY;
+               goto out;
+       }
 
+       ret = ib_destroy_qp(qp);
        if (ret)
-               return ret;
+               goto out;
 
-       idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
+       idr_remove(&ib_uverbs_qp_idr, cmd.qp_handle);
 
        mutex_lock(&file->mutex);
-       list_del(&uobj->list);
+       list_del(&uobj->uevent.uobject.list);
        mutex_unlock(&file->mutex);
 
-       ib_uverbs_release_uevent(file, &obj->uevent);
+       ib_uverbs_release_uevent(file, &uobj->uevent);
 
-       resp.events_reported = obj->uevent.events_reported;
+       resp.events_reported = uobj->uevent.events_reported;
 
-       put_uobj(uobj);
+       kfree(uobj);
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp))
-               return -EFAULT;
+               ret = -EFAULT;
 
-       return in_len;
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
+
+       return ret ? ret : in_len;
 }
 
 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
-                           const char __user *buf, int in_len,
-                           int out_len)
+                            const char __user *buf, int in_len,
+                            int out_len)
 {
        struct ib_uverbs_post_send      cmd;
        struct ib_uverbs_post_send_resp resp;
@@ -1417,7 +1103,6 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
        struct ib_send_wr              *wr = NULL, *last, *next, *bad_wr;
        struct ib_qp                   *qp;
        int                             i, sg_ind;
-       int                             is_ud;
        ssize_t                         ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
@@ -1434,11 +1119,12 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
        if (!user_wr)
                return -ENOMEM;
 
-       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
-       if (!qp)
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
+       if (!qp || qp->uobject->context != file->ucontext)
                goto out;
 
-       is_ud = qp->qp_type == IB_QPT_UD;
        sg_ind = 0;
        last = NULL;
        for (i = 0; i < cmd.wr_count; ++i) {
@@ -1446,12 +1132,12 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
                                   buf + sizeof cmd + i * cmd.wqe_size,
                                   cmd.wqe_size)) {
                        ret = -EFAULT;
-                       goto out_put;
+                       goto out;
                }
 
                if (user_wr->num_sge + sg_ind > cmd.sge_count) {
                        ret = -EINVAL;
-                       goto out_put;
+                       goto out;
                }
 
                next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
@@ -1459,7 +1145,7 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
                               GFP_KERNEL);
                if (!next) {
                        ret = -ENOMEM;
-                       goto out_put;
+                       goto out;
                }
 
                if (!last)
@@ -1475,12 +1161,12 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
                next->send_flags = user_wr->send_flags;
                next->imm_data   = (__be32 __force) user_wr->imm_data;
 
-               if (is_ud) {
-                       next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
-                                                    file->ucontext);
+               if (qp->qp_type == IB_QPT_UD) {
+                       next->wr.ud.ah = idr_find(&ib_uverbs_ah_idr,
+                                                 user_wr->wr.ud.ah);
                        if (!next->wr.ud.ah) {
                                ret = -EINVAL;
-                               goto out_put;
+                               goto out;
                        }
                        next->wr.ud.remote_qpn  = user_wr->wr.ud.remote_qpn;
                        next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
@@ -1517,7 +1203,7 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
                                           sg_ind * sizeof (struct ib_sge),
                                           next->num_sge * sizeof (struct ib_sge))) {
                                ret = -EFAULT;
-                               goto out_put;
+                               goto out;
                        }
                        sg_ind += next->num_sge;
                } else
@@ -1537,18 +1223,15 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
                         &resp, sizeof resp))
                ret = -EFAULT;
 
-out_put:
-       put_qp_read(qp);
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        while (wr) {
-               if (is_ud && wr->wr.ud.ah)
-                       put_ah_read(wr->wr.ud.ah);
                next = wr->next;
                kfree(wr);
                wr = next;
        }
 
-out:
        kfree(user_wr);
 
        return ret ? ret : in_len;
@@ -1640,8 +1323,8 @@ err:
 }
 
 ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
-                           const char __user *buf, int in_len,
-                           int out_len)
+                            const char __user *buf, int in_len,
+                            int out_len)
 {
        struct ib_uverbs_post_recv      cmd;
        struct ib_uverbs_post_recv_resp resp;
@@ -1658,15 +1341,14 @@ ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
        if (IS_ERR(wr))
                return PTR_ERR(wr);
 
-       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
-       if (!qp)
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
+       if (!qp || qp->uobject->context != file->ucontext)
                goto out;
 
        resp.bad_wr = 0;
        ret = qp->device->post_recv(qp, wr, &bad_wr);
-
-       put_qp_read(qp);
-
        if (ret)
                for (next = wr; next; next = next->next) {
                        ++resp.bad_wr;
@@ -1680,6 +1362,8 @@ ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
                ret = -EFAULT;
 
 out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
+
        while (wr) {
                next = wr->next;
                kfree(wr);
@@ -1690,8 +1374,8 @@ out:
 }
 
 ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
-                               const char __user *buf, int in_len,
-                               int out_len)
+                            const char __user *buf, int in_len,
+                            int out_len)
 {
        struct ib_uverbs_post_srq_recv      cmd;
        struct ib_uverbs_post_srq_recv_resp resp;
@@ -1708,15 +1392,14 @@ ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
        if (IS_ERR(wr))
                return PTR_ERR(wr);
 
-       srq = idr_read_srq(cmd.srq_handle, file->ucontext);
-       if (!srq)
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+       srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
+       if (!srq || srq->uobject->context != file->ucontext)
                goto out;
 
        resp.bad_wr = 0;
        ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
-
-       put_srq_read(srq);
-
        if (ret)
                for (next = wr; next; next = next->next) {
                        ++resp.bad_wr;
@@ -1730,6 +1413,8 @@ ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
                ret = -EFAULT;
 
 out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
+
        while (wr) {
                next = wr->next;
                kfree(wr);
@@ -1761,15 +1446,17 @@ ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
        if (!uobj)
                return -ENOMEM;
 
-       init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_key);
-       down_write(&uobj->mutex);
+       mutex_lock(&ib_uverbs_idr_mutex);
 
-       pd = idr_read_pd(cmd.pd_handle, file->ucontext);
-       if (!pd) {
+       pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
+       if (!pd || pd->uobject->context != file->ucontext) {
                ret = -EINVAL;
-               goto err;
+               goto err_up;
        }
 
+       uobj->user_handle = cmd.user_handle;
+       uobj->context     = file->ucontext;
+
        attr.dlid              = cmd.attr.dlid;
        attr.sl                = cmd.attr.sl;
        attr.src_path_bits     = cmd.attr.src_path_bits;
@@ -1785,13 +1472,21 @@ ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
        ah = ib_create_ah(pd, &attr);
        if (IS_ERR(ah)) {
                ret = PTR_ERR(ah);
-               goto err_put;
+               goto err_up;
+       }
+
+       ah->uobject = uobj;
+
+retry:
+       if (!idr_pre_get(&ib_uverbs_ah_idr, GFP_KERNEL)) {
+               ret = -ENOMEM;
+               goto err_destroy;
        }
 
-       ah->uobject  = uobj;
-       uobj->object = ah;
+       ret = idr_get_new(&ib_uverbs_ah_idr, ah, &uobj->id);
 
-       ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
+       if (ret == -EAGAIN)
+               goto retry;
        if (ret)
                goto err_destroy;
 
@@ -1800,32 +1495,27 @@ ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_copy;
+               goto err_idr;
        }
 
-       put_pd_read(pd);
-
        mutex_lock(&file->mutex);
        list_add_tail(&uobj->list, &file->ucontext->ah_list);
        mutex_unlock(&file->mutex);
 
-       uobj->live = 1;
-
-       up_write(&uobj->mutex);
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        return in_len;
 
-err_copy:
-       idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
+err_idr:
+       idr_remove(&ib_uverbs_ah_idr, uobj->id);
 
 err_destroy:
        ib_destroy_ah(ah);
 
-err_put:
-       put_pd_read(pd);
+err_up:
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
-err:
-       put_uobj_write(uobj);
+       kfree(uobj);
        return ret;
 }
 
@@ -1835,34 +1525,35 @@ ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
        struct ib_uverbs_destroy_ah cmd;
        struct ib_ah               *ah;
        struct ib_uobject          *uobj;
-       int                         ret;
+       int                         ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
-       if (!uobj)
-               return -EINVAL;
-       ah = uobj->object;
+       mutex_lock(&ib_uverbs_idr_mutex);
 
-       ret = ib_destroy_ah(ah);
-       if (!ret)
-               uobj->live = 0;
+       ah = idr_find(&ib_uverbs_ah_idr, cmd.ah_handle);
+       if (!ah || ah->uobject->context != file->ucontext)
+               goto out;
 
-       put_uobj_write(uobj);
+       uobj = ah->uobject;
 
+       ret = ib_destroy_ah(ah);
        if (ret)
-               return ret;
+               goto out;
 
-       idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
+       idr_remove(&ib_uverbs_ah_idr, cmd.ah_handle);
 
        mutex_lock(&file->mutex);
        list_del(&uobj->list);
        mutex_unlock(&file->mutex);
 
-       put_uobj(uobj);
+       kfree(uobj);
 
-       return in_len;
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
+
+       return ret ? ret : in_len;
 }
 
 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
@@ -1871,43 +1562,47 @@ ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
 {
        struct ib_uverbs_attach_mcast cmd;
        struct ib_qp                 *qp;
-       struct ib_uqp_object         *obj;
+       struct ib_uqp_object         *uobj;
        struct ib_uverbs_mcast_entry *mcast;
-       int                           ret;
+       int                           ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
-       if (!qp)
-               return -EINVAL;
+       mutex_lock(&ib_uverbs_idr_mutex);
 
-       obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
+       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
+       if (!qp || qp->uobject->context != file->ucontext)
+               goto out;
 
-       list_for_each_entry(mcast, &obj->mcast_list, list)
+       uobj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
+
+       list_for_each_entry(mcast, &uobj->mcast_list, list)
                if (cmd.mlid == mcast->lid &&
                    !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
                        ret = 0;
-                       goto out_put;
+                       goto out;
                }
 
        mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
        if (!mcast) {
                ret = -ENOMEM;
-               goto out_put;
+               goto out;
        }
 
        mcast->lid = cmd.mlid;
        memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
 
        ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
-       if (!ret)
-               list_add_tail(&mcast->list, &obj->mcast_list);
-       else
+       if (!ret) {
+               uobj = container_of(qp->uobject, struct ib_uqp_object,
+                                   uevent.uobject);
+               list_add_tail(&mcast->list, &uobj->mcast_list);
+       } else
                kfree(mcast);
 
-out_put:
-       put_qp_read(qp);
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        return ret ? ret : in_len;
 }
@@ -1917,7 +1612,7 @@ ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
                               int out_len)
 {
        struct ib_uverbs_detach_mcast cmd;
-       struct ib_uqp_object         *obj;
+       struct ib_uqp_object         *uobj;
        struct ib_qp                 *qp;
        struct ib_uverbs_mcast_entry *mcast;
        int                           ret = -EINVAL;
@@ -1925,17 +1620,19 @@ ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
-       if (!qp)
-               return -EINVAL;
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
+       if (!qp || qp->uobject->context != file->ucontext)
+               goto out;
 
        ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
        if (ret)
-               goto out_put;
+               goto out;
 
-       obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
+       uobj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
 
-       list_for_each_entry(mcast, &obj->mcast_list, list)
+       list_for_each_entry(mcast, &uobj->mcast_list, list)
                if (cmd.mlid == mcast->lid &&
                    !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
                        list_del(&mcast->list);
@@ -1943,8 +1640,8 @@ ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
                        break;
                }
 
-out_put:
-       put_qp_read(qp);
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        return ret ? ret : in_len;
 }
@@ -1956,7 +1653,7 @@ ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
        struct ib_uverbs_create_srq      cmd;
        struct ib_uverbs_create_srq_resp resp;
        struct ib_udata                  udata;
-       struct ib_uevent_object         *obj;
+       struct ib_uevent_object         *uobj;
        struct ib_pd                    *pd;
        struct ib_srq                   *srq;
        struct ib_srq_init_attr          attr;
@@ -1972,17 +1669,17 @@ ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
                   (unsigned long) cmd.response + sizeof resp,
                   in_len - sizeof cmd, out_len - sizeof resp);
 
-       obj = kmalloc(sizeof *obj, GFP_KERNEL);
-       if (!obj)
+       uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
+       if (!uobj)
                return -ENOMEM;
 
-       init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &srq_lock_key);
-       down_write(&obj->uobject.mutex);
+       mutex_lock(&ib_uverbs_idr_mutex);
 
-       pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
-       if (!pd) {
+       pd  = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
+
+       if (!pd || pd->uobject->context != file->ucontext) {
                ret = -EINVAL;
-               goto err;
+               goto err_up;
        }
 
        attr.event_handler  = ib_uverbs_srq_event_handler;
@@ -1991,62 +1688,67 @@ ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
        attr.attr.max_sge   = cmd.max_sge;
        attr.attr.srq_limit = cmd.srq_limit;
 
-       obj->events_reported     = 0;
-       INIT_LIST_HEAD(&obj->event_list);
+       uobj->uobject.user_handle = cmd.user_handle;
+       uobj->uobject.context     = file->ucontext;
+       uobj->events_reported     = 0;
+       INIT_LIST_HEAD(&uobj->event_list);
 
        srq = pd->device->create_srq(pd, &attr, &udata);
        if (IS_ERR(srq)) {
                ret = PTR_ERR(srq);
-               goto err_put;
+               goto err_up;
        }
 
        srq->device        = pd->device;
        srq->pd            = pd;
-       srq->uobject       = &obj->uobject;
+       srq->uobject       = &uobj->uobject;
        srq->event_handler = attr.event_handler;
        srq->srq_context   = attr.srq_context;
        atomic_inc(&pd->usecnt);
        atomic_set(&srq->usecnt, 0);
 
-       obj->uobject.object = srq;
-       ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
+       memset(&resp, 0, sizeof resp);
+
+retry:
+       if (!idr_pre_get(&ib_uverbs_srq_idr, GFP_KERNEL)) {
+               ret = -ENOMEM;
+               goto err_destroy;
+       }
+
+       ret = idr_get_new(&ib_uverbs_srq_idr, srq, &uobj->uobject.id);
+
+       if (ret == -EAGAIN)
+               goto retry;
        if (ret)
                goto err_destroy;
 
-       memset(&resp, 0, sizeof resp);
-       resp.srq_handle = obj->uobject.id;
-       resp.max_wr     = attr.attr.max_wr;
-       resp.max_sge    = attr.attr.max_sge;
+       resp.srq_handle = uobj->uobject.id;
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_copy;
+               goto err_idr;
        }
 
-       put_pd_read(pd);
-
        mutex_lock(&file->mutex);
-       list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
+       list_add_tail(&uobj->uobject.list, &file->ucontext->srq_list);
        mutex_unlock(&file->mutex);
 
-       obj->uobject.live = 1;
-
-       up_write(&obj->uobject.mutex);
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        return in_len;
 
-err_copy:
-       idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
+err_idr:
+       idr_remove(&ib_uverbs_srq_idr, uobj->uobject.id);
 
 err_destroy:
        ib_destroy_srq(srq);
+       atomic_dec(&pd->usecnt);
 
-err_put:
-       put_pd_read(pd);
+err_up:
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
-err:
-       put_uobj_write(&obj->uobject);
+       kfree(uobj);
        return ret;
 }
 
@@ -2062,105 +1764,70 @@ ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       srq = idr_read_srq(cmd.srq_handle, file->ucontext);
-       if (!srq)
-               return -EINVAL;
+       mutex_lock(&ib_uverbs_idr_mutex);
+
+       srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
+       if (!srq || srq->uobject->context != file->ucontext) {
+               ret = -EINVAL;
+               goto out;
+       }
 
        attr.max_wr    = cmd.max_wr;
        attr.srq_limit = cmd.srq_limit;
 
        ret = ib_modify_srq(srq, &attr, cmd.attr_mask);
 
-       put_srq_read(srq);
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
 
        return ret ? ret : in_len;
 }
 
-ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
-                           const char __user *buf,
-                           int in_len, int out_len)
-{
-       struct ib_uverbs_query_srq      cmd;
-       struct ib_uverbs_query_srq_resp resp;
-       struct ib_srq_attr              attr;
-       struct ib_srq                   *srq;
-       int                             ret;
-
-       if (out_len < sizeof resp)
-               return -ENOSPC;
-
-       if (copy_from_user(&cmd, buf, sizeof cmd))
-               return -EFAULT;
-
-       srq = idr_read_srq(cmd.srq_handle, file->ucontext);
-       if (!srq)
-               return -EINVAL;
-
-       ret = ib_query_srq(srq, &attr);
-
-       put_srq_read(srq);
-
-       if (ret)
-               return ret;
-
-       memset(&resp, 0, sizeof resp);
-
-       resp.max_wr    = attr.max_wr;
-       resp.max_sge   = attr.max_sge;
-       resp.srq_limit = attr.srq_limit;
-
-       if (copy_to_user((void __user *) (unsigned long) cmd.response,
-                        &resp, sizeof resp))
-               return -EFAULT;
-
-       return in_len;
-}
-
 ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
                              const char __user *buf, int in_len,
                              int out_len)
 {
        struct ib_uverbs_destroy_srq      cmd;
        struct ib_uverbs_destroy_srq_resp resp;
-       struct ib_uobject                *uobj;
        struct ib_srq                    *srq;
-       struct ib_uevent_object          *obj;
+       struct ib_uevent_object          *uobj;
        int                               ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
-       if (!uobj)
-               return -EINVAL;
-       srq = uobj->object;
-       obj = container_of(uobj, struct ib_uevent_object, uobject);
+       mutex_lock(&ib_uverbs_idr_mutex);
 
-       ret = ib_destroy_srq(srq);
-       if (!ret)
-               uobj->live = 0;
+       memset(&resp, 0, sizeof resp);
 
-       put_uobj_write(uobj);
+       srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
+       if (!srq || srq->uobject->context != file->ucontext)
+               goto out;
+
+       uobj = container_of(srq->uobject, struct ib_uevent_object, uobject);
 
+       ret = ib_destroy_srq(srq);
        if (ret)
-               return ret;
+               goto out;
 
-       idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
+       idr_remove(&ib_uverbs_srq_idr, cmd.srq_handle);
 
        mutex_lock(&file->mutex);
-       list_del(&uobj->list);
+       list_del(&uobj->uobject.list);
        mutex_unlock(&file->mutex);
 
-       ib_uverbs_release_uevent(file, obj);
+       ib_uverbs_release_uevent(file, uobj);
 
-       memset(&resp, 0, sizeof resp);
-       resp.events_reported = obj->events_reported;
+       resp.events_reported = uobj->events_reported;
 
-       put_uobj(uobj);
+       kfree(uobj);
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp))
                ret = -EFAULT;
 
+out:
+       mutex_unlock(&ib_uverbs_idr_mutex);
+
        return ret ? ret : in_len;
 }