From nobody Fri Feb 13 15:41:12 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C0923CE7A8A for ; Sat, 23 Sep 2023 03:07:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230019AbjIWDHn (ORCPT ); Fri, 22 Sep 2023 23:07:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229887AbjIWDHL (ORCPT ); Fri, 22 Sep 2023 23:07:11 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0718B1A7; Fri, 22 Sep 2023 20:07:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695438425; x=1726974425; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mUkcYfLatdROP3kNi+VVBQokEDDPoz07w/WU/sAEnUA=; b=aqMQTtLgGuf15Tse8w9MPy4+p26ouclTOBIzA+hYGIu+gIGEohogC+CP /5q+y3K6kdY/5ZQTc3g/WD02temprhNtd0bdlc7Jq7fQ+fyqtfpuG26JA kbapsrrtre+ISa5PlDVXzWI/k0SS9i+mZaT6DkBL80zrPb6uvla85c2eY BWSfT6iO/qGp6GGC0PyCLW/pUxnVgmZW7lskREAsFzXTJRcsO1BupQOB0 bEUmW88gVGv/APd+sinwJFAik+EwevVFJvDIjtLbfKKkSRmVWHDwtTOLg es3spATI39p3kx3MY+zzjsCfdeW6l/IbqzhPOjKMT7AK3UZLbqYaeWf6d g==; X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="447466778" X-IronPort-AV: E=Sophos;i="6.03,169,1694761200"; d="scan'208";a="447466778" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2023 20:07:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="891048551" X-IronPort-AV: E=Sophos;i="6.03,169,1694761200"; d="scan'208";a="891048551" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by fmsmga001.fm.intel.com with ESMTP; 22 Sep 2023 20:06:08 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, 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 Subject: [PATCH v5 08/18] x86/sgx: Use a list to track to-be-reclaimed pages Date: Fri, 22 Sep 2023 20:06:47 -0700 Message-Id: <20230923030657.16148-9-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230923030657.16148-1-haitao.huang@linux.intel.com> References: <20230923030657.16148-1-haitao.huang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Sean Christopherson Change sgx_reclaim_pages() to use a list rather than an array for storing the epc_pages which will be reclaimed. This change is needed to transition to the LRU implementation for EPC cgroup support. When the EPC cgroup is implemented, the reclaiming process will do a pre-order tree walk for the subtree starting from the limit-violating cgroup. When each node is visited, candidate pages are selected from its "reclaimable" LRU list and moved into this temporary list. Passing a list from node to node for temporary storage in this walk is more straightforward than using an array. 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 --- V4: - Changes needed for patch reordering - Revised commit message V3: - Removed list wrappers --- arch/x86/kernel/cpu/sgx/main.c | 40 +++++++++++++++------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index c1ae19a154d0..fba06dc5abfe 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -293,12 +293,11 @@ static void sgx_reclaimer_write(struct sgx_epc_page *= epc_page, */ static void sgx_reclaim_pages(void) { - struct sgx_epc_page *chunk[SGX_NR_TO_SCAN]; struct sgx_backing backing[SGX_NR_TO_SCAN]; + struct sgx_epc_page *epc_page, *tmp; struct sgx_encl_page *encl_page; - struct sgx_epc_page *epc_page; pgoff_t page_index; - int cnt =3D 0; + LIST_HEAD(iso); int ret; int i; =20 @@ -314,18 +313,22 @@ static void sgx_reclaim_pages(void) =20 if (kref_get_unless_zero(&encl_page->encl->refcount) !=3D 0) { sgx_epc_page_set_state(epc_page, SGX_EPC_PAGE_RECLAIM_IN_PROGRESS); - chunk[cnt++] =3D epc_page; + list_move_tail(&epc_page->list, &iso); } else { - /* The owner is freeing the page. No need to add the - * page back to the list of reclaimable pages. + /* The owner is freeing the page, remove it from the + * LRU list */ sgx_epc_page_reset_state(epc_page); + list_del_init(&epc_page->list); } } spin_unlock(&sgx_global_lru.lock); =20 - for (i =3D 0; i < cnt; i++) { - epc_page =3D chunk[i]; + if (list_empty(&iso)) + return; + + i =3D 0; + list_for_each_entry_safe(epc_page, tmp, &iso, list) { encl_page =3D epc_page->owner; =20 if (!sgx_reclaimer_age(epc_page)) @@ -340,6 +343,7 @@ static void sgx_reclaim_pages(void) goto skip; } =20 + i++; encl_page->desc |=3D SGX_ENCL_PAGE_BEING_RECLAIMED; mutex_unlock(&encl_page->encl->lock); continue; @@ -347,27 +351,19 @@ static void sgx_reclaim_pages(void) skip: spin_lock(&sgx_global_lru.lock); sgx_epc_page_set_state(epc_page, SGX_EPC_PAGE_RECLAIMABLE); - list_add_tail(&epc_page->list, &sgx_global_lru.reclaimable); + list_move_tail(&epc_page->list, &sgx_global_lru.reclaimable); spin_unlock(&sgx_global_lru.lock); =20 kref_put(&encl_page->encl->refcount, sgx_encl_release); - - chunk[i] =3D NULL; - } - - for (i =3D 0; i < cnt; i++) { - epc_page =3D chunk[i]; - if (epc_page) - sgx_reclaimer_block(epc_page); } =20 - for (i =3D 0; i < cnt; i++) { - epc_page =3D chunk[i]; - if (!epc_page) - continue; + list_for_each_entry(epc_page, &iso, list) + sgx_reclaimer_block(epc_page); =20 + i =3D 0; + list_for_each_entry_safe(epc_page, tmp, &iso, list) { encl_page =3D epc_page->owner; - sgx_reclaimer_write(epc_page, &backing[i]); + sgx_reclaimer_write(epc_page, &backing[i++]); =20 kref_put(&encl_page->encl->refcount, sgx_encl_release); sgx_epc_page_reset_state(epc_page); --=20 2.25.1