From: Matthew Wilcox <willy@infradead.org>
Date: Fri, 15 Mar 2019 12:21:44 +0000 (-0400)
Subject: habanalabs: Convert ctx_handles to XArray
X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=8c3440dd19ea84b9857bb27570a089f45338a9b6;p=users%2Fwilly%2Fxarray.git

habanalabs: Convert ctx_handles to XArray

Signed-off-by: Matthew Wilcox <willy@infradead.org>
---

diff --git a/drivers/misc/habanalabs/context.c b/drivers/misc/habanalabs/context.c
index 8682590e3f6e..f22fe5ef6011 100644
--- a/drivers/misc/habanalabs/context.c
+++ b/drivers/misc/habanalabs/context.c
@@ -59,7 +59,7 @@ int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
 {
 	struct hl_ctx_mgr *mgr = &hpriv->ctx_mgr;
 	struct hl_ctx *ctx;
-	int rc;
+	int rc, id;
 
 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx) {
@@ -78,12 +78,9 @@ 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);
-
+	rc = xa_alloc(&mgr->ctx_handles, &id, ctx, xa_limit_31b, GFP_KERNEL);
 	if (rc < 0) {
-		dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
+		dev_err(hdev->dev, "Failed to allocate ID for a new CTX\n");
 		hl_ctx_free(hdev, ctx);
 		goto out_err;
 	}
@@ -201,8 +198,7 @@ struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq)
  */
 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr)
 {
-	mutex_init(&mgr->ctx_lock);
-	idr_init(&mgr->ctx_handles);
+	xa_init_flags(&mgr->ctx_handles, XA_FLAGS_ALLOC1);
 }
 
 /*
@@ -217,14 +213,8 @@ void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr)
 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr)
 {
 	struct hl_ctx *ctx;
-	struct idr *idp;
-	u32 id;
-
-	idp = &mgr->ctx_handles;
+	unsigned long id;
 
-	idr_for_each_entry(idp, ctx, id)
+	xa_for_each(&mgr->ctx_handles, id, ctx)
 		hl_ctx_free(hdev, ctx);
-
-	idr_destroy(&mgr->ctx_handles);
-	mutex_destroy(&mgr->ctx_lock);
 }
diff --git a/drivers/misc/habanalabs/habanalabs.h b/drivers/misc/habanalabs/habanalabs.h
index 399093ee50cd..d91332a9b876 100644
--- a/drivers/misc/habanalabs/habanalabs.h
+++ b/drivers/misc/habanalabs/habanalabs.h
@@ -652,12 +652,10 @@ struct hl_ctx {
 
 /**
  * struct hl_ctx_mgr - for handling multiple contexts.
- * @ctx_lock: protects ctx_handles.
- * @ctx_handles: idr to hold all ctx handles.
+ * @ctx_handles: holds all ctx handles.
  */
 struct hl_ctx_mgr {
-	struct mutex		ctx_lock;
-	struct idr		ctx_handles;
+	struct xarray		ctx_handles;
 };