public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ktime_add_ns() may overflow on 32bit architectures
@ 2013-03-19 12:29 David Engraf
  2013-03-19 12:38 ` Eric Dumazet
  2013-04-08 20:20 ` John Stultz
  0 siblings, 2 replies; 5+ messages in thread
From: David Engraf @ 2013-03-19 12:29 UTC (permalink / raw)
  To: Thomas Gleixner, John Stultz; +Cc: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 989 bytes --]

Hello,

I've triggered an overflow when using ktime_add_ns() on a 32bit 
architecture not supporting CONFIG_KTIME_SCALAR.

When passing a very high value for u64 nsec, e.g. 7881299347898368000 
the do_div() function converts this value to seconds (7881299347) which 
is still to high to pass to the ktime_set() function as long. The result 
in my case is a negative value.

The problem on my system occurs in the tick-sched.c, 
tick_nohz_stop_sched_tick() when time_delta is set to 
timekeeping_max_deferment(). The check for time_delta < KTIME_MAX is 
valid, thus ktime_add_ns() is called with a too large value resulting in 
a negative expire value. This leads to an endless loop in the ticker code:

time_delta: 7881299347898368000
expires = ktime_add_ns(last_update, time_delta)
expires: negative value

This error doesn't occurs on 64bit or architectures supporting 
CONFIG_KTIME_SCALAR (e.g. ARM, x86-32).

Best regards
- David

Signed-off-by: David Engraf <david.engraf@sysgo.com>


[-- Attachment #2: ktime_add_ns.patch --]
[-- Type: text/x-diff, Size: 425 bytes --]

diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c
index cc47812..320a7aa 100644
--- a/kernel/hrtimer.c
+++ b/kernel/hrtimer.c
@@ -275,6 +275,10 @@ ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
 	} else {
 		unsigned long rem = do_div(nsec, NSEC_PER_SEC);
 
+		/* Make sure nsec fits into long */
+		if (unlikely(nsec > KTIME_SEC_MAX))
+			return (ktime_t){ .tv64 = KTIME_MAX };
+
 		tmp = ktime_set((long)nsec, rem);
 	}
 

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ktime_add_ns() may overflow on 32bit architectures
  2013-03-19 12:29 [PATCH] ktime_add_ns() may overflow on 32bit architectures David Engraf
@ 2013-03-19 12:38 ` Eric Dumazet
  2013-03-19 12:53   ` David Engraf
  2013-04-08 20:20 ` John Stultz
  1 sibling, 1 reply; 5+ messages in thread
From: Eric Dumazet @ 2013-03-19 12:38 UTC (permalink / raw)
  To: David Engraf; +Cc: Thomas Gleixner, John Stultz, linux-kernel

On Tue, 2013-03-19 at 13:29 +0100, David Engraf wrote:
> Hello,
> 
> I've triggered an overflow when using ktime_add_ns() on a 32bit 
> architecture not supporting CONFIG_KTIME_SCALAR.
> 
> When passing a very high value for u64 nsec, e.g. 7881299347898368000 
> the do_div() function converts this value to seconds (7881299347) which 
> is still to high to pass to the ktime_set() function as long. The result 
> in my case is a negative value.
> 
> The problem on my system occurs in the tick-sched.c, 
> tick_nohz_stop_sched_tick() when time_delta is set to 
> timekeeping_max_deferment(). The check for time_delta < KTIME_MAX is 
> valid, thus ktime_add_ns() is called with a too large value resulting in 
> a negative expire value. This leads to an endless loop in the ticker code:
> 
> time_delta: 7881299347898368000
> expires = ktime_add_ns(last_update, time_delta)
> expires: negative value
> 
> This error doesn't occurs on 64bit or architectures supporting 
> CONFIG_KTIME_SCALAR (e.g. ARM, x86-32).
> 
> Best regards
> - David
> 
> Signed-off-by: David Engraf <david.engraf@sysgo.com>
> 

But check already exists for 64bit arches in ktime_set()




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ktime_add_ns() may overflow on 32bit architectures
  2013-03-19 12:38 ` Eric Dumazet
@ 2013-03-19 12:53   ` David Engraf
  0 siblings, 0 replies; 5+ messages in thread
From: David Engraf @ 2013-03-19 12:53 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: Thomas Gleixner, John Stultz, linux-kernel

Am 19.03.2013 13:38, schrieb Eric Dumazet:
> On Tue, 2013-03-19 at 13:29 +0100, David Engraf wrote:
>> Hello,
>>
>> I've triggered an overflow when using ktime_add_ns() on a 32bit
>> architecture not supporting CONFIG_KTIME_SCALAR.
>>
>> When passing a very high value for u64 nsec, e.g. 7881299347898368000
>> the do_div() function converts this value to seconds (7881299347) which
>> is still to high to pass to the ktime_set() function as long. The result
>> in my case is a negative value.
>>
>> The problem on my system occurs in the tick-sched.c,
>> tick_nohz_stop_sched_tick() when time_delta is set to
>> timekeeping_max_deferment(). The check for time_delta < KTIME_MAX is
>> valid, thus ktime_add_ns() is called with a too large value resulting in
>> a negative expire value. This leads to an endless loop in the ticker code:
>>
>> time_delta: 7881299347898368000
>> expires = ktime_add_ns(last_update, time_delta)
>> expires: negative value
>>
>> This error doesn't occurs on 64bit or architectures supporting
>> CONFIG_KTIME_SCALAR (e.g. ARM, x86-32).
>>
>> Best regards
>> - David
>>
>> Signed-off-by: David Engraf <david.engraf@sysgo.com>
>>
>
> But check already exists for 64bit arches in ktime_set()
>

Yes, but not for 32bit arches. 64-bit arches doesn't run into this 
problem because ktime_add_ns() can directly calculate the result without 
calling do_div() and ktime_set().


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ktime_add_ns() may overflow on 32bit architectures
  2013-03-19 12:29 [PATCH] ktime_add_ns() may overflow on 32bit architectures David Engraf
  2013-03-19 12:38 ` Eric Dumazet
@ 2013-04-08 20:20 ` John Stultz
  2013-04-09  7:08   ` David Engraf
  1 sibling, 1 reply; 5+ messages in thread
From: John Stultz @ 2013-04-08 20:20 UTC (permalink / raw)
  To: David Engraf; +Cc: Thomas Gleixner, linux-kernel

On 03/19/2013 05:29 AM, David Engraf wrote:
> Hello,
>
> I've triggered an overflow when using ktime_add_ns() on a 32bit 
> architecture not supporting CONFIG_KTIME_SCALAR.
>
> When passing a very high value for u64 nsec, e.g. 7881299347898368000 
> the do_div() function converts this value to seconds (7881299347) 
> which is still to high to pass to the ktime_set() function as long. 
> The result in my case is a negative value.
>
> The problem on my system occurs in the tick-sched.c, 
> tick_nohz_stop_sched_tick() when time_delta is set to 
> timekeeping_max_deferment(). The check for time_delta < KTIME_MAX is 
> valid, thus ktime_add_ns() is called with a too large value resulting 
> in a negative expire value. This leads to an endless loop in the 
> ticker code:
>
> time_delta: 7881299347898368000
> expires = ktime_add_ns(last_update, time_delta)
> expires: negative value
>
> This error doesn't occurs on 64bit or architectures supporting 
> CONFIG_KTIME_SCALAR (e.g. ARM, x86-32).

Sorry, this fell through the cracks. I see Andrew caught it, but I've 
queued for 3.10 in my tree as well.

This should be tagged for -stable as well, no?

thanks
-john


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ktime_add_ns() may overflow on 32bit architectures
  2013-04-08 20:20 ` John Stultz
@ 2013-04-09  7:08   ` David Engraf
  0 siblings, 0 replies; 5+ messages in thread
From: David Engraf @ 2013-04-09  7:08 UTC (permalink / raw)
  To: John Stultz; +Cc: Thomas Gleixner, linux-kernel

Am 08.04.2013 22:20, schrieb John Stultz:
> On 03/19/2013 05:29 AM, David Engraf wrote:
>> Hello,
>>
>> I've triggered an overflow when using ktime_add_ns() on a 32bit
>> architecture not supporting CONFIG_KTIME_SCALAR.
>>
>> When passing a very high value for u64 nsec, e.g. 7881299347898368000
>> the do_div() function converts this value to seconds (7881299347)
>> which is still to high to pass to the ktime_set() function as long.
>> The result in my case is a negative value.
>>
>> The problem on my system occurs in the tick-sched.c,
>> tick_nohz_stop_sched_tick() when time_delta is set to
>> timekeeping_max_deferment(). The check for time_delta < KTIME_MAX is
>> valid, thus ktime_add_ns() is called with a too large value resulting
>> in a negative expire value. This leads to an endless loop in the
>> ticker code:
>>
>> time_delta: 7881299347898368000
>> expires = ktime_add_ns(last_update, time_delta)
>> expires: negative value
>>
>> This error doesn't occurs on 64bit or architectures supporting
>> CONFIG_KTIME_SCALAR (e.g. ARM, x86-32).
>
> Sorry, this fell through the cracks. I see Andrew caught it, but I've
> queued for 3.10 in my tree as well.
>
> This should be tagged for -stable as well, no?

Yes, please tag it for -stable as well because I had the problem with 
kernel 3.0 and it can overflow on all current version.

Best regards
- David

> thanks
> -john
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-04-09  7:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-19 12:29 [PATCH] ktime_add_ns() may overflow on 32bit architectures David Engraf
2013-03-19 12:38 ` Eric Dumazet
2013-03-19 12:53   ` David Engraf
2013-04-08 20:20 ` John Stultz
2013-04-09  7:08   ` David Engraf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox