Skip to content

Commit bb2baeb

Browse files
mzhang3579bonzini
authored andcommitted
KVM: SVM: improve the code readability for ASID management
KVM SEV code uses bitmaps to manage ASID states. ASID 0 was always skipped because it is never used by VM. Thus, in existing code, ASID value and its bitmap postion always has an 'offset-by-1' relationship. Both SEV and SEV-ES shares the ASID space, thus KVM uses a dynamic range [min_asid, max_asid] to handle SEV and SEV-ES ASIDs separately. Existing code mixes the usage of ASID value and its bitmap position by using the same variable called 'min_asid'. Fix the min_asid usage: ensure that its usage is consistent with its name; allocate extra size for ASID 0 to ensure that each ASID has the same value with its bitmap position. Add comments on ASID bitmap allocation to clarify the size change. Signed-off-by: Mingwei Zhang <[email protected]> Cc: Tom Lendacky <[email protected]> Cc: Marc Orr <[email protected]> Cc: David Rientjes <[email protected]> Cc: Alper Gun <[email protected]> Cc: Dionna Glaze <[email protected]> Cc: Sean Christopherson <[email protected]> Cc: Vipin Sharma <[email protected]> Cc: Peter Gonda <[email protected]> Cc: Joerg Roedel <[email protected]> Message-Id: <[email protected]> [Fix up sev_asid_free to also index by ASID, as suggested by Sean Christopherson, and use nr_asids in sev_cpu_init. - Paolo] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 179c6c2 commit bb2baeb

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static DEFINE_MUTEX(sev_bitmap_lock);
6464
unsigned int max_sev_asid;
6565
static unsigned int min_sev_asid;
6666
static unsigned long sev_me_mask;
67+
static unsigned int nr_asids;
6768
static unsigned long *sev_asid_bitmap;
6869
static unsigned long *sev_reclaim_asid_bitmap;
6970

@@ -78,11 +79,11 @@ struct enc_region {
7879
/* Called with the sev_bitmap_lock held, or on shutdown */
7980
static int sev_flush_asids(int min_asid, int max_asid)
8081
{
81-
int ret, pos, error = 0;
82+
int ret, asid, error = 0;
8283

8384
/* Check if there are any ASIDs to reclaim before performing a flush */
84-
pos = find_next_bit(sev_reclaim_asid_bitmap, max_asid, min_asid);
85-
if (pos >= max_asid)
85+
asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid);
86+
if (asid > max_asid)
8687
return -EBUSY;
8788

8889
/*
@@ -115,15 +116,15 @@ static bool __sev_recycle_asids(int min_asid, int max_asid)
115116

116117
/* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */
117118
bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
118-
max_sev_asid);
119-
bitmap_zero(sev_reclaim_asid_bitmap, max_sev_asid);
119+
nr_asids);
120+
bitmap_zero(sev_reclaim_asid_bitmap, nr_asids);
120121

121122
return true;
122123
}
123124

124125
static int sev_asid_new(struct kvm_sev_info *sev)
125126
{
126-
int pos, min_asid, max_asid, ret;
127+
int asid, min_asid, max_asid, ret;
127128
bool retry = true;
128129
enum misc_res_type type;
129130

@@ -143,11 +144,11 @@ static int sev_asid_new(struct kvm_sev_info *sev)
143144
* SEV-enabled guests must use asid from min_sev_asid to max_sev_asid.
144145
* SEV-ES-enabled guest can use from 1 to min_sev_asid - 1.
145146
*/
146-
min_asid = sev->es_active ? 0 : min_sev_asid - 1;
147+
min_asid = sev->es_active ? 1 : min_sev_asid;
147148
max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid;
148149
again:
149-
pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_asid);
150-
if (pos >= max_asid) {
150+
asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid);
151+
if (asid > max_asid) {
151152
if (retry && __sev_recycle_asids(min_asid, max_asid)) {
152153
retry = false;
153154
goto again;
@@ -157,11 +158,11 @@ static int sev_asid_new(struct kvm_sev_info *sev)
157158
goto e_uncharge;
158159
}
159160

160-
__set_bit(pos, sev_asid_bitmap);
161+
__set_bit(asid, sev_asid_bitmap);
161162

162163
mutex_unlock(&sev_bitmap_lock);
163164

164-
return pos + 1;
165+
return asid;
165166
e_uncharge:
166167
misc_cg_uncharge(type, sev->misc_cg, 1);
167168
put_misc_cg(sev->misc_cg);
@@ -179,13 +180,12 @@ static int sev_get_asid(struct kvm *kvm)
179180
static void sev_asid_free(struct kvm_sev_info *sev)
180181
{
181182
struct svm_cpu_data *sd;
182-
int cpu, pos;
183+
int cpu;
183184
enum misc_res_type type;
184185

185186
mutex_lock(&sev_bitmap_lock);
186187

187-
pos = sev->asid - 1;
188-
__set_bit(pos, sev_reclaim_asid_bitmap);
188+
__set_bit(sev->asid, sev_reclaim_asid_bitmap);
189189

190190
for_each_possible_cpu(cpu) {
191191
sd = per_cpu(svm_data, cpu);
@@ -1857,12 +1857,17 @@ void __init sev_hardware_setup(void)
18571857
min_sev_asid = edx;
18581858
sev_me_mask = 1UL << (ebx & 0x3f);
18591859

1860-
/* Initialize SEV ASID bitmaps */
1861-
sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
1860+
/*
1861+
* Initialize SEV ASID bitmaps. Allocate space for ASID 0 in the bitmap,
1862+
* even though it's never used, so that the bitmap is indexed by the
1863+
* actual ASID.
1864+
*/
1865+
nr_asids = max_sev_asid + 1;
1866+
sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
18621867
if (!sev_asid_bitmap)
18631868
goto out;
18641869

1865-
sev_reclaim_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
1870+
sev_reclaim_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL);
18661871
if (!sev_reclaim_asid_bitmap) {
18671872
bitmap_free(sev_asid_bitmap);
18681873
sev_asid_bitmap = NULL;
@@ -1907,7 +1912,7 @@ void sev_hardware_teardown(void)
19071912
return;
19081913

19091914
/* No need to take sev_bitmap_lock, all VMs have been destroyed. */
1910-
sev_flush_asids(0, max_sev_asid);
1915+
sev_flush_asids(1, max_sev_asid);
19111916

19121917
bitmap_free(sev_asid_bitmap);
19131918
bitmap_free(sev_reclaim_asid_bitmap);
@@ -1921,7 +1926,7 @@ int sev_cpu_init(struct svm_cpu_data *sd)
19211926
if (!sev_enabled)
19221927
return 0;
19231928

1924-
sd->sev_vmcbs = kcalloc(max_sev_asid + 1, sizeof(void *), GFP_KERNEL);
1929+
sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL);
19251930
if (!sd->sev_vmcbs)
19261931
return -ENOMEM;
19271932

0 commit comments

Comments
 (0)