Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev,
	James Hilliard <james.hilliard1@gmail.com>,
	Jakub Kicinski <kuba@kernel.org>, Sasha Levin <sashal@kernel.org>
Subject: [PATCH 6.6 0060/1167] ptp: vclock: propagate physical clock sampling errors
Date: Fri,  4 Dec 2026 18:51:27 +0900	[thread overview]
Message-ID: <20261204100703.646860186@linuxfoundation.org> (raw)
In-Reply-To: <20261204100702.047403948@linuxfoundation.org>

6.6-stable review patch.  If anyone has any objections, please let me know.

------------------

From: James Hilliard <james.hilliard1@gmail.com>

[ Upstream commit d1bdfdafd663dd00b01d43eb3391f87718f2a09d ]

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.

Retry the first failed refresh after 10 ms instead of waiting another two
seconds. With the existing 32-bit nanosecond mask, timecounter_cyc2time()
can interpret a fresh timestamp as being in the past once the last
successful refresh is about 2.147 seconds old. An isolated read failure
should not deliberately defer the next attempt until that sample is four
seconds old.

Double the retry delay after consecutive failures, up to the normal
two-second refresh interval, and reset it after a successful refresh.
Each virtual clock has its own worker, and a failed parent read can involve
bus traffic and error logging. Backoff limits that repeated work during a
persistent failure while retaining a prompt first retry.

Limit each retry delay to the next regular refresh deadline. Otherwise,
backoff can skip the next two-second refresh opportunity and lose a full
counter wrap even when the parent has recovered before that refresh was
due. Advance the deadline when an attempt reaches it without resetting the
backoff, so sustained failures still settle at the normal refresh interval.
Keep the retry state private to each clock's auxiliary worker.

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. Queue the first
refresh immediately after registration so registration latency is not
followed by another full refresh interval.

Keep the existing cyclecounter mask and scaling. These scheduling changes
do not guarantee timely samples: prolonged read failures or worker delays
can still exceed the half-wrap conversion window, and a gap of about
4.295 seconds between successful samples can lose a full counter wrap.
Do not assume that getcycles64() exposes a full-width nanosecond counter.

Fixes: 5d43f951b1ac ("ptp: add ptp virtual clock driver framework")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
Link: https://patch.msgid.link/20261026-ptp-vclock-sampling-v6-3-e705612541f1@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/ptp/ptp_private.h |  6 ++-
 drivers/ptp/ptp_sysfs.c   |  4 +-
 drivers/ptp/ptp_vclock.c  | 89 ++++++++++++++++++++++++++++++---------
 3 files changed, 76 insertions(+), 23 deletions(-)

diff --git a/drivers/ptp/ptp_private.h b/drivers/ptp/ptp_private.h
index 5f3697c61e2b7..d69ab0106c9c3 100644
--- a/drivers/ptp/ptp_private.h
+++ b/drivers/ptp/ptp_private.h
@@ -72,7 +72,11 @@ 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 */
+	/* Owned by the auxiliary worker. */
+	unsigned long retry_delay;
+	unsigned long refresh_deadline;
 };
 
 /*
diff --git a/drivers/ptp/ptp_sysfs.c b/drivers/ptp/ptp_sysfs.c
index 31e176e91d78c..77a0c1082cd93 100644
--- a/drivers/ptp/ptp_sysfs.c
+++ b/drivers/ptp/ptp_sysfs.c
@@ -222,8 +222,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;
diff --git a/drivers/ptp/ptp_vclock.c b/drivers/ptp/ptp_vclock.c
index a88c424593732..d112934351dc9 100644
--- a/drivers/ptp/ptp_vclock.c
+++ b/drivers/ptp/ptp_vclock.c
@@ -4,6 +4,7 @@
  *
  * Copyright 2021 NXP
  */
+#include <linux/jiffies.h>
 #include <linux/slab.h>
 #include <linux/hashtable.h>
 #include "ptp_private.h"
