include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / infiniband / core / uverbs_cmd.c
index 8c89abc..6fcfbeb 100644 (file)
@@ -1,7 +1,8 @@
 /*
  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
- * Copyright (c) 2005 Cisco Systems.  All rights reserved.
+ * Copyright (c) 2005, 2006, 2007 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
  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
- *
- * $Id: uverbs_cmd.c 2708 2005-06-24 17:27:21Z roland $
  */
 
 #include <linux/file.h>
 #include <linux/fs.h>
+#include <linux/slab.h>
 
 #include <asm/uaccess.h>
 
 #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);               \
                (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) {
+               if (uobj->context == context)
+                       kref_get(&uobj->ref);
+               else
+                       uobj = NULL;
+       }
+       spin_unlock(&ib_uverbs_idr_lock);
+
+       return uobj;
+}
+
+static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
+                                       struct ib_ucontext *context, int nested)
+{
+       struct ib_uobject *uobj;
+
+       uobj = __idr_get_uobj(idr, id, context);
+       if (!uobj)
+               return NULL;
+
+       if (nested)
+               down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
+       else
+               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,
+                         int nested)
+{
+       struct ib_uobject *uobj;
+
+       uobj = idr_read_uobj(idr, id, context, nested);
+       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, 0);
+}
+
+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, int nested)
+{
+       return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
+}
+
+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, 0);
+}
+
+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, 0);
+}
+
+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, 0);
+}
+
+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)
@@ -67,7 +273,7 @@ ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       down(&file->mutex);
+       mutex_lock(&file->mutex);
 
        if (file->ucontext) {
                ret = -EINVAL;
@@ -79,8 +285,10 @@ 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))
-               return PTR_ERR(file->ucontext);
+       if (IS_ERR(ucontext)) {
+               ret = PTR_ERR(ucontext);
+               goto err;
+       }
 
        ucontext->device = ibdev;
        INIT_LIST_HEAD(&ucontext->pd_list);
@@ -90,13 +298,19 @@ ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
        INIT_LIST_HEAD(&ucontext->qp_list);
        INIT_LIST_HEAD(&ucontext->srq_list);
        INIT_LIST_HEAD(&ucontext->ah_list);
+       ucontext->closing = 0;
 
        resp.num_comp_vectors = file->device->num_comp_vectors;
 
-       filp = ib_uverbs_alloc_event_file(file, 1, &resp.async_fd);
+       ret = get_unused_fd();
+       if (ret < 0)
+               goto err_free;
+       resp.async_fd = ret;
+
+       filp = ib_uverbs_alloc_event_file(file, 1);
        if (IS_ERR(filp)) {
                ret = PTR_ERR(filp);
-               goto err_free;
+               goto err_fd;
        }
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
@@ -119,19 +333,21 @@ ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
 
        fd_install(resp.async_fd, filp);
 
-       up(&file->mutex);
+       mutex_unlock(&file->mutex);
 
        return in_len;
 
 err_file:
-       put_unused_fd(resp.async_fd);
        fput(filp);
 
+err_fd:
+       put_unused_fd(resp.async_fd);
+
 err_free:
        ibdev->dealloc_ucontext(ucontext);
 
 err:
-       up(&file->mutex);
+       mutex_unlock(&file->mutex);
        return ret;
 }
 
@@ -157,7 +373,7 @@ ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
        memset(&resp, 0, sizeof resp);
 
        resp.fw_ver                    = attr.fw_ver;
-       resp.node_guid                 = attr.node_guid;
+       resp.node_guid                 = file->device->ib_dev->node_guid;
        resp.sys_image_guid            = attr.sys_image_guid;
        resp.max_mr_size               = attr.max_mr_size;
        resp.page_size_cap             = attr.page_size_cap;
@@ -277,7 +493,8 @@ ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
        if (!uobj)
                return -ENOMEM;
 
-       uobj->context = file->ucontext;
+       init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
+       down_write(&uobj->mutex);
 
        pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
                                            file->ucontext, &udata);
@@ -290,20 +507,10 @@ ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
        pd->uobject = uobj;
        atomic_set(&pd->usecnt, 0);
 
-       down(&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;
+       uobj->object = pd;
+       ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
        if (ret)
-               goto err_up;
+               goto err_idr;
 
        memset(&resp, 0, sizeof resp);
        resp.pd_handle = uobj->id;
@@ -311,26 +518,27 @@ retry:
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_idr;
+               goto err_copy;
        }
 
-       down(&file->mutex);
+       mutex_lock(&file->mutex);
        list_add_tail(&uobj->list, &file->ucontext->pd_list);
-       up(&file->mutex);
+       mutex_unlock(&file->mutex);
 
-       up(&ib_uverbs_idr_mutex);
+       uobj->live = 1;
+
+       up_write(&uobj->mutex);
 
        return in_len;
 
-err_idr:
-       idr_remove(&ib_uverbs_pd_idr, uobj->id);
+err_copy:
+       idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
 
-err_up:
-       up(&ib_uverbs_idr_mutex);
+err_idr:
        ib_dealloc_pd(pd);
 
 err:
-       kfree(uobj);
+       put_uobj_write(uobj);
        return ret;
 }
 
