From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E03CC4332B for ; Mon, 15 Mar 2021 16:04:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 23DA064F06 for ; Mon, 15 Mar 2021 16:04:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233968AbhCOQDy (ORCPT ); Mon, 15 Mar 2021 12:03:54 -0400 Received: from mga18.intel.com ([134.134.136.126]:37616 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231995AbhCOQDX (ORCPT ); Mon, 15 Mar 2021 12:03:23 -0400 IronPort-SDR: KgqixJFJGa/vFk24lsNIh+S6pI+yqy6Y8UeTJKHolwHqsjGTJ9Fr3vTsudri9Go1XLjvh8ar0j II6iRWL4zXtA== X-IronPort-AV: E=McAfee;i="6000,8403,9924"; a="176707007" X-IronPort-AV: E=Sophos;i="5.81,249,1610438400"; d="scan'208";a="176707007" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Mar 2021 09:03:22 -0700 IronPort-SDR: iFfO3e2NKZ7Zv28ata6VSNp3ghnX8s8Ad5CZDnPnA9JowPFueihUZ6yMqjOS7uBTBcfVrFbXdh BXyJNCgJpOow== X-IronPort-AV: E=Sophos;i="5.81,249,1610438400"; d="scan'208";a="601474008" Received: from lguadamu-mobl1.amr.corp.intel.com (HELO [10.213.176.188]) ([10.213.176.188]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Mar 2021 09:03:21 -0700 Subject: Re: [PATCH v4 2/3] x86/sgx: Replace section local dirty page lists with a global list To: Jarkko Sakkinen , linux-sgx@vger.kernel.org Cc: Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org References: <20210313160119.1318533-1-jarkko@kernel.org> <20210313160119.1318533-3-jarkko@kernel.org> From: Dave Hansen Autocrypt: addr=dave.hansen@intel.com; keydata= xsFNBE6HMP0BEADIMA3XYkQfF3dwHlj58Yjsc4E5y5G67cfbt8dvaUq2fx1lR0K9h1bOI6fC oAiUXvGAOxPDsB/P6UEOISPpLl5IuYsSwAeZGkdQ5g6m1xq7AlDJQZddhr/1DC/nMVa/2BoY 2UnKuZuSBu7lgOE193+7Uks3416N2hTkyKUSNkduyoZ9F5twiBhxPJwPtn/wnch6n5RsoXsb ygOEDxLEsSk/7eyFycjE+btUtAWZtx+HseyaGfqkZK0Z9bT1lsaHecmB203xShwCPT49Blxz VOab8668QpaEOdLGhtvrVYVK7x4skyT3nGWcgDCl5/Vp3TWA4K+IofwvXzX2ON/Mj7aQwf5W iC+3nWC7q0uxKwwsddJ0Nu+dpA/UORQWa1NiAftEoSpk5+nUUi0WE+5DRm0H+TXKBWMGNCFn c6+EKg5zQaa8KqymHcOrSXNPmzJuXvDQ8uj2J8XuzCZfK4uy1+YdIr0yyEMI7mdh4KX50LO1 pmowEqDh7dLShTOif/7UtQYrzYq9cPnjU2ZW4qd5Qz2joSGTG9eCXLz5PRe5SqHxv6ljk8mb ApNuY7bOXO/A7T2j5RwXIlcmssqIjBcxsRRoIbpCwWWGjkYjzYCjgsNFL6rt4OL11OUF37wL QcTl7fbCGv53KfKPdYD5hcbgu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Q5GXH0JQ/TZXWygyRFyyI2FqNTx4WHqUn3yFj8rwTAU1tluRUYyeLy0ayUlKBH ybj0N71vWO936MqP6haFERzuPAIpxj2ezwu0xb1GjTk4ynna6h5GjnKgdfOWoRtoWndMZxbA z5cecg== Message-ID: Date: Mon, 15 Mar 2021 09:03:21 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20210313160119.1318533-3-jarkko@kernel.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/13/21 8:01 AM, Jarkko Sakkinen wrote: > Reset initialized EPC pages in sgx_dirty_page_list to uninitialized state, > and free them using sgx_free_epc_page(). Do two passes, as for SECS pages > the first round can fail, if all child pages have not yet been removed. > The driver puts all pages on startup first to sgx_dirty_page_list, as the > initialization could be triggered by kexec(), meaning that pages have been > reserved for active enclaves before the operation. > > The section local lists are redundant, as sgx_free_epc_page() figures > out the correction by using epc_page->section. During normal runtime, the "ksgxd" daemon behaves like a version of kswapd just for SGX. But, its first job is to initialize enclave memory. This is done in a a separate thread because this initialization can be quite slow. Currently, the SGX boot code places each enclave page on a sgx_section-local list (init_laundry_list). Once it starts up, the ksgxd code walks over that list and populates the actual SGX page allocator. However, the per-section structures are going away to make way for the SGX NUMA allocator. There's also little need to have a per-section structure; the enclave pages are all treated identically, and they can be placed on the correct allocator list from metadata stoered in the enclave page itself. > diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c > index 65004fb8a91f..cb4561444b96 100644 > --- a/arch/x86/kernel/cpu/sgx/main.c > +++ b/arch/x86/kernel/cpu/sgx/main.c > @@ -27,39 +27,10 @@ static LIST_HEAD(sgx_active_page_list); > static DEFINE_SPINLOCK(sgx_reclaimer_lock); > > /* > - * Reset dirty EPC pages to uninitialized state. Laundry can be left with SECS > - * pages whose child pages blocked EREMOVE. > + * When the driver initialized, EPC pages go first here, as they could be > + * initialized to an active enclave, on kexec entry. > */ > -static void sgx_sanitize_section(struct sgx_epc_section *section) > -{ > - struct sgx_epc_page *page; > - LIST_HEAD(dirty); > - int ret; > - > - /* init_laundry_list is thread-local, no need for a lock: */ > - while (!list_empty(§ion->init_laundry_list)) { > - if (kthread_should_stop()) > - return; > - > - /* needed for access to ->page_list: */ > - spin_lock(§ion->lock); > - > - page = list_first_entry(§ion->init_laundry_list, > - struct sgx_epc_page, list); > - > - ret = __eremove(sgx_get_epc_virt_addr(page)); > - if (!ret) > - list_move(&page->list, §ion->page_list); > - else > - list_move_tail(&page->list, &dirty); > - > - spin_unlock(§ion->lock); > - > - cond_resched(); > - } > - > - list_splice(&dirty, §ion->init_laundry_list); > -} > +static LIST_HEAD(sgx_dirty_page_list); > > static bool sgx_reclaimer_age(struct sgx_epc_page *epc_page) > { > @@ -400,25 +371,48 @@ static bool sgx_should_reclaim(unsigned long watermark) > > static int ksgxd(void *p) > { > - int i; > + struct sgx_epc_page *page; > + LIST_HEAD(dirty); > + int i, ret; > > set_freezable(); > > /* > - * Sanitize pages in order to recover from kexec(). The 2nd pass is > - * required for SECS pages, whose child pages blocked EREMOVE. > + * Reset initialized EPC pages in sgx_dirty_page_list to uninitialized state, > + * and free them using sgx_free_epc_page(). I'm not a fan of comments that tell us verbatim what the code does. > Do two passes, as for SECS pages the > + * first round can fail, if all child pages have not yet been removed. The > + * driver puts all pages on startup first to sgx_dirty_page_list, as the > + * initialization could be triggered by kexec(), meaning that pages have been > + * reserved for active enclaves before the operation. > */ > - for (i = 0; i < sgx_nr_epc_sections; i++) > - sgx_sanitize_section(&sgx_epc_sections[i]); > > - for (i = 0; i < sgx_nr_epc_sections; i++) { > - sgx_sanitize_section(&sgx_epc_sections[i]); FWIW, I don't like the removal of the helper here. I really like kernel threads' top-level function to be very understandable and clean. This makes it quite a bit harder to figure out what is going on. For instance, we could just have a sgx_sanitize_pages() which has a local dirty list and just calls: void sgx_santitize_pages(void) { LIST_HEAD(dirty); /* * Comment about two passes */ __sgx_sanitize_pages(&dirty) __sgx_sanitize_pages(&dirty) } > + /* sgx_dirty_page_list is thread-local to ksgxd, no need for a lock: */ > + for (i = 0; i < 2 && !list_empty(&sgx_dirty_page_list); i++) { > + while (!list_empty(&sgx_dirty_page_list)) { > + if (kthread_should_stop()) > + return 0; > + > + page = list_first_entry(&sgx_dirty_page_list, struct sgx_epc_page, list); > + > + ret = __eremove(sgx_get_epc_virt_addr(page)); > + if (!ret) { > + /* The page is clean - move to the free list. */ I'd even say: /* * page is now sanitized. Make it * available via the SGX page allocator: */ See what that does? It actually links the "cleaning" to the freeing. > + list_del(&page->list); > + sgx_free_epc_page(page); > + } else { > + /* The page is not yet clean - move to the dirty list. */ > + list_move_tail(&page->list, &dirty); > + } > + > + cond_resched(); > + } > > - /* Should never happen. */ > - if (!list_empty(&sgx_epc_sections[i].init_laundry_list)) > - WARN(1, "EPC section %d has unsanitized pages.\n", i); > + list_splice(&dirty, &sgx_dirty_page_list); > } > > + if (!list_empty(&sgx_dirty_page_list)) > + WARN(1, "EPC section %d has unsanitized pages.\n", i); > + > while (!kthread_should_stop()) { > if (try_to_freeze()) > continue; > @@ -632,13 +626,12 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size, > section->phys_addr = phys_addr; > spin_lock_init(§ion->lock); > INIT_LIST_HEAD(§ion->page_list); > - INIT_LIST_HEAD(§ion->init_laundry_list); > > for (i = 0; i < nr_pages; i++) { > section->pages[i].section = index; > section->pages[i].flags = 0; > section->pages[i].owner = NULL; > - list_add_tail(§ion->pages[i].list, §ion->init_laundry_list); > + list_add_tail(§ion->pages[i].list, &sgx_dirty_page_list); > } ...