Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp104224ybg; Sun, 31 May 2020 18:21:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbF8X5mVMikiOznYKxf4v6I6vdYpVVC9Tm7DL+lnx9et7C1KwAHlwI66g0bljsoVsT2Zf8 X-Received: by 2002:a05:6402:1812:: with SMTP id g18mr387269edy.96.1590974503686; Sun, 31 May 2020 18:21:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590974503; cv=none; d=google.com; s=arc-20160816; b=jrHylWU+CYd4xaxLSSVXWP6yVpd2U16cNVcGwUx6ADa9RlNtk86HoOeh4RiWYgPkof P1y/xXQBHkCBvlMLsMTBuvZ0i1f8s/KZFonvPadayjI35++jvpeeWb2OPmMAh1Undi95 K7hexGS6taE2PZ9U//rqV1Asazo/dIngoag2J+87VQLX9strPlsnhuzEV97zS23Jj4aI ZA4MXHseEJV6e4jPWoFYdjLRsajfHE00YheSCecYjvvzlVV2u9DgAq364+GD6Um7hdVN pLNUoeQLY3QUJsxgqBy/RDlbQ5SUbdi9PnvDZTwuuETvIaob8LEVHm8z1az3dfS858eY Y/hw== 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=f0a8CHb4aokoED9PLNfWqZ/kXfFELIyW/J8TBAd5Mfw=; b=g74z4WwGLy/r4b76iB5qZ0JiJu03vNmAuZ8FjGzeHgEgdEY0CIso+tMVxnu5hfHJn1 YQyph38I88o8lKz04Mih8nU+cGDDC+AOZbxr9WRajwaTTmxj/Kz4EAhXxTZiFSTTWOkE +8uE5bvC4d7wkgOjMs6DrNb979R/KGncfrElwjqlAMOVpSqdn5tfnAPK4uRAsH91kWLR 3rRknhLUUVL6OrewmHd8d6kInjvMGtUnOBqfxDuaIqE1lrO7Sm3YHPQYTZ8Lys7yPwLm ZDgtdGEKk9ufGBBV6iQCgB2JfOyUTclgUbTd45k2sTVOcfRapQ8TG7q4z0nedDuxJvSi 0EBQ== 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 k24si9946950eds.422.2020.05.31.18.21.21; Sun, 31 May 2020 18:21:43 -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 S1728125AbgFABSu (ORCPT + 99 others); Sun, 31 May 2020 21:18:50 -0400 Received: from mga18.intel.com ([134.134.136.126]:63119 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726860AbgFABSt (ORCPT ); Sun, 31 May 2020 21:18:49 -0400 IronPort-SDR: P42l34IoKb1L4sgQqLStLGzJlZXibXnMga52VjJMjR/UDcGmgIddws18gklOZrclB30D+KOWa6 eCpPQjZf/pMw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2020 18:18:49 -0700 IronPort-SDR: 18KRKNZf5O37yRROewJ56XqmbArw2jJNguq9FHLKMpifWUskQIQKrJ7ZV3AepZ6tw45ce2k6Gk YafusxSsAk/w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,459,1583222400"; d="scan'208";a="268157093" Received: from hbetts-mobl.ger.corp.intel.com (HELO localhost) ([10.252.59.178]) by orsmga003.jf.intel.com with ESMTP; 31 May 2020 18:18:39 -0700 From: Jarkko Sakkinen To: linux-kernel@vger.kernel.org, x86@kernel.org, linux-sgx@vger.kernel.org Cc: 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 v31 09/21] x86/sgx: Add __sgx_alloc_epc_page() and sgx_free_epc_page() Date: Mon, 1 Jun 2020 04:16:33 +0300 Message-Id: <20200601011645.794040-10-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200601011645.794040-1-jarkko.sakkinen@linux.intel.com> References: <20200601011645.794040-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 | 61 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 3 ++ 2 files changed, 64 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index c5831e3db14a..b776d249289f 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -83,6 +83,67 @@ 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 6508bfefd6d9..aae6bfb3bc8a 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -50,4 +50,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