From patchwork Wed Jan 10 00:39:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13515556 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 790F82915 for ; Wed, 10 Jan 2024 00:39:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="E6lIprKj" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-6d9bd2deabfso2094009b3a.0 for ; Tue, 09 Jan 2024 16:39:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704847183; x=1705451983; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ePMAACFe2MrWq1DNaP6F2TM5DnJ2ZLx/mnijbLthbpo=; b=E6lIprKjPCtPRRdwnoIxtEsxJQ8n/4jQgGAXVfiyYDu2sDnAbVzdXlgaDoVRcJxWGf KReekSIarPOLgOw5RU6Wirk3bYpxKqSuFwhy6Ov0n56TeppuG0gtI3kVW6IKgh399gj+ qsEgBrTh1azkW892HlSAcYoI0QgQ0zs5ehV3vDtkQYYejAzB2eJNiniMnxfSoNBAZQPB oo0w5jxocV97czR84GLg+4NmEUBN1XwJnFc7Exfzv2Q8iQMvCXWoKf4pcKfHNSfIjo7S ROcPCmLWm0VbnIHz/puEggHHPcFcAwweOtaBgIUMqH7wsKZe08bATIl5SP6ODw/jduKs QuAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704847183; x=1705451983; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ePMAACFe2MrWq1DNaP6F2TM5DnJ2ZLx/mnijbLthbpo=; b=WDTYY2uJY74RnsEefMMrBUlZ94/5Z6Q9Fv410hKVbP4Y+m429L3RRJAVDCZNteJ4Zb KL/PlD7FKTzwlX3L53W/hBORFg1xkM2RDRJBr/i7/HrbiVUDUwLaQyTQEFS1cVbAqbrF t1urX6s5ccKZXQAZOpDK3GUEG08R3Y1GPLhxcxS/jwkyKRQBb2HNMRIQ7AYpYQA9d2Cc e0BM3aW8/6fJiZ6JgpPbWhQjvyMUWxY1v3+2vv6FLL2gkvgZImTrY6qpfCMhhUc7cBkB p8eyEhW75wa9YRNUvfY/xZTmDVzt1YCgKt1Aaahl3Nb60qahq7vxqKZ406HDLWX8DxcC AGcA== X-Gm-Message-State: AOJu0YyfGVlzcuFYiyPTwUvJK09wurVgZYpTaS01138J7TAMbJRMsUUO ZHXufWZJA9Kg7eoKvLsbtkzIfRtYKEfzsRwA0g== X-Google-Smtp-Source: AGHT+IHOtJaaLG3E7ais7QPCemsOQWravXElMjvqSYr4650maL97nrx0BDvmc6awukPm55MRPpouDk2C+R0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ce8b:b0:1d3:e449:fb53 with SMTP id f11-20020a170902ce8b00b001d3e449fb53mr1035plg.4.1704847182436; Tue, 09 Jan 2024 16:39:42 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 16:39:35 -0800 In-Reply-To: <20240110003938.490206-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240110003938.490206-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240110003938.490206-2-seanjc@google.com> Subject: [PATCH 1/4] KVM: Add dedicated arch hook for querying if vCPU was preempted in-kernel From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Like Xu Plumb in a dedicated hook for querying whether or not a vCPU was preempted in-kernel. Unlike literally every other architecture, x86's VMX can check if a vCPU is in kernel context if and only if the vCPU is loaded on the current pCPU. x86's kvm_arch_vcpu_in_kernel() works around the limitation by querying kvm_get_running_vcpu() and redirecting to vcpu->arch.preempted_in_kernel as needed. But that's unnecessary, confusing, and fragile, e.g. x86 has had at least one bug where KVM incorrectly used a stale preempted_in_kernel. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Yuan Yao --- arch/x86/kvm/x86.c | 5 +++++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 15 +++++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 27e23714e960..415509918c7f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13091,6 +13091,11 @@ bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu) return false; } +bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu) +{ + return kvm_arch_vcpu_in_kernel(vcpu); +} + bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) { if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7e7fd25b09b3..28b020404a41 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1505,6 +1505,7 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); +bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_post_init_vm(struct kvm *kvm); void kvm_arch_pre_destroy_vm(struct kvm *kvm); int kvm_arch_create_vm_debugfs(struct kvm *kvm); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 10bfc88a69f7..6326852bfb3d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4042,11 +4042,22 @@ static bool vcpu_dy_runnable(struct kvm_vcpu *vcpu) return false; } +/* + * By default, simply query the target vCPU's current mode when checking if a + * vCPU was preempted in kernel mode. All architectures except x86 (or more + * specifical, except VMX) allow querying whether or not a vCPU is in kernel + * mode even if the vCPU is NOT loaded, i.e. using kvm_arch_vcpu_in_kernel() + * directly for cross-vCPU checks is functionally correct and accurate. + */ +bool __weak kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu) +{ + return kvm_arch_vcpu_in_kernel(vcpu); +} + bool __weak kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu) { return false; } - void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) { struct kvm *kvm = me->kvm; @@ -4080,7 +4091,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) continue; if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode && !kvm_arch_dy_has_pending_interrupt(vcpu) && - !kvm_arch_vcpu_in_kernel(vcpu)) + !kvm_arch_vcpu_preempted_in_kernel(vcpu)) continue; if (!kvm_vcpu_eligible_for_directed_yield(vcpu)) continue; From patchwork Wed Jan 10 00:39:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13515557 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9FFE2469E for ; Wed, 10 Jan 2024 00:39:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XSWtWN7f" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dbed6083a91so4540033276.3 for ; Tue, 09 Jan 2024 16:39:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704847184; x=1705451984; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=6xnKyEv4OBvotGnyOqTbS4nPA0NPUNoMw8n+LkAwHEU=; b=XSWtWN7fzQbbzIAWT1B2BF6Rik9aDdNQwqiBQYaglkEYF9/kL4ySWJKtEqxVcU0Q6h pHwuPqrAlH2dVU2o1GoOkk2E9GmumodNOpx7kAt0sC5LAizstjgCp95Fo9m+hEhOAKS9 J0YmrRRGwrgTrLZYWkhE7AbvXVD51ZaW6H+iL0OjbX2L91abrNhlS4TcU19J8VGra3we HKX1qpkn0nVmD9hqJqJNIqAUTrYJzuzkeU+dhe2D7vkyR4fN3ShQdAUEMOjFfo1ra313 RTGhtvCLyTQTwUCJtpa7qcLGkIrKjPLQVh2TLxtqk+0kQtrYN5+rKcfgFx8UcdtAhknj oV9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704847184; x=1705451984; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6xnKyEv4OBvotGnyOqTbS4nPA0NPUNoMw8n+LkAwHEU=; b=kMIYHVGq7mv05M1KrM/oH9Pe1h16noY8Se6VPWmB411y55/T5VphQ76//s49Lg61ng y5IZNibcWGvFd4/+f79pbOlU6ymea2CN3hf2pteG7zn4SRWPCh88d2e2CDfmwIzNJmwe isntOke5wM1i5FQJkkvcOXVQowXVZzh3juSHwCrBd2TajbXOXi+HoywvXdar9QkeFAb7 WJ5DWLCMPd+cpuODW9FtUKCZ6jLM0Y6hla9nR0ji1T1nILQ+g7uwUIyXV33B4GdeJ/y3 wwHLo7vYEiCXd7mfWHPuPUehnVb8gBvsIyDWWfuacgzJR4IIWHUq5FKCxUdHW0+c0Pm7 Fw0w== X-Gm-Message-State: AOJu0YzbVvV4nI4TqtuHaTRU+AQfmdDspR9jQXXiNudkmJDwfBTzGD4e /uI024IpJrC3+eLPib890j1HrLazLGuGUlizAw== X-Google-Smtp-Source: AGHT+IGJdgMga5DZLPUMl5FS/PauMfNNe4vf3zX11dmX3/7XA4baZSYAUHhTmPFHLa7ontbom+AG5NPk4tM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1348:b0:dbc:c697:63bd with SMTP id g8-20020a056902134800b00dbcc69763bdmr83934ybu.0.1704847184802; Tue, 09 Jan 2024 16:39:44 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 16:39:36 -0800 In-Reply-To: <20240110003938.490206-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240110003938.490206-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240110003938.490206-3-seanjc@google.com> Subject: [PATCH 2/4] KVM: x86: Rely solely on preempted_in_kernel flag for directed yield From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Like Xu Snapshot preempted_in_kernel using kvm_arch_vcpu_in_kernel() so that the flag is "accurate" (or rather, consistent and deterministic within KVM) for guest with protected state, and explicitly use preempted_in_kernel when checking if a vCPU was preempted in kernel mode instead of bouncing through kvm_arch_vcpu_in_kernel(). Drop the gnarly logic in kvm_arch_vcpu_in_kernel() that redirects to preempted_in_kernel if the target vCPU is not the "running", i.e. loaded, vCPU, as the only reason that code existed was for the directed yield case where KVM wants to check the CPL of a vCPU that may or may not be loaded on the current pCPU. Cc: Like Xu Signed-off-by: Sean Christopherson Reviewed-by: Yuan Yao --- arch/x86/kvm/x86.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 415509918c7f..77494f9c8d49 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5062,8 +5062,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) int idx; if (vcpu->preempted) { - if (!vcpu->arch.guest_state_protected) - vcpu->arch.preempted_in_kernel = !static_call(kvm_x86_get_cpl)(vcpu); + vcpu->arch.preempted_in_kernel = kvm_arch_vcpu_in_kernel(vcpu); /* * Take the srcu lock as memslots will be accessed to check the gfn @@ -13093,7 +13092,7 @@ bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu) bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu) { - return kvm_arch_vcpu_in_kernel(vcpu); + return vcpu->arch.preempted_in_kernel; } bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) @@ -13116,9 +13115,6 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) if (vcpu->arch.guest_state_protected) return true; - if (vcpu != kvm_get_running_vcpu()) - return vcpu->arch.preempted_in_kernel; - return static_call(kvm_x86_get_cpl)(vcpu) == 0; } From patchwork Wed Jan 10 00:39:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13515558 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C1EE523A for ; Wed, 10 Jan 2024 00:39:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YZutqnol" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-28c05e74e36so4145162a91.0 for ; Tue, 09 Jan 2024 16:39:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704847186; x=1705451986; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Pqe71wm1M+G2sxafHwJo+rZxz6yjUptaYMkbv+1aMM0=; b=YZutqnol1HrLWmrIvBtq9YKcUcRem3wQuRHt0Z6JPlQJdhkdkkIy6b0H1Novt9dfrw x3Y1+dfoAsXhqHNqwgbrnIiRlbJrc1EZam8ca79n9SxW1xiOyB/jpe0vrw2kSzE91B7u 68QOdMNX6JKL3kBG7xhNoNfRph7JkNN9N6Do87ftmzaNsunYJBzQp7iEaDfW7t9SUP/I i3twlbvl2vaB70CY//4mX6SxTNsvZNX//3chVKTAw8c3k3CCWemLxE1nYOb0uQb0DkwQ zmJGzI7RlvjNTEOkWNt4KIEgXvGz/9cvXycDoWCuV8bse1qrm3bqFacrFtIUWnDFt3rd hUhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704847186; x=1705451986; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Pqe71wm1M+G2sxafHwJo+rZxz6yjUptaYMkbv+1aMM0=; b=xJNOFV7Co3ZVdWklRz1rIOQs9K/hTBOcWrHtFes6M5M1edKQgr1TWTlLWeQaTMkRFO 1xsQPQWyWK/nj6roBLuXX+tqAQd4ySJAknp76DMkW4NbFqdZ8M7qCb030lXrtgxz/fE6 V9+Pz7qzuhLztKaDQ8CajQciztNE5hj8qZyxT99yOB5IkDkyq+Ga2vKek1I7ljxeoWBW 1Q54ufnojvTAYO8eW6HiZIx2OrCQfwQ76XZUQFcVZxsbbHE3Ud82RkvBD2bBSmIBH+cn 9gsNhOuvZ8FYWqVPM66AFqjVTT/m7ZhuBdbmceTSglzVuyv99cdPDTnER18VOjljNK8J Xvjw== X-Gm-Message-State: AOJu0YxNIoDGXlQCQYnfM+yTCE/d0wSbSe6ocoRdUqeSpLCBXDKEKAZ0 1N4Sw+I9EKeL7SUHuW4ccyqggCgUlVJP6qSPnQ== X-Google-Smtp-Source: AGHT+IHy3G82NSplUegkyA2Eh3U1cZHYVq7yVh3SDYUhCqB4HtyzIO3xeq0bjWvyF5FfhYIa/SDY5MlL1wM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:38c2:b0:28b:4489:38fc with SMTP id nn2-20020a17090b38c200b0028b448938fcmr10289pjb.2.1704847186504; Tue, 09 Jan 2024 16:39:46 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 16:39:37 -0800 In-Reply-To: <20240110003938.490206-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240110003938.490206-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240110003938.490206-4-seanjc@google.com> Subject: [PATCH 3/4] KVM: x86: Clean up directed yield API for "has pending interrupt" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Like Xu Directly return the boolean result of whether or not a vCPU has a pending interrupt instead of effectively doing: if (true) return true; return false; Signed-off-by: Sean Christopherson Reviewed-by: Yuan Yao --- arch/x86/kvm/x86.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 77494f9c8d49..b7996a75d9a3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13083,11 +13083,8 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu) { - if (kvm_vcpu_apicv_active(vcpu) && - static_call(kvm_x86_dy_apicv_has_pending_interrupt)(vcpu)) - return true; - - return false; + return kvm_vcpu_apicv_active(vcpu) && + static_call(kvm_x86_dy_apicv_has_pending_interrupt)(vcpu); } bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu) From patchwork Wed Jan 10 00:39:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13515559 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60BA0B663 for ; Wed, 10 Jan 2024 00:39:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eKOR2f2l" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-daee86e2d70so3305646276.0 for ; Tue, 09 Jan 2024 16:39:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704847188; x=1705451988; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=D4tvOVGYXZj07Mh3AbKScJlIRDF+gPUSkrxYwSocJBU=; b=eKOR2f2lty8vnakGYTNRzUplaVRRnXqedSIK3sreykitPI9o67BRVaBrxSgFLagtTX E7idXtH06VESju55/7pn2PyR9wabHnLKrg5hFMvFSPAUBTkBm0GI+QypdLjs+qCwUrkd Tr00OJcLOoRFHnPquwThbMR409+RjGDPa2clm160YkiOxvc3vjm8NNC0mPmNBFOulQpN pUunmH8MValLTymY4F4IUS2XDYXCzQ/GV6a4oGQ/P7ko0du8/zY5NfxmxWjypNQDV4Pf AxCYZDRc/nO9+8OaCOAOu+L839lOI1VXgGoH5etbh4FrviDVDQGYl4DsLckswoz7/7vG Y7hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704847188; x=1705451988; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=D4tvOVGYXZj07Mh3AbKScJlIRDF+gPUSkrxYwSocJBU=; b=VSZbGCuffn/irNqG1zdpog59gbFVavIqr+hTKg9LAyRfK0sH9nKK7KMDsLRrJaMQfA 9/h3JYGFeVAGrqz7CIXx/mDLNn2FevJzee9Lc7mkc0KcQJmWvYyXqYMX2N4D3+f2WB+1 c99HpQw7bPYJuPqtoi5YlwPJPZjd7zFBmX+hlo1jeFacJ4XFn6qgTxC+KslwxedZukiT Qb6/JiCsJx0NU7yN5+7Ztc0oyHc+++pJYs69mypyLUMgKugYQTIKicZerVGN3dlXlHFk V+YBGR5NYijBB3LohhdAmN8LlmGdXNSrYrkqdt3XrKw/put92GK/0jP8sF0NiF8+VKz1 LBgw== X-Gm-Message-State: AOJu0YwG8QaYqqo+9gjA5QcddGJXxs5/i0COgRreQdipT3fCoQzXZtvM hpLDqmUp6KU7/dOZ8BGc1rdq/nlsvAqrGUv+fw== X-Google-Smtp-Source: AGHT+IFuJjrM+MDgoFfdbmi17tmfVjaKhqfjfC+jS0/h9snOExZty5PrssXn0VPV/aGi9MJjghmUZutE7hA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:8749:0:b0:dbe:d426:c456 with SMTP id e9-20020a258749000000b00dbed426c456mr6512ybn.4.1704847188423; Tue, 09 Jan 2024 16:39:48 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 16:39:38 -0800 In-Reply-To: <20240110003938.490206-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240110003938.490206-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240110003938.490206-5-seanjc@google.com> Subject: [PATCH 4/4] KVM: Add a comment explaining the directed yield pending interrupt logic From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Like Xu Add a comment to explain why KVM treats vCPUs with pending interrupts as in-kernel when a vCPU wants to yield to a vCPU that was preempted while running in kernel mode. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6326852bfb3d..4a9e7513c585 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4089,6 +4089,13 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) continue; if (kvm_vcpu_is_blocking(vcpu) && !vcpu_dy_runnable(vcpu)) continue; + + /* + * Treat the target vCPU as being in-kernel if it has a + * pending interrupt, as the vCPU trying to yield may + * be spinning waiting on IPI delivery, i.e. the target + * vCPU is in-kernel for the purposes of directed yield. + */ if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode && !kvm_arch_dy_has_pending_interrupt(vcpu) && !kvm_arch_vcpu_preempted_in_kernel(vcpu))