Skip to contentSkip to conversation

timer:clock:ptp: add support the dynamic posix clock alarm set for ptp

PATCH In review Po Liu 2 participants 5 messages May 5, 2019 – May 8, 2019

Subsystems (7)
lore
Share & tools
lore

Po Liu

author
#0 lore raw

PATCH 1/1 timer:clock:ptp: add support the dynamic posix clock alarm set for ptp

Current kernel code do not support the dynamic posix clock alarm set. This code would support it by the posix timer structure.

319 const struct k_clock clock_posix_dynamic = {

320         .clock_getres   = pc_clock_getres,
321         .clock_set      = pc_clock_settime,
322         .clock_get      = pc_clock_gettime,
323         .clock_adj      = pc_clock_adjtime,
324 +       .timer_create   = pc_timer_create,
325 +       .timer_del      = pc_timer_delete,
326 +       .timer_set      = pc_timer_set,
327 +       .timer_arm      = pc_timer_arm,

}

This won't change the user space system call code. Normally the user space set alarm by timer_create() and timer_settime(). Reference code are tools/testing/selftests/ptp/testptp.c.

Some case requiring providing the alarm set for user space by ptp clock.

---
 drivers/net/ethernet/freescale/enetc/enetc_ptp.c |  1 +
 drivers/ptp/ptp_clock.c                          | 39 ++++++++++++++-
 drivers/ptp/ptp_qoriq.c                          | 44 +++++++++++++++++
 include/linux/fsl/ptp_qoriq.h                    |  3 ++
 include/linux/posix-clock.h                      |  3 +-
 include/linux/ptp_clock_kernel.h                 |  5 +-
 kernel/time/posix-clock.c                        | 60 ++++++++++++++++++++++++
 7 files changed, 152 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/enetc/enetc_ptp.c +1 -0
index 8c1497e..35e2f2a 100644
@@ -21,6 +21,7 @@
 	.gettime64	= ptp_qoriq_gettime,
 	.settime64	= ptp_qoriq_settime,
 	.enable		= ptp_qoriq_enable,
+	.alarm		= ptp_qoriq_alarm,
 };
 
 static int enetc_ptp_probe(struct pci_dev *pdev,
drivers/ptp/ptp_clock.c +38 -1
index 79bd102..72d06a8 100644
@@ -23,7 +23,9 @@
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/sched/task.h>
 #include <linux/posix-clock.h>
+#include <linux/posix-timers.h>
 #include <linux/pps_kernel.h>
 #include <linux/slab.h>
 #include <linux/syscalls.h>
@@ -166,12 +168,31 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
 	return err;
 }
 
+static int ptp_clock_alarm(struct posix_clock *pc, ktime_t expires,
+			   bool absolute, struct k_itimer *timr)
+{
+	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
+	struct ptp_clock_info *ops;
+
+	ops = ptp->info;
+	if (!ops)
+		return -EINVAL;
+
+	if (!ops->alarm)
+		return -EINVAL;
+
+	ops->alarm(ops, expires, absolute, timr);
+
+	return 0;
+}
+
 static struct posix_clock_operations ptp_clock_ops = {
 	.owner		= THIS_MODULE,
 	.clock_adjtime	= ptp_clock_adjtime,
 	.clock_gettime	= ptp_clock_gettime,
 	.clock_getres	= ptp_clock_getres,
 	.clock_settime	= ptp_clock_settime,
+	.clock_alarm	= ptp_clock_alarm,
 	.ioctl		= ptp_ioctl,
 	.open		= ptp_open,
 	.poll		= ptp_poll,
@@ -324,6 +345,20 @@ int ptp_clock_unregister(struct ptp_clock *ptp)
 }
 EXPORT_SYMBOL(ptp_clock_unregister);
 