@@ -13,6 +14,7 @@
 #define PTP_VCLOCK_FADJ_SHIFT		9
 #define PTP_VCLOCK_FADJ_DENOMINATOR	15625ULL
 #define PTP_VCLOCK_REFRESH_INTERVAL	(HZ * 2)
+#define PTP_VCLOCK_RETRY_INTERVAL		msecs_to_jiffies(10)
 
 /* protects vclock_hash addition/deletion */
 static DEFINE_SPINLOCK(vclock_hash_lock);
@@ -42,21 +44,39 @@ static void ptp_vclock_hash_del(struct ptp_vclock *vclock)
 	synchronize_srcu(&vclock_srcu);
 }
 
+/* The caller holds vclock->lock, or has not published the clock yet. */
+static int ptp_vclock_sample(struct ptp_vclock *vclock)
+{
+	struct ptp_clock *ptp = vclock->pclock;
+	struct timespec64 ts;
+	int err;
+
+	err = ptp->info->getcycles64(ptp->info, &ts);
+	if (!err)
+		vclock->cycles = timespec64_to_ns(&ts);
+
+	return err;
+}
+
 static int ptp_vclock_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
 {
 	struct ptp_vclock *vclock = info_to_vclock(ptp);
 	s64 adj;
+	int err;
 
 	adj = (s64)scaled_ppm << PTP_VCLOCK_FADJ_SHIFT;
 	adj = div_s64(adj, PTP_VCLOCK_FADJ_DENOMINATOR);
 
 	if (mutex_lock_interruptible(&vclock->lock))
 		return -EINTR;
-	timecounter_read(&vclock->tc);
-	vclock->cc.mult = PTP_VCLOCK_CC_MULT + adj;
+	err = ptp_vclock_sample(vclock);
+	if (!err) {
+		timecounter_read(&vclock->tc);
+		vclock->cc.mult = PTP_VCLOCK_CC_MULT + adj;
+	}
 	mutex_unlock(&vclock->lock);
 
-	return 0;
+	return err;
 }
 
 static int ptp_vclock_adjtime(struct ptp_clock_info *ptp, s64 delta)
