linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff scheduled out while running
@ 2024-05-03 18:17 David Matlack
  2024-05-03 18:17 ` [PATCH v3 1/3] KVM: Introduce vcpu->wants_to_run David Matlack
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: David Matlack @ 2024-05-03 18:17 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kvm, David Hildenbrand, Paul Walmsley, David Matlack, linux-riscv,
	Claudio Imbrenda, Janosch Frank, Marc Zyngier, Huacai Chen,
	Zenghui Yu, Palmer Dabbelt, Christian Borntraeger, Albert Ou,
	Suzuki K Poulose, Nicholas Piggin, Bibo Mao, loongarch,
	Atish Patra, kvmarm, linux-arm-kernel, Sean Christopherson,
	linux-mips, Oliver Upton, James Morse, kvm-riscv, Anup Patel,
	Tianrui Zhao, linuxppc-dev

This series changes KVM to mark a vCPU as preempted/ready if-and-only-if
it's scheduled out while running. i.e. Do not mark a vCPU
preempted/ready if it's scheduled out during a non-KVM_RUN ioctl() or
when userspace is doing KVM_RUN with immediate_exit=true.

This is a logical extension of commit 54aa83c90198 ("KVM: x86: do not
set st->preempted when going back to user space"), which  stopped
marking a vCPU as preempted when returning to userspace. But if userspace
invokes a KVM vCPU ioctl() that gets preempted, the vCPU will be marked
preempted/ready. This is arguably incorrect behavior since the vCPU was
not actually preempted while the guest was running, it was preempted
while doing something on behalf of userspace.

In practice, this avoids KVM dirtying guest memory via the steal time
page after userspace has paused vCPUs, e.g. for Live Migration, which
allows userspace to collect the final dirty bitmap before or in parallel
with saving vCPU state without having to worry about saving vCPU state
triggering writes to guest memory.

Patch 1 introduces vcpu->wants_to_run to allow KVM to detect when a vCPU
is in its core run loop.

Patch 2 renames immediated_exit to immediated_exit__unsafe within KVM to
ensure that any new references get extra scrutiny.

Patch 3 perform leverages vcpu->wants_to_run to contrain when
vcpu->preempted and vcpu->ready are set.

v3:
 - Use READ_ONCE() to read immediate_exit [Sean]
 - Replace use of immediate_exit with !wants_to_run to avoid TOCTOU [Sean]
 - Hide/Rename immediate_exit in KVM to harden against TOCTOU bugs [Sean]

v2: https://lore.kernel.org/kvm/20240307163541.92138-1-dmatlack@google.com/
 - Drop Google-specific "PRODKERNEL: " shortlog prefix [me]

v1: https://lore.kernel.org/kvm/20231218185850.1659570-1-dmatlack@google.com/

David Matlack (3):
  KVM: Introduce vcpu->wants_to_run
  KVM: Ensure new code that references immediate_exit gets extra
    scrutiny
  KVM: Mark a vCPU as preempted/ready iff it's scheduled out while
    running

 arch/arm64/kvm/arm.c       |  2 +-
 arch/loongarch/kvm/vcpu.c  |  2 +-
 arch/mips/kvm/mips.c       |  2 +-
 arch/powerpc/kvm/powerpc.c |  2 +-
 arch/riscv/kvm/vcpu.c      |  2 +-
 arch/s390/kvm/kvm-s390.c   |  2 +-
 arch/x86/kvm/x86.c         |  4 ++--
 include/linux/kvm_host.h   |  1 +
 include/uapi/linux/kvm.h   | 15 ++++++++++++++-
 virt/kvm/kvm_main.c        |  5 ++++-
 10 files changed, 27 insertions(+), 10 deletions(-)


base-commit: 296655d9bf272cfdd9d2211d099bcb8a61b93037
-- 
2.45.0.rc1.225.g2a3ae87e7f-goog


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 1/3] KVM: Introduce vcpu->wants_to_run
  2024-05-03 18:17 [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff scheduled out while running David Matlack
@ 2024-05-03 18:17 ` David Matlack
  2024-05-03 18:17 ` [PATCH v3 2/3] KVM: Ensure new code that references immediate_exit gets extra scrutiny David Matlack
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: David Matlack @ 2024-05-03 18:17 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kvm, David Hildenbrand, Paul Walmsley, David Matlack, linux-riscv,
	Claudio Imbrenda, Janosch Frank, Marc Zyngier, Huacai Chen,
	Zenghui Yu, Palmer Dabbelt, Christian Borntraeger, Albert Ou,
	Suzuki K Poulose, Nicholas Piggin, Bibo Mao, loongarch,
	Atish Patra, kvmarm, linux-arm-kernel, Sean Christopherson,
	linux-mips, Oliver Upton, James Morse, kvm-riscv, Anup Patel,
	Tianrui Zhao, linuxppc-dev

Introduce vcpu->wants_to_run to indicate when a vCPU is in its core run
loop, i.e. when the vCPU is running the KVM_RUN ioctl and immediate_exit
was not set.

Replace all references to vcpu->run->immediate_exit with
!vcpu->wants_to_run to avoid TOCTOU races with userspace. For example, a
malicious userspace could invoked KVM_RUN with immediate_exit=true and
then after KVM reads it to set wants_to_run=false, flip it to false.
This would result in the vCPU running in KVM_RUN with
wants_to_run=false. This wouldn't cause any real bugs today but is a
dangerous landmine.

Signed-off-by: David Matlack <dmatlack@google.com>
---
 arch/arm64/kvm/arm.c       | 2 +-
 arch/loongarch/kvm/vcpu.c  | 2 +-
 arch/mips/kvm/mips.c       | 2 +-
 arch/powerpc/kvm/powerpc.c | 2 +-
 arch/riscv/kvm/vcpu.c      | 2 +-
 arch/s390/kvm/kvm-s390.c   | 2 +-
 arch/x86/kvm/x86.c         | 4 ++--
 include/linux/kvm_host.h   | 1 +
 virt/kvm/kvm_main.c        | 3 +++
 9 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index c4a0a35e02c7..c587e5d9396e 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -986,7 +986,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
 	vcpu_load(vcpu);
 
-	if (run->immediate_exit) {
+	if (!vcpu->wants_to_run) {
 		ret = -EINTR;
 		goto out;
 	}
diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
index 3a8779065f73..847ef54f3a84 100644
--- a/arch/loongarch/kvm/vcpu.c
+++ b/arch/loongarch/kvm/vcpu.c
@@ -1163,7 +1163,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 			kvm_complete_iocsr_read(vcpu, run);
 	}
 
-	if (run->immediate_exit)
+	if (!vcpu->wants_to_run)
 		return r;
 
 	/* Clear exit_reason */
diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c
index 231ac052b506..f1a99962027a 100644
--- a/arch/mips/kvm/mips.c
+++ b/arch/mips/kvm/mips.c
@@ -436,7 +436,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 		vcpu->mmio_needed = 0;
 	}
 
-	if (vcpu->run->immediate_exit)
+	if (!vcpu->wants_to_run)
 		goto out;
 
 	lose_fpu(1);
diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
index d32abe7fe6ab..961aadc71de2 100644
--- a/arch/powerpc/kvm/powerpc.c
+++ b/arch/powerpc/kvm/powerpc.c
@@ -1852,7 +1852,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
 	kvm_sigset_activate(vcpu);
 
-	if (run->immediate_exit)
+	if (!vcpu->wants_to_run)
 		r = -EINTR;
 	else
 		r = kvmppc_vcpu_run(vcpu);
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index b5ca9f2e98ac..3d8349470ee6 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -711,7 +711,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 		return ret;
 	}
 
-	if (run->immediate_exit) {
+	if (!vcpu->wants_to_run) {
 		kvm_vcpu_srcu_read_unlock(vcpu);
 		return -EINTR;
 	}
diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
index 5147b943a864..b1ea25aacbf9 100644
--- a/arch/s390/kvm/kvm-s390.c
+++ b/arch/s390/kvm/kvm-s390.c
@@ -5033,7 +5033,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 	if (vcpu->kvm->arch.pv.dumping)
 		return -EINVAL;
 
-	if (kvm_run->immediate_exit)
+	if (!vcpu->wants_to_run)
 		return -EINTR;
 
 	if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2d2619d3eee4..f70ae1558684 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11396,7 +11396,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
 	kvm_vcpu_srcu_read_lock(vcpu);
 	if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
-		if (kvm_run->immediate_exit) {
+		if (!vcpu->wants_to_run) {
 			r = -EINTR;
 			goto out;
 		}
@@ -11474,7 +11474,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 		WARN_ON_ONCE(vcpu->mmio_needed);
 	}
 
-	if (kvm_run->immediate_exit) {
+	if (!vcpu->wants_to_run) {
 		r = -EINTR;
 		goto out;
 	}
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index afbc99264ffa..f9b9ce0c3cd9 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -380,6 +380,7 @@ struct kvm_vcpu {
 		bool dy_eligible;
 	} spin_loop;
 #endif
+	bool wants_to_run;
 	bool preempted;
 	bool ready;
 	struct kvm_vcpu_arch arch;
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 38b498669ef9..bdea5b978f80 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4425,7 +4425,10 @@ static long kvm_vcpu_ioctl(struct file *filp,
 				synchronize_rcu();
 			put_pid(oldpid);
 		}
+		vcpu->wants_to_run = !READ_ONCE(vcpu->run->immediate_exit);
 		r = kvm_arch_vcpu_ioctl_run(vcpu);
+		vcpu->wants_to_run = false;
+
 		trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
 		break;
 	}
-- 
2.45.0.rc1.225.g2a3ae87e7f-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 2/3] KVM: Ensure new code that references immediate_exit gets extra scrutiny
  2024-05-03 18:17 [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff scheduled out while running David Matlack
  2024-05-03 18:17 ` [PATCH v3 1/3] KVM: Introduce vcpu->wants_to_run David Matlack
