]> www.infradead.org Git - users/willy/xarray.git/commitdiff
habanalabs: Convert hl_devs_idr to XArray
authorMatthew Wilcox <willy@infradead.org>
Fri, 15 Mar 2019 03:06:24 +0000 (23:06 -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/habanalabs_drv.c

index 6f6dbe93f1dfece654092027b0f8306ff55168cc..f17f91b582390481bfe3305d1e77596da3006898 100644 (file)
@@ -23,8 +23,7 @@ MODULE_LICENSE("GPL v2");
 
 static int hl_major;
 static struct class *hl_class;
-static DEFINE_IDR(hl_devs_idr);
-static DEFINE_MUTEX(hl_devs_idr_lock);
+static DEFINE_XARRAY_ALLOC(hl_devs);
 
 static int timeout_locked = 5;
 static int reset_on_lockup = 1;
@@ -85,9 +84,7 @@ int hl_device_open(struct inode *inode, struct file *filp)
        struct hl_fpriv *hpriv;
        int rc;
 
-       mutex_lock(&hl_devs_idr_lock);
-       hdev = idr_find(&hl_devs_idr, iminor(inode));
-       mutex_unlock(&hl_devs_idr_lock);
+       hdev = xa_load(&hl_devs, iminor(inode));
 
        if (!hdev) {
                pr_err("Couldn't find device %d:%d\n",
@@ -238,42 +235,22 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
        /* Set default DMA mask to 32 bits */
        hdev->dma_mask = 32;
 
-       mutex_lock(&hl_devs_idr_lock);
-
        if (minor == -1) {
-               rc = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
-                               GFP_KERNEL);
-       } else {
-               void *old_idr = idr_replace(&hl_devs_idr, hdev, minor);
+               rc = xa_alloc(&hl_devs, &minor, hdev,
+                               XA_LIMIT(0, HL_MAX_MINORS - 1), GFP_KERNEL);
 
-               if (IS_ERR_VALUE(old_idr)) {
-                       rc = PTR_ERR(old_idr);
-                       pr_err("Error %d when trying to replace minor %d\n",
-                               rc, minor);
-                       mutex_unlock(&hl_devs_idr_lock);
-                       goto free_hdev;
-               }
-               rc = minor;
+       } else {
+               rc = xa_insert(&hl_devs, minor, hdev, GFP_KERNEL);
        }
 
-       mutex_unlock(&hl_devs_idr_lock);
-
+free_hdev:
        if (rc < 0) {
-               if (rc == -ENOSPC) {
-                       pr_err("too many devices in the system\n");
-                       rc = -EBUSY;
-               }
-               goto free_hdev;
+               kfree(hdev);
+       } else {
+               *dev = hdev;
+               hdev->id = minor;
        }
 
-       hdev->id = rc;
-
-       *dev = hdev;
-
-       return 0;
-
-free_hdev:
-       kfree(hdev);
        return rc;
 }
 
@@ -286,10 +263,7 @@ free_hdev:
 void destroy_hdev(struct hl_device *hdev)
 {
        /* Remove device from the device list */
-       mutex_lock(&hl_devs_idr_lock);
-       idr_remove(&hl_devs_idr, hdev->id);
-       mutex_unlock(&hl_devs_idr_lock);
-
+       xa_erase(&hl_devs, hdev->id);
        kfree(hdev);
 }
 
@@ -461,8 +435,6 @@ static void __exit hl_exit(void)
        class_destroy(hl_class);
        unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
 
-       idr_destroy(&hl_devs_idr);
-
        pr_debug("driver removed\n");
 }