Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ice: unreleased phc->lock on error path in
  ice_ptp_rebuild_owner()
- [Medium] ice: premature phc->update_time store in
  ice_ptp_read_cycles()
--

commit b8fa2e5442cd684f10151ff2ab484f07d9521f1c
Author: Jacob Keller <[email protected]>

ice: use struct timecounter for E825 and E830

This commit switches E825 and E830 devices to use a seqcount_latch-protected
timecounter and cyclecounter in struct ice_phc instead of directly adjusting
the hardware PHC timers and using the legacy timestamp extension logic.

> diff --git a/drivers/net/ethernet/intel/ice/ice_ptp.c 
> b/drivers/net/ethernet/intel/ice/ice_ptp.c
> index d07dcf619bb18..a74a2075e5f53 100644
> --- a/drivers/net/ethernet/intel/ice/ice_ptp.c
> +++ b/drivers/net/ethernet/intel/ice/ice_ptp.c
> @@ -272,6 +272,202 @@ int ice_ptp_read_src_clk_reg(struct ice_pf *pf,
>       return 0;
>  }
>  
> +/**
> + * ice_ptp_read_cycles - cyclecounter read() callback function
> + * @cc: the hardware cycle counter structure
> + *
> + * Read the PHC time via ice_ptp_read_src_clk_reg() and report it for use
> + * within timecounter_read() and timecounter_init().
> + *
> + * Although the cycle counter is masked to 32-bits for timestamp extension,
> + * this still reports the full 64-bit time to enable determining the phase
> + * difference between the PHC hardware value and the timecounter properly.
> + *
> + * Return: The current PHC timer value from the hardware on success.
> + *         Since failure cannot be meaningfully reported, return the previous
> + *         cycle_last value when unable to access the device.
> + */
> +u64 ice_ptp_read_cycles(struct cyclecounter *cc)
> +{
> +     struct ice_adapter *adapter;
> +     struct ice_phc *phc;
> +     struct ice_pf *pf;
> +     u64 time;
> +     int err;
> +
> +     phc = container_of(cc, struct ice_phc, cc);
> +     adapter = container_of(phc, struct ice_adapter, phc);
> +
> +     scoped_guard(rcu) {
> +             pf = rcu_dereference(adapter->ctrl_pf);
> +             if (!pf)
> +                     return phc->tc[0].cycle_last;
> +
> +             err = ice_ptp_read_src_clk_reg(pf, NULL, &time);
> +             if (err)
> +                     return phc->tc[0].cycle_last;
> +
> +             /* Paired with smp_load_acquire in ice_ptp_extend_32b_ts */
> +             smp_store_release(&phc->update_time, ktime_get());
> +             return time;
> +     }
> +}

[Severity: Medium]
Can publishing phc->update_time inside ice_ptp_read_cycles() allow
ice_ptp_extend_32b_ts() to extend a timestamp against an uninitialized or
stale phc->tc[1]?

In ice_phc_init() and ice_phc_refresh(), write_seqcount_latch_begin() sets
phc->seq to an odd value (directing readers to phc->tc[1]) before calling
timecounter_init() or timecounter_read() on phc->tc[0]:

CPU0                                  CPU1
ice_phc_init()
  write_seqcount_latch_begin(&phc->seq)
  timecounter_init(&phc->tc[0], ...)
    ice_ptp_read_cycles()
      smp_store_release(&phc->update_time, ...)
                                      ice_ptp_extend_32b_ts()
                                        smp_load_acquire(&phc->update_time)
                                        ice_phc_cyc2time_with_trace()
                                          seq = read_seqcount_latch(&phc->seq)
                                          /* idx == 1 */
                                          timecounter_cyc2time(&phc->tc[1], ...)
                                          <-- reads uninitialized/stale tc[1]
    phc->tc[0].cycle_last = ...
    phc->tc[0].nsec = ...
  write_seqcount_latch(&phc->seq)
  phc->tc[1].cycle_last = ...
  phc->tc[1].nsec = ...
  write_seqcount_latch_end(&phc->seq)

Because read_seqcount_latch_retry() succeeds while phc->seq remains at the
same odd value, the reader converts the hardware timestamp using the old
phc->tc[1] instead of discarding it or using the updated timecounter.
Updating phc->update_time after write_seqcount_latch_end() (or after
write_seqcount_latch() once phc->tc[0] is populated and latched) would avoid
exposing update_time before a valid timecounter snapshot is visible.

[ ... ]

> @@ -3207,14 +3519,24 @@ static int ice_ptp_rebuild_owner(struct ice_pf *pf)
>       if (phc->update_time) {
>               s64 delta = ktime_sub(ktime_get(), phc->update_time);
>  
> -             ts = ns_to_timespec64(phc->cached_time + delta);
> +             if (phc->use_timecounter)
> +                     ns = phc->tc[0].nsec + delta;
> +             else
> +                     ns = phc->cached_time + delta;
>       } else {
> -             ts = ktime_to_timespec64(ktime_get_real());
> +             ns = ktime_get_real_ns();
> +     }
> +
> +     if (phc->use_timecounter) {
> +             ice_phc_init(phc, ns);
> +     } else {
> +             struct timespec64 ts = ns_to_timespec64(ns);
> +
> +             err = ice_ptp_write_init(pf, &ts);
> +             if (err)
> +                     goto err_unlock;
>       }
>       mutex_unlock(&phc->lock);
> -     err = ice_ptp_write_init(pf, &ts);
> -     if (err)
> -             goto err_unlock;

[Severity: Medium]
Does jumping to err_unlock when ice_ptp_write_init() fails leave phc->lock
held?  The err_unlock path only calls ice_ptp_unlock(hw), so any subsequent
attempt to acquire phc->lock on a non-timecounter device will deadlock.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=4

Reply via email to