@@ -339,37 +547,34 @@ 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 = -EINVAL;
+       int                         ret;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       down(&ib_uverbs_idr_mutex);
+       uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
+       if (!uobj)
+               return -EINVAL;
 
-       pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
-       if (!pd || pd->uobject->context != file->ucontext)
-               goto out;
+       ret = ib_dealloc_pd(uobj->object);
+       if (!ret)
+               uobj->live = 0;
 
-       uobj = pd->uobject;
+       put_uobj_write(uobj);
 
-       ret = ib_dealloc_pd(pd);
        if (ret)
-               goto out;
+               return ret;
 
-       idr_remove(&ib_uverbs_pd_idr, cmd.pd_handle);
+       idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
 
-       down(&file->mutex);
+       mutex_lock(&file->mutex);
        list_del(&uobj->list);
-       up(&file->mutex);
+       mutex_unlock(&file->mutex);
 
-       kfree(uobj);
-
-out:
-       up(&ib_uverbs_idr_mutex);
+       put_uobj(uobj);
 
-       return ret ? ret : in_len;
+       return in_len;
 }
 
 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
@@ -379,7 +584,7 @@ ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
        struct ib_uverbs_reg_mr      cmd;
        struct ib_uverbs_reg_mr_resp resp;
        struct ib_udata              udata;
-       struct ib_umem_object       *obj;
+       struct ib_uobject           *uobj;
        struct ib_pd                *pd;
        struct ib_mr                *mr;
        int                          ret;
@@ -405,98 +610,71 @@ ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
            !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
                return -EINVAL;
 
-       obj = kmalloc(sizeof *obj, GFP_KERNEL);
-       if (!obj)
+       uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
+       if (!uobj)
                return -ENOMEM;
 
-       obj->uobject.context = file->ucontext;
+       init_uobj(uobj, 0, file->ucontext, &mr_lock_key);
+       down_write(&uobj->mutex);
 
-       /*
-        * We ask for writable memory if any access flags other than
-        * "remote read" are set.  "Local write" and "remote write"
-        * obviously require write access.  "Remote atomic" can do
-        * things like fetch and add, which will modify memory, and
-        * "MW bind" can change permissions by binding a window.
-        */
-       ret = ib_umem_get(file->device->ib_dev, &obj->umem,
-                         (void *) (unsigned long) cmd.start, cmd.length,
-                         !!(cmd.access_flags & ~IB_ACCESS_REMOTE_READ));
-       if (ret)
-               goto err_free;
-
-       obj->umem.virt_base = cmd.hca_va;
-
-       down(&ib_uverbs_idr_mutex);
-
-       pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
-       if (!pd || pd->uobject->context != file->ucontext) {
+       pd = idr_read_pd(cmd.pd_handle, file->ucontext);
+       if (!pd) {
                ret = -EINVAL;
-               goto err_up;
-       }
-
-       if (!pd->device->reg_user_mr) {
-               ret = -ENOSYS;
-               goto err_up;
+               goto err_free;
        }
 
-       mr = pd->device->reg_user_mr(pd, &obj->umem, cmd.access_flags, &udata);
+       mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
+                                    cmd.access_flags, &udata);
        if (IS_ERR(mr)) {
                ret = PTR_ERR(mr);
-               goto err_up;
+               goto err_put;
        }
 
        mr->device  = pd->device;
        mr->pd      = pd;
-       mr->uobject = &obj->uobject;
+       mr->uobject = uobj;
        atomic_inc(&pd->usecnt);
        atomic_set(&mr->usecnt, 0);
 
-       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;
+       uobj->object = mr;
+       ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
        if (ret)
                goto err_unreg;
 
-       resp.mr_handle = obj->uobject.id;
+       memset(&resp, 0, sizeof resp);
+       resp.lkey      = mr->lkey;
+       resp.rkey      = mr->rkey;
+       resp.mr_handle = uobj->id;
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_idr;
+               goto err_copy;
        }
 
-       down(&file->mutex);
-       list_add_tail(&obj->uobject.list, &file->ucontext->mr_list);
-       up(&file->mutex);
+       put_pd_read(pd);
+
+       mutex_lock(&file->mutex);
+       list_add_tail(&uobj->list, &file->ucontext->mr_list);
+       mutex_unlock(&file->mutex);
 
-       up(&ib_uverbs_idr_mutex);
+       uobj->live = 1;
+
+       up_write(&uobj->mutex);
 
        return in_len;
 
-err_idr:
-       idr_remove(&ib_uverbs_mr_idr, obj->uobject.id);
+err_copy:
+       idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
 
 err_unreg:
        ib_dereg_mr(mr);
 
-err_up:
-       up(&ib_uverbs_idr_mutex);
-
-       ib_umem_release(file->device->ib_dev, &obj->umem);
+err_put:
+       put_pd_read(pd);
 
 err_free:
-       kfree(obj);
+       put_uobj_write(uobj);
        return ret;
 }
 
