1 From 3b4d888114a5f6c1e848f892a2236db4ccf8345f Mon Sep 17 00:00:00 2001
2 From: Angelo Ruocco <angeloruocco90@gmail.com>
3 Date: Tue, 21 May 2019 10:01:54 +0200
4 Subject: [PATCH] cgroup: let a symlink too be created with a cftype file
6 This commit enables a cftype to have a symlink (of any name) that
7 points to the file associated with the cftype.
9 Signed-off-by: Angelo Ruocco <angeloruocco90@gmail.com>
10 Signed-off-by: Paolo Valente <paolo.valente@linaro.org>
11 Signed-off-by: Jens Axboe <axboe@kernel.dk>
12 (cherry picked from commit 54b7b868e826b294687c439b68ec55fe20cafe5b)
13 Signed-off-by: Li Yang <leoyang.li@nxp.com>
15 include/linux/cgroup-defs.h | 3 +++
16 kernel/cgroup/cgroup.c | 33 +++++++++++++++++++++++++++++----
17 2 files changed, 32 insertions(+), 4 deletions(-)
19 --- a/include/linux/cgroup-defs.h
20 +++ b/include/linux/cgroup-defs.h
21 @@ -106,6 +106,8 @@ enum {
22 CFTYPE_WORLD_WRITABLE = (1 << 4), /* (DON'T USE FOR NEW FILES) S_IWUGO */
23 CFTYPE_DEBUG = (1 << 5), /* create when cgroup_debug */
25 + CFTYPE_SYMLINKED = (1 << 6), /* pointed to by symlink too */
27 /* internal flags, do not use outside cgroup core proper */
28 __CFTYPE_ONLY_ON_DFL = (1 << 16), /* only on default hierarchy */
29 __CFTYPE_NOT_ON_DFL = (1 << 17), /* not on default hierarchy */
30 @@ -544,6 +546,7 @@ struct cftype {
31 * end of cftype array.
33 char name[MAX_CFTYPE_NAME];
34 + char link_name[MAX_CFTYPE_NAME];
35 unsigned long private;
38 --- a/kernel/cgroup/cgroup.c
39 +++ b/kernel/cgroup/cgroup.c
40 @@ -1465,8 +1465,8 @@ struct cgroup *task_cgroup_from_root(str
42 static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
44 -static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
46 +static char *cgroup_fill_name(struct cgroup *cgrp, const struct cftype *cft,
47 + char *buf, bool write_link_name)
49 struct cgroup_subsys *ss = cft->ss;
51 @@ -1476,13 +1476,26 @@ static char *cgroup_file_name(struct cgr
53 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
54 dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
56 + write_link_name ? cft->link_name : cft->name);
58 - strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
59 + strscpy(buf, write_link_name ? cft->link_name : cft->name,
60 + CGROUP_FILE_NAME_MAX);
65 +static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
68 + return cgroup_fill_name(cgrp, cft, buf, false);
71 +static char *cgroup_link_name(struct cgroup *cgrp, const struct cftype *cft,
74 + return cgroup_fill_name(cgrp, cft, buf, true);
78 * cgroup_file_mode - deduce file mode of a control file
79 * @cft: the control file in question
80 @@ -1641,6 +1654,9 @@ static void cgroup_rm_file(struct cgroup
83 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
84 + if (cft->flags & CFTYPE_SYMLINKED)
85 + kernfs_remove_by_name(cgrp->kn,
86 + cgroup_link_name(cgrp, cft, name));
90 @@ -3896,6 +3912,7 @@ static int cgroup_add_file(struct cgroup
92 char name[CGROUP_FILE_NAME_MAX];
93 struct kernfs_node *kn;
94 + struct kernfs_node *kn_link;
95 struct lock_class_key *key = NULL;
98 @@ -3926,6 +3943,14 @@ static int cgroup_add_file(struct cgroup
99 spin_unlock_irq(&cgroup_file_kn_lock);
102 + if (cft->flags & CFTYPE_SYMLINKED) {
103 + kn_link = kernfs_create_link(cgrp->kn,
104 + cgroup_link_name(cgrp, cft, name),
106 + if (IS_ERR(kn_link))
107 + return PTR_ERR(kn_link);