BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ben Gardon" <bgardon@google.com>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Tejun Heo" <tj@kernel.org>
Cc: "JP Kobryn" <jp.kobryn@linux.dev>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"David Rientjes" <rientjes@google.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Josh Don" <joshdon@google.com>,
	"Sweet Tea Dorminy" <sweettea-kernel@dorminy.me>,
	"Chuyi Zhou" <zhouchuyi@bytedance.com>,
	"Yafang Shao" <laoar.shao@gmail.com>,
	benbwgardon@gmail.com, bpf@vger.kernel.org, linux-mm@kvack.org,
	"Michal Hocko" <mhocko@suse.com>,
	mkoutny@suse.com
Subject: Re: [PATCH bpf-next v4 04/17] mm, bpf: introduce bpf_out_of_memory() BPF kfunc
Date: Wed, 02 Dec 2026 21:18:17 +0000	[thread overview]
Message-ID: <sashiko-outbox-246636@kernel.org> (raw)
In-Reply-To: <20261202210415.517651-5-bgardon@google.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: unguarded task_will_free_mem() in out_of_memory() for
  BPF-declared OOMs
- [Medium] mm: oom_cpuset_eligible() filters global BPF OOM victims by
  current cpuset
- [Low] mm, bpf: inconsistent dump_header() handling for BPF-declared
  OOMs
--

commit 9e727626010671020de30395390afd2758b2e296
Author: Roman Gushchin <roman.gushchin@linux.dev>

mm, bpf: introduce bpf_out_of_memory() BPF kfunc

This commit introduces the bpf_out_of_memory() kfunc to allow BPF programs
to proactively declare an OOM condition and invoke OOM handling.