@@ -506,37 +684,36 @@ ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
 {
        struct ib_uverbs_dereg_mr cmd;
        struct ib_mr             *mr;
-       struct ib_umem_object    *memobj;
+       struct ib_uobject        *uobj;
        int                       ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       down(&ib_uverbs_idr_mutex);
-
-       mr = idr_find(&ib_uverbs_mr_idr, cmd.mr_handle);
-       if (!mr || mr->uobject->context != file->ucontext)
-               goto out;
+       uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
+       if (!uobj)
+               return -EINVAL;
 
-       memobj = container_of(mr->uobject, struct ib_umem_object, uobject);
+       mr = uobj->object;
 
        ret = ib_dereg_mr(mr);
-       if (ret)
-               goto out;
+       if (!ret)
+               uobj->live = 0;
 
-       idr_remove(&ib_uverbs_mr_idr, cmd.mr_handle);
+       put_uobj_write(uobj);
 
-       down(&file->mutex);
-       list_del(&memobj->uobject.list);
-       up(&file->mutex);
+       if (ret)
+               return ret;
 
-       ib_umem_release(file->device->ib_dev, &memobj->umem);
-       kfree(memobj);
+       idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
 
-out:
-       up(&ib_uverbs_idr_mutex);
+       mutex_lock(&file->mutex);
+       list_del(&uobj->list);
+       mutex_unlock(&file->mutex);
 
-       return ret ? ret : in_len;
+       put_uobj(uobj);
+
+       return in_len;
 }
 
 ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
@@ -546,6 +723,7 @@ ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
        struct ib_uverbs_create_comp_channel       cmd;
        struct ib_uverbs_create_comp_channel_resp  resp;
        struct file                               *filp;
+       int ret;
 
        if (out_len < sizeof resp)
                return -ENOSPC;
@@ -553,9 +731,16 @@ ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);
-       if (IS_ERR(filp))
+       ret = get_unused_fd();
+       if (ret < 0)
+               return ret;
+       resp.fd = ret;
+
+       filp = ib_uverbs_alloc_event_file(file, 0);
+       if (IS_ERR(filp)) {
+               put_unused_fd(resp.fd);
                return PTR_ERR(filp);
+       }
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
@@ -575,7 +760,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           *uobj;
+       struct ib_ucq_object           *obj;
        struct ib_uverbs_event_file    *ev_file = NULL;
        struct ib_cq                   *cq;
        int                             ret;
@@ -593,79 +778,119 @@ ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
        if (cmd.comp_vector >= file->device->num_comp_vectors)
                return -EINVAL;
 
-       if (cmd.comp_channel >= 0)
-               ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
-
-       uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
-       if (!uobj)
+       obj = kmalloc(sizeof *obj, GFP_KERNEL);
+       if (!obj)
                return -ENOMEM;
 
-       uobj->uobject.user_handle   = cmd.user_handle;
-       uobj->uobject.context       = file->ucontext;
-       uobj->comp_events_reported  = 0;
-       uobj->async_events_reported = 0;
-       INIT_LIST_HEAD(&uobj->comp_list);
-       INIT_LIST_HEAD(&uobj->async_list);
+       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) {
+                       ret = -EINVAL;
+                       goto err;
+               }
+       }
+
+       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);
 
        cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
+                                            cmd.comp_vector,
                                             file->ucontext, &udata);
        if (IS_ERR(cq)) {
                ret = PTR_ERR(cq);
-               goto err;
+               goto err_file;
        }
 
        cq->device        = file->device->ib_dev;
-       cq->uobject       = &uobj->uobject;
+       cq->uobject       = &obj->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);
 
-       down(&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;
+       obj->uobject.object = cq;
+       ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
        if (ret)
-               goto err_up;
+               goto err_free;
 
        memset(&resp, 0, sizeof resp);
-       resp.cq_handle = uobj->uobject.id;
+       resp.cq_handle = obj->uobject.id;
        resp.cqe       = cq->cqe;
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_idr;
+               goto err_copy;
        }
 
-       down(&file->mutex);
-       list_add_tail(&uobj->uobject.list, &file->ucontext->cq_list);
-       up(&file->mutex);
+       mutex_lock(&file->mutex);
+       list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
+       mutex_unlock(&file->mutex);
 
-       up(&ib_uverbs_idr_mutex);
+       obj->uobject.live = 1;
+
+       up_write(&obj->uobject.mutex);
 
        return in_len;
 
-err_idr:
-       idr_remove(&ib_uverbs_cq_idr, uobj->uobject.id);
+err_copy:
+       idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
 
-err_up:
-       up(&ib_uverbs_idr_mutex);
+err_free:
        ib_destroy_cq(cq);
 
+err_file:
+       if (ev_file)
+               ib_uverbs_release_ucq(file, ev_file, obj);
+
 err:
-       kfree(uobj);
+       put_uobj_write(&obj->uobject);
        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, 0);
+       if (!cq)
+               return -EINVAL;
+
+       ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
+       if (ret)
+               goto out;
+
+       resp.cqe = cq->cqe;
+
+       if (copy_to_user((void __user *) (unsigned long) cmd.response,
+                        &resp, sizeof resp.cqe))
+               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)
@@ -692,23 +917,24 @@ ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
                goto out_wc;
        }
 
