Skip to content

Commit 3003d55

Browse files
agotorvalds
authored andcommitted
pps: fix race in PPS_FETCH handler
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. Signed-off-by: Alexander Gordeev <lasaine@lvk.cs.msu.su> Acked-by: Rodolfo Giometti <giometti@linux.it> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 86d921f commit 3003d55

3 files changed

Lines changed: 10 additions & 6 deletions

File tree

‎drivers/pps/kapi.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ void pps_event(int source, struct pps_ktime *ts, int event, void *data)
326326

327327
/* Wake up if captured something */
328328
if (captured) {
329-
pps->go = ~0;
330-
wake_up_interruptible(&pps->queue);
329+
pps->last_ev++;
330+
wake_up_interruptible_all(&pps->queue);
331331

332332
kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
333333
}

‎drivers/pps/pps.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,20 @@ static long pps_cdev_ioctl(struct file *file,
136136

137137
case PPS_FETCH: {
138138
struct pps_fdata fdata;
139+
unsigned int ev;
139140

140141
pr_debug("PPS_FETCH: source %d\n", pps->id);
141142

142143
err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
143144
if (err)
144145
return -EFAULT;
145146

146-
pps->go = 0;
147+
ev = pps->last_ev;
147148

148149
/* Manage the timeout */
149150
if (fdata.timeout.flags & PPS_TIME_INVALID)
150-
err = wait_event_interruptible(pps->queue, pps->go);
151+
err = wait_event_interruptible(pps->queue,
152+
ev != pps->last_ev);
151153
else {
152154
unsigned long ticks;
153155

@@ -159,7 +161,9 @@ static long pps_cdev_ioctl(struct file *file,
159161

160162
if (ticks != 0) {
161163
err = wait_event_interruptible_timeout(
162-
pps->queue, pps->go, ticks);
164+
pps->queue,
165+
ev != pps->last_ev,
166+
ticks);
163167
if (err == 0)
164168
return -ETIMEDOUT;
165169
}

‎include/linux/pps_kernel.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct pps_device {
5555
struct pps_ktime clear_tu;
5656
int current_mode; /* PPS mode at event time */
5757

58-
int go; /* PPS event is arrived? */
58+
unsigned int last_ev; /* last PPS event id */
5959
wait_queue_head_t queue; /* PPS event queue */
6060

6161
unsigned int id; /* PPS source unique ID */

0 commit comments

Comments
 (0)