stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	alan@lxorguk.ukuu.org.uk,
	Martin Schwidefsky <schwidefsky@de.ibm.com>,
	Heiko Carstens <heiko.carstens@de.ibm.com>
Subject: [ 05/21] s390/time: fix sched_clock() overflow
Date: Fri, 18 Jan 2013 17:19:23 -0800	[thread overview]
Message-ID: <20130119010712.231782774@linuxfoundation.org> (raw)
In-Reply-To: <20130119010710.897187774@linuxfoundation.org>

3.4-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Heiko Carstens <heiko.carstens@de.ibm.com>

commit ed4f20943cd4c7b55105c04daedf8d63ab6d499c upstream.

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().

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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 arch/s390/include/asm/timex.h |   28 ++++++++++++++++++++++++++++
 arch/s390/kernel/time.c       |    2 +-
 arch/s390/kvm/interrupt.c     |    2 +-
 3 files changed, 30 insertions(+), 2 deletions(-)

--- a/arch/s390/include/asm/timex.h
+++ b/arch/s390/include/asm/timex.h
@@ -137,4 +137,32 @@ static inline unsigned long long get_clo
 	return get_clock_xt() - sched_clock_base_cc;
 }
 
+/**
+ * tod_to_ns - convert a TOD format value to nanoseconds
+ * @todval: to be converted TOD format value
+ * Returns: number of nanoseconds that correspond to the TOD format value
+ *
+ * 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:
+ *
+ *    ns = (todval * 125) >> 9;
+ *
+ * In order to avoid an overflow with the multiplication we can rewrite this.
+ * With a split todval == 2^32 * th + tl (th upper 32 bits, tl lower 32 bits)
+ * we end up with
+ *
+ *    ns = ((2^32 * th + tl) * 125 ) >> 9;
+ * -> ns = (2^23 * th * 125) + ((tl * 125) >> 9);
+ *
+ */
+static inline unsigned long long tod_to_ns(unsigned long long todval)
+{
+	unsigned long long ns;
+
+	ns = ((todval >> 32) << 23) * 125;
+	ns += ((todval & 0xffffffff) * 125) >> 9;
+	return ns;
+}
+
 #endif
--- a/arch/s390/kernel/time.c
+++ b/arch/s390/kernel/time.c
@@ -64,7 +64,7 @@ static DEFINE_PER_CPU(struct clock_event
  */
 unsigned long long notrace __kprobes sched_clock(void)
 {
-	return (get_clock_monotonic() * 125) >> 9;
+	return tod_to_ns(get_clock_monotonic());
 }
 
 /*
--- a/arch/s390/kvm/interrupt.c
+++ b/arch/s390/kvm/interrupt.c
@@ -390,7 +390,7 @@ int kvm_s390_handle_wait(struct kvm_vcpu
 		return 0;
 	}
 
-	sltime = ((vcpu->arch.sie_block->ckc - now)*125)>>9;
+	sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
 
 	hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
 	VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);



  parent reply	other threads:[~2013-01-19  1:19 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-19  1:19 [ 00/21] 3.4.27-stable review Greg Kroah-Hartman
2013-01-19  1:19 ` [ 01/21] sh: Fix FDPIC binary loader Greg Kroah-Hartman
2013-01-19  1:19 ` [ 02/21] tcm_fc: Do not indicate retry capability to initiators Greg Kroah-Hartman
2013-01-19  1:19 ` [ 03/21] tcm_fc: Do not report target role when target is not defined Greg Kroah-Hartman
2013-01-19  1:19 ` [ 04/21] target: Release se_cmd when LUN lookup fails for TMR Greg Kroah-Hartman
2013-01-19  1:19 ` Greg Kroah-Hartman [this message]
2013-01-19  1:19 ` [ 06/21] x86/Sandy Bridge: reserve pages when integrated graphics is present Greg Kroah-Hartman
2013-01-19  1:19 ` [ 07/21] ALSA: usb - fix race in creation of M-Audio Fast track pro driver Greg Kroah-Hartman
2013-01-19  1:19 ` [ 08/21] ext4: init pagevec in ext4_da_block_invalidatepages Greg Kroah-Hartman
2013-01-19  1:19 ` [ 09/21] r8169: avoid NAPI scheduling delay Greg Kroah-Hartman
2013-01-19  1:19 ` [ 10/21] target: Add link_magic for fabric allow_link destination target_items Greg Kroah-Hartman
2013-01-19  4:13   ` Ben Hutchings
2013-01-19  1:19 ` [ 11/21] intel-iommu: Prevent devices with RMRRs from being placed into SI Domain Greg Kroah-Hartman
2013-01-19  1:19 ` [ 12/21] igb: release already assigned MSI-X interrupts if setup fails Greg Kroah-Hartman
2013-01-19  1:19 ` [ 13/21] drbd: add missing part_round_stats to _drbd_start_io_acct Greg Kroah-Hartman
2013-01-19  1:19 ` [ 14/21] xen/grant-table: correctly initialize grant table version 1 Greg Kroah-Hartman
2013-01-19  1:19 ` [ 15/21] xen: Fix stack corruption in xen_failsafe_callback for 32bit PVOPS guests Greg Kroah-Hartman
2013-01-19  1:19 ` [ 16/21] USB: option: add TP-LINK HSUPA Modem MA180 Greg Kroah-Hartman
2013-01-19  1:19 ` [ 17/21] USB: option: blacklist network interface on ONDA MT8205 4G LTE Greg Kroah-Hartman
2013-01-19  1:19 ` [ 18/21] serial:ifx6x60:Delete SPI timer when shut down port Greg Kroah-Hartman
2013-01-19  1:19 ` [ 19/21] tty: 8250_dw: Fix inverted arguments to serial_out in IRQ handler Greg Kroah-Hartman
2013-01-19  1:19 ` [ 20/21] staging: wlan-ng: Fix clamping of returned SSID length Greg Kroah-Hartman
2013-01-19  1:19 ` [ 21/21] staging: vt6656: Fix inconsistent structure packing Greg Kroah-Hartman
2013-01-19 18:51 ` [ 00/21] 3.4.27-stable review Shuah Khan
2013-01-20  9:04 ` Satoru Takeuchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130119010712.231782774@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=heiko.carstens@de.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=schwidefsky@de.ibm.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).