Skip to content

ptp: Add PHC timestamp quality attributes

Arthur Kiyanovski linux-doc linux-kselftest netdev 9 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v7 Latest7 patches 0 repliesNew
  2. v6 posting 2/27 patches 4 repliesSuperseded
  3. v6 posting 1/20 of 7 patches 0 repliesSuperseded
Earlier versions · 6
VersionPostedPatchesRepliesStatus
v57 patches15 repliesSuperseded
v47 patches18 repliesSuperseded
v37 patches15 repliesSuperseded
v2 posting 2/28 patches4 repliesSuperseded
v2 posting 1/20 of 8 patches0 repliesSuperseded
v18 patches0 repliesSuperseded

Changes in v7 from the cover letter

- ptp_vmclock: only set PTP_ATTRS_VALID_ERROR_BOUND when the hypervisor flags the base error valid (VMCLOCK_FLAG_TIME_MAXERROR_VALID); otherwise the bit stays clear so userspace sees "not provided" rather than a UINT_MAX that looks like a real ~4.29 s bound. (Simon Horman, sashiko)
- ptp_vmclock: drop the counter_period_shift >= 128 guard added in v3. counter_period_shift is read from the shared page and passed to the same helper on the main timestamp path in vmclock_get_crosststamp() with no bounds check, so guarding only the new attrs path diverged from the rest of the driver. With the guard gone the attrs path reads the field once, so the divergent double-read the review flagged no longer exists. If the shift should be bounds-checked, that is a fix to the existing driver and better handled on its own. (Simon Horman, sashiko)
- ptp_vmclock: clear att->valid and att->error_bound at the top of vmclock_populate_ptp_attributes(). The function runs once per seq_count retry iteration and only ORs into att->valid, so a discarded torn read could otherwise leave PTP_ATTRS_VALID_ERROR_BOUND set together with a stale error_bound that is not part of the accepted snapshot. (sashiko)
- ptp_vmclock: drop David Woodhouse's Reviewed-by from patch 4 as the patch changed.
- No UAPI changes; patches 1-3 and 5-7 are unchanged from v6.

Patches in v7 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/7ptp: Add ioctls for PHC timestamps with quality attributesdrivers/ptp/ptp_chardev.c drivers/ptp/ptp_clock.c include/linux/ptp_clock_kernel.h +1 more+500 -150 replies
2/7selftests/ptp: Extract print_system_timestamp helper in testptptools/testing/selftests/ptp/testptp.c+38 -380 replies
3/7selftests/ptp: Add testptp support for attributes ioctlstools/testing/selftests/ptp/testptp.c+120 -40 replies
4/7ptp: ptp_vmclock: Implement attributes ioctlsdrivers/ptp/ptp_vmclock.c+183 -160 replies
5/7net: ena: Update PHC admin interface for error bound supportdrivers/net/ethernet/amazon/ena/ena_admin_defs.h drivers/net/ethernet/amazon/ena/ena_com.c+17 -110 replies
6/7net: ena: Add error bound to PHC communication layerDocumentation/networking/device_drivers/ethernet/amazon/ena.rst drivers/net/ethernet/amazon/ena/ena_com.c drivers/net/ethernet/amazon/ena/ena_com.h +2 more+36 -170 replies
7/7net: ena: Implement gettimexattrs64 callback for PTP attributesdrivers/net/ethernet/amazon/ena/ena_phc.c+48 -100 replies

Reviews trailers per patch

4 of 7 patches have a Reviewed-by or Acked-by.

PatchDavid Woodhouse
1/7
2/7R-b
3/7
4/7
5/7R-b
6/7R-b
7/7R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v7 thread

Cover letter [PATCH v7 net-next 0/7] ptp: Add PHC timestamp quality attributes

This series adds quality attributes to PTP Hardware Clock (PHC) timestamps, allowing userspace to obtain error bound, clock status, timescale, and system counter values alongside timestamps in a single call.

Motivation
----------
The existing PTP APIs return timestamps without any indication of their quality. Applications that need clock accuracy and synchronization status commonly rely on external tools such as ptp4l, which implement synchronization logic and can export their measurement of clock accuracy. For managed PHC devices - such as the ENA network adapter, whose clock is synchronized by the device without userspace involvement - these tools are not available, and the existing APIs lack a way to report quality metrics to consumers of time.

This was previously proposed as an RFC [1] with a single ioctl. Based on community feedback, the design was reworked to cover both the extended (multi-sample) and precise (cross-timestamp) paths.

Design
------
The UAPI was redesigned based on Thomas Gleixner's proposal [2]:

