All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Haitao Huang" <haitao.huang@linux.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>,
	"Mehta, Sohil" <sohil.mehta@intel.com>,
	"tj@kernel.org" <tj@kernel.org>,
	"mingo@redhat.com" <mingo@redhat.com>,
	"bp@alien8.de" <bp@alien8.de>, "Huang, Kai" <kai.huang@intel.com>
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 v10 05/14] x86/sgx: Implement basic EPC misc cgroup functionality
Date: Thu, 04 Apr 2024 20:24:04 -0500	[thread overview]
Message-ID: <op.2lqd6eavwjvjmi@hhuan26-mobl.amr.corp.intel.com> (raw)
In-Reply-To: <89b4e053db21c31859cf2572428fd9d4ab4475ab.camel@intel.com>

On Thu, 28 Mar 2024 07:53:45 -0500, Huang, Kai <kai.huang@intel.com> wrote:

>
>> --- /dev/null
>> +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.c
>> @@ -0,0 +1,74 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +// Copyright(c) 2022 Intel Corporation.
>
> It's 2024 now.
>
> And looks you need to use C style comment for /* Copyright ... */, after  
> looking
> at some other C files.
>
Ok, will update years and use C style.

>> +
>> +#include <linux/atomic.h>
>> +#include <linux/kernel.h>
>> +#include "epc_cgroup.h"
>> +
>> +/* The root SGX EPC cgroup */
>> +static struct sgx_cgroup sgx_cg_root;
>> +
>> +/**
>> + * sgx_cgroup_try_charge() - try to charge cgroup for a single EPC page
>> + *
>> + * @sgx_cg:	The EPC cgroup to be charged for the page.
>> + * Return:
>> + * * %0 - If successfully charged.
>> + * * -errno - for failures.
>> + */
>> +int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg)
>> +{
>> +	return misc_cg_try_charge(MISC_CG_RES_SGX_EPC, sgx_cg->cg, PAGE_SIZE);
>> +}
>> +
>> +/**
>> + * sgx_cgroup_uncharge() - uncharge a cgroup for an EPC page
>> + * @sgx_cg:	The charged sgx cgroup
>> + */
>> +void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg)
>> +{
>> +	misc_cg_uncharge(MISC_CG_RES_SGX_EPC, sgx_cg->cg, PAGE_SIZE);
>> +}
>> +
>> +static void sgx_cgroup_free(struct misc_cg *cg)
>> +{
>> +	struct sgx_cgroup *sgx_cg;
>> +
>> +	sgx_cg = sgx_cgroup_from_misc_cg(cg);
>> +	if (!sgx_cg)
>> +		return;
>> +
>> +	kfree(sgx_cg);
>> +}
>> +
>> +static int sgx_cgroup_alloc(struct misc_cg *cg);
>
> Again, this declaration can be removed if you move the below structure  
> ...
>
>> +
>> +const struct misc_res_ops sgx_cgroup_ops = {
>> +	.alloc = sgx_cgroup_alloc,
>> +	.free = sgx_cgroup_free,
>> +};
>> +
>> +static void sgx_cgroup_misc_init(struct misc_cg *cg, struct sgx_cgroup  
>> *sgx_cg)
>> +{
>> +	cg->res[MISC_CG_RES_SGX_EPC].priv = sgx_cg;
>> +	sgx_cg->cg = cg;
>> +}
>> +
>> +static int sgx_cgroup_alloc(struct misc_cg *cg)
>> +{
>> +	struct sgx_cgroup *sgx_cg;
>> +
>> +	sgx_cg = kzalloc(sizeof(*sgx_cg), GFP_KERNEL);
>> +	if (!sgx_cg)
>> +		return -ENOMEM;
>> +
>> +	sgx_cgroup_misc_init(cg, sgx_cg);
>> +
>> +	return 0;
>> +}
>
> ... here.
>

yes, thanks

>> +
>> +void sgx_cgroup_init(void)
>> +{
>> +	misc_cg_set_ops(MISC_CG_RES_SGX_EPC, &sgx_cgroup_ops);
>> +	sgx_cgroup_misc_init(misc_cg_root(), &sgx_cg_root);
>> +}
>> diff --git a/arch/x86/kernel/cpu/sgx/epc_cgroup.h  
>> b/arch/x86/kernel/cpu/sgx/epc_cgroup.h
>> new file mode 100644
>> index 000000000000..8f794e23fad6
>> --- /dev/null
>> +++ b/arch/x86/kernel/cpu/sgx/epc_cgroup.h
>> @@ -0,0 +1,70 @@
>> +/* SPDX-License-Identifier: GPL-2.0 */
>> +/* Copyright(c) 2022 Intel Corporation. */
>> +#ifndef _SGX_EPC_CGROUP_H_
>> +#define _SGX_EPC_CGROUP_H_
>> +
>> +#include <asm/sgx.h>
>> +#include <linux/cgroup.h>
>> +#include <linux/misc_cgroup.h>
>> +
>> +#include "sgx.h"
>> +
>> +#ifndef CONFIG_CGROUP_SGX_EPC
>
> Nit: add an empty line to make text more breathable.
>

ok

>> +#define MISC_CG_RES_SGX_EPC MISC_CG_RES_TYPES
>> +struct sgx_cgroup;
>> +
>> +static inline struct sgx_cgroup *sgx_get_current_cg(void)
>> +{
>> +	return NULL;
>> +}
>> +
>> +static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg) { }
>> +
>> +static inline int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg)
>> +{
>> +	return 0;
>> +}
>> +
>> +static inline void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg) { }
>> +
>> +static inline void sgx_cgroup_init(void) { }
>> +#else
>
> Nit: I prefer two empty lines before and after the 'else'.
>

ok

>> +struct sgx_cgroup {
>> +	struct misc_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);
>> +}
>> +
>> +/**
>> + * sgx_get_current_cg() - get the EPC cgroup of current process.
>> + *
>> + * Returned cgroup has its ref count increased by 1. Caller must call
>> + * sgx_put_cg() to return the reference.
>> + *
>> + * Return: EPC cgroup to which the current task belongs to.
>> + */
>> +static inline struct sgx_cgroup *sgx_get_current_cg(void)
>> +{
>> +	return sgx_cgroup_from_misc_cg(get_current_misc_cg());
>> +}
>
> Again, I _think_ you need to check whether get_current_misc_cg() returns  
> NULL?
>
> Misc cgroup can be disabled by command line even it is on in the Kconfig.
>
> I am not expert on cgroup, so could you check on this?
>

Good catch. Will add NULL check in sgx_cgroup_from_misc_cg()

>> +
>> +/**
>> + * sgx_put_sgx_cg() - Put the EPC cgroup and reduce its ref count.
>> + * @sgx_cg - EPC cgroup to put.
>> + */
>> +static inline void sgx_put_cg(struct sgx_cgroup *sgx_cg)
>> +{
>> +	if (sgx_cg)
>> +		put_misc_cg(sgx_cg->cg);
>> +}
>> +
>> +int sgx_cgroup_try_charge(struct sgx_cgroup *sgx_cg);
>> +void sgx_cgroup_uncharge(struct sgx_cgroup *sgx_cg);
>> +void sgx_cgroup_init(void);
>> +
>> +#endif
>> +
>> +#endif /* _SGX_EPC_CGROUP_H_ */
>> diff --git a/arch/x86/kernel/cpu/sgx/main.c  
>> b/arch/x86/kernel/cpu/sgx/main.c
>> index d219f14365d4..023af54c1beb 100644
>> --- 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>
>>  #include <linux/node.h>
>>  #include <linux/pagemap.h>
>>  #include <linux/ratelimit.h>
>> @@ -17,6 +18,7 @@
>>  #include "driver.h"
>>  #include "encl.h"
>>  #include "encls.h"
>> +#include "epc_cgroup.h"
>>
>>  struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
>>  static int sgx_nr_epc_sections;
>> @@ -558,7 +560,16 @@ 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 sgx_cgroup *sgx_cg;
>>  	struct sgx_epc_page *page;
>> +	int ret;
>> +
>> +	sgx_cg = sgx_get_current_cg();
>> +	ret = sgx_cgroup_try_charge(sgx_cg);
>> +	if (ret) {
>> +		sgx_put_cg(sgx_cg);
>> +		return ERR_PTR(ret);
>> +	}
>>
>>  	for ( ; ; ) {
>>  		page = __sgx_alloc_epc_page();
>> @@ -567,8 +578,10 @@ struct sgx_epc_page *sgx_alloc_epc_page(void  
>> *owner, enum sgx_reclaim reclaim)
>>  			break;
>>  		}
>>
>> -		if (list_empty(&sgx_active_page_list))
>> -			return ERR_PTR(-ENOMEM);
>> +		if (list_empty(&sgx_active_page_list)) {
>> +			page = ERR_PTR(-ENOMEM);
>> +			break;
>> +		}
>>
>>  		if (reclaim == SGX_NO_RECLAIM) {
>>  			page = ERR_PTR(-EBUSY);
>> @@ -580,10 +593,24 @@ struct sgx_epc_page *sgx_alloc_epc_page(void  
>> *owner, enum sgx_reclaim reclaim)
>>  			break;
>>  		}
>>
>> +		/*
>> +		 * Need to do a global reclamation if cgroup was not full but free
>> +		 * physical pages run out, causing __sgx_alloc_epc_page() to fail.
>> +		 */
>
> Again, to me this comment shouldn't be here, because it doesn't add any  
> more
> information.
>
> If you can reach here, you have passed the charge().  In fact, I believe  
> this
> doesn't matter:
> When you fail to allocate, you just need to reclaim.
>
> Now you only have the global reclamation, thus you need to reclaim from  
> it.
>
> Perhaps it is useful when you have per-cgroup LRU list.  In that case  
> you can
> put this comment there.
>

Ok

>>  		sgx_reclaim_pages();
>>  		cond_resched();
>>  	}
>>
>> +#ifdef CONFIG_CGROUP_SGX_EPC
>> +	if (!IS_ERR(page)) {
>> +		WARN_ON_ONCE(page->sgx_cg);
>> +		/* sgx_put_cg() in sgx_free_epc_page() */
>> +		page->sgx_cg = sgx_cg;
>> +	} else {
>> +		sgx_cgroup_uncharge(sgx_cg);
>> +		sgx_put_cg(sgx_cg);
>> +	}
>> +#endif
>
> Again, IMHO having CONFIG_CGROUP_SGX_EPC here is ugly, because it  
> doesn't even
> match the try_charge() above, which doesn't have the  
> CONFIG_CGROUP_SGX_EPC.
>
> If you add a wrapper in "epc_cgroup.h"
>
Agree. but in sgx.h so sgx_epc_page struct is not exposed in epc_cgroup.h.

> static inline void sgx_epc_page_set_cgroup(struct epc_page *epc_page, 
> 					   struct sgx_cgroup *sgx_cg)
> {
> #ifdef CONFIG_CGROUP_SGX_EPC
> 	epc_page->sgx_cg = sgx_cg;
> #endif		
> }
>
> Then I believe the above can be simplified to:
>
> 	if (!IS_ERR(page)) {
> 		sgx_epc_page_set_cgroup(page, sgx_cg);
> 	} else {
> 		sgx_cgroup_uncharge(sgx_cg);
> 		sgx_put_cg(sgx_cg);
> 	}
>
>>  	if (sgx_should_reclaim(SGX_NR_LOW_PAGES))
>>  		wake_up(&ksgxd_waitq);
>>
>> @@ -604,6 +631,14 @@ void sgx_free_epc_page(struct sgx_epc_page *page)
>>  	struct sgx_epc_section *section = &sgx_epc_sections[page->section];
>>  	struct sgx_numa_node *node = section->node;
>>
>> +#ifdef CONFIG_CGROUP_SGX_EPC
>> +	if (page->sgx_cg) {
>> +		sgx_cgroup_uncharge(page->sgx_cg);
>> +		sgx_put_cg(page->sgx_cg);
>> +		page->sgx_cg = NULL;
>> +	}
>> +#endif
>> +
>
> Similarly, how about adding a wrapper in "epc_cgroup.h"
>
> struct sgx_cgroup *sgx_epc_page_get_cgroup(struct sgx_epc_page *page)
> {
> #ifdef CONFIG_CGROUP_SGX_EPC
> 	return page->sgx_cg;
> #else
> 	return NULL;
> #endif
> }
>
> Then this can be:
>
> 	sgx_cg = sgx_epc_page_get_cgroup(page);
> 	sgx_cgroup_uncharge(sgx_cg);
> 	sgx_put_cg(sgx_cg);
> 	sgx_epc_page_set_cgroup(page, NULL);
>

sure.
>>  	spin_lock(&node->lock);
>>
>>  	page->owner = NULL;
>> @@ -643,6 +678,11 @@ static bool __init sgx_setup_epc_section(u64  
>> phys_addr, u64 size,
>>  		section->pages[i].flags = 0;
>>  		section->pages[i].owner = NULL;
>>  		section->pages[i].poison = 0;
>> +
>> +#ifdef CONFIG_CGROUP_SGX_EPC
>> +		section->pages[i].sgx_cg = NULL;
>> +#endif
>
> Can use the wrapper too.
>
yes.
> [...]
>
> (Btw, I'll be away for couple of days due to public holiday and will  
> review the
> rest starting from late next week).
Thanks
Haitao

  parent reply	other threads:[~2024-04-05  1:24 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-28  0:22 [PATCH v10 00/14] Add Cgroup support for SGX EPC memory Haitao Huang
2024-03-28  0:22 ` [PATCH v10 01/14] x86/sgx: Replace boolean parameters with enums Haitao Huang
2024-03-28  0:22 ` [PATCH v10 02/14] cgroup/misc: Add per resource callbacks for CSS events Haitao Huang
2024-03-28  0:22 ` [PATCH v10 03/14] cgroup/misc: Export APIs for SGX driver Haitao Huang
2024-03-28  0:22 ` [PATCH v10 04/14] cgroup/misc: Add SGX EPC resource type Haitao Huang
2024-03-28  0:22 ` [PATCH v10 05/14] x86/sgx: Implement basic EPC misc cgroup functionality Haitao Huang
2024-03-28 12:53   ` Huang, Kai
2024-03-30 11:17     ` Jarkko Sakkinen
2024-04-01  9:29       ` Huang, Kai
2024-04-01 14:30         ` Jarkko Sakkinen
2024-04-05  1:24     ` Haitao Huang [this message]
2024-04-05  2:55       ` Huang, Kai
2024-03-28  0:22 ` [PATCH v10 06/14] x86/sgx: Add sgx_epc_lru_list to encapsulate LRU list Haitao Huang
2024-03-28  0:22 ` [PATCH v10 07/14] x86/sgx: Abstract tracking reclaimable pages in LRU Haitao Huang
2024-03-28  0:22 ` [PATCH v10 08/14] x86/sgx: Add basic EPC reclamation flow for cgroup Haitao Huang
2024-04-03 13:08   ` Huang, Kai
2024-04-04 17:05     ` Haitao Huang
2024-04-05  2:59       ` Huang, Kai
2024-04-05  3:07       ` Huang, Kai
2024-04-13 20:56         ` Jarkko Sakkinen
2024-03-28  0:22 ` [PATCH v10 09/14] x86/sgx: Implement async reclamation " Haitao Huang
2024-04-04 11:16   ` Huang, Kai
2024-04-04 15:39     ` Haitao Huang
2024-03-28  0:22 ` [PATCH v10 10/14] x86/sgx: Charge mem_cgroup for per-cgroup reclamation Haitao Huang
2024-03-28  0:22 ` [PATCH v10 11/14] x86/sgx: Abstract check for global reclaimable pages Haitao Huang
2024-03-28  0:22 ` [PATCH v10 12/14] x86/sgx: Turn on per-cgroup EPC reclamation Haitao Huang
2024-04-08 12:20   ` Huang, Kai
2024-04-08 18:03     ` Haitao Huang
2024-04-08 22:37       ` Huang, Kai
2024-04-09  4:23         ` Haitao Huang
2024-04-09  9:03           ` Michal Koutný
2024-04-09 15:34             ` Haitao Huang
2024-04-10 18:28               ` Haitao Huang
2024-03-28  0:22 ` [PATCH v10 13/14] Docs/x86/sgx: Add description for cgroup support Haitao Huang
2024-03-28  0:22 ` [PATCH v10 14/14] selftests/sgx: Add scripts for EPC cgroup testing Haitao Huang
2024-03-30 11:29   ` Jarkko Sakkinen
2024-03-31 17:44     ` [PATCH] selftests/sgx: Improve cgroup test scripts Haitao Huang
2024-04-01 14:22       ` Jarkko Sakkinen
2024-04-01 22:55         ` Haitao Huang
2024-04-02  7:37           ` Jarkko Sakkinen
2024-04-02  1:42         ` [PATCH v2] " Haitao Huang
2024-04-02  7:43           ` Jarkko Sakkinen
2024-04-02 17:31             ` Haitao Huang
2024-04-03 15:34               ` 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=op.2lqd6eavwjvjmi@hhuan26-mobl.amr.corp.intel.com \
    --to=haitao.huang@linux.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=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.