From patchwork Thu Oct 25 17:20:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emilio Cota X-Patchwork-Id: 989230 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=braap.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=braap.org header.i=@braap.org header.b="r1gqXHa3"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="P80YzKfc"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42gv8p632nz9sCV for ; Fri, 26 Oct 2018 04:23:06 +1100 (AEDT) Received: from localhost ([::1]:56019 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFjLc-0006jF-44 for incoming@patchwork.ozlabs.org; Thu, 25 Oct 2018 13:23:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40351) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFjKh-0006ht-93 for qemu-devel@nongnu.org; Thu, 25 Oct 2018 13:22:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gFjKc-0000Mn-Ja for qemu-devel@nongnu.org; Thu, 25 Oct 2018 13:22:05 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:36331) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gFjKb-0000CB-RV for qemu-devel@nongnu.org; Thu, 25 Oct 2018 13:22:01 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 6D509221F5; Thu, 25 Oct 2018 13:21:17 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 25 Oct 2018 13:21:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h= from:to:cc:subject:date:message-id:in-reply-to:references; s= mesmtp; bh=sI6lJztZK5hJVK1lfweSOxsqopisVE0RPdjo68zG7tk=; b=r1gqX Ha32nSV8ypr7AT+gRoZGv+QFij8IxR6iRkwGYzAfEyA5iFd3dYP28PQVl5cdNd3F wbuE5ImqOTTCvTx74ntlcTlHOkIzfGqsQr2B0yrcmJ/NFj2IUOjYI4cuEw3Jal5u I7IadLI/2TXh8LrtmkJBLp5PmFaF/OoUCHgKkY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=sI6lJztZK5hJVK1lfweSOxsqopisV E0RPdjo68zG7tk=; b=P80YzKfcwJj+qGzCrUnfOpEOHYg7eKqkSiWHTaLHOGxGP FTrrxKIIl4K1KKa7hTNAy2878rjJ8g97VPGwj0/Y0kGn/rDjq/pxRJ/byMzBpp/v le5zhWyiuwEDIWsKyYbRM5AJR8lwCEVhrhCgFL+Ha3H/Af4VW7IxDAbZT0ywcqOL j5BJmjYt1zd3/ZaOh2HyMc5HRxMZT7EWfzKu+zqTSYbQyk14oZGFpP5w/N17u54I p9Xtc25AoTlwC3GU03Jg6yd5x2lsGb6D1HJLMXA4lCAhIYewJxmrFgFhqoJXBjZS mOYrOT0Zu2jQUiRxzPgqflPVLo10eC00x1NkLSuwA== X-ME-Sender: X-ME-Proxy: Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id F3A7AE455F; Thu, 25 Oct 2018 13:21:16 -0400 (EDT) From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Thu, 25 Oct 2018 13:20:53 -0400 Message-Id: <20181025172057.20414-45-cota@braap.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181025172057.20414-1-cota@braap.org> References: <20181025172057.20414-1-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.27 Subject: [Qemu-devel] [RFC 44/48] cpus: lockstep execution support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , =?utf-8?q?Alex_Benn=C3=A9e?= , =?utf-8?q?Llu=C3=ADs_Vilanova?= , Pavel Dovgalyuk , Stefan Hajnoczi Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Emilio G. Cota --- include/qom/cpu.h | 27 +++++++++++ cpus.c | 113 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/include/qom/cpu.h b/include/qom/cpu.h index 1ac56fe84b..5841421a20 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -279,6 +279,12 @@ typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data); struct qemu_work_item; +enum cpu_lockstep { + CPU_LOCKSTEP_RUN, + CPU_LOCKSTEP_STOP_REQUEST, + CPU_LOCKSTEP_WAIT, +}; + #define CPU_UNSET_NUMA_NODE_ID -1 #define CPU_TRACE_DSTATE_MAX_EVENTS 32 @@ -364,6 +370,7 @@ struct CPUState { QemuCond halt_cond; QSIMPLEQ_HEAD(, qemu_work_item) work_list; uint32_t halted; + enum cpu_lockstep lockstep; bool created; bool stop; bool stopped; @@ -1019,6 +1026,26 @@ static inline void cpu_interrupt(CPUState *cpu, int mask) cpu_interrupt_handler(cpu, mask); } +/** + * cpu_lockstep_enable - Enable execution of CPUs in lockstep + * + * Note: this feature is MTTCG-only. + * Lockstep execution allows CPUs to partition their execution into windows + * whose start is synchronized with that of other CPUs. This can have many + * uses, e.g. limiting execution skew in the guest. + * + * See also: cpu_lockstep_request_stop() + */ +void cpu_lockstep_enable(void); + +/** + * cpu_lockstep_request_stop - Finish the CPU's execution window + * @cpu: the CPU of interest + * + * See also: cpu_lockstep_enable() + */ +void cpu_lockstep_request_stop(CPUState *cpu); + #else /* USER_ONLY */ void cpu_interrupt(CPUState *cpu, int mask); diff --git a/cpus.c b/cpus.c index 3efe89354d..a446632a5c 100644 --- a/cpus.c +++ b/cpus.c @@ -80,6 +80,14 @@ int64_t max_advance; static QEMUTimer *throttle_timer; static unsigned int throttle_percentage; +/* lockstep execution */ +static bool lockstep_enabled; +static bool lockstep_ongoing_wakeup; +static QemuMutex lockstep_lock; +static int n_lockstep_running_cpus; +static int n_lockstep_cpus; +static CPUState **lockstep_cpus; + #define CPU_THROTTLE_PCT_MIN 1 #define CPU_THROTTLE_PCT_MAX 99 #define CPU_THROTTLE_TIMESLICE_NS 10000000 @@ -1174,6 +1182,11 @@ static bool cpu_can_run(CPUState *cpu) if (cpu_is_stopped(cpu)) { return false; } + if (lockstep_enabled && + (cpu->lockstep == CPU_LOCKSTEP_STOP_REQUEST || + cpu->lockstep == CPU_LOCKSTEP_WAIT)) { + return false; + } return true; } @@ -1246,6 +1259,7 @@ void qemu_init_cpu_loop(void) { qemu_init_sigbus(); qemu_mutex_init(&qemu_global_mutex); + qemu_mutex_init(&lockstep_lock); qemu_thread_get_self(&io_thread); } @@ -1298,6 +1312,90 @@ static void qemu_wait_io_event_common(CPUState *cpu) cpu_mutex_lock(cpu); } +void cpu_lockstep_enable(void) +{ + atomic_xchg(&lockstep_enabled, true); +} + +void cpu_lockstep_request_stop(CPUState *cpu) +{ + bool locked = cpu_mutex_locked(cpu); + + g_assert(lockstep_enabled); + if (!locked) { + cpu_mutex_lock(cpu); + } + g_assert(cpu->lockstep == CPU_LOCKSTEP_RUN || + cpu->lockstep == CPU_LOCKSTEP_STOP_REQUEST); + cpu->lockstep = CPU_LOCKSTEP_STOP_REQUEST; + if (!locked) { + cpu_mutex_unlock(cpu); + } + cpu_exit(cpu); +} + +static void lockstep_resume(CPUState *cpu, run_on_cpu_data ignored) +{ + g_assert(lockstep_enabled); + cpu_mutex_lock(cpu); + g_assert(cpu->lockstep == CPU_LOCKSTEP_WAIT); + cpu->lockstep = CPU_LOCKSTEP_RUN; + cpu_mutex_unlock(cpu); +} + +static void lockstep_check_stop(CPUState *cpu) +{ + if (!lockstep_enabled) { + return; + } + if (cpu->lockstep == CPU_LOCKSTEP_STOP_REQUEST || + (cpu->lockstep == CPU_LOCKSTEP_RUN && cpu_thread_is_idle(cpu))) { + qemu_mutex_lock(&lockstep_lock); + cpu->lockstep = CPU_LOCKSTEP_WAIT; + n_lockstep_running_cpus--; + if (n_lockstep_running_cpus == 0) { + int i; + + /* wake up all waiting cpus */ + lockstep_ongoing_wakeup = true; + n_lockstep_running_cpus = n_lockstep_cpus; + qemu_mutex_unlock(&lockstep_lock); + cpu_mutex_unlock(cpu); + for (i = 0; i < n_lockstep_cpus; i++) { + run_on_cpu_no_bql(lockstep_cpus[i], lockstep_resume, + RUN_ON_CPU_NULL); + } + cpu_mutex_lock(cpu); + qemu_mutex_lock(&lockstep_lock); + lockstep_ongoing_wakeup = false; + } + qemu_mutex_unlock(&lockstep_lock); + } +} + +static void cpu_lockstep_init(CPUState *cpu) +{ + if (!lockstep_enabled) { + return; + } + qemu_mutex_lock(&lockstep_lock); + /* + * HACK: avoid racing with a wakeup, which would miss the addition + * of this CPU; just wait until no wakeup is ongoing. + */ + while (unlikely(lockstep_ongoing_wakeup)) { + qemu_mutex_unlock(&lockstep_lock); + sched_yield(); + qemu_mutex_lock(&lockstep_lock); + } + lockstep_cpus = g_realloc(lockstep_cpus, + (n_lockstep_cpus + 1) * sizeof(CPUState *)); + lockstep_cpus[n_lockstep_cpus++] = cpu; + n_lockstep_running_cpus++; + qemu_mutex_unlock(&lockstep_lock); + cpu->lockstep = CPU_LOCKSTEP_RUN; +} + static void qemu_tcg_rr_wait_io_event(CPUState *cpu) { g_assert(qemu_mutex_iothread_locked()); @@ -1321,6 +1419,15 @@ static void qemu_tcg_rr_wait_io_event(CPUState *cpu) cpu_mutex_unlock(cpu); } +static inline bool lockstep_is_waiting(CPUState *cpu) +{ + if (!lockstep_enabled) { + return true; + } + g_assert(cpu_mutex_locked(cpu)); + return cpu->lockstep == CPU_LOCKSTEP_WAIT; +} + static void qemu_wait_io_event(CPUState *cpu) { bool asleep = false; @@ -1328,7 +1435,9 @@ static void qemu_wait_io_event(CPUState *cpu) g_assert(cpu_mutex_locked(cpu)); g_assert(!qemu_mutex_iothread_locked()); - while (cpu_thread_is_idle(cpu)) { + lockstep_check_stop(cpu); + + while (cpu_thread_is_idle(cpu) && lockstep_is_waiting(cpu)) { if (!asleep) { asleep = true; qemu_plugin_vcpu_idle_cb(cpu); @@ -1884,6 +1993,8 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) cpu->can_do_io = 1; current_cpu = cpu; qemu_cond_signal(&cpu->cond); + /* init lockstep */ + cpu_lockstep_init(cpu); /* process any pending work */ cpu->exit_request = 1;