RDMA/core: Allow the ioctl layer to abort a fully created uobject
authorJason Gunthorpe <jgg@mellanox.com>
Tue, 19 May 2020 07:27:05 +0000 (10:27 +0300)
committerJason Gunthorpe <jgg@mellanox.com>
Thu, 21 May 2020 23:10:46 +0000 (20:10 -0300)
While creating a uobject every create reaches a point where the uobject is
fully initialized. For ioctls that go on to copy_to_user this means they
need to open code the destruction of a fully created uobject - ie the
RDMA_REMOVE_DESTROY sort of flow.

Open coding this creates bugs, eg the CQ does not properly flush the
events list when it does its error unwind.

Provide a uverbs_finalize_uobj_create() function which indicates that the
uobject is fully initialized and that abort should call to destroy_hw to
destroy the uobj->object and related.

Methods can call this function if they go on to have error cases after
setting uobj->object. Once done those error cases can simply do return,
without an error unwind.

Link: https://lore.kernel.org/r/20200519072711.257271-2-leon@kernel.org
Signed-off-by: Yishai Hadas <yishaih@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
13 files changed:
drivers/infiniband/core/rdma_core.c
drivers/infiniband/core/rdma_core.h
drivers/infiniband/core/uverbs_cmd.c
drivers/infiniband/core/uverbs_ioctl.c
drivers/infiniband/core/uverbs_std_types_cq.c
drivers/infiniband/core/uverbs_std_types_mr.c
drivers/infiniband/hw/mlx5/devx.c
drivers/infiniband/hw/mlx5/main.c
drivers/infiniband/hw/mlx5/qos.c
include/rdma/ib_verbs.h
include/rdma/uverbs_ioctl.h
include/rdma/uverbs_std_types.h
include/rdma/uverbs_types.h

