[intel-sgx-kernel-dev,RFC,04/12] intel_sgx: remove tgid_ctx, track tgid pid in encl
diff mbox

Message ID 1497461858-20309-5-git-send-email-sean.j.christopherson@intel.com
State New
Headers show

Commit Message

Sean Christopherson June 14, 2017, 5:37 p.m. UTC
Remove struct sgx_tgid_ctx as grouping enclaves per-process is no
longer necessary, e.g. for EPC swapping purposes.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 drivers/platform/x86/intel_sgx/sgx.h            | 17 ++----
 drivers/platform/x86/intel_sgx/sgx_ioctl.c      | 74 ++-----------------------
 drivers/platform/x86/intel_sgx/sgx_main.c       | 11 ++--
 drivers/platform/x86/intel_sgx/sgx_page_cache.c |  4 +-
 drivers/platform/x86/intel_sgx/sgx_util.c       | 11 ++--
 5 files changed, 19 insertions(+), 98 deletions(-)

Patch
diff mbox

diff --git a/drivers/platform/x86/intel_sgx/sgx.h b/drivers/platform/x86/intel_sgx/sgx.h
index 93c1730..eba67ce 100644
--- a/drivers/platform/x86/intel_sgx/sgx.h
+++ b/drivers/platform/x86/intel_sgx/sgx.h
@@ -111,13 +111,6 @@  struct sgx_encl_page {
 	unsigned int va_offset;
 };
 