> diff --git a/mm/oom_kill.c b/mm/oom_kill.c
> index 85da2a39e18a1..9b5df00f98abd 100644
> --- a/mm/oom_kill.c
> +++ b/mm/oom_kill.c
[ ... ]
> @@ -1024,7 +1026,8 @@ static void oom_kill_process(struct oom_control *oc, const char *message)
>  	task_unlock(victim);
>  
>  	if (__ratelimit(&oom_rs)) {
> -		dump_header(oc);
> +		if (!is_bpf_declared_oom(oc))
> +			dump_header(oc);
>  		dump_oom_victim(oc, victim);
>  	}
>  
[ ... ]
> @@ -1138,7 +1141,8 @@ bool out_of_memory(struct oom_control *oc)
>  		oc->nodemask = NULL;
>  	check_panic_on_oom(oc);
>  
> -	if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task &&
> +	if (!is_memcg_oom(oc) && !is_bpf_declared_oom(oc) &&
> +	    sysctl_oom_kill_allocating_task &&
>  	    current->mm && !oom_unkillable_task(current) &&
>  	    oom_cpuset_eligible(current, oc) &&
>  	    current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {

[Severity: Medium]
Should the task_will_free_mem(current) shortcut earlier in out_of_memory()
also check !is_bpf_declared_oom(oc)?

mm/oom_kill.c:out_of_memory() {
    ...
	if (task_will_free_mem(current)) {
		mark_oom_victim(current);
		queue_oom_reaper(current);
		return true;
	}
    ...
}

If the task calling bpf_out_of_memory() has a pending fatal signal or is in
a concurrent group exit (when BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK is unset, when
oom_lock is acquired on the uncontended fastpath, or when SIGKILL arrives
after taking oom_lock), out_of_memory() treats current as an allocating
task, marks and reaps it as the OOM victim, and returns true without
running OOM handling on the target domain.

> @@ -1165,7 +1169,8 @@ bool out_of_memory(struct oom_control *oc)
>  		 * system level, we cannot survive this and will enter
>  		 * an endless loop in the allocator. Bail out now.
>  		 */
> -		if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
> +		if (!is_sysrq_oom(oc) && !is_memcg_oom(oc) &&
> +		    !is_bpf_declared_oom(oc))
>  			panic("System is deadlocked on memory\n");
>  	}
>  	if (oc->chosen && oc->chosen != (void *)-1UL)

[Severity: Low]
Should dump_header(oc) in the !oc->chosen path right above this check also
be skipped (or ratelimited) when is_bpf_declared_oom(oc) is true?  Repeated
bpf_out_of_memory() calls on a domain with no killable tasks will otherwise
emit unratelimited stack traces, meminfo, and task dumps for current.
Conversely, because oom_handler_name(oc) is only logged inside
dump_header(), skipping dump_header() in oom_kill_process() for
BPF-declared OOMs suppresses logging the active bpf_oom_ops handler name
when a victim is killed.

> @@ -1319,6 +1324,66 @@ __bpf_kfunc int bpf_oom_kill_process(struct oom_control *oc,
>  	return 0;
>  }
>  
> +/**
> + * bpf_out_of_memory() - Declare an OOM condition and invoke OOM handling
> + * @memcg__nullable: target memory cgroup, or NULL for system-wide OOM
> + * @order: allocation page order (0..MAX_PAGE_ORDER), passed through to
> + *         bpf_oom_ops handlers as a memory-reclaim size hint
> + * @flags: bitmask of &enum bpf_oom_flags
> + *
> + * Synchronizes on oom_lock: waits (interruptible by fatal signals) when
> + * BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK is set, or returns -EBUSY immediately if
> + * oom_lock is contended. Root @memcg__nullable is normalized to NULL
> + * (system-wide OOM); dying memory cgroups are rejected with -EINVAL.
> + *
> + * Return: 1 if forward progress was made, 0 if no process was terminated, or a
> + * negative errno on failure.
> + */
> +__bpf_kfunc int bpf_out_of_memory(struct mem_cgroup *memcg__nullable,
> +				  int order, u64 flags)
> +{
> +	struct oom_control oc = {
> +		.gfp_mask = GFP_KERNEL,
> +		.order = order,
> +		.bpf_declared = true,
> +	};
> +	int ret;
> +
> +	if (flags & ~BPF_OOM_FLAGS_ALL)
> +		return -EINVAL;
> +
> +	/*
> +	 * Keep order so callers can signal a higher-order memory need to
> +	 * reactive bpf_oom_ops handlers (or exercise high-order OOM policies).
> +	 */
> +	if (oc.order < 0 || oc.order > MAX_PAGE_ORDER)
> +		return -EINVAL;
> +
> +	if (memcg__nullable && mem_cgroup_is_root(memcg__nullable))
> +		memcg__nullable = NULL;
> +
> +	if (memcg__nullable && memcg_is_dying(memcg__nullable))
> +		return -EINVAL;
> +
> +	oc.memcg = memcg__nullable;
> +
> +	if (flags & BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK) {
> +		ret = mutex_lock_killable(&oom_lock);
> +		if (ret)
> +			return ret;
> +	} else if (!mutex_trylock(&oom_lock)) {
> +		return -EBUSY;
> +	}
> +
> +	if (oc.memcg)
> +		memcg_memory_event(oc.memcg, MEMCG_OOM);
> +
> +	ret = out_of_memory(&oc);
> +
> +	mutex_unlock(&oom_lock);
> +	return ret;
> +}

[Severity: Medium]
On a system-wide BPF-declared OOM (oc.memcg == NULL and oc.nodemask ==
NULL), if execution falls back to select_bad_process(), does
oom_evaluate_task() -> oom_cpuset_eligible() unintentionally filter
candidate victims against current's cpuset?

mm/oom_kill.c:oom_cpuset_eligible() {
    ...
		if (mask) {
			ret = mempolicy_in_oom_domain(tsk, mask);
		} else {
			ret = cpuset_mems_allowed_intersects(current, tsk);
		}
    ...
}

Under CONFIG_NUMA, if the task calling bpf_out_of_memory() is confined to a
cpuset covering a subset of NUMA nodes, tasks on disjoint NUMA nodes will
be skipped as ineligible even though the declared OOM is unconstrained across
the whole system.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202210415.517651-1-bgardon@google.com?part=4

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

Thread overview: 21+ 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:03 ` [PATCH bpf-next v4 03/17] mm, bpf: introduce bpf_oom_kill_process() BPF kfunc Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 04/17] mm, bpf: introduce bpf_out_of_memory() " Ben Gardon
2026-12-02 21:18   ` sashiko-bot [this message]
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:15   ` sashiko-bot
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:04 ` [PATCH bpf-next v4 12/17] mm, bpf: introduce bpf_oom_proactive_kill() for proactive OOM prevention 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:15   ` sashiko-bot
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: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

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=sashiko-outbox-246636@kernel.org \
    --to=sashiko-bot@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=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hannes@cmpxchg.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=joshdon@google.com \
    --cc=jp.kobryn@linux.dev \
    --cc=laoar.shao@gmail.com \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mhocko@kernel.org \
    --cc=mhocko@suse.com \
    --cc=mkoutny@suse.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.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