From patchwork Wed Jun 14 17:37:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 9787103 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id ACF836038E for ; Wed, 14 Jun 2017 17:38:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 958331FF13 for ; Wed, 14 Jun 2017 17:38:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8A66C27813; Wed, 14 Jun 2017 17:38:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B7CE426E3A for ; Wed, 14 Jun 2017 17:38:01 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 2087021A143E3; Wed, 14 Jun 2017 10:36:45 -0700 (PDT) X-Original-To: intel-sgx-kernel-dev@lists.01.org Delivered-To: intel-sgx-kernel-dev@lists.01.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 274A021A18AAA for ; Wed, 14 Jun 2017 10:36:44 -0700 (PDT) Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 Jun 2017 10:37:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.39,341,1493708400"; d="scan'208";a="113210022" Received: from sjchrist-ts.jf.intel.com ([10.54.74.20]) by orsmga005.jf.intel.com with ESMTP; 14 Jun 2017 10:37:45 -0700 From: Sean Christopherson To: intel-sgx-kernel-dev@lists.01.org Date: Wed, 14 Jun 2017 10:37:30 -0700 Message-Id: <1497461858-20309-5-git-send-email-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1497461858-20309-1-git-send-email-sean.j.christopherson@intel.com> References: <1497461858-20309-1-git-send-email-sean.j.christopherson@intel.com> Subject: [intel-sgx-kernel-dev] [RFC][PATCH 04/12] intel_sgx: remove tgid_ctx, track tgid pid in encl X-BeenThere: intel-sgx-kernel-dev@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Project: Intel® Software Guard Extensions for Linux*: https://01.org/intel-software-guard-extensions" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-sgx-kernel-dev-bounces@lists.01.org Sender: "intel-sgx-kernel-dev" X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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(-) 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(¤t->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);