]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ptp: Enable auxiliary clocks for PTP_SYS_OFFSET_EXTENDED
authorThomas Gleixner <tglx@linutronix.de>
Tue, 1 Jul 2025 13:27:02 +0000 (15:27 +0200)
committerPaolo Abeni <pabeni@redhat.com>
Thu, 3 Jul 2025 13:36:06 +0000 (15:36 +0200)
Allow ioctl(PTP_SYS_OFFSET_EXTENDED*) to select CLOCK_AUX clock ids for
generating the pre and post hardware readout timestamps.

Aside of adding these clocks to the clock ID validation, this also requires
to check the timestamp to be valid, i.e. the seconds value being greater
than or equal zero. This is necessary because AUX clocks can be
asynchronously enabled or disabled, so there is no way to validate the
availability upfront.

The same could have been achieved by handing the return value of
ktime_get_aux_ts64() all the way down to the IOCTL call site, but that'd
require to modify all existing ptp::gettimex64() callbacks and their inner
call chains. The timestamp check achieves the same with less churn and less
complicated code all over the place.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Link: https://patch.msgid.link/20250701132628.491315452@linutronix.de
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
drivers/ptp/ptp_chardev.c

index 6b611fe5a95ea37b25391770109c43f8909bbf7c..4ca5a464a46a8d66340ec8660d1f532352ca06fa 100644 (file)
@@ -325,13 +325,22 @@ static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg)
        if (IS_ERR(extoff))
                return PTR_ERR(extoff);
 
-       if (extoff->n_samples > PTP_MAX_SAMPLES ||
-           extoff->rsv[0] || extoff->rsv[1] ||
-           (extoff->clockid != CLOCK_REALTIME &&
-            extoff->clockid != CLOCK_MONOTONIC &&
-            extoff->clockid != CLOCK_MONOTONIC_RAW))
+       if (extoff->n_samples > PTP_MAX_SAMPLES || extoff->rsv[0] || extoff->rsv[1])
                return -EINVAL;
 
+       switch (extoff->clockid) {
+       case CLOCK_REALTIME:
+       case CLOCK_MONOTONIC:
+       case CLOCK_MONOTONIC_RAW:
+               break;
+       case CLOCK_AUX ... CLOCK_AUX_LAST:
+               if (IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS))
+                       break;
+               fallthrough;
+       default:
+               return -EINVAL;
+       }
+
        sts.clockid = extoff->clockid;
        for (unsigned int i = 0; i < extoff->n_samples; i++) {
                struct timespec64 ts;
@@ -340,6 +349,11 @@ static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg)
                err = ptp->info->gettimex64(ptp->info, &ts, &sts);
                if (err)
                        return err;
+
+               /* Filter out disabled or unavailable clocks */
+               if (sts.pre_ts.tv_sec < 0 || sts.post_ts.tv_sec < 0)
+                       return -EINVAL;
+
                extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
                extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
                extoff->ts[i][1].sec = ts.tv_sec;