Skip to contentSkip to conversation

ptp: make virtual clock sampling and teardown failure-safe

PATCH Superseded James Hilliard 3 participants 10 messages Oct 13 – Oct 15

Subsystems (4)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover ptp: make virtual clock sampling and teardown failure-safe

Fix virtual-clock sampling errors, registration rollback and parent teardown in separate patches. Keep failed PHC reads out of the timecounter and serialize extended samples with virtual-clock adjustments. Also preserve the zero sentinel for packets without hardware timestamps.

Unlike v2, this revision retains the existing cyclecounter mask, scaling and conversion windows. It does not assume full-width nanosecond samples or promise recovery of elapsed time after an unobserved counter wrap.

Changes in v3:
- Split the independent fixes into separate patches, as requested by Paolo.
- Retain the existing timecounter arithmetic and conversion windows; drop
the full-width rewrite and extended-read anchor updates from v2.
- Propagate registration errors through sysfs, including a failed initial
PHC sample, and initialize the counter before publishing the clock.
- Preserve absent hardware timestamps instead of converting zero.
- Correct the sysfs teardown rationale and its Fixes tag.
- Rebase onto current net.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Drain virtual clock sysfs changes before parent unregister walks children.
- Use full-width samples and overflow-safe scaling across long sampling gaps.
- Update the anchor for extended and cross-timestamp reads under the clock lock.
- Link to v1: https://patch.msgid.link/[email protected]

Assisted-by Codex:gpt-6-astra

---
James Hilliard (5):
ptp: drain virtual clock sysfs operations before unregistering children
ptp: unwind partially completed virtual clock creation
ptp: vclock: propagate physical clock sampling errors
ptp: vclock: serialize extended samples with counter updates
ptp: vclock: preserve missing hardware timestamps

 drivers/ptp/ptp_clock.c   |   5 +++
 drivers/ptp/ptp_private.h |   4 +-
 drivers/ptp/ptp_sysfs.c   |  18 ++++++++-
 drivers/ptp/ptp_vclock.c  | 100 +++++++++++++++++++++++++++++-----------------
 4 files changed, 87 insertions(+), 40 deletions(-)

---
base-commit: 513444cc62e231a11c9b15ea88de0890da9b2b4f
change-id: 20260930-ptp-vclock-sampling-902f98408c93

Best regards,

Signature · 1 line
James Hilliard <[email protected]>
#1 lore raw

PATCH v3 1/5 ptp: drain virtual clock sysfs operations before unregistering children

Parent clock removal walks its virtual clocks before removing the n_vclocks sysfs attribute. The mutex taken by ptp_vclock_in_use() is released before that walk, so a concurrent sysfs deletion can pick the same child and unregister and free its ptp_vclock a second time. A reference held by the device iterator does not protect that separately allocated virtual clock.

Remove n_vclocks before walking the children. Removing the attribute prevents new stores and drains stores already running, without holding n_vclocks_mux across a callback that needs that mutex. Skip virtual clocks, which do not have this attribute.

This race was found by code inspection of virtual-clock registration failure cleanup and parent removal.

Fixes bfcbb76b0f59 ("ptp: unregister virtual clocks when unregistering physical clock.")
---
 drivers/ptp/ptp_clock.c   | 5 +++++
 drivers/ptp/ptp_private.h | 1 +
 drivers/ptp/ptp_sysfs.c   | 6 ++++++
 3 files changed, 12 insertions(+)
drivers/ptp/ptp_clock.c +5 -0
index 4111342d64f0..47ffc773065a 100644
@@ -508,6 +508,11 @@ static int unregister_vclock(struct device *dev, void *data)
 
 int ptp_clock_unregister(struct ptp_clock *ptp)
 {
+	/*
+	 * Stop and drain virtual-clock creation and deletion before walking the
+	 * children. Do not hold n_vclocks_mux while waiting for sysfs callbacks.
+	 */
+	ptp_vclock_remove_sysfs(ptp);
 	if (ptp_vclock_in_use(ptp)) {
 		device_for_each_child(&ptp->dev, NULL, unregister_vclock);
 	}
drivers/ptp/ptp_private.h +1 -0
index db4039d642b4..ec8633126d6b 100644
@@ -169,6 +169,7 @@ extern const struct attribute_group *ptp_groups[];
 
 int ptp_populate_pin_groups(struct ptp_clock *ptp);
 void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
+void ptp_vclock_remove_sysfs(struct ptp_clock *ptp);
 
 struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock);
 void ptp_vclock_unregister(struct ptp_vclock *vclock);
