From patchwork Fri Aug 30 04:35:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13784259 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1A28ECA0EE1 for ; Fri, 30 Aug 2024 04:44:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=k204YuuBNkPIQSpU0QhI7EvnHBfhBz5F6jzs/OiMdls=; b=e49mmt/UM4cWhhhZwCXxqon8HA tvz5lL3NSOV3bX8Z2nnPjKJoIWr5aMKXiSwLEySeqdKUWjjQXMifncSk0A5PyBAZoyHKUNg0BoIGl uJ3zX3FNyV5ufSNT4s+LVbBCln/VN9BwOll/O6iISs88lbvWD7GxKaDy50pJe2j/fL6Q34f8uj7GI bg1CzjSNN8EQoCfcJkBe99qXlfZQsIOaHzJy+xGvAvcn6UhGNeEc4HHH189wH5C2+TNPO7UXxxJcB 5f7WTx9MrE7LTsMuazB+U1I4GgMn+YR7QwXwMumitHFVRCwXiTZtB89UXs2woN0ZZf7FQx7/t2wik TtbMylRw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjtUl-00000004hxh-0W3b; Fri, 30 Aug 2024 04:44:23 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjtN8-00000004fxR-0yyd for linux-arm-kernel@bombadil.infradead.org; Fri, 30 Aug 2024 04:36:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To:Sender: Content-Transfer-Encoding:Content-ID:Content-Description; bh=k204YuuBNkPIQSpU0QhI7EvnHBfhBz5F6jzs/OiMdls=; b=qAdOqGm2WKyaFItVwHD8LNd0u4 REiAfHuOekjKUBd9O7vWZRnwS16XUcmYtGCcr7Z1z4xJ5X4vngLqcKHHkDYN6w/xC4xcEoHevg0pL p7o/FUZwyOf5jfRA5LQ+a50cHurdczS6SOQaeWKwx58+L9tSopVF6FC3b/yblQLup/NDbHDd0lxug YiL7oRdSGq2TaCGcOOqaqQUMGBH3fKdzv5SsXRxxMksQk3muxoExLWwW2yJu/60cpv/n5AlAYb7fv AtnUrSf9ele9JZxRO5H5Wm+jkZsHPD3UXjWcpLC9xbI5pXgWS6+KfUGGj0WRLheWGi8Kcn4D+NJvb 7sjvVorQ==; Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjtN3-0000000BYg3-2rh3 for linux-arm-kernel@lists.infradead.org; Fri, 30 Aug 2024 04:36:28 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-6d3aa5a672dso14697187b3.2 for ; Thu, 29 Aug 2024 21:36:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724992581; x=1725597381; darn=lists.infradead.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=k204YuuBNkPIQSpU0QhI7EvnHBfhBz5F6jzs/OiMdls=; b=4D33Jke9HvJx2wZAxItEo+TL+9L4+raUHKAxX2pkIv7MPGLU62KhRwKkiSoHD5/NQP LphELKeUTrIMhrd8HH4BtR6enOh/gtn1/rIv0xaLknGmH8yUHko2abMSu9Izj4FlE5Rw pyqoK0cTXPFgg/WUZ9Q8MspotVHk3zZupXszD2h3KJIw6Wi4eBytGiFSvT+GLAiHK/Ey IEUEP189XKoAeFSglXs1xWvR4TWATBZF3+2sErFdTCRoFfSfiwSLN8lai5V6JlYmZLBH 2sXRmUY9OvrY68Zi8lLqn32LUCoznaMM3Uxu0YiLrPo8E73H7cQqUYXK2kZu1l6aH2YP elvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724992581; x=1725597381; 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=k204YuuBNkPIQSpU0QhI7EvnHBfhBz5F6jzs/OiMdls=; b=VqbMwSJmGG78up7ROmvSGwmGd12BDk2v1zCN9bwOFDQweo5llwZ076X+njvCjFQk+a dVaRb9Kiq5WjdYBfpkQgeLz1VY5oTjVP961mPIyxbw9Dcq37wybP8/A515Sbizq7skf7 XOkYN7dyFfb1+03lEcYqe85IHuyluFSDPalcfPRAF0EpuwZf2noYAfBZ5CtN7ZOCpz+f X1hzOHxi7GeFsv35gzf5r4G+dw9bzc12RsyQ0LiLjiKFFnJHE48PprglLe7RdzDGIQGU F1foyx8Lw7iDs8bF5Ayb2U5GZAgw/hJKz7jRKp1jKcno1WoZJUcwTD0HJZDUucVH2QxX 3OJQ== X-Forwarded-Encrypted: i=1; AJvYcCVlvTsYyCIoxVggZ58rJPcC7qsQNbc1/u9/9XCFuV8NVf3daTDcg8J4GkbYO+CL0+2LY2nJ5FptOw6AqFsSSz2z@lists.infradead.org X-Gm-Message-State: AOJu0Yyu4hRXUbJq5UhdrjpWpprlorAiT1D8QNA74f8/GLoqCatXniS8 FvQpFPDgoOJUka+JbB85f6ZU01FV0FXHWzOO94a8Z7Mt/+imCeDbpaLEHdLSMt7iEGUWEr4J243 ZsQ== X-Google-Smtp-Source: AGHT+IH7doeLworevO5q5a0Cpx/I2Hf/rkCrWJ1Q/dDQPDqM64XFN6CvEdH2toTw5kd6ygYMiJVKDoge7UM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:e1f:b0:650:93e3:fe73 with SMTP id 00721157ae682-6d41000b45bmr405537b3.5.1724992580739; Thu, 29 Aug 2024 21:36:20 -0700 (PDT) Date: Thu, 29 Aug 2024 21:35:58 -0700 In-Reply-To: <20240830043600.127750-1-seanjc@google.com> Mime-Version: 1.0 References: <20240830043600.127750-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240830043600.127750-9-seanjc@google.com> Subject: [PATCH v4 08/10] KVM: Add arch hooks for enabling/disabling virtualization From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Chao Gao , Kai Huang , Farrah Chen X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240830_053626_042686_4105715A X-CRM114-Status: GOOD ( 14.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add arch hooks that are invoked when KVM enables/disable virtualization. x86 will use the hooks to register an "emergency disable" callback, which is essentially an x86-specific shutdown notifier that is used when the kernel is doing an emergency reboot/shutdown/kexec. Add comments for the declarations to help arch code understand exactly when the callbacks are invoked. Alternatively, the APIs themselves could communicate most of the same info, but kvm_arch_pre_enable_virtualization() and kvm_arch_post_disable_virtualization() are a bit cumbersome, and make it a bit less obvious that they are intended to be implemented as a pair. Reviewed-by: Chao Gao Reviewed-by: Kai Huang Acked-by: Kai Huang Tested-by: Farrah Chen Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 14 ++++++++++++++ virt/kvm/kvm_main.c | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 63e46a5f3812..50ace701d468 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1514,6 +1514,20 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} #endif #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING +/* + * kvm_arch_{enable,disable}_virtualization() are called on one CPU, under + * kvm_usage_lock, immediately after/before 0=>1 and 1=>0 transitions of + * kvm_usage_count, i.e. at the beginning of the generic hardware enabling + * sequence, and at the end of the generic hardware disabling sequence. + */ +void kvm_arch_enable_virtualization(void); +void kvm_arch_disable_virtualization(void); +/* + * kvm_arch_{enable,disable}_virtualization_cpu() are called on "every" CPU to + * do the actual twiddling of hardware bits. The hooks are called on all + * online CPUs when KVM enables/disabled virtualization, and on a single CPU + * when that CPU is onlined/offlined (including for Resume/Suspend). + */ int kvm_arch_enable_virtualization_cpu(void); void kvm_arch_disable_virtualization_cpu(void); #endif diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 55779fbb37ec..9ae4bd8a3712 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5582,6 +5582,16 @@ static DEFINE_PER_CPU(bool, virtualization_enabled); static DEFINE_MUTEX(kvm_usage_lock); static int kvm_usage_count; +__weak void kvm_arch_enable_virtualization(void) +{ + +} + +__weak void kvm_arch_disable_virtualization(void) +{ + +} + static int kvm_enable_virtualization_cpu(void) { if (__this_cpu_read(virtualization_enabled)) @@ -5681,6 +5691,8 @@ static int kvm_enable_virtualization(void) if (kvm_usage_count++) return 0; + kvm_arch_enable_virtualization(); + r = cpuhp_setup_state(CPUHP_AP_KVM_ONLINE, "kvm/cpu:online", kvm_online_cpu, kvm_offline_cpu); if (r) @@ -5711,6 +5723,7 @@ static int kvm_enable_virtualization(void) unregister_syscore_ops(&kvm_syscore_ops); cpuhp_remove_state(CPUHP_AP_KVM_ONLINE); err_cpuhp: + kvm_arch_disable_virtualization(); --kvm_usage_count; return r; } @@ -5724,6 +5737,7 @@ static void kvm_disable_virtualization(void) unregister_syscore_ops(&kvm_syscore_ops); cpuhp_remove_state(CPUHP_AP_KVM_ONLINE); + kvm_arch_disable_virtualization(); } static int kvm_init_virtualization(void)