[PATCHv6 07/16] pps: move idr stuff to pps.c

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Alexander Gordeev
Date: Friday, December 17, 2010 - 12:54 pm

Since now idr is only used to manage char device id's and not used in
kernel API anymore it should be moved to pps.c. This also makes it
possible to release id only at actual device freeing so nobody can
register a pps device with the same id while our device is not freed
yet.

Acked-by: Rodolfo Giometti <giometti@linux.it>
Signed-off-by: Alexander Gordeev <lasaine@lvk.cs.msu.su>
---
 drivers/pps/kapi.c |   56 ++-------------------------------------------------
 drivers/pps/pps.c  |   50 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 54 deletions(-)

diff --git a/drivers/pps/kapi.c b/drivers/pps/kapi.c
index e8847c1..c42d3cb 100644
--- a/drivers/pps/kapi.c
+++ b/drivers/pps/kapi.c
@@ -27,19 +27,11 @@
 #include <linux/sched.h>
 #include <linux/time.h>
 #include <linux/spinlock.h>
-#include <linux/idr.h>
 #include <linux/fs.h>
 #include <linux/pps_kernel.h>
 #include <linux/slab.h>
 
 /*
- * Local variables
- */
-
-static DEFINE_SPINLOCK(pps_idr_lock);
-static DEFINE_IDR(pps_idr);
-
-/*
  * Local functions
  */
 
@@ -76,7 +68,6 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 		int default_params)
 {
 	struct pps_device *pps;
-	int id;
 	int err;
 
 	/* Sanity checks */
@@ -117,54 +108,18 @@ struct pps_device *pps_register_source(struct pps_source_info *info,
 	init_waitqueue_head(&pps->queue);
 	spin_lock_init(&pps->lock);
 
-	/* Get new ID for the new PPS source */
-	if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
-		err = -ENOMEM;
-		goto kfree_pps;
-	}
-
-	spin_lock_irq(&pps_idr_lock);
-
-	/* Now really allocate the PPS source.
-	 * After idr_get_new() calling the new source will be freely available
-	 * into the kernel.
-	 */
-	err = idr_get_new(&pps_idr, pps, &id);
-	if (err < 0) {
-		spin_unlock_irq(&pps_idr_lock);
-		goto kfree_pps;
-	}
-
-	id = id & MAX_ID_MASK;
-	if (id >= PPS_MAX_SOURCES) {
-		spin_unlock_irq(&pps_idr_lock);
-
-		pr_err("%s: too many PPS sources in the system\n",
-					info->name);
-		err = -EBUSY;
-		goto free_idr;
-	}
-	pps->id = id;
-
-	spin_unlock_irq(&pps_idr_lock);
-
 	/* Create the char device */
 	err = pps_register_cdev(pps);
 	if (err < 0) {
 		pr_err("%s: unable to create char device\n",
 					info->name);
-		goto free_idr;
+		goto kfree_pps;
 	}
 
 	dev_info(pps->dev, "new PPS source %s\n", info->name);
 
 	return pps;
 
-free_idr:
-	spin_lock_irq(&pps_idr_lock);
-	idr_remove(&pps_idr, id);
-	spin_unlock_irq(&pps_idr_lock);
-
 kfree_pps:
 	kfree(pps);
 
@@ -184,15 +139,10 @@ EXPORT_SYMBOL(pps_register_source);
 
 void pps_unregister_source(struct pps_device *pps)
 {
-	unsigned int id = pps->id;
-
 	pps_unregister_cdev(pps);
 
-	spin_lock_irq(&pps_idr_lock);
-	idr_remove(&pps_idr, pps->id);
-	spin_unlock_irq(&pps_idr_lock);
-
-	kfree(pps);
+	/* don't have to kfree(pps) here because it will be done on
+	 * device destruction */
 }
 EXPORT_SYMBOL(pps_unregister_source);
 
diff --git a/drivers/pps/pps.c b/drivers/pps/pps.c
index 9f7c2e8..79b4455 100644
--- a/drivers/pps/pps.c
+++ b/drivers/pps/pps.c
@@ -30,6 +30,7 @@
 #include <linux/cdev.h>
 #include <linux/poll.h>
 #include <linux/pps_kernel.h>
+#include <linux/slab.h>
 
 /*
  * Local variables
@@ -38,6 +39,9 @@
 static dev_t pps_devt;
 static struct class *pps_class;
 
+static DEFINE_SPINLOCK(pps_idr_lock);
+static DEFINE_IDR(pps_idr);
+
 /*
  * Char device methods
  */
@@ -229,11 +233,48 @@ static const struct file_operations pps_cdev_fops = {
 	.release	= pps_cdev_release,
 };
 
+static void pps_device_destruct(struct device *dev)
+{
+	struct pps_device *pps = dev_get_drvdata(dev);
+
+	/* release id here to protect others from using it while it's
+	 * still in use */
+	spin_lock_irq(&pps_idr_lock);
+	idr_remove(&pps_idr, pps->id);
+	spin_unlock_irq(&pps_idr_lock);
+
+	kfree(dev);
+	kfree(pps);
+}
+
 int pps_register_cdev(struct pps_device *pps)
 {
 	int err;
 	dev_t devt;
 
+	/* Get new ID for the new PPS source */
+	if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0)
+		return -ENOMEM;
+
+	/* Now really allocate the PPS source.
+	 * After idr_get_new() calling the new source will be freely available
+	 * into the kernel.
+	 */
+	spin_lock_irq(&pps_idr_lock);
+	err = idr_get_new(&pps_idr, pps, &pps->id);
+	spin_unlock_irq(&pps_idr_lock);
+
+	if (err < 0)
+		return err;
+
+	pps->id &= MAX_ID_MASK;
+	if (pps->id >= PPS_MAX_SOURCES) {
+		pr_err("%s: too many PPS sources in the system\n",
+					pps->info.name);
+		err = -EBUSY;
+		goto free_idr;
+	}
+
 	devt = MKDEV(MAJOR(pps_devt), pps->id);
 
 	cdev_init(&pps->cdev, &pps_cdev_fops);
@@ -243,13 +284,15 @@ 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);
-		return err;
+		goto free_idr;
 	}
 	pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
 							"pps%d", pps->id);
 	if (IS_ERR(pps->dev))
 		goto del_cdev;
 
+	pps->dev->release = pps_device_destruct;
+
 	pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
 			MAJOR(pps_devt), pps->id);
 
@@ -258,6 +301,11 @@ int pps_register_cdev(struct pps_device *pps)
 del_cdev:
 	cdev_del(&pps->cdev);
 
+free_idr:
+	spin_lock_irq(&pps_idr_lock);
+	idr_remove(&pps_idr, pps->id);
+	spin_unlock_irq(&pps_idr_lock);
+
 	return err;
 }
 
-- 
1.7.2.3

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCHv6 00/16] pps: several fixes and improvements, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 03/16] pps: fix race in PPS_FETCH handler, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 04/16] pps: unify timestamp gathering, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 05/16] pps: access pps device by direct pointer, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 06/16] pps: convert printk/pr_* to dev_*, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 07/16] pps: move idr stuff to pps.c, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 08/16] pps: do not disable interrupts for idr ope ..., Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 09/16] pps: use BUG_ON for kernel API safety checks, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 10/16] pps: simplify conditions a bit, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 11/16] pps: timestamp is always passed to dcd_cha ..., Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 12/16] ntp: add hardpps implementation, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 13/16] pps: capture MONOTONIC_RAW timestamps as well, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 14/16] pps: add kernel consumer support, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 15/16] pps: add parallel port PPS client, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
[PATCHv6 16/16] pps: add parallel port PPS signal generator, Alexander Gordeev, (Fri Dec 17, 12:54 pm)
Re: [PATCHv6 07/16] pps: move idr stuff to pps.c, Andrew Morton, (Fri Dec 17, 5:13 pm)
Re: [PATCHv6 15/16] pps: add parallel port PPS client, Andrew Morton, (Fri Dec 17, 5:17 pm)
Re: [PATCHv6 00/16] pps: several fixes and improvements, Andrew Morton, (Fri Dec 17, 5:19 pm)
Re: [PATCHv6 15/16] pps: add parallel port PPS client, Alexander Gordeev, (Fri Dec 17, 5:50 pm)
Re: [PATCHv6 16/16] pps: add parallel port PPS signal gene ..., Alexander Gordeev, (Fri Dec 17, 5:52 pm)
Re: [PATCHv6 00/16] pps: several fixes and improvements, Alexander Gordeev, (Fri Dec 17, 6:00 pm)
Re: [PATCHv6 07/16] pps: move idr stuff to pps.c, Alexander Gordeev, (Fri Dec 17, 6:07 pm)
Re: [PATCHv6 15/16] pps: add parallel port PPS client, Andrew Morton, (Fri Dec 17, 6:13 pm)
Re: [PATCHv6 00/16] pps: several fixes and improvements, Andrew Morton, (Fri Dec 17, 6:14 pm)
Re: [PATCHv6 07/16] pps: move idr stuff to pps.c, Andrew Morton, (Fri Dec 17, 6:18 pm)
[PATCHv7 00/16] changed some patches, Alexander Gordeev, (Mon Dec 20, 4:54 am)
[PATCHv7 08/16] pps: make idr lock a mutex and protect idr ..., Alexander Gordeev, (Mon Dec 20, 4:54 am)
[PATCHv7 12/16] ntp: add hardpps implementation, Alexander Gordeev, (Mon Dec 20, 4:54 am)
[PATCHv7 13/16] pps: capture MONOTONIC_RAW timestamps as well, Alexander Gordeev, (Mon Dec 20, 4:54 am)
[PATCHv7 14/16] pps: add kernel consumer support, Alexander Gordeev, (Mon Dec 20, 4:54 am)
[PATCHv7 15/16] pps: add parallel port PPS client, Alexander Gordeev, (Mon Dec 20, 4:54 am)
[PATCHv7 16/16] pps: add parallel port PPS signal generator, Alexander Gordeev, (Mon Dec 20, 4:54 am)
Re: [PATCHv7 16/16] pps: add parallel port PPS signal gene ..., Alexander Gordeev, (Thu Dec 23, 6:37 pm)