From patchwork Fri May 5 10:38:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 98614 Delivered-To: patch@linaro.org Received: by 10.140.96.100 with SMTP id j91csp50966qge; Fri, 5 May 2017 03:42:42 -0700 (PDT) X-Received: by 10.200.49.46 with SMTP id g43mr4977867qtb.229.1493980962144; Fri, 05 May 2017 03:42:42 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id g13si4478215qke.63.2017.05.05.03.42.41 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 05 May 2017 03:42:42 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:46185 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6ah5-0005lA-J8 for patch@linaro.org; Fri, 05 May 2017 06:42:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52552) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6acO-0001kO-Ph for qemu-devel@nongnu.org; Fri, 05 May 2017 06:37:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6acM-0006hT-1X for qemu-devel@nongnu.org; Fri, 05 May 2017 06:37:48 -0400 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]:38057) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d6acL-0006hE-OE for qemu-devel@nongnu.org; Fri, 05 May 2017 06:37:45 -0400 Received: by mail-wm0-x22b.google.com with SMTP id 142so2193782wma.1 for ; Fri, 05 May 2017 03:37:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0Ifc9M3/T3Gpinmp5zHYu47Xs9+QxkkkW2XJKkdvdvg=; b=I2NTTsZJdSVRZHD29TWVZi67AFTB5u7jyqTsEqMpQiUd/vq2EFPrdRr9ocOUMP+ppi rpHyfeiG9Drti7kLdcURFFwq2Zu95h5HClval39VcMDIBsbt2fNZFeN5AUBl57KJzVi9 aYsUEj/68MfS8j3TiIoiajSL0q0bpsEFEdWW4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0Ifc9M3/T3Gpinmp5zHYu47Xs9+QxkkkW2XJKkdvdvg=; b=cbW1ydOLK+HP9coQ2uVzGy6tlsdIULl6YaPbkVmho7TykbtedY7gR9wkUpSVqyAUpW 0Efh8DHbr1mKLpa/BAAK8l5k6I1m4xNwjjte+WOJCGhqo7C0p5XpILajlg9QbNDKi3LH Rm/mh+iqsoer8NXvVfJUsu6o5uuydnTwUptgG5qSNwqW/vEYpRN2H89m66nCf0Nf+m6B mWl3JtYxHe4SIlKV9VRfLdFdX1CHy4sDYeFddvRQdsEkKBXTynrvlD49vB8Zm/DB7eJq C5oyZGgJ7oFwcBoQ03eFylYF/hPxNiuPHh+4cVomnEy9M8iVr4wFzySLqSC9a4/MejKk wFmA== X-Gm-Message-State: AN3rC/6Wel5OWibvq6PDp0dhVk/lEyUotoR1LoeQ6r6onGFLyjejfciw 5w1I7eBfJsUXL2M6 X-Received: by 10.28.147.198 with SMTP id v189mr4806110wmd.52.1493980664588; Fri, 05 May 2017 03:37:44 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id j133sm1908331wmg.26.2017.05.05.03.37.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 May 2017 03:37:43 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id CDDA73E01B2; Fri, 5 May 2017 11:38:22 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: pbonzini@redhat.com, boost.lists@gmail.com, pavel.dovgaluk@ispras.ru Date: Fri, 5 May 2017 11:38:15 +0100 Message-Id: <20170505103822.20641-3-alex.bennee@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170505103822.20641-1-alex.bennee@linaro.org> References: <20170505103822.20641-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::22b Subject: [Qemu-devel] [RFC PATCH v1 2/9] cpus: push BQL lock to qemu_*_wait_io_event 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: Eduardo Habkost , "open list:Overall" , Peter Crosthwaite , qemu-devel@nongnu.org, cota@braap.org, =?utf-8?q?Alex_Benn=C3=A9e?= , Richard Henderson Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" We only really need to grab the lock for initial setup (so we don't race with the thread-spawning thread). After that we can drop the lock for the whole main loop and only grab it for waiting for IO events. There is a slight wrinkle for the round-robin TCG thread as we also expire timers which needs to be done under BQL as they are in the main-loop. This is stage one of reducing the lock impact as we can drop the requirement of implicit BQL for async work and only grab the lock when we need to sleep on the cpu->halt_cond. Signed-off-by: Alex Bennée --- cpus.c | 27 ++++++++++++++++++++------- kvm-all.c | 4 ---- target/i386/hax-all.c | 2 -- 3 files changed, 20 insertions(+), 13 deletions(-) -- 2.11.0 diff --git a/cpus.c b/cpus.c index 740b8dc3f8..89ae8cb30a 100644 --- a/cpus.c +++ b/cpus.c @@ -1069,6 +1069,8 @@ static bool qemu_tcg_should_sleep(CPUState *cpu) static void qemu_tcg_wait_io_event(CPUState *cpu) { + qemu_mutex_lock_iothread(); + while (qemu_tcg_should_sleep(cpu)) { stop_tcg_kick_timer(); qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); @@ -1077,15 +1079,21 @@ static void qemu_tcg_wait_io_event(CPUState *cpu) start_tcg_kick_timer(); qemu_wait_io_event_common(cpu); + + qemu_mutex_unlock_iothread(); } static void qemu_kvm_wait_io_event(CPUState *cpu) { + qemu_mutex_lock_iothread(); + while (cpu_thread_is_idle(cpu)) { qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); } qemu_wait_io_event_common(cpu); + + qemu_mutex_unlock_iothread(); } static void *qemu_kvm_cpu_thread_fn(void *arg) @@ -1111,6 +1119,8 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) /* signal CPU creation */ cpu->created = true; + qemu_mutex_unlock_iothread(); + qemu_cond_signal(&qemu_cpu_cond); do { @@ -1153,10 +1163,10 @@ static void *qemu_dummy_cpu_thread_fn(void *arg) /* signal CPU creation */ cpu->created = true; + qemu_mutex_unlock_iothread(); qemu_cond_signal(&qemu_cpu_cond); while (1) { - qemu_mutex_unlock_iothread(); do { int sig; r = sigwait(&waitset, &sig); @@ -1167,6 +1177,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg) } qemu_mutex_lock_iothread(); qemu_wait_io_event_common(cpu); + qemu_mutex_unlock_iothread(); } return NULL; @@ -1255,11 +1266,9 @@ static int tcg_cpu_exec(CPUState *cpu) #ifdef CONFIG_PROFILER ti = profile_getclock(); #endif - qemu_mutex_unlock_iothread(); cpu_exec_start(cpu); ret = cpu_exec(cpu); cpu_exec_end(cpu); - qemu_mutex_lock_iothread(); #ifdef CONFIG_PROFILER tcg_time += profile_getclock() - ti; #endif @@ -1318,6 +1327,7 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg) qemu_wait_io_event_common(cpu); } } + qemu_mutex_unlock_iothread(); start_tcg_kick_timer(); @@ -1327,6 +1337,9 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg) cpu->exit_request = 1; while (1) { + + qemu_mutex_lock_iothread(); + /* Account partial waits to QEMU_CLOCK_VIRTUAL. */ qemu_account_warp_timer(); @@ -1335,6 +1348,8 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg) */ handle_icount_deadline(); + qemu_mutex_unlock_iothread(); + if (!cpu) { cpu = first_cpu; } @@ -1360,9 +1375,7 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg) cpu_handle_guest_debug(cpu); break; } else if (r == EXCP_ATOMIC) { - qemu_mutex_unlock_iothread(); cpu_exec_step_atomic(cpu); - qemu_mutex_lock_iothread(); break; } } else if (cpu->stop) { @@ -1403,6 +1416,7 @@ static void *qemu_hax_cpu_thread_fn(void *arg) current_cpu = cpu; hax_init_vcpu(cpu); + qemu_mutex_unlock_iothread(); qemu_cond_signal(&qemu_cpu_cond); while (1) { @@ -1452,6 +1466,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) cpu->created = true; cpu->can_do_io = 1; current_cpu = cpu; + qemu_mutex_unlock_iothread(); qemu_cond_signal(&qemu_cpu_cond); /* process any pending work */ @@ -1476,9 +1491,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) g_assert(cpu->halted); break; case EXCP_ATOMIC: - qemu_mutex_unlock_iothread(); cpu_exec_step_atomic(cpu); - qemu_mutex_lock_iothread(); default: /* Ignore everything else? */ break; diff --git a/kvm-all.c b/kvm-all.c index 90b8573656..4cb5347205 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -1963,8 +1963,6 @@ int kvm_cpu_exec(CPUState *cpu) return EXCP_HLT; } - qemu_mutex_unlock_iothread(); - do { MemTxAttrs attrs; @@ -2093,8 +2091,6 @@ int kvm_cpu_exec(CPUState *cpu) } } while (ret == 0); - qemu_mutex_lock_iothread(); - if (ret < 0) { cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_CODE); vm_stop(RUN_STATE_INTERNAL_ERROR); diff --git a/target/i386/hax-all.c b/target/i386/hax-all.c index ef13015215..71f3c01977 100644 --- a/target/i386/hax-all.c +++ b/target/i386/hax-all.c @@ -513,9 +513,7 @@ static int hax_vcpu_hax_exec(CPUArchState *env) hax_vcpu_interrupt(env); - qemu_mutex_unlock_iothread(); hax_ret = hax_vcpu_run(vcpu); - qemu_mutex_lock_iothread(); current_cpu = cpu; /* Simply continue the vcpu_run if system call interrupted */