Skip to content

Commit

Permalink
KVM: SVM: Create SEV cgroup controller.
Browse files Browse the repository at this point in the history
Create SEV cgroup controller for SEV ASIDs on the AMD platform.

SEV ASIDs are used to encrypt virtual machines memory and isolate the
guests from the hypervisor. However, number of SEV ASIDs are limited on
a platform. This leads to the resource constraints and cause issues
like:

1. Some applications exhausting all of the SEV ASIDs and depriving
   others on a host.
2. No capability with the system admin to allocate and limit the number
   of SEV ASIDs used by tasks.
3. Difficult for the cloud service providers to optimally schedule VMs
   and sandboxes across its fleet without knowing the overall picture of
   SEV ASIDs usage.

SEV controller tracks the usage and provides capability to limit SEV
ASIDs used by tasks.

Controller is enabled by CGROUP_SEV config option, it is dependent on
KVM_AMD_SEV option in the config file.

SEV Controller has 3 interface files:

1. max - Sets the max limit of the SEV ASIDs in the cgroup.

2. current - Shows the current count of the SEV ASIDs in the cgroup.

3. events - Event file to show the SEV ASIDs allocation denied in the
	    cgroup.

When kvm-amd module is installed it calls SEV controller API and informs
how many SEV ASIDs are available on the platform. Controller use this
value to allocate an array which stores ASID to cgroup mapping.

New SEV ASID allocation gets charged to the task's SEV cgroup. Migration
of charge is not supported, so, a charged ASID remains charged to the
same cgroup until that SEV ASID is freed. This feature is similar to the
memory cgroup as it is a stateful resource

On deletion of an empty cgroup whose tasks have moved to some other
cgroup but a SEV ASID is still charged to it, the SEV ASID gets mapped
to the parent cgroup.

Mapping array tells which cgroup to uncharge, and update mapping when
the cgroup is deleted. Mapping array is freed when kvm-amd module is
unloaded.

Signed-off-by: Vipin Sharma <vipinsh@google.com>
Reviewed-by: David Rientjes <rientjes@google.com>
Reviewed-by: Dionna Glaze <dionnaglaze@google.com>
Reviewed-by: Erdem Aktas <erdemaktas@google.com>
  • Loading branch information
shvipin authored and intel-lab-lkp committed Sep 22, 2020
1 parent e792415 commit a6ea990
Show file tree
Hide file tree
Showing 6 changed files with 487 additions and 1 deletion.
1 change: 1 addition & 0 deletions arch/x86/kvm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \
i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \
hyperv.o debugfs.o mmu/mmu.o mmu/page_track.o
kvm-$(CONFIG_CGROUP_SEV) += svm/sev_cgroup.o

kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o vmx/evmcs.o vmx/nested.o
kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o svm/sev.o
Expand Down
16 changes: 15 additions & 1 deletion arch/x86/kvm/svm/sev.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "x86.h"
#include "svm.h"
#include "sev_cgroup.h"

static int sev_flush_asids(void);
static DECLARE_RWSEM(sev_deactivate_lock);
Expand Down Expand Up @@ -80,7 +81,7 @@ static bool __sev_recycle_asids(void)
static int sev_asid_new(void)
{
bool retry = true;
int pos;
int pos, ret;

mutex_lock(&sev_bitmap_lock);

Expand All @@ -98,6 +99,12 @@ static int sev_asid_new(void)
return -EBUSY;
}

ret = sev_asid_try_charge(pos);
if (ret) {
mutex_unlock(&sev_bitmap_lock);
return ret;
}

__set_bit(pos, sev_asid_bitmap);

mutex_unlock(&sev_bitmap_lock);
Expand Down Expand Up @@ -127,6 +134,8 @@ static void sev_asid_free(int asid)
sd->sev_vmcbs[pos] = NULL;
}

sev_asid_uncharge(pos);

mutex_unlock(&sev_bitmap_lock);
}

Expand Down Expand Up @@ -1142,6 +1151,9 @@ int __init sev_hardware_setup(void)
if (!status)
return 1;

if (sev_cgroup_setup(max_sev_asid))
return 1;

/*
* Check SEV platform status.
*
Expand All @@ -1156,6 +1168,7 @@ int __init sev_hardware_setup(void)
pr_info("SEV supported\n");

err:
sev_cgroup_teardown();
kfree(status);
return rc;
}
Expand All @@ -1169,6 +1182,7 @@ void sev_hardware_teardown(void)
bitmap_free(sev_reclaim_asid_bitmap);

sev_flush_asids();
sev_cgroup_teardown();
}

void pre_sev_run(struct vcpu_svm *svm, int cpu)
Expand Down
Loading

0 comments on commit a6ea990

Please sign in to comment.