[PATCH v1 07/14] mm: allow specifying custom oom constraint for bpf triggers

Roman Gushchin posted 14 patches 1 month, 2 weeks ago
[PATCH v1 07/14] mm: allow specifying custom oom constraint for bpf triggers
Posted by Roman Gushchin 1 month, 2 weeks ago
Currently there is a hard-coded list of possible oom constraints:
NONE, CPUSET, MEMORY_POLICY & MEMCG. Add a new one: CONSTRAINT_BPF.
Also, add an ability to specify a custom constraint name
when calling bpf_out_of_memory(). If an empty string is passed
as an argument, CONSTRAINT_BPF is displayed.

The resulting output in dmesg will look like this:

[  315.224875] kworker/u17:0 invoked oom-killer: gfp_mask=0x0(), order=0, oom_score_adj=0
               oom_policy=default
[  315.226532] CPU: 1 UID: 0 PID: 74 Comm: kworker/u17:0 Not tainted 6.16.0-00015-gf09eb0d6badc #102 PREEMPT(full)
[  315.226534] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-5.fc42 04/01/2014
[  315.226536] Workqueue: bpf_psi_wq bpf_psi_handle_event_fn
[  315.226542] Call Trace:
[  315.226545]  <TASK>
[  315.226548]  dump_stack_lvl+0x4d/0x70
[  315.226555]  dump_header+0x59/0x1c6
[  315.226561]  oom_kill_process.cold+0x8/0xef
[  315.226565]  out_of_memory+0x111/0x5c0
[  315.226577]  bpf_out_of_memory+0x6f/0xd0
[  315.226580]  ? srso_alias_return_thunk+0x5/0xfbef5
[  315.226589]  bpf_prog_3018b0cf55d2c6bb_handle_psi_event+0x5d/0x76
[  315.226594]  bpf__bpf_psi_ops_handle_psi_event+0x47/0xa7
[  315.226599]  bpf_psi_handle_event_fn+0x63/0xb0
[  315.226604]  process_one_work+0x1fc/0x580
[  315.226616]  ? srso_alias_return_thunk+0x5/0xfbef5
[  315.226624]  worker_thread+0x1d9/0x3b0
[  315.226629]  ? __pfx_worker_thread+0x10/0x10
[  315.226632]  kthread+0x128/0x270
[  315.226637]  ? lock_release+0xd4/0x2d0
[  315.226645]  ? __pfx_kthread+0x10/0x10
[  315.226649]  ret_from_fork+0x81/0xd0
[  315.226652]  ? __pfx_kthread+0x10/0x10
[  315.226655]  ret_from_fork_asm+0x1a/0x30
[  315.226667]  </TASK>
[  315.239745] memory: usage 42240kB, limit 9007199254740988kB, failcnt 0
[  315.240231] swap: usage 0kB, limit 0kB, failcnt 0
[  315.240585] Memory cgroup stats for /cgroup-test-work-dir673/oom_test/cg2:
[  315.240603] anon 42897408
[  315.241317] file 0
[  315.241493] kernel 98304
...
[  315.255946] Tasks state (memory values in pages):
[  315.256292] [  pid  ]   uid  tgid total_vm      rss rss_anon rss_file rss_shmem pgtables_bytes swapents oom_score_adj name
[  315.257107] [    675]     0   675   162013    10969    10712      257         0   155648        0             0 test_progs
[  315.257927] oom-kill:constraint=CONSTRAINT_BPF_PSI_MEM,nodemask=(null),cpuset=/,mems_allowed=0,oom_memcg=/cgroup-test-work-dir673/oom_test/cg2,task_memcg=/cgroup-test-work-dir673/oom_test/cg2,task=test_progs,pid=675,uid=0
[  315.259371] Memory cgroup out of memory: Killed process 675 (test_progs) total-vm:648052kB, anon-rss:42848kB, file-rss:1028kB, shmem-rss:0kB, UID:0 pgtables:152kB oom_score_adj:0

Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
---
 include/linux/oom.h |  4 ++++
 mm/oom_kill.c       | 38 +++++++++++++++++++++++++++++---------
 2 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/include/linux/oom.h b/include/linux/oom.h
index ef453309b7ea..4b04944b42de 100644
--- a/include/linux/oom.h
+++ b/include/linux/oom.h
@@ -19,6 +19,7 @@ enum oom_constraint {
 	CONSTRAINT_CPUSET,
 	CONSTRAINT_MEMORY_POLICY,
 	CONSTRAINT_MEMCG,
+	CONSTRAINT_BPF,
 };
 
 /*
@@ -58,6 +59,9 @@ struct oom_control {
 
 	/* Policy name */
 	const char *bpf_policy_name;
+
+	/* BPF-specific constraint name */
+	const char *bpf_constraint;
 #endif
 };
 
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index df409f0fac45..67afcd43a5f7 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -240,13 +240,6 @@ long oom_badness(struct task_struct *p, unsigned long totalpages)
 	return points;
 }
 
-static const char * const oom_constraint_text[] = {
-	[CONSTRAINT_NONE] = "CONSTRAINT_NONE",
-	[CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET",
-	[CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY",
-	[CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG",
-};
-
 static const char *oom_policy_name(struct oom_control *oc)
 {
 #ifdef CONFIG_BPF_SYSCALL
@@ -256,6 +249,27 @@ static const char *oom_policy_name(struct oom_control *oc)
 	return "default";
 }
 
+static const char *oom_constraint_text(struct oom_control *oc)
+{
+	switch (oc->constraint) {
+	case CONSTRAINT_NONE:
+		return "CONSTRAINT_NONE";
+	case CONSTRAINT_CPUSET:
+		return "CONSTRAINT_CPUSET";
+	case CONSTRAINT_MEMORY_POLICY:
+		return "CONSTRAINT_MEMORY_POLICY";
+	case CONSTRAINT_MEMCG:
+		return "CONSTRAINT_MEMCG";
+#ifdef CONFIG_BPF_SYSCALL
+	case CONSTRAINT_BPF:
+		return oc->bpf_constraint ? : "CONSTRAINT_BPF";
+#endif
+	default:
+		WARN_ON_ONCE(1);
+		return "";
+	}
+}
+
 /*
  * Determine the type of allocation constraint.
  */
@@ -267,6 +281,9 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc)
 	bool cpuset_limited = false;
 	int nid;
 
+	if (oc->constraint == CONSTRAINT_BPF)
+		return CONSTRAINT_BPF;
+
 	if (is_memcg_oom(oc)) {
 		oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1;
 		return CONSTRAINT_MEMCG;
@@ -458,7 +475,7 @@ static void dump_oom_victim(struct oom_control *oc, struct task_struct *victim)
 {
 	/* one line summary of the oom killer context. */
 	pr_info("oom-kill:constraint=%s,nodemask=%*pbl",
-			oom_constraint_text[oc->constraint],
+			oom_constraint_text(oc),
 			nodemask_pr_args(oc->nodemask));
 	cpuset_print_current_mems_allowed();
 	mem_cgroup_print_oom_context(oc->memcg, victim);
@@ -1344,11 +1361,14 @@ __bpf_kfunc int bpf_oom_kill_process(struct oom_control *oc,
  * Returns a negative value if an error has been occurred.
  */
 __bpf_kfunc int bpf_out_of_memory(struct mem_cgroup *memcg__nullable,
-				  int order, bool wait_on_oom_lock)
+				  int order, bool wait_on_oom_lock,
+				  const char *constraint_text__nullable)
 {
 	struct oom_control oc = {
 		.memcg = memcg__nullable,
 		.order = order,
+		.constraint = CONSTRAINT_BPF,
+		.bpf_constraint = constraint_text__nullable,
 	};
 	int ret;
 
-- 
2.50.1
Re: [PATCH v1 07/14] mm: allow specifying custom oom constraint for bpf triggers
Posted by ChaosEsque Team 1 day, 17 hours ago
Roman Gushchin...
RUSSKIEEEEEE

On Mon, Aug 18, 2025 at 1:05 PM Roman Gushchin <roman.gushchin@linux.dev> wrote:
>
> Currently there is a hard-coded list of possible oom constraints:
> NONE, CPUSET, MEMORY_POLICY & MEMCG. Add a new one: CONSTRAINT_BPF.
> Also, add an ability to specify a custom constraint name
> when calling bpf_out_of_memory(). If an empty string is passed
> as an argument, CONSTRAINT_BPF is displayed.
>
> The resulting output in dmesg will look like this:
>
> [  315.224875] kworker/u17:0 invoked oom-killer: gfp_mask=0x0(), order=0, oom_score_adj=0
>                oom_policy=default
> [  315.226532] CPU: 1 UID: 0 PID: 74 Comm: kworker/u17:0 Not tainted 6.16.0-00015-gf09eb0d6badc #102 PREEMPT(full)
> [  315.226534] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-5.fc42 04/01/2014
> [  315.226536] Workqueue: bpf_psi_wq bpf_psi_handle_event_fn
> [  315.226542] Call Trace:
> [  315.226545]  <TASK>
> [  315.226548]  dump_stack_lvl+0x4d/0x70
> [  315.226555]  dump_header+0x59/0x1c6
> [  315.226561]  oom_kill_process.cold+0x8/0xef
> [  315.226565]  out_of_memory+0x111/0x5c0
> [  315.226577]  bpf_out_of_memory+0x6f/0xd0
> [  315.226580]  ? srso_alias_return_thunk+0x5/0xfbef5
> [  315.226589]  bpf_prog_3018b0cf55d2c6bb_handle_psi_event+0x5d/0x76
> [  315.226594]  bpf__bpf_psi_ops_handle_psi_event+0x47/0xa7
> [  315.226599]  bpf_psi_handle_event_fn+0x63/0xb0
> [  315.226604]  process_one_work+0x1fc/0x580
> [  315.226616]  ? srso_alias_return_thunk+0x5/0xfbef5
> [  315.226624]  worker_thread+0x1d9/0x3b0
> [  315.226629]  ? __pfx_worker_thread+0x10/0x10
> [  315.226632]  kthread+0x128/0x270
> [  315.226637]  ? lock_release+0xd4/0x2d0
> [  315.226645]  ? __pfx_kthread+0x10/0x10
> [  315.226649]  ret_from_fork+0x81/0xd0
> [  315.226652]  ? __pfx_kthread+0x10/0x10
> [  315.226655]  ret_from_fork_asm+0x1a/0x30
> [  315.226667]  </TASK>
> [  315.239745] memory: usage 42240kB, limit 9007199254740988kB, failcnt 0
> [  315.240231] swap: usage 0kB, limit 0kB, failcnt 0
> [  315.240585] Memory cgroup stats for /cgroup-test-work-dir673/oom_test/cg2:
> [  315.240603] anon 42897408
> [  315.241317] file 0
> [  315.241493] kernel 98304
> ...
> [  315.255946] Tasks state (memory values in pages):
> [  315.256292] [  pid  ]   uid  tgid total_vm      rss rss_anon rss_file rss_shmem pgtables_bytes swapents oom_score_adj name
> [  315.257107] [    675]     0   675   162013    10969    10712      257         0   155648        0             0 test_progs
> [  315.257927] oom-kill:constraint=CONSTRAINT_BPF_PSI_MEM,nodemask=(null),cpuset=/,mems_allowed=0,oom_memcg=/cgroup-test-work-dir673/oom_test/cg2,task_memcg=/cgroup-test-work-dir673/oom_test/cg2,task=test_progs,pid=675,uid=0
> [  315.259371] Memory cgroup out of memory: Killed process 675 (test_progs) total-vm:648052kB, anon-rss:42848kB, file-rss:1028kB, shmem-rss:0kB, UID:0 pgtables:152kB oom_score_adj:0
>
> Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
> ---
>  include/linux/oom.h |  4 ++++
>  mm/oom_kill.c       | 38 +++++++++++++++++++++++++++++---------
>  2 files changed, 33 insertions(+), 9 deletions(-)
>
> diff --git a/include/linux/oom.h b/include/linux/oom.h
> index ef453309b7ea..4b04944b42de 100644
> --- a/include/linux/oom.h
> +++ b/include/linux/oom.h
> @@ -19,6 +19,7 @@ enum oom_constraint {
>         CONSTRAINT_CPUSET,
>         CONSTRAINT_MEMORY_POLICY,
>         CONSTRAINT_MEMCG,
> +       CONSTRAINT_BPF,
>  };
>
>  /*
> @@ -58,6 +59,9 @@ struct oom_control {
>
>         /* Policy name */
>         const char *bpf_policy_name;
> +
> +       /* BPF-specific constraint name */
> +       const char *bpf_constraint;
>  #endif
>  };
>
> diff --git a/mm/oom_kill.c b/mm/oom_kill.c
> index df409f0fac45..67afcd43a5f7 100644
> --- a/mm/oom_kill.c
> +++ b/mm/oom_kill.c
> @@ -240,13 +240,6 @@ long oom_badness(struct task_struct *p, unsigned long totalpages)
>         return points;
>  }
>
> -static const char * const oom_constraint_text[] = {
> -       [CONSTRAINT_NONE] = "CONSTRAINT_NONE",
> -       [CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET",
> -       [CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY",
> -       [CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG",
> -};
> -
>  static const char *oom_policy_name(struct oom_control *oc)
>  {
>  #ifdef CONFIG_BPF_SYSCALL
> @@ -256,6 +249,27 @@ static const char *oom_policy_name(struct oom_control *oc)
>         return "default";
>  }
>
> +static const char *oom_constraint_text(struct oom_control *oc)
> +{
> +       switch (oc->constraint) {
> +       case CONSTRAINT_NONE:
> +               return "CONSTRAINT_NONE";
> +       case CONSTRAINT_CPUSET:
> +               return "CONSTRAINT_CPUSET";
> +       case CONSTRAINT_MEMORY_POLICY:
> +               return "CONSTRAINT_MEMORY_POLICY";
> +       case CONSTRAINT_MEMCG:
> +               return "CONSTRAINT_MEMCG";
> +#ifdef CONFIG_BPF_SYSCALL
> +       case CONSTRAINT_BPF:
> +               return oc->bpf_constraint ? : "CONSTRAINT_BPF";
> +#endif
> +       default:
> +               WARN_ON_ONCE(1);
> +               return "";
> +       }
> +}
> +
>  /*
>   * Determine the type of allocation constraint.
>   */
> @@ -267,6 +281,9 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc)
>         bool cpuset_limited = false;
>         int nid;
>
> +       if (oc->constraint == CONSTRAINT_BPF)
> +               return CONSTRAINT_BPF;
> +
>         if (is_memcg_oom(oc)) {
>                 oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1;
>                 return CONSTRAINT_MEMCG;
> @@ -458,7 +475,7 @@ static void dump_oom_victim(struct oom_control *oc, struct task_struct *victim)
>  {
>         /* one line summary of the oom killer context. */
>         pr_info("oom-kill:constraint=%s,nodemask=%*pbl",
> -                       oom_constraint_text[oc->constraint],
> +                       oom_constraint_text(oc),
>                         nodemask_pr_args(oc->nodemask));
>         cpuset_print_current_mems_allowed();
>         mem_cgroup_print_oom_context(oc->memcg, victim);
> @@ -1344,11 +1361,14 @@ __bpf_kfunc int bpf_oom_kill_process(struct oom_control *oc,
>   * Returns a negative value if an error has been occurred.
>   */
>  __bpf_kfunc int bpf_out_of_memory(struct mem_cgroup *memcg__nullable,
> -                                 int order, bool wait_on_oom_lock)
> +                                 int order, bool wait_on_oom_lock,
> +                                 const char *constraint_text__nullable)
>  {
>         struct oom_control oc = {
>                 .memcg = memcg__nullable,
>                 .order = order,
> +               .constraint = CONSTRAINT_BPF,
> +               .bpf_constraint = constraint_text__nullable,
>         };
>         int ret;
>
> --
> 2.50.1
>
>