Skip to contentSkip to conversation

x86/sgx: Misc fixes for v31

PATCH Applied Sean Christopherson 2 participants 14 messages May 29, 2020 – Jun 1, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86/sgx: Misc fixes for v31

One bug fix and a variety of cleanups that apply on top of your current master, commit b625c3d1370ca ("x86/sgx: Update MAINTAINERS").

Sean Christopherson (6):
x86/sgx: Fix inadvertant early return from sgx_ioctl() x86/sgx: Remove unnecessary globals after merging reclaim.c into
main.c
x86/sgx: Rename sgx_free_page() to sgx_free_epc_page() x86/sgx: Replace "grab" with "alloc" in VA page helper x86/sgx: Update stale comment in EPC page allocators x86/sgx: Drop the message that fires when there are too many EPC
sections

 arch/x86/kernel/cpu/sgx/encl.c  | 16 +++++++-------
 arch/x86/kernel/cpu/sgx/encl.h  |  2 +-
 arch/x86/kernel/cpu/sgx/ioctl.c | 10 ++++-----
 arch/x86/kernel/cpu/sgx/main.c  | 39 +++++++++++++--------------------
 arch/x86/kernel/cpu/sgx/sgx.h   |  5 +----
 5 files changed, 30 insertions(+), 42 deletions(-)
Signature · 1 line
2.26.0
#1 lore raw

PATCH 1/6 x86/sgx: Fix inadvertant early return from sgx_ioctl()

Set 'ret' instead of returning if the enclave is dead in sgx_ioctl(), otherwise the goto that was added doesn't take effect.

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/ioctl.c +1 -1
index ec5a0f017014b..2019ee1dc0b3e 100644
@@ -790,7 +790,7 @@ long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 		return -EBUSY;
 
 	if (encl_flags & SGX_ENCL_DEAD) {
-		return -EFAULT;
+		ret = -EFAULT;
 		goto out;
 	}
 
Signature · 1 line
2.26.0
#2 lore raw

PATCH 2/6 x86/sgx: Remove unnecessary globals after merging reclaim.c into main.c

In reply to Sean Christopherson

Make all variables that are only used in main.c static, rearranging code as needed.

---
 arch/x86/kernel/cpu/sgx/main.c | 16 ++++++----------
 arch/x86/kernel/cpu/sgx/sgx.h  |  3 ---
 2 files changed, 6 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +6 -10
index 5043d3700960b..ef7061bfa2a07 100644
@@ -14,9 +14,12 @@
 #include "encls.h"
 
 static struct task_struct *ksgxswapd_tsk;
-DECLARE_WAIT_QUEUE_HEAD(ksgxswapd_waitq);
-LIST_HEAD(sgx_active_page_list);
-DEFINE_SPINLOCK(sgx_active_page_list_lock);
+static DECLARE_WAIT_QUEUE_HEAD(ksgxswapd_waitq);
+static LIST_HEAD(sgx_active_page_list);
+static DEFINE_SPINLOCK(sgx_active_page_list_lock);
+
+struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
+static int sgx_nr_epc_sections;
 
 static void sgx_sanitize_section(struct sgx_epc_section *section)
 {
@@ -45,10 +48,6 @@ static void sgx_sanitize_section(struct sgx_epc_section *section)
 	}
 }
 
-extern struct task_struct *ksgxswapd_tsk;
-extern struct wait_queue_head(ksgxswapd_waitq);
-extern spinlock_t sgx_active_page_list_lock;
-
 static unsigned long sgx_nr_free_pages(void)
 {
 	unsigned long cnt = 0;
@@ -491,9 +490,6 @@ void sgx_reclaim_pages(void)
 	}
 }
 
-struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
-int sgx_nr_epc_sections;
-
 static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_section *section)
 {
 	struct sgx_epc_page *page;
arch/x86/kernel/cpu/sgx/sgx.h +0 -3
index 923028d568540..5b9dbcef981b0 100644
@@ -41,14 +41,11 @@ struct sgx_epc_section {
 #define SGX_NR_LOW_PAGES		32
 #define SGX_NR_HIGH_PAGES		64
 
-extern struct list_head sgx_active_page_list;
-
 void sgx_mark_page_reclaimable(struct sgx_epc_page *page);
 int sgx_unmark_page_reclaimable(struct sgx_epc_page *page);
 void sgx_reclaim_pages(void);
 
 extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
-extern int sgx_nr_epc_sections;
 
 static inline struct sgx_epc_section *sgx_get_epc_section(
 		struct sgx_epc_page *page)
Signature · 1 line
2.26.0
#6 lore raw

PATCH 3/6 x86/sgx: Rename sgx_free_page() to sgx_free_epc_page()

In reply to Sean Christopherson

Add "epc" into the free page helper to match sgx_alloc_epc_page().

---
 arch/x86/kernel/cpu/sgx/encl.c  | 10 +++++-----
 arch/x86/kernel/cpu/sgx/ioctl.c |  6 +++---
 arch/x86/kernel/cpu/sgx/main.c  | 12 ++++++------
 arch/x86/kernel/cpu/sgx/sgx.h   |  2 +-
 4 files changed, 15 insertions(+), 15 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +5 -5
index c52c937b8d67b..e9d72f05a1f12 100644
@@ -73,7 +73,7 @@ static struct sgx_epc_page *sgx_encl_eldu(struct sgx_encl_page *encl_page,
 
 	ret = __sgx_encl_eldu(encl_page, epc_page, secs_page);
 	if (ret) {
-		sgx_free_page(epc_page);
+		sgx_free_epc_page(epc_page);
 		return ERR_PTR(ret);
 	}
 
@@ -482,7 +482,7 @@ void sgx_encl_destroy(struct sgx_encl *encl)
 			if (sgx_unmark_page_reclaimable(entry->epc_page))
 				continue;
 
-			sgx_free_page(entry->epc_page);
+			sgx_free_epc_page(entry->epc_page);
 			encl->secs_child_cnt--;
 			entry->epc_page = NULL;
 		}
@@ -493,7 +493,7 @@ void sgx_encl_destroy(struct sgx_encl *encl)
 	}
 
 	if (!encl->secs_child_cnt && encl->secs.epc_page) {
-		sgx_free_page(encl->secs.epc_page);
+		sgx_free_epc_page(encl->secs.epc_page);
 		encl->secs.epc_page = NULL;
 	}
 
@@ -506,7 +506,7 @@ void sgx_encl_destroy(struct sgx_encl *encl)
 		va_page = list_first_entry(&encl->va_pages, struct sgx_va_page,
 					   list);
 		list_del(&va_page->list);
-		sgx_free_page(va_page->epc_page);
+		sgx_free_epc_page(va_page->epc_page);
 		kfree(va_page);
 	}
 }
@@ -706,7 +706,7 @@ struct sgx_epc_page *sgx_grab_va_page(void)
 	ret = __epa(sgx_get_epc_addr(epc_page));
 	if (ret) {
 		WARN_ONCE(1, "EPA returned %d (0x%x)", ret, ret);
-		sgx_free_page(epc_page);
+		sgx_free_epc_page(epc_page);
 		return ERR_PTR(-EFAULT);
 	}
 
arch/x86/kernel/cpu/sgx/ioctl.c +3 -3
index 2019ee1dc0b3e..f7232a5343262 100644
@@ -50,7 +50,7 @@ static void sgx_encl_shrink(struct sgx_encl *encl, struct sgx_va_page *va_page)
 	encl->page_cnt--;
 
 	if (va_page) {
-		sgx_free_page(va_page->epc_page);
+		sgx_free_epc_page(va_page->epc_page);
 		list_del(&va_page->list);
 		kfree(va_page);
 	}
@@ -223,7 +223,7 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
 	return 0;
 
 err_out:
-	sgx_free_page(encl->secs.epc_page);
+	sgx_free_epc_page(encl->secs.epc_page);
 	encl->secs.epc_page = NULL;
 
 err_out_backing:
@@ -449,7 +449,7 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 	up_read(&current->mm->mmap_sem);
 
 err_out_free:
