mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: bot+bpf-ci@kernel.org
To: bgardon@google.com,ast@kernel.org,daniel@iogearbox.net,andrii@kernel.org,akpm@linux-foundation.org,hannes@cmpxchg.org,mhocko@kernel.org,shakeel.butt@linux.dev,roman.gushchin@linux.dev,tj@kernel.org
Cc: jp.kobryn@linux.dev,eddyz87@gmail.com,memxor@gmail.com,martin.lau@linux.dev,song@kernel.org,yonghong.song@linux.dev,jolsa@kernel.org,emil@etsalapatis.com,ihor.solodrai@linux.dev,rientjes@google.com,surenb@google.com,joshdon@google.com,sweettea-kernel@dorminy.me,zhouchuyi@bytedance.com,laoar.shao@gmail.com,benbwgardon@gmail.com,bpf@vger.kernel.org,linux-mm@kvack.org,cgroups@vger.kernel.org,linux-kernel@vger.kernel.org,bgardon@google.com,john.fastabend@gmail.com,mhocko@suse.com,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next v4 02/17] mm, bpf: introduce BPF OOM struct ops
Date: Wed,  2 Dec 2026 21:59:02 +0000 (UTC)	[thread overview]
Message-ID: <21f18a2c74a444895ff8daf1b42a4991873790df70b904b5f4934a99d7a7f373@mail.kernel.org> (raw)
In-Reply-To: <20261202210415.517651-3-bgardon@google.com>

[-- Attachment #1: Type: text/plain, Size: 3454 bytes --]

> 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

  reply	other threads:[~2026-12-02 21:59 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 21:03 [PATCH bpf-next v4 00/17] mm: BPF OOM Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 01/17] bpf: mark struct oom_control's memcg field as TRUSTED_OR_NULL Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 02/17] mm, bpf: introduce BPF OOM struct ops Ben Gardon
2026-12-02 21:59   ` bot+bpf-ci [this message]
2026-12-03  5:40   ` kernel test robot
2026-12-02 21:03 ` [PATCH bpf-next v4 03/17] mm, bpf: introduce bpf_oom_kill_process() BPF kfunc Ben Gardon
2026-12-02 21:58   ` bot+bpf-ci
2026-12-02 21:03 ` [PATCH bpf-next v4 04/17] mm, bpf: introduce bpf_out_of_memory() " Ben Gardon
2026-12-02 21:59   ` bot+bpf-ci
2026-12-02 21:03 ` [PATCH bpf-next v4 05/17] selftests/bpf: Add BPF OOM struct ops test Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 06/17] sched/psi: add a tracepoint to psi_avgs_work() Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 07/17] sched/psi: add cgroup_id field to psi_group structure Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 08/17] mm, bpf: allow calling bpf_out_of_memory() from a PSI tracepoint Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 09/17] selftests/bpf: Enable CONFIG_PSI Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 10/17] selftests/bpf: Add PSI tracepoint OOM test Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 11/17] selftests/bpf: Add systemd-oomd style OOM policy test Ben Gardon
2026-12-02 21:58   ` bot+bpf-ci
2026-12-02 21:04 ` [PATCH bpf-next v4 12/17] mm, bpf: introduce bpf_oom_proactive_kill() for proactive OOM prevention Ben Gardon
2026-12-02 21:58   ` bot+bpf-ci
2026-12-03  1:19   ` Alexei Starovoitov
2026-12-03 18:35     ` Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 13/17] selftests/bpf: Add bpf_oom_proactive_kill() proactive OOM test Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 14/17] selftests/bpf: Add negative and verifier tests for BPF OOM kfuncs Ben Gardon
2026-12-02 21:58   ` bot+bpf-ci
2026-12-02 21:04 ` [PATCH bpf-next v4 15/17] mm, bpf: introduce bpf_mem_cgroup_raise_max() kfunc Ben Gardon
2026-12-02 21:58   ` bot+bpf-ci
2026-12-02 21:04 ` [PATCH bpf-next v4 16/17] selftests/bpf: Add bpf_mem_cgroup_raise_max() OOM test Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 17/17] Documentation/bpf: Document BPF OOM handling and kfuncs Ben Gardon
2026-12-02 21:58   ` bot+bpf-ci
2026-12-02 21:38 ` [PATCH bpf-next v4 00/17] mm: BPF OOM Ben Gardon
2026-12-03  1:24 ` Alexei Starovoitov
2026-12-03 18:16   ` Ben Gardon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=21f18a2c74a444895ff8daf1b42a4991873790df70b904b5f4934a99d7a7f373@mail.kernel.org \
    --to=bot+bpf-ci@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=benbwgardon@gmail.com \
    --cc=bgardon@google.com \
    --cc=bpf@vger.kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hannes@cmpxchg.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=joshdon@google.com \
    --cc=jp.kobryn@linux.dev \
    --cc=laoar.shao@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mhocko@kernel.org \
    --cc=mhocko@suse.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=song@kernel.org \
    --cc=surenb@google.com \
    --cc=sweettea-kernel@dorminy.me \
    --cc=tj@kernel.org \
    --cc=yonghong.song@linux.dev \
    --cc=zhouchuyi@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®