linux-sgx.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86/sgx: Replace @is_secs_child with @pt in sgx_encl_ewb()
@ 2019-08-23  2:30 Jarkko Sakkinen
  2019-08-23 13:45 ` Jarkko Sakkinen
  0 siblings, 1 reply; 2+ messages in thread
From: Jarkko Sakkinen @ 2019-08-23  2:30 UTC (permalink / raw)
  To: linux-sgx; +Cc: Jarkko Sakkinen, Sean Christopherson

Sean complained about boolean parameters so I though that maybe it would
be good to rework the EWB flow in a way that it operates by a page type and
here is the result. The contract is and will be that as long as you give
the correct page type, you will get what you want. That is why I set
REG and TCS appropriately even though they are treated the same way
at this point of time.

Cc: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
---
 arch/x86/kernel/cpu/sgx/reclaim.c | 43 +++++++++++++++++++------------
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c
index 00f596c64a2d..f91e12c21344 100644
--- a/arch/x86/kernel/cpu/sgx/reclaim.c
+++ b/arch/x86/kernel/cpu/sgx/reclaim.c
@@ -221,7 +221,7 @@ static void sgx_reclaimer_block(struct sgx_epc_page *epc_page)
 
 static int __sgx_encl_ewb(struct sgx_encl *encl, struct sgx_epc_page *epc_page,
 			  struct sgx_va_page *va_page, unsigned int va_offset,
-			  bool is_secs_child)
+			  unsigned int pt)
 {
 	struct sgx_encl_page *encl_page = epc_page->owner;
 	struct sgx_pageinfo pginfo;
@@ -232,10 +232,14 @@ static int __sgx_encl_ewb(struct sgx_encl *encl, struct sgx_epc_page *epc_page,
 	struct page *pcmd;
 	int ret;
 
-	if (is_secs_child)
-		page_index = SGX_ENCL_PAGE_INDEX(encl_page);
-	else
+	if (pt != SGX_PAGE_TYPE_SECS && pt != SGX_PAGE_TYPE_TCS &&
+	    pt != SGX_PAGE_TYPE_REG)
+		return -EINVAL;
+
+	if (pt == SGX_PAGE_TYPE_SECS)
 		page_index = PFN_DOWN(encl->size);
+	else
+		page_index = SGX_ENCL_PAGE_INDEX(encl_page);
 
 	pcmd_index = sgx_pcmd_index(encl, page_index);
 	pcmd_offset = sgx_pcmd_offset(page_index);
@@ -300,7 +304,7 @@ static const cpumask_t *sgx_encl_ewb_cpumask(struct sgx_encl *encl)
 	return cpumask;
 }
 
-static void sgx_encl_ewb(struct sgx_epc_page *epc_page, bool is_secs_child)
+static void sgx_encl_ewb(struct sgx_epc_page *epc_page, unsigned int pt)
 {
 	struct sgx_encl_page *encl_page = epc_page->owner;
 	struct sgx_encl *encl = encl_page->encl;
@@ -317,8 +321,7 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, bool is_secs_child)
 		if (sgx_va_page_full(va_page))
 			list_move_tail(&va_page->list, &encl->va_pages);
 
-		ret = __sgx_encl_ewb(encl, epc_page, va_page, va_offset,
-				     is_secs_child);
+		ret = __sgx_encl_ewb(encl, epc_page, va_page, va_offset, pt);
 		if (ret == SGX_NOT_TRACKED) {
 			ret = __etrack(sgx_epc_addr(encl->secs.epc_page));
 			if (ret) {
@@ -327,8 +330,8 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, bool is_secs_child)
 					ENCLS_WARN(ret, "ETRACK");
 			}
 
-			ret = __sgx_encl_ewb(encl, epc_page, va_page,
-					     va_offset, is_secs_child);
+			ret = __sgx_encl_ewb(encl, epc_page, va_page, va_offset,
+					     pt);
 			if (ret == SGX_NOT_TRACKED) {
 				/*
 				 * Slow path, send IPIs to kick cpus out of the
@@ -340,7 +343,7 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, bool is_secs_child)
 				on_each_cpu_mask(sgx_encl_ewb_cpumask(encl),
 						 sgx_ipi_cb, NULL, 1);
 				ret = __sgx_encl_ewb(encl, epc_page, va_page,
-						     va_offset, is_secs_child);
+						     va_offset, pt);
 			}
 		}
 
@@ -350,12 +353,13 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, bool is_secs_child)
 
 		encl_page->desc |= va_offset;
 		encl_page->va_page = va_page;
-	} else if (is_secs_child) {
+	} else if (pt != SGX_PAGE_TYPE_SECS) {
 		ret = __eremove(sgx_epc_addr(epc_page));
 		WARN(ret, "EREMOVE returned %d\n", ret);
 	}
 
-	if (!is_secs_child)
+	/* The reclaimer is not aware of SECS pages. */
+	if (pt == SGX_PAGE_TYPE_SECS)
 		sgx_free_page(epc_page);
 
 	encl_page->epc_page = NULL;
@@ -365,15 +369,22 @@ 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;
+
+	if (encl_page->desc & SGX_ENCL_PAGE_TCS)
+		pt = SGX_PAGE_TYPE_TCS;
+	else
+		pt = SGX_PAGE_TYPE_REG;
 
 	mutex_lock(&encl->lock);
 
-	sgx_encl_ewb(epc_page, true);
+	sgx_encl_ewb(epc_page, pt);
+
 	encl->secs_child_cnt--;
+
 	if (!encl->secs_child_cnt &&
-	    (encl->flags & (SGX_ENCL_DEAD | SGX_ENCL_INITIALIZED))) {
-		sgx_encl_ewb(encl->secs.epc_page, false);
-	}
+	    (encl->flags & (SGX_ENCL_DEAD | SGX_ENCL_INITIALIZED)))
+		sgx_encl_ewb(encl->secs.epc_page, SGX_PAGE_TYPE_SECS);
 
 	mutex_unlock(&encl->lock);
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] x86/sgx: Replace @is_secs_child with @pt in sgx_encl_ewb()
  2019-08-23  2:30 [PATCH] x86/sgx: Replace @is_secs_child with @pt in sgx_encl_ewb() Jarkko Sakkinen
@ 2019-08-23 13:45 ` Jarkko Sakkinen
  0 siblings, 0 replies; 2+ messages in thread
From: Jarkko Sakkinen @ 2019-08-23 13:45 UTC (permalink / raw)
  To: linux-sgx; +Cc: Sean Christopherson

On Fri, 2019-08-23 at 05:30 +0300, Jarkko Sakkinen wrote:
> Sean complained about boolean parameters so I though that maybe it would
> be good to rework the EWB flow in a way that it operates by a page type and
> here is the result. The contract is and will be that as long as you give
> the correct page type, you will get what you want. That is why I set
> REG and TCS appropriately even though they are treated the same way
> at this point of time.
> 
> Cc: Sean Christopherson <sean.j.christopherson@intel.com>
> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>

One remark: I convert this to use SGX_SECINFO_TCS etc. Better to prefer
those. We use SGX_PAGE_TYPE only in EPA wrapper. Could even consider
removing those.

/Jarkko


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-08-23 13:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-23  2:30 [PATCH] x86/sgx: Replace @is_secs_child with @pt in sgx_encl_ewb() Jarkko Sakkinen
2019-08-23 13:45 ` Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).