1 From e858aad3ddcaa8a3c512c65c11dd9f20117febaa Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Wed, 31 Jan 2024 15:56:07 -0800
4 Subject: KVM: SVM: Use unsigned integers when dealing with ASIDs
6 From: Sean Christopherson <seanjc@google.com>
8 [ Upstream commit 466eec4a22a76c462781bf6d45cb02cbedf21a61 ]
10 Convert all local ASID variables and parameters throughout the SEV code
11 from signed integers to unsigned integers. As ASIDs are fundamentally
12 unsigned values, and the global min/max variables are appropriately
13 unsigned integers, too.
15 Functionally, this is a glorified nop as KVM guarantees min_sev_asid is
16 non-zero, and no CPU supports -1u as the _only_ asid, i.e. the signed vs.
17 unsigned goof won't cause problems in practice.
19 Opportunistically use sev_get_asid() in sev_flush_encrypted_page() instead
20 of open coding an equivalent.
22 Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
23 Link: https://lore.kernel.org/r/20240131235609.4161407-3-seanjc@google.com
24 Signed-off-by: Sean Christopherson <seanjc@google.com>
25 Stable-dep-of: 0aa6b90ef9d7 ("KVM: SVM: Add support for allowing zero SEV ASIDs")
26 Signed-off-by: Sasha Levin <sashal@kernel.org>
28 arch/x86/kvm/svm/sev.c | 18 ++++++++++--------
29 arch/x86/kvm/trace.h | 10 +++++-----
30 2 files changed, 15 insertions(+), 13 deletions(-)
32 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
33 index e86231c3b8a54..ea68a08cc89c2 100644
34 --- a/arch/x86/kvm/svm/sev.c
35 +++ b/arch/x86/kvm/svm/sev.c
36 @@ -84,9 +84,10 @@ struct enc_region {
39 /* Called with the sev_bitmap_lock held, or on shutdown */
40 -static int sev_flush_asids(int min_asid, int max_asid)
41 +static int sev_flush_asids(unsigned int min_asid, unsigned int max_asid)
43 - int ret, asid, error = 0;
47 /* Check if there are any ASIDs to reclaim before performing a flush */
48 asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid);
49 @@ -116,7 +117,7 @@ static inline bool is_mirroring_enc_context(struct kvm *kvm)
52 /* Must be called with the sev_bitmap_lock held */
53 -static bool __sev_recycle_asids(int min_asid, int max_asid)
54 +static bool __sev_recycle_asids(unsigned int min_asid, unsigned int max_asid)
56 if (sev_flush_asids(min_asid, max_asid))
58 @@ -143,8 +144,9 @@ static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)
60 static int sev_asid_new(struct kvm_sev_info *sev)
62 - int asid, min_asid, max_asid, ret;
63 + unsigned int asid, min_asid, max_asid;
67 WARN_ON(sev->misc_cg);
68 sev->misc_cg = get_current_misc_cg();
69 @@ -187,7 +189,7 @@ static int sev_asid_new(struct kvm_sev_info *sev)
73 -static int sev_get_asid(struct kvm *kvm)
74 +static unsigned int sev_get_asid(struct kvm *kvm)
76 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
78 @@ -284,8 +286,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
80 static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
82 + unsigned int asid = sev_get_asid(kvm);
83 struct sev_data_activate activate;
84 - int asid = sev_get_asid(kvm);
87 /* activate ASID on the given handle */
88 @@ -2314,7 +2316,7 @@ int sev_cpu_init(struct svm_cpu_data *sd)
90 static void sev_flush_encrypted_page(struct kvm_vcpu *vcpu, void *va)
92 - int asid = to_kvm_svm(vcpu->kvm)->sev_info.asid;
93 + unsigned int asid = sev_get_asid(vcpu->kvm);
96 * Note! The address must be a kernel address, as regular page walk
97 @@ -2632,7 +2634,7 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)
98 void pre_sev_run(struct vcpu_svm *svm, int cpu)
100 struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu);
101 - int asid = sev_get_asid(svm->vcpu.kvm);
102 + unsigned int asid = sev_get_asid(svm->vcpu.kvm);
104 /* Assign the asid allocated with this SEV guest */
106 diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h
107 index 83843379813ee..b82e6ed4f0241 100644
108 --- a/arch/x86/kvm/trace.h
109 +++ b/arch/x86/kvm/trace.h
110 @@ -732,13 +732,13 @@ TRACE_EVENT(kvm_nested_intr_vmexit,
111 * Tracepoint for nested #vmexit because of interrupt pending
113 TRACE_EVENT(kvm_invlpga,
114 - TP_PROTO(__u64 rip, int asid, u64 address),
115 + TP_PROTO(__u64 rip, unsigned int asid, u64 address),
116 TP_ARGS(rip, asid, address),
119 - __field( __u64, rip )
120 - __field( int, asid )
121 - __field( __u64, address )
122 + __field( __u64, rip )
123 + __field( unsigned int, asid )
124 + __field( __u64, address )
128 @@ -747,7 +747,7 @@ TRACE_EVENT(kvm_invlpga,
129 __entry->address = address;
132 - TP_printk("rip: 0x%016llx asid: %d address: 0x%016llx",
133 + TP_printk("rip: 0x%016llx asid: %u address: 0x%016llx",
134 __entry->rip, __entry->asid, __entry->address)