This patch adds to most IOCTL operations a check of whether the calling
process has a valid context.

This is in preparation for when contexts will be created by the user and
not the driver.

Signed-off-by: Oded Gabbay <[email protected]>
---
 drivers/misc/habanalabs/command_buffer.c     |  6 ++++++
 drivers/misc/habanalabs/command_submission.c | 12 ++++++++++++
 drivers/misc/habanalabs/context.c            |  7 ++++++-
 drivers/misc/habanalabs/habanalabs.h         |  5 +++--
 drivers/misc/habanalabs/habanalabs_ioctl.c   | 12 ++++++++++++
 drivers/misc/habanalabs/memory.c             |  6 ++++++
 6 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/drivers/misc/habanalabs/command_buffer.c 
b/drivers/misc/habanalabs/command_buffer.c
index e495f44064fa..981caa8ec068 100644
--- a/drivers/misc/habanalabs/command_buffer.c
+++ b/drivers/misc/habanalabs/command_buffer.c
@@ -221,6 +221,12 @@ int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
                return -EBUSY;
        }
 
+       if (!hl_ctx_is_valid(hpriv)) {
+               dev_err_ratelimited(hdev->dev,
+                       "Can't execute CB IOCTL, missing a valid context\n");
+               return -EACCES;
+       }
+
        switch (args->in.op) {
        case HL_CB_OP_CREATE:
                rc = hl_cb_create(hdev, &hpriv->cb_mgr, args->in.cb_size,
diff --git a/drivers/misc/habanalabs/command_submission.c 
b/drivers/misc/habanalabs/command_submission.c
index f4e2c2ad0057..56910dee6026 100644
--- a/drivers/misc/habanalabs/command_submission.c
+++ b/drivers/misc/habanalabs/command_submission.c
@@ -613,6 +613,12 @@ int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data)
                goto out;
        }
 
+       if (!hl_ctx_is_valid(hpriv)) {
+               dev_err_ratelimited(hdev->dev,
+                       "Can't execute CS IOCTL, missing a valid context\n");
+               return -EACCES;
+       }
+
        do_ctx_switch = atomic_cmpxchg(&ctx->thread_ctx_switch_token, 1, 0);
 
        if (do_ctx_switch || (args->in.cs_flags & HL_CS_FLAGS_FORCE_RESTORE)) {
@@ -759,6 +765,12 @@ int hl_cs_wait_ioctl(struct hl_fpriv *hpriv, void *data)
        u64 seq = args->in.seq;
        long rc;
 
+       if (!hl_ctx_is_valid(hpriv)) {
+               dev_err_ratelimited(hdev->dev,
+                       "Can't execute WAIT_CS IOCTL, missing a valid 
context\n");
+               return -EACCES;
+       }
+
        rc = _hl_cs_wait_ioctl(hdev, hpriv->ctx, args->in.timeout_us, seq);
 
        memset(args, 0, sizeof(*args));
diff --git a/drivers/misc/habanalabs/context.c 
b/drivers/misc/habanalabs/context.c
index 1d8390418234..771a1055e67b 100644
--- a/drivers/misc/habanalabs/context.c
+++ b/drivers/misc/habanalabs/context.c
@@ -85,7 +85,7 @@ int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv 
*hpriv)
        hl_hpriv_get(hpriv);
        ctx->hpriv = hpriv;
 
-       /* TODO: remove for multiple contexts */
+       /* TODO: remove for multiple contexts per process */
        hpriv->ctx = ctx;
        hdev->user_ctx = ctx;
 
@@ -196,6 +196,11 @@ struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 
seq)
        return fence;
 }
 
+bool hl_ctx_is_valid(struct hl_fpriv *hpriv)
+{
+       return hpriv->ctx ? true : false;
+}
+
 /*
  * hl_ctx_mgr_init - initialize the context manager
  *
diff --git a/drivers/misc/habanalabs/habanalabs.h 
b/drivers/misc/habanalabs/habanalabs.h
index e41800e68578..475cdaa0005e 100644
--- a/drivers/misc/habanalabs/habanalabs.h
+++ b/drivers/misc/habanalabs/habanalabs.h
@@ -905,7 +905,7 @@ struct hl_debug_params {
  * @hdev: habanalabs device structure.
  * @filp: pointer to the given file structure.
  * @taskpid: current process ID.
- * @ctx: current executing context.
+ * @ctx: current executing context. TODO: remove for multiple ctx per process
  * @ctx_mgr: context manager to handle multiple context for this FD.
  * @cb_mgr: command buffer manager to handle multiple buffers for this FD.
  * @debugfs_list: list of relevant ASIC debugfs.
@@ -916,7 +916,7 @@ struct hl_fpriv {
        struct hl_device        *hdev;
        struct file             *filp;
        struct pid              *taskpid;
-       struct hl_ctx           *ctx; /* TODO: remove for multiple ctx */
+       struct hl_ctx           *ctx;
        struct hl_ctx_mgr       ctx_mgr;
        struct hl_cb_mgr        cb_mgr;
        struct list_head        debugfs_list;
@@ -1422,6 +1422,7 @@ int hl_ctx_put(struct hl_ctx *ctx);
 struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
+bool hl_ctx_is_valid(struct hl_fpriv *hpriv);
 
 int hl_device_init(struct hl_device *hdev, struct class *hclass);
 void hl_device_fini(struct hl_device *hdev);
diff --git a/drivers/misc/habanalabs/habanalabs_ioctl.c 
b/drivers/misc/habanalabs/habanalabs_ioctl.c
index 07127576b3e8..60b18af66283 100644
--- a/drivers/misc/habanalabs/habanalabs_ioctl.c
+++ b/drivers/misc/habanalabs/habanalabs_ioctl.c
@@ -208,6 +208,12 @@ static int hl_info_ioctl(struct hl_fpriv *hpriv, void 
*data)
                return -EBUSY;
        }
 
+       if (!hl_ctx_is_valid(hpriv)) {
+               dev_err_ratelimited(hdev->dev,
+                       "Can't execute INFO IOCTL, missing a valid context\n");
+               return -EACCES;
+       }
+
        switch (args->op) {
        case HL_INFO_HW_IP_INFO:
                rc = hw_ip_info(hdev, args);
@@ -247,6 +253,12 @@ static int hl_debug_ioctl(struct hl_fpriv *hpriv, void 
*data)
                return -EBUSY;
        }
 
+       if (!hl_ctx_is_valid(hpriv)) {
+               dev_err_ratelimited(hdev->dev,
+                       "Can't execute DEBUG IOCTL, missing a valid context\n");
+               return -EACCES;
+       }
+
        switch (args->op) {
        case HL_DEBUG_OP_ETR:
        case HL_DEBUG_OP_ETF:
diff --git a/drivers/misc/habanalabs/memory.c b/drivers/misc/habanalabs/memory.c
index 42d237cae1dc..fddbca623bd2 100644
--- a/drivers/misc/habanalabs/memory.c
+++ b/drivers/misc/habanalabs/memory.c
@@ -1154,6 +1154,12 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
                return -EBUSY;
        }
 
+       if (!hl_ctx_is_valid(hpriv)) {
+               dev_err_ratelimited(hdev->dev,
+                       "Can't execute MEMORY IOCTL, missing a valid 
context\n");
+               return -EACCES;
+       }
+
        if (!hdev->mmu_enable)
                return mem_ioctl_no_mmu(hpriv, args);
 
-- 
2.17.1

Reply via email to