38f34ed2c004200fe71ed4bdeb75856743e990f9
[openwrt/staging/dangole.git] /
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
5
6 This commit enables a cftype to have a symlink (of any name) that
7 points to the file associated with the cftype.
8
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>
14 ---
15 include/linux/cgroup-defs.h | 3 +++
16 kernel/cgroup/cgroup.c | 33 +++++++++++++++++++++++++++++----
17 2 files changed, 32 insertions(+), 4 deletions(-)
18
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 */
24
25 + CFTYPE_SYMLINKED = (1 << 6), /* pointed to by symlink too */
26 +
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.
32 */
33 char name[MAX_CFTYPE_NAME];
34 + char link_name[MAX_CFTYPE_NAME];
35 unsigned long private;
36
37 /*
38 --- a/kernel/cgroup/cgroup.c
39 +++ b/kernel/cgroup/cgroup.c
40 @@ -1465,8 +1465,8 @@ struct cgroup *task_cgroup_from_root(str
41
42 static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
43
44 -static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
45 - char *buf)
46 +static char *cgroup_fill_name(struct cgroup *cgrp, const struct cftype *cft,
47 + char *buf, bool write_link_name)
48 {
49 struct cgroup_subsys *ss = cft->ss;
50
51 @@ -1476,13 +1476,26 @@ static char *cgroup_file_name(struct cgr
52
53 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
54 dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
55 - cft->name);
56 + write_link_name ? cft->link_name : cft->name);
57 } else {
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);
61 }
62 return buf;
63 }
64
65 +static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
66 + char *buf)
67 +{
68 + return cgroup_fill_name(cgrp, cft, buf, false);
69 +}
70 +
71 +static char *cgroup_link_name(struct cgroup *cgrp, const struct cftype *cft,
72 + char *buf)
73 +{
74 + return cgroup_fill_name(cgrp, cft, buf, true);
75 +}
76 +
77 /**
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
81 }
82
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));
87 }
88
89 /**
90 @@ -3871,6 +3887,7 @@ static int cgroup_add_file(struct cgroup
91 {
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;
96 int ret;
97
98 @@ -3901,6 +3918,14 @@ static int cgroup_add_file(struct cgroup
99 spin_unlock_irq(&cgroup_file_kn_lock);
100 }
101
102 + if (cft->flags & CFTYPE_SYMLINKED) {
103 + kn_link = kernfs_create_link(cgrp->kn,
104 + cgroup_link_name(cgrp, cft, name),
105 + kn);
106 + if (IS_ERR(kn_link))
107 + return PTR_ERR(kn_link);
108 + }
109 +
110 return 0;
111 }
112