ptp: ocp: have adjtime handle negative delta_ns correctly
PATCH 1/1 ptp: ocp: have adjtime handle negative delta_ns correctly
delta_ns is a s64, but it was being passed ptp_ocp_adjtime_coarse as an u64. Also, it turns out that timespec64_add_ns() only handles positive values, so the math needs to be updated.
Fix by passing in the correct signed value, then adding to a nanosecond version of the timespec.
--- drivers/ptp/ptp_ocp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/ptp/ptp_ocp.c +4 -2
@@ -841,16 +841,18 @@ __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) }static void -ptp_ocp_adjtime_coarse (struct ptp_ocp *bp,u64 delta_ns) +ptp_ocp_adjtime_coarse (struct ptp_ocp *bp,s64 delta_ns) {struct timespec64 ts;unsigned long flags;int err; +s64 ns;spin_lock_irqsave (&bp->lock, flags); err =__ptp_ocp_gettime_locked (bp, &ts,NULL );if (likely (!err)) { -timespec64_add_ns (&ts, delta_ns); + ns =timespec64_to_ns (&ts) + delta_ns; + ts =ns_to_timespec64 (ns);__ptp_ocp_settime_locked (bp, &ts); }spin_unlock_irqrestore (&bp->lock, flags);
Signature · 1 line
2.31.1
Quoted 25 lines · Jonathan Lemon
delta_nsis a s64, but it was being passedptp_ocp_adjtime_coarseas an u64. Also, it turns out thattimespec64_add_ns()only handles positive values, so the math needs to be updated.Fix by passing in the correct signed value, then adding to a nanosecond version of the timespec.
Fixes '90f8f4c0e3ce ("ptp: ocp: Add ptp_ocp_adjtime_coarse for large adjustments")'Signed-off-by Jonathan Lemon <[email protected]>--- drivers/ptp/ptp_ocp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/ptp/ptp_ocp.c +1
-1@@ -841,16 +841,18 @@ __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) }static void -ptp_ocp_adjtime_coarse (struct ptp_ocp *bp,u64 delta_ns) +ptp_ocp_adjtime_coarse (struct ptp_ocp *bp,s64 delta_ns) {struct timespec64 ts;
drivers/ptp/ptp_ocp.c __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) · line 855
Quoted 10 lines
drivers/ptp/ptp_ocp.c +3
-1unsigned long flags;int err; +s64 ns;spin_lock_irqsave (&bp->lock, flags); err =__ptp_ocp_gettime_locked (bp, &ts,NULL );if (likely (!err)) { -timespec64_add_ns (&ts, delta_ns); + ns =timespec64_to_ns (&ts) + delta_ns; + ts =ns_to_timespec64 (ns);
Maybe use set_normalized_timespec64 instead of this ugly transformations and additional variable?
Quoted 3 lines
drivers/ptp/ptp_ocp.c +0
-0__ptp_ocp_settime_locked (bp, &ts); }spin_unlock_irqrestore (&bp->lock, flags);
Quoted 29 lines · Vadim Fedorenko
Quoted 28 lines · Jonathan Lemon
delta_nsis a s64, but it was being passedptp_ocp_adjtime_coarseas an u64. Also, it turns out thattimespec64_add_ns()only handles positive values, so the math needs to be updated.Fix by passing in the correct signed value, then adding to a nanosecond version of the timespec.
Fixes '90f8f4c0e3ce ("ptp: ocp: Add ptp_ocp_adjtime_coarse for large adjustments")'Signed-off-by Jonathan Lemon <[email protected]>--- drivers/ptp/ptp_ocp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/ptp/ptp_ocp.c +2
-1@@ -841,16 +841,18 @@ __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) }static void -ptp_ocp_adjtime_coarse (struct ptp_ocp *bp,u64 delta_ns) +ptp_ocp_adjtime_coarse (struct ptp_ocp *bp,s64 delta_ns) {struct timespec64 ts;unsigned long flags;int err; +s64 ns;spin_lock_irqsave (&bp->lock, flags);
Quoted 8 lines
drivers/ptp/ptp_ocp.c __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) · line 855
Quoted 5 lines
drivers/ptp/ptp_ocp.c +2
-1err =__ptp_ocp_gettime_locked (bp, &ts,NULL );if (likely (!err)) { -timespec64_add_ns (&ts, delta_ns); + ns =timespec64_to_ns (&ts) + delta_ns; + ts =ns_to_timespec64 (ns);Maybe use
set_normalized_timespec64instead of this ugly transformations and additional variable?
I don't see how that would work - set_normalized_timespec64 just sets the ts from a <sec>.<nsec> value. In this case, delta_ns need to be added in to the ts value, but timespec64_add_ns() doeesn't handle negative values, hence the conversion / add / reconversion.
Signature · 1 line
Jonathan
Quoted 39 lines · Jonathan Lemon
Quoted 29 lines · Vadim Fedorenko
Quoted 28 lines · Jonathan Lemon
delta_nsis a s64, but it was being passedptp_ocp_adjtime_coarseas an u64. Also, it turns out thattimespec64_add_ns()only handles positive values, so the math needs to be updated.Fix by passing in the correct signed value, then adding to a nanosecond version of the timespec.
Fixes '90f8f4c0e3ce ("ptp: ocp: Add ptp_ocp_adjtime_coarse for large adjustments")'Signed-off-by Jonathan Lemon <[email protected]>--- drivers/ptp/ptp_ocp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/ptp/ptp_ocp.c +2
-1@@ -841,16 +841,18 @@ __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) }static void -ptp_ocp_adjtime_coarse (struct ptp_ocp *bp,u64 delta_ns) +ptp_ocp_adjtime_coarse (struct ptp_ocp *bp,s64 delta_ns) {struct timespec64 ts;unsigned long flags;int err; +s64 ns;spin_lock_irqsave (&bp->lock, flags);Quoted 8 lines
drivers/ptp/ptp_ocp.c __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) · line 855
Quoted 5 lines
drivers/ptp/ptp_ocp.c +2
-1err =__ptp_ocp_gettime_locked (bp, &ts,NULL );if (likely (!err)) { -timespec64_add_ns (&ts, delta_ns); + ns =timespec64_to_ns (&ts) + delta_ns; + ts =ns_to_timespec64 (ns);Maybe use
set_normalized_timespec64instead of this ugly transformations and additional variable?
Quoted 4 lines
I don't see how that would work -
set_normalized_timespec64just sets the ts from a <sec>.<nsec> value. In this case,delta_nsneed to be added in to the ts value, buttimespec64_add_ns()doeesn't handle negative values, hence the conversion / add / reconversion.
Could be like:
- timespec64_add_ns(&ts, delta_ns);
+ set_normalized_timespec64(&ts, ts.tv_sec, ts.tv_nsec + delta_ns);
That's actually without multiplication and division caused by two conversions.