habanalabs: add handle field to context structure
authorOded Gabbay <oded.gabbay@gmail.com>
Mon, 15 Jul 2019 18:55:57 +0000 (21:55 +0300)
committerOded Gabbay <oded.gabbay@gmail.com>
Thu, 5 Sep 2019 11:55:26 +0000 (14:55 +0300)
This patch adds a field to the context's structure that will hold a unique
handle for the context.

This will be needed when the user will create the context.

Signed-off-by: Oded Gabbay <oded.gabbay@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/misc/habanalabs/context.c
drivers/misc/habanalabs/habanalabs.h

index 8682590e3f6ef14ee397e767d339d71eeee00e2f..1d83904182344813faffee6f528dcdccd3ef5c98 100644 (file)
@@ -67,9 +67,20 @@ int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
                goto out_err;
        }
 
+       mutex_lock(&mgr->ctx_lock);
+       rc = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
+       mutex_unlock(&mgr->ctx_lock);
+
+       if (rc < 0) {
+               dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
+               goto free_ctx;
+       }
+
+       ctx->handle = rc;
+
        rc = hl_ctx_init(hdev, ctx, false);
        if (rc)
-               goto free_ctx;
+               goto remove_from_idr;
 
        hl_hpriv_get(hpriv);
        ctx->hpriv = hpriv;
@@ -78,18 +89,12 @@ int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
        hpriv->ctx = ctx;
        hdev->user_ctx = ctx;
 
-       mutex_lock(&mgr->ctx_lock);
-       rc = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
-       mutex_unlock(&mgr->ctx_lock);
-
-       if (rc < 0) {
-               dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
-               hl_ctx_free(hdev, ctx);
-               goto out_err;
-       }
-
        return 0;
 
+remove_from_idr:
+       mutex_lock(&mgr->ctx_lock);
+       idr_remove(&mgr->ctx_handles, ctx->handle);
+       mutex_unlock(&mgr->ctx_lock);
 free_ctx:
        kfree(ctx);
 out_err:
index e041afe895cf75da57aa454bdb11dc8d2092fe28..16f16f7c1e3a25aaf12dd81e58af70ab025a6a9c 100644 (file)
@@ -636,6 +636,7 @@ struct hl_va_range {
  *                             execution phase before the context switch phase
  *                             has finished.
  * @asid: context's unique address space ID in the device's MMU.
+ * @handle: context's opaque handle for user
  */
 struct hl_ctx {
        DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
@@ -657,6 +658,7 @@ struct hl_ctx {
        atomic_t                thread_ctx_switch_token;
        u32                     thread_ctx_switch_wait_token;
        u32                     asid;
+       u32                     handle;
 };
 
 /**