IB/core: Add idr based standard types
authorMatan Barak <matanb@mellanox.com>
Tue, 4 Apr 2017 10:31:43 +0000 (13:31 +0300)
committerDoug Ledford <dledford@redhat.com>
Wed, 5 Apr 2017 17:28:04 +0000 (13:28 -0400)
This patch adds the standard idr based types. These types are
used in downstream patches in order to initialize, destroy and
lookup IB standard objects which are based on idr objects.

An idr object requires filling out several parameters. Its op pointer
should point to uverbs_idr_ops and its size should be at least the
size of ib_uobject. We add a macro to make the type declaration easier.

Signed-off-by: Matan Barak <matanb@mellanox.com>
Reviewed-by: Yishai Hadas <yishaih@mellanox.com>
Reviewed-by: Sean Hefty <sean.hefty@intel.com>
Signed-off-by: Doug Ledford <dledford@redhat.com>
drivers/infiniband/core/Makefile
drivers/infiniband/core/uverbs.h
drivers/infiniband/core/uverbs_cmd.c
drivers/infiniband/core/uverbs_main.c
drivers/infiniband/core/uverbs_std_types.c [new file with mode: 0644]
include/rdma/uverbs_std_types.h [new file with mode: 0644]
include/rdma/uverbs_types.h

index d29f910d98c9bbedb1fd1073ba1018d0b6d83aa7..6ebd9ad95010ed8a82a4f4ba4d16de19198dd145 100644 (file)
@@ -30,4 +30,4 @@ ib_umad-y :=                  user_mad.o
 ib_ucm-y :=                    ucm.o
 
 ib_uverbs-y :=                 uverbs_main.o uverbs_cmd.o uverbs_marshall.o \
-                               rdma_core.o
+                               rdma_core.o uverbs_std_types.o
index 6215735fa98e64365c6e0b2cec90f281f9ec00b7..cf0519dff3a24d842f4f89fa356e4ac4225c89e3 100644 (file)
@@ -201,9 +201,12 @@ void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr);
 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
 void ib_uverbs_event_handler(struct ib_event_handler *handler,
                             struct ib_event *event);
-void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev, struct ib_xrcd *xrcd);
+int ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev, struct ib_xrcd *xrcd,
+                          enum rdma_remove_reason why);
 
 int uverbs_dealloc_mw(struct ib_mw *mw);
+void ib_uverbs_detach_umcast(struct ib_qp *qp,
+                            struct ib_uqp_object *uobj);
 
 struct ib_uverbs_flow_spec {
        union {
index 03c4f68a88e1b0aaec32888774a2c91e048f05e9..79de69dc9f1c2cc091e2f2c28893400a9ad0883c 100644 (file)
@@ -958,19 +958,25 @@ out:
        return ret;
 }
 
-void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
-                           struct ib_xrcd *xrcd)
+int ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
+                          struct ib_xrcd *xrcd,
+                          enum rdma_remove_reason why)
 {
        struct inode *inode;
+       int ret;
 
        inode = xrcd->inode;
        if (inode && !atomic_dec_and_test(&xrcd->usecnt))
-               return;
+               return 0;
 
-       ib_dealloc_xrcd(xrcd);
+       ret = ib_dealloc_xrcd(xrcd);
 
-       if (inode)
+       if (why == RDMA_REMOVE_DESTROY && ret)
+               atomic_inc(&xrcd->usecnt);
+       else if (inode)
                xrcd_table_delete(dev, inode);
+
+       return ret;
 }
 
 ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
index f6812fb6cd0ce25225be0899858c2b772d7544ce..e1db6782d0d4f5ff0cf1a155d47aaebb9ca41089 100644 (file)
@@ -201,8 +201,8 @@ void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
        spin_unlock_irq(&file->async_file->lock);
 }
 
-static void ib_uverbs_detach_umcast(struct ib_qp *qp,
-                                   struct ib_uqp_object *uobj)
+void ib_uverbs_detach_umcast(struct ib_qp *qp,
+                            struct ib_uqp_object *uobj)
 {
        struct ib_uverbs_mcast_entry *mcast, *tmp;
 
@@ -331,7 +331,9 @@ static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
                        container_of(uobj, struct ib_uxrcd_object, uobject);
 
                idr_remove_uobj(uobj);
-               ib_uverbs_dealloc_xrcd(file->device, xrcd);
+               ib_uverbs_dealloc_xrcd(file->device, xrcd,
+                                      file->ucontext ? RDMA_REMOVE_CLOSE :
+                                      RDMA_REMOVE_DRIVER_REMOVE);
                kfree(uxrcd);
        }
        mutex_unlock(&file->device->xrcd_tree_mutex);
