From patchwork Mon Oct 30 18:20:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haitao Huang X-Patchwork-Id: 159840 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp2415752vqb; Mon, 30 Oct 2023 11:21:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEgM5HGroGACJbzyjKJF6zrEUeoB1Oywz1mm4Z5e04SaH4N94UO1Cc1WgQQf3o5oz9u/v9Y X-Received: by 2002:a05:6a00:1348:b0:693:3c11:4293 with SMTP id k8-20020a056a00134800b006933c114293mr9488744pfu.14.1698690119151; Mon, 30 Oct 2023 11:21:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698690119; cv=none; d=google.com; s=arc-20160816; b=B6aOVdZtqGKMD84u58IxYL6DzZOmfksiZJh3FsFPKtuboEnK6XvRBJ8za4gPP8/zWB UWY+xKGA8zS28ZCcQaAuIlFEoQ4qIwBNWLEmgp+le8SyDXZyJdJ9LkhQ4LRNFax60d21 wsd6ymrZ59L/mjmCnhj7CwkpOc0HE5N+jefRgGsn1QdCyRQbVapHEFsBW8kEh1olAuzp r5Z+VPjdPPr/8vYxcTknrxbj5/Aqw6sQYx0rOIIW+XPWL4EU4yP8+s0tFLWbke4Bmp5r gufoqmXU3oLl7fqvD92iybuvmkJJ1pfPSTzwh29l6djqjHyOOj6BSjT8LlsgRAA3gj5S FApw== 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=sbGYrmfIoiAmWVQwv3OJD08Ob0eCcSzxZVCVsh5ykTg=; fh=CTa835q0iyfQFiV5yjYPzrfvG/ulw1To85Pi/STRmhA=; b=uNQ7HZusZr2NFeFLRH7cBCNtByHn9PTisz13ph+D1IpobXF5kIsG8+JlEEeiDy2iAd iU7k2eaaIrr4prgezgGvdvyvzHkHzwNkOZU6I73vZKpK2YyzpZ8E+fZFFztke6RNLT+c sWLwiq33au0ViNkOttqJPNXAlYDSdONGv7RGO2BtrZoegAf95rjI2gBeOrpAUD+Ti8Rn np12Nz2xcJRUW4q39ow2Mjqu8xGwQlqTmytJgEqdG2nVvermtvoLkeEPW4Occ2M05rOu F09jFvTXR1Jt1oCD0gKof4dMfBi46Bafn6G61RSZSSmmaisX7PCb0ivAteFHzcphS5+L AOPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=APdDxlNd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id k18-20020a056a00135200b006be062e4ecbsi5189228pfu.32.2023.10.30.11.21.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 11:21:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=APdDxlNd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 605EB805B203; Mon, 30 Oct 2023 11:21:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233070AbjJ3SUz (ORCPT + 32 others); Mon, 30 Oct 2023 14:20:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232146AbjJ3SUh (ORCPT ); Mon, 30 Oct 2023 14:20:37 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C0CFF5; Mon, 30 Oct 2023 11:20:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698690034; x=1730226034; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XIxXbhwXmB5hOPvlgZIYcW3lql519F0tLhTnN6zai0s=; b=APdDxlNdZB1f7M5kGUsK2yrTPYYLB2wfafYkYj9yTa8h7/gWN77H71N/ gOsJgqZzytZLvTZP4qFJAEzYTogj9b9fzd55rOREzguqmBtsZiaY1M2ZV 1QJO6+dBu8tnWaqDKRX29sDfM968iQPWwQpJJU8EUNmgBpXWKEbs9Ac7s O9bC18PFnuZrS5uMNF2Is/k3WF9ZAb0fX1AqAug2utUXJUugD7mwNrmXy 889ZhmxsCCrDT7mbLKO/4Bl9IwheHgiqkEB19ThyAOSQB+Uh7yajaArNi lNgc46hkPUbePUht/wpQRO3nq8Rje5ohyu2hjNCtzszvX1pmUsxNKUkMi w==; X-IronPort-AV: E=McAfee;i="6600,9927,10879"; a="367479572" X-IronPort-AV: E=Sophos;i="6.03,263,1694761200"; d="scan'208";a="367479572" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2023 11:20:29 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10879"; a="789529517" X-IronPort-AV: E=Sophos;i="6.03,263,1694761200"; d="scan'208";a="789529517" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orsmga008.jf.intel.com with ESMTP; 30 Oct 2023 11:20:29 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, Sean Christopherson , Haitao Huang Subject: [PATCH v6 06/12] x86/sgx: Use sgx_epc_lru_list for existing active page list Date: Mon, 30 Oct 2023 11:20:07 -0700 Message-Id: <20231030182013.40086-7-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231030182013.40086-1-haitao.huang@linux.intel.com> References: <20231030182013.40086-1-haitao.huang@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 30 Oct 2023 11:21:43 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781205690210468932 X-GMAIL-MSGID: 1781205690210468932 From: Sean Christopherson In future each cgroup needs a LRU list to track reclaimable pages. For now just replace the existing sgx_active_page_list in the reclaimer and its spinlock with a global sgx_epc_lru_list struct. Signed-off-by: Sean Christopherson Co-developed-by: Kristen Carlson Accardi Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Cc: Sean Christopherson --- V5: - Spelled out SECS, VA (Jarkko) V4: - No change, only reordered the patch. V3: - Remove usage of list wrapper --- arch/x86/kernel/cpu/sgx/main.c | 39 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 07606f391540..d347acd717fd 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -28,10 +28,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_list sgx_global_lru; static atomic_long_t sgx_nr_free_pages = ATOMIC_LONG_INIT(0); @@ -306,13 +305,13 @@ 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 = list_first_entry_or_null(&sgx_global_lru.reclaimable, + struct sgx_epc_page, list); + 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->owner; @@ -324,7 +323,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]; @@ -347,9 +346,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); + list_add_tail(&epc_page->list, &sgx_global_lru.reclaimable); + spin_unlock(&sgx_global_lru.lock); kref_put(&encl_page->encl->refcount, sgx_encl_release); @@ -380,7 +379,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); } /* @@ -432,6 +431,8 @@ static bool __init sgx_page_reclaimer_init(void) ksgxd_tsk = tsk; + sgx_lru_init(&sgx_global_lru); + return true; } @@ -507,10 +508,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); + list_add_tail(&page->list, &sgx_global_lru.reclaimable); + spin_unlock(&sgx_global_lru.lock); } /** @@ -525,18 +526,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 +575,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) {