Re: [PATCH bpf-next v3 4/9] bpf: Add log support for cgroup prog link_create
From: Leon Hwang
Date: Tue Nov 24 2026 - 01:27:23 EST
On 24/11/26 00:04, bot+bpf-ci@xxxxxxxxxx wrote:
>> bpf: Add log support for cgroup prog link_create
>
> TL;DR: The new bpf_log() calls in cgroup attach run under cgroup_mutex and
> cgroup_bpf_mutex, so a copy_to_user() fault on the user log buffer may stall
> other cgroup operations. Consider logging after unlock.
>
>> diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
>> --- a/kernel/bpf/cgroup.c
>> +++ b/kernel/bpf/cgroup.c
>
> [ ... ]
>
>> @@ -1045,23 +1060,33 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,
>> atype = bpf_cgroup_atype_find(type, new_prog->aux->attach_btf_id);
>> if (atype < 0)
>> return -EINVAL;
>> - if (revision && revision != cgrp->bpf.revisions[atype])
>> + if (revision && revision != cgrp->bpf.revisions[atype]) {
>> + bpf_log(log, "Cgroup revision mismatch: expected %llu, actual %llu.\n",
>> + revision, cgrp->bpf.revisions[atype]);
>> return -ESTALE;
>> + }
>
> [ ... ]
>
>> @@ -1143,13 +1168,14 @@ static int cgroup_bpf_attach(struct cgroup *cgrp,
>> cgroup_bpf_lock_hierarchy();
>> ret = __cgroup_bpf_attach(cgrp, prog, replace_prog, link, type, flags,
>> - id_or_fd, revision);
>> + id_or_fd, revision, log);
>> cgroup_bpf_unlock_hierarchy();
>> return ret;
>
> Can these bpf_log() calls block other cgroup users while the locks are
> held?
>
> cgroup_bpf_attach() takes cgroup_mutex and cgroup_bpf_mutex through
> cgroup_bpf_lock_hierarchy() before calling __cgroup_bpf_attach(), and
> the new messages are emitted from inside it (and from get_prog_list()
> via insert_pl_to_hlist()):
>
> cgroup_bpf_attach()
> cgroup_bpf_lock_hierarchy()
> __cgroup_bpf_attach()
> bpf_log()
> bpf_verifier_vlog()
> copy_to_user(log->ubuf + ..., log->kbuf, ...)
>
> log->ubuf is the user supplied log_buf, so if that page is not resident
> (swapped out, or backed by FUSE or userfaultfd), copy_to_user() can sleep
> in the fault for as long as the caller chooses. While it sleeps,
> cgroup_mutex and cgroup_bpf_mutex remain held, so cgroup mkdir/rmdir,
> migration, BPF attach/detach, BPF_PROG_QUERY and the cgroup bpf release
> work would all wait.
>
> Commit 40aec6bab02e ("bpf, cgroup: Protect cgroup_bpf state with its own
> mutex") split these paths out from cgroup_mutex to avoid hung tasks, and
> before this patch the attach path did not touch user memory under these
> locks.
>
> This isn't a lock ordering problem, since cgroup_mutex -> mmap_lock
> already exists, but would it be better to record the message and emit it
> from cgroup_bpf_link_attach() after cgroup_bpf_unlock_hierarchy()?
I think it's okay to call bpf_log() inside those locks' critical
section, even though copy_to_user() could unlikely trigger page fault.
I didn't have good idea to address such concern. Probably, in
cgroup_bpf_attach(), prepare a local buffer before
cgroup_bpf_lock_hierarchy(), pass the buffer to __cgroup_bpf_attach() in
order to store the log messages, then call bpf_log() after
cgroup_bpf_unlock_hierarchy() if __cgroup_bpf_attach() returns error.
Thanks,
Leon