From nobody Sat Feb 7 09:09:51 2026 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 26FE82D1034 for ; Tue, 10 Jun 2025 22:58:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749596313; cv=none; b=V6UmlkFodxEjAxVwhKaeLCNiz6rBBX97FLHHpLMoIgkUhpl3jgcyi7grhP5Wkpd50jKwcxo8BhoQzkmW1qLUrshxUOGbfw65PS7irto+YinnzDVfC1lSWFafJGD+JIOORsDMd2JZm1F5x0sO3J8Duf9aYtY0aX3qpyQuazWYCGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749596313; c=relaxed/simple; bh=VH8WRfFFUUs5JJlTZjXJW3PCCeZLDh78G1qzdoW4OoU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Z23+zhtItxcYoOcN9gn4ctbrXP4AtBjbFlCcVf5NVkO5GHtgqoALfKrbT0gG88lKrK7z92fypdAXzxR11xAFqnNTgeGsy72PWUVf3b6r6q5+SlPg/o2V/vVbHJMmRDEWEHMJOMH+ioQUoLXsrPsbntCC0xGEwM86CwwvyQobPO8= 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=W3ymdE3r; arc=none smtp.client-ip=209.85.210.202 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="W3ymdE3r" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-74620e98ec8so234842b3a.1 for ; Tue, 10 Jun 2025 15:58:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749596312; x=1750201112; 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=A6jp+MxEVwUWr2OGrLLa/UhZgsS9fBawhSOksR6+SUo=; b=W3ymdE3rUHwwtQmNBZETi7DM3KoU9YnjkteSoOKFM+6okzOk1vj+YKx9fwTXkYmyC+ qZRE3w8+1pJESZwwI8OuYzvWXCs6HstBnzGFinNDUfmtydKdXoIBjRLwg006fpi+k8LO urhVWp1a6zUVVY4a81EhddGrzl3W2wDHxI215NvdRSx3mbX4/OEVS2tGTUMSwwVlEBVe /tynQa/9tAODoAy0r9Gn8hDpMfQ1HEyCMe8hMg2K+EbEzx3Pf0KKtW2x2RQ36sAG0is7 eN9+nsNEhs/tMgtIjV7UPuQqFfnfQjvpJan74G/9A3winCZj4DaULQV+zHooGvsomCdr +0CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749596312; x=1750201112; 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=A6jp+MxEVwUWr2OGrLLa/UhZgsS9fBawhSOksR6+SUo=; b=un93o14yUwQ/ipMGZS44BuMbrRP9sZsc6BhJmTmR8vDq8G8ILVueTKm8RdEdPqt37C wEYxwkQJ1C6dtBl4QCwqGdP1RJykH9pAPn+GcEjzoPP2BkG1/i8Dk9WtRuN+6AyG9gdn MpXpZnSJZP3hkASV/FlZhP+v6FVvgOjmVQOHw7yJcOFVVxjaSIMHOacDuiIhMZd8+ThW 763D+1R2JVz4A5ByVQqkXJ4Zi3CRDgRtLy/L9KbJQ99KpUtOD6SA3NoFCemj5Zh5VDr7 1ihzbF0jBy1UX4aeUth1PE4iXW/x+kQjGn5w2NpO9qJedFEw1DNl5FkEAzgj+DWiXfAJ NOMg== X-Forwarded-Encrypted: i=1; AJvYcCXdiuNn49p062XiEGFr6epjlXIF6V25mMjms4tYGWrF4ON3jOeerAdseJXN3OY2AahS6lvC4//VxZTciqM=@vger.kernel.org X-Gm-Message-State: AOJu0YzrWCo8fgqrUisAOHv7l6ezIpXnR5iLLJUdEMho4iA1CZYUEYU9 RBf8i8LH3gkQzpNhF5FUxHHSGBn0BwaIFGf276lKo6xYaUv2dbdBTrch+bC6TnT0cfBxWi2irLC 5oDUvgA== X-Google-Smtp-Source: AGHT+IHUbTWEMdxOysQIgDGJSqdbtnuSvVC/t8qT8y4a8ukS+CVQAVwHYn3Az15lUwMqTPo8MV2aPVRifBI= X-Received: from pfax8.prod.google.com ([2002:aa7:9188:0:b0:746:fd4c:1fd0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:17a3:b0:748:6a12:1b47 with SMTP id d2e1a72fcca58-7486d3729a9mr1291570b3a.10.1749596311602; Tue, 10 Jun 2025 15:58:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 10 Jun 2025 15:57:35 -0700 In-Reply-To: <20250610225737.156318-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: <20250610225737.156318-1-seanjc@google.com> X-Mailer: git-send-email 2.50.0.rc0.642.g800a2b2222-goog Message-ID: <20250610225737.156318-31-seanjc@google.com> Subject: [PATCH v2 30/32] KVM: SVM: Add a helper to allocate and initialize permissions bitmaps From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Gao , Borislav Petkov , Xin Li , Dapeng Mi , Francesco Lavra , Manali Shukla Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper to allocate and initialize an MSR or I/O permissions map, as the logic is identical between the two map types, the only difference is the size of the bitmap. Opportunistically add a comment to explain why the bitmaps are initialized with 0xff, e.g. instead of the more common zero-initialized behavior, which is the main motivation for deduplicating the code. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 31 +++++++++++++++---------------- arch/x86/kvm/svm/svm.h | 8 +++++++- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index fc41ec70b6de..e3c49c763225 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -728,19 +728,23 @@ void svm_enable_intercept_for_msr(struct kvm_vcpu *vc= pu, u32 msr, int type) svm->nested.force_msr_bitmap_recalc =3D true; } =20 -void *svm_vcpu_alloc_msrpm(void) +void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask) { - unsigned int order =3D get_order(MSRPM_SIZE); - struct page *pages =3D alloc_pages(GFP_KERNEL_ACCOUNT, order); - void *msrpm; + unsigned int order =3D get_order(size); + struct page *pages =3D alloc_pages(gfp_mask, order); + void *pm; =20 if (!pages) return NULL; =20 - msrpm =3D page_address(pages); - memset(msrpm, 0xff, PAGE_SIZE * (1 << order)); + /* + * Set all bits in the permissions map so that all MSR and I/O accesses + * are intercepted by default. + */ + pm =3D page_address(pages); + memset(pm, 0xff, PAGE_SIZE * (1 << order)); =20 - return msrpm; + return pm; } =20 static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) @@ -5325,11 +5329,8 @@ static __init void svm_set_cpu_caps(void) =20 static __init int svm_hardware_setup(void) { - int cpu; - struct page *iopm_pages; void *iopm_va; - int r; - unsigned int order =3D get_order(IOPM_SIZE); + int cpu, r; =20 /* * NX is required for shadow paging and for NPT if the NX huge pages @@ -5410,13 +5411,11 @@ static __init int svm_hardware_setup(void) pr_info("LBR virtualization supported\n"); } =20 - iopm_pages =3D alloc_pages(GFP_KERNEL, order); - if (!iopm_pages) + iopm_va =3D svm_alloc_permissions_map(IOPM_SIZE, GFP_KERNEL); + if (!iopm_va) return -ENOMEM; =20 - iopm_va =3D page_address(iopm_pages); - memset(iopm_va, 0xff, PAGE_SIZE * (1 << order)); - iopm_base =3D __sme_page_pa(iopm_pages); + iopm_base =3D __sme_set(__pa(iopm_va)); =20 /* * Note, SEV setup consumes npt_enabled and enable_mmio_caching (which diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 489adc2ca3f5..8d3279563261 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -667,7 +667,13 @@ BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set) /* svm.c */ extern bool dump_invalid_vmcb; =20 -void *svm_vcpu_alloc_msrpm(void); +void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask); + +static inline void *svm_vcpu_alloc_msrpm(void) +{ + return svm_alloc_permissions_map(MSRPM_SIZE, GFP_KERNEL_ACCOUNT); +} + void svm_vcpu_free_msrpm(void *msrpm); void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb); void svm_enable_lbrv(struct kvm_vcpu *vcpu); --=20 2.50.0.rc0.642.g800a2b2222-goog