ptp: Provide support for auxiliary clocks for PTP_SYS_OFFSET_EXTENDED
Subsystems (4)
v2 cover ptp: Provide support for auxiliary clocks for PTP_SYS_OFFSET_EXTENDED
This is a follow up to the V1 series, which can be found here:
https://lore.kernel.org/all/[email protected]
to address the merge logistics problem, which I created myself.
Changes vs. V1:
- Make patch 1, which provides the timestamping function temporarily define CLOCK_AUX* if undefined so that it can be merged independently,
- Add a missing check for CONFIG_POSIX_AUX_CLOCK in the PTP IOCTL
- Picked up tags
Merge logistics if agreed on:
1) Patch #1 is applied to the tip tree on top of plain v6.16-rc1 and tagged
2) That tag is merged into tip:timers/ptp and the temporary CLOCK_AUX define is removed in a subsequent commit
3) Network folks merge the tag and apply patches #2 + #3
So the only fallout from this are the extra merges in both trees and the cleanup commit in the tip tree. But that way there are no dependencies and no duplicate commits with different SHAs.
Thoughts?
Due to the above constraints there is no branch offered to pull from right now. Sorry for the inconveniance. Should have thought about that earlier.
Thanks,
tglx
--- drivers/ptp/ptp_chardev.c | 24 +++++++++++++++++++----- include/linux/ptp_clock_kernel.h | 34 ++++------------------------------ include/linux/timekeeping.h | 10 ++++++++++ kernel/time/timekeeping.c | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 35 deletions(-)
PATCH v2 1/3 timekeeping: Provide ktime_get_clock_ts64()
PTP implements an inline switch case for taking timestamps from various POSIX clock IDs, which already consumes quite some text space. Expanding it for auxiliary clocks really becomes too big for inlining.
Provide a out of line version.
The function invalidates the timestamp in case the clock is invalid. The invalidation allows to implement a validation check without the need to propagate a return value through deep existing call chains.
Due to merge logistics this temporarily defines CLOCK_AUX[_LAST] if undefined, so that the plain branch, which does not contain any of the core timekeeper changes, can be pulled into the networking tree as prerequisite for the PTP side changes. These temporary defines are removed after that branch is merged into the tip::timers/ptp branch. That way the result in -next or upstream in the next merge window has zero dependencies.
---
V2: Provide a workaround for networking folks to handle merge logistics.
--- include/linux/timekeeping.h | 10 ++++++++++ kernel/time/timekeeping.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)
include/linux/timekeeping.h +10 -0
@@ -44,6 +44,7 @@ extern void ktime_get_ts64(struct timespextern void ktime_get_real_ts64 (struct timespec64 *tv);extern void ktime_get_coarse_ts64 (struct timespec64 *ts);extern void ktime_get_coarse_real_ts64 (struct timespec64 *ts); +extern void ktime_get_clock_ts64 (clockid_t id,struct timespec64 *ts);/* Multigrain timestamp interfaces */ extern void ktime_get_coarse_real_ts64_mg (struct timespec64 *ts);
@@ -345,4 +346,13 @@ void read_persistent_wall_and_boot_offseextern int update_persistent_clock64 (struct timespec64 now);#endif +/* Temporary workaround to avoid merge dependencies and cross tree messes */ +#ifndef CLOCK_AUX +#define CLOCK_AUX MAX_CLOCKS +#define MAX_AUX_CLOCKS 8 +#define CLOCK_AUX_LAST (CLOCK_AUX + MAX_AUX_CLOCKS - 1) + +static inline bool ktime_get_aux_ts64 (clockid_t id,struct timespec64 *kt) {return false; } +#endif +#endif
kernel/time/timekeeping.c +33 -0
@@ -1573,6 +1573,39 @@ void ktime_get_raw_ts64(struct timespec6 }EXPORT_SYMBOL (ktime_get_raw_ts64); +/** +* ktime_get_clock_ts64 - Returns time of a clock in a timespec +* @id: POSIX clock ID of the clock to read +* @ts: Pointer to the timespec64 to be set +* +* The timestamp is invalidated (@ts->sec is set to -1) if the +* clock @id is not available. +*/ +void ktime_get_clock_ts64 (clockid_t id,struct timespec64 *ts) +{ +/* Invalidate time stamp */ + ts->tv_sec =-1 ; + ts->tv_nsec =0 ; + +switch (id) { +case CLOCK_REALTIME : +ktime_get_real_ts64 (ts); +return ; +case CLOCK_MONOTONIC : +ktime_get_ts64 (ts); +return ; +case CLOCK_MONOTONIC_RAW : +ktime_get_raw_ts64 (ts); +return ; + case CLOCK_AUX ... CLOCK_AUX_LAST: +if (IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS )) +ktime_get_aux_ts64 (id, ts); +return ; +default : +WARN_ON_ONCE (1 ); + } +} +EXPORT_SYMBOL_GPL (ktime_get_clock_ts64);/** * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
[tip: timers/ptp] timekeeping: Provide ktime_get_clock_ts64()
The following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 5b605dbee07dda8fd538af1f07cbf1baf0a49cbc Gitweb: https://git.kernel.org/tip/5b605dbee07dda8fd538af1f07cbf1baf0a49cbc Author: Thomas Gleixner <[email protected]> AuthorDate: Tue, 01 Jul 2025 15:26:58 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Thu, 03 Jul 2025 14:18:06 +02:00
timekeeping: Provide ktime_get_clock_ts64()
PTP implements an inline switch case for taking timestamps from various POSIX clock IDs, which already consumes quite some text space. Expanding it for auxiliary clocks really becomes too big for inlining.
Provide a out of line version.
The function invalidates the timestamp in case the clock is invalid. The invalidation allows to implement a validation check without the need to propagate a return value through deep existing call chains.
Due to merge logistics this temporarily defines CLOCK_AUX[_LAST] if undefined, so that the plain branch, which does not contain any of the core timekeeper changes, can be pulled into the networking tree as prerequisite for the PTP side changes. These temporary defines are removed after that branch is merged into the tip::timers/ptp branch. That way the result in -next or upstream in the next merge window has zero dependencies.
--- include/linux/timekeeping.h | 10 ++++++++++ kernel/time/timekeeping.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)
include/linux/timekeeping.h +10 -0
@@ -44,6 +44,7 @@ extern void ktime_get_ts64(struct timespec64 *ts);extern void ktime_get_real_ts64 (struct timespec64 *tv);extern void ktime_get_coarse_ts64 (struct timespec64 *ts);extern void ktime_get_coarse_real_ts64 (struct timespec64 *ts); +extern void ktime_get_clock_ts64 (clockid_t id,struct timespec64 *ts);/* Multigrain timestamp interfaces */ extern void ktime_get_coarse_real_ts64_mg (struct timespec64 *ts);
@@ -345,4 +346,13 @@ void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,extern int update_persistent_clock64 (struct timespec64 now);#endif +/* Temporary workaround to avoid merge dependencies and cross tree messes */ +#ifndef CLOCK_AUX +#define CLOCK_AUX MAX_CLOCKS +#define MAX_AUX_CLOCKS 8 +#define CLOCK_AUX_LAST (CLOCK_AUX + MAX_AUX_CLOCKS - 1) + +static inline bool ktime_get_aux_ts64 (clockid_t id,struct timespec64 *kt) {return false; } +#endif +#endif
kernel/time/timekeeping.c +33 -0
@@ -1573,6 +1573,39 @@ void ktime_get_raw_ts64(struct timespec64 *ts) }EXPORT_SYMBOL (ktime_get_raw_ts64); +/** +* ktime_get_clock_ts64 - Returns time of a clock in a timespec +* @id: POSIX clock ID of the clock to read +* @ts: Pointer to the timespec64 to be set +* +* The timestamp is invalidated (@ts->sec is set to -1) if the +* clock @id is not available. +*/ +void ktime_get_clock_ts64 (clockid_t id,struct timespec64 *ts) +{ +/* Invalidate time stamp */ + ts->tv_sec =-1 ; + ts->tv_nsec =0 ; + +switch (id) { +case CLOCK_REALTIME : +ktime_get_real_ts64 (ts); +return ; +case CLOCK_MONOTONIC : +ktime_get_ts64 (ts); +return ; +case CLOCK_MONOTONIC_RAW : +ktime_get_raw_ts64 (ts); +return ; + case CLOCK_AUX ... CLOCK_AUX_LAST: +if (IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS )) +ktime_get_aux_ts64 (id, ts); +return ; +default : +WARN_ON_ONCE (1 ); + } +} +EXPORT_SYMBOL_GPL (ktime_get_clock_ts64);/** * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
PATCH v2 2/3 ptp: Use ktime_get_clock_ts64() for timestamping
In reply to Thomas GleixnerThe inlined ptp_read_system_[pre|post]ts() switch cases expand to a copious amount of text in drivers, e.g. ~500 bytes in e1000e. Adding auxiliary clock support to the inlines would increase it further.
Replace the inline switch case with a call to ktime_get_clock_ts64(), which reduces the code size in drivers and allows to access auxiliary clocks once they are enabled in the IOCTL parameter filter.
No functional change.
--- include/linux/ptp_clock_kernel.h | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-)
include/linux/ptp_clock_kernel.h +4 -30
@@ -477,40 +477,14 @@ static inline ktime_t ptp_convert_timeststatic inline void ptp_read_system_prets (struct ptp_system_timestamp *sts) { -if (sts) { -switch (sts->clockid) { -case CLOCK_REALTIME : -ktime_get_real_ts64 (&sts->pre_ts); -break ; -case CLOCK_MONOTONIC : -ktime_get_ts64 (&sts->pre_ts); -break ; -case CLOCK_MONOTONIC_RAW : -ktime_get_raw_ts64 (&sts->pre_ts); -break ; -default : -break ; - } - } +if (sts) +ktime_get_clock_ts64 (sts->clockid, &sts->pre_ts); }static inline void ptp_read_system_postts (struct ptp_system_timestamp *sts) { -if (sts) { -switch (sts->clockid) { -case CLOCK_REALTIME : -ktime_get_real_ts64 (&sts->post_ts); -break ; -case CLOCK_MONOTONIC : -ktime_get_ts64 (&sts->post_ts); -break ; -case CLOCK_MONOTONIC_RAW : -ktime_get_raw_ts64 (&sts->post_ts); -break ; -default : -break ; - } - } +if (sts) +ktime_get_clock_ts64 (sts->clockid, &sts->post_ts); }#endif
PATCH v2 3/3 ptp: Enable auxiliary clocks for PTP_SYS_OFFSET_EXTENDED
In reply to Thomas GleixnerAllow 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.
---
V2: Catch CONFIG_POSIX_AUX_CLOCKS=n right at the clockid check
--- drivers/ptp/ptp_chardev.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
drivers/ptp/ptp_chardev.c +19 -5
@@ -325,13 +325,22 @@ static long ptp_sys_offset_extended(struif (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(stru 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;
Paolo Abeni
maintainerQuoted 23 lines · Thomas Gleixner
This is a follow up to the V1 series, which can be found here:
https://lore.kernel.org/all/[email protected]to address the merge logistics problem, which I created myself.
Changes vs. V1:
- Make patch 1, which provides the timestamping function temporarily define CLOCK_AUX* if undefined so that it can be merged independently,
- Add a missing check for
CONFIG_POSIX_AUX_CLOCKin the PTP IOCTL- Picked up tags
Merge logistics if agreed on:
1) Patch #1 is applied to the tip tree on top of plain v6.16-rc1 and tagged
2) That tag is merged into tip:timers/ptp and the temporary
CLOCK_AUXdefine is removed in a subsequent commit
Quoted 7 lines
3) Network folks merge the tag and apply patches #2 + #3
So the only fallout from this are the extra merges in both trees and the cleanup commit in the tip tree. But that way there are no dependencies and no duplicate commits with different SHAs.
Thoughts?
I'm sorry for the latency here; the plan works for me! I'll wait for the tag reference.
Could you please drop a notice here when such tag will be available?
Thanks,
Paolo
Paolo!
Quoted 11 lines · Paolo Abeni
Quoted 10 lines · Thomas Gleixner
Merge logistics if agreed on:
1) Patch #1 is applied to the tip tree on top of plain v6.16-rc1 and tagged
2) That tag is merged into tip:timers/ptp and the temporary
CLOCK_AUXdefine is removed in a subsequent commit3) Network folks merge the tag and apply patches #2 + #3
Quoted 8 lines
Quoted 5 lines
So the only fallout from this are the extra merges in both trees and the cleanup commit in the tip tree. But that way there are no dependencies and no duplicate commits with different SHAs.
Thoughts?
I'm sorry for the latency here; the plan works for me! I'll wait for the tag reference.
No problem. Rome wasn't built in a day either :)
Quoted 1 line
Could you please drop a notice here when such tag will be available?
Here you go:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git ktime-get-clock-ts64-for-ptp
I merged it locally into net-next, applied the PTP patches on top and verified that the combination with the tip timers/ptp branch, which has the tag integrated and the workaround removed, creates the expected working result.
Thanks,
tglx
Paolo Abeni
maintainerQuoted 25 lines · Thomas Gleixner
Quoted 11 lines · Paolo Abeni
Quoted 10 lines · Thomas Gleixner
Merge logistics if agreed on:
1) Patch #1 is applied to the tip tree on top of plain v6.16-rc1 and tagged
2) That tag is merged into tip:timers/ptp and the temporary
CLOCK_AUXdefine is removed in a subsequent commit3) Network folks merge the tag and apply patches #2 + #3
Quoted 8 lines
Quoted 5 lines
So the only fallout from this are the extra merges in both trees and the cleanup commit in the tip tree. But that way there are no dependencies and no duplicate commits with different SHAs.
Thoughts?
I'm sorry for the latency here; the plan works for me! I'll wait for the tag reference.
No problem. Rome wasn't built in a day either :)
Quoted 1 line
Could you please drop a notice here when such tag will be available?
Quoted 8 lines
Here you go:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitktime-get-clock-ts64-for-ptpI merged it locally into net-next, applied the PTP patches on top and verified that the combination with the tip timers/ptp branch, which has the tag integrated and the workaround removed, creates the expected working result.
I had to wrestle a bit with the script I use - since the whole thing was a little different from my usual workflow - but it's in now, hoping I did not mess badly with something.
/P
Quoted 5 lines · Paolo Abeni
Quoted 4 lines · Thomas Gleixner
Here you go:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitktime-get-clock-ts64-for-ptp
Quoted 8 lines
Quoted 4 lines
I merged it locally into net-next, applied the PTP patches on top and verified that the combination with the tip timers/ptp branch, which has the tag integrated and the workaround removed, creates the expected working result.
I had to wrestle a bit with the script I use - since the whole thing was a little different from my usual workflow - but it's in now, hoping I did not mess badly with something.
Looks perfect!
Thank you for the extra mile!
tglx
[tip: timers/ptp] timekeeping: Remove the temporary CLOCK_AUX workaround
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 8959338617a85e35820e3a7fa21801cf55b068bf Gitweb: https://git.kernel.org/tip/8959338617a85e35820e3a7fa21801cf55b068bf Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 03 Jul 2025 14:39:28 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Thu, 03 Jul 2025 14:44:15 +02:00
timekeeping: Remove the temporary CLOCK_AUX workaround
ktime_get_clock_ts64() was provided for the networking tree as a stand alone commit based on v6.16-rc1. It contains a temporary workaround for the CLOCK_AUX* defines, which are only available in the timekeeping tree.
As this commit is now merged into the timers/ptp branch, which contains the real CLOCK_AUX* defines, the workaround is obsolete.
Remove it.
--- include/linux/timekeeping.h | 9 --------- 1 file changed, 9 deletions(-)
include/linux/timekeeping.h +0 -9
@@ -357,13 +357,4 @@ void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,extern int update_persistent_clock64 (struct timespec64 now);#endif -/* Temporary workaround to avoid merge dependencies and cross tree messes */ -#ifndef CLOCK_AUX -#define CLOCK_AUX MAX_CLOCKS -#define MAX_AUX_CLOCKS 8 -#define CLOCK_AUX_LAST (CLOCK_AUX + MAX_AUX_CLOCKS - 1) - -static inline bool ktime_get_aux_ts64 (clockid_t id,struct timespec64 *kt) {return false; } -#endif -#endif
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 9 lines · On Tue, 1 Jul 2025 15:26:56 +0200 (CEST) you wrote:
This is a follow up to the V1 series, which can be found here:
https://lore.kernel.org/all/[email protected]to address the merge logistics problem, which I created myself.
Changes vs. V1:
[...]
Here is the summary with links:
- [V2,1/3] timekeeping: Provide ktime_get_clock_ts64()
https://git.kernel.org/netdev/net-next/c/5b605dbee07d - [V2,2/3] ptp: Use ktime_get_clock_ts64() for timestamping https://git.kernel.org/netdev/net-next/c/4c09a4cebd03 - [V2,3/3] ptp: Enable auxiliary clocks for PTP_SYS_OFFSET_EXTENDED https://git.kernel.org/netdev/net-next/c/17c395bba1a3
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html