Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp24399rdh; Fri, 22 Sep 2023 23:25:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEX3zSIWqPX2P6yBIvH5buB0FQ5wvvtpl9DoATFZRZjkHChpXk5CaOFp89t+v/HUpJGm7eW X-Received: by 2002:a05:6870:a987:b0:19f:aee0:e169 with SMTP id ep7-20020a056870a98700b0019faee0e169mr1973382oab.30.1695450339440; Fri, 22 Sep 2023 23:25:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695450339; cv=none; d=google.com; s=arc-20160816; b=0zfrxJ6D+pDDfIwHpB6/8WGEUUmnTjO1VeIk/BU98agRoB+6Aoo8V6ucSRtrAEwrqV i7YRFsYFJZEQHo4H+oeqe3g0UF2x2AL8hWC89wE/BwqFWlyWtDaVxH1nPWUs6Cyb8gkF xAXmx3D5TAADNReHYtlKIlm7VYQ0fkJJEJWPOQ5tXg5R0Tl5ZljwAnaI6H7dSzBbA54R ZxpbSsT5+ScuB1HukxLlWMEhQtojQ/q/Dv378AxGF8lmtvLybf4lp/rOEkda27qQ8U0b ZuQwJO8m+seHbXBOAVTSJQiVUQmFnhXeoBepfYNM4bhF+5v6Sbg/3Nqoq4fxfCn0mlZe wI7w== 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=SZwQwU5dvzN5rv4w4i3huwpfgT/B+hJ4n03esTL2240=; fh=j8PE345l5Ydlo3KwK7JeWnjqRgjiq4AteUoOZeOwa0I=; b=gXGvzf0SccJ8N/PFFHsZIcpXgKBqbe2ki6EmIGaBhBgc+JN9WCESYZD4314mOXFKFp j450lZ3WKtad7jM9J0pAed9xO5lvgUPLw+K6JEAzED7ydvUvPBk2c3A5MIzUkw05+cWm NmYAt+BLGFcAxuvrbr8JFG8A5iNiq8w7fJlqGBD9V3HmeLCLhbIUGj7mFJzINOgD3OkN lzF/DBaIrNIiOfkB+eAU6kXNP6kq7R/y+pDQy7fnDs1nEGtHi2BKOyPWGIqyoq/8xFb8 NJt8b6FX+pJNll+YVXqSUHbmcdXL74f136PAGRoGO0Soxm6tZpSzVFkHq0SDoIHDorjB DGAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BtF9ojUd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id h11-20020a65480b000000b0051b90567f99si5286054pgs.690.2023.09.22.23.25.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 23:25:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BtF9ojUd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 2A68E80DF262; Fri, 22 Sep 2023 20:07:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229899AbjIWDHO (ORCPT + 99 others); Fri, 22 Sep 2023 23:07:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229684AbjIWDHH (ORCPT ); Fri, 22 Sep 2023 23:07:07 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CB3F180; Fri, 22 Sep 2023 20:07:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695438421; x=1726974421; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gHiEoeQWB3v495nDv7XQWXxhBS4gYS7Eqf+K1QD7IOs=; b=BtF9ojUdSsWayAE/JTA8gOmnE+8hkmQCtKXsaT6XKxRJSuanN3siCs5M xtglpOdWbAgW8fiZCJ5+JWX3c083r04w+oqmZlhpiBo0OnnqoKAQCvf42 k/aPjLVN1R8NQr9qzP4FEZVeQaji+E/0VQo2gK+RJi24dAK1hL3i+G0xK ImHTjO+XIR1sqlcMhE7LR1YwcOVR+t026J+WqzjQpt0sGkoCb/SV8fQK8 IgGnT7a8qf+i9A1NYbgaXRf7t0QzFIaTJsX3DMRbc/gmImWRqQyPaBUV4 0dZqYvztPBlnxthOT+0EZUVwNcq154tBBVN/2wosa0mBsx8rQnLIwo5kA w==; X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="447466730" X-IronPort-AV: E=Sophos;i="6.03,169,1694761200"; d="scan'208";a="447466730" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2023 20:07:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="891048521" X-IronPort-AV: E=Sophos;i="6.03,169,1694761200"; d="scan'208";a="891048521" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by fmsmga001.fm.intel.com with ESMTP; 22 Sep 2023 20:06:03 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, 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 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 Subject: [PATCH v5 02/18] cgroup/misc: Add SGX EPC resource type and export APIs for SGX driver Date: Fri, 22 Sep 2023 20:06:41 -0700 Message-Id: <20230923030657.16148-3-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230923030657.16148-1-haitao.huang@linux.intel.com> References: <20230923030657.16148-1-haitao.huang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 22 Sep 2023 20:07:21 -0700 (PDT) From: Kristen Carlson Accardi Add SGX EPC memory, MISC_CG_RES_SGX_EPC, to be a valid resource type for the misc controller. Add per resource type private data so that SGX can store additional per cgroup data in misc_cg->misc_cg_res[MISC_CG_RES_SGX_EPC]. Export misc_cg_root() so the SGX driver can initialize and add those additional structures to the root misc cgroup as part of initialization for EPC cgroup support. This bootstraps the same additional initialization for non-root cgroups in the 'alloc()' callback added in the previous patch. The SGX driver, as the EPC memory provider, will have a background worker to reclaim EPC pages to make room for new allocations in the same cgroup when its usage counter reaches near the limit controlled by the cgroup and its ancestors. Therefore it needs to do a walk from the current cgroup up to the root. To enable this walk, 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 kernel/cgroup/misc.c to use the new name. Signed-off-by: Kristen Carlson Accardi Signed-off-by: Haitao Huang --- V5: - Revised commit message (Jarkko) V4: - Moved this to the second in the series. --- 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 96a88822815a..87f29f8597e1 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 { u64 max; atomic64_t usage; atomic64_t events; + void *priv; /* per resource callback ops */ int (*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); 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_try_charge(enum misc_res_type type, struct misc_cg *cg, u64 amount); @@ -78,6 +84,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. * @@ -102,6 +122,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) { diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c index 62c9198dee21..4633b8629e63 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 u64 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. @@ -150,7 +149,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); @@ -163,12 +162,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; @@ -190,7 +189,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); -- 2.25.1