summaryrefslogtreecommitdiff
path: root/include/linux
diff options
context:
space:
mode:
authorShakeel Butt <shakeel.butt@linux.dev>2026-11-12 09:27:15 -0800
committerAlexei Starovoitov <ast@kernel.org>2026-11-14 18:57:57 +0000
commit40aec6bab02e0977f75a87d466005f6492987848 (patch)
treeadedc34a422985088f7ed1d17b9e00895cf7956f /include/linux
parentb2e007371909ed9f88ff7227e0353f6bb88387e5 (diff)
bpf, cgroup: Protect cgroup_bpf state with its own mutex
In the Meta fleet, tasks that only read or free cgroup bpf state often wait minutes for cgroup_mutex: - In one day, hung-task reports (blocked over 120 seconds) showed this for BPF_PROG_QUERY on 1,075 hosts, the cgroup bpf release work on 458 hosts and link info on 94 hosts. - The query callers are a container manager's health checks. Over 30 days, 6,700 hosts reported one hung. The typical wait is 11 minutes, the longest 75 minutes. - The cgroup_mutex holder is almost always a cgroup mkdir or rmdir that is waiting for kernfs_rwsem. Only attach, detach and replace need cgroup_mutex. They update the effective arrays of all descendants, so the hierarchy must not change. Add cgroup_bpf_mutex for the bpf state. Attach, detach and replace take cgroup_mutex and then cgroup_bpf_mutex. Everything else (query, link info, the release work, dropping an LSM attach type, freeing a cgroup storage map) takes only cgroup_bpf_mutex. cgroup_bpf_inherit() still runs at mkdir with only cgroup_mutex, so mkdir never waits for cgroup_bpf_mutex. It writes the new cgroup, which no one else can see yet. What it reads from the ancestors changes only under cgroup_mutex, and the release work can't run for them because they are online. The release work no longer waits for cgroup_mutex, so attach must check that the cgroup is still alive. The previous patch does that. With a test delay that makes a mkdir hold cgroup_mutex for 5 seconds, BPF_PROG_QUERY and link info now return in under a millisecond instead of 4.6 seconds. Attach and detach still wait. Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://patch.msgid.link/20261112172721.440275-4-shakeel.butt@linux.dev
Diffstat (limited to 'include/linux')
-rw-r--r--include/linux/bpf-cgroup-defs.h1
-rw-r--r--include/linux/bpf-cgroup.h3
2 files changed, 4 insertions, 0 deletions
diff --git a/include/linux/bpf-cgroup-defs.h b/include/linux/bpf-cgroup-defs.h
index 0147b8bec973..671f27ba6a62 100644
--- a/include/linux/bpf-cgroup-defs.h
+++ b/include/linux/bpf-cgroup-defs.h
@@ -53,6 +53,7 @@ enum cgroup_bpf_attach_type {
MAX_CGROUP_BPF_ATTACH_TYPE
};
+/* Locking: see cgroup_bpf_mutex in kernel/bpf/cgroup.c */
struct cgroup_bpf {
/* array of effective progs in this cgroup */
struct bpf_prog_array __rcu *effective[MAX_CGROUP_BPF_ATTACH_TYPE];
diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
index 02ef7f899b3d..dfa7a75507c9 100644
--- a/include/linux/bpf-cgroup.h
+++ b/include/linux/bpf-cgroup.h
@@ -421,6 +421,9 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,
__ret; \
})
+/* Take cgroup_mutex first if you need both. See kernel/bpf/cgroup.c. */
+void cgroup_bpf_lock(void);
+void cgroup_bpf_unlock(void);
int cgroup_bpf_prog_attach(const union bpf_attr *attr,
enum bpf_prog_type ptype, struct bpf_prog *prog);
int cgroup_bpf_prog_detach(const union bpf_attr *attr,