All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oded Gabbay <oded.gabbay@gmail.com>
To: linux-kernel@vger.kernel.org, oshpigelman@habana.ai,
	ttayar@habana.ai, gregkh@linuxfoundation.org
Subject: [PATCH 2/9] habanalabs: verify context is valid in IOCTLs
Date: Sun, 28 Jul 2019 14:28:11 +0300	[thread overview]
Message-ID: <20190728112818.30397-3-oded.gabbay@gmail.com> (raw)
In-Reply-To: <20190728112818.30397-1-oded.gabbay@gmail.com>

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 <oded.gabbay@gmail.com>
---
 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


  parent reply	other threads:[~2019-07-28 11:28 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-28 11:28 [PATCH 0/9] habanalabs: support open device by multiple processes Oded Gabbay
2019-07-28 11:28 ` [PATCH 1/9] habanalabs: add handle field to context structure Oded Gabbay
2019-07-28 11:28 ` Oded Gabbay [this message]
2019-07-28 11:28 ` [PATCH 3/9] habanalabs: create context in lazy mode Oded Gabbay
2019-07-28 11:28 ` [PATCH 4/9] habanalabs: don't change frequency if user context is valid Oded Gabbay
2019-07-28 11:28 ` [PATCH 5/9] habanalabs: maintain a list of file private data objects Oded Gabbay
2019-07-28 11:28 ` [PATCH 6/9] habanalabs: define user context as compute context Oded Gabbay
2019-07-28 11:28 ` [PATCH 7/9] habanalabs: protect only pointer dereference in hard-reset Oded Gabbay
2019-07-28 11:28 ` [PATCH 8/9] habanalabs: kill user process after CS rollback Oded Gabbay
2019-07-28 11:28 ` [PATCH 9/9] habanalabs: allow multiple processes to open FD Oded Gabbay
2019-07-28 11:44   ` Greg KH
2019-07-28 11:56     ` Oded Gabbay
2019-07-28 12:04       ` Greg KH
2019-07-28 12:06         ` Oded Gabbay
2019-07-28 12:12           ` Greg KH
2019-07-28 12:18             ` Oded Gabbay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190728112818.30397-3-oded.gabbay@gmail.com \
    --to=oded.gabbay@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oshpigelman@habana.ai \
    --cc=ttayar@habana.ai \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.