From patchwork Fri Dec 2 18:36:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kristen Carlson Accardi X-Patchwork-Id: 29062 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1015851wrr; Fri, 2 Dec 2022 10:43:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf4DvwcbTOvOI4+Jlkr4iJOhHcDIXX456qRVqw+ZLevx0abEKRB9lDxcymmlXhJdoKQfyJ3I X-Received: by 2002:a05:6402:3485:b0:468:89dd:d326 with SMTP id v5-20020a056402348500b0046889ddd326mr48041227edc.352.1670006579929; Fri, 02 Dec 2022 10:42:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670006579; cv=none; d=google.com; s=arc-20160816; b=th2P5vcCvuEtwDE6gjAjd8kVW5v2uvq0WPTJXuqlV88FaTiQ6CBsNs7KAxCf5AGSA8 AuxSYAF9shtNMjttqUS/Fk2e9cHgz6EqWXctVH9OL6PHyspbZClEEr6evC1iVBmn3uOw 84kbctL57hD3S+U6/HOd2itYHdC2/c/7KBXMG0IZdmknToBzmKyFJVSFto53D4DZ1508 elb75ZkBYctxca9fKI7HgQXRByTEmG38ONXi9/8HcaMR9bFGt3LifZg5IhImbIlHbiCJ KRFvtDqnt35gNV9v/+5EaXZBDUFw3LZmkRjmA+nAGaXPwU1juvqdk8nXtXjkfO/9UnTR jO/A== 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=Shbdv/RzowbzXZk5Gwzb6k9CETg8vFVEujR+OagodhI=; b=FGor8yo/97cVEVqmcvwfSA2uLZni0gmYXL9PAv7mONMjaTBqPLL/2+oh6CWqXh0xm0 UNJ9HpzS4jlJamxJYr1NpBj7AX/z3TWCUjBU+6KeMPFJS0yoO5rfsl+2YeLNA+Lvww9Z zNG70aMwtDyWGW0QcqlBVyo0S3pk5GG3gqnLShpDY0tgTeTnAioqncZg8h4a2C6U4f7W KB66nxOib8/056A4eKbZNblcGBdeAGtg+TJROgMlAlj5DfL9scieVdGsSKEsZdNdcCKm Da9vGmmhfn23BSg7iRYARLAsRQ1brvGr/mBKHFdgapiH53QMG+sJFLegGxEcQHNudnF/ Wrzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ms2hcNGd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qq28-20020a17090720dc00b007a2d966eeccsi5590567ejb.686.2022.12.02.10.42.36; Fri, 02 Dec 2022 10:42:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ms2hcNGd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234537AbiLBSha (ORCPT + 99 others); Fri, 2 Dec 2022 13:37:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234506AbiLBShP (ORCPT ); Fri, 2 Dec 2022 13:37:15 -0500 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 576EBEDD4D; Fri, 2 Dec 2022 10:37:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670006233; x=1701542233; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FAwE3GKmpLc7cwMFuZTK8aRPKxUsCUSpv5pSdwsqWPQ=; b=Ms2hcNGdTwatlgqSU7ec4xBy21vbehj3JCOMEbeBxrJ/nahdq5E7zSrW tY9+QTkKj5SKmmuVsdwd64VbavZkbAzPRWm/dahmRJss/MDtYvPM+kcrN /RC38sLT0b8FFGoTwyDq60QjGaUsdbT5bwV2HZ0ijyHNTN9VdVqfT5Z5w 6mtrT8UNwS03Q43pDBjbEbyz9jasN5xD3eCpx8BNWyyTlaOSasrA7JFhI o0UWtea9hf31vc4bccwrGRTa/L/co1hqPx7KGBYRT+K52iplFMwSQZKzH lDdJ+a3dvYeTLgvcM4FKPz615baGrWqkZR4AkaF6JVaO0yFRXaU4vhtAO Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="314724529" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="314724529" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 10:37:12 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="713717399" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="713717399" Received: from kcaskeyx-mobl1.amr.corp.intel.com (HELO kcaccard-desk.amr.corp.intel.com) ([10.251.1.207]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 10:37:11 -0800 From: Kristen Carlson Accardi To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, cgroups@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: zhiquan1.li@intel.com, Kristen Carlson Accardi , Sean Christopherson Subject: [PATCH v2 04/18] x86/sgx: Use sgx_epc_lru_lists for existing active page list Date: Fri, 2 Dec 2022 10:36:40 -0800 Message-Id: <20221202183655.3767674-5-kristen@linux.intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221202183655.3767674-1-kristen@linux.intel.com> References: <20221202183655.3767674-1-kristen@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751128819678792569?= X-GMAIL-MSGID: =?utf-8?q?1751128819678792569?= Replace the existing sgx_active_page_list and its spinlock with a global sgx_epc_lru_lists struct. Signed-off-by: Sean Christopherson Signed-off-by: Kristen Carlson Accardi Cc: Sean Christopherson --- arch/x86/kernel/cpu/sgx/main.c | 39 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index ffce6fc70a1f..447cf4b8580c 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -26,10 +26,9 @@ static DEFINE_XARRAY(sgx_epc_address_space); /* * These variables are part of the state of the reclaimer, and must be accessed - * with sgx_reclaimer_lock acquired. + * with sgx_global_lru.lock acquired. */ -static LIST_HEAD(sgx_active_page_list); -static DEFINE_SPINLOCK(sgx_reclaimer_lock); +static struct sgx_epc_lru_lists sgx_global_lru; static atomic_long_t sgx_nr_free_pages = ATOMIC_LONG_INIT(0); @@ -298,14 +297,12 @@ static void __sgx_reclaim_pages(void) int ret; int i; - spin_lock(&sgx_reclaimer_lock); + spin_lock(&sgx_global_lru.lock); for (i = 0; i < SGX_NR_TO_SCAN; i++) { - if (list_empty(&sgx_active_page_list)) + epc_page = sgx_epc_pop_reclaimable(&sgx_global_lru); + if (!epc_page) break; - epc_page = list_first_entry(&sgx_active_page_list, - struct sgx_epc_page, list); - list_del_init(&epc_page->list); encl_page = epc_page->encl_owner; if (kref_get_unless_zero(&encl_page->encl->refcount) != 0) @@ -316,7 +313,7 @@ static void __sgx_reclaim_pages(void) */ epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED; } - spin_unlock(&sgx_reclaimer_lock); + spin_unlock(&sgx_global_lru.lock); for (i = 0; i < cnt; i++) { epc_page = chunk[i]; @@ -339,9 +336,9 @@ static void __sgx_reclaim_pages(void) continue; skip: - spin_lock(&sgx_reclaimer_lock); - list_add_tail(&epc_page->list, &sgx_active_page_list); - spin_unlock(&sgx_reclaimer_lock); + spin_lock(&sgx_global_lru.lock); + sgx_epc_push_reclaimable(&sgx_global_lru, epc_page); + spin_unlock(&sgx_global_lru.lock); kref_put(&encl_page->encl->refcount, sgx_encl_release); @@ -378,7 +375,7 @@ static void sgx_reclaim_pages(void) static bool sgx_should_reclaim(unsigned long watermark) { return atomic_long_read(&sgx_nr_free_pages) < watermark && - !list_empty(&sgx_active_page_list); + !list_empty(&sgx_global_lru.reclaimable); } /* @@ -433,6 +430,8 @@ static bool __init sgx_page_reclaimer_init(void) ksgxd_tsk = tsk; + sgx_lru_init(&sgx_global_lru); + return true; } @@ -508,10 +507,10 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void) */ void sgx_mark_page_reclaimable(struct sgx_epc_page *page) { - spin_lock(&sgx_reclaimer_lock); + spin_lock(&sgx_global_lru.lock); page->flags |= SGX_EPC_PAGE_RECLAIMER_TRACKED; - list_add_tail(&page->list, &sgx_active_page_list); - spin_unlock(&sgx_reclaimer_lock); + sgx_epc_push_reclaimable(&sgx_global_lru, page); + spin_unlock(&sgx_global_lru.lock); } /** @@ -526,18 +525,18 @@ void sgx_mark_page_reclaimable(struct sgx_epc_page *page) */ int sgx_unmark_page_reclaimable(struct sgx_epc_page *page) { - spin_lock(&sgx_reclaimer_lock); + spin_lock(&sgx_global_lru.lock); if (page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) { /* The page is being reclaimed. */ if (list_empty(&page->list)) { - spin_unlock(&sgx_reclaimer_lock); + spin_unlock(&sgx_global_lru.lock); return -EBUSY; } list_del(&page->list); page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED; } - spin_unlock(&sgx_reclaimer_lock); + spin_unlock(&sgx_global_lru.lock); return 0; } @@ -574,7 +573,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim) break; } - if (list_empty(&sgx_active_page_list)) + if (list_empty(&sgx_global_lru.reclaimable)) return ERR_PTR(-ENOMEM); if (!reclaim) {