cgroup: Improve cftype add/rm error handling
PATCH 1/2 cgroup: Improve cftype add/rm error handling
Let's track whether a cftype is currently added or not using a new flag __CFTYPE_ADDED so that duplicate operations can be failed safely and consistently allow using empty cftypes.
---
Hello,
If no one objects, imma apply these two cgroup file handling cleanup patches to cgroup/for-6.1 in a few days.
Thanks.
include/linux/cgroup-defs.h | 1 + kernel/cgroup/cgroup.c | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-)
include/linux/cgroup-defs.h +1 -0
@@ -131,6 +131,7 @@ enum {/* internal flags, do not use outside cgroup core proper */ __CFTYPE_ONLY_ON_DFL = (1 <<16 ),/* only on default hierarchy */ __CFTYPE_NOT_ON_DFL = (1 <<17 ),/* not on default hierarchy */ + __CFTYPE_ADDED = (1 << 18), };/*
kernel/cgroup/cgroup.c +20 -7
@@ -4166,19 +4166,26 @@ static void cgroup_exit_cftypes(struct c cft->ss =NULL ;/* revert flags set by cgroup core while adding @cfts */ - cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL); + cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL | + __CFTYPE_ADDED); } }static int cgroup_init_cftypes (struct cgroup_subsys *ss,struct cftype *cfts) {struct cftype *cft; +int ret =0 ;for (cft = cfts; cft->name[0 ] !='\0' ; cft++) {struct kernfs_ops *kf_ops;WARN_ON (cft->ss || cft->kf_ops); +if (cft->flags & __CFTYPE_ADDED) { + ret = -EBUSY ; +break ; + } +if ((cft->flags &CFTYPE_PRESSURE ) && !cgroup_psi_enabled ())continue ;
@@ -4194,26 +4201,26 @@ static int cgroup_init_cftypes(struct cgif (cft->max_write_len && cft->max_write_len !=PAGE_SIZE ) { kf_ops =kmemdup (kf_ops,sizeof (*kf_ops),GFP_KERNEL );if (!kf_ops) { -cgroup_exit_cftypes (cfts); -return -ENOMEM ; + ret = -ENOMEM ; +break ; } kf_ops->atomic_write_len = cft->max_write_len; } cft->kf_ops = kf_ops; cft->ss = ss; + cft->flags |= __CFTYPE_ADDED; } -return 0 ; +if (ret) +cgroup_exit_cftypes (cfts); +return ret; }static int cgroup_rm_cftypes_locked (struct cftype *cfts) {lockdep_assert_held (&cgroup_mutex); -if (!cfts || !cfts[0 ].ss) -return -ENOENT ; -list_del (&cfts->node);cgroup_apply_cftypes (cfts, false);cgroup_exit_cftypes (cfts);
@@ -4235,6 +4242,12 @@ int cgroup_rm_cftypes(struct cftype *cft {int ret; +if (!cfts || cfts[0 ].name[0 ] =='\0' ) +return 0 ; + +if (!(cfts[0 ].flags & __CFTYPE_ADDED)) +return -ENOENT ; +mutex_lock (&cgroup_mutex); ret =cgroup_rm_cftypes_locked (cfts);mutex_unlock (&cgroup_mutex);
A message that is not in the archive (sent off-list or not yet received): YxUUISLVLEIRBwEY-NiLfg/[email protected]
PATCH 2/2 cgroup: Remove CFTYPE_PRESSURE
CFTYPE_PRESSURE is used to flag PSI related files so that they are not created if PSI is disabled during boot. It's a bit weird to use a generic flag to mark a specific file type. Let's instead move the PSI files into its own cftypes array and add/rm them conditionally. This is a bit more code but cleaner.
No userland visible changes.
--- include/linux/cgroup-defs.h | 1 kernel/cgroup/cgroup.c | 64 +++++++++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 28 deletions(-)
include/linux/cgroup-defs.h +0 -1
@@ -126,7 +126,6 @@ enum {CFTYPE_NO_PREFIX = (1 <<3 ),/* (DON'T USE FOR NEW FILES) no subsys prefix */ CFTYPE_WORLD_WRITABLE = (1 <<4 ),/* (DON'T USE FOR NEW FILES) S_IWUGO */ CFTYPE_DEBUG = (1 <<5 ),/* create when cgroup_debug */ -CFTYPE_PRESSURE = (1 <<6 ),/* only if pressure feature is enabled */ /* internal flags, do not use outside cgroup core proper */ __CFTYPE_ONLY_ON_DFL = (1 << 16), /* only on default hierarchy */
kernel/cgroup/cgroup.c +37 -27
133 lines not on this page · raw message
Hello.
Quoted 2 lines · Tejun Heo
Let's track whether a cftype is currently added or not using a new flag
__CFTYPE_ADDED
IIUC, the flag is equal to (cft->ss || cft->kf_ops), particularly the information is carried in cfs->kf_ops too.
Is the effect of cgroup_init_cftypes proper setup of cft->kf_ops? I.e. isn't it simpler to just check that field (without the new flag)?
(No objection to current form, just asking whether I understand the impact.)
Thanks,
Michal
A message that is not in the archive (sent off-list or not yet received): 20220905131435.GA1765-9OudH3eul5jcvrawFnH+a6VXKuFTiq87@public.gmane.org
Hello,
Quoted 2 lines · Michal Koutný
IIUC, the flag is equal to (
cft->ss||cft->kf_ops), particularly the information is carried incfs->kf_opstoo.
->ss will be NULL for core files even after added. ->kf_ops can be used instead of the flag.
Quoted 5 lines
Is the effect of
cgroup_init_cftypesproper setup ofcft->kf_ops? I.e. isn't it simpler to just check that field (without the new flag)?(No objection to current form, just asking whether I understand the impact.)
I prefer having it as a separate flag because it's explicit and can be tested together with other flags. It's a weak preference tho and I can go either way if it bothers you much.
Thanks.
Signature · 1 line
tejun
A message that is not in the archive (sent off-list or not yet received): YxeCdHfk2nOUISDw-NiLfg/[email protected]
Quoted 3 lines · Tejun Heo
I prefer having it as a separate flag because it's explicit and can be tested together with other flags. It's a weak preference tho and I can go either way if it bothers you much.
No trouble, please proceed with the new flag.
BTW, while I was just looking over the patch I noticed that in
@@ -1717,14 +1722,22 @@ static int css_populate_dir(struct cgrou
return 0;
if (!css->ss) {- if (cgroup_on_dfl(cgrp))
- cfts = cgroup_base_files;
- else
- cfts = cgroup1_base_files;
-
- ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
- if (ret < 0)
- return ret;
+ if (cgroup_on_dfl(cgrp)) {
+ ret = cgroup_addrm_files(&cgrp->self, cgrp,
+ cgroup_base_files, true);
+ if (ret < 0)
+ return ret;
+
+ if (cgroup_psi_enabled()) {
+ ret = cgroup_addrm_files(&cgrp->self, cgrp,
+ cgroup_psi_files, true);
+ if (ret < 0)
+ return ret;
Before the return here, the function should revert the base files first (or silence the return value to 0 if such a partial population is acceptable).
(Actually, it looks like the revert in the subsys branch is unnecessary as callers of css_populate_dir() would issue css_clear_dir() upon failure eventually.)
Thanks,
Michal
A message that is not in the archive (sent off-list or not yet received): 20220906191112.GF30763-9OudH3eul5jcvrawFnH+a6VXKuFTiq87@public.gmane.org
Hello,
Quoted 6 lines · Michal Koutný
Before the return here, the function should revert the base files first (or silence the return value to 0 if such a partial population is acceptable).
(Actually, it looks like the revert in the subsys branch is unnecessary as callers of
css_populate_dir()would issuecss_clear_dir()upon failure eventually.)
Yeah, so, the contract there is a bit unusual in that on failure the helpers don't need to cleanup after themselves as they'll get cleaned up together by the caller when it nukes the cgroup which was being created. While a bit unusual, it's simpler / safer this way, so...
Thanks.
Signature · 1 line
tejun
Quoted 3 lines · Tejun Heo
Let's track whether a cftype is currently added or not using a new flag
__CFTYPE_ADDEDso that duplicate operations can be failed safely and consistently allow using empty cftypes.
Applying 1-2 to cgroup/for-6.1.
Thanks.
Signature · 1 line
tejun