Skip to contentSkip to conversation

ptp: only allow phase values lower than 1 period

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

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
Changes in v2:
Be sure to also deny the case where the period is equal to the phase. This represents a 360 degree offset, which is equivalent to a phase of zero, so it should be rejected on the grounds of having a modulo equivalent as well.

 drivers/ptp/ptp_chardev.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/ptp/ptp_chardev.c +13 -0
index e0e6f85966e1..ee48eb61b49c 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
#1 lore raw
Quoted 22 lines · Vladimir Oltean

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.

---
Changes in v2:
Be sure to also deny the case where the period is equal to the phase. This represents a 360 degree offset, which is equivalent to a phase of zero, so it should be rejected on the grounds of having a modulo equivalent as well.

 drivers/ptp/ptp_chardev.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/ptp/ptp_chardev.c +0 -0
index e0e6f85966e1..ee48eb61b49c 100644

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

Quoted 9 lines
drivers/ptp/ptp_chardev.c +6 -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) {
+				/*
+				 * The phase should be specified modulo the
+				 * period, therefore anything larger than 1
+				 * period is invalid.
+				 */

A nit: this could read "therefore anything equal or larger than 1 period is invalid"? a number modulo itself is 0, right? and we use ">=" below as well now.

I do think it's relatively clear from context so it probably isn't worth a re-roll.

Quoted 11 lines
drivers/ptp/ptp_chardev.c +7 -0
+				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;
#2 lore raw
Quoted 5 lines · Jacob Keller

A nit: this could read "therefore anything equal or larger than 1 period is invalid"? a number modulo itself is 0, right? and we use ">=" below as well now.

Thanks, I've corrected that too, now.

-Vladimir