Skip to content

Commit ed4f209

Browse files
heicarstMartin Schwidefsky
authored andcommitted
s390/time: fix sched_clock() overflow
Converting a 64 Bit TOD format value to nanoseconds means that the value must be divided by 4.096. In order to achieve that we multiply with 125 and divide by 512. When used within sched_clock() this triggers an overflow after appr. 417 days. Resulting in a sched_clock() return value that is much smaller than previously and therefore may cause all sort of weird things in subsystems that rely on a monotonic sched_clock() behaviour. To fix this implement a tod_to_ns() helper function which converts TOD values without overflow and call this function from both places that open coded the conversion: sched_clock() and kvm_s390_handle_wait(). Cc: stable@kernel.org Reviewed-by: Martin Schwidefsky <schwidefsky@de.ibm.com> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent 7bdc229 commit ed4f209

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

arch/s390/include/asm/timex.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,32 @@ static inline unsigned long long get_clock_monotonic(void)
128128
return get_clock_xt() - sched_clock_base_cc;
129129
}
130130

131+
/**
132+
* tod_to_ns - convert a TOD format value to nanoseconds
133+
* @todval: to be converted TOD format value
134+
* Returns: number of nanoseconds that correspond to the TOD format value
135+
*
136+
* Converting a 64 Bit TOD format value to nanoseconds means that the value
137+
* must be divided by 4.096. In order to achieve that we multiply with 125
138+
* and divide by 512:
139+
*
140+
* ns = (todval * 125) >> 9;
141+
*
142+
* In order to avoid an overflow with the multiplication we can rewrite this.
143+
* With a split todval == 2^32 * th + tl (th upper 32 bits, tl lower 32 bits)
144+
* we end up with
145+
*
146+
* ns = ((2^32 * th + tl) * 125 ) >> 9;
147+
* -> ns = (2^23 * th * 125) + ((tl * 125) >> 9);
148+
*
149+
*/
150+
static inline unsigned long long tod_to_ns(unsigned long long todval)
151+
{
152+
unsigned long long ns;
153+
154+
ns = ((todval >> 32) << 23) * 125;
155+
ns += ((todval & 0xffffffff) * 125) >> 9;
156+
return ns;
157+
}
158+
131159
#endif

arch/s390/kernel/time.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static DEFINE_PER_CPU(struct clock_event_device, comparators);
6363
*/
6464
unsigned long long notrace __kprobes sched_clock(void)
6565
{
66-
return (get_clock_monotonic() * 125) >> 9;
66+
return tod_to_ns(get_clock_monotonic());
6767
}
6868

6969
/*

arch/s390/kvm/interrupt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
408408
return 0;
409409
}
410410

411-
sltime = ((vcpu->arch.sie_block->ckc - now)*125)>>9;
411+
sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
412412

413413
hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
414414
VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);

0 commit comments

Comments
 (0)