Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1236257ybh; Thu, 16 Jul 2020 06:58:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgdBgt/YyOaU9gtSJPlf6hMh/bfrkT/stXHlshJVa8BiRgs7nMcNuZv+iOObcrrSf0I8gv X-Received: by 2002:aa7:c504:: with SMTP id o4mr4465572edq.311.1594907930411; Thu, 16 Jul 2020 06:58:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594907930; cv=none; d=google.com; s=arc-20160816; b=OaC0WYK57vp38UiXU91TKznxPFar0JygcZAC/hb/Nho0cX3ASiB9/332mzjBt2OnzK 0ruDfzkSF3KPshD9Q2EFSEUvKvcSgKLNe2GeXBMx5WQXWoa4g5bAX1Jo81koIFD5FCVg WzwD4wPZqot5ARBzqgZmFLV8EQ9QtjM3JPMmuTCOxOv5qJbUmlkElzcRrRinXg5jj88l DoZoFqUCuRZRol0fdc01PIUUtDnCkgVrT+Pmf9CNRnmCfCbKBgyLR3DjBxxdjgPtanaf qwMYIFuAJfqjEJam0DgVpdfzFQODdK5FZQKVCYpXMrc4Px2jqLlpgKxHkYoxRv+/FZBm Lbbw== 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=q1As+2Ivnr/yXYs3ZRujEFcnV4SrI+ZyfUApXTdwJ38=; b=wK1xU0ZJE/z5K33q88z9HySh9QVIkzMcdfnrUVElsv2Hja7b4R5cXi5aSB1e7dIMJP ERzoU9uF9Zhw8jlOK08q/JH6gwRLARNLqzAxYeX35V0Xbvpqda479h8Je2h4g8T3qewz vwNOl5/LbUfDhsWoSt6Y75wCGSOgghWLv7Nbw/pt7mdzkMqKks8m+gyq3Hlw23MzXgta Xf6dpK/YGAll/c0iG0TzFrYU835tWWcMTuKWug8jFvToVyZGlTpYqROF8ZZfD8hKO9zg sx+UJdK5rCVfA/LnvZasmVNwbDwYodSfTT4qP0Ckip6j+e9ktU6J11kJQBTqHqlfY0UQ DImQ== 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 w5si3336321edx.179.2020.07.16.06.58.27; Thu, 16 Jul 2020 06:58:50 -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 S1728995AbgGPNzU (ORCPT + 99 others); Thu, 16 Jul 2020 09:55:20 -0400 Received: from mga11.intel.com ([192.55.52.93]:48248 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728929AbgGPNzU (ORCPT ); Thu, 16 Jul 2020 09:55:20 -0400 IronPort-SDR: RGaWZH6GzWmXIwZSzNWYPrgGkz4QJPcxNK7vtVcgHR4Nz+QtI3xCEeH23+v7UaSO7Jr2xQ54p2 +021cKGvtFzw== X-IronPort-AV: E=McAfee;i="6000,8403,9683"; a="147368627" X-IronPort-AV: E=Sophos;i="5.75,359,1589266800"; d="scan'208";a="147368627" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jul 2020 06:55:19 -0700 IronPort-SDR: /J8TKlXZxJdbzCmy9cTdXOofHyJ50NDnFPOjSupd5YkC7TdG7zc8KDjVPkyZciif1HppLibeAI zIEbzN3K3y5w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,359,1589266800"; d="scan'208";a="325150344" Received: from lettner-mobl.ger.corp.intel.com (HELO localhost) ([10.252.32.212]) by FMSMGA003.fm.intel.com with ESMTP; 16 Jul 2020 06:55:09 -0700 From: Jarkko Sakkinen To: x86@kernel.org, linux-sgx@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jarkko Sakkinen , Jethro Beekman , Sean Christopherson , akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, asapek@google.com, bp@alien8.de, cedric.xing@intel.com, chenalexchen@google.com, conradparker@google.com, cyhanish@google.com, dave.hansen@intel.com, haitao.huang@intel.com, josh@joshtriplett.org, kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com, ludloff@google.com, luto@kernel.org, nhorman@redhat.com, npmccallum@redhat.com, puiterwijk@redhat.com, rientjes@google.com, tglx@linutronix.de, yaozhangx@google.com Subject: [PATCH v36 09/24] x86/sgx: Add __sgx_alloc_epc_page() and sgx_free_epc_page() Date: Thu, 16 Jul 2020 16:52:48 +0300 Message-Id: <20200716135303.276442-10-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200716135303.276442-1-jarkko.sakkinen@linux.intel.com> References: <20200716135303.276442-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 Add __sgx_alloc_epc_page(), which iterates through EPC sections and borrows a page structure that is not used by anyone else. When a page is no longer needed it must be released with sgx_free_epc_page(). This function implicitly calls ENCLS[EREMOVE], which will return the page to the uninitialized state (i.e. not required from caller part). Acked-by: Jethro Beekman Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/main.c | 62 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 3 ++ 2 files changed, 65 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index c5831e3db14a..97c6895fb6c9 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -83,6 +83,68 @@ static bool __init sgx_page_reclaimer_init(void) return true; } +static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(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_alloc_epc_page() - Allocate an EPC page + * + * Iterate through EPC sections and borrow a free EPC page to the caller. When a + * page is no longer needed it must be released with sgx_free_epc_page(). + * + * Return: + * an EPC page, + * -errno on error + */ +struct sgx_epc_page *__sgx_alloc_epc_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_alloc_epc_page_from_section(section); + spin_unlock(§ion->lock); + + if (page) + return page; + } + + return ERR_PTR(-ENOMEM); +} + +/** + * sgx_free_epc_page() - Free an EPC page + * @page: an EPC page + * + * Call EREMOVE for an EPC page and insert it back to the list of free pages. + */ +void sgx_free_epc_page(struct sgx_epc_page *page) +{ + struct sgx_epc_section *section = sgx_get_epc_section(page); + int ret; + + ret = __eremove(sgx_get_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 dff4f5f16d09..fce756c3434b 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -49,4 +49,7 @@ static inline void *sgx_get_epc_addr(struct sgx_epc_page *page) return section->va + (page->desc & PAGE_MASK) - section->pa; } +struct sgx_epc_page *__sgx_alloc_epc_page(void); +void sgx_free_epc_page(struct sgx_epc_page *page); + #endif /* _X86_SGX_H */ -- 2.25.1