[intel-sgx-kernel-dev,v2,1/2] intel_sgx: pin the backing page inside sgx_eldu
diff mbox

Message ID 20170116233656.6225-2-jarkko.sakkinen@linux.intel.com
State New
Headers show

Commit Message

Jarkko Sakkinen Jan. 16, 2017, 11:36 p.m. UTC
Encapsulted pinning of the backing page into sgx_eldu in order to
drop duplicate code.

Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
---
 drivers/platform/x86/intel_sgx_vma.c | 54 ++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 30 deletions(-)

Comments

Sean Christopherson Jan. 20, 2017, 9:46 p.m. UTC | #1
Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> Encapsulted pinning of the backing page into sgx_eldu in order to
> drop duplicate code.
> 
> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> ---
>  drivers/platform/x86/intel_sgx_vma.c | 54
> ++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 30
> deletions(-)
> 
> diff --git a/drivers/platform/x86/intel_sgx_vma.c
> b/drivers/platform/x86/intel_sgx_vma.c index 11d1c3b..73ec032 100644
> --- a/drivers/platform/x86/intel_sgx_vma.c
> +++ b/drivers/platform/x86/intel_sgx_vma.c
> @@ -93,25 +93,33 @@ static void sgx_vma_close(struct vm_area_struct *vma)
>  	kref_put(&encl->refcount, sgx_encl_release);
>  }
>  
> -static int do_eldu(struct sgx_encl *encl,
> -		   struct sgx_encl_page *encl_page,
> -		   struct sgx_epc_page *epc_page,
> -		   struct page *backing,
> -		   bool is_secs)
> +static int sgx_eldu(struct sgx_encl *encl,
> +		    struct sgx_encl_page *encl_page,
> +		    struct sgx_epc_page *epc_page,
> +		    bool is_secs)
>  {
> +	struct page *backing;
>  	struct sgx_page_info pginfo;
>  	void *secs_ptr = NULL;
>  	void *epc_ptr;
>  	void *va_ptr;
>  	int ret;
>  
> -	pginfo.srcpge = (unsigned long)kmap_atomic(backing);
> +	backing = sgx_get_backing(encl, encl_page);
> +	if (IS_ERR(backing)) {
> +		ret = PTR_ERR(backing);
> +		sgx_warn(encl, "pinning the backing page for ELDU failed
> with %d\n",
> +			 ret);
> +		return ret;
> +	}
> +
>  	if (!is_secs)
>  		secs_ptr = sgx_get_epc_page(encl->secs_page.epc_page);
>  	pginfo.secs = (unsigned long)secs_ptr;
>  
>  	epc_ptr = sgx_get_epc_page(epc_page);
>  	va_ptr = sgx_get_epc_page(encl_page->va_page->epc_page);
> +	pginfo.srcpge = (unsigned long)kmap_atomic(backing);
>  
>  	pginfo.linaddr = is_secs ? 0 : encl_page->addr;
>  	pginfo.pcmd = (unsigned long)&encl_page->pcmd;
> @@ -121,16 +129,19 @@ static int do_eldu(struct sgx_encl *encl,
>  		     (unsigned long)va_ptr +
>  		     encl_page->va_offset);
>  
> +	kunmap_atomic((void *)(unsigned long)pginfo.srcpge);
>  	sgx_put_epc_page(va_ptr);
>  	sgx_put_epc_page(epc_ptr);
>  
>  	if (!is_secs)
>  		sgx_put_epc_page(secs_ptr);
>  
> -	kunmap_atomic((void *)(unsigned long)pginfo.srcpge);
> -	WARN_ON(ret);
> -	if (ret)
> +	sgx_put_backing(backing, false);
> +
> +	if (ret) {
> +		sgx_err(encl, "ELDU returned %d\n", ret);
>  		return -EFAULT;
> +	}
>  
>  	return 0;
>  }
> @@ -142,7 +153,6 @@ static struct sgx_encl_page *sgx_vma_do_fault(struct
> vm_area_struct *vma, struct sgx_encl_page *entry;
>  	struct sgx_epc_page *epc_page = NULL;
>  	struct sgx_epc_page *secs_epc_page = NULL;
> -	struct page *backing;
>  	unsigned int free_flags = SGX_FREE_SKIP_EREMOVE;
>  	int rc;
>  
> @@ -201,15 +211,7 @@ static struct sgx_encl_page *sgx_vma_do_fault(struct
> vm_area_struct *vma, goto out;
>  		}
>  
> -		backing = sgx_get_backing(encl, &encl->secs_page);
> -		if (IS_ERR(backing)) {
> -			entry = (void *)backing;
> -			goto out;
> -		}
> -
> -		rc = do_eldu(encl, &encl->secs_page, secs_epc_page,
> -			     backing, true /* is_secs */);
> -		sgx_put_backing(backing, 0);
> +		rc = sgx_eldu(encl, &encl->secs_page, secs_epc_page, true);
>  		if (rc)
>  			goto out;
>  
> @@ -220,28 +222,20 @@ static struct sgx_encl_page *sgx_vma_do_fault(struct
> vm_area_struct *vma, secs_epc_page = NULL;
>  	}
>  
> -	backing = sgx_get_backing(encl, entry);
> -	if (IS_ERR(backing)) {
> -		entry = (void *)backing;
> -		goto out;
> -	}
> -
> -	rc = do_eldu(encl, entry, epc_page, backing, false /* is_secs */);
>  	if (rc) {
> -		sgx_put_backing(backing, 0);
>  		entry = ERR_PTR(rc);
>  		goto out;
>  	}

This "if (rc)" check needs to be moved below vm_insert_pfn, as is rc is being
checked before it's initialized and the result of vm_insert_pfn is not being
checked.


>  
> -	rc = vm_insert_pfn(vma, entry->addr, PFN_DOWN(epc_page->pa));
> -	sgx_put_backing(backing, 0);
> -
> +	rc = sgx_eldu(encl, entry, epc_page, false /* is_secs */);
>  	if (rc) {
>  		free_flags = 0;
>  		entry = ERR_PTR(rc);
>  		goto out;
>  	}

free_flags needs to be moved outside of and below "if (rc)"


>  
> +	rc = vm_insert_pfn(vma, entry->addr, PFN_DOWN(epc_page->pa));
> +
>  	encl->secs_child_cnt++;
>  
>  	entry->epc_page = epc_page;
> -- 
> 2.9.3
> 
> _______________________________________________
> intel-sgx-kernel-dev mailing list
> intel-sgx-kernel-dev@lists.01.org
> https://lists.01.org/mailman/listinfo/intel-sgx-kernel-dev

Patch
diff mbox

diff --git a/drivers/platform/x86/intel_sgx_vma.c b/drivers/platform/x86/intel_sgx_vma.c
index 11d1c3b..73ec032 100644
--- a/drivers/platform/x86/intel_sgx_vma.c
+++ b/drivers/platform/x86/intel_sgx_vma.c
@@ -93,25 +93,33 @@  static void sgx_vma_close(struct vm_area_struct *vma)
 	kref_put(&encl->refcount, sgx_encl_release);
 }
 
