mirror of https://lore.kernel.org/lkml/
 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 3/9] habanalabs: create context in lazy mode
Date: Sun, 28 Jul 2019 14:28:12 +0300	[thread overview]
Message-ID: <20190728112818.30397-4-oded.gabbay@gmail.com> (raw)
In-Reply-To: <20190728112818.30397-1-oded.gabbay@gmail.com>

Instead of creating the context when the user opens the device, create the
context when the first IOCTL that needs a context is called.

This change is required to provide backward compatibility for older
user-space when the driver will require the user to create a context.

Signed-off-by: Oded Gabbay <oded.gabbay@gmail.com>
---
 drivers/misc/habanalabs/context.c        | 29 +++++++++++++++++++++++-
 drivers/misc/habanalabs/device.c         |  5 +++-
 drivers/misc/habanalabs/habanalabs.h     |  2 ++
 drivers/misc/habanalabs/habanalabs_drv.c | 13 -----------
 4 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/drivers/misc/habanalabs/context.c b/drivers/misc/habanalabs/context.c
index 771a1055e67b..3c1f7c29e908 100644
--- a/drivers/misc/habanalabs/context.c
+++ b/drivers/misc/habanalabs/context.c
@@ -198,7 +198,34 @@ struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq)
 
 bool hl_ctx_is_valid(struct hl_fpriv *hpriv)
 {
-	return hpriv->ctx ? true : false;
+	struct hl_device *hdev = hpriv->hdev;
+	bool valid = true;
+	int rc;
+
+	/* First thing, to minimize latency impact, check if context exists.
+	 * If so, exit immediately
+	 */
+	if (hpriv->ctx)
+		return true;
+
+	mutex_lock(&hdev->lazy_ctx_creation_lock);
+
+	/* We must check again the existence of the context. This is because
+	 * there could be competing threads in the same process that call
+	 * IOCTLs at the same time. In this case, we must protect against double
+	 * creation of a context
+	 */
+	if (!hpriv->ctx) {
+		rc = hl_ctx_create(hdev, hpriv);
+		if (rc) {
+			dev_err(hdev->dev, "Failed to create context %d\n", rc);
+			valid = false;
+		}
+	}
+
+	mutex_unlock(&hdev->lazy_ctx_creation_lock);
+
+	return valid;
 }
 
 /*
diff --git a/drivers/misc/habanalabs/device.c b/drivers/misc/habanalabs/device.c
index 0c4894dd9c02..b63beb73ec76 100644
--- a/drivers/misc/habanalabs/device.c
+++ b/drivers/misc/habanalabs/device.c
@@ -58,8 +58,9 @@ static void hpriv_release(struct kref *ref)
 	/* Now the FD is really closed */
 	atomic_dec(&hdev->fd_open_cnt);
 
-	/* This allows a new user context to open the device */
+	mutex_lock(&hdev->lazy_ctx_creation_lock);
 	hdev->user_ctx = NULL;
+	mutex_unlock(&hdev->lazy_ctx_creation_lock);
 }
 
 void hl_hpriv_get(struct hl_fpriv *hpriv)
@@ -233,6 +234,7 @@ static int device_early_init(struct hl_device *hdev)
 	mutex_init(&hdev->send_cpu_message_lock);
 	mutex_init(&hdev->debug_lock);
 	mutex_init(&hdev->mmu_cache_lock);
+	mutex_init(&hdev->lazy_ctx_creation_lock);
 	INIT_LIST_HEAD(&hdev->hw_queues_mirror_list);
 	spin_lock_init(&hdev->hw_queues_mirror_lock);
 	atomic_set(&hdev->in_reset, 0);
@@ -262,6 +264,7 @@ static int device_early_init(struct hl_device *hdev)
  */
 static void device_early_fini(struct hl_device *hdev)
 {
+	mutex_destroy(&hdev->lazy_ctx_creation_lock);
 	mutex_destroy(&hdev->mmu_cache_lock);
 	mutex_destroy(&hdev->debug_lock);
 	mutex_destroy(&hdev->send_cpu_message_lock);
diff --git a/drivers/misc/habanalabs/habanalabs.h b/drivers/misc/habanalabs/habanalabs.h
index 475cdaa0005e..d0e55839d673 100644
--- a/drivers/misc/habanalabs/habanalabs.h
+++ b/drivers/misc/habanalabs/habanalabs.h
@@ -1183,6 +1183,7 @@ struct hl_device_reset_work {
  * @cb_pool: list of preallocated CBs.
  * @cb_pool_lock: protects the CB pool.
  * @user_ctx: current user context executing.
+ * @lazy_ctx_creation_lock: lock to protect lazy creation of context
  * @dram_used_mem: current DRAM memory consumption.
  * @timeout_jiffies: device CS timeout value.
  * @max_power: the max power of the device, as configured by the sysadmin. This
@@ -1261,6 +1262,7 @@ struct hl_device {
 
 	/* TODO: remove user_ctx for multiple process support */
 	struct hl_ctx			*user_ctx;
+	struct mutex			lazy_ctx_creation_lock;
 
 	atomic64_t			dram_used_mem;
 	u64				timeout_jiffies;
diff --git a/drivers/misc/habanalabs/habanalabs_drv.c b/drivers/misc/habanalabs/habanalabs_drv.c
index 678f61646ca9..b2c52e46e130 100644
--- a/drivers/misc/habanalabs/habanalabs_drv.c
+++ b/drivers/misc/habanalabs/habanalabs_drv.c
@@ -141,12 +141,6 @@ int hl_device_open(struct inode *inode, struct file *filp)
 	hl_cb_mgr_init(&hpriv->cb_mgr);
 	hl_ctx_mgr_init(&hpriv->ctx_mgr);
 
-	rc = hl_ctx_create(hdev, hpriv);
-	if (rc) {
-		dev_err(hdev->dev, "Failed to open FD (CTX fail)\n");
-		goto out_err;
-	}
-
 	hpriv->taskpid = find_get_pid(current->pid);
 
 	/*
@@ -160,13 +154,6 @@ int hl_device_open(struct inode *inode, struct file *filp)
 
 	return 0;
 
-out_err:
-	filp->private_data = NULL;
-	hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
-	hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
-	mutex_destroy(&hpriv->restore_phase_mutex);
-	kfree(hpriv);
-
 close_device:
 	atomic_dec(&hdev->fd_open_cnt);
 	return rc;
-- 
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 ` [PATCH 2/9] habanalabs: verify context is valid in IOCTLs Oded Gabbay
2019-07-28 11:28 ` Oded Gabbay [this message]
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-4-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®