From: Roman Gushchin Introduce cgroup BPF struct_ops (bpf_oom_ops, attach type CGROUP_OOM_OPS) for implementing custom OOM policies. Policies can be attached to a cgroup via bpf_map__attach_cgroup_opts(). Root cgroup attachments cover system-wide OOMs as well as fallback from descendant cgroups. By default, effective handlers run from the OOMing memory cgroup upward toward the root unless an ancestor attaches with BPF_F_PREORDER. The handle_out_of_memory callback receives the oom_control describing the OOM scope and the cgroup where the policy is attached. Returning a positive value signals that the OOM condition has been handled. This could be by terminating a task, expanding a memcg limit, or backing off while a previously selected task exits. Returning <= 0 delegates to the next handler in the effective chain and ultimately to the in-kernel OOM handler. SysRq-triggered OOMs bypass BPF handlers so SysRq-f remains a failsafe. Existing heuristics and sysctls such as panic_on_oom and sysctl_oom_kill_allocating_task continue to run prior to bpf_handle_oom(). Signed-off-by: Roman Gushchin Co-developed-by: Ben Gardon Signed-off-by: Ben Gardon --- v3 -> v4: - Adopted mainline cgroup struct_ops attachment (CGROUP_OOM_OPS) and changed handle_out_of_memory() to receive (struct oom_control *, struct cgroup *). - Dropped oc->bpf_memory_freed dual-reporting check so handlers can return > 0 to back off while an existing selected task exits; treat only positive returns (> 0) as handled so negative errnos delegate. - Bypassed bpf_handle_oom() on SysRq-triggered OOMs (is_sysrq_oom(oc)). - Added .validate callback requiring handle_out_of_memory, validated ops->name via bpf_obj_name_cpy(), switched bpf_oom_func_proto() to curated bpf_base_func_proto() (blocking user-copy/stack helpers), walked up offline cgroups to the nearest online ancestor, and encapsulated sleepable RCU Tasks Trace locking in bpf-cgroup.h. v3: https://lore.kernel.org/all/20260127024421.494929-8-roman.gushchin@linux.dev/ MAINTAINERS | 2 + include/linux/bpf-cgroup-defs.h | 1 + include/linux/bpf-cgroup.h | 16 ++- include/linux/bpf.h | 1 + include/linux/bpf_oom.h | 44 ++++++++ include/linux/oom.h | 17 +++ kernel/bpf/cgroup.c | 1 + mm/Makefile | 3 + mm/bpf_oom.c | 187 ++++++++++++++++++++++++++++++++ mm/oom_kill.c | 10 ++ 10 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 include/linux/bpf_oom.h create mode 100644 mm/bpf_oom.c diff --git a/MAINTAINERS b/MAINTAINERS index b430b29e3878..d4ef52c5594b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5177,7 +5177,9 @@ M: Shakeel Butt L: bpf@vger.kernel.org L: linux-mm@kvack.org S: Maintained +F: include/linux/bpf_oom.h F: mm/bpf_memcontrol.c +F: mm/bpf_oom.c BPF [MISC] L: bpf@vger.kernel.org diff --git a/include/linux/bpf-cgroup-defs.h b/include/linux/bpf-cgroup-defs.h index 671f27ba6a62..ff650a422e36 100644 --- a/include/linux/bpf-cgroup-defs.h +++ b/include/linux/bpf-cgroup-defs.h @@ -48,6 +48,7 @@ enum cgroup_bpf_attach_type { CGROUP_UNIX_GETSOCKNAME, CGROUP_INET_SOCK_RELEASE, CGROUP_TCP_SOCK_OPS, + CGROUP_OOM_OPS, CGROUP_LSM_START, CGROUP_LSM_END = CGROUP_LSM_START + CGROUP_LSM_NUM - 1, MAX_CGROUP_BPF_ATTACH_TYPE diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index 0b63c2cc1399..13a74791be46 100644 --- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -112,14 +113,25 @@ struct bpf_prog_list { u32 flags; }; +static inline void bpf_cgroup_struct_ops_lock_sleepable(void) +{ + rcu_read_lock_trace(); +} + +static inline void bpf_cgroup_struct_ops_unlock_sleepable(void) +{ + rcu_read_unlock_trace(); +} + #define bpf_cgroup_struct_ops_foreach(var, item, cgrp, atype) \ - for (item = rcu_dereference((cgrp)->bpf.effective[atype])->items;\ + for (item = rcu_dereference_check((cgrp)->bpf.effective[atype], \ + rcu_read_lock_trace_held())->items;\ ((var) = READ_ONCE(item->kdata)); \ item++) static inline bool cgroup_bpf_is_struct_ops_atype(enum cgroup_bpf_attach_type atype) { - return atype == CGROUP_TCP_SOCK_OPS; + return atype == CGROUP_TCP_SOCK_OPS || atype == CGROUP_OOM_OPS; } void cgroup_bpf_struct_ops_register(int atype, u32 type_id, void *cfi_stubs, bool mult_trace); int cgroup_bpf_struct_ops_attach(struct bpf_map *map, const union bpf_attr *attr); diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 96e249bd00fb..82f99b92c481 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2761,6 +2761,7 @@ struct bpf_prog_array_item { }; union { struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]; + struct cgroup *cgroup; u64 bpf_cookie; }; }; diff --git a/include/linux/bpf_oom.h b/include/linux/bpf_oom.h new file mode 100644 index 000000000000..23e3131a12be --- /dev/null +++ b/include/linux/bpf_oom.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef __BPF_OOM_H +#define __BPF_OOM_H + +struct cgroup; +struct oom_control; + +#define BPF_OOM_NAME_MAX_LEN 64 + +/** + * struct bpf_oom_ops - BPF operations for customizing OOM handling + */ +struct bpf_oom_ops { + /** + * @handle_out_of_memory: Out of memory BPF handler, invoked before the + * in-kernel OOM selector. + * + * Receives @oc describing the OOM scope/constraint and @cgrp where this + * policy instance is attached. Return a positive value (> 0) if the + * OOM event was handled (either by freeing/expanding memory or + * backing off for an already-exiting task), or <= 0 to defer to the + * next handler in the hierarchy and ultimately the in-kernel OOM + * handler. + */ + int (*handle_out_of_memory)(struct oom_control *oc, + struct cgroup *cgrp); + + /** + * @name: BPF OOM policy name logged in OOM reports. + */ + char name[BPF_OOM_NAME_MAX_LEN]; +}; + +#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_MEMCG) +bool bpf_handle_oom(struct oom_control *oc); +#else +static inline bool bpf_handle_oom(struct oom_control *oc) +{ + return false; +} +#endif + +#endif /* __BPF_OOM_H */ diff --git a/include/linux/oom.h b/include/linux/oom.h index 00da05d227e6..ca3192c42533 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -51,8 +51,25 @@ struct oom_control { /* Used to print the constraint info. */ enum oom_constraint constraint; + +#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_MEMCG) + /* Active BPF OOM handler name */ + const char *bpf_handler_name; +#endif }; +#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_MEMCG) +static inline const char *oom_handler_name(const struct oom_control *oc) +{ + return oc->bpf_handler_name; +} +#else +static inline const char *oom_handler_name(const struct oom_control *oc) +{ + return NULL; +} +#endif + extern struct mutex oom_lock; extern struct mutex oom_adj_mutex; diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index e4e680dfb63d..dba22e99197d 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -572,6 +572,7 @@ static void prog_list_init_item(struct bpf_prog_list *pl, struct bpf_prog_array_ { if (prog_list_is_struct_ops(pl)) { item->kdata = bpf_struct_ops_map_kdata(pl->link->map); + item->cgroup = pl->link->cgroup; return; } diff --git a/mm/Makefile b/mm/Makefile index 2a3ec53d62ee..66b53e569eed 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -107,6 +107,9 @@ obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o ifdef CONFIG_BPF_SYSCALL obj-$(CONFIG_MEMCG) += bpf_memcontrol.o endif +ifdef CONFIG_CGROUP_BPF +obj-$(CONFIG_MEMCG) += bpf_oom.o +endif obj-$(CONFIG_CGROUP_HUGETLB) += hugetlb_cgroup.o obj-$(CONFIG_GUP_TEST) += gup_test.o obj-$(CONFIG_DMAPOOL_TEST) += dmapool_test.o diff --git a/mm/bpf_oom.c b/mm/bpf_oom.c new file mode 100644 index 000000000000..bc0838d55335 --- /dev/null +++ b/mm/bpf_oom.c @@ -0,0 +1,187 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * BPF-driven OOM killer customization + * + * Author: Roman Gushchin + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +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(); + oc->bpf_handler_name = NULL; + + return ret; +} + +static struct cgroup *bpf_oom_cgroup(struct mem_cgroup *memcg) +{ + if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !mem_cgroup_disabled()) { + struct cgroup *cgrp; + + memcg = memcg ?: root_mem_cgroup; + cgrp = READ_ONCE(memcg->css.cgroup); + if (cgrp->root == &cgrp_dfl_root) + return cgrp; + } + + /* + * Fall back to the cgroup v2 root when memory cgroups are disabled or + * mounted on cgroup v1 (cgroup BPF only attaches to the v2 hierarchy). + */ + return &cgrp_dfl_root.cgrp; +} + +/** + * bpf_handle_oom() - Invoke attached cgroup BPF OOM policies + * @oc: OOM control structure describing the OOM event + * + * Return: true if a BPF policy handled the OOM, false to fall back to the + * in-kernel OOM handler. + */ +bool bpf_handle_oom(struct oom_control *oc) +{ + const struct bpf_prog_array_item *item; + struct bpf_oom_ops *bpf_oom_ops; + struct cgroup *cgrp; + int ret = 0; + + lockdep_assert_held(&oom_lock); + + if (!cgroup_bpf_enabled(CGROUP_OOM_OPS)) + return false; + + cgrp = bpf_oom_cgroup(oc->memcg); + + bpf_cgroup_struct_ops_lock_sleepable(); + while (cgrp && !(READ_ONCE(cgrp->self.flags) & CSS_ONLINE)) + cgrp = cgroup_parent(cgrp); + if (!cgrp) + goto out_unlock; + + bpf_cgroup_struct_ops_foreach(bpf_oom_ops, item, cgrp, CGROUP_OOM_OPS) { + ret = bpf_ops_handle_oom(bpf_oom_ops, item->cgroup, oc); + if (ret > 0) + break; + } + +out_unlock: + bpf_cgroup_struct_ops_unlock_sleepable(); + + return ret > 0; +} + +static int __handle_out_of_memory(struct oom_control *oc, + struct cgroup *cgrp) +{ + return 0; +} + +static struct bpf_oom_ops __bpf_oom_ops = { + .handle_out_of_memory = __handle_out_of_memory, +}; + +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); + } +} + +static bool bpf_oom_ops_is_valid_access(int off, int size, + enum bpf_access_type type, + const struct bpf_prog *prog, + struct bpf_insn_access_aux *info) +{ + return bpf_tracing_btf_ctx_access(off, size, type, prog, info); +} + +static const struct bpf_verifier_ops bpf_oom_verifier_ops = { + .get_func_proto = bpf_oom_func_proto, + .is_valid_access = bpf_oom_ops_is_valid_access, +}; + +static int bpf_oom_ops_check_member(const struct btf_type *t, + const struct btf_member *member, + const struct bpf_prog *prog) +{ + return 0; +} + +static int bpf_oom_ops_init_member(const struct btf_type *t, + const struct btf_member *member, + void *kdata, const void *udata) +{ + const struct bpf_oom_ops *uops = udata; + struct bpf_oom_ops *ops = kdata; + u32 moff = __btf_member_bit_offset(t, member) / 8; + + switch (moff) { + case offsetof(struct bpf_oom_ops, name): + if (uops->name[0]) { + if (bpf_obj_name_cpy(ops->name, uops->name, + sizeof(ops->name)) <= 0) + return -EINVAL; + } else { + strscpy_pad(ops->name, "bpf_defined_policy", + sizeof(ops->name)); + } + return 1; + } + return 0; +} + +static int bpf_oom_ops_validate(void *kdata) +{ + struct bpf_oom_ops *ops = kdata; + + if (!ops->handle_out_of_memory) + return -EINVAL; + + return 0; +} + +static int bpf_oom_ops_init(struct btf *btf) +{ + return 0; +} + +static struct bpf_struct_ops bpf_oom_bpf_ops = { + .verifier_ops = &bpf_oom_verifier_ops, + .check_member = bpf_oom_ops_check_member, + .init_member = bpf_oom_ops_init_member, + .validate = bpf_oom_ops_validate, + .init = bpf_oom_ops_init, + .name = "bpf_oom_ops", + .cgroup_atype = CGROUP_OOM_OPS, + .owner = THIS_MODULE, + .cfi_stubs = &__bpf_oom_ops, + .free_after_mult_rcu_gp = true, +}; + +static int __init bpf_oom_struct_ops_init(void) +{ + return register_bpf_struct_ops(&bpf_oom_bpf_ops, bpf_oom_ops); +} +late_initcall(bpf_oom_struct_ops_init); diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 5d48bd862c27..fa9088c965b6 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include "internal.h" @@ -451,6 +452,8 @@ static void dump_header(struct oom_control *oc) pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%d\n", current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order, current->signal->oom_score_adj); + if (oom_handler_name(oc)) + pr_warn("oom bpf handler: %s\n", oom_handler_name(oc)); if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order) pr_warn("COMPACTION is disabled!!!\n"); @@ -1145,6 +1148,13 @@ bool out_of_memory(struct oom_control *oc) return true; } + /* + * Let BPF handle the OOM first unless triggered via SysRq, which + * skips to the kernel OOM handler. + */ + if (!is_sysrq_oom(oc) && bpf_handle_oom(oc)) + return true; + select_bad_process(oc); /* Found nothing?!?! */ if (!oc->chosen) { -- 2.56.1.911.g883d8bcb20-goog