index bf8e149d31911a3463f5bf7abd704a44afaec031..de38585152755a9ce0211ef436ea75323c6f5893 100644 (file)
@@ -130,6 +130,17 @@ static int uverbs_destroy_uobject(struct ib_uobject *uobj,
        lockdep_assert_held(&ufile->hw_destroy_rwsem);
        assert_uverbs_usecnt(uobj, UVERBS_LOOKUP_WRITE);
 
+       if (reason == RDMA_REMOVE_ABORT_HWOBJ) {
+               reason = RDMA_REMOVE_ABORT;
+               ret = uobj->uapi_object->type_class->destroy_hw(uobj, reason,
+                                                               attrs);
+               /*
+                * Drivers are not permitted to ignore RDMA_REMOVE_ABORT, see
+                * ib_is_destroy_retryable, cleanup_retryable == false here.
+                */
+               WARN_ON(ret);
+       }
+
        if (reason == RDMA_REMOVE_ABORT) {
                WARN_ON(!list_empty(&uobj->list));
                WARN_ON(!uobj->context);
@@ -647,11 +658,15 @@ void rdma_alloc_commit_uobject(struct ib_uobject *uobj,
  * object and anything else connected to uobj before calling this.
  */
 void rdma_alloc_abort_uobject(struct ib_uobject *uobj,
-                             struct uverbs_attr_bundle *attrs)
+                             struct uverbs_attr_bundle *attrs,
+                             bool hw_obj_valid)
 {
        struct ib_uverbs_file *ufile = uobj->ufile;
 
-       uverbs_destroy_uobject(uobj, RDMA_REMOVE_ABORT, attrs);
+       uverbs_destroy_uobject(uobj,
+                              hw_obj_valid ? RDMA_REMOVE_ABORT_HWOBJ :
+                                             RDMA_REMOVE_ABORT,
+                              attrs);
 
        /* Matches the down_read in rdma_alloc_begin_uobject */
        up_read(&ufile->hw_destroy_rwsem);
@@ -921,8 +936,8 @@ uverbs_get_uobject_from_file(u16 object_id, enum uverbs_obj_access access,
 }
 
 void uverbs_finalize_object(struct ib_uobject *uobj,
-                           enum uverbs_obj_access access, bool commit,
-                           struct uverbs_attr_bundle *attrs)
+                           enum uverbs_obj_access access, bool hw_obj_valid,
+                           bool commit, struct uverbs_attr_bundle *attrs)
 {
        /*
         * refcounts should be handled at the object level and not at the
@@ -945,7 +960,7 @@ void uverbs_finalize_object(struct ib_uobject *uobj,
                if (commit)
                        rdma_alloc_commit_uobject(uobj, attrs);
                else
-                       rdma_alloc_abort_uobject(uobj, attrs);
+                       rdma_alloc_abort_uobject(uobj, attrs, hw_obj_valid);
                break;
        default:
                WARN_ON(true);
index 33978e0f1262c3b83bb3d570bff35efd94550b2c..2b529233e1595b10dc257a0c9982480cfbb6d9f3 100644 (file)
@@ -64,8 +64,8 @@ uverbs_get_uobject_from_file(u16 object_id, enum uverbs_obj_access access,
                             s64 id, struct uverbs_attr_bundle *attrs);
 
 void uverbs_finalize_object(struct ib_uobject *uobj,
-                           enum uverbs_obj_access access, bool commit,
-                           struct uverbs_attr_bundle *attrs);
+                           enum uverbs_obj_access access, bool hw_obj_valid,
+                           bool commit, struct uverbs_attr_bundle *attrs);
 
 int uverbs_output_written(const struct uverbs_attr_bundle *bundle, size_t idx);
 
index d5642bcf93ee701916adbbf5f1a6d60dc36af000..86c97221872dbac769ea5a98c65a20d34d615150 100644 (file)
@@ -311,7 +311,7 @@ static int ib_uverbs_get_context(struct uverbs_attr_bundle *attrs)
        return 0;
 
 err_uobj:
-       rdma_alloc_abort_uobject(uobj, attrs);
+       rdma_alloc_abort_uobject(uobj, attrs, false);
 err_ucontext:
        kfree(attrs->context);
        attrs->context = NULL;
index 538affbc517e68f0675ad36fb211572af4470331..42c5696f03bd756afc72f5541ba7f98251fa7b3c 100644 (file)
@@ -58,6 +58,7 @@ struct bundle_priv {
 
        DECLARE_BITMAP(uobj_finalize, UVERBS_API_ATTR_BKEY_LEN);
        DECLARE_BITMAP(spec_finalize, UVERBS_API_ATTR_BKEY_LEN);
+       DECLARE_BITMAP(uobj_hw_obj_valid, UVERBS_API_ATTR_BKEY_LEN);
 
        /*
         * Must be last. bundle ends in a flex array which overlaps
@@ -230,7 +231,8 @@ static void uverbs_free_idrs_array(const struct uverbs_api_attr *attr_uapi,
 
        for (i = 0; i != attr->len; i++)
                uverbs_finalize_object(attr->uobjects[i],
-                                      spec->u2.objs_arr.access, commit, attrs);
+                                      spec->u2.objs_arr.access, false, commit,
+                                      attrs);
 }
 
 static int uverbs_process_attr(struct bundle_priv *pbundle,
@@ -502,7 +504,9 @@ static void bundle_destroy(struct bundle_priv *pbundle, bool commit)
 
                uverbs_finalize_object(
                        attr->obj_attr.uobject,
-                       attr->obj_attr.attr_elm->spec.u.obj.access, commit,
+                       attr->obj_attr.attr_elm->spec.u.obj.access,
+                       test_bit(i, pbundle->uobj_hw_obj_valid),
+                       commit,
                        &pbundle->bundle);
        }
 
@@ -590,6 +594,8 @@ static int ib_uverbs_cmd_verbs(struct ib_uverbs_file *ufile,
               sizeof(pbundle->bundle.attr_present));
        memset(pbundle->uobj_finalize, 0, sizeof(pbundle->uobj_finalize));
        memset(pbundle->spec_finalize, 0, sizeof(pbundle->spec_finalize));
+       memset(pbundle->uobj_hw_obj_valid, 0,
+              sizeof(pbundle->uobj_hw_obj_valid));
 
        ret = ib_uverbs_run_method(pbundle, hdr->num_attrs);
        bundle_destroy(pbundle, ret == 0);
@@ -784,3 +790,15 @@ int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
        }
        return uverbs_copy_to(bundle, idx, from, size);
 }
+
+/* Once called an abort will call through to the type's destroy_hw() */
+void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *bundle,
+                                u16 idx)
+{
+       struct bundle_priv *pbundle =
+               container_of(bundle, struct bundle_priv, bundle);
+
+       __set_bit(uapi_bkey_attr(uapi_key_attr(idx)),
+                 pbundle->uobj_hw_obj_valid);
+}
+EXPORT_SYMBOL(uverbs_finalize_uobj_create);
index da4110a0eea2c5511bf9bbee125b461df49f12f5..73fbbeb2586cf0db2fc2dc024d4dfde1325f97d2 100644 (file)
@@ -129,16 +129,12 @@ static int UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE)(
        obj->uevent.uobject.object = cq;
        obj->uevent.uobject.user_handle = user_handle;
        rdma_restrack_uadd(&cq->res);
+       uverbs_finalize_uobj_create(attrs, UVERBS_ATTR_CREATE_CQ_HANDLE);
 
        ret = uverbs_copy_to(attrs, UVERBS_ATTR_CREATE_CQ_RESP_CQE, &cq->cqe,
                             sizeof(cq->cqe));
-       if (ret)
-               goto err_cq;
+       return ret;
 
-       return 0;
-err_cq:
-       ib_destroy_cq_user(cq, uverbs_get_cleared_udata(attrs));
-       cq = NULL;
 err_free:
        kfree(cq);
 err_event_file:
index c1286a52dc8451d4a81eb918c1f0f0482630e732..a2722ef8496e9a03bf40c574e0f882c62c674d36 100644 (file)
@@ -136,21 +136,15 @@ static int UVERBS_HANDLER(UVERBS_METHOD_DM_MR_REG)(
 
        uobj->object = mr;
 
+       uverbs_finalize_uobj_create(attrs, UVERBS_ATTR_REG_DM_MR_HANDLE);
+
        ret = uverbs_copy_to(attrs, UVERBS_ATTR_REG_DM_MR_RESP_LKEY, &mr->lkey,
                             sizeof(mr->lkey));
        if (ret)
-               goto err_dereg;
+               return ret;
 
        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_user(mr, uverbs_get_cleared_udata(attrs));
-
        return ret;
 }
 
index c339dd5ee6947323ca13314812cee4ce047c9ab6..3047e7d60a9ba82231ecc01aee6c9323a820f057 100644 (file)
@@ -2218,14 +2218,12 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_UMEM_REG)(
        obj->mdev = dev->mdev;
        uobj->object = obj;
        devx_obj_build_destroy_cmd(cmd.in, cmd.out, obj->dinbox, &obj->dinlen, &obj_id);
-       err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_UMEM_REG_OUT_ID, &obj_id, sizeof(obj_id));
-       if (err)
-               goto err_umem_destroy;
+       uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_DEVX_UMEM_REG_HANDLE);
 