-	sgx_free_page(epc_page);
+	sgx_free_epc_page(epc_page);
 	kfree(encl_page);
 
 	/*
arch/x86/kernel/cpu/sgx/main.c +6 -6
index ef7061bfa2a07..855b14237e7c4 100644
@@ -376,7 +376,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page,
 
 	if (!encl->secs_child_cnt) {
 		if (atomic_read(&encl->flags) & SGX_ENCL_DEAD) {
-			sgx_free_page(encl->secs.epc_page);
+			sgx_free_epc_page(encl->secs.epc_page);
 			encl->secs.epc_page = NULL;
 		} else if (atomic_read(&encl->flags) & SGX_ENCL_INITIALIZED) {
 			ret = sgx_encl_get_backing(encl, PFN_DOWN(encl->size),
@@ -386,7 +386,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page,
 
 			sgx_encl_ewb(encl->secs.epc_page, &secs_backing);
 
-			sgx_free_page(encl->secs.epc_page);
+			sgx_free_epc_page(encl->secs.epc_page);
 			encl->secs.epc_page = NULL;
 
 			sgx_encl_put_backing(&secs_backing, true);
@@ -508,7 +508,7 @@ static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_sec
  * __sgx_alloc_epc_page() - Grab a free EPC page
  *
  * Iterate through EPC sections and borrow a free EPC page to the caller. When a
- * page is no longer needed it must be released with sgx_free_page().
+ * page is no longer needed it must be released with sgx_free_epc_page().
  *
  * Return:
  *   an EPC page,
@@ -539,7 +539,7 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void)
  * @reclaim:	reclaim pages if necessary
  *
  * Iterate through EPC sections and borrow a free EPC page to the caller. When a
- * page is no longer needed it must be released with sgx_free_page(). If
+ * page is no longer needed it must be released with sgx_free_epc_page(). If
  * @reclaim is set to true, directly reclaim pages when we are out of pages. No
  * mm's can be locked when @reclaim is set to true.
  *
@@ -585,12 +585,12 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim)
 }
 
 /**
- * sgx_free_page() - Free an EPC page
+ * sgx_free_epc_page() - Free an EPC page
  * @page:	an EPC page
  *
  * Call EREMOVE for an EPC page and insert it back to the list of free pages.
  */
-void sgx_free_page(struct sgx_epc_page *page)
+void sgx_free_epc_page(struct sgx_epc_page *page)
 {
 	struct sgx_epc_section *section = sgx_get_epc_section(page);
 	int ret;
arch/x86/kernel/cpu/sgx/sgx.h +1 -1
index 5b9dbcef981b0..2983c1a3d725d 100644
@@ -62,6 +62,6 @@ static inline void *sgx_get_epc_addr(struct sgx_epc_page *page)
 
 struct sgx_epc_page *__sgx_alloc_epc_page(void);
 struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim);
-void sgx_free_page(struct sgx_epc_page *page);
+void sgx_free_epc_page(struct sgx_epc_page *page);
 
 #endif /* _X86_SGX_H */
Signature · 1 line
2.26.0
#8 lore raw

PATCH 4/6 x86/sgx: Replace "grab" with "alloc" in VA page helper

In reply to Sean Christopherson

Rename sgx_grab_va_page() to sgx_alloc_va_page() to align with sgx_alloc_epc_page() as well as sgx_free_epc_page(); the latter is used directly when freeing the VA page, i.e. the names should be consistent.

---
 arch/x86/kernel/cpu/sgx/encl.c  | 6 +++---
 arch/x86/kernel/cpu/sgx/encl.h  | 2 +-
 arch/x86/kernel/cpu/sgx/ioctl.c | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +3 -3
index e9d72f05a1f12..df4ae76dd83f6 100644
@@ -686,15 +686,15 @@ struct sgx_encl_page *sgx_encl_reserve_page(struct sgx_encl *encl,
 }
 
 /**
- * sgx_grab_va_page() - Grab a Version Array (VA) page
+ * sgx_alloc_va_page() - Allocate a Version Array (VA) page
  *
- * Grab a free EPC page instance and convert it to a Version Array (VA) page.
+ * Allocate a free EPC page and convert it to a Version Array (VA) page.
  *
  * Return:
  *   a VA page,
  *   -errno otherwise
  */
-struct sgx_epc_page *sgx_grab_va_page(void)
+struct sgx_epc_page *sgx_alloc_va_page(void)
 {
 	struct sgx_epc_page *epc_page;
 	int ret;
arch/x86/kernel/cpu/sgx/encl.h +1 -1
index 625ad44a83215..f0f72e5912445 100644
@@ -120,7 +120,7 @@ int sgx_encl_test_and_clear_young(struct mm_struct *mm,
 struct sgx_encl_page *sgx_encl_reserve_page(struct sgx_encl *encl,
 					    unsigned long addr);
 
-struct sgx_epc_page *sgx_grab_va_page(void);
+struct sgx_epc_page *sgx_alloc_va_page(void);
 unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page);
 void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset);
 bool sgx_va_page_full(struct sgx_va_page *va_page);
