From: Haitao Huang <haitao.huang@linux.intel.com>
To: jarkko@kernel.org, dave.hansen@linux.intel.com,
kai.huang@intel.com, tj@kernel.org, mkoutny@suse.com,
chenridong@huawei.com, linux-kernel@vger.kernel.org,
linux-sgx@vger.kernel.org, x86@kernel.org,
cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com,
bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com,
tim.c.chen@linux.intel.com
Cc: zhiquan1.li@intel.com, kristen@linux.intel.com,
seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com,
mikko.ylinen@linux.intel.com, yangjie@microsoft.com,
chrisyan@microsoft.com
Subject: [PATCH v17 12/16] x86/sgx: Implement direct reclamation for cgroups
Date: Fri, 30 Aug 2024 09:40:33 -0700 [thread overview]
Message-ID: <20240830164038.39343-13-haitao.huang@linux.intel.com> (raw)
In-Reply-To: <20240830164038.39343-1-haitao.huang@linux.intel.com>
sgx_reclaim_direct() was introduced to preemptively reclaim some pages
as the best effort to avoid on-demand reclamation that can stall forward
progress in some situations, e.g., allocating pages to load previously
reclaimed page to perform EDMM operations on [1].
Currently when the global usage is close to the capacity,
sgx_reclaim_direct() makes one invocation to sgx_reclaim_pages_global()
but does not guarantee there are free pages available for later
allocations to succeed. In other words, the only goal here is to reduce
the chance of on-demand reclamation at allocation time. In cases of
allocation failure, the caller, the EDMM ioctl()'s, would return -EAGAIN
to user space and let the user space to decide whether to retry or not.
With EPC cgroups enabled, usage of a cgroup can also reach its limit
(usually much lower than capacity) and trigger per-cgroup reclamation.
Implement a similar strategy to reduce the chance of on-demand
per-cgroup reclamation for this use case.
Create a wrapper, sgx_cgroup_reclaim_direct(), to perform a preemptive
reclamation at cgroup level, and have sgx_reclaim_direct() call it when
EPC cgroup is enabled.
[1] https://lore.kernel.org/all/a0d8f037c4a075d56bf79f432438412985f7ff7a.1652137848.git.reinette.chatre@intel.com/T/#u
Signed-off-by: Haitao Huang <haitao.huang@linux.intel.com>
Reviewed-by: Kai Huang <kai.huang@intel.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
---
V17:
- Improve comments and capitalization. (Kai)
---
arch/x86/kernel/cpu/sgx/epc_cgroup.c | 15 +++++++++++++++
arch/x86/kernel/cpu/sgx/epc_cgroup.h | 3 +++
arch/x86/kernel/cpu/sgx/main.c | 4 ++++
3 files changed, 22 insertions(+)
diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.c b/arch/x86/kernel/cpu/sgx/epc_cgroup.c
index 4faff943ce15..7394f78dec49 100644
--- a/arch/x86/kernel/cpu/sgx/epc_cgroup.c
+++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c
@@ -240,6 +240,21 @@ static bool sgx_cgroup_should_reclaim(struct sgx_cgroup *sgx_cg)
return (cur >= max);
}
+/**
+ * sgx_cgroup_reclaim_direct() - Preemptive reclamation.
+ *
+ * Scan and attempt to reclaim %SGX_NR_TO_SCAN as best effort to make later
+ * EPC allocation quicker.
+ */
+void sgx_cgroup_reclaim_direct(void)
+{
+ struct sgx_cgroup *sgx_cg = sgx_get_current_cg();
+
+ if (sgx_cgroup_should_reclaim(sgx_cg))
+ sgx_cgroup_reclaim_pages(sgx_cg, current->mm, SGX_NR_TO_SCAN);
+ sgx_put_cg(sgx_cg);
+}
+
/*
* Asynchronous work flow to reclaim pages from the cgroup when the cgroup is
* at/near its maximum capacity.
diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h b/arch/x86/kernel/cpu/sgx/epc_cgroup.h
index 2285dbfc9462..a530c9611332 100644
--- a/arch/x86/kernel/cpu/sgx/epc_cgroup.h
+++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h
@@ -35,6 +35,8 @@ static inline int __init sgx_cgroup_wq_init(void)
static inline void __init sgx_cgroup_wq_deinit(void) { }
+static inline void sgx_cgroup_reclaim_direct(void) { }
+
#else /* CONFIG_CGROUP_MISC */
struct sgx_cgroup {
@@ -86,6 +88,7 @@ static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg)
int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg, enum sgx_reclaim reclaim);
void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg);
+void sgx_cgroup_reclaim_direct(void);
void __init sgx_cgroup_init(void);
int __init sgx_cgroup_wq_init(void);
void __init sgx_cgroup_wq_deinit(void);
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index b47d627bd370..6f293115b75e 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -414,6 +414,10 @@ static void sgx_reclaim_pages_global(struct mm_struct *charge_mm)
*/
void sgx_reclaim_direct(void)
{
+ /* Reduce chance of per-cgroup reclamation for later allocation */
+ sgx_cgroup_reclaim_direct();
+
+ /* Reduce chance of the global reclamation for later allocation */
if (sgx_should_reclaim_global(SGX_NR_LOW_PAGES))
sgx_reclaim_pages_global(current->mm);
}
--
2.43.0
next prev parent reply other threads:[~2024-08-30 16:41 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-30 16:40 [PATCH v17 00/16] Add Cgroup support for SGX EPC memory Haitao Huang
2024-08-30 16:40 ` [PATCH v17 01/16] x86/sgx: Replace boolean parameters with enums Haitao Huang
2024-08-30 16:40 ` [PATCH v17 02/16] cgroup/misc: Add per resource callbacks for CSS events Haitao Huang
2024-09-02 9:43 ` Huang, Kai
2024-08-30 16:40 ` [PATCH v17 03/16] cgroup/misc: Expose APIs for SGX driver Haitao Huang
2024-08-30 16:40 ` [PATCH v17 04/16] cgroup/misc: Add SGX EPC resource type Haitao Huang
2024-08-30 16:40 ` [PATCH v17 05/16] x86/sgx: Implement basic EPC misc cgroup functionality Haitao Huang
2024-08-30 16:40 ` [PATCH v17 06/16] x86/sgx: Add sgx_epc_lru_list to encapsulate LRU list Haitao Huang
2024-08-30 16:40 ` [PATCH v17 07/16] x86/sgx: Abstract tracking reclaimable pages in LRU Haitao Huang
2024-08-30 16:40 ` [PATCH v17 08/16] x86/sgx: Encapsulate uses of the global LRU Haitao Huang
2024-08-30 16:40 ` [PATCH v17 09/16] x86/sgx: Add basic EPC reclamation flow for cgroup Haitao Huang
2024-08-30 16:40 ` [PATCH v17 10/16] x86/sgx: Implement async reclamation " Haitao Huang
2024-08-30 16:40 ` [PATCH v17 11/16] x86/sgx: Charge mem_cgroup for per-cgroup reclamation Haitao Huang
2024-08-30 16:40 ` Haitao Huang [this message]
2024-08-30 16:40 ` [PATCH v17 13/16] x86/sgx: Revise global reclamation for EPC cgroups Haitao Huang
2024-08-30 16:40 ` [PATCH v17 14/16] x86/sgx: Turn on per-cgroup EPC reclamation Haitao Huang
2024-08-30 16:40 ` [PATCH v17 15/16] Docs/x86/sgx: Add description for cgroup support Haitao Huang
2024-08-30 16:40 ` [PATCH v17 16/16] selftests/sgx: Add scripts for EPC cgroup testing Haitao Huang
[not found] ` <FR4P281MB364378DFE998FADC1CE8B615DF1C2@FR4P281MB3643.DEUP281.PROD.OUTLOOK.COM>
2025-01-15 20:16 ` [PATCH v17 00/16] Add Cgroup support for SGX EPC memory Jarkko Sakkinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240830164038.39343-13-haitao.huang@linux.intel.com \
--to=haitao.huang@linux.intel.com \
--cc=anakrish@microsoft.com \
--cc=bp@alien8.de \
--cc=cgroups@vger.kernel.org \
--cc=chenridong@huawei.com \
--cc=chrisyan@microsoft.com \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=jarkko@kernel.org \
--cc=kai.huang@intel.com \
--cc=kristen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sgx@vger.kernel.org \
--cc=mikko.ylinen@linux.intel.com \
--cc=mingo@redhat.com \
--cc=mkoutny@suse.com \
--cc=seanjc@google.com \
--cc=sohil.mehta@intel.com \
--cc=tglx@linutronix.de \
--cc=tim.c.chen@linux.intel.com \
--cc=tj@kernel.org \
--cc=x86@kernel.org \
--cc=yangjie@microsoft.com \
--cc=zhanb@microsoft.com \
--cc=zhiquan1.li@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®