]> www.infradead.org Git - users/willy/xarray.git/commitdiff
habanalabs: Convert cb_handles to XArray
authorMatthew Wilcox <willy@infradead.org>
Fri, 15 Mar 2019 12:15:52 +0000 (08:15 -0400)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Fri, 9 Aug 2019 01:38:19 +0000 (21:38 -0400)
Signed-off-by: Matthew Wilcox <willy@infradead.org>
drivers/misc/habanalabs/command_buffer.c
drivers/misc/habanalabs/habanalabs.h

index e495f44064fa8d89462d614287f5ddc4f1857190..5bceeddbf4663c50d9b53287b93057e81783a9c1 100644 (file)
@@ -144,22 +144,17 @@ int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
        cb->hdev = hdev;
        cb->ctx_id = ctx_id;
 
-       spin_lock(&mgr->cb_lock);
-       rc = idr_alloc(&mgr->cb_handles, cb, 1, 0, GFP_ATOMIC);
-       spin_unlock(&mgr->cb_lock);
-
+       rc = xa_alloc(&mgr->cb_handles, &cb->id, cb, xa_limit_31b, GFP_KERNEL);
        if (rc < 0) {
-               dev_err(hdev->dev, "Failed to allocate IDR for a new CB\n");
+               dev_err(hdev->dev, "Failed to allocate ID for a new CB\n");
                goto release_cb;
        }
 
-       cb->id = rc;
-
        kref_init(&cb->refcount);
        spin_lock_init(&cb->lock);
 
        /*
-        * idr is 32-bit so we can safely OR it with a mask that is above
+        * id is 32-bit so we can safely OR it with a mask that is above
         * 32 bit
         */
        *handle = cb->id | HL_MMAP_CB_MASK;
@@ -183,22 +178,14 @@ int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle)
        u32 handle;
        int rc = 0;
 
-       /*
-        * handle was given to user to do mmap, I need to shift it back to
-        * how the idr module gave it to me
-        */
+       /* convert the handle back to an ID */
        cb_handle >>= PAGE_SHIFT;
        handle = (u32) cb_handle;
 
-       spin_lock(&mgr->cb_lock);
-
-       cb = idr_find(&mgr->cb_handles, handle);
+       cb = xa_erase(&mgr->cb_handles, handle);
        if (cb) {
-               idr_remove(&mgr->cb_handles, handle);
-               spin_unlock(&mgr->cb_lock);
                kref_put(&cb->refcount, cb_release);
        } else {
-               spin_unlock(&mgr->cb_lock);
                dev_err(hdev->dev,
                        "CB destroy failed, no match to handle 0x%x\n", handle);
                rc = -EINVAL;
@@ -341,11 +328,11 @@ struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
 {
        struct hl_cb *cb;
 
-       spin_lock(&mgr->cb_lock);
-       cb = idr_find(&mgr->cb_handles, handle);
+       xa_lock(&mgr->cb_handles);
+       cb = xa_load(&mgr->cb_handles, handle);
 
        if (!cb) {
-               spin_unlock(&mgr->cb_lock);
+               xa_unlock(&mgr->cb_handles);
                dev_warn(hdev->dev,
                        "CB get failed, no match to handle %d\n", handle);
                return NULL;
@@ -353,7 +340,7 @@ struct hl_cb *hl_cb_get(struct hl_device *hdev, struct hl_cb_mgr *mgr,
 
        kref_get(&cb->refcount);
 
-       spin_unlock(&mgr->cb_lock);
+       xa_unlock(&mgr->cb_handles);
 
        return cb;
 
@@ -366,26 +353,20 @@ void hl_cb_put(struct hl_cb *cb)
 
 void hl_cb_mgr_init(struct hl_cb_mgr *mgr)
 {
-       spin_lock_init(&mgr->cb_lock);
-       idr_init(&mgr->cb_handles);
+       xa_init_flags(&mgr->cb_handles, XA_FLAGS_ALLOC1);
 }
 
 void hl_cb_mgr_fini(struct hl_device *hdev, struct hl_cb_mgr *mgr)
 {
        struct hl_cb *cb;
-       struct idr *idp;
-       u32 id;
-
-       idp = &mgr->cb_handles;
+       unsigned long id;
 
-       idr_for_each_entry(idp, cb, id) {
+       xa_for_each(&mgr->cb_handles, id, cb) {
                if (kref_put(&cb->refcount, cb_release) != 1)
                        dev_err(hdev->dev,
-                               "CB %d for CTX ID %d is still alive\n",
+                               "CB %ld for CTX ID %d is still alive\n",
                                id, cb->ctx_id);
        }
-
-       idr_destroy(&mgr->cb_handles);
 }
 
 struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size)
index 10da9940ee0dd1ac2cbad4291dee1d297897877a..399093ee50cd9d70438471981461fa0b962879dd 100644 (file)
@@ -236,12 +236,10 @@ struct hl_dma_fence {
 
 /**
  * struct hl_cb_mgr - describes a Command Buffer Manager.
- * @cb_lock: protects cb_handles.
- * @cb_handles: an idr to hold all command buffer handles.
+ * @cb_handles: an XArray to hold all command buffer handles.
  */
 struct hl_cb_mgr {
-       spinlock_t              cb_lock;
-       struct idr              cb_handles; /* protected by cb_lock */
+       struct xarray           cb_handles;
 };
 
 /**