drivers/ptp/ptp_sysfs.c +6 -0
index dc398c6b7528..53388b123198 100644
@@ -263,6 +263,12 @@ static ssize_t n_vclocks_store(struct device *dev,
 }
 static DEVICE_ATTR_RW(n_vclocks);
 
+void ptp_vclock_remove_sysfs(struct ptp_clock *ptp)
+{
+	if (!ptp->is_virtual_clock)
+		device_remove_file(&ptp->dev, &dev_attr_n_vclocks);
+}
+
 static ssize_t max_vclocks_show(struct device *dev,
 				struct device_attribute *attr, char *page)
 {
Signature · 1 line
2.53.0
Reviewed-by on v3 1/5
#2 lore raw

Re: [PATCH net v3 1/5] ptp: drain virtual clock sysfs operations before unregistering children

Quoted 59 lines · James Hilliard

Parent clock removal walks its virtual clocks before removing the n_vclocks sysfs attribute. The mutex taken by ptp_vclock_in_use() is released before that walk, so a concurrent sysfs deletion can pick the same child and unregister and free its ptp_vclock a second time. A reference held by the device iterator does not protect that separately allocated virtual clock.

Remove n_vclocks before walking the children. Removing the attribute prevents new stores and drains stores already running, without holding n_vclocks_mux across a callback that needs that mutex. Skip virtual clocks, which do not have this attribute.

This race was found by code inspection of virtual-clock registration failure cleanup and parent removal.

Fixes bfcbb76b0f59 ("ptp: unregister virtual clocks when unregistering physical clock.")
---
  drivers/ptp/ptp_clock.c   | 5 +++++
  drivers/ptp/ptp_private.h | 1 +
  drivers/ptp/ptp_sysfs.c   | 6 ++++++
  3 files changed, 12 insertions(+)
drivers/ptp/ptp_clock.c +5 -0
index 4111342d64f0..47ffc773065a 100644
@@ -508,6 +508,11 @@ static int unregister_vclock(struct device *dev, void *data)
 
  int ptp_clock_unregister(struct ptp_clock *ptp)
  {
+	/*
+	 * Stop and drain virtual-clock creation and deletion before walking the
+	 * children. Do not hold n_vclocks_mux while waiting for sysfs callbacks.
+	 */
+	ptp_vclock_remove_sysfs(ptp);
  	if (ptp_vclock_in_use(ptp)) {
  		device_for_each_child(&ptp->dev, NULL, unregister_vclock);
  	}
drivers/ptp/ptp_private.h +1 -0
index db4039d642b4..ec8633126d6b 100644
@@ -169,6 +169,7 @@ extern const struct attribute_group *ptp_groups[];
 
  int ptp_populate_pin_groups(struct ptp_clock *ptp);
  void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
+void ptp_vclock_remove_sysfs(struct ptp_clock *ptp);
 
  struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock);
  void ptp_vclock_unregister(struct ptp_vclock *vclock);
drivers/ptp/ptp_sysfs.c +0 -0
index dc398c6b7528..53388b123198 100644
@@ -263,6 +263,12 @@ static ssize_t n_vclocks_store(struct device *dev,
  }
  static DEVICE_ATTR_RW(n_vclocks);

drivers/ptp/ptp_sysfs.c static ssize_t n_vclocks_store(struct device *dev, · line 274

Quoted 10 lines
drivers/ptp/ptp_sysfs.c +6 -0
 
+void ptp_vclock_remove_sysfs(struct ptp_clock *ptp)
+{
+	if (!ptp->is_virtual_clock)
+		device_remove_file(&ptp->dev, &dev_attr_n_vclocks);
+}
+
  static ssize_t max_vclocks_show(struct device *dev,
  				struct device_attribute *attr, char *page)
  {
#3 lore raw

PATCH v3 2/5 ptp: unwind partially completed virtual clock creation

In reply to James Hilliard

n_vclocks_store() publishes the new clock count only after every child has been registered. If a later registration fails, it returns without unregistering the children already created by that request. Those clocks are left out of n_vclocks, and a subsequent request can overwrite their index entries without removing them.

Unregister the newly created children in reverse order and clear their index entries on failure. Leave the previously installed clocks and their count unchanged. The n_vclocks mutex serializes this rollback with other sysfs changes, and parent removal drains the store before walking the children.

This was found by code inspection while adding initial PHC sample error handling to virtual clock registration.

Fixes 73f37068d540 ("ptp: support ptp physical/virtual clocks conversion")
---
 drivers/ptp/ptp_sysfs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/ptp/ptp_sysfs.c +7 -1
index 53388b123198..fabc1c9b1752 100644
@@ -225,7 +225,7 @@ static ssize_t n_vclocks_store(struct device *dev,
 		for (i = 0; i < num - ptp->n_vclocks; i++) {
 			vclock = ptp_vclock_register(ptp);
 			if (!vclock)
-				goto out;
+				goto err_register;
 
 			*(ptp->vclock_index + ptp->n_vclocks + i) =
 				vclock->clock->index;
@@ -257,6 +257,12 @@ static ssize_t n_vclocks_store(struct device *dev,
 	mutex_unlock(&ptp->n_vclocks_mux);
 
 	return count;
+err_register:
+	num = i;
+	if (num)
+		device_for_each_child_reverse(dev, &num, unregister_vclock);
+	for (num = 0; num < i; num++)
+		ptp->vclock_index[ptp->n_vclocks + num] = -1;
 out:
 	mutex_unlock(&ptp->n_vclocks_mux);
 	return err;
Signature · 1 line
2.53.0
#4 lore raw

PATCH v3 3/5 ptp: vclock: propagate physical clock sampling errors

In reply to James Hilliard

The cyclecounter read callback cannot report errors. ptp_vclock_read() ignores the return from the physical clock and consumes its output even if the read failed. A zero sample followed by the real counter can add an extra 32-bit wrap to virtual time. This was found by code inspection of reset-time PHC access and the virtual clock callers.

Sample the parent before calling timecounter_read() or timecounter_init() and have the cyclecounter callback return the cached successful sample. On failure, leave the counter and frequency adjustment unchanged and return the PHC error to the caller. The refresh worker can retry later without consuming an invalid sample.

Take the initial sample and initialize the counter before publishing the clock. Return an error pointer from registration and propagate its errno through n_vclocks, including when the initial sample fails.

Keep the existing cyclecounter mask, scaling and refresh interval. This does not recover elapsed time if sampling is unavailable long enough to miss a counter wrap, nor does it assume that getcycles64() exposes a full-width nanosecond counter.

Fixes 5d43f951b1ac ("ptp: add ptp virtual clock driver framework")
---
 drivers/ptp/ptp_private.h |  3 ++-
 drivers/ptp/ptp_sysfs.c   |  4 ++-
 drivers/ptp/ptp_vclock.c  | 66 +++++++++++++++++++++++++++++++++--------------
 3 files changed, 52 insertions(+), 21 deletions(-)
drivers/ptp/ptp_private.h +2 -1
index ec8633126d6b..e389e0653afd 100644
@@ -81,7 +81,8 @@ struct ptp_vclock {
 	struct hlist_node vclock_hash_node;
 	struct cyclecounter cc;
 	struct timecounter tc;
-	struct mutex lock;	/* protects tc/cc */
+	u64 cycles;
+	struct mutex lock;	/* protects tc/cc and the cached sample */
 };
 
 /*
drivers/ptp/ptp_sysfs.c +3 -1
index fabc1c9b1752..026220b112c2 100644
@@ -224,8 +224,10 @@ static ssize_t n_vclocks_store(struct device *dev,
 	if (num > ptp->n_vclocks) {
 		for (i = 0; i < num - ptp->n_vclocks; i++) {
 			vclock = ptp_vclock_register(ptp);
-			if (!vclock)
+			if (IS_ERR(vclock)) {
+				err = PTR_ERR(vclock);
 				goto err_register;
+			}
 
 			*(ptp->vclock_index + ptp->n_vclocks + i) =
 				vclock->clock->index;
drivers/ptp/ptp_vclock.c +47 -19

138 lines not on this page · raw message

Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH net v3 3/5] ptp: vclock: propagate physical clock sampling errors

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] After one failed PHC sample, the virtual clock's anchor (tc->cycle_last) goes stale for a full extra refresh period, so the…
- [Low] ptp_vclock_register() now takes the initial PHC sample and calls timecounter_init() (drivers/ptp/ptp_vclock.c:237-240) before…

--

ptp: vclock: propagate physical clock sampling errors

This patch samples the parent PHC before timecounter_read() or timecounter_init(). The cyclecounter read callback now returns the cached successful sample, so a failed read no longer feeds a zero into the timecounter. Sampling errors are returned to the vclock callers, and ptp_vclock_register() takes the initial sample before publishing the clock and returns an ERR_PTR that n_vclocks_store() propagates.

Quoted 4 lines
drivers/ptp/ptp_vclock.c +0 -0
index 84cb527f59ccc..eea207e5a8892 100644

[ ... ]

Quoted 9 lines
drivers/ptp/ptp_vclock.c +1 -1
@@ -75,15 +93,16 @@ static int ptp_vclock_gettime(struct ptp_clock_info *ptp,
 			      struct timespec64 *ts)
 {
 	struct ptp_vclock *vclock = info_to_vclock(ptp);
-	u64 ns;
+	int err;
 
 	if (mutex_lock_interruptible(&vclock->lock))
 		return -EINTR;

drivers/ptp/ptp_vclock.c static int ptp_vclock_gettime(struct ptp_clock_info *ptp, · line 106

Quoted 10 lines
drivers/ptp/ptp_vclock.c +4 -3
-	ns = timecounter_read(&vclock->tc);
+	err = ptp_vclock_sample(vclock);
+	if (!err)
+		*ts = ns_to_timespec64(timecounter_read(&vclock->tc));
 	mutex_unlock(&vclock->lock);
-	*ts = ns_to_timespec64(ns);
 
-	return 0;
+	return err;
 }

[Severity: Medium]
The commit message says "The refresh worker can retry later without consuming an invalid sample". Does the retry come soon enough?

ptp_vclock_refresh() calls this function, ignores its return value, and always reschedules after the full interval:

drivers/ptp/ptp_vclock.c:ptp_vclock_refresh() {
	...
	ptp_vclock_gettime(&vclock->info, &ts);

	return PTP_VCLOCK_REFRESH_INTERVAL;
}

PTP_VCLOCK_REFRESH_INTERVAL is HZ * 2. After one failed sample here, tc->cycle_last is about 4s old by the time of the next retry.

The cyclecounter is CYCLECOUNTER_MASK(32) of nanoseconds, which is about 4.295s. timecounter_cyc2time() treats any delta above mask / 2 (about 2.147s) as a timestamp in the past:

include/linux/timecounter.h:timecounter_cyc2time() {
	...
	if (unlikely(delta > cc->mask / 2)) {
		delta = (tc->cycle_last - cycle_tstamp) & cc->mask;
		nsec -= cc_cyc2ns_backwards(cc, delta, frac);
	...
}

Between about 2.147s and 4s after the last good sample, would ptp_convert_timestamp(), ptp_vclock_gettimex() and ptp_vclock_getcrosststamp() all return times about 2^32 ns too early?

ptp_convert_timestamp() handles every hardware timestamp on a socket bound to the vclock. ptp_vclock_gettimex() is the only gettime path when the parent has getcyclesx64.

If the next refresh also fails, the next successful timecounter_read() sees a delta above 2^32 ns, and that delta gets masked. Can virtual time then permanently lose a full 2^32 ns wrap?

For that to happen, the parent only has to be unavailable at two consecutive refresh points about 2s apart. That is shorter than "long enough to miss a counter wrap", as the commit message describes it.

Could ptp_vclock_refresh() return a short delay after a failed sample? ptp_aux_kworker() requeues the work with any non-negative delay that do_aux_work returns.

[ ... ]

Quoted 12 lines
drivers/ptp/ptp_vclock.c +5 -2
@@ -214,20 +234,28 @@ struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock)
 
 	mutex_init(&vclock->lock);
 
+	err = ptp_vclock_sample(vclock);
+	if (err)
+		goto free_vclock;
+	timecounter_init(&vclock->tc, &vclock->cc, 0);
+
 	vclock->clock = ptp_clock_register(&vclock->info, &pclock->dev);
-	if (IS_ERR_OR_NULL(vclock->clock)) {
-		kfree(vclock);

drivers/ptp/ptp_vclock.c struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock) · line 250

Quoted 10 lines
drivers/ptp/ptp_vclock.c +3 -2
-		return NULL;
+	if (IS_ERR(vclock->clock)) {
+		err = PTR_ERR(vclock->clock);
+		goto free_vclock;
 	}
 
 	ptp_vclock_set_subclass(vclock->clock);
 
-	timecounter_init(&vclock->tc, &vclock->cc, 0);
 	ptp_schedule_worker(vclock->clock, PTP_VCLOCK_REFRESH_INTERVAL);

[Severity: Low]
The initial sample and timecounter_init() now run before ptp_clock_register(), but the first refresh is still queued only after registration returns. Does this make the gap between tc->cycle_last and the first refresh longer by however long ptp_clock_register() takes?

ptp_clock_register() can sleep in GFP_KERNEL allocations, in kthread_run_worker() and in posix_clock_register().

Suppose that time plus kworker latency is more than about 147ms. Then conversions through ptp_convert_timestamp() or ptp_vclock_gettimex() just before the first refresh would fall past the 2.147s half-window and come out about 2^32 ns early. If it is more than about 2.29s, the first timecounter_read() would lose a full wrap.

The reorder does fix the old race, where the posix clock was exposed before timecounter_init() while tc.cc was still NULL. Either of these would keep that fix without the longer gap:

- re-sample and re-initialise the timecounter under vclock->lock once ptp_clock_register() succeeds
- queue the first refresh with a short or zero delay

Quoted 9 lines
drivers/ptp/ptp_vclock.c +4 -0
 	ptp_vclock_hash_add(vclock);
 
 	return vclock;
+
+free_vclock:
+	kfree(vclock);
+	return ERR_PTR(err);
 }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261013-ptp-vclock-sampling-v3-0-d3ee4ea62fc6%40gmail.com
#6 lore raw

PATCH v3 4/5 ptp: vclock: serialize extended samples with counter updates

In reply to James Hilliard

The extended and cross-timestamp callbacks sample the physical clock before taking the virtual clock mutex. A concurrent frequency adjustment can advance the timecounter and change its multiplier in between, so the older sample is converted using a different frequency than the one in effect when it was taken.

Take the mutex before sampling and keep it held through the conversion. Retain timecounter_cyc2time() and its existing conversion window. A PHC sample need not advance the counter: for example, e1000e explicitly allows non-monotonic SYSTIM readings. Treating a slightly older sample as a forward delta would instead advance virtual time by nearly a full counter wrap.

This race was found by code inspection of the virtual clock sampling paths.

Fixes f0067ebfc42b ("ptp: add gettimex64() to virtual clocks.")
Fixes 21fad63084c2 ("ptp: add getcrosststamp() to virtual clocks.")
---
 drivers/ptp/ptp_vclock.c | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)
drivers/ptp/ptp_vclock.c +14 -18
index eea207e5a889..6675466bc307 100644
@@ -113,20 +113,18 @@ static int ptp_vclock_gettimex(struct ptp_clock_info *ptp,
 	struct ptp_clock *pptp = vclock->pclock;
 	struct timespec64 pts;
 	int err;
-	u64 ns;
-
-	err = pptp->info->getcyclesx64(pptp->info, &pts, sts);
-	if (err)
-		return err;
 
 	if (mutex_lock_interruptible(&vclock->lock))
 		return -EINTR;
-	ns = timecounter_cyc2time(&vclock->tc, timespec64_to_ns(&pts));
-	mutex_unlock(&vclock->lock);
+	err = pptp->info->getcyclesx64(pptp->info, &pts, sts);
+	if (!err) {
+		u64 ns = timecounter_cyc2time(&vclock->tc, timespec64_to_ns(&pts));
 
-	*ts = ns_to_timespec64(ns);
+		*ts = ns_to_timespec64(ns);
+	}
+	mutex_unlock(&vclock->lock);
 
-	return 0;
+	return err;
 }
 
 static int ptp_vclock_settime(struct ptp_clock_info *ptp,
@@ -152,20 +150,18 @@ static int ptp_vclock_getcrosststamp(struct ptp_clock_info *ptp,
 	struct ptp_vclock *vclock = info_to_vclock(ptp);
 	struct ptp_clock *pptp = vclock->pclock;
 	int err;
-	u64 ns;
-
-	err = pptp->info->getcrosscycles(pptp->info, xtstamp);
-	if (err)
-		return err;
 
 	if (mutex_lock_interruptible(&vclock->lock))
 		return -EINTR;
-	ns = timecounter_cyc2time(&vclock->tc, ktime_to_ns(xtstamp->device));
-	mutex_unlock(&vclock->lock);
+	err = pptp->info->getcrosscycles(pptp->info, xtstamp);
+	if (!err) {
+		u64 ns = timecounter_cyc2time(&vclock->tc, ktime_to_ns(xtstamp->device));
 
-	xtstamp->device = ns_to_ktime(ns);
+		xtstamp->device = ns_to_ktime(ns);
+	}
+	mutex_unlock(&vclock->lock);
 
-	return 0;
+	return err;
 }
 
 static long ptp_vclock_refresh(struct ptp_clock_info *ptp)
Signature · 1 line
2.53.0
#7 lore raw

PATCH v3 5/5 ptp: vclock: preserve missing hardware timestamps

In reply to James Hilliard

Socket timestamp delivery converts hardware timestamps to a bound virtual clock before checking whether a hardware timestamp is present. A zero timestamp means that the packet has no hardware timestamp, but timecounter_cyc2time() can turn that zero into a nonzero virtual time. The socket then reports a hardware timestamp which was never supplied.

Return zero without conversion when the input timestamp is absent. This also covers skb_get_tx_timestamp(), which checks for an absent timestamp after the same conversion.

This was identified during review of virtual clock sampling changes.

Fixes d7c088265588 ("net: socket: support hardware timestamp conversion to PHC bound")
---
 drivers/ptp/ptp_vclock.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/ptp/ptp_vclock.c +2 -0
index 6675466bc307..4a62047f0ce0 100644
@@ -307,6 +307,8 @@ ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index)
 	u64 ns;
 
 	ns = ktime_to_ns(*hwtstamp);
+	if (!ns)
+		return 0;
 
 	srcu_idx = srcu_read_lock(&vclock_srcu);
 
Signature · 1 line
2.53.0
Reviewed-by on v3 5/5
#8 lore raw

Re: [PATCH net v3 5/5] ptp: vclock: preserve missing hardware timestamps

Quoted 24 lines · James Hilliard

Socket timestamp delivery converts hardware timestamps to a bound virtual clock before checking whether a hardware timestamp is present. A zero timestamp means that the packet has no hardware timestamp, but timecounter_cyc2time() can turn that zero into a nonzero virtual time. The socket then reports a hardware timestamp which was never supplied.

Return zero without conversion when the input timestamp is absent. This also covers skb_get_tx_timestamp(), which checks for an absent timestamp after the same conversion.

This was identified during review of virtual clock sampling changes.

Fixes d7c088265588 ("net: socket: support hardware timestamp conversion to PHC bound")
---
  drivers/ptp/ptp_vclock.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/ptp/ptp_vclock.c +0 -0
index 6675466bc307..4a62047f0ce0 100644

drivers/ptp/ptp_vclock.c ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index) · line 314

Quoted 8 lines
drivers/ptp/ptp_vclock.c +2 -0
@@ -307,6 +307,8 @@ ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index)
  	u64 ns;
 
  	ns = ktime_to_ns(*hwtstamp);
+	if (!ns)
+		return 0;
 
  	srcu_idx = srcu_read_lock(&vclock_srcu);
 
#9 lore raw

Re: [PATCH net v3 5/5] ptp: vclock: preserve missing hardware timestamps

In reply to James Hilliard
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The patch adds a new early return to ptp_convert_timestamp() in drivers/ptp/ptp_vclock.c: when the input hardware timestamp is zero…

--

ptp: vclock: preserve missing hardware timestamps

Socket timestamp delivery converts hardware timestamps to a vclock time before it checks whether a hardware timestamp is present. With a zero input, timecounter_cyc2time() can return a nonzero value. This patch makes ptp_convert_timestamp() return 0 without converting when the input timestamp is zero.

drivers/ptp/ptp_vclock.c ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index) · line 311

Quoted 10 lines
drivers/ptp/ptp_vclock.c +2 -0
index 6675466bc3074..4a62047f0ce0f 100644
@@ -307,6 +307,8 @@ ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index)
 	u64 ns;
 
 	ns = ktime_to_ns(*hwtstamp);
+	if (!ns)
+		return 0;

[Severity: Low]

This isn't a bug, but should the kernel-doc for ptp_convert_timestamp() in include/linux/ptp_clock_kernel.h be updated as well? It still says:

* Returns: converted timestamp, or 0 on error.

The new early return gives 0 on a path that is not an error. It passes through the "no hardware timestamp" case. After this patch, a 0 return means either a failed lookup or conversion, or a zero @hwtstamp.

Both callers in net/socket.c already treat 0 as "no timestamp", so nothing changes at runtime. A wording such as "converted timestamp, or 0 if @hwtstamp is zero or on error" would match the new behaviour.

Quoted 3 lines
drivers/ptp/ptp_vclock.c +0 -0
 	srcu_idx = srcu_read_lock(&vclock_srcu);
 
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261013-ptp-vclock-sampling-v3-0-d3ee4ea62fc6%40gmail.com