Skip to content

Commit b29d145

Browse files
author
Hangbin Liu
committed
ptp: annotate data-race around q->head and q->tail
JIRA: https://issues.redhat.com/browse/RHEL-38600 Upstream Status: net.git commit 73bde5a commit 73bde5a Author: Eric Dumazet <edumazet@google.com> Date: Thu Nov 9 17:48:59 2023 +0000 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> Signed-off-by: Hangbin Liu <haliu@redhat.com>
1 parent c6642d4 commit b29d145

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
@@ -490,7 +490,8 @@ ssize_t ptp_read(struct posix_clock *pc,
490490

491491
for (i = 0; i < cnt; i++) {
492492
event[i] = queue->buf[queue->head];
493-
queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
493+
/* Paired with READ_ONCE() in queue_cnt() */
494+
WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
494495
}
495496

496497
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
@@ -56,10 +56,11 @@ static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
5656
dst->t.sec = seconds;
5757
dst->t.nsec = remainder;
5858

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

62-
queue->tail = (queue->tail + 1) % PTP_MAX_TIMESTAMPS;
63+
WRITE_ONCE(queue->tail, (queue->tail + 1) % PTP_MAX_TIMESTAMPS);
6364

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

drivers/ptp/ptp_private.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ struct ptp_vclock {
7676
* that a writer might concurrently increment the tail does not
7777
* matter, since the queue remains nonempty nonetheless.
7878
*/
79-
static inline int queue_cnt(struct timestamp_event_queue *q)
79+
static inline int queue_cnt(const struct timestamp_event_queue *q)
8080
{
81-
int cnt = q->tail - q->head;
81+
/*
82+
* Paired with WRITE_ONCE() in enqueue_external_timestamp(),
83+
* ptp_read(), extts_fifo_show().
84+
*/
85+
int cnt = READ_ONCE(q->tail) - READ_ONCE(q->head);
8286
return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt;
8387
}
8488

drivers/ptp/ptp_sysfs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ static ssize_t extts_fifo_show(struct device *dev,
9090
qcnt = queue_cnt(queue);
9191
if (qcnt) {
9292
event = queue->buf[queue->head];
93-
queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
93+
/* Paired with READ_ONCE() in queue_cnt() */
94+
WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
9495
}
9596
spin_unlock_irqrestore(&queue->lock, flags);
9697

0 commit comments

Comments
 (0)