From nobody Sat May 10 03:18:21 2025
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 145A520C489
	for <linux-kernel@vger.kernel.org>; Tue,  1 Apr 2025 15:57:18 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=209.85.216.74
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743523040; cv=none;
 b=rDxT5mJBXPN8+q9+SoT/iPrWVYQQTEZK+fSS5KbaTZwVAIcrc5tpXBZe/PR0azWS8QXpcMB354fjQWV8/B0fO+mg4SEj2Af2svx1rW7qiMHduGSuqp5ZYdF5oKlvRxARmVBYqp6Zp6Ig7Zrl8Yj79IPGnZBBebOQ/4Zk5gE+CCA=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743523040; c=relaxed/simple;
	bh=XFMN/1TjDkt0VHZXeAmqM88pM/Yy9sojmu6ZVcp+G5A=;
	h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From:
	 To:Cc:Content-Type;
 b=QrKKBUHefiWrVGfb+1mjaaXm3BrOC9jpqd/aPjLAmoTnTGBxrNLlSxzfVgF4gJI9f2oWO2iStYwXVEJ2Q1oIVmfHDS0hSOdLUhTLOF8gOIbEcMKsTZrmShO9isBkLQK+GgfS7HALao0LqVBDtLgaLiUamrxmv1TaG4DP6UEXMko=
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=32CcTCKq; arc=none smtp.client-ip=209.85.216.74
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="32CcTCKq"
Received: by mail-pj1-f74.google.com with SMTP id
 98e67ed59e1d1-2ff799be8f5so9534983a91.1
        for <linux-kernel@vger.kernel.org>;
 Tue, 01 Apr 2025 08:57:18 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=google.com; s=20230601; t=1743523038; x=1744127838;
 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=XfD+/olMCjjTlr+W07rgl23+xXZX9pNmPmnT7WHnWhU=;
        b=32CcTCKqt2w3lQA/Q/QL5MQ4SJaL6xsMoDUbEf2R6UjFalcXqi/fpiUPyNUX8bY848
         DGUE+Kz9cN0kMDVnB6H2Jb/Ppm2ZfHJbRPAGQp9aabNhzW2KnkkYQCvmIAwnfUjca2cm
         6y5OLvBYqyT+rU2ePgqaLLBJaiHBu51b3oJ+pYIb83ReCiGlwOWGcFP7liyLqLQMzMsG
         KmVjkka4s3MYHeF7T3sz/eNhyn8LKoxqsmkZJ04G2crkAX85uf5J1sujsA+qHdoeVYwh
         LjWprGBws2qA/8eTlqJPVi8MwtlxKI8i641nqQRBFYTvAFROZI++hkIRBsdpjVlSCPLW
         4/Rw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20230601; t=1743523038; x=1744127838;
        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=XfD+/olMCjjTlr+W07rgl23+xXZX9pNmPmnT7WHnWhU=;
        b=Z34423uxinP79BLThALehkX5wzOrLQQTHET0xxyIEiy2KACmujJnOQfcU3+t9C4by6
         wmO3VxAlmEyTDuj4ZFpscOZKyHZXtx8RSk1AudSMIlhwdZUBQjwx7YlG6cq4zpwd/V9I
         mWjD/kBVM0yOBRAkm66aIqYJ1AvcKKMXf3f2F9PWFB6XNfUL9Z5y4CLGvwgE93sAi3Ys
         FNHA+UHVOrcaniVewf16VUEQK/Zk91b3AkcvY1ngAjx9eeM9k81aBe4PtV9HFoYMpmB8
         B3/4TY9GUFlGs97L6mOzgcqabvi2eVL+MYyyNBITU1zcvqit/sB12Om/kNvo1JFpqIee
         RgTQ==
X-Forwarded-Encrypted: i=1;
 AJvYcCU7yvXSk48h5CHlvEVGV8zzIDIJX2c4w3TSo+zB2m4dq4w6p1wXeF0C8hKwDBtDk/4ts8Z7n/emCOVv3QA=@vger.kernel.org
X-Gm-Message-State: AOJu0Yy293GSg+rPCj6vr+FqG5pycqTOngCw/rS6IW/Sd3NzzBPO41wr
	d4H0uCkw9gB0aXfVlYuayzHqVtZHRL/nY5rFNfY0jfesSvWcrqxCwMIx714ugs6d1DeQ1sn+cGy
	Yeg==
X-Google-Smtp-Source: 
 AGHT+IGCmi+AM1Kq1m2h89y+RWMJjrT8URQip51SgDFCFbGhP2V4jxmx+W28wYOkSn98iKOm32ePbQd01rw=
X-Received: from pjyp8.prod.google.com ([2002:a17:90a:e708:b0:2ef:78ff:bc3b])
 (user=seanjc job=prod-delivery.src-stubby-dispatcher) by
 2002:a17:90b:3b85:b0:2ff:6e72:b8e9
 with SMTP id 98e67ed59e1d1-30532154023mr19843757a91.25.1743523038368; Tue, 01
 Apr 2025 08:57:18 -0700 (PDT)
