IB/uverbs: Add alloc/free dm uverbs ioctl support
authorAriel Levkovich <lariel@mellanox.com>
Thu, 5 Apr 2018 15:53:24 +0000 (18:53 +0300)
committerJason Gunthorpe <jgg@mellanox.com>
Thu, 5 Apr 2018 17:16:39 +0000 (11:16 -0600)
This change adds uverbs support for allocation/freeing
of device memory commands.

A new uverbs object is defined of type idr to represent
and track the new resource type allocation per context.

The API requires provider driver to implement 2 new ib_device
callbacks - one for allocation and one for deallocation which
return and accept (respectively) the ib_dm object which represents
the allocated memory on the device.

The support is added via the ioctl command infrastructure
only.

Signed-off-by: Ariel Levkovich <lariel@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
drivers/infiniband/core/Makefile
drivers/infiniband/core/uverbs.h
drivers/infiniband/core/uverbs_std_types.c
drivers/infiniband/core/uverbs_std_types_dm.c [new file with mode: 0644]
include/rdma/ib_verbs.h
include/uapi/rdma/ib_user_ioctl_cmds.h

index 445c5504f605892944d11a5630187741a9fed7c0..636da34f83086f9cb1e92340a66bb8043469ad6d 100644 (file)
@@ -35,4 +35,4 @@ ib_ucm-y :=                   ucm.o
 ib_uverbs-y :=                 uverbs_main.o uverbs_cmd.o uverbs_marshall.o \
                                rdma_core.o uverbs_std_types.o uverbs_ioctl.o \
                                uverbs_ioctl_merge.o uverbs_std_types_cq.o \
-                               uverbs_std_types_flow_action.o
+                               uverbs_std_types_flow_action.o uverbs_std_types_dm.o
index 3229e87d03cb6d84db5278295910edf9c037bb25..cfb51618ab7a7366255c3c426cd8aa4f03bb3a20 100644 (file)
@@ -286,6 +286,7 @@ extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_WQ);
 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_RWQ_IND_TBL);
 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_XRCD);
 extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_FLOW_ACTION);
+extern const struct uverbs_object_def UVERBS_OBJECT(UVERBS_OBJECT_DM);
 
 #define IB_UVERBS_DECLARE_CMD(name)                                    \
        ssize_t ib_uverbs_##name(struct ib_uverbs_file *file,           \
index 173eab8d34823b050dc1d90a11a02b1946f2757e..4fedf59ec3963acd71d32b5a0bf92cd294b84c70 100644 (file)
@@ -311,7 +311,8 @@ static DECLARE_UVERBS_OBJECT_TREE(uverbs_default_objects,
                                  &UVERBS_OBJECT(UVERBS_OBJECT_WQ),
                                  &UVERBS_OBJECT(UVERBS_OBJECT_RWQ_IND_TBL),
                                  &UVERBS_OBJECT(UVERBS_OBJECT_XRCD),
-                                 &UVERBS_OBJECT(UVERBS_OBJECT_FLOW_ACTION));
+                                 &UVERBS_OBJECT(UVERBS_OBJECT_FLOW_ACTION),
+                                 &UVERBS_OBJECT(UVERBS_OBJECT_DM));
 
 const struct uverbs_object_tree_def *uverbs_default_get_objects(void)
 {
diff --git a/drivers/infiniband/core/uverbs_std_types_dm.c b/drivers/infiniband/core/uverbs_std_types_dm.c
new file mode 100644 (file)
index 0000000..8b68157
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018, 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 "uverbs.h"
+#include <rdma/uverbs_std_types.h>
+
+static int uverbs_free_dm(struct ib_uobject *uobject,
+                         enum rdma_remove_reason why)
+{
+       struct ib_dm *dm = uobject->object;
+
+       if (why == RDMA_REMOVE_DESTROY && atomic_read(&dm->usecnt))
+               return -EBUSY;
+
+       return dm->device->dealloc_dm(dm);
+}
+
+static int UVERBS_HANDLER(UVERBS_METHOD_DM_ALLOC)(struct ib_device *ib_dev,
+                                                 struct ib_uverbs_file *file,
+                                                 struct uverbs_attr_bundle *attrs)
+{
+       struct ib_ucontext *ucontext = file->ucontext;
+       struct ib_dm_alloc_attr attr = {};
+       struct ib_uobject *uobj;
+       struct ib_dm *dm;
+       int ret;
+
+       if (!ib_dev->alloc_dm)
+               return -EOPNOTSUPP;
+
+       ret = uverbs_copy_from(&attr.length, attrs,
+                              UVERBS_ATTR_ALLOC_DM_LENGTH);
+       if (ret)
+               return ret;
+
+       ret = uverbs_copy_from(&attr.alignment, attrs,
+                              UVERBS_ATTR_ALLOC_DM_ALIGNMENT);
+       if (ret)
+               return ret;
+
+       uobj = uverbs_attr_get(attrs, UVERBS_ATTR_ALLOC_DM_HANDLE)->obj_attr.uobject;
+
+       dm = ib_dev->alloc_dm(ib_dev, ucontext, &attr, attrs);
+       if (IS_ERR(dm))
+               return PTR_ERR(dm);
+
+       dm->device  = ib_dev;
+       dm->length  = attr.length;
+       dm->uobject = uobj;
+       atomic_set(&dm->usecnt, 0);
+
+       uobj->object = dm;
+
+       return 0;
+}
+
+static DECLARE_UVERBS_NAMED_METHOD(UVERBS_METHOD_DM_ALLOC,
+       &UVERBS_ATTR_IDR(UVERBS_ATTR_ALLOC_DM_HANDLE, UVERBS_OBJECT_DM,
+                        UVERBS_ACCESS_NEW,
+                        UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
+       &UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_LENGTH,
+                           UVERBS_ATTR_TYPE(u64),
+                           UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
+       &UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_ALIGNMENT,
+                           UVERBS_ATTR_TYPE(u32),
+                           UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)));
+
+static DECLARE_UVERBS_NAMED_METHOD_WITH_HANDLER(UVERBS_METHOD_DM_FREE,
+       uverbs_destroy_def_handler,
+       &UVERBS_ATTR_IDR(UVERBS_ATTR_FREE_DM_HANDLE,
+                        UVERBS_OBJECT_DM,
+                        UVERBS_ACCESS_DESTROY,
+                        UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)));
+
+DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_DM,
+                           /* 1 is used in order to free the DM after MRs */
+                           &UVERBS_TYPE_ALLOC_IDR(1, uverbs_free_dm),
+                           &UVERBS_METHOD(UVERBS_METHOD_DM_ALLOC),
+                           &UVERBS_METHOD(UVERBS_METHOD_DM_FREE));
index ed425627efd8812c76cd965907d5d24561b1d994..6806c4f5657a16e83fec6d0a613647f908e4ebe1 100644 (file)
@@ -321,6 +321,12 @@ struct ib_cq_caps {
        u16     max_cq_moderation_period;
 };
 
