Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp143786lqh; Wed, 27 Mar 2024 18:29:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUrzVAxC/xmM7YT2DwCIeBSh3gOVeRC8RABVwvzgjB1NXD/hyq/vmeTM/MLahEQR5CnpfpK2GVq4KE2qx8mtp4ZhvbeRGGQNs0R4deGUw== X-Google-Smtp-Source: AGHT+IF1G3dlWRaBQ79a1SxHuJrMjZyGe3lEBPOpoJ9L2nh9D6XSz5VmfhVyQkZX7Ui1hkys+xJI X-Received: by 2002:a05:6358:339f:b0:17e:69a1:a797 with SMTP id i31-20020a056358339f00b0017e69a1a797mr1066036rwd.30.1711589349102; Wed, 27 Mar 2024 18:29:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711589349; cv=pass; d=google.com; s=arc-20160816; b=YbB1EtHRyEMDy8GNkKu6NCELz6ChZtZkadaCMognQTRTxAte3TQJf7AbRlHdDIPpr3 GqV6pFTTon5xEITyW7AxPCdA23DJiD1Xa5XvcldwYNNlfK6kVCXFbjq3LXv0X19V0BhD +CvN8j891hwacokSScAUNZi5XAqamcSK2sMDXR7Ps7mD/yJer4AQ5qEoag5yaXAo8wIW Y58tkpCAkWqmcJC/mspCcMVwcEtAFhTgDlB8U9ULfY4xkt3aLoUsFtoZdTm4Td9NLe6W MY1XdLN5a18Kbou3JY39/hHGZDhgD4VcOGAyRalDzvwpyEpJ465xrjSMRYDsUNGfTEpZ CjHw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=iLEyFx0P6NYdEvPtE2Qi27PUUsMtIIA8mpDQ6Yy10Ac=; fh=G1r0sJ0F/0v82zumykPUwC7a5Du5vVkWNEWao5ifD3o=; b=o3Sp4TGOEVPyuheO/qOgC/pWFiV6TZDQ/VMUYs+Cdb0g8ZuQmpMui1AM1ah/JXJZTX hDM3H6ZUJN1Zxf0vlvbRYofArMX1ksiawla1i9COE1GK6XzKiCW4f1nq6w09zbwu8gqc y9a59avSznrCni7We+JCz6+/GO4X/aEkU2jPUaGy0AYt5siMBXpcSdVht/RdPiRiityK 11RnN2aFlXHI4jnxAdIqaxh1YOQEqxIojZPIyZSzLFG33Alf4HcDq7A83bOiUN8taYeZ QwEKsmjqLyfZ/EtNTkA7F5hueq3Diomf1eDx+/a4uM4zWJt0izFQq30kUjThyaalfVnu fF+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VGqfSHjd; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-122220-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122220-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id u12-20020a63df0c000000b005d8e379746asi323032pgg.630.2024.03.27.18.29.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 18:29:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122220-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VGqfSHjd; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-122220-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122220-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 8A65F29D3F7 for ; Thu, 28 Mar 2024 00:24:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 44D0C2B9A1; Thu, 28 Mar 2024 00:22:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="VGqfSHjd" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D570E17758; Thu, 28 Mar 2024 00:22:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711585368; cv=none; b=hYaC8PU2223TSPtUmaYyhoksm+INcb2o9R1j37GcTKID0aTtrfRvJQtm9d9aEebL9Oz7YrQv5trVyNsCRuzbou1/D8Cu7dicma3Uqqj6Zcm/yFFv1ecDCDpUpb6ss/bldP+NULERBdKN/bJStmp51KQpvueCdXKZZtu+mA5fB30= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711585368; c=relaxed/simple; bh=/la6h34UbOyJvuY0sNGGCyRIQ+kIJpDUkLZq0geldjc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FcWPiu/EQmcHFiMXkklNs6kBEakY0oG1KFayJ//cczDsl8f3yT7fphdrLLVzioj1BhdL/sraTiZxTac5bskWLQcd7PpirIy4MAdsvHtrUMtYez4hF67ub3CpHNiAj8kDh9+C5qn0nhm9l0mynDFieOdo+s7OmIP63GsIaTbX3Do= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=VGqfSHjd; arc=none smtp.client-ip=192.198.163.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711585367; x=1743121367; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/la6h34UbOyJvuY0sNGGCyRIQ+kIJpDUkLZq0geldjc=; b=VGqfSHjdj5AoIdKdkhfscwIfOxIU0V79E3W8St4kpDkhg0vdvs4dEeY5 zszV8DFPs3LCdM0Tq0cLislsHLxiUFQ1IwOnxhKfha3dy71qdzBvnS4ZQ viwIYadkQ0U8eNIJi8rSxfPsRUiopxqgYCD8zepn9Lc4OIKw+Hvi0tKL5 CJtnVEkciiBV3xP/rwpIqnQ+8Tu14HF5egNA1aWVj8EG4TeLJm+NtF0XL DGjsty06eBcBjNM7ciiM8nr1818m2R4hDglCIdFXYPV1WEwo32GZP67Ov xUNrldrOSTADC/qNkb3hLDq6wHtHUHTGBNLQ0TwDVAULjAlYgeRUMwMB6 A==; X-CSE-ConnectionGUID: RtUXnwtnQl27e2A5QPtE0Q== X-CSE-MsgGUID: D+/7dBorTFqOM0ujR8/MTw== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="6580688" X-IronPort-AV: E=Sophos;i="6.07,160,1708416000"; d="scan'208";a="6580688" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Mar 2024 17:22:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,160,1708416000"; d="scan'208";a="16411797" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa009.jf.intel.com with ESMTP; 27 Mar 2024 17:22:31 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@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, tim.c.chen@linux.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, chrisyan@microsoft.com Subject: [PATCH v10 06/14] x86/sgx: Add sgx_epc_lru_list to encapsulate LRU list Date: Wed, 27 Mar 2024 17:22:21 -0700 Message-Id: <20240328002229.30264-7-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240328002229.30264-1-haitao.huang@linux.intel.com> References: <20240328002229.30264-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sean Christopherson Introduce a data structure to wrap the existing reclaimable list and its spinlock. Each cgroup later will have one instance of this structure to track EPC pages allocated for processes associated with the same cgroup. Just like the global SGX reclaimer (ksgxd), an EPC cgroup reclaims pages from the reclaimable list in this structure when its usage reaches near its limit. Use this structure to encapsulate the LRU list and its lock used by the global reclaimer. 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 Reviewed-by: Jarkko Sakkinen --- V6: - removed introduction to unreclaimables in commit message. V4: - Removed unneeded comments for the spinlock and the non-reclaimables. (Kai, Jarkko) - Revised the commit to add introduction comments for unreclaimables and multiple LRU lists.(Kai) - Reordered the patches: delay all changes for unreclaimables to later, and this one becomes the first change in the SGX subsystem. V3: - Removed the helper functions and revised commit messages. --- arch/x86/kernel/cpu/sgx/main.c | 39 +++++++++++++++++----------------- arch/x86/kernel/cpu/sgx/sgx.h | 15 +++++++++++++ 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 023af54c1beb..4991eb0af748 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 @@ static 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; } @@ -578,7 +579,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim reclaim) break; } - if (list_empty(&sgx_active_page_list)) { + if (list_empty(&sgx_global_lru.reclaimable)) { page = ERR_PTR(-ENOMEM); break; } diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index 6accc81d19a9..fd28e2e98620 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -98,6 +98,21 @@ static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page) return section->virt_addr + index * PAGE_SIZE; } +/* + * Contains EPC pages tracked by the global reclaimer (ksgxd) or an EPC + * cgroup. + */ +struct sgx_epc_lru_list { + spinlock_t lock; + struct list_head reclaimable; +}; + +static inline void sgx_lru_init(struct sgx_epc_lru_list *lru) +{ + spin_lock_init(&lru->lock); + INIT_LIST_HEAD(&lru->reclaimable); +} + void sgx_free_epc_page(struct sgx_epc_page *page); void sgx_reclaim_direct(void); -- 2.25.1