-static int do_eldu(struct sgx_encl *encl,
-		   struct sgx_encl_page *encl_page,
-		   struct sgx_epc_page *epc_page,
-		   struct page *backing,
-		   bool is_secs)
+static int sgx_eldu(struct sgx_encl *encl,
+		    struct sgx_encl_page *encl_page,
+		    struct sgx_epc_page *epc_page,
+		    bool is_secs)
 {
+	struct page *backing;
 	struct sgx_page_info pginfo;
 	void *secs_ptr = NULL;
 	void *epc_ptr;
 	void *va_ptr;
 	int ret;
 
-	pginfo.srcpge = (unsigned long)kmap_atomic(backing);
+	backing = sgx_get_backing(encl, encl_page);
+	if (IS_ERR(backing)) {
+		ret = PTR_ERR(backing);
+		sgx_warn(encl, "pinning the backing page for ELDU failed with %d\n",
+			 ret);
+		return ret;
+	}
+
 	if (!is_secs)
 		secs_ptr = sgx_get_epc_page(encl->secs_page.epc_page);
 	pginfo.secs = (unsigned long)secs_ptr;
 
 	epc_ptr = sgx_get_epc_page(epc_page);
 	va_ptr = sgx_get_epc_page(encl_page->va_page->epc_page);
+	pginfo.srcpge = (unsigned long)kmap_atomic(backing);
 
 	pginfo.linaddr = is_secs ? 0 : encl_page->addr;
 	pginfo.pcmd = (unsigned long)&encl_page->pcmd;
@@ -121,16 +129,19 @@  static int do_eldu(struct sgx_encl *encl,
 		     (unsigned long)va_ptr +
 		     encl_page->va_offset);
 
+	kunmap_atomic((void *)(unsigned long)pginfo.srcpge);
 	sgx_put_epc_page(va_ptr);
 	sgx_put_epc_page(epc_ptr);
 
 	if (!is_secs)
 		sgx_put_epc_page(secs_ptr);
 
-	kunmap_atomic((void *)(unsigned long)pginfo.srcpge);
-	WARN_ON(ret);
-	if (ret)
+	sgx_put_backing(backing, false);
+
+	if (ret) {
+		sgx_err(encl, "ELDU returned %d\n", ret);
 		return -EFAULT;
+	}
 
 	return 0;
 }
