Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp670987rdd; Tue, 9 Jan 2024 16:40:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IH+v73qkBMV47rmUAU7ogmR/AzXjCDBGgM2+Vi7YhdptECFHC3g3My2XFGZi5ABEPNsK14N X-Received: by 2002:a05:6a20:b129:b0:196:8da7:dbc9 with SMTP id ed41-20020a056a20b12900b001968da7dbc9mr1650240pzb.33.1704847226877; Tue, 09 Jan 2024 16:40:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704847226; cv=none; d=google.com; s=arc-20160816; b=nAPjIBOZOwpt0b5X5CZb+UjPjnDF9W4oqI1evWNQToj/yBPmEcZld5c9llm7lakBmK 8vN3+BHWaHpuMyNfhBSjZEi0R0iI0K2WAxdchoh+QEhPbEKdCP4096jF0lGLklT1iMiS So5G9MQULvuCUoOCxKwXSGmZxZMltQG47akmUzWEeSqM1/BujWNxw3uVlzt682JcjWB7 3jHjsF2q9D1Ybm11CeX61XnnTkE3DTs9meJzczhd+tAn9YA1Nk6SnJcXlu1THcByzz4p 0y4YwGUCGWXFwBQjggPCIhjchBmH4jkB09E2Az/hXstc181E6uHiWJNN8rkzfE/as0ZK CJjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=ePMAACFe2MrWq1DNaP6F2TM5DnJ2ZLx/mnijbLthbpo=; fh=/j5JD0kNmt6w2ElaoSjWxIvbDK7Ar0LSE5o5EAaCtME=; b=MRSVRMBrrYzcq4HICf/M5VM3aPOTzRCfVLprmk14qIH2GFJKBOikiOOwPTEjVKVvVM U4fvUX3mF8oqh1JPYq0s8KGkrGcZX7f3Qjmd5tmdRJOwkEvTvii83UxiNKnIop1umDNl cmBnNlR8JSiA4r4kfLE5O2zkMa+tcjtrpLVQulYGtvBwpRaY4lDKR9B+SC9PE3/V9Wx9 8ldlVU60SHbW5Iksj0f3mVG9IwtaIHFYrCiuCV9Fx+at/q+awqyqDdF/U6/cN/5WJk2L Y6K5dLu3X/Q5vcj0tPRN8IBuobthr/YvzZe7Tc6Rcfa+M0v1NXtw+kDtVuQ8MeNgolMt wZag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=E6lIprKj; spf=pass (google.com: domain of linux-kernel+bounces-21588-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21588-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id f3-20020a635103000000b005ce2b993254si2439978pgb.204.2024.01.09.16.40.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 16:40:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21588-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=E6lIprKj; spf=pass (google.com: domain of linux-kernel+bounces-21588-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21588-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D49752826AC for ; Wed, 10 Jan 2024 00:40:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6C0C34A15; Wed, 10 Jan 2024 00:39:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="E6lIprKj" Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 7E73A2916 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 Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-6da129ea7dfso2123547b3a.2 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=OQvZmAYV0owa0h66UweJG4PV3pRQ3Iedprx/3dhNRfd59zgcQHEzYUaX2Jag/bs4k9 PT42ClGJPn/xON0WVLOgihHw9tC6SxxQlVifYIYiV15vlAchcW/0h7ZYriCtdE72ip5p IuMoADR128k3H6+uHq/ftKnJBHAq289ZJIzCCE2FcsRmBQhJnQTNB6NQs9ZeTOw52+gS 1/rkRPl65k/NnlcHF2wc9Ye/UurhVl5WPEvckDRrVsQn0ciadJ0mFVNzQskmBkmGx3wH 4849KVPr+Z9iAtJLrU594fNchTyhCyvYyReH4dMX0AcDdJvsRVI5H2leq7sr90PKmdbR rLyw== X-Gm-Message-State: AOJu0Yy4U08f3I8Uk8hV+Gl437wDpadSKP/+q86t9iGNOo2dPVjSORlG vFgn8qRWwvXpI5h29sX/s2L+wt6osoB0Z2xIyA== 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: linux-kernel@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 Content-Type: text/plain; charset="UTF-8" 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 --- 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; -- 2.43.0.472.g3155946c3a-goog