[RFC PATCH v2 2/5] mm: Add policy_name to identify OOM policies

From: Chuyi Zhou
Date: Thu Aug 10 2023 - 04:14:09 EST


This patch adds a new metadata policy_name in oom_control and report it
in dump_header(), so we can know what has been the selection policy. In
BPF program, we can call kfunc set_oom_policy_name to set the current
user-defined policy name. The in-kernel policy_name is "default".

Signed-off-by: Chuyi Zhou <zhouchuyi@xxxxxxxxxxxxx>
---
include/linux/oom.h | 7 +++++++
mm/oom_kill.c | 42 +++++++++++++++++++++++++++++++++++++++---
2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/include/linux/oom.h b/include/linux/oom.h
index 7d0c9c48a0c5..69d0f2ec6ea6 100644
--- a/include/linux/oom.h
+++ b/include/linux/oom.h
@@ -22,6 +22,10 @@ enum oom_constraint {
CONSTRAINT_MEMCG,
};

+enum {
+ POLICY_NAME_LEN = 16,
+};
+
/*
* Details of the page allocation that triggered the oom killer that are used to
* determine what should be killed.
@@ -52,6 +56,9 @@ struct oom_control {

/* Used to print the constraint info. */
enum oom_constraint constraint;
+
+ /* Used to report the policy info. */
+ char policy_name[POLICY_NAME_LEN];
};

extern struct mutex oom_lock;
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 255c9ef1d808..3239dcdba4d7 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -443,6 +443,35 @@ static int dump_task(struct task_struct *p, void *arg)
return 0;
}

+__bpf_kfunc void set_oom_policy_name(struct oom_control *oc, const char *src, size_t sz)
+{
+ memset(oc->policy_name, 0, sizeof(oc->policy_name));
+
+ if (sz > POLICY_NAME_LEN)
+ sz = POLICY_NAME_LEN;
+
+ memcpy(oc->policy_name, src, sz);
+}
+
+__diag_push();
+__diag_ignore_all("-Wmissing-prototypes",
+ "kfuncs which will be used in BPF programs");
+
+__weak noinline void bpf_set_policy_name(struct oom_control *oc)
+{
+}
+
+__diag_pop();
+
+BTF_SET8_START(bpf_oom_policy_kfunc_ids)
+BTF_ID_FLAGS(func, set_oom_policy_name)
+BTF_SET8_END(bpf_oom_policy_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_oom_policy_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &bpf_oom_policy_kfunc_ids,
+};
+
/**
* dump_tasks - dump current memory state of all system tasks
* @oc: pointer to struct oom_control
@@ -484,8 +513,8 @@ static void dump_oom_summary(struct oom_control *oc, struct task_struct *victim)

static void dump_header(struct oom_control *oc, struct task_struct *p)
{
- pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
- current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
+ pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, policy_name=%s, oom_score_adj=%hd\n",
+ current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order, oc->policy_name,
current->signal->oom_score_adj);
if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order)
pr_warn("COMPACTION is disabled!!!\n");
@@ -775,8 +804,11 @@ static int __init oom_init(void)
err = register_btf_fmodret_id_set(&oom_bpf_fmodret_set);
if (err)
pr_warn("error while registering oom fmodret entrypoints: %d", err);
+ err = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+ &bpf_oom_policy_kfunc_set);
+ if (err)
+ pr_warn("error while registering oom kfunc entrypoints: %d", err);
#endif
-
return 0;
}
subsys_initcall(oom_init)
@@ -1196,6 +1228,10 @@ bool out_of_memory(struct oom_control *oc)
return true;
}

+ set_oom_policy_name(oc, "default", sizeof("default"));
+
+ bpf_set_policy_name(oc);
+
select_bad_process(oc);
/* Found nothing?!?! */
if (!oc->chosen) {
--
2.20.1