@@ -142,7 +153,6 @@  static struct sgx_encl_page *sgx_vma_do_fault(struct vm_area_struct *vma,
 	struct sgx_encl_page *entry;
 	struct sgx_epc_page *epc_page = NULL;
 	struct sgx_epc_page *secs_epc_page = NULL;
-	struct page *backing;
 	unsigned int free_flags = SGX_FREE_SKIP_EREMOVE;
 	int rc;
 
@@ -201,15 +211,7 @@  static struct sgx_encl_page *sgx_vma_do_fault(struct vm_area_struct *vma,
 			goto out;
 		}
 
-		backing = sgx_get_backing(encl, &encl->secs_page);
-		if (IS_ERR(backing)) {
-			entry = (void *)backing;
-			goto out;
-		}
-
-		rc = do_eldu(encl, &encl->secs_page, secs_epc_page,
-			     backing, true /* is_secs */);
-		sgx_put_backing(backing, 0);
+		rc = sgx_eldu(encl, &encl->secs_page, secs_epc_page, true);
 		if (rc)
 			goto out;
 
@@ -220,28 +222,20 @@  static struct sgx_encl_page *sgx_vma_do_fault(struct vm_area_struct *vma,
 		secs_epc_page = NULL;
 	}
 
-	backing = sgx_get_backing(encl, entry);
-	if (IS_ERR(backing)) {
-		entry = (void *)backing;
-		goto out;
-	}
-
-	rc = do_eldu(encl, entry, epc_page, backing, false /* is_secs */);
 	if (rc) {
-		sgx_put_backing(backing, 0);
 		entry = ERR_PTR(rc);
 		goto out;
 	}
 
-	rc = vm_insert_pfn(vma, entry->addr, PFN_DOWN(epc_page->pa));
-	sgx_put_backing(backing, 0);
-
+	rc = sgx_eldu(encl, entry, epc_page, false /* is_secs */);
 	if (rc) {
 		free_flags = 0;
 		entry = ERR_PTR(rc);
 		goto out;
 	}
 
+	rc = vm_insert_pfn(vma, entry->addr, PFN_DOWN(epc_page->pa));
+
 	encl->secs_child_cnt++;
 
 	entry->epc_page = epc_page;