+struct ib_dm_alloc_attr {
+       u64     length;
+       u32     alignment;
+       u32     flags;
+};
+
 struct ib_device_attr {
        u64                     fw_ver;
        __be64                  sys_image_guid;
@@ -1769,6 +1775,14 @@ struct ib_qp {
        struct rdma_restrack_entry     res;
 };
 
+struct ib_dm {
+       struct ib_device  *device;
+       u32                length;
+       u32                flags;
+       struct ib_uobject *uobject;
+       atomic_t           usecnt;
+};
+
 struct ib_mr {
        struct ib_device  *device;
        struct ib_pd      *pd;
@@ -2425,7 +2439,11 @@ struct ib_device {
        int                        (*modify_flow_action_esp)(struct ib_flow_action *action,
                                                             const struct ib_flow_action_attrs_esp *attr,
                                                             struct uverbs_attr_bundle *attrs);
-
+       struct ib_dm *             (*alloc_dm)(struct ib_device *device,
+                                              struct ib_ucontext *context,
+                                              struct ib_dm_alloc_attr *attr,
+                                              struct uverbs_attr_bundle *attrs);
+       int                        (*dealloc_dm)(struct ib_dm *dm);
        /**
         * rdma netdev operation
         *
index 500b64a444adb9dbf1a99589f53c017bcf353c0c..6034df2625c607bb74f96777176f670ed79def3a 100644 (file)
@@ -54,6 +54,7 @@ enum uverbs_default_objects {
        UVERBS_OBJECT_RWQ_IND_TBL,
        UVERBS_OBJECT_WQ,
        UVERBS_OBJECT_FLOW_ACTION,
+       UVERBS_OBJECT_DM,
 };
 
 enum {
@@ -100,4 +101,18 @@ enum uverbs_methods_actions_flow_action_ops {
        UVERBS_METHOD_FLOW_ACTION_ESP_MODIFY,
 };
 
+enum uverbs_attrs_alloc_dm_cmd_attr_ids {
+       UVERBS_ATTR_ALLOC_DM_HANDLE,
+       UVERBS_ATTR_ALLOC_DM_LENGTH,
+       UVERBS_ATTR_ALLOC_DM_ALIGNMENT,
+};
+
+enum uverbs_attrs_free_dm_cmd_attr_ids {
+       UVERBS_ATTR_FREE_DM_HANDLE,
+};
+
+enum uverbs_methods_dm {
+       UVERBS_METHOD_DM_ALLOC,
+       UVERBS_METHOD_DM_FREE,
+};
 #endif