[RFC PATCH 12/20] x86/sgx: Add helper to retrieve SGX EPC LRU given an EPC page

Kristen Carlson Accardi
2022-09-22T17:13:11Z

From: Sean Christopherson <sean.j.christopherson@intel.com>

Introduce a function that will be used to retrieve an LRU
from an EPC page.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Kristen Carlson Accardi <kristen@linux.intel.com>
Cc: Sean Christopherson <seanjc@google.com>
---
arch/x86/kernel/cpu/sgx/main.c | 30 ++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 98531f6fb448..9f2cb264a347 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -31,6 +31,10 @@ static DEFINE_XARRAY(sgx_epc_address_space);
* with sgx_global_lru.lock acquired.
*/
static struct sgx_epc_lru sgx_global_lru;
+static inline struct sgx_epc_lru *sgx_lru(struct sgx_epc_page *epc_page)
+{
+ return &sgx_global_lru;
+}

static atomic_long_t sgx_nr_free_pages = ATOMIC_LONG_INIT(0);

@@ -299,6 +303,7 @@ static int sgx_reclaim_pages(int nr_to_scan, bool ignore_age)
struct sgx_backing backing[SGX_MAX_NR_TO_RECLAIM];
struct sgx_encl_page *encl_page;
struct sgx_epc_page *epc_page, *tmp;
+ struct sgx_epc_lru *lru;
pgoff_t page_index;
LIST_HEAD(iso);
int ret;
@@ -354,10 +359,11 @@ static int sgx_reclaim_pages(int nr_to_scan, bool ignore_age)
continue;

skip:
- spin_lock(&sgx_global_lru.lock);
+ lru = sgx_lru(epc_page);
+ spin_lock(&lru->lock);
epc_page->flags &= ~SGX_EPC_PAGE_RECLAIM_IN_PROGRESS;
- list_move_tail(&epc_page->list, &sgx_global_lru.reclaimable);
- spin_unlock(&sgx_global_lru.lock);
+ list_move_tail(&epc_page->list, &lru->reclaimable);
+ spin_unlock(&lru->lock);

kref_put(&encl_page->encl->refcount, sgx_encl_release);
}
@@ -514,14 +520,16 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void)
*/
void sgx_record_epc_page(struct sgx_epc_page *page, unsigned long flags)
{
- spin_lock(&sgx_global_lru.lock);
+ struct sgx_epc_lru *lru = sgx_lru(page);
+
+ spin_lock(&lru->lock);
WARN_ON(page->flags & SGX_EPC_PAGE_RECLAIM_FLAGS);
page->flags |= flags;
if (flags & SGX_EPC_PAGE_RECLAIMER_TRACKED)
- list_add_tail(&page->list, &sgx_global_lru.reclaimable);
+ list_add_tail(&page->list, &lru->reclaimable);
else
- list_add_tail(&page->list, &sgx_global_lru.unreclaimable);
- spin_unlock(&sgx_global_lru.lock);
+ list_add_tail(&page->list, &lru->unreclaimable);
+ spin_unlock(&lru->lock);
}

/**
@@ -536,14 +544,16 @@ void sgx_record_epc_page(struct sgx_epc_page *page, unsigned long flags)
*/
int sgx_drop_epc_page(struct sgx_epc_page *page)
{
- spin_lock(&sgx_global_lru.lock);
+ struct sgx_epc_lru *lru = sgx_lru(page);
+
+ spin_lock(&lru->lock);
if ((page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) &&
(page->flags & SGX_EPC_PAGE_RECLAIM_IN_PROGRESS)) {
- spin_unlock(&sgx_global_lru.lock);
+ spin_unlock(&lru->lock);
return -EBUSY;
}
list_del(&page->list);
- spin_unlock(&sgx_global_lru.lock);
+ spin_unlock(&lru->lock);

page->flags &= ~SGX_EPC_PAGE_RECLAIM_FLAGS;

--
2.37.3