Skip to content

Commit 018d26f

Browse files
zeildavem330
authored andcommitted
cgroup, netclassid: periodically release file_lock on classid updating
In our production environment we have faced with problem that updating classid in cgroup with heavy tasks cause long freeze of the file tables in this tasks. By heavy tasks we understand tasks with many threads and opened sockets (e.g. balancers). This freeze leads to an increase number of client timeouts. This patch implements following logic to fix this issue: аfter iterating 1000 file descriptors file table lock will be released thus providing a time gap for socket creation/deletion. Now update is non atomic and socket may be skipped using calls: dup2(oldfd, newfd); close(oldfd); But this case is not typical. Moreover before this patch skip is possible too by hiding socket fd in unix socket buffer. New sockets will be allocated with updated classid because cgroup state is updated before start of the file descriptors iteration. So in common cases this patch has no side effects. Signed-off-by: Dmitry Yakunin <[email protected]> Reviewed-by: Konstantin Khlebnikov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ce9a418 commit 018d26f

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

net/core/netclassid_cgroup.c

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,60 @@ static void cgrp_css_free(struct cgroup_subsys_state *css)
5353
kfree(css_cls_state(css));
5454
}
5555

56+
/*
57+
* To avoid freezing of sockets creation for tasks with big number of threads
58+
* and opened sockets lets release file_lock every 1000 iterated descriptors.
59+
* New sockets will already have been created with new classid.
60+
*/
61+
62+
struct update_classid_context {
63+
u32 classid;
64+
unsigned int batch;
65+
};
66+
67+
#define UPDATE_CLASSID_BATCH 1000
68+
5669
static int update_classid_sock(const void *v, struct file *file, unsigned n)
5770
{
5871
int err;
72+
struct update_classid_context *ctx = (void *)v;
5973
struct socket *sock = sock_from_file(file, &err);
6074

6175
if (sock) {
6276
spin_lock(&cgroup_sk_update_lock);
63-
sock_cgroup_set_classid(&sock->sk->sk_cgrp_data,
64-
(unsigned long)v);
77+
sock_cgroup_set_classid(&sock->sk->sk_cgrp_data, ctx->classid);
6578
spin_unlock(&cgroup_sk_update_lock);
6679
}
80+
if (--ctx->batch == 0) {
81+
ctx->batch = UPDATE_CLASSID_BATCH;
82+
return n + 1;
83+
}
6784
return 0;
6885
}
6986

87+
static void update_classid_task(struct task_struct *p, u32 classid)
88+
{
89+
struct update_classid_context ctx = {
90+
.classid = classid,
91+
.batch = UPDATE_CLASSID_BATCH
92+
};
93+
unsigned int fd = 0;
94+
95+
do {
96+
task_lock(p);
97+
fd = iterate_fd(p->files, fd, update_classid_sock, &ctx);
98+
task_unlock(p);
99+
cond_resched();
100+
} while (fd);
101+
}
102+
70103
static void cgrp_attach(struct cgroup_taskset *tset)
71104
{
72105
struct cgroup_subsys_state *css;
73106
struct task_struct *p;
74107

75108
cgroup_taskset_for_each(p, css, tset) {
76-
task_lock(p);
77-
iterate_fd(p->files, 0, update_classid_sock,
78-
(void *)(unsigned long)css_cls_state(css)->classid);
79-
task_unlock(p);
109+
update_classid_task(p, css_cls_state(css)->classid);
80110
}
81111
}
82112

@@ -98,10 +128,7 @@ static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft,
98128

99129
css_task_iter_start(css, 0, &it);
100130
while ((p = css_task_iter_next(&it))) {
101-
task_lock(p);
102-
iterate_fd(p->files, 0, update_classid_sock,
103-
(void *)(unsigned long)cs->classid);
104-
task_unlock(p);
131+
update_classid_task(p, cs->classid);
105132
cond_resched();
106133
}
107134
css_task_iter_end(&it);

0 commit comments

Comments
 (0)