From patchwork Fri Dec 2 18:36:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kristen Carlson Accardi X-Patchwork-Id: 29074 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1017024wrr; Fri, 2 Dec 2022 10:46:07 -0800 (PST) X-Google-Smtp-Source: AA0mqf77WOXXCnT0vChe8PiDPeKoOqZKO6hB7VDPSnOeyzwxpV5B6PnS/EncoR/6qZaPITIhM6Ei X-Received: by 2002:a17:906:eb41:b0:7b6:ff1c:c4f0 with SMTP id mc1-20020a170906eb4100b007b6ff1cc4f0mr46820365ejb.359.1670006767368; Fri, 02 Dec 2022 10:46:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670006767; cv=none; d=google.com; s=arc-20160816; b=omyujMsPpnb6yy3gBOpQdmRyOea+dfyvI/loJeT2AEwuWvDo9PeEwkpcZxWda0jDlC N/ZlseogCbJoRio1iyLnEOWpo/ZQGsbpMIaWJzW8xH7t0mQmkxyjwb44z9pyL77SyPLS oWnwJ+OhhGVu1amv/dIetf/L/PfO3QDYG4xUA29at9PR2MaVWELpma8+Bq0ZN0ug2Arm YK8E4o3def0AENsZq1cmWFqONmWYVkU5LxMkOulYL7dhslZPxsn3mk7HJ3mEjUeRwzuJ y9/VcAKCHLYZqPM6+A+ZRS7oRlrHSNiL/JkskZx9H9WK7jIZRbZ6tsQLy3pHFC9kk1pV L4Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Tq2UqLkAMA2BAc10lqp9A6xwlobNAqNJlvSHJDDIgaA=; b=Z+I/fvTZni/qhgJcjg58HVVLAAdD7fdfrXyEXPj3GbpaqwUNsuVmZfj4ntJfUHTwW0 tucaYHfUSdhwaPxHmu78jO/OZ7RWjQS/EzuKBs4XW52VcbE+/OQJ8IBsLDCEwilQiuQP qKWzUAzWJW+/R4G8L3tIq1dRM8MUYSjzLhMwI1ytDrc8U1KseRpvHJbmDBCtcKqMeaHH neTwpzvZ2sWAU8b2ir67GB3PcpvHQOU3upGd9C1PkMihcnLEhmoDtR2a03exmbbEAlUQ ZNQAb/DXa8ozGuXSaOrMEsLymeidnTJKlcKotQ0Ep31DFeGl4JvZSfSFThBBBZvV6sgz lUVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N+ZkGZgx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oz44-20020a1709077dac00b007af0d7322a8si6821962ejc.836.2022.12.02.10.45.43; Fri, 02 Dec 2022 10:46:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N+ZkGZgx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234624AbiLBSjI (ORCPT + 99 others); Fri, 2 Dec 2022 13:39:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234629AbiLBSiX (ORCPT ); Fri, 2 Dec 2022 13:38:23 -0500 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B1CEDFFE; Fri, 2 Dec 2022 10:37:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670006261; x=1701542261; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FELkx3f6pgyMIZAcnt5cglOY+kr2mG0MHu5R8dRR8ng=; b=N+ZkGZgx/ORltXiH1jfyYkVf8h3zUX1l70A19b80TWocatq0+ZNEhSt4 tNR4y7nCtZ5dcPUnNdMVFWe95xPIzj16HQ1TFiHpoOYylEIHVoip+0ApL veK3BcuauoKSc6gL4WhpUR9bu+rJmoSn3TZHekgQilhQ3UaVMGL1BcA3c vcazw9Cbu8BdOWs2ZW+NF8mR5gV89qM2ZTL+MAaeyhv5kSfogV6VCm4yM xj/CCxDnJND5ofSAIM9Et49c3BOFcjhjAaNjzB5sJvD1HQy563LZxsMRH klFuHjvzqkFiBrywVrNbJqOOb7eNhUiNVagv6PhM6OaMY50ISwOSrs//3 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="314724686" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="314724686" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 10:37:37 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="713717579" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="713717579" Received: from kcaskeyx-mobl1.amr.corp.intel.com (HELO kcaccard-desk.amr.corp.intel.com) ([10.251.1.207]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 10:37:33 -0800 From: Kristen Carlson Accardi To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, cgroups@vger.kernel.org, Zefan Li , Johannes Weiner Cc: zhiquan1.li@intel.com, Kristen Carlson Accardi Subject: [PATCH v2 16/18] cgroup/misc: Prepare for SGX usage Date: Fri, 2 Dec 2022 10:36:52 -0800 Message-Id: <20221202183655.3767674-17-kristen@linux.intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221202183655.3767674-1-kristen@linux.intel.com> References: <20221202183655.3767674-1-kristen@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751129015871014619?= X-GMAIL-MSGID: =?utf-8?q?1751129015871014619?= The SGX driver will need to get access to the root misc_cg object to do iterative walks and also determine if a charge will be towards the root cgroup or not. To manage the SGX EPC memory via the misc controller, the SGX driver will also need to be able to iterate over the misc cgroup hierarchy. Move parent_misc() into misc_cgroup.h and make inline to make this function available to SGX, rename it to misc_cg_parent(), and update misc.c to use the new name. Add per resource type private data so that SGX can store additional per cgroup data with the misc_cg struct. Allow SGX EPC memory to be a valid resource type for the misc controller. Signed-off-by: Kristen Carlson Accardi --- include/linux/misc_cgroup.h | 29 +++++++++++++++++++++++++++++ kernel/cgroup/misc.c | 25 ++++++++++++------------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h index 83620e7c4bb1..53a64d3bb6d7 100644 --- a/include/linux/misc_cgroup.h +++ b/include/linux/misc_cgroup.h @@ -17,6 +17,10 @@ enum misc_res_type { MISC_CG_RES_SEV, /* AMD SEV-ES ASIDs resource */ MISC_CG_RES_SEV_ES, +#endif +#ifdef CONFIG_CGROUP_SGX_EPC + /* SGX EPC memory resource */ + MISC_CG_RES_SGX_EPC, #endif MISC_CG_RES_TYPES }; @@ -37,6 +41,7 @@ struct misc_res { unsigned long max; atomic_long_t usage; atomic_long_t events; + void *priv; /* per resource callback ops */ int (*misc_cg_alloc)(struct misc_cg *cg); @@ -59,6 +64,7 @@ struct misc_cg { struct misc_res res[MISC_CG_RES_TYPES]; }; +struct misc_cg *misc_cg_root(void); unsigned long misc_cg_res_total_usage(enum misc_res_type type); int misc_cg_set_capacity(enum misc_res_type type, unsigned long capacity); int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, @@ -80,6 +86,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. * @@ -104,6 +124,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 unsigned long misc_cg_res_total_usage(enum misc_res_type type) { diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c index 3d17afd5b7a8..e1e506847dea 100644 --- a/kernel/cgroup/misc.c +++ b/kernel/cgroup/misc.c @@ -24,6 +24,10 @@ static const char *const misc_res_name[] = { /* AMD SEV-ES ASIDs resource */ "sev_es", #endif +#ifdef CONFIG_CGROUP_SGX_EPC + /* Intel SGX EPC memory bytes */ + "sgx_epc", +#endif }; /* Root misc cgroup */ @@ -40,18 +44,13 @@ static struct misc_cg root_cg; static unsigned long misc_res_capacity[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. @@ -151,7 +150,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, 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 = atomic_long_add_return(amount, &res->usage); @@ -164,12 +163,12 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, return 0; err_charge: - for (j = i; j; j = parent_misc(j)) { + for (j = i; j; j = misc_cg_parent(j)) { atomic_long_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; @@ -192,7 +191,7 @@ void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg, 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);