-       return 0;
+       err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_UMEM_REG_OUT_ID, &obj_id,
+                            sizeof(obj_id));
+       return err;
 
-err_umem_destroy:
-       mlx5_cmd_exec(obj->mdev, obj->dinbox, obj->dinlen, cmd.out, sizeof(cmd.out));
 err_umem_release:
        ib_umem_release(obj->umem);
 err_obj_free:
index 26f0b39c7f745e5bc151de8c9baf4af84c13528b..623d7898ae6db004ff487ca7b6711ab90a937d23 100644 (file)
@@ -6187,26 +6187,20 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
        mmap_offset = mlx5_entry_to_mmap_offset(entry);
        length = entry->rdma_entry.npages * PAGE_SIZE;
        uobj->object = entry;
+       uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
 
        err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
                             &mmap_offset, sizeof(mmap_offset));
        if (err)
-               goto err;
+               return err;
 
        err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
                             &entry->page_idx, sizeof(entry->page_idx));
        if (err)
-               goto err;
+               return err;
 
        err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
                             &length, sizeof(length));
-       if (err)
-               goto err;
-
-       return 0;
-
-err:
-       rdma_user_mmap_entry_remove(&entry->rdma_entry);
        return err;
 }
 
@@ -6320,26 +6314,20 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
        mmap_offset = mlx5_entry_to_mmap_offset(entry);
        length = entry->rdma_entry.npages * PAGE_SIZE;
        uobj->object = entry;
