From patchwork Fri May 5 10:38:16 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: 98610 Delivered-To: patch@linaro.org Received: by 10.140.96.100 with SMTP id j91csp49808qge; Fri, 5 May 2017 03:39:38 -0700 (PDT) X-Received: by 10.237.53.124 with SMTP id b57mr39644266qte.240.1493980778829; Fri, 05 May 2017 03:39:38 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id j12si3857654qta.322.2017.05.05.03.39.38 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 05 May 2017 03:39:38 -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]:46171 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6ae8-0002p2-CZ for patch@linaro.org; Fri, 05 May 2017 06:39:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52547) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6acO-0001kK-Oe for qemu-devel@nongnu.org; Fri, 05 May 2017 06:37:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6acM-0006ht-PS for qemu-devel@nongnu.org; Fri, 05 May 2017 06:37:48 -0400 Received: from mail-wr0-x236.google.com ([2a00:1450:400c:c0c::236]:35806) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d6acM-0006hP-Ii for qemu-devel@nongnu.org; Fri, 05 May 2017 06:37:46 -0400 Received: by mail-wr0-x236.google.com with SMTP id z52so1382916wrc.2 for ; Fri, 05 May 2017 03:37:46 -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=J7pbJPElMpl2w4dxyhLtwY0a9V8tQ9I8PPeYNnuqM4o=; b=fTRnoMWkv2fUcG7c4F9/wxpVxvPbMnzLtdvJ0xeGfliOM+eoUH0fKSknw2M3xGUlNa k9hFq18efmdVTNQ+1QM1n95fO/tUjFaYky8tMgxYDdpR3lzuQxoZGv7482V/F7aoh8Cc 800Cl2WdM6IpIDUi442HIpbyoKeiYYpZkzkyo= 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=J7pbJPElMpl2w4dxyhLtwY0a9V8tQ9I8PPeYNnuqM4o=; b=W1aA9Vr9l3owLxHEENIeToQRLm9CtS3gKr085zuNUWEU229ysi+dkPIWsLz+qG3x8Z /HqiEyPgtRCCDNQxnvsmqaabziTgzlCCwiOeiOs3QICuMtF5SJtBFCR1u1lMSQXrbgHZ qeIDdem+1mNjLRPQm58+2igJeNG8TdkVXyrE9aH6LGYz7rZ/cW+kcEKaJKvOQYR4DDRL 7sjwMhPKU9CiQzs9uB8csR8NRyJhI0QfrrD+nupGoRRpDJBQNSM3OkmO4Wdn0ATcZ+Sy vY0PC/qX6XGD8qXvEWtMKfHGTHa7SvGgA8SPwJnb7FBlozj+271HBI9E4jVxyKx2Yosf wH7Q== X-Gm-Message-State: AN3rC/5dhr83VCuDp+OQWSGyA5mcyOz4I3LDAkGqxgUOJEaRTLwEQMnq ioBIoohJlr0wHGvd X-Received: by 10.223.169.34 with SMTP id u31mr36586826wrc.46.1493980665456; Fri, 05 May 2017 03:37:45 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id 184sm1733953wmr.30.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 DFA8F3E01BB; 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:16 +0100 Message-Id: <20170505103822.20641-4-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:c0c::236 Subject: [Qemu-devel] [RFC PATCH v1 3/9] cpus: only take BQL for sleeping threads 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: Richard Henderson , cota@braap.org, =?utf-8?q?Alex_Be?= =?utf-8?b?bm7DqWU=?= , qemu-devel@nongnu.org, Peter Crosthwaite Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Now the only real need to hold the BQL is for when we sleep on the cpu->halt conditional. The lock is actually dropped while the thread sleeps so the actual window for contention is pretty small. This also means we can remove the special case hack for exclusive work and simply declare that work no longer has an implicit BQL held. This isn't a major problem async work is generally only changing things in the context of its own vCPU. If it needs to work across vCPUs it should be using the exclusive mechanism or possibly taking the lock itself. Signed-off-by: Alex Bennée --- cpus-common.c | 13 +++++-------- cpus.c | 10 ++++------ 2 files changed, 9 insertions(+), 14 deletions(-) -- 2.11.0 diff --git a/cpus-common.c b/cpus-common.c index 59f751ecf9..64661c3193 100644 --- a/cpus-common.c +++ b/cpus-common.c @@ -310,6 +310,11 @@ void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, queue_work_on_cpu(cpu, wi); } +/* Work items run outside of the BQL. This is essential for avoiding a + * deadlock for exclusive work but also applies to non-exclusive work. + * If the work requires cross-vCPU changes then it should use the + * exclusive mechanism. + */ void process_queued_cpu_work(CPUState *cpu) { struct qemu_work_item *wi; @@ -327,17 +332,9 @@ void process_queued_cpu_work(CPUState *cpu) } qemu_mutex_unlock(&cpu->work_mutex); if (wi->exclusive) { - /* Running work items outside the BQL avoids the following deadlock: - * 1) start_exclusive() is called with the BQL taken while another - * CPU is running; 2) cpu_exec in the other CPU tries to takes the - * BQL, so it goes to sleep; start_exclusive() is sleeping too, so - * neither CPU can proceed. - */ - qemu_mutex_unlock_iothread(); start_exclusive(); wi->func(cpu, wi->data); end_exclusive(); - qemu_mutex_lock_iothread(); } else { wi->func(cpu, wi->data); } diff --git a/cpus.c b/cpus.c index 89ae8cb30a..df279dd320 100644 --- a/cpus.c +++ b/cpus.c @@ -1069,31 +1069,29 @@ 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)) { + qemu_mutex_lock_iothread(); stop_tcg_kick_timer(); qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); + qemu_mutex_unlock_iothread(); } 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_mutex_lock_iothread(); qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); + qemu_mutex_unlock_iothread(); } qemu_wait_io_event_common(cpu); - - qemu_mutex_unlock_iothread(); } static void *qemu_kvm_cpu_thread_fn(void *arg)