@@ -75,15 +95,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;
-	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;
 }
 
 static int ptp_vclock_gettimex(struct ptp_clock_info *ptp,
@@ -115,13 +136,16 @@ static int ptp_vclock_settime(struct ptp_clock_info *ptp,
 {
 	struct ptp_vclock *vclock = info_to_vclock(ptp);
 	u64 ns = timespec64_to_ns(ts);
+	int err;
 
 	if (mutex_lock_interruptible(&vclock->lock))
 		return -EINTR;
-	timecounter_init(&vclock->tc, &vclock->cc, ns);
+	err = ptp_vclock_sample(vclock);
+	if (!err)
+		timecounter_init(&vclock->tc, &vclock->cc, ns);
 	mutex_unlock(&vclock->lock);
 
-	return 0;
+	return err;
 }
 
 static int ptp_vclock_getcrosststamp(struct ptp_clock_info *ptp,
@@ -151,8 +175,24 @@ static long ptp_vclock_refresh(struct ptp_clock_info *ptp)
 	struct ptp_vclock *vclock = info_to_vclock(ptp);
 	struct timespec64 ts;
 
-	ptp_vclock_gettime(&vclock->info, &ts);
+	/* Retry transient failures promptly, but back off during long outages. */
+	if (ptp_vclock_gettime(&vclock->info, &ts)) {
+		unsigned long now = jiffies;
+
+		/* Retries must not postpone the next regular refresh attempt. */
+		if (!vclock->retry_delay || time_after_eq(now, vclock->refresh_deadline))
+			vclock->refresh_deadline = now + PTP_VCLOCK_REFRESH_INTERVAL;
+
+		if (!vclock->retry_delay)
+			vclock->retry_delay = PTP_VCLOCK_RETRY_INTERVAL;
+		else
+			vclock->retry_delay = min_t(unsigned long,
+						    vclock->retry_delay * 2,
+						    PTP_VCLOCK_REFRESH_INTERVAL);
+		return min(vclock->retry_delay, vclock->refresh_deadline - now);
+	}
 
+	vclock->retry_delay = 0;
 	return PTP_VCLOCK_REFRESH_INTERVAL;
 }
 
@@ -174,12 +214,9 @@ static const struct ptp_clock_info ptp_vclock_info = {
 static u64 ptp_vclock_read(const struct cyclecounter *cc)
 {
 	struct ptp_vclock *vclock = cc_to_vclock(cc);
-	struct ptp_clock *ptp = vclock->pclock;
-	struct timespec64 ts = {};
-
-	ptp->info->getcycles64(ptp->info, &ts);
 
-	return timespec64_to_ns(&ts);
+	/* The cyclecounter callback cannot report a failed PHC read. */
+	return vclock->cycles;
 }
 
 static const struct cyclecounter ptp_vclock_cc = {
@@ -192,10 +229,11 @@ static const struct cyclecounter ptp_vclock_cc = {
 struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock)
 {
 	struct ptp_vclock *vclock;
+	int err;
 
 	vclock = kzalloc(sizeof(*vclock), GFP_KERNEL);
 	if (!vclock)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	vclock->pclock = pclock;
 	vclock->info = ptp_vclock_info;
@@ -214,20 +252,29 @@ 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);
-		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);
+	/* The initial sample predates registration; refresh without more delay. */
+	ptp_schedule_worker(vclock->clock, 0);
 
 	ptp_vclock_hash_add(vclock);
 
 	return vclock;
+
+free_vclock:
+	kfree(vclock);
+	return ERR_PTR(err);
 }
 
 void ptp_vclock_unregister(struct ptp_vclock *vclock)
-- 
2.53.0




  parent reply	other threads:[~2026-12-04 10:22 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  9:50 [PATCH 6.6 0000/1167] 6.6.164-rc1 review Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0001/1167] netfilter: nft_ct: expectation timeouts are passed in milliseconds Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0002/1167] netfilter: nft_ct: move custom expectation support to helper Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0003/1167] mm/khugepaged: preserve dirty bits when collapsing PTE-mapped THPs Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0004/1167] drm/amdgpu: Check cs flags Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0005/1167] bpf: Fix memory access flags in helper prototypes Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0006/1167] bpf: Drop inet_conn_established from lsm sleepable hooks Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0007/1167] mm: page_alloc: remove ALLOC_NON_BLOCK from ALLOC_RESERVES Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0008/1167] irqchip/sifive-plic: Avoid signed shift in interrupt enable mask Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0009/1167] bpf: Fix verifier assumptions of bpf_d_paths output buffer Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0010/1167] bpf: Specify access type of bpf_sysctl_get_name args Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0011/1167] fs: aio: reject partial mremap to avoid Null-pointer-dereference error Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0012/1167] virtio_net: check RX buffer length before DMA sync Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0013/1167] mm/ksm: fix userfaultfd protection during merge and unmerge Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0014/1167] udf: validate VAT inode size for old VAT format Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0015/1167] udf: validate vat20->lengthHeader and VAT inode size in udf_load_vat() Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0016/1167] io_uring: enable toggle of iowait usage when waiting on CQEs Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0017/1167] io_uring: fail soft links on negative IOU_COMPLETE results Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0018/1167] io_uring/rw: fail soft links on IOPOLL short or error IO Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0019/1167] io_uring/timeout: dont rearm a multishot timeout on cancel Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0020/1167] io_uring/rsrc: keep the old file when a file update fails Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0021/1167] io_uring: cleanup handle_tw_list() calling convention Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0022/1167] io_uring: pass in counter to handle_tw_list() rather than return it Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0023/1167] io_uring/sqpoll: manage task_work privately Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0024/1167] io_uring: wake SQPOLL task when task_work is added to an empty queue Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0025/1167] io_uring/sqpoll: use the correct check for pending task_work Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0026/1167] io_uring/sqpoll: early exit thread if task_context wasnt allocated Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0027/1167] io_uring/sqpoll: ensure that normal task_work is also run timely Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0028/1167] io_uring: signal SQPOLL task_work with TWA_SIGNAL_NO_IPI Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0029/1167] io_uring/sqpoll: ensure task state is TASK_RUNNING when running task_work Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0030/1167] io_uring: dont touch sqd->thread off tw add Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0031/1167] io_uring/sqpoll: zero sqd->thread on tctx errors Greg Kroah-Hartman
