Skip to content

Commit 2efe412

Browse files
name2965kuba-moo
authored andcommitted
ptp: prevent possible ABBA deadlock in ptp_clock_freerun()
syzbot reported the following ABBA deadlock: CPU0 CPU1 ---- ---- n_vclocks_store() lock(&ptp->n_vclocks_mux) [1] (physical clock) pc_clock_adjtime() lock(&clk->rwsem) [2] (physical clock) ... ptp_clock_freerun() ptp_vclock_in_use() lock(&ptp->n_vclocks_mux) [3] (physical clock) ptp_clock_unregister() posix_clock_unregister() lock(&clk->rwsem) [4] (virtual clock) Since ptp virtual clock is registered only under ptp physical clock, both ptp_clock and posix_clock must be physical clocks for ptp_vclock_in_use() to lock &ptp->n_vclocks_mux and check ptp->n_vclocks. However, when unregistering vclocks in n_vclocks_store(), the locking ptp->n_vclocks_mux is a physical clock lock, but clk->rwsem of ptp_clock_unregister() called through device_for_each_child_reverse() is a virtual clock lock. Therefore, clk->rwsem used in CPU0 and clk->rwsem used in CPU1 are different locks, but in lockdep, a false positive occurs because the possibility of deadlock is determined through lock-class. To solve this, lock subclass annotation must be added to the posix_clock rwsem of the vclock. Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=7cfb66a237c4a5fb22ad Fixes: 73f3706 ("ptp: support ptp physical/virtual clocks conversion") Signed-off-by: Jeongjun Park <[email protected]> Acked-by: Richard Cochran <[email protected]> Reviewed-by: Vladimir Oltean <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent c04fdca commit 2efe412

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

drivers/ptp/ptp_private.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
#define PTP_DEFAULT_MAX_VCLOCKS 20
2525
#define PTP_MAX_CHANNELS 2048
2626

27+
enum {
28+
PTP_LOCK_PHYSICAL = 0,
29+
PTP_LOCK_VIRTUAL,
30+
};
31+
2732
struct timestamp_event_queue {
2833
struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS];
2934
int head;

drivers/ptp/ptp_vclock.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ static long ptp_vclock_refresh(struct ptp_clock_info *ptp)
154154
return PTP_VCLOCK_REFRESH_INTERVAL;
155155
}
156156

157+
static void ptp_vclock_set_subclass(struct ptp_clock *ptp)
158+
{
159+
lockdep_set_subclass(&ptp->clock.rwsem, PTP_LOCK_VIRTUAL);
160+
}
161+
157162
static const struct ptp_clock_info ptp_vclock_info = {
158163
.owner = THIS_MODULE,
159164
.name = "ptp virtual clock",
@@ -213,6 +218,8 @@ struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock)
213218
return NULL;
214219
}
215220

221+
ptp_vclock_set_subclass(vclock->clock);
222+
216223
timecounter_init(&vclock->tc, &vclock->cc, 0);
217224
ptp_schedule_worker(vclock->clock, PTP_VCLOCK_REFRESH_INTERVAL);
218225

0 commit comments

Comments
 (0)