Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp4064318pxb; Tue, 17 Nov 2020 10:16:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJy+13AbGKzAXWpaCR+Uqg+wDBnUvuZy00T9Odw9SDTrcsAST9MSnhrR+rwMo5TInsVZpc0C X-Received: by 2002:a17:906:c186:: with SMTP id g6mr19583184ejz.465.1605637016478; Tue, 17 Nov 2020 10:16:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605637016; cv=none; d=google.com; s=arc-20160816; b=tV4AayiEUJRSyH4c8Y4s494P/gsA38qCCFVWqiJQLYwON4cAfGWPiD6Wa2qdqoVmHn IHCmJ8SWmjBxVs+/qWk1cxA5TX/59pCI0BnZZRz1PMckz/8wMbVbqqJYTfRshoKZDddp qYzcaIl6zTbg0DnVd3BTHJBCcMyDIE3iVceQAxNOh2ewCY7BZspOzQC9VWUj3GWEEEAY nSPecnM4l5rwdKFz3/8vaeCVs26arhjp4Z0y4ebN8AshkDv05/tYKJRsXz8PJbS6sbSr SQRloXsh8P5dCxdYMsp/lOlGHFetp0vkSLqTscq/ksxh41rHRFk01B7vWKTa4rLNTd97 gWfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ZpJJINXhNGkuJcefi2AWkQKza6T0tTQbS/E52PfN7ew=; b=WslcnUvlpgo4j0GaPcpjqgKSzdsP8PxksA4Ky44curccwTO6zmD2QYkoTEhb66/5U8 U0ZlfnYNuqNYYftlNmCJ/Ynl4b+WOVi1Vz1hW2+XUw8R3RBxp8/AQrhk4uPSQ1hM1BYm vgsOWWtIzZHyS3VVP9nCUlUOmW5RloDDDURXwrsunhaF2QxjjvGEuX1iTc8bo5ma5qTp r9fRbc9Fz91W0/XUZscxKMhqi5ThoKqvhRr4Sx2Yz22M3WxSB7jHEwYbqEjWyvnyG46R T9qiclU8yoA3FyyEVqrM/0fjjyrRorlS45LylsgSe6wgeGZX2SsLuNCCxaY4X/8RVqN+ VHGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MqKVL5Z3; 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 u4si11946647eja.497.2020.11.17.10.16.32; Tue, 17 Nov 2020 10:16:56 -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=MqKVL5Z3; 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 S1730319AbgKQSOs (ORCPT + 99 others); Tue, 17 Nov 2020 13:14:48 -0500 Received: from mail.kernel.org ([198.145.29.99]:54662 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726731AbgKQSOr (ORCPT ); Tue, 17 Nov 2020 13:14:47 -0500 Received: from kernel.org (83-245-197-237.elisa-laajakaista.fi [83.245.197.237]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CCDF42462E; Tue, 17 Nov 2020 18:14:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605636886; bh=Zo7ZhZu4hr8dzjmIJAUlUJ3adtcEXpkIdPJcQMt35fo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MqKVL5Z3z6XJugchnRsrPdGtTqFSAwKErinRMrpKjlsllktUog87TeKw/xbg9Ghgo fTYz7EN8IV6XNHNq22DIIBuEVjnQVYo+dWZ35LcmQEyPHO+RJ15g5FgBIEOLvKYvW4 iqr3beMOKSzENA7F0tgAWvxN2thwEGjXQfV3QB9k= Date: Tue, 17 Nov 2020 20:14:41 +0200 From: Jarkko Sakkinen To: Hillf Danton Cc: x86@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Christopherson , Jethro Beekman , Serge Ayoun , akpm@linux-foundation.org Subject: Re: [PATCH v41 09/24] x86/sgx: Add SGX page allocator functions Message-ID: <20201117181426.GF8524@kernel.org> References: <20201112220135.165028-1-jarkko@kernel.org> <20201114093256.7800-1-hdanton@sina.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201114093256.7800-1-hdanton@sina.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Nov 14, 2020 at 05:32:56PM +0800, Hillf Danton wrote: > On Fri, 13 Nov 2020 00:01:20 +0200 Jarkko Sakkinen wrote: > > > > 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); > > Add head in favor of allocators coming before LLC becomes cold. Nice suggestion, thank you. /Jarkko