All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, "James Hogan" <james.hogan@imgtec.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>,
	"Ralf Baechle" <ralf@linux-mips.org>,
	linux-mips@linux-mips.org, kvm@vger.kernel.org
Subject: [PATCH 4.4 59/86] MIPS: KVM: Fix timer IRQ race when freezing timer
Date: Mon, 30 May 2016 13:49:48 -0700	[thread overview]
Message-ID: <20160530204939.407129729@linuxfoundation.org> (raw)
In-Reply-To: <20160530204937.379068148@linuxfoundation.org>

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

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

From: James Hogan <james.hogan@imgtec.com>

commit 4355c44f063d3de4f072d796604c7f4ba4085cc3 upstream.

There's a particularly narrow and subtle race condition when the
software emulated guest timer is frozen which can allow a guest timer
interrupt to be missed.

This happens due to the hrtimer expiry being inexact, so very
occasionally the freeze time will be after the moment when the emulated
CP0_Count transitions to the same value as CP0_Compare (so an IRQ should
be generated), but before the moment when the hrtimer is due to expire
(so no IRQ is generated). The IRQ won't be generated when the timer is
resumed either, since the resume CP0_Count will already match CP0_Compare.

With VZ guests in particular this is far more likely to happen, since
the soft timer may be frozen frequently in order to restore the timer
state to the hardware guest timer. This happens after 5-10 hours of
guest soak testing, resulting in an overflow in guest kernel timekeeping
calculations, hanging the guest. A more focussed test case to
intentionally hit the race (with the help of a new hypcall to cause the
timer state to migrated between hardware & software) hits the condition
fairly reliably within around 30 seconds.

Instead of relying purely on the inexact hrtimer expiry to determine
whether an IRQ should be generated, read the guest CP0_Compare and
directly check whether the freeze time is before or after it. Only if
CP0_Count is on or after CP0_Compare do we check the hrtimer expiry to
determine whether the last IRQ has already been generated (which will
have pushed back the expiry by one timer period).

Fixes: e30492bbe95a ("MIPS: KVM: Rewrite count/compare timer emulation")
Signed-off-by: James Hogan <james.hogan@imgtec.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Radim Krčmář" <rkrcmar@redhat.com>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: linux-mips@linux-mips.org
Cc: kvm@vger.kernel.org
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 arch/mips/kvm/emulate.c |   28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