- A unified data structure (struct ptp_sys_offset_attrs) is used for both extended and precise ioctls.

- A u32 valid bitmask in struct ptp_clock_attrs indicates which attributes are populated, replacing sentinel values. Drivers set only the bits for attributes they provide.

- System counter values (cycles + counter_id) are carried in struct ptp_sys_time alongside each system timestamp. These are populated by the timekeeping core cross-timestamp infrastructure, which is now merged in net-next [3] - drivers do not fill them. This series therefore applies directly to net-next with no out-of-tree dependency.

- Graceful degradation: the attrs ioctls work even on devices without attrs callbacks, falling back to gettimex64 / getcrosststamp and returning attrs.valid = 0. The capability bits reflect this fallback so cap-respecting userspace does not skip the ioctl on base-only drivers.

A capability flag is added to ptp_clock_caps so userspace can discover attributes support.

Patches 2-3 add testptp support for the new ioctls.

Patch 4 implements the attributes for ptp_vmclock, reporting error bound, clock status, and timescale.

Patches 5-7 implement the attributes for the ENA driver, reporting error bound from the device's PHC layer.

v7:
- ptp_vmclock: only set PTP_ATTRS_VALID_ERROR_BOUND when the hypervisor flags the base error valid (VMCLOCK_FLAG_TIME_MAXERROR_VALID); otherwise the bit stays clear so userspace sees "not provided" rather than a UINT_MAX that looks like a real ~4.29 s bound. (Simon Horman, sashiko)
- ptp_vmclock: drop the counter_period_shift >= 128 guard added in v3. counter_period_shift is read from the shared page and passed to the same helper on the main timestamp path in vmclock_get_crosststamp() with no bounds check, so guarding only the new attrs path diverged from the rest of the driver. With the guard gone the attrs path reads the field once, so the divergent double-read the review flagged no longer exists. If the shift should be bounds-checked, that is a fix to the existing driver and better handled on its own. (Simon Horman, sashiko)
- ptp_vmclock: clear att->valid and att->error_bound at the top of vmclock_populate_ptp_attributes(). The function runs once per seq_count retry iteration and only ORs into att->valid, so a discarded torn read could otherwise leave PTP_ATTRS_VALID_ERROR_BOUND set together with a stale error_bound that is not part of the accepted snapshot. (sashiko)
- ptp_vmclock: drop David Woodhouse's Reviewed-by from patch 4 as the patch changed.
- No UAPI changes; patches 1-3 and 5-7 are unchanged from v6.

v6:
- ptp: reject a non-zero reserved field (request.rsv[]) in the attrs ioctls, matching ptp_sys_offset_extended(). (Jakub Kicinski)
- ptp: zero-initialize the per-sample system-timestamp snapshot in ptp_sys_offset_extended_attrs() so no uninitialized or stale stack data can reach userspace. (Jakub Kicinski)
- ptp: translate the kernel enum clocksource_ids to the UAPI enum ptp_counter_id before returning sys_counter_id; sources that are not a raw hardware counter (e.g. kvmclock) are reported as PTP_COUNTER_UNKNOWN with sys_counter = 0. (Jakub Kicinski, David Woodhouse)
- ptp: initialize the precise cross-timestamp struct and set its clock_id from the validated request; validate clock_id against a precise-path allowlist (CLOCK_REALTIME + CLOCK_AUX) so unsupported clocks return -EINVAL instead of triggering a WARN in get_device_system_crosststamp(). (Jakub Kicinski, David Woodhouse)
- ptp: make the extended_attrs/precise_attrs capability bits reflect the base-callback fallback (gettimexattrs64 || gettimex64, getcrosststampattrs || getcrosststamp) and document the fallback and the att.valid semantics. (Jakub Kicinski)
- ptp: echo the full request header back from the attrs ioctls so valid/clock_id/num_samples/rsv round-trip. (Jakub Kicinski)
- ptp: clarify the FREE_RUNNING vs UNRELIABLE and HOLDOVER status comments and the @error_bound kernel-doc (a maximum bound, not a statistical estimate; not to be trusted when status is UNKNOWN or UNRELIABLE). (Carolina Jubran)
- ptp: document that both @sts and @att may be NULL in the gettimexattrs64 kernel-doc. (Jakub Kicinski)
- selftests/ptp: print the returned system counter value and id (sys_counter, sys_counter_id) in the attrs output.
- Exposing the underlying hw_csid/hw_cycles for derived clocksources (kvmclock, ART, ...) is left to a follow-up; the UAPI leaves room to add them without an ABI change. (David Woodhouse)

