From nobody Fri Nov 1 04:25:11 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 1E2EA1A38DF; Tue, 30 Apr 2024 19:51:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506676; cv=none; b=VDCXtw6ZQX/f5TXDI1xqYHk2e4UWIYg3V39f621JRjGIdyq5swD/lexNqoZeqKFYUB7sOuUeTNVv0GtVxHCjmuCyE3IYG5Kh3d+SRREA2kdw59X1Br2LO7WreR1IOc/HSxznRh5cww1gGbUgdmas9i/XDncdUmIqekSxTqkYTas= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506676; c=relaxed/simple; bh=o2sZVV9T8C2/6mebL8Cg/D4rmwm+HgGq3e+Nu1tzHr8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dbuV5dabsIZrkmzCW1mJaFc7EB5Rkge2TnODxPpsqbHb6L4tgjxRxdCgynVLfN7eq62wqUpOLY6GqjJfa/NwS+p8scWcWF6Qozjb2k2pYyjmjMWNiGENNF8Yk6Ie2wwMOvZiSDEjncMTeAN5Tf3X5oHcH/oUWzaNG+UpeHqwmjg= 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=PckxnKAD; arc=none smtp.client-ip=198.175.65.16 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="PckxnKAD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506676; x=1746042676; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=o2sZVV9T8C2/6mebL8Cg/D4rmwm+HgGq3e+Nu1tzHr8=; b=PckxnKADNKQSipbUikpJevxdnc2XRluKwtS1TL7b4EiRE9DiM7HRFFrz ReLRGR0U2v/qHKJAwofX1EmgEbBapRbGZxvE84Z+lCAZceEwCcn83kErl CPRKJoBiwaPgUg+4jVPKV09xntv8YkMu886J7Y/sKj1JwtwgWVF4SacYb jjIvnYwPsJ4oXifl5vw7Hn/u0WykuPKHW+szMfAv5rjziHx1/UOX4rfvY UI6BrpriO+Yr44PAK17d8vePA7GXqHV2sbye2+Gh9UNFeBOtlwkc13XW9 BMXbT0tCT0i5zU8WZZdm7B4Hqbb4dXWe32pQO0ZchO4RB7Y74MhRxrrZJ A==; X-CSE-ConnectionGUID: e7+oDhUxQaGagPEHCAg4jA== X-CSE-MsgGUID: d9TeIHTKTvaGGQIvadrksg== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355607" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355607" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:10 -0700 X-CSE-ConnectionGUID: mHtn3ZRxT7iGu0TI8dOEAg== X-CSE-MsgGUID: BtRd/gZ2SOKjmdKRmJk6QA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280313" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:09 -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 v13 06/14] x86/sgx: Add sgx_epc_lru_list to encapsulate LRU list Date: Tue, 30 Apr 2024 12:51:00 -0700 Message-Id: <20240430195108.5676-7-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-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: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Kai Huang Tested-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 1226ea0d5b3c..59736dd02ca7 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -27,10 +27,9 @@ static DEFINE_XARRAY(sgx_epc_address_space); =20 /* * These variables are part of the state of the reclaimer, and must be acc= essed - * 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; =20 static atomic_long_t sgx_nr_free_pages =3D ATOMIC_LONG_INIT(0); =20 @@ -305,13 +304,13 @@ static void sgx_reclaim_pages(void) int ret; int i; =20 - spin_lock(&sgx_reclaimer_lock); + spin_lock(&sgx_global_lru.lock); for (i =3D 0; i < SGX_NR_TO_SCAN; i++) { - if (list_empty(&sgx_active_page_list)) + epc_page =3D list_first_entry_or_null(&sgx_global_lru.reclaimable, + struct sgx_epc_page, list); + if (!epc_page) break; =20 - epc_page =3D list_first_entry(&sgx_active_page_list, - struct sgx_epc_page, list); list_del_init(&epc_page->list); encl_page =3D epc_page->owner; =20 @@ -323,7 +322,7 @@ static void sgx_reclaim_pages(void) */ epc_page->flags &=3D ~SGX_EPC_PAGE_RECLAIMER_TRACKED; } - spin_unlock(&sgx_reclaimer_lock); + spin_unlock(&sgx_global_lru.lock); =20 for (i =3D 0; i < cnt; i++) { epc_page =3D chunk[i]; @@ -346,9 +345,9 @@ static void sgx_reclaim_pages(void) continue; =20 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); =20 kref_put(&encl_page->encl->refcount, sgx_encl_release); =20 @@ -379,7 +378,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); } =20 /* @@ -431,6 +430,8 @@ static bool __init sgx_page_reclaimer_init(void) =20 ksgxd_tsk =3D tsk; =20 + sgx_lru_init(&sgx_global_lru); + return true; } =20 @@ -506,10 +507,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 |=3D 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); } =20 /** @@ -524,18 +525,18 @@ void sgx_mark_page_reclaimable(struct sgx_epc_page *p= age) */ 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; } =20 list_del(&page->list); page->flags &=3D ~SGX_EPC_PAGE_RECLAIMER_TRACKED; } - spin_unlock(&sgx_reclaimer_lock); + spin_unlock(&sgx_global_lru.lock); =20 return 0; } @@ -577,7 +578,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, en= um sgx_reclaim reclaim) break; } =20 - if (list_empty(&sgx_active_page_list)) { + if (list_empty(&sgx_global_lru.reclaimable)) { page =3D ERR_PTR(-ENOMEM); break; } diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index fae8eef10232..3cf5a59a4eac 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -114,6 +114,21 @@ static inline void *sgx_get_epc_virt_addr(struct sgx_e= pc_page *page) return section->virt_addr + index * PAGE_SIZE; } =20 +/* + * 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); =20 void sgx_reclaim_direct(void); --=20 2.25.1