IB/uverbs: Add device memory registration ioctl support
authorAriel Levkovich <lariel@mellanox.com>
Thu, 5 Apr 2018 15:53:25 +0000 (18:53 +0300)
committerJason Gunthorpe <jgg@mellanox.com>
Thu, 5 Apr 2018 17:16:39 +0000 (11:16 -0600)
Adding new ioctl method for the MR object - REG_DM_MR.

This command can be used by users to register an allocated
device memory buffer as an MR and receive lkey and rkey
to be used within work requests.

It is added as a new method under the MR object and using a new
ib_device callback - reg_dm_mr.
The command creates a standard ib_mr object which represents the
registered memory.

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_std_types.c
drivers/infiniband/core/uverbs_std_types_mr.c [new file with mode: 0644]
drivers/infiniband/core/verbs.c
include/rdma/ib_verbs.h
include/rdma/uverbs_ioctl.h
include/uapi/rdma/ib_user_ioctl_cmds.h

index 636da34f83086f9cb1e92340a66bb8043469ad6d..dda9e856e3fa334d125d2ecdedadc42719197067 100644 (file)
@@ -35,4 +35,5 @@ 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_dm.o
+                               uverbs_std_types_flow_action.o uverbs_std_types_dm.o \
+                               uverbs_std_types_mr.o
index 4fedf59ec3963acd71d32b5a0bf92cd294b84c70..569f48bd821e9d4f0b7e3d4e6ccd2c55edff799b 100644 (file)
@@ -144,12 +144,6 @@ static int uverbs_free_srq(struct ib_uobject *uobject,
        return ret;
 }
 
-static int uverbs_free_mr(struct ib_uobject *uobject,
-                         enum rdma_remove_reason why)
-{
-       return ib_dereg_mr((struct ib_mr *)uobject->object);
-}
-
 static int uverbs_free_xrcd(struct ib_uobject *uobject,
                            enum rdma_remove_reason why)
 {
@@ -265,10 +259,6 @@ DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_QP,
 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_MW,
                            &UVERBS_TYPE_ALLOC_IDR(0, uverbs_free_mw));
 
-DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_MR,
-                           /* 1 is used in order to free the MR after all the MWs */
-                           &UVERBS_TYPE_ALLOC_IDR(1, uverbs_free_mr));
-
 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_SRQ,
                            &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_usrq_object), 0,
                                                      uverbs_free_srq));
