From nobody Tue Feb 10 17:08:38 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D7B75D27E; Thu, 8 Jan 2026 17:58:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767895131; cv=none; b=ptKd24gq46b+zdmVYOQ4PI+0pLt08FHXbhzytlQYbboJOkaeLBZc9BJkMZko4rLcVr4lWhkNq8jxYWyZqfjQlMebYUGW4EvFPPBLX0TVVqs8sBjfJT+KllIGljUV8qIgGfMeUgnMQmL+6Xvo5FVao4j7GSvPmpgKUJqZHngN34s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767895131; c=relaxed/simple; bh=G0nxEsEsmQ1cbhrcW96b49pK99m48bXk0jVoLoffOGE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B0STH6vZUQ1WAjIrot1KYlYd6MvJPIEv5hGysZ+HlroKGPnK2DAFh4KS4oP+oRTZ5llnGoLfRceE9M24n/V66JcFEh1RJQwGVbU2KjdktesM0SXlYBPVtN2qmdE+rqJ/1JJfuR58Gc/CPh6A02jHYnfDJB0PBk6PzInoPiIyxB0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8662E497; Thu, 8 Jan 2026 09:58:42 -0800 (PST) Received: from ewhatever.cambridge.arm.com (ewhatever.cambridge.arm.com [10.1.197.1]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 7C9E73F5A1; Thu, 8 Jan 2026 09:58:46 -0800 (PST) From: Suzuki K Poulose To: kvmarm@lists.linux.dev Cc: kvm@vger.kernel.org, maz@kernel.org, will@kernel.org, oupton@kernel.org, aneesh.kumar@kernel.org, steven.price@arm.com, linux-kernel@vger.kernel.org, alexandru.elisei@arm.com, tabba@google.com, Suzuki K Poulose , Oliver Upton Subject: [kvmtool PATCH v5 01/15] Allow pausing the VM from vcpu thread Date: Thu, 8 Jan 2026 17:57:39 +0000 Message-ID: <20260108175753.1292097-2-suzuki.poulose@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260108175753.1292097-1-suzuki.poulose@arm.com> References: <20260108175753.1292097-1-suzuki.poulose@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pausing the VM from a vCPU thread doesn't work today, as it waits indefinit= ely for a signal that never comes. By using the "current_kvm_cpu", enlighten the kvm__pause() to skip the current CPU and do it inline. This also brings in a restriction that a following kvm__continue() must be called from the same v= CPU thread. Cc: Will Deacon Cc: Oliver Upton Link: https://lore.kernel.org/all/20230918104028.GA17744@willie-the-truck/ Reviewed-by: Marc Zyngier Signed-off-by: Suzuki K Poulose --- Changes since: v4 - Drop duplicate assignment of pause_req_cpu (Marc) --- kvm.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/kvm.c b/kvm.c index 07089cf1..7a52c6a8 100644 --- a/kvm.c +++ b/kvm.c @@ -59,6 +59,8 @@ const char *kvm_exit_reasons[] =3D { =20 static int pause_event; static DEFINE_MUTEX(pause_lock); +static struct kvm_cpu *pause_req_cpu; + extern struct kvm_ext kvm_req_ext[]; =20 static char kvm_dir[PATH_MAX]; @@ -573,9 +575,25 @@ void kvm__reboot(struct kvm *kvm) =20 void kvm__continue(struct kvm *kvm) { + /* + * We must ensure that the resume request comes from the same context + * as the one requested the pause, especially if it was issued from a + * vCPU thread. + */ + if (current_kvm_cpu) { + if (pause_req_cpu !=3D current_kvm_cpu || + !current_kvm_cpu->paused) + die("Trying to resume VM from invalid context"); + current_kvm_cpu->paused =3D 0; + } mutex_unlock(&pause_lock); } =20 +/* + * Mark all active CPUs as paused, until kvm__continue() is issued. + * NOTE: If this is called from a cpu thread, kvm__continue() must + * be called from the same thread. + */ void kvm__pause(struct kvm *kvm) { int i, paused_vcpus =3D 0; @@ -590,10 +608,16 @@ void kvm__pause(struct kvm *kvm) if (pause_event < 0) die("Failed creating pause notification event"); for (i =3D 0; i < kvm->nrcpus; i++) { - if (kvm->cpus[i]->is_running && kvm->cpus[i]->paused =3D=3D 0) - pthread_kill(kvm->cpus[i]->thread, SIGKVMPAUSE); - else - paused_vcpus++; + if (kvm->cpus[i]->is_running && kvm->cpus[i]->paused =3D=3D 0) { + if (current_kvm_cpu !=3D kvm->cpus[i]) { + pthread_kill(kvm->cpus[i]->thread, SIGKVMPAUSE); + continue; + } else if (current_kvm_cpu) { + current_kvm_cpu->paused =3D 1; + /* fall through to update our count */ + } + } + paused_vcpus++; } =20 while (paused_vcpus < kvm->nrcpus) { @@ -604,6 +628,8 @@ void kvm__pause(struct kvm *kvm) paused_vcpus +=3D cur_read; } close(pause_event); + /* Remember the context requesting pause */ + pause_req_cpu =3D current_kvm_cpu; } =20 void kvm__notify_paused(void) --=20 2.43.0