History

bpf: decouple the lifetime of cgroup_bpf from cgroup itself

Currently the lifetime of bpf programs attached to a cgroup is bound to the lifetime of the cgroup itself. It means that if a user forgets (or intentionally avoids) to detach a bpf program before removing the cgroup, it will stay attached up to the release of the cgroup. Since the cgroup can stay in the dying state (the state between being rmdir()'ed and being released) for a very long time, it leads to a waste of memory. Also, it blocks a possibility to implement the memcg-based memory accounting for bpf objects, because a circular reference dependency will occur. Charged memory pages are pinning the corresponding memory cgroup, and if the memory cgroup is pinning the attached bpf program, nothing will be ever released. A dying cgroup can not contain any processes, so the only chance for an attached bpf program to be executed is a live socket associated with the cgroup. So in order to release all bpf data early, let's count associated sockets using a new percpu refcounter. On cgroup removal the counter is transitioned to the atomic mode, and as soon as it reaches 0, all bpf programs are detached. Because cgroup_bpf_release() can block, it can't be called from the percpu ref counter callback directly, so instead an asynchronous work is scheduled. The reference counter is not socket specific, and can be used for any other types of programs, which can be executed from a cgroup-bpf hook outside of the process context, had such a need arise in the future.

Roman Gushchin committed 7 years ago

Commit 4bfc0bb1 parent 37b54ae4 files · +72 −9Browse files

Showing the changes to include/linux (2 of 4 files). Show all

Unified · Split

2 files
  1. include/linux/bpf-cgroup.h +9 −2
  2. include/linux/cgroup.h +18 −0
include/linux/bpf-cgroup.h+9−2
@@ -6,6 +6,7 @@ · 5 unmodified lines
66 #include <linux/errno.h>
77 #include <linux/jump_label.h>
88 #include <linux/percpu.h>
9+#include <linux/percpu-refcount.h>
910 #include <linux/rbtree.h>
1011 #include <uapi/linux/bpf.h>
1112
@@ -72,10 +73,16 @@ struct cgroup_bpf { · 60 unmodified lines
7273
7374 /* temp storage for effective prog array used by prog_attach/detach */
7475 struct bpf_prog_array __rcu *inactive;
76+
77+ /* reference counter used to detach bpf programs after cgroup removal */
78+ struct percpu_ref refcnt;
79+
80+ /* cgroup_bpf is released using a work queue */
81+ struct work_struct release_work;
7582 };
7683
77−void cgroup_bpf_put(struct cgroup *cgrp);
7884 int cgroup_bpf_inherit(struct cgroup *cgrp);
85+void cgroup_bpf_offline(struct cgroup *cgrp);
7986
8087 int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
8188 enum bpf_attach_type type, u32 flags);
@@ -283,8 +290,8 @@ int cgroup_bpf_prog_query(const union bpf_attr *attr, · 201 unmodified lines
283290
284291 struct bpf_prog;
285292 struct cgroup_bpf {};
286−static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
287293 static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
294+static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
288295
289296 static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
290297 enum bpf_prog_type ptype,
include/linux/cgroup.h+18−0
@@ -924,4 +924,22 @@ static inline bool cgroup_task_frozen(struct task_struct *task) · 923 unmodified lines
924924
925925 #endif /* !CONFIG_CGROUPS */
926926
927+#ifdef CONFIG_CGROUP_BPF
928+static inline void cgroup_bpf_get(struct cgroup *cgrp)
929+{
930+ percpu_ref_get(&cgrp->bpf.refcnt);
931+}
932+
933+static inline void cgroup_bpf_put(struct cgroup *cgrp)
934+{
935+ percpu_ref_put(&cgrp->bpf.refcnt);
936+}
937+
938+#else /* CONFIG_CGROUP_BPF */
939+
940+static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
941+static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
942+
943+#endif /* CONFIG_CGROUP_BPF */
944+
927945 #endif /* _LINUX_CGROUP_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t