diff mbox

[intel-sgx-kernel-dev,1/2] intel_sgx: separate EPC bank addition from page cache init

Message ID 1493660629-16888-2-git-send-email-sean.j.christopherson@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Sean Christopherson May 1, 2017, 5:43 p.m. UTC
Move the init code to process an EPC bank into a separate function,
sgx_add_epc_bank, which can be safely called multiple times.  Call
sgx_page_cache_init only once after all EPC banks have been added.
This fixes a bug where ksgxswapd_tsk is overwritten and lost when
there is more than one EPC bank.

Reported-by: Kai Huang <kai.huang@linux.intel.com>
Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 drivers/platform/x86/intel_sgx/sgx.h            |  3 ++-
 drivers/platform/x86/intel_sgx/sgx_main.c       |  6 +++++-
 drivers/platform/x86/intel_sgx/sgx_page_cache.c | 12 ++++++++----
 3 files changed, 15 insertions(+), 6 deletions(-)
diff mbox

Patch

diff --git a/drivers/platform/x86/intel_sgx/sgx.h b/drivers/platform/x86/intel_sgx/sgx.h
index e59ff01..f169af3 100644
--- a/drivers/platform/x86/intel_sgx/sgx.h
+++ b/drivers/platform/x86/intel_sgx/sgx.h
@@ -223,7 +223,8 @@  enum sgx_alloc_flags {
 };
 
 int ksgxswapd(void *p);
-int sgx_page_cache_init(resource_size_t start, unsigned long size);
+int sgx_add_epc_bank(resource_size_t start, unsigned long size);
+int sgx_page_cache_init(void);
 void sgx_page_cache_teardown(void);
 struct sgx_epc_page *sgx_alloc_page(unsigned int flags);
 int sgx_free_page(struct sgx_epc_page *entry, struct sgx_encl *encl);
diff --git a/drivers/platform/x86/intel_sgx/sgx_main.c b/drivers/platform/x86/intel_sgx/sgx_main.c
index 5890643..98ee05d 100644
--- a/drivers/platform/x86/intel_sgx/sgx_main.c
+++ b/drivers/platform/x86/intel_sgx/sgx_main.c
@@ -289,7 +289,7 @@  static int sgx_dev_init(struct device *dev)
 			goto out_iounmap;
 		}
 #endif
-		ret = sgx_page_cache_init(sgx_epc_banks[i].start,
+		ret = sgx_add_epc_bank(sgx_epc_banks[i].start,
 			sgx_epc_banks[i].end - sgx_epc_banks[i].start);
 		if (ret) {
 			sgx_nr_epc_banks = i+1;
@@ -297,6 +297,10 @@  static int sgx_dev_init(struct device *dev)
 		}
 	}
 
+	ret = sgx_page_cache_init();
+	if (ret)
+		goto out_iounmap;
+
 	wq_flags = WQ_UNBOUND | WQ_FREEZABLE;
 #ifdef WQ_NON_REENETRANT
 	wq_flags |= WQ_NON_REENTRANT;
diff --git a/drivers/platform/x86/intel_sgx/sgx_page_cache.c b/drivers/platform/x86/intel_sgx/sgx_page_cache.c
index 59a67cb..d4cf0cc 100644
--- a/drivers/platform/x86/intel_sgx/sgx_page_cache.c
+++ b/drivers/platform/x86/intel_sgx/sgx_page_cache.c
@@ -416,7 +416,7 @@  int ksgxswapd(void *p)
 	return 0;
 }
 
-int sgx_page_cache_init(resource_size_t start, unsigned long size)
+int sgx_add_epc_bank(resource_size_t start, unsigned long size)
 {
 	unsigned long i;
 	struct sgx_epc_page *new_epc_page, *entry;
@@ -435,9 +435,6 @@  int sgx_page_cache_init(resource_size_t start, unsigned long size)
 		spin_unlock(&sgx_free_list_lock);
 	}
 
-	sgx_nr_high_pages = 2 * sgx_nr_low_pages;
-	ksgxswapd_tsk = kthread_run(ksgxswapd, NULL, "ksgxswapd");
-
 	return 0;
 err_freelist:
 	list_for_each_safe(parser, temp, &sgx_free_list) {
@@ -450,6 +447,13 @@  int sgx_page_cache_init(resource_size_t start, unsigned long size)
 	return -ENOMEM;
 }
 
+int sgx_page_cache_init(void)
+{
+	sgx_nr_high_pages = 2 * sgx_nr_low_pages;
+	ksgxswapd_tsk = kthread_run(ksgxswapd, NULL, "ksgxswapd");
+	return 0;
+}
+
 void sgx_page_cache_teardown(void)
 {
 	struct sgx_epc_page *entry;