diff --git a/drivers/infiniband/core/uverbs_std_types_mr.c b/drivers/infiniband/core/uverbs_std_types_mr.c
new file mode 100644 (file)
index 0000000..68f7cad
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * 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_mr(struct ib_uobject *uobject,
+                         enum rdma_remove_reason why)
+{
+       return ib_dereg_mr((struct ib_mr *)uobject->object);
+}
+
+static int UVERBS_HANDLER(UVERBS_METHOD_DM_MR_REG)(struct ib_device *ib_dev,
+                                                  struct ib_uverbs_file *file,
+                                                  struct uverbs_attr_bundle *attrs)
+{
+       struct ib_dm_mr_attr attr = {};
+       struct ib_uobject *uobj;
+       struct ib_dm *dm;
+       struct ib_pd *pd;
+       struct ib_mr *mr;
+       int ret;
+
+       if (!ib_dev->reg_dm_mr)
+               return -EOPNOTSUPP;
+
+       ret = uverbs_copy_from(&attr.offset, attrs, UVERBS_ATTR_REG_DM_MR_OFFSET);
+       if (ret)
+               return ret;
+
+       ret = uverbs_copy_from(&attr.length, attrs,
+                              UVERBS_ATTR_REG_DM_MR_LENGTH);
+       if (ret)
+               return ret;
+
+       ret = uverbs_copy_from(&attr.access_flags, attrs,
+                              UVERBS_ATTR_REG_DM_MR_ACCESS_FLAGS);
+       if (ret)
+               return ret;
+
+       if (!(attr.access_flags & IB_ZERO_BASED))
+               return -EINVAL;
+
+       ret = ib_check_mr_access(attr.access_flags);
+       if (ret)
+               return ret;
+
+       pd = uverbs_attr_get_obj(attrs, UVERBS_ATTR_REG_DM_MR_PD_HANDLE);
+
+       dm = uverbs_attr_get_obj(attrs, UVERBS_ATTR_REG_DM_MR_DM_HANDLE);
+
+       uobj = uverbs_attr_get(attrs, UVERBS_ATTR_REG_DM_MR_HANDLE)->obj_attr.uobject;
+
+       if (attr.offset > dm->length || attr.length > dm->length ||
+           attr.length > dm->length - attr.offset)
+               return -EINVAL;
+
+       mr = pd->device->reg_dm_mr(pd, dm, &attr, attrs);
+       if (IS_ERR(mr))
+               return PTR_ERR(mr);
+
+       mr->device  = pd->device;
+       mr->pd      = pd;
+       mr->dm      = dm;
+       mr->uobject = uobj;
+       atomic_inc(&pd->usecnt);
+       atomic_inc(&dm->usecnt);
+
+       uobj->object = mr;
+
+       ret = uverbs_copy_to(attrs, UVERBS_ATTR_REG_DM_MR_RESP_LKEY, &mr->lkey,
+                            sizeof(mr->lkey));
+       if (ret)
+               goto err_dereg;
+
+       ret = uverbs_copy_to(attrs, UVERBS_ATTR_REG_DM_MR_RESP_RKEY,
+                            &mr->rkey, sizeof(mr->rkey));
+       if (ret)
+               goto err_dereg;
+
+       return 0;
+
+err_dereg:
+       ib_dereg_mr(mr);
+
+       return ret;
+}
+
+static DECLARE_UVERBS_NAMED_METHOD(UVERBS_METHOD_DM_MR_REG,
+       &UVERBS_ATTR_IDR(UVERBS_ATTR_REG_DM_MR_HANDLE, UVERBS_OBJECT_MR,
+                        UVERBS_ACCESS_NEW,
+                        UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
+       &UVERBS_ATTR_PTR_IN(UVERBS_ATTR_REG_DM_MR_OFFSET,
+                           UVERBS_ATTR_TYPE(u64),
+                           UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
+       &UVERBS_ATTR_PTR_IN(UVERBS_ATTR_REG_DM_MR_LENGTH,
+                           UVERBS_ATTR_TYPE(u64),
+                           UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
+       &UVERBS_ATTR_IDR(UVERBS_ATTR_REG_DM_MR_PD_HANDLE, UVERBS_OBJECT_PD,
+                        UVERBS_ACCESS_READ,
+                        UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
+       &UVERBS_ATTR_PTR_IN(UVERBS_ATTR_REG_DM_MR_ACCESS_FLAGS,
+                           UVERBS_ATTR_TYPE(u32),
+                           UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
+       &UVERBS_ATTR_IDR(UVERBS_ATTR_REG_DM_MR_DM_HANDLE, UVERBS_OBJECT_DM,
+                        UVERBS_ACCESS_READ,
+                        UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
+       &UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_REG_DM_MR_RESP_LKEY,
+                            UVERBS_ATTR_TYPE(u32),
+                            UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
+       &UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_REG_DM_MR_RESP_RKEY,
+                            UVERBS_ATTR_TYPE(u32),
+                            UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)));
+
+DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_MR,
+                           /* 1 is used in order to free the MR after all the MWs */
+                           &UVERBS_TYPE_ALLOC_IDR(1, uverbs_free_mr),
+                           &UVERBS_METHOD(UVERBS_METHOD_DM_MR_REG));
index 95e3b307c93a81b825ac6fde7e14a57c650a17ee..7eff3aeffe01f37e8b0f2a6494911ca1e362eb6b 100644 (file)
@@ -1616,12 +1616,16 @@ EXPORT_SYMBOL(ib_resize_cq);
 int ib_dereg_mr(struct ib_mr *mr)
 {
        struct ib_pd *pd = mr->pd;
+       struct ib_dm *dm = mr->dm;
        int ret;
 
        rdma_restrack_del(&mr->res);
        ret = mr->device->dereg_mr(mr);
-       if (!ret)
+       if (!ret) {
                atomic_dec(&pd->usecnt);
+               if (dm)
+                       atomic_dec(&dm->usecnt);
+       }
 
        return ret;
 }
index 6806c4f5657a16e83fec6d0a613647f908e4ebe1..4bd24c48b1adaa63825d9e7d830b97dc8371398c 100644 (file)
@@ -321,6 +321,12 @@ struct ib_cq_caps {
        u16     max_cq_moderation_period;
 };
 
+struct ib_dm_mr_attr {
+       u64             length;
+       u64             offset;
+       u32             access_flags;
+};
+
 struct ib_dm_alloc_attr {
        u64     length;
        u32     alignment;
@@ -1797,6 +1803,8 @@ struct ib_mr {
                struct list_head        qp_entry;       /* FR */
        };
 
+       struct ib_dm      *dm;
+
        /*
         * Implementation details of the RDMA core, don't use in drivers:
         */
@@ -2444,6 +2452,9 @@ struct ib_device {
                                               struct ib_dm_alloc_attr *attr,
                                               struct uverbs_attr_bundle *attrs);
        int                        (*dealloc_dm)(struct ib_dm *dm);
+       struct ib_mr *             (*reg_dm_mr)(struct ib_pd *pd, struct ib_dm *dm,
+                                               struct ib_dm_mr_attr *attr,
+                                               struct uverbs_attr_bundle *attrs);
        /**
         * rdma netdev operation
         *
index 3d6ac684b8f064da1a1916616288359482a7661a..4a4201d997a73c9c769b23b8c34d225c828ab786 100644 (file)
@@ -408,6 +408,18 @@ static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs
        return attr->ptr_attr.enum_id;
 }
 
+static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
+                                       u16 idx)
+{
+       struct ib_uobject *uobj =
+               uverbs_attr_get(attrs_bundle, idx)->obj_attr.uobject;
+
+       if (IS_ERR(uobj))
+               return uobj;
+
+       return uobj->object;
+}
+
 static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
                                 size_t idx, const void *from, size_t size)
 {
index 6034df2625c607bb74f96777176f670ed79def3a..83e3890eef20e178de0bcbd5edf5ff524d584e5e 100644 (file)
@@ -115,4 +115,20 @@ enum uverbs_methods_dm {
        UVERBS_METHOD_DM_ALLOC,
        UVERBS_METHOD_DM_FREE,
 };
+
+enum uverbs_attrs_reg_dm_mr_cmd_attr_ids {
+       UVERBS_ATTR_REG_DM_MR_HANDLE,
+       UVERBS_ATTR_REG_DM_MR_OFFSET,
+       UVERBS_ATTR_REG_DM_MR_LENGTH,
+       UVERBS_ATTR_REG_DM_MR_PD_HANDLE,
+       UVERBS_ATTR_REG_DM_MR_ACCESS_FLAGS,
+       UVERBS_ATTR_REG_DM_MR_DM_HANDLE,
+       UVERBS_ATTR_REG_DM_MR_RESP_LKEY,
+       UVERBS_ATTR_REG_DM_MR_RESP_RKEY,
+};
+
+enum uverbs_methods_mr {
+       UVERBS_METHOD_DM_MR_REG,
+};
+
 #endif