-       down(&ib_uverbs_idr_mutex);
-       cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
-       if (!cq || cq->uobject->context != file->ucontext) {
+       cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
+       if (!cq) {
                ret = -EINVAL;
                goto out;
        }
 
        resp->count = ib_poll_cq(cq, cmd.ne, wc);
 
+       put_cq_read(cq);
+
        for (i = 0; i < resp->count; i++) {
                resp->wc[i].wr_id          = wc[i].wr_id;
                resp->wc[i].status         = wc[i].status;
                resp->wc[i].opcode         = wc[i].opcode;
                resp->wc[i].vendor_err     = wc[i].vendor_err;
                resp->wc[i].byte_len       = wc[i].byte_len;
-               resp->wc[i].imm_data       = wc[i].imm_data;
-               resp->wc[i].qp_num         = wc[i].qp_num;
+               resp->wc[i].ex.imm_data    = (__u32 __force) wc[i].ex.imm_data;
+               resp->wc[i].qp_num         = wc[i].qp->qp_num;
                resp->wc[i].src_qp         = wc[i].src_qp;
                resp->wc[i].wc_flags       = wc[i].wc_flags;
                resp->wc[i].pkey_index     = wc[i].pkey_index;
@@ -722,7 +948,6 @@ ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
                ret = -EFAULT;
 
 out:
-       up(&ib_uverbs_idr_mutex);
        kfree(resp);
 
 out_wc:
@@ -736,21 +961,20 @@ ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
 {
        struct ib_uverbs_req_notify_cq cmd;
        struct ib_cq                  *cq;
-       int                            ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       down(&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;
-       }
-       up(&ib_uverbs_idr_mutex);
+       cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
+       if (!cq)
+               return -EINVAL;
 
-       return ret;
+       ib_req_notify_cq(cq, cmd.solicited_only ?
+                        IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
+
+       put_cq_read(cq);
+
+       return in_len;
 }
 
 ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
@@ -759,52 +983,50 @@ 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            *uobj;
+       struct ib_ucq_object            *obj;
        struct ib_uverbs_event_file     *ev_file;
-       u64                              user_handle;
        int                              ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       memset(&resp, 0, sizeof resp);
-
-       down(&ib_uverbs_idr_mutex);
+       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);
 
-       cq = idr_find(&ib_uverbs_cq_idr, cmd.cq_handle);
-       if (!cq || cq->uobject->context != file->ucontext)
-               goto out;
+       ret = ib_destroy_cq(cq);
+       if (!ret)
+               uobj->live = 0;
 
-       user_handle = cq->uobject->user_handle;
-       uobj        = container_of(cq->uobject, struct ib_ucq_object, uobject);
-       ev_file     = cq->cq_context;
+       put_uobj_write(uobj);
 
-       ret = ib_destroy_cq(cq);
        if (ret)
-               goto out;
+               return ret;
 
-       idr_remove(&ib_uverbs_cq_idr, cmd.cq_handle);
+       idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
 
-       down(&file->mutex);
-       list_del(&uobj->uobject.list);
-       up(&file->mutex);
+       mutex_lock(&file->mutex);
+       list_del(&uobj->list);
+       mutex_unlock(&file->mutex);
 
-       ib_uverbs_release_ucq(file, ev_file, uobj);
+       ib_uverbs_release_ucq(file, ev_file, obj);
 
-       resp.comp_events_reported  = uobj->comp_events_reported;
-       resp.async_events_reported = uobj->async_events_reported;
+       memset(&resp, 0, sizeof resp);
+       resp.comp_events_reported  = obj->comp_events_reported;
+       resp.async_events_reported = obj->async_events_reported;
 
-       kfree(uobj);
+       put_uobj(uobj);
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp))
-               ret = -EFAULT;
-
-out:
-       up(&ib_uverbs_idr_mutex);
+               return -EFAULT;
 
-       return ret ? ret : in_len;
+       return in_len;
 }
 
 ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
@@ -814,7 +1036,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_uevent_object        *uobj;
+       struct ib_uqp_object           *obj;
        struct ib_pd                   *pd;
        struct ib_cq                   *scq, *rcq;
        struct ib_srq                  *srq;
@@ -832,23 +1054,22 @@ 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);
 
-       uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
-       if (!uobj)
+       obj = kmalloc(sizeof *obj, GFP_KERNEL);
+       if (!obj)
                return -ENOMEM;
 
-       down(&ib_uverbs_idr_mutex);
+       init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
+       down_write(&obj->uevent.uobject.mutex);
 
-       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;
+       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, 0);
+       rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
+               scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);
 
-       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))) {
+       if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {
                ret = -EINVAL;
-               goto err_up;
+               goto err_put;
        }
 
        attr.event_handler = ib_uverbs_qp_event_handler;
@@ -858,6 +1079,7 @@ ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
        attr.srq           = srq;
        attr.sq_sig_type   = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
        attr.qp_type       = cmd.qp_type;
+       attr.create_flags  = 0;
 
        attr.cap.max_send_wr     = cmd.max_send_wr;
        attr.cap.max_recv_wr     = cmd.max_recv_wr;
@@ -865,15 +1087,14 @@ 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;
 
-       uobj->uobject.user_handle = cmd.user_handle;
-       uobj->uobject.context     = file->ucontext;
-       uobj->events_reported     = 0;
-       INIT_LIST_HEAD(&uobj->event_list);
+       obj->uevent.events_reported     = 0;
+       INIT_LIST_HEAD(&obj->uevent.event_list);
+       INIT_LIST_HEAD(&obj->mcast_list);
 
        qp = pd->device->create_qp(pd, &attr, &udata);
        if (IS_ERR(qp)) {
                ret = PTR_ERR(qp);
-               goto err_up;
+               goto err_put;
        }
 
        qp->device        = pd->device;
@@ -881,7 +1102,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       = &uobj->uobject;
+       qp->uobject       = &obj->uevent.uobject;
        qp->event_handler = attr.event_handler;
        qp->qp_context    = attr.qp_context;
        qp->qp_type       = attr.qp_type;
@@ -891,56 +1112,169 @@ ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
        if (attr.srq)
                atomic_inc(&attr.srq->usecnt);
 
-       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->uobject.id);
-
-       if (ret == -EAGAIN)
-               goto retry;
+       obj->uevent.uobject.object = qp;
+       ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
        if (ret)
                goto err_destroy;
 
