diff mbox

[intel-sgx-kernel-dev,v2,2/2] intel_sgx: ensure ksgxswapd_tsk is never invalid/stale

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

Commit Message

Sean Christopherson Aug. 1, 2017, 10:36 p.m. UTC
Use a tmp variable to hold and query the result of kthread_run and
only set ksgxswapd_tsk if kthread_run is successful, and nullify
ksgxswapd_tsk when it is stopped.  Ensuring ksgxswapd_tsk is never
invalid/stale eliminates a path to a potential kernel panic, e.g.
if, in the future, sgx_page_cache_teardown needs to be called even
when sgx_page_cache_init fails.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 drivers/platform/x86/intel_sgx/sgx_page_cache.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/drivers/platform/x86/intel_sgx/sgx_page_cache.c b/drivers/platform/x86/intel_sgx/sgx_page_cache.c
index f1553324..1368747 100644
--- a/drivers/platform/x86/intel_sgx/sgx_page_cache.c
+++ b/drivers/platform/x86/intel_sgx/sgx_page_cache.c
@@ -421,9 +421,14 @@  int sgx_add_epc_bank(resource_size_t start, unsigned long size)
 
 int sgx_page_cache_init(void)
 {
+	struct task_struct *tmp;
+
 	sgx_nr_high_pages = 2 * sgx_nr_low_pages;
-	ksgxswapd_tsk = kthread_run(ksgxswapd, NULL, "ksgxswapd");
-	return PTR_ERR_OR_ZERO(ksgxswapd_tsk);
+
+	tmp = kthread_run(ksgxswapd, NULL, "ksgxswapd");
+	if (!IS_ERR(tmp))
+		ksgxswapd_tsk = tmp;
+	return PTR_ERR_OR_ZERO(tmp);
 }
 
 void sgx_page_cache_teardown(void)
@@ -431,8 +436,10 @@  void sgx_page_cache_teardown(void)
 	struct sgx_epc_page *entry;
 	struct list_head *parser, *temp;
 
-	if (ksgxswapd_tsk)
+	if (ksgxswapd_tsk) {
 		kthread_stop(ksgxswapd_tsk);
+		ksgxswapd_tsk = NULL;
+	}
 
 	spin_lock(&sgx_free_list_lock);
 	list_for_each_safe(parser, temp, &sgx_free_list) {