[PATCHv6 03/16] pps: fix race in PPS_FETCH handler

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

There was a race in PPS_FETCH ioctl handler when several processes want
to obtain PPS data simultaneously using sleeping PPS_FETCH. They all
sleep most of the time in the system call.
With the old approach when the first process waiting on the pps queue
is waken up it makes new system call right away and zeroes pps->go. So
other processes continue to sleep. This is a clear race condition
because of the global 'go' variable.
With the new approach pps->last_ev holds some value increasing at each
PPS event. PPS_FETCH ioctl handler saves current value to the local
variable at the very beginning so it can safely check that there is a
new event by just comparing both variables.

Acked-by: Rodolfo Giometti <giometti@linux.it>
Signed-off-by: Alexander Gordeev <lasaine@lvk.cs.msu.su>
---
 drivers/pps/kapi.c         |    4 ++--
 drivers/pps/pps.c          |   10 +++++++---
 include/linux/pps_kernel.h |    2 +-
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/pps/kapi.c b/drivers/pps/kapi.c
index 55f3961..3f89f5eb 100644
--- a/drivers/pps/kapi.c
+++ b/drivers/pps/kapi.c
@@ -326,8 +326,8 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)
 
 	/* Wake up if captured something */
 	if (captured) {
-		pps->go = ~0;
-		wake_up_interruptible(&pps->queue);
+		pps->last_ev++;
+		wake_up_interruptible_all(&pps->queue);
 
 		kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
 	}
diff --git a/drivers/pps/pps.c b/drivers/pps/pps.c
index c76afb9..dc7e66c 100644
--- a/drivers/pps/pps.c
+++ b/drivers/pps/pps.c
@@ -136,6 +136,7 @@ static long pps_cdev_ioctl(struct file *file,
 
 	case PPS_FETCH: {
 		struct pps_fdata fdata;
+		unsigned int ev;
 
 		pr_debug("PPS_FETCH: source %d\n", pps->id);
 
@@ -143,11 +144,12 @@ static long pps_cdev_ioctl(struct file *file,
 		if (err)
 			return -EFAULT;
 
-		pps->go = 0;
+		ev = pps->last_ev;
 
 		/* Manage the timeout */
 		if (fdata.timeout.flags & PPS_TIME_INVALID)
-			err = wait_event_interruptible(pps->queue, pps->go);
+			err = wait_event_interruptible(pps->queue,
+					ev != pps->last_ev);
 		else {
 			unsigned long ticks;
 
@@ -159,7 +161,9 @@ static long pps_cdev_ioctl(struct file *file,
 
 			if (ticks != 0) {
 				err = wait_event_interruptible_timeout(
-						pps->queue, pps->go, ticks);
+						pps->queue,
+						ev != pps->last_ev,
+						ticks);
 				if (err == 0)
 					return -ETIMEDOUT;
 			}
diff --git a/include/linux/pps_kernel.h b/include/linux/pps_kernel.h
index c930d11..c3aed4b 100644
--- a/include/linux/pps_kernel.h
+++ b/include/linux/pps_kernel.h
@@ -55,7 +55,7 @@ struct pps_device {
 	struct pps_ktime clear_tu;
 	int current_mode;			/* PPS mode at event time */
 
-	int go;					/* PPS event is arrived? */
+	unsigned int last_ev;			/* last PPS event id */
 	wait_queue_head_t queue;		/* PPS event queue */
 
 	unsigned int id;			/* PPS source unique ID */
-- 
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)