x86/sgx: Limit EPC overcommit
v2 cover x86/sgx: Limit EPC overcommit
SGX currently allows EPC pages to be overcommitted. If the system is out of enclave memory, EPC pages are swapped to normal RAM via a per enclave shared memory area. This shared memory is not charged to the enclave or the task mapping it, making it hard to account for using normal methods. Since SGX will allow EPC pages to be overcommitted without limits, enclaves can consume system memory for these backing pages without limits.
In order to prevent this, set a cap on the amount of overcommit SGX allows. Whenever a backing page is requested by an enclave, track the total amount of shared memory pages used across all enclaves and return an error if the overcommit limit has been reached. This will restrict the total amount of backing pages that all enclaves can consume to a maximum amount, and prevent enclaves from consuming all the system RAM for backing pages.
The overcommit percentage has a value of 150, which limits shared memory page consumption to 1.5x the number of EPC pages in the system.
Changes from v1
----------------
* removed module parameter and disable boolean
* increased over commit percentage to 150% from 100%
Kristen Carlson Accardi (2):
x86/sgx: Add accounting for tracking overcommit
x86/sgx: account backing pages
arch/x86/kernel/cpu/sgx/encl.c | 76 ++++++++++++++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/encl.h | 6 ++- arch/x86/kernel/cpu/sgx/main.c | 52 +++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/sgx.h | 2 + 4 files changed, 128 insertions(+), 8 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 1/2 x86/sgx: Add accounting for tracking overcommit
When the system runs out of enclave memory, SGX can reclaim EPC pages by swapping to normal RAM. This normal RAM is allocated via a per-enclave shared memory area. The shared memory area is not mapped into the enclave or the task mapping it, which makes its memory use opaque (including to the OOM killer). Having lots of hard to find memory around is problematic, especially when there is no limit.
Introduce a global counter that can be used to limit the number of pages that enclaves are able to consume for backing storage. This parameter is a percentage value that is used in conjunction with the number of EPC pages in the system to set a cap on the amount of backing RAM that can be consumed.
The overcommit percentage value is 150, which limits the total number of shared memory pages that may be consumed by all enclaves as backing pages to 1.5X of EPC pages on the system.
For example, on an SGX system that has 128MB of EPC, this would cap the amount of normal RAM that SGX consumes for its shared memory areas at 192MB.
The SGX overcommit percent works differently than the core VM overcommit limit. Enclaves request backing pages one page at a time, and the number of in use backing pages that are allowed is a global resource that is limited for all enclaves.
Introduce a pair of functions which can be used by callers when requesting backing RAM pages. These functions are responsible for accounting the page charges. A request may return an error if the request will cause the counter to exceed the backing page cap.
--- arch/x86/kernel/cpu/sgx/main.c | 46 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 2 ++ 2 files changed, 48 insertions(+)
arch/x86/kernel/cpu/sgx/main.c +46 -0
@@ -43,6 +43,45 @@ static struct sgx_numa_node *sgx_numa_nodes;static LIST_HEAD (sgx_dirty_page_list); +/* +* Limits the amount of normal RAM that SGX can consume for EPC +* overcommit to the total EPC pages * sgx_overcommit_percent / 100 +*/ +static int sgx_overcommit_percent =150 ; + +/* The number of pages that can be allocated globally for backing storage. */ +static atomic_long_t sgx_nr_available_backing_pages; + +/** +* sgx_charge_mem() - charge for a page used for backing storage +* +* Backing storage usage is capped by the sgx_nr_available_backing_pages. +* If the backing storage usage is over the overcommit limit, +* return an error. +* +* Return: +* 0: The page requested does not exceed the limit +* -ENOMEM: The page requested exceeds the overcommit limit +*/ +int sgx_charge_mem (void ) +{ +if (!atomic_long_add_unless (&sgx_nr_available_backing_pages,-1 ,0 )) +return -ENOMEM ; + +return 0 ; +} + +/** +* sgx_uncharge_mem() - uncharge a page previously used for backing storage +* +* When backing storage is no longer in use, increment the +* sgx_nr_available_backing_pages counter. +*/ +void sgx_uncharge_mem (void ) +{ +atomic_long_inc (&sgx_nr_available_backing_pages); +} +/* * Reset post-kexec EPC pages to the uninitialized state. The pages are removed * from the input list, and made available for the page allocator. SECS pages
@@ -786,6 +825,8 @@ static bool __init sgx_page_cache_init(void)u64 pa, size;int nid;int i; +u64 total_epc_bytes =0 ; +u64 available_backing_bytes; sgx_numa_nodes =kmalloc_array (num_possible_nodes (),sizeof (*sgx_numa_nodes),GFP_KERNEL );if (!sgx_numa_nodes)
@@ -830,6 +871,7 @@ static bool __init sgx_page_cache_init(void) sgx_epc_sections[i].node = &sgx_numa_nodes[nid]; sgx_numa_nodes[nid].size += size; + total_epc_bytes += size; sgx_nr_epc_sections++; }
@@ -839,6 +881,10 @@ static bool __init sgx_page_cache_init(void)return false; } + available_backing_bytes = total_epc_bytes * (sgx_overcommit_percent /100 ); + +atomic_long_set (&sgx_nr_available_backing_pages, available_backing_bytes >>PAGE_SHIFT ); +return true; }
arch/x86/kernel/cpu/sgx/sgx.h +2 -0
@@ -89,6 +89,8 @@ void sgx_free_epc_page(struct sgx_epc_page *page);void sgx_mark_page_reclaimable (struct sgx_epc_page *page);int sgx_unmark_page_reclaimable (struct sgx_epc_page *page);struct sgx_epc_page *sgx_alloc_epc_page (void *owner,bool reclaim); +int sgx_charge_mem (void ); +void sgx_uncharge_mem (void );#ifdef CONFIG_X86_SGX_KVM int __init sgx_vepc_init (void );
Signature · 1 line
2.20.1
PATCH v2 2/2 x86/sgx: account backing pages
In reply to Kristen Carlson AccardiSGX may allow EPC pages to be overcommitted. If the system is out of enclave memory, EPC pages are swapped to normal RAM via a per enclave shared memory area. This shared memory is not charged to the enclave or the task mapping it, making it hard to account for using normal methods.
In order to avoid unlimited usage of normal RAM, enclaves must be charged for each new page used for backing storage, and uncharged when they are no longer using a backing page.
Modify the existing flow for requesting backing pages to reduce the available backing page counter and confirm that the limit has not been exceeded. Backing page usage for loading EPC pages back out of the shared memory do not incur a charge.
When a backing page is released from usage, increment the available backing page counter.
When swapping EPC pages to RAM, in addition to storing the page contents, SGX must store some additional metadata to protect against a malicious kernel when the page is swapped back in. This additional metadata is called Paging Crypto MetaData. PCMD is allocated from the same shared memory area as the backing page contents and consumes RAM the same way.
PCMD is 128 bytes in size, and there is one PCMD structure per page written to shared RAM. The page index for the PCMD page is calculated from the page index of the backing page, so it is possible that the PCMD structures are not packed into the minimum number of pages possible. If 32 PCMDs can fit onto a single page, then PCMD usage is 1/32 of total EPC pages. In the worst case, PCMD can consume the same amount of RAM as EPC backing pages (1:1). For simplicity, this implementation does not account for PCMD page usage.
--- arch/x86/kernel/cpu/sgx/encl.c | 76 ++++++++++++++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/encl.h | 6 ++- arch/x86/kernel/cpu/sgx/main.c | 6 +-- 3 files changed, 80 insertions(+), 8 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +73 -3
@@ -32,7 +32,7 @@ static int __sgx_encl_eldu(struct sgx_encl_page *encl_page,else page_index =PFN_DOWN (encl->size); - ret =sgx_encl_get_backing (encl, page_index, &b); + ret =sgx_encl_lookup_backing (encl, page_index, &b);if (ret)return ret;
@@ -407,6 +407,12 @@ void sgx_encl_release(struct kref *ref)sgx_encl_free_epc_page (entry->epc_page); encl->secs_child_cnt--; entry->epc_page =NULL ; + }else { +/* +* If there is no epc_page, it means it has been +* swapped out. Uncharge the backing storage. +*/ +sgx_uncharge_mem (); }kfree (entry);
@@ -574,8 +580,8 @@ static struct page *sgx_encl_get_backing_page(struct sgx_encl *encl,* 0 on success, * -errno otherwise. */ -int sgx_encl_get_backing (struct sgx_encl *encl,unsigned long page_index, -struct sgx_backing *backing) +static int sgx_encl_get_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing) {pgoff_t pcmd_index =PFN_DOWN (encl->size) +1 + (page_index >>5 );struct page *contents;
@@ -601,6 +607,62 @@ int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index,return 0 ; } +/** +* sgx_encl_alloc_backing() - allocate a new backing storage page +* @encl: an enclave pointer +* @page_index: enclave page index +* @backing: data for accessing backing storage for the page +* +* Confirm that the global overcommit limit has not been reached before +* requesting a new backing storage page for storing the encrypted contents +* and Paging Crypto MetaData (PCMD) of an enclave page. This is called when +* there is no existing backing page, just before writing to the backing +* storage with EWB. +* +* Return: +* 0 on success, +* -errno otherwise. +*/ +int sgx_encl_alloc_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing) +{ +int ret; + +if (sgx_charge_mem ()) +return -ENOMEM ; + + ret =sgx_encl_get_backing (encl, page_index, backing); +if (ret) +sgx_uncharge_mem (); + +return ret; +} + +/** +* sgx_encl_lookup_backing() - retrieve an existing backing storage page +* @encl: an enclave pointer +* @page_index: enclave page index +* @backing: data for accessing backing storage for the page +* +* Retrieve a backing page for loading data back into an EPC page with ELDU. +* This call does not cause a charge to the overcommit limit because a page +* has already been allocated, but has been swapped out or is in RAM +* +* It is the caller's responsibility to ensure that it is appropriate to +* use sgx_encl_lookup_backing() rather than sgx_encl_alloc_backing(). If +* lookup is not used correctly, this will cause an allocation that is +* not accounted for. +* +* Return: +* 0 on success, +* -errno otherwise. +*/ +int sgx_encl_lookup_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing) +{ +return sgx_encl_get_backing (encl, page_index, backing); +} +/** * sgx_encl_put_backing() - Unpin the backing storage * @backing: data for accessing backing storage for the page
@@ -608,9 +670,17 @@ int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index,*/ void sgx_encl_put_backing (struct sgx_backing *backing,bool do_write) { +/* +* If the page is being written to by the reclaimer, it is +* still in use and the backing page usage should not be +* uncharged. However, if the page is not being written to, +* it is no longer in use and may be uncharged. +*/ if (do_write) {set_page_dirty (backing->pcmd);set_page_dirty (backing->contents); + }else { +sgx_uncharge_mem (); }put_page (backing->pcmd);
arch/x86/kernel/cpu/sgx/encl.h +4 -2
@@ -105,8 +105,10 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,void sgx_encl_release (struct kref *ref);int sgx_encl_mm_add (struct sgx_encl *encl,struct mm_struct *mm); -int sgx_encl_get_backing (struct sgx_encl *encl,unsigned long page_index, -struct sgx_backing *backing); +int sgx_encl_alloc_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing); +int sgx_encl_lookup_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing);void sgx_encl_put_backing (struct sgx_backing *backing,bool do_write);int sgx_encl_test_and_clear_young (struct mm_struct *mm,struct sgx_encl_page *page);
arch/x86/kernel/cpu/sgx/main.c +3 -3
@@ -347,8 +347,8 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page, encl->secs_child_cnt--;if (!encl->secs_child_cnt &&test_bit (SGX_ENCL_INITIALIZED , &encl->flags)) { - ret =sgx_encl_get_backing (encl,PFN_DOWN (encl->size), - &secs_backing); + ret =sgx_encl_alloc_backing (encl,PFN_DOWN (encl->size), + &secs_backing);if (ret) goto out;
@@ -418,7 +418,7 @@ static void sgx_reclaim_pages(void) goto skip; page_index =PFN_DOWN (encl_page->desc - encl_page->encl->base); - ret =sgx_encl_get_backing (encl_page->encl, page_index, &backing[i]); + ret =sgx_encl_alloc_backing (encl_page->encl, page_index, &backing[i]);if (ret) goto skip;
Signature · 1 line
2.20.1
Re: [PATCH v2 1/2] x86/sgx: Add accounting for tracking overcommit
In reply to Kristen Carlson AccardiQuoted 3 lines · Kristen Carlson Accardi
The overcommit percentage value is 150, which limits the total number of shared memory pages that may be consumed by all enclaves as backing pages to 1.5X of EPC pages on the system.
Hi Kristen,
Could you give some background on how this value was chosen and how it might impact userspace?
Re: [PATCH v2 1/2] x86/sgx: Add accounting for tracking overcommit
Quoted 7 lines · Dave Hansen
Quoted 5 lines · Kristen Carlson Accardi
The overcommit percentage value is 150, which limits the total number of
shared memory pages that may be consumed by all enclaves as backing pages
to 1.5X of EPC pages on the system.
Quoted 5 lines
Hi Kristen,
Could you give some background on how this value was chosen and how it
might impact userspace?
Yes,
The value of 1.5x the number of EPC pages was chosen because it will handle the most common case of a few enclaves that don't need much overcommit without any impact to user space. In the less commone case where there are many enclaves or a few large enclaves which need a lot of overcommit due to large EPC memory requirements, the reclaimer may fail to allocate a backing page for swapping if the limit has been reached. In this case the page will not be able to be reclaimed and the system will not be able to allocate any new EPC pages. Any ioctl or call to add new EPC pages will get -ENOMEM, so for example, new enclaves will fail to load, and new EPC pages will not be able to be added.
Does that make sense?
Kristen
Jarkko Sakkinen
maintainerRe: [PATCH v2 1/2] x86/sgx: Add accounting for tracking overcommit
In reply to Kristen Carlson AccardiQuoted 88 lines · Kristen Carlson Accardi
When the system runs out of enclave memory, SGX can reclaim EPC pages by swapping to normal RAM. This normal RAM is allocated via a per-enclave shared memory area. The shared memory area is not mapped into the enclave or the task mapping it, which makes its memory use opaque (including to the OOM killer). Having lots of hard to find memory around is problematic, especially when there is no limit.
Introduce a global counter that can be used to limit the number of pages that enclaves are able to consume for backing storage. This parameter is a percentage value that is used in conjunction with the number of EPC pages in the system to set a cap on the amount of backing RAM that can be consumed.
The overcommit percentage value is 150, which limits the total number of shared memory pages that may be consumed by all enclaves as backing pages to 1.5X of EPC pages on the system.
For example, on an SGX system that has 128MB of EPC, this would cap the amount of normal RAM that SGX consumes for its shared memory areas at 192MB.
The SGX overcommit percent works differently than the core VM overcommit limit. Enclaves request backing pages one page at a time, and the number of in use backing pages that are allowed is a global resource that is limited for all enclaves.
Introduce a pair of functions which can be used by callers when requesting backing RAM pages. These functions are responsible for accounting the page charges. A request may return an error if the request will cause the counter to exceed the backing page cap.
Signed-off-by Kristen Carlson Accardi <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 46 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 2 ++ 2 files changed, 48 insertions(+)arch/x86/kernel/cpu/sgx/main.c +39
-0@@ -43,6 +43,45 @@ static struct sgx_numa_node *sgx_numa_nodes;static LIST_HEAD (sgx_dirty_page_list); +/* +* Limits the amount of normal RAM that SGX can consume for EPC +* overcommit to the total EPC pages * sgx_overcommit_percent / 100 +*/ +static int sgx_overcommit_percent =150 ; + +/* The number of pages that can be allocated globally for backing storage. */ +static atomic_long_t sgx_nr_available_backing_pages; + +/** +* sgx_charge_mem() - charge for a page used for backing storage +* +* Backing storage usage is capped by the sgx_nr_available_backing_pages. +* If the backing storage usage is over the overcommit limit, +* return an error. +* +* Return: +* 0: The page requested does not exceed the limit +* -ENOMEM: The page requested exceeds the overcommit limit +*/ +int sgx_charge_mem (void ) +{ +if (!atomic_long_add_unless (&sgx_nr_available_backing_pages,-1 ,0 )) +return -ENOMEM ; + +return 0 ; +} + +/** +* sgx_uncharge_mem() - uncharge a page previously used for backing storage +* +* When backing storage is no longer in use, increment the +* sgx_nr_available_backing_pages counter. +*/ +void sgx_uncharge_mem (void ) +{ +atomic_long_inc (&sgx_nr_available_backing_pages); +} +/* * Reset post-kexec EPC pages to the uninitialized state. The pages are removed * from the input list, and made available for the page allocator. SECS pages
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 829
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0@@ -786,6 +825,8 @@ static bool __init sgx_page_cache_init(void)u64 pa, size;int nid;int i; +u64 total_epc_bytes =0 ; +u64 available_backing_bytes;
Use reverse-christmas-tree declaration order here. This was required for the original patch set, so I expect this to still hold.
Quoted 12 lines
arch/x86/kernel/cpu/sgx/main.c +1
-0sgx_numa_nodes =kmalloc_array (num_possible_nodes (),sizeof (*sgx_numa_nodes),GFP_KERNEL );if (!sgx_numa_nodes)@@ -830,6 +871,7 @@ static bool __init sgx_page_cache_init(void) sgx_epc_sections[i].node = &sgx_numa_nodes[nid]; sgx_numa_nodes[nid].size += size; + total_epc_bytes += size; sgx_nr_epc_sections++; }
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 885
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0@@ -839,6 +881,10 @@ static bool __init sgx_page_cache_init(void)return false; } + available_backing_bytes = total_epc_bytes * (sgx_overcommit_percent /100 ); +
IMHO this empty line could be removed, and group these statements together.
Quoted 21 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0+atomic_long_set (&sgx_nr_available_backing_pages, available_backing_bytes >>PAGE_SHIFT ); +return true; }arch/x86/kernel/cpu/sgx/sgx.h +2
-0@@ -89,6 +89,8 @@ void sgx_free_epc_page(struct sgx_epc_page *page);void sgx_mark_page_reclaimable (struct sgx_epc_page *page);int sgx_unmark_page_reclaimable (struct sgx_epc_page *page);struct sgx_epc_page *sgx_alloc_epc_page (void *owner,bool reclaim); +int sgx_charge_mem (void ); +void sgx_uncharge_mem (void );#ifdef CONFIG_X86_SGX_KVM int __init sgx_vepc_init (void );Signature · 1 line
2.20.1
Other than that, looks good to me.
BR,
Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 2/2] x86/sgx: account backing pages
In reply to Kristen Carlson AccardiQuoted 127 lines · Kristen Carlson Accardi
SGX may allow EPC pages to be overcommitted. If the system is out of enclave memory, EPC pages are swapped to normal RAM via a per enclave shared memory area. This shared memory is not charged to the enclave or the task mapping it, making it hard to account for using normal methods.
In order to avoid unlimited usage of normal RAM, enclaves must be charged for each new page used for backing storage, and uncharged when they are no longer using a backing page.
Modify the existing flow for requesting backing pages to reduce the available backing page counter and confirm that the limit has not been exceeded. Backing page usage for loading EPC pages back out of the shared memory do not incur a charge.
When a backing page is released from usage, increment the available backing page counter.
When swapping EPC pages to RAM, in addition to storing the page contents, SGX must store some additional metadata to protect against a malicious kernel when the page is swapped back in. This additional metadata is called Paging Crypto MetaData. PCMD is allocated from the same shared memory area as the backing page contents and consumes RAM the same way.
PCMD is 128 bytes in size, and there is one PCMD structure per page written to shared RAM. The page index for the PCMD page is calculated from the page index of the backing page, so it is possible that the PCMD structures are not packed into the minimum number of pages possible. If 32 PCMDs can fit onto a single page, then PCMD usage is 1/32 of total EPC pages. In the worst case, PCMD can consume the same amount of RAM as EPC backing pages (1:1). For simplicity, this implementation does not account for PCMD page usage.
Signed-off-by Kristen Carlson Accardi <[email protected]>--- arch/x86/kernel/cpu/sgx/encl.c | 76 ++++++++++++++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/encl.h | 6 ++- arch/x86/kernel/cpu/sgx/main.c | 6 +-- 3 files changed, 80 insertions(+), 8 deletions(-)arch/x86/kernel/cpu/sgx/encl.c +54
-3@@ -32,7 +32,7 @@ static int __sgx_encl_eldu(struct sgx_encl_page *encl_page,else page_index =PFN_DOWN (encl->size); - ret =sgx_encl_get_backing (encl, page_index, &b); + ret =sgx_encl_lookup_backing (encl, page_index, &b);if (ret)return ret;@@ -407,6 +407,12 @@ void sgx_encl_release(struct kref *ref)sgx_encl_free_epc_page (entry->epc_page); encl->secs_child_cnt--; entry->epc_page =NULL ; + }else { +/* +* If there is no epc_page, it means it has been +* swapped out. Uncharge the backing storage. +*/ +sgx_uncharge_mem (); }kfree (entry);@@ -574,8 +580,8 @@ static struct page *sgx_encl_get_backing_page(struct sgx_encl *encl,* 0 on success, * -errno otherwise. */ -int sgx_encl_get_backing (struct sgx_encl *encl,unsigned long page_index, -struct sgx_backing *backing) +static int sgx_encl_get_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing) {pgoff_t pcmd_index =PFN_DOWN (encl->size) +1 + (page_index >>5 );struct page *contents;@@ -601,6 +607,62 @@ int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index,return 0 ; } +/** +* sgx_encl_alloc_backing() - allocate a new backing storage page +* @encl: an enclave pointer +* @page_index: enclave page index +* @backing: data for accessing backing storage for the page +* +* Confirm that the global overcommit limit has not been reached before +* requesting a new backing storage page for storing the encrypted contents +* and Paging Crypto MetaData (PCMD) of an enclave page. This is called when +* there is no existing backing page, just before writing to the backing +* storage with EWB. +* +* Return: +* 0 on success, +* -errno otherwise. +*/ +int sgx_encl_alloc_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing) +{ +int ret; + +if (sgx_charge_mem ()) +return -ENOMEM ; + + ret =sgx_encl_get_backing (encl, page_index, backing); +if (ret) +sgx_uncharge_mem (); + +return ret; +} + +/** +* sgx_encl_lookup_backing() - retrieve an existing backing storage page +* @encl: an enclave pointer +* @page_index: enclave page index +* @backing: data for accessing backing storage for the page +* +* Retrieve a backing page for loading data back into an EPC page with ELDU. +* This call does not cause a charge to the overcommit limit because a page +* has already been allocated, but has been swapped out or is in RAM +* +* It is the caller's responsibility to ensure that it is appropriate to +* use sgx_encl_lookup_backing() rather than sgx_encl_alloc_backing(). If +* lookup is not used correctly, this will cause an allocation that is +* not accounted for.
arch/x86/kernel/cpu/sgx/encl.c int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index, · line 664
Quoted 10 lines
arch/x86/kernel/cpu/sgx/encl.c +10
-0+* +* Return: +* 0 on success, +* -errno otherwise. +*/ +int sgx_encl_lookup_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing) +{ +return sgx_encl_get_backing (encl, page_index, backing); +}
IMHO, sgx_encl_backing() should be open-coded here.
Quoted 27 lines
arch/x86/kernel/cpu/sgx/encl.c +9
-0+/** * sgx_encl_put_backing() - Unpin the backing storage * @backing: data for accessing backing storage for the page @@ -608,9 +670,17 @@ int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index,*/ void sgx_encl_put_backing (struct sgx_backing *backing,bool do_write) { +/* +* If the page is being written to by the reclaimer, it is +* still in use and the backing page usage should not be +* uncharged. However, if the page is not being written to, +* it is no longer in use and may be uncharged. +*/ if (do_write) {set_page_dirty (backing->pcmd);set_page_dirty (backing->contents); + }else { +sgx_uncharge_mem (); }put_page (backing->pcmd);arch/x86/kernel/cpu/sgx/encl.h +0
-0
arch/x86/kernel/cpu/sgx/encl.h int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start, · line 109
Quoted 7 lines
arch/x86/kernel/cpu/sgx/encl.h +2
-2@@ -105,8 +105,10 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,void sgx_encl_release (struct kref *ref);int sgx_encl_mm_add (struct sgx_encl *encl,struct mm_struct *mm); -int sgx_encl_get_backing (struct sgx_encl *encl,unsigned long page_index, -struct sgx_backing *backing); +int sgx_encl_alloc_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing);
This might sound overly long but it really would make this a lot clearer than alloc, which can mean pretty much anything:
sgx_encl_lookup_and_charge_backing()
What do you think? That leaves no guesswork what the function does.
Quoted 32 lines
arch/x86/kernel/cpu/sgx/encl.h +2
-0+int sgx_encl_lookup_backing (struct sgx_encl *encl,unsigned long page_index, +struct sgx_backing *backing);void sgx_encl_put_backing (struct sgx_backing *backing,bool do_write);int sgx_encl_test_and_clear_young (struct mm_struct *mm,struct sgx_encl_page *page);arch/x86/kernel/cpu/sgx/main.c +3
-3@@ -347,8 +347,8 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page, encl->secs_child_cnt--;if (!encl->secs_child_cnt &&test_bit (SGX_ENCL_INITIALIZED , &encl->flags)) { - ret =sgx_encl_get_backing (encl,PFN_DOWN (encl->size), - &secs_backing); + ret =sgx_encl_alloc_backing (encl,PFN_DOWN (encl->size), + &secs_backing);if (ret) goto out;@@ -418,7 +418,7 @@ static void sgx_reclaim_pages(void) goto skip; page_index =PFN_DOWN (encl_page->desc - encl_page->encl->base); - ret =sgx_encl_get_backing (encl_page->encl, page_index, &backing[i]); + ret =sgx_encl_alloc_backing (encl_page->encl, page_index, &backing[i]);if (ret) goto skip;Signature · 1 line
2.20.1
/Jarkko
Re: [PATCH v2 1/2] x86/sgx: Add accounting for tracking overcommit
In reply to Kristen Carlson AccardiQuoted 27 lines · Kristen Carlson Accardi
Quoted 7 lines · Dave Hansen
Quoted 5 lines · Kristen Carlson Accardi
The overcommit percentage value is 150, which limits the total number of
shared memory pages that may be consumed by all enclaves as backing pages
to 1.5X of EPC pages on the system.Quoted 5 lines
Hi Kristen,
Could you give some background on how this value was chosen and how it
might impact userspace?Yes,
The value of 1.5x the number of EPC pages was chosen because it will handle the most common case of a few enclaves that don't need much overcommit without any impact to user space. In the less commone case where there are many enclaves or a few large enclaves which need a lot of overcommit due to large EPC memory requirements, the reclaimer may fail to allocate a backing page for swapping if the limit has been reached. In this case the page will not be able to be reclaimed and the system will not be able to allocate any new EPC pages. Any ioctl or call to add new EPC pages will get-ENOMEM, so for example, new enclaves will fail to load, and new EPC pages will not be able to be added.
Quoted 1 line
Does that make sense?
If the system has a ton of RAM but limited EPC, I think it makes sense to allow more EPC swapping, can we do min(0.5*RAM, 2*EPC)? I suppose if the system is used for heavy enclave load, user would be willing to at least use half of RAM.
Thanks
Haitao
Re: [PATCH v2 1/2] x86/sgx: Add accounting for tracking overcommit
Quoted 2 lines · Haitao Huang
If the system has a ton of RAM but limited EPC, I think it makes sense to allow more EPC swapping, can we do
min(0.5*RAM, 2*EPC)? I suppose if the system is used for heavy enclave load, user would be willing to at least use half of RAM.
If I have 100GB of RAM and 100MB of EPC, can I really *meaningfully* run 50GB of enclaves? In that case, if everything was swapped out evenly, I would only have a 499/500 chance that a given page reference would fault.
This isn't about a "heavy enclave load". If there is *that* much swapped-out enclave memory, will an enclave even make meaningful forward progress?
Re: [PATCH v2 1/2] x86/sgx: Add accounting for tracking overcommit
Quoted 6 lines · Dave Hansen
Quoted 2 lines · Haitao Huang
If the system has a ton of RAM but limited EPC, I think it makes sense to allow more EPC swapping, can we do
min(0.5*RAM, 2*EPC)? I suppose if the system is used for heavy enclave load, user would be willing to at least use half of RAM.If I have 100GB of RAM and 100MB of EPC, can I really *meaningfully* run 50GB of enclaves? In that case, if everything was swapped out evenly, I would only have a 499/500 chance that a given page reference would fault.
The formula will cap swapping at 2*EPC so only 200MB swapped out. So the miss is at most 1/3. The original hard coded cap 1.5*EPC may still consume too much RAM if RAM<1.5*EPC.
Quoted 1 line
This isn't about a "heavy enclave load". If there is *that* much swapped-out enclave memory, will an enclave even make meaningful forward progress?
Re: [PATCH v2 1/2] x86/sgx: Add accounting for tracking overcommit
Quoted 9 lines · Haitao Huang
Quoted 5 lines · Dave Hansen
Quoted 2 lines · Haitao Huang
If the system has a ton of RAM but limited EPC, I think it makes sense to allow more EPC swapping, can we do
min(0.5*RAM, 2*EPC)? I suppose if the system is used for heavy enclave load, user would be willing to at least use half of RAM.If I have 100GB of RAM and 100MB of EPC, can I really *meaningfully* run 50GB of enclaves? In that case, if everything was swapped out evenly, I would only have a 499/500 chance that a given page reference would fault.
The formula will cap swapping at 2*EPC so only 200MB swapped out. So the miss is at most 1/3. The original hard coded cap 1.5*EPC may still consume too much RAM if RAM<1.5*EPC.
Oh, sorry, I read that backwards.
Basing it on the amount of RAM is a bit nasty. You might either really overly restrict the amount of allowed EPC, or you have to handle hotplug.
Re: [PATCH v2 1/2] x86/sgx: Add accounting for tracking overcommit
Quoted 28 lines · Dave Hansen
Quoted 20 lines · Haitao Huang
Quoted 9 lines · Dave Hansen
Quoted 7 lines · Haitao Huang
If the system has a ton of RAM but limited EPC, I think it makes
sense to allow more EPC swapping, can we domin(0.5*RAM, 2*EPC)?
I suppose if the system is used for heavy enclave load, user would be
willing to at least use half of RAM.Quoted 7 lines
If I have 100GB of RAM and 100MB of EPC, can I really *meaningfully*
run 50GB of enclaves? In that case, if everything was swapped out
evenly, I would only have a 499/500 chance that a given page reference
would fault.Quoted 6 lines
The formula will cap swapping at 2*EPC so only 200MB swapped out. So
the miss is at most 1/3.
The original hard coded cap 1.5*EPC may still consume too much RAM if
RAM<1.5*EPC.
Quoted 6 lines
Oh, sorry, I read that backwards.
Basing it on the amount of RAM is a bit nasty. You might either really
overly restrict the amount of allowed EPC, or you have to handle hotplug.
My opinion is that we should keep the current algorithm for now as it is pretty straightforward, and cgroups will eventually allow for more control.
Re: [PATCH v2 1/2] x86/sgx: Add accounting for tracking overcommit
In reply to Jarkko SakkinenQuoted 115 lines · Jarkko Sakkinen
Quoted 113 lines · Kristen Carlson Accardi
When the system runs out of enclave memory, SGX can reclaim EPC pages
by swapping to normal RAM. This normal RAM is allocated via a per-enclave shared memory area. The shared memory area is not mapped
into the enclave or the task mapping it, which makes its memory use opaque (including to the OOM killer). Having lots of hard to find memory around is problematic, especially when there is no limit.Introduce a global counter that can be used to limit the number of pages
that enclaves are able to consume for backing storage. This parameter
is a percentage value that is used in conjunction with the number of
EPC pages in the system to set a cap on the amount of backing RAM that
can be consumed.The overcommit percentage value is 150, which limits the total number of
shared memory pages that may be consumed by all enclaves as backing pages
to 1.5X of EPC pages on the system.For example, on an SGX system that has 128MB of EPC, this would cap the amount of normal RAM that SGX consumes for its shared memory
areas at 192MB.The SGX overcommit percent works differently than the core VM overcommit
limit. Enclaves request backing pages one page at a time, and the number
of in use backing pages that are allowed is a global resource that is
limited for all enclaves.Introduce a pair of functions which can be used by callers when requesting
backing RAM pages. These functions are responsible for accounting the
page charges. A request may return an error if the request will cause the
counter to exceed the backing page cap.Signed-off-by Kristen Carlson Accardi <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 46 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 2 ++ 2 files changed, 48 insertions(+)arch/x86/kernel/cpu/sgx/main.c +3
-0@@ -43,6 +43,45 @@ static struct sgx_numa_node *sgx_numa_nodes;static LIST_HEAD (sgx_dirty_page_list); +/* +* Limits the amount of normal RAM that SGX can consume for EPC +* overcommit to the total EPC pages * sgx_overcommit_percent / 100
+ */arch/x86/kernel/cpu/sgx/main.c +3
-0+static int sgx_overcommit_percent =150 ; + +/* The number of pages that can be allocated globally for backing storage. */
arch/x86/kernel/cpu/sgx/main.c +6
-0+static atomic_long_t sgx_nr_available_backing_pages; + +/** +* sgx_charge_mem() - charge for a page used for backing storage +* +* Backing storage usage is capped by the
sgx_nr_available_backing_pages.
+ * If the backing storage usage is over the overcommit limit,
+ * return an error.
+ *
+ * Return:
+ * 0: The page requested does not exceed the limit
+ *-ENOMEM: The page requested exceeds the overcommit limit
+ */arch/x86/kernel/cpu/sgx/main.c +10
-1+int sgx_charge_mem(void) +{ + if (!atomic_long_add_unless(&sgx_nr_available_backing_pages, -1, 0)) + return -ENOMEM; + + return 0; +} + +/** + * sgx_uncharge_mem() - uncharge a page previously used forbacking storage
+ *
+ * When backing storage is no longer in use, increment the
+ *sgx_nr_available_backing_pagescounter.
+ */arch/x86/kernel/cpu/sgx/main.c +5
-0+void sgx_uncharge_mem (void ) +{ +atomic_long_inc (&sgx_nr_available_backing_pages); +} +/* * Reset post-kexec EPC pages to the uninitialized state. The pages are removed
* from the input list, and made available for the page allocator.
SECS pagesarch/x86/kernel/cpu/sgx/main.c +0
-0@@ -786,6 +825,8 @@ static bool __init sgx_page_cache_init(void)u64 pa, size;
Quoted 8 lines
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 829
Quoted 4 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0int nid;int i; +u64 total_epc_bytes =0 ; +u64 available_backing_bytes;Use reverse-christmas-tree declaration order here. This was required for
the original patch set, so I expect this to still hold.
sure.
Quoted 17 lines
Quoted 17 lines
arch/x86/kernel/cpu/sgx/main.c +1
-0sgx_numa_nodes =kmalloc_array (num_possible_nodes (),sizeof (*sgx_numa_nodes),GFP_KERNEL );if (!sgx_numa_nodes)@@ -830,6 +871,7 @@ static bool __init sgx_page_cache_init(void) sgx_epc_sections[i].node = &sgx_numa_nodes[nid]; sgx_numa_nodes[nid].size += size; + total_epc_bytes += size; sgx_nr_epc_sections++; }@@ -839,6 +881,10 @@ static bool __init sgx_page_cache_init(void)return false; }
Quoted 6 lines
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 886
Quoted 3 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0+ available_backing_bytes = total_epc_bytes * (sgx_overcommit_percent /100 ); +IMHO this empty line could be removed, and group these statements together.
ok.
Quoted 21 lines
Quoted 21 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0+atomic_long_set (&sgx_nr_available_backing_pages, available_backing_bytes >>PAGE_SHIFT ); +return true;}
arch/x86/kernel/cpu/sgx/sgx.h +2
-0@@ -89,6 +89,8 @@ void sgx_free_epc_page(struct sgx_epc_page *page);void sgx_mark_page_reclaimable (struct sgx_epc_page *page);int sgx_unmark_page_reclaimable (struct sgx_epc_page *page);struct sgx_epc_page *sgx_alloc_epc_page (void *owner,bool reclaim); +int sgx_charge_mem (void ); +void sgx_uncharge_mem (void );
Quoted 7 lines
Quoted 5 lines
#ifdef
CONFIG_X86_SGX_KVM
int__initsgx_vepc_init(void);Signature · 1 line
2.20.1Other than that, looks good to me.
thanks for taking another look. I will incorporate your suggestions into the next revision.
Re: [PATCH v2 2/2] x86/sgx: account backing pages
In reply to Jarkko SakkinenQuoted 157 lines · Jarkko Sakkinen
Quoted 155 lines · Kristen Carlson Accardi
SGX may allow EPC pages to be overcommitted. If the system is out of enclave memory, EPC pages are swapped to normal RAM via a per enclave shared memory area. This shared memory is not charged to the enclave or the task mapping it, making it hard to account for using normal methods.
In order to avoid unlimited usage of normal RAM, enclaves must be charged for each new page used for backing storage, and uncharged when they are no longer using a backing page.
Modify the existing flow for requesting backing pages to reduce the available backing page counter and confirm that the limit has not been exceeded. Backing page usage for loading EPC pages back out of the shared memory do not incur a charge.
When a backing page is released from usage, increment the available backing page counter.
When swapping EPC pages to RAM, in addition to storing the page contents, SGX must store some additional metadata to protect against a malicious kernel when the page is swapped back in. This additional metadata is called Paging Crypto MetaData. PCMD is allocated from the same shared memory area as the backing page contents and consumes RAM the same way.
PCMD is 128 bytes in size, and there is one PCMD structure per page written to shared RAM. The page index for the PCMD page is calculated from the page index of the backing page, so it is possible
that the PCMD structures are not packed into the minimum number of pages possible. If 32 PCMDs can fit onto a single page, then PCMD usage is 1/32 of total EPC pages. In the worst case, PCMD can consume the same amount of RAM as EPC backing pages (1:1). For simplicity, this implementation does not account for PCMD page usage.Signed-off-by Kristen Carlson Accardi <[email protected]>--- arch/x86/kernel/cpu/sgx/encl.c | 76 ++++++++++++++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/encl.h | 6 ++- arch/x86/kernel/cpu/sgx/main.c | 6 +-- 3 files changed, 80 insertions(+), 8 deletions(-)arch/x86/kernel/cpu/sgx/encl.c +4
-1@@ -32,7 +32,7 @@ static int __sgx_encl_eldu(struct sgx_encl_page *encl_page, else page_index =PFN_DOWN (encl->size); - ret =sgx_encl_get_backing (encl, page_index, &b); + ret =sgx_encl_lookup_backing (encl, page_index, &b);if (ret)return ret;@@ -407,6 +407,12 @@ void sgx_encl_release(struct kref *ref)sgx_encl_free_epc_page (entry->epc_page); encl->secs_child_cnt--; entry->epc_page =NULL ; + }else { +/* +* If there is no epc_page, it means it has been
arch/x86/kernel/cpu/sgx/encl.c +5
-2+* swapped out. Uncharge the backing storage. +*/ +sgx_uncharge_mem (); }kfree (entry);@@ -574,8 +580,8 @@ static struct page*sgx_encl_get_backing_page(struct sgx_encl *encl, * 0 on success, * -errno otherwise. */ -int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index, - struct sgx_backing *backing) +static int sgx_encl_get_backing(struct sgx_encl *encl, unsignedlong page_index, +struct sgx_backing *backing){
pgoff_tpcmd_index=PFN_DOWN(encl->size)+ 1 + (page_index>>
5);arch/x86/kernel/cpu/sgx/encl.c +7
-0struct page *contents;@@ -601,6 +607,62 @@ int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index, return 0; } +/** +* sgx_encl_alloc_backing() - allocate a new backing storage page +* @encl: an enclave pointer +* @page_index: enclave page index +* @backing: data for accessing backing storage for the page +* +* Confirm that the global overcommit limit has not been reached before
+ * requesting a new backing storage page for storing the encrypted contents
+ * and Paging Crypto MetaData (PCMD) of an enclave page. This is called when
+ * there is no existing backing page, just before writing to the backing
+ * storage with EWB.
+ *
+ * Return:
+ * 0 on success,
+ * -errno otherwise.
+ */
+int sgx_encl_alloc_backing(structsgx_encl*encl, unsigned longpage_index,arch/x86/kernel/cpu/sgx/encl.c +16
-0+ struct sgx_backing *backing) +{ +int ret; + +if (sgx_charge_mem ()) +return -ENOMEM ; + + ret =sgx_encl_get_backing (encl, page_index, backing); +if (ret) +sgx_uncharge_mem (); + +return ret; +} + +/** +* sgx_encl_lookup_backing() - retrieve an existing backing storage page
+ * @encl: an enclave pointer
+ * @page_index: enclave page index
+ * @backing: data for accessing backing storage for the page
+ *
+ * Retrieve a backing page for loading data back into an EPC page with ELDU.
+ * This call does not cause a charge to the overcommit limit because a page
+ * has already been allocated, but has been swapped out or is in RAM
+ *
+ * It is the caller's responsibility to ensure that it is appropriate to
+ * usesgx_encl_lookup_backing()rather thansgx_encl_alloc_backing(). If
+ * lookup is not used correctly, this will cause an allocation that is
+ * not accounted for.
+ *
+ * Return:
+ * 0 on success,
+ * -errno otherwise.
+ */
Quoted 8 lines
arch/x86/kernel/cpu/sgx/encl.c int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index, · line 664
Quoted 6 lines
+int sgx_encl_lookup_backing(struct
sgx_encl*encl, unsigned longpage_index,arch/x86/kernel/cpu/sgx/encl.c +4
-0+ struct sgx_backing *backing) +{ +return sgx_encl_get_backing (encl, page_index, backing); +}IMHO,
sgx_encl_backing()should be open-coded here.
I can understand your hesitation, but I agree with Dave here that wrapping the function makes the code more clear. I would prefer to keep this the way it is.
Re: [PATCH v2 2/2] x86/sgx: account backing pages
Quoted 10 lines · Kristen Carlson Accardi
Quoted 7 lines
arch/x86/kernel/cpu/sgx/encl.c int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index, · line 664
Quoted 6 lines
+int sgx_encl_lookup_backing(struct
sgx_encl*encl, unsigned longpage_index,arch/x86/kernel/cpu/sgx/encl.c +4
-0+ struct sgx_backing *backing) +{ +return sgx_encl_get_backing (encl, page_index, backing); +}IMHO,
sgx_encl_backing()should be open-coded here.I can understand your hesitation, but I agree with Dave here that wrapping the function makes the code more clear. I would prefer to keep this the way it is.
I'd also like to see sgx_encl_lookup_backing() and sgx_encl_alloc_backing() diverge more in the future.
For instance, sgx_encl_alloc_backing() could ensure that the page does not exist in the file before doing the sgx_encl_get_backing() call. This would ensure that it truly *does* allocate a page and does not just return a previously-allocated page.
sgx_encl_lookup_backing() could ensure the opposite: that the page *DOES* exist in the file before doing the sgx_encl_get_backing() call. This would ensure that it does not allocate a page in a case where we expected an old, existing page to be present.
Jarkko Sakkinen
maintainerRe: [PATCH v2 2/2] x86/sgx: account backing pages
In reply to Kristen Carlson AccardiQuoted 167 lines · Kristen Carlson Accardi
Quoted 157 lines · Jarkko Sakkinen
Quoted 155 lines · Kristen Carlson Accardi
SGX may allow EPC pages to be overcommitted. If the system is out of enclave memory, EPC pages are swapped to normal RAM via a per enclave shared memory area. This shared memory is not charged to the enclave or the task mapping it, making it hard to account for using normal methods.
In order to avoid unlimited usage of normal RAM, enclaves must be charged for each new page used for backing storage, and uncharged when they are no longer using a backing page.
Modify the existing flow for requesting backing pages to reduce the available backing page counter and confirm that the limit has not been exceeded. Backing page usage for loading EPC pages back out of the shared memory do not incur a charge.
When a backing page is released from usage, increment the available backing page counter.
When swapping EPC pages to RAM, in addition to storing the page contents, SGX must store some additional metadata to protect against a malicious kernel when the page is swapped back in. This additional metadata is called Paging Crypto MetaData. PCMD is allocated from the same shared memory area as the backing page contents and consumes RAM the same way.
PCMD is 128 bytes in size, and there is one PCMD structure per page written to shared RAM. The page index for the PCMD page is calculated from the page index of the backing page, so it is possible
that the PCMD structures are not packed into the minimum number of pages possible. If 32 PCMDs can fit onto a single page, then PCMD usage is 1/32 of total EPC pages. In the worst case, PCMD can consume the same amount of RAM as EPC backing pages (1:1). For simplicity, this implementation does not account for PCMD page usage.Signed-off-by Kristen Carlson Accardi <[email protected]>--- arch/x86/kernel/cpu/sgx/encl.c | 76 ++++++++++++++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/encl.h | 6 ++- arch/x86/kernel/cpu/sgx/main.c | 6 +-- 3 files changed, 80 insertions(+), 8 deletions(-)arch/x86/kernel/cpu/sgx/encl.c +4
-1@@ -32,7 +32,7 @@ static int __sgx_encl_eldu(struct sgx_encl_page *encl_page, else page_index =PFN_DOWN (encl->size); - ret =sgx_encl_get_backing (encl, page_index, &b); + ret =sgx_encl_lookup_backing (encl, page_index, &b);if (ret)return ret;@@ -407,6 +407,12 @@ void sgx_encl_release(struct kref *ref)sgx_encl_free_epc_page (entry->epc_page); encl->secs_child_cnt--; entry->epc_page =NULL ; + }else { +/* +* If there is no epc_page, it means it has been
arch/x86/kernel/cpu/sgx/encl.c +5
-2+* swapped out. Uncharge the backing storage. +*/ +sgx_uncharge_mem (); }kfree (entry);@@ -574,8 +580,8 @@ static struct page*sgx_encl_get_backing_page(struct sgx_encl *encl, * 0 on success, * -errno otherwise. */ -int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index, - struct sgx_backing *backing) +static int sgx_encl_get_backing(struct sgx_encl *encl, unsignedlong page_index, +struct sgx_backing *backing){
pgoff_tpcmd_index=PFN_DOWN(encl->size)+ 1 + (page_index>>
5);arch/x86/kernel/cpu/sgx/encl.c +7
-0struct page *contents;@@ -601,6 +607,62 @@ int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index, return 0; } +/** +* sgx_encl_alloc_backing() - allocate a new backing storage page +* @encl: an enclave pointer +* @page_index: enclave page index +* @backing: data for accessing backing storage for the page +* +* Confirm that the global overcommit limit has not been reached before
+ * requesting a new backing storage page for storing the encrypted contents
+ * and Paging Crypto MetaData (PCMD) of an enclave page. This is called when
+ * there is no existing backing page, just before writing to the backing
+ * storage with EWB.
+ *
+ * Return:
+ * 0 on success,
+ * -errno otherwise.
+ */
+int sgx_encl_alloc_backing(structsgx_encl*encl, unsigned longpage_index,arch/x86/kernel/cpu/sgx/encl.c +16
-0+ struct sgx_backing *backing) +{ +int ret; + +if (sgx_charge_mem ()) +return -ENOMEM ; + + ret =sgx_encl_get_backing (encl, page_index, backing); +if (ret) +sgx_uncharge_mem (); + +return ret; +} + +/** +* sgx_encl_lookup_backing() - retrieve an existing backing storage page
+ * @encl: an enclave pointer
+ * @page_index: enclave page index
+ * @backing: data for accessing backing storage for the page
+ *
+ * Retrieve a backing page for loading data back into an EPC page with ELDU.
+ * This call does not cause a charge to the overcommit limit because a page
+ * has already been allocated, but has been swapped out or is in RAM
+ *
+ * It is the caller's responsibility to ensure that it is appropriate to
+ * usesgx_encl_lookup_backing()rather thansgx_encl_alloc_backing(). If
+ * lookup is not used correctly, this will cause an allocation that is
+ * not accounted for.
+ *
+ * Return:
+ * 0 on success,
+ * -errno otherwise.
+ */Quoted 8 lines
arch/x86/kernel/cpu/sgx/encl.c int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index, · line 664
Quoted 6 lines
+int sgx_encl_lookup_backing(struct
sgx_encl*encl, unsigned longpage_index,arch/x86/kernel/cpu/sgx/encl.c +4
-0+ struct sgx_backing *backing) +{ +return sgx_encl_get_backing (encl, page_index, backing); +}IMHO,
sgx_encl_backing()should be open-coded here.
Quoted 3 lines
I can understand your hesitation, but I agree with Dave here that wrapping the function makes the code more clear. I would prefer to keep this the way it is.
What if sgx_encl_get_backing() was changed as "static inline", if the only motivation is encapsulation?
/Jarkko
Re: [PATCH v2 2/2] x86/sgx: account backing pages
Quoted 5 lines · Jarkko Sakkinen
Quoted 3 lines
I can understand your hesitation, but I agree with Dave here that wrapping the function makes the code more clear. I would prefer to keep this the way it is.
What if
sgx_encl_get_backing()was changed as "static inline", if the only motivation is encapsulation?
What would the purpose be of adding an 'inline' to the function definition?
Jarkko Sakkinen
maintainerRe: [PATCH v2 2/2] x86/sgx: account backing pages
In reply to Dave HansenQuoted 20 lines · Dave Hansen
Quoted 10 lines · Kristen Carlson Accardi
Quoted 7 lines
arch/x86/kernel/cpu/sgx/encl.c int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index, · line 664
Quoted 6 lines
+int sgx_encl_lookup_backing(struct
sgx_encl*encl, unsigned longpage_index,arch/x86/kernel/cpu/sgx/encl.c +4
-0+ struct sgx_backing *backing) +{ +return sgx_encl_get_backing (encl, page_index, backing); +}IMHO,
sgx_encl_backing()should be open-coded here.I can understand your hesitation, but I agree with Dave here that wrapping the function makes the code more clear. I would prefer to keep this the way it is.
I'd also like to see
sgx_encl_lookup_backing()andsgx_encl_alloc_backing()diverge more in the future.For instance,
sgx_encl_alloc_backing()could ensure that the page does not exist in the file before doing thesgx_encl_get_backing()call. This would ensure that it truly *does* allocate a page and does not just return a previously-allocated page.
Quoted 4 lines
sgx_encl_lookup_backing()could ensure the opposite: that the page *DOES* exist in the file before doing thesgx_encl_get_backing()call. This would ensure that it does not allocate a page in a case where we expected an old, existing page to be present.
Would it be a too big tretch to add these and make the whole scheme fully legit? Does not sound like an extremely huge stretch and there is now a full cycle amount of time make it happen before 5.18 merge window.
/Jarkko
Re: [PATCH v2 2/2] x86/sgx: account backing pages
Quoted 3 lines · Jarkko Sakkinen
Would it be a too big tretch to add these and make the whole scheme fully legit? Does not sound like an extremely huge stretch and there is now a full cycle amount of time make it happen before 5.18 merge window.
No, it's not a big stretch. If anyone wants it for 5.18 (or whatever) just send the patch.
Jarkko Sakkinen
maintainerRe: [PATCH v2 2/2] x86/sgx: account backing pages
Quoted 7 lines · Dave Hansen
Quoted 3 lines · Jarkko Sakkinen
Would it be a too big tretch to add these and make the whole scheme fully legit? Does not sound like an extremely huge stretch and there is now a full cycle amount of time make it happen before 5.18 merge window.
No, it's not a big stretch. If anyone wants it for 5.18 (or whatever) just send the patch.
Fixing this to work with the current mainline is first and foremost needed:
https://lore.kernel.org/linux-sgx/[email protected]/T/#t
With a fixeed version of that, wouldn't it be then just matter of calling shmem_getpage() with SGX_NOALLOC to check the state of the file in both calls?
/Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 2/2] x86/sgx: account backing pages
In reply to Dave HansenQuoted 8 lines · Dave Hansen
Quoted 5 lines · Jarkko Sakkinen
Quoted 3 lines
I can understand your hesitation, but I agree with Dave here that wrapping the function makes the code more clear. I would prefer to keep this the way it is.
What if
sgx_encl_get_backing()was changed as "static inline", if the only motivation is encapsulation?What would the purpose be of adding an 'inline' to the function definition?
Agreed, not much sense to do this. I just had troubles to get the way things were encapsulated before your response.
/Jarkko
Quoted 28 lines · Kristen Carlson Accardi
SGX currently allows EPC pages to be overcommitted. If the system is out of enclave memory, EPC pages are swapped to normal RAM via a per enclave shared memory area. This shared memory is not charged to the enclave or the task mapping it, making it hard to account for using normal methods. Since SGX will allow EPC pages to be overcommitted without limits, enclaves can consume system memory for these backing pages without limits.
In order to prevent this, set a cap on the amount of overcommit SGX allows. Whenever a backing page is requested by an enclave, track the total amount of shared memory pages used across all enclaves and return an error if the overcommit limit has been reached. This will restrict the total amount of backing pages that all enclaves can consume to a maximum amount, and prevent enclaves from consuming all the system RAM for backing pages.
The overcommit percentage has a value of 150, which limits shared memory page consumption to 1.5x the number of EPC pages in the system.
Changes from v1
----------------
* removed module parameter and disable boolean
* increased over commit percentage to 150% from 100%Kristen Carlson Accardi (2):
x86/sgx: Add accounting for tracking overcommit
x86/sgx: account backing pages
Quoted 9 lines
arch/x86/kernel/cpu/sgx/encl.c | 76 ++++++++++++++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/encl.h | 6 ++- arch/x86/kernel/cpu/sgx/main.c | 52 +++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/sgx.h | 2 + 4 files changed, 128 insertions(+), 8 deletions(-)Signature · 1 line
2.20.1
I've tested also these. Looking at the feedback, there's nothing game changing, so you could add for the next version:
/Jarkko
Quoted 39 lines · Jarkko Sakkinen
Quoted 28 lines · Kristen Carlson Accardi
SGX currently allows EPC pages to be overcommitted. If the system is out of enclave memory, EPC pages are swapped to normal RAM via a per enclave shared memory area. This shared memory is not charged to the enclave or the task mapping it, making it hard to account for using normal methods. Since SGX will allow EPC pages to be overcommitted without limits, enclaves can consume system memory for these backing pages without limits.
In order to prevent this, set a cap on the amount of overcommit SGX allows. Whenever a backing page is requested by an enclave, track the total amount of shared memory pages used across all enclaves and return an error if the overcommit limit has been reached. This will restrict the total amount of backing pages that all enclaves can consume to a maximum amount, and prevent enclaves from consuming all the system RAM for backing pages.
The overcommit percentage has a value of 150, which limits shared memory page consumption to 1.5x the number of EPC pages in the system.
Changes from v1
----------------
* removed module parameter and disable boolean
* increased over commit percentage to 150% from 100%Kristen Carlson Accardi (2):
x86/sgx: Add accounting for tracking overcommit
x86/sgx: account backing pagesQuoted 9 lines
arch/x86/kernel/cpu/sgx/encl.c | 76 ++++++++++++++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/encl.h | 6 ++- arch/x86/kernel/cpu/sgx/main.c | 52 +++++++++++++++++++++-- arch/x86/kernel/cpu/sgx/sgx.h | 2 + 4 files changed, 128 insertions(+), 8 deletions(-)Signature · 1 line
2.20.1
Quoted 5 lines
I've tested also these. Looking at the feedback, there's nothing game changing, so you could add for the next version:
Tested-by Jarkko Sakkinen <[email protected]>
The test environment was a VM running my desktop [*] and I just run many instances of kselftest as a test case.
[*] i5-9600KF CPU
/Jarkko