-struct sgx_tgid_ctx {
-	struct pid *tgid;
-	struct kref refcount;
-	struct list_head encl_list;
-	struct list_head list;
-};
-
 enum sgx_encl_flags {
 	SGX_ENCL_INITIALIZED	= BIT(0),
 	SGX_ENCL_DEBUG		= BIT(1),
@@ -131,6 +124,7 @@  struct sgx_encl {
 	unsigned int secs_child_cnt;
 	struct mutex lock;
 	struct mm_struct *mm;
+	struct pid *tgid;
 	struct file *backing;
 	struct file *pcmd;
 	struct kref refcount;
@@ -141,7 +135,6 @@  struct sgx_encl {
 	struct list_head add_page_reqs;
 	struct work_struct add_page_work;
 	struct sgx_encl_page secs_page;
-	struct sgx_tgid_ctx *tgid_ctx;
 	struct list_head encl_list;
 	struct mmu_notifier mmu_notifier;
 };
@@ -164,11 +157,10 @@  extern u32 sgx_ssaframesize_tbl[64];
 extern bool sgx_has_sgx2;
 
 extern const struct vm_operations_struct sgx_vm_ops;
-extern atomic_t sgx_nr_pids;
 
 #define sgx_pr_ratelimited(level, encl, fmt, ...)			  \
 	pr_ ## level ## _ratelimited("intel_sgx: [%d:0x%p] " fmt,	  \
-				     pid_nr((encl)->tgid_ctx->tgid),	  \
+				     pid_nr((encl)->tgid),	  	  \
 				     (void *)(encl)->base, ##__VA_ARGS__)
 
 #define sgx_dbg(encl, fmt, ...) sgx_pr_ratelimited(debug, encl, fmt, ##__VA_ARGS__)
@@ -208,10 +200,9 @@  struct sgx_encl_page *sgx_fault_page(struct vm_area_struct *vma,
 				     unsigned int flags);
 
 void sgx_encl_release(struct kref *ref);
-void sgx_tgid_ctx_release(struct kref *ref);
 
-extern struct mutex sgx_tgid_ctx_mutex;
-extern struct list_head sgx_tgid_ctx_list;
+extern struct mutex sgx_encl_mutex;
+extern struct list_head sgx_encl_list;
 extern struct task_struct *ksgxswapd_tsk;
 
 int ksgxswapd(void *p);
diff --git a/drivers/platform/x86/intel_sgx/sgx_ioctl.c b/drivers/platform/x86/intel_sgx/sgx_ioctl.c
index 17999c6..374e2a3 100644
--- a/drivers/platform/x86/intel_sgx/sgx_ioctl.c
+++ b/drivers/platform/x86/intel_sgx/sgx_ioctl.c
@@ -78,69 +78,6 @@  struct sgx_add_page_req {
 };
 
 static u16 sgx_isvsvnle_min;
-atomic_t sgx_nr_pids = ATOMIC_INIT(0);
-
-static struct sgx_tgid_ctx *sgx_find_tgid_ctx(struct pid *tgid)
-{
-	struct sgx_tgid_ctx *ctx;
-
-	list_for_each_entry(ctx, &sgx_tgid_ctx_list, list)
-		if (pid_nr(ctx->tgid) == pid_nr(tgid))
-			return ctx;
-
-	return NULL;
-}
-
-static int sgx_add_to_tgid_ctx(struct sgx_encl *encl)
-{
-	struct sgx_tgid_ctx *ctx;
-	struct pid *tgid = get_pid(task_tgid(current));
-
-	mutex_lock(&sgx_tgid_ctx_mutex);
-
-	ctx = sgx_find_tgid_ctx(tgid);
-	if (ctx) {
-		if (kref_get_unless_zero(&ctx->refcount)) {
-			encl->tgid_ctx = ctx;
-			mutex_unlock(&sgx_tgid_ctx_mutex);
-			put_pid(tgid);
-			return 0;
-		}
-		else
-			list_del_init(&ctx->list);
-	}
-
-	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
-	if (!ctx) {
-		mutex_unlock(&sgx_tgid_ctx_mutex);
-		put_pid(tgid);
-		return -ENOMEM;
-	}
-
-	ctx->tgid = tgid;
-	kref_init(&ctx->refcount);
-	INIT_LIST_HEAD(&ctx->encl_list);
-
-	list_add(&ctx->list, &sgx_tgid_ctx_list);
-	atomic_inc(&sgx_nr_pids);
-
-	encl->tgid_ctx = ctx;
-
-	mutex_unlock(&sgx_tgid_ctx_mutex);
-	return 0;
-}
-
-void sgx_tgid_ctx_release(struct kref *ref)
-{
-	struct sgx_tgid_ctx *pe =
-		container_of(ref, struct sgx_tgid_ctx, refcount);
-	mutex_lock(&sgx_tgid_ctx_mutex);
-	list_del(&pe->list);
-	atomic_dec(&sgx_nr_pids);
-	mutex_unlock(&sgx_tgid_ctx_mutex);
-	put_pid(pe->tgid);
-	kfree(pe);
-}
 
 static int sgx_find_and_get_encl(unsigned long addr, struct sgx_encl **encl)
 {
@@ -470,6 +407,7 @@  static long sgx_ioc_enclave_create(struct file *filep, unsigned int cmd,
 	INIT_WORK(&encl->add_page_work, sgx_add_page_worker);
 
 	encl->mm = current->mm;
+	encl->tgid = get_pid(task_tgid(current));
 	encl->base = secs->base;
 	encl->size = secs->size;
 	encl->backing = backing;
@@ -482,10 +420,6 @@  static long sgx_ioc_enclave_create(struct file *filep, unsigned int cmd,
 		goto out;
 	}
 
-	ret = sgx_add_to_tgid_ctx(encl);
-	if (ret)
-		goto out;
-
 	ret = sgx_init_page(encl, &encl->secs_page, encl->base + encl->size,
 			    0);
 	if (ret)
@@ -534,9 +468,9 @@  static long sgx_ioc_enclave_create(struct file *filep, unsigned int cmd,
 	vma->vm_private_data = encl;
 	up_read(&current->mm->mmap_sem);
 
-	mutex_lock(&sgx_tgid_ctx_mutex);
-	list_add_tail(&encl->encl_list, &encl->tgid_ctx->encl_list);
-	mutex_unlock(&sgx_tgid_ctx_mutex);
+	mutex_lock(&sgx_encl_mutex);
+	list_add_tail(&encl->encl_list, &sgx_encl_list);
+	mutex_unlock(&sgx_encl_mutex);
 
 out:
 	if (ret && encl)
diff --git a/drivers/platform/x86/intel_sgx/sgx_main.c b/drivers/platform/x86/intel_sgx/sgx_main.c
index 98ee05d..fc9d8ea 100644
--- a/drivers/platform/x86/intel_sgx/sgx_main.c
+++ b/drivers/platform/x86/intel_sgx/sgx_main.c
@@ -235,18 +235,15 @@  static int sgx_init_platform(void)
 
 static int sgx_pm_suspend(struct device *dev)
 {
-	struct sgx_tgid_ctx *ctx;
 	struct sgx_encl *encl;
 
 	kthread_stop(ksgxswapd_tsk);
 	ksgxswapd_tsk = NULL;
 
-	list_for_each_entry(ctx, &sgx_tgid_ctx_list, list) {
-		list_for_each_entry(encl, &ctx->encl_list, encl_list) {
-			sgx_invalidate(encl, false);
-			encl->flags |= SGX_ENCL_SUSPEND;
-			flush_work(&encl->add_page_work);
-		}
+	list_for_each_entry(encl, &sgx_encl_list, encl_list) {
+		sgx_invalidate(encl, false);
+		encl->flags |= SGX_ENCL_SUSPEND;
+		flush_work(&encl->add_page_work);
 	}
 
 	return 0;
diff --git a/drivers/platform/x86/intel_sgx/sgx_page_cache.c b/drivers/platform/x86/intel_sgx/sgx_page_cache.c
index d47e4e6..9976205 100644
--- a/drivers/platform/x86/intel_sgx/sgx_page_cache.c
+++ b/drivers/platform/x86/intel_sgx/sgx_page_cache.c
@@ -74,8 +74,8 @@  static DEFINE_SPINLOCK(sgx_free_list_lock);
 static LIST_HEAD(sgx_active_list);
 static DEFINE_SPINLOCK(sgx_active_list_lock);
 
-LIST_HEAD(sgx_tgid_ctx_list);
-DEFINE_MUTEX(sgx_tgid_ctx_mutex);
+LIST_HEAD(sgx_encl_list);
+DEFINE_MUTEX(sgx_encl_mutex);
 static unsigned int sgx_nr_total_epc_pages;
 static unsigned int sgx_nr_free_pages;
 static unsigned int sgx_nr_low_pages = SGX_NR_LOW_EPC_PAGES_DEFAULT;
diff --git a/drivers/platform/x86/intel_sgx/sgx_util.c b/drivers/platform/x86/intel_sgx/sgx_util.c
index 94efee8..543faa3 100644
--- a/drivers/platform/x86/intel_sgx/sgx_util.c
+++ b/drivers/platform/x86/intel_sgx/sgx_util.c
@@ -381,10 +381,9 @@  void sgx_encl_release(struct kref *ref)
 	struct radix_tree_iter iter;
 	void **slot;
 
-	mutex_lock(&sgx_tgid_ctx_mutex);
-	if (!list_empty(&encl->encl_list))
-		list_del(&encl->encl_list);
-	mutex_unlock(&sgx_tgid_ctx_mutex);
+	mutex_lock(&sgx_encl_mutex);
+	list_del(&encl->encl_list);
+	mutex_unlock(&sgx_encl_mutex);
 
 	if (encl->mmu_notifier.ops)
 		mmu_notifier_unregister_no_release(&encl->mmu_notifier,
@@ -413,8 +412,8 @@  void sgx_encl_release(struct kref *ref)
 
 	encl->secs_page.epc_page = NULL;
 
-	if (encl->tgid_ctx)
-		kref_put(&encl->tgid_ctx->refcount, sgx_tgid_ctx_release);
+	if (encl->tgid)
+		put_pid(encl->tgid);
 
 	if (encl->backing)
 		fput(encl->backing);