Skip to content

Commit 2a56a1f

Browse files
htejundavem330
authored andcommitted
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 <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 297dbde commit 2a56a1f

File tree

12 files changed

+76
-38
lines changed

12 files changed

+76
-38
lines changed

include/linux/cgroup-defs.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,40 @@ static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
542542

543543
#endif /* CONFIG_CGROUPS */
544544

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+
545581
#endif /* _LINUX_CGROUP_DEFS_H */

include/net/cls_cgroup.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ static inline u32 task_cls_classid(struct task_struct *p)
4141
return classid;
4242
}
4343

44-
static inline void sock_update_classid(struct sock *sk)
44+
static inline void sock_update_classid(struct sock_cgroup_data *skcd)
4545
{
4646
u32 classid;
4747

4848
classid = task_cls_classid(current);
49-
if (classid != sk->sk_classid)
50-
sk->sk_classid = classid;
49+
sock_cgroup_set_classid(skcd, classid);
5150
}
5251

5352
static inline u32 task_get_classid(const struct sk_buff *skb)
@@ -64,17 +63,17 @@ static inline u32 task_get_classid(const struct sk_buff *skb)
6463
* softirqs always disables bh.
6564
*/
6665
if (in_serving_softirq()) {
67-
/* If there is an sk_classid we'll use that. */
66+
/* If there is an sock_cgroup_classid we'll use that. */
6867
if (!skb->sk)
6968
return 0;
7069

71-
classid = skb->sk->sk_classid;
70+
classid = sock_cgroup_classid(&skb->sk->sk_cgrp_data);
7271
}
7372

7473
return classid;
7574
}
7675
#else /* !CONFIG_CGROUP_NET_CLASSID */
77-
static inline void sock_update_classid(struct sock *sk)
76+
static inline void sock_update_classid(struct sock_cgroup_data *skcd)
7877
{
7978
}
8079

include/net/netprio_cgroup.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ struct netprio_map {
2525
u32 priomap[];
2626
};
2727

28-
void sock_update_netprioidx(struct sock *sk);
29-
3028
static inline u32 task_netprioidx(struct task_struct *p)
3129
{
3230
struct cgroup_subsys_state *css;
@@ -38,13 +36,25 @@ static inline u32 task_netprioidx(struct task_struct *p)
3836
rcu_read_unlock();
3937
return idx;
4038
}
39+
40+
static inline void sock_update_netprioidx(struct sock_cgroup_data *skcd)
41+
{
42+
if (in_interrupt())
43+
return;
44+
45+
sock_cgroup_set_prioidx(skcd, task_netprioidx(current));
46+
}
47+
4148
#else /* !CONFIG_CGROUP_NET_PRIO */
49+
4250
static inline u32 task_netprioidx(struct task_struct *p)
4351
{
4452
return 0;
4553
}
4654

47-
#define sock_update_netprioidx(sk)
55+
static inline void sock_update_netprioidx(struct sock_cgroup_data *skcd)
56+
{
57+
}
4858

4959
#endif /* CONFIG_CGROUP_NET_PRIO */
5060
#endif /* _NET_CLS_CGROUP_H */

include/net/sock.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include <linux/static_key.h>
6060
#include <linux/sched.h>
6161
#include <linux/wait.h>
62+
#include <linux/cgroup-defs.h>
6263

6364
#include <linux/filter.h>
6465
#include <linux/rculist_nulls.h>
@@ -308,8 +309,7 @@ struct cg_proto;
308309
* @sk_send_head: front of stuff to transmit
309310
* @sk_security: used by security modules
310311
* @sk_mark: generic packet mark
311-
* @sk_cgrp_prioidx: socket group's priority map index
312-
* @sk_classid: this socket's cgroup classid
312+
* @sk_cgrp_data: cgroup data for this cgroup
313313
* @sk_cgrp: this socket's cgroup-specific proto data
314314
* @sk_write_pending: a write to stream socket waits to start
315315
* @sk_state_change: callback to indicate change in the state of the sock
@@ -443,12 +443,7 @@ struct sock {
443443
#ifdef CONFIG_SECURITY
444444
void *sk_security;
445445
#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;
452447
struct cg_proto *sk_cgrp;
453448
void (*sk_state_change)(struct sock *sk);
454449
void (*sk_data_ready)(struct sock *sk);

net/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,22 @@ config XPS
250250
depends on SMP
251251
default y
252252

253+
config SOCK_CGROUP_DATA
254+
bool
255+
default n
256+
253257
config CGROUP_NET_PRIO
254258
bool "Network priority cgroup"
255259
depends on CGROUPS
260+
select SOCK_CGROUP_DATA
256261
---help---
257262
Cgroup subsystem for use in assigning processes to network priorities on
258263
a per-interface basis.
259264

260265
config CGROUP_NET_CLASSID
261266
bool "Network classid cgroup"
262267
depends on CGROUPS
268+
select SOCK_CGROUP_DATA
263269
---help---
264270
Cgroup subsystem for use as general purpose socket classid marker that is
265271
being used in cls_cgroup and for netfilter matching.

net/core/dev.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2929,7 +2929,8 @@ static void skb_update_prio(struct sk_buff *skb)
29292929
struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap);
29302930

29312931
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);
29332934

29342935
if (prioidx < map->priomap_len)
29352936
skb->priority = map->priomap[prioidx];

net/core/netclassid_cgroup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ static int update_classid_sock(const void *v, struct file *file, unsigned n)
6262
struct socket *sock = sock_from_file(file, &err);
6363

6464
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);
6767
return 0;
6868
}
6969

net/core/netprio_cgroup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ static int update_netprio(const void *v, struct file *file, unsigned n)
223223
int err;
224224
struct socket *sock = sock_from_file(file, &err);
225225
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);
227228
return 0;
228229
}
229230

net/core/scm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
289289
/* Bump the usage count and install the file. */
290290
sock = sock_from_file(fp[i], &err);
291291
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);
294294
}
295295
fd_install(new_fd, get_file(fp[i]));
296296
}

net/core/sock.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,17 +1393,6 @@ static void sk_prot_free(struct proto *prot, struct sock *sk)
13931393
module_put(owner);
13941394
}
13951395

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-
14071396
/**
14081397
* sk_alloc - All socket objects are allocated here
14091398
* @net: the applicable net namespace
@@ -1432,8 +1421,8 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
14321421
sock_net_set(sk, net);
14331422
atomic_set(&sk->sk_wmem_alloc, 1);
14341423

1435-
sock_update_classid(sk);
1436-
sock_update_netprioidx(sk);
1424+
sock_update_classid(&sk->sk_cgrp_data);
1425+
sock_update_netprioidx(&sk->sk_cgrp_data);
14371426
}
14381427

14391428
return sk;

net/netfilter/nft_meta.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void nft_meta_get_eval(const struct nft_expr *expr,
174174
sk = skb_to_full_sk(skb);
175175
if (!sk || !sk_fullsock(sk))
176176
goto err;
177-
*dest = sk->sk_classid;
177+
*dest = sock_cgroup_classid(&sk->sk_cgrp_data);
178178
break;
179179
#endif
180180
default:

net/netfilter/xt_cgroup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ cgroup_mt(const struct sk_buff *skb, struct xt_action_param *par)
4242
if (skb->sk == NULL || !sk_fullsock(skb->sk))
4343
return false;
4444

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;
4647
}
4748

4849
static struct xt_match cgroup_mt_reg __read_mostly = {

0 commit comments

Comments
 (0)