Add Cgroup support for SGX EPC memory
Subsystems (5)
v15 cover Add Cgroup support for SGX EPC memory
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, e.g., support for pod level control in a Kubernates cluster on a VM or bare-metal host [1,2].
This patchset implements the support for sgx_epc memory within the misc cgroup controller. A user can use the misc cgroup controller to set and enforce a max limit on total EPC usage per cgroup. The implementation reports current usage and events of reaching the limit per cgroup as well as the total system capacity.
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, which are normal system memory allocated via shmem and accounted by the memory controller. Similar to per-cgroup reclamation done by the memory controller, the EPC misc controller needs to implement a per-cgroup EPC reclaiming process: when the EPC usage of a cgroup reaches its hard limit ('sgx_epc' entry in the 'misc.max' file), the cgroup starts swapping out some EPC pages within the same cgroup to make room for new allocations.
For that, this implementation tracks reclaimable EPC pages in a separate LRU list in each cgroup, and below are more details and justification of this design.
Track EPC pages in per-cgroup LRUs (from Dave)
----------------------------------------------
tl;dr: A cgroup hitting its limit should be as similar as possible to the system running out of EPC memory. The only two choices to implement that are nasty changes the existing LRU scanning algorithm, or to add new LRUs. The result: Add a new LRU for each cgroup and scans those instead. Replace the existing global cgroup with the root cgroup's LRU (only when this new support is compiled in, obviously).
The existing EPC memory management aims to be a miniature version of the core VM where EPC memory can be overcommitted and reclaimed. EPC allocations can wait for reclaim. The alternative to waiting would have been to send a signal and let the enclave die.
This series attempts to implement that same logic for cgroups, for the same
reasons: it's preferable to wait for memory to become available and let reclaim happen than to do things that are fatal to enclaves.
There is currently a global reclaimable page SGX LRU list. That list (and the existing scanning algorithm) is essentially useless for doing reclaim when a cgroup hits its limit because the cgroup's pages are scattered around that LRU. It is unspeakably inefficient to scan a linked list with millions of entries for what could be dozens of pages from a cgroup that needs reclaim.
Even if unspeakably slow reclaim was accepted, the existing scanning algorithm only picks a few pages off the head of the global LRU. It would either need to hold the list locks for unreasonable amounts of time, or be taught to scan the list in pieces, which has its own challenges.
Unreclaimable Enclave Pages
---------------------------
There are a variety of page types for enclaves, each serving different purposes [5]. Although the SGX architecture supports swapping for all types, some special pages, e.g., Version Array(VA) and Secure Enclave Control Structure (SECS)[5], holds meta data of reclaimed pages and enclaves. That makes reclamation of such pages more intricate to manage. The SGX driver global reclaimer currently does not swap out VA pages. It only swaps the SECS page of an enclave when all other associated pages have been swapped out. The cgroup reclaimer follows the same approach and does not track those in per-cgroup LRUs and considers them as unreclaimable pages. The allocation of these pages is counted towards the usage of a specific cgroup and is subject to the cgroup's set EPC limits.
Earlier versions of this series implemented forced enclave-killing to reclaim VA and SECS pages. That was designed to enforce the 'max' limit, particularly in scenarios where a user or administrator reduces this limit post-launch of enclaves. However, subsequent discussions [3, 4] indicated that such preemptive enforcement is not necessary for the misc-controllers. Therefore, reclaiming SECS/VA pages by force-killing enclaves were removed, and the limit is only enforced at the time of new EPC allocation request. When a cgroup hits its limit but nothing left in the LRUs of the subtree, i.e., nothing to reclaim in the cgroup, any new attempt to allocate EPC within that cgroup will result in an 'ENOMEM'.
Unreclaimable Guest VM EPC Pages
--------------------------------
The EPC pages allocated for guest VMs by the virtual EPC driver are not reclaimable by the host kernel [6]. Therefore an EPC cgroup also treats those as unreclaimable and returns ENOMEM when its limit is hit and nothing reclaimable left within the cgroup. The virtual EPC driver translates the ENOMEM error resulted from an EPC allocation request into a SIGBUS to the user process exactly the same way handling host running out of physical EPC.
This work was originally authored by Sean Christopherson a few years ago, and previously modified by Kristen C. Accardi to utilize the misc cgroup controller rather than a custom controller. I have been updating the patches based on review comments since V2 [7-19], simplified the implementation/design, added selftest scripts, fixed some stability issues found from testing.
Thanks to all for the review/test/tags/feedback provided on the previous versions.
I appreciate your further reviewing/testing and providing tags if appropriate.
---
V15:
- Disable SGX when sgx_cgroup_init() fails instead of using BUG_ON() (Jarkko)
- Reset capacity if sgx_cgroup_init() fails. (Kai)
- Style fixes (Jarkko, Kai)
- In misc.c, only invoke the ->free() callbacks for resource types whose ->alloc() callback was called and returned success. (Ridong)
V14:
- modified sgx_cgroup_reclaim_pages() to return the next node. Caller can use it as the new starting node for next round of reclamation attempt if needed. This is to fix a corner case
where a super busy top level cgroup may block reclamation in lower level cgroups. (Kai)
- Move renaming of sgx_should_reclaim_global() to the patch 'x86/sgx: Add basic EPC reclamation flow for cgroup'. (Kai)
v13:
- Only allocate workqueue for SGX cgroup when misc is enabled and BUG_ON() when allocation fails
- Add more tags
- Commit logs and style improvements (Kai)
- Test script improvements (Jarkko)
V12:
- Integrate test scripts to kselftests "run_tests" target. (Jarkko)
- Remove CGROUP_SGX_EPC kconfig, conditionally compile with CGROUP_MISC enabled. (Jarkko)
- Explain why taking 'struct misc_cg *cg' as parameter, but not 'struct misc_res *res' in the changelog for patch #2. (Kai)
- Remove "unlikely" in patch #2 (Kai)
V11:
- Update copyright years and use c style (Kai)
- Improve and simplify test scripts: remove cgroup-tools and bash dependency, drop cgroup v1. (Jarkko, Michal)
- Add more stub/wrapper functions to minimize #ifdefs in c file. (Kai)
- Revise commit message for patch #8 to clarify design rational (Kai)
- Print error instead of WARN for init failure. (Kai)
- Add check for need to queue an async reclamation before returning from sgx_cgroup_try_charge(), do so if needed.
V10:
- Use enum instead of boolean for the 'reclaim' parameters in sgx_alloc_epc_page(). (Dave, Jarkko)
- Pass mm struct instead of a boolean 'indirect'. (Dave, Jarkko)
- Add comments/macros to clarify the cgroup async reclaimer design. (Kai)
- Simplify sgx_reclaim_pages() signature, removing a pointer passed in. (Kai)
- Clarify design of sgx_cgroup_reclaim_pages(). (Kai)
- Does not return a value for callers to check.
- Its usage pattern is similar to that of sgx_reclaim_pages() now
- Add cond_resched() in the loop in the cgroup reclaimer to improve liveliness.
- Add logic for cgroup level reclamation in sgx_reclaim_direct()
- Restructure V9 patches 7-10 to make them flow better. (Kai)
- Disable cgroup if workqueue allocation failed during init. (Kai)
- Shorten names for EPC cgroup functions, structures and variables. (Jarkko)
- Separate out a helper for for addressing single iteration of the loop in sgx_cgroup_try_charge(). (Jarkko)
- More cleanup/clarifying/comments/style fixes. (Kai, Jarkko)
V9:
- Add comments for static variables outside functions. (Jarkko)
- Remove unnecessary ifs. (Tim)
- Add more Reviewed-By: tags from Jarkko and TJ.
V8:
- Style fixes. (Jarkko)
- Abstract _misc_res_free/alloc() (Jarkko)
- Remove unneeded NULL checks. (Jarkko)
V7:
- Split the large patch for the final EPC implementation, #10 in V6, into smaller ones. (Dave, Kai)
- Scan and reclaim one cgroup at a time, don't split sgx_reclaim_pages() into two functions (Kai)
- Removed patches to introduce the EPC page states, list for storing candidate pages for reclamation. (not needed due to above changes)
- 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 required callback functions (Jarkko)
- Initialize epc cgroup in sgx driver init function. (Kai)
- Moved addition of priv field to patch 4 where it was used first. (Jarkko)
- Split sgx_get_current_epc_cg() out of sgx_epc_cg_try_charge() (Kai)
- Use a static for root cgroup (Kai)
[1]https://lore.kernel.org/all/DM6PR21MB11772A6ED915825854B419D6C4989@DM6PR21MB1177.namprd21.prod.outlook.com/ [2]https://lore.kernel.org/all/ZD7Iutppjj+muH4p@himmelriiki/ [3]https://lore.kernel.org/lkml/[email protected]/ [4]https://lore.kernel.org/lkml/yz44wukoic3syy6s4fcrngagurkjhe2hzka6kvxbajdtro3fwu@zd2ilht7wcw3/ [5]Documentation/arch/x86/sgx.rst, Section"Enclave Page Types" [6]Documentation/arch/x86/sgx.rst, Section "Virtual EPC"
[7]v2: https://lore.kernel.org/all/[email protected]/
[8]v3: https://lore.kernel.org/linux-sgx/[email protected]/
[9]v4: https://lore.kernel.org/all/[email protected]/
[10]v5: https://lore.kernel.org/all/[email protected]/
[11]v6: https://lore.kernel.org/linux-sgx/[email protected]/
[12]v7: https://lore.kernel.org/linux-sgx/[email protected]/T/#t
[13]v8: https://lore.kernel.org/linux-sgx/[email protected]/T/#t
[14]v9: https://lore.kernel.org/lkml/[email protected]/T/
[15]v10: https://lore.kernel.org/linux-sgx/[email protected]/T/#t
[16]v11: https://lore.kernel.org/lkml/[email protected]/
[17]v12: https://lore.kernel.org/lkml/[email protected]/
[18]v13: https://lore.kernel.org/lkml/[email protected]/
[19]v14: https://lore.kernel.org/linux-sgx/[email protected]/T/#t
Haitao Huang (3):
x86/sgx: Replace boolean parameters with enums x86/sgx: Charge mem_cgroup for per-cgroup reclamation selftests/sgx: Add scripts for EPC cgroup testing
Kristen Carlson Accardi (9):
cgroup/misc: Add per resource callbacks for CSS events cgroup/misc: Export APIs for SGX driver cgroup/misc: Add SGX EPC resource type x86/sgx: Implement basic EPC misc cgroup functionality x86/sgx: Abstract tracking reclaimable pages in LRU x86/sgx: Add basic EPC reclamation flow for cgroup x86/sgx: Abstract check for global reclaimable pages x86/sgx: Implement async reclamation for cgroup x86/sgx: Turn on per-cgroup EPC reclamation
Sean Christopherson (2):
x86/sgx: Add sgx_epc_lru_list to encapsulate LRU list Docs/x86/sgx: Add description for cgroup support
Documentation/arch/x86/sgx.rst | 83 +++++ arch/x86/kernel/cpu/sgx/Makefile | 1 + arch/x86/kernel/cpu/sgx/encl.c | 41 +- arch/x86/kernel/cpu/sgx/encl.h | 7 +- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 352 ++++++++++++++++++ arch/x86/kernel/cpu/sgx/epc_cgroup.h | 106 ++++++ arch/x86/kernel/cpu/sgx/ioctl.c | 10 +- arch/x86/kernel/cpu/sgx/main.c | 243 +++++++++--- arch/x86/kernel/cpu/sgx/sgx.h | 51 ++- arch/x86/kernel/cpu/sgx/virt.c | 2 +- include/linux/misc_cgroup.h | 41 ++ kernel/cgroup/misc.c | 107 +++++- 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 + 19 files changed, 1373 insertions(+), 111 deletions(-) create mode 100644 arch/x86/kernel/cpu/sgx/epc_cgroup.c create mode 100644 arch/x86/kernel/cpu/sgx/epc_cgroup.h 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
base-commit: 1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0
Signature · 1 line
2.43.0
PATCH v15 1/14 x86/sgx: Replace boolean parameters with enums
From: Haitao Huang <[email protected]>
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
--- 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(-)
arch/x86/kernel/cpu/sgx/encl.c +6 -6
@@ -217,7 +217,7 @@ static struct sgx_epc_page *sgx_encl_eldu(struct sgx_encl_page *encl_page,struct sgx_epc_page *epc_page;int ret; - epc_page =sgx_alloc_epc_page (encl_page, false); + epc_page =sgx_alloc_epc_page (encl_page,SGX_NO_RECLAIM );if (IS_ERR (epc_page))return epc_page;
@@ -359,14 +359,14 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma, goto err_out_unlock; } - epc_page =sgx_alloc_epc_page (encl_page, false); + epc_page =sgx_alloc_epc_page (encl_page,SGX_NO_RECLAIM );if (IS_ERR (epc_page)) {if (PTR_ERR (epc_page) == -EBUSY ) vmret =VM_FAULT_NOPAGE ; goto err_out_unlock; } - va_page =sgx_encl_grow (encl, false); + va_page =sgx_encl_grow (encl,SGX_NO_RECLAIM );if (IS_ERR (va_page)) {if (PTR_ERR (va_page) == -EBUSY ) vmret =VM_FAULT_NOPAGE ;
@@ -1232,8 +1232,8 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr)/** * 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, unsigned 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;
arch/x86/kernel/cpu/sgx/encl.h +2 -2
@@ -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);#endif /* _X86_ENCL_H */
arch/x86/kernel/cpu/sgx/ioctl.c +5 -5
@@ -17,7 +17,7 @@#include "encl.h" #include "encls.h" -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 =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; - va_page =sgx_encl_grow (encl, true); + va_page =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) encl->backing = backing; - secs_epc =sgx_alloc_epc_page (&encl->secs, true); + secs_epc =sgx_alloc_epc_page (&encl->secs,SGX_DO_RECLAIM );if (IS_ERR (secs_epc)) { ret =PTR_ERR (secs_epc); goto err_out_backing;
@@ -269,13 +269,13 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,if (IS_ERR (encl_page))return PTR_ERR (encl_page); - epc_page =sgx_alloc_epc_page (encl_page, true); + epc_page =sgx_alloc_epc_page (encl_page,SGX_DO_RECLAIM );if (IS_ERR (epc_page)) {kfree (encl_page);return PTR_ERR (epc_page); } - va_page =sgx_encl_grow (encl, true); + va_page =sgx_encl_grow (encl,SGX_DO_RECLAIM );if (IS_ERR (va_page)) { ret =PTR_ERR (va_page); goto err_out_free;
arch/x86/kernel/cpu/sgx/main.c +7 -7
@@ -464,14 +464,14 @@ static struct sgx_epc_page *__sgx_alloc_epc_page_from_node(int nid)/** * __sgx_alloc_epc_page() - Allocate an EPC page * -* Iterate through NUMA nodes and reserve ia free EPC page to the caller. Start +* Iterate through NUMA nodes and reserve a free EPC page to the caller. Start * 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 =numa_node_id ();
@@ -543,12 +543,12 @@ int sgx_unmark_page_reclaimable(struct sgx_epc_page *page)/** * sgx_alloc_epc_page() - Allocate an EPC page * @owner: the owner of the EPC page -* @reclaim: reclaim pages if necessary +* @reclaim: whether reclaim pages if necessary * * Iterate through EPC sections and borrow a free EPC page to the caller. When a * page is no longer needed it must be released with sgx_free_epc_page(). If -* @reclaim is set to true, directly reclaim pages when we are out of pages. 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 out of +* pages. No mm's can be locked for SGX_DO_RECLAIM. * * Finally, wake up ksgxd when the number of pages goes below the watermark * before returning back to the caller.
@@ -557,7 +557,7 @@ int sgx_unmark_page_reclaimable(struct sgx_epc_page *page)* 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 reclaim) {struct sgx_epc_page *page;
@@ -571,7 +571,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim)if (list_empty (&sgx_active_page_list))return ERR_PTR (-ENOMEM ); -if (!reclaim) { +if (reclaim ==SGX_NO_RECLAIM ) { page =ERR_PTR (-EBUSY );break ; }
arch/x86/kernel/cpu/sgx/sgx.h +11 -2
@@ -29,6 +29,16 @@/* Pages on free list */ #define SGX_EPC_PAGE_IS_FREE BIT(1) +/** +* 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_epc_page *page)return section->virt_addr + index *PAGE_SIZE ; } -struct sgx_epc_page *__sgx_alloc_epc_page (void );void sgx_free_epc_page (struct sgx_epc_page *page);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 reclaim);void sgx_ipi_cb (void *info);
arch/x86/kernel/cpu/sgx/virt.c +1 -1
@@ -46,7 +46,7 @@ static int __sgx_vepc_fault(struct sgx_vepc *vepc,if (epc_page)return 0 ; - epc_page =sgx_alloc_epc_page (vepc, false); + epc_page =sgx_alloc_epc_page (vepc,SGX_NO_RECLAIM );if (IS_ERR (epc_page))return PTR_ERR (epc_page);
Signature · 1 line
2.43.0
PATCH v15 2/14 cgroup/misc: Add per resource callbacks for CSS events
In reply to Huang, HaitaoFrom: Kristen Carlson Accardi <[email protected]>
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.
---
V15:
- Style fixes (Jarkko, Kai)
- _misc_cg_res_free() takes the last index. Only call ->free() for those res types with ->alloc() successfully called. (Ridong)
V12:
- Add comments in commit to clarify reason to pass in misc_cg, not misc_res. (Kai)
- Remove unlikely (Kai)
V8:
- Abstract out _misc_cg_res_free() and _misc_cg_res_alloc() (Jarkko)
V7:
- Make ops one per resource type and store them in array (Michal)
- Rename the ops struct to misc_res_ops, and enforce the constraints of required 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, 87 insertions(+), 6 deletions(-)
include/linux/misc_cgroup.h +11 -0
@@ -27,6 +27,16 @@ struct misc_cg;#include <linux/cgroup.h> +/** +* struct misc_res_ops: per resource type callback ops. +* @alloc: invoked for resource specific initialization when cgroup is allocated. +* @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 {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 *ops);int misc_cg_try_charge (enum misc_res_type type,struct misc_cg *cg,u64 amount);void misc_cg_uncharge (enum misc_res_type type,struct misc_cg *cg,u64 amount);
kernel/cgroup/misc.c +76 -6
121 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v15 3/14 cgroup/misc: Export APIs for SGX driver
In reply to Huang, HaitaoFrom: Kristen Carlson Accardi <[email protected]>
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.
---
V6:
- Make commit messages more concise and split the original patch into two(Kai)
--- include/linux/misc_cgroup.h | 24 ++++++++++++++++++++++++ kernel/cgroup/misc.c | 21 ++++++++------------- 2 files changed, 32 insertions(+), 13 deletions(-)
include/linux/misc_cgroup.h +24 -0
@@ -64,6 +64,7 @@ struct misc_cg {struct misc_res res[MISC_CG_RES_TYPES ]; }; +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 *ops);
@@ -84,6 +85,20 @@ static inline struct misc_cg *css_misc(struct cgroup_subsys_state *css) return css ? container_of(css, struct misc_cg, css) : NULL; } +/** +* 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 parent. +*/ +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 task. *
@@ -108,6 +123,15 @@ static inline void put_misc_cg(struct misc_cg *cg) }#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 ; +}static inline u64 misc_cg_res_total_usage (enum misc_res_type type) {
kernel/cgroup/misc.c +8 -13
@@ -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 ];/** -* 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 parent. +* 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);/** * 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 ; -for (i = cg; i; i =parent_misc (i)) { +for (i = cg; i; i =misc_cg_parent (i)) { res = &i->res[type]; new_usage =atomic64_add_return (amount, &res->usage);
@@ -196,12 +191,12 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, u64 amount)return 0 ; err_charge: -for (j = i; j; j =parent_misc (j)) { +for (j = i; j; j =misc_cg_parent (j)) {atomic64_inc (&j->res[type].events);cgroup_file_notify (&j->events_file); } -for (j = cg; j != i; j =parent_misc (j)) +for (j = cg; j != i; j =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 misc_cg *cg, u64 amount)if (!(amount &&valid_type (type) && cg))return ; -for (i = cg; i; i =parent_misc (i)) +for (i = cg; i; i =misc_cg_parent (i))misc_cg_cancel_charge (type, i, amount); }EXPORT_SYMBOL_GPL (misc_cg_uncharge);
Signature · 1 line
2.43.0
PATCH v15 4/14 cgroup/misc: Add SGX EPC resource type
In reply to Huang, HaitaoFrom: Kristen Carlson Accardi <[email protected]>
Add SGX EPC memory, MISC_CG_RES_SGX_EPC, to be a valid resource type for the misc controller.
---
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(+)
include/linux/misc_cgroup.h +4 -0
@@ -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 , #endifMISC_CG_RES_TYPES };
kernel/cgroup/misc.c +4 -0
@@ -24,6 +24,10 @@ static const char *const misc_res_name[] = {/* AMD SEV-ES ASIDs resource */ "sev_es",#endif +#ifdef CONFIG_X86_SGX +/* Intel SGX EPC memory bytes */ + "sgx_epc", +#endif };/* Root misc cgroup */
Signature · 1 line
2.43.0
PATCH v15 5/14 x86/sgx: Implement basic EPC misc cgroup functionality
In reply to Huang, HaitaoFrom: Kristen Carlson Accardi <[email protected]>
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.
---
V15:
- Declare __init for sgx_cgroup_init() (Jarkko)
- Disable SGX when sgx_cgroup_init() fails (Jarkko)
V13:
- Remove unneeded includes. (Kai)
V12:
- Remove CONFIG_CGROUP_SGX_EPC and make sgx cgroup implementation conditionally compiled with CONFIG_CGROUP_MISC. (Jarkko)
V11:
- Update copyright and format better (Kai)
- Create wrappers to remove #ifdefs in c file. (Kai)
- Remove unneeded comments (Kai)
V10:
- Shorten function, variable, struct names, s/sgx_epc_cgroup/sgx_cgroup. (Jarkko)
- 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 | 73 ++++++++++++++++++++++++++++ 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, 208 insertions(+), 3 deletions(-) create mode 100644 arch/x86/kernel/cpu/sgx/epc_cgroup.c create mode 100644 arch/x86/kernel/cpu/sgx/epc_cgroup.h
arch/x86/kernel/cpu/sgx/Makefile +1 -0
@@ -4,3 +4,4 @@ obj-y += \ ioctl.o \ main.o obj-$(CONFIG_X86_SGX_KVM) += virt.o +obj-$(CONFIG_CGROUP_MISC) += epc_cgroup.o
arch/x86/kernel/cpu/sgx/epc_cgroup.c new file +73 -0
73 lines not on this page · raw message
arch/x86/kernel/cpu/sgx/epc_cgroup.h new file +72 -0
72 lines not on this page · raw message
arch/x86/kernel/cpu/sgx/main.c +39 -3
105 lines not on this page · raw message
arch/x86/kernel/cpu/sgx/sgx.h +21 -0
@@ -39,14 +39,35 @@ enum sgx_reclaim { SGX_DO_RECLAIM }; +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 }; +static inline void sgx_epc_page_set_cgroup (struct sgx_epc_page *page,struct sgx_cgroup *cg) +{ +#ifdef CONFIG_CGROUP_MISC + page->sgx_cg = cg; +#endif +} + +static inline struct sgx_cgroup *sgx_epc_page_get_cgroup (struct sgx_epc_page *page) +{ +#ifdef CONFIG_CGROUP_MISC +return page->sgx_cg; +#else +return NULL ; +#endif +} +/* * Contains the tracking data for NUMA nodes having EPC pages. Most importantly, * the free page list local to the node is stored here.
include/linux/misc_cgroup.h +2 -0
@@ -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; };/**
Signature · 1 line
2.43.0
Re: [PATCH v15 05/14] x86/sgx: Implement basic EPC misc cgroup functionality
Review static int __init sgx_init(void) · line 963
Quoted 11 lines
(quoted diff) +2
-1@@ -921,7 +956,8 @@ static int __init sgx_init(void) if (!sgx_page_cache_init()) return -ENOMEM; - if (!sgx_page_reclaimer_init()) { + if (!sgx_page_reclaimer_init() || !sgx_cgroup_init()) { + misc_cg_set_capacity(MISC_CG_RES_SGX_EPC, 0); ret = -ENOMEM; goto err_page_cache; }
This code change is wrong due to two reasons:
1) If sgx_page_reclaimer_init() was successful, but sgx_cgroup_init() failed, you actually need to 'goto err_kthread' because the ksgxd() kernel thread is already created and is running.
2) There are other cases after here that can also result in sgx_init() to fail completely, e.g., registering sgx_dev_provision mics device. We need to reset the capacity to 0 for those cases as well.
AFAICT, you need something like:
(quoted diff) +0 -0
b/arch/x86/kernel/cpu/sgx/main.c
index 27892e57c4ef..46f9c26992a7 100644
arch/x86/kernel/cpu/sgx/main.c +9 -1
@@ -930,6 +930,10 @@ static int __init sgx_init(void)if (ret) goto err_kthread; + ret =sgx_cgroup_init (); +if (ret) + goto err_provision; +/* * Always try to initialize the native *and* KVM drivers. * The KVM driver is less picky than the native one and
@@ -941,10 +945,12 @@ static int __init sgx_init(void) ret =sgx_drv_init ();if (sgx_vepc_init () && ret) - goto err_provision; + goto err_cgroup;return 0 ; +err_cgroup: +/* SGX EPC cgroup cleanup */ err_provision:misc_deregister (&sgx_dev_provision);
@@ -952,6 +958,8 @@ static int __init sgx_init(void)kthread_stop (ksgxd_tsk); err_page_cache: +misc_misc_cg_set_capacity (MISC_CG_RES_SGX_EPC ,0 ); +for (i =0 ; i < sgx_nr_epc_sections; i++) {vfree (sgx_epc_sections[i].pages);memunmap (sgx_epc_sections[i].virt_addr);
I put the sgx_cgroup_init() before sgx_drv_init() and sgx_vepc_init(), otherwise you will need sgx_drv_cleanup() and sgx_vepc_cleanup() respectively when sgx_cgroup_init() fails.
This looks a little bit weird too, though:
Calling misc_misc_cg_set_capacity() to reset capacity to 0 is done at end of sgx_init() error path, because the "set capacity" part is done in sgx_epc_cache_init().
But logically, both "set capacity" and "reset capacity to 0" should be SGX EPC cgroup operation, so it's more reasonable to do "set capacity" in sgx_cgroup_init() and do "reset to 0" in the
/* SGX EPC cgroup cleanup */
as shown above.
Eventually, you will need to do EPC cgroup cleanup anyway, e.g., to free the workqueue, so it's odd to have two places to handle EPC cgroup cleanup.
I understand the reason "set capacity" part is done in sgx_page_cache_init() now is because in that function you can easily get the capacity. But the fact is @sgx_numa_nodes also tracks EPC size for each node, so you can also get the total EPC size from @sgx_numa_node in sgx_cgroup_init() and set capacity there.
In this case, you can put "reset capacity to 0" and "free workqueue" together as the "SGX EPC cgroup cleanup", which is way more clear IMHO.
Re: [PATCH v15 05/14] x86/sgx: Implement basic EPC misc cgroup functionality
Quoted 59 lines · Kai
Review static int __init sgx_init(void) · line 963
Quoted 11 lines
(quoted diff) +2
-1@@ -921,7 +956,8 @@ static int __init sgx_init(void) if (!sgx_page_cache_init()) return -ENOMEM; - if (!sgx_page_reclaimer_init()) { + if (!sgx_page_reclaimer_init() || !sgx_cgroup_init()) { + misc_cg_set_capacity(MISC_CG_RES_SGX_EPC, 0); ret = -ENOMEM; goto err_page_cache; }This code change is wrong due to two reasons:
1) If
sgx_page_reclaimer_init()was successful, butsgx_cgroup_init()failed, you actually need to 'gotoerr_kthread' because theksgxd()kernel thread is already created and is running.2) There are other cases after here that can also result in
sgx_init()to fail completely, e.g., registeringsgx_dev_provisionmics device. We need to reset the capacity to 0 for those cases as well.AFAICT, you need something like:
arch/x86/kernel/cpu/sgx/main.c +7
-1@@ -930,6 +930,10 @@ static int __init sgx_init(void)if (ret) goto err_kthread; + ret =sgx_cgroup_init (); +if (ret) + goto err_provision; +/* * Always try to initialize the native *and* KVM drivers. * The KVM driver is less picky than the native one and @@ -941,10 +945,12 @@ static int __init sgx_init(void) ret =sgx_drv_init ();if (sgx_vepc_init () && ret) - goto err_provision; + goto err_cgroup;return 0 ; +err_cgroup: +/* SGX EPC cgroup cleanup */ err_provision:misc_deregister (&sgx_dev_provision);@@ -952,6 +958,8 @@ static int __init sgx_init(void)kthread_stop (ksgxd_tsk);
err_page_cache:arch/x86/kernel/cpu/sgx/main.c +2
-0+misc_misc_cg_set_capacity (MISC_CG_RES_SGX_EPC ,0 ); +for (i =0 ; i < sgx_nr_epc_sections; i++) {vfree (sgx_epc_sections[i].pages);memunmap (sgx_epc_sections[i].virt_addr);
Quoted 4 lines
I put the
sgx_cgroup_init()beforesgx_drv_init()andsgx_vepc_init(), otherwise you will needsgx_drv_cleanup()andsgx_vepc_cleanup()respectively whensgx_cgroup_init()fails.
Yes, good catch.
Quoted 16 lines
This looks a little bit weird too, though:
Calling
misc_misc_cg_set_capacity()to reset capacity to 0 is done at end
ofsgx_init()error path, because the "set capacity" part is done insgx_epc_cache_init(). But logically, both "set capacity" and "reset capacity to 0" should be SGX EPC cgroup operation, so it's more reasonable to do "set capacity" insgx_cgroup_init()and do "reset to 0" in the/* SGX EPC cgroup cleanup */as shown above.
Eventually, you will need to do EPC cgroup cleanup anyway, e.g., to free the workqueue, so it's odd to have two places to handle EPC cgroup cleanup.
Quoted 9 lines
I understand the reason "set capacity" part is done in
sgx_page_cache_init()now is because in that function you can easily get the capacity. But the fact is @sgx_numa_nodes also tracks EPC size for each node, so you can also get the total EPC size from @sgx_numa_node insgx_cgroup_init()and set capacity there.In this case, you can put "reset capacity to 0" and "free workqueue" together as the "SGX EPC cgroup cleanup", which is way more clear IMHO.
Okay, will expose @sgx_numa_nodes to epc_cgroup.c and do the calculations in sgx_cgroup_init(). Thanks
Haitao
Re: [PATCH v15 05/14] x86/sgx: Implement basic EPC misc cgroup functionality
Quoted 94 lines · Haitao Huang
Quoted 61 lines · Kai
Review static int __init sgx_init(void) · line 963
Quoted 11 lines
(quoted diff) +2
-1@@ -921,7 +956,8 @@ static int __init sgx_init(void) if (!sgx_page_cache_init()) return -ENOMEM; - if (!sgx_page_reclaimer_init()) { + if (!sgx_page_reclaimer_init() || !sgx_cgroup_init()) { + misc_cg_set_capacity(MISC_CG_RES_SGX_EPC, 0); ret = -ENOMEM; goto err_page_cache; }This code change is wrong due to two reasons:
1) If
sgx_page_reclaimer_init()was successful, butsgx_cgroup_init()failed, you actually need to 'gotoerr_kthread' because theksgxd()kernel
thread is already created and is running.2) There are other cases after here that can also result in
sgx_init()to fail completely, e.g., registeringsgx_dev_provisionmics device. We need
to reset the capacity to 0 for those cases as well.AFAICT, you need something like:
arch/x86/kernel/cpu/sgx/main.c +7
-1@@ -930,6 +930,10 @@ static int __init sgx_init(void)if (ret) goto err_kthread; + ret =sgx_cgroup_init (); +if (ret) + goto err_provision; +/* * Always try to initialize the native *and* KVM drivers. * The KVM driver is less picky than the native one and @@ -941,10 +945,12 @@ static int __init sgx_init(void) ret =sgx_drv_init ();if (sgx_vepc_init () && ret) - goto err_provision; + goto err_cgroup;return 0 ; +err_cgroup: +/* SGX EPC cgroup cleanup */ err_provision:misc_deregister (&sgx_dev_provision);@@ -952,6 +958,8 @@ static int __init sgx_init(void)kthread_stop (ksgxd_tsk);
err_page_cache:arch/x86/kernel/cpu/sgx/main.c +2
-0+misc_misc_cg_set_capacity (MISC_CG_RES_SGX_EPC ,0 ); +for (i =0 ; i < sgx_nr_epc_sections; i++) {vfree (sgx_epc_sections[i].pages);memunmap (sgx_epc_sections[i].virt_addr);Quoted 4 lines
I put the
sgx_cgroup_init()beforesgx_drv_init()andsgx_vepc_init(), otherwise you will needsgx_drv_cleanup()andsgx_vepc_cleanup()respectively whensgx_cgroup_init()fails.Yes, good catch.
Quoted 24 lines
This looks a little bit weird too, though:
Calling
misc_misc_cg_set_capacity()to reset capacity to 0 is done at end ofsgx_init()error path, because the "set capacity" part is done insgx_epc_cache_init().
But logically, both "set capacity" and "reset capacity to 0" should be SGX
EPC cgroup operation, so it's more reasonable to do "set capacity" insgx_cgroup_init()and do "reset to 0" in the/* SGX EPC cgroup cleanup */as shown above.
Eventually, you will need to do EPC cgroup cleanup anyway, e.g., to free the workqueue, so it's odd to have two places to handle EPC cgroup cleanup.
I understand the reason "set capacity" part is done in
sgx_page_cache_init()now is because in that function you can easily get the capacity. But the fact is @sgx_numa_nodes also tracks EPC size for each node, so you can also get the total EPC size from @sgx_numa_node insgx_cgroup_init()and set capacity there.
Quoted 6 lines
Quoted 3 lines
In this case, you can put "reset capacity to 0" and "free workqueue" together as the "SGX EPC cgroup cleanup", which is way more clear IMHO.
Okay, will expose @sgx_numa_nodes to
epc_cgroup.cand do the calculations insgx_cgroup_init().
Looks you will also need to expose @sgx_numa_mask, which looks overkill.
Other options:
1) Expose a function to return total EPC pages/size in "sgx.h".
2) Move out the new 'capacity' variable in this patch as a global variable and expose it in "sgx.h" (perhaps rename to 'sgx_total_epc_pages/size').
3) Make sgx_cgroup_init() to take an argument of total EPC pages/size, and pass it in sgx_init().
For 3) there are also options to get total EPC pages/size:
a) Move out the new 'capacity' variable in this patch as a static.
b) Add a function to calculate total EPC pages/size from sgx_numa_nodes.
Hmm.. I think we can just use option 2)?
Re: [PATCH v15 05/14] x86/sgx: Implement basic EPC misc cgroup functionality
Quoted 118 lines · Kai
Quoted 98 lines · Haitao Huang
Quoted 14 lines · Kai
Review static int __init sgx_init(void) · line 963
Quoted 11 lines
(quoted diff) +2
-1@@ -921,7 +956,8 @@ static int __init sgx_init(void) if (!sgx_page_cache_init()) return -ENOMEM; - if (!sgx_page_reclaimer_init()) { + if (!sgx_page_reclaimer_init() || !sgx_cgroup_init()) { + misc_cg_set_capacity(MISC_CG_RES_SGX_EPC, 0); ret = -ENOMEM; goto err_page_cache; }This code change is wrong due to two reasons:
Quoted 6 lines
1) If
sgx_page_reclaimer_init()was successful, butsgx_cgroup_init()failed, you actually need to 'gotoerr_kthread' because theksgxd()kernel
thread is already created and is running.2) There are other cases after here that can also result in
sgx_init()toQuoted 41 lines
fail completely, e.g., registering
sgx_dev_provisionmics device. We need
to reset the capacity to 0 for those cases as well.AFAICT, you need something like:
arch/x86/kernel/cpu/sgx/main.c +7
-1@@ -930,6 +930,10 @@ static int __init sgx_init(void)if (ret) goto err_kthread; + ret =sgx_cgroup_init (); +if (ret) + goto err_provision; +/* * Always try to initialize the native *and* KVM drivers. * The KVM driver is less picky than the native one and @@ -941,10 +945,12 @@ static int __init sgx_init(void) ret =sgx_drv_init ();if (sgx_vepc_init () && ret) - goto err_provision; + goto err_cgroup;return 0 ; +err_cgroup: +/* SGX EPC cgroup cleanup */ err_provision:misc_deregister (&sgx_dev_provision);@@ -952,6 +958,8 @@ static int __init sgx_init(void)kthread_stop (ksgxd_tsk);
err_page_cache:arch/x86/kernel/cpu/sgx/main.c +2
-0+misc_misc_cg_set_capacity (MISC_CG_RES_SGX_EPC ,0 ); +for (i =0 ; i < sgx_nr_epc_sections; i++) {vfree (sgx_epc_sections[i].pages);memunmap (sgx_epc_sections[i].virt_addr);Quoted 4 lines
I put the
sgx_cgroup_init()beforesgx_drv_init()andsgx_vepc_init(), otherwise you will needsgx_drv_cleanup()andsgx_vepc_cleanup()respectively whensgx_cgroup_init()fails.Yes, good catch.
Quoted 3 lines
This looks a little bit weird too, though:
Calling
misc_misc_cg_set_capacity()to reset capacity to 0 is done atend
Quoted 7 lines
of
sgx_init()error path, because the "set capacity" part is done insgx_epc_cache_init().
But logically, both "set capacity" and "reset capacity to 0" should be SGX
EPC cgroup operation, so it's more reasonable to do "set capacity" insgx_cgroup_init()and do "reset to 0" in theQuoted 5 lines
/* SGX EPC cgroup cleanup */as shown above.
Eventually, you will need to do EPC cgroup cleanup anyway, e.g., to
free
Quoted 5 lines
the workqueue, so it's odd to have two places to handle EPC cgroup cleanup.
I understand the reason "set capacity" part is done in
sgx_page_cache_init()now is because in that function you can easilyget
Quoted 1 line
the capacity. But the fact is @sgx_numa_nodes also tracks EPC size
for
Quoted 1 line
each node, so you can also get the total EPC size from @sgx_numa_node
in
Quoted 9 lines
Quoted 4 lines
sgx_cgroup_init()and set capacity there.In this case, you can put "reset capacity to 0" and "free workqueue" together as the "SGX EPC cgroup cleanup", which is way more clear
IMHO.
Quoted 1 line
Okay, will expose @sgx_numa_nodes to
epc_cgroup.cand do the calculations insgx_cgroup_init().Looks you will also need to expose @sgx_numa_mask, which looks overkill.
Other options:
1) Expose a function to return total EPC pages/size in "
sgx.h".2) Move out the new 'capacity' variable in this patch as a global variable and expose it in "
sgx.h" (perhaps rename to 'sgx_total_epc_pages/size').
Quoted 10 lines
3) Make
sgx_cgroup_init()to take an argument of total EPC pages/size, and pass it insgx_init(). For 3) there are also options to get total EPC pages/size:a) Move out the new 'capacity' variable in this patch as a static.
b) Add a function to calculate total EPC pages/size from
sgx_numa_nodes.Hmm.. I think we can just use option 2)?
I was about doing this in sgx_cgroup_init():
for (i = 0; i < num_possible_nodes(); i++)
capacity += sgx_numa_nodes[i].size;any concern using num_possible_nodes()?
I think case handled in sgx_page_cache_init() for a node with no epc (or mask). Only requirement is sgx_cgroup_init() called after sgx_page_cache_init(). Haitao
Re: [PATCH v15 05/14] x86/sgx: Implement basic EPC misc cgroup functionality
Quoted 136 lines · Haitao Huang
Quoted 126 lines · Kai
Quoted 101 lines · Haitao Huang
Quoted 14 lines · Kai
Review static int __init sgx_init(void) · line 963
Quoted 11 lines
(quoted diff) +2
-1@@ -921,7 +956,8 @@ static int __init sgx_init(void) if (!sgx_page_cache_init()) return -ENOMEM; - if (!sgx_page_reclaimer_init()) { + if (!sgx_page_reclaimer_init() || !sgx_cgroup_init()) { + misc_cg_set_capacity(MISC_CG_RES_SGX_EPC, 0); ret = -ENOMEM; goto err_page_cache; }This code change is wrong due to two reasons:
Quoted 6 lines
1) If
sgx_page_reclaimer_init()was successful, butsgx_cgroup_init()failed, you actually need to 'gotoerr_kthread' because theksgxd()kernel
thread is already created and is running.2) There are other cases after here that can also result in
sgx_init()toQuoted 41 lines
fail completely, e.g., registering
sgx_dev_provisionmics device. We need
to reset the capacity to 0 for those cases as well.AFAICT, you need something like:
arch/x86/kernel/cpu/sgx/main.c +7
-1@@ -930,6 +930,10 @@ static int __init sgx_init(void)if (ret) goto err_kthread; + ret =sgx_cgroup_init (); +if (ret) + goto err_provision; +/* * Always try to initialize the native *and* KVM drivers. * The KVM driver is less picky than the native one and @@ -941,10 +945,12 @@ static int __init sgx_init(void) ret =sgx_drv_init ();if (sgx_vepc_init () && ret) - goto err_provision; + goto err_cgroup;return 0 ; +err_cgroup: +/* SGX EPC cgroup cleanup */ err_provision:misc_deregister (&sgx_dev_provision);@@ -952,6 +958,8 @@ static int __init sgx_init(void)kthread_stop (ksgxd_tsk);
err_page_cache:arch/x86/kernel/cpu/sgx/main.c +2
-0+misc_misc_cg_set_capacity (MISC_CG_RES_SGX_EPC ,0 ); +for (i =0 ; i < sgx_nr_epc_sections; i++) {vfree (sgx_epc_sections[i].pages);memunmap (sgx_epc_sections[i].virt_addr);Quoted 4 lines
I put the
sgx_cgroup_init()beforesgx_drv_init()andsgx_vepc_init(), otherwise you will needsgx_drv_cleanup()andsgx_vepc_cleanup()respectively whensgx_cgroup_init()fails.Yes, good catch.
Quoted 3 lines
This looks a little bit weird too, though:
Calling
misc_misc_cg_set_capacity()to reset capacity to 0 is done atend
Quoted 7 lines
of
sgx_init()error path, because the "set capacity" part is done insgx_epc_cache_init().
But logically, both "set capacity" and "reset capacity to 0" should be SGX
EPC cgroup operation, so it's more reasonable to do "set capacity" insgx_cgroup_init()and do "reset to 0" in theQuoted 5 lines
/* SGX EPC cgroup cleanup */as shown above.
Eventually, you will need to do EPC cgroup cleanup anyway, e.g., to
free
Quoted 5 lines
the workqueue, so it's odd to have two places to handle EPC cgroup cleanup.
I understand the reason "set capacity" part is done in
sgx_page_cache_init()now is because in that function you can easilyget
Quoted 1 line
the capacity. But the fact is @sgx_numa_nodes also tracks EPC size
for
Quoted 1 line
each node, so you can also get the total EPC size from @sgx_numa_node
in
Quoted 2 lines
sgx_cgroup_init()and set capacity there.Quoted 8 lines
Quoted 2 lines
In this case, you can put "reset capacity to 0" and "free workqueue" together as the "SGX EPC cgroup cleanup", which is way more clear
IMHO.
Quoted 1 line
Okay, will expose @sgx_numa_nodes to
epc_cgroup.cand do the calculations
insgx_cgroup_init().Looks you will also need to expose @sgx_numa_mask, which looks overkill.
Other options:
1) Expose a function to return total EPC pages/size in "
sgx.h".2) Move out the new 'capacity' variable in this patch as a global variable
and expose it in "sgx.h" (perhaps rename to 'sgx_total_epc_pages/size').3) Make
sgx_cgroup_init()to take an argument of total EPC pages/size, and
pass it insgx_init().
For 3) there are also options to get total EPC pages/size:Quoted 7 lines
a) Move out the new 'capacity' variable in this patch as a static.
b) Add a function to calculate total EPC pages/size from
sgx_numa_nodes.Hmm.. I think we can just use option 2)?
I was about doing this in
sgx_cgroup_init():
Quoted 9 lines
for (i = 0; i < num_possible_nodes(); i++) capacity += sgx_numa_nodes[i].size;any concern using
num_possible_nodes()?I think case handled in
sgx_page_cache_init()for a node with no epc (or mask). Only requirement issgx_cgroup_init()called aftersgx_page_cache_init().
All other code uses sgx_numa_mask to tell whether a node has EPC. It would be great if we use this way consistently in all SGX code.
PATCH v15 6/14 x86/sgx: Add sgx_epc_lru_list to encapsulate LRU list
In reply to Huang, HaitaoFrom: Sean Christopherson <[email protected]>
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.
---
V15:
- Add comment for spinlock. (Jarkko, Kai)
V6:
- removed introduction to unreclaimables in commit message.
V4:
- Removed unneeded comments for the spinlock and the non-reclaimables. (Kai, Jarkko)
- Revised the commit to add introduction comments for unreclaimables and multiple LRU lists.(Kai)
- Reordered the patches: delay all changes for unreclaimables to later, and this one becomes the first change in the SGX subsystem.
V3:
- Removed the helper functions and revised commit messages.
--- arch/x86/kernel/cpu/sgx/main.c | 39 +++++++++++++++++----------------- arch/x86/kernel/cpu/sgx/sgx.h | 16 ++++++++++++++ 2 files changed, 36 insertions(+), 19 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +20 -19
@@ -28,10 +28,9 @@ static DEFINE_XARRAY(sgx_epc_address_space);/* * These variables are part of the state of the reclaimer, and must be accessed -* with sgx_reclaimer_lock acquired. +* with sgx_global_lru.lock acquired. */ -static LIST_HEAD (sgx_active_page_list); -static DEFINE_SPINLOCK (sgx_reclaimer_lock); +static struct sgx_epc_lru_list sgx_global_lru;static atomic_long_t sgx_nr_free_pages =ATOMIC_LONG_INIT (0 );
@@ -306,13 +305,13 @@ static void sgx_reclaim_pages(void)int ret;int i; -spin_lock (&sgx_reclaimer_lock); +spin_lock (&sgx_global_lru.lock);for (i =0 ; i <SGX_NR_TO_SCAN ; i++) { -if (list_empty (&sgx_active_page_list)) + epc_page =list_first_entry_or_null (&sgx_global_lru.reclaimable, + struct sgx_epc_page, list); +if (!epc_page)break ; - epc_page =list_first_entry (&sgx_active_page_list, - struct sgx_epc_page, list);list_del_init (&epc_page->list); encl_page = epc_page->owner;
@@ -324,7 +323,7 @@ static void sgx_reclaim_pages(void)*/ epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED ; } -spin_unlock (&sgx_reclaimer_lock); +spin_unlock (&sgx_global_lru.lock);for (i =0 ; i < cnt; i++) { epc_page = chunk[i];
@@ -347,9 +346,9 @@ static void sgx_reclaim_pages(void)continue ; skip: -spin_lock (&sgx_reclaimer_lock); -list_add_tail (&epc_page->list, &sgx_active_page_list); -spin_unlock (&sgx_reclaimer_lock); +spin_lock (&sgx_global_lru.lock); +list_add_tail (&epc_page->list, &sgx_global_lru.reclaimable); +spin_unlock (&sgx_global_lru.lock);kref_put (&encl_page->encl->refcount, sgx_encl_release);
@@ -380,7 +379,7 @@ static void sgx_reclaim_pages(void)static bool sgx_should_reclaim (unsigned long watermark) {return atomic_long_read (&sgx_nr_free_pages) < watermark && - !list_empty (&sgx_active_page_list); + !list_empty (&sgx_global_lru.reclaimable); }/*
@@ -432,6 +431,8 @@ static bool __init sgx_page_reclaimer_init(void) ksgxd_tsk = tsk; +sgx_lru_init (&sgx_global_lru); +return true; }
@@ -507,10 +508,10 @@ static struct sgx_epc_page *__sgx_alloc_epc_page(void)*/ void sgx_mark_page_reclaimable (struct sgx_epc_page *page) { -spin_lock (&sgx_reclaimer_lock); +spin_lock (&sgx_global_lru.lock); page->flags |=SGX_EPC_PAGE_RECLAIMER_TRACKED ; -list_add_tail (&page->list, &sgx_active_page_list); -spin_unlock (&sgx_reclaimer_lock); +list_add_tail (&page->list, &sgx_global_lru.reclaimable); +spin_unlock (&sgx_global_lru.lock); }/**
@@ -525,18 +526,18 @@ void sgx_mark_page_reclaimable(struct sgx_epc_page *page)*/ int sgx_unmark_page_reclaimable (struct sgx_epc_page *page) { -spin_lock (&sgx_reclaimer_lock); +spin_lock (&sgx_global_lru.lock);if (page->flags &SGX_EPC_PAGE_RECLAIMER_TRACKED ) {/* The page is being reclaimed. */ if (list_empty (&page->list)) { -spin_unlock (&sgx_reclaimer_lock); +spin_unlock (&sgx_global_lru.lock);return -EBUSY ; }list_del (&page->list); page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED ; } -spin_unlock (&sgx_reclaimer_lock); +spin_unlock (&sgx_global_lru.lock);return 0 ; }
@@ -578,7 +579,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim reclaim)break ; } -if (list_empty (&sgx_active_page_list)) { +if (list_empty (&sgx_global_lru.reclaimable)) { page =ERR_PTR (-ENOMEM );break ; }
arch/x86/kernel/cpu/sgx/sgx.h +16 -0
@@ -114,6 +114,22 @@ static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page)return section->virt_addr + index *PAGE_SIZE ; } +/* +* Contains EPC pages tracked by the global reclaimer (ksgxd) or an EPC +* cgroup. +*/ +struct sgx_epc_lru_list { +/* Use this lock to protect access from multiple reclamation worker threads */ +spinlock_t lock; +struct list_head reclaimable; +}; + +static inline void sgx_lru_init (struct sgx_epc_lru_list *lru) +{ +spin_lock_init (&lru->lock); +INIT_LIST_HEAD (&lru->reclaimable); +} +void sgx_free_epc_page (struct sgx_epc_page *page);void sgx_reclaim_direct (void );
Signature · 1 line
2.43.0
PATCH v15 7/14 x86/sgx: Abstract tracking reclaimable pages in LRU
In reply to Huang, HaitaoFrom: Kristen Carlson Accardi <[email protected]>
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.
---
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(-)
arch/x86/kernel/cpu/sgx/main.c +18 -12
@@ -32,6 +32,11 @@ static DEFINE_XARRAY(sgx_epc_address_space);*/ static struct sgx_epc_lru_list sgx_global_lru; +static inline struct sgx_epc_lru_list *sgx_epc_page_lru (struct sgx_epc_page *epc_page) +{ +return &sgx_global_lru; +} +static atomic_long_t sgx_nr_free_pages =ATOMIC_LONG_INIT (0 );/* Nodes with one or more EPC sections. */
@@ -500,25 +505,24 @@ static struct sgx_epc_page *__sgx_alloc_epc_page(void) }/** -* sgx_mark_page_reclaimable() - Mark a page as reclaimable +* sgx_mark_page_reclaimable() - Mark a page as reclaimable and track it in 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 =sgx_epc_page_lru (page); + +spin_lock (&lru->lock); page->flags |=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); }/** -* 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 list. +* Clear the reclaimable flag if set and remove the page from its LRU. * * Return: * 0 on success,
@@ -526,18 +530,20 @@ void sgx_mark_page_reclaimable(struct sgx_epc_page *page)*/ int sgx_unmark_page_reclaimable (struct sgx_epc_page *page) { -spin_lock (&sgx_global_lru.lock); +struct sgx_epc_lru_list *lru =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 ; }list_del (&page->list); page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED ; } -spin_unlock (&sgx_global_lru.lock); +spin_unlock (&lru->lock);return 0 ; }
Signature · 1 line
2.43.0
PATCH v15 8/14 x86/sgx: Add basic EPC reclamation flow for cgroup
In reply to Huang, HaitaoFrom: Kristen Carlson Accardi <[email protected]>
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. In other cases, the caller may invoke this function in a loop to ensure enough pages reclaimed for its usage. To ensure all descendant groups scanned in a round-robin fashion in those cases, sgx_cgroup_reclaim_pages() takes in a starting cgroup and returns the next cgroup that the caller can pass in as the new starting cgroup for a subsequent call.
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.
---
V14:
- Allow sgx_cgroup_reclaim_pages() to continue from previous tree-walk. It takes in a 'start' node and returns the 'next' node for the caller to use as the new 'start'. This is to ensure pages in lower level cgroups can be reclaimed if all pages in upper level nodes are "too young". (Kai)
- Move renaming sgx_should_reclaim() to sgx_should_reclaim_global() from a later patch to this one. (Kai)
V11:
- Use commit message suggested by Kai
- Remove "usage" comments for functions. (Kai)
V10:
- Simplify the signature by removing a pointer to nr_to_scan (Kai)
- Return pages attempted instead of reclaimed as it is really what the cgroup caller needs to track progress. This further simplifies the design.
- Merge patch for exposing sgx_reclaim_pages() with basic synchronous reclamation. (Kai)
- Shorten names for EPC cgroup functions. (Jarkko)
- Fix/add comments to justify the design (Kai)
- Separate out a helper for for addressing single iteration of the loop in sgx_cgroup_try_charge(). (Jarkko)
V9:
- Add comments for static variables. (Jarkko)
V8:
- Use width of 80 characters in text paragraphs. (Jarkko)
- Remove alignment for substructure variables. (Jarkko)
V7:
- Reworked from patch 9 of V6, "x86/sgx: Restructure top-level EPC reclaim function". Do not split the top level function (Kai)
- Dropped patches 7 and 8 of V6.
- Split this out from the big patch, #10 in V6. (Dave, Kai)
--- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 149 ++++++++++++++++++++++++++- arch/x86/kernel/cpu/sgx/epc_cgroup.h | 5 +- arch/x86/kernel/cpu/sgx/main.c | 55 ++++++---- arch/x86/kernel/cpu/sgx/sgx.h | 1 + 4 files changed, 183 insertions(+), 27 deletions(-)
arch/x86/kernel/cpu/sgx/epc_cgroup.c +146 -3
@@ -8,16 +8,158 @@static struct sgx_cgroup sgx_cg_root;/** -* 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 lists. +*/ +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 = true; + +/* +* Caller must ensure css_root ref acquired +*/ + css_root = &root->css; + +rcu_read_lock (); +css_for_each_descendant_pre (pos, css_root) { +if (!css_tryget (pos)) +break ; + +rcu_read_unlock (); + + sgx_cg =sgx_cgroup_from_misc_cg (css_misc (pos)); + +spin_lock (&sgx_cg->lru.lock); + ret =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. +* @start: The descendant cgroup from which to start the tree walking. +* +* This function performs a pre-order walk in the cgroup tree under the given +* root, starting from the node %start, or from the root if %start is NULL. The +* function will attempt to reclaim pages at each node until a fixed number of +* pages (%SGX_NR_TO_SCAN) are attempted for reclamation. No guarantee of +* success 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. +* +* In some cases, a caller may want to ensure enough reclamation until its +* specific need is met. In those cases, the caller should invoke this function +* in a loop, and at each iteration passes in the same root and the next node +* returned from the previous call as the new %start. +* +* Return: The next misc cgroup in the subtree to continue the scanning and +* attempt for more reclamation from this subtree if needed. Caller must +* release the reference if the returned is not used as %start for a subsequent +* call. +*/ +static struct misc_cg *sgx_cgroup_reclaim_pages (struct misc_cg *root,struct misc_cg *start) +{ +struct cgroup_subsys_state *css_root, *pos; +struct cgroup_subsys_state *next =NULL ; +struct sgx_cgroup *sgx_cg; +unsigned int cnt =0 ; + +/* Caller must ensure css_root and start ref's acquired */ + css_root = &root->css; +if (start) + pos = &start->css; +else + pos = css_root; + +while (cnt <SGX_NR_TO_SCAN ) { + sgx_cg =sgx_cgroup_from_misc_cg (css_misc (pos)); + cnt +=sgx_reclaim_pages (&sgx_cg->lru); + +rcu_read_lock (); + + next =css_next_descendant_pre (pos, css_root); + +if (pos != css_root) +css_put (pos); + +if (!next || !css_tryget (next)) { +/* We are done if next is NULL or not safe to continue +* the walk if next is dead. Return NULL and the caller +* determines whether to restart from root. +*/ +rcu_read_unlock (); +return NULL ; + } + +rcu_read_unlock (); + pos = next; + } + +return css_misc (next); +} + +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 reclaim) { -return misc_cg_try_charge (MISC_CG_RES_SGX_EPC , sgx_cg->cg,PAGE_SIZE ); +int ret; +struct misc_cg *cg_next =NULL ; + +for (;;) { + ret =__sgx_cgroup_try_charge (sgx_cg); + +if (ret != -EBUSY ) + goto out; + +if (reclaim ==SGX_NO_RECLAIM ) { + ret = -ENOMEM ; + goto out; + } + + cg_next =sgx_cgroup_reclaim_pages (sgx_cg->cg, cg_next); +cond_resched (); + } + +out: +if (cg_next != sgx_cg->cg) +put_misc_cg (cg_next); +return ret; }/**
@@ -42,6 +184,7 @@ static void sgx_cgroup_free(struct misc_cg *cg)static void sgx_cgroup_misc_init (struct misc_cg *cg,struct sgx_cgroup *sgx_cg) { +sgx_lru_init (&sgx_cg->lru); cg->res[MISC_CG_RES_SGX_EPC ].priv = sgx_cg; sgx_cg->cg = cg; }
arch/x86/kernel/cpu/sgx/epc_cgroup.h +3 -2
@@ -20,7 +20,7 @@ static inline struct sgx_cgroup *sgx_get_current_cg(void)static inline void sgx_put_cg (struct sgx_cgroup *sgx_cg) { } -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 sgx_reclaim reclaim) {return 0 ; }
@@ -33,6 +33,7 @@ static inline void sgx_cgroup_init(void) { }struct sgx_cgroup {struct misc_cg *cg; +struct sgx_epc_lru_list lru; };static inline struct sgx_cgroup *sgx_cgroup_from_misc_cg (struct misc_cg *cg)
@@ -63,7 +64,7 @@ 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); +int sgx_cgroup_try_charge (struct sgx_cgroup *sgx_cg,enum sgx_reclaim reclaim);void sgx_cgroup_uncharge (struct sgx_cgroup *sgx_cg);int __init sgx_cgroup_init (void );
arch/x86/kernel/cpu/sgx/main.c +33 -22
131 lines not on this page · raw message
arch/x86/kernel/cpu/sgx/sgx.h +1 -0
@@ -136,6 +136,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 reclaim); +unsigned int sgx_reclaim_pages (struct sgx_epc_lru_list *lru);void sgx_ipi_cb (void *info);
Signature · 1 line
2.43.0
Re: [PATCH v15 08/14] x86/sgx: Add basic EPC reclamation flow for cgroup
Quoted 33 lines · Haitao
From: Kristen Carlson Accardi <[email protected]>
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 ofSGX_NR_TO_SCAN(16) pages. Specifically, it always "scans", or "isolates"SGX_NR_TO_SCANpages 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 newsgx_reclaim_pages()untilSGX_NR_TO_SCANpages are "scanned".This implementation, encapsulated in
sgx_cgroup_reclaim_pages(), always tries to reclaimSGX_NR_TO_SCANpages 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.
[...]
Quoted 6 lines
In other cases, the caller may invoke this function in a loop to ensure enough pages reclaimed for its usage. To ensure all descendant groups scanned in a round-robin fashion in those cases,
sgx_cgroup_reclaim_pages()takes in a starting cgroup and returns the next cgroup that the caller can pass in as the new starting cgroup for a subsequent call.
AFAICT this part is new, and I believe this "round-robin" thing is just for the "global reclaim"? Or is it also for per-cgroup reclaim where more than SGX_NR_TO_SCAN pages needs to be reclaimed?
I wish the changelog should just point out what consumers will use this new sgx_cgroup_reclaim_pages(), like:
The sgx_cgroup_reclaim_pages() will be used in three cases:
1) direct/sync per-cgroup reclaim in try_charge()
2) indirect/async per-cgroup reclaim triggered in try_charge()
3) global reclaim
And then describe how will they use sgx_cgroup_reclaim_pages():
Both 1) and 2) can result in needing to reclaim more than SGX_NR_TO_SCAN pages, in which case we should <fill in how to reclaim>.
For 3), the new global reclaim should try tot match the existing global reclaim behaviour, that is to try to treat all EPC pages equally. <continue to explain how can sgx_cgroup_reclaim_pages() achieve this.>
With above context, we can justify why to make sgx_cgroup_reclaim_pages() in this form.
Quoted 21 lines
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_SCANpages): when LRUs have less thanSGX_NR_TO_SCANreclaimable pages, the actual reclaim of EPC pages will be split into smaller batches _across_ multiple LRUs with each being smaller thanSGX_NR_TO_SCANpages.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_SCANpages _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 anioctl().
Quoted 8 lines
With this building block in place, add synchronous reclamation support in
sgx_cgroup_try_charge(): trigger a call tosgx_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().
It seems you also should mention the new global reclaim will also use this sgx_cgroup_reclaim_pages()?
[...]
Quoted 21 lines
+/**
+ *sgx_cgroup_reclaim_pages()- reclaim EPC from a cgroup tree
+ * @root: The root of cgroup tree to reclaim from.
+ * @start: The descendant cgroup from which to start the tree walking.
+ *
+ * This function performs a pre-order walk in the cgroup tree under the given
+ * root, starting from the node %start, or from the root if %start is NULL. The
+ * function will attempt to reclaim pages at each node until a fixed number of
+ * pages (%SGX_NR_TO_SCAN) are attempted for reclamation. No guarantee of
+ * success 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.
+ *
+ * In some cases, a caller may want to ensure enough reclamation until its
+ * specific need is met. In those cases, the caller should invoke this function
+ * in a loop, and at each iteration passes in the same root and the next node
+ * returned from the previous call as the new %start.
+ *
+ * Return: The next misc cgroup in the subtree to continue the scanning and
+ * attempt for more reclamation from this subtree if needed.
[...]
Quoted 4 lines
Caller must
+ * release the reference if the returned is not used as %start for a subsequent
+ * call.
This sentence isn't clear to me.
First of all, release the reference "of what"? The %start, or the one returned by this function?
And is it because of ...
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 82
Quoted 9 lines
+ */
arch/x86/kernel/cpu/sgx/epc_cgroup.c +8
-0+static struct misc_cg *sgx_cgroup_reclaim_pages (struct misc_cg *root,struct misc_cg *start) +{ +struct cgroup_subsys_state *css_root, *pos; +struct cgroup_subsys_state *next =NULL ; +struct sgx_cgroup *sgx_cg; +unsigned int cnt =0 ; + +/* Caller must ensure css_root and start ref's acquired */
... the caller must acquire the ref of both @css_root and @css_start, and
...
Quoted 6 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +6
-0+ css_root = &root->css; +if (start) + pos = &start->css; +else + pos = css_root; +
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 98
Quoted 10 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +10
-0+while (cnt <SGX_NR_TO_SCAN ) { + sgx_cg =sgx_cgroup_from_misc_cg (css_misc (pos)); + cnt +=sgx_reclaim_pages (&sgx_cg->lru); + +rcu_read_lock (); + + next =css_next_descendant_pre (pos, css_root); + +if (pos != css_root) +css_put (pos);
... the ref is decreased internally?
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 104
Quoted 6 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +6
-0+ +if (!next || !css_tryget (next)) { +/* We are done if next is NULL or not safe to continue +* the walk if next is dead. Return NULL and the caller +* determines whether to restart from root. +*/
Incorrect comment style.
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 110
Quoted 6 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +6
-0+rcu_read_unlock (); +return NULL ; + } + +rcu_read_unlock (); + pos = next;
There's no ref grab here, wouldn't the above ...
if (pos != css_root) css_put(pos);
... decrease the ref w/o having it been increased?
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 113
Quoted 3 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +3
-0+ } + +return css_misc (next);
Here AFAICT the ref isn't increased, but ...
[...]
Quoted 23 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +15
-2+/** +* 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 reclaim) { -return misc_cg_try_charge (MISC_CG_RES_SGX_EPC , sgx_cg->cg,PAGE_SIZE ); +int ret; +struct misc_cg *cg_next =NULL ; + +for (;;) { + ret =__sgx_cgroup_try_charge (sgx_cg); + +if (ret != -EBUSY ) + goto out; + +if (reclaim ==SGX_NO_RECLAIM ) { + ret = -ENOMEM ;
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 161
Quoted 10 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +10
-0+ goto out; + } + + cg_next =sgx_cgroup_reclaim_pages (sgx_cg->cg, cg_next); +cond_resched (); + } + +out: +if (cg_next != sgx_cg->cg) +put_misc_cg (cg_next);
... if I am reading correctly, here you does the put anyway.
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 163
Quoted 3 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +1
-0+return ret; }
And when there are more than SGX_NR_TO_SCAN pages that need to reclaim, the above ...
for (;;) {
cg_next = sgx_cgroup_reclaim_pages(sgx_cg->cg, cg_next);
}... actually tries to reclaim those pages from @sgx_cg _AND_ it's descendants, and tries to do it _EQUALLY_.
Is this desired, or should we always try to reclaim from the @sgx_cg first, but only moves to the desendants when the @sgx_cg shouldn't be reclaimed anymore?
Anyway, it's different from the previous behaviour.
[...]
Quoted 16 lines
arch/x86/kernel/cpu/sgx/main.c +6
-1-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); } +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
arch/x86/kernel/cpu/sgx/main.c static bool sgx_should_reclaim(unsigned long watermark) · line 411
Quoted 9 lines
arch/x86/kernel/cpu/sgx/main.c +2
-2@@ -394,8 +405,8 @@ static bool sgx_should_reclaim(unsigned long watermark)*/ void sgx_reclaim_direct (void ) { -if (sgx_should_reclaim (SGX_NR_LOW_PAGES )) -sgx_reclaim_pages (); +if (sgx_should_reclaim_global (SGX_NR_LOW_PAGES )) +sgx_reclaim_pages_global (); }
I wish the rename was mentioned in the changelog too.
Re: [PATCH v15 08/14] x86/sgx: Add basic EPC reclamation flow for cgroup
Quoted 63 lines · Kai
Quoted 32 lines · Haitao
From: Kristen Carlson Accardi <[email protected]>
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 ofSGX_NR_TO_SCAN(16) pages. Specifically, it always "scans", or "isolates"SGX_NR_TO_SCANpages 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 newsgx_reclaim_pages()untilSGX_NR_TO_SCANpages are "scanned".
This implementation, encapsulated insgx_cgroup_reclaim_pages(), always tries to reclaimSGX_NR_TO_SCANpages 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.[...]
Quoted 6 lines
In other cases, the caller may invoke this function in a loop to ensure enough pages reclaimed for its usage. To ensure all descendant groups scanned in a round-robin fashion in those cases,
sgx_cgroup_reclaim_pages()takes in a starting cgroup and returns the next cgroup that the caller can pass in as the new starting cgroup for a subsequent call.AFAICT this part is new, and I believe this "round-robin" thing is just for the "global reclaim"? Or is it also for per-cgroup reclaim where more than
SGX_NR_TO_SCANpages needs to be reclaimed?I wish the changelog should just point out what consumers will use this new
sgx_cgroup_reclaim_pages(), like:The
sgx_cgroup_reclaim_pages()will be used in three cases:1) direct/sync per-cgroup reclaim in
try_charge()
2) indirect/async per-cgroup reclaim triggered intry_charge()
3) global reclaimAnd then describe how will they use
sgx_cgroup_reclaim_pages():Both 1) and 2) can result in needing to reclaim more than
SGX_NR_TO_SCANpages, in which case we should <fill in how to reclaim>.
Quoted 7 lines
For 3), the new global reclaim should try tot match the existing global reclaim behaviour, that is to try to treat all EPC pages equally. <continue to explain how can
sgx_cgroup_reclaim_pages()achieve this.>With above context, we can justify why to make
sgx_cgroup_reclaim_pages()in this form.
This new part is only to address the issue you raised in this thread:
https://lore.kernel.org/lkml/[email protected]/
Really it has nothing to do whether global, direct/async, per-cgroup contexts. They all should use the function the same way. This paragraph describes the design and
I thought the above new statements justify the reason we return 'next' so it can reclaim into descedant in round-robin fashion? No sure we need get into details of different usages of the functions which are in code actually?
I'll address the new global reclamation behavior later when this function was actually used for that. And the difference really exists before and not because of this change.
Quoted 75 lines
Quoted 21 lines
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_SCANpages): when LRUs have less thanSGX_NR_TO_SCANreclaimable pages, the actual reclaim of EPC pages will be split into smaller batches _across_ multiple LRUs with each being smaller thanSGX_NR_TO_SCANpages.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_SCANpages _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 anioctl().Quoted 8 lines
With this building block in place, add synchronous reclamation support in
sgx_cgroup_try_charge(): trigger a call tosgx_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().It seems you also should mention the new global reclaim will also use this
sgx_cgroup_reclaim_pages()?[...]
Quoted 31 lines
+/**
+ *sgx_cgroup_reclaim_pages()- reclaim EPC from a cgroup tree
+ * @root: The root of cgroup tree to reclaim from.
+ * @start: The descendant cgroup from which to start the tree walking.
+ *
+ * This function performs a pre-order walk in the cgroup tree under the given
+ * root, starting from the node %start, or from the root if %start is NULL. The
+ * function will attempt to reclaim pages at each node until a fixed number of
+ * pages (%SGX_NR_TO_SCAN) are attempted for reclamation. No guarantee of
+ * success 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.
+ *
+ * In some cases, a caller may want to ensure enough reclamation until its
+ * specific need is met. In those cases, the caller should invoke this function
+ * in a loop, and at each iteration passes in the same root and the next node
+ * returned from the previous call as the new %start.
+ *
+ * Return: The next misc cgroup in the subtree to continue the scanning and
+ * attempt for more reclamation from this subtree if needed.[...]
Quoted 5 lines
Caller must
+ * release the reference if the returned is not used as %start for a subsequent
+ * call.
Quoted 5 lines
This sentence isn't clear to me.
First of all, release the reference "of what"? The %start, or the one returned by this function?
will this be better?
* Return: A reference to next misc cgroup in the subtree to continue the
* scanning and attempt for more reclamation from this subtree if needed.
* Caller must release the returned reference if the returned is not used
as
* %start for a subsequent call.
Quoted 15 lines
And is it because of ...
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 82
Quoted 10 lines
+ */
+static structmisc_cg*sgx_cgroup_reclaim_pages(structmisc_cg*root, structmisc_cg*start)arch/x86/kernel/cpu/sgx/epc_cgroup.c +7
-0+{ +struct cgroup_subsys_state *css_root, *pos; +struct cgroup_subsys_state *next =NULL ; +struct sgx_cgroup *sgx_cg; +unsigned int cnt =0 ; + +/* Caller must ensure css_root and start ref's acquired */ ... the caller must acquire the ref of both @css_root and @css_start, and
...
yes
Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +10
-0+ css_root = &root->css; +if (start) + pos = &start->css; +else + pos = css_root; + +while (cnt <SGX_NR_TO_SCAN ) { + sgx_cg =sgx_cgroup_from_misc_cg (css_misc (pos)); + cnt +=sgx_reclaim_pages (&sgx_cg->lru); +
Quoted 9 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 98
Quoted 6 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +6
-0+rcu_read_lock (); + + next =css_next_descendant_pre (pos, css_root); + +if (pos != css_root) +css_put (pos);... the ref is decreased internally?
Right.
Quoted 9 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 104
Quoted 6 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +6
-0+ +if (!next || !css_tryget (next)) { +/* We are done if next is NULL or not safe to continue +* the walk if next is dead. Return NULL and the caller +* determines whether to restart from root. +*/ Incorrect comment style.
Will fix
Quoted 9 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 110
Quoted 6 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +6
-0+rcu_read_unlock (); +return NULL ; + } + +rcu_read_unlock (); + pos = next;There's no ref grab here, wouldn't the above ...
tryget() done to next
Quoted 11 lines
if (pos != css_root) css_put(pos);... decrease the ref w/o having it been increased?
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 113
Quoted 3 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +3
-0+ } + +return css_misc (next);Here AFAICT the ref isn't increased, but ...
We only return next when next && css_tryget(next), otherwise NULL.
Quoted 31 lines
[...]
Quoted 28 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +4
-1+/** +* 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_reclaimreclaim)
{arch/x86/kernel/cpu/sgx/epc_cgroup.c +15
-1-return misc_cg_try_charge (MISC_CG_RES_SGX_EPC , sgx_cg->cg,PAGE_SIZE ); +int ret; +struct misc_cg *cg_next =NULL ; + +for (;;) { + ret =__sgx_cgroup_try_charge (sgx_cg); + +if (ret != -EBUSY ) + goto out; + +if (reclaim ==SGX_NO_RECLAIM ) { + ret = -ENOMEM ; + goto out; + } + + cg_next =sgx_cgroup_reclaim_pages (sgx_cg->cg, cg_next);
Quoted 9 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 161
Quoted 6 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +6
-0+cond_resched (); + } + +out: +if (cg_next != sgx_cg->cg) +put_misc_cg (cg_next);... if I am reading correctly, here you does the put anyway.
cg_next ref is increased or it is NULL.
Only puts the last one no longer to be passed back in to sgx_cgroup_reclaim_page()
Quoted 6 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c line 163
Quoted 3 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +1
-0+return ret; }And when there are more than
SGX_NR_TO_SCANpages that need to reclaim, the above ...
Note, all sgx_cgroup_reclaim_pages() guarantees is scanning SGX_NR_TO_SCAN pages.
Quoted 5 lines
for (;;) {
cg_next = sgx_cgroup_reclaim_pages(sgx_cg->cg, cg_next);
}Quoted 7 lines
... actually tries to reclaim those pages from @sgx_cg _AND_ it's descendants, and tries to do it _EQUALLY_.
Is this desired, or should we always try to reclaim from the @sgx_cg first, but only moves to the desendants when the @sgx_cg shouldn't be reclaimed anymore?
we still reclaim in sgx_cg in first scan and attempt of reclaiming for SGX_NR_TOS_CAN pages, but if it turns out that did not satisfy caller needs, then caller goes on to reclaim from descendants by passing in 'next' as starting point.
Quoted 2 lines
Anyway, it's different from the previous behaviour.
Again, this is to fix the issue you raised. I consider it improved behavior :-)
Quoted 23 lines
[...]
Quoted 21 lines
arch/x86/kernel/cpu/sgx/main.c +6
-1-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); } +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 bearch/x86/kernel/cpu/sgx/main.c +0
-0@@ -394,8 +405,8 @@ static bool sgx_should_reclaim(unsigned longwatermark) */ void sgx_reclaim_direct (void ) {
Quoted 9 lines
arch/x86/kernel/cpu/sgx/main.c static bool sgx_should_reclaim(unsigned long watermark) · line 411
Quoted 6 lines
arch/x86/kernel/cpu/sgx/main.c +2
-2-if (sgx_should_reclaim (SGX_NR_LOW_PAGES )) -sgx_reclaim_pages (); +if (sgx_should_reclaim_global (SGX_NR_LOW_PAGES )) +sgx_reclaim_pages_global (); }I wish the rename was mentioned in the changelog too.
OK
Re: [PATCH v15 08/14] x86/sgx: Add basic EPC reclamation flow for cgroup
Quoted 38 lines
Quoted 28 lines
Quoted 7 lines
In other cases, the caller may invoke this function in a loop to ensure enough pages reclaimed for its usage. To ensure all descendant groups scanned in a round-robin fashion in those cases,
sgx_cgroup_reclaim_pages()takes in a starting cgroup and returns the next cgroup that the caller can pass in as the new starting cgroup for a subsequent call.AFAICT this part is new, and I believe this "round-robin" thing is just for the "global reclaim"? Or is it also for per-cgroup reclaim where more
thanSGX_NR_TO_SCANpages needs to be reclaimed?I wish the changelog should just point out what consumers will use this new
sgx_cgroup_reclaim_pages(), like:The
sgx_cgroup_reclaim_pages()will be used in three cases:1) direct/sync per-cgroup reclaim in
try_charge()
2) indirect/async per-cgroup reclaim triggered intry_charge()
3) global reclaimAnd then describe how will they use
sgx_cgroup_reclaim_pages():Both 1) and 2) can result in needing to reclaim more than
SGX_NR_TO_SCANpages, in which case we should <fill in how to reclaim>.Quoted 7 lines
For 3), the new global reclaim should try tot match the existing global reclaim behaviour, that is to try to treat all EPC pages equally. <continue to explain how can
sgx_cgroup_reclaim_pages()achieve this.>With above context, we can justify why to make
sgx_cgroup_reclaim_pages()in this form.This new part is only to address the issue you raised in this thread:
https://lore.kernel.org/lkml/[email protected]/
Quoted 7 lines
Really it has nothing to do whether global, direct/async, per-cgroup contexts. They all should use the function the same way. This paragraph describes the design and
I thought the above new statements justify the reason we return 'next' so it can reclaim into descedant in round-robin fashion? No sure we need get into details of different usages of the functions which are in code actually?
Please clearly define the behaviour of "per-cgroup reclaim" first.
I can understand "global reclaim" means we essentially want to treats all EPC pages equally. But it's not obvious to me what is the desired behaviour of "per-cgroup reclaim", especially when the behaviour is different between this version and the previous versions (see below).
Quoted 1 line
[...]
Quoted 18 lines
Quoted 2 lines
And when there are more than
SGX_NR_TO_SCANpages that need to reclaim, the above ...Note, all
sgx_cgroup_reclaim_pages()guarantees is scanningSGX_NR_TO_SCANpages.Quoted 5 lines
for (;;) { cg_next = sgx_cgroup_reclaim_pages(sgx_cg->cg, cg_next); }Quoted 7 lines
... actually tries to reclaim those pages from @sgx_cg _AND_ it's descendants, and tries to do it _EQUALLY_.
Is this desired, or should we always try to reclaim from the @sgx_cg first, but only moves to the desendants when the @sgx_cg shouldn't be reclaimed anymore?
Quoted 4 lines
we still reclaim in
sgx_cgin first scan and attempt of reclaiming forSGX_NR_TOS_CANpages, but if it turns out that did not satisfy caller needs, then caller goes on to reclaim from descendants by passing in 'next' as starting point.
But why?
Quoted 5 lines
Quoted 3 lines
Anyway, it's different from the previous behaviour.
Again, this is to fix the issue you raised. I consider it improved behavior :-)
Please clearly define the _EXPECTED_ hebaviour of "per-cgroup reclaim" first.
Quoted 1 line
We have two choices:
1) Always try to reclaim desired number of pages from the given cgroup, but only moves to reclaim from descendants when there's less than SGX_NR_TO_SCAN pages left;
2) Always try to reclaim desired number of pages _EQUALLY_ from the given cgroup _AND_ its descendants (in granularity of reclaiming SGX_NR_TO_SCAN pages each time).
The 1) was the old behavour in the previous versions, 2) is the new behaviour in this version.
I am not against any option, but the patch needs to be clear on which option to choose and why it is desired/better.
PATCH v15 9/14 x86/sgx: Abstract check for global reclaimable pages
In reply to Huang, HaitaoFrom: Kristen Carlson Accardi <[email protected]>
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().
---
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 | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +10 -2
@@ -37,6 +37,14 @@ static inline struct sgx_epc_lru_list *sgx_epc_page_lru(struct sgx_epc_page *epcreturn &sgx_global_lru; } +/* +* 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 =ATOMIC_LONG_INIT (0 );/* Nodes with one or more EPC sections. */
@@ -390,7 +398,7 @@ unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list *lru)static bool sgx_should_reclaim_global (unsigned long watermark) {return atomic_long_read (&sgx_nr_free_pages) < watermark && - !list_empty (&sgx_global_lru.reclaimable); +sgx_can_reclaim_global (); }static void sgx_reclaim_pages_global (void )
@@ -596,7 +604,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim reclaim)break ; } -if (list_empty (&sgx_global_lru.reclaimable)) { +if (!sgx_can_reclaim_global ()) { page =ERR_PTR (-ENOMEM );break ; }
Signature · 1 line
2.43.0
PATCH v15 10/14 x86/sgx: Implement async reclamation for cgroup
In reply to Huang, HaitaoFrom: Kristen Carlson Accardi <[email protected]>
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().
---
V15:
- Disable SGX when sgx_cgroup_init() fails instead of BUG_ON() (Jarkko)
- Reset capacity to zero when sgx_cgroup_init() fails. (Kai) V13:
- Revert to BUG_ON() in case of workq allocation failure in init and only alloc if misc is enabled.
V11:
- Print error instead of WARN (Kai)
- Add check for need to queue an async reclamation before returning from try_charge(), do so if needed. This is to be consistent with global reclaimer to minimize thrashing during allocation time.
V10:
- Split asynchronous flow in separate patch. (Kai)
- Consider cgroup disabled when the workqueue allocation fail during init. (Kai)
- Abstract out sgx_cgroup_should_reclaim().
V9:
- Add comments for static variables. (Jarkko)
V8:
- Remove alignment for substructure variables. (Jarkko)
V7:
- Split this out from the big patch, #10 in V6. (Dave, Kai)
--- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 135 ++++++++++++++++++++++++++- arch/x86/kernel/cpu/sgx/epc_cgroup.h | 1 + 2 files changed, 135 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/epc_cgroup.c +134 -1
@@ -4,9 +4,63 @@#include <linux/slab.h> #include "epc_cgroup.h" +/* +* 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 minimal +* free pages would barely leave any page for use, causing excessive reclamation +* and thrashing. +* +* Define the following limit, below which cgroup does not maintain the minimal +* free page threshold. Set this to quadruple of the minimal so at least 75% +* 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; +/* +* 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 = sgx_cg->cg; +u64 m =U64_MAX ; + +while (i) { + m =min (m,READ_ONCE (i->res[MISC_CG_RES_SGX_EPC ].max)); + i =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
@@ -113,6 +167,65 @@ static struct misc_cg *sgx_cgroup_reclaim_pages(struct misc_cg *root, struct misreturn css_misc (next); } +/** +* 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 make +* 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 within 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 =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 -=SGX_CG_MIN_FREE_PAGE ; + + cur =sgx_cgroup_page_counter_read (sgx_cg); + +return (cur >= 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 = container_of(work, struct sgx_cgroup, reclaim_work); +struct misc_cg *cg_next =NULL ; + +/* +* 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)) { + cg_next =sgx_cgroup_reclaim_pages (sgx_cg->cg, cg_next); +cond_resched (); + } + +if (cg_next != sgx_cg->cg) +put_misc_cg (cg_next); +} +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 ))
@@ -148,7 +261,8 @@ int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim reclaim) goto out;if (reclaim ==SGX_NO_RECLAIM ) { - ret = -ENOMEM ; +queue_work (sgx_cg_wq, &sgx_cg->reclaim_work); + ret = -EBUSY ; goto out; }
@@ -156,6 +270,9 @@ int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim reclaim)cond_resched (); } +if (sgx_cgroup_should_reclaim (sgx_cg)) +queue_work (sgx_cg_wq, &sgx_cg->reclaim_work); + out:if (cg_next != sgx_cg->cg)put_misc_cg (cg_next);
@@ -179,12 +296,14 @@ static void sgx_cgroup_free(struct misc_cg *cg)if (!sgx_cg)return ; +cancel_work_sync (&sgx_cg->reclaim_work);kfree (sgx_cg); }static void sgx_cgroup_misc_init (struct misc_cg *cg,struct sgx_cgroup *sgx_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 = sgx_cg; sgx_cg->cg = cg; }
@@ -209,8 +328,22 @@ const struct misc_res_ops sgx_cgroup_ops = {int __init 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 =alloc_workqueue ("sgx_cg_wq" ,WQ_UNBOUND |WQ_FREEZABLE , +WQ_UNBOUND_MAX_ACTIVE ); +return -ENOMEM ; + } +return 0 ; }
arch/x86/kernel/cpu/sgx/epc_cgroup.h +1 -0
@@ -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; };static inline struct sgx_cgroup *sgx_cgroup_from_misc_cg (struct misc_cg *cg)
Signature · 1 line
2.43.0
Re: [PATCH v15 10/14] x86/sgx: Implement async reclamation for cgroup
arch/x86/kernel/cpu/sgx/epc_cgroup.c const struct misc_res_ops sgx_cgroup_ops = { · line 345
Quoted 5 lines
arch/x86/kernel/cpu/sgx/epc_cgroup.c +4
-0+if (cgroup_subsys_enabled (misc_cgrp_subsys)) { + sgx_cg_wq =alloc_workqueue ("sgx_cg_wq" ,WQ_UNBOUND |WQ_FREEZABLE , +WQ_UNBOUND_MAX_ACTIVE ); +return -ENOMEM ;
Argh, missing if(!sgx_cg_wq), sorry for the brain fart. Tests pass only because I made another silly mistake in checking return of this function. :-(
Haitao
PATCH v15 11/14 x86/sgx: Charge mem_cgroup for per-cgroup reclamation
In reply to Huang, HaitaoFrom: Haitao Huang <[email protected]>
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.
---
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 | 11 +++++++---- arch/x86/kernel/cpu/sgx/main.c | 29 +++++++++++++--------------- arch/x86/kernel/cpu/sgx/sgx.h | 2 +- 5 files changed, 37 insertions(+), 37 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +15 -14
@@ -993,23 +993,23 @@ static int __sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_inde }/* -* 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 enclave. +* +* 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 =NULL ;struct sgx_encl_mm *encl_mm;int idx; -/* -* 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);/* * 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(struct 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(struct 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 =sgx_encl_get_mem_cgroup (encl); +struct mem_cgroup *encl_memcg =sgx_encl_get_mem_cgroup (encl, charge_mm);struct mem_cgroup *memcg =set_active_memcg (encl_memcg);int ret;
arch/x86/kernel/cpu/sgx/encl.h +1 -2
@@ -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); -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);
arch/x86/kernel/cpu/sgx/epc_cgroup.c +7 -4
@@ -107,6 +107,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. * @start: The descendant cgroup from which to start the tree walking. +* @charge_mm: The mm to charge for backing store allocation. * * This function performs a pre-order walk in the cgroup tree under the given * root, starting from the node %start, or from the root if %start is NULL. The
@@ -126,7 +127,8 @@ static bool sgx_cgroup_lru_empty(struct misc_cg *root)* release the reference if the returned is not used as %start for a subsequent * call. */ -static struct misc_cg *sgx_cgroup_reclaim_pages (struct misc_cg *root,struct misc_cg *start) +static struct misc_cg *sgx_cgroup_reclaim_pages (struct misc_cg *root,struct misc_cg *start, +struct mm_struct *charge_mm) {struct cgroup_subsys_state *css_root, *pos;struct cgroup_subsys_state *next =NULL ;
@@ -142,7 +144,7 @@ static struct misc_cg *sgx_cgroup_reclaim_pages(struct misc_cg *root, struct miswhile (cnt <SGX_NR_TO_SCAN ) { sgx_cg =sgx_cgroup_from_misc_cg (css_misc (pos)); - cnt +=sgx_reclaim_pages (&sgx_cg->lru); + cnt +=sgx_reclaim_pages (&sgx_cg->lru, charge_mm);rcu_read_lock ();
@@ -218,7 +220,8 @@ static void sgx_cgroup_reclaim_work_func(struct work_struct *work)* blocked until a worker makes its way through the global work queue. */ while (sgx_cgroup_should_reclaim (sgx_cg)) { - cg_next =sgx_cgroup_reclaim_pages (sgx_cg->cg, cg_next); +/* Indirect reclaim, no mm to charge, so NULL: */ + cg_next =sgx_cgroup_reclaim_pages (sgx_cg->cg, cg_next,NULL );cond_resched (); }
@@ -266,7 +269,7 @@ int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim reclaim) goto out; } - cg_next =sgx_cgroup_reclaim_pages (sgx_cg->cg, cg_next); + cg_next =sgx_cgroup_reclaim_pages (sgx_cg->cg, cg_next, current->mm);cond_resched (); }
arch/x86/kernel/cpu/sgx/main.c +13 -16
@@ -261,8 +261,8 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, } } -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 = epc_page->owner;struct sgx_encl *encl = encl_page->encl;
@@ -278,7 +278,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page,if (!encl->secs_child_cnt &&test_bit (SGX_ENCL_INITIALIZED , &encl->flags)) { ret =sgx_encl_alloc_backing (encl,PFN_DOWN (encl->size), - &secs_backing); + &secs_backing, charge_mm);if (ret) goto out;
@@ -297,6 +297,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page,/** * sgx_reclaim_pages() - Attempt to reclaim a fixed number of pages from an 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 them to * the enclave's private shmem files. Skip the pages, which have been accessed
@@ -312,7 +313,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_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_struct *charge_mm) {struct sgx_epc_page *chunk[SGX_NR_TO_SCAN ];struct sgx_backing backing[SGX_NR_TO_SCAN ];
@@ -352,7 +353,7 @@ unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list *lru) page_index =PFN_DOWN (encl_page->desc - encl_page->encl->base);mutex_lock (&encl_page->encl->lock); - ret =sgx_encl_alloc_backing (encl_page->encl, page_index, &backing[i]); + ret =sgx_encl_alloc_backing (encl_page->encl, page_index, &backing[i], charge_mm);if (ret) {mutex_unlock (&encl_page->encl->lock); goto skip;
@@ -384,7 +385,7 @@ unsigned int sgx_reclaim_pages(struct sgx_epc_lru_list *lru)continue ; encl_page = epc_page->owner; -sgx_reclaimer_write (epc_page, &backing[i]); +sgx_reclaimer_write (epc_page, &backing[i], charge_mm);kref_put (&encl_page->encl->refcount, sgx_encl_release); epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED ;
@@ -401,9 +402,9 @@ static bool sgx_should_reclaim_global(unsigned long watermark)sgx_can_reclaim_global (); } -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); }/*
@@ -414,7 +415,7 @@ static void sgx_reclaim_pages_global(void)void sgx_reclaim_direct (void ) {if (sgx_should_reclaim_global (SGX_NR_LOW_PAGES )) -sgx_reclaim_pages_global (); +sgx_reclaim_pages_global (current->mm); }static int ksgxd (void *p)
@@ -437,7 +438,8 @@ static int ksgxd(void *p) sgx_should_reclaim_global(SGX_NR_HIGH_PAGES));if (sgx_should_reclaim_global (SGX_NR_HIGH_PAGES )) -sgx_reclaim_pages_global (); +/* Indirect reclaim, no mm to charge, so NULL: */ +sgx_reclaim_pages_global (NULL );cond_resched (); }
@@ -460,11 +462,6 @@ static bool __init sgx_page_reclaimer_init(void)return true; } -bool current_is_ksgxd (void ) -{ -return current == ksgxd_tsk; -} -static struct sgx_epc_page *__sgx_alloc_epc_page_from_node (int nid) {struct sgx_numa_node *node = &sgx_numa_nodes[nid];
@@ -619,7 +616,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim reclaim)break ; } -sgx_reclaim_pages_global (); +sgx_reclaim_pages_global (current->mm);cond_resched (); }
arch/x86/kernel/cpu/sgx/sgx.h +1 -1
@@ -136,7 +136,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 reclaim); -unsigned int sgx_reclaim_pages (struct sgx_epc_lru_list *lru); +unsigned int sgx_reclaim_pages (struct sgx_epc_lru_list *lru,struct mm_struct *charge_mm);void sgx_ipi_cb (void *info);
Signature · 1 line
2.43.0
PATCH v15 12/14 x86/sgx: Turn on per-cgroup EPC reclamation
In reply to Huang, HaitaoFrom: Kristen Carlson Accardi <[email protected]>
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. Similar to sgx_cgroup_reclaim_pages(), return the next cgroup so callers can use it as the new starting node for next round of reclamation if needed.
Also update sgx_can_reclaim_global(), to check emptiness of LRUs of all cgroups when EPC cgroup is enabled, otherwise only check the global LRU.
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.
With these changes, the global reclamation and per-cgroup reclamation both work properly with all pages tracked in per-cgroup LRUs.
---
V14:
- Update global reclamation to use the new sgx_cgroup_reclaim_pages() to iterate cgroups at lower level if the top cgroups are too busy.
V13:
- Use IS_ENABLED(CONFIG_CGROUP_MISC) in sgx_can_reclaim_global(). (Kai)
V12:
- Remove CONFIG_CGROUP_SGX_EPC, conditional compile SGX Cgroup for CONFIGCONFIG_CGROUPMISC. (Jarkko)
V11:
- Reword the comments for global reclamation for allocation failure after passing cgroup charging. (Kai)
- Add stub functions to remove ifdefs in c file (Kai)
- Add more detailed comments to clarify each page belongs to one cgroup, or the root. (Kai)
V10:
- Add comment to clarify each page belongs to one cgroup, or the root by default. (Kai)
- Merge the changes that expose sgx_cgroup_* functions to this patch.
- Add changes for sgx_reclaim_direct() that was missed previously.
V7:
- Split this out from the big patch, #10 in V6. (Dave, Kai)
--- arch/x86/kernel/cpu/sgx/epc_cgroup.c | 8 +-- arch/x86/kernel/cpu/sgx/epc_cgroup.h | 32 +++++++++++ arch/x86/kernel/cpu/sgx/main.c | 80 +++++++++++++++++++++++++--- 3 files changed, 109 insertions(+), 11 deletions(-)
arch/x86/kernel/cpu/sgx/epc_cgroup.c +4 -4
@@ -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 lists. */ -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;
@@ -127,8 +127,8 @@ static bool sgx_cgroup_lru_empty(struct misc_cg *root)* release the reference if the returned is not used as %start for a subsequent * call. */ -static struct misc_cg *sgx_cgroup_reclaim_pages (struct misc_cg *root,struct misc_cg *start, -struct mm_struct *charge_mm) +struct misc_cg *sgx_cgroup_reclaim_pages (struct misc_cg *root,struct misc_cg *start, +struct mm_struct *charge_mm) {struct cgroup_subsys_state *css_root, *pos;struct cgroup_subsys_state *next =NULL ;
@@ -181,7 +181,7 @@ static struct misc_cg *sgx_cgroup_reclaim_pages(struct misc_cg *root, struct mis* threshold (%SGX_CG_MIN_FREE_PAGE) and there are reclaimable pages within 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;
arch/x86/kernel/cpu/sgx/epc_cgroup.h +32 -0
@@ -13,6 +13,11 @@#define MISC_CG_RES_SGX_EPC MISC_CG_RES_TYPESstruct sgx_cgroup ; +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,25 @@ static inline int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclstatic inline void sgx_cgroup_uncharge (struct sgx_cgroup *sgx_cg) { } +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 ) { } +static inline struct misc_cg *sgx_cgroup_reclaim_pages (struct misc_cg *root, +struct misc_cg *next_cg, +struct mm_struct *charge_mm) +{ +return NULL ; +} +#else /* CONFIG_CGROUP_MISC */ struct sgx_cgroup {
@@ -37,6 +59,11 @@ struct sgx_cgroup {struct work_struct reclaim_work; }; +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 *cg) {return (struct sgx_cgroup *)(cg->res[MISC_CG_RES_SGX_EPC ].priv);
@@ -67,6 +94,11 @@ static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg)int sgx_cgroup_try_charge (struct sgx_cgroup *sgx_cg,enum sgx_reclaim reclaim);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); +struct misc_cg *sgx_cgroup_reclaim_pages (struct misc_cg *root, +struct misc_cg *next_cg, +struct mm_struct *charge_mm);int __init sgx_cgroup_init (void );#endif /* CONFIG_CGROUP_MISC */
arch/x86/kernel/cpu/sgx/main.c +73 -7
137 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v15 12/14] x86/sgx: Turn on per-cgroup EPC reclamation
Quoted 23 lines · Haitao
From: Kristen Carlson Accardi <[email protected]>
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. Similar tosgx_cgroup_reclaim_pages(), return the next cgroup so callers can use it as the new starting node for next round of reclamation if needed.
Quoted 5 lines
Also update
sgx_can_reclaim_global(), to check emptiness of LRUs of all cgroups when EPC cgroup is enabled, otherwise only check the global LRU.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.
Reading above, it's not clear how the _new_ global reclaim works with multiple LRUs.
E.g., the current global reclaim essentially treats all EPC pages equally when scanning those pages. From the above, I don't see how this is achieved in the new global reclaim.
The changelog should:
1) describe the how does existing global reclaim work, and then describe how to achieve the same beahviour in the new global reclaim which works with multiple LRUs;
2) If there's any behaviour difference between the "existing" vs the "new" global reclaim, the changelog should point out the difference, and explain why such difference is OK.
Quoted 4 lines
With these changes, the global reclamation and per-cgroup reclamation both work properly with all pages tracked in per-cgroup LRUs.
[...]
Quoted 14 lines
arch/x86/kernel/cpu/sgx/main.c +6
-1-static void sgx_reclaim_pages_global (struct mm_struct *charge_mm) +static struct misc_cg *sgx_reclaim_pages_global (struct misc_cg *next_cg, +struct mm_struct *charge_mm) { +if (IS_ENABLED (CONFIG_CGROUP_MISC )) +return sgx_cgroup_reclaim_pages (misc_cg_root (), next_cg, charge_mm); +sgx_reclaim_pages (&sgx_global_lru, charge_mm); +return NULL ; }/*
arch/x86/kernel/cpu/sgx/main.c static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) · line 447
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0@@ -414,12 +443,35 @@ static void sgx_reclaim_pages_global(struct mm_struct *charge_mm)*/ void sgx_reclaim_direct (void ) { +struct sgx_cgroup *sgx_cg =sgx_get_current_cg (); +struct misc_cg *cg =misc_from_sgx (sgx_cg);
From below @sgx_cg could be NULL. It's not immediately clear whether calling misc_from_sgx(sgx_cg) unconditionally is safe here.
Leave the initiaization of @cg to a later phase where @sgx_cg is guaranteed not being NULL, or initialize @cg to NULL here and update later.
arch/x86/kernel/cpu/sgx/main.c static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) · line 456
Quoted 9 lines
arch/x86/kernel/cpu/sgx/main.c +9
-0+struct misc_cg *next_cg =NULL ; + +/* +* Make sure there are some free pages at both cgroup and global levels. +* In both cases, only make one attempt of reclamation to avoid lengthy +* block on the caller. +*/ +if (sgx_cg &&sgx_cgroup_should_reclaim (sgx_cg)) + next_cg =sgx_cgroup_reclaim_pages (cg, next_cg, current->mm);
I don't quite follow the logic.
First of all, sgx_cgroup_reclaim_pages() isn't called in a loop, so why not just do:
next_cg = sgx_cgroup_reclaim_pages(cg, NULL, current->mm);
And what is the point of set @next_cg here, since ...
arch/x86/kernel/cpu/sgx/main.c static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) · line 461
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +5
-0+ +if (next_cg != cg) +put_misc_cg (next_cg); + + next_cg =NULL ;
... here @next_cg is reset to NULL ?
Looks the only reason is you need to do ...
put_misc_cg(next_cg);
... above?
This piece of code appears repeatedly in this file. Is there any way we can get rid of it?
arch/x86/kernel/cpu/sgx/main.c static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) · line 463
Quoted 3 lines
arch/x86/kernel/cpu/sgx/main.c +1
-1if (sgx_should_reclaim_global (SGX_NR_LOW_PAGES )) -sgx_reclaim_pages_global (current->mm); + next_cg =sgx_reclaim_pages_global (next_cg, current->mm);
And this doesn't seems "global reclaim" at all?
Because it essentially equals to:
next_cg = sgx_reclaim_pages_global(NULL, current->mm);
which always reclaims from the ROOT.
So each call to sgx_reclaim_direct() will always reclaim from the ROOT -- any other LRUs in the hierarchy will unlikely to get any chance to be reclaimed.
Quoted 15 lines
arch/x86/kernel/cpu/sgx/main.c +7
-0+ +if (next_cg !=misc_cg_root ()) +put_misc_cg (next_cg); + +sgx_put_cg (sgx_cg); }static int ksgxd (void *p) { +struct misc_cg *next_cg =NULL ; +set_freezable ();/*
arch/x86/kernel/cpu/sgx/main.c static int ksgxd(void *p) · line 495
Quoted 9 lines
arch/x86/kernel/cpu/sgx/main.c +3
-2@@ -437,11 +489,15 @@ static int ksgxd(void *p)kthread_should_stop () || sgx_should_reclaim_global(SGX_NR_HIGH_PAGES)); -if (sgx_should_reclaim_global (SGX_NR_HIGH_PAGES )) +while (!kthread_should_stop () &&sgx_should_reclaim_global (SGX_NR_HIGH_PAGES )) {/* Indirect reclaim, no mm to charge, so NULL: */ -sgx_reclaim_pages_global (NULL ); + next_cg =sgx_reclaim_pages_global (next_cg,NULL ); +cond_resched ();
Should the 'put_misc_cg(next_cg)' be done within the while() loop but not below?
arch/x86/kernel/cpu/sgx/main.c static int ksgxd(void *p) · line 500
Quoted 6 lines
arch/x86/kernel/cpu/sgx/main.c +4
-1+ } -cond_resched (); +if (next_cg !=misc_cg_root ()) +put_misc_cg (next_cg); + next_cg =NULL ;
Again, it doesn't seems "global reclaim" here, since you always restart from the ROOT once the target pages have been reclaimed.
AFAICT it's completely different from the existing global reclaim.
Quoted 15 lines
arch/x86/kernel/cpu/sgx/main.c +1
-0}return 0 ;@@ -583,6 +639,7 @@ int sgx_unmark_page_reclaimable(struct sgx_epc_page *page)*/ struct sgx_epc_page *sgx_alloc_epc_page (void *owner,enum sgx_reclaim reclaim) { +struct misc_cg *next_cg =NULL ;struct sgx_cgroup *sgx_cg;struct sgx_epc_page *page;int ret;@@ -616,10 +673,19 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim reclaim)break ; }
arch/x86/kernel/cpu/sgx/main.c struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim reclaim) · line 684
Quoted 10 lines
arch/x86/kernel/cpu/sgx/main.c +7
-1-sgx_reclaim_pages_global (current->mm); +/* +* 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. +*/ + next_cg =sgx_reclaim_pages_global (next_cg, current->mm);cond_resched (); }
Ditto IIUC.
Re: [PATCH v15 12/14] x86/sgx: Turn on per-cgroup EPC reclamation
Hi Kai
Quoted 40 lines · Kai
Quoted 23 lines · Haitao
From: Kristen Carlson Accardi <[email protected]>
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. Similar tosgx_cgroup_reclaim_pages(), return the next cgroup so callers can use it as the new starting node for next round of reclamation if needed.Quoted 5 lines
Also update
sgx_can_reclaim_global(), to check emptiness of LRUs of all cgroups when EPC cgroup is enabled, otherwise only check the global LRU.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.Reading above, it's not clear how the _new_ global reclaim works with multiple LRUs.
E.g., the current global reclaim essentially treats all EPC pages equally when scanning those pages. From the above, I don't see how this is achieved in the new global reclaim.
The changelog should:
Quoted 7 lines
1) describe the how does existing global reclaim work, and then describe how to achieve the same beahviour in the new global reclaim which works with multiple LRUs;
2) If there's any behaviour difference between the "existing" vs the "new" global reclaim, the changelog should point out the difference, and explain why such difference is OK.
Sure I can explain. here is what I plan to add for v16:
Note the original global reclaimer has
only one LRU and always scans and reclaims from the head of this global LRU. The new global reclaimer always starts the scanning from the root node, only moves down to its descendants if more reclamation is needed or the root node does not have SGX_NR_TO_SCAN (16) pages in the LRU. This makes the enclave pages in the root node more likely being reclaimed if they are not frequently used (not 'young'). Unless we track pages in one LRU again, we can not really match exactly the same behavior of the original global reclaimer. And one-LRU approach would make per-cgroup reclamation scanning and reclaiming too complex. On the other hand, this design is acceptable for following reasons:
1) For all purposes of using cgroups, enclaves will need live in non-root (leaf for cgroup v2) nodes where limits can be enforced per-cgroup.
2) Global reclamation now only happens in situation mentioned above when a lower level cgroup not at its limit can't allocate due to over commit at global level.
3) The pages in root being slightly penalized are not busily used anyway.
4) In cases that multiple rounds of reclamation is needed, the caller of sgx_reclaim_page_global() can still recall for reclaiming in 'next' descendant in round robin way, each round scans for SGX_NR_SCAN pages from the head of 'next' cgroup's LRU.
Quoted 33 lines
Quoted 5 lines
With these changes, the global reclamation and per-cgroup reclamation both work properly with all pages tracked in per-cgroup LRUs.
[...]
Quoted 15 lines
(quoted diff) +1
-1-static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) +static struct misc_cg *sgx_reclaim_pages_global(struct misc_cg
*next_cg,arch/x86/kernel/cpu/sgx/main.c +5
-0+ struct mm_struct *charge_mm) { +if (IS_ENABLED (CONFIG_CGROUP_MISC )) +return sgx_cgroup_reclaim_pages (misc_cg_root (), next_cg, charge_mm); +sgx_reclaim_pages (&sgx_global_lru, charge_mm); +return NULL ; }/* arch/x86/kernel/cpu/sgx/main.c static void sgx_reclaim_pages_global(struct · line 448
Quoted 6 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0@@ -414,12 +443,35 @@ static void sgx_reclaim_pages_global(structmm_struct *charge_mm) */ void sgx_reclaim_direct (void ) { +struct sgx_cgroup *sgx_cg =sgx_get_current_cg (); +struct misc_cg *cg =misc_from_sgx (sgx_cg);From below @sgx_cg could be NULL. It's not immediately clear whether calling
misc_from_sgx(sgx_cg)unconditionally is safe here.
Quoted 3 lines
Leave the initiaization of @cg to a later phase where @sgx_cg is guaranteed not being NULL, or initialize @cg to NULL here and update later.
Ok
Quoted 14 lines
arch/x86/kernel/cpu/sgx/main.c static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) · line 456
Quoted 11 lines
arch/x86/kernel/cpu/sgx/main.c +4
-0+struct misc_cg *next_cg =NULL ; + +/* +* Make sure there are some free pages at both cgroup and global levels.
(quoted diff) +1
-0+ * In both cases, only make one attempt of reclamation to avoidlengthy
arch/x86/kernel/cpu/sgx/main.c +4
-0+* block on the caller. +*/ +if (sgx_cg &&sgx_cgroup_should_reclaim (sgx_cg)) + next_cg =sgx_cgroup_reclaim_pages (cg, next_cg, current->mm);I don't quite follow the logic.
Quoted 5 lines
First of all,
sgx_cgroup_reclaim_pages()isn't called in a loop, so why not just do:next_cg = sgx_cgroup_reclaim_pages(cg, NULL, current->mm);
Okay, I can replace it with NULL. I thought it was convenient to grep all such calls to see if they are used consistently.
Quoted 11 lines
And what is the point of set @next_cg here, since ...
arch/x86/kernel/cpu/sgx/main.c static void sgx_reclaim_pages_global(struct mm_struct *charge_mm) · line 461
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +5
-0+ +if (next_cg != cg) +put_misc_cg (next_cg); + + next_cg =NULL ;... here @next_cg is reset to NULL ?
Quoted 6 lines
Looks the only reason is you need to do ...
put_misc_cg(next_cg);... above?
yes
Quoted 3 lines
This piece of code appears repeatedly in this file. Is there any way we can get rid of it?
sgx_cgroup_reclaim_pages() returns 'next' in case caller needs to run it in loop. In this case, no.
I could create a thin wrapper for this case, but since there is only one such case, I did not fee it's worth the extra layer.
Quoted 8 lines
Review
Quoted 3 lines
if (
sgx_should_reclaim_global(SGX_NR_LOW_PAGES))(quoted diff) +1
-1- sgx_reclaim_pages_global(current->mm); + next_cg = sgx_reclaim_pages_global(next_cg, current->mm);And this doesn't seems "global reclaim" at all?
Because it essentially equals to:
Quoted 7 lines
next_cg = sgx_reclaim_pages_global(NULL, current->mm);which always reclaims from the ROOT.
So each call to
sgx_reclaim_direct()will always reclaim from the ROOT -- any other LRUs in the hierarchy will unlikely to get any chance to be reclaimed.
No, the current cgroup is always checked and called to reclaim if needed. Global reclaim only happens when system-wide water mark SGX_NR_LOW_PAGES is violated.
Quoted 19 lines
Quoted 19 lines
arch/x86/kernel/cpu/sgx/main.c +7
-0+ +if (next_cg !=misc_cg_root ()) +put_misc_cg (next_cg); + +sgx_put_cg (sgx_cg); }static int ksgxd (void *p) { +struct misc_cg *next_cg =NULL ; +set_freezable ();/* @@ -437,11 +489,15 @@ static int ksgxd(void *p)kthread_should_stop () || sgx_should_reclaim_global(SGX_NR_HIGH_PAGES));
Quoted 10 lines
arch/x86/kernel/cpu/sgx/main.c static int ksgxd(void *p) · line 495
Quoted 7 lines
arch/x86/kernel/cpu/sgx/main.c +3
-2-if (sgx_should_reclaim_global (SGX_NR_HIGH_PAGES )) +while (!kthread_should_stop () &&sgx_should_reclaim_global (SGX_NR_HIGH_PAGES )) {/* Indirect reclaim, no mm to charge, so NULL: */ -sgx_reclaim_pages_global (NULL ); + next_cg =sgx_reclaim_pages_global (next_cg,NULL ); +cond_resched ();Should the '
put_misc_cg(next_cg)' be done within thewhile()loop but not below?
No, whenever there is a loop, we want to reclaim in round robin way.
Quoted 10 lines
arch/x86/kernel/cpu/sgx/main.c static int ksgxd(void *p) · line 500
Quoted 6 lines
arch/x86/kernel/cpu/sgx/main.c +4
-1+ } -cond_resched (); +if (next_cg !=misc_cg_root ()) +put_misc_cg (next_cg); + next_cg =NULL ;Again, it doesn't seems "global reclaim" here, since you always restart from the ROOT once the target pages have been reclaimed.
Quoted 2 lines
AFAICT it's completely different from the existing global reclaim.
Hopefully adding comments mentioned above clarifies this.
Quoted 32 lines
Quoted 18 lines
(quoted diff) +0
-0}return 0;
arch/x86/kernel/cpu/sgx/main.c +1
-0@@ -583,6 +639,7 @@ int sgx_unmark_page_reclaimable(struct sgx_epc_page*page) */ struct sgx_epc_page *sgx_alloc_epc_page (void *owner,enum sgx_reclaim reclaim) { +struct misc_cg *next_cg =NULL ;struct sgx_cgroup *sgx_cg;struct
sgx_epc_page*page;
int ret;arch/x86/kernel/cpu/sgx/main.c +0
-0@@ -616,10 +673,19 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, enum sgx_reclaim reclaim) break; }arch/x86/kernel/cpu/sgx/main.c struct sgx_epc_page *sgx_alloc_epc_page(void · line 685
Quoted 10 lines
arch/x86/kernel/cpu/sgx/main.c +7
-1-sgx_reclaim_pages_global (current->mm); +/* +* 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. +*/ + next_cg =sgx_reclaim_pages_global (next_cg, current->mm);cond_resched (); }Ditto IIUC.
Re: [PATCH v15 12/14] x86/sgx: Turn on per-cgroup EPC reclamation
Quoted 68 lines · Haitao Huang
Hi Kai
Quoted 44 lines · Kai
Quoted 23 lines · Haitao
From: Kristen Carlson Accardi <[email protected]>
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. Similar tosgx_cgroup_reclaim_pages(), return the next cgroup so callers can use it as the new starting node for next round of reclamation if needed.Quoted 5 lines
Also update
sgx_can_reclaim_global(), to check emptiness of LRUs of all cgroups when EPC cgroup is enabled, otherwise only check the global LRU.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.Reading above, it's not clear how the _new_ global reclaim works with multiple LRUs.
E.g., the current global reclaim essentially treats all EPC pages equally when scanning those pages. From the above, I don't see how this is achieved in the new global reclaim.
The changelog should:
1) describe the how does existing global reclaim work, and then describe how to achieve the same beahviour in the new global reclaim which works with multiple LRUs;
Quoted 5 lines
2) If there's any behaviour difference between the "existing" vs the "new"
global reclaim, the changelog should point out the difference, and explain
why such difference is OK.Sure I can explain. here is what I plan to add for v16:
Note the original global reclaimer has
only one LRU and always scans and reclaims from the head of this global LRU. The new global reclaimer always starts the scanning from the root node, only moves down to its descendants if more reclamation is needed or the root node does not haveSGX_NR_TO_SCAN(16) pages in the LRU. This makes the enclave pages in the root node more likely being reclaimed if they are not frequently used (not 'young'). Unless we track pages in one LRU again, we can not really match exactly the same behavior of the original global reclaimer. And one-LRU approach would make per-cgroup reclamation scanning and reclaiming too complex. On the other hand, this design is acceptable for following reasons:
Quoted 3 lines
1) For all purposes of using cgroups, enclaves will need live in non-root (leaf for cgroup v2) nodes where limits can be enforced per-cgroup.
I don't see how it matters. If ROOT is empty, then it moves to the first descendant.
Quoted 3 lines
2) Global reclamation now only happens in situation mentioned above when a lower level cgroup not at its limit can't allocate due to over commit at global level.
Really? In sgx_reclaim_direct() the code says:
/*
* Make sure there are some free pages at both cgroup and global levels.
* In both cases, only make one attempt of reclamation to avoid lengthy
* block on the caller.
*/
Yeah only one attempt will be made for global level but it is still global reclaim.
Quoted 2 lines
3) The pages in root being slightly penalized are not busily used anyway.
The 1) says in practice the root will have no enclaves, thus no EPC at all.
In other words, in practice the global reclaim will always skip the root and move to the first descendant.
Quoted 4 lines
4) In cases that multiple rounds of reclamation is needed, the caller of
sgx_reclaim_page_global()can still recall for reclaiming in 'next' descendant in round robin way, each round scans forSGX_NR_SCANpages from the head of 'next' cgroup's LRU.
"multiple rounds of reclamation" isn't clear enough. Does it mean multiple call of sgx_cgroup_reclaim_pages(), or does it mean each trigger of global reclaim?
The current patch seems to be the former. See the 'next_cg' is reset to NULL for each loop of the main loop in ksgxd().
This essentially means each trigger of global reclaim will start from the ROOT, or in practice the first descendant (based on 1) and 3) above) will always be the victim of each global reclaim.
I agree it's hard to _EXACTLY_ match the existing global reclaim, but IMHO we should at least treats all cgroups equally.
PATCH v15 13/14 Docs/x86/sgx: Add description for cgroup support
In reply to Huang, HaitaoFrom: Sean Christopherson <[email protected]>
Add initial documentation of how to regulate the distribution of SGX Enclave Page Cache (EPC) memory via the Miscellaneous cgroup controller.
---
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(+)
Documentation/arch/x86/sgx.rst +83 -0
@@ -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 +============== + +The "sgx_epc" resource within the Miscellaneous cgroup controller regulates +distribution of SGX EPC memory, which is a subset of system RAM that is used 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 subsystem and +is not accounted by the memory controller. Note that this is true only for EPC +memory itself, i.e. normal memory allocations related to SGX and EPC memory, +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 analogous +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 by 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 cgroup 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. The + 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 from 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 are 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 virtual + EPC driver returns SIGBUS to the user space process to indicate failure + on new EPC allocation requests. + + The misc.max limit is non-preemptive. If a user writes a limit lower + than the current usage to this file, the cgroup will not preemptively + deallocate pages currently in use, and will only start blocking the 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 boundary. + +Migration +--------- + +Once an EPC page is charged to a cgroup (during allocation), it remains charged +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.
Signature · 1 line
2.43.0
PATCH v15 14/14 selftests/sgx: Add scripts for EPC cgroup testing
In reply to Huang, HaitaoFrom: Haitao Huang <[email protected]>
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.
---
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
tools/testing/selftests/sgx/Makefile +2 -1
@@ -20,7 +20,8 @@ ENCL_LDFLAGS := -Wl,-T,test_encl.lds,--build-id=none ifeq ($(CAN_BUILD_X86_64), 1) TEST_CUSTOM_PROGS := $(OUTPUT)/test_sgx -TEST_FILES := $(OUTPUT)/test_encl.elf +TEST_FILES := $(OUTPUT)/test_encl.elf ash_cgexec.sh +TEST_PROGS := run_epc_cg_selftests.sh all: $(TEST_CUSTOM_PROGS) $(OUTPUT)/test_encl.elf endif
tools/testing/selftests/sgx/README new file +109 -0
109 lines not on this page · raw message
tools/testing/selftests/sgx/ash_cgexec.sh new file +16 -0
@@ -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 <v2 cgroup path> <command> [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 "$@" +
tools/testing/selftests/sgx/config new file +4 -0
@@ -0,0 +1,4 @@ +CONFIG_CGROUPS=y +CONFIG_CGROUP_MISC=y +CONFIG_MEMCG=y +CONFIG_X86_SGX=y
tools/testing/selftests/sgx/run_epc_cg_selftests.sh new file +295 -0
295 lines not on this page · raw message
tools/testing/selftests/sgx/settings new file +2 -0
@@ -0,0 +1,2 @@ +# This timeout may need be increased for platforms with EPC larger than 4G +timeout=140
tools/testing/selftests/sgx/watch_misc_for_tests.sh new file +11 -0
@@ -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 'events'" + exit 1 +fi + +watch -n 1 'find /sys/fs/cgroup -wholename "*/sgx_test*/misc.'$1'" -exec \ + sh -c '\''echo "$1:"; cat "$1"'\'' _ {} \;'
Signature · 1 line
2.43.0