Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: net_cls: move cgroupfs classid handling into core

Zefan Li requested [1] to perform the following cleanup/refactoring:

- Split cgroupfs classid handling into net core to better express a
possible more generic use.

- Disable module support for cgroupfs bits as the majority of other
cgroupfs subsystems do not have that, and seems to be not wished
from cgroup side. Zefan probably might want to follow-up for netprio
later on.

- By this, code can be further reduced which previously took care of
functionality built when compiled as module.

cgroupfs bits are being placed under net/core/netclassid_cgroup.c, so
that we are consistent with {netclassid,netprio}_cgroup naming that is
under net/core/ as suggested by Zefan.

No change in functionality, but only code refactoring that is being
done here.

[1] http://patchwork.ozlabs.org/patch/304825/

Suggested-by: Li Zefan <lizefan@huawei.com>
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Cc: Zefan Li <lizefan@huawei.com>
Cc: Thomas Graf <tgraf@suug.ch>
Cc: cgroups@vger.kernel.org
Acked-by: Li Zefan <lizefan@huawei.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

authored by

Daniel Borkmann and committed by
Pablo Neira Ayuso
fe1217c4 14abfa16

+143 -151
+1 -1
include/linux/cgroup_subsys.h
··· 31 31 SUBSYS(freezer) 32 32 #endif 33 33 34 - #if IS_SUBSYS_ENABLED(CONFIG_NET_CLS_CGROUP) 34 + #if IS_SUBSYS_ENABLED(CONFIG_CGROUP_NET_CLASSID) 35 35 SUBSYS(net_cls) 36 36 #endif 37 37
+12 -28
include/net/cls_cgroup.h
··· 16 16 #include <linux/cgroup.h> 17 17 #include <linux/hardirq.h> 18 18 #include <linux/rcupdate.h> 19 + #include <net/sock.h> 19 20 20 - #if IS_ENABLED(CONFIG_NET_CLS_CGROUP) 21 - struct cgroup_cls_state 22 - { 21 + #ifdef CONFIG_CGROUP_NET_CLASSID 22 + struct cgroup_cls_state { 23 23 struct cgroup_subsys_state css; 24 24 u32 classid; 25 25 }; 26 26 27 - void sock_update_classid(struct sock *sk); 27 + struct cgroup_cls_state *task_cls_state(struct task_struct *p); 28 28 29 - #if IS_BUILTIN(CONFIG_NET_CLS_CGROUP) 30 29 static inline u32 task_cls_classid(struct task_struct *p) 31 30 { 32 31 u32 classid; ··· 40 41 41 42 return classid; 42 43 } 43 - #elif IS_MODULE(CONFIG_NET_CLS_CGROUP) 44 - static inline u32 task_cls_classid(struct task_struct *p) 44 + 45 + static inline void sock_update_classid(struct sock *sk) 45 46 { 46 - struct cgroup_subsys_state *css; 47 - u32 classid = 0; 47 + u32 classid; 48 48 49 - if (in_interrupt()) 50 - return 0; 51 - 52 - rcu_read_lock(); 53 - css = task_css(p, net_cls_subsys_id); 54 - if (css) 55 - classid = container_of(css, 56 - struct cgroup_cls_state, css)->classid; 57 - rcu_read_unlock(); 58 - 59 - return classid; 49 + classid = task_cls_classid(current); 50 + if (classid != sk->sk_classid) 51 + sk->sk_classid = classid; 60 52 } 61 - #endif 62 - #else /* !CGROUP_NET_CLS_CGROUP */ 53 + #else /* !CONFIG_CGROUP_NET_CLASSID */ 63 54 static inline void sock_update_classid(struct sock *sk) 64 55 { 65 56 } 66 - 67 - static inline u32 task_cls_classid(struct task_struct *p) 68 - { 69 - return 0; 70 - } 71 - #endif /* CGROUP_NET_CLS_CGROUP */ 57 + #endif /* CONFIG_CGROUP_NET_CLASSID */ 72 58 #endif /* _NET_CLS_CGROUP_H */
+7
net/Kconfig
··· 245 245 Cgroup subsystem for use in assigning processes to network priorities on 246 246 a per-interface basis 247 247 248 + config CGROUP_NET_CLASSID 249 + boolean "Network classid cgroup" 250 + depends on CGROUPS 251 + ---help--- 252 + Cgroup subsystem for use as general purpose socket classid marker that is 253 + being used in cls_cgroup and for netfilter matching. 254 + 248 255 config NET_RX_BUSY_POLL 249 256 boolean 250 257 default y
+1
net/core/Makefile
··· 22 22 obj-$(CONFIG_NET_DROP_MONITOR) += drop_monitor.o 23 23 obj-$(CONFIG_NETWORK_PHY_TIMESTAMPING) += timestamping.o 24 24 obj-$(CONFIG_NETPRIO_CGROUP) += netprio_cgroup.o 25 + obj-$(CONFIG_CGROUP_NET_CLASSID) += netclassid_cgroup.o
+120
net/core/netclassid_cgroup.c
··· 1 + /* 2 + * net/core/netclassid_cgroup.c Classid Cgroupfs Handling 3 + * 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. 8 + * 9 + * Authors: Thomas Graf <tgraf@suug.ch> 10 + */ 11 + 12 + #include <linux/module.h> 13 + #include <linux/slab.h> 14 + #include <linux/cgroup.h> 15 + #include <linux/fdtable.h> 16 + #include <net/cls_cgroup.h> 17 + #include <net/sock.h> 18 + 19 + static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state *css) 20 + { 21 + return css ? container_of(css, struct cgroup_cls_state, css) : NULL; 22 + } 23 + 24 + struct cgroup_cls_state *task_cls_state(struct task_struct *p) 25 + { 26 + return css_cls_state(task_css(p, net_cls_subsys_id)); 27 + } 28 + EXPORT_SYMBOL_GPL(task_cls_state); 29 + 30 + static struct cgroup_subsys_state * 31 + cgrp_css_alloc(struct cgroup_subsys_state *parent_css) 32 + { 33 + struct cgroup_cls_state *cs; 34 + 35 + cs = kzalloc(sizeof(*cs), GFP_KERNEL); 36 + if (!cs) 37 + return ERR_PTR(-ENOMEM); 38 + 39 + return &cs->css; 40 + } 41 + 42 + static int cgrp_css_online(struct cgroup_subsys_state *css) 43 + { 44 + struct cgroup_cls_state *cs = css_cls_state(css); 45 + struct cgroup_cls_state *parent = css_cls_state(css_parent(css)); 46 + 47 + if (parent) 48 + cs->classid = parent->classid; 49 + 50 + return 0; 51 + } 52 + 53 + static void cgrp_css_free(struct cgroup_subsys_state *css) 54 + { 55 + kfree(css_cls_state(css)); 56 + } 57 + 58 + static int update_classid(const void *v, struct file *file, unsigned n) 59 + { 60 + int err; 61 + struct socket *sock = sock_from_file(file, &err); 62 + 63 + if (sock) 64 + sock->sk->sk_classid = (u32)(unsigned long)v; 65 + 66 + return 0; 67 + } 68 + 69 + static void cgrp_attach(struct cgroup_subsys_state *css, 70 + struct cgroup_taskset *tset) 71 + { 72 + struct cgroup_cls_state *cs = css_cls_state(css); 73 + void *v = (void *)(unsigned long)cs->classid; 74 + struct task_struct *p; 75 + 76 + cgroup_taskset_for_each(p, css, tset) { 77 + task_lock(p); 78 + iterate_fd(p->files, 0, update_classid, v); 79 + task_unlock(p); 80 + } 81 + } 82 + 83 + static u64 read_classid(struct cgroup_subsys_state *css, struct cftype *cft) 84 + { 85 + return css_cls_state(css)->classid; 86 + } 87 + 88 + static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft, 89 + u64 value) 90 + { 91 + css_cls_state(css)->classid = (u32) value; 92 + 93 + return 0; 94 + } 95 + 96 + static struct cftype ss_files[] = { 97 + { 98 + .name = "classid", 99 + .read_u64 = read_classid, 100 + .write_u64 = write_classid, 101 + }, 102 + { } /* terminate */ 103 + }; 104 + 105 + struct cgroup_subsys net_cls_subsys = { 106 + .name = "net_cls", 107 + .css_alloc = cgrp_css_alloc, 108 + .css_online = cgrp_css_online, 109 + .css_free = cgrp_css_free, 110 + .attach = cgrp_attach, 111 + .subsys_id = net_cls_subsys_id, 112 + .base_cftypes = ss_files, 113 + .module = THIS_MODULE, 114 + }; 115 + 116 + static int __init init_netclassid_cgroup(void) 117 + { 118 + return cgroup_load_subsys(&net_cls_subsys); 119 + } 120 + __initcall(init_netclassid_cgroup);
-12
net/core/sock.c
··· 1308 1308 module_put(owner); 1309 1309 } 1310 1310 1311 - #if IS_ENABLED(CONFIG_NET_CLS_CGROUP) 1312 - void sock_update_classid(struct sock *sk) 1313 - { 1314 - u32 classid; 1315 - 1316 - classid = task_cls_classid(current); 1317 - if (classid != sk->sk_classid) 1318 - sk->sk_classid = classid; 1319 - } 1320 - EXPORT_SYMBOL(sock_update_classid); 1321 - #endif 1322 - 1323 1311 #if IS_ENABLED(CONFIG_NETPRIO_CGROUP) 1324 1312 void sock_update_netprioidx(struct sock *sk) 1325 1313 {
+1
net/sched/Kconfig
··· 435 435 config NET_CLS_CGROUP 436 436 tristate "Control Group Classifier" 437 437 select NET_CLS 438 + select CGROUP_NET_CLASSID 438 439 depends on CGROUPS 439 440 ---help--- 440 441 Say Y here if you want to classify packets based on the control
+1 -110
net/sched/cls_cgroup.c
··· 11 11 12 12 #include <linux/module.h> 13 13 #include <linux/slab.h> 14 - #include <linux/types.h> 15 - #include <linux/string.h> 16 - #include <linux/errno.h> 17 14 #include <linux/skbuff.h> 18 - #include <linux/cgroup.h> 19 15 #include <linux/rcupdate.h> 20 - #include <linux/fdtable.h> 21 16 #include <net/rtnetlink.h> 22 17 #include <net/pkt_cls.h> 23 18 #include <net/sock.h> 24 19 #include <net/cls_cgroup.h> 25 - 26 - static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state *css) 27 - { 28 - return css ? container_of(css, struct cgroup_cls_state, css) : NULL; 29 - } 30 - 31 - static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p) 32 - { 33 - return css_cls_state(task_css(p, net_cls_subsys_id)); 34 - } 35 - 36 - static struct cgroup_subsys_state * 37 - cgrp_css_alloc(struct cgroup_subsys_state *parent_css) 38 - { 39 - struct cgroup_cls_state *cs; 40 - 41 - cs = kzalloc(sizeof(*cs), GFP_KERNEL); 42 - if (!cs) 43 - return ERR_PTR(-ENOMEM); 44 - return &cs->css; 45 - } 46 - 47 - static int cgrp_css_online(struct cgroup_subsys_state *css) 48 - { 49 - struct cgroup_cls_state *cs = css_cls_state(css); 50 - struct cgroup_cls_state *parent = css_cls_state(css_parent(css)); 51 - 52 - if (parent) 53 - cs->classid = parent->classid; 54 - return 0; 55 - } 56 - 57 - static void cgrp_css_free(struct cgroup_subsys_state *css) 58 - { 59 - kfree(css_cls_state(css)); 60 - } 61 - 62 - static int update_classid(const void *v, struct file *file, unsigned n) 63 - { 64 - int err; 65 - struct socket *sock = sock_from_file(file, &err); 66 - if (sock) 67 - sock->sk->sk_classid = (u32)(unsigned long)v; 68 - return 0; 69 - } 70 - 71 - static void cgrp_attach(struct cgroup_subsys_state *css, 72 - struct cgroup_taskset *tset) 73 - { 74 - struct task_struct *p; 75 - struct cgroup_cls_state *cs = css_cls_state(css); 76 - void *v = (void *)(unsigned long)cs->classid; 77 - 78 - cgroup_taskset_for_each(p, css, tset) { 79 - task_lock(p); 80 - iterate_fd(p->files, 0, update_classid, v); 81 - task_unlock(p); 82 - } 83 - } 84 - 85 - static u64 read_classid(struct cgroup_subsys_state *css, struct cftype *cft) 86 - { 87 - return css_cls_state(css)->classid; 88 - } 89 - 90 - static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft, 91 - u64 value) 92 - { 93 - css_cls_state(css)->classid = (u32) value; 94 - return 0; 95 - } 96 - 97 - static struct cftype ss_files[] = { 98 - { 99 - .name = "classid", 100 - .read_u64 = read_classid, 101 - .write_u64 = write_classid, 102 - }, 103 - { } /* terminate */ 104 - }; 105 - 106 - struct cgroup_subsys net_cls_subsys = { 107 - .name = "net_cls", 108 - .css_alloc = cgrp_css_alloc, 109 - .css_online = cgrp_css_online, 110 - .css_free = cgrp_css_free, 111 - .attach = cgrp_attach, 112 - .subsys_id = net_cls_subsys_id, 113 - .base_cftypes = ss_files, 114 - .module = THIS_MODULE, 115 - }; 116 20 117 21 struct cls_cgroup_head { 118 22 u32 handle; ··· 213 309 214 310 static int __init init_cgroup_cls(void) 215 311 { 216 - int ret; 217 - 218 - ret = cgroup_load_subsys(&net_cls_subsys); 219 - if (ret) 220 - goto out; 221 - 222 - ret = register_tcf_proto_ops(&cls_cgroup_ops); 223 - if (ret) 224 - cgroup_unload_subsys(&net_cls_subsys); 225 - 226 - out: 227 - return ret; 312 + return register_tcf_proto_ops(&cls_cgroup_ops); 228 313 } 229 314 230 315 static void __exit exit_cgroup_cls(void) 231 316 { 232 317 unregister_tcf_proto_ops(&cls_cgroup_ops); 233 - 234 - cgroup_unload_subsys(&net_cls_subsys); 235 318 } 236 319 237 320 module_init(init_cgroup_cls);