From patchwork Mon Mar 20 20:00:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 9635239 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 681CC6020B for ; Mon, 20 Mar 2017 20:00:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5557C2793B for ; Mon, 20 Mar 2017 20:00:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 471E827A98; Mon, 20 Mar 2017 20:00:09 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_NONE,T_DKIM_INVALID autolearn=no 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 ACDC52793B for ; Mon, 20 Mar 2017 20:00:08 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 21B638046B; Mon, 20 Mar 2017 13:00:08 -0700 (PDT) X-Original-To: intel-sgx-kernel-dev@lists.01.org Delivered-To: intel-sgx-kernel-dev@lists.01.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (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 34CB28046B for ; Mon, 20 Mar 2017 13:00:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intel.com; i=@intel.com; q=dns/txt; s=intel; t=1490040006; x=1521576006; h=from:to:cc:subject:date:message-id; bh=x90y3XaJ9Hdg2aYSlSbLvl3p2XAHifRb8KhkVNDwD9c=; b=LV7iuD4qXzMJdYJIc1pDNY967zV5CIi00++DGRl2CQfZ7UOrQu+GKtBP H+nRWfePasy58QI0tMaQaBNUb7r79A==; Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Mar 2017 13:00:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.36,195,1486454400"; d="scan'208"; a="1110445199" Received: from sjchrist-ts.jf.intel.com ([10.54.74.20]) by orsmga001.jf.intel.com with ESMTP; 20 Mar 2017 13:00:05 -0700 From: Sean Christopherson To: intel-sgx-kernel-dev@lists.01.org Date: Mon, 20 Mar 2017 13:00:02 -0700 Message-Id: <1490040002-31158-1-git-send-email-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [intel-sgx-kernel-dev] [PATCH] intel_sgx: remove sgx_(un)pin_mm and call down/up_read(&mmap_sem) directly 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 sgx_pin_mm and sgx_unpin_mm and instead directly call down_read and up_read on encl->mm->mmap_sem. Add a check for SGX_ENCL_DEAD in sgx_isolate_pages as it previously relied on sgx_pin_mm to check for the enclave being dead. The other code in sgx_(un)pin_mm was either redundant and/or unnecessary. As both consumers of sgx_pin_mm lock the encl after sgx_pin_mm, it is more accurate to check SGX_ENCL_DEAD after the consumer locks the encl. Locking the enclave for the purposes of checking SGX_ENCL_DEAD is not functionally necessary when acquiring mmap_sem. Incrementing mm_count when locking encl->mm->mmap_sem is unnecessary as mm_count is incremented when encl->mmu_notifier is registered and is not decremented until the notifier is released in sgx_encl_release. Signed-off-by: Sean Christopherson --- drivers/platform/x86/intel_sgx.h | 2 -- drivers/platform/x86/intel_sgx_ioctl.c | 9 +++------ drivers/platform/x86/intel_sgx_page_cache.c | 12 ++++++------ drivers/platform/x86/intel_sgx_util.c | 27 --------------------------- 4 files changed, 9 insertions(+), 41 deletions(-) diff --git a/drivers/platform/x86/intel_sgx.h b/drivers/platform/x86/intel_sgx.h index adb5b17..a4c8265 100644 --- a/drivers/platform/x86/intel_sgx.h +++ b/drivers/platform/x86/intel_sgx.h @@ -217,8 +217,6 @@ int sgx_eremove(struct sgx_epc_page *epc_page); struct vm_area_struct *sgx_find_vma(struct sgx_encl *encl, unsigned long addr); void sgx_zap_tcs_ptes(struct sgx_encl *encl, struct vm_area_struct *vma); -bool sgx_pin_mm(struct sgx_encl *encl); -void sgx_unpin_mm(struct sgx_encl *encl); void sgx_invalidate(struct sgx_encl *encl); int sgx_find_encl(struct mm_struct *mm, unsigned long addr, struct vm_area_struct **vma); diff --git a/drivers/platform/x86/intel_sgx_ioctl.c b/drivers/platform/x86/intel_sgx_ioctl.c index e0e2f14..32739cf 100644 --- a/drivers/platform/x86/intel_sgx_ioctl.c +++ b/drivers/platform/x86/intel_sgx_ioctl.c @@ -220,10 +220,7 @@ static bool sgx_process_add_page_req(struct sgx_add_page_req *req) if (IS_ERR(epc_page)) return false; - if (!sgx_pin_mm(encl)) { - sgx_free_page(epc_page, encl, 0); - return false; - } + down_read(&encl->mm->mmap_sem); mutex_lock(&encl->lock); @@ -271,12 +268,12 @@ static bool sgx_process_add_page_req(struct sgx_add_page_req *req) list_add_tail(&encl_page->load_list, &encl->load_list); mutex_unlock(&encl->lock); - sgx_unpin_mm(encl); + up_read(&encl->mm->mmap_sem); return true; out: sgx_free_page(epc_page, encl, 0); mutex_unlock(&encl->lock); - sgx_unpin_mm(encl); + up_read(&encl->mm->mmap_sem); return false; } diff --git a/drivers/platform/x86/intel_sgx_page_cache.c b/drivers/platform/x86/intel_sgx_page_cache.c index e4f6b95..35dbf8d 100644 --- a/drivers/platform/x86/intel_sgx_page_cache.c +++ b/drivers/platform/x86/intel_sgx_page_cache.c @@ -195,6 +195,9 @@ static void sgx_isolate_pages(struct sgx_encl *encl, mutex_lock(&encl->lock); + if (encl->flags & SGX_ENCL_DEAD) + goto out; + for (i = 0; i < nr_to_scan; i++) { if (list_empty(&encl->load_list)) break; @@ -211,7 +214,7 @@ static void sgx_isolate_pages(struct sgx_encl *encl, list_move_tail(&entry->load_list, &encl->load_list); } } - +out: mutex_unlock(&encl->lock); } @@ -392,14 +395,11 @@ static void sgx_swap_pages(unsigned long nr_to_scan) if (!encl) goto out; - if (!sgx_pin_mm(encl)) - goto out_enclave; - + down_read(&encl->mm->mmap_sem); sgx_isolate_pages(encl, &cluster, nr_to_scan); sgx_write_pages(encl, &cluster); - sgx_unpin_mm(encl); + up_read(&encl->mm->mmap_sem); -out_enclave: kref_put(&encl->refcount, sgx_encl_release); out: kref_put(&ctx->refcount, sgx_tgid_ctx_release); diff --git a/drivers/platform/x86/intel_sgx_util.c b/drivers/platform/x86/intel_sgx_util.c index 234a5fb..2671f00 100644 --- a/drivers/platform/x86/intel_sgx_util.c +++ b/drivers/platform/x86/intel_sgx_util.c @@ -147,33 +147,6 @@ void sgx_zap_tcs_ptes(struct sgx_encl *encl, struct vm_area_struct *vma) } } -bool sgx_pin_mm(struct sgx_encl *encl) -{ - mutex_lock(&encl->lock); - if (encl->flags & SGX_ENCL_DEAD) { - mutex_unlock(&encl->lock); - return false; - } - - atomic_inc(&encl->mm->mm_count); - mutex_unlock(&encl->lock); - - down_read(&encl->mm->mmap_sem); - - if (encl->flags & SGX_ENCL_DEAD) { - sgx_unpin_mm(encl); - return false; - } - - return true; -} - -void sgx_unpin_mm(struct sgx_encl *encl) -{ - up_read(&encl->mm->mmap_sem); - mmdrop(encl->mm); -} - void sgx_invalidate(struct sgx_encl *encl) { struct vm_area_struct *vma;