]> git.ipfire.org Git - people/ms/linux.git/commit
x86/sgx: Free backing memory after faulting the enclave page
authorJarkko Sakkinen <jarkko@kernel.org>
Thu, 3 Mar 2022 22:38:58 +0000 (00:38 +0200)
committerDave Hansen <dave.hansen@linux.intel.com>
Fri, 11 Mar 2022 18:31:06 +0000 (10:31 -0800)
commit08999b2489b4c9b939d7483dbd03702ee4576d96
tree1caeaa66ccb59a313116799eabcecf628cd602e9
parenta365a65f9ca1ceb9cf1ac29db4a4f51df7c507ad
x86/sgx: Free backing memory after faulting the enclave page

There is a limited amount of SGX memory (EPC) on each system.  When that
memory is used up, SGX has its own swapping mechanism which is similar
in concept but totally separate from the core mm/* code.  Instead of
swapping to disk, SGX swaps from EPC to normal RAM.  That normal RAM
comes from a shared memory pseudo-file and can itself be swapped by the
core mm code.  There is a hierarchy like this:

EPC <-> shmem <-> disk

After data is swapped back in from shmem to EPC, the shmem backing
storage needs to be freed.  Currently, the backing shmem is not freed.
This effectively wastes the shmem while the enclave is running.  The
memory is recovered when the enclave is destroyed and the backing
storage freed.

Sort this out by freeing memory with shmem_truncate_range(), as soon as
a page is faulted back to the EPC.  In addition, free the memory for
PCMD pages as soon as all PCMD's in a page have been marked as unused
by zeroing its contents.

Cc: stable@vger.kernel.org
Fixes: 1728ab54b4be ("x86/sgx: Add a page reclaimer")
Reported-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Link: https://lkml.kernel.org/r/20220303223859.273187-1-jarkko@kernel.org
arch/x86/kernel/cpu/sgx/encl.c