x86/sgx: Convert sgx_reclaim_pages() to sgx_reclaim_page()
RFC PATCH 1/1 x86/sgx: Convert sgx_reclaim_pages() to sgx_reclaim_page()
Convert sgx_reclaim_pages() to sgx_reclaim_page(), which reclaims only a signle page and returns it back to the caller instead of freeing it.
A simpler flow will allow also simplify sgx_alloc_page() flow and make it constant time. With this change it is easer to pin backing storage before the pages are blocked, which stabilizes the EBLOCK/EWB/ETRACK flow: only an EPCM conflict could cause a EWB to fail.
Because sgx_alloc_page() operations run in parallel on different threads ETRACK's will still group multiple EBLOCK operations. ksgxswapd can be improved in future with multi-threading (e.g.a work queue).
---
Untested because I do not want to spend much time on this unless the high level idea is welcomed.
arch/x86/kernel/cpu/sgx/main.c | 14 ++--- arch/x86/kernel/cpu/sgx/reclaim.c | 87 ++++++++++++++----------------- arch/x86/kernel/cpu/sgx/sgx.h | 2 +- 3 files changed, 46 insertions(+), 57 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +7 -7
@@ -71,8 +71,9 @@ static struct sgx_epc_page *sgx_try_alloc_page(void *owner)struct sgx_epc_page *sgx_alloc_page (void *owner,bool reclaim) {struct sgx_epc_page *entry; +int i; -for ( ; ; ) { +for (i =0 ; i <SGX_NR_TO_SCAN ; i++) { entry =sgx_try_alloc_page (owner);if (entry)break ;
@@ -85,13 +86,12 @@ struct sgx_epc_page *sgx_alloc_page(void *owner, bool reclaim)break ; } -if (signal_pending (current)) { - entry =ERR_PTR (-ERESTARTSYS ); -break ; - } + entry =sgx_reclaim_page (); -sgx_reclaim_pages (); -schedule (); +if (PTR_ERR (entry) == -EBUSY ) +continue ; +else +return entry; }if (sgx_calc_free_cnt () <SGX_NR_LOW_PAGES )
arch/x86/kernel/cpu/sgx/reclaim.c +38 -49
125 lines not on this page · raw message
arch/x86/kernel/cpu/sgx/sgx.h +1 -1
@@ -79,7 +79,7 @@ extern spinlock_t sgx_active_page_list_lock;int sgx_page_reclaimer_init (void );void sgx_mark_page_reclaimable (struct sgx_epc_page *page);unsigned long sgx_calc_free_cnt (void ); -void sgx_reclaim_pages (void ); +struct sgx_epc_page *sgx_reclaim_page (void );struct sgx_epc_page *sgx_alloc_page (void *owner,bool reclaim);int __sgx_free_page (struct sgx_epc_page *page);
Signature · 1 line
2.20.1
Quoted 5 lines · Jarkko Sakkinen
Convert
sgx_reclaim_pages()tosgx_reclaim_page(), which reclaims only a signle page and returns it back to the caller instead of freeing it.A simpler flow will allow also simplify
sgx_alloc_page()flow and make it constant time. With this change it is easer to pin backing storage before
Nit: it doesn't run in constant time, its upper bound is simply lower.
Quoted 6 lines
the pages are blocked, which stabilizes the EBLOCK/EWB/ETRACK flow: only an EPCM conflict could cause a EWB to fail.
Because
sgx_alloc_page()operations run in parallel on different threads ETRACK's will still group multiple EBLOCK operations. ksgxswapd can be improved in future with multi-threading (e.g.a work queue).
I won't be able to give this a thorough review for a few days due to other tasks. We should definitely do some rudimentary performance testing given the potential impact.
Quoted 8 lines · Sean Christopherson
Quoted 5 lines · Jarkko Sakkinen
Convert
sgx_reclaim_pages()tosgx_reclaim_page(), which reclaims only a signle page and returns it back to the caller instead of freeing it.A simpler flow will allow also simplify
sgx_alloc_page()flow and make it constant time. With this change it is easer to pin backing storage beforeNit: it doesn't run in constant time, its upper bound is simply lower.
Not diving into this discussion but point taken :-)
Quoted 10 lines
Quoted 6 lines
the pages are blocked, which stabilizes the EBLOCK/EWB/ETRACK flow: only an EPCM conflict could cause a EWB to fail.
Because
sgx_alloc_page()operations run in parallel on different threads ETRACK's will still group multiple EBLOCK operations. ksgxswapd can be improved in future with multi-threading (e.g.a work queue).I won't be able to give this a thorough review for a few days due to other tasks. We should definitely do some rudimentary performance testing given the potential impact.
This is enough of review for me at this point. Hold on for the 2nd version. I'll rework ksgxswapd for that and generally take this small step further. Even if this would not turn out to be a success story I think it is definitely worth of investigating so that we *know* that we are doing right things right.
/Jarkko