From: Vadim Fedorenko <vadim.fedorenko@linux.dev>
To: Miroslav Lichvar <mlichvar@redhat.com>, netdev@vger.kernel.org
Cc: Richard Cochran <richardcochran@gmail.com>,
Thomas Gleixner <tglx@linutronix.de>,
John Stultz <jstultz@google.com>, Arnd Bergmann <arnd@arndb.de>
Subject: Re: [PATCH v2 net-next] ptp: Limit time setting of PTP clocks
Date: Thu, 28 Aug 2025 13:53:17 +0100 [thread overview]
Message-ID: <1f56589b-ffd5-4ed2-afb5-18cf2e7692a6@linux.dev> (raw)
In-Reply-To: <20250828103300.1387025-1-mlichvar@redhat.com>
On 28/08/2025 11:32, Miroslav Lichvar wrote:
> Networking drivers implementing PTP clocks and kernel socket code
> handling hardware timestamps use the 64-bit signed ktime_t type counting
> nanoseconds. When a PTP clock reaches the maximum value in year 2262,
> the timestamps returned to applications will overflow into year 1667.
> The same thing happens when injecting a large offset with
> clock_adjtime(ADJ_SETOFFSET).
>
> The commit 7a8e61f84786 ("timekeeping: Force upper bound for setting
> CLOCK_REALTIME") limited the maximum accepted value setting the system
> clock to 30 years before the maximum representable value (i.e. year
> 2232) to avoid the overflow, assuming the system will not run for more
> than 30 years.
>
> Enforce the same limit for PTP clocks. Don't allow negative values and
> values closer than 30 years to the maximum value. Drivers may implement
> an even lower limit if the hardware registers cannot represent the whole
> interval between years 1970 and 2262 in the required resolution.
>
> Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
> Cc: Richard Cochran <richardcochran@gmail.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: John Stultz <jstultz@google.com>
> Cc: Arnd Bergmann <arnd@arndb.de>
> ---
>
> Notes:
> v2:
> - leave tv_nsec validation separate (Jakub)
>
> drivers/ptp/ptp_clock.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c
> index 1cc06b7cb17e..3e0726c6f55b 100644
> --- a/drivers/ptp/ptp_clock.c
> +++ b/drivers/ptp/ptp_clock.c
> @@ -100,6 +100,9 @@ static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp
> return -EBUSY;
> }
>
> + if (!timespec64_valid_settod(tp))
> + return -EINVAL;
> +
> return ptp->info->settime64(ptp->info, tp);
> }
>
> @@ -130,7 +133,7 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
> ops = ptp->info;
>
> if (tx->modes & ADJ_SETOFFSET) {
> - struct timespec64 ts;
> + struct timespec64 ts, ts2;
> ktime_t kt;
> s64 delta;
>
> @@ -143,6 +146,14 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
> if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC)
> return -EINVAL;
>
> + /* Make sure the offset is valid */
> + err = ptp_clock_gettime(pc, &ts2);
> + if (err)
> + return err;
> + ts2 = timespec64_add(ts2, ts);
> + if (!timespec64_valid_settod(&ts2))
> + return -EINVAL;
> +
> kt = timespec64_to_ktime(ts);
> delta = ktime_to_ns(kt);
> err = ops->adjtime(ops, delta);
Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
next prev parent reply other threads:[~2025-08-28 12:53 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-28 10:32 [PATCH v2 net-next] ptp: Limit time setting of PTP clocks Miroslav Lichvar
2025-08-28 12:53 ` Vadim Fedorenko [this message]
2025-09-01 20:10 ` patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1f56589b-ffd5-4ed2-afb5-18cf2e7692a6@linux.dev \
--to=vadim.fedorenko@linux.dev \
--cc=arnd@arndb.de \
--cc=jstultz@google.com \
--cc=mlichvar@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=richardcochran@gmail.com \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).