Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1370302lqt; Fri, 7 Jun 2024 17:08:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWHIi6tpopAMi51BeTIOhei+kVD2OJ9SpBSnkWwAORZjBX6s9Vg/H4WpgvlfW/kyNJFmN+kNT2Gj+EG5GE997Vn46hDyKEkAAIv46sEtQ== X-Google-Smtp-Source: AGHT+IExatPJ/1v3kKt0yZiVHmTeG7pDjkSuTMiNzdq0XmRVo1lRMviOPQvxOQh5iX+KpzhEL7Y0 X-Received: by 2002:a17:902:c94f:b0:1f6:8a19:4562 with SMTP id d9443c01a7336-1f6dfc426d6mr30371475ad.24.1717805300037; Fri, 07 Jun 2024 17:08:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717805300; cv=pass; d=google.com; s=arc-20160816; b=wdTIctcWJUCr0lzvQeDqMDb9Cs5/b8+BGNxFluOW8rouB5P50n7OhOTWjtIfnenOFL gbptdeq7HZ+Eqd5xUbdSuu89ebztd71qKJZx9noaVq94jhzRPdFKBsPES39B4yVOJZZ0 cU6ERXmsXRMfJQHuXQcfiBxAmEn1IDrmxu0brjvwfzDyR50RRqHdavOoKWPSsX0HU4PX IybZ7zL2XYQqQcjrIsLEjY2cP1UFAYZSW76dKrlyQbXLz4sr8B6TysvlKI4yuEfpeS6/ ZvBvCBEaIyPeMgA5yq/NatitSP65OSxVE8F0eby0v4u88gKRDE1IdQXTW8y7Stlt4590 BP7w== ARC-Message-Signature: i=2; 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=U44HT/MSZO+MfGZ9R6nvDDZFtdhwkwnAqQLWwNh4/1A=; fh=m4sW7XrRzWWItfzpZ3uu4AcA8vJKr1nO4IXowS1JSWs=; b=Y0XclwoTrxMkBXpxyGJSSkFyAa/CzqRyj/jxxzVP3BeTaNdSWmeCePKmquyXwuaybO Bxxc1IdSbDHQGqohYREUshNwygaY0KOroUKoOj7Q3K8cYYsPs8CpWfYvu9R+S8uHLs1Y PhCGBsc4oVJjah3KoXEdSs1jMcX1Up7fmyFZa41yGBYjfSzJ0kufe5U73R7yjlnKaaLN JyOh69HwM0lxOzS3jgL+7qaKDff/llUK/dtshdy+IQlvO3fFpaOKTvdb+9kDwBmuMVLY /IC85z1T0IWlhvWvPfwTXbU+/93bMjwW4E8VlPx50rMKkGfYhusSxVRaVSokTcr+7t24 jB5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YTC645Sx; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-206843-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206843-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f6bd76da36si3985955ad.140.2024.06.07.17.08.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 17:08:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206843-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YTC645Sx; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-206843-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206843-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id BA5F0B21DE5 for ; Sat, 8 Jun 2024 00:08:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 13EA817984; Sat, 8 Jun 2024 00:06:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YTC645Sx" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 8A29BD520 for ; Sat, 8 Jun 2024 00:06:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717805212; cv=none; b=kpLoBB6mW5REfYiC+QMsyn0MMlJWhhqGEu7tGJ+xzwwRDTUyC9ozsRjZ7X4vKdWOfmXTlObi4X6JBbOlCgwppz2lXZSh5WNfoHNuClDCYNHgbymLUJe3+9MfPAe78GT4p3i3oySUzsPPe+JsWKw1DWa4Gyy0sed9Afq1gylVVR8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717805212; c=relaxed/simple; bh=kosHQz2mO5WsDhLLqPXFZqPb2PZkIqYIvZGswimzFcE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YZt51uU96QFt6p49wz+w6IQ8EwIgVfhT1lPcZiWcsD6BHLhc/t3TVtsJWvlRG35gbLmnpTMv1EQFZnqMNtCIpGiDaYhbkTz947ybwEegV7M6Z6clWfya4J4yXkVpXTPt6X7mK/GTrhzTZZoYOrIXJilkQNnsKKqzfsSkh3pSxgo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YTC645Sx; arc=none smtp.client-ip=209.85.128.201 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-yw1-f201.google.com with SMTP id 00721157ae682-627dd6a56caso41965277b3.3 for ; Fri, 07 Jun 2024 17:06:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717805209; x=1718410009; 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=U44HT/MSZO+MfGZ9R6nvDDZFtdhwkwnAqQLWwNh4/1A=; b=YTC645Sx/o+2Hf/holcYvVQV2vL1CfjQRv7IO1YIiSkwFwZg9qPFWwTSHRGL2D342+ UmhCDQFrUZhMjqzujwawLsrvhuyiIBN0qDSqFIAnk1Hu0luUqT6Tdf5s58oiuJJZOjAi QSFkaBdGrrEfeDRx/ATFTEUt8kqsDReilNurIjFdi34LoTe77CAYzNe8HCkjziudHehH DzUrXL+kPCrmV/J1qr/eWOua+pLdkN3emVvLRMnMK/2ecEOIENHRVQpH0piCzRxXgzqu qIgDkkzb+26gqAfdsFlc4Qb08RMEa4He+NY2kfClr4QCaJJRz68imWj532NsYgEIqco0 NqGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717805209; x=1718410009; 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=U44HT/MSZO+MfGZ9R6nvDDZFtdhwkwnAqQLWwNh4/1A=; b=DgaYk1KI3Mb9KCoEYww4W+Wegk4ZwE/xYxpuR3MeX1L074YoNwJOpLXRJk2nfVssA2 unTNP87Ukup8quuNtbrh8Ng7/HAoWOfrzqvF4pVnLSE0zo3uurIKQ89ubVr1tEH3/L3U jgn0knrU2lj3qkR28zoEvjbVLCBP5bsnBf/gtJDeRf5DzNC+WoxSpFRGOAQBaEdZv1uQ RZV3AUH7Yy8+Q75+mWXa9wUVH2pCe/htgCyYUE8YoEfUz2HJnZKMXT32A0oxx41C3lGE Avn4w5D0o1lLn2FVTeMhnTuvsFf/HJ8U0NRknCAERa1SkK7Utrq0zLqOK0IfPjBVQAbm Efuw== X-Forwarded-Encrypted: i=1; AJvYcCW17nEyBuihy3+Q6vBal2tjeJivxzr7eSVuHxMm2POoKVVDlMnpG5v7m4GwEuXU1mlS6CJEZlQzpUsreMt5n2cwpJpSzHINB6+97NpV X-Gm-Message-State: AOJu0YzlRx8fV/AqV6bG0WYbW7k71IvL6Zks3HPNKdheL36uY7t3e3iM qV92cvnUoXBkj7W7zHPn+Hag9o3pQydfMKhxPJjNZ0dvaQxiJl0dl38+fwFHN4Ur8QWUwpaDjBt /+w== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:18cf:b0:dfa:48f9:1855 with SMTP id 3f1490d57ef6-dfaf65228b6mr492235276.3.1717805209598; Fri, 07 Jun 2024 17:06:49 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 7 Jun 2024 17:06:35 -0700 In-Reply-To: <20240608000639.3295768-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: <20240608000639.3295768-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240608000639.3295768-5-seanjc@google.com> Subject: [PATCH v3 4/8] KVM: Add a module param to allow enabling virtualization when KVM is loaded From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Gao , Kai Huang Content-Type: text/plain; charset="UTF-8" Add an off-by-default module param, enable_virt_at_load, to let userspace force virtualization to be enabled in hardware when KVM is initialized, i.e. just before /dev/kvm is exposed to userspace. Enabling virtualization during KVM initialization allows userspace to avoid the additional latency when creating/destroying the first/last VM. Now that KVM uses the cpuhp framework to do per-CPU enabling, the latency could be non-trivial as the cpuhup bringup/teardown is serialized across CPUs, e.g. the latency could be problematic for use case that need to spin up VMs quickly. Enabling virtualizaton during initialization will also allow KVM to setup the Intel TDX Module, which requires VMX to be fully enabled, without needing additional APIs to temporarily enable virtualization. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 98e52d12f137..7bdd744e4821 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5495,6 +5495,9 @@ static struct miscdevice kvm_dev = { }; #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING +static bool enable_virt_at_load; +module_param(enable_virt_at_load, bool, 0444); + __visible bool kvm_rebooting; EXPORT_SYMBOL_GPL(kvm_rebooting); @@ -5645,15 +5648,41 @@ static void kvm_disable_virtualization(void) unregister_syscore_ops(&kvm_syscore_ops); cpuhp_remove_state(CPUHP_AP_KVM_ONLINE); } + +static int kvm_init_virtualization(void) +{ + if (enable_virt_at_load) + return kvm_enable_virtualization(); + + return 0; +} + +static void kvm_uninit_virtualization(void) +{ + if (enable_virt_at_load) + kvm_disable_virtualization(); + + WARN_ON(kvm_usage_count); +} #else /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */ static int kvm_enable_virtualization(void) { return 0; } +static int kvm_init_virtualization(void) +{ + return 0; +} + static void kvm_disable_virtualization(void) { +} + +static void kvm_uninit_virtualization(void) +{ + } #endif /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */ @@ -6395,6 +6424,10 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) kvm_gmem_init(module); + r = kvm_init_virtualization(); + if (r) + goto err_virt; + /* * Registration _must_ be the very last thing done, as this exposes * /dev/kvm to userspace, i.e. all infrastructure must be setup! @@ -6408,6 +6441,8 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) return 0; err_register: + kvm_uninit_virtualization(); +err_virt: kvm_vfio_ops_exit(); err_vfio: kvm_async_pf_deinit(); @@ -6433,6 +6468,8 @@ void kvm_exit(void) */ misc_deregister(&kvm_dev); + kvm_uninit_virtualization(); + debugfs_remove_recursive(kvm_debugfs_dir); for_each_possible_cpu(cpu) free_cpumask_var(per_cpu(cpu_kick_mask, cpu)); -- 2.45.2.505.gda0bf45e8d-goog