Skip to contentSkip to conversation

ptp: only allow phase values lower than 1 period

PATCH Superseded Vladimir Oltean 3 participants 5 messages Aug 3, 2020 – Aug 4, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ptp: only allow phase values lower than 1 period

The way we define the phase (the difference between the time of the signal's rising edge, and the closest integer multiple of the period), it doesn't make sense to have a phase value larger than 1 period.

So deny these settings coming from the user.

---
 drivers/ptp/ptp_chardev.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/ptp/ptp_chardev.c +13 -0
index e0e6f85966e1..02fcd5e8b998 100644
@@ -218,6 +218,19 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 					break;
 				}
 			}
+			if (perout->flags & PTP_PEROUT_PHASE) {
+				/*
+				 * The phase should be specified modulo the
+				 * period, therefore anything larger than 1
+				 * period is invalid.
+				 */
+				if (perout->phase.sec > perout->period.sec ||
+				    (perout->phase.sec == perout->period.sec &&
+				     perout->phase.nsec > perout->period.nsec)) {
+					err = -ERANGE;
+					break;
+				}
+			}
 		} else if (cmd == PTP_PEROUT_REQUEST) {
 			req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
 			req.perout.rsv[0] = 0;
Signature · 1 line
2.25.1
maintainer
#1 lore raw

From: Vladimir Oltean <[email protected]>
Date: Mon, 3 Aug 2020 22:49:21 +0300

Quoted 7 lines

The way we define the phase (the difference between the time of the signal's rising edge, and the closest integer multiple of the period), it doesn't make sense to have a phase value larger than 1 period.

So deny these settings coming from the user.

Richard, please review.

maintainer
#2 lore raw
In reply to Vladimir Oltean

From: Vladimir Oltean <[email protected]>
Date: Mon, 3 Aug 2020 22:49:21 +0300

Quoted 7 lines

The way we define the phase (the difference between the time of the signal's rising edge, and the closest integer multiple of the period), it doesn't make sense to have a phase value larger than 1 period.

So deny these settings coming from the user.

Richard, I need your review on this patch.

Thank you.

maintainer
#3 lore raw
In reply to Vladimir Oltean
Quoted 6 lines · Vladimir Oltean
drivers/ptp/ptp_chardev.c +2 -0
@@ -218,6 +218,19 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 					break;
 				}
 			}
+			if (perout->flags & PTP_PEROUT_PHASE) {
+				/*

drivers/ptp/ptp_chardev.c long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg) · line 232

Quoted 10 lines
drivers/ptp/ptp_chardev.c +10 -0
+				 * The phase should be specified modulo the
+				 * period, therefore anything larger than 1
+				 * period is invalid.
+				 */
+				if (perout->phase.sec > perout->period.sec ||
+				    (perout->phase.sec == perout->period.sec &&
+				     perout->phase.nsec > perout->period.nsec)) {
+					err = -ERANGE;
+					break;
+				}

So if perout->period={1,0} and perout->phase={1,0} then the phase has wrapped 360 degrees back to zero.

Shouldn't this code catch that case as well?

So why not test for (perout->phase.nsec >= perout->period.nsec) instead?

Thanks,
Richard

#4 lore raw
Quoted 21 lines · Richard Cochran
Quoted 6 lines · Vladimir Oltean
drivers/ptp/ptp_chardev.c +2 -0
@@ -218,6 +218,19 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 					break;
 				}
 			}
+			if (perout->flags & PTP_PEROUT_PHASE) {
+				/*

drivers/ptp/ptp_chardev.c long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg) · line 232

Quoted 10 lines
drivers/ptp/ptp_chardev.c +10 -0
+				 * The phase should be specified modulo the
+				 * period, therefore anything larger than 1
+				 * period is invalid.
+				 */
+				if (perout->phase.sec > perout->period.sec ||
+				    (perout->phase.sec == perout->period.sec &&
+				     perout->phase.nsec > perout->period.nsec)) {
+					err = -ERANGE;
+					break;
+				}

So if perout->period={1,0} and perout->phase={1,0} then the phase has wrapped 360 degrees back to zero.

Quoted 6 lines

Shouldn't this code catch that case as well?

So why not test for (perout->phase.nsec >= perout->period.nsec) instead?

Thanks,
Richard

Oof, I guess this is what one would call 'brain fart'. In my mind, checking for equality between period and phase required an extra 'if', which I was reluctant to add (or to even think about, it seems). I converted the nsec check to >= and it works as it should (I checked with a modified ts2phc).

ts2phc[326.764]: config item /dev/ptp1.ts2phc.perout_phase is 1000000000 ts2phc[326.764]: config item /dev/ptp1.ts2phc.pulsewidth is 500000000 ts2phc[326.764]: PTP_PEROUT_REQUEST2 failed: Numerical result out of range

I'm sending a v2 with your change very soon.

Thanks.
-Vladimir