@ 2024-05-03 18:17 ` David Matlack
  2024-05-03 18:17 ` [PATCH v3 3/3] KVM: Mark a vCPU as preempted/ready iff it's scheduled out while running David Matlack
  2024-06-18 21:41 ` [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff " Sean Christopherson
  3 siblings, 0 replies; 7+ messages in thread
From: David Matlack @ 2024-05-03 18:17 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kvm, David Hildenbrand, Paul Walmsley, David Matlack, linux-riscv,
	Claudio Imbrenda, Janosch Frank, Marc Zyngier, Huacai Chen,
	Zenghui Yu, Palmer Dabbelt, Christian Borntraeger, Albert Ou,
	Suzuki K Poulose, Nicholas Piggin, Bibo Mao, loongarch,
	Atish Patra, kvmarm, linux-arm-kernel, Sean Christopherson,
	linux-mips, Oliver Upton, James Morse, kvm-riscv, Anup Patel,
	Tianrui Zhao, linuxppc-dev

Ensure that any new KVM code that references immediate_exit gets extra
scrutiny by renaming it to immediate_exit__unsafe in kernel code.

All fields in struct kvm_run are subject to TOCTOU races since they are
mapped into userspace, which may be malicious or buggy. To protect KVM,
this commit introduces a new macro that appends __unsafe to field names
in struct kvm_run, hinting to developers and reviewers that accessing
this field must be done carefully.

Apply the new macro to immediate_exit, since userspace can make
immediate_exit inconsistent with vcpu->wants_to_run, i.e. accessing
immediate_exit directly could lead to unexpected bugs in the future.

Signed-off-by: David Matlack <dmatlack@google.com>
---
 include/uapi/linux/kvm.h | 15 ++++++++++++++-
 virt/kvm/kvm_main.c      |  2 +-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 2190adbe3002..3611ad3b9c2a 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -192,11 +192,24 @@ struct kvm_xen_exit {
 /* Flags that describe what fields in emulation_failure hold valid data. */
 #define KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES (1ULL << 0)
 
+/*
+ * struct kvm_run can be modified by userspace at any time, so KVM must be
+ * careful to avoid TOCTOU bugs. In order to protect KVM, HINT_UNSAFE_IN_KVM()
+ * renames fields in struct kvm_run from <symbol> to <symbol>__unsafe when
+ * compiled into the kernel, ensuring that any use within KVM is obvious and
+ * gets extra scrutiny.
+ */
+#ifdef __KERNEL__
+#define HINT_UNSAFE_IN_KVM(_symbol) _symbol##__unsafe
+#else
+#define HINT_UNSAFE_IN_KVM(_symbol) _symbol
+#endif
+
 /* for KVM_RUN, returned by mmap(vcpu_fd, offset=0) */
 struct kvm_run {
 	/* in */
 	__u8 request_interrupt_window;
-	__u8 immediate_exit;
+	__u8 HINT_UNSAFE_IN_KVM(immediate_exit);
 	__u8 padding1[6];
 
 	/* out */
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index bdea5b978f80..2b29851a90bd 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4425,7 +4425,7 @@ static long kvm_vcpu_ioctl(struct file *filp,
 				synchronize_rcu();
 			put_pid(oldpid);
 		}
-		vcpu->wants_to_run = !READ_ONCE(vcpu->run->immediate_exit);
+		vcpu->wants_to_run = !READ_ONCE(vcpu->run->immediate_exit__unsafe);
 		r = kvm_arch_vcpu_ioctl_run(vcpu);
 		vcpu->wants_to_run = false;
 
-- 
2.45.0.rc1.225.g2a3ae87e7f-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 3/3] KVM: Mark a vCPU as preempted/ready iff it's scheduled out while running
  2024-05-03 18:17 [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff scheduled out while running David Matlack
  2024-05-03 18:17 ` [PATCH v3 1/3] KVM: Introduce vcpu->wants_to_run David Matlack
  2024-05-03 18:17 ` [PATCH v3 2/3] KVM: Ensure new code that references immediate_exit gets extra scrutiny David Matlack
@ 2024-05-03 18:17 ` David Matlack
  2024-06-18 21:41 ` [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff " Sean Christopherson
  3 siblings, 0 replies; 7+ messages in thread
From: David Matlack @ 2024-05-03 18:17 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kvm, David Hildenbrand, Paul Walmsley, David Matlack, linux-riscv,
	Claudio Imbrenda, Janosch Frank, Marc Zyngier, Huacai Chen,
	Zenghui Yu, Palmer Dabbelt, Christian Borntraeger, Albert Ou,
	Suzuki K Poulose, Nicholas Piggin, Bibo Mao, loongarch,
	Atish Patra, kvmarm, linux-arm-kernel, Sean Christopherson,
	linux-mips, Oliver Upton, James Morse, kvm-riscv, Anup Patel,
	Tianrui Zhao, linuxppc-dev

Mark a vCPU as preempted/ready if-and-only-if it's scheduled out while
running. i.e. Do not mark a vCPU preempted/ready if it's scheduled out
during a non-KVM_RUN ioctl() or when userspace is doing KVM_RUN with
immediate_exit.

Commit 54aa83c90198 ("KVM: x86: do not set st->preempted when going back
to user space") stopped marking a vCPU as preempted when returning to
userspace, but if userspace then invokes a KVM vCPU ioctl() that gets
preempted, the vCPU will be marked preempted/ready. This is arguably
incorrect behavior since the vCPU was not actually preempted while the
guest was running, it was preempted while doing something on behalf of
userspace.

This commit also avoids KVM dirtying guest memory after userspace has
paused vCPUs, e.g. for Live Migration, which allows userspace to collect
the final dirty bitmap before or in parallel with saving vCPU state
without having to worry about saving vCPU state triggering writes to
guest memory.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: David Matlack <dmatlack@google.com>
---
 virt/kvm/kvm_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 2b29851a90bd..3973e62acc7c 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -6302,7 +6302,7 @@ static void kvm_sched_out(struct preempt_notifier *pn,
 {
 	struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
 
-	if (current->on_rq) {
+	if (current->on_rq && vcpu->wants_to_run) {
 		WRITE_ONCE(vcpu->preempted, true);
 		WRITE_ONCE(vcpu->ready, true);
 	}
-- 
2.45.0.rc1.225.g2a3ae87e7f-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff scheduled out while running
  2024-05-03 18:17 [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff scheduled out while running David Matlack
                   ` (2 preceding siblings ...)
  2024-05-03 18:17 ` [PATCH v3 3/3] KVM: Mark a vCPU as preempted/ready iff it's scheduled out while running David Matlack
@ 2024-06-18 21:41 ` Sean Christopherson
  2024-07-01 17:51   ` David Matlack
  3 siblings, 1 reply; 7+ messages in thread
From: Sean Christopherson @ 2024-06-18 21:41 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, David Matlack
  Cc: kvm, David Hildenbrand, Paul Walmsley, linux-riscv,
	Claudio Imbrenda, Janosch Frank, Marc Zyngier, Huacai Chen,
	Zenghui Yu, Palmer Dabbelt, Christian Borntraeger, Albert Ou,
	Suzuki K Poulose, Nicholas Piggin, Bibo Mao, loongarch,
	Atish Patra, kvmarm, linux-arm-kernel, linux-mips, Oliver Upton,
	James Morse, kvm-riscv, Anup Patel, Tianrui Zhao, linuxppc-dev

On Fri, 03 May 2024 11:17:31 -0700, David Matlack wrote:
> This series changes KVM to mark a vCPU as preempted/ready if-and-only-if
> it's scheduled out while running. i.e. Do not mark a vCPU
> preempted/ready if it's scheduled out during a non-KVM_RUN ioctl() or
> when userspace is doing KVM_RUN with immediate_exit=true.
> 
> This is a logical extension of commit 54aa83c90198 ("KVM: x86: do not
> set st->preempted when going back to user space"), which  stopped
> marking a vCPU as preempted when returning to userspace. But if userspace
> invokes a KVM vCPU ioctl() that gets preempted, the vCPU will be marked
> preempted/ready. This is arguably incorrect behavior since the vCPU was
> not actually preempted while the guest was running, it was preempted
> while doing something on behalf of userspace.
> 
> [...]

Applied to kvm-x86 generic, with minor changelog tweaks (me thinks you've been
away from upstream too long ;-) ).  Thanks!

[1/3] KVM: Introduce vcpu->wants_to_run
      https://github.com/kvm-x86/linux/commit/a6816314af57
[2/3] KVM: Ensure new code that references immediate_exit gets extra scrutiny
      https://github.com/kvm-x86/linux/commit/4b23e0c199b2
[3/3] KVM: Mark a vCPU as preempted/ready iff it's scheduled out while running
      https://github.com/kvm-x86/linux/commit/118964562969

--
https://github.com/kvm-x86/linux/tree/next

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff scheduled out while running
  2024-06-18 21:41 ` [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff " Sean Christopherson
@ 2024-07-01 17:51   ` David Matlack
  2024-07-10 15:51     ` Sean Christopherson
  0 siblings, 1 reply; 7+ messages in thread
From: David Matlack @ 2024-07-01 17:51 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: kvm, David Hildenbrand, Paul Walmsley, linux-riscv,
	Claudio Imbrenda, Janosch Frank, Marc Zyngier, Huacai Chen,
	Zenghui Yu, Palmer Dabbelt, Christian Borntraeger, Albert Ou,
	Suzuki K Poulose, Nicholas Piggin, Bibo Mao, loongarch,
	Atish Patra, kvmarm, linux-arm-kernel, linux-mips, Oliver Upton,
	James Morse, kvm-riscv, Anup Patel, Paolo Bonzini, Tianrui Zhao,
	linuxppc-dev

On Tue, Jun 18, 2024 at 2:41 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Fri, 03 May 2024 11:17:31 -0700, David Matlack wrote:
> > This series changes KVM to mark a vCPU as preempted/ready if-and-only-if
> > it's scheduled out while running. i.e. Do not mark a vCPU
> > preempted/ready if it's scheduled out during a non-KVM_RUN ioctl() or
> > when userspace is doing KVM_RUN with immediate_exit=true.
> >
> > This is a logical extension of commit 54aa83c90198 ("KVM: x86: do not
> > set st->preempted when going back to user space"), which  stopped
> > marking a vCPU as preempted when returning to userspace. But if userspace
> > invokes a KVM vCPU ioctl() that gets preempted, the vCPU will be marked
> > preempted/ready. This is arguably incorrect behavior since the vCPU was
> > not actually preempted while the guest was running, it was preempted
> > while doing something on behalf of userspace.
> >
> > [...]
>
> Applied to kvm-x86 generic, with minor changelog tweaks (me thinks you've been
> away from upstream too long ;-) ).  Thanks!

Thanks for the cleanups. Looks like you replaced "[Tt]his commit"
throughout. Anything else (so I can avoid the same mistakes in the
future)?

>
> [1/3] KVM: Introduce vcpu->wants_to_run
>       https://github.com/kvm-x86/linux/commit/a6816314af57
> [2/3] KVM: Ensure new code that references immediate_exit gets extra scrutiny
>       https://github.com/kvm-x86/linux/commit/4b23e0c199b2
> [3/3] KVM: Mark a vCPU as preempted/ready iff it's scheduled out while running
>       https://github.com/kvm-x86/linux/commit/118964562969
>
> --
> https://github.com/kvm-x86/linux/tree/next

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff scheduled out while running
  2024-07-01 17:51   ` David Matlack
@ 2024-07-10 15:51     ` Sean Christopherson
  0 siblings, 0 replies; 7+ messages in thread
From: Sean Christopherson @ 2024-07-10 15:51 UTC (permalink / raw)
  To: David Matlack
  Cc: kvm, David Hildenbrand, Paul Walmsley, linux-riscv,
	Claudio Imbrenda, Janosch Frank, Marc Zyngier, Huacai Chen,
	Zenghui Yu, Palmer Dabbelt, Christian Borntraeger, Albert Ou,
	Suzuki K Poulose, Nicholas Piggin, Bibo Mao, loongarch,
	Atish Patra, kvmarm, linux-arm-kernel, linux-mips, Oliver Upton,
	James Morse, kvm-riscv, Anup Patel, Paolo Bonzini, Tianrui Zhao,
	linuxppc-dev

On Mon, Jul 01, 2024, David Matlack wrote:
> On Tue, Jun 18, 2024 at 2:41 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > On Fri, 03 May 2024 11:17:31 -0700, David Matlack wrote:
> > > This series changes KVM to mark a vCPU as preempted/ready if-and-only-if
> > > it's scheduled out while running. i.e. Do not mark a vCPU
> > > preempted/ready if it's scheduled out during a non-KVM_RUN ioctl() or
> > > when userspace is doing KVM_RUN with immediate_exit=true.
> > >
> > > This is a logical extension of commit 54aa83c90198 ("KVM: x86: do not
> > > set st->preempted when going back to user space"), which  stopped
> > > marking a vCPU as preempted when returning to userspace. But if userspace
> > > invokes a KVM vCPU ioctl() that gets preempted, the vCPU will be marked
> > > preempted/ready. This is arguably incorrect behavior since the vCPU was
> > > not actually preempted while the guest was running, it was preempted
> > > while doing something on behalf of userspace.
> > >
> > > [...]
> >
> > Applied to kvm-x86 generic, with minor changelog tweaks (me thinks you've been
> > away from upstream too long ;-) ).  Thanks!
> 
> Thanks for the cleanups. Looks like you replaced "[Tt]his commit"
> throughout. Anything else (so I can avoid the same mistakes in the
> future)?

I don't think so?  The "This commit" stuff is the only thing that I remember, so
any other tweaks can't be that important :-)

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-07-10 15:52 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-03 18:17 [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff scheduled out while running David Matlack
2024-05-03 18:17 ` [PATCH v3 1/3] KVM: Introduce vcpu->wants_to_run David Matlack
2024-05-03 18:17 ` [PATCH v3 2/3] KVM: Ensure new code that references immediate_exit gets extra scrutiny David Matlack
2024-05-03 18:17 ` [PATCH v3 3/3] KVM: Mark a vCPU as preempted/ready iff it's scheduled out while running David Matlack
2024-06-18 21:41 ` [PATCH v3 0/3] KVM: Set vcpu->preempted/ready iff " Sean Christopherson
2024-07-01 17:51   ` David Matlack
2024-07-10 15:51     ` Sean Christopherson

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).