Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2338423pxk; Mon, 14 Sep 2020 10:35:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+RLBJeQAElsXZSGhF4eMkS7jJgbVdsZw7J8zSEtlFp1GUALYKgLto5K2rpGsApuAveroN X-Received: by 2002:a17:906:24d6:: with SMTP id f22mr15424422ejb.85.1600104955291; Mon, 14 Sep 2020 10:35:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600104955; cv=none; d=google.com; s=arc-20160816; b=f4anVCZCWT7LF2Ucwb5bQQXlY7H5RYUTl3AWuPy/OSSDYOAHaZawqOtJqj2N52Le1A kiv7KSujB2Y3wgD/NFGjWSJN0DiUzODykSFp8G3zEWyOBaQqMzFlwaSdl9izqLJti6Mf Y9+qGjVymFWMzp31u8ymVLzvU2FyWCrvS68PFpNkvUFqjEehPLYCsNczLVA2SzzbChPT ft1A7HhAPd2pWKf3DGkf40S067W27BIUvZMRIvybuMf3tEqLDLF4bBx+IgljG9mcpLZT c8s0W1qmTIYEfnouY6eSAoTuubqz4q8JSBDHshuUg2U14YfariGfZHOhcfO3faaVlLIn QaGg== 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 :message-id:date:subject:cc:to:from:ironport-sdr:ironport-sdr; bh=VQfxU9etoAX9NoRDjTZJvNO+0WR8I6R02xxEqCNX2jQ=; b=EknHb7LnaBoEX6GllvliTraWztv1L/W27c8ADFt+0cpgxF1t8lPfPJ8FALevyOheen bfwYQw8cyxeYiin5DymyhVAtWHeTLHm0kiwKrx7LEfivGCsj4p/obFqymIKBGe2sOyb4 b3qyyk1NbZL/j94ms3zoku9eKYkt3LnQGqVo790bGhiz2YwNKeZLkAiLu+oX3lsV+Wt/ +xDLSl6ly/aJzX4C8RnVqo5Hq0BlPiTHohTRdTfE/0CwKQoRJHfiCgAHeQtwgKS5pI/g DlhkFAOAp8/xKcrgteXH3qEuqrnH3okYv/pDkuC6higZX6yi74POlG9e+Xt8PuEYWCum IJJA== 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 u6si6848345edx.293.2020.09.14.10.35.32; Mon, 14 Sep 2020 10:35:55 -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 S1726137AbgINRfD (ORCPT + 99 others); Mon, 14 Sep 2020 13:35:03 -0400 Received: from mga09.intel.com ([134.134.136.24]:60769 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726039AbgINR0l (ORCPT ); Mon, 14 Sep 2020 13:26:41 -0400 IronPort-SDR: mEJgPqSdaZHrDn9e+UsUq0JURUmEH00ZPPYibaKhAiEkQmjn7kvtlOiRFC6iwxgpd+MnFfwM9n gsj8/29Ou5jA== X-IronPort-AV: E=McAfee;i="6000,8403,9744"; a="160055266" X-IronPort-AV: E=Sophos;i="5.76,426,1592895600"; d="scan'208";a="160055266" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2020 10:26:40 -0700 IronPort-SDR: EXUArbWF6MqzxQVzC9rRVE2IA6NQYNT/XVBpbxMnmAssoFi6iFw2p5l2uddSLDCmYby6YzuDUo k6epqzo8iOFw== X-IronPort-AV: E=Sophos;i="5.76,426,1592895600"; d="scan'208";a="482431915" Received: from mgorski-mobl.ger.corp.intel.com (HELO localhost) ([10.249.43.120]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2020 10:26:33 -0700 From: Jarkko Sakkinen To: x86@kernel.org, linux-sgx@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jarkko Sakkinen , Jethro Beekman , Darren Kenny , 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 v37 09/24] x86/sgx: Add __sgx_alloc_epc_page() and sgx_free_epc_page() Date: Mon, 14 Sep 2020 20:26:27 +0300 Message-Id: <20200914172627.7693-1-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 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 Reviewed-by: Darren Kenny 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