Reply-To: Sean Christopherson <seanjc@google.com>
Date: Tue,  1 Apr 2025 08:57:12 -0700
In-Reply-To: <20250401155714.838398-1-seanjc@google.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
Mime-Version: 1.0
References: <20250401155714.838398-1-seanjc@google.com>
X-Mailer: git-send-email 2.49.0.472.ge94155a9ec-goog
Message-ID: <20250401155714.838398-2-seanjc@google.com>
Subject: [PATCH v2 1/3] KVM: x86/mmu: Dynamically allocate shadow MMU's hashed
 page list
From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
 Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Vipin Sharma <vipinsh@google.com>
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

Dynamically allocate the (massive) array of hashed lists used to track
shadow pages, as the array itself is 32KiB, i.e. is an order-3 allocation
all on its own, and is *exactly* an order-3 allocation.  Dynamically
allocating the array will allow allocating "struct kvm" using regular
kmalloc(), and will also allow deferring allocation of the array until
it's actually needed, i.e. until the first shadow root is allocated.

Cc: Vipin Sharma <vipinsh@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Vipin Sharma <vipinsh@google.com>
---
 arch/x86/include/asm/kvm_host.h |  4 ++--
 arch/x86/kvm/mmu/mmu.c          | 23 ++++++++++++++++++++++-
 arch/x86/kvm/x86.c              |  5 ++++-
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos=
t.h
index a884ab544335..e523d7d8a107 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1333,7 +1333,7 @@ struct kvm_arch {
 	bool has_private_mem;
 	bool has_protected_state;
 	bool pre_fault_allowed;
-	struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
+	struct hlist_head *mmu_page_hash;
 	struct list_head active_mmu_pages;
 	/*
 	 * A list of kvm_mmu_page structs that, if zapped, could possibly be
@@ -1985,7 +1985,7 @@ void kvm_mmu_vendor_module_exit(void);
=20
 void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
 int kvm_mmu_create(struct kvm_vcpu *vcpu);
-void kvm_mmu_init_vm(struct kvm *kvm);
+int kvm_mmu_init_vm(struct kvm *kvm);
 void kvm_mmu_uninit_vm(struct kvm *kvm);
=20
 void kvm_mmu_init_memslot_memory_attributes(struct kvm *kvm,
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 63bb77ee1bb1..6b9c72405860 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -3880,6 +3880,18 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *v=
cpu)
 	return r;
 }
=20
+static int kvm_mmu_alloc_page_hash(struct kvm *kvm)
+{
+	typeof(kvm->arch.mmu_page_hash) h;
+
+	h =3D kcalloc(KVM_NUM_MMU_PAGES, sizeof(*h), GFP_KERNEL_ACCOUNT);
+	if (!h)
+		return -ENOMEM;
+
+	kvm->arch.mmu_page_hash =3D h;
+	return 0;
+}
+
 static int mmu_first_shadow_root_alloc(struct kvm *kvm)
 {
 	struct kvm_memslots *slots;
@@ -6673,13 +6685,19 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)
 		kvm_tdp_mmu_zap_invalidated_roots(kvm, true);
 }
=20
-void kvm_mmu_init_vm(struct kvm *kvm)
+int kvm_mmu_init_vm(struct kvm *kvm)
 {
+	int r;
+
 	kvm->arch.shadow_mmio_value =3D shadow_mmio_value;
 	INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
 	INIT_LIST_HEAD(&kvm->arch.possible_nx_huge_pages);
 	spin_lock_init(&kvm->arch.mmu_unsync_pages_lock);
=20
+	r =3D kvm_mmu_alloc_page_hash(kvm);
+	if (r)
+		return r;
+
 	if (tdp_mmu_enabled)
 		kvm_mmu_init_tdp_mmu(kvm);
=20
@@ -6690,6 +6708,7 @@ void kvm_mmu_init_vm(struct kvm *kvm)
=20
 	kvm->arch.split_desc_cache.kmem_cache =3D pte_list_desc_cache;
 	kvm->arch.split_desc_cache.gfp_zero =3D __GFP_ZERO;
+	return 0;
 }
=20
 static void mmu_free_vm_memory_caches(struct kvm *kvm)
@@ -6701,6 +6720,8 @@ static void mmu_free_vm_memory_caches(struct kvm *kvm)
=20
 void kvm_mmu_uninit_vm(struct kvm *kvm)
 {
+	kfree(kvm->arch.mmu_page_hash);
+
 	if (tdp_mmu_enabled)
 		kvm_mmu_uninit_tdp_mmu(kvm);
=20
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c841817a914a..4070f9d34521 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -12721,7 +12721,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long=
 type)
 	if (ret)
 		goto out;
=20
-	kvm_mmu_init_vm(kvm);
+	ret =3D kvm_mmu_init_vm(kvm);
+	if (ret)
+		goto out_cleanup_page_track;
=20
 	ret =3D kvm_x86_call(vm_init)(kvm);
 	if (ret)
@@ -12774,6 +12776,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long=
 type)
=20
 out_uninit_mmu:
 	kvm_mmu_uninit_vm(kvm);
+out_cleanup_page_track:
 	kvm_page_track_cleanup(kvm);
 out:
 	return ret;
--=20
2.49.0.472.ge94155a9ec-goog