From patchwork Mon Jan 16 14:19:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 9518935 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 81C8D601C3 for ; Mon, 16 Jan 2017 14:19:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 630292848E for ; Mon, 16 Jan 2017 14:19:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 57A3E28491; Mon, 16 Jan 2017 14:19:25 +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 6105D2848E for ; Mon, 16 Jan 2017 14:19:24 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 6D9C981DA9 for ; Mon, 16 Jan 2017 06:19:23 -0800 (PST) X-Original-To: intel-sgx-kernel-dev@lists.01.org Delivered-To: intel-sgx-kernel-dev@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) (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 238BB81DA9 for ; Mon, 16 Jan 2017 06:19:23 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP; 16 Jan 2017 06:19:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.33,239,1477983600"; d="scan'208"; a="1083593386" Received: from jsakkine-mobl1.tm.intel.com (HELO localhost) ([10.237.50.85]) by orsmga001.jf.intel.com with ESMTP; 16 Jan 2017 06:19:21 -0800 From: Jarkko Sakkinen To: intel-sgx-kernel-dev@lists.01.org Date: Mon, 16 Jan 2017 16:19:18 +0200 Message-Id: <20170116141918.17382-1-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.9.3 Subject: [intel-sgx-kernel-dev] [PATCH] intel_sgx: use VM_DONTCOPY X-BeenThere: intel-sgx-kernel-dev@lists.01.org X-Mailman-Version: 2.1.21 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 In order to have well defined semantics for forking move into using VM_DONTCOPY for the enclave VMA. Signed-off-by: Jarkko Sakkinen Reported-by: Jethro Beekman --- drivers/platform/x86/intel_sgx_main.c | 7 ++----- drivers/platform/x86/intel_sgx_vma.c | 27 +++++---------------------- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/drivers/platform/x86/intel_sgx_main.c b/drivers/platform/x86/intel_sgx_main.c index 93dd708..b5e414a 100644 --- a/drivers/platform/x86/intel_sgx_main.c +++ b/drivers/platform/x86/intel_sgx_main.c @@ -103,11 +103,8 @@ long sgx_compat_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) static int sgx_mmap(struct file *file, struct vm_area_struct *vma) { vma->vm_ops = &sgx_vm_ops; -#if !defined(VM_RESERVED) - vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_IO; -#else - vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_RESERVED | VM_IO; -#endif + vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_IO | + VM_DONTCOPY; return 0; } diff --git a/drivers/platform/x86/intel_sgx_vma.c b/drivers/platform/x86/intel_sgx_vma.c index e670405..11d1c3b 100644 --- a/drivers/platform/x86/intel_sgx_vma.c +++ b/drivers/platform/x86/intel_sgx_vma.c @@ -71,34 +71,18 @@ static void sgx_vma_open(struct vm_area_struct *vma) { struct sgx_encl *encl = vma->vm_private_data; - - /* When forking for the second time vm_private_data is already set to - * NULL. - */ - if (!encl) { - zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start); + if (!encl) return; - } - - /* Invalidate enclave when the process has been forked. */ - mutex_lock(&encl->lock); - if (encl->mm != vma->vm_mm) { - sgx_invalidate(encl); - vma->vm_private_data = NULL; - } - mutex_unlock(&encl->lock); - if (vma->vm_private_data) - kref_get(&encl->refcount); + /* kref cannot underflow because ECREATE ioctl checks that there is only + * one single VMA for the enclave before proceeding. + */ + kref_get(&encl->refcount); } static void sgx_vma_close(struct vm_area_struct *vma) { struct sgx_encl *encl = vma->vm_private_data; - - /* When forking for the second time vm_private_data is already set to - * NULL. - */ if (!encl) return; @@ -106,7 +90,6 @@ static void sgx_vma_close(struct vm_area_struct *vma) zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start); encl->flags |= SGX_ENCL_DEAD; mutex_unlock(&encl->lock); - kref_put(&encl->refcount, sgx_encl_release); }