+       uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
 
        err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
                             &mmap_offset, sizeof(mmap_offset));
        if (err)
-               goto err;
+               return err;
 
        err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
                             &entry->page_idx, sizeof(entry->page_idx));
        if (err)
-               goto err;
+               return err;
 
        err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
                             &length, sizeof(length));
-       if (err)
-               goto err;
-
-       return 0;
-
-err:
-       rdma_user_mmap_entry_remove(&entry->rdma_entry);
        return err;
 }
 
index cac878a70edb03d82ef4f83985ac53ed338c0895..dce92554142a29137c721c72c7d2734a3a38353e 100644 (file)
@@ -69,17 +69,14 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_PP_OBJ_ALLOC)(
        if (err)
                goto err;
 
-       err = uverbs_copy_to(attrs, MLX5_IB_ATTR_PP_OBJ_ALLOC_INDEX,
-                            &pp_entry->index, sizeof(pp_entry->index));
-       if (err)
-               goto clean;
-
        pp_entry->mdev = dev->mdev;
        uobj->object = pp_entry;
-       return 0;
+       uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_PP_OBJ_ALLOC_HANDLE);
+
+       err = uverbs_copy_to(attrs, MLX5_IB_ATTR_PP_OBJ_ALLOC_INDEX,
+                            &pp_entry->index, sizeof(pp_entry->index));
+       return err;
 
-clean:
-       mlx5_rl_remove_rate_raw(dev->mdev, pp_entry->index);
 err:
        kfree(pp_entry);
        return err;
index 641f4751b0620036e9cabbc573e93b0193647b15..c988e9205cf9914c370e93f7a6aa496380dd6872 100644 (file)
@@ -1491,6 +1491,11 @@ enum rdma_remove_reason {
        RDMA_REMOVE_DRIVER_REMOVE,
        /* uobj is being cleaned-up before being committed */
        RDMA_REMOVE_ABORT,
+       /*
+        * uobj has been fully created, with the uobj->object set, but is being
+        * cleaned up before being comitted
+        */
+       RDMA_REMOVE_ABORT_HWOBJ,
 };
 
 struct ib_rdmacg_object {
index 9f3b1e0040468160e0e200ff7cfaaa5811597a9c..5bd2b037e9147c9d93b15860ff66fe58291cf7ba 100644 (file)
@@ -737,6 +737,9 @@ uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
        return attr->ptr_attr.len;
 }
 
+void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle,
+                                u16 idx);
+
 /*
  * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
  * attribute.
index 1b28ce1aba07bf3b8b71a7cf5b4979b47f573672..d6784be27e4b865c8dbfee0781af3a9f8d860187 100644 (file)
@@ -107,7 +107,7 @@ static inline void uobj_put_write(struct ib_uobject *uobj)
 static inline void uobj_alloc_abort(struct ib_uobject *uobj,
                                    struct uverbs_attr_bundle *attrs)
 {
-       rdma_alloc_abort_uobject(uobj, attrs);
+       rdma_alloc_abort_uobject(uobj, attrs, false);
 }
 
 static inline struct ib_uobject *
index f1cbdae67250916db2eca686244dc0e8cdd12d0c..c15b298aa62f11685201ea15d1685507b366a080 100644 (file)
@@ -139,7 +139,8 @@ void rdma_lookup_put_uobject(struct ib_uobject *uobj,
 struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_api_object *obj,
                                            struct uverbs_attr_bundle *attrs);
 void rdma_alloc_abort_uobject(struct ib_uobject *uobj,
-                             struct uverbs_attr_bundle *attrs);
+                             struct uverbs_attr_bundle *attrs,
+                             bool hw_obj_valid);
 void rdma_alloc_commit_uobject(struct ib_uobject *uobj,
                               struct uverbs_attr_bundle *attrs);