+int alarm_timer_event(struct k_itimer *timr, int si_private)
+{
+	int ret = -1;
+
+	timr->sigq->info.si_sys_private = si_private;
+
+	rcu_read_lock();
+	ret = send_sigqueue(timr->sigq, timr->it_pid, PIDTYPE_PID);
+	rcu_read_unlock();
+
+	/* If we failed to send the signal the timer stops. */
+	return ret > 0;
+}
+
 void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
 {
 	struct pps_event_time evt;
@@ -331,8 +366,10 @@ void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
 	switch (event->type) {
 
 	case PTP_CLOCK_ALARM:
+		if (!event->timr)
+			break;
+		alarm_timer_event(event->timr, 0);
 		break;
-
 	case PTP_CLOCK_EXTTS:
 		enqueue_external_timestamp(&ptp->tsevq, event);
 		wake_up_interruptible(&ptp->tsev_wq);
drivers/ptp/ptp_qoriq.c +44 -0

66 lines not on this page · raw message

include/linux/fsl/ptp_qoriq.h +3 -0
index 992bf9f..2928df4 100644
@@ -143,6 +143,7 @@ struct ptp_qoriq {
 	spinlock_t lock; /* protects regs */
 	struct ptp_clock *clock;
 	struct ptp_clock_info caps;
+	struct k_itimer *timr;
 	struct resource *rsrc;
 	struct dentry *debugfs_root;
 	struct device *dev;
@@ -190,6 +191,8 @@ int ptp_qoriq_init(struct ptp_qoriq *ptp_qoriq, void __iomem *base,
 int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts);
 int ptp_qoriq_settime(struct ptp_clock_info *ptp,
 		      const struct timespec64 *ts);
+int ptp_qoriq_alarm(struct ptp_clock_info *ptp, ktime_t expires,
+		    bool absolute, struct k_itimer *timr);
 int ptp_qoriq_enable(struct ptp_clock_info *ptp,
 		     struct ptp_clock_request *rq, int on);
 #ifdef CONFIG_DEBUG_FS
include/linux/posix-clock.h +2 -1
index 18674d7..80cc214 100644
@@ -59,7 +59,8 @@ struct posix_clock_operations {
 
 	int  (*clock_settime)(struct posix_clock *pc,
 			      const struct timespec64 *ts);
-
+	int  (*clock_alarm)(struct posix_clock *pc, ktime_t expires,
+			    bool absolute, struct k_itimer *timr);
 	/*
 	 * Optional character device methods:
 	 */
include/linux/ptp_clock_kernel.h +4 -1
index 7121bbe..b51f64b 100644
@@ -24,7 +24,7 @@
 #include <linux/device.h>
 #include <linux/pps_kernel.h>
 #include <linux/ptp_clock.h>
-
+#include <linux/posix-timers.h>
 
 struct ptp_clock_request {
 	enum {
@@ -148,6 +148,8 @@ struct ptp_clock_info {
 	int (*getcrosststamp)(struct ptp_clock_info *ptp,
 			      struct system_device_crosststamp *cts);
 	int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts);
+	int (*alarm)(struct ptp_clock_info *p, ktime_t expires,
+		     bool absolute, struct k_itimer *timr);
 	int (*enable)(struct ptp_clock_info *ptp,
 		      struct ptp_clock_request *request, int on);
 	int (*verify)(struct ptp_clock_info *ptp, unsigned int pin,
@@ -180,6 +182,7 @@ struct ptp_clock_event {
 		u64 timestamp;
 		struct pps_event_time pps_times;
 	};
+	struct k_itimer *timr;
 };
 
 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
kernel/time/posix-clock.c +60 -0

75 lines not on this page · raw message

Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 13 lines · Po Liu

Current kernel code do not support the dynamic posix clock alarm set. This code would support it by the posix timer structure.

319 const struct k_clock clock_posix_dynamic = {

320         .clock_getres   = pc_clock_getres,
321         .clock_set      = pc_clock_settime,
322         .clock_get      = pc_clock_gettime,
323         .clock_adj      = pc_clock_adjtime,
324 +       .timer_create   = pc_timer_create,
325 +       .timer_del      = pc_timer_delete,
326 +       .timer_set      = pc_timer_set,
327 +       .timer_arm      = pc_timer_arm,
Quoted 2 lines

}

Sorry, NAK, since we decided some time ago not to support timer_* operations on dynamic clocks. You get much better application level timer performance by synchronizing CLOCK_REALTIME to your PHC and using clock_nanosleep() with CLOCK_REALTIME or CLOCK_MONOTONIC.

Quoted 3 lines

This won't change the user space system call code. Normally the user space set alarm by timer_create() and timer_settime(). Reference code are tools/testing/selftests/ptp/testptp.c.

That program still has misleading examples. Sorry about that. I'll submit a patch to remove them.

kernel/time/posix-clock.c static int pc_clock_settime(clockid_t id, const struct timespec64 *ts) · line 367

Quoted 5 lines
kernel/time/posix-clock.c +5 -0
+static int pc_timer_create(struct k_itimer *new_timer)
+{
+	return 0;
+}
+

This of course would never work. Consider what happens when two or more timers are created and armed.

Thanks,
Richard

author
#2 lore raw

Hi Richard,

Thank you for your reply.

Br,
Po Liu

Quoted 32 lines

-----Original Message-----
From: Richard Cochran [mailto:[email protected]]
Sent: 2019年5月7日 21:50
To: Po Liu <[email protected]>
Cc: [email protected]; [email protected]; linuxppc- [email protected]; [email protected]; Y.b. Lu <[email protected]>; Claudiu Manoil <[email protected]>; [email protected]; Leo Li <[email protected]>; Roy Zang <[email protected]>; Mingkai Hu <[email protected]>; [email protected]
Subject: [EXT] Re: [PATCH v1] timer:clock:ptp: add support the dynamic posix clock alarm set for ptp

Caution: EXT Email

Quoted 13 lines · Po Liu

Current kernel code do not support the dynamic posix clock alarm set. This code would support it by the posix timer structure.

319 const struct k_clock clock_posix_dynamic = {

320         .clock_getres   = pc_clock_getres,
321         .clock_set      = pc_clock_settime,
322         .clock_get      = pc_clock_gettime,
323         .clock_adj      = pc_clock_adjtime,
324 +       .timer_create   = pc_timer_create,
325 +       .timer_del      = pc_timer_delete,
326 +       .timer_set      = pc_timer_set,
327 +       .timer_arm      = pc_timer_arm,
Quoted 2 lines

}

Quoted 4 lines

Sorry, NAK, since we decided some time ago not to support timer_* operations on dynamic clocks. You get much better application level timer performance by synchronizing CLOCK_REALTIME to your PHC and using clock_nanosleep() with CLOCK_REALTIME or CLOCK_MONOTONIC.

The code intend to get alarm by interrupt of ptp hardware. The code to fix ptp not support to application layer to get the alarm interrupt.
Do you mean the synchronizing at application layer by PHC (using clock_nanosleep()) to the CLOCK_REALTIME source? Then the kernel could using the hrtimer with CLOCK_REALTIME?

Quoted 7 lines
Quoted 4 lines

This won't change the user space system call code. Normally the user space set alarm by timer_create() and timer_settime(). Reference code are tools/testing/selftests/ptp/testptp.c.

That program still has misleading examples. Sorry about that. I'll submit a patch to remove them.

Is there any replace method for an application code to get alarm interrupt by the ptp source?

Quoted 11 lines

drivers/ptp/ptp_clock.c static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) · line 188

Quoted 5 lines
drivers/ptp/ptp_clock.c +4 -0
+static int pc_timer_create(struct k_itimer *new_timer) {
+     return 0;
+}
+

This of course would never work. Consider what happens when two or more timers are created and armed.

Thanks,
Richard

maintainer
#3 lore raw
Quoted 10 lines · Po Liu
Quoted 4 lines

Sorry, NAK, since we decided some time ago not to support timer_* operations on dynamic clocks. You get much better application level timer performance by synchronizing CLOCK_REALTIME to your PHC and using clock_nanosleep() with CLOCK_REALTIME or CLOCK_MONOTONIC.

The code intend to get alarm by interrupt of ptp hardware. The code to fix ptp not support to application layer to get the alarm interrupt. Do you mean the synchronizing at application layer by PHC (using clock_nanosleep()) to the CLOCK_REALTIME source? Then the kernel could using the hrtimer with CLOCK_REALTIME?

Yes, or with CLOCK_MONOTONIC.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

This won't change the user space system call code. Normally the user space set alarm by timer_create() and timer_settime(). Reference code are tools/testing/selftests/ptp/testptp.c.

That program still has misleading examples. Sorry about that. I'll submit a patch to remove them.

Is there any replace method for an application code to get alarm interrupt by the ptp source?

No the alarm functionality has been removed. It will not be coming back, unless there are really strong arguments to support it.

Here is the result of a study of a prototype alarm method. It shows why the hrtimer method is better.

https://sourceforge.net/p/linuxptp/mailman/message/35535965/

Thanks,
Richard

maintainer
#4 lore raw
Quoted 2 lines · Richard Cochran

No the alarm functionality has been removed. It will not be coming back, unless there are really strong arguments to support it.

Here is some more background:

commit 3a06c7ac24f9f24ec059cd77c2dbdf7fbfd0aaaf
Author: Thomas Gleixner <[email protected]>
Date: Tue May 30 23:15:38 2017 +0200

posix-clocks: Remove interval timer facility and mmap/fasync callbacks

The only user of this facility is ptp_clock, which does not implement any of those functions.

Remove them to prevent accidental users. Especially the interval timer interfaces are now more or less impossible to implement because the necessary infrastructure has been confined to the core code. Aside of that it's really complex to make these callbacks implemented according to spec as the alarm timer implementation demonstrates. If at all then a nanosleep callback might be a reasonable extension. For now keep just what ptp_clock needs.

Quoted 4 lines

Here is the result of a study of a prototype alarm method. It shows why the hrtimer method is better.

   https://sourceforge.net/p/linuxptp/mailman/message/35535965/

That test was with a PCIe card. With a SoC that has a PHC as a built in peripheral, the hardware solution might outperform hrtimers.

So you might consider adding clock_nanosleep() for dynamic posix clocks. But your code will have to support multiple users at the same time.

Thanks,
Richard