diff --git a/drivers/infiniband/core/uverbs_std_types.c b/drivers/infiniband/core/uverbs_std_types.c
new file mode 100644 (file)
index 0000000..a514556
--- /dev/null
@@ -0,0 +1,244 @@
+/*
+ * Copyright (c) 2017, Mellanox Technologies inc.  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
+ * General Public License (GPL) Version 2, available from the file
+ * COPYING in the main directory of this source tree, or the
+ * OpenIB.org BSD license below:
+ *
+ *     Redistribution and use in source and binary forms, with or
+ *     without modification, are permitted provided that the following
+ *     conditions are met:
+ *
+ *      - Redistributions of source code must retain the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer.
+ *
+ *      - Redistributions in binary form must reproduce the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * 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.
+ */
+
+#include <rdma/uverbs_std_types.h>
+#include <rdma/ib_user_verbs.h>
+#include <rdma/ib_verbs.h>
+#include <linux/bug.h>
+#include <linux/file.h>
+#include "rdma_core.h"
+#include "uverbs.h"
+
+int uverbs_free_ah(struct ib_uobject *uobject,
+                  enum rdma_remove_reason why)
+{
+       return ib_destroy_ah((struct ib_ah *)uobject->object);
+}
+
+int uverbs_free_flow(struct ib_uobject *uobject,
+                    enum rdma_remove_reason why)
+{
+       return ib_destroy_flow((struct ib_flow *)uobject->object);
+}
+
+int uverbs_free_mw(struct ib_uobject *uobject,
+                  enum rdma_remove_reason why)
+{
+       return uverbs_dealloc_mw((struct ib_mw *)uobject->object);
+}
+
+int uverbs_free_qp(struct ib_uobject *uobject,
+                  enum rdma_remove_reason why)
+{
+       struct ib_qp *qp = uobject->object;
+       struct ib_uqp_object *uqp =
+               container_of(uobject, struct ib_uqp_object, uevent.uobject);
+       int ret;
+
+       if (why == RDMA_REMOVE_DESTROY) {
+               if (!list_empty(&uqp->mcast_list))
+                       return -EBUSY;
+       } else if (qp == qp->real_qp) {
+               ib_uverbs_detach_umcast(qp, uqp);
+       }
+
+       ret = ib_destroy_qp(qp);
+       if (ret && why == RDMA_REMOVE_DESTROY)
+               return ret;
+
+       if (uqp->uxrcd)
+               atomic_dec(&uqp->uxrcd->refcnt);
+
+       ib_uverbs_release_uevent(uobject->context->ufile, &uqp->uevent);
+       return ret;
+}
+
+int uverbs_free_rwq_ind_tbl(struct ib_uobject *uobject,
+                           enum rdma_remove_reason why)
+{
+       struct ib_rwq_ind_table *rwq_ind_tbl = uobject->object;
+       struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl;
+       int ret;
+
+       ret = ib_destroy_rwq_ind_table(rwq_ind_tbl);
+       if (!ret || why != RDMA_REMOVE_DESTROY)
+               kfree(ind_tbl);
+       return ret;
+}
+
+int uverbs_free_wq(struct ib_uobject *uobject,
+                  enum rdma_remove_reason why)
+{
+       struct ib_wq *wq = uobject->object;
+       struct ib_uwq_object *uwq =
+               container_of(uobject, struct ib_uwq_object, uevent.uobject);
+       int ret;
+
+       ret = ib_destroy_wq(wq);
+       if (!ret || why != RDMA_REMOVE_DESTROY)
+               ib_uverbs_release_uevent(uobject->context->ufile, &uwq->uevent);
+       return ret;
+}
+
+int uverbs_free_srq(struct ib_uobject *uobject,
+                   enum rdma_remove_reason why)
+{
+       struct ib_srq *srq = uobject->object;
+       struct ib_uevent_object *uevent =
+               container_of(uobject, struct ib_uevent_object, uobject);
+       enum ib_srq_type  srq_type = srq->srq_type;
+       int ret;
+
+       ret = ib_destroy_srq(srq);
+
+       if (ret && why == RDMA_REMOVE_DESTROY)
+               return ret;
+
+       if (srq_type == IB_SRQT_XRC) {
+               struct ib_usrq_object *us =
+                       container_of(uevent, struct ib_usrq_object, uevent);
+
+               atomic_dec(&us->uxrcd->refcnt);
+       }
+
+       ib_uverbs_release_uevent(uobject->context->ufile, uevent);
+       return ret;
+}
+
+int uverbs_free_cq(struct ib_uobject *uobject,
+                  enum rdma_remove_reason why)
+{
+       struct ib_cq *cq = uobject->object;
+       struct ib_uverbs_event_file *ev_file = cq->cq_context;
+       struct ib_ucq_object *ucq =
+               container_of(uobject, struct ib_ucq_object, uobject);
+       int ret;
+
+       ret = ib_destroy_cq(cq);
+       if (!ret || why != RDMA_REMOVE_DESTROY)
+               ib_uverbs_release_ucq(uobject->context->ufile, ev_file, ucq);
+       return ret;
+}
+
+int uverbs_free_mr(struct ib_uobject *uobject,
+                  enum rdma_remove_reason why)
+{
+       return ib_dereg_mr((struct ib_mr *)uobject->object);
+}
+
+int uverbs_free_xrcd(struct ib_uobject *uobject,
+                    enum rdma_remove_reason why)
+{
+       struct ib_xrcd *xrcd = uobject->object;
+       struct ib_uxrcd_object *uxrcd =
+               container_of(uobject, struct ib_uxrcd_object, uobject);
+       int ret;
+
+       mutex_lock(&uobject->context->ufile->device->xrcd_tree_mutex);
+       if (why == RDMA_REMOVE_DESTROY && atomic_read(&uxrcd->refcnt))
+               ret = -EBUSY;
+       else
+               ret = ib_uverbs_dealloc_xrcd(uobject->context->ufile->device,
+                                            xrcd, why);
+       mutex_unlock(&uobject->context->ufile->device->xrcd_tree_mutex);
+
+       return ret;
+}
+
+int uverbs_free_pd(struct ib_uobject *uobject,
+                  enum rdma_remove_reason why)
+{
+       struct ib_pd *pd = uobject->object;
+
+       if (why == RDMA_REMOVE_DESTROY && atomic_read(&pd->usecnt))
+               return -EBUSY;
+
+       ib_dealloc_pd((struct ib_pd *)uobject->object);
+       return 0;
+}
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_cq = {
+       .type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_ucq_object), 0),
+       .destroy_object = uverbs_free_cq,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_qp = {
+       .type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uqp_object), 0),
+       .destroy_object = uverbs_free_qp,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_mw = {
+       .type = UVERBS_TYPE_ALLOC_IDR(0),
+       .destroy_object = uverbs_free_mw,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_mr = {
+       /* 1 is used in order to free the MR after all the MWs */
+       .type = UVERBS_TYPE_ALLOC_IDR(1),
+       .destroy_object = uverbs_free_mr,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_srq = {
+       .type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_usrq_object), 0),
+       .destroy_object = uverbs_free_srq,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_ah = {
+       .type = UVERBS_TYPE_ALLOC_IDR(0),
+       .destroy_object = uverbs_free_ah,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_flow = {
+       .type = UVERBS_TYPE_ALLOC_IDR(0),
+       .destroy_object = uverbs_free_flow,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_wq = {
+       .type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uwq_object), 0),
+       .destroy_object = uverbs_free_wq,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_rwq_ind_table = {
+       .type = UVERBS_TYPE_ALLOC_IDR(0),
+       .destroy_object = uverbs_free_rwq_ind_tbl,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_xrcd = {
+       .type = UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uxrcd_object), 0),
+       .destroy_object = uverbs_free_xrcd,
+};
+
+const struct uverbs_obj_idr_type uverbs_type_attrs_pd = {
+       /* 2 is used in order to free the PD after MRs */
+       .type = UVERBS_TYPE_ALLOC_IDR(2),
+       .destroy_object = uverbs_free_pd,
+};
diff --git a/include/rdma/uverbs_std_types.h b/include/rdma/uverbs_std_types.h
new file mode 100644 (file)
index 0000000..2edb776
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2017, Mellanox Technologies inc.  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
+ * General Public License (GPL) Version 2, available from the file
+ * COPYING in the main directory of this source tree, or the
+ * OpenIB.org BSD license below:
+ *
+ *     Redistribution and use in source and binary forms, with or
+ *     without modification, are permitted provided that the following
+ *     conditions are met:
+ *
+ *      - Redistributions of source code must retain the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer.
+ *
+ *      - Redistributions in binary form must reproduce the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * 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.
+ */
+
+#ifndef _UVERBS_STD_TYPES__
+#define _UVERBS_STD_TYPES__
+
+#include <rdma/uverbs_types.h>
+
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_cq;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_qp;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_rwq_ind_table;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_wq;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_srq;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_ah;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_flow;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_mr;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_mw;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_pd;
+extern const struct uverbs_obj_idr_type uverbs_type_attrs_xrcd;
+#endif
+
index 0777e405f22a08cfc9e628635ebf2bb4b1568e28..66368b5a30066515626d003c7a8e8f57fbd92f5e 100644 (file)
@@ -129,4 +129,18 @@ void rdma_alloc_abort_uobject(struct ib_uobject *uobj);
 int __must_check rdma_remove_commit_uobject(struct ib_uobject *uobj);
 int rdma_alloc_commit_uobject(struct ib_uobject *uobj);
 
+extern const struct uverbs_obj_type_class uverbs_idr_class;
+
+#define UVERBS_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) -    \
+                                  sizeof(char))
+#define UVERBS_TYPE_ALLOC_IDR_SZ(_size, _order)                                \
+       {                                                               \
+               .destroy_order = _order,                                \
+               .type_class = &uverbs_idr_class,                        \
+               .obj_size = (_size) +                                   \
+                         UVERBS_BUILD_BUG_ON((_size) <                 \
+                                             sizeof(struct ib_uobject)), \
+       }
+#define UVERBS_TYPE_ALLOC_IDR(_order)                                  \
+        UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uobject), _order)
 #endif