From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7F1D19DF45; Tue, 30 Apr 2024 19:51:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506671; cv=none; b=edRVdfF1OhhZshw32SL1PiKpc/PaxG/QzEXu86f7J3n6aT37LdbHQWI4zoWCOgJvlQDbsxqtAMLLCdzHh9yww1QparYG6oWtlZkxbi/fvxD88togN61IgwO+10U2Zz1SCjwmCTY1C2tWsAn2c52tBnpu8muv8yzcQHWQ3YP+o9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506671; c=relaxed/simple; bh=GPZvGvi6PM3uewi5xKf/fHnXTlqZU38tXJf5pvUdLGY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k+BuCv1VrUuQBE2nwJQRd4ifnAD3ylLmJfixtd6FfIlq9wL5bV2zo9VG8J9q91I2SFZO5tDtxJ1wpAIx2P16GX5CMTeb2q3I+Sz8yBWrKYD1wC5LOyVFh1jjkagl7JpTCSMtFPTXAZfFvgqscmLbN4iMQvgPGcenHlOPv9pUj0s= 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=HQGlaccv; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HQGlaccv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506670; x=1746042670; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GPZvGvi6PM3uewi5xKf/fHnXTlqZU38tXJf5pvUdLGY=; b=HQGlaccvqisCvw+U79xf5XEHFwVcQIyRgGOqJeWTOn0GYg6dxv4uxA1F ZxLPXlq2Q3N7xxFxU/kUARH+1qy/54+l9Kwhd0gZlpUPNuIPrHEg05e9x vXi8z18hy7/TxLBVYVmrUnK8Q2HSnlWvn1l09cs+jJ3tpuJPnaFFvG7m/ SB8TgWfl6OfX4VJl+YSdjrUujVap5qkyGD2IKCwVpBn25QKlAb1aenTTx 3nKUvj2ZXEc1S9S0VddkDWod4CdnbP/1NcY2RicbCAgHFOaxVfM21eSUS 1e0BkoS99izdnvF5P8wf+7tMqC2DKVsCelu5dELKK08Yn3tVaTkXA03j3 Q==; X-CSE-ConnectionGUID: c2F3+0HcTzWfulIqrI8SWg== X-CSE-MsgGUID: IQ9p5ODPQHKkWRT4xbINvg== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355568" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355568" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:09 -0700 X-CSE-ConnectionGUID: xhfAvDGNRUGG+vdA0LI1CQ== X-CSE-MsgGUID: oklgzl/9RIyKNWwJdY9GyA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280297" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:09 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 01/14] x86/sgx: Replace boolean parameters with enums Date: Tue, 30 Apr 2024 12:50:55 -0700 Message-Id: <20240430195108.5676-2-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 166692f2d501..d219f14365d4 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -463,14 +463,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(); @@ -542,12 +542,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. @@ -556,7 +556,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 @@ -570,7 +570,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.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3F0A19DF5E; Tue, 30 Apr 2024 19:51:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506673; cv=none; b=Vpv8Czz6bzbJrWwxtavchBvBENRmLB1SSYH2/S3DCrZm1KyrVWgdOWJ1x7+Qo19clLNrdSe1s1vi/GpNuOD6Vu1hyKSjepo1Y64vLWvk10AvZFq6SIBSeXxNUu/3ajj4t78C436LV+1AxhFQAMM39uJj/eMDFi7jqUaoFOzRZIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506673; c=relaxed/simple; bh=FiuxKeDzaIT6o2ExoNnP4zdRxGfrm0SktFxj+ktBG5g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZA1pNtHKNdR+zKJoTeZYOCi2P+A53OGlWVH5RvACbmn/bJXnoJi8Ffa0hYCHXtoYPI5I1EZcn8LazCMrvMPLnqYgKKa1SJQHL/1kIYZRanHtSETEI7HAuCxPM+xnnvrZAGUb4bqQWAVcrzq2k07i3i/dKVreJb9ooJ4wCWit/4M= 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=kExedZgB; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="kExedZgB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506672; x=1746042672; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FiuxKeDzaIT6o2ExoNnP4zdRxGfrm0SktFxj+ktBG5g=; b=kExedZgBmGq+koPCsdij2UZbzjQLBe3jU8KZNnRXkfaA6YQ/QiEmzggL eT2PCJ1VbpXB0qv8iJrrcPbgpL/Lje3AiY+lRJNZBItz0vebEpYKEa8VB s4yVodiANTy8ce6VBpQgI+c3ftz+GMIUPpQq+wN113QLVxAin04kHj2K8 7NHDchk7mN7CZx8y5UeJ6iMWguGRislmjTZIyzptWB+Hc32aM9gp71oQY 1RBAH5OcjWRsLl6YMCNg1RLyRyC3w8pvN/MON5lEowDYSs4m1ulo7pyrn McwmuCJLzS7iEZcq8FZhiQEUAhJd8a+3mYouVHCch1J05sUyi/iowJmfr Q==; X-CSE-ConnectionGUID: ss3/HclnT7iLK3PRIGy39g== X-CSE-MsgGUID: fqzvTCXhQg2Cyduhfa32Nw== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355569" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355569" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:10 -0700 X-CSE-ConnectionGUID: GTj3wyjfQYuxWh3/H1JmSA== X-CSE-MsgGUID: 8RreDekoTSi9bDMYsmW4+g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280300" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:09 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 02/14] cgroup/misc: Add per resource callbacks for CSS events Date: Tue, 30 Apr 2024 12:50:56 -0700 Message-Id: <20240430195108.5676-3-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: 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. This design passes a struct misc_cg into the callbacks. An alternative to pass only a struct misc_res was considered for encapsulating how misc_cg stores its misc_res array. However, the SGX driver would still need to access the misc_res array in the statically defined misc root_cg during initialization to set resource specific fields. Therefore, some extra getter/setter APIs to abstract such access to the misc_res array within the misc_cg struct would be needed. That seems an overkill at the moment and is deferred to later improvement when there are more users of these callbacks. 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 Reviewed-by: Kai Huang Tested-by: Jarkko Sakkinen --- 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 | 82 +++++++++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h index e799b1f8d05b..0806d4436208 100644 --- a/include/linux/misc_cgroup.h +++ b/include/linux/misc_cgroup.h @@ -27,6 +27,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. @@ -56,6 +66,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 79a3717a5803..4a602d68cf7d 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,36 @@ int misc_cg_set_capacity(enum misc_res_type type, u64 = capacity) } EXPORT_SYMBOL_GPL(misc_cg_set_capacity); =20 +/** + * misc_cg_set_ops() - set resource specific operations. + * @type: Type of the misc res. + * @ops: Operations for the given type. + * + * 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->alloc) { + pr_err("%s: alloc missing\n", __func__); + return -EINVAL; + } + + if (!ops->free) { + pr_err("%s: free missing\n", __func__); + return -EINVAL; + } + + misc_res_ops[type] =3D ops; + return 0; +} +EXPORT_SYMBOL_GPL(misc_cg_set_ops); + /** * misc_cg_cancel_charge() - Cancel the charge from the misc cgroup. * @type: Misc res type in misc cg to cancel the charge from. @@ -371,6 +404,33 @@ static struct cftype misc_cg_files[] =3D { {} }; =20 +static inline int _misc_cg_res_alloc(struct misc_cg *cg) +{ + 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); + if (misc_res_ops[i]) { + ret =3D misc_res_ops[i]->alloc(cg); + if (ret) + return ret; + } + } + + return 0; +} + +static inline void _misc_cg_res_free(struct misc_cg *cg) +{ + enum misc_res_type i; + + for (i =3D 0; i < MISC_CG_RES_TYPES; i++) + if (misc_res_ops[i]) + misc_res_ops[i]->free(cg); +} + /** * misc_cg_alloc() - Allocate misc cgroup. * @parent_css: Parent cgroup. @@ -383,20 +443,25 @@ 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; + struct misc_cg *parent_cg, *cg; + int ret; =20 if (!parent_css) { - cg =3D &root_cg; + parent_cg =3D cg =3D &root_cg; } else { cg =3D kzalloc(sizeof(*cg), GFP_KERNEL); if (!cg) return ERR_PTR(-ENOMEM); + parent_cg =3D css_misc(parent_css); } =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) { + _misc_cg_res_free(cg); + if (likely(parent_css)) + kfree(cg); + + return ERR_PTR(ret); } =20 return &cg->css; @@ -410,7 +475,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); + kfree(cg); } =20 /* Cgroup controller callbacks */ --=20 2.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B355119DF72; Tue, 30 Apr 2024 19:51:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506674; cv=none; b=ERr3CxpOeBADiVvhypu9M1VZfmxmUzwvGZTs4n+TbAVuQwTkeFsNsZK2K//Zo5ypbYETL17la2/CePe7xFm9Bt6UI8Y9P9NnaK29NmmJ+Aa8wAZc0EzBmRl4dBE0riZP4ig8BExDT44mp/wq9GmY/+RJGdnHK6VhJe59gfs5FiI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506674; c=relaxed/simple; bh=eqN2yKPP04cTarojAaLprGk14sHMHhoIfgiMAqyE0Ac=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Mc77QYMup00MuqjtlFJ+U21gfCqi0k9G9kPwGlEjNnUv6KFeQL3drq6Wznc/RODvmuPAtcKkRGIVDdCwkOIYhSLRSvvfnb5kCniCwgdU317virDUWqa6fgmJFXcEIToWraZ9Gks59B4m8QSOsffyahxlIqKs3gA8bMVshfCW8Bk= 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=m/eoPvNf; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="m/eoPvNf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506673; x=1746042673; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eqN2yKPP04cTarojAaLprGk14sHMHhoIfgiMAqyE0Ac=; b=m/eoPvNfWCUtRiR3fgXzmHzZUhcRj8RYNkGGezGnQZGY5bu0FFrHtRID JTF1GCpHPXl7GaXdpcZcK4Zx1L0fUpkY3+0XCrtwaxBoVkXEE4iP4nsP4 M2au59Jhc37DTPE4YKrEvOkWv19pwIEUGWfqz/MaNNfnXrXS7lOIZ2icb 9HkUTKgkSD0sgb0HM6a0uKWf9uiiGU9b+r6pZKy/Z9RR5iVa/PnaV/Mll gwuHkXxS+7+PESsqhhlPWt5SXWuqBdbUeZiY1Q35vL7gXWoW7h8JWywnI DslBq4jxKTFqfr7hrccFLELRsO26lW+I+wQ8rmuqdqmkWfXbwZkNflNIb g==; X-CSE-ConnectionGUID: hs2gBOaqSGODLV+FzQyo9Q== X-CSE-MsgGUID: dEAXykIlSwO1v5WgF/ZyMw== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355578" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355578" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:10 -0700 X-CSE-ConnectionGUID: HuK9eSsuQG6pF2yJOFyjPQ== X-CSE-MsgGUID: 3mi3TR56SUOIIyJPKyXLMQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280303" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:09 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 03/14] cgroup/misc: Export APIs for SGX driver Date: Tue, 30 Apr 2024 12:50:57 -0700 Message-Id: <20240430195108.5676-4-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: 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(). Export misc_cg_parent() to enable this walk. The SGX driver also needs start a global level reclamation from the root. Export 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 --- 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 | 21 ++++++++------------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h index 0806d4436208..541a5611c597 100644 --- a/include/linux/misc_cgroup.h +++ b/include/linux/misc_cgroup.h @@ -64,6 +64,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); @@ -84,6 +85,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. * @@ -108,6 +123,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 4a602d68cf7d..7d852139121a 100644 --- a/kernel/cgroup/misc.c +++ b/kernel/cgroup/misc.c @@ -43,18 +43,13 @@ 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; } +EXPORT_SYMBOL_GPL(misc_cg_root); =20 /** * valid_type() - Check if @type is valid or not. @@ -183,7 +178,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); @@ -196,12 +191,12 @@ int misc_cg_try_charge(enum misc_res_type type, struc= t misc_cg *cg, u64 amount) return 0; =20 err_charge: - for (j =3D i; j; j =3D parent_misc(j)) { + for (j =3D i; j; j =3D misc_cg_parent(j)) { atomic64_inc(&j->res[type].events); cgroup_file_notify(&j->events_file); } =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; @@ -223,7 +218,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.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 804121A0AE0; Tue, 30 Apr 2024 19:51:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506675; cv=none; b=KmNnOHR4N5GWcKctfIBMX9H5K1ui6ixewG4eVo22/SKf+5mHyfMJEydaceij5fvzMZV/ayjKxxhAwT2mP6ooashrYgU6M9TmDKHBDiSXCuDpW8mdnkztq/LljR1uVX69SVY1XtkFftmOkAMPIe1Ra/AAlMK915kPnCKVgrAlpZA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506675; c=relaxed/simple; bh=sJFW0ltM0O1BHkI+u582PzoUZyLLTIZYbeD8rl/i1fI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=n4N1jZM5P+NhSTb4kRWZND58SY16IUN5hv+246JWOxYfRyPJh1QAKm6gxb2ofEOdNqUD7eDc39wRsXMd4mwmjdqCRMrWSBWGhfpRUj1DlXQzaGBiBKKVRgEDfKnti+ODuKFIE7zMzpQ+13ZYtS5wt6G+3/6AfI4NF0CgEsUONeI= 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=Dg4vw4Us; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Dg4vw4Us" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506674; x=1746042674; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sJFW0ltM0O1BHkI+u582PzoUZyLLTIZYbeD8rl/i1fI=; b=Dg4vw4UsAQpXScTpGBfLrl4UwMSOroo+yiHjBW/oohyKgknVAOJLvL63 wYU6NMjJE8yV/WlhtcQ9HDedRthHdZgK3sJkCJzONMPU+g5pUBoGBucwu PEcRBvuY0ZsijdHN05ZGMn+BtznuxSLUEVg5Fvuf3Fld5mD+jj1/p1tqM NUV5lMEG9jT9P5vRSkY3gyeR/Uq2dGnLZHDPDSuqP+Y/+la6vLUU8wjHt kv72cV0/gXL1/YJpzJ4JOd9XpRST6LsKKHMyE/v2xTuF+wUg01T1GwKVr rv3CmWGz0yHvEngfEvXC3QhwI6n8hPS2/sQUIn6JJzv2DCwMJxNF7eGu2 w==; X-CSE-ConnectionGUID: EXfjhI7SRzyQxVfPqrqzWg== X-CSE-MsgGUID: JIvjvmxpQc+5wldY0RNKfQ== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355588" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355588" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:10 -0700 X-CSE-ConnectionGUID: bJ/wsL4tQaevpbYxy7+Nwg== X-CSE-MsgGUID: qCjyJEOFQbGK+BPsQXrJZA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280306" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:09 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 04/14] cgroup/misc: Add SGX EPC resource type Date: Tue, 30 Apr 2024 12:50:58 -0700 Message-Id: <20240430195108.5676-5-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: 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 541a5611c597..440ed2bb8053 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, /* AMD SEV-ES ASIDs resource */ MISC_CG_RES_SEV_ES, +#endif +#ifdef CONFIG_X86_SGX + /* SGX EPC memory resource */ + MISC_CG_RES_SGX_EPC, #endif MISC_CG_RES_TYPES }; diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c index 7d852139121a..863f9147602b 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.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA0AE1A0AF0; Tue, 30 Apr 2024 19:51:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506675; cv=none; b=iF+aIiZCNI6cGBQJ9erJAkqW+ZdnOPlmTnYa0rElfiX42XlBgMpRY33362fgkc5IiiEiF7OsJ/WQVsC6yJ1nVFXbRdgaV7MX3x5GlTAjK36v2w0C4CQnpciBJEtPNlxa1RZkV1p20FfaRv5IRWg+rneWe2HuRECsbwOOGLh18a4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506675; c=relaxed/simple; bh=rT52NBmeUzFxtsFnovjnWlw09IBD1D0aOlTsU/mpOao=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HtmhGYMY1PWeoiOq1BAQQCVnNIzzr9+BgWMNRzrHW7I72JTFl9+UtUUt+7+b9EA3negpPG35dshvPzVNab8o3Bq96bNGK9RJAzMN6gBDq/geCXMOOHohwkWugj0E+zdUewA74YwI3fI9qrw/AP4eyASSB6R+h5PjFBnzvCYjCk8= 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=R2MnmrXP; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="R2MnmrXP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506674; x=1746042674; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rT52NBmeUzFxtsFnovjnWlw09IBD1D0aOlTsU/mpOao=; b=R2MnmrXP88LilRRN6Ret2f9wFidpGAK+BzPOmuLI3A0AUUYYLWc7nSjb ql6MrrjgpcZoPvNKXkDWobZs0vPZocHg+MZZ/KeA5HCTZZsVaLscDxpW9 x//xQkvXEtUzoov21vgZCcuQq98OVsgleXczuBuc3O/BSw8hlj7M0RrHg EA8kQGQRxHyzZ8yAeuObyEIETUrCTqIO/61F2Rms45hJS9878Swh8cfla L7UGEvgqgHa0uLLSbJYqn1UXkih0w6N8OfL5NuZDeNYUsQiRlOO8P1mhb 1sid1tmD5ftA4qqs3Y2EK3LUCU5COmiFXMKntG6xb8OfSzezn10A6SGjF w==; X-CSE-ConnectionGUID: DDUfwuBeQLONKeTjhQOoAg== X-CSE-MsgGUID: cem/3D9eSqyglNsdmrBkAw== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355598" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355598" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:10 -0700 X-CSE-ConnectionGUID: mCRbP32yRny7Ohty1cBh1w== X-CSE-MsgGUID: Xzr64fAeQxeLfF2//Tydag== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280310" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:09 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 05/14] x86/sgx: Implement basic EPC misc cgroup functionality Date: Tue, 30 Apr 2024 12:50:59 -0700 Message-Id: <20240430195108.5676-6-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: 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 --- 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 | 71 +++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/epc_cgroup.h | 72 ++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/main.c | 42 +++++++++++++++- arch/x86/kernel/cpu/sgx/sgx.h | 21 ++++++++ include/linux/misc_cgroup.h | 2 + 6 files changed, 207 insertions(+), 2 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..5c484fd10160 --- /dev/null +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -0,0 +1,71 @@ +// 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, +}; + +void sgx_cgroup_init(void) +{ + misc_cg_set_ops(MISC_CG_RES_SGX_EPC, &sgx_cgroup_ops); + sgx_cgroup_misc_init(misc_cg_root(), &sgx_cg_root); +} 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..bd9606479e67 --- /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 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 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 d219f14365d4..1226ea0d5b3c 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -17,6 +17,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; @@ -558,7 +559,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(); @@ -567,8 +577,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); @@ -584,6 +596,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 @@ -602,8 +623,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; @@ -643,6 +672,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 @@ -787,6 +818,7 @@ static void __init arch_update_sysfs_visibility(int nid= ) {} static bool __init sgx_page_cache_init(void) { u32 eax, ebx, ecx, edx, type; + u64 capacity =3D 0; u64 pa, size; int nid; int i; @@ -837,6 +869,7 @@ static bool __init sgx_page_cache_init(void) =20 sgx_epc_sections[i].node =3D &sgx_numa_nodes[nid]; sgx_numa_nodes[nid].size +=3D size; + capacity +=3D size; =20 sgx_nr_epc_sections++; } @@ -846,6 +879,8 @@ static bool __init sgx_page_cache_init(void) return false; } =20 + misc_cg_set_capacity(MISC_CG_RES_SGX_EPC, capacity); + return true; } =20 @@ -942,6 +977,9 @@ static int __init sgx_init(void) if (sgx_vepc_init() && ret) goto err_provision; =20 + /* Setup cgroup if either the native or vepc driver is active */ + 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..fae8eef10232 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. diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h index 440ed2bb8053..c9b47a5e966a 100644 --- a/include/linux/misc_cgroup.h +++ b/include/linux/misc_cgroup.h @@ -46,11 +46,13 @@ struct misc_res_ops { * @max: Maximum limit on 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; atomic64_t usage; atomic64_t events; + void *priv; }; =20 /** --=20 2.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E2EA1A38DF; Tue, 30 Apr 2024 19:51:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506676; cv=none; b=VDCXtw6ZQX/f5TXDI1xqYHk2e4UWIYg3V39f621JRjGIdyq5swD/lexNqoZeqKFYUB7sOuUeTNVv0GtVxHCjmuCyE3IYG5Kh3d+SRREA2kdw59X1Br2LO7WreR1IOc/HSxznRh5cww1gGbUgdmas9i/XDncdUmIqekSxTqkYTas= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506676; c=relaxed/simple; bh=o2sZVV9T8C2/6mebL8Cg/D4rmwm+HgGq3e+Nu1tzHr8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dbuV5dabsIZrkmzCW1mJaFc7EB5Rkge2TnODxPpsqbHb6L4tgjxRxdCgynVLfN7eq62wqUpOLY6GqjJfa/NwS+p8scWcWF6Qozjb2k2pYyjmjMWNiGENNF8Yk6Ie2wwMOvZiSDEjncMTeAN5Tf3X5oHcH/oUWzaNG+UpeHqwmjg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=PckxnKAD; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="PckxnKAD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506676; x=1746042676; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=o2sZVV9T8C2/6mebL8Cg/D4rmwm+HgGq3e+Nu1tzHr8=; b=PckxnKADNKQSipbUikpJevxdnc2XRluKwtS1TL7b4EiRE9DiM7HRFFrz ReLRGR0U2v/qHKJAwofX1EmgEbBapRbGZxvE84Z+lCAZceEwCcn83kErl CPRKJoBiwaPgUg+4jVPKV09xntv8YkMu886J7Y/sKj1JwtwgWVF4SacYb jjIvnYwPsJ4oXifl5vw7Hn/u0WykuPKHW+szMfAv5rjziHx1/UOX4rfvY UI6BrpriO+Yr44PAK17d8vePA7GXqHV2sbye2+Gh9UNFeBOtlwkc13XW9 BMXbT0tCT0i5zU8WZZdm7B4Hqbb4dXWe32pQO0ZchO4RB7Y74MhRxrrZJ A==; X-CSE-ConnectionGUID: e7+oDhUxQaGagPEHCAg4jA== X-CSE-MsgGUID: d9TeIHTKTvaGGQIvadrksg== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355607" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355607" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:10 -0700 X-CSE-ConnectionGUID: mHtn3ZRxT7iGu0TI8dOEAg== X-CSE-MsgGUID: BtRd/gZ2SOKjmdKRmJk6QA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280313" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:09 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 06/14] x86/sgx: Add sgx_epc_lru_list to encapsulate LRU list Date: Tue, 30 Apr 2024 12:51:00 -0700 Message-Id: <20240430195108.5676-7-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sean Christopherson Introduce a data structure to wrap the existing reclaimable list and its spinlock. Each cgroup later will have one instance of this structure to track EPC pages allocated for processes associated with the same cgroup. Just like the global SGX reclaimer (ksgxd), an EPC cgroup reclaims pages from the reclaimable list in this structure when its usage reaches near its limit. Use this structure to encapsulate the LRU list and its lock used by the global reclaimer. Signed-off-by: Sean Christopherson Co-developed-by: Kristen Carlson Accardi Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Cc: Sean Christopherson Reviewed-by: Jarkko Sakkinen Reviewed-by: Kai Huang Tested-by: Jarkko Sakkinen --- V6: - removed introduction to unreclaimables in commit message. V4: - Removed unneeded comments for the spinlock and the non-reclaimables. (Kai, Jarkko) - Revised the commit to add introduction comments for unreclaimables and multiple LRU lists.(Kai) - Reordered the patches: delay all changes for unreclaimables to later, and this one becomes the first change in the SGX subsystem. V3: - Removed the helper functions and revised commit messages. --- arch/x86/kernel/cpu/sgx/main.c | 39 +++++++++++++++++----------------- arch/x86/kernel/cpu/sgx/sgx.h | 15 +++++++++++++ 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 1226ea0d5b3c..59736dd02ca7 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -27,10 +27,9 @@ static DEFINE_XARRAY(sgx_epc_address_space); =20 /* * These variables are part of the state of the reclaimer, and must be acc= essed - * with sgx_reclaimer_lock acquired. + * with sgx_global_lru.lock acquired. */ -static LIST_HEAD(sgx_active_page_list); -static DEFINE_SPINLOCK(sgx_reclaimer_lock); +static struct sgx_epc_lru_list sgx_global_lru; =20 static atomic_long_t sgx_nr_free_pages =3D ATOMIC_LONG_INIT(0); =20 @@ -305,13 +304,13 @@ static void sgx_reclaim_pages(void) int ret; int i; =20 - spin_lock(&sgx_reclaimer_lock); + spin_lock(&sgx_global_lru.lock); for (i =3D 0; i < SGX_NR_TO_SCAN; i++) { - if (list_empty(&sgx_active_page_list)) + epc_page =3D list_first_entry_or_null(&sgx_global_lru.reclaimable, + struct sgx_epc_page, list); + if (!epc_page) break; =20 - epc_page =3D list_first_entry(&sgx_active_page_list, - struct sgx_epc_page, list); list_del_init(&epc_page->list); encl_page =3D epc_page->owner; =20 @@ -323,7 +322,7 @@ static void sgx_reclaim_pages(void) */ epc_page->flags &=3D ~SGX_EPC_PAGE_RECLAIMER_TRACKED; } - spin_unlock(&sgx_reclaimer_lock); + spin_unlock(&sgx_global_lru.lock); =20 for (i =3D 0; i < cnt; i++) { epc_page =3D chunk[i]; @@ -346,9 +345,9 @@ static void sgx_reclaim_pages(void) continue; =20 skip: - spin_lock(&sgx_reclaimer_lock); - list_add_tail(&epc_page->list, &sgx_active_page_list); - spin_unlock(&sgx_reclaimer_lock); + spin_lock(&sgx_global_lru.lock); + list_add_tail(&epc_page->list, &sgx_global_lru.reclaimable); + spin_unlock(&sgx_global_lru.lock); =20 kref_put(&encl_page->encl->refcount, sgx_encl_release); =20 @@ -379,7 +378,7 @@ static void sgx_reclaim_pages(void) static bool sgx_should_reclaim(unsigned long watermark) { return atomic_long_read(&sgx_nr_free_pages) < watermark && - !list_empty(&sgx_active_page_list); + !list_empty(&sgx_global_lru.reclaimable); } =20 /* @@ -431,6 +430,8 @@ static bool __init sgx_page_reclaimer_init(void) =20 ksgxd_tsk =3D tsk; =20 + sgx_lru_init(&sgx_global_lru); + return true; } =20 @@ -506,10 +507,10 @@ static struct sgx_epc_page *__sgx_alloc_epc_page(void) */ void sgx_mark_page_reclaimable(struct sgx_epc_page *page) { - spin_lock(&sgx_reclaimer_lock); + spin_lock(&sgx_global_lru.lock); page->flags |=3D SGX_EPC_PAGE_RECLAIMER_TRACKED; - list_add_tail(&page->list, &sgx_active_page_list); - spin_unlock(&sgx_reclaimer_lock); + list_add_tail(&page->list, &sgx_global_lru.reclaimable); + spin_unlock(&sgx_global_lru.lock); } =20 /** @@ -524,18 +525,18 @@ void sgx_mark_page_reclaimable(struct sgx_epc_page *p= age) */ int sgx_unmark_page_reclaimable(struct sgx_epc_page *page) { - spin_lock(&sgx_reclaimer_lock); + spin_lock(&sgx_global_lru.lock); if (page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) { /* The page is being reclaimed. */ if (list_empty(&page->list)) { - spin_unlock(&sgx_reclaimer_lock); + spin_unlock(&sgx_global_lru.lock); return -EBUSY; } =20 list_del(&page->list); page->flags &=3D ~SGX_EPC_PAGE_RECLAIMER_TRACKED; } - spin_unlock(&sgx_reclaimer_lock); + spin_unlock(&sgx_global_lru.lock); =20 return 0; } @@ -577,7 +578,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, en= um sgx_reclaim reclaim) break; } =20 - if (list_empty(&sgx_active_page_list)) { + if (list_empty(&sgx_global_lru.reclaimable)) { page =3D ERR_PTR(-ENOMEM); break; } diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index fae8eef10232..3cf5a59a4eac 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -114,6 +114,21 @@ static inline void *sgx_get_epc_virt_addr(struct sgx_e= pc_page *page) return section->virt_addr + index * PAGE_SIZE; } =20 +/* + * Contains EPC pages tracked by the global reclaimer (ksgxd) or an EPC + * cgroup. + */ +struct sgx_epc_lru_list { + spinlock_t lock; + struct list_head reclaimable; +}; + +static inline void sgx_lru_init(struct sgx_epc_lru_list *lru) +{ + spin_lock_init(&lru->lock); + INIT_LIST_HEAD(&lru->reclaimable); +} + void sgx_free_epc_page(struct sgx_epc_page *page); =20 void sgx_reclaim_direct(void); --=20 2.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A84311BED61; Tue, 30 Apr 2024 19:51:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506677; cv=none; b=VmqAIdNDujbYH9A80ChYJpDCX+iuu0bNe3OyOgAio8RLykTPTocZEvbsowr3sd/4IB8pcDLhb9i7UhssfJMDu+p8bzDpCn+Zd+YpYtAZ/o16QBkUcsBJ6cwPV4d9SeK+agfaMS0KWLHu0fT2jW0OP5vhlYL7np/4o4/2VKezKBg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506677; c=relaxed/simple; bh=8yj/lS6EpRslsBVdm+2I5U+2edHK+4A85pLtHuDrUTk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dGc5irtRkHQDNKAR2G9JoMlZVEAPRdjGFpV2sSMKiy/SuX3jdGXi0jDYTYHpLOudQDTr+LJv6SE2ZDswbjbLkqk6ZShYJm1D9O+6/7OglBrU7PJ8eRzYWlB7Km8cL4SP0il+BDD7M5DJpqbmY3fR7qqFpKZ38umgJovzjPLMGRk= 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=TDQboc9l; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="TDQboc9l" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506676; x=1746042676; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8yj/lS6EpRslsBVdm+2I5U+2edHK+4A85pLtHuDrUTk=; b=TDQboc9loioYkYY8/DSgFx5+1Huzsz2M8Kkhod8SjoYU+Zx5t4/xWTtr EeEevPfh6H5iU5fHuLBQTxDDwZ0aLbDXQWwHhujcdQtgd0vwrMjhgCiHq xzW/dmZlthh3QluMf45545g3dtpwVf55/gFMzfBqz9y9i+HVG/uNFUcNy 7pSyWeCbGcI/wyeDRkHh2AoLTc7E4axpvJEpsKqjAFCBl7OAtZGpq5tQr caNfDw7p7PDMPc23w8jtSkTfswOAXh8zs2IqVRBFTq65S4mHWdsO6cdRh BzCSy3o1+0VyCPmQz4vSjYS12KkZCOOi3M3C3ImdAXZ5d0n2RY+nfipvf g==; X-CSE-ConnectionGUID: 5NQLWrIFTgGs7Xe334FZjQ== X-CSE-MsgGUID: yB6g2oRCSrKJhW73sE7Vbg== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355616" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355616" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:11 -0700 X-CSE-ConnectionGUID: BKvdZbL5QsmrVP/AtFEu4g== X-CSE-MsgGUID: T0Y47VMqRXG16cPUKqCySw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280316" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:09 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 07/14] x86/sgx: Abstract tracking reclaimable pages in LRU Date: Tue, 30 Apr 2024 12:51:01 -0700 Message-Id: <20240430195108.5676-8-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: 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 59736dd02ca7..2bf9cca3be21 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -31,6 +31,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. */ @@ -499,25 +504,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, @@ -525,18 +529,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.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75E081BED8F; Tue, 30 Apr 2024 19:51:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506678; cv=none; b=DKIxiPmX3ZxORBGCqJ5XoND2tsq+Uf6W1UFKS4/N9m7E+nY9Etspm8GZLBW9Kd20ZUtYH38N0c8H1P927jWrxHP0gD4Amw621vusTZ73/DO19TjRPdrJgHOhkL/SgsxieG/rhbHItqBJNUEqnK8ikQf0XC8RMPCpXPhxmsnvr5I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506678; c=relaxed/simple; bh=PugzS5EKRz2QdYLaT+4rmoqFNrMd2wbigD4HBPDiqlE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JQ8h2OFkMi287WoYCTMqwLiox+w+gYIqzqbSDLuRPtMB4f2mRw71S1ET7hMxqYtJr28+ppi6dAM4pVgt+Ik7V+4RyK/6UfWvqSXuAC4DqE8JDe9YDKapyCKIHoqyIoblYUzD2vmrurNFXRLscHG3lNad48vSOhQvosl6HabYyi8= 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=nwkGR22F; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nwkGR22F" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506677; x=1746042677; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PugzS5EKRz2QdYLaT+4rmoqFNrMd2wbigD4HBPDiqlE=; b=nwkGR22FVC1/kHI5kMhbR82Vkwh7HMXdjGbyGWZUSTClkQDoxad6xLWC pm+bdecIgdEcWEZaDqqcXlY8sd0IFdmyGARs5y2XugZLRsNriqXlPyxPQ KV9qKxkauO2Al/0jcvwUgkWVxlSUsCMjQMEojEH/6s1eIxT8w7+FkMmOU 1OU27+o5jGdQy8XxzXz2AswK1/nicwLbBoEAwMAe+geYLIcEos3e/rFKJ qxNTtG5pZ+SDT1gKarTDL3SkmGRRwKro4i4uCWySeDxe6b/8REFLfLFCl yJsteWxHaAdJ+zcfX40GynReSacz7tX2/Rg+WVdxVFCT6QVIqJE2afw1I g==; X-CSE-ConnectionGUID: kT+g45X9RTyNAKs0n9dvEw== X-CSE-MsgGUID: +ZVKwdjiSyebH/DGMIl6fQ== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355634" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355634" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:11 -0700 X-CSE-ConnectionGUID: eW41EU/UTiaPFJh94i2fuA== X-CSE-MsgGUID: lyjLJE5vTimDbfARcajb0g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280319" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:10 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 08/14] x86/sgx: Add basic EPC reclamation flow for cgroup Date: Tue, 30 Apr 2024 12:51:02 -0700 Message-Id: <20240430195108.5676-9-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kristen Carlson Accardi 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 (and/or its descendants) to reduce its usage so the new allocation can succeed. Add the basic building blocks to support per-cgroup reclamation. Currently the kernel only has one place to reclaim EPC pages: the global EPC LRU list. To support the "per-cgroup" EPC reclaim, maintain an LRU list for each EPC cgroup, and introduce a "cgroup" variant function to reclaim EPC pages from a given EPC cgroup and its descendants. Currently the kernel does the global EPC reclaim in sgx_reclaim_page(). It always tries to reclaim EPC pages in batch of SGX_NR_TO_SCAN (16) pages. Specifically, it always "scans", or "isolates" SGX_NR_TO_SCAN pages from the global LRU, and then tries to reclaim these pages at once for better performance. Implement the "cgroup" variant EPC reclaim in a similar way, but keep the implementation simple: 1) change sgx_reclaim_pages() to take an LRU as input, and return the pages that are "scanned" and attempted for reclamation (but not necessarily reclaimed successfully); 2) loop the given EPC cgroup and its descendants and do the new sgx_reclaim_pages() until SGX_NR_TO_SCAN pages are "scanned". This implementation, encapsulated in sgx_cgroup_reclaim_pages(), always tries to reclaim SGX_NR_TO_SCAN pages from the LRU of the given EPC cgroup, and only moves to its descendants when there's no enough reclaimable EPC pages to "scan" in its LRU. It should be enough for most cases. 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): 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. Alternatively, the current sgx_reclaim_pages() could be changed to return the actual "reclaimed" pages, but not "scanned" pages. However, the reclamation is a lengthy process, forcing a successful reclamation of predetermined number of pages may block the caller for too long. And that may not be acceptable in some synchronous contexts, e.g., in serving an ioctl(). With this building block in place, add synchronous reclamation support in sgx_cgroup_try_charge(): trigger a call to sgx_cgroup_reclaim_pages() if the cgroup reaches its limit and the caller allows synchronous reclaim as indicated by s newly added parameter. A later patch will add support for asynchronous reclamation reusing sgx_cgroup_reclaim_pages(). Note all reclaimable EPC pages are still tracked in the global LRU thus no per-cgroup reclamation is actually active at the moment. 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 Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Reviewed-by: Kai Huang Tested-by: Jarkko Sakkinen --- 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 | 119 ++++++++++++++++++++++++++- arch/x86/kernel/cpu/sgx/epc_cgroup.h | 5 +- arch/x86/kernel/cpu/sgx/main.c | 45 ++++++---- arch/x86/kernel/cpu/sgx/sgx.h | 1 + 4 files changed, 148 insertions(+), 22 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c index 5c484fd10160..3602616726ff 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -8,16 +8,128 @@ static struct sgx_cgroup sgx_cg_root; =20 /** - * 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)) + break; + + 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; +} + +/** + * sgx_cgroup_reclaim_pages() - reclaim EPC from a cgroup tree + * @root: The root of cgroup tree to reclaim from. + * + * This function performs a pre-order walk in the cgroup tree under the gi= ven + * root, attempting to reclaim pages at each node until a fixed number of = pages + * (%SGX_NR_TO_SCAN) are attempted for reclamation. No guarantee of succes= s on + * the actual reclamation process. In extreme cases, if all pages in front= of + * the LRUs are recently accessed, i.e., considered "too young" to reclaim= , no + * page will actually be reclaimed after walking the whole tree. + */ +static void sgx_cgroup_reclaim_pages(struct misc_cg *root) +{ + struct cgroup_subsys_state *css_root; + struct cgroup_subsys_state *pos; + struct sgx_cgroup *sgx_cg; + unsigned int cnt =3D 0; + + /* 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)) + break; + rcu_read_unlock(); + + sgx_cg =3D sgx_cgroup_from_misc_cg(css_misc(pos)); + cnt +=3D sgx_reclaim_pages(&sgx_cg->lru); + + rcu_read_lock(); + css_put(pos); + + if (cnt >=3D SGX_NR_TO_SCAN) + break; + } + + rcu_read_unlock(); +} + +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) + return ret; + + if (reclaim =3D=3D SGX_NO_RECLAIM) + return -ENOMEM; + + sgx_cgroup_reclaim_pages(sgx_cg->cg); + cond_resched(); + } + + return 0; } =20 /** @@ -42,6 +154,7 @@ static void sgx_cgroup_free(struct misc_cg *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; } diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx= /epc_cgroup.h index bd9606479e67..538524f5669d 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,7 @@ static inline void sgx_cgroup_init(void) { } =20 struct sgx_cgroup { struct misc_cg *cg; + struct sgx_epc_lru_list lru; }; =20 static inline struct sgx_cgroup *sgx_cgroup_from_misc_cg(struct misc_cg *c= g) @@ -63,7 +64,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 sgx_cgroup_init(void); =20 diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 2bf9cca3be21..4eb0d417f7b6 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -285,11 +285,14 @@ static void sgx_reclaimer_write(struct sgx_epc_page *= epc_page, mutex_unlock(&encl->lock); } =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. +/** + * 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 + * 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 @@ -297,8 +300,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(void) +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]; @@ -309,10 +314,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 @@ -327,7 +331,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]; @@ -350,9 +354,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 @@ -378,6 +382,8 @@ static void sgx_reclaim_pages(void) =20 sgx_free_epc_page(epc_page); } + + return cnt; } =20 static bool sgx_should_reclaim(unsigned long watermark) @@ -386,6 +392,11 @@ static bool sgx_should_reclaim(unsigned long watermark) !list_empty(&sgx_global_lru.reclaimable); } =20 +static void sgx_reclaim_pages_global(void) +{ + sgx_reclaim_pages(&sgx_global_lru); +} + /* * sgx_reclaim_direct() should be called (without enclave's mutex held) * in locations where SGX memory resources might be low and might be @@ -394,7 +405,7 @@ static bool sgx_should_reclaim(unsigned long watermark) void sgx_reclaim_direct(void) { if (sgx_should_reclaim(SGX_NR_LOW_PAGES)) - sgx_reclaim_pages(); + sgx_reclaim_pages_global(); } =20 static int ksgxd(void *p) @@ -417,7 +428,7 @@ static int ksgxd(void *p) sgx_should_reclaim(SGX_NR_HIGH_PAGES)); =20 if (sgx_should_reclaim(SGX_NR_HIGH_PAGES)) - sgx_reclaim_pages(); + sgx_reclaim_pages_global(); =20 cond_resched(); } @@ -571,7 +582,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); @@ -599,7 +610,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 diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index 3cf5a59a4eac..89adac646381 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -135,6 +135,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.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27AA719DF45; Tue, 30 Apr 2024 19:51:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506679; cv=none; b=jOcxoIsVu5BYSsMVKvEqHl+rjdZz9+kmljBSczTZNc30FMJSBx+3EP70DT37GIEZ79j9ctQSXoDJgrsl8BiDaW/Z1l/+q/Rirb+/cPUuStuPKP75l0kawG4eawsYUQV0aQzYzJKtve84gLIl/HNfspXusFKd5/quRtR98V5QgMk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506679; c=relaxed/simple; bh=vd67voYNeFYGJbH8NvBLVIFAr3MlRQX4iimkRSDJacQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lUPUCrjk5C+B/GfOn5ZQ3/QLCtcB98HW9c8Ul0QohnP0ByLs9NGSC/lhF8++7/4pz5hhX1y7JutDOpZBUt0NY2RxcrZeXoJ+hyE5N5zYo8fS74cKWwKweiZ1jtFETXaFyhQ1NXh+t8VkZcA9hHDFUfid/GMog+m89moA3sUulBg= 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=k6lbRfFU; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="k6lbRfFU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506678; x=1746042678; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vd67voYNeFYGJbH8NvBLVIFAr3MlRQX4iimkRSDJacQ=; b=k6lbRfFUjLBaNyaP+IKYx4pR0BsvC+Zt/Bb74R5SF5TwJS3N4alpCGwc h7di8xx8h0nfe/OsjCHAvGpMdZCbcxgBsP/iqTUr7NxaSzH/WBN3FDbmP NFt8vBdTK3gTKtHzTSUqbLpjik4LD7cU+lWlxzTU8PWirSuQV4C1QPuwl 1gIFNuyBCLPQhJl4J0KEM4/u7CCpl5XayDgwyi/LqvfCITcjxMMhPWE0H ORor89vPcVwGD1c0LxZRXA0L6LU+2hF+CvEtkUoRdk85FEhCUXd+1OQF2 dwtzRRGiKxMkpy3Bj8fmk+jhCShz/yuxA3t+32AkPyvfcUwWIiojsjJdK A==; X-CSE-ConnectionGUID: I+8AmNJCTPmGlc3zAyu9dQ== X-CSE-MsgGUID: CiiySe6FSoynOgWfIhaFfA== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355643" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355643" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:11 -0700 X-CSE-ConnectionGUID: DMR1U50CRAyuvWlnaBcinQ== X-CSE-MsgGUID: owEN2AdwTpCPEG1s8xWpFw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280323" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:10 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 09/14] x86/sgx: Implement async reclamation for cgroup Date: Tue, 30 Apr 2024 12:51:03 -0700 Message-Id: <20240430195108.5676-10-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: 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 be triggered to avoid blocking the page fault handling. Create a workqueue, corresponding work item and function definitions for EPC cgroup to support the asynchronous reclamation. In sgx_cgroup_try_charge(), if caller does not allow synchronous reclamation, queue an asynchronous work into the workqueue. Reclaiming only when the usage is at or very close to the limit would cause thrashing. To avoid that, before returning from sgx_cgroup_try_charge(), check the need for reclamation (usage too close to the limit), queue an async work if needed, similar to how the global reclaimer wakes up its reclaiming thread after each allocation in sgx_alloc_epc_pages(). 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 --- 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 | 135 ++++++++++++++++++++++++++- arch/x86/kernel/cpu/sgx/epc_cgroup.h | 1 + 2 files changed, 134 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c index 3602616726ff..6368611cb29e 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -4,9 +4,63 @@ #include #include "epc_cgroup.h" =20 +/* + * The minimal free pages 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; + +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 @@ -89,6 +143,61 @@ static void sgx_cgroup_reclaim_pages(struct misc_cg *ro= ot) rcu_read_unlock(); } =20 +/** + * sgx_cgroup_should_reclaim() - check if EPC reclamation is needed for a = cgroup + * @sgx_cg: The cgroup to be checked. + * + * This function can be used to guard a call to sgx_cgroup_reclaim_pages()= where + * the minimal number of free page needs be maintained for the cgroup to m= ake + * good forward progress. + * + * Return: %true if number of free pages available for the cgroup below a + * threshold (%SGX_CG_MIN_FREE_PAGE) and there are reclaimable pages withi= n the + * 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 number of free pages + * so there is always a few pages available to serve new allocation + * requests 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 *sgx_cg =3D container_of(work, struct sgx_cgroup, recla= im_work); + + /* + * This work func is scheduled by sgx_cgroup_try_charge() when it cannot + * directly reclaim, i.e., EPC allocation in a fault handler. Waiting to + * reclaim until the cgroup is actually at its limit is less performant, + * as it means the task scheduling this asynchronous work is effectively + * blocked until a worker makes its way through the global work queue. + */ + while (sgx_cgroup_should_reclaim(sgx_cg)) { + sgx_cgroup_reclaim_pages(sgx_cg->cg); + 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)) @@ -122,13 +231,18 @@ int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, = enum sgx_reclaim reclaim) if (ret !=3D -EBUSY) return ret; =20 - if (reclaim =3D=3D SGX_NO_RECLAIM) - return -ENOMEM; + if (reclaim =3D=3D SGX_NO_RECLAIM) { + queue_work(sgx_cg_wq, &sgx_cg->reclaim_work); + return -EBUSY; + } =20 sgx_cgroup_reclaim_pages(sgx_cg->cg); cond_resched(); } =20 + if (sgx_cgroup_should_reclaim(sgx_cg)) + queue_work(sgx_cg_wq, &sgx_cg->reclaim_work); + return 0; } =20 @@ -149,12 +263,14 @@ static void sgx_cgroup_free(struct misc_cg *cg) if (!sgx_cg) return; =20 + cancel_work_sync(&sgx_cg->reclaim_work); 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); + 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; } @@ -179,6 +295,21 @@ const struct misc_res_ops sgx_cgroup_ops =3D { =20 void sgx_cgroup_init(void) { + /* + * misc root always exists even if misc is disabled from command line. + * Initialize properly. + */ misc_cg_set_ops(MISC_CG_RES_SGX_EPC, &sgx_cgroup_ops); sgx_cgroup_misc_init(misc_cg_root(), &sgx_cg_root); + + /* + * Only alloc additional resource for workqueue when misc is enabled. + * User can disable sgx or disable misc to avoid the failure + */ + if (cgroup_subsys_enabled(misc_cgrp_subsys)) { + sgx_cg_wq =3D alloc_workqueue("sgx_cg_wq", WQ_UNBOUND | WQ_FREEZABLE, + WQ_UNBOUND_MAX_ACTIVE); + BUG_ON(!sgx_cg_wq); + } + } diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx= /epc_cgroup.h index 538524f5669d..2044e0d64076 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.h +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h @@ -34,6 +34,7 @@ static inline void sgx_cgroup_init(void) { } struct sgx_cgroup { struct misc_cg *cg; struct sgx_epc_lru_list lru; + struct work_struct reclaim_work; }; =20 static inline struct sgx_cgroup *sgx_cgroup_from_misc_cg(struct misc_cg *c= g) --=20 2.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 711F41A0B15; Tue, 30 Apr 2024 19:51:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506676; cv=none; b=nS8Yvt4Z0Xx93dBRX1YBoOm9A6tr96yP7CguvsLCz+FfMKdsi3oFJSZX4ozRxiMx+SBV+SNU0l5zgdp/ag9c0M+V4ZtZigpOa1IoxwfWkeALVdRp2iJF+5vVsok0DKTDA96/ZrcM8a7dzYRRZqhkTLlGPedeiu4baFxHYNHqxGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506676; c=relaxed/simple; bh=a3YUf9k9YVbRQrRZuYkKnD56ieDY9hK4sn6cvothPig=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=o4hWbwvt+cqSjiMvhhbU9CwliVpPJyTAGWWnC41SXzhcJBYfm1KyyZGQpGxOWpoBTlW34J0/lnQwAe8wNQ2EsN35s/8ScSkVY3/w0c0rH59ta3/WcbhN7YOsXGmnjd/B5VcDYWqHrQO5R/7z8AwKhQRYS8D+Fdbp1jxPwQppsL0= 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=HqvwXI7o; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HqvwXI7o" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506675; x=1746042675; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=a3YUf9k9YVbRQrRZuYkKnD56ieDY9hK4sn6cvothPig=; b=HqvwXI7oMjOJrMZ7SllpOuZGiuUu92JHNw2x3qxcjfqRC7u+P3ALpROg IOzuEFqBXVMwZBEMV9sg26ot7GjKb0lIcMwgC6upw+WVt73EhUowi2NQ7 SFxAzIgCVomHJ0t/ScUyHYW+KEUhREOETBEaYRM9AW7GP9HzbaxiFf/4b aJhTq2T3kRkkSQpMYuzI86jBJoO0QBbaW5SQtQodp5Ja/twKdUk2++uOL 21iozUwZXxEKJu1Unr2NnXav7hrCZjwPv6vHkjfTh0zHCpRkxZ0CJvF+s /D4IfgBdwCK0CMv2DdUVWTwY9QvjCs9eg/uVe60v/IptcRdoI4icoHDQJ w==; X-CSE-ConnectionGUID: 6lDX2zuRQUe5xwFWgaY1Hw== X-CSE-MsgGUID: kZP3QLQwQZ68z/TGBTI4VQ== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355625" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355625" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:11 -0700 X-CSE-ConnectionGUID: umx7MrSnTkuJ6jeWi79qaQ== X-CSE-MsgGUID: NFB5mH+tRUikrNk67vYCjQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280326" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:10 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 10/14] x86/sgx: Charge mem_cgroup for per-cgroup reclamation Date: Tue, 30 Apr 2024 12:51:04 -0700 Message-Id: <20240430195108.5676-11-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 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 | 10 ++++++---- arch/x86/kernel/cpu/sgx/main.c | 29 +++++++++++++--------------- arch/x86/kernel/cpu/sgx/sgx.h | 2 +- 5 files changed, 36 insertions(+), 37 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 6368611cb29e..c237bc3330ee 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -106,6 +106,7 @@ static bool sgx_cgroup_lru_empty(struct misc_cg *root) /** * sgx_cgroup_reclaim_pages() - reclaim EPC from a cgroup tree * @root: The root of cgroup tree to reclaim from. + * @charge_mm: The mm to charge for backing store allocation. * * This function performs a pre-order walk in the cgroup tree under the gi= ven * root, attempting to reclaim pages at each node until a fixed number of = pages @@ -114,7 +115,7 @@ static bool sgx_cgroup_lru_empty(struct misc_cg *root) * the LRUs are recently accessed, i.e., considered "too young" to reclaim= , no * page will actually be reclaimed after walking the whole tree. */ -static void sgx_cgroup_reclaim_pages(struct misc_cg *root) +static void sgx_cgroup_reclaim_pages(struct misc_cg *root, struct mm_struc= t *charge_mm) { struct cgroup_subsys_state *css_root; struct cgroup_subsys_state *pos; @@ -131,7 +132,7 @@ static void sgx_cgroup_reclaim_pages(struct misc_cg *ro= ot) rcu_read_unlock(); =20 sgx_cg =3D sgx_cgroup_from_misc_cg(css_misc(pos)); - cnt +=3D sgx_reclaim_pages(&sgx_cg->lru); + cnt +=3D sgx_reclaim_pages(&sgx_cg->lru, charge_mm); =20 rcu_read_lock(); css_put(pos); @@ -193,7 +194,8 @@ static void sgx_cgroup_reclaim_work_func(struct work_st= ruct *work) * blocked until a worker makes its way through the global work queue. */ while (sgx_cgroup_should_reclaim(sgx_cg)) { - sgx_cgroup_reclaim_pages(sgx_cg->cg); + /* Indirect reclaim, no mm to charge, so NULL: */ + sgx_cgroup_reclaim_pages(sgx_cg->cg, NULL); cond_resched(); } } @@ -236,7 +238,7 @@ int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, en= um sgx_reclaim reclaim) return -EBUSY; } =20 - sgx_cgroup_reclaim_pages(sgx_cg->cg); + sgx_cgroup_reclaim_pages(sgx_cg->cg, current->mm); cond_resched(); } =20 diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 4eb0d417f7b6..f1bd15620b83 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -252,8 +252,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; @@ -269,7 +269,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 @@ -288,6 +288,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 @@ -303,7 +304,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]; @@ -343,7 +344,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; @@ -375,7 +376,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; @@ -392,9 +393,9 @@ static bool sgx_should_reclaim(unsigned long watermark) !list_empty(&sgx_global_lru.reclaimable); } =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 /* @@ -405,7 +406,7 @@ static void sgx_reclaim_pages_global(void) void sgx_reclaim_direct(void) { if (sgx_should_reclaim(SGX_NR_LOW_PAGES)) - sgx_reclaim_pages_global(); + sgx_reclaim_pages_global(current->mm); } =20 static int ksgxd(void *p) @@ -428,7 +429,8 @@ static int ksgxd(void *p) sgx_should_reclaim(SGX_NR_HIGH_PAGES)); =20 if (sgx_should_reclaim(SGX_NR_HIGH_PAGES)) - sgx_reclaim_pages_global(); + /* Indirect reclaim, no mm to charge, so NULL: */ + sgx_reclaim_pages_global(NULL); =20 cond_resched(); } @@ -451,11 +453,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]; @@ -610,7 +607,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, en= um sgx_reclaim reclaim) break; } =20 - 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 89adac646381..72b022755ff1 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -135,7 +135,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.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF66F1C2311; Tue, 30 Apr 2024 19:51:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506680; cv=none; b=ivpkiY3WVYTGLPvX1TPqmJxDXGuShUxUNREpLAEVRLEtH0uUgdrg+7sNG3BOIqYMEmUM5zbPUXAU1vtFxwte6bkBrkkJSsKvztkSUrayvmwfp5BDP2mlUbGtncBV0bw1DqO/GAlweaMPhCqU3T55/GoHBfsKq7sJ37ThfEdZ2iU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506680; c=relaxed/simple; bh=vA6ZnbvvIxoz6BaBtRRnsHl7+KQ4VZ90cLxN0JYW3to=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tJede2GcpqCogPTsF1dWNrNRUazcZPRkPr6TW/QDvBID89p6zBKN3B1kdjcF0x3Api+lnW9BaUFqv5PoEd5hHB5RGaOhy4wlBAmczT5PJWOLQirNw341039SVR5wOuRz+i6yjXmdIWWcSgr4MeH5E+WUbnjVZSnK0uZCfwCszlo= 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=b4XN2KnJ; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="b4XN2KnJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506679; x=1746042679; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vA6ZnbvvIxoz6BaBtRRnsHl7+KQ4VZ90cLxN0JYW3to=; b=b4XN2KnJkHv3TzOxtCO/s8FA5MJg1WsUACXFiKCVF2rdVv6lDf57y6RW r5ukxc93mejrjhNxWmx+Q3Zyjw2uVqo76zW9vpLRT/fiMNlUj9p06uOSP bFQHazgetWTeW7cHH5MkR8sd38s1S7XirCjvnIhkzMf72ZatllwNnvq4T r2ZK8TqG2FXx2FhCEUyevFmrezhLbGpkzMJXLWFhTKLhwjCq/mf2xVsLL 1x1DaAiD4bVQDm7KwGS5ep1d3jMGbyzxpZZ//QFaV8sS3d/XD9TlWUXt+ 62zvD5GmGJt8zWgfuFinezjQHuas6ZcOfHJqANbFr/pa3fGrDDuQxI1N5 g==; X-CSE-ConnectionGUID: twSQKtAkTXKk9cKBZkw6lQ== X-CSE-MsgGUID: ciGXz50rSg6Hr5/LRdBQFw== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355653" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355653" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:11 -0700 X-CSE-ConnectionGUID: tJ69Im9zSv2PS3VmVAkA0g== X-CSE-MsgGUID: mDXn3KcLRe2j9ulH+ZKQog== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280329" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:10 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 11/14] x86/sgx: Abstract check for global reclaimable pages Date: Tue, 30 Apr 2024 12:51:05 -0700 Message-Id: <20240430195108.5676-12-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kristen Carlson Accardi For the global reclaimer to determine if any page available for reclamation at the global level, it currently only checks for emptiness of the global LRU. That will be inadequate when pages are tracked in multiple LRUs, one per cgroup. For this purpose, create a new helper, sgx_can_reclaim_global(), to abstract this check. Currently it only checks the global LRU, later will check emptiness of LRUs of all cgroups when per-cgroup tracking is turned on. Replace all the checks for emptiness of the global LRU, list_empty(&sgx_global_lru.reclaimable), with calls to sgx_can_reclaim_global(). Rename sgx_should_reclaim() to sgx_should_reclaim_global() as it is used to check if a global reclamation should be performed. 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 --- 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 | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index f1bd15620b83..92bd3151a589 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -36,6 +36,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. */ @@ -387,10 +395,10 @@ unsigned int sgx_reclaim_pages(struct sgx_epc_lru_lis= t *lru, struct mm_struct *c return cnt; } =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(); } =20 static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) @@ -405,7 +413,7 @@ static void sgx_reclaim_pages_global(struct mm_struct *= charge_mm) */ void sgx_reclaim_direct(void) { - if (sgx_should_reclaim(SGX_NR_LOW_PAGES)) + if (sgx_should_reclaim_global(SGX_NR_LOW_PAGES)) sgx_reclaim_pages_global(current->mm); } =20 @@ -426,9 +434,9 @@ 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)) + if (sgx_should_reclaim_global(SGX_NR_HIGH_PAGES)) /* Indirect reclaim, no mm to charge, so NULL: */ sgx_reclaim_pages_global(NULL); =20 @@ -592,7 +600,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; } @@ -620,7 +628,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.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 854881C2328; Tue, 30 Apr 2024 19:51:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506681; cv=none; b=a4cxI/kClgiZN1IeRNRMgo/J/7UYsHYyuFNzoQvKQVy7gSEzqabM6MoVX3Xxx57aPPLXof3Mc3QLab8x+0qhRj62VU3ejXjEgFLWAfsWQ+dgX0s8S4NiVMv4AZFaVt+3jHT8yz+hsSMkfMeEzx4sKsBbaWgqvMnJskAIoeXQTfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506681; c=relaxed/simple; bh=onq+S2VHYNSRS3tDtzRHiVMeMhXIxDBBmK/t3+y2SVA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X8R8qciTav5UhRTQIM+gCd9OYT/TptGBJNZc6Ob7CwV77F49/aeflIxGVaF8Kf0mdHTV2v8lLTV72AIS8rdTSPFy9aYAu80T3A6Mg4mNZ6eMnNPRKJSBuIRjyjqp6cvbS3TnVn+FBbNTycya1Rk5LTVZe6yBvKwc97+oANGwvEw= 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=cKFbN+pp; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="cKFbN+pp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506680; x=1746042680; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=onq+S2VHYNSRS3tDtzRHiVMeMhXIxDBBmK/t3+y2SVA=; b=cKFbN+ppHDPFScvda1Q4Ud6+aCniBhpCH648jnUcoDiVOOztun1115Xt nvb4U3nt4g6W3ZB+xHJl75i3/6uB2dlXrqXQwwmgtUgooFqeQQy7QgJKX VFjTcpIRxVPd77uNFmQjG+nAbKr1Tq/6KzcrdlgJI3Liww62PrboDAkE9 Ru6MRx6qH17KDIXDaDxL4xxwuAf7e7ZoCRZsoJpxN5k6IAoWisxRUsT00 rMkFgTqEI/DSTnzxZ9h6BdYTqSkPYjFVDD+pcxpg6hKIULF38lF14PREy dVv58/WOjSl/nMAVdr2LJ0vGfz4YopYiEke1aYUQuoKYijtwGMgHZTT6g w==; X-CSE-ConnectionGUID: UCREiIwgQlS5wWgmaNnNHA== X-CSE-MsgGUID: UtoQfjBKR8iLb6t7dPSXWA== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355662" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355662" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:11 -0700 X-CSE-ConnectionGUID: r1SfAsntQgy8xAyIotX+JA== X-CSE-MsgGUID: CAgmg+WHQFSn8BvilJYGfg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280332" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:10 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 12/14] x86/sgx: Turn on per-cgroup EPC reclamation Date: Tue, 30 Apr 2024 12:51:06 -0700 Message-Id: <20240430195108.5676-13-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: 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. This makes all EPC pages tracked in per-cgroup LRUs and the global reclaimer (ksgxd) will not be able to reclaim any pages from the global LRU. However, in cases of over-committing, i.e., the sum of cgroup limits greater than the total capacity, cgroups may never reclaim but the total usage can still be near the capacity. Therefore a global reclamation is still needed in those cases and it should be performed from the root cgroup. Modify sgx_reclaim_pages_global(), to reclaim from the root EPC cgroup when cgroup is enabled, otherwise from the global LRU. Export sgx_cgroup_reclaim_pages() in the header file so it can be reused for this purpose. Similarly, modify sgx_can_reclaim_global(), to check emptiness of LRUs of all cgroups when EPC cgroup is enabled, otherwise only check the global LRU. Export sgx_cgroup_lru_empty() so it can be reused for this purpose. Finally, change sgx_reclaim_direct(), to check and ensure there are free pages at cgroup level so forward progress can be made by the caller. Export sgx_cgroup_should_reclaim() for reuse. 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 Tested-by: Mikko Ylinen Tested-by: Jarkko Sakkinen --- 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 | 6 ++-- arch/x86/kernel/cpu/sgx/epc_cgroup.h | 27 ++++++++++++++++ arch/x86/kernel/cpu/sgx/main.c | 46 ++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx= /epc_cgroup.c index c237bc3330ee..fe2fd6034023 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c @@ -67,7 +67,7 @@ static inline u64 sgx_cgroup_max_pages_to_root(struct sgx= _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; @@ -115,7 +115,7 @@ static bool sgx_cgroup_lru_empty(struct misc_cg *root) * the LRUs are recently accessed, i.e., considered "too young" to reclaim= , no * page will actually be reclaimed after walking the whole tree. */ -static void sgx_cgroup_reclaim_pages(struct misc_cg *root, struct mm_struc= t *charge_mm) +void sgx_cgroup_reclaim_pages(struct misc_cg *root, struct mm_struct *char= ge_mm) { struct cgroup_subsys_state *css_root; struct cgroup_subsys_state *pos; @@ -156,7 +156,7 @@ static void sgx_cgroup_reclaim_pages(struct misc_cg *ro= ot, struct mm_struct *cha * threshold (%SGX_CG_MIN_FREE_PAGE) and there are reclaimable pages withi= n the * cgroup. */ -static bool sgx_cgroup_should_reclaim(struct sgx_cgroup *sgx_cg) +bool sgx_cgroup_should_reclaim(struct sgx_cgroup *sgx_cg) { u64 cur, max; =20 diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx= /epc_cgroup.h index 2044e0d64076..9d69608eadf6 100644 --- a/arch/x86/kernel/cpu/sgx/epc_cgroup.h +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h @@ -13,6 +13,11 @@ #define MISC_CG_RES_SGX_EPC MISC_CG_RES_TYPES struct sgx_cgroup; =20 +static inline struct misc_cg *misc_from_sgx(struct sgx_cgroup *sgx_cg) +{ + return NULL; +} + static inline struct sgx_cgroup *sgx_get_current_cg(void) { return NULL; @@ -27,8 +32,22 @@ 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 bool sgx_cgroup_should_reclaim(struct sgx_cgroup *sgx_cg) +{ + return false; +} + static inline void sgx_cgroup_init(void) { } =20 +static inline void sgx_cgroup_reclaim_pages(struct misc_cg *root, struct m= m_struct *charge_mm) +{ +} + #else /* CONFIG_CGROUP_MISC */ =20 struct sgx_cgroup { @@ -37,6 +56,11 @@ struct sgx_cgroup { struct work_struct reclaim_work; }; =20 +static inline struct misc_cg *misc_from_sgx(struct sgx_cgroup *sgx_cg) +{ + return sgx_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); @@ -67,6 +91,9 @@ 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); +bool sgx_cgroup_should_reclaim(struct sgx_cgroup *sgx_cg); +void sgx_cgroup_reclaim_pages(struct misc_cg *root, struct mm_struct *cha= rge_mm); void sgx_cgroup_init(void); =20 #endif /* CONFIG_CGROUP_MISC */ diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 92bd3151a589..8748358fd00f 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -31,9 +31,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 /* @@ -41,7 +62,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) { - 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); @@ -403,7 +427,10 @@ static bool sgx_should_reclaim_global(unsigned long wa= termark) =20 static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) { - sgx_reclaim_pages(&sgx_global_lru, charge_mm); + if (IS_ENABLED(CONFIG_CGROUP_MISC)) + sgx_cgroup_reclaim_pages(misc_cg_root(), charge_mm); + else + sgx_reclaim_pages(&sgx_global_lru, charge_mm); } =20 /* @@ -413,6 +440,15 @@ static void sgx_reclaim_pages_global(struct mm_struct = *charge_mm) */ void sgx_reclaim_direct(void) { + struct sgx_cgroup *sgx_cg =3D sgx_get_current_cg(); + + /* Make sure there are some free pages at cgroup level */ + if (sgx_cg && sgx_cgroup_should_reclaim(sgx_cg)) { + sgx_cgroup_reclaim_pages(misc_from_sgx(sgx_cg), current->mm); + sgx_put_cg(sgx_cg); + } + + /* Make sure there are some free pages at global level */ if (sgx_should_reclaim_global(SGX_NR_LOW_PAGES)) sgx_reclaim_pages_global(current->mm); } @@ -615,6 +651,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(current->mm); cond_resched(); } --=20 2.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFD0C1C65F8; Tue, 30 Apr 2024 19:51:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506682; cv=none; b=abGKhvRzYYMzQCU5aMsEHrycb0MsPaFNarw4J9HYLaBdi6FMAZgnWjrFnnJyUO0+ocWI60T1H3NmunGyoih42k7/0k4bZe+a511mKOZlRifsYbleq2qTPL1lV42eErsStiOnhbT9eNMZYOem+t98luBnysSO1+f8+XSABcHovfE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506682; c=relaxed/simple; bh=kSEcIKMK5oUy8ODnWLSE1xveKiDKwhOKsrguoASuNCs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V/Xtq5Dq2GnmAkiF1AK/m9/LHPubaUSipeujToH2QzFphhMH75AFI2lpK+Hyc+zPoFmK4b/ae8qlUSLT+ML8Y5b+pmxByuKDNIedhMN5j47F2c/6O7J6HPsY601Wf9GgrHm1q5hBqiGTnX6esX+pxz5T4qKdciw25RPoGlSUBHM= 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=UoPB1GK1; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="UoPB1GK1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506681; x=1746042681; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kSEcIKMK5oUy8ODnWLSE1xveKiDKwhOKsrguoASuNCs=; b=UoPB1GK1vkPViwiCAlBXDjm60pHYp2jaZRk3msxoyKSUh+nQ/0jJWmri l8YZn7lEx9zyWTQ0I1c+6gHRJBFLcYma1SYVRXD3GvZU+hVnhCcbrIcqF 6KnG/wGCQ7lyX/8P33VRaTEezBs6QttnMh9PGFmQ2BDk9vi+IFymU1qMM qynaOfu06J7qrIeIS1GUIc+a4onDJZjgm4EzKbkK8rDkW3cmlTenIEYoY L0JV/Tl56Vx3sevihzg1QAX5yzrag+1GPv0Zdsj6lymA5dyyRe7YV7nVT Y8hjeR5X6yJu75zF4fxgUa3b31akgUEcqPIqelwv+1lGpHkMaRl8IovTH g==; X-CSE-ConnectionGUID: bwJZ4G2XT2aC1eFplr6FCQ== X-CSE-MsgGUID: bFI/DCRAS/+jlUPmXHeb9A== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355681" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355681" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:12 -0700 X-CSE-ConnectionGUID: 1uj7Rhn4RqiwdnMRayYQEg== X-CSE-MsgGUID: tjcOOGgGQzGvQTpu9b6loQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280335" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:10 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 13/14] Docs/x86/sgx: Add description for cgroup support Date: Tue, 30 Apr 2024 12:51:07 -0700 Message-Id: <20240430195108.5676-14-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sean Christopherson 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 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.25.1 From nobody Fri Nov 1 02:35:14 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F21571C0DD6; Tue, 30 Apr 2024 19:51:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506681; cv=none; b=E0Nx/pdMh+KflW8Lj5kpK7HdtgD+wsXQlmYQ27oUweff38LwPzWAiYETLmEU6GckQ7ecpEKDS/pkJKvq68KNKLah8g8GhQcHGT8xYx9QeWGp5WchSEYzYIze97QBWRtoc48pCwp/psrJWIqEq2NMOxOhXrzp4hIbUGwD7SUOO+A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714506681; c=relaxed/simple; bh=cdTt/RfbXiPxDDfK6wWOc6D308ILWNE4x9utyKswqpw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WNo4XDJ6pFM2IW07IBiE6aKftpIgYLT1TURNQCMUvUwTtpEgQwFu8UzDGC6j/lpGN+PLswTd3IdSEhPdua49L4+66QgBf/q5zsnqh8PK0tmJGf/M6iiWyjUu3PgZ54hbWJIWgiotW8QaScAdtnw9br1MKHm7nLeY3d25Rt413/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=L7LP5TmF; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="L7LP5TmF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714506679; x=1746042679; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cdTt/RfbXiPxDDfK6wWOc6D308ILWNE4x9utyKswqpw=; b=L7LP5TmFEYLSpgBIxpIiVEa+jTUGhNUGHEiNnezI546ssL2jtrSyr9VZ kuCqyNMjZN0q1fxu1HHVYLnkniOHfmQF3pP3c+LML+RUk2hb0S20uwolE SV0Lxz2wt40dOP+JLgpvLsd7FLSdu7jo4xw3bbsptUc3BS7DKFJbqrNN4 5SaLmR+nZvVmCjy1TxCvjHHpiYjQucVQY4ARN3BQQ55i0F/qLxuxIxART 7l0EjV8ATo10S6dQVqBT9oBsw04uhx8t8xAF94n/Z7vCp6juOWC+4+wss 8ull1njJ+yrztJwU66pvS5LxLPc34OT4frXvFML3N1L6O5pXBSNDK5cLh w==; X-CSE-ConnectionGUID: XzxeMYmhSv6OZS6ShaIXww== X-CSE-MsgGUID: ufjR+xeuQTOBLtLPOS6B8A== X-IronPort-AV: E=McAfee;i="6600,9927,11060"; a="10355671" X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="10355671" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2024 12:51:12 -0700 X-CSE-ConnectionGUID: +rfVlnmcQnqaSR9pzKA70Q== X-CSE-MsgGUID: r8tW38zPTnGyBCUcnUycmg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,243,1708416000"; d="scan'208";a="31280339" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa003.jf.intel.com with ESMTP; 30 Apr 2024 12:51:11 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com, tim.c.chen@linux.intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com, chrisyan@microsoft.com Subject: [PATCH v13 14/14] selftests/sgx: Add scripts for EPC cgroup testing Date: Tue, 30 Apr 2024 12:51:08 -0700 Message-Id: <20240430195108.5676-15-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240430195108.5676-1-haitao.huang@linux.intel.com> References: <20240430195108.5676-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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 | 295 ++++++++++++++++++ tools/testing/selftests/sgx/settings | 2 + .../selftests/sgx/watch_misc_for_tests.sh | 11 + 7 files changed, 439 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 867f88ce2570..739376af9e33 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..f3d463c09cc2 --- /dev/null +++ b/tools/testing/selftests/sgx/run_epc_cg_selftests.sh @@ -0,0 +1,295 @@ +#!/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 +# We will only set limit in test1 and run tests in test3 +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..22f228d3b8c2 --- /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=3D120 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.25.1