Skip to contentSkip to conversation

Virtual PTP clock improvements and fix

PATCH Applied Miroslav Lichvar 2 participants 6 messages Feb 2, 2022 – Feb 3, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover Virtual PTP clock improvements and fix

v2:
- dropped patch changing initial time of virtual clocks

The first patch fixes an oops when unloading a driver with PTP clock and enabled virtual clocks.

The other patches add missing features to make synchronization with virtual clocks work as well as with the physical clock.

Miroslav Lichvar (4):
ptp: unregister virtual clocks when unregistering physical clock.
ptp: increase maximum adjustment of virtual clocks.
ptp: add gettimex64() to virtual clocks.
ptp: add getcrosststamp() to virtual clocks.

 drivers/ptp/ptp_clock.c  | 11 ++++++--
 drivers/ptp/ptp_vclock.c | 56 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 62 insertions(+), 5 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/4 ptp: unregister virtual clocks when unregistering physical clock.

When unregistering a physical clock which has some virtual clocks, unregister the virtual clocks with it.

This fixes the following oops, which can be triggered by unloading a driver providing a PTP clock when it has enabled virtual clocks:

BUG: unable to handle page fault for address: ffffffffc04fc4d8
Oops: 0000 [#1] PREEMPT SMP NOPTI
RIP: 0010:ptp_vclock_read+0x31/0xb0
Call Trace:
 timecounter_read+0xf/0x50
 ptp_vclock_refresh+0x2c/0x50
 ? ptp_clock_release+0x40/0x40
 ptp_aux_kworker+0x17/0x30
 kthread_worker_fn+0x9b/0x240
 ? kthread_should_park+0x30/0x30
 kthread+0xe2/0x110
 ? kthread_complete_and_exit+0x20/0x20
 ret_from_fork+0x22/0x30
Fixes 73f37068d540 ("ptp: support ptp physical/virtual clocks conversion")
Cc Yangbo Lu <[email protected]>
Cc Yang Yingliang <[email protected]>
---
 drivers/ptp/ptp_clock.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/ptp/ptp_clock.c +9 -2
index 0e4bc8b9329d..b6f2cfd15dd2 100644
@@ -317,11 +317,18 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
 }
 EXPORT_SYMBOL(ptp_clock_register);
 
