diff mbox series

[net] ptp: ocp: have adjtime handle negative delta_ns correctly

Message ID 20220505234050.3378-1-jonathan.lemon@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net] ptp: ocp: have adjtime handle negative delta_ns correctly | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 3 of 3 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes fail Problems with Fixes tag: 1
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 20 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jonathan Lemon May 5, 2022, 11:40 p.m. UTC
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.

Fixes: '90f8f4c0e3ce ("ptp: ocp: Add ptp_ocp_adjtime_coarse for large adjustments")'
Signed-off-by: Jonathan Lemon <jonathan.lemon@gmail.com>
---
 drivers/ptp/ptp_ocp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Vadim Fedorenko May 7, 2022, 12:19 a.m. UTC | #1
On 06.05.2022 00:40, Jonathan Lemon wrote:
> 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.
> 
> Fixes: '90f8f4c0e3ce ("ptp: ocp: Add ptp_ocp_adjtime_coarse for large adjustments")'
> Signed-off-by: Jonathan Lemon <jonathan.lemon@gmail.com>
> ---
>   drivers/ptp/ptp_ocp.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
> index dd45471f6780..65e592ec272e 100644
> --- a/drivers/ptp/ptp_ocp.c
> +++ b/drivers/ptp/ptp_ocp.c
> @@ -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);

Maybe use set_normalized_timespec64 instead of this ugly transformations and 
additional variable?

>   		__ptp_ocp_settime_locked(bp, &ts);
>   	}
>   	spin_unlock_irqrestore(&bp->lock, flags);
Jonathan Lemon May 8, 2022, 4:55 a.m. UTC | #2
On Sat, May 07, 2022 at 01:19:54AM +0100, Vadim Fedorenko wrote:
> On 06.05.2022 00:40, Jonathan Lemon wrote:
> > 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.
> > 
> > Fixes: '90f8f4c0e3ce ("ptp: ocp: Add ptp_ocp_adjtime_coarse for large adjustments")'
> > Signed-off-by: Jonathan Lemon <jonathan.lemon@gmail.com>
> > ---
> >   drivers/ptp/ptp_ocp.c | 6 ++++--
> >   1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
> > index dd45471f6780..65e592ec272e 100644
> > --- a/drivers/ptp/ptp_ocp.c
> > +++ b/drivers/ptp/ptp_ocp.c
> > @@ -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);
> 
> Maybe use set_normalized_timespec64 instead 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.
Vadim Fedorenko May 9, 2022, 1:22 a.m. UTC | #3
On 08.05.2022 05:55, Jonathan Lemon wrote:
> On Sat, May 07, 2022 at 01:19:54AM +0100, Vadim Fedorenko wrote:
>> On 06.05.2022 00:40, Jonathan Lemon wrote:
>>> 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.
>>>
>>> Fixes: '90f8f4c0e3ce ("ptp: ocp: Add ptp_ocp_adjtime_coarse for large adjustments")'
>>> Signed-off-by: Jonathan Lemon <jonathan.lemon@gmail.com>
>>> ---
>>>    drivers/ptp/ptp_ocp.c | 6 ++++--
>>>    1 file changed, 4 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
>>> index dd45471f6780..65e592ec272e 100644
>>> --- a/drivers/ptp/ptp_ocp.c
>>> +++ b/drivers/ptp/ptp_ocp.c
>>> @@ -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);
>>
>> Maybe use set_normalized_timespec64 instead 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.

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.
diff mbox series

Patch

diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
index dd45471f6780..65e592ec272e 100644
--- a/drivers/ptp/ptp_ocp.c
+++ b/drivers/ptp/ptp_ocp.c
@@ -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);