The overall flow is somewhat simpler if sgx_reclaimer_write() takes care of freeing and removing pages and sgx_encl_ewb() focuses only on doing ENCLS[EWB]. Move sgx_free_page() and __eremove() from sgx_encl_ewb() to sgx_reclaimer_write(). Cc: Sean Christopherson <sean.j.christopherson@xxxxxxxxx> Cc: Shay Katz-zamir <shay.katz-zamir@xxxxxxxxx> Cc: Serge Ayoun <serge.ayoun@xxxxxxxxx> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxxxxxxxx> --- arch/x86/kernel/cpu/sgx/reclaim.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c index cfda38a9b05c..353888256b2b 100644 --- a/arch/x86/kernel/cpu/sgx/reclaim.c +++ b/arch/x86/kernel/cpu/sgx/reclaim.c @@ -357,16 +357,7 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, unsigned int pt) encl_page->desc |= va_offset; encl_page->va_page = va_page; - } else if (pt != SGX_SECINFO_SECS) { - ret = __eremove(sgx_epc_addr(epc_page)); - WARN(ret, "EREMOVE returned %d\n", ret); } - - /* The reclaimer is not aware of SECS pages. */ - if (pt == SGX_SECINFO_SECS) - sgx_free_page(epc_page); - - encl_page->epc_page = NULL; } static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) @@ -374,6 +365,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) struct sgx_encl_page *encl_page = epc_page->owner; struct sgx_encl *encl = encl_page->encl; unsigned int pt; + int ret; if (encl_page->desc & SGX_ENCL_PAGE_TCS) pt = SGX_SECINFO_TCS; @@ -383,13 +375,22 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) mutex_lock(&encl->lock); sgx_encl_ewb(epc_page, pt); + if (atomic_read(&encl->flags) & SGX_ENCL_DEAD) { + ret = __eremove(sgx_epc_addr(epc_page)); + WARN(ret, "EREMOVE returned %d\n", ret); + } + encl_page->epc_page = NULL; encl->secs_child_cnt--; if (!encl->secs_child_cnt && (atomic_read(&encl->flags) & - (SGX_ENCL_DEAD | SGX_ENCL_INITIALIZED))) + (SGX_ENCL_DEAD | SGX_ENCL_INITIALIZED))) { sgx_encl_ewb(encl->secs.epc_page, SGX_SECINFO_SECS); + sgx_free_page(encl->secs.epc_page); + + encl->secs.epc_page = NULL; + } mutex_unlock(&encl->lock); } -- 2.20.1