Skip to contentSkip to conversation

fix potential jiffies overflow

PATCH In review Atsushi Nemoto 4 participants 8 messages Mar 2, 2006 – Mar 5, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix potential jiffies overflow

I found i386 timer_resume is updating jiffies, not jiffies_64. It looks there is a potential overflow problem. Is this a correct fix?

arch/i386/kernel/time.c +1 -1
index a14d594..e4ed172 100644
@@ -413,7 +413,7 @@ static int timer_resume(struct sys_devic
 	xtime.tv_sec = sec;
 	xtime.tv_nsec = 0;
 	write_sequnlock_irqrestore(&xtime_lock, flags);
-	jiffies += sleep_length;
+	jiffies_64 += sleep_length;
 	wall_jiffies += sleep_length;
 	if (last_timer->resume)
 		last_timer->resume();
#1 lore raw
Quoted 10 lines · Atsushi Nemoto

I found i386 timer_resume is updating jiffies, not jiffies_64. It looks there is a potential overflow problem. Is this a correct fix?

arch/i386/kernel/time.c +0 -0
index a14d594..e4ed172 100644

arch/i386/kernel/time.c static int timer_resume(struct sys_devic · line 419

Quoted 8 lines
arch/i386/kernel/time.c +1 -1
@@ -413,7 +413,7 @@ static int timer_resume(struct sys_devic
        xtime.tv_sec = sec;
        xtime.tv_nsec = 0;
        write_sequnlock_irqrestore(&xtime_lock, flags);
-       jiffies += sleep_length;
+       jiffies_64 += sleep_length;
        wall_jiffies += sleep_length;
        if (last_timer->resume)
                last_timer->resume();

The 64-bit jiffies value is not atomic. You need to hold xtime_lock to read it.

#2 lore raw
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line · Ram Gupta

I found i386 timer_resume is updating jiffies, not jiffies_64. It looks there is a potential overflow problem. Is this a correct fix?

ram> The 64-bit jiffies value is not atomic. You need to hold ram> xtime_lock to read it.

OK, and I guess wall_jiffies also needs xtime_lock.

I found i386 timer_resume is updating jiffies, not jiffies_64. It looks there is a potential overflow problem. And jiffies_64 and wall_jiffies should be protected by xtime_lock.

arch/i386/kernel/time.c +2 -2
index a14d594..9d30747 100644
@@ -412,9 +412,9 @@ static int timer_resume(struct sys_devic
 	write_seqlock_irqsave(&xtime_lock, flags);
 	xtime.tv_sec = sec;
 	xtime.tv_nsec = 0;
-	write_sequnlock_irqrestore(&xtime_lock, flags);
-	jiffies += sleep_length;
+	jiffies_64 += sleep_length;
 	wall_jiffies += sleep_length;
+	write_sequnlock_irqrestore(&xtime_lock, flags);
 	if (last_timer->resume)
 		last_timer->resume();
 	cur_timer = last_timer;
#3 lore raw
Quoted 24 lines · Atsushi Nemoto
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · Ram Gupta

I found i386 timer_resume is updating jiffies, not jiffies_64. It looks there is a potential overflow problem. Is this a correct fix?

ram> The 64-bit jiffies value is not atomic. You need to hold ram> xtime_lock to read it.

OK, and I guess wall_jiffies also needs xtime_lock.

I found i386 timer_resume is updating jiffies, not jiffies_64. It looks there is a potential overflow problem. And jiffies_64 and wall_jiffies should be protected by xtime_lock.

arch/i386/kernel/time.c +0 -0
index a14d594..9d30747 100644
@@ -412,9 +412,9 @@ static int timer_resume(struct sys_devic
 	write_seqlock_irqsave(&xtime_lock, flags);

arch/i386/kernel/time.c static int timer_resume(struct sys_devic · line 420

Quoted 10 lines
arch/i386/kernel/time.c +2 -2
 	xtime.tv_sec = sec;
 	xtime.tv_nsec = 0;
-	write_sequnlock_irqrestore(&xtime_lock, flags);
-	jiffies += sleep_length;
+	jiffies_64 += sleep_length;
 	wall_jiffies += sleep_length;
+	write_sequnlock_irqrestore(&xtime_lock, flags);
 	if (last_timer->resume)
 		last_timer->resume();
 	cur_timer = last_timer;

Thanks, that looks like 2.6.16 material.

What happens if the machine slept for more than 49.7 days?

#4 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Andrew Morton

akpm> Thanks, that looks like 2.6.16 material.

akpm> What happens if the machine slept for more than 49.7 days?

Well, jiffies will lose 49.7 days... Then, how about this? We can sleep 136 years.

arch/i386/kernel/time.c +2 -2
index a14d594..be5d079 100644
@@ -400,7 +400,7 @@ static int timer_resume(struct sys_devic
 {
 	unsigned long flags;
 	unsigned long sec;
-	unsigned long sleep_length;
+	u64 sleep_length;
 
 #ifdef CONFIG_HPET_TIMER
 	if (is_hpet_enabled())
@@ -408,7 +408,7 @@ static int timer_resume(struct sys_devic
 #endif
 	setup_pit_timer();
 	sec = get_cmos_time() + clock_cmos_diff;
-	sleep_length = (get_cmos_time() - sleep_start) * HZ;
+	sleep_length = (u64)(get_cmos_time() - sleep_start) * HZ;
 	write_seqlock_irqsave(&xtime_lock, flags);
 	xtime.tv_sec = sec;
 	xtime.tv_nsec = 0;
#5 lore raw
Quoted 25 lines · Atsushi Nemoto
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · Andrew Morton

akpm> Thanks, that looks like 2.6.16 material.

akpm> What happens if the machine slept for more than 49.7 days?

Well, jiffies will lose 49.7 days... Then, how about this? We can sleep 136 years.

arch/i386/kernel/time.c +1 -1
index a14d594..be5d079 100644
@@ -400,7 +400,7 @@ static int timer_resume(struct sys_devic
 {
 	unsigned long flags;
 	unsigned long sec;
-	unsigned long sleep_length;
+	u64 sleep_length;
 
 #ifdef CONFIG_HPET_TIMER
 	if (is_hpet_enabled())

arch/i386/kernel/time.c static int timer_resume(struct sys_devic · line 414

Quoted 8 lines
arch/i386/kernel/time.c +1 -1
@@ -408,7 +408,7 @@ static int timer_resume(struct sys_devic
 #endif
 	setup_pit_timer();
 	sec = get_cmos_time() + clock_cmos_diff;
-	sleep_length = (get_cmos_time() - sleep_start) * HZ;
+	sleep_length = (u64)(get_cmos_time() - sleep_start) * HZ;
 	write_seqlock_irqsave(&xtime_lock, flags);
 	xtime.tv_sec = sec;
 	xtime.tv_nsec = 0;

but...

	wall_jiffies += sleep_length;

wall_jiffies is 32-bit.

#6 lore raw
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line · Andrew Morton

Well, jiffies will lose 49.7 days... Then, how about this? We can sleep 136 years.

akpm> but...

akpm> wall_jiffies += sleep_length;

akpm> wall_jiffies is 32-bit.

Yes ... and I think wall_jiffies can be removed completely (with update_times() cleanup). Of course it should not be 2.6.16 material.

---
Atsushi Nemoto

#7 lore raw
In reply to Atsushi Nemoto

arch/i386/kernel/time.c static int timer_resume(struct sys_devic · line 411

Quoted 4 lines · Atsushi Nemoto
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Andrew Morton
arch/i386/kernel/time.c +1 -1
 	sec = get_cmos_time() + clock_cmos_diff;
-	sleep_length = (get_cmos_time() - sleep_start) * HZ;
+	sleep_length = (u64)(get_cmos_time() - sleep_start) * HZ;

Calls to get_cmos_time() currently take an average of .5 seconds as it waits for a clock edge to go by. I've got some patches queueing up to eliminate this but it's probably worth caching the result here anyway.

Signature · 1 line
Mathematics is the supreme nostalgia of our time.