cgroup: export list of delegatable control files using sysfs
PATCH 1/2 cgroup: export list of delegatable control files using sysfs
Delegatable cgroup v2 control files may require special handling (e.g. chowning), and the exact list of such files varies between kernel versions (and likely to be extended in the future).
To guarantee correctness of this list and simplify the life of userspace (systemd, first of all), let's export the list via /sys/kernel/cgroup/delegates pseudo-file.
Format is siple: each control file name is printed on a new line. Example:
$ cat /sys/kernel/cgroup/delegates cgroup.procs cgroup.subtree_control
--- kernel/cgroup/cgroup.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
kernel/cgroup/cgroup.c +61 -0
@@ -5832,3 +5832,64 @@ int cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog,return ret; }#endif /* CONFIG_CGROUP_BPF */ + +#ifdef CONFIG_SYSFS +static ssize_t show_delegatable_files (struct cftype *files,char *buf, +ssize_t size,const char *prefix) +{ +struct cftype *cft; +ssize_t ret =0 ; + +for (cft = files; cft && cft->name[0 ] !='\0' ; cft++) { +if (!(cft->flags &CFTYPE_NS_DELEGATABLE )) +continue ; + +if (prefix) + ret +=snprintf (buf + ret, size - ret,"%s." , prefix); + + ret +=snprintf (buf + ret, size - ret,"%s\n" , cft->name); + +if (unlikely (ret >= size)) { +WARN_ON (1 ); +break ; + } + } + +return ret; +} + +static ssize_t delegates_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf) +{ +struct cgroup_subsys *ss; +int ssid; +ssize_t ret =0 ; + + ret =show_delegatable_files (cgroup_base_files, buf,PAGE_SIZE - ret, +NULL ); + +for_each_subsys (ss, ssid) + ret +=show_delegatable_files (ss->dfl_cftypes, buf + ret, +PAGE_SIZE - ret, + cgroup_subsys_name[ssid]); + +return ret; +} +static struct kobj_attribute cgroup_delegates_attr =__ATTR_RO (delegates); + +static struct attribute *cgroup_sysfs_attrs[] = { + &cgroup_delegates_attr.attr, +NULL , +}; + +static const struct attribute_group cgroup_sysfs_attr_group = { + .attrs = cgroup_sysfs_attrs, + .name ="cgroup" , +}; + +static int __init cgroup_sysfs_init (void ) +{ +return sysfs_create_group (kernel_kobj, &cgroup_sysfs_attr_group); +} +subsys_initcall (cgroup_sysfs_init); +#endif /* CONFIG_SYSFS */
Signature · 1 line
2.13.6
PATCH 2/2 cgroup: export list of cgroups v2 features using sysfs
The active development of cgroups v2 sometimes leads to a creation of interfaces, which are not turned on by default (to provide backward compatibility). It's handy to know from userspace, which cgroup v2 features are supported without calculating it based on the kernel version. So, let's export the list of such features using /sys/kernel/cgroup/features pseudo-file.
The list is hardcoded and has to be extended when new functionality is added. Each feature is printed on a new line.
Example:
$ cat /sys/kernel/cgroup/features nsdelegate
--- kernel/cgroup/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/cgroup/cgroup.c +8 -0
@@ -5877,8 +5877,16 @@ static ssize_t delegates_show(struct kobject *kobj, struct kobj_attribute *attr, }static struct kobj_attribute cgroup_delegates_attr =__ATTR_RO (delegates); +static ssize_t features_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf) +{ +return snprintf (buf,PAGE_SIZE ,"nsdelegate\n" ); +} +static struct kobj_attribute cgroup_features_attr =__ATTR_RO (features); +static struct attribute *cgroup_sysfs_attrs[] = { &cgroup_delegates_attr.attr, + &cgroup_features_attr.attr,NULL , };
Signature · 1 line
2.13.6
Tejun Heo
maintainerQuoted 11 lines · Roman Gushchin
Delegatable cgroup v2 control files may require special handling (e.g. chowning), and the exact list of such files varies between kernel versions (and likely to be extended in the future).
To guarantee correctness of this list and simplify the life of userspace (systemd, first of all), let's export the list via
/sys/kernel/cgroup/delegatespseudo-file.Format is siple: each control file name is printed on a new line. Example:
$ cat /sys/kernel/cgroup/delegates
Generally looks good to me but I'm not sure whether "delegates" is the right name for the file. According to dictionary, it doesn't seem to match too well to what we wanna mean here, which is "these files belong to the child and should be transferred to the child when delegating the cgroup".
Thanks.
Signature · 1 line
tejun
Tejun Heo
maintainerHello, Roman.
Quoted 13 lines · Tejun Heo
Quoted 11 lines · Roman Gushchin
Delegatable cgroup v2 control files may require special handling (e.g. chowning), and the exact list of such files varies between kernel versions (and likely to be extended in the future).
To guarantee correctness of this list and simplify the life of userspace (systemd, first of all), let's export the list via
/sys/kernel/cgroup/delegatespseudo-file.Format is siple: each control file name is printed on a new line. Example:
$ cat /sys/kernel/cgroup/delegates
Quoted 5 lines
Generally looks good to me but I'm not sure whether "delegates" is the right name for the file. According to dictionary, it doesn't seem to match too well to what we wanna mean here, which is "these files belong to the child and should be transferred to the child when delegating the cgroup".
So, "delegates" as the plural noun means people who represent. Let's go with "delegate" which can be read as a verb term meaning "delegate these files together".
Thanks.
Signature · 1 line
tejun
PATCH v2 1/2 cgroup: export list of delegatable control files using sysfs
Delegatable cgroup v2 control files may require special handling (e.g. chowning), and the exact list of such files varies between kernel versions (and likely to be extended in the future).
To guarantee correctness of this list and simplify the life of userspace (systemd, first of all), let's export the list via /sys/kernel/cgroup/delegate pseudo-file.
Format is siple: each control file name is printed on a new line. Example:
$ cat /sys/kernel/cgroup/delegate cgroup.procs cgroup.subtree_control
--- kernel/cgroup/cgroup.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
kernel/cgroup/cgroup.c +61 -0
@@ -5832,3 +5832,64 @@ int cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog,return ret; }#endif /* CONFIG_CGROUP_BPF */ + +#ifdef CONFIG_SYSFS +static ssize_t show_delegatable_files (struct cftype *files,char *buf, +ssize_t size,const char *prefix) +{ +struct cftype *cft; +ssize_t ret =0 ; + +for (cft = files; cft && cft->name[0 ] !='\0' ; cft++) { +if (!(cft->flags &CFTYPE_NS_DELEGATABLE )) +continue ; + +if (prefix) + ret +=snprintf (buf + ret, size - ret,"%s." , prefix); + + ret +=snprintf (buf + ret, size - ret,"%s\n" , cft->name); + +if (unlikely (ret >= size)) { +WARN_ON (1 ); +break ; + } + } + +return ret; +} + +static ssize_t delegate_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf) +{ +struct cgroup_subsys *ss; +int ssid; +ssize_t ret =0 ; + + ret =show_delegatable_files (cgroup_base_files, buf,PAGE_SIZE - ret, +NULL ); + +for_each_subsys (ss, ssid) + ret +=show_delegatable_files (ss->dfl_cftypes, buf + ret, +PAGE_SIZE - ret, + cgroup_subsys_name[ssid]); + +return ret; +} +static struct kobj_attribute cgroup_delegate_attr =__ATTR_RO (delegate); + +static struct attribute *cgroup_sysfs_attrs[] = { + &cgroup_delegate_attr.attr, +NULL , +}; + +static const struct attribute_group cgroup_sysfs_attr_group = { + .attrs = cgroup_sysfs_attrs, + .name ="cgroup" , +}; + +static int __init cgroup_sysfs_init (void ) +{ +return sysfs_create_group (kernel_kobj, &cgroup_sysfs_attr_group); +} +subsys_initcall (cgroup_sysfs_init); +#endif /* CONFIG_SYSFS */
Signature · 1 line
2.13.6
PATCH v2 2/2 cgroup: export list of cgroups v2 features using sysfs
The active development of cgroups v2 sometimes leads to a creation of interfaces, which are not turned on by default (to provide backward compatibility). It's handy to know from userspace, which cgroup v2 features are supported without calculating it based on the kernel version. So, let's export the list of such features using /sys/kernel/cgroup/features pseudo-file.
The list is hardcoded and has to be extended when new functionality is added. Each feature is printed on a new line.
Example:
$ cat /sys/kernel/cgroup/features nsdelegate
--- kernel/cgroup/cgroup.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/cgroup/cgroup.c +8 -0
@@ -5877,8 +5877,16 @@ static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr, }static struct kobj_attribute cgroup_delegate_attr =__ATTR_RO (delegate); +static ssize_t features_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf) +{ +return snprintf (buf,PAGE_SIZE ,"nsdelegate\n" ); +} +static struct kobj_attribute cgroup_features_attr =__ATTR_RO (features); +static struct attribute *cgroup_sysfs_attrs[] = { &cgroup_delegate_attr.attr, + &cgroup_features_attr.attr,NULL , };
Signature · 1 line
2.13.6
Tejun Heo
maintainerRe: [PATCH v2 2/2] cgroup: export list of cgroups v2 features using sysfs
Quoted 10 lines · Roman Gushchin
The active development of cgroups v2 sometimes leads to a creation of interfaces, which are not turned on by default (to provide backward compatibility). It's handy to know from userspace, which cgroup v2 features are supported without calculating it based on the kernel version. So, let's export the list of such features using
/sys/kernel/cgroup/featurespseudo-file.The list is hardcoded and has to be extended when new functionality is added. Each feature is printed on a new line.
Quoted 7 lines
Example:
$ cat /sys/kernel/cgroup/features nsdelegateSigned-off-by Roman Gushchin <[email protected]>Cc Tejun Heo <[email protected]>
Applied 1-2 to cgroup/for-4.15.
Thanks.
Signature · 1 line
tejun