qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 14/17] icount: prepare the code for future races in calling qemu_clock_warp
Date: Thu, 17 Oct 2013 17:48:52 +0200	[thread overview]
Message-ID: <1382024935-28297-15-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1382024935-28297-1-git-send-email-pbonzini@redhat.com>

Computing the deadline of all vm_clocks is somewhat expensive and calls
out to qemu-timer.c; two reasons not to do it in the seqlock's write-side
critical section.  This however opens the door for races in setting and
reading vm_clock_warp_start.

To plug them, we need to cover the case where a new deadline slips in
between the call to qemu_clock_deadline_ns_all and the actual modification
of the icount_warp_timer.  Restrict changes to vm_clock_warp_start and
the icount_warp_timer's expiration time, to only move them back (which
would simply cause an early wakeup).

If a vm_clock timer is cancelled while CPUs are idle, this might cause the
icount_warp_timer to fire unnecessarily.  This is not a problem, after it
fires the timer becomes inactive and the next call to timer_mod_anticipate
will be precise.

In addition to this, we must deactivate the icount_warp_timer _before_
checking whether CPUs are idle.  This way, if the "last" CPU becomes idle
during the call to timer_del we will still set up the icount_warp_timer.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 cpus.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/cpus.c b/cpus.c
index bc365b7..34d5e04 100644
--- a/cpus.c
+++ b/cpus.c
@@ -329,6 +329,7 @@ void qtest_clock_warp(int64_t dest)
 
 void qemu_clock_warp(QEMUClockType type)
 {
+    int64_t clock;
     int64_t deadline;
 
     /*
@@ -348,8 +349,8 @@ void qemu_clock_warp(QEMUClockType type)
      * the earliest QEMU_CLOCK_VIRTUAL timer.
      */
     icount_warp_rt(NULL);
-    if (!all_cpu_threads_idle() || !qemu_clock_has_timers(QEMU_CLOCK_VIRTUAL)) {
-        timer_del(icount_warp_timer);
+    timer_del(icount_warp_timer);
+    if (!all_cpu_threads_idle()) {
         return;
     }
 
@@ -358,17 +359,11 @@ void qemu_clock_warp(QEMUClockType type)
 	return;
     }
 
-    vm_clock_warp_start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
     /* We want to use the earliest deadline from ALL vm_clocks */
+    clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
     deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
-
-    /* Maintain prior (possibly buggy) behaviour where if no deadline
-     * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
-     * INT32_MAX nanoseconds ahead, we still use INT32_MAX
-     * nanoseconds.
-     */
-    if ((deadline < 0) || (deadline > INT32_MAX)) {
-        deadline = INT32_MAX;
+    if (deadline < 0) {
+        return;
     }
 
     if (deadline > 0) {
@@ -389,7 +384,10 @@ void qemu_clock_warp(QEMUClockType type)
          * you will not be sending network packets continuously instead of
          * every 100ms.
          */
-        timer_mod(icount_warp_timer, vm_clock_warp_start + deadline);
+        if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
+            vm_clock_warp_start = clock;
+        }
+        timer_mod_anticipate(icount_warp_timer, clock + deadline);
     } else if (deadline == 0) {
         qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
     }
-- 
1.8.3.1

  parent reply	other threads:[~2013-10-17 15:49 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-17 15:48 [Qemu-devel] [PULL 00/17] Memory/threading changes for 1.7 Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 01/17] memory: fix 128 arithmetic in info mtree Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 02/17] compatfd: switch to QemuThread Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 03/17] portio: Allow to mark portio lists as coalesced MMIO flushing Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 04/17] cirrus: Mark vga io region " Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 05/17] vga: Mark relevant portio lists regions " Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 06/17] seqlock: introduce read-write seqlock Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 07/17] timer: protect timers_state's clock with seqlock Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 08/17] qemu-thread: add QemuEvent Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 09/17] timer: make qemu_clock_enable sync between disable and timer's cb Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 10/17] timer: extract timer_mod_ns_locked and timerlist_rearm Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 11/17] timer: add timer_mod_anticipate and timer_mod_anticipate_ns Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 12/17] icount: use cpu_get_icount() directly Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 13/17] icount: reorganize icount_warp_rt Paolo Bonzini
2013-10-17 15:48 ` Paolo Bonzini [this message]
2013-10-17 15:48 ` [Qemu-devel] [PULL 15/17] icount: document (future) locking rules for icount Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 16/17] icount: make it thread-safe Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 17/17] exec: remove qemu_safe_ram_ptr Paolo Bonzini

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=1382024935-28297-15-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.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).