v5:
- ptp: simplify ptp_sys_offset_{extended,precise}_attrs() to copy the request header onto the stack and allocate the result buffer once, instead of memdup_user() + kfree() + kzalloc(). (Saeed Bshara)
- ptp: deduplicate the kernel-doc for gettimexattrs64 / getcrosststampattrs by referencing the base gettimex64 / getcrosststamp callbacks. (Saeed Bshara)
- ptp: add missing kernel-doc member descriptions for struct ptp_timestamp and struct ptp_sys_offset_attrs.
- selftests/ptp: map the clockid to a name and print once instead of three duplicated printfs. (Saeed Bshara)
- selftests/ptp: make -a a modifier on -x/-X instead of an exclusive option; drop -A (use -X -a); trim the unknown-clock output; print "not reported" for attributes the device does not report. (Saeed Bshara)
- ptp_vmclock: scope the local 'st' inside the SUPPORT_KVMCLOCK block to fix an unused-variable warning on !CONFIG_KVM_GUEST. (kernel test robot)
- No UAPI changes; the ioctl structures are unchanged from v4.

v4:
- Complete UAPI redesign per Thomas Gleixner's proposal [2]:
unified data structure with u32 valid bitmask, system counter in ptp_sys_time (populated by core, not drivers), graceful degradation for devices without attrs callbacks. (Thomas Gleixner, David Woodhouse)
- Counter values moved from driver attrs callback to timekeeping core infrastructure - drivers no longer set counter_id or counter_value.
- Flexible array member for timestamps[] (kernel bounds the copy, userspace allocates for num_samples requested).
- Drop separate ptp_clock_attributes kernel struct - driver callbacks fill the UAPI ptp_clock_attrs directly.

v3:
- Remove patch 5/8 from v2 (return-code bugfix) - sent separately as [PATCH net] to the net tree.
- Zero-initialize struct ptp_clock_attributes in PTP core ioctl handlers to prevent stack leak of unset fields. (Simon Horman, sashiko)
- ptp_vmclock: validate counter_period_shift < 128 to prevent undefined behavior on untrusted hypervisor input. (sashiko)
- ptp_vmclock: add overflow check on err_hi * NSEC_PER_SEC to prevent silent wraparound producing erroneously small error bound. (sashiko)
- ptp_vmclock: report PTP_TIMESCALE_TAI after tai_adjust() to avoid timescale mismatch. (sashiko)
- ENA: set counter_id = 0, counter_value = 0 in gettimexattrs64 for defense-in-depth. (sashiko)

v2:
- Fix build bisectability: move ena_com.c consumer updates into patch 6/8 and ena_phc.c caller update into patch 7/8 so each patch compiles independently.
- Add missing Cc for Amit Bernstein (co-author of ENA patches).

[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/all/87se7ht25o.ffs@tglx/
[3] https://lore.kernel.org/all/[email protected]/

Arthur Kiyanovski (7):
ptp: Add ioctls for PHC timestamps with quality attributes selftests/ptp: Extract print_system_timestamp helper in testptp selftests/ptp: Add testptp support for attributes ioctls
ptp: ptp_vmclock: Implement attributes ioctls
net: ena: Update PHC admin interface for error bound support
net: ena: Add error bound to PHC communication layer
net: ena: Implement gettimexattrs64 callback for PTP attributes

 .../device_drivers/ethernet/amazon/ena.rst    |   2 +
 .../net/ethernet/amazon/ena/ena_admin_defs.h  |  17 +-
 drivers/net/ethernet/amazon/ena/ena_com.c     |  51 ++--
 drivers/net/ethernet/amazon/ena/ena_com.h     |   5 +-
 drivers/net/ethernet/amazon/ena/ena_debugfs.c |   3 +
 drivers/net/ethernet/amazon/ena/ena_phc.c     |  61 +++-
 drivers/ptp/ptp_chardev.c                     | 208 ++++++++++++-
 drivers/ptp/ptp_clock.c                       |   4 +-
 drivers/ptp/ptp_vmclock.c                     | 199 +++++++++++-
 include/linux/ptp_clock_kernel.h              |  20 ++
 include/uapi/linux/ptp_clock.h                | 283 +++++++++++++++++-
 tools/testing/selftests/ptp/testptp.c         | 198 +++++++++---
 12 files changed, 941 insertions(+), 110 deletions(-)

base-commit: 548b86839f7fb819a4d6c83b71c73ec378d24275

Signature · 1 line
2.47.3