Re: [PATCH bpf-next v4 02/17] mm, bpf: introduce BPF OOM struct ops

From: bot+bpf-ci

Date: Wed Dec 02 2026 - 16:59:17 EST


> mm, bpf: introduce BPF OOM struct ops

TL;DR: BPF OOM handlers run under oom_lock but may still call sleepable
mm-accessing copy_from_user kfuncs, which look able to deadlock (on mmap_lock
or oom_lock); a kfunc filter or non-sleepable handlers may be needed.

> diff --git a/mm/bpf_oom.c b/mm/bpf_oom.c
> new file mode 100644
> --- /dev/null
> +++ b/mm/bpf_oom.c

[ ... ]

> +static int bpf_ops_handle_oom(struct bpf_oom_ops *bpf_oom_ops,
> + struct cgroup *cgrp,
> + struct oom_control *oc)
> +{
> + int ret;
> +
> + oc->bpf_handler_name = &bpf_oom_ops->name[0];
> + pagefault_disable();
> + ret = bpf_oom_ops->handle_out_of_memory(oc, cgrp);
> + pagefault_enable();

[ ... ]

> +static const struct bpf_func_proto *
> +bpf_oom_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
> +{
> + switch (func_id) {
> + case BPF_FUNC_copy_from_user:
> + case BPF_FUNC_copy_from_user_task:
> + case BPF_FUNC_get_task_stack:
> + return NULL;
> + default:
> + return bpf_base_func_proto(func_id, prog);
> + }
> +}

The helpers that can fault in or lock another task's mm are hidden here, and
the handler runs under pagefault_disable() with oom_lock held. Can the
kfunc versions of the same operations still be reached?

common_btf_ids is registered for BPF_PROG_TYPE_UNSPEC in kfunc_init(), so
every struct_ops program can call bpf_copy_from_user_task_str(),
bpf_copy_from_user_mm(), bpf_copy_from_user_mm_str(),
bpf_copy_from_user_task_dynptr() and bpf_copy_from_user_task_str_dynptr().
All are KF_SLEEPABLE | KF_PERFMON.

Sleepable handlers are accepted because bpf_oom_ops_check_member() exists
(check_struct_ops_btf_id() rejects prog->sleepable only when
st_ops->check_member is NULL). Later patches in the series need sleepable
handlers in practice, since bpf_oom_kill_process() is KF_SLEEPABLE. A
trusted task argument can come from bpf_task_from_pid().

These kfuncs go through __copy_remote_mm_str()/__access_remote_vm(), which
call mmap_read_lock_killable(mm) and then get_user_page_vma_remote(), which
faults pages in via handle_mm_fault(). pagefault_disable() does not stop
this, since it only affects the arch exception path.

Two deadlocks look possible. First:

Task A holds mmap_write_lock(A->mm), e.g. in mmap(), and allocates:
__alloc_pages_may_oom()
mutex_trylock(&oom_lock) fails, sleeps, did_some_progress = 1, retries

The BPF handler holds oom_lock and calls bpf_copy_from_user_task_str() on A:
mmap_read_lock_killable(A->mm) <- blocks

Neither side can make progress. The in-kernel OOM code avoids this by never
blocking on mmap_lock (the oom reaper uses mmap_read_trylock()).

Second, the handler reads a swapped-out page of a task in a memcg at its
limit. GUP faults the page in and charges it to that memcg:

try_charge_memcg()
mem_cgroup_oom()
mem_cgroup_out_of_memory()
mutex_lock_killable(&oom_lock)

which blocks on the mutex the same task already holds.

No later patch in the series changes mm/bpf_oom.c or filters these kfuncs.
Should the handler use a kfunc filter that rejects mm-accessing or
KF_SLEEPABLE kfuncs other than the OOM-specific ones, or should handlers be
non-sleepable apart from an explicit allowlist?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/46974333971