]> www.infradead.org Git - users/willy/xarray.git/commitdiff
pps: Convert pps_idr to XArray
authorMatthew Wilcox <willy@infradead.org>
Mon, 18 Feb 2019 21:29:20 +0000 (16:29 -0500)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Fri, 9 Aug 2019 01:38:18 +0000 (21:38 -0400)
The pps_idr_lock mutex is unnecessary thanks to the included xa_lock.

Signed-off-by: Matthew Wilcox <willy@infradead.org>
drivers/pps/kapi.c
drivers/pps/pps.c

index d9d566f70ed199679773c63d8df9df9a46982cee..cd3172a69d2f7beb9d14197a8abc43f684ffb29c 100644 (file)
@@ -89,8 +89,9 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
                goto pps_register_source_exit;
        }
 
-       /* These initializations must be done before calling idr_alloc()
-        * in order to avoid reces into pps_event().
+       /*
+        * These initializations must be done before calling
+        * pps_register_cdev() in order to avoid races in pps_event().
         */
        pps->params.api_version = PPS_API_VERS;
        pps->params.mode = default_params;
index 22a65ad4e46e6b187c885274aa3bfdb7ebcdb90c..c7215782893db99ff52ca6fb9b6ae6ad8e58a2c6 100644 (file)
 #include <linux/init.h>
 #include <linux/sched.h>
 #include <linux/uaccess.h>
-#include <linux/idr.h>
 #include <linux/mutex.h>
 #include <linux/cdev.h>
 #include <linux/poll.h>
 #include <linux/pps_kernel.h>
 #include <linux/slab.h>
+#include <linux/xarray.h>
 
 #include "kc.h"
 
@@ -28,8 +28,7 @@
 static dev_t pps_devt;
 static struct class *pps_class;
 
-static DEFINE_MUTEX(pps_idr_lock);
-static DEFINE_IDR(pps_idr);
+static DEFINE_XARRAY_ALLOC(pps_devs);
 
 /*
  * Char device methods
@@ -336,9 +335,7 @@ static void pps_device_destruct(struct device *dev)
 
        /* Now we can release the ID for re-use */
        pr_debug("deallocating pps%d\n", pps->id);
-       mutex_lock(&pps_idr_lock);
-       idr_remove(&pps_idr, pps->id);
-       mutex_unlock(&pps_idr_lock);
+       xa_erase(&pps_devs, pps->id);
 
        kfree(dev);
        kfree(pps);
@@ -349,22 +346,19 @@ int pps_register_cdev(struct pps_device *pps)
        int err;
        dev_t devt;
 
-       mutex_lock(&pps_idr_lock);
        /*
-        * Get new ID for the new PPS source.  After idr_alloc() calling
-        * the new source will be freely available into the kernel.
+        * Get new ID for the new PPS source.  After this call
+        * the new source will be freely available in the kernel.
         */
-       err = idr_alloc(&pps_idr, pps, 0, PPS_MAX_SOURCES, GFP_KERNEL);
+       err = xa_alloc(&pps_devs, &pps->id, pps,
+                       XA_LIMIT(0, PPS_MAX_SOURCES - 1), GFP_KERNEL);
        if (err < 0) {
-               if (err == -ENOSPC) {
+               if (err == -EBUSY) {
                        pr_err("%s: too many PPS sources in the system\n",
                               pps->info.name);
-                       err = -EBUSY;
                }
-               goto out_unlock;
+               goto out;
        }
-       pps->id = err;
-       mutex_unlock(&pps_idr_lock);
 
        devt = MKDEV(MAJOR(pps_devt), pps->id);
 
@@ -375,7 +369,7 @@ int pps_register_cdev(struct pps_device *pps)
        if (err) {
                pr_err("%s: failed to add char device %d:%d\n",
                                pps->info.name, MAJOR(pps_devt), pps->id);
-               goto free_idr;
+               goto erase;
        }
        pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
                                                        "pps%d", pps->id);
@@ -394,12 +388,9 @@ int pps_register_cdev(struct pps_device *pps)
 
 del_cdev:
        cdev_del(&pps->cdev);
-
-free_idr:
-       mutex_lock(&pps_idr_lock);
-       idr_remove(&pps_idr, pps->id);
-out_unlock:
-       mutex_unlock(&pps_idr_lock);
+erase:
+       xa_erase(&pps_devs, pps->id);
+out:
        return err;
 }
 
@@ -425,19 +416,17 @@ void pps_unregister_cdev(struct pps_device *pps)
  *
  * The cookie is automatically set to NULL in pps_unregister_source()
  * so that it will not be used again, even if the pps device cannot
- * be removed from the idr due to pending references holding the minor
+ * be removed from the array due to pending references holding the minor
  * number in use.
  */
 struct pps_device *pps_lookup_dev(void const *cookie)
 {
        struct pps_device *pps;
-       unsigned id;
+       unsigned long id;
 
-       rcu_read_lock();
-       idr_for_each_entry(&pps_idr, pps, id)
+       xa_for_each(&pps_devs, id, pps)
                if (cookie == pps->lookup_cookie)
                        break;
-       rcu_read_unlock();
        return pps;
 }
 EXPORT_SYMBOL(pps_lookup_dev);