+static int unregister_vclock(struct device *dev, void *data)
+{
+	struct ptp_clock *ptp = dev_get_drvdata(dev);
+
+	ptp_vclock_unregister(info_to_vclock(ptp->info));
+	return 0;
+}
+
 int ptp_clock_unregister(struct ptp_clock *ptp)
 {
 	if (ptp_vclock_in_use(ptp)) {
-		pr_err("ptp: virtual clock in use\n");
-		return -EBUSY;
+		device_for_each_child(&ptp->dev, NULL, unregister_vclock);
 	}
 
 	ptp->defunct = 1;
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/4 ptp: increase maximum adjustment of virtual clocks.

In reply to Miroslav Lichvar

Increase the maximum frequency offset of virtual clocks to 50% to enable faster slewing corrections.

This value cannot be represented as scaled ppm when long has 32 bits, but that is already the case for other drivers, even those that provide the adjfine() function, i.e. 32-bit applications are expected to check for the limit.

---
 drivers/ptp/ptp_vclock.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/ptp/ptp_vclock.c +1 -2
index ab1d233173e1..5aa2b32d9dc7 100644
@@ -84,8 +84,7 @@ static long ptp_vclock_refresh(struct ptp_clock_info *ptp)
 static const struct ptp_clock_info ptp_vclock_info = {
 	.owner		= THIS_MODULE,
 	.name		= "ptp virtual clock",
-	/* The maximum ppb value that long scaled_ppm can support */
-	.max_adj	= 32767999,
+	.max_adj	= 500000000,
 	.adjfine	= ptp_vclock_adjfine,
 	.adjtime	= ptp_vclock_adjtime,
 	.gettime64	= ptp_vclock_gettime,
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 3/4 ptp: add gettimex64() to virtual clocks.

In reply to Miroslav Lichvar

If the physical clock has the gettimex64() function, provide a gettimex64() wrapper in the virtual clock to enable more accurate and stable synchronization.

This adds support for the PTP_SYS_OFFSET_EXTENDED ioctl.

---
 drivers/ptp/ptp_vclock.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/ptp/ptp_vclock.c +28 -1
index 5aa2b32d9dc7..2f0b46386176 100644
@@ -57,6 +57,30 @@ static int ptp_vclock_gettime(struct ptp_clock_info *ptp,
 	return 0;
 }
 
+static int ptp_vclock_gettimex(struct ptp_clock_info *ptp,
+			       struct timespec64 *ts,
+			       struct ptp_system_timestamp *sts)
+{
+	struct ptp_vclock *vclock = info_to_vclock(ptp);
+	struct ptp_clock *pptp = vclock->pclock;
+	struct timespec64 pts;
+	unsigned long flags;
+	int err;
+	u64 ns;
+
+	err = pptp->info->gettimex64(pptp->info, &pts, sts);
+	if (err)
+		return err;
+
+	spin_lock_irqsave(&vclock->lock, flags);
+	ns = timecounter_cyc2time(&vclock->tc, timespec64_to_ns(&pts));
+	spin_unlock_irqrestore(&vclock->lock, flags);
+
+	*ts = ns_to_timespec64(ns);
+
+	return 0;
+}
+
 static int ptp_vclock_settime(struct ptp_clock_info *ptp,
 			      const struct timespec64 *ts)
 {
@@ -87,7 +111,6 @@ static const struct ptp_clock_info ptp_vclock_info = {
 	.max_adj	= 500000000,
 	.adjfine	= ptp_vclock_adjfine,
 	.adjtime	= ptp_vclock_adjtime,
-	.gettime64	= ptp_vclock_gettime,
 	.settime64	= ptp_vclock_settime,
 	.do_aux_work	= ptp_vclock_refresh,
 };
@@ -123,6 +146,10 @@ struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock)
 
 	vclock->pclock = pclock;
 	vclock->info = ptp_vclock_info;
+	if (pclock->info->gettimex64)
+		vclock->info.gettimex64 = ptp_vclock_gettimex;
+	else
+		vclock->info.gettime64 = ptp_vclock_gettime;
 	vclock->cc = ptp_vclock_cc;
 
 	snprintf(vclock->info.name, PTP_CLOCK_NAME_LEN, "ptp%d_virt",
Signature · 1 line
2.34.1
#4 lore raw

PATCH v2 4/4 ptp: add getcrosststamp() to virtual clocks.

In reply to Miroslav Lichvar

If the physical clock supports cross timestamping (it has the getcrosststamp() function), provide a wrapper in the virtual clock to enable cross timestamping.

This adds support for the PTP_SYS_OFFSET_PRECISE ioctl.

---
 drivers/ptp/ptp_vclock.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/ptp/ptp_vclock.c +24 -0
index 2f0b46386176..cb179a3ea508 100644
@@ -95,6 +95,28 @@ static int ptp_vclock_settime(struct ptp_clock_info *ptp,
 	return 0;
 }
 
+static int ptp_vclock_getcrosststamp(struct ptp_clock_info *ptp,
+				     struct system_device_crosststamp *xtstamp)
+{
+	struct ptp_vclock *vclock = info_to_vclock(ptp);
+	struct ptp_clock *pptp = vclock->pclock;
+	unsigned long flags;
+	int err;
+	u64 ns;
+
+	err = pptp->info->getcrosststamp(pptp->info, xtstamp);
+	if (err)
+		return err;
+
+	spin_lock_irqsave(&vclock->lock, flags);
+	ns = timecounter_cyc2time(&vclock->tc, ktime_to_ns(xtstamp->device));
+	spin_unlock_irqrestore(&vclock->lock, flags);
+
+	xtstamp->device = ns_to_ktime(ns);
+
+	return 0;
+}
+
 static long ptp_vclock_refresh(struct ptp_clock_info *ptp)
 {
 	struct ptp_vclock *vclock = info_to_vclock(ptp);
@@ -150,6 +172,8 @@ struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock)
 		vclock->info.gettimex64 = ptp_vclock_gettimex;
 	else
 		vclock->info.gettime64 = ptp_vclock_gettime;
+	if (pclock->info->getcrosststamp)
+		vclock->info.getcrosststamp = ptp_vclock_getcrosststamp;
 	vclock->cc = ptp_vclock_cc;
 
 	snprintf(vclock->info.name, PTP_CLOCK_NAME_LEN, "ptp%d_virt",
Signature · 1 line
2.34.1
#5 lore raw
In reply to Miroslav Lichvar
This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Quoted 10 lines · On Wed, 2 Feb 2022 10:33:54 +0100 you wrote:

v2:
- dropped patch changing initial time of virtual clocks

The first patch fixes an oops when unloading a driver with PTP clock and enabled virtual clocks.

The other patches add missing features to make synchronization with virtual clocks work as well as with the physical clock.

[...]

Here is the summary with links:
- [PATCHv2,net-next,1/4] ptp: unregister virtual clocks when unregistering physical clock.

    https://git.kernel.org/netdev/net-next/c/bfcbb76b0f59
  - [PATCHv2,net-next,2/4] ptp: increase maximum adjustment of virtual clocks.
    https://git.kernel.org/netdev/net-next/c/f77222d693cc
  - [PATCHv2,net-next,3/4] ptp: add gettimex64() to virtual clocks.
    https://git.kernel.org/netdev/net-next/c/f0067ebfc42b
  - [PATCHv2,net-next,4/4] ptp: add getcrosststamp() to virtual clocks.
    https://git.kernel.org/netdev/net-next/c/21fad63084c2

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html