2 * net/core/netclassid_cgroup.c Classid Cgroupfs Handling
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Thomas Graf <tgraf@suug.ch>
12 #include <linux/slab.h>
13 #include <linux/cgroup.h>
14 #include <linux/fdtable.h>
15 #include <linux/sched/task.h>
17 #include <net/cls_cgroup.h>
20 static inline struct cgroup_cls_state
*css_cls_state(struct cgroup_subsys_state
*css
)
22 return css
? container_of(css
, struct cgroup_cls_state
, css
) : NULL
;
25 struct cgroup_cls_state
*task_cls_state(struct task_struct
*p
)
27 return css_cls_state(task_css_check(p
, net_cls_cgrp_id
,
28 rcu_read_lock_bh_held()));
30 EXPORT_SYMBOL_GPL(task_cls_state
);
32 static struct cgroup_subsys_state
*
33 cgrp_css_alloc(struct cgroup_subsys_state
*parent_css
)
35 struct cgroup_cls_state
*cs
;
37 cs
= kzalloc(sizeof(*cs
), GFP_KERNEL
);
39 return ERR_PTR(-ENOMEM
);
44 static int cgrp_css_online(struct cgroup_subsys_state
*css
)
46 struct cgroup_cls_state
*cs
= css_cls_state(css
);
47 struct cgroup_cls_state
*parent
= css_cls_state(css
->parent
);
50 cs
->classid
= parent
->classid
;
55 static void cgrp_css_free(struct cgroup_subsys_state
*css
)
57 kfree(css_cls_state(css
));
60 static int update_classid_sock(const void *v
, struct file
*file
, unsigned n
)
63 struct socket
*sock
= sock_from_file(file
, &err
);
66 spin_lock(&cgroup_sk_update_lock
);
67 sock_cgroup_set_classid(&sock
->sk
->sk_cgrp_data
,
69 spin_unlock(&cgroup_sk_update_lock
);
74 static void cgrp_attach(struct cgroup_taskset
*tset
)
76 struct cgroup_subsys_state
*css
;
77 struct task_struct
*p
;
79 cgroup_taskset_for_each(p
, css
, tset
) {
81 iterate_fd(p
->files
, 0, update_classid_sock
,
82 (void *)(unsigned long)css_cls_state(css
)->classid
);
87 static u64
read_classid(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
89 return css_cls_state(css
)->classid
;
92 static int write_classid(struct cgroup_subsys_state
*css
, struct cftype
*cft
,
95 struct cgroup_cls_state
*cs
= css_cls_state(css
);
96 struct css_task_iter it
;
97 struct task_struct
*p
;
99 cgroup_sk_alloc_disable();
101 cs
->classid
= (u32
)value
;
103 css_task_iter_start(css
, 0, &it
);
104 while ((p
= css_task_iter_next(&it
))) {
106 iterate_fd(p
->files
, 0, update_classid_sock
,
107 (void *)(unsigned long)cs
->classid
);
110 css_task_iter_end(&it
);
115 static struct cftype ss_files
[] = {
118 .read_u64
= read_classid
,
119 .write_u64
= write_classid
,
124 struct cgroup_subsys net_cls_cgrp_subsys
= {
125 .css_alloc
= cgrp_css_alloc
,
126 .css_online
= cgrp_css_online
,
127 .css_free
= cgrp_css_free
,
128 .attach
= cgrp_attach
,
129 .legacy_cftypes
= ss_files
,