2026-12-04  9:50 ` [PATCH 6.6 0032/1167] io_uring/sqpoll: Increase task_work submission batch size Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0033/1167] io_uring: fix task_work add use-after-free with SQPOLL Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0034/1167] io_uring/sqpoll: do the return-to-userspace work in the SQPOLL thread Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0035/1167] net: sfp: add quirk for Lantech 8330-265D Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0036/1167] Input: destroy force-feedback device on input unregistration Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0037/1167] staging: media: atomisp: use array3_size() for overflow-safe allocation Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0038/1167] fs/9p: switch vfsmount to use v9fs_get_new_inode Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0039/1167] fs/9p: rework qid2ino logic Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0040/1167] 9p: skip intermediate directory entry name copy in p9dirent_read() Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0041/1167] xfrm: zero-initialise km_event in the replay-notify path Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0042/1167] batman-adv: avoid WARN_ON for orig_ifinfo allocation failure Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0043/1167] wifi: ath9k_htc: start ANI when an AP interface begins to beacon Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0044/1167] scsi: aic7xxx: Fix REQUEST SENSE allocation length on big-endian hosts Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0045/1167] scsi: aha1542: Remove unneeded scsi_dma_unmap() call Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0046/1167] 9p: fall back to the normal open path for non-regular files Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0047/1167] 9p: do not store the create fid on special files Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0048/1167] wifi: ath10k: Stop unanswered temperature reads Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0049/1167] seg6: fix transport header for inner IPv4 with IP options Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0050/1167] gpio: omap: Propagate runtime PM failures in probe and request Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0051/1167] btrfs: verity: propagate orphan insertion errors when enabling verity Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0052/1167] btrfs: scrub: fix lost wakeup and hang when cancelling a scrub that is finishing Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0053/1167] btrfs: zoned: fix double free of bitmaps on clone failure Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0054/1167] net: ethernet: mtk_wed: fix reset condition for rx wdma Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0055/1167] net: ethernet: mtk_wed: free up rings during detach Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0056/1167] net: ethernet: mtk_wed: introduce versioning utility routines Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0057/1167] net: ethernet: mtk_wed: setup WDMA_RING_TX(0) for non-DBDC MT7986 Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0058/1167] ptp: drain virtual clock sysfs operations before unregistering children Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0059/1167] ptp: unwind partially completed virtual clock creation Greg Kroah-Hartman
2026-12-04  9:51 ` Greg Kroah-Hartman [this message]
2026-12-04  9:51 ` [PATCH 6.6 0061/1167] ptp: vclock: serialize extended samples with counter updates Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0062/1167] scsi: target: Validate REGISTER AND MOVE TransportID length Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0063/1167] tipc: reject attaching bearer of different media to the same occupied device Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0064/1167] spi: pic32-sqi: fail transfer when the BD ring is exhausted Greg Kroah-Hartman
2026-12-04  9:51 ` [PATCH 6.6 0065/1167] bpf: Require prog fd or capability for cgroup detach Greg Kroah-Hartman

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=20261204100703.646860186@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=james.hilliard1@gmail.com \
    --cc=kuba@kernel.org \
    --cc=patches@lists.linux.dev \
    --cc=sashal@kernel.org \
    --cc=stable@vger.kernel.org \
    /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