-       resp.qp_handle = uobj->uobject.id;
+       memset(&resp, 0, sizeof resp);
+       resp.qpn             = qp->qp_num;
+       resp.qp_handle       = obj->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;
+       resp.max_send_wr     = attr.cap.max_send_wr;
+       resp.max_inline_data = attr.cap.max_inline_data;
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_idr;
+               goto err_copy;
        }
 
-       down(&file->mutex);
-       list_add_tail(&uobj->uobject.list, &file->ucontext->qp_list);
-       up(&file->mutex);
+       put_pd_read(pd);
+       put_cq_read(scq);
+       if (rcq != scq)
+               put_cq_read(rcq);
+       if (srq)
+               put_srq_read(srq);
 
-       up(&ib_uverbs_idr_mutex);
+       mutex_lock(&file->mutex);
+       list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
+       mutex_unlock(&file->mutex);
+
+       obj->uevent.uobject.live = 1;
+
+       up_write(&obj->uevent.uobject.mutex);
 
        return in_len;
 
-err_idr:
-       idr_remove(&ib_uverbs_qp_idr, uobj->uobject.id);
+err_copy:
+       idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
 
 err_destroy:
        ib_destroy_qp(qp);
 
-err_up:
-       up(&ib_uverbs_idr_mutex);
-
-       kfree(uobj);
+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.sq_draining            = attr->sq_draining;
+       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;
+
+       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;
+}
+
 ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
                            const char __user *buf, int in_len,
                            int out_len)
 {
        struct ib_uverbs_modify_qp cmd;
+       struct ib_udata            udata;
        struct ib_qp              *qp;
        struct ib_qp_attr         *attr;
        int                        ret;
@@ -948,14 +1282,15 @@ ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
+       INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
+                  out_len);
+
        attr = kmalloc(sizeof *attr, GFP_KERNEL);
        if (!attr)
                return -ENOMEM;
 
-       down(&ib_uverbs_idr_mutex);
-
-       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
-       if (!qp || qp->uobject->context != file->ucontext) {
+       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
+       if (!qp) {
                ret = -EINVAL;
                goto out;
        }
@@ -970,7 +1305,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.pkey_index;
+       attr->alt_pkey_index      = cmd.alt_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;
@@ -1006,14 +1341,16 @@ ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
        attr->alt_ah_attr.ah_flags          = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
        attr->alt_ah_attr.port_num          = cmd.alt_dest.port_num;
 
-       ret = ib_modify_qp(qp, attr, cmd.attr_mask);
+       ret = qp->device->modify_qp(qp, attr, cmd.attr_mask, &udata);
+
+       put_qp_read(qp);
+
        if (ret)
                goto out;
 
        ret = in_len;
 
 out:
-       up(&ib_uverbs_idr_mutex);
        kfree(attr);
 
        return ret;
@@ -1025,8 +1362,9 @@ 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_uevent_object         *uobj;
+       struct ib_uqp_object            *obj;
        int                              ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
@@ -1034,43 +1372,48 @@ ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
 
        memset(&resp, 0, sizeof resp);
 
-       down(&ib_uverbs_idr_mutex);
-
-       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
-       if (!qp || qp->uobject->context != file->ucontext)
-               goto out;
+       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);
 
-       uobj = container_of(qp->uobject, struct ib_uevent_object, uobject);
+       if (!list_empty(&obj->mcast_list)) {
+               put_uobj_write(uobj);
+               return -EBUSY;
+       }
 
        ret = ib_destroy_qp(qp);
+       if (!ret)
+               uobj->live = 0;
+
+       put_uobj_write(uobj);
+
        if (ret)
-               goto out;
+               return ret;
 
-       idr_remove(&ib_uverbs_qp_idr, cmd.qp_handle);
+       idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
 
-       down(&file->mutex);
-       list_del(&uobj->uobject.list);
-       up(&file->mutex);
+       mutex_lock(&file->mutex);
+       list_del(&uobj->list);
+       mutex_unlock(&file->mutex);
 
-       ib_uverbs_release_uevent(file, uobj);
+       ib_uverbs_release_uevent(file, &obj->uevent);
 
-       resp.events_reported = uobj->events_reported;
+       resp.events_reported = obj->uevent.events_reported;
 
-       kfree(uobj);
+       put_uobj(uobj);
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp))
-               ret = -EFAULT;
-
-out:
-       up(&ib_uverbs_idr_mutex);
+               return -EFAULT;
 