--- a/arch/mips/kvm/emulate.c
+++ b/arch/mips/kvm/emulate.c
@@ -302,12 +302,31 @@ static inline ktime_t kvm_mips_count_tim
  */
 static uint32_t kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now)
 {
-	ktime_t expires;
+	struct mips_coproc *cop0 = vcpu->arch.cop0;
+	ktime_t expires, threshold;
+	uint32_t count, compare;
 	int running;
 
-	/* Is the hrtimer pending? */
+	/* Calculate the biased and scaled guest CP0_Count */
+	count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
+	compare = kvm_read_c0_guest_compare(cop0);
+
+	/*
+	 * Find whether CP0_Count has reached the closest timer interrupt. If
+	 * not, we shouldn't inject it.
+	 */
+	if ((int32_t)(count - compare) < 0)
+		return count;
+
+	/*
+	 * The CP0_Count we're going to return has already reached the closest
+	 * timer interrupt. Quickly check if it really is a new interrupt by
+	 * looking at whether the interval until the hrtimer expiry time is
+	 * less than 1/4 of the timer period.
+	 */
 	expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
-	if (ktime_compare(now, expires) >= 0) {
+	threshold = ktime_add_ns(now, vcpu->arch.count_period / 4);
+	if (ktime_before(expires, threshold)) {
 		/*
 		 * Cancel it while we handle it so there's no chance of
 		 * interference with the timeout handler.
@@ -329,8 +348,7 @@ static uint32_t kvm_mips_read_count_runn
 		}
 	}
 
-	/* Return the biased and scaled guest CP0_Count */
-	return vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
+	return count;
 }
 
 /**

  parent reply	other threads:[~2016-05-30 21:00 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-30 20:48 [PATCH 4.4 00/86] 4.4.12-stable review Greg Kroah-Hartman
2016-05-30 20:48 ` [PATCH 4.4 01/86] Btrfs: dont use src fd for printk Greg Kroah-Hartman
2016-05-30 20:48 ` [PATCH 4.4 02/86] perf/x86/intel/pt: Generate PMI in the STOP region as well Greg Kroah-Hartman
2016-05-30 20:48 ` [PATCH 4.4 03/86] perf/core: Fix perf_event_open() vs. execve() race Greg Kroah-Hartman
2016-05-30 20:48 ` [PATCH 4.4 05/86] ext4: iterate over buffer heads correctly in move_extent_per_page() Greg Kroah-Hartman
2016-05-30 20:48 ` [PATCH 4.4 06/86] arm64: Fix typo in the pmdp_huge_get_and_clear() definition Greg Kroah-Hartman
2016-05-30 20:48 ` [PATCH 4.4 07/86] arm64: Ensure pmd_present() returns false after pmd_mknotpresent() Greg Kroah-Hartman
2016-05-30 20:48 ` [PATCH 4.4 08/86] arm64: Implement ptep_set_access_flags() for hardware AF/DBM Greg Kroah-Hartman
2016-05-30 20:48 ` [PATCH 4.4 09/86] arm64: Implement pmdp_set_access_flags() " Greg Kroah-Hartman
2016-05-30 20:48 ` [PATCH 4.4 10/86] arm64: cpuinfo: Missing NULL terminator in compat_hwcap_str Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 11/86] arm/arm64: KVM: Enforce Break-Before-Make on Stage-2 page tables Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 12/86] kvm: arm64: Fix EC field in inject_abt64 Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 13/86] remove directory incorrectly tries to set delete on close on non-empty directories Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 14/86] fs/cifs: correctly to anonymous authentication via NTLMSSP Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 15/86] fs/cifs: correctly to anonymous authentication for the LANMAN authentication Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 16/86] fs/cifs: correctly to anonymous authentication for the NTLM(v1) authentication Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 17/86] fs/cifs: correctly to anonymous authentication for the NTLM(v2) authentication Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 18/86] asix: Fix offset calculation in asix_rx_fixup() causing slow transmissions Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 19/86] ring-buffer: Use long for nr_pages to avoid overflow failures Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 20/86] ring-buffer: Prevent overflow of size in ring_buffer_resize() Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 21/86] crypto: caam - fix caam_jr_alloc() ret code Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 22/86] crypto: talitos - fix ahash algorithms registration Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 23/86] crypto: sun4i-ss - Replace spinlock_bh by spin_lock_irq{save|restore} Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 24/86] clk: qcom: msm8916: Fix crypto clock flags Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 25/86] sched/loadavg: Fix loadavg artifacts on fully idle and on fully loaded systems Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 26/86] mfd: omap-usb-tll: Fix scheduling while atomic BUG Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 27/86] Input: pwm-beeper - fix - scheduling while atomic Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 28/86] irqchip/gic: Ensure ordering between read of INTACK and shared data Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 29/86] irqchip/gic-v3: Configure all interrupts as non-secure Group-1 Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 30/86] can: fix handling of unmodifiable configuration options Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 31/86] mmc: mmc: Fix partition switch timeout for some eMMCs Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 32/86] mmc: sdhci-acpi: Remove MMC_CAP_BUS_WIDTH_TEST for Intel controllers Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 33/86] ACPI / osi: Fix an issue that acpi_osi=!* cannot disable ACPICA internal strings Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 35/86] mmc: longer timeout for long read time quirk Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 36/86] mmc: sdhci-pci: Remove MMC_CAP_BUS_WIDTH_TEST for Intel controllers Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 37/86] Bluetooth: vhci: fix open_timeout vs. hdev race Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 38/86] Bluetooth: vhci: purge unhandled skbs Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 39/86] Bluetooth: vhci: Fix race at creating hci device Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 40/86] mei: fix NULL dereferencing during FW initiated disconnection Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 41/86] mei: amthif: discard not read messages Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 42/86] mei: bus: call mei_cl_read_start under device lock Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 43/86] USB: serial: mxuport: fix use-after-free in probe error path Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 44/86] USB: serial: keyspan: " Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 45/86] USB: serial: quatech2: " Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 46/86] USB: serial: io_edgeport: fix memory leaks in attach " Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 47/86] USB: serial: io_edgeport: fix memory leaks in probe " Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 48/86] USB: serial: option: add support for Cinterion PH8 and AHxx Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 49/86] USB: serial: option: add more ZTE device ids Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 50/86] USB: serial: option: add even " Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 51/86] usb: gadget: f_fs: Fix EFAULT generation for async read operations Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 53/86] usb: misc: usbtest: fix pattern tests for scatterlists Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 54/86] USB: leave LPM alone if possible when binding/unbinding interface drivers Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 55/86] usb: gadget: udc: core: Fix argument of dev_err() in usb_gadget_map_request() Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 56/86] staging: comedi: das1800: fix possible NULL dereference Greg Kroah-Hartman
2016-05-30 20:49 ` Greg Kroah-Hartman [this message]
2016-05-30 20:49 ` [PATCH 4.4 60/86] MIPS: KVM: Fix timer IRQ race when writing CP0_Compare Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 62/86] xen/x86: actually allocate legacy interrupts on PV guests Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 63/86] tty: vt, return error when con_startup fails Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 64/86] TTY: n_gsm, fix false positive WARN_ON Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 65/86] tty/serial: atmel: fix hardware handshake selection Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 66/86] Fix OpenSSH pty regression on close Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 68/86] serial: 8250_mid: use proper bar for DNV platform Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 69/86] serial: 8250_mid: recognize interrupt source in handler Greg Kroah-Hartman
2016-05-30 20:49 ` [PATCH 4.4 70/86] serial: samsung: Reorder the sequence of clock control when call s3c24xx_serial_set_termios() Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 71/86] locking,qspinlock: Fix spin_is_locked() and spin_unlock_wait() Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 72/86] clk: bcm2835: add locking to pll*_on/off methods Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 73/86] mcb: Fixed bar number assignment for the gdd Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 74/86] ALSA: hda/realtek - New codecs support for ALC234/ALC274/ALC294 Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 75/86] ALSA: hda - Fix headphone noise on Dell XPS 13 9360 Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 76/86] ALSA: hda/realtek - Add support for ALC295/ALC3254 Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 77/86] ALSA: hda - Fix headset mic detection problem for one Dell machine Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 78/86] IB/srp: Fix a debug kernel crash Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 79/86] thunderbolt: Fix double free of drom buffer Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 80/86] SIGNAL: Move generic copy_siginfo() to signal.h Greg Kroah-Hartman
2016-05-30 20:50   ` Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 81/86] UBI: Fix static volume checks when Fastmap is used Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 82/86] hpfs: fix remount failure when there are no options changed Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 83/86] hpfs: implement the show_options method Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 84/86] scsi: Add intermediate STARGET_REMOVE state to scsi_target_state Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 85/86] Revert "scsi: fix soft lockup in scsi_remove_target() on module removal" Greg Kroah-Hartman
2016-05-30 20:50 ` [PATCH 4.4 86/86] kbuild: move -Wunused-const-variable to W=1 warning level Greg Kroah-Hartman
2016-06-01 14:20 ` [PATCH 4.4 00/86] 4.4.12-stable review Shuah Khan

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=20160530204939.407129729@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=james.hogan@imgtec.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@linux-mips.org \
    --cc=pbonzini@redhat.com \
    --cc=ralf@linux-mips.org \
    --cc=rkrcmar@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.