Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp109220ybk; Thu, 14 May 2020 17:49:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx5ZOcsMkkrOiC4yhMRaiHqeQSI4q1zaE56foHYvdM/Q5bEuH12z3JaHOSzZ9CyHVVdaLeQ X-Received: by 2002:aa7:cc84:: with SMTP id p4mr681779edt.216.1589503774842; Thu, 14 May 2020 17:49:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589503774; cv=none; d=google.com; s=arc-20160816; b=HQwa1VVSgcD1UmtLeyGVZlXhNr9q3lcjEUjVvVPHfjVBEWP+InlDyWfbqHbwSMooOq MVdBwPUe49cJwrhLMsvZqFXymQy+MmnGl85MmTnejO/DLnBliox4ZgRL0PIqZ9jK7IOY d6olcSG7tUrHmyPvRPpJ5HFdH32AfEjw1yx1xoCFTcwYuELlUVrVJuaDjr5kzNEqsHt+ Rg3PmMnGHPmtTerCmFzBP9KQQ1XnKb+p9q7/21VRqNmZGaogl53kZuAqd+ZqcowPdnb7 cq3ob1C1pNwie0p/j0/vUhE6rQMzA1MPoE9u81LruaHqX0zzui9o3bkpMS9X05MQSLLZ Cb8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=FhSl44Y9sCpXWAcIx3eShFjXIEcq+mOy3g0H+EMrDmM=; b=UOd5ncz+RkYwWwJqC7HaR4Ws1eZX+D8aZvqrLa1XSLYSppU5d+MbF2iENnMDU+3kcJ emu/feoNUfbu/ASHCHogwHuZAYtZubyrExhQfnafK0XSfmqeUN16tbEX44Z3tFUhIJIy jlAZITr0B/5E6j3ys0anZOpOW+6rffwhGBaZ7MpUEufdvLQHmj49st4xcK00jIV3Bv8g A0nWoY04DZ3qaweRSRTymfaSofi8cYeA2I3x9E/cNwBzCMWcIW4dXBZubpIq1ihz7eqG rDMbnpOy7Y234NAjuJSFj/Y5vmLMROu0hFO7rVwgz2ewsV6H8Daku+aqTk9vL2jTAN+W 4HOg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d7si310621ejo.296.2020.05.14.17.49.10; Thu, 14 May 2020 17:49:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728347AbgEOApo (ORCPT + 99 others); Thu, 14 May 2020 20:45:44 -0400 Received: from mga09.intel.com ([134.134.136.24]:63259 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726170AbgEOApn (ORCPT ); Thu, 14 May 2020 20:45:43 -0400 IronPort-SDR: 5EX4rDW6/3GM19S5AIGlVBo2E16vixzyTN1LwLAAALjhePuusqduUmvuE0qnjyrBB/cbDk8NPq lC16m3feXcnQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 May 2020 17:45:43 -0700 IronPort-SDR: ZbcrqZTNWyse2c2kvB+HM45ArxZmSwVRvs+JuESD4ZdBGykoZ6by/U4sDyrL8mJVZ97tYO6e6u yONlLLhntIcw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,393,1583222400"; d="scan'208";a="253644196" Received: from ashadrin-mobl1.ccr.corp.intel.com (HELO localhost) ([10.249.38.112]) by fmsmga008.fm.intel.com with ESMTP; 14 May 2020 17:45:35 -0700 From: Jarkko Sakkinen To: linux-kernel@vger.kernel.org, x86@kernel.org, linux-sgx@vger.kernel.org Cc: akpm@linux-foundation.org, dave.hansen@intel.com, sean.j.christopherson@intel.com, nhorman@redhat.com, npmccallum@redhat.com, haitao.huang@intel.com, andriy.shevchenko@linux.intel.com, tglx@linutronix.de, kai.svahn@intel.com, bp@alien8.de, josh@joshtriplett.org, luto@kernel.org, kai.huang@intel.com, rientjes@google.com, cedric.xing@intel.com, puiterwijk@redhat.com, Jarkko Sakkinen , Jethro Beekman Subject: [PATCH v30 08/20] x86/sgx: Add functions to allocate and free EPC pages Date: Fri, 15 May 2020 03:43:58 +0300 Message-Id: <20200515004410.723949-9-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200515004410.723949-1-jarkko.sakkinen@linux.intel.com> References: <20200515004410.723949-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add functions for allocating page from Enclave Page Cache (EPC). A page is allocated by going through the EPC sections and returning the first free page. When a page is freed, it might have a valid state, which means that the callee has assigned it to an enclave, which are protected memory ares used to run code protected from outside access. The page is returned back to the invalid state with ENCLS[EREMOVE] [1]. [1] Intel SDM: 40.3 INTELĀ® SGX SYSTEM LEAF FUNCTION REFERENCE Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Acked-by: Jethro Beekman Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/main.c | 60 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 3 ++ 2 files changed, 63 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 38424c1e8341..60d82e7537c8 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -13,6 +13,66 @@ struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; int sgx_nr_epc_sections; +static struct sgx_epc_page *__sgx_try_alloc_page(struct sgx_epc_section *section) +{ + struct sgx_epc_page *page; + + if (list_empty(§ion->page_list)) + return NULL; + + page = list_first_entry(§ion->page_list, struct sgx_epc_page, list); + list_del_init(&page->list); + return page; +} + +/** + * sgx_try_alloc_page() - Allocate an EPC page + * + * Try to grab a page from the free EPC page list. + * + * Return: + * a pointer to a &struct sgx_epc_page instance, + * -errno on error + */ +struct sgx_epc_page *sgx_try_alloc_page(void) +{ + struct sgx_epc_section *section; + struct sgx_epc_page *page; + int i; + + for (i = 0; i < sgx_nr_epc_sections; i++) { + section = &sgx_epc_sections[i]; + spin_lock(§ion->lock); + page = __sgx_try_alloc_page(section); + spin_unlock(§ion->lock); + + if (page) + return page; + } + + return ERR_PTR(-ENOMEM); +} + +/** + * sgx_free_page() - Free an EPC page + * @page: pointer a previously allocated EPC page + * + * EREMOVE an EPC page and insert it back to the list of free pages. + */ +void sgx_free_page(struct sgx_epc_page *page) +{ + struct sgx_epc_section *section = sgx_epc_section(page); + int ret; + + ret = __eremove(sgx_epc_addr(page)); + if (WARN_ONCE(ret, "EREMOVE returned %d (0x%x)", ret, ret)) + return; + + spin_lock(§ion->lock); + list_add_tail(&page->list, §ion->page_list); + spin_unlock(§ion->lock); +} + static void __init sgx_free_epc_section(struct sgx_epc_section *section) { struct sgx_epc_page *page; diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index aad30980be32..aa85f85412d8 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -67,4 +67,7 @@ extern struct task_struct *ksgxswapd_tsk; bool __init sgx_page_reclaimer_init(void); +struct sgx_epc_page *sgx_try_alloc_page(void); +void sgx_free_page(struct sgx_epc_page *page); + #endif /* _X86_SGX_H */ -- 2.25.1