arch/x86/kernel/cpu/sgx/ioctl.c +1 -1
index f7232a5343262..4f70cb8144ffd 100644
@@ -32,7 +32,7 @@ static struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl)
 		if (!va_page)
 			return ERR_PTR(-ENOMEM);
 
-		va_page->epc_page = sgx_grab_va_page();
+		va_page->epc_page = sgx_alloc_va_page();
 		if (IS_ERR(va_page->epc_page)) {
 			err = ERR_CAST(va_page->epc_page);
 			kfree(va_page);
Signature · 1 line
2.26.0
#10 lore raw

PATCH 5/6 x86/sgx: Update stale comment in EPC page allocators

In reply to Sean Christopherson

Replace "Grab" with "Allocate" in the function comments for the EPC page allocators.

---
 arch/x86/kernel/cpu/sgx/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +2 -2
index 855b14237e7c4..ba5486a1ef6f4 100644
@@ -505,7 +505,7 @@ static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_sec
 }
 
 /**
- * __sgx_alloc_epc_page() - Grab a free EPC page
+ * __sgx_alloc_epc_page() - Allocate a free EPC page
  *
  * Iterate through EPC sections and borrow a free EPC page to the caller. When a
  * page is no longer needed it must be released with sgx_free_epc_page().
@@ -534,7 +534,7 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void)
 }
 
 /**
- * sgx_alloc_epc_page() - Grab a free EPC page
+ * sgx_alloc_epc_page() - Allocate a free EPC page
  * @owner:	the owner of the EPC page
  * @reclaim:	reclaim pages if necessary
  *
Signature · 1 line
2.26.0
#12 lore raw

PATCH 6/6 x86/sgx: Drop the message that fires when there are too many EPC sections

In reply to Sean Christopherson

Terminate the section loop with '<' instead of '<=' and drop the associated pr_warn in sgx_page_cache_init(). Per Boris, there's no point in warning the user if they can't do anything about it.

---
 arch/x86/kernel/cpu/sgx/main.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +1 -6
index ba5486a1ef6f4..c51a3ad7fcce0 100644
@@ -692,7 +692,7 @@ static bool __init sgx_page_cache_init(void)
 	u64 pa, size;
 	int i;
 
-	for (i = 0; i <= ARRAY_SIZE(sgx_epc_sections); i++) {
+	for (i = 0; i < ARRAY_SIZE(sgx_epc_sections); i++) {
 		cpuid_count(SGX_CPUID, i + SGX_CPUID_FIRST_VARIABLE_SUB_LEAF,
 			    &eax, &ebx, &ecx, &edx);
 
@@ -705,11 +705,6 @@ static bool __init sgx_page_cache_init(void)
 			break;
 		}
 
-		if (i == ARRAY_SIZE(sgx_epc_sections)) {
-			pr_warn("No free slot for an EPC section\n");
-			break;
-		}
-
 		pa = sgx_calc_section_metric(eax, ebx);
 		size = sgx_calc_section_metric(ecx, edx);
 
Signature · 1 line
2.26.0
#3 lore raw

Re: [PATCH for_v31 2/6] x86/sgx: Remove unnecessary globals after merging reclaim.c into main.c

In reply to Sean Christopherson
Quoted 4 lines · Sean Christopherson

Make all variables that are only used in main.c static, rearranging code as needed.

As with merging, it is a multiphase process to make these changes.

I.e. I'll do manually similar changes but cannot use this patch for anything.

/Jarkko

Quoted 68 lines
---
 arch/x86/kernel/cpu/sgx/main.c | 16 ++++++----------
 arch/x86/kernel/cpu/sgx/sgx.h  |  3 ---
 2 files changed, 6 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +6 -10
index 5043d3700960b..ef7061bfa2a07 100644
@@ -14,9 +14,12 @@
 #include "encls.h"
 
 static struct task_struct *ksgxswapd_tsk;
-DECLARE_WAIT_QUEUE_HEAD(ksgxswapd_waitq);
-LIST_HEAD(sgx_active_page_list);
-DEFINE_SPINLOCK(sgx_active_page_list_lock);
+static DECLARE_WAIT_QUEUE_HEAD(ksgxswapd_waitq);
+static LIST_HEAD(sgx_active_page_list);
+static DEFINE_SPINLOCK(sgx_active_page_list_lock);
+
+struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
+static int sgx_nr_epc_sections;
 
 static void sgx_sanitize_section(struct sgx_epc_section *section)
 {
@@ -45,10 +48,6 @@ static void sgx_sanitize_section(struct sgx_epc_section *section)
 	}
 }
 
-extern struct task_struct *ksgxswapd_tsk;
-extern struct wait_queue_head(ksgxswapd_waitq);
-extern spinlock_t sgx_active_page_list_lock;
-
 static unsigned long sgx_nr_free_pages(void)
 {
 	unsigned long cnt = 0;
@@ -491,9 +490,6 @@ void sgx_reclaim_pages(void)
 	}
 }
 
-struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
-int sgx_nr_epc_sections;
-
 static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_section *section)
 {
 	struct sgx_epc_page *page;
arch/x86/kernel/cpu/sgx/sgx.h +0 -3
index 923028d568540..5b9dbcef981b0 100644
@@ -41,14 +41,11 @@ struct sgx_epc_section {
 #define SGX_NR_LOW_PAGES		32
 #define SGX_NR_HIGH_PAGES		64
 
-extern struct list_head sgx_active_page_list;
-
 void sgx_mark_page_reclaimable(struct sgx_epc_page *page);
 int sgx_unmark_page_reclaimable(struct sgx_epc_page *page);
 void sgx_reclaim_pages(void);
 
 extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
-extern int sgx_nr_epc_sections;
 
 static inline struct sgx_epc_section *sgx_get_epc_section(
 		struct sgx_epc_page *page)
Signature · 1 line
2.26.0
#4 lore raw

Re: [PATCH for_v31 2/6] x86/sgx: Remove unnecessary globals after merging reclaim.c into main.c

Quoted 10 lines · Jarkko Sakkinen
Quoted 4 lines · Sean Christopherson

Make all variables that are only used in main.c static, rearranging code as needed.

As with merging, it is a multiphase process to make these changes.

I.e. I'll do manually similar changes but cannot use this patch for anything.

Also this moves sgx_nr_epc_sections and sgx_epc_sections to a wrong location. Reclaimer code is before other code. I don't understand why sgx_epc_sections had to be moved as part of changing variables to static.

/Jarkko

#5 lore raw

Re: [PATCH for_v31 2/6] x86/sgx: Remove unnecessary globals after merging reclaim.c into main.c

Quoted 9 lines · Jarkko Sakkinen
Quoted 8 lines · Jarkko Sakkinen
Quoted 4 lines · Sean Christopherson

Make all variables that are only used in main.c static, rearranging code as needed.

As with merging, it is a multiphase process to make these changes.

Quoted 7 lines
Quoted 2 lines

I.e. I'll do manually similar changes but cannot use this patch for anything.

Also this moves sgx_nr_epc_sections and sgx_epc_sections to a wrong location. Reclaimer code is before other code. I don't understand why sgx_epc_sections had to be moved as part of changing variables to static.

I think should be right now :-)

/Jarkko

#7 lore raw

Re: [PATCH for_v31 3/6] x86/sgx: Rename sgx_free_page() to sgx_free_epc_page()

In reply to Sean Christopherson
Quoted 3 lines · Sean Christopherson

Add "epc" into the free page helper to match sgx_alloc_epc_page().

Would be better if you find something like this you just send patch against the commit that introduces the symbol. That way the patch would have at least some utilitary use.

/Jarkko

#9 lore raw

Re: [PATCH for_v31 4/6] x86/sgx: Replace "grab" with "alloc" in VA page helper

In reply to Sean Christopherson
Quoted 5 lines · Sean Christopherson

Rename sgx_grab_va_page() to sgx_alloc_va_page() to align with sgx_alloc_epc_page() as well as sgx_free_epc_page(); the latter is used directly when freeing the VA page, i.e. the names should be consistent.

Thanks I applied this directly to the page reclaimer patch.

/Jarkko

#13 lore raw

Re: [PATCH for_v31 6/6] x86/sgx: Drop the message that fires when there are too many EPC sections

In reply to Sean Christopherson
Quoted 5 lines · Sean Christopherson

Terminate the section loop with '<' instead of '<=' and drop the associated pr_warn in sgx_page_cache_init(). Per Boris, there's no point in warning the user if they can't do anything about it.

Applied this directly to the correct patch, thanks.

/Jarkko