-       return ret ? ret : in_len;
+       return 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;
@@ -1078,6 +1421,7 @@ 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))
@@ -1094,12 +1438,11 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
        if (!user_wr)
                return -ENOMEM;
 
-       down(&ib_uverbs_idr_mutex);
-
-       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
-       if (!qp || qp->uobject->context != file->ucontext)
+       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
+       if (!qp)
                goto out;
 
+       is_ud = qp->qp_type == IB_QPT_UD;
        sg_ind = 0;
        last = NULL;
        for (i = 0; i < cmd.wr_count; ++i) {
@@ -1107,12 +1450,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;
+                       goto out_put;
                }
 
                if (user_wr->num_sge + sg_ind > cmd.sge_count) {
                        ret = -EINVAL;
-                       goto out;
+                       goto out_put;
                }
 
                next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
@@ -1120,7 +1463,7 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
                               GFP_KERNEL);
                if (!next) {
                        ret = -ENOMEM;
-                       goto out;
+                       goto out_put;
                }
 
                if (!last)
@@ -1134,27 +1477,36 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
                next->num_sge    = user_wr->num_sge;
                next->opcode     = user_wr->opcode;
                next->send_flags = user_wr->send_flags;
-               next->imm_data   = user_wr->imm_data;
 
-               if (qp->qp_type == IB_QPT_UD) {
-                       next->wr.ud.ah = idr_find(&ib_uverbs_ah_idr,
-                                                 user_wr->wr.ud.ah);
+               if (is_ud) {
+                       next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
+                                                    file->ucontext);
                        if (!next->wr.ud.ah) {
                                ret = -EINVAL;
-                               goto out;
+                               goto out_put;
                        }
                        next->wr.ud.remote_qpn  = user_wr->wr.ud.remote_qpn;
                        next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
                } else {
                        switch (next->opcode) {
-                       case IB_WR_RDMA_WRITE:
                        case IB_WR_RDMA_WRITE_WITH_IMM:
+                               next->ex.imm_data =
+                                       (__be32 __force) user_wr->ex.imm_data;
+                       case IB_WR_RDMA_WRITE:
                        case IB_WR_RDMA_READ:
                                next->wr.rdma.remote_addr =
                                        user_wr->wr.rdma.remote_addr;
                                next->wr.rdma.rkey        =
                                        user_wr->wr.rdma.rkey;
                                break;
+                       case IB_WR_SEND_WITH_IMM:
+                               next->ex.imm_data =
+                                       (__be32 __force) user_wr->ex.imm_data;
+                               break;
+                       case IB_WR_SEND_WITH_INV:
+                               next->ex.invalidate_rkey =
+                                       user_wr->ex.invalidate_rkey;
+                               break;
                        case IB_WR_ATOMIC_CMP_AND_SWP:
                        case IB_WR_ATOMIC_FETCH_AND_ADD:
                                next->wr.atomic.remote_addr =
@@ -1178,7 +1530,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;
+                               goto out_put;
                        }
                        sg_ind += next->num_sge;
                } else
@@ -1198,15 +1550,18 @@ ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
                         &resp, sizeof resp))
                ret = -EFAULT;
 
-out:
-       up(&ib_uverbs_idr_mutex);
+out_put:
+       put_qp_read(qp);
 
        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;
@@ -1298,8 +1653,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;
@@ -1316,14 +1671,15 @@ ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
        if (IS_ERR(wr))
                return PTR_ERR(wr);
 
-       down(&ib_uverbs_idr_mutex);
-
-       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
-       if (!qp || qp->uobject->context != file->ucontext)
+       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
+       if (!qp)
                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;
@@ -1331,14 +1687,11 @@ ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
                                break;
                }
 
-
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp))
                ret = -EFAULT;
 
 out:
-       up(&ib_uverbs_idr_mutex);
-
        while (wr) {
                next = wr->next;
                kfree(wr);
@@ -1349,8 +1702,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;
@@ -1367,14 +1720,15 @@ ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
        if (IS_ERR(wr))
                return PTR_ERR(wr);
 
-       down(&ib_uverbs_idr_mutex);
-
-       srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
-       if (!srq || srq->uobject->context != file->ucontext)
+       srq = idr_read_srq(cmd.srq_handle, file->ucontext);
+       if (!srq)
                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;
@@ -1382,14 +1736,11 @@ ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
                                break;
                }
 
-
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp))
                ret = -EFAULT;
 
 out:
-       up(&ib_uverbs_idr_mutex);
-
        while (wr) {
                next = wr->next;
                kfree(wr);
@@ -1421,21 +1772,20 @@ ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
        if (!uobj)
                return -ENOMEM;
 
-       down(&ib_uverbs_idr_mutex);
+       init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_key);
+       down_write(&uobj->mutex);
 
-       pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
-       if (!pd || pd->uobject->context != file->ucontext) {
+       pd = idr_read_pd(cmd.pd_handle, file->ucontext);
+       if (!pd) {
                ret = -EINVAL;
-               goto err_up;
+               goto err;
        }
 
-       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;
        attr.static_rate       = cmd.attr.static_rate;
+       attr.ah_flags          = cmd.attr.is_global ? IB_AH_GRH : 0;
        attr.port_num          = cmd.attr.port_num;
        attr.grh.flow_label    = cmd.attr.grh.flow_label;
        attr.grh.sgid_index    = cmd.attr.grh.sgid_index;
@@ -1446,21 +1796,13 @@ 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_up;
+               goto err_put;
        }
 
-       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);
-
-       if (ret == -EAGAIN)
-               goto retry;
+       ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
        if (ret)
                goto err_destroy;
 
@@ -1469,27 +1811,32 @@ retry:
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_idr;
+               goto err_copy;
        }
 
-       down(&file->mutex);
+       put_pd_read(pd);
+
+       mutex_lock(&file->mutex);
        list_add_tail(&uobj->list, &file->ucontext->ah_list);
-       up(&file->mutex);
+       mutex_unlock(&file->mutex);
+
+       uobj->live = 1;
 
-       up(&ib_uverbs_idr_mutex);
+       up_write(&uobj->mutex);
 
        return in_len;
 
-err_idr:
-       idr_remove(&ib_uverbs_ah_idr, uobj->id);
+err_copy:
+       idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
 
 err_destroy:
        ib_destroy_ah(ah);
 
-err_up:
-       up(&ib_uverbs_idr_mutex);
+err_put:
+       put_pd_read(pd);
 
-       kfree(uobj);
+err:
+       put_uobj_write(uobj);
        return ret;
 }
 
@@ -1499,35 +1846,34 @@ 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 = -EINVAL;
+       int                         ret;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       down(&ib_uverbs_idr_mutex);
+       uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
+       if (!uobj)
+               return -EINVAL;
+       ah = uobj->object;
 
-       ah = idr_find(&ib_uverbs_ah_idr, cmd.ah_handle);
-       if (!ah || ah->uobject->context != file->ucontext)
-               goto out;
+       ret = ib_destroy_ah(ah);
+       if (!ret)
+               uobj->live = 0;
 
-       uobj = ah->uobject;
+       put_uobj_write(uobj);
 
-       ret = ib_destroy_ah(ah);
        if (ret)
-               goto out;
+               return ret;
 
-       idr_remove(&ib_uverbs_ah_idr, cmd.ah_handle);
+       idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
 
-       down(&file->mutex);
+       mutex_lock(&file->mutex);
        list_del(&uobj->list);
-       up(&file->mutex);
-
-       kfree(uobj);
+       mutex_unlock(&file->mutex);
 
-out:
-       up(&ib_uverbs_idr_mutex);
+       put_uobj(uobj);
 
-       return ret ? ret : in_len;
+       return in_len;
 }
 
 ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
@@ -1536,18 +1882,43 @@ ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
 {
        struct ib_uverbs_attach_mcast cmd;
        struct ib_qp                 *qp;
-       int                           ret = -EINVAL;
+       struct ib_uqp_object         *obj;
+       struct ib_uverbs_mcast_entry *mcast;
+       int                           ret;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       down(&ib_uverbs_idr_mutex);
+       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
+       if (!qp)
+               return -EINVAL;
 
-       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
-       if (qp && qp->uobject->context == file->ucontext)
-               ret = ib_attach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
+       obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
 
-       up(&ib_uverbs_idr_mutex);
+       list_for_each_entry(mcast, &obj->mcast_list, list)
+               if (cmd.mlid == mcast->lid &&
+                   !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
+                       ret = 0;
+                       goto out_put;
+               }
+
+       mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
+       if (!mcast) {
+               ret = -ENOMEM;
+               goto out_put;
+       }
+
+       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
+               kfree(mcast);
+
+out_put:
+       put_qp_read(qp);
 
        return ret ? ret : in_len;
 }
@@ -1557,19 +1928,34 @@ 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_qp                 *qp;
+       struct ib_uverbs_mcast_entry *mcast;
        int                           ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       down(&ib_uverbs_idr_mutex);
+       qp = idr_read_qp(cmd.qp_handle, file->ucontext);
+       if (!qp)
+               return -EINVAL;
 
-       qp = idr_find(&ib_uverbs_qp_idr, cmd.qp_handle);
-       if (qp && qp->uobject->context == file->ucontext)
-               ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
+       ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
+       if (ret)
+               goto out_put;
+
+       obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
+
+       list_for_each_entry(mcast, &obj->mcast_list, list)
+               if (cmd.mlid == mcast->lid &&
+                   !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
+                       list_del(&mcast->list);
+                       kfree(mcast);
+                       break;
+               }
 
-       up(&ib_uverbs_idr_mutex);
+out_put:
+       put_qp_read(qp);
 
        return ret ? ret : in_len;
 }
@@ -1581,7 +1967,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         *uobj;
+       struct ib_uevent_object         *obj;
        struct ib_pd                    *pd;
        struct ib_srq                   *srq;
        struct ib_srq_init_attr          attr;
@@ -1597,17 +1983,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);
 
-       uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
-       if (!uobj)
+       obj = kmalloc(sizeof *obj, GFP_KERNEL);
+       if (!obj)
                return -ENOMEM;
 
-       down(&ib_uverbs_idr_mutex);
-
-       pd  = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);
+       init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &srq_lock_key);
+       down_write(&obj->uobject.mutex);
 
-       if (!pd || pd->uobject->context != file->ucontext) {
+       pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
+       if (!pd) {
                ret = -EINVAL;
-               goto err_up;
+               goto err;
        }
 
        attr.event_handler  = ib_uverbs_srq_event_handler;
@@ -1616,66 +2002,62 @@ 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;
 
-       uobj->uobject.user_handle = cmd.user_handle;
-       uobj->uobject.context     = file->ucontext;
-       uobj->events_reported     = 0;
-       INIT_LIST_HEAD(&uobj->event_list);
+       obj->events_reported     = 0;
+       INIT_LIST_HEAD(&obj->event_list);
 
        srq = pd->device->create_srq(pd, &attr, &udata);
        if (IS_ERR(srq)) {
                ret = PTR_ERR(srq);
-               goto err_up;
+               goto err_put;
        }
 
        srq->device        = pd->device;
        srq->pd            = pd;
-       srq->uobject       = &uobj->uobject;
+       srq->uobject       = &obj->uobject;
        srq->event_handler = attr.event_handler;
        srq->srq_context   = attr.srq_context;
        atomic_inc(&pd->usecnt);
        atomic_set(&srq->usecnt, 0);
 
-       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;
+       obj->uobject.object = srq;
+       ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
        if (ret)
                goto err_destroy;
 
-       resp.srq_handle = uobj->uobject.id;
+       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;
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp)) {
                ret = -EFAULT;
-               goto err_idr;
+               goto err_copy;
        }
 
-       down(&file->mutex);
-       list_add_tail(&uobj->uobject.list, &file->ucontext->srq_list);
-       up(&file->mutex);
+       put_pd_read(pd);
 
-       up(&ib_uverbs_idr_mutex);
+       mutex_lock(&file->mutex);
+       list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
+       mutex_unlock(&file->mutex);
+
+       obj->uobject.live = 1;
+
+       up_write(&obj->uobject.mutex);
 
        return in_len;
 
-err_idr:
-       idr_remove(&ib_uverbs_srq_idr, uobj->uobject.id);
+err_copy:
+       idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
 
 err_destroy:
        ib_destroy_srq(srq);
 
-err_up:
-       up(&ib_uverbs_idr_mutex);
+err_put:
+       put_pd_read(pd);
 
-       kfree(uobj);
+err:
+       put_uobj_write(&obj->uobject);
        return ret;
 }
 
@@ -1684,6 +2066,7 @@ ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
                             int out_len)
 {
        struct ib_uverbs_modify_srq cmd;
+       struct ib_udata             udata;
        struct ib_srq              *srq;
        struct ib_srq_attr          attr;
        int                         ret;
@@ -1691,71 +2074,108 @@ ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       down(&ib_uverbs_idr_mutex);
+       INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
+                  out_len);
 
-       srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
-       if (!srq || srq->uobject->context != file->ucontext) {
-               ret = -EINVAL;
-               goto out;
-       }
+       srq = idr_read_srq(cmd.srq_handle, file->ucontext);
+       if (!srq)
+               return -EINVAL;
 
        attr.max_wr    = cmd.max_wr;
-       attr.max_sge   = cmd.max_sge;
        attr.srq_limit = cmd.srq_limit;
 
-       ret = ib_modify_srq(srq, &attr, cmd.attr_mask);
+       ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
 
-out:
-       up(&ib_uverbs_idr_mutex);
+       put_srq_read(srq);
 
        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          *uobj;
+       struct ib_uevent_object          *obj;
        int                               ret = -EINVAL;
 
        if (copy_from_user(&cmd, buf, sizeof cmd))
                return -EFAULT;
 
-       down(&ib_uverbs_idr_mutex);
-
-       memset(&resp, 0, sizeof resp);
+       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);
 
-       srq = idr_find(&ib_uverbs_srq_idr, cmd.srq_handle);
-       if (!srq || srq->uobject->context != file->ucontext)
-               goto out;
+       ret = ib_destroy_srq(srq);
+       if (!ret)
+               uobj->live = 0;
 
-       uobj = container_of(srq->uobject, struct ib_uevent_object, uobject);
+       put_uobj_write(uobj);
 
-       ret = ib_destroy_srq(srq);
        if (ret)
-               goto out;
+               return ret;
 
-       idr_remove(&ib_uverbs_srq_idr, cmd.srq_handle);
+       idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
 
-       down(&file->mutex);
-       list_del(&uobj->uobject.list);
-       up(&file->mutex);
+       mutex_lock(&file->mutex);
+       list_del(&uobj->list);
+       mutex_unlock(&file->mutex);
 
-       ib_uverbs_release_uevent(file, uobj);
+       ib_uverbs_release_uevent(file, obj);
 
-       resp.events_reported = uobj->events_reported;
+       memset(&resp, 0, sizeof resp);
+       resp.events_reported = obj->events_reported;
 
-       kfree(uobj);
+       put_uobj(uobj);
 
        if (copy_to_user((void __user *) (unsigned long) cmd.response,
                         &resp, sizeof resp))
                ret = -EFAULT;
 
-out:
-       up(&ib_uverbs_idr_mutex);
-
        return ret ? ret : in_len;
 }