From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 810721B2EC5; Fri, 30 Aug 2024 16:41:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036094; cv=none; b=vB+wnYkk2oiQsk+U0lrCjv1RX/nsEgD+nmwiiDTvW8RHwJj83HnbcW4VmO9WMnkhD12orAk5ptJiPTHha/2swnm9mPbcFvwEc5riYyyFPR6lC4IYZyqPUjB76gg89SJc17lmrEcwJIVe5E74QytD+VU76JzkMArWQ61M0uel5S8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036094; c=relaxed/simple; bh=aBp+AO4BxlLvxNYOsjganDc4V0zfIfXD4mqngD4w4vw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cMjzikfV6U4LPkdiy5vOsGgTwbYF0QsAYNcEZwANynXZnez5aQX4EJ6HbZgK3eaD7fA4nRXnWWRh58CXK+WjuPr9/kZH0slSdtc9hvHFEM5jMUiXp+XM2xtSdkxIL8sVbSsaX62NZG3ORk0W0MhGpQf1HM/eG5PwQ9/G+U9DLkg= 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=ZkYVwpJ+; arc=none smtp.client-ip=192.198.163.11 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="ZkYVwpJ+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036092; x=1756572092; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aBp+AO4BxlLvxNYOsjganDc4V0zfIfXD4mqngD4w4vw=; b=ZkYVwpJ+1ZDz6Z/W29l9EVE5cgL0TwmiUGYzIEDgPipq9PUH+rj+l6vi yJnEC7H1m/BOIJ+aX80woH35VySZU8vHI1c+khDysLfajRFBV7n11Xjd4 BmEe0svTVbhXgQx0uDjuwwKrEw7CHSbava1tvsHYHxtDw0UKcDU3PpGi3 3Z3zWgsVdMppIfWGUDNigXfGIedt+uJyblch08ycgAAC/LU9jONbii/4I YX0pXdEbmHazoIS8SfePvnYfOLRnvLR5wM+virb9FhFUMwoS/VK4UAR9f h0fQZYFc3rNyKHOwqzcsXv+7BTlZOV5r9qoYTo8kBTKWw11mr2ipeHbgh g==; X-CSE-ConnectionGUID: gWSsdHIkSM+3ZO7w6h25OQ== X-CSE-MsgGUID: EjwDtvGtTk+GKD8TmvmXsw== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34299912" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34299912" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:51 -0700 X-CSE-ConnectionGUID: +ozK9Qf7SAyVvYnX4/vZHw== X-CSE-MsgGUID: 2ozohFFjQ5OCeYySSB2XbA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440450" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 01/16] x86/sgx: Replace boolean parameters with enums Date: Fri, 30 Aug 2024 09:40:22 -0700 Message-ID: <20240830164038.39343-2-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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" Replace boolean parameters for 'reclaim' in the function sgx_alloc_epc_page() and its callers with an enum. Also opportunistically remove non-static declaration of __sgx_alloc_epc_page() and a typo Signed-off-by: Haitao Huang Suggested-by: Jarkko Sakkinen Suggested-by: Dave Hansen Reviewed-by: Jarkko Sakkinen Reviewed-by: Kai Huang Tested-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/encl.c | 12 ++++++------ arch/x86/kernel/cpu/sgx/encl.h | 4 ++-- arch/x86/kernel/cpu/sgx/ioctl.c | 10 +++++----- arch/x86/kernel/cpu/sgx/main.c | 14 +++++++------- arch/x86/kernel/cpu/sgx/sgx.h | 13 +++++++++++-- arch/x86/kernel/cpu/sgx/virt.c | 2 +- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index 279148e72459..f474179b6f77 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -217,7 +217,7 @@ static struct sgx_epc_page *sgx_encl_eldu(struct sgx_en= cl_page *encl_page, struct sgx_epc_page *epc_page; int ret; =20 - epc_page =3D sgx_alloc_epc_page(encl_page, false); + epc_page =3D sgx_alloc_epc_page(encl_page, SGX_NO_RECLAIM); if (IS_ERR(epc_page)) return epc_page; =20 @@ -359,14 +359,14 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_s= truct *vma, goto err_out_unlock; } =20 - epc_page =3D sgx_alloc_epc_page(encl_page, false); + epc_page =3D sgx_alloc_epc_page(encl_page, SGX_NO_RECLAIM); if (IS_ERR(epc_page)) { if (PTR_ERR(epc_page) =3D=3D -EBUSY) vmret =3D VM_FAULT_NOPAGE; goto err_out_unlock; } =20 - va_page =3D sgx_encl_grow(encl, false); + va_page =3D sgx_encl_grow(encl, SGX_NO_RECLAIM); if (IS_ERR(va_page)) { if (PTR_ERR(va_page) =3D=3D -EBUSY) vmret =3D VM_FAULT_NOPAGE; @@ -1232,8 +1232,8 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsi= gned long addr) =20 /** * sgx_alloc_va_page() - Allocate a Version Array (VA) page - * @reclaim: Reclaim EPC pages directly if none available. Enclave - * mutex should not be held if this is set. + * @reclaim: Whether reclaim EPC pages directly if none available. Enclave + * mutex should not be held for SGX_DO_RECLAIM. * * Allocate a free EPC page and convert it to a Version Array (VA) page. * @@ -1241,7 +1241,7 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsi= gned long addr) * a VA page, * -errno otherwise */ -struct sgx_epc_page *sgx_alloc_va_page(bool reclaim) +struct sgx_epc_page *sgx_alloc_va_page(enum sgx_reclaim reclaim) { struct sgx_epc_page *epc_page; int ret; diff --git a/arch/x86/kernel/cpu/sgx/encl.h b/arch/x86/kernel/cpu/sgx/encl.h index f94ff14c9486..fe15ade02ca1 100644 --- a/arch/x86/kernel/cpu/sgx/encl.h +++ b/arch/x86/kernel/cpu/sgx/encl.h @@ -116,14 +116,14 @@ struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_= encl *encl, unsigned long offset, u64 secinfo_flags); void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr); -struct sgx_epc_page *sgx_alloc_va_page(bool reclaim); +struct sgx_epc_page *sgx_alloc_va_page(enum sgx_reclaim reclaim); unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page); void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset); bool sgx_va_page_full(struct sgx_va_page *va_page); void sgx_encl_free_epc_page(struct sgx_epc_page *page); struct sgx_encl_page *sgx_encl_load_page(struct sgx_encl *encl, unsigned long addr); -struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl, bool reclaim); +struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl, enum sgx_reclaim = reclaim); void sgx_encl_shrink(struct sgx_encl *encl, struct sgx_va_page *va_page); =20 #endif /* _X86_ENCL_H */ diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioct= l.c index b65ab214bdf5..793a0ba2cb16 100644 --- a/arch/x86/kernel/cpu/sgx/ioctl.c +++ b/arch/x86/kernel/cpu/sgx/ioctl.c @@ -17,7 +17,7 @@ #include "encl.h" #include "encls.h" =20 -struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl, bool reclaim) +struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl, enum sgx_reclaim = reclaim) { struct sgx_va_page *va_page =3D NULL; void *err; @@ -64,7 +64,7 @@ static int sgx_encl_create(struct sgx_encl *encl, struct = sgx_secs *secs) struct file *backing; long ret; =20 - va_page =3D sgx_encl_grow(encl, true); + va_page =3D sgx_encl_grow(encl, SGX_DO_RECLAIM); if (IS_ERR(va_page)) return PTR_ERR(va_page); else if (va_page) @@ -83,7 +83,7 @@ static int sgx_encl_create(struct sgx_encl *encl, struct = sgx_secs *secs) =20 encl->backing =3D backing; =20 - secs_epc =3D sgx_alloc_epc_page(&encl->secs, true); + secs_epc =3D sgx_alloc_epc_page(&encl->secs, SGX_DO_RECLAIM); if (IS_ERR(secs_epc)) { ret =3D PTR_ERR(secs_epc); goto err_out_backing; @@ -269,13 +269,13 @@ static int sgx_encl_add_page(struct sgx_encl *encl, u= nsigned long src, if (IS_ERR(encl_page)) return PTR_ERR(encl_page); =20 - epc_page =3D sgx_alloc_epc_page(encl_page, true); + epc_page =3D sgx_alloc_epc_page(encl_page, SGX_DO_RECLAIM); if (IS_ERR(epc_page)) { kfree(encl_page); return PTR_ERR(epc_page); } =20 - va_page =3D sgx_encl_grow(encl, true); + va_page =3D sgx_encl_grow(encl, SGX_DO_RECLAIM); if (IS_ERR(va_page)) { ret =3D PTR_ERR(va_page); goto err_out_free; diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 27892e57c4ef..e64073fb4256 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -464,14 +464,14 @@ static struct sgx_epc_page *__sgx_alloc_epc_page_from= _node(int nid) /** * __sgx_alloc_epc_page() - Allocate an EPC page * - * Iterate through NUMA nodes and reserve ia free EPC page to the caller. = Start + * Iterate through NUMA nodes and reserve a free EPC page to the caller. S= tart * from the NUMA node, where the caller is executing. * * Return: * - an EPC page: A borrowed EPC pages were available. * - NULL: Out of EPC pages. */ -struct sgx_epc_page *__sgx_alloc_epc_page(void) +static struct sgx_epc_page *__sgx_alloc_epc_page(void) { struct sgx_epc_page *page; int nid_of_current =3D numa_node_id(); @@ -543,12 +543,12 @@ int sgx_unmark_page_reclaimable(struct sgx_epc_page *= page) /** * sgx_alloc_epc_page() - Allocate an EPC page * @owner: the owner of the EPC page - * @reclaim: reclaim pages if necessary + * @reclaim: whether reclaim pages if necessary * * Iterate through EPC sections and borrow a free EPC page to the caller. = When a * page is no longer needed it must be released with sgx_free_epc_page(). = If - * @reclaim is set to true, directly reclaim pages when we are out of page= s. No - * mm's can be locked when @reclaim is set to true. + * @reclaim is set to SGX_DO_RECLAIM, directly reclaim pages when we are o= ut of + * pages. No mm's can be locked for SGX_DO_RECLAIM. * * Finally, wake up ksgxd when the number of pages goes below the watermark * before returning back to the caller. @@ -557,7 +557,7 @@ int sgx_unmark_page_reclaimable(struct sgx_epc_page *pa= ge) * an EPC page, * -errno on error */ -struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim) +struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim recl= aim) { struct sgx_epc_page *page; =20 @@ -571,7 +571,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bo= ol reclaim) if (list_empty(&sgx_active_page_list)) return ERR_PTR(-ENOMEM); =20 - if (!reclaim) { + if (reclaim =3D=3D SGX_NO_RECLAIM) { page =3D ERR_PTR(-EBUSY); break; } diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index d2dad21259a8..ca34cd4f58ac 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -29,6 +29,16 @@ /* Pages on free list */ #define SGX_EPC_PAGE_IS_FREE BIT(1) =20 +/** + * enum sgx_reclaim - Whether EPC reclamation is allowed within a function. + * %SGX_NO_RECLAIM: Do not reclaim EPC pages. + * %SGX_DO_RECLAIM: Reclaim EPC pages as needed. + */ +enum sgx_reclaim { + SGX_NO_RECLAIM, + SGX_DO_RECLAIM +}; + struct sgx_epc_page { unsigned int section; u16 flags; @@ -83,13 +93,12 @@ static inline void *sgx_get_epc_virt_addr(struct sgx_ep= c_page *page) return section->virt_addr + index * PAGE_SIZE; } =20 -struct sgx_epc_page *__sgx_alloc_epc_page(void); void sgx_free_epc_page(struct sgx_epc_page *page); =20 void sgx_reclaim_direct(void); void sgx_mark_page_reclaimable(struct sgx_epc_page *page); int sgx_unmark_page_reclaimable(struct sgx_epc_page *page); -struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim); +struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim recl= aim); =20 void sgx_ipi_cb(void *info); =20 diff --git a/arch/x86/kernel/cpu/sgx/virt.c b/arch/x86/kernel/cpu/sgx/virt.c index 7aaa3652e31d..e7fdc3a9abae 100644 --- a/arch/x86/kernel/cpu/sgx/virt.c +++ b/arch/x86/kernel/cpu/sgx/virt.c @@ -46,7 +46,7 @@ static int __sgx_vepc_fault(struct sgx_vepc *vepc, if (epc_page) return 0; =20 - epc_page =3D sgx_alloc_epc_page(vepc, false); + epc_page =3D sgx_alloc_epc_page(vepc, SGX_NO_RECLAIM); if (IS_ERR(epc_page)) return PTR_ERR(epc_page); =20 --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 ED5181B3B10; Fri, 30 Aug 2024 16:41:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036095; cv=none; b=tswq8S+koFYOBMvUeq5eFAs5OkKwGM/4Nwef2Z/WQ71A64rxalrZwWC3dz3P4d9W4zHORobacZGQhc6/d4BOK41lQ67S2x89vxmjyRoutPhbo/Kg+gyd1rTOlvinL7eGv+dgK7cUz6nJxNl+XLxMjgX05xrBHxPFpN2UpRVVgyc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036095; c=relaxed/simple; bh=bHMXR4kBlHaQNWZ1sZmWtMAydRtALmz756Tvz0peBSA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PENBnlUd4+8NmZCC445WGrT6KnJgpXl0pDKrdCHQGvOgWHFhFiqKKASFcWzYglS0lFjYxYyQXHUwrqRMfIvfb6tce8n23fqGVBoJ1KaCivtdTgZYkYW8itAKlW1NEkDyctyZeyYLrfDyWsBAp9mQXlPtfHfMvIgoNYhCWNvmBH0= 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=k2TYlCki; arc=none smtp.client-ip=192.198.163.11 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="k2TYlCki" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036093; x=1756572093; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bHMXR4kBlHaQNWZ1sZmWtMAydRtALmz756Tvz0peBSA=; b=k2TYlCkiU2iMMHmNGyEsUc7jm8a60lkyiR+MRCdtg42TfIoFmVwGwFpO nc4MPO0SKe04Sm0KCaqXopIwAFmnEE24kVQ9xKzmoVfZRU3VZCAaeli6h iB6KwTzm1qk4jbkoGcbTQczhVq8MAQdBC+ozMlpsWd36uag8O33rgMDqe MGL144sRw3aPeoVfvZaGsto3fUDtJPoAwbBkmP2jrkiSjLZ0ObqlVI910 IfMWUxRJO4YqVJTgwfW71j5jcaorY9cpSZRjZ0Lx7o7qcAIZhQeX5Hrce W+N/xGvXzrzyVfA4/VoXA01V4p0ST+PeP7GdMgBh85hLnfE2/BXWqiYWU Q==; X-CSE-ConnectionGUID: ftDaNkHCTqy3Ex29BokULA== X-CSE-MsgGUID: Cs9hV5WhT+C6VLu9CUKtlA== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34299925" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34299925" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: iOqFgDb9Th6apccG67sCEA== X-CSE-MsgGUID: 5YqiamlnRsu+S/vmsVLAfg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440455" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 02/16] cgroup/misc: Add per resource callbacks for CSS events Date: Fri, 30 Aug 2024 09:40:23 -0700 Message-ID: <20240830164038.39343-3-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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: Kristen Carlson Accardi The misc cgroup controller (subsystem) currently does not perform resource type specific action for Cgroups Subsystem State (CSS) events: the 'css_alloc' event when a cgroup is created and the 'css_free' event when a cgroup is destroyed. Define callbacks for those events and allow resource providers to register the callbacks per resource type as needed. This will be utilized later by the EPC misc cgroup support implemented in the SGX driver. The SGX callbacks will need to access the 'struct misc_cg'. Pass 'struct misc_cg' to the callbacks but not the 'struct misc_res' because the latter doesn't have a pointer pointing back to 'struct misc_cg'. Link: https://lore.kernel.org/lkml/op.2kdw36otwjvjmi@hhuan26-mobl.amr.corp.= intel.com/ Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Reviewed-by: Jarkko Sakkinen Reviewed-by: Tejun Heo Tested-by: Jarkko Sakkinen Acked-by: Kai Huang --- V17: - remove EXPORT_SYMBOL_GPL and add READ_ONCE, WRITE_ONCE to be consistent, = remove unused parent_cg. (Kai) V16: - Don't call the ops if capacity is zero. (Kai) - Simplify the last paragraph of the commit message. (Kai) V15: - Style fixes (Jarkko, Kai) - _misc_cg_res_free() takes the last index. Only call ->free() for those re= s types with ->alloc() successfully called. (Ridong) V12: - Add comments in commit to clarify reason to pass in misc_cg, not misc_res. (Kai) - Remove unlikely (Kai) V8: - Abstract out _misc_cg_res_free() and _misc_cg_res_alloc() (Jarkko) V7: - Make ops one per resource type and store them in array (Michal) - Rename the ops struct to misc_res_ops, and enforce the constraints of req= uired callback functions (Jarkko) - Moved addition of priv field to patch 4 where it was used first. (Jarkko) V6: - Create ops struct for per resource callbacks (Jarkko) - Drop max_write callback (Dave, Michal) - Style fixes (Kai) --- include/linux/misc_cgroup.h | 11 +++++ kernel/cgroup/misc.c | 88 ++++++++++++++++++++++++++++++++++--- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h index 49eef10c8e59..e5159770a68e 100644 --- a/include/linux/misc_cgroup.h +++ b/include/linux/misc_cgroup.h @@ -28,6 +28,16 @@ struct misc_cg; =20 #include =20 +/** + * struct misc_res_ops: per resource type callback ops. + * @alloc: invoked for resource specific initialization when cgroup is all= ocated. + * @free: invoked for resource specific cleanup when cgroup is deallocated. + */ +struct misc_res_ops { + int (*alloc)(struct misc_cg *cg); + void (*free)(struct misc_cg *cg); +}; + /** * struct misc_res: Per cgroup per misc type resource * @max: Maximum limit on the resource. @@ -62,6 +72,7 @@ struct misc_cg { =20 u64 misc_cg_res_total_usage(enum misc_res_type type); int misc_cg_set_capacity(enum misc_res_type type, u64 capacity); +int misc_cg_set_ops(enum misc_res_type type, const struct misc_res_ops *op= s); int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, u64 am= ount); void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg, u64 amo= unt); =20 diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c index 0e26068995a6..b06343ab9651 100644 --- a/kernel/cgroup/misc.c +++ b/kernel/cgroup/misc.c @@ -39,6 +39,9 @@ static struct misc_cg root_cg; */ static u64 misc_res_capacity[MISC_CG_RES_TYPES]; =20 +/* Resource type specific operations */ +static const struct misc_res_ops *misc_res_ops[MISC_CG_RES_TYPES]; + /** * parent_misc() - Get the parent of the passed misc cgroup. * @cgroup: cgroup whose parent needs to be fetched. @@ -105,6 +108,40 @@ int misc_cg_set_capacity(enum misc_res_type type, u64 = capacity) } EXPORT_SYMBOL_GPL(misc_cg_set_capacity); =20 +/** + * misc_cg_set_ops() - register resource specific operations. + * @type: Type of the misc res. + * @ops: Operations for the given type. + * + * The callbacks in @ops will not be invoked if the capacity of @type is 0. + * + * Context: Any context. + * Return: + * * %0 - Successfully registered the operations. + * * %-EINVAL - If @type is invalid, or the operations missing any require= d callbacks. + */ +int misc_cg_set_ops(enum misc_res_type type, const struct misc_res_ops *op= s) +{ + if (!valid_type(type)) + return -EINVAL; + + if (!ops) + return -EINVAL; + + if (!ops->alloc) { + pr_err("%s: alloc missing\n", __func__); + return -EINVAL; + } + + if (!ops->free) { + pr_err("%s: free missing\n", __func__); + return -EINVAL; + } + + WRITE_ONCE(misc_res_ops[type], ops); + return 0; +} + /** * misc_cg_cancel_charge() - Cancel the charge from the misc cgroup. * @type: Misc res type in misc cg to cancel the charge from. @@ -439,6 +476,41 @@ static struct cftype misc_cg_files[] =3D { {} }; =20 +static inline void _misc_cg_res_free(struct misc_cg *cg, enum misc_res_typ= e last) +{ + const struct misc_res_ops *ops; + enum misc_res_type i; + + for (i =3D 0; i <=3D last; i++) { + ops =3D READ_ONCE(misc_res_ops[i]); + if (ops && READ_ONCE(misc_res_capacity[i])) + ops->free(cg); + } +} + +static inline int _misc_cg_res_alloc(struct misc_cg *cg) +{ + const struct misc_res_ops *ops; + enum misc_res_type i; + int ret; + + for (i =3D 0; i < MISC_CG_RES_TYPES; i++) { + WRITE_ONCE(cg->res[i].max, MAX_NUM); + atomic64_set(&cg->res[i].usage, 0); + + ops =3D READ_ONCE(misc_res_ops[i]); + if (ops && READ_ONCE(misc_res_capacity[i])) { + ret =3D ops->alloc(cg); + if (ret) { + _misc_cg_res_free(cg, i); + return ret; + } + } + } + + return 0; +} + /** * misc_cg_alloc() - Allocate misc cgroup. * @parent_css: Parent cgroup. @@ -451,8 +523,8 @@ static struct cftype misc_cg_files[] =3D { static struct cgroup_subsys_state * misc_cg_alloc(struct cgroup_subsys_state *parent_css) { - enum misc_res_type i; struct misc_cg *cg; + int ret; =20 if (!parent_css) { cg =3D &root_cg; @@ -462,9 +534,12 @@ misc_cg_alloc(struct cgroup_subsys_state *parent_css) return ERR_PTR(-ENOMEM); } =20 - for (i =3D 0; i < MISC_CG_RES_TYPES; i++) { - WRITE_ONCE(cg->res[i].max, MAX_NUM); - atomic64_set(&cg->res[i].usage, 0); + ret =3D _misc_cg_res_alloc(cg); + if (ret) { + if (likely(parent_css)) + kfree(cg); + + return ERR_PTR(ret); } =20 return &cg->css; @@ -478,7 +553,10 @@ misc_cg_alloc(struct cgroup_subsys_state *parent_css) */ static void misc_cg_free(struct cgroup_subsys_state *css) { - kfree(css_misc(css)); + struct misc_cg *cg =3D css_misc(css); + + _misc_cg_res_free(cg, MISC_CG_RES_TYPES - 1); + kfree(cg); } =20 /* Cgroup controller callbacks */ --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 C3E021B81D2; Fri, 30 Aug 2024 16:41:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036096; cv=none; b=IQwOJuFd9Yi7Vsu1zHI4uYOF4BFd5D9EucsQtaHeHAn7tVEGe0uPpOrMUtcayNxEWalnmCzC8WALGKLBfpFqc8tiniPfOMukCz12jdHdjPnXZGvGiuHBuX2cBFZ3ErptI1QNgp3uyfT7pm3ODuH/f0w0dfKfktrLj9T1y4kPSI8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036096; c=relaxed/simple; bh=qQz3KW0mRe/vY0iIKXAMbxtAf+LKEJFOzU5inEYCuzI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OpSIcQx74cO2vxHTNAScGN2do4aGUklcjGVfdRik0yDoC5mqMu6soK8EugRW5Yt4RzXjYqRcASX3j69pUzQh28O4Kc3au7JRm3tkY96/PT01Z8w7sh67nFaq1+4Yps5Fyvi4XVbiPN46Y/ljsSE8k20nUyc/pQSlBVjv8DhOLRo= 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=kwzVmlIh; arc=none smtp.client-ip=192.198.163.11 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="kwzVmlIh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036094; x=1756572094; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qQz3KW0mRe/vY0iIKXAMbxtAf+LKEJFOzU5inEYCuzI=; b=kwzVmlIh4qmlNnQB/9vyFg22MMYc5mLXfIooQCatOXO3CFmew/5B443c 7l3u8Wbrzj6R/E0AqVjtLVjF2bFkcyEnzATTutJ5gJow9YM8JE+hWPu7N DS7qikTWtRdagJPir79DtEIwQwLbSxIeKcM1QA6Il7avCHhMz9foU4MwN XVW8HkQo3MetRfDzJJXwqR1b5ZV2cWmnCIiKf+r3sz8pwO7G6Yi3vGY/u srBHgZJfDqj5JyEa/XKR1WEpyidYWFlY49DuUhbNw5lD4nyOYYS55h2Br 7Uf446KR2WGg/5oRZcrRMIsFfwVqMzLaf2yxj3YRr7/aDTZw2G5sRw/oB A==; X-CSE-ConnectionGUID: n/SXqgpoRmOCEYSW5m8TRA== X-CSE-MsgGUID: tbCOJ5U6ShSuIokcOGPU5Q== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34299954" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34299954" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: KoRjFvu7TwWUE1r+kDBYHw== X-CSE-MsgGUID: 27iPO1OrQ32+lOftYxFZGQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440458" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 03/16] cgroup/misc: Expose APIs for SGX driver Date: Fri, 30 Aug 2024 09:40:24 -0700 Message-ID: <20240830164038.39343-4-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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: Kristen Carlson Accardi The SGX EPC cgroup will reclaim EPC pages when usage in a cgroup reaches its or ancestor's limit. This requires a walk from the current cgroup up to the root similar to misc_cg_try_charge(). Expose misc_cg_parent() to enable this walk. The SGX driver also needs start a global level reclamation from the root. Expose misc_cg_root() for the SGX driver to access. Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Reviewed-by: Jarkko Sakkinen Reviewed-by: Tejun Heo Reviewed-by: Kai Huang Tested-by: Jarkko Sakkinen --- V17: - Remove EXPORT_SYMBOL_GPL as these are currently used by the builtin SGX driver only. (Kai) V6: - Make commit messages more concise and split the original patch into two(K= ai) --- include/linux/misc_cgroup.h | 24 ++++++++++++++++++++++++ kernel/cgroup/misc.c | 20 +++++++------------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h index e5159770a68e..75c711333ad4 100644 --- a/include/linux/misc_cgroup.h +++ b/include/linux/misc_cgroup.h @@ -70,6 +70,7 @@ struct misc_cg { struct misc_res res[MISC_CG_RES_TYPES]; }; =20 +struct misc_cg *misc_cg_root(void); u64 misc_cg_res_total_usage(enum misc_res_type type); int misc_cg_set_capacity(enum misc_res_type type, u64 capacity); int misc_cg_set_ops(enum misc_res_type type, const struct misc_res_ops *op= s); @@ -90,6 +91,20 @@ static inline struct misc_cg *css_misc(struct cgroup_sub= sys_state *css) return css ? container_of(css, struct misc_cg, css) : NULL; } =20 +/** + * misc_cg_parent() - Get the parent of the passed misc cgroup. + * @cgroup: cgroup whose parent needs to be fetched. + * + * Context: Any context. + * Return: + * * struct misc_cg* - Parent of the @cgroup. + * * %NULL - If @cgroup is null or the passed cgroup does not have a paren= t. + */ +static inline struct misc_cg *misc_cg_parent(struct misc_cg *cgroup) +{ + return cgroup ? css_misc(cgroup->css.parent) : NULL; +} + /* * get_current_misc_cg() - Find and get the misc cgroup of the current tas= k. * @@ -114,6 +129,15 @@ static inline void put_misc_cg(struct misc_cg *cg) } =20 #else /* !CONFIG_CGROUP_MISC */ +static inline struct misc_cg *misc_cg_root(void) +{ + return NULL; +} + +static inline struct misc_cg *misc_cg_parent(struct misc_cg *cg) +{ + return NULL; +} =20 static inline u64 misc_cg_res_total_usage(enum misc_res_type type) { diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c index b06343ab9651..757b534bec5f 100644 --- a/kernel/cgroup/misc.c +++ b/kernel/cgroup/misc.c @@ -43,17 +43,11 @@ static u64 misc_res_capacity[MISC_CG_RES_TYPES]; static const struct misc_res_ops *misc_res_ops[MISC_CG_RES_TYPES]; =20 /** - * parent_misc() - Get the parent of the passed misc cgroup. - * @cgroup: cgroup whose parent needs to be fetched. - * - * Context: Any context. - * Return: - * * struct misc_cg* - Parent of the @cgroup. - * * %NULL - If @cgroup is null or the passed cgroup does not have a paren= t. + * misc_cg_root() - Return the root misc cgroup. */ -static struct misc_cg *parent_misc(struct misc_cg *cgroup) +struct misc_cg *misc_cg_root(void) { - return cgroup ? css_misc(cgroup->css.parent) : NULL; + return &root_cg; } =20 /** @@ -176,7 +170,7 @@ static void misc_cg_event(enum misc_res_type type, stru= ct misc_cg *cg) atomic64_inc(&cg->res[type].events_local); cgroup_file_notify(&cg->events_local_file); =20 - for (; parent_misc(cg); cg =3D parent_misc(cg)) { + for (; misc_cg_parent(cg); cg =3D misc_cg_parent(cg)) { atomic64_inc(&cg->res[type].events); cgroup_file_notify(&cg->events_file); } @@ -211,7 +205,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct = misc_cg *cg, u64 amount) if (!amount) return 0; =20 - for (i =3D cg; i; i =3D parent_misc(i)) { + for (i =3D cg; i; i =3D misc_cg_parent(i)) { res =3D &i->res[type]; =20 new_usage =3D atomic64_add_return(amount, &res->usage); @@ -227,7 +221,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct = misc_cg *cg, u64 amount) err_charge: misc_cg_event(type, i); =20 - for (j =3D cg; j !=3D i; j =3D parent_misc(j)) + for (j =3D cg; j !=3D i; j =3D misc_cg_parent(j)) misc_cg_cancel_charge(type, j, amount); misc_cg_cancel_charge(type, i, amount); return ret; @@ -249,7 +243,7 @@ void misc_cg_uncharge(enum misc_res_type type, struct m= isc_cg *cg, u64 amount) if (!(amount && valid_type(type) && cg)) return; =20 - for (i =3D cg; i; i =3D parent_misc(i)) + for (i =3D cg; i; i =3D misc_cg_parent(i)) misc_cg_cancel_charge(type, i, amount); } EXPORT_SYMBOL_GPL(misc_cg_uncharge); --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 81E161B9B29; Fri, 30 Aug 2024 16:41:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036097; cv=none; b=Nn+ZR153IT/paLo5g/kEcOv5VZ3icY7IuyLwhkRMHeYwPuxx0MOiQYD6wCB8/7A2NaumtB2stfq1t24Pn3jtka+jBOUZz4mKb/58GYKDR6fzuzhZ7J9noTanRNjJaGwMEL1FajsomBO/CzDPXgWU2eEdd4praDHuKViw5qsNe00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036097; c=relaxed/simple; bh=SlMrBaraTav424oIll4o22m9QSwHg8CcZxGkFfA7+FA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hBG4/QD7XeY46JjakOFd0ySikkVSctJKdfRqJw60AKtg0pGZrYuBl3rHLU1pFax2oRUzE8gcbeQsVZ6HMCqKLrNirnr0sDIl9KNGg2mSCB+DHkCihK2cuikAyiBrf3cOvCCh04b/z5mITWLupiv3fSN1TwahDOZUf1ErJ9CzFzc= 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=J4/2Ap+O; arc=none smtp.client-ip=192.198.163.11 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="J4/2Ap+O" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036095; x=1756572095; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SlMrBaraTav424oIll4o22m9QSwHg8CcZxGkFfA7+FA=; b=J4/2Ap+Occx6TCQE4yxuwVriavpMF4A0JJ/bxSpPcdoewbYuvzd7hQhQ Ov4xNAQBHCrHocY5aWRp6o3rf+Uc4yHrITVrm0Kzxf19qNyvQEdrQ5Lsf SUmJzRS/rfWkL/S0tvfSrzc6rDuOQH0y/gvM5XxjDTPWapUo92cF8A75e IIaPgaCebSLngNXBAPxxis2n0la86FBnOGB4fWotR5J2TeYHI8HEnH11x CgbwWooSDm336a0sO+JTFAt2Ek16oW/3p3xpRwMNFypPFVTZkPnKATit/ MPM4RbRxIHo/1/2Ph5rVuFziZGlLvKzb8YgtxmiKzlHdR9ttmv2LVRMw2 A==; X-CSE-ConnectionGUID: NIOi3cu3RJStj87gzjudyQ== X-CSE-MsgGUID: ajZbBjTlS7mJoNgi4CRK4g== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34299970" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34299970" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: s1uh38juQHqKxClFQViwUA== X-CSE-MsgGUID: IIGvwhomSnKdL6tcWtojeg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440461" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 04/16] cgroup/misc: Add SGX EPC resource type Date: Fri, 30 Aug 2024 09:40:25 -0700 Message-ID: <20240830164038.39343-5-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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: Kristen Carlson Accardi Add SGX EPC memory, MISC_CG_RES_SGX_EPC, to be a valid resource type for the misc controller. Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Reviewed-by: Jarkko Sakkinen Reviewed-by: Kai Huang Tested-by: Jarkko Sakkinen --- V12: - Remove CONFIG_CGROUP_SGX_EPC (Jarkko) V6: - Split the original patch into this and the preceding one (Kai) --- include/linux/misc_cgroup.h | 4 ++++ kernel/cgroup/misc.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h index 75c711333ad4..b4119869b0d1 100644 --- a/include/linux/misc_cgroup.h +++ b/include/linux/misc_cgroup.h @@ -17,6 +17,10 @@ enum misc_res_type { MISC_CG_RES_SEV, /** @MISC_CG_RES_SEV_ES: AMD SEV-ES ASIDs resource */ MISC_CG_RES_SEV_ES, +#endif +#ifdef CONFIG_X86_SGX + /** @MISC_CG_RES_SGX_EPC: SGX EPC memory resource */ + MISC_CG_RES_SGX_EPC, #endif /** @MISC_CG_RES_TYPES: count of enum misc_res_type constants */ MISC_CG_RES_TYPES diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c index 757b534bec5f..cc7206734cde 100644 --- a/kernel/cgroup/misc.c +++ b/kernel/cgroup/misc.c @@ -24,6 +24,10 @@ static const char *const misc_res_name[] =3D { /* AMD SEV-ES ASIDs resource */ "sev_es", #endif +#ifdef CONFIG_X86_SGX + /* Intel SGX EPC memory bytes */ + "sgx_epc", +#endif }; =20 /* Root misc cgroup */ --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 A7B281BB68E; Fri, 30 Aug 2024 16:41:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036100; cv=none; b=ksnPqIARwQkp1eBLHbvCN/oRYPUDzkujFXmXnUj9z24oeSNIM1MUvGYApdsMqNZS9YGOdGj1Cu13ZRgJx1SYuuwUM3Hcdkhtv2YcQEEizNIxRPQjIb48KrAKUSgzqPlBpzdq5VYkagVpAN4rPhrHQRNCAMSOcXTsLpsDeAQe+0Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036100; c=relaxed/simple; bh=K/MgWsnfH75WRIJzv24IgSeYzE5Q8QxnzxXKTeRzHr0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NaSsrnHMo/FMYqT4EBojZ1h/nszNDuhGP/pikUeMsdoI0cRd/WS2KmqGbcDkchOsk/38q4SD2nhnLqvQlvxJIPd8QQexXdrF+09uYDBGH3Pjhe3n1MhrEqIglve04fM0vTKHG6gANE19csnDejdFYs4mP31nWJAI2Y58scsY2No= 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=Sgu9DCYk; arc=none smtp.client-ip=192.198.163.11 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="Sgu9DCYk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036097; x=1756572097; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=K/MgWsnfH75WRIJzv24IgSeYzE5Q8QxnzxXKTeRzHr0=; b=Sgu9DCYk858cA6VALhkToDL6nm2NYvrPS52cTplcj2wUSjGW3iOeQ5bj MfjDJzCxv22pguRLW9VxKb1E2bNxsoPcO7RlnFp4CNC84SuW14Gn8hEs/ VFns7HuCynx8twzGPdmxW2/LZo1yo63OFOtE95geHTSSMdEeT42hMzygX QzglYOeh6o/PmmRwmvnZbLvQcrNvS4VSpcEJGPEy1eYLeF+T27NOZGMvj olKVqQR/Ha6MV3dOs8pkOj+78NktPVDAkZ4P+ccEftj19DOK4xCeZeVAS UezZRVfeuTbzqho6xaaFfBiWIwFwiM5DUdx/DFxFz0l4LAvuizvyGVQwP Q==; X-CSE-ConnectionGUID: S1+yiAKoQGKU0tGv04dN8Q== X-CSE-MsgGUID: ByMaZ3pRT6q6oDgohU4vHw== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34300007" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34300007" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: zy5ZD+1hRIOXrUe26kEVBQ== X-CSE-MsgGUID: j07sulMYRSyoEhy6Xuz1Ww== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440464" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 05/16] x86/sgx: Implement basic EPC misc cgroup functionality Date: Fri, 30 Aug 2024 09:40:26 -0700 Message-ID: <20240830164038.39343-6-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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: Kristen Carlson Accardi SGX Enclave Page Cache (EPC) memory allocations are separate from normal RAM allocations, and are managed solely by the SGX subsystem. The existing cgroup memory controller cannot be used to limit or account for SGX EPC memory, which is a desirable feature in some environments. For instance, within a Kubernetes environment, while a user may specify a particular EPC quota for a pod, the orchestrator requires a mechanism to enforce that the pod's actual runtime EPC usage does not exceed the allocated quota. Utilize the misc controller [admin-guide/cgroup-v2.rst, 5-9. Misc] to limit and track EPC allocations per cgroup. Earlier patches have added the "sgx_epc" resource type in the misc cgroup subsystem. Add basic support in SGX driver as the "sgx_epc" resource provider: - Set "capacity" of EPC by calling misc_cg_set_capacity() - Update EPC usage counter, "current", by calling charge and uncharge APIs for EPC allocation and deallocation, respectively. - Setup sgx_epc resource type specific callbacks, which perform initialization and cleanup during cgroup allocation and deallocation, respectively. With these changes, the misc cgroup controller enables users to set a hard limit for EPC usage in the "misc.max" interface file. It reports current usage in "misc.current", the total EPC memory available in "misc.capacity", and the number of times EPC usage reached the max limit in "misc.events". For now, the EPC cgroup simply blocks additional EPC allocation in sgx_alloc_epc_page() when the limit is reached. Reclaimable pages are still tracked in the global active list, only reclaimed by the global reclaimer when the total free page count is lower than a threshold. Later patches will reorganize the tracking and reclamation code in the global reclaimer and implement per-cgroup tracking and reclaiming. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Reviewed-by: Jarkko Sakkinen Reviewed-by: Tejun Heo Reviewed-by: Kai Huang Tested-by: Jarkko Sakkinen --- V17: - Rename sgx_cgroup_register() -> sgx_cgroup_init(), and move the sgx_cgroup_misc_init call to it. (Kai) V16: - Proper handling for failures during init (Kai) - Register ops and capacity at the end when SGX is ready to handle callbacks. V15: - Declare __init for sgx_cgroup_init() (Jarkko) - Disable SGX when sgx_cgroup_init() fails (Jarkko) V13: - Remove unneeded includes. (Kai) V12: - Remove CONFIG_CGROUP_SGX_EPC and make sgx cgroup implementation conditionally compiled with CONFIG_CGROUP_MISC. (Jarkko) V11: - Update copyright and format better (Kai) - Create wrappers to remove #ifdefs in c file. (Kai) - Remove unneeded comments (Kai) V10: - Shorten function, variable, struct names, s/sgx_epc_cgroup/sgx_cgroup. (J= arkko) - Use enums instead of booleans for the parameters. (Dave, Jarkko) V8: - Remove null checks for epc_cg in try_charge()/uncharge(). (Jarkko) - Remove extra space, '_INTEL'. (Jarkko) V7: - Use a static for root cgroup (Kai) - Wrap epc_cg field in sgx_epc_page struct with #ifdef (Kai) - Correct check for charge API return (Kai) - Start initialization in SGX device driver init (Kai) - Remove unneeded BUG_ON (Kai) - Split sgx_get_current_epc_cg() out of sgx_epc_cg_try_charge() (Kai) V6: - Split the original large patch"Limit process EPC usage with misc cgroup controller" and restructure it (Kai) --- arch/x86/kernel/cpu/sgx/Makefile | 1 + arch/x86/kernel/cpu/sgx/epc_cgroup.c | 87 ++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/epc_cgroup.h | 72 +++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/main.c | 41 +++++++++++-- arch/x86/kernel/cpu/sgx/sgx.h | 24 ++++++++ include/linux/misc_cgroup.h | 2 + 6 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 arch/x86/kernel/cpu/sgx/epc_cgroup.c create mode 100644 arch/x86/kernel/cpu/sgx/epc_cgroup.h diff --git a/arch/x86/kernel/cpu/sgx/Makefile b/arch/x86/kernel/cpu/sgx/Mak= efile index 9c1656779b2a..081cb424575e 100644 --- a/arch/x86/kernel/cpu/sgx/Makefile +++ b/arch/x86/kernel/cpu/sgx/Makefile @@ -4,3 +4,4 @@ obj-y +=3D \ ioctl.o \ main.o obj-$(CONFIG_X86_SGX_KVM) +=3D virt.o +obj-$(CONFIG_CGROUP_MISC) +=3D epc_cgroup.o diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c new file mode 100644 index 000000000000..b219ec21c637 --- /dev/null +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright(c) 2022-2024 Intel Corporation. */ + +#include +#include "epc_cgroup.h" + +/* The root SGX EPC cgroup */ +static struct sgx_cgroup sgx_cg_root; + +/** + * sgx_cgroup_try_charge() - try to charge cgroup for a single EPC page + * + * @sgx_cg: The EPC cgroup to be charged for the page. + * Return: + * * %0 - If successfully charged. + * * -errno - for failures. + */ +int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg) +{ + return misc_cg_try_charge(MISC_CG_RES_SGX_EPC, sgx_cg->cg, PAGE_SIZE); +} + +/** + * sgx_cgroup_uncharge() - uncharge a cgroup for an EPC page + * @sgx_cg: The charged sgx cgroup. + */ +void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg) +{ + misc_cg_uncharge(MISC_CG_RES_SGX_EPC, sgx_cg->cg, PAGE_SIZE); +} + +static void sgx_cgroup_free(struct misc_cg *cg) +{ + struct sgx_cgroup *sgx_cg; + + sgx_cg =3D sgx_cgroup_from_misc_cg(cg); + if (!sgx_cg) + return; + + kfree(sgx_cg); +} + +static void sgx_cgroup_misc_init(struct misc_cg *cg, struct sgx_cgroup *sg= x_cg) +{ + cg->res[MISC_CG_RES_SGX_EPC].priv =3D sgx_cg; + sgx_cg->cg =3D cg; +} + +static int sgx_cgroup_alloc(struct misc_cg *cg) +{ + struct sgx_cgroup *sgx_cg; + + sgx_cg =3D kzalloc(sizeof(*sgx_cg), GFP_KERNEL); + if (!sgx_cg) + return -ENOMEM; + + sgx_cgroup_misc_init(cg, sgx_cg); + + return 0; +} + +const struct misc_res_ops sgx_cgroup_ops =3D { + .alloc =3D sgx_cgroup_alloc, + .free =3D sgx_cgroup_free, +}; + +/* + * Register capacity and ops for SGX cgroup and init the root cgroup. + * Only called at the end of sgx_init() when SGX is ready to handle the ops + * callbacks. No failures allowed in this function. + */ +void __init sgx_cgroup_init(void) +{ + unsigned int nid =3D first_node(sgx_numa_mask); + unsigned int first =3D nid; + u64 capacity =3D 0; + + sgx_cgroup_misc_init(misc_cg_root(), &sgx_cg_root); + misc_cg_set_ops(MISC_CG_RES_SGX_EPC, &sgx_cgroup_ops); + + /* sgx_numa_mask is not empty when this is called */ + do { + capacity +=3D sgx_numa_nodes[nid].size; + nid =3D next_node_in(nid, sgx_numa_mask); + } while (nid !=3D first); + misc_cg_set_capacity(MISC_CG_RES_SGX_EPC, capacity); +} diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx= /epc_cgroup.h new file mode 100644 index 000000000000..52c9faf31fa1 --- /dev/null +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _SGX_EPC_CGROUP_H_ +#define _SGX_EPC_CGROUP_H_ + +#include +#include +#include + +#include "sgx.h" + +#ifndef CONFIG_CGROUP_MISC + +#define MISC_CG_RES_SGX_EPC MISC_CG_RES_TYPES +struct sgx_cgroup; + +static inline struct sgx_cgroup *sgx_get_current_cg(void) +{ + return NULL; +} + +static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg) { } + +static inline int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg) +{ + return 0; +} + +static inline void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg) { } + +static inline void __init sgx_cgroup_init(void) { } + +#else /* CONFIG_CGROUP_MISC */ + +struct sgx_cgroup { + struct misc_cg *cg; +}; + +static inline struct sgx_cgroup *sgx_cgroup_from_misc_cg(struct misc_cg *c= g) +{ + return (struct sgx_cgroup *)(cg->res[MISC_CG_RES_SGX_EPC].priv); +} + +/** + * sgx_get_current_cg() - get the EPC cgroup of current process. + * + * Returned cgroup has its ref count increased by 1. Caller must call + * sgx_put_cg() to return the reference. + * + * Return: EPC cgroup to which the current task belongs to. + */ +static inline struct sgx_cgroup *sgx_get_current_cg(void) +{ + /* get_current_misc_cg() never returns NULL when Kconfig enabled */ + return sgx_cgroup_from_misc_cg(get_current_misc_cg()); +} + +/** + * sgx_put_cg() - Put the EPC cgroup and reduce its ref count. + * @sgx_cg - EPC cgroup to put. + */ +static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg) +{ + put_misc_cg(sgx_cg->cg); +} + +int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg); +void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg); +void __init sgx_cgroup_init(void); + +#endif /* CONFIG_CGROUP_MISC */ + +#endif /* _SGX_EPC_CGROUP_H_ */ diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index e64073fb4256..395481e9ba55 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -18,6 +18,7 @@ #include "driver.h" #include "encl.h" #include "encls.h" +#include "epc_cgroup.h" =20 struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; static int sgx_nr_epc_sections; @@ -35,14 +36,14 @@ static DEFINE_SPINLOCK(sgx_reclaimer_lock); static atomic_long_t sgx_nr_free_pages =3D ATOMIC_LONG_INIT(0); =20 /* Nodes with one or more EPC sections. */ -static nodemask_t sgx_numa_mask; +nodemask_t sgx_numa_mask; =20 /* * Array with one list_head for each possible NUMA node. Each * list contains all the sgx_epc_section's which are on that * node. */ -static struct sgx_numa_node *sgx_numa_nodes; +struct sgx_numa_node *sgx_numa_nodes; =20 static LIST_HEAD(sgx_dirty_page_list); =20 @@ -559,7 +560,16 @@ int sgx_unmark_page_reclaimable(struct sgx_epc_page *p= age) */ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim recl= aim) { + struct sgx_cgroup *sgx_cg; struct sgx_epc_page *page; + int ret; + + sgx_cg =3D sgx_get_current_cg(); + ret =3D sgx_cgroup_try_charge(sgx_cg); + if (ret) { + sgx_put_cg(sgx_cg); + return ERR_PTR(ret); + } =20 for ( ; ; ) { page =3D __sgx_alloc_epc_page(); @@ -568,8 +578,10 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, e= num sgx_reclaim reclaim) break; } =20 - if (list_empty(&sgx_active_page_list)) - return ERR_PTR(-ENOMEM); + if (list_empty(&sgx_active_page_list)) { + page =3D ERR_PTR(-ENOMEM); + break; + } =20 if (reclaim =3D=3D SGX_NO_RECLAIM) { page =3D ERR_PTR(-EBUSY); @@ -585,6 +597,15 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, e= num sgx_reclaim reclaim) cond_resched(); } =20 + if (!IS_ERR(page)) { + WARN_ON_ONCE(sgx_epc_page_get_cgroup(page)); + /* sgx_put_cg() in sgx_free_epc_page() */ + sgx_epc_page_set_cgroup(page, sgx_cg); + } else { + sgx_cgroup_uncharge(sgx_cg); + sgx_put_cg(sgx_cg); + } + if (sgx_should_reclaim(SGX_NR_LOW_PAGES)) wake_up(&ksgxd_waitq); =20 @@ -603,8 +624,16 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, e= num sgx_reclaim reclaim) void sgx_free_epc_page(struct sgx_epc_page *page) { struct sgx_epc_section *section =3D &sgx_epc_sections[page->section]; + struct sgx_cgroup *sgx_cg =3D sgx_epc_page_get_cgroup(page); struct sgx_numa_node *node =3D section->node; =20 + /* sgx_cg could be NULL if called from __sgx_sanitize_pages() */ + if (sgx_cg) { + sgx_cgroup_uncharge(sgx_cg); + sgx_put_cg(sgx_cg); + sgx_epc_page_set_cgroup(page, NULL); + } + spin_lock(&node->lock); =20 page->owner =3D NULL; @@ -644,6 +673,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr,= u64 size, section->pages[i].flags =3D 0; section->pages[i].owner =3D NULL; section->pages[i].poison =3D 0; + sgx_epc_page_set_cgroup(§ion->pages[i], NULL); + list_add_tail(§ion->pages[i].list, &sgx_dirty_page_list); } =20 @@ -943,6 +974,8 @@ static int __init sgx_init(void) if (sgx_vepc_init() && ret) goto err_provision; =20 + sgx_cgroup_init(); + return 0; =20 err_provision: diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index ca34cd4f58ac..c5208da7c8eb 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -39,14 +39,35 @@ enum sgx_reclaim { SGX_DO_RECLAIM }; =20 +struct sgx_cgroup; + struct sgx_epc_page { unsigned int section; u16 flags; u16 poison; struct sgx_encl_page *owner; struct list_head list; +#ifdef CONFIG_CGROUP_MISC + struct sgx_cgroup *sgx_cg; +#endif }; =20 +static inline void sgx_epc_page_set_cgroup(struct sgx_epc_page *page, stru= ct sgx_cgroup *cg) +{ +#ifdef CONFIG_CGROUP_MISC + page->sgx_cg =3D cg; +#endif +} + +static inline struct sgx_cgroup *sgx_epc_page_get_cgroup(struct sgx_epc_pa= ge *page) +{ +#ifdef CONFIG_CGROUP_MISC + return page->sgx_cg; +#else + return NULL; +#endif +} + /* * Contains the tracking data for NUMA nodes having EPC pages. Most import= antly, * the free page list local to the node is stored here. @@ -58,6 +79,9 @@ struct sgx_numa_node { spinlock_t lock; }; =20 +extern nodemask_t sgx_numa_mask; +extern struct sgx_numa_node *sgx_numa_nodes; + /* * The firmware can define multiple chunks of EPC to the different areas o= f the * physical memory e.g. for memory areas of the each node. This structure = is diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h index b4119869b0d1..df88e1ff9877 100644 --- a/include/linux/misc_cgroup.h +++ b/include/linux/misc_cgroup.h @@ -48,6 +48,7 @@ struct misc_res_ops { * @watermark: Historical maximum usage of the resource. * @usage: Current usage of the resource. * @events: Number of times, the resource limit exceeded. + * @priv: resource specific data. */ struct misc_res { u64 max; @@ -55,6 +56,7 @@ struct misc_res { atomic64_t usage; atomic64_t events; atomic64_t events_local; + void *priv; }; =20 /** --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 4B8B61BD001; Fri, 30 Aug 2024 16:41:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036102; cv=none; b=R1nVR7pNfQGrr+LIprZ7p/k99ozLpnwV7AtMMlwTmcmV4SwNTifsZrFUXkFUyjCn+Hvw1OJuwRATT/AgpMzZzWpQ4VIZehwoYMuW96qZDSl2nFj2RYO8gO1z2CyRES/T4Kfm6h38QjKB4ILQL1PuonGDo8ZeawkQLcnAnC5pCnU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036102; c=relaxed/simple; bh=BaZE4tbPVRUjlkLgAvEb+bxaQQNPjPs/SFIPFgoiTkA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Djx9DUFzIoaAT61SZ8MkVp+d7tjQMgSs3OhcfWu+oFaFNoYh7rfLRuIAUgeO6ba3haak3U3l+Zs19XQR46kWJ1j74QCKxofh8JkBKbWdvzIATIBvfIq5bNMXjHUBzPljh8kkCAu+odGYnqR0cFHO0Mz7SqmzIkZ5Eq2AJ+vtG+o= 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=JfnI5e8V; arc=none smtp.client-ip=192.198.163.11 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="JfnI5e8V" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036100; x=1756572100; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BaZE4tbPVRUjlkLgAvEb+bxaQQNPjPs/SFIPFgoiTkA=; b=JfnI5e8VfaZsS1Kvkid7eFvAUtrPSk6VVvaiMZC7avx7vJw7Pi60ra6D ZZyh1wk0r8MJ2yVJLYoy9mfNLMouDMhtYJqsXfQRMhlOtDa00bIZn9+rk KjSNYdaS9XB66PX4tBvJ0rpYrju3g8DQF8B3wfGvFlb+uJnPOkGztCU09 leV5/EIUb4txgUb9Oi7XIpOWVW5xGeK5Abt3VtVkmDVjca0rCjqk17EeM begt+Yfl2yU/b6ZSBk3fbJFKhAIxDw2mUr0u/TnzqA7oWbWCzoRt/qlqC hjoeIiSxW3HUkkT7mHnA0Go2qPRgsG81qG5INPLz3hfjnXNOACS35vpBc A==; X-CSE-ConnectionGUID: PGb420aoQZuR+tXOIlX8/Q== X-CSE-MsgGUID: T/Pv0TevQTKIOY9mxG8jXw== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34300037" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34300037" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: zk7Al/wCSU6yfJfrofns4w== X-CSE-MsgGUID: ZML4hZeMS0Wy7X0VKG6qXw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440467" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 06/16] x86/sgx: Add sgx_epc_lru_list to encapsulate LRU list Date: Fri, 30 Aug 2024 09:40:27 -0700 Message-ID: <20240830164038.39343-7-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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 --- V17: - Make comment for spinlock more accurate. V15: - Add comment for spinlock. (Jarkko, Kai) 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 | 16 ++++++++++++++ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 395481e9ba55..b009d5bea40f 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); =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 @@ -306,13 +305,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 @@ -324,7 +323,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]; @@ -347,9 +346,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 @@ -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); } =20 /* @@ -432,6 +431,8 @@ static bool __init sgx_page_reclaimer_init(void) =20 ksgxd_tsk =3D tsk; =20 + sgx_lru_init(&sgx_global_lru); + return true; } =20 @@ -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 |=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 /** @@ -525,18 +526,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; } @@ -578,7 +579,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 c5208da7c8eb..5a575a9427e7 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -117,6 +117,22 @@ 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 { + /* Lock for concurrent access to @reclaimable. */ + 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.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 6B6831BDA90; Fri, 30 Aug 2024 16:41:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036104; cv=none; b=Sz57PbYT1KXKnZhFHs2qpSTx1DMhObvXOrsRGVudmjkV3MAZNeKMDMuAz6xpoMaZZ9I5PWZqOjR2AZMrfNsoUWZ5sNp/U0ewNXxqV31Ns/4LttzCiSBOSFLXKz97EYB1gARTov2GpNhyyUHgzjBttsduL6Zj0BetmScciI9TQ1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036104; c=relaxed/simple; bh=G1ez0Ej4oKwyZ0VpIOg/yXt53MVqG5+NKbA51DFw3xY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JvkmYYANkc/3Yl0dZkfBWJe85d3H9A20HGFX3sTB6iFDmfXtj/5MRR4iE1iMxhNvW3l+atnvc5SwwB9WmzKdDmxS9gt292/jH4v1n777NlnrPaLqZn97wNtDThovYaZVgTRI3WAYjsn22rgXsyCyNkFFmvYUEFfebTbWBZIq04E= 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=aE1nNgil; arc=none smtp.client-ip=192.198.163.11 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="aE1nNgil" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036102; x=1756572102; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=G1ez0Ej4oKwyZ0VpIOg/yXt53MVqG5+NKbA51DFw3xY=; b=aE1nNgilP6KRhQ1qxkFi4u2QiOxTrTWMqDhoTdhfgE9qJop8OX/2F8Kb ZqauniIuxzb7EUWNa5xpajYPUhfPV2WpLp0m5rKPEuwpQPc7aPe/E38k7 BOyDNXoU77Dxlbkg11uKCVRXLsvLl88mlUiiNJsRd1xIExs2vLiMQaKFI MfCp4Tw0SxK6JKkU6DzcKeyoAnPyA14V99A3vB/QPuYUYGuI6UhWCUpAw 9No0/t8QlsEZQEPdHdtKdny6wUgp3WpY2VtbSekQbaq/phodrs+BSkh4c gMpxsr98QJF5s2kYrb7327zCiDaiQ6iWV7f0sW00bvehr6jaldZLnpYkb g==; X-CSE-ConnectionGUID: JZmQSxqDQ2K4p25mvf0NKQ== X-CSE-MsgGUID: o9c4MnuYSPGy5OmLY7VV2Q== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34300043" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34300043" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: NYJR4U6mTQ+uicDwnoYM+Q== X-CSE-MsgGUID: GD3GZ4cHTZmlcsQbqQn4sg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440470" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 07/16] x86/sgx: Abstract tracking reclaimable pages in LRU Date: Fri, 30 Aug 2024 09:40:28 -0700 Message-ID: <20240830164038.39343-8-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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: Kristen Carlson Accardi The SGX driver tracks reclaimable EPC pages by adding a newly allocated page into the global LRU list in sgx_mark_page_reclaimable(), and doing the opposite in sgx_unmark_page_reclaimable(). To support SGX EPC cgroup, the SGX driver will need to maintain an LRU list for each cgroup, and each newly allocated EPC page will need to be added to the LRU of associated cgroup, not always the global LRU list. When sgx_mark_page_reclaimable() is called, the cgroup that the newly allocated EPC page belongs to is already known, i.e., it has been set to the 'struct sgx_epc_page'. Add a helper, sgx_epc_page_lru(), to return the LRU that the EPC page should be added to for the given EPC page. Currently it just returns the global LRU. Change sgx_{mark|unmark}_page_reclaimable() to use the helper function to get the LRU from the EPC page instead of referring to the global LRU directly. This allows EPC page being able to be tracked in "per-cgroup" LRU when that becomes ready. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Reviewed-by: Jarkko Sakkinen Reviewed-by: Kai Huang Tested-by: Jarkko Sakkinen --- V13: - Revise commit log (Kai) - Rename sgx_lru_list() to sgx_epc_page_lru() V7: - Split this out from the big patch, #10 in V6. (Dave, Kai) --- arch/x86/kernel/cpu/sgx/main.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index b009d5bea40f..bfbdb3247761 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -32,6 +32,11 @@ static DEFINE_XARRAY(sgx_epc_address_space); */ static struct sgx_epc_lru_list sgx_global_lru; =20 +static inline struct sgx_epc_lru_list *sgx_epc_page_lru(struct sgx_epc_pag= e *epc_page) +{ + return &sgx_global_lru; +} + static atomic_long_t sgx_nr_free_pages =3D ATOMIC_LONG_INIT(0); =20 /* Nodes with one or more EPC sections. */ @@ -500,25 +505,24 @@ static struct sgx_epc_page *__sgx_alloc_epc_page(void) } =20 /** - * sgx_mark_page_reclaimable() - Mark a page as reclaimable + * sgx_mark_page_reclaimable() - Mark a page as reclaimable and track it i= n a LRU. * @page: EPC page - * - * Mark a page as reclaimable and add it to the active page list. Pages - * are automatically removed from the active list when freed. */ void sgx_mark_page_reclaimable(struct sgx_epc_page *page) { - spin_lock(&sgx_global_lru.lock); + struct sgx_epc_lru_list *lru =3D sgx_epc_page_lru(page); + + spin_lock(&lru->lock); page->flags |=3D SGX_EPC_PAGE_RECLAIMER_TRACKED; - list_add_tail(&page->list, &sgx_global_lru.reclaimable); - spin_unlock(&sgx_global_lru.lock); + list_add_tail(&page->list, &lru->reclaimable); + spin_unlock(&lru->lock); } =20 /** - * sgx_unmark_page_reclaimable() - Remove a page from the reclaim list + * sgx_unmark_page_reclaimable() - Remove a page from its tracking LRU * @page: EPC page * - * Clear the reclaimable flag and remove the page from the active page lis= t. + * Clear the reclaimable flag if set and remove the page from its LRU. * * Return: * 0 on success, @@ -526,18 +530,20 @@ void sgx_mark_page_reclaimable(struct sgx_epc_page *p= age) */ int sgx_unmark_page_reclaimable(struct sgx_epc_page *page) { - spin_lock(&sgx_global_lru.lock); + struct sgx_epc_lru_list *lru =3D sgx_epc_page_lru(page); + + spin_lock(&lru->lock); if (page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) { /* The page is being reclaimed. */ if (list_empty(&page->list)) { - spin_unlock(&sgx_global_lru.lock); + spin_unlock(&lru->lock); return -EBUSY; } =20 list_del(&page->list); page->flags &=3D ~SGX_EPC_PAGE_RECLAIMER_TRACKED; } - spin_unlock(&sgx_global_lru.lock); + spin_unlock(&lru->lock); =20 return 0; } --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 424381B5820; Fri, 30 Aug 2024 16:41:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036095; cv=none; b=gHZKgOFqrGMNYwDsi2VbLZxdCBVyOZlfohfO0Ohh9d59SH8dmz9N47qCGDLF5tZI9qlq8TuEO/YsNWw9uUeIvZKY6gO6lykS3Zkv3pXAOYNlZMU4nm8U/RRdo8nZhVRXVoo1hVfi6S+6eVx9en+4DwQY4+yXsss79w/JPyKulRw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036095; c=relaxed/simple; bh=/lbtpIyXtuTFQtNRAZhbpbZmEUcgbTF0BGfZ94D+zjU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Fdx2gp6GdBrT2y6o+Kkex7QUaY7YgZvm1514o9jIkToFZGqQhZYtP9AQfQ+B7DWnxrcum3f2ktLIQ/YCrfck05WABJvUSbWQ4558DLKJLCh5GCP4Fcz2GSrwVVU0Yeu/69XL8M7N+AMo7GiQMVDotM/BjRglHKIzM8MOMDLOPKI= 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=MP5bqCxV; arc=none smtp.client-ip=192.198.163.11 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="MP5bqCxV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036093; x=1756572093; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/lbtpIyXtuTFQtNRAZhbpbZmEUcgbTF0BGfZ94D+zjU=; b=MP5bqCxVmSEf5T47gN4DHOqTsEyW1t0Obk+RUS1wTct2/qoe/aMNu0GB 2CzIBSGiBI+DTk9glQboz3PvOxzG+Paceo3iTVQeGrxWKoLMHR9p8Samk Uqnl6+fcBBAm8i95krKZEKLQkbj4fENh8YEqeQSs8C+TRMq0fCj9G8HlV BSipPZ2KI87RfXYV1VNsu/aZu+BNQDkNhtNYcz2pK830gOBnWxzY+x1oX nZ5JY3Ty5Ei+qhEywbm0rREhqbga7Iz+rh7riLQ9sOmLQA9RQRiDppx7h Tp/uwqWsNgPCza5UHTp5NwrUNIbsjaDJecIlRCYZqAmaV0yji93eLk6lT A==; X-CSE-ConnectionGUID: aFaHWeDLSgSrM1XgvYBNQw== X-CSE-MsgGUID: SLnL7oMzT9+7kWzfioQnzg== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34299936" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34299936" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: zRrrewg9QfeJTnA7Z5BuXA== X-CSE-MsgGUID: nzCvf2pESjSufhst/XkCIA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440473" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 08/16] x86/sgx: Encapsulate uses of the global LRU Date: Fri, 30 Aug 2024 09:40:29 -0700 Message-ID: <20240830164038.39343-9-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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" To support the per-cgroup reclamation, each cgroup will have its own "per-cgroup LRU" and EPC pages will be in its owner cgroup's LRU instead of the global LRU. Abstract the code that is directly working with the global LRU into functions reusable with per-cgroup LRUs. Currently the basic reclamation procedure, sgx_reclaim_pages() directly reclaims pages from the global LRU. Change it to take in an LRU. Note the global EPC reclamation will still be needed when the total EPC usage reaches the system capacity while usages of some cgroups are below their respective limits. Create a separate wrapper for the global reclamation, sgx_reclaim_pages_global(), passing in the global LRU to the new sgx_reclaim_pages() now. Later it will be revised to reclaim from multiple LRUs from all EPC cgroups instead of a single global LRU. Wrap the existing emptiness check of the global LRU with a helper so that it can be changed later to work with multiple LRUs when per-cgroup LRU comes to play. Also the per-cgroup EPC reclaim and global EPC reclaim will have different check on whether they should be done. Rename the existing sgx_should_reclaim() to sgx_should_reclaim_global() to separate the two cases. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Co-developed-by: Kristen Carlson Accardi Signed-off-by: Kristen Carlson Accardi Signed-off-by: Haitao Huang Reviewed-by: Kai Huang Reviewed-by: Jarkko Sakkinen --- V17: - remove comments in sgx_can_reclaim_global(), move it to later patch. (Kai) v16: - Regroup all abstraction related to global LRU usage to this patch from different patches in previous version. Position this before adding per-cgroup reclaim. (Kai) V13: - Rename sgx_can_reclaim() to sgx_can_reclaim_global() and sgx_should_reclaim() to sgx_should_reclaim_global(). (Kai) V10: - Add comments for the new function. (Jarkko) V7: - Split this out from the big patch, #10 in V6. (Dave, Kai) --- arch/x86/kernel/cpu/sgx/main.c | 56 +++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index bfbdb3247761..5c2c3940c234 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -37,6 +37,14 @@ static inline struct sgx_epc_lru_list *sgx_epc_page_lru(= struct sgx_epc_page *epc return &sgx_global_lru; } =20 +/* + * Check if there is any reclaimable page at global level. + */ +static inline bool sgx_can_reclaim_global(void) +{ + return !list_empty(&sgx_global_lru.reclaimable); +} + static atomic_long_t sgx_nr_free_pages =3D ATOMIC_LONG_INIT(0); =20 /* Nodes with one or more EPC sections. */ @@ -287,10 +295,10 @@ static void sgx_reclaimer_write(struct sgx_epc_page *= epc_page, } =20 /* - * Take a fixed number of pages from the head of the active page pool and - * reclaim them to the enclave's private shmem files. Skip the pages, whic= h have - * been accessed since the last scan. Move those pages to the tail of acti= ve - * page pool so that the pages get scanned in LRU like fashion. + * Take a fixed number of pages from the head of a given LRU and reclaim t= hem to + * the enclave's private shmem files. Skip the pages, which have been acce= ssed + * since the last scan. Move those pages to the tail of the list so that t= he + * pages get scanned in LRU like fashion. * * Batch process a chunk of pages (at the moment 16) in order to degrade a= mount * of IPI's and ETRACK's potentially required. sgx_encl_ewb() does degrade= a bit @@ -299,7 +307,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *ep= c_page, * problematic as it would increase the lock contention too much, which wo= uld * halt forward progress. */ -static void sgx_reclaim_pages(void) +static void sgx_reclaim_pages(struct sgx_epc_lru_list *lru) { struct sgx_epc_page *chunk[SGX_NR_TO_SCAN]; struct sgx_backing backing[SGX_NR_TO_SCAN]; @@ -310,10 +318,9 @@ static void sgx_reclaim_pages(void) int ret; int i; =20 - spin_lock(&sgx_global_lru.lock); + spin_lock(&lru->lock); for (i =3D 0; i < SGX_NR_TO_SCAN; i++) { - epc_page =3D list_first_entry_or_null(&sgx_global_lru.reclaimable, - struct sgx_epc_page, list); + epc_page =3D list_first_entry_or_null(&lru->reclaimable, struct sgx_epc_= page, list); if (!epc_page) break; =20 @@ -328,7 +335,7 @@ static void sgx_reclaim_pages(void) */ epc_page->flags &=3D ~SGX_EPC_PAGE_RECLAIMER_TRACKED; } - spin_unlock(&sgx_global_lru.lock); + spin_unlock(&lru->lock); =20 for (i =3D 0; i < cnt; i++) { epc_page =3D chunk[i]; @@ -351,9 +358,9 @@ static void sgx_reclaim_pages(void) continue; =20 skip: - spin_lock(&sgx_global_lru.lock); - list_add_tail(&epc_page->list, &sgx_global_lru.reclaimable); - spin_unlock(&sgx_global_lru.lock); + spin_lock(&lru->lock); + list_add_tail(&epc_page->list, &lru->reclaimable); + spin_unlock(&lru->lock); =20 kref_put(&encl_page->encl->refcount, sgx_encl_release); =20 @@ -381,10 +388,15 @@ static void sgx_reclaim_pages(void) } } =20 -static bool sgx_should_reclaim(unsigned long watermark) +static bool sgx_should_reclaim_global(unsigned long watermark) { return atomic_long_read(&sgx_nr_free_pages) < watermark && - !list_empty(&sgx_global_lru.reclaimable); + sgx_can_reclaim_global(); +} + +static void sgx_reclaim_pages_global(void) +{ + sgx_reclaim_pages(&sgx_global_lru); } =20 /* @@ -394,8 +406,8 @@ static bool sgx_should_reclaim(unsigned long watermark) */ void sgx_reclaim_direct(void) { - if (sgx_should_reclaim(SGX_NR_LOW_PAGES)) - sgx_reclaim_pages(); + if (sgx_should_reclaim_global(SGX_NR_LOW_PAGES)) + sgx_reclaim_pages_global(); } =20 static int ksgxd(void *p) @@ -415,10 +427,10 @@ static int ksgxd(void *p) =20 wait_event_freezable(ksgxd_waitq, kthread_should_stop() || - sgx_should_reclaim(SGX_NR_HIGH_PAGES)); + sgx_should_reclaim_global(SGX_NR_HIGH_PAGES)); =20 - if (sgx_should_reclaim(SGX_NR_HIGH_PAGES)) - sgx_reclaim_pages(); + if (sgx_should_reclaim_global(SGX_NR_HIGH_PAGES)) + sgx_reclaim_pages_global(); =20 cond_resched(); } @@ -585,7 +597,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, en= um sgx_reclaim reclaim) break; } =20 - if (list_empty(&sgx_global_lru.reclaimable)) { + if (!sgx_can_reclaim_global()) { page =3D ERR_PTR(-ENOMEM); break; } @@ -600,7 +612,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, en= um sgx_reclaim reclaim) break; } =20 - sgx_reclaim_pages(); + sgx_reclaim_pages_global(); cond_resched(); } =20 @@ -613,7 +625,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, en= um sgx_reclaim reclaim) sgx_put_cg(sgx_cg); } =20 - if (sgx_should_reclaim(SGX_NR_LOW_PAGES)) + if (sgx_should_reclaim_global(SGX_NR_LOW_PAGES)) wake_up(&ksgxd_waitq); =20 return page; --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 26D0C1B8E91; Fri, 30 Aug 2024 16:41:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036097; cv=none; b=CW+2ZLCebxdmh9vePf8tUOdPZlWBurkSeN4T5IWpe8pwlGk9eRuMbFazlhfxQjYnlFawC9hmP1YQzkSCviNrZMJBwCIxWfw5U4ucSwQRcrY45RQ3D4+d1qGHHcrO92F8kuLOm3+UDWx4bzRi/f7Zs9xjkUWs1CP7ZeN0k/nHN64= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036097; c=relaxed/simple; bh=PSe/WDzO15CmV9p2LpeTU07NC/nFu52QGiBAEcvQjnQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=m//ufL2D8eQ0lYTtVc5K8belhEdmYFZKuqMxmcbzVBeuHFR4AwNDA60WOCc+XimJ0R2tUtS/3Cv8LdO6GlhrvMeoVhdtwkBssXXkN2/jlE9cFcT2j72mwKzppVmNZoXMdo/HR+5kNCyE+1CaVW8kAgLJ7BjyVHfiF/ZweO96aB8= 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=J6A5EonN; arc=none smtp.client-ip=192.198.163.11 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="J6A5EonN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036095; x=1756572095; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PSe/WDzO15CmV9p2LpeTU07NC/nFu52QGiBAEcvQjnQ=; b=J6A5EonN3B1y5mVQEaaHjgBs9wb8cCKM/fNJV238S2C4iQz3FrHwWJl5 GObDfOO3a2OoWB5KGlogRQTeI/Psc0r8h+HAr0/GMBUY2INkRxFXu6ofn /NOlOsqwlZmvKlMBru7hz8CQRdIa//HzBUHf824vKWraqRG19aoSRJ9iW xXuac2mQY6BdfXAbfbpaY+0/zJsWC5oT6MYf0kHnkNnyM7Hi0maxeKiVr +Sf9PpHPwCF/MoO4XtxMYtxz6iKClwvCi6kFvm671D4wAokJAIk70HKj3 WLYBueStd/EbGIqRA9ziu3cU5eIINUAf7BdrKI1aOPG1kqTRtlVbUYmbc Q==; X-CSE-ConnectionGUID: JnLYrc2pRLOp6FFmCbHrpw== X-CSE-MsgGUID: CoPeEm7kTn6DrpW8P6z6hw== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34299966" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34299966" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: rdZkMM25QzSVoOKLqj9ayw== X-CSE-MsgGUID: ZzWOYqPvQVmRBOab6K6dtQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440477" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 09/16] x86/sgx: Add basic EPC reclamation flow for cgroup Date: Fri, 30 Aug 2024 09:40:30 -0700 Message-ID: <20240830164038.39343-10-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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" Currently in the EPC page allocation, the kernel simply fails the allocation when the current EPC cgroup fails to charge due to its usage reaching limit. This is not ideal. When that happens, a better way is to reclaim EPC page(s) from the current EPC cgroup to reduce its usage so the new allocation can succeed. Currently, all EPC pages are tracked in a single global LRU, and the "global EPC reclamation" supports the following 3 cases: 1) On-demand asynchronous reclamation: For allocation requests that can not wait for reclamation but can be retried, an asynchronous reclamation is triggered, in which the global reclaimer, ksgxd, keeps reclaiming EPC pages until the free page count is above a minimal threshold. 2) On-demand synchronous reclamation: For allocations that can wait for reclamation, the EPC page allocator, sgx_alloc_epc_page() reclaims EPC page(s) immediately until at least one free page is available for allocation. 3) Preemptive reclamation: For some allocation requests, e.g., allocation for reloading a reclaimed page to change its permissions or page type, the kernel invokes sgx_reclaim_direct() to preemptively reclaim EPC page(s) as a best effort to minimize on-demand reclamation for subsequent allocations. Similarly, a "per-cgroup reclamation" is needed to support the above 3 cases as well: 1) For on-demand asynchronous reclamation, a per-cgroup reclamation needs to be invoked to maintain a minimal difference between the usage and the limit for each cgroup, analogous to the minimal free page threshold maintained by the global reclaimer. 2) For on-demand synchronous reclamation, sgx_cgroup_try_charge() needs to invoke the per-cgroup reclamation until the cgroup usage becomes at least one page lower than its limit. 3) For preemptive reclamation, sgx_reclaim_direct() needs to invoke the per-cgroup reclamation to minimize per-cgroup on-demand reclamation for subsequent allocations. To support the per-cgroup reclamation, introduce a "per-cgroup LRU" to track all EPC pages belong to the owner cgroup to utilize the existing sgx_reclaim_pages(). Currently, the global reclamation treats all EPC pages equally as it scans all EPC pages in FIFO order in the global LRU. The "per-cgroup reclamation" needs to somehow achieve the same fairness of all EPC pages that are tracked in the multiple LRUs of the given cgroup and all the descendants to reflect the nature of the cgroup. The idea is to achieve such fairness by scanning "all EPC cgroups" of the subtree (the given cgroup and all the descendants) equally in turns, and in the scan to each cgroup, apply the existing sgx_reclaim_pages() to its LRU. This basic flow is encapsulated in a new function, sgx_cgroup_reclaim_pages(). Expose sgx_reclaim_pages() for use in sgx_cgroup_reclaim_pages(). And modify sgx_reclaim_pages() to return the number of pages scanned so sgx_cgroup_reclaim_pages() can track scanning progress and determine whether enough scanning is done or to continue the scanning for next descendant. Whenever reclaiming in a subtree of a given root is needed, start the scanning from the next descendant where scanning was stopped at last time. To keep track of the next descendant cgroup to scan, add a new field, next_cg, in the sgx_cgroup struct. Create an iterator function, sgx_cgroup_next_get(), atomically returns a valid reference of the descendant for next round of scanning and advances @next_cg to next valid descendant in a preorder walk. This iterator function is used in sgx_cgroup_reclaim_pages() to iterate descendants for scanning. Separately also advances @next_cg to next valid descendant when the cgroup referenced by @next_cg is to be freed. Add support for on-demand synchronous reclamation in sgx_cgroup_try_charge(), applying sgx_cgroup_reclaim_pages() iteratively until cgroup usage is lower than its limit. Later patches will reuse sgx_cgroup_reclaim_pages() to add support for asynchronous and preemptive reclamation. Note all reclaimable EPC pages are still tracked in the global LRU thus no per-cgroup reclamation is actually active at the moment: -ENOMEM is returned by __sgx_cgroup_try_charge() when LRUs are empty. Per-cgroup tracking and reclamation will be turned on in the end after all necessary infrastructure is in place. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Co-developed-by: Kristen Carlson Accardi Signed-off-by: Kristen Carlson Accardi Signed-off-by: Haitao Huang Reviewed-by: Kai Huang Reviewed-by: Jarkko Sakkinen --- V17: - Fix grammar in commit and improve comments.(Kai) - Fix error in initializing 'next' in sgx_cgroup_next_descendant_pre() V16: - Revise commit log to define reclamation requirement and the design more c= learly. (Kai) - Revise sgx_cgroup_reclaim_pages() to scan cgroups in subtree more fairly, track next_cg in each sgx_cgroup and add helpers that is used to iterate descendant in sgx_cgroup_reclaim_pages().(Kai) V14: - Allow sgx_cgroup_reclaim_pages() to continue from previous tree-walk. It takes in a 'start' node and returns the 'next' node for the caller to use as the new 'start'. This is to ensure pages in lower level cgroups can be reclaimed if all pages in upper level nodes are "too young". (Kai) - Move renaming sgx_should_reclaim() to sgx_should_reclaim_global() from a later patch to this one. (Kai) V11: - Use commit message suggested by Kai - Remove "usage" comments for functions. (Kai) V10: - Simplify the signature by removing a pointer to nr_to_scan (Kai) - Return pages attempted instead of reclaimed as it is really what the cgroup caller needs to track progress. This further simplifies the design. - Merge patch for exposing sgx_reclaim_pages() with basic synchronous reclamation. (Kai) - Shorten names for EPC cgroup functions. (Jarkko) - Fix/add comments to justify the design (Kai) - Separate out a helper for for addressing single iteration of the loop in sgx_cgroup_try_charge(). (Jarkko) V9: - Add comments for static variables. (Jarkko) V8: - Use width of 80 characters in text paragraphs. (Jarkko) - Remove alignment for substructure variables. (Jarkko) V7: - Reworked from patch 9 of V6, "x86/sgx: Restructure top-level EPC reclaim function". Do not split the top level function (Kai) - Dropped patches 7 and 8 of V6. - Split this out from the big patch, #10 in V6. (Dave, Kai) --- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 208 ++++++++++++++++++++++++++- arch/x86/kernel/cpu/sgx/epc_cgroup.h | 18 ++- arch/x86/kernel/cpu/sgx/main.c | 19 ++- arch/x86/kernel/cpu/sgx/sgx.h | 1 + 4 files changed, 238 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c index b219ec21c637..abbefa9e21d5 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -7,17 +7,203 @@ /* The root SGX EPC cgroup */ static struct sgx_cgroup sgx_cg_root; =20 +/* + * Return the next descendant in a preorder walk, given a root, @root and a + * cgroup, @cg, to start the walk from. Return @root if no descendant left= for + * this walk. Otherwise, return next descendant with its refcnt incremente= d. + */ +static struct sgx_cgroup *sgx_cgroup_next_descendant_pre(struct sgx_cgroup= *root, + struct sgx_cgroup *cg) +{ + struct cgroup_subsys_state *next =3D &cg->cg->css; + + rcu_read_lock(); + for (;;) { + next =3D css_next_descendant_pre(next, &root->cg->css); + if (!next) { + next =3D &root->cg->css; + break; + } + + if (css_tryget(next)) + break; + } + rcu_read_unlock(); + + return sgx_cgroup_from_misc_cg(css_misc(next)); +} + +/* + * For a given root, @root, if a given cgroup, @cg, is the next cgroup to + * reclaim pages from, i.e., referenced by @root->next_cg, then advance + * @root->next_cg to the next valid cgroup in a preorder walk or the root = if no + * more descendants left to walk. + * + * Called from sgx_cgroup_free() when @cg is to be freed so it can no long= er be + * used as 'next_cg'. + */ +static inline void sgx_cgroup_next_skip(struct sgx_cgroup *root, struct sg= x_cgroup *cg) +{ + struct sgx_cgroup *p; + + spin_lock(&root->next_cg_lock); + p =3D root->next_cg; + spin_unlock(&root->next_cg_lock); + + /* Already moved by other threads, no need to update */ + if (cg !=3D p) + return; + + p =3D sgx_cgroup_next_descendant_pre(root, cg); + + spin_lock(&root->next_cg_lock); + if (root->next_cg =3D=3D cg) + root->next_cg =3D p; + spin_unlock(&root->next_cg_lock); + + /* Decrement refcnt so cgroup pointed to by p can be released */ + if (p !=3D cg && p !=3D root) + sgx_put_cg(p); +} + +/* + * Return the cgroup currently referenced by @root->next_cg and advance + * @root->next_cg to the next descendant or @root. The returned cgroup ha= s its + * refcnt incremented if it is not @root and caller must release the refcn= t. + */ +static inline struct sgx_cgroup *sgx_cgroup_next_get(struct sgx_cgroup *ro= ot) +{ + struct sgx_cgroup *p; + + /* + * Acquire a reference for the to-be-returned cgroup and advance + * next_cg with the lock so the same cg not returned to two threads. + */ + spin_lock(&root->next_cg_lock); + + p =3D root->next_cg; + + /* Advance the to-be-returned to next descendant if current one is dying = */ + if (p !=3D root && !css_tryget(&p->cg->css)) + p =3D sgx_cgroup_next_descendant_pre(root, p); + + /* Advance next_cg */ + root->next_cg =3D sgx_cgroup_next_descendant_pre(root, p); + + /* Decrement ref here so it can be released by cgroup subsystem */ + if (root->next_cg !=3D root) + sgx_put_cg(root->next_cg); + + spin_unlock(&root->next_cg_lock); + + /* p is root or refcnt incremented */ + return p; +} + /** - * sgx_cgroup_try_charge() - try to charge cgroup for a single EPC page + * sgx_cgroup_lru_empty() - check if a cgroup tree has no pages on its LRUs + * @root: Root of the tree to check * + * Return: %true if all cgroups under the specified root have empty LRU li= sts. + */ +static bool sgx_cgroup_lru_empty(struct misc_cg *root) +{ + struct cgroup_subsys_state *css_root; + struct cgroup_subsys_state *pos; + struct sgx_cgroup *sgx_cg; + bool ret =3D true; + + /* + * Caller must ensure css_root ref acquired + */ + css_root =3D &root->css; + + rcu_read_lock(); + css_for_each_descendant_pre(pos, css_root) { + if (!css_tryget(pos)) + continue; + + rcu_read_unlock(); + + sgx_cg =3D sgx_cgroup_from_misc_cg(css_misc(pos)); + + spin_lock(&sgx_cg->lru.lock); + ret =3D list_empty(&sgx_cg->lru.reclaimable); + spin_unlock(&sgx_cg->lru.lock); + + rcu_read_lock(); + css_put(pos); + if (!ret) + break; + } + + rcu_read_unlock(); + + return ret; +} + +/* + * Scan at least @nr_to_scan pages and attempt to reclaim them from the su= btree of @root. + */ +static inline void sgx_cgroup_reclaim_pages(struct sgx_cgroup *root, + unsigned int nr_to_scan) +{ + struct sgx_cgroup *next_cg =3D NULL; + unsigned int cnt =3D 0; + + while (!sgx_cgroup_lru_empty(root->cg) && cnt < nr_to_scan) { + next_cg =3D sgx_cgroup_next_get(root); + cnt +=3D sgx_reclaim_pages(&next_cg->lru); + if (next_cg !=3D root) + sgx_put_cg(next_cg); + } +} + +static int __sgx_cgroup_try_charge(struct sgx_cgroup *epc_cg) +{ + if (!misc_cg_try_charge(MISC_CG_RES_SGX_EPC, epc_cg->cg, PAGE_SIZE)) + return 0; + + /* No reclaimable pages left in the cgroup */ + if (sgx_cgroup_lru_empty(epc_cg->cg)) + return -ENOMEM; + + if (signal_pending(current)) + return -ERESTARTSYS; + + return -EBUSY; +} + +/** + * sgx_cgroup_try_charge() - try to charge cgroup for a single EPC page * @sgx_cg: The EPC cgroup to be charged for the page. + * @reclaim: Whether or not synchronous EPC reclaim is allowed. * Return: * * %0 - If successfully charged. * * -errno - for failures. */ -int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg) +int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim recl= aim) { - return misc_cg_try_charge(MISC_CG_RES_SGX_EPC, sgx_cg->cg, PAGE_SIZE); + int ret; + + for (;;) { + ret =3D __sgx_cgroup_try_charge(sgx_cg); + + if (ret !=3D -EBUSY) + goto out; + + if (reclaim =3D=3D SGX_NO_RECLAIM) { + ret =3D -ENOMEM; + goto out; + } + + sgx_cgroup_reclaim_pages(sgx_cg, 1); + + cond_resched(); + } + +out: + return ret; } =20 /** @@ -32,18 +218,34 @@ void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg) static void sgx_cgroup_free(struct misc_cg *cg) { struct sgx_cgroup *sgx_cg; + struct misc_cg *p; =20 sgx_cg =3D sgx_cgroup_from_misc_cg(cg); if (!sgx_cg) return; =20 + /* + * Notify ancestors to not reclaim from this dying cgroup. + * Not start from this cgroup itself because at this point no reference + * of this cgroup being hold, i.e., all pages in this cgroup are freed + * and LRU is empty, so no reclamation possible. + */ + p =3D misc_cg_parent(cg); + while (p) { + sgx_cgroup_next_skip(sgx_cgroup_from_misc_cg(p), sgx_cg); + p =3D misc_cg_parent(p); + } + kfree(sgx_cg); } =20 static void sgx_cgroup_misc_init(struct misc_cg *cg, struct sgx_cgroup *sg= x_cg) { + sgx_lru_init(&sgx_cg->lru); cg->res[MISC_CG_RES_SGX_EPC].priv =3D sgx_cg; sgx_cg->cg =3D cg; + sgx_cg->next_cg =3D sgx_cg; + spin_lock_init(&sgx_cg->next_cg_lock); } =20 static int sgx_cgroup_alloc(struct misc_cg *cg) diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx= /epc_cgroup.h index 52c9faf31fa1..879d5a4a2fcd 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.h +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h @@ -20,7 +20,7 @@ static inline struct sgx_cgroup *sgx_get_current_cg(void) =20 static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg) { } =20 -static inline int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg) +static inline int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sg= x_reclaim reclaim) { return 0; } @@ -33,6 +33,20 @@ static inline void __init sgx_cgroup_init(void) { } =20 struct sgx_cgroup { struct misc_cg *cg; + struct sgx_epc_lru_list lru; + /* + * Pointer to the next cgroup to scan when the per-cgroup reclamation + * is triggered next time. It does not hold a reference to prevent it + * from being freed in order to allow the misc cgroup subsystem to + * release and free the cgroup as needed, e.g., when admin wants to + * delete the cgroup. When the cgroup pointed to is being freed, + * sgx_cgroup_next_cg_skip(), will be invoked to update the pointer to + * next accessible cgroup in a preorder walk of the subtree of the same + * root. + */ + struct sgx_cgroup *next_cg; + /* Lock to protect concurrent access to @next_cg */ + spinlock_t next_cg_lock; }; =20 static inline struct sgx_cgroup *sgx_cgroup_from_misc_cg(struct misc_cg *c= g) @@ -63,7 +77,7 @@ static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg) put_misc_cg(sgx_cg->cg); } =20 -int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg); +int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim recl= aim); void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg); void __init sgx_cgroup_init(void); =20 diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 5c2c3940c234..d41cf6145975 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -294,7 +294,10 @@ static void sgx_reclaimer_write(struct sgx_epc_page *e= pc_page, mutex_unlock(&encl->lock); } =20 -/* +/** + * sgx_reclaim_pages() - Attempt to reclaim a fixed number of pages from a= n LRU + * @lru: The LRU from which pages are reclaimed. + * * Take a fixed number of pages from the head of a given LRU and reclaim t= hem to * the enclave's private shmem files. Skip the pages, which have been acce= ssed * since the last scan. Move those pages to the tail of the list so that t= he @@ -306,8 +309,10 @@ static void sgx_reclaimer_write(struct sgx_epc_page *e= pc_page, * + EWB) but not sufficiently. Reclaiming one page at a time would also be * problematic as it would increase the lock contention too much, which wo= uld * halt forward progress. + * + * Return: Number of pages attempted for reclamation. */ -static void sgx_reclaim_pages(struct sgx_epc_lru_list *lru) +unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list *lru) { struct sgx_epc_page *chunk[SGX_NR_TO_SCAN]; struct sgx_backing backing[SGX_NR_TO_SCAN]; @@ -386,6 +391,8 @@ static void sgx_reclaim_pages(struct sgx_epc_lru_list *= lru) =20 sgx_free_epc_page(epc_page); } + + return cnt; } =20 static bool sgx_should_reclaim_global(unsigned long watermark) @@ -584,7 +591,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, en= um sgx_reclaim reclaim) int ret; =20 sgx_cg =3D sgx_get_current_cg(); - ret =3D sgx_cgroup_try_charge(sgx_cg); + ret =3D sgx_cgroup_try_charge(sgx_cg, reclaim); if (ret) { sgx_put_cg(sgx_cg); return ERR_PTR(ret); @@ -612,6 +619,12 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, e= num sgx_reclaim reclaim) break; } =20 + /* + * At this point, the usage within this cgroup is under its + * limit but there is no physical page left for allocation. + * Perform a global reclaim to get some pages released from any + * cgroup with reclaimable pages. + */ sgx_reclaim_pages_global(); cond_resched(); } diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index 5a575a9427e7..710fff30182d 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -139,6 +139,7 @@ void sgx_reclaim_direct(void); void sgx_mark_page_reclaimable(struct sgx_epc_page *page); int sgx_unmark_page_reclaimable(struct sgx_epc_page *page); struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim recl= aim); +unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list *lru); =20 void sgx_ipi_cb(void *info); =20 --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 6B6351BDA88; Fri, 30 Aug 2024 16:41:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036104; cv=none; b=BrFkpf4l1spkxP95tnwJ0O2LN3WScFIJVDHgYc1XGJZvUsqEqGpHPI7h3Rnm7JPdbdqFL8prlHUIt9DWFe4bHd+ipCeA+R3txszX2sewZzJ9KKZnIiCep9fBvwAuWr4HkDsB3Tv9lVLv7XfzA5fuEwi2G/6j2K7pJyiOZ35EPQo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036104; c=relaxed/simple; bh=DFJqJfyqJgzpVWSIte70DFHcz7I0BjM4RG7nfiuqhbw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TPu/Ur0OqAwhA4SVBhvYqp2f2+ElUUq1vWsVsw9LqMg9IVmfVFfifJG9DMYEx+Ap+lSmGFJA5LN8rLWuY5u5pizC67x2lbVpgmRpAIeaDKYWzcx1/h87anCrX0vn2huEodssTcvETUnLeut7+tJsu9KUjLxRonO8IUBJd2zmqxk= 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=T+qRexDD; arc=none smtp.client-ip=192.198.163.11 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="T+qRexDD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036102; x=1756572102; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DFJqJfyqJgzpVWSIte70DFHcz7I0BjM4RG7nfiuqhbw=; b=T+qRexDDBm647/RhPSe1wsGMauykGGgb26OgdNcIkFQCCo6pvwzrgznm alQNFmmfB4in84Nsntw8yrvEGk3DuROQjHcU9clI/XVDHb1WwWoAh6zzB I6K8Qz+5lOBs+cTBmLDwfAGyAWuVoHPUqM7X7Z+BHUwyvvUDJZ0zQASRu exLAeHFU5vNLJj97PCLegqLsExTN5zYzUQOykr6WfOIPfvACq0jfbpf+C 3S4ndZFDPTBNvOPU0FLO01jPJdMLs+gL1bkymB8ey4fFPGriHB28DzZXG WxBar0fGqsLxTaF1q7eOaNBWdh5k2mk83h/WhsB+aS2F/bhJ6FWOuQbOw w==; X-CSE-ConnectionGUID: 0FJ+8EnCRyKe3X3HOxfqHg== X-CSE-MsgGUID: pVMo5wGlQcShVfK2R7CIZw== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34300047" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34300047" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: 5QUfa82bS1yMDZt1SCxENA== X-CSE-MsgGUID: RGywgsBaTSuRAnQMosaYoQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440481" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 10/16] x86/sgx: Implement async reclamation for cgroup Date: Fri, 30 Aug 2024 09:40:31 -0700 Message-ID: <20240830164038.39343-11-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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: Kristen Carlson Accardi In cases EPC pages need be allocated during a page fault and the cgroup usage is near its limit, an asynchronous reclamation needs to be triggered to avoid blocking the page fault handling. To keep implementation simple, use a workqueue instead of kthread to schedule the asynchronous reclamation work. Add corresponding work item and function definitions for EPC cgroup. In sgx_cgroup_try_charge(), if caller does not allow synchronous reclamation, queue an asynchronous work into the workqueue. The current global reclaimer, ksgxd, maintains a threshold for the minimal free EPC pages to avoid thrashing when allocating EPC pages. Similarly, only reclaiming EPC pages from the current cgroup when its usage actually reaches limit could also cause thrashing. To avoid that, define a similar "per-cgroup usage threshold" and actively trigger asynchronous per-cgroup EPC reclamation when the usage reaches the threshold after try_charge() is successful. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Tested-by: Jarkko Sakkinen Reviewed-by: Jarkko Sakkinen Reviewed-by: Kai Huang --- V17: - Rename sgx_cgroup_{init,deinit} to sgx_cgroup_wq_{init,deinit}. (Kai) - Remove '/**' (Kai) V16: - Destroy workqueue in sgx_cgroup_deinit() - Reuse the new sgx_cgroup_reclaim_pages() to scan at least SGX_NR_TO_SCAN pages for each round async reclaim. - Revise commit message. (Kai) V15: - Disable SGX when sgx_cgroup_init() fails instead of BUG_ON() (Jarkko) - Reset capacity to zero when sgx_cgroup_init() fails. (Kai) V13: - Revert to BUG_ON() in case of workq allocation failure in init and only alloc if misc is enabled. V11: - Print error instead of WARN (Kai) - Add check for need to queue an async reclamation before returning from try_charge(), do so if needed. This is to be consistent with global reclaimer to minimize thrashing during allocation time. V10: - Split asynchronous flow in separate patch. (Kai) - Consider cgroup disabled when the workqueue allocation fail during init. (Kai) - Abstract out sgx_cgroup_should_reclaim(). V9: - Add comments for static variables. (Jarkko) V8: - Remove alignment for substructure variables. (Jarkko) V7: - Split this out from the big patch, #10 in V6. (Dave, Kai) --- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 124 ++++++++++++++++++++++++++- arch/x86/kernel/cpu/sgx/epc_cgroup.h | 9 ++ arch/x86/kernel/cpu/sgx/main.c | 9 +- 3 files changed, 140 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c index abbefa9e21d5..abcf71fe2cba 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -4,9 +4,37 @@ #include #include "epc_cgroup.h" =20 +/* + * The minimal free pages, or the minimal margin between limit and usage + * maintained by per-cgroup reclaimer. + * + * Set this to the low threshold used by the global reclaimer, ksgxd. + */ +#define SGX_CG_MIN_FREE_PAGE (SGX_NR_LOW_PAGES) + +/* + * If the cgroup limit is close to SGX_CG_MIN_FREE_PAGE, maintaining the m= inimal + * free pages would barely leave any page for use, causing excessive recla= mation + * and thrashing. + * + * Define the following limit, below which cgroup does not maintain the mi= nimal + * free page threshold. Set this to quadruple of the minimal so at least 7= 5% + * pages used without being reclaimed. + */ +#define SGX_CG_LOW_LIMIT (SGX_CG_MIN_FREE_PAGE * 4) + /* The root SGX EPC cgroup */ static struct sgx_cgroup sgx_cg_root; =20 +/* + * The work queue that reclaims EPC pages in the background for cgroups. + * + * A cgroup schedules a work item into this queue to reclaim pages within = the + * same cgroup when its usage limit is reached and synchronous reclamation= is not + * an option, i.e., in a page fault handler. + */ +static struct workqueue_struct *sgx_cg_wq; + /* * Return the next descendant in a preorder walk, given a root, @root and a * cgroup, @cg, to start the walk from. Return @root if no descendant left= for @@ -100,6 +128,34 @@ static inline struct sgx_cgroup *sgx_cgroup_next_get(s= truct sgx_cgroup *root) return p; } =20 +static inline u64 sgx_cgroup_page_counter_read(struct sgx_cgroup *sgx_cg) +{ + return atomic64_read(&sgx_cg->cg->res[MISC_CG_RES_SGX_EPC].usage) / PAGE_= SIZE; +} + +static inline u64 sgx_cgroup_max_pages(struct sgx_cgroup *sgx_cg) +{ + return READ_ONCE(sgx_cg->cg->res[MISC_CG_RES_SGX_EPC].max) / PAGE_SIZE; +} + +/* + * Get the lower bound of limits of a cgroup and its ancestors. Used in + * sgx_cgroup_should_reclaim() to determine if EPC usage of a cgroup is + * close to its limit or its ancestors' hence reclamation is needed. + */ +static inline u64 sgx_cgroup_max_pages_to_root(struct sgx_cgroup *sgx_cg) +{ + struct misc_cg *i =3D sgx_cg->cg; + u64 m =3D U64_MAX; + + while (i) { + m =3D min(m, READ_ONCE(i->res[MISC_CG_RES_SGX_EPC].max)); + i =3D misc_cg_parent(i); + } + + return m / PAGE_SIZE; +} + /** * sgx_cgroup_lru_empty() - check if a cgroup tree has no pages on its LRUs * @root: Root of the tree to check @@ -159,6 +215,43 @@ static inline void sgx_cgroup_reclaim_pages(struct sgx= _cgroup *root, } } =20 +/* Check whether EPC reclaim should be performed for a given EPC cgroup.*/ +static bool sgx_cgroup_should_reclaim(struct sgx_cgroup *sgx_cg) +{ + u64 cur, max; + + if (sgx_cgroup_lru_empty(sgx_cg->cg)) + return false; + + max =3D sgx_cgroup_max_pages_to_root(sgx_cg); + + /* + * Unless the limit is very low, maintain a minimal "credit" available + * for charge to avoid per-cgroup reclamation and to serve new + * allocation requests more quickly. + */ + if (max > SGX_CG_LOW_LIMIT) + max -=3D SGX_CG_MIN_FREE_PAGE; + + cur =3D sgx_cgroup_page_counter_read(sgx_cg); + + return (cur >=3D max); +} + +/* + * Asynchronous work flow to reclaim pages from the cgroup when the cgroup= is + * at/near its maximum capacity. + */ +static void sgx_cgroup_reclaim_work_func(struct work_struct *work) +{ + struct sgx_cgroup *root =3D container_of(work, struct sgx_cgroup, reclaim= _work); + + while (sgx_cgroup_should_reclaim(root)) { + sgx_cgroup_reclaim_pages(root, SGX_NR_TO_SCAN); + cond_resched(); + } +} + static int __sgx_cgroup_try_charge(struct sgx_cgroup *epc_cg) { if (!misc_cg_try_charge(MISC_CG_RES_SGX_EPC, epc_cg->cg, PAGE_SIZE)) @@ -193,7 +286,8 @@ int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, en= um sgx_reclaim reclaim) goto out; =20 if (reclaim =3D=3D SGX_NO_RECLAIM) { - ret =3D -ENOMEM; + queue_work(sgx_cg_wq, &sgx_cg->reclaim_work); + ret =3D -EBUSY; goto out; } =20 @@ -202,6 +296,9 @@ int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, en= um sgx_reclaim reclaim) cond_resched(); } =20 + if (sgx_cgroup_should_reclaim(sgx_cg)) + queue_work(sgx_cg_wq, &sgx_cg->reclaim_work); + out: return ret; } @@ -224,6 +321,7 @@ static void sgx_cgroup_free(struct misc_cg *cg) if (!sgx_cg) return; =20 + cancel_work_sync(&sgx_cg->reclaim_work); /* * Notify ancestors to not reclaim from this dying cgroup. * Not start from this cgroup itself because at this point no reference @@ -242,6 +340,7 @@ static void sgx_cgroup_free(struct misc_cg *cg) static void sgx_cgroup_misc_init(struct misc_cg *cg, struct sgx_cgroup *sg= x_cg) { sgx_lru_init(&sgx_cg->lru); + INIT_WORK(&sgx_cg->reclaim_work, sgx_cgroup_reclaim_work_func); cg->res[MISC_CG_RES_SGX_EPC].priv =3D sgx_cg; sgx_cg->cg =3D cg; sgx_cg->next_cg =3D sgx_cg; @@ -266,6 +365,29 @@ const struct misc_res_ops sgx_cgroup_ops =3D { .free =3D sgx_cgroup_free, }; =20 +/* + * Initialize the workqueue for cgroups. + */ +int __init sgx_cgroup_wq_init(void) +{ + sgx_cg_wq =3D alloc_workqueue("sgx_cg_wq", WQ_UNBOUND | WQ_FREEZABLE, + WQ_UNBOUND_MAX_ACTIVE); + if (!sgx_cg_wq) { + pr_err("alloc_workqueue() failed for SGX cgroup.\n"); + return -ENOMEM; + } + + return 0; +} + +/* + * Only called during init to unwind what's done in sgx_cgroup_wq_init() + */ +void __init sgx_cgroup_wq_deinit(void) +{ + destroy_workqueue(sgx_cg_wq); +} + /* * Register capacity and ops for SGX cgroup and init the root cgroup. * Only called at the end of sgx_init() when SGX is ready to handle the ops diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx= /epc_cgroup.h index 879d5a4a2fcd..2285dbfc9462 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.h +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h @@ -28,12 +28,19 @@ static inline int sgx_cgroup_try_charge(struct sgx_cgro= up *sgx_cg, enum sgx_recl static inline void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg) { } =20 static inline void __init sgx_cgroup_init(void) { } +static inline int __init sgx_cgroup_wq_init(void) +{ + return 0; +} + +static inline void __init sgx_cgroup_wq_deinit(void) { } =20 #else /* CONFIG_CGROUP_MISC */ =20 struct sgx_cgroup { struct misc_cg *cg; struct sgx_epc_lru_list lru; + struct work_struct reclaim_work; /* * Pointer to the next cgroup to scan when the per-cgroup reclamation * is triggered next time. It does not hold a reference to prevent it @@ -80,6 +87,8 @@ static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg) int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim recl= aim); void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg); void __init sgx_cgroup_init(void); +int __init sgx_cgroup_wq_init(void); +void __init sgx_cgroup_wq_deinit(void); =20 #endif /* CONFIG_CGROUP_MISC */ =20 diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index d41cf6145975..7b1a19d659ab 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -993,6 +993,10 @@ static int __init sgx_init(void) if (ret) goto err_kthread; =20 + ret =3D sgx_cgroup_wq_init(); + if (ret) + goto err_provision; + /* * Always try to initialize the native *and* KVM drivers. * The KVM driver is less picky than the native one and @@ -1004,12 +1008,15 @@ static int __init sgx_init(void) ret =3D sgx_drv_init(); =20 if (sgx_vepc_init() && ret) - goto err_provision; + goto err_cgroup; =20 sgx_cgroup_init(); =20 return 0; =20 +err_cgroup: + sgx_cgroup_wq_deinit(); + err_provision: misc_deregister(&sgx_dev_provision); =20 --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 B8C821BA889; Fri, 30 Aug 2024 16:41:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036099; cv=none; b=ooLJpQZGQGxJSkh/HP+/+HKNnMx3Ou5OryAOj4fehE0raW7cvYVhjZWd+45SbsfQm6bYmsn+R97LpzpFgkWkSXy0W2ktC2W+J5szU4JJfe0/TT2R3n1RNbyc/RLohRrAe+CYNzJud1RvipXZRv7/n5DrsIib08X0hn4A214qunk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036099; c=relaxed/simple; bh=cSUiz2tNGwgEsoBqGQqPp0TwkbfNiXAR0X6hEyUaOrw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IsuZ24nZ5jrebuws47Iik5tFNwIsoc2Uep5N4m7PobcihT+aL4COqFxAYMsgI81j5UqCFFXOHlyw2xRepIFAuO986AU3IQGn7pFqVJ75eW2pgZg1/Q1oskOvmfrkXGNN0kOJnNbj43btN9Duh2+iT9E3Co6DfMt6JzfZG/kQ9Wg= 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=U8IIdEmJ; arc=none smtp.client-ip=192.198.163.11 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="U8IIdEmJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036097; x=1756572097; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cSUiz2tNGwgEsoBqGQqPp0TwkbfNiXAR0X6hEyUaOrw=; b=U8IIdEmJGkWk0Yz4196mp6MfjPzF01ei8ysNy1/kdc/NDzOXN7HOG+WL H2i+XCB/EE2V7ihSvDKawcrQVfC0iD/Lmo04Ds7ICrAYXQX7hOwoctSiE uHzGyuyW0kgxMGbqGMxoil96gAyZpxkmiU1MzcZDSco9IPK5Zws4vmEWg pXGrcNV1L7OVraNXvITfSPrj8QW9Uo6lSL5YqrYEJAOi+cUDMXb6AI+Sg yDg63UefAneORUpzHxAT5ntoopXBw8J2bCyvFeWYwZWlx4BqAvGQSnl8q f7uRS8AiAVyj+DtAXfimHChlUrp0cc+XklKxdyKTlxYjOJ8RU45kzoVx3 g==; X-CSE-ConnectionGUID: 1ma+oj33T3m6wmQDm4rGSQ== X-CSE-MsgGUID: v7rGO38FSNG/uht9wcC0ig== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34299989" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34299989" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: FJJp92RSTl2YfDe2dn05AA== X-CSE-MsgGUID: 6XT/41hcTTi0q9g0YckHdg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440485" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 11/16] x86/sgx: Charge mem_cgroup for per-cgroup reclamation Date: Fri, 30 Aug 2024 09:40:32 -0700 Message-ID: <20240830164038.39343-12-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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" Enclave Page Cache(EPC) memory can be swapped out to regular system memory, and the consumed memory should be charged to a proper mem_cgroup. Currently the selection of mem_cgroup to charge is done in sgx_encl_get_mem_cgroup(). But it considers all contexts other than the ksgxd thread are user processes. With the new EPC cgroup implementation, the swapping can also happen in EPC cgroup work-queue threads. In those cases, it improperly selects the root mem_cgroup to charge for the RAM usage. Remove current_is_ksgxd() and change sgx_encl_get_mem_cgroup() to take an additional argument to explicitly specify the mm struct to charge for allocations. Callers from background kthreads not associated with a charging mm struct would set it to NULL, while callers in user process contexts set it to current->mm. Internally, it handles the case when the charging mm given is NULL, by searching for an mm struct from enclave's mm_list. Signed-off-by: Haitao Huang Reported-by: Mikko Ylinen Reviewed-by: Kai Huang Reviewed-by: Jarkko Sakkinen Tested-by: Mikko Ylinen Tested-by: Jarkko Sakkinen --- V10: - Pass mm struct instead of a boolean 'indirect'. (Dave, Jarkko) V9: - Reduce number of if statements. (Tim) V8: - Limit text paragraphs to 80 characters wide. (Jarkko) --- arch/x86/kernel/cpu/sgx/encl.c | 29 ++++++++++++++-------------- arch/x86/kernel/cpu/sgx/encl.h | 3 +-- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 9 ++++++--- arch/x86/kernel/cpu/sgx/main.c | 29 +++++++++++++--------------- arch/x86/kernel/cpu/sgx/sgx.h | 2 +- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index f474179b6f77..7b77dad41daf 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -993,23 +993,23 @@ static int __sgx_encl_get_backing(struct sgx_encl *en= cl, unsigned long page_inde } =20 /* - * When called from ksgxd, returns the mem_cgroup of a struct mm stored - * in the enclave's mm_list. When not called from ksgxd, just returns - * the mem_cgroup of the current task. + * Find the mem_cgroup to charge for memory allocated on behalf of an encl= ave. + * + * Used in sgx_encl_alloc_backing() for backing store allocation. + * + * Return the mem_cgroup of the given charge_mm. Otherwise return the mem_= cgroup + * of a struct mm stored in the enclave's mm_list. */ -static struct mem_cgroup *sgx_encl_get_mem_cgroup(struct sgx_encl *encl) +static struct mem_cgroup *sgx_encl_get_mem_cgroup(struct sgx_encl *encl, + struct mm_struct *charge_mm) { struct mem_cgroup *memcg =3D NULL; struct sgx_encl_mm *encl_mm; int idx; =20 - /* - * If called from normal task context, return the mem_cgroup - * of the current task's mm. The remainder of the handling is for - * ksgxd. - */ - if (!current_is_ksgxd()) - return get_mem_cgroup_from_mm(current->mm); + /* Use the charge_mm if given. */ + if (charge_mm) + return get_mem_cgroup_from_mm(charge_mm); =20 /* * Search the enclave's mm_list to find an mm associated with @@ -1047,8 +1047,9 @@ static struct mem_cgroup *sgx_encl_get_mem_cgroup(str= uct sgx_encl *encl) * @encl: an enclave pointer * @page_index: enclave page index * @backing: data for accessing backing storage for the page + * @charge_mm: the mm to charge for the allocation * - * When called from ksgxd, sets the active memcg from one of the + * When charge_mm is NULL, sets the active memcg from one of the * mms in the enclave's mm_list prior to any backing page allocation, * in order to ensure that shmem page allocations are charged to the * enclave. Create a backing page for loading data back into an EPC page = with @@ -1060,9 +1061,9 @@ static struct mem_cgroup *sgx_encl_get_mem_cgroup(str= uct sgx_encl *encl) * -errno otherwise. */ int sgx_encl_alloc_backing(struct sgx_encl *encl, unsigned long page_index, - struct sgx_backing *backing) + struct sgx_backing *backing, struct mm_struct *charge_mm) { - struct mem_cgroup *encl_memcg =3D sgx_encl_get_mem_cgroup(encl); + struct mem_cgroup *encl_memcg =3D sgx_encl_get_mem_cgroup(encl, charge_mm= ); struct mem_cgroup *memcg =3D set_active_memcg(encl_memcg); int ret; =20 diff --git a/arch/x86/kernel/cpu/sgx/encl.h b/arch/x86/kernel/cpu/sgx/encl.h index fe15ade02ca1..5ce9d108290f 100644 --- a/arch/x86/kernel/cpu/sgx/encl.h +++ b/arch/x86/kernel/cpu/sgx/encl.h @@ -103,12 +103,11 @@ static inline int sgx_encl_find(struct mm_struct *mm,= unsigned long addr, int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start, unsigned long end, unsigned long vm_flags); =20 -bool current_is_ksgxd(void); void sgx_encl_release(struct kref *ref); int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm); const cpumask_t *sgx_encl_cpumask(struct sgx_encl *encl); int sgx_encl_alloc_backing(struct sgx_encl *encl, unsigned long page_index, - struct sgx_backing *backing); + struct sgx_backing *backing, struct mm_struct *charge_mm); void sgx_encl_put_backing(struct sgx_backing *backing); int sgx_encl_test_and_clear_young(struct mm_struct *mm, struct sgx_encl_page *page); diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c index abcf71fe2cba..4faff943ce15 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -200,8 +200,10 @@ static bool sgx_cgroup_lru_empty(struct misc_cg *root) =20 /* * Scan at least @nr_to_scan pages and attempt to reclaim them from the su= btree of @root. + * Charge backing store allocation to the given mm, @charge_mm. */ static inline void sgx_cgroup_reclaim_pages(struct sgx_cgroup *root, + struct mm_struct *charge_mm, unsigned int nr_to_scan) { struct sgx_cgroup *next_cg =3D NULL; @@ -209,7 +211,7 @@ static inline void sgx_cgroup_reclaim_pages(struct sgx_= cgroup *root, =20 while (!sgx_cgroup_lru_empty(root->cg) && cnt < nr_to_scan) { next_cg =3D sgx_cgroup_next_get(root); - cnt +=3D sgx_reclaim_pages(&next_cg->lru); + cnt +=3D sgx_reclaim_pages(&next_cg->lru, charge_mm); if (next_cg !=3D root) sgx_put_cg(next_cg); } @@ -247,7 +249,8 @@ static void sgx_cgroup_reclaim_work_func(struct work_st= ruct *work) struct sgx_cgroup *root =3D container_of(work, struct sgx_cgroup, reclaim= _work); =20 while (sgx_cgroup_should_reclaim(root)) { - sgx_cgroup_reclaim_pages(root, SGX_NR_TO_SCAN); + /* Indirect reclaim, no mm to charge, so NULL: */ + sgx_cgroup_reclaim_pages(root, NULL, SGX_NR_TO_SCAN); cond_resched(); } } @@ -291,7 +294,7 @@ int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, en= um sgx_reclaim reclaim) goto out; } =20 - sgx_cgroup_reclaim_pages(sgx_cg, 1); + sgx_cgroup_reclaim_pages(sgx_cg, current->mm, 1); =20 cond_resched(); } diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 7b1a19d659ab..b47d627bd370 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -261,8 +261,8 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, } } =20 -static void sgx_reclaimer_write(struct sgx_epc_page *epc_page, - struct sgx_backing *backing) +static void sgx_reclaimer_write(struct sgx_epc_page *epc_page, struct sgx_= backing *backing, + struct mm_struct *charge_mm) { struct sgx_encl_page *encl_page =3D epc_page->owner; struct sgx_encl *encl =3D encl_page->encl; @@ -278,7 +278,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *ep= c_page, =20 if (!encl->secs_child_cnt && test_bit(SGX_ENCL_INITIALIZED, &encl->flags)= ) { ret =3D sgx_encl_alloc_backing(encl, PFN_DOWN(encl->size), - &secs_backing); + &secs_backing, charge_mm); if (ret) goto out; =20 @@ -297,6 +297,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *ep= c_page, /** * sgx_reclaim_pages() - Attempt to reclaim a fixed number of pages from a= n LRU * @lru: The LRU from which pages are reclaimed. + * @charge_mm: The mm to charge for backing store allocation. * * Take a fixed number of pages from the head of a given LRU and reclaim t= hem to * the enclave's private shmem files. Skip the pages, which have been acce= ssed @@ -312,7 +313,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *ep= c_page, * * Return: Number of pages attempted for reclamation. */ -unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list *lru) +unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list *lru, struct mm_str= uct *charge_mm) { struct sgx_epc_page *chunk[SGX_NR_TO_SCAN]; struct sgx_backing backing[SGX_NR_TO_SCAN]; @@ -352,7 +353,7 @@ unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list = *lru) page_index =3D PFN_DOWN(encl_page->desc - encl_page->encl->base); =20 mutex_lock(&encl_page->encl->lock); - ret =3D sgx_encl_alloc_backing(encl_page->encl, page_index, &backing[i]); + ret =3D sgx_encl_alloc_backing(encl_page->encl, page_index, &backing[i],= charge_mm); if (ret) { mutex_unlock(&encl_page->encl->lock); goto skip; @@ -384,7 +385,7 @@ unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list = *lru) continue; =20 encl_page =3D epc_page->owner; - sgx_reclaimer_write(epc_page, &backing[i]); + sgx_reclaimer_write(epc_page, &backing[i], charge_mm); =20 kref_put(&encl_page->encl->refcount, sgx_encl_release); epc_page->flags &=3D ~SGX_EPC_PAGE_RECLAIMER_TRACKED; @@ -401,9 +402,9 @@ static bool sgx_should_reclaim_global(unsigned long wat= ermark) sgx_can_reclaim_global(); } =20 -static void sgx_reclaim_pages_global(void) +static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) { - sgx_reclaim_pages(&sgx_global_lru); + sgx_reclaim_pages(&sgx_global_lru, charge_mm); } =20 /* @@ -414,7 +415,7 @@ static void sgx_reclaim_pages_global(void) void sgx_reclaim_direct(void) { if (sgx_should_reclaim_global(SGX_NR_LOW_PAGES)) - sgx_reclaim_pages_global(); + sgx_reclaim_pages_global(current->mm); } =20 static int ksgxd(void *p) @@ -437,7 +438,8 @@ static int ksgxd(void *p) sgx_should_reclaim_global(SGX_NR_HIGH_PAGES)); =20 if (sgx_should_reclaim_global(SGX_NR_HIGH_PAGES)) - sgx_reclaim_pages_global(); + /* Indirect reclaim, no mm to charge, so NULL: */ + sgx_reclaim_pages_global(NULL); =20 cond_resched(); } @@ -460,11 +462,6 @@ static bool __init sgx_page_reclaimer_init(void) return true; } =20 -bool current_is_ksgxd(void) -{ - return current =3D=3D ksgxd_tsk; -} - static struct sgx_epc_page *__sgx_alloc_epc_page_from_node(int nid) { struct sgx_numa_node *node =3D &sgx_numa_nodes[nid]; @@ -625,7 +622,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, en= um sgx_reclaim reclaim) * Perform a global reclaim to get some pages released from any * cgroup with reclaimable pages. */ - sgx_reclaim_pages_global(); + sgx_reclaim_pages_global(current->mm); cond_resched(); } =20 diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index 710fff30182d..c428f81ab227 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -139,7 +139,7 @@ void sgx_reclaim_direct(void); void sgx_mark_page_reclaimable(struct sgx_epc_page *page); int sgx_unmark_page_reclaimable(struct sgx_epc_page *page); struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim recl= aim); -unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list *lru); +unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list *lru, struct mm_str= uct *charge_mm); =20 void sgx_ipi_cb(void *info); =20 --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 B04DF1BB69D; Fri, 30 Aug 2024 16:41:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036099; cv=none; b=sqcEFiYpcgXRqBgw4wGu+beJcP80Zku/hWZorJnieqZdR900XHKyxhJ9VdR+h+sW5dvnxNcIbnhDQ63ucy8yiN8L9t4+A5t0USCevrFqdg2Ry9w5f9h69AvnNjQxrkgY8ZbxKZ8xMJrwlRnrpabgNaQHgUrsPGzGLC91jAgjWKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036099; c=relaxed/simple; bh=utO0vDU8+xd4y+Lphxyt7zUfR4oNivb7eGaN4/0AoIk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k5150OsoiBxRKMQ0i2NaKwdGeRlRU+ldrQPDMsSCupu3440XjVlr8KM/5es2E4ClKKh2/XDMAXNBOCVMHPOvCBgfxuVcEBuXSkHR5vasAxoTMVgO22NUtMHRU3ugQVhk9DqxJifsT9Ia/Qvmc1hHocyHOpZgqk4cI9VFIj5fcQQ= 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=brjERrd4; arc=none smtp.client-ip=192.198.163.11 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="brjERrd4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036097; x=1756572097; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=utO0vDU8+xd4y+Lphxyt7zUfR4oNivb7eGaN4/0AoIk=; b=brjERrd47rA1m4fzG5tYe4Yj4CFNbomPyoY6RyE4Qmn+ovvK+aGoZA3C VJPL955dDZLVlQXxRFXcDQmTOYOEStv0MdXNaE+6e3+cKXAONWcfmwplu HZEJ6MVR2cdgA5f2AuLMjlzUql57Xt9y0Ysr7xxAXXTVgGX/nq7GeLFaB 3xzjCkcVLQtZ3GklHV2yTGmZR+INfO7lcUim1kgWdrkyoJR5NzpU/LQmh BJZZ0VCviM8noJk5/8xJMO4kdGIJv9nnCC7d17WQ0D8HLVtCMwgcyUQDi NejK6euExhRNfseuSwt2uwJKJgJuifHs2RneQZ2C1peYzi1lcna6SUM81 g==; X-CSE-ConnectionGUID: zIgJiMt/TvWwB8Q0MQFZsg== X-CSE-MsgGUID: s2msaEdITxO9MmIVSuEDOQ== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34300011" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34300011" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: uGSIMSGDTCyx8c2aIlHQkw== X-CSE-MsgGUID: 8gEMLy4VRVqCGovAwtZBoQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440488" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 12/16] x86/sgx: Implement direct reclamation for cgroups Date: Fri, 30 Aug 2024 09:40:33 -0700 Message-ID: <20240830164038.39343-13-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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" sgx_reclaim_direct() was introduced to preemptively reclaim some pages as the best effort to avoid on-demand reclamation that can stall forward progress in some situations, e.g., allocating pages to load previously reclaimed page to perform EDMM operations on [1]. Currently when the global usage is close to the capacity, sgx_reclaim_direct() makes one invocation to sgx_reclaim_pages_global() but does not guarantee there are free pages available for later allocations to succeed. In other words, the only goal here is to reduce the chance of on-demand reclamation at allocation time. In cases of allocation failure, the caller, the EDMM ioctl()'s, would return -EAGAIN to user space and let the user space to decide whether to retry or not. With EPC cgroups enabled, usage of a cgroup can also reach its limit (usually much lower than capacity) and trigger per-cgroup reclamation. Implement a similar strategy to reduce the chance of on-demand per-cgroup reclamation for this use case. Create a wrapper, sgx_cgroup_reclaim_direct(), to perform a preemptive reclamation at cgroup level, and have sgx_reclaim_direct() call it when EPC cgroup is enabled. [1] https://lore.kernel.org/all/a0d8f037c4a075d56bf79f432438412985f7ff7a.16= 52137848.git.reinette.chatre@intel.com/T/#u Signed-off-by: Haitao Huang Reviewed-by: Kai Huang Reviewed-by: Jarkko Sakkinen --- V17: - Improve comments and capitalization. (Kai) --- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 15 +++++++++++++++ arch/x86/kernel/cpu/sgx/epc_cgroup.h | 3 +++ arch/x86/kernel/cpu/sgx/main.c | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c index 4faff943ce15..7394f78dec49 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -240,6 +240,21 @@ static bool sgx_cgroup_should_reclaim(struct sgx_cgrou= p *sgx_cg) return (cur >=3D max); } =20 +/** + * sgx_cgroup_reclaim_direct() - Preemptive reclamation. + * + * Scan and attempt to reclaim %SGX_NR_TO_SCAN as best effort to make later + * EPC allocation quicker. + */ +void sgx_cgroup_reclaim_direct(void) +{ + struct sgx_cgroup *sgx_cg =3D sgx_get_current_cg(); + + if (sgx_cgroup_should_reclaim(sgx_cg)) + sgx_cgroup_reclaim_pages(sgx_cg, current->mm, SGX_NR_TO_SCAN); + sgx_put_cg(sgx_cg); +} + /* * Asynchronous work flow to reclaim pages from the cgroup when the cgroup= is * at/near its maximum capacity. diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx= /epc_cgroup.h index 2285dbfc9462..a530c9611332 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.h +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h @@ -35,6 +35,8 @@ static inline int __init sgx_cgroup_wq_init(void) =20 static inline void __init sgx_cgroup_wq_deinit(void) { } =20 +static inline void sgx_cgroup_reclaim_direct(void) { } + #else /* CONFIG_CGROUP_MISC */ =20 struct sgx_cgroup { @@ -86,6 +88,7 @@ static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg) =20 int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim recl= aim); void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg); +void sgx_cgroup_reclaim_direct(void); void __init sgx_cgroup_init(void); int __init sgx_cgroup_wq_init(void); void __init sgx_cgroup_wq_deinit(void); diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index b47d627bd370..6f293115b75e 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -414,6 +414,10 @@ static void sgx_reclaim_pages_global(struct mm_struct = *charge_mm) */ void sgx_reclaim_direct(void) { + /* Reduce chance of per-cgroup reclamation for later allocation */ + sgx_cgroup_reclaim_direct(); + + /* Reduce chance of the global reclamation for later allocation */ if (sgx_should_reclaim_global(SGX_NR_LOW_PAGES)) sgx_reclaim_pages_global(current->mm); } --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 4B8691BCA1E; Fri, 30 Aug 2024 16:41:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036102; cv=none; b=mqb7IlaNoaf2AHltTTT5fYnUc2MQ8bRccoT246o3iXhUSrAf8s1waicdU70VIDbAYsdX/SwKNmTV0CotPuV5+UPRbqoYvToi9N63JE4xEkRXkqpzuoTTuS1M1XWQnXziuD5j4093hAWO1iSyx/bD3H/oAHgPSf1MhdKIwJ+rosY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036102; c=relaxed/simple; bh=YUKariCmlfVCxX2JHHjkResiD6JNgD6M4hXa3h5e0FA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GtOdFBwmDtNgQN4z3OyzbFEeEBjNnS01OR/oK3azf8EKdF6DyJhUI9bp6prr4PtfrAbRNELSuDkyoyTIroTCcoLwQsGjPSfaFYQD2/k4zVrEmoXEsZUvnaKr7EC/i2BJ7s2zynTdAlE3JRP2+pi2L6dosArRQ1Y41swN4t4KW/0= 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=C59t0xkK; arc=none smtp.client-ip=192.198.163.11 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="C59t0xkK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036100; x=1756572100; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YUKariCmlfVCxX2JHHjkResiD6JNgD6M4hXa3h5e0FA=; b=C59t0xkKda7/iFfllRmr6u0FAlecNN3oof9PohTuulQnVXqKyrv3nFiq +mCEe+SPG0LmVUCORALN5rBc1bEMYT3yYD7QD4XT6Cw1FfQoKxnP+MUyL Ul+hzT8UArfK627/9wR/tXdcU9PC7QlciSI5AM6tw7+k959Dhg10CrH/g YtyRC3EIYb7H3kaqLHTEgRv1BGPwvkxVaD5YA9rniytmys+k5nXOavxSE XcMXatmYyiATqUO/hhgO5AVTH909+yoUUBklgQ3i1L39CgRewE0WaCpKr EzppeGeR/z4hb3LTto3ujiPqioxGT3iiUbrA74Qq5ol2fmxE8Flws0p9v g==; X-CSE-ConnectionGUID: bPNPjF2OSoC/phbSeVDeOQ== X-CSE-MsgGUID: LkVxBFFkRLKTS1JGPoyXlg== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34300029" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34300029" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:52 -0700 X-CSE-ConnectionGUID: kFxoRvDKQlqtdLTpIGtf4g== X-CSE-MsgGUID: w/LfNAl2RnmoZVGifFjhzg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440491" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:53 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 13/16] x86/sgx: Revise global reclamation for EPC cgroups Date: Fri, 30 Aug 2024 09:40:34 -0700 Message-ID: <20240830164038.39343-14-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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" With EPC cgroups, the global reclamation function, sgx_reclaim_pages_global(), can no longer apply to the global LRU as pages are now in per-cgroup LRUs. Create a wrapper, sgx_cgroup_reclaim_global() to invoke sgx_cgroup_reclaim_pages() passing in the root cgroup. The wrapper will scan and attempt to reclaim SGX_NR_TO_SCAN pages just like the current global reclaim. Note this simple implementation doesn't _exactly_ mimic the current global EPC reclaim (which always tries to do the actual reclaim in batch of SGX_NR_TO_SCAN pages): in rare cases when LRUs have less than SGX_NR_TO_SCAN reclaimable pages, the actual reclaim of EPC pages will be split into smaller batches _across_ multiple LRUs with each being smaller than SGX_NR_TO_SCAN pages. A more precise way to mimic the current global EPC reclaim would be to have a new function to only "scan" (or "isolate") SGX_NR_TO_SCAN pages _across_ the given EPC cgroup _AND_ its descendants, and then do the actual reclaim in one batch. But this is unnecessarily complicated at this stage to address such rare cases. Signed-off-by: Haitao Huang Reviewed-by: Kai Huang Reviewed-by: Jarkko Sakkinen --- V17: - Add comments in sgx_can_reclaim_global(). (Kai) - Remove inaccurate comments in commit msg. (Kai) --- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 12 ++++++++++++ arch/x86/kernel/cpu/sgx/epc_cgroup.h | 2 ++ arch/x86/kernel/cpu/sgx/main.c | 14 ++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c index 7394f78dec49..c85ec1a6ebf0 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -255,6 +255,18 @@ void sgx_cgroup_reclaim_direct(void) sgx_put_cg(sgx_cg); } =20 +/** + * sgx_cgroup_reclaim_pages_global() - Perform one round of global reclama= tion. + * + * @charge_mm: The mm to be charged for the backing store of reclaimed pag= es. + * + * Try to scan and attempt reclamation from root cgroup for %SGX_NR_TO_SCA= N pages. + */ +void sgx_cgroup_reclaim_pages_global(struct mm_struct *charge_mm) +{ + sgx_cgroup_reclaim_pages(&sgx_cg_root, charge_mm, SGX_NR_TO_SCAN); +} + /* * Asynchronous work flow to reclaim pages from the cgroup when the cgroup= is * at/near its maximum capacity. diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx= /epc_cgroup.h index a530c9611332..08cee70d975d 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.h +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h @@ -36,6 +36,7 @@ static inline int __init sgx_cgroup_wq_init(void) static inline void __init sgx_cgroup_wq_deinit(void) { } =20 static inline void sgx_cgroup_reclaim_direct(void) { } +static inline void sgx_cgroup_reclaim_pages_global(struct mm_struct *charg= e_mm) { } =20 #else /* CONFIG_CGROUP_MISC */ =20 @@ -89,6 +90,7 @@ static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg) int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim recl= aim); void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg); void sgx_cgroup_reclaim_direct(void); +void sgx_cgroup_reclaim_pages_global(struct mm_struct *charge_mm); void __init sgx_cgroup_init(void); int __init sgx_cgroup_wq_init(void); void __init sgx_cgroup_wq_deinit(void); diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 6f293115b75e..24dbcbc0f596 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -42,6 +42,13 @@ static inline struct sgx_epc_lru_list *sgx_epc_page_lru(= struct sgx_epc_page *epc */ static inline bool sgx_can_reclaim_global(void) { + /* + * Now all EPC pages are still tracked in the @sgx_global_lru, so only + * check @sgx_global_lru. + * + * When EPC pages are tracked in the actual per-cgroup LRUs, + * replace with sgx_cgroup_lru_empty(misc_cg_root()). + */ return !list_empty(&sgx_global_lru.reclaimable); } =20 @@ -404,6 +411,13 @@ static bool sgx_should_reclaim_global(unsigned long wa= termark) =20 static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) { + /* + * Now all EPC pages are still tracked in the @sgx_global_lru. + * Still reclaim from it. + * + * When EPC pages are tracked in the actual per-cgroup LRUs, + * sgx_cgroup_reclaim_pages_global() will be called. + */ sgx_reclaim_pages(&sgx_global_lru, charge_mm); } =20 --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 274E51BE226; Fri, 30 Aug 2024 16:41:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036105; cv=none; b=uTmj1dKrQ3KzKic0XL4lrzVvzZ83aHFIm7EP1ETIrDtF9sqN0UA8vGbxv/fQ5YD5LAVegurMaFapbBiHDTYUtBOjSr7vpVNrDur3LSQgJu3SOAN3MBlVaxHfzyhasUqFZqKjDs2wfr4W7C0k9N0wxhu5MFWd2sTYPFAlxhdRS2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036105; c=relaxed/simple; bh=HW08YzCMbDUlzWibsu2BQT7zCWQmJ38zDrn1Sj5hFek=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PuSpDlUlWezjrRLau70zEKqKjb3t/Ks2AJYsIc9zYj/7qyj0N9RMYZvZ87uHSWdrg+sZCv5tFydNcg1r3ZBHzI5ICPiJ6U7FvtDMs6FXzJmsTONXWd2jERaB+gAT8DrVoyHn5Gjzrusu1KZQnp5K2k0ha7JnI0pJpv89EBYWyio= 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=Nm9BrIb+; arc=none smtp.client-ip=192.198.163.11 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="Nm9BrIb+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036103; x=1756572103; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HW08YzCMbDUlzWibsu2BQT7zCWQmJ38zDrn1Sj5hFek=; b=Nm9BrIb+b3oN3BQ71Gffuuj+w/TKKKHaBeDTsPFmVlMhHhPUViim+ncD QlrJWjANYhDvq4Hf2EpshRlzUtwQl63j64keN4MYEWhyQ9q42AeW4rPmh 0sQl5Nw5eDrtHsCI8LCLd0eSc+oPKjeo6JhXTtmHzDm26gsl37zZjlsCK 3fVr4pwnpECDHf/7xtRA9n40axLm0GLUP45C/jJp5RlkgTso1f4sa48Ms BB32L8CRIJno4n6WamI32HJSpUJSuMhtvRiyz2JSugHfi8djhfg/r+ctg 7h2VN2kzkpB2indG8yFa/FNBPeq1D0RxpvsS7uAoBZDpM4d0NLN54zlOH g==; X-CSE-ConnectionGUID: tWayIOc+Tz+yk1em0JXupQ== X-CSE-MsgGUID: BKKg3dMPSXqzJ96HTVQAaw== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34300082" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34300082" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:53 -0700 X-CSE-ConnectionGUID: TPFkUI+ITH2jiQyafg6qrA== X-CSE-MsgGUID: 5OkrLE2qQ0Sf0F7vOEFmtw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440494" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:53 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 14/16] x86/sgx: Turn on per-cgroup EPC reclamation Date: Fri, 30 Aug 2024 09:40:35 -0700 Message-ID: <20240830164038.39343-15-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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: Kristen Carlson Accardi Previous patches have implemented all infrastructure needed for per-cgroup EPC page tracking and reclaiming. But all reclaimable EPC pages are still tracked in the global LRU as sgx_epc_page_lru() always returns reference to the global LRU. Change sgx_epc_page_lru() to return the LRU of the cgroup in which the given EPC page is allocated. Update sgx_can_reclaim_global(), to check emptiness of LRUs of all cgroups, and update sgx_reclaim_pages_global(), to utilize sgx_cgroup_reclaim_pages_global(), when EPC cgroup is enabled. With these changes, the global reclamation and per-cgroup reclamation both work properly with all pages tracked in per-cgroup LRUs. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Reviewed-by: Jarkko Sakkinen Reviewed-by: Kai Huang Tested-by: Mikko Ylinen --- V16: - Separated out the global and direct reclamation to earlier patch.(Kai) V14: - Update global reclamation to use the new sgx_cgroup_reclaim_pages() to iterate cgroups at lower level if the top cgroups are too busy. V13: - Use IS_ENABLED(CONFIG_CGROUP_MISC) in sgx_can_reclaim_global(). (Kai) V12: - Remove CONFIG_CGROUP_SGX_EPC, conditional compile SGX Cgroup for CONFIGCONFIG_CGROUPMISC. (Jarkko) V11: - Reword the comments for global reclamation for allocation failure after passing cgroup charging. (Kai) - Add stub functions to remove ifdefs in c file (Kai) - Add more detailed comments to clarify each page belongs to one cgroup, or= the root. (Kai) V10: - Add comment to clarify each page belongs to one cgroup, or the root by default. (Kai) - Merge the changes that expose sgx_cgroup_* functions to this patch. - Add changes for sgx_reclaim_direct() that was missed previously. V7: - Split this out from the big patch, #10 in V6. (Dave, Kai) --- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 2 +- arch/x86/kernel/cpu/sgx/epc_cgroup.h | 6 ++++ arch/x86/kernel/cpu/sgx/main.c | 45 ++++++++++++++++++---------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c index c85ec1a6ebf0..af6e4dfbf1af 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -162,7 +162,7 @@ static inline u64 sgx_cgroup_max_pages_to_root(struct s= gx_cgroup *sgx_cg) * * Return: %true if all cgroups under the specified root have empty LRU li= sts. */ -static bool sgx_cgroup_lru_empty(struct misc_cg *root) +bool sgx_cgroup_lru_empty(struct misc_cg *root) { struct cgroup_subsys_state *css_root; struct cgroup_subsys_state *pos; diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx= /epc_cgroup.h index 08cee70d975d..ddc0e7bd8ce3 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.h +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h @@ -27,6 +27,11 @@ static inline int sgx_cgroup_try_charge(struct sgx_cgrou= p *sgx_cg, enum sgx_recl =20 static inline void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg) { } =20 +static inline bool sgx_cgroup_lru_empty(struct misc_cg *root) +{ + return true; +} + static inline void __init sgx_cgroup_init(void) { } static inline int __init sgx_cgroup_wq_init(void) { @@ -89,6 +94,7 @@ static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg) =20 int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim recl= aim); void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg); +bool sgx_cgroup_lru_empty(struct misc_cg *root); void sgx_cgroup_reclaim_direct(void); void sgx_cgroup_reclaim_pages_global(struct mm_struct *charge_mm); void __init sgx_cgroup_init(void); diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 24dbcbc0f596..ec6c068aaf5d 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -32,9 +32,30 @@ static DEFINE_XARRAY(sgx_epc_address_space); */ static struct sgx_epc_lru_list sgx_global_lru; =20 +/* + * Get the per-cgroup or global LRU list that tracks the given reclaimable= page. + */ static inline struct sgx_epc_lru_list *sgx_epc_page_lru(struct sgx_epc_pag= e *epc_page) { +#ifdef CONFIG_CGROUP_MISC + /* + * epc_page->sgx_cg here is never NULL during a reclaimable epc_page's + * life between sgx_alloc_epc_page() and sgx_free_epc_page(): + * + * In sgx_alloc_epc_page(), epc_page->sgx_cg is set to the return from + * sgx_get_current_cg() which is the misc cgroup of the current task, or + * the root by default even if the misc cgroup is disabled by kernel + * command line. + * + * epc_page->sgx_cg is only unset by sgx_free_epc_page(). + * + * This function is never used before sgx_alloc_epc_page() or after + * sgx_free_epc_page(). + */ + return &epc_page->sgx_cg->lru; +#else return &sgx_global_lru; +#endif } =20 /* @@ -42,14 +63,10 @@ static inline struct sgx_epc_lru_list *sgx_epc_page_lru= (struct sgx_epc_page *epc */ static inline bool sgx_can_reclaim_global(void) { - /* - * Now all EPC pages are still tracked in the @sgx_global_lru, so only - * check @sgx_global_lru. - * - * When EPC pages are tracked in the actual per-cgroup LRUs, - * replace with sgx_cgroup_lru_empty(misc_cg_root()). - */ - return !list_empty(&sgx_global_lru.reclaimable); + if (IS_ENABLED(CONFIG_CGROUP_MISC)) + return !sgx_cgroup_lru_empty(misc_cg_root()); + else + return !list_empty(&sgx_global_lru.reclaimable); } =20 static atomic_long_t sgx_nr_free_pages =3D ATOMIC_LONG_INIT(0); @@ -411,14 +428,10 @@ static bool sgx_should_reclaim_global(unsigned long w= atermark) =20 static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) { - /* - * Now all EPC pages are still tracked in the @sgx_global_lru. - * Still reclaim from it. - * - * When EPC pages are tracked in the actual per-cgroup LRUs, - * sgx_cgroup_reclaim_pages_global() will be called. - */ - sgx_reclaim_pages(&sgx_global_lru, charge_mm); + if (IS_ENABLED(CONFIG_CGROUP_MISC)) + sgx_cgroup_reclaim_pages_global(charge_mm); + else + sgx_reclaim_pages(&sgx_global_lru, charge_mm); } =20 /* --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 236001BE86F; Fri, 30 Aug 2024 16:41:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036107; cv=none; b=t3aiZCfiiIRXMm1RpILeIPX7a7otb2r4ddjClLIxCgh5sy/n8xTQZcqHO57K2oR86JQrdxFNiPIMTKp+PCAw2NEBqrzUWEe+BDUp+FfKtZLDVl8gAU1KeZimlUDQyV/IZjFUqP2Y/K603v7VVJcv9Im4zXvG0aG5/+yvBbrc+vI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036107; c=relaxed/simple; bh=1yv03DejpjIhtoLqLThgUtevm5kwrLeq6sL2gn4HWZE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qk8wb01H5D5g6GnOkWPWJiw+lm2aNdIuR4S62UAsHBthR2HCfWdJqVeLM7eA1Fh16zqzWBv1OtXG7ln6uEthwpki9tesRrkRJp7KDk9qWiyYPNQTvwxrhbUpvwvuLsms0g4Z16GwdmQaEgQKWKpW0hLvUNoxK0Z5t93oWWTC4jo= 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=akuSZ1rU; arc=none smtp.client-ip=192.198.163.11 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="akuSZ1rU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036105; x=1756572105; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1yv03DejpjIhtoLqLThgUtevm5kwrLeq6sL2gn4HWZE=; b=akuSZ1rU88mg76s5NkB47/2lHriyREmCqBdELWd+KNXjvdfhYAbyOgnL 7dEr9UNRm/Q6zpOSrem34fLSxM+K5yc6gV1D+Xgfx7l5Di7Od22M5A9Ln HCbXp7SGWFneHnToONc4nbwD3rdytOD3dVX0ielCbKlN6JXjaSfpOWwx3 dvQdaZR3k2NvWcVLaLgqSsi6SeiMQXDskgHHfF0eTvvDkKOf5A9NEmduW 106HIKoGalIgGZiAhk8xVhmi58QCMNqkqXMnZOrchcKyxbdEJkkHdMUrD 3Nk8P5W56AFYVmJL+/aNcxyCeKnO1XFbAkmdbxJ3LN+V+V3pmCO4L/Wlr w==; X-CSE-ConnectionGUID: FLXBmN/wR9OGKaWZLOUYjw== X-CSE-MsgGUID: WzEFAseOTuSR4boLiR7DRA== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34300083" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34300083" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:53 -0700 X-CSE-ConnectionGUID: bIs4DxTyTRKm72XcuZt/qA== X-CSE-MsgGUID: 6mqIKI+hQN6R5h4dIbKN1Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440497" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:53 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 15/16] Docs/x86/sgx: Add description for cgroup support Date: Fri, 30 Aug 2024 09:40:36 -0700 Message-ID: <20240830164038.39343-16-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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 Add initial documentation of how to regulate the distribution of SGX Enclave Page Cache (EPC) memory via the Miscellaneous cgroup controller. 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: Bagas Sanjaya Reviewed-by: Jarkko Sakkinen Acked-by: Kai Huang Tested-by: Mikko Ylinen Tested-by: Jarkko Sakkinen --- V8: - Limit text width to 80 characters to be consistent. V6: - Remove mentioning of VMM specific behavior on handling SIGBUS - Remove statement of forced reclamation, add statement to specify ENOMEM returned when no reclamation possible. - Added statements on the non-preemptive nature for the max limit - Dropped Reviewed-by tag because of changes V4: - Fix indentation (Randy) - Change misc.events file to be read-only - Fix a typo for 'subsystem' - Add behavior when VMM overcommit EPC with a cgroup (Mikko) --- Documentation/arch/x86/sgx.rst | 83 ++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/Documentation/arch/x86/sgx.rst b/Documentation/arch/x86/sgx.rst index d90796adc2ec..c537e6a9aa65 100644 --- a/Documentation/arch/x86/sgx.rst +++ b/Documentation/arch/x86/sgx.rst @@ -300,3 +300,86 @@ to expected failures and handle them as follows: first call. It indicates a bug in the kernel or the userspace client if any of the second round of ``SGX_IOC_VEPC_REMOVE_ALL`` calls has a return code other than 0. + + +Cgroup Support +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The "sgx_epc" resource within the Miscellaneous cgroup controller regulates +distribution of SGX EPC memory, which is a subset of system RAM that is us= ed to +provide SGX-enabled applications with protected memory, and is otherwise +inaccessible, i.e. shows up as reserved in /proc/iomem and cannot be +read/written outside of an SGX enclave. + +Although current systems implement EPC by stealing memory from RAM, for all +intents and purposes the EPC is independent from normal system memory, e.g= . must +be reserved at boot from RAM and cannot be converted between EPC and normal +memory while the system is running. The EPC is managed by the SGX subsyst= em and +is not accounted by the memory controller. Note that this is true only fo= r EPC +memory itself, i.e. normal memory allocations related to SGX and EPC memo= ry, +e.g. the backing memory for evicted EPC pages, are accounted, limited and +protected by the memory controller. + +Much like normal system memory, EPC memory can be overcommitted via virtual +memory techniques and pages can be swapped out of the EPC to their backing= store +(normal system memory allocated via shmem). The SGX EPC subsystem is anal= ogous +to the memory subsystem, and it implements limit and protection models for= EPC +memory. + +SGX EPC Interface Files +----------------------- + +For a generic description of the Miscellaneous controller interface files, +please see Documentation/admin-guide/cgroup-v2.rst + +All SGX EPC memory amounts are in bytes unless explicitly stated otherwise= . If +a value which is not PAGE_SIZE aligned is written, the actual value used b= y the +controller will be rounded down to the closest PAGE_SIZE multiple. + + misc.capacity + A read-only flat-keyed file shown only in the root cgroup. The sgx= _epc + resource will show the total amount of EPC memory available on the + platform. + + misc.current + A read-only flat-keyed file shown in the non-root cgroups. The sgx= _epc + resource will show the current active EPC memory usage of the cgro= up and + its descendants. EPC pages that are swapped out to backing RAM are= not + included in the current count. + + misc.max + A read-write single value file which exists on non-root cgroups. T= he + sgx_epc resource will show the EPC usage hard limit. The default is + "max". + + If a cgroup's EPC usage reaches this limit, EPC allocations, e.g.,= for + page fault handling, will be blocked until EPC can be reclaimed fr= om the + cgroup. If there are no pages left that are reclaimable within the= same + group, the kernel returns ENOMEM. + + The EPC pages allocated for a guest VM by the virtual EPC driver a= re not + reclaimable by the host kernel. In case the guest cgroup's limit is + reached and no reclaimable pages left in the same cgroup, the virt= ual + EPC driver returns SIGBUS to the user space process to indicate fa= ilure + on new EPC allocation requests. + + The misc.max limit is non-preemptive. If a user writes a limit low= er + than the current usage to this file, the cgroup will not preemptiv= ely + deallocate pages currently in use, and will only start blocking th= e next + allocation and reclaiming EPC at that time. + + misc.events + A read-only flat-keyed file which exists on non-root cgroups. + A value change in this file generates a file modified event. + + max + The number of times the cgroup has triggered a reclaim due= to + its EPC usage approaching (or exceeding) its max EPC bound= ary. + +Migration +--------- + +Once an EPC page is charged to a cgroup (during allocation), it remains ch= arged +to the original cgroup until the page is released or reclaimed. Migrating a +process to a different cgroup doesn't move the EPC charges that it incurred +while in the previous cgroup to its new cgroup. --=20 2.43.0 From nobody Fri Nov 1 04:33:25 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 54CF81BD004; Fri, 30 Aug 2024 16:41:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036102; cv=none; b=TDQnSMjvIeca3rZh5AOLKQXWhwrNkAELzQj9I1tmRngW7xVDlBkzxdmqNr0UBxy4+eDyhD09yANmkeoWA2c+eubBFXETPvvNXLCOGNOXvYa0NabESPRI89knZv3yJ3kcNwfNWRE0ArFcQTch/krjEDpRiMchPkBwIuQ/ooTbV94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725036102; c=relaxed/simple; bh=RmM+XSvPBk5DaUpLqbo3+g0JbIk2NZO23NlBJalmJ2Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uJtXUegSWVZdMK9CauoKtq4OppZqeF7x2brb63yjlRirpqeq9ORrGDZvFKtIK+SHFEI5ri9i9U6Rv5I8xi0EMu5unugWT0cg15fBdsJVkSB10jM/waQIJBRKPL8w8HeWPHd3Pq6uv+7WDK4aWTZ5G0SJaTXd1J7akN4cuwGMpGA= 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=e7pEPqEZ; arc=none smtp.client-ip=192.198.163.11 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="e7pEPqEZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725036100; x=1756572100; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RmM+XSvPBk5DaUpLqbo3+g0JbIk2NZO23NlBJalmJ2Y=; b=e7pEPqEZdNWQnPqO5Ql1knCXz+aoDMzMwVS/HlHgnPXZkZHSqZWCF4q5 AwptO8t5r7a6K2Je6LJkhHo7KxcGRAtiTTLa082MFbZQs0SUcghz2o59v iJp0hnrWZov8TSwawFpz28NH8nt+SJhO+MIc6JcHnMcJW4Wjl2JnicvUY DnPXy7847Fm3sS8uEVr2bvg1S3CLw4KpxgvVxjgfMCJ88Vn14lW6t1YKJ Er0lKPFsHMhLGqwEdlp7MXXVCl14NFv90okk+rRwz2HSbUX8xDJuydoy3 lGnKoSscwZVkedWuzql1KYqCZejr0d7z3s0VRSE8xAB29kywPoXEqMclh g==; X-CSE-ConnectionGUID: zJ8Ah4A6R2GB0U3FTOtXtg== X-CSE-MsgGUID: rbrX1TkJRKWdxW9w3wBs9A== X-IronPort-AV: E=McAfee;i="6700,10204,11180"; a="34300051" X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="34300051" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:53 -0700 X-CSE-ConnectionGUID: JBxFfYYYSOCfPigogfhCJA== X-CSE-MsgGUID: Phdp5+M4QQOXFjzCq4uSqQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,189,1719903600"; d="scan'208";a="101440500" Received: from b4969164b36c.jf.intel.com ([10.165.59.5]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2024 09:40:53 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, chenridong@huawei.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 v17 16/16] selftests/sgx: Add scripts for EPC cgroup testing Date: Fri, 30 Aug 2024 09:40:37 -0700 Message-ID: <20240830164038.39343-17-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com> References: <20240830164038.39343-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" With different cgroups, the script starts one or multiple concurrent SGX selftests (test_sgx), each to run the unclobbered_vdso_oversubscribed test case, which loads an enclave of EPC size equal to the EPC capacity available on the platform. The script checks results against the expectation set for each cgroup and reports success or failure. The script creates 3 different cgroups at the beginning with following expectations: 1) small - intentionally small enough to fail the test loading an enclave of size equal to the capacity. 2) large - large enough to run up to 4 concurrent tests but fail some if more than 4 concurrent tests are run. The script starts 4 expecting at least one test to pass, and then starts 5 expecting at least one test to fail. 3) larger - limit is the same as the capacity, large enough to run lots of concurrent tests. The script starts 8 of them and expects all pass. Then it reruns the same test with one process randomly killed and usage checked to be zero after all processes exit. The script also includes a test with low mem_cg limit and large sgx_epc limit to verify that the RAM used for per-cgroup reclamation is charged to a proper mem_cg. For this test, it turns off swapping before start, and turns swapping back on afterwards. Add README to document how to run the tests. Signed-off-by: Haitao Huang Reviewed-by: Jarkko Sakkinen Tested-by: Jarkko Sakkinen Acked-by: Kai Huang --- V13: - More improvement on handling error cases and style fixes. - Add settings file for custom timeout V12: - Integrate the scripts to the "run_tests" target. (Jarkko) V11: - Remove cgroups-tools dependency and make scripts ash compatible. (Jarkko) - Drop support for cgroup v1 and simplify. (Michal, Jarkko) - Add documentation for functions. (Jarkko) - Turn off swapping before memcontrol tests and back on after - Format and style fixes, name for hard coded values V7: - Added memcontrol test. V5: - Added script with automatic results checking, remove the interactive script. - The script can run independent from the series below. --- tools/testing/selftests/sgx/Makefile | 3 +- tools/testing/selftests/sgx/README | 109 +++++++ tools/testing/selftests/sgx/ash_cgexec.sh | 16 + tools/testing/selftests/sgx/config | 4 + .../selftests/sgx/run_epc_cg_selftests.sh | 294 ++++++++++++++++++ tools/testing/selftests/sgx/settings | 2 + .../selftests/sgx/watch_misc_for_tests.sh | 11 + 7 files changed, 438 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/sgx/README create mode 100755 tools/testing/selftests/sgx/ash_cgexec.sh create mode 100644 tools/testing/selftests/sgx/config create mode 100755 tools/testing/selftests/sgx/run_epc_cg_selftests.sh create mode 100644 tools/testing/selftests/sgx/settings create mode 100755 tools/testing/selftests/sgx/watch_misc_for_tests.sh diff --git a/tools/testing/selftests/sgx/Makefile b/tools/testing/selftests= /sgx/Makefile index 03b5e13b872b..3e673b8ace3f 100644 --- a/tools/testing/selftests/sgx/Makefile +++ b/tools/testing/selftests/sgx/Makefile @@ -20,7 +20,8 @@ ENCL_LDFLAGS :=3D -Wl,-T,test_encl.lds,--build-id=3Dnone =20 ifeq ($(CAN_BUILD_X86_64), 1) TEST_CUSTOM_PROGS :=3D $(OUTPUT)/test_sgx -TEST_FILES :=3D $(OUTPUT)/test_encl.elf +TEST_FILES :=3D $(OUTPUT)/test_encl.elf ash_cgexec.sh +TEST_PROGS :=3D run_epc_cg_selftests.sh =20 all: $(TEST_CUSTOM_PROGS) $(OUTPUT)/test_encl.elf endif diff --git a/tools/testing/selftests/sgx/README b/tools/testing/selftests/s= gx/README new file mode 100644 index 000000000000..f84406bf29a4 --- /dev/null +++ b/tools/testing/selftests/sgx/README @@ -0,0 +1,109 @@ +SGX selftests + +The SGX selftests includes a c program (test_sgx) that covers basic user s= pace +facing APIs and a shell scripts (run_sgx_cg_selftests.sh) testing SGX misc +cgroup. The SGX cgroup test script requires root privileges and runs a +specific test case of the test_sgx in different cgroups configured by the +script. More details about the cgroup test can be found below. + +All SGX selftests can run with or without kselftest framework. + +WITH KSELFTEST FRAMEWORK +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +BUILD +----- + +Build executable file "test_sgx" from top level directory of the kernel so= urce: + $ make -C tools/testing/selftests TARGETS=3Dsgx + +RUN +--- + +Run all sgx tests as sudo or root since the cgroup tests need to configure= cgroup +limits in files under /sys/fs/cgroup. + + $ sudo make -C tools/testing/selftests/sgx run_tests + +Without sudo, SGX cgroup tests will be skipped. + +On platforms with large Enclave Page Cache (EPC) and/or less cpu cores, yo= u may +need adjust the timeout in 'settings' to avoid timeouts. + +More details about kselftest framework can be found in +Documentation/dev-tools/kselftest.rst. + +WITHOUT KSELFTEST FRAMEWORK +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D + +BUILD +----- + +Build executable file "test_sgx" from this +directory(tools/testing/selftests/sgx/): + + $ make + +RUN +--- + +Run all non-cgroup tests: + + $ ./test_sgx + +To test SGX cgroup: + + $ sudo ./run_sgx_cg_selftests.sh + +THE SGX CGROUP TEST SCRIPTS +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D + +Overview of the main cgroup test script +--------------------------------------- + +With different cgroups, the script (run_sgx_cg_selftests.sh) starts one or +multiple concurrent SGX selftests (test_sgx), each to run the +unclobbered_vdso_oversubscribed test case, which loads an enclave of EPC s= ize +equal to the EPC capacity available on the platform. The script checks res= ults +against the expectation set for each cgroup and reports success or failure. + +The script creates 3 different cgroups at the beginning with following +expectations: + + 1) small - intentionally small enough to fail the test loading an enclav= e of + size equal to the capacity. + + 2) large - large enough to run up to 4 concurrent tests but fail some if= more + than 4 concurrent tests are run. The script starts 4 expecting at + least one test to pass, and then starts 5 expecting at least one + test to fail. + + 3) larger - limit is the same as the capacity, large enough to run lots = of + concurrent tests. The script starts 8 of them and expects all + pass. Then it reruns the same test with one process randomly + killed and usage checked to be zero after all processes exit. + +The script also includes a test with low mem_cg limit (memory.max) and the +'large' sgx_epc limit to verify that the RAM used for per-cgroup reclamati= on is +charged to a proper mem_cg. To validate mem_cg OOM-kills processes when its +memory.max limit is reached due to SGX EPC reclamation, the script turns o= ff +swapping before start, and turns swapping back on afterwards for this part= icular +test. + +The helper script +------------------------------------------------------ + +To monitor the SGX cgroup settings and behaviors or trouble-shoot during +testing, the helper script, watch_misc_for_tests.sh, can be used to watch +relevant entries in cgroupfs files. For example, to watch the SGX cgroup +'current' counter changes during testing, run this in a separate terminal = from +this directory: + + $ ./watch_misc_for_tests.sh current + +For more details about SGX cgroups, see "Cgroup Support" in +Documentation/arch/x86/sgx.rst. + +The scripts require cgroup v2 support. More details about cgroup v2 can be= found +in Documentation/admin-guide/cgroup-v2.rst. + diff --git a/tools/testing/selftests/sgx/ash_cgexec.sh b/tools/testing/self= tests/sgx/ash_cgexec.sh new file mode 100755 index 000000000000..cfa5d2b0e795 --- /dev/null +++ b/tools/testing/selftests/sgx/ash_cgexec.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env sh +# SPDX-License-Identifier: GPL-2.0 +# Copyright(c) 2024 Intel Corporation. + +# Start a program in a given cgroup. +# Supports V2 cgroup paths, relative to /sys/fs/cgroup +if [ "$#" -lt 2 ]; then + echo "Usage: $0 [args...]" + exit 1 +fi +# Move this shell to the cgroup. +echo 0 >/sys/fs/cgroup/$1/cgroup.procs +shift +# Execute the command within the cgroup +exec "$@" + diff --git a/tools/testing/selftests/sgx/config b/tools/testing/selftests/s= gx/config new file mode 100644 index 000000000000..e7f1db1d3eff --- /dev/null +++ b/tools/testing/selftests/sgx/config @@ -0,0 +1,4 @@ +CONFIG_CGROUPS=3Dy +CONFIG_CGROUP_MISC=3Dy +CONFIG_MEMCG=3Dy +CONFIG_X86_SGX=3Dy diff --git a/tools/testing/selftests/sgx/run_epc_cg_selftests.sh b/tools/te= sting/selftests/sgx/run_epc_cg_selftests.sh new file mode 100755 index 000000000000..dab648e0bb53 --- /dev/null +++ b/tools/testing/selftests/sgx/run_epc_cg_selftests.sh @@ -0,0 +1,294 @@ +#!/usr/bin/env sh +# SPDX-License-Identifier: GPL-2.0 +# Copyright(c) 2023, 2024 Intel Corporation. + +PROCESS_SUCCESS=3D1 +PROCESS_FAILURE=3D0 +# Wait for a process and check for expected exit status. +# +# Arguments: +# $1 - the pid of the process to wait and check. +# $2 - 1 if expecting success, 0 for failure. +# +# Return: +# 0 if the exit status of the process matches the expectation. +# 1 otherwise. +wait_check_process_status() { + pid=3D$1 + check_for_success=3D$2 + + wait "$pid" + status=3D$? + + if [ $check_for_success -eq $PROCESS_SUCCESS ] && [ $status -eq 0 ]; t= hen + echo "# Process $pid succeeded." + return 0 + elif [ $check_for_success -eq $PROCESS_FAILURE ] && [ $status -ne 0 ];= then + echo "# Process $pid returned failure." + return 0 + fi + return 1 +} + +# Wait for a set of processes and check for expected exit status +# +# Arguments: +# $1 - 1 if expecting success, 0 for failure. +# remaining args - The pids of the processes +# +# Return: +# 0 if exit status of any process matches the expectation. +# 1 otherwise. +wait_and_detect_for_any() { + check_for_success=3D$1 + + shift + detected=3D1 # 0 for success detection + + for pid in $@; do + if wait_check_process_status "$pid" "$check_for_success"; then + detected=3D0 + # Wait for other processes to exit + fi + done + + return $detected +} + +# Kselftest framework requirement - SKIP code is 4. +ksft_skip=3D4 +if [ "$(id -u)" -ne 0 ]; then + echo "SKIP: SGX cgroup tests need root privileges." + exit $ksft_skip +fi + +cg_root=3D/sys/fs/cgroup +if [ ! -d "$cg_root/$test_root_cg" ]; then + echo "SKIP: SGX cgroup tests require v2 cgroups." + exit $ksft_skip +fi +test_root_cg=3Dsgx_kselftest +#make sure we start clean +if [ -d "$cg_root/$test_root_cg" ]; then + echo "SKIP: Please clean up $cg_root/$test_root_cg." + exit $ksft_skip +fi + +test_cg_small_parent=3D$test_root_cg/sgx_test_small_parent +test_cg_large=3D$test_root_cg/sgx_test_large +test_cg_small=3D$test_cg_small_parent/sgx_test_small +test_cg_larger=3D$test_root_cg/sgx_test_larger + +clean_up() +{ + # Wait a little for cgroups to reset counters for dead processes. + sleep 2 + rmdir $cg_root/$test_cg_large + rmdir $cg_root/$test_cg_small + rmdir $cg_root/$test_cg_larger + rmdir $cg_root/$test_cg_small_parent + rmdir $cg_root/$test_root_cg +} + +mkdir $cg_root/$test_root_cg && \ +mkdir $cg_root/$test_cg_small_parent && \ +mkdir $cg_root/$test_cg_large && \ +mkdir $cg_root/$test_cg_small && \ +mkdir $cg_root/$test_cg_larger +if [ $? -ne 0 ]; then + echo "FAIL: Failed creating cgroups." + exit 1 +fi + +# Turn on misc and memory controller in non-leaf nodes +echo "+misc" > $cg_root/cgroup.subtree_control && \ +echo "+memory" > $cg_root/cgroup.subtree_control && \ +echo "+misc" > $cg_root/$test_root_cg/cgroup.subtree_control && \ +echo "+memory" > $cg_root/$test_root_cg/cgroup.subtree_control && \ +echo "+misc" > $cg_root/$test_cg_small_parent/cgroup.subtree_control +if [ $? -ne 0 ]; then + echo "FAIL: can't set up cgroups, make sure misc and memory cgroups ar= e enabled." + clean_up + exit 1 +fi + +epc_capacity=3D$(grep "sgx_epc" "$cg_root/misc.capacity" | awk '{print $2}= ') + +# This is below number of VA pages needed for enclave of capacity size. So +# should fail oversubscribed cases +epc_small_limit=3D$(( epc_capacity / 512 )) + +# At least load one enclave of capacity size successfully, maybe up to 4. +# But some may fail if we run more than 4 concurrent enclaves of capacity = size. +epc_large_limit=3D$(( epc_small_limit * 4 )) + +# Load lots of enclaves +epc_larger_limit=3D$epc_capacity +echo "# Setting up SGX cgroup limits." +echo "sgx_epc $epc_small_limit" > $cg_root/$test_cg_small_parent/misc.max = && \ +echo "sgx_epc $epc_large_limit" > $cg_root/$test_cg_large/misc.max && \ +echo "sgx_epc $epc_larger_limit" > $cg_root/$test_cg_larger/misc.max +if [ $? -ne 0 ]; then + echo "# Failed setting up misc limits for sgx_epc." + echo "SKIP: Kernel does not support SGX cgroup." + clean_up + exit $ksft_skip +fi + +test_cmd=3D"./test_sgx -t unclobbered_vdso_oversubscribed" + +echo "# Start unclobbered_vdso_oversubscribed with small EPC limit, expect= ing failure..." +./ash_cgexec.sh $test_cg_small $test_cmd >/dev/null 2>&1 +if [ $? -eq 0 ]; then + echo "FAIL: Fail on small EPC limit, not expecting any test passes." + clean_up + exit 1 +else + echo "# Test failed as expected." +fi + +echo "PASS: small EPC limit test." + +echo "# Start 4 concurrent unclobbered_vdso_oversubscribed tests with larg= e EPC limit, \ +expecting at least one success...." + +pids=3D"" +for i in 1 2 3 4; do + ( + ./ash_cgexec.sh $test_cg_large $test_cmd >/dev/null 2>&1 + ) & + pids=3D"$pids $!" +done + +if wait_and_detect_for_any $PROCESS_SUCCESS "$pids"; then + echo "PASS: large EPC limit positive testing." +else + echo "FAIL: Failed on large EPC limit positive testing, no test passes= ." + clean_up + exit 1 +fi + +echo "# Start 5 concurrent unclobbered_vdso_oversubscribed tests with larg= e EPC limit, \ +expecting at least one failure...." +pids=3D"" +for i in 1 2 3 4 5; do + ( + ./ash_cgexec.sh $test_cg_large $test_cmd >/dev/null 2>&1 + ) & + pids=3D"$pids $!" +done + +if wait_and_detect_for_any $PROCESS_FAILURE "$pids"; then + echo "PASS: large EPC limit negative testing." +else + echo "FAIL: Failed on large EPC limit negative testing, no test fails." + clean_up + exit 1 +fi + +echo "# Start 8 concurrent unclobbered_vdso_oversubscribed tests with larg= er EPC limit, \ +expecting no failure...." +pids=3D"" +for i in 1 2 3 4 5 6 7 8; do + ( + ./ash_cgexec.sh $test_cg_larger $test_cmd >/dev/null 2>&1 + ) & + pids=3D"$pids $!" +done + +if wait_and_detect_for_any $PROCESS_FAILURE "$pids"; then + echo "FAIL: Failed on larger EPC limit, at least one test fails." + clean_up + exit 1 +else + echo "PASS: larger EPC limit tests." +fi + +echo "# Start 8 concurrent unclobbered_vdso_oversubscribed tests with larg= er EPC limit,\ + randomly kill one, expecting no failure...." +pids=3D"" +for i in 1 2 3 4 5 6 7 8; do + ( + ./ash_cgexec.sh $test_cg_larger $test_cmd >/dev/null 2>&1 + ) & + pids=3D"$pids $!" +done +random_number=3D$(awk 'BEGIN{srand();print int(rand()*5)}') +sleep $((random_number + 1)) + +# Randomly select a process to kill +# Make sure usage counter not leaked at the end. +random_index=3D$(awk 'BEGIN{srand();print int(rand()*8)}') +counter=3D0 +for pid in $pids; do + if [ "$counter" -eq "$random_index" ]; then + pid_to_kill=3D$pid + break + fi + counter=3D$((counter + 1)) +done + +kill $pid_to_kill +echo "# Killed process with PID: $pid_to_kill" + +any_failure=3D0 +for pid in $pids; do + wait "$pid" + status=3D$? + if [ "$pid" !=3D "$pid_to_kill" ]; then + if [ $status -ne 0 ]; then + echo "# Process $pid returned failure." + any_failure=3D1 + fi + fi +done + +if [ $any_failure -ne 0 ]; then + echo "FAIL: Failed on random killing, at least one test fails." + clean_up + exit 1 +fi +echo "PASS: larger EPC limit test with a process randomly killed." + +mem_limit_too_small=3D$((epc_capacity - 2 * epc_large_limit)) + +echo "$mem_limit_too_small" > $cg_root/$test_cg_large/memory.max +if [ $? -ne 0 ]; then + echo "FAIL: Failed setting up memory controller." + clean_up + exit 1 +fi + +echo "# Start 4 concurrent unclobbered_vdso_oversubscribed tests with larg= e EPC limit, \ +and too small RAM limit, expecting all failures...." +# Ensure swapping off so the OOM killer is activated when mem_cgroup limit= is hit. +swapoff -a +pids=3D"" +for i in 1 2 3 4; do + ( + ./ash_cgexec.sh $test_cg_large $test_cmd >/dev/null 2>&1 + ) & + pids=3D"$pids $!" +done + +if wait_and_detect_for_any $PROCESS_SUCCESS "$pids"; then + echo "FAIL: Failed on tests with memcontrol, some tests did not fail." + clean_up + swapon -a + exit 1 +else + swapon -a + echo "PASS: large EPC limit tests with memcontrol." +fi + +sleep 2 + +epc_usage=3D$(grep '^sgx_epc' "$cg_root/$test_root_cg/misc.current" | awk = '{print $2}') +if [ "$epc_usage" -ne 0 ]; then + echo "FAIL: Final usage is $epc_usage, not 0." +else + echo "PASS: leakage check." + echo "PASS: ALL cgroup limit tests, cleanup cgroups..." +fi +clean_up +echo "# Done SGX cgroup tests." diff --git a/tools/testing/selftests/sgx/settings b/tools/testing/selftests= /sgx/settings new file mode 100644 index 000000000000..4bf7dcbf9fa8 --- /dev/null +++ b/tools/testing/selftests/sgx/settings @@ -0,0 +1,2 @@ +# This timeout may need be increased for platforms with EPC larger than 4G +timeout=3D140 diff --git a/tools/testing/selftests/sgx/watch_misc_for_tests.sh b/tools/te= sting/selftests/sgx/watch_misc_for_tests.sh new file mode 100755 index 000000000000..9280a5e0962b --- /dev/null +++ b/tools/testing/selftests/sgx/watch_misc_for_tests.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env sh +# SPDX-License-Identifier: GPL-2.0 +# Copyright(c) 2023, 2024 Intel Corporation. + +if [ -z "$1" ]; then + echo "No argument supplied, please provide 'max', 'current', or 'event= s'" + exit 1 +fi + +watch -n 1 'find /sys/fs/cgroup -wholename "*/sgx_test*/misc.'$1'" -exec \ + sh -c '\''echo "$1:"; cat "$1"'\'' _ {} \;' --=20 2.43.0