Skip to contentSkip to conversation

time: Fix casting issue in tk_set_xtime and tk_xtime_add

PATCH In review John Stultz 3 participants 4 messages Jul 23, 2012 – Aug 19, 2012 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 time: Fix casting issue in tk_set_xtime and tk_xtime_add

Fix missing casts that can cause boot problems on 32bit systems, most easily observed with Xen systems. This issue was introduced w/ 1e75fa8be9fb61e1af46b5b3b176347a4c958ca1.

---
 kernel/time/timekeeping.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timekeeping.c +2 -2
index f045cc5..cf364db 100644
@@ -108,13 +108,13 @@ static struct timespec tk_xtime(struct timekeeper *tk)
 static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts)
 {
 	tk->xtime_sec = ts->tv_sec;
-	tk->xtime_nsec = ts->tv_nsec << tk->shift;
+	tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift;
 }
 
 static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
 {
 	tk->xtime_sec += ts->tv_sec;
-	tk->xtime_nsec += ts->tv_nsec << tk->shift;
+	tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
 }
 
 /**
Signature · 1 line
1.7.9.5
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · John Stultz

Fix missing casts that can cause boot problems on 32bit systems, most easily observed with Xen systems. This issue was introduced w/ 1e75fa8be9fb61e1af46b5b3b176347a4c958ca1.

P.

#2 lore raw
In reply to John Stultz
Commit-ID:  b44d50dcacea0d485ca2ff9140f8cc28ee22f28d
Gitweb:     http://git.kernel.org/tip/b44d50dcacea0d485ca2ff9140f8cc28ee22f28d
Author:     John Stultz <[email protected]>
AuthorDate: Mon, 23 Jul 2012 16:22:37 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 24 Jul 2012 16:48:45 +0200

time: Fix casting issue in tk_set_xtime and tk_xtime_add

commit 1e75fa8b (time: Condense timekeeper.xtime into xtime_sec) introduced helper functions which apply a timespec to the core internal timekeeper data. The internal storage type is u64. The timespec tv_nsec value must be shifted before set or added to the internal value. tv_nsec is a long, which is 32bit on a 32bit system, so without casting tv_nsec to u64 we lose the bits which are shifted over the 32bit boundary.

Add the proper typecasts.

---
 kernel/time/timekeeping.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/time/timekeeping.c +2 -2
index 5980e90..8f2aba1 100644
@@ -108,13 +108,13 @@ static struct timespec tk_xtime(struct timekeeper *tk)
 static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts)
 {
 	tk->xtime_sec = ts->tv_sec;
-	tk->xtime_nsec = ts->tv_nsec << tk->shift;
+	tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift;
 }
 
 static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
 {
 	tk->xtime_sec += ts->tv_sec;
-	tk->xtime_nsec += ts->tv_nsec << tk->shift;
+	tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
 }
 
 /**
#3 lore raw

PATCH 1/1 time: Fix casting issue in timekeeping_forward_now

In reply to John Stultz

arch_gettimeoffset returns a u32 value which when shifted by tk->shift can overflow. Cast it to u64 first.

---
Found this by inspection, I don't know if any existing users of ARCH_USES_GETTIMEOFFSET are affected. In other words, this is untested, but looks pretty obvious.

Can tk->shift be bigger than 30? If so then the shifts in update_wall_time need to be adjusted as well.

Andreas.

---
 kernel/time/timekeeping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
index e16af19..8776d66 100644
@@ -276,7 +276,7 @@ static void timekeeping_forward_now(struct timekeeper *tk)
 	tk->xtime_nsec += cycle_delta * tk->mult;
 
 	/* If arch requires, add in gettimeoffset() */
-	tk->xtime_nsec += arch_gettimeoffset() << tk->shift;
+	tk->xtime_nsec += (u64)arch_gettimeoffset() << tk->shift;
 
 	tk_normalize_xtime(tk);
 
Signature · 6 lines
1.7.11.5

-- 
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."