Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp695893pxb; Thu, 12 Nov 2020 14:05:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJwb+TI32fKt+ut+rbU7HbVRKSlnRjpec9fOoHcHZhEoQS9N48NzIYe7kuMy9lKZ5RrFDwml X-Received: by 2002:aa7:dc09:: with SMTP id b9mr2165119edu.285.1605218707552; Thu, 12 Nov 2020 14:05:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605218707; cv=none; d=google.com; s=arc-20160816; b=0rSbO3ItQDS/qmGERIvn1rHiwUO3GO2C8MnXkc/ZKTxtcBqVqIq1kfib2cNfegjcrA oFU9Sj3eugpnQZSzTvvXUdJt5C8z6grNsqc84cYUL7Ie+RZ40PWWkqUsbHyoS1V6gPYX vjoG97PNnDzlBfUBva9Zt2FXwtufU6sG7tBufvBMEXZNSljQQQ3lyNNECj5kMnZKkBVf HdSoK/ziqbywADEr6rkHGNUVwNG7xKfsFjxiQvWZmglNnh8UDVN5C2AjHbNvJ+1Z+x/y kBHm7CbWUGIMDiPvbnfBrCzhc6TWp06Dqdph7Ikb73oO2PrsRFJlH4RMjl8QfeA9kOKi 26yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=D9lF/6um6V/qjrHgo4TYaqluXNEBSSwirPnlZK2wtys=; b=ChMuCImBuHqD7nyIdVGDoos+/tLnuelZn+lzhypUGA2uMt50SCiqQRQH4qT4KUVnip OgVbLwrOQD2RkOsw3awqrbyNI+2JWha2AuAN7yVreJfpuvnhPZ5MmoYvHkFIUHheJVYr FC0Ux/VaLMtLy9y4Xwo3h2Cnt0qOjcjXMZYoHPSPTczHAf5kstWTY/PCg7Li38lQAAoB ONyQdjpQp/Ub+py4kARDiVQK7RdejEZNxqWG11KsbYsulYNh+ipr40by/gGWl+NbUzNy vrT6/8ow4+3tbMXoG4zuodKRW/W/uqUJDUQPUXtMUeJB0PJF9pRhcMNOxo87aXVHqKvi x7dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ityVixk+; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jz19si5541571ejb.238.2020.11.12.14.04.45; Thu, 12 Nov 2020 14:05:07 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=ityVixk+; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727647AbgKLWCs (ORCPT + 99 others); Thu, 12 Nov 2020 17:02:48 -0500 Received: from mail.kernel.org ([198.145.29.99]:50610 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727633AbgKLWCq (ORCPT ); Thu, 12 Nov 2020 17:02:46 -0500 Received: from suppilovahvero.lan (83-245-197-237.elisa-laajakaista.fi [83.245.197.237]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D813A22246; Thu, 12 Nov 2020 22:02:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605218565; bh=C3z26/BwZTBylkxi3CkpNoxIGz4D3IOIGLA5Db/CwfM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ityVixk+RnaXvYGALjt+RY+rXY6uXSV3Z0yoLltodktLCRmUovibpR7gwdvVzxwy1 qzjXxPhQH3Gnv3zJV1fU6dRt6SYPo+6pednEwsjRMYAXz3EtJohHrF/EmCWYyfsZ2T AyKeIs1nlrbXq7tlmgxynL+TbR8KqChpxOJAbAys= From: Jarkko Sakkinen To: x86@kernel.org, linux-sgx@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jarkko Sakkinen , Jethro Beekman , 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, 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, sean.j.christopherson@intel.com, tglx@linutronix.de, yaozhangx@google.com, mikko.ylinen@intel.com Subject: [PATCH v41 09/24] x86/sgx: Add SGX page allocator functions Date: Fri, 13 Nov 2020 00:01:20 +0200 Message-Id: <20201112220135.165028-10-jarkko@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201112220135.165028-1-jarkko@kernel.org> References: <20201112220135.165028-1-jarkko@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The previous patch initialized a simple SGX page allocator. Add functions for runtime allocation and free. This allocator and its algorithms are as simple as it gets. They do a linear search across all EPC sections and find the first free page. They are not NUMA aware and only hand out individual pages. The SGX hardware does not support large pages, so something more complicated like a buddy allocator is unwarranted. The free function (sgx_free_epc_page()) implicitly calls ENCLS[EREMOVE], which returns the page to the uninitialized state. This ensures that the page is ready for use at the next allocation. Acked-by: Jethro Beekman # v40 # Co-developed-by: Sean Christopherson # Signed-off-by: Sean Christopherson Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/main.c | 65 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 3 ++ 2 files changed, 68 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 187a237eec38..2e53afc288a4 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -85,6 +85,71 @@ 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; + + spin_lock(§ion->lock); + + if (list_empty(§ion->page_list)) { + spin_unlock(§ion->lock); + return NULL; + } + + page = list_first_entry(§ion->page_list, struct sgx_epc_page, list); + list_del_init(&page->list); + + spin_unlock(§ion->lock); + 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]; + + page = __sgx_alloc_epc_page_from_section(section); + 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_epc_sections[page->section]; + int ret; + + ret = __eremove(sgx_get_epc_virt_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 bool __init sgx_setup_epc_section(u64 phys_addr, u64 size, unsigned long index, struct sgx_epc_section *section) diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index 02afa84dd8fd..bd9dcb1ffcfa 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -57,4 +57,7 @@ static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page) return section->virt_addr + index * PAGE_SIZE; } +struct sgx_epc_page *__sgx_alloc_epc_page(void); +void sgx_free_epc_page(struct sgx_epc_page *page); + #endif /* _X86_SGX_H */ -- 2.27.0