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

net: wrap sock->sk_cgrp_prioidx and ->sk_classid inside a struct

Introduce sock->sk_cgrp_data which is a struct sock_cgroup_data.
->sk_cgroup_prioidx and ->sk_classid are moved into it. The struct
and its accessors are defined in cgroup-defs.h. This is to prepare
for overloading the fields with a cgroup pointer.

This patch mostly performs equivalent conversions but the followings
are noteworthy.

* Equality test before updating classid is removed from
sock_update_classid(). This shouldn't make any noticeable
difference and a similar test will be implemented on the helper side
later.

* sock_update_netprioidx() now takes struct sock_cgroup_data and can
be moved to netprio_cgroup.h without causing include dependency
loop. Moved.

* The dummy version of sock_update_netprioidx() converted to a static
inline function while at it.

Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Tejun Heo and committed by
David S. Miller
2a56a1fe 297dbde1

+76 -38
+36
include/linux/cgroup-defs.h
··· 542 542 543 543 #endif /* CONFIG_CGROUPS */ 544 544 545 + #ifdef CONFIG_SOCK_CGROUP_DATA 546 + 547 + struct sock_cgroup_data { 548 + u16 prioidx; 549 + u32 classid; 550 + }; 551 + 552 + static inline u16 sock_cgroup_prioidx(struct sock_cgroup_data *skcd) 553 + { 554 + return skcd->prioidx; 555 + } 556 + 557 + static inline u32 sock_cgroup_classid(struct sock_cgroup_data *skcd) 558 + { 559 + return skcd->classid; 560 + } 561 + 562 + static inline void sock_cgroup_set_prioidx(struct sock_cgroup_data *skcd, 563 + u16 prioidx) 564 + { 565 + skcd->prioidx = prioidx; 566 + } 567 + 568 + static inline void sock_cgroup_set_classid(struct sock_cgroup_data *skcd, 569 + u32 classid) 570 + { 571 + skcd->classid = classid; 572 + } 573 + 574 + #else /* CONFIG_SOCK_CGROUP_DATA */ 575 + 576 + struct sock_cgroup_data { 577 + }; 578 + 579 + #endif /* CONFIG_SOCK_CGROUP_DATA */ 580 + 545 581 #endif /* _LINUX_CGROUP_DEFS_H */
+5 -6
include/net/cls_cgroup.h
··· 41 41 return classid; 42 42 } 43 43 44 - static inline void sock_update_classid(struct sock *sk) 44 + static inline void sock_update_classid(struct sock_cgroup_data *skcd) 45 45 { 46 46 u32 classid; 47 47 48 48 classid = task_cls_classid(current); 49 - if (classid != sk->sk_classid) 50 - sk->sk_classid = classid; 49 + sock_cgroup_set_classid(skcd, classid); 51 50 } 52 51 53 52 static inline u32 task_get_classid(const struct sk_buff *skb) ··· 63 64 * softirqs always disables bh. 64 65 */ 65 66 if (in_serving_softirq()) { 66 - /* If there is an sk_classid we'll use that. */ 67 + /* If there is an sock_cgroup_classid we'll use that. */ 67 68 if (!skb->sk) 68 69 return 0; 69 70 70 - classid = skb->sk->sk_classid; 71 + classid = sock_cgroup_classid(&skb->sk->sk_cgrp_data); 71 72 } 72 73 73 74 return classid; 74 75 } 75 76 #else /* !CONFIG_CGROUP_NET_CLASSID */ 76 - static inline void sock_update_classid(struct sock *sk) 77 + static inline void sock_update_classid(struct sock_cgroup_data *skcd) 77 78 { 78 79 } 79 80
+13 -3
include/net/netprio_cgroup.h
··· 25 25 u32 priomap[]; 26 26 }; 27 27 28 - void sock_update_netprioidx(struct sock *sk); 29 - 30 28 static inline u32 task_netprioidx(struct task_struct *p) 31 29 { 32 30 struct cgroup_subsys_state *css; ··· 36 38 rcu_read_unlock(); 37 39 return idx; 38 40 } 41 + 42 + static inline void sock_update_netprioidx(struct sock_cgroup_data *skcd) 43 + { 44 + if (in_interrupt()) 45 + return; 46 + 47 + sock_cgroup_set_prioidx(skcd, task_netprioidx(current)); 48 + } 49 + 39 50 #else /* !CONFIG_CGROUP_NET_PRIO */ 51 + 40 52 static inline u32 task_netprioidx(struct task_struct *p) 41 53 { 42 54 return 0; 43 55 } 44 56 45 - #define sock_update_netprioidx(sk) 57 + static inline void sock_update_netprioidx(struct sock_cgroup_data *skcd) 58 + { 59 + } 46 60 47 61 #endif /* CONFIG_CGROUP_NET_PRIO */ 48 62 #endif /* _NET_CLS_CGROUP_H */
+3 -8
include/net/sock.h
··· 59 59 #include <linux/static_key.h> 60 60 #include <linux/sched.h> 61 61 #include <linux/wait.h> 62 + #include <linux/cgroup-defs.h> 62 63 63 64 #include <linux/filter.h> 64 65 #include <linux/rculist_nulls.h> ··· 309 308 * @sk_send_head: front of stuff to transmit 310 309 * @sk_security: used by security modules 311 310 * @sk_mark: generic packet mark 312 - * @sk_cgrp_prioidx: socket group's priority map index 313 - * @sk_classid: this socket's cgroup classid 311 + * @sk_cgrp_data: cgroup data for this cgroup 314 312 * @sk_cgrp: this socket's cgroup-specific proto data 315 313 * @sk_write_pending: a write to stream socket waits to start 316 314 * @sk_state_change: callback to indicate change in the state of the sock ··· 443 443 #ifdef CONFIG_SECURITY 444 444 void *sk_security; 445 445 #endif 446 - #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) 447 - u16 sk_cgrp_prioidx; 448 - #endif 449 - #ifdef CONFIG_CGROUP_NET_CLASSID 450 - u32 sk_classid; 451 - #endif 446 + struct sock_cgroup_data sk_cgrp_data; 452 447 struct cg_proto *sk_cgrp; 453 448 void (*sk_state_change)(struct sock *sk); 454 449 void (*sk_data_ready)(struct sock *sk);
+6
net/Kconfig
··· 250 250 depends on SMP 251 251 default y 252 252 253 + config SOCK_CGROUP_DATA 254 + bool 255 + default n 256 + 253 257 config CGROUP_NET_PRIO 254 258 bool "Network priority cgroup" 255 259 depends on CGROUPS 260 + select SOCK_CGROUP_DATA 256 261 ---help--- 257 262 Cgroup subsystem for use in assigning processes to network priorities on 258 263 a per-interface basis. ··· 265 260 config CGROUP_NET_CLASSID 266 261 bool "Network classid cgroup" 267 262 depends on CGROUPS 263 + select SOCK_CGROUP_DATA 268 264 ---help--- 269 265 Cgroup subsystem for use as general purpose socket classid marker that is 270 266 being used in cls_cgroup and for netfilter matching.
+2 -1
net/core/dev.c
··· 2929 2929 struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap); 2930 2930 2931 2931 if (!skb->priority && skb->sk && map) { 2932 - unsigned int prioidx = skb->sk->sk_cgrp_prioidx; 2932 + unsigned int prioidx = 2933 + sock_cgroup_prioidx(&skb->sk->sk_cgrp_data); 2933 2934 2934 2935 if (prioidx < map->priomap_len) 2935 2936 skb->priority = map->priomap[prioidx];
+2 -2
net/core/netclassid_cgroup.c
··· 62 62 struct socket *sock = sock_from_file(file, &err); 63 63 64 64 if (sock) 65 - sock->sk->sk_classid = (u32)(unsigned long)v; 66 - 65 + sock_cgroup_set_classid(&sock->sk->sk_cgrp_data, 66 + (unsigned long)v); 67 67 return 0; 68 68 } 69 69
+2 -1
net/core/netprio_cgroup.c
··· 223 223 int err; 224 224 struct socket *sock = sock_from_file(file, &err); 225 225 if (sock) 226 - sock->sk->sk_cgrp_prioidx = (u32)(unsigned long)v; 226 + sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data, 227 + (unsigned long)v); 227 228 return 0; 228 229 } 229 230
+2 -2
net/core/scm.c
··· 289 289 /* Bump the usage count and install the file. */ 290 290 sock = sock_from_file(fp[i], &err); 291 291 if (sock) { 292 - sock_update_netprioidx(sock->sk); 293 - sock_update_classid(sock->sk); 292 + sock_update_netprioidx(&sock->sk->sk_cgrp_data); 293 + sock_update_classid(&sock->sk->sk_cgrp_data); 294 294 } 295 295 fd_install(new_fd, get_file(fp[i])); 296 296 }
+2 -13
net/core/sock.c
··· 1393 1393 module_put(owner); 1394 1394 } 1395 1395 1396 - #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) 1397 - void sock_update_netprioidx(struct sock *sk) 1398 - { 1399 - if (in_interrupt()) 1400 - return; 1401 - 1402 - sk->sk_cgrp_prioidx = task_netprioidx(current); 1403 - } 1404 - EXPORT_SYMBOL_GPL(sock_update_netprioidx); 1405 - #endif 1406 - 1407 1396 /** 1408 1397 * sk_alloc - All socket objects are allocated here 1409 1398 * @net: the applicable net namespace ··· 1421 1432 sock_net_set(sk, net); 1422 1433 atomic_set(&sk->sk_wmem_alloc, 1); 1423 1434 1424 - sock_update_classid(sk); 1425 - sock_update_netprioidx(sk); 1435 + sock_update_classid(&sk->sk_cgrp_data); 1436 + sock_update_netprioidx(&sk->sk_cgrp_data); 1426 1437 } 1427 1438 1428 1439 return sk;
+1 -1
net/netfilter/nft_meta.c
··· 174 174 sk = skb_to_full_sk(skb); 175 175 if (!sk || !sk_fullsock(sk)) 176 176 goto err; 177 - *dest = sk->sk_classid; 177 + *dest = sock_cgroup_classid(&sk->sk_cgrp_data); 178 178 break; 179 179 #endif 180 180 default:
+2 -1
net/netfilter/xt_cgroup.c
··· 42 42 if (skb->sk == NULL || !sk_fullsock(skb->sk)) 43 43 return false; 44 44 45 - return (info->id == skb->sk->sk_classid) ^ info->invert; 45 + return (info->id == sock_cgroup_classid(&skb->sk->sk_cgrp_data)) ^ 46 + info->invert; 46 47 } 47 48 48 49 static struct xt_match cgroup_mt_reg __read_mostly = {