Skip to content

Commit 73bde5a

Browse files
edumazetkuba-moo
authored andcommitted
ptp: annotate data-race around q->head and q->tail
As I was working on a syzbot report, I found that KCSAN would probably complain that reading q->head or q->tail without barriers could lead to invalid results. Add corresponding READ_ONCE() and WRITE_ONCE() to avoid load-store tearing. Fixes: d94ba80 ("ptp: Added a brand new class driver for ptp clocks.") Signed-off-by: Eric Dumazet <edumazet@google.com> Acked-by: Richard Cochran <richardcochran@gmail.com> Link: https://lore.kernel.org/r/20231109174859.3995880-1-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 4b3812d commit 73bde5a

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

drivers/ptp/ptp_chardev.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags,
572572

573573
for (i = 0; i < cnt; i++) {
574574
event[i] = queue->buf[queue->head];
575-
queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
575+
/* Paired with READ_ONCE() in queue_cnt() */
576+
WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
576577
}
577578

578579
spin_unlock_irqrestore(&queue->lock, flags);

drivers/ptp/ptp_clock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
5757
dst->t.sec = seconds;
5858
dst->t.nsec = remainder;
5959

60+
/* Both WRITE_ONCE() are paired with READ_ONCE() in queue_cnt() */
6061
if (!queue_free(queue))
61-
queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
62+
WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
6263

63-
queue->tail = (queue->tail + 1) % PTP_MAX_TIMESTAMPS;
64+
WRITE_ONCE(queue->tail, (queue->tail + 1) % PTP_MAX_TIMESTAMPS);
6465

6566
spin_unlock_irqrestore(&queue->lock, flags);
6667
}

drivers/ptp/ptp_private.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ struct ptp_vclock {
8585
* that a writer might concurrently increment the tail does not
8686
* matter, since the queue remains nonempty nonetheless.
8787
*/
88-
static inline int queue_cnt(struct timestamp_event_queue *q)
88+
static inline int queue_cnt(const struct timestamp_event_queue *q)
8989
{
90-
int cnt = q->tail - q->head;
90+
/*
91+
* Paired with WRITE_ONCE() in enqueue_external_timestamp(),
92+
* ptp_read(), extts_fifo_show().
93+
*/
94+
int cnt = READ_ONCE(q->tail) - READ_ONCE(q->head);
9195
return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt;
9296
}
9397

drivers/ptp/ptp_sysfs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ static ssize_t extts_fifo_show(struct device *dev,
9494
qcnt = queue_cnt(queue);
9595
if (qcnt) {
9696
event = queue->buf[queue->head];
97-
queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
97+
/* Paired with READ_ONCE() in queue_cnt() */
98+
WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
9899
}
99100
spin_unlock_irqrestore(&queue->lock, flags);
100101

0 commit comments

Comments
 (0)