Skip to content

Commit e66f6ba

Browse files
committed
mm: allow specifying custom oom constraint for bpf triggers
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 torvalds#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 <[email protected]>
1 parent fcf1e11 commit e66f6ba

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

include/linux/oom.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ enum oom_constraint {
1919
CONSTRAINT_CPUSET,
2020
CONSTRAINT_MEMORY_POLICY,
2121
CONSTRAINT_MEMCG,
22+
CONSTRAINT_BPF,
2223
};
2324

2425
enum bpf_oom_flags {
@@ -63,6 +64,9 @@ struct oom_control {
6364

6465
/* Policy name */
6566
const char *bpf_policy_name;
67+
68+
/* BPF-specific constraint name */
69+
const char *bpf_constraint;
6670
#endif
6771
};
6872

mm/oom_kill.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,6 @@ long oom_badness(struct task_struct *p, unsigned long totalpages)
240240
return points;
241241
}
242242

243-
static const char * const oom_constraint_text[] = {
244-
[CONSTRAINT_NONE] = "CONSTRAINT_NONE",
245-
[CONSTRAINT_CPUSET] = "CONSTRAINT_CPUSET",
246-
[CONSTRAINT_MEMORY_POLICY] = "CONSTRAINT_MEMORY_POLICY",
247-
[CONSTRAINT_MEMCG] = "CONSTRAINT_MEMCG",
248-
};
249-
250243
static const char *oom_policy_name(struct oom_control *oc)
251244
{
252245
#ifdef CONFIG_BPF_SYSCALL
@@ -256,6 +249,27 @@ static const char *oom_policy_name(struct oom_control *oc)
256249
return "default";
257250
}
258251

252+
static const char *oom_constraint_text(struct oom_control *oc)
253+
{
254+
switch (oc->constraint) {
255+
case CONSTRAINT_NONE:
256+
return "CONSTRAINT_NONE";
257+
case CONSTRAINT_CPUSET:
258+
return "CONSTRAINT_CPUSET";
259+
case CONSTRAINT_MEMORY_POLICY:
260+
return "CONSTRAINT_MEMORY_POLICY";
261+
case CONSTRAINT_MEMCG:
262+
return "CONSTRAINT_MEMCG";
263+
#ifdef CONFIG_BPF_SYSCALL
264+
case CONSTRAINT_BPF:
265+
return oc->bpf_constraint ? : "CONSTRAINT_BPF";
266+
#endif
267+
default:
268+
WARN_ON_ONCE(1);
269+
return "";
270+
}
271+
}
272+
259273
/*
260274
* Determine the type of allocation constraint.
261275
*/
@@ -267,6 +281,9 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc)
267281
bool cpuset_limited = false;
268282
int nid;
269283

284+
if (oc->constraint == CONSTRAINT_BPF)
285+
return CONSTRAINT_BPF;
286+
270287
if (is_memcg_oom(oc)) {
271288
oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1;
272289
return CONSTRAINT_MEMCG;
@@ -458,7 +475,7 @@ static void dump_oom_victim(struct oom_control *oc, struct task_struct *victim)
458475
{
459476
/* one line summary of the oom killer context. */
460477
pr_info("oom-kill:constraint=%s,nodemask=%*pbl",
461-
oom_constraint_text[oc->constraint],
478+
oom_constraint_text(oc),
462479
nodemask_pr_args(oc->nodemask));
463480
cpuset_print_current_mems_allowed();
464481
mem_cgroup_print_oom_context(oc->memcg, victim);
@@ -1350,11 +1367,14 @@ __bpf_kfunc int bpf_oom_kill_process(struct oom_control *oc,
13501367
* Returns a negative value if an error occurred.
13511368
*/
13521369
__bpf_kfunc int bpf_out_of_memory(struct mem_cgroup *memcg__nullable,
1353-
int order, u64 flags)
1370+
int order, u64 flags,
1371+
const char *constraint_text__nullable)
13541372
{
13551373
struct oom_control oc = {
13561374
.memcg = memcg__nullable,
13571375
.order = order,
1376+
.constraint = CONSTRAINT_BPF,
1377+
.bpf_constraint = constraint_text__nullable,
13581378
};
13591379
int ret;
13601380

0 commit comments

Comments
 (0)