Przemyslaw Korba said:> ice_ptp_update_cached_phctime() reads the PHC
and then stores the value
> in the cached time that is used to extend Tx and Rx timestamps. It is
> called both from the periodic work and, after the PHC time is changed,
> from the settime/adjtime paths.
>
> Nothing orders these refreshes. The periodic work can read the PHC just
> before software changes its time, be delayed, and then store its reading
> after the refresh that follows the change. The cache then holds the old
> time until the next periodic update, up to 500 ms later. Timestamps
> extended in that window keep the correct low 32 bits but take the upper
> bits from the old time, so they are off by a multiple of 2^32 ns. Two
> concurrent time changes can race in the same way.
>
> For a follower that steps a large initial offset this makes ptp4l see
> the offset remain unchanged after the step, followed by a clockcheck
> failure and a second UNCALIBRATED transition:
>
> port 1: UNCALIBRATED to SLAVE on MASTER_CLOCK_SELECTED
> clockcheck: clock jumped forward or running faster than expected!
> port 1: SLAVE to UNCALIBRATED on SYNCHRONIZATION_FAULT
> port 1: UNCALIBRATED to SLAVE on MASTER_CLOCK_SELECTED
>
> In the failing case the offset after the step was exactly 5867 * 2^32 ns
> plus noise, i.e. only the low 32 bits of the time were right.
>
> Hold a spinlock across both the PHC read and the store of the cached
> time. Whichever refresh reads the PHC last then also stores last, so an
> older reading can never overwrite a newer one. The lock is only held for
> the register read and is taken before the GLTSYN_TIME lock that
> ice_ptp_read_src_clk_reg() takes, which is never held while taking it.
>
> Steps to reproduce:
> 1. Create QinQ (802.1ad outer, 802.1Q inner) VLANs on two directly
> connected E810 ports.
> 2. Run a PTP leader on one side and ptp4l -s on the other, with the
> follower PHC about 7 hours behind the leader, and run iperf3 on the
> VLAN.
> 3. Restart the follower repeatedly.
>
> This is not reliably reproducible, as it depends on the periodic work
> overlapping the step. With a 200 ms delay added between the PHC read and
> the store, 3 of 20 runs show the second UNCALIBRATED transition and
> SYNCHRONIZATION_FAULT without this change.
>
> With a random delay of up to 100 ms added at the same place, and 4
> parallel "phc_ctl adj 1" loops running for 20 seconds, the stored value
> went backwards 579 times in 1509 stores without this change, and 0
> times in 399 stores with it.
>
> Tested on E810-C.
>
> Fixes: 77a781155a65 ("ice: enable receive hardware timestamping")
> Reviewed-by: Milena Olech <[email protected]>
> Signed-off-by: Przemyslaw Korba <[email protected]>
> ---
> drivers/net/ethernet/intel/ice/ice_adapter.c | 1 +
> drivers/net/ethernet/intel/ice/ice_adapter.h | 4 ++++
> drivers/net/ethernet/intel/ice/ice_ptp.c | 9 +++++++++
> 3 files changed, 14 insertions(+)
>
> diff --git a/drivers/net/ethernet/intel/ice/ice_adapter.c
> b/drivers/net/ethernet/intel/ice/ice_adapter.c
> index 207e4fe97be7..9874380b642d 100644
> --- a/drivers/net/ethernet/intel/ice/ice_adapter.c
> +++ b/drivers/net/ethernet/intel/ice/ice_adapter.c
> @@ -91,6 +91,7 @@ static struct ice_adapter *ice_adapter_new(struct pci_dev
> *pdev)
> timecounter_init(&adapter->phc.tc, &adapter->phc.cc, 0);
>
> seqlock_init(&adapter->phc.tc_lock);
> + spin_lock_init(&adapter->phc.cache_lock);
>
I think v4 of my timecounter series fixes this because each update is
protected by the phc->lock.
The periodic update takes that lock in ice_ptp_update_cached_phctime,
and readers get a consistent view because of the write_seqcount_latch.
The updates to the cached time only ever happen under the phc->lock
mutex and the read for the time to save also happens under lock. Thus, I
think this patch should not be needed with that timecounter series.
Do you agree?
> return adapter;
> }
> diff --git a/drivers/net/ethernet/intel/ice/ice_adapter.h
> b/drivers/net/ethernet/intel/ice/ice_adapter.h
> index d8da68e73177..45c82f8d7f43 100644
> --- a/drivers/net/ethernet/intel/ice/ice_adapter.h
> +++ b/drivers/net/ethernet/intel/ice/ice_adapter.h
> @@ -51,6 +51,8 @@ struct ice_port_list {
> *
> * Legacy timestamp extension fields:
> * @cached_time: Cached copy of the PHC time for timestamp extension
> + * @cache_lock: Serializes reading the PHC and updating @cached_time, so that
> + * an older reading can never overwrite a newer one
> *
> * struct timecounter fields:
> * @tc: timecounter representing the current time
> @@ -64,6 +66,8 @@ struct ice_port_list {
> struct ice_phc {
> /* Legacy timestamp extension fields */
> u64 cached_time;
> + /* Protects reading the PHC for, and updating, cached_time */
> + spinlock_t cache_lock;
>
> /* struct timecounter fields */
> struct timecounter tc;
> diff --git a/drivers/net/ethernet/intel/ice/ice_ptp.c
> b/drivers/net/ethernet/intel/ice/ice_ptp.c
> index 488db8298059..1486a6fc35f5 100644
> --- a/drivers/net/ethernet/intel/ice/ice_ptp.c
> +++ b/drivers/net/ethernet/intel/ice/ice_ptp.c
> @@ -1079,6 +1079,15 @@ static void ice_ptp_update_cached_phctime(struct
> ice_pf *pf)
> u64 systime;
> int err;
>
> + /* The periodic work and the time set/adjust paths all refresh
> + * the cache. Hold the lock across both the PHC read and the
> + * store, so that whichever refresh reads the PHC last is also
> + * the one that stores last. Otherwise, a reading taken before
> + * a time change can overwrite a newer one and leave timestamps
> + * extended against the old time until the next update.
> + */
> + guard(spinlock)(&phc->cache_lock);
> +
> err = ice_ptp_read_src_clk_reg(pf, NULL, &systime);
> if (err) {
> /* The source clock is gone, so the cache can no
>
> base-commit: 34a70a40c954fb8044ecd124840a2c38cd3aa4cc
> --
> 2.43.0
>