ptp: prevent possible ABBA deadlock in ptp_clock_freerun()
PATCH v3 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)Functions like clock_adjtime() can only be called with physical clocks. Therefore, all structures used in this function are physical clocks.
However, when unregistering vclocks in n_vclocks_store(), 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.
Therefore, to prevent such false positive in lockdep, a subclass annotation must be added to the lock used in the virtual clock structure.
---
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 | 16 ++++++++++++++++ 2 files changed, 21 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 +16 -0
@@ -154,6 +154,20 @@ static long ptp_vclock_refresh(struct ptp_clock_info *ptp)return PTP_VCLOCK_REFRESH_INTERVAL ; } +#ifdef CONFIG_LOCKDEP +static void ptp_vclock_set_subclass (struct ptp_clock *ptp) +{ +lockdep_set_subclass (&ptp->n_vclocks_mux,PTP_LOCK_VIRTUAL ); +lockdep_set_subclass (&ptp->clock.rwsem,PTP_LOCK_VIRTUAL ); +lockdep_set_subclass (&ptp->tsevqs_lock,PTP_LOCK_VIRTUAL ); +lockdep_set_subclass (&ptp->pincfg_mux,PTP_LOCK_VIRTUAL ); +} +#else +static void ptp_vclock_set_subclass (struct ptp_clock *ptp) +{ +} +#endif +static const struct ptp_clock_info ptp_vclock_info = { .owner =THIS_MODULE , .name ="ptp virtual clock" ,
@@ -213,6 +227,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 5 lines · Jeongjun Park
drivers/ptp/ptp_vclock.c +0
-0
drivers/ptp/ptp_vclock.c static long ptp_vclock_refresh(struct ptp_clock_info *ptp) · line 163
Quoted 10 lines
drivers/ptp/ptp_vclock.c +7
-0@@ -154,6 +154,20 @@ static long ptp_vclock_refresh(struct ptp_clock_info *ptp)return PTP_VCLOCK_REFRESH_INTERVAL ; } +#ifdef CONFIG_LOCKDEP +static void ptp_vclock_set_subclass (struct ptp_clock *ptp) +{ +lockdep_set_subclass (&ptp->n_vclocks_mux,PTP_LOCK_VIRTUAL ); +lockdep_set_subclass (&ptp->clock.rwsem,PTP_LOCK_VIRTUAL ); +lockdep_set_subclass (&ptp->tsevqs_lock,PTP_LOCK_VIRTUAL ); +lockdep_set_subclass (&ptp->pincfg_mux,PTP_LOCK_VIRTUAL );
Every other lock except &ptp->clock.rwsem is unrelated, and I wouldn't touch what is unrelated as part of a bug fix. That, plus I believe this breaks the data encapsulation of struct posix_clock. At least CC the "POSIX CLOCKS and TIMERS" maintainers in v4, so that they're aware of your intentions.
drivers/ptp/ptp_vclock.c static long ptp_vclock_refresh(struct ptp_clock_info *ptp) · line 170
Quoted 7 lines
drivers/ptp/ptp_vclock.c +7
-0+} +#else +static void ptp_vclock_set_subclass (struct ptp_clock *ptp) +{ +} +#endif +
lockdep_set_subclass() has shim definitions for CONFIG_LOCKDEP=n, you don't need the #ifdef.
Quoted 10 lines · Vladimir Oltean
Quoted 8 lines · Jeongjun Park
drivers/ptp/ptp_vclock.c +0
-0@@ -154,6 +154,20 @@ static long ptp_vclock_refresh(struct ptp_clock_info *ptp)return PTP_VCLOCK_REFRESH_INTERVAL ; }
Quoted 13 lines
drivers/ptp/ptp_vclock.c static long ptp_vclock_refresh(struct ptp_clock_info *ptp) · line 163
Quoted 7 lines
drivers/ptp/ptp_vclock.c +7
-0+#ifdef CONFIG_LOCKDEP +static void ptp_vclock_set_subclass (struct ptp_clock *ptp) +{ +lockdep_set_subclass (&ptp->n_vclocks_mux,PTP_LOCK_VIRTUAL ); +lockdep_set_subclass (&ptp->clock.rwsem,PTP_LOCK_VIRTUAL ); +lockdep_set_subclass (&ptp->tsevqs_lock,PTP_LOCK_VIRTUAL ); +lockdep_set_subclass (&ptp->pincfg_mux,PTP_LOCK_VIRTUAL );Every other lock except
&ptp->clock.rwsemis unrelated, and I wouldn't touch what is unrelated as part of a bug fix. That, plus I believe this breaks the data encapsulation of structposix_clock. At least CC the "POSIX CLOCKS and TIMERS" maintainers in v4, so that they're aware of your intentions.
Okay, I'll CC the posix_clock maintainers.
However, I think ptp->n_vclocks_mux also needs to be annotating lock subclass because there may be false positives due to recursive locking between physical and virtual clocks.
Quoted 3 lines · Jeongjun Park
However, I think
ptp->n_vclocks_muxalso needs to be annotating lock subclass because there may be false positives due to recursive locking between physical and virtual clocks.
Did you miss the part where I reiterated, in my review comment to your v2, that after commit 5ab73b010cad ("ptp: fix breakage after ptp_vclock_in_use() rework"), ptp->n_vclocks_mux is only acquired by physical clocks, not by virtual clocks?
Also, in general I think it would be useful to include more substantial pieces of my explanation in your commit message, or link to it in its entirety. I am worried that the info from it becomes denatured, for example this piece from your commit message: "Functions like clock_adjtime() can only be called with physical clocks." I did not say that, I said that **in order for the clock_adjtime() call to acquire &ptp->n_vclocks_mux**, then the clock must have been physical. In general, adjusting a virtual clock is perfectly possible, thus your restatement is false, and it proves a lack of understanding of the ptp->n_vclocks_mux locking convention.