Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ice: 32-bit build failure in ice_ptp_extend_32b_ts() on 64-bit
  update_time
--

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

ice: store cached PHC time in ice_adapter

This commit moves the cached PHC time and its update timestamp from
per-PF and per-Rx-ring fields into a shared struct ice_phc embedded in
struct ice_adapter, updated by the clock-owning PF under a mutex and read
locklessly during timestamp extension.

> diff --git a/drivers/net/ethernet/intel/ice/ice_adapter.h 
> b/drivers/net/ethernet/intel/ice/ice_adapter.h
> index ce1fe08462652..ade829c05000a 100644
> --- a/drivers/net/ethernet/intel/ice/ice_adapter.h
> +++ b/drivers/net/ethernet/intel/ice/ice_adapter.h
> @@ -35,6 +35,25 @@ struct ice_port_list {
>       struct srcu_struct srcu;
>  };
>  
> +/**
> + * struct ice_phc - PTP Hardware Clock fields
> + * @cached_time: Cached copy of the PHC time for timestamp extension
> + * @update_time: Monotonic time when PHC was laste updated
> + * @lock: For protecting updates to the PHC fields.
> + *
> + * Readers are lockless and rely on READ_ONCE/WRITE_ONCE for the cached_time
> + * and smp_acquire/smp_release for the update_time field.
> + *
> + * Writers must acquire the lock mutex to ensure that the periodic task 
> cannot
> + * write back an out-of-date cached value after a clock adjustment.
> + */
> +struct ice_phc {
> +     u64 cached_time;
> +     ktime_t update_time;
> +     /* Serialize updates to cached_time */
> +     struct mutex lock;
> +};

[ ... ]

> diff --git a/drivers/net/ethernet/intel/ice/ice_ptp.c 
> b/drivers/net/ethernet/intel/ice/ice_ptp.c
> index bf37c2a7ff863..d07dcf619bb18 100644
> --- a/drivers/net/ethernet/intel/ice/ice_ptp.c
> +++ b/drivers/net/ethernet/intel/ice/ice_ptp.c
> @@ -309,13 +310,26 @@ int ice_ptp_read_src_clk_reg(struct ice_pf *pf,
>   * This is prevented by (a) periodically updating the cached PHC time once
>   * a second, and (b) discarding any Tx timestamp packet if it has waited for
>   * a timestamp for more than one second.
> + *
> + * Return: the extended timestamp value, or 0 if phc->update_time is stale.
>   */
> -static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp)
> +static u64 ice_ptp_extend_32b_ts(struct ice_phc *phc,
> +                              u32 in_tstamp, bool trace_rx)
>  {
> +     u64 ns, cached_phc_time;
>       u32 delta, phc_time_lo;
> -     u64 ns;
> +     ktime_t update_time;
>  
> -     /* Extract the lower 32 bits of the PHC time */
> +     /* Discard the timestamp if update_time hasn't been initialized */
> +     update_time = smp_load_acquire(&phc->update_time);
> +     if (!update_time)
> +             return 0;
> +
> +     /* Discard the hardware timestamp if the cached PHC time is too old */
> +     if (ktime_ms_delta(ktime_get_coarse(), update_time) > 2000)
> +             return 0;
> +
> +     cached_phc_time = READ_ONCE(phc->cached_time);

[Severity: High]
Does calling smp_load_acquire() and smp_store_release() (in
ice_ptp_update_cached_phctime()) on phc->update_time break 32-bit builds?

ktime_t is a 64-bit s64 on all architectures, whereas smp_load_acquire()
and smp_store_release() enforce compiletime_assert_atomic_type(), which
requires a native word size up to sizeof(long).  On 32-bit builds with
CONFIG_ICE and CONFIG_PTP_1588_CLOCK enabled, compilation fails with
"Need native word sized stores/loads for atomicity".

In addition, lockless READ_ONCE() and WRITE_ONCE() accesses on 64-bit
phc->cached_time and phc->update_time are not atomic on 32-bit
architectures and can tear when ice_ptp_extend_32b_ts() runs concurrently
with ice_ptp_update_cached_phctime().  The follow-up commit "ice: use
struct timecounter for E825 and E830" keeps these smp_load_acquire() and
smp_store_release() calls on phc->update_time.  Protecting the fields
with a seqcount_t or u64_stats_sync (or storing a word-sized jiffies
timestamp when a 64-bit read isn't needed) would avoid both the build
failure and 64-bit tearing.

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

Reply via email to