Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp2256647ybi; Sat, 13 Jul 2019 10:11:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqwoyiw+lr3M1adBkmebNR8jrei61sQXcYK63/ei5eovfU2oUdY1WlMAhfyrmxs4zVPZu0zF X-Received: by 2002:a63:b1d:: with SMTP id 29mr17774268pgl.103.1563037889567; Sat, 13 Jul 2019 10:11:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563037889; cv=none; d=google.com; s=arc-20160816; b=t7xPQMJmAZdT0alCil4683DWtMsUGuEy/jdqxM0huJGCCfZGv5AkPULf4JrRZT1D3s bo5mxKVQl7mTY5NFQyh95QUafInT0NTXGjNO8zuOxnkBsOI2XgSAy+p9QhjhTHzXFgsv A3LMFw6vt0k0vmTOqoYXZjdSg1ov4SDeFghFx6FmVMVvxdTqLWmW6hHmWolKE1GDIb1B UucODDoxGb9jZoosOz0SP1luklc0DI4GUHmS/etQu8S8J3OancfQLlP4r2c2M/NT4asP T8DRLJxqcz8n/4xuEyPN+1/kYMMORkyxa7a7kS1PivoU4i+0Wx34L6/9hTvyofPs6XJQ jigA== 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; bh=za99vLngKITBG6x+wocJfpSGVVDsrbxxDERyJJez0c4=; b=mLTMu401JQbE7JRqn7C5OsiAS0PIuVLMxch78XNfhStFMGBFPN03trp67mRv8oEGYI kAWcSK2FkG7UzlfChcOYnonGLfBsiR7c7yYYTiroPyHU5F5NsFfn03KnRdWXh7DlPngz tiTZVfVL5thXSD4wdAP4NRYV9nILjLpipRht6e0mbM5WRr8HV6hVh2abTVpuXxIRFqVk 27Mw0t0o00wDKw9x/xuft9v1o0tH5PBREe4/XIU1HrYHJgY1XJfLVDfRR4ob10P3BKL2 p8ym2S+TRL4O63dBj5cXSd62kjufsXqjudhgEFZaqb75OxEYau28iDpqru+g0jMwvL+I 5YKg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id w9si11137011pgr.250.2019.07.13.10.11.14; Sat, 13 Jul 2019 10:11:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728106AbfGMRKm (ORCPT + 99 others); Sat, 13 Jul 2019 13:10:42 -0400 Received: from mga04.intel.com ([192.55.52.120]:24667 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727784AbfGMRKm (ORCPT ); Sat, 13 Jul 2019 13:10:42 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Jul 2019 10:10:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,487,1557212400"; d="scan'208";a="341981368" Received: from hbriegel-mobl.ger.corp.intel.com (HELO localhost) ([10.252.50.48]) by orsmga005.jf.intel.com with ESMTP; 13 Jul 2019 10:10:31 -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, serge.ayoun@intel.com, shay.katz-zamir@intel.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, Jarkko Sakkinen Subject: [PATCH v21 13/28] x86/sgx: Add functions to allocate and free EPC pages Date: Sat, 13 Jul 2019 20:07:49 +0300 Message-Id: <20190713170804.2340-14-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190713170804.2340-1-jarkko.sakkinen@linux.intel.com> References: <20190713170804.2340-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org At this time there is no support for reclaiming pages prior to the owner explicitly freeing the page. As for freeing pages, because freeing a page is expected to succeed in the vast majority of cases and because most call sites will not be equipped to handle failure, provide a variant for freeing a page that warns on failure, e.g. due to ENCLS[EREMOVE] failing. Signed-off-by: Jarkko Sakkinen Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- arch/x86/kernel/cpu/sgx/main.c | 90 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 4 ++ 2 files changed, 94 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index e2317f6e4374..6b4727df72ca 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -9,6 +9,7 @@ #include #include #include "arch.h" +#include "encls.h" #include "sgx.h" struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; @@ -16,6 +17,95 @@ EXPORT_SYMBOL_GPL(sgx_epc_sections); int sgx_nr_epc_sections; +static struct sgx_epc_page *sgx_section_get_page( + struct sgx_epc_section *section) +{ + struct sgx_epc_page *page; + + if (!section->free_cnt) + return NULL; + + page = list_first_entry(§ion->page_list, + struct sgx_epc_page, list); + list_del_init(&page->list); + section->free_cnt--; + return page; +} + +/** + * sgx_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_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_section_get_page(section); + spin_unlock(§ion->lock); + + if (page) + return page; + } + + return ERR_PTR(-ENOMEM); +} +EXPORT_SYMBOL_GPL(sgx_alloc_page); + +/** + * __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. + * + * Return: + * 0 on success + * SGX error code if EREMOVE fails + */ +int __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 (ret) + return ret; + + spin_lock(§ion->lock); + list_add_tail(&page->list, §ion->page_list); + section->free_cnt++; + spin_unlock(§ion->lock); + + return 0; +} +EXPORT_SYMBOL_GPL(__sgx_free_page); + +/** + * sgx_free_page - Free an EPC page and WARN on failure + * @page: pointer to a previously allocated EPC page + * + * EREMOVE an EPC page and insert it back to the list of free pages, and WARN + * if EREMOVE fails. For use when the call site cannot (or chooses not to) + * handle failure, i.e. the page is leaked on failure. + */ +void sgx_free_page(struct sgx_epc_page *page) +{ + int ret; + + ret = __sgx_free_page(page); + WARN(ret > 0, "sgx: EREMOVE returned %d (0x%x)", ret, ret); +} +EXPORT_SYMBOL_GPL(sgx_free_page); + static __init void 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 3009ec816339..210510a28ce0 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -64,4 +64,8 @@ extern int sgx_nr_epc_sections; int sgx_page_reclaimer_init(void); +struct sgx_epc_page *sgx_alloc_page(void); +int __sgx_free_page(struct sgx_epc_page *page); +void sgx_free_page(struct sgx_epc_page *page); + #endif /* _X86_SGX_H */ -- 2.20.1