ptp: prevent possible ABBA deadlock in ptp_clock_freerun()
PATCH v4 1/1 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.
---
v4: Remove unnecessary lock class annotation and CC "POSIX CLOCKS and TIMERS" maintainer
- Link to v3: https://lore.kernel.org/all/[email protected]/
v3: Annotate lock subclass to prevent false positives of lockdep
- Link to v2: https://lore.kernel.org/all/[email protected]/
v2: Add CC Vladimir
- Link to v1: https://lore.kernel.org/all/[email protected]/
--- drivers/ptp/ptp_private.h | 5 +++++ drivers/ptp/ptp_vclock.c | 7 +++++++ 2 files changed, 12 insertions(+)
drivers/ptp/ptp_private.h +5 -0
@@ -24,6 +24,11 @@#define PTP_DEFAULT_MAX_VCLOCKS 20#define PTP_MAX_CHANNELS 2048 +enum { +PTP_LOCK_PHYSICAL =0 , +PTP_LOCK_VIRTUAL , +}; +struct timestamp_event_queue {struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS ];int head;
drivers/ptp/ptp_vclock.c +7 -0
@@ -154,6 +154,11 @@ static long ptp_vclock_refresh(struct ptp_clock_info *ptp)return PTP_VCLOCK_REFRESH_INTERVAL ; } +static void ptp_vclock_set_subclass (struct ptp_clock *ptp) +{ +lockdep_set_subclass (&ptp->clock.rwsem,PTP_LOCK_VIRTUAL ); +} +static const struct ptp_clock_info ptp_vclock_info = { .owner =THIS_MODULE , .name ="ptp virtual clock" ,
@@ -213,6 +218,8 @@ struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock)return NULL ; } +ptp_vclock_set_subclass (vclock->clock); +timecounter_init (&vclock->tc, &vclock->cc,0 );ptp_schedule_worker (vclock->clock,PTP_VCLOCK_REFRESH_INTERVAL );
--
Quoted 9 lines · Jeongjun Park
However, when unregistering vclocks in
n_vclocks_store(), the lockingptp->n_vclocks_muxis a physical clock lock, butclk->rwsemofptp_clock_unregister()called throughdevice_for_each_child_reverse()is a virtual clock lock.Therefore,
clk->rwsemused in CPU0 andclk->rwsemused in CPU1 are different locks, but in lockdep, a false positive occurs because the possibility of deadlock is determined through lock-class.
Quoted 7 lines
To solve this, lock subclass annotation must be added to the
posix_clockrwsem of the vclock.Reported-by [email protected]Fixes 73f37068d540 ("ptp: support ptp physical/virtual clocks conversion")Signed-off-by Jeongjun Park <[email protected]>
Quoted 40 lines · Jeongjun Park
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_clockandposix_clockmust be physical clocks forptp_vclock_in_use()to lock&ptp->n_vclocks_muxand checkptp->n_vclocks.However, when unregistering vclocks in
n_vclocks_store(), the lockingptp->n_vclocks_muxis a physical clock lock, butclk->rwsemofptp_clock_unregister()called throughdevice_for_each_child_reverse()is a virtual clock lock.Therefore,
clk->rwsemused in CPU0 andclk->rwsemused 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_clockrwsem of the vclock.Reported-by [email protected]Fixes 73f37068d540 ("ptp: support ptp physical/virtual clocks conversion")Signed-off-by Jeongjun Park <[email protected]>
Quoted 8 lines
---
v4: Remove unnecessary lock class annotation and CC "POSIX CLOCKS and TIMERS" maintainer
- Link to v3: https://lore.kernel.org/all/[email protected]/
v3: Annotate lock subclass to prevent false positives of lockdep
- Link to v2: https://lore.kernel.org/all/[email protected]/
v2: Add CC Vladimir
- Link to v1: https://lore.kernel.org/all/[email protected]/
---
For the general idea:
Quoted 25 lines
drivers/ptp/ptp_private.h | 5 +++++ drivers/ptp/ptp_vclock.c | 7 +++++++ 2 files changed, 12 insertions(+)drivers/ptp/ptp_private.h +5
-0@@ -24,6 +24,11 @@#define PTP_DEFAULT_MAX_VCLOCKS 20#define PTP_MAX_CHANNELS 2048 +enum { +PTP_LOCK_PHYSICAL =0 , +PTP_LOCK_VIRTUAL , +}; +struct timestamp_event_queue {struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS ];int head;drivers/ptp/ptp_vclock.c +0
-0
drivers/ptp/ptp_vclock.c static long ptp_vclock_refresh(struct ptp_clock_info *ptp) · line 159
Quoted 6 lines
drivers/ptp/ptp_vclock.c +3
-0@@ -154,6 +154,11 @@ static long ptp_vclock_refresh(struct ptp_clock_info *ptp)return PTP_VCLOCK_REFRESH_INTERVAL ; } +static void ptp_vclock_set_subclass (struct ptp_clock *ptp) +{ +lockdep_set_subclass (&ptp->clock.rwsem,PTP_LOCK_VIRTUAL );
Just not sure whether the PTP clock should be exposing this API, or the POSIX clock, who actually owns the rwsem.
Quoted 15 lines
drivers/ptp/ptp_vclock.c +4
-1+} +static const struct ptp_clock_info ptp_vclock_info = { .owner =THIS_MODULE , .name ="ptp virtual clock" ,@@ -213,6 +218,8 @@ struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock)return NULL ; } +ptp_vclock_set_subclass (vclock->clock); +timecounter_init (&vclock->tc, &vclock->cc,0 );ptp_schedule_worker (vclock->clock,PTP_VCLOCK_REFRESH_INTERVAL ); --
Jakub Kicinski
maintainerQuoted 6 lines · On Tue, 29 Jul 2025 18:48:11 +0300 Vladimir Oltean wrote:
drivers/ptp/ptp_vclock.c static long ptp_vclock_refresh(struct ptp_clock_info *ptp) · line 159
Quoted 3 lines
drivers/ptp/ptp_vclock.c +3
-0+static void ptp_vclock_set_subclass (struct ptp_clock *ptp) +{ +lockdep_set_subclass (&ptp->clock.rwsem,PTP_LOCK_VIRTUAL );Just not sure whether the PTP clock should be exposing this API, or the POSIX clock, who actually owns the rwsem.
Hi Thomas, how do you feel about PTP setting lockdep class on the clock rwsem? Link: https://lore.kernel.org/[email protected]
Quoted 34 lines · Jeongjun Park
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_clockandposix_clockmust be physical clocks forptp_vclock_in_use()to lock&ptp->n_vclocks_muxand checkptp->n_vclocks.However, when unregistering vclocks in
n_vclocks_store(), the lockingptp->n_vclocks_muxis a physical clock lock, butclk->rwsemofptp_clock_unregister()called throughdevice_for_each_child_reverse()is a virtual clock lock.Therefore,
clk->rwsemused in CPU0 andclk->rwsemused in CPU1 are different locks, but in lockdep, a false positive occurs because the possibility of deadlock is determined through lock-class.
Quoted 5 lines
To solve this, lock subclass annotation must be added to the
posix_clockrwsem of the vclock.
Quoted 56 lines
Fixes 73f37068d540 ("ptp: support ptp physical/virtual clocks conversion")Signed-off-by Jeongjun Park <[email protected]>---
v4: Remove unnecessary lock class annotation and CC "POSIX CLOCKS and TIMERS" maintainer
- Link to v3: https://lore.kernel.org/all/[email protected]/
v3: Annotate lock subclass to prevent false positives of lockdep
- Link to v2: https://lore.kernel.org/all/[email protected]/
v2: Add CC Vladimir
- Link to v1: https://lore.kernel.org/all/[email protected]/--- drivers/ptp/ptp_private.h | 5 +++++ drivers/ptp/ptp_vclock.c | 7 +++++++ 2 files changed, 12 insertions(+)drivers/ptp/ptp_private.h +5
-0@@ -24,6 +24,11 @@#define PTP_DEFAULT_MAX_VCLOCKS 20#define PTP_MAX_CHANNELS 2048 +enum { +PTP_LOCK_PHYSICAL =0 , +PTP_LOCK_VIRTUAL , +}; +struct timestamp_event_queue {struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS ];int head;drivers/ptp/ptp_vclock.c +7
-1@@ -154,6 +154,11 @@ static long ptp_vclock_refresh(struct ptp_clock_info *ptp)return PTP_VCLOCK_REFRESH_INTERVAL ; } +static void ptp_vclock_set_subclass (struct ptp_clock *ptp) +{ +lockdep_set_subclass (&ptp->clock.rwsem,PTP_LOCK_VIRTUAL ); +} +static const struct ptp_clock_info ptp_vclock_info = { .owner =THIS_MODULE , .name ="ptp virtual clock" ,@@ -213,6 +218,8 @@ struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock)return NULL ; } +ptp_vclock_set_subclass (vclock->clock); +timecounter_init (&vclock->tc, &vclock->cc,0 );ptp_schedule_worker (vclock->clock,PTP_VCLOCK_REFRESH_INTERVAL ); --