From: "Huang, Kai" <kai.huang@intel.com>
To: "hpa@zytor.com" <hpa@zytor.com>,
"tim.c.chen@linux.intel.com" <tim.c.chen@linux.intel.com>,
"linux-sgx@vger.kernel.org" <linux-sgx@vger.kernel.org>,
"x86@kernel.org" <x86@kernel.org>,
"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>,
"jarkko@kernel.org" <jarkko@kernel.org>,
"cgroups@vger.kernel.org" <cgroups@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"mkoutny@suse.com" <mkoutny@suse.com>,
"tglx@linutronix.de" <tglx@linutronix.de>,
"haitao.huang@linux.intel.com" <haitao.huang@linux.intel.com>,
"Mehta, Sohil" <sohil.mehta@intel.com>,
"tj@kernel.org" <tj@kernel.org>,
"mingo@redhat.com" <mingo@redhat.com>,
"bp@alien8.de" <bp@alien8.de>
Cc: "mikko.ylinen@linux.intel.com" <mikko.ylinen@linux.intel.com>,
"seanjc@google.com" <seanjc@google.com>,
"anakrish@microsoft.com" <anakrish@microsoft.com>,
"Zhang, Bo" <zhanb@microsoft.com>,
"kristen@linux.intel.com" <kristen@linux.intel.com>,
"yangjie@microsoft.com" <yangjie@microsoft.com>,
"Li, Zhiquan1" <zhiquan1.li@intel.com>,
"chrisyan@microsoft.com" <chrisyan@microsoft.com>
Subject: Re: [PATCH v12 05/14] x86/sgx: Implement basic EPC misc cgroup functionality
Date: Tue, 16 Apr 2024 13:22:06 +0000 [thread overview]
Message-ID: <a5e009636c5144622e0a910a459cd9d05976715e.camel@intel.com> (raw)
In-Reply-To: <20240416032011.58578-6-haitao.huang@linux.intel.com>
On Mon, 2024-04-15 at 20:20 -0700, Haitao Huang wrote:
> From: Kristen Carlson Accardi <kristen@linux.intel.com>
>
> SGX Enclave Page Cache (EPC) memory allocations are separate from normal
> RAM allocations, and are managed solely by the SGX subsystem. The
> existing cgroup memory controller cannot be used to limit or account for
> SGX EPC memory, which is a desirable feature in some environments. For
> instance, within a Kubernetes environment, while a user may specify a
> particular EPC quota for a pod, the orchestrator requires a mechanism to
> enforce that the pod's actual runtime EPC usage does not exceed the
> allocated quota.
>
> Utilize the misc controller [admin-guide/cgroup-v2.rst, 5-9. Misc] to
> limit and track EPC allocations per cgroup. Earlier patches have added
> the "sgx_epc" resource type in the misc cgroup subsystem. Add basic
> support in SGX driver as the "sgx_epc" resource provider:
>
> - Set "capacity" of EPC by calling misc_cg_set_capacity()
> - Update EPC usage counter, "current", by calling charge and uncharge
> APIs for EPC allocation and deallocation, respectively.
> - Setup sgx_epc resource type specific callbacks, which perform
> initialization and cleanup during cgroup allocation and deallocation,
> respectively.
>
> With these changes, the misc cgroup controller enables users to set a hard
> limit for EPC usage in the "misc.max" interface file. It reports current
> usage in "misc.current", the total EPC memory available in
> "misc.capacity", and the number of times EPC usage reached the max limit
> in "misc.events".
>
> For now, the EPC cgroup simply blocks additional EPC allocation in
> sgx_alloc_epc_page() when the limit is reached. Reclaimable pages are
> still tracked in the global active list, only reclaimed by the global
> reclaimer when the total free page count is lower than a threshold.
>
> Later patches will reorganize the tracking and reclamation code in the
> global reclaimer and implement per-cgroup tracking and reclaiming.
>
> Co-developed-by: Sean Christopherson <sean.j.christopherson@intel.com>
> Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
> Signed-off-by: Kristen Carlson Accardi <kristen@linux.intel.com>
> Co-developed-by: Haitao Huang <haitao.huang@linux.intel.com>
> Signed-off-by: Haitao Huang <haitao.huang@linux.intel.com>
> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
> Reviewed-by: Tejun Heo <tj@kernel.org>
> Tested-by: Jarkko Sakkinen <jarkko@kernel.org>
I don't see any big issue, so feel free to add:
Reviewed-by: Kai Huang <kai.huang@intel.com>
Nitpickings below:
[...]
> --- /dev/null
> +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c
> @@ -0,0 +1,72 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright(c) 2022-2024 Intel Corporation. */
> +
> +#include <linux/atomic.h>
> +#include <linux/kernel.h>
It doesn't seem you need the above two here.
Probably they are needed in later patches, in that case we can move to the
relevant patch(es) that they got used.
However I think it's better to explicitly include <linux/slab.h> since
kzalloc()/kfree() are used.
Btw, I am not sure whether you want to use <linux/kernel.h> because looks
it contains a lot of unrelated staff. Anyway I guess nobody cares.
> +#include "epc_cgroup.h"
> +
> +/* The root SGX EPC cgroup */
> +static struct sgx_cgroup sgx_cg_root;
The comment isn't necessary (sorry didn't notice before), because the code
is pretty clear saying that IMHO.
[...]
>
> --- /dev/null
> +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h
> @@ -0,0 +1,72 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef _SGX_EPC_CGROUP_H_
> +#define _SGX_EPC_CGROUP_H_
> +
> +#include <asm/sgx.h>
I don't see why you need <asm/sgx.h> here. Also, ...
> +#include <linux/cgroup.h>
> +#include <linux/misc_cgroup.h>
> +
> +#include "sgx.h"
... "sgx.h" already includes <asm/sgx.h>
[...]
>
> +static inline struct sgx_cgroup *sgx_get_current_cg(void)
> +{
> + /* get_current_misc_cg() never returns NULL when Kconfig enabled */
> + return sgx_cgroup_from_misc_cg(get_current_misc_cg());
> +}
I spent some time looking into this. And yes if I was reading code
correctly the get_current_misc_cg() should never return NULL when Kconfig
is on.
I typed my analysis below in [*]. And it would be helpful if any cgroup
expert can have a second eye on this.
[...]
> --- a/arch/x86/kernel/cpu/sgx/main.c
> +++ b/arch/x86/kernel/cpu/sgx/main.c
> @@ -6,6 +6,7 @@
> #include <linux/highmem.h>
> #include <linux/kthread.h>
> #include <linux/miscdevice.h>
> +#include <linux/misc_cgroup.h>
Is this needed? I believe SGX variants in "epc_cgroup.h" should be enough
for sgx/main.c?
[...]
[*] IIUC get_current_misc_cg() should never return NULL when Kconfig is on
(code indent slight adjusted for text wrap).
Firstly, during kernel boot there's always a valid @css allocated for MISC
cgroup, regardless whether it is disabled in kernel command line.
int __init cgroup_init(void)
{
...
for_each_subsys(ss, ssid) {
if (ss->early_init) {
...
} else {
cgroup_init_subsys(ss, false);
}
...
if (!cgroup_ssid_enabled(ssid))
continue;
...
}
...
}
cgroup_init_subsys() makes a valid @css is allocated for MISC cgroup and
set the pointer to the @init_css_set.
static void __init cgroup_init_subsys(struct cgroup_subsys *ss,
...)
{
struct cgroup_subsys_state *css;
...
css = ss->css_alloc(NULL);
/* We don't handle early failures gracefully */
BUG_ON(IS_ERR(css));
...
init_css_set.subsys[ss->id] = css;
...
}
All processes are by default associated to the @init_css_set:
void cgroup_fork(struct task_struct *child)
{
RCU_INIT_POINTER(child->cgroups, &init_css_set);
INIT_LIST_HEAD(&child->cg_list);
}
At runtime, when a new cgroup is created in the hierarchy, the "cgroup"
can have a NULL @css if some subsystem is not enabled in it:
static int cgroup_apply_control_enable(struct cgroup *cgrp)
{
struct cgroup *dsct;
struct cgroup_subsys_state *d_css;
struct cgroup_subsys *ss;
int ssid, ret;
cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
for_each_subsys(ss, ssid) {
struct cgroup_subsys_state *css =
cgroup_css(dsct, ss);
if (!(cgroup_ss_mask(dsct) &
(1 << ss->id)))
continue;
if (!css) {
css = css_create(dsct, ss);
if (IS_ERR(css))
return PTR_ERR(css);
}
...
}
}
}
We can see if cgroup_ss_mask(dsct) doesn't have subsystem enabled, the
css_create() won't be invoked, and cgroup->subsys[ssid] will remain NULL.
However, when a process is bound to a specific cgroup, the kernel tries to
get the cgorup's "effective css", and it seems this "effective css" cannot
be NULL if the subsys has a valid 'struct cgroup_subsys' provided, which
the MISC cgroup does.
There are couple of code paths can lead to this, but they all reach to
static struct css_set *find_existing_css_set(...)
{
struct cgroup_root *root = cgrp->root;
struct cgroup_subsys *ss;
struct css_set *cset;
unsigned long key;
int i;
for_each_subsys(ss, i) {
if (root->subsys_mask & (1UL << i)) {
/*
* @ss is in this hierarchy, so we want
* the effective css from @cgrp.
*/
template[i] = cgroup_e_css_by_mask(cgrp,
ss);
} else {
/*
* @ss is not in this hierarchy, so we
* don't want to change the css.
*/
template[i] = old_cset->subsys[i];
}
}
...
}
Which calls cgroup_e_css_by_mask() to get the "effective css" when subsys
is enabled in the root cgroup (which means MISC cgroup is not disabled by
kernel command line), or get the default css, which is @init_css_set-
>subsys[ssid], which is always valid for MISC cgroup.
And more specifically, the "effective css" in the cgroup_e_css_by_mask()
is done by searching the entire hierarchy, so MISC cgroup will always have
a valid "effective css".
static struct cgroup_subsys_state *cgroup_e_css_by_mask(
struct cgroup *cgrp,
struct cgroup_subsys *ss)
{
lockdep_assert_held(&cgroup_mutex);
if (!ss)
return &cgrp->self;
...
while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
cgrp = cgroup_parent(cgrp);
if (!cgrp)
return NULL;
}
return cgroup_css(cgrp, ss);
}
The comment of cgroup_e_css_by_mask() says:
* Similar to cgroup_css() but returns the effective css, which is defined
* as the matching css of the nearest ancestor including self which has @ss
* enabled. If @ss is associated with the hierarchy @cgrp is on, this
* function is guaranteed to return non-NULL css.
It's hard for me to interpret the second sentence, specifically, what does
"@ss is associated with the hierarchy @cgrp is on" mean. I interpret it
as "subsys is enabled in root and/or any descendants".
But again, in the find_existing_css_set() it is called when the root
cgroup has enabled the subsys, so it should always return a non-NULL css.
And that means for any process, get_current_misc_cg() cannot be NULL.
next prev parent reply other threads:[~2024-04-16 13:22 UTC|newest]
Thread overview: 64+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-16 3:19 [PATCH v12 00/14] Add Cgroup support for SGX EPC memory Haitao Huang
2024-04-16 3:19 ` [PATCH v12 01/14] x86/sgx: Replace boolean parameters with enums Haitao Huang
2024-04-16 3:19 ` [PATCH v12 02/14] cgroup/misc: Add per resource callbacks for CSS events Haitao Huang
2024-04-16 3:20 ` [PATCH v12 03/14] cgroup/misc: Export APIs for SGX driver Haitao Huang
2024-04-16 3:20 ` [PATCH v12 04/14] cgroup/misc: Add SGX EPC resource type Haitao Huang
2024-04-16 3:20 ` [PATCH v12 05/14] x86/sgx: Implement basic EPC misc cgroup functionality Haitao Huang
2024-04-16 13:22 ` Huang, Kai [this message]
2024-04-18 22:41 ` Haitao Huang
2024-04-18 23:29 ` Huang, Kai
2024-04-19 18:15 ` Haitao Huang
2024-04-19 22:21 ` Huang, Kai
2024-04-16 22:23 ` Haitao Huang
2024-04-16 3:20 ` [PATCH v12 06/14] x86/sgx: Add sgx_epc_lru_list to encapsulate LRU list Haitao Huang
2024-04-16 3:20 ` [PATCH v12 07/14] x86/sgx: Abstract tracking reclaimable pages in LRU Haitao Huang
2024-04-16 14:07 ` Huang, Kai
2024-04-16 22:48 ` Haitao Huang
2024-04-16 3:20 ` [PATCH v12 08/14] x86/sgx: Add basic EPC reclamation flow for cgroup Haitao Huang
2024-04-17 23:51 ` Huang, Kai
2024-04-23 15:53 ` Haitao Huang
2024-04-16 3:20 ` [PATCH v12 09/14] x86/sgx: Implement async reclamation " Haitao Huang
2024-04-19 1:32 ` Huang, Kai
2024-04-19 18:55 ` Haitao Huang
2024-04-19 22:44 ` Huang, Kai
2024-04-20 1:14 ` Haitao Huang
2024-04-22 0:22 ` Huang, Kai
2024-04-22 16:17 ` Haitao Huang
2024-04-22 22:16 ` Huang, Kai
2024-04-23 13:08 ` Haitao Huang
2024-04-23 14:19 ` Huang, Kai
2024-04-23 15:30 ` Haitao Huang
2024-04-23 22:13 ` Huang, Kai
2024-04-24 0:26 ` Haitao Huang
2024-04-24 2:13 ` Huang, Kai
2024-04-16 3:20 ` [PATCH v12 10/14] x86/sgx: Charge mem_cgroup for per-cgroup reclamation Haitao Huang
2024-04-23 7:21 ` Huang, Kai
2024-04-16 3:20 ` [PATCH v12 11/14] x86/sgx: Abstract check for global reclaimable pages Haitao Huang
2024-04-16 3:20 ` [PATCH v12 12/14] x86/sgx: Turn on per-cgroup EPC reclamation Haitao Huang
2024-04-29 10:49 ` Huang, Kai
2024-04-29 16:05 ` Haitao Huang
2024-04-29 22:18 ` Huang, Kai
2024-04-30 1:31 ` Haitao Huang
2024-04-16 3:20 ` [PATCH v12 13/14] Docs/x86/sgx: Add description for cgroup support Haitao Huang
2024-04-21 7:18 ` Bagas Sanjaya
2024-04-23 7:29 ` Huang, Kai
2024-04-16 3:20 ` [PATCH v12 14/14] selftests/sgx: Add scripts for EPC cgroup testing Haitao Huang
2024-04-16 5:16 ` Haitao Huang
2024-04-16 5:42 ` Huang, Kai
2024-04-16 14:15 ` Jarkko Sakkinen
2024-04-26 14:28 ` Dave Hansen
2024-04-28 22:03 ` Jarkko Sakkinen
2024-04-29 16:18 ` Haitao Huang
2024-04-29 16:43 ` Jarkko Sakkinen
2024-04-29 17:14 ` Haitao Huang
2024-04-16 15:00 ` Haitao Huang
2024-04-16 14:05 ` Jarkko Sakkinen
2024-04-16 14:10 ` Jarkko Sakkinen
2024-04-16 14:54 ` Haitao Huang
2024-04-16 16:08 ` Jarkko Sakkinen
2024-04-16 22:04 ` Haitao Huang
2024-04-16 22:21 ` Haitao Huang
2024-04-17 3:05 ` Haitao Huang
2024-04-17 22:46 ` Jarkko Sakkinen
2024-04-24 19:42 ` Haitao Huang
2024-04-25 4:51 ` 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=a5e009636c5144622e0a910a459cd9d05976715e.camel@intel.com \
--to=kai.huang@intel.com \
--cc=anakrish@microsoft.com \
--cc=bp@alien8.de \
--cc=cgroups@vger.kernel.org \
--cc=chrisyan@microsoft.com \
--cc=dave.hansen@linux.intel.com \
--cc=haitao.huang@linux.intel.com \
--cc=hpa@zytor.com \
--cc=jarkko@kernel.org \
--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®