Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp94419rdd; Mon, 8 Jan 2024 19:37:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IFlw8ftOApqltB79iY4VZodbaXl1/ICxJhun7ZTnvxuaVjL6c84tiqvPTRJDcwIU9cuvk48 X-Received: by 2002:a05:620a:490f:b0:782:41bf:6ef0 with SMTP id vy15-20020a05620a490f00b0078241bf6ef0mr5278912qkn.27.1704771463163; Mon, 08 Jan 2024 19:37:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704771463; cv=none; d=google.com; s=arc-20160816; b=bdTOEVrAfLSRu8k1nK3yXTDWduGHNXNlUwwcy54Cd0/fwpQdO8cGaawZ5pRn0uSURn VffYXkqWCKwL1buv8HwBGd5JPifsu9Q/BIbO4J3mUDLw8z3vXOgFOWBtVrIk8Hg5iyDa QsWGXqGE1lSPbfaRPH/HqOORbQtja8Qx7TTCYnpuHrY8pZT2M8ylo5U/uhtK8B/pWViX 4vHc/uejegedVB91VEVXvC2VvOF1i750kzfA2Feouf+NIniTWLCZGy5DkdNLkikl80tl fYri74itRmhafRoGFQnCDC99OjawRzuCdxekciIaVp90VauPOsUzETJQEDdSkJvZtezn Pg2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:message-id:organization:from :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:references:subject:cc:to :dkim-signature; bh=ezW6TkVmAVF17JoLfSrLQSOyy8kOGGxq3JCRmQW9ExQ=; fh=IW1LR/8Q6y7RtoOuca0SdGcbM6tV0e3oBDAGRULYchA=; b=kpszqxsg6ISXnF9Ej4I/ZhFGffnRRrhPb1I/5tqT1MgtPPb8i7Hvg+j/QhaNwoa2Ap QwAHT4fEGaWI/+KBpkgNSgT6mF8tJrdmiyVzuYTqU9zz8EKS9yvzndDrYCPBPaSwd/nP f/R77zk+C2YTNKQcc0dImbWbI8Cfo9u+2/nsfYLT3XikyEi3S01fFfF20cyVkXl4be/o 4mw4EaeYqT1+jmWhwZ1b3umS2IcMtPwKxQIeZR2ZXgY2PmmlbbSHp0iYbfAGJALwJg8q DEF8pAdFK/rBsP39n9go8l1YeI+a+1eF/MLCjt/eVoLtNwwzDSIIvWEno2+WA64d26DR uWNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="SEc/OzOn"; spf=pass (google.com: domain of linux-kernel+bounces-20329-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20329-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d17-20020a05620a241100b007830ce6519fsi1284826qkn.649.2024.01.08.19.37.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 19:37:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-20329-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="SEc/OzOn"; spf=pass (google.com: domain of linux-kernel+bounces-20329-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20329-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D78DD1C22029 for ; Tue, 9 Jan 2024 03:37:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 608204699; Tue, 9 Jan 2024 03:37:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="SEc/OzOn" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8ECE3259D; Tue, 9 Jan 2024 03:37:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704771453; x=1736307453; h=to:cc:subject:references:date:mime-version: content-transfer-encoding:from:message-id:in-reply-to; bh=5jyuMJax7bmYWl+lCmULXdHEsvXolm204ScVfBGcp5Y=; b=SEc/OzOnLEFwlyPyVwUszzInE1xla+gQQvqlQQ6VREsAUDZyDsJvA3/u GNmGpf4gWsR4LiGJcZxh2+IbOgCaD+bifiv96eQ1IbtbWmSmPkIUEeMgw G+bKI7QWFvY/ZOY5o9IolSQhfUsioecfiOxXa7QDd+fffXzk6ZE0amAxP 3UdtMLWqimbCXLrCv8vYlR6TW0/m/QmLcOB7XyS34uqRFRHvh4pF4NbtN OUskKX4XXcYVycUGmdjyI6L3n6K59+1DihKo1O1SEtlJlMXZfdg6SfjKo xK1VnbVMKPOeq2tbr1zDySyxsQ+e0W0TA+nnynZ/4gaFZc5hBkjch5la3 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10947"; a="5440704" X-IronPort-AV: E=Sophos;i="6.04,181,1695711600"; d="scan'208";a="5440704" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jan 2024 19:37:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10947"; a="731328961" X-IronPort-AV: E=Sophos;i="6.04,181,1695711600"; d="scan'208";a="731328961" Received: from hhuan26-mobl.amr.corp.intel.com ([10.92.17.168]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-SHA; 08 Jan 2024 19:37:28 -0800 Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes To: dave.hansen@linux.intel.com, tj@kernel.org, mkoutny@suse.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, "Jarkko Sakkinen" 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: Re: [PATCH v6 01/12] cgroup/misc: Add per resource callbacks for CSS events References: <20231030182013.40086-1-haitao.huang@linux.intel.com> <20231030182013.40086-2-haitao.huang@linux.intel.com> Date: Mon, 08 Jan 2024 21:37:26 -0600 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: "Haitao Huang" Organization: Intel Message-ID: In-Reply-To: User-Agent: Opera Mail/1.0 (Win32) On Wed, 15 Nov 2023 14:25:59 -0600, Jarkko Sakkinen wrote: > On Mon Oct 30, 2023 at 8:20 PM EET, Haitao Huang wrote: >> From: Kristen Carlson Accardi >> >> The misc cgroup controller (subsystem) currently does not perform >> resource type specific action for Cgroups Subsystem State (CSS) events: >> the 'css_alloc' event when a cgroup is created and the 'css_free' event >> when a cgroup is destroyed. >> >> Define callbacks for those events and allow resource providers to >> register the callbacks per resource type as needed. This will be >> utilized later by the EPC misc cgroup support implemented in the SGX >> driver. >> >> Also add per resource type private data for those callbacks to store and >> access resource specific data. >> >> Signed-off-by: Kristen Carlson Accardi >> Co-developed-by: Haitao Huang >> Signed-off-by: Haitao Huang >> --- >> V6: >> - Create ops struct for per resource callbacks (Jarkko) >> - Drop max_write callback (Dave, Michal) >> - Style fixes (Kai) >> --- >> include/linux/misc_cgroup.h | 14 ++++++++++++++ >> kernel/cgroup/misc.c | 27 ++++++++++++++++++++++++--- >> 2 files changed, 38 insertions(+), 3 deletions(-) >> >> diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h >> index e799b1f8d05b..5dc509c27c3d 100644 >> --- a/include/linux/misc_cgroup.h >> +++ b/include/linux/misc_cgroup.h >> @@ -27,16 +27,30 @@ struct misc_cg; >> >> #include >> >> +/** >> + * struct misc_operations_struct: per resource callback ops. >> + * @alloc: invoked for resource specific initialization when cgroup is >> allocated. >> + * @free: invoked for resource specific cleanup when cgroup is >> deallocated. >> + */ >> +struct misc_operations_struct { >> + int (*alloc)(struct misc_cg *cg); >> + void (*free)(struct misc_cg *cg); >> +}; > > Maybe just misc_operations, or even misc_ops? > With Michal's suggestion to make ops per-resource-type, I'll rename this misc_res_ops (I was following vm_operations_struct as example) >> + >> /** >> * struct misc_res: Per cgroup per misc type resource >> * @max: Maximum limit on the resource. >> * @usage: Current usage of the resource. >> * @events: Number of times, the resource limit exceeded. >> + * @priv: resource specific data. >> + * @misc_ops: resource specific operations. >> */ >> struct misc_res { >> u64 max; >> atomic64_t usage; >> atomic64_t events; >> + void *priv; > > priv is the wrong patch, it just confuses the overall picture heere. > please move it to 04/12. Let's deal with the callbacks here. > Ok >> + const struct misc_operations_struct *misc_ops; >> }; > > misc_ops would be at least consistent with this, as misc_res also has an > acronym. > >> >> /** >> diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c >> index 79a3717a5803..d971ede44ebf 100644 >> --- a/kernel/cgroup/misc.c >> +++ b/kernel/cgroup/misc.c >> @@ -383,23 +383,37 @@ static struct cftype misc_cg_files[] = { >> static struct cgroup_subsys_state * >> misc_cg_alloc(struct cgroup_subsys_state *parent_css) >> { >> + struct misc_cg *parent_cg, *cg; >> enum misc_res_type i; >> - struct misc_cg *cg; >> + int ret; >> >> if (!parent_css) { >> - cg = &root_cg; >> + parent_cg = cg = &root_cg; >> } else { >> cg = kzalloc(sizeof(*cg), GFP_KERNEL); >> if (!cg) >> return ERR_PTR(-ENOMEM); >> + parent_cg = css_misc(parent_css); >> } >> >> for (i = 0; i < MISC_CG_RES_TYPES; i++) { >> WRITE_ONCE(cg->res[i].max, MAX_NUM); >> atomic64_set(&cg->res[i].usage, 0); >> + if (parent_cg->res[i].misc_ops && parent_cg->res[i].misc_ops->alloc) >> { >> + ret = parent_cg->res[i].misc_ops->alloc(cg); >> + if (ret) >> + goto alloc_err; > > The patch set only has a use case for both operations defined - any > partial combinations should never be allowed. > > To enforce this invariant you could create a set of operations (written > out of top of my head): > > static int misc_res_init(struct misc_res *res, struct misc_ops *ops) > { > if (!misc_ops->alloc) { > pr_err("%s: alloc missing\n", __func__); > return -EINVAL; > } > > if (!misc_ops->free) { > pr_err("%s: free missing\n", __func__); > return -EINVAL; > } > > res->misc_ops = misc_ops; > return 0; > } > > static inline int misc_res_alloc(struct misc_cg *cg, struct misc_res > *res) > { > int ret; > > if (!res->misc_ops) > return 0; > > return res->misc_ops->alloc(cg); > } > > static inline void misc_res_free(struct misc_cg *cg, struct misc_res > *res) > { > int ret; > > if (!res->misc_ops) > return 0; > > return res->misc_ops->alloc(cg); > } > > Now if anything has misc_ops, it will also always have *both* callback, > and nothing half-baked gets in. > > The above loops would be then: > > for (i = 0; i < MISC_CG_RES_TYPES; i++) { > WRITE_ONCE(cg->res[i].max, MAX_NUM); > atomic64_set(&cg->res[i].usage, 0); > ret = misc_res_alloc(&parent_cg->res[i]); > if (ret) > goto alloc_err; > > Cleaner and better guards for state consistency. In 04/12 you need to > then call misc_res_init() instead of direct assignment. > > BR, Jarkko Will combine these with the use of a static operations array suggested by Michal. Thanks Haitao