Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp3324853rwo; Fri, 4 Aug 2023 03:09:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEfKKSuiK5fofoTE0f2wmOv1kly3UzfiUEG5JqruY0i77YCeBcqYg2CndY91oJ8JqysFLwd X-Received: by 2002:a05:6a20:3c8a:b0:130:11e4:d693 with SMTP id b10-20020a056a203c8a00b0013011e4d693mr1642510pzj.53.1691143765103; Fri, 04 Aug 2023 03:09:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691143765; cv=none; d=google.com; s=arc-20160816; b=Ue8kwJ1hmxBAw72GXBZ06plrQslQa+aDnGHt9yBkjwWsbLqE4LjROiAon3kzMFpuF+ py8RLUfl8Kq8t6RGRCjsKGJQj4aTve3k1Mrp8aifLg6weaHENuUlu73/Sh9Jx2w4m2W6 xQzXM4bBeG7b44jgbDa6Dr0KoQhXStpyVe7Fsjb53tObMoKkje+LSkTFvbZjCRZWIKzy oS8z1KmSNFGyJMo0vmRMQL6CREVWXwo4ls7LWvClLSr++FWMCYBX89QGv9LZ1NcsN2J2 /L0YdCUjdASYPzCxOJYtQwwsvJED46rl0/K7HdF/GrQ8FoIBIqT6UQUdXvlE6S88fWMl QWwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZnVxinOLSDbVFQcoUFkv5fif4kt7MH5yiiDW0k4Isz4=; fh=5K/5O+uaWy/TXWHq+XYNoCsNLzz39Nw0/H1YBlI/NpI=; b=JDy9h7/JdPr5al6aWdWQX1mkwrvW5btxJ5urX/H2+NSy3JsXVbEbv84T0XcbaCvR2W Js49vGHQqSxh67a7fqzJgMMfI5/07pMi9aRxEJdveW2apaVVLLz4/XFquEV35NQmG8J6 w9D+52Xo3F90WWoy0Xz9wEHulHMx9qdc6mKwlxgPR6YEWIF6CnYuG2ix8VHtpqQ46Tp0 cYvnLo1NKEX9076+hDBl4TpiSD5qMV51SBSSwTH99lDAsqCpTeKeDMGVnkerXn+zKaxD HhT3jjqjzqHm8A7dFyuewLcTuCy9BF/xUwpvyr64XudJkdXpPZW5ePuHMkGhDCKQPUay cgZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=SnYq7pN1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 3-20020a630b03000000b005646f8a9506si1540390pgl.426.2023.08.04.03.09.13; Fri, 04 Aug 2023 03:09:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=SnYq7pN1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230488AbjHDJi0 (ORCPT + 99 others); Fri, 4 Aug 2023 05:38:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230281AbjHDJiY (ORCPT ); Fri, 4 Aug 2023 05:38:24 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D3BE30EA for ; Fri, 4 Aug 2023 02:38:23 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-686f8614ce5so1773901b3a.3 for ; Fri, 04 Aug 2023 02:38:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1691141902; x=1691746702; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZnVxinOLSDbVFQcoUFkv5fif4kt7MH5yiiDW0k4Isz4=; b=SnYq7pN1xKt+8a/z1C+XFiuu2Ak1e5y+1Mhw4KDi+FAv5ci79AUGSFoPX+kdGaVg+F ljIZlgKY99++RUbA5xeIKLBsHHuwqQptGQ6yHf0MZ+5awXqsZx8LSY7v2Jeb702P0gGy aSNqUTc0vxHv3ZjhTdhYeX04dsH34jOpByFF8IwXYFZXrLOqi/Y1A0nxMr48P2Mked+W lRhz4HZu79RK7uZdHXLNBgFvOnMSLWkjTr8zn1WtUyr6KDzyoSKWOQMC6Rzh/DpUgiOJ HwIl5c3AZ3RSsOFH/IRN44JBRMBmMiVnj4F2MzK87P7LwMhl5vt40rFY7T6HqGGit5ae fJJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691141902; x=1691746702; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZnVxinOLSDbVFQcoUFkv5fif4kt7MH5yiiDW0k4Isz4=; b=C3U6qvfMFlU7/rXiMIeqjcR8XIsPyc57g5DqtzHIvZOvrskmjRho+2S6hUBQ6DqDsM rpQgQ3KpoTw65H8fPlVHjiBUZRASSFkZqqHXZjbTZSHurtYkBv1NTrAlzob4RJlAcZEL /7yF+07Ezyb6MOoydyqWx9oLA2nLBnnH8ntunIubs7nMIEBBStc2+2CC8IlugKSh5m3G hbCl65N08SKmQquNPkc4WJjoAVlzHbgb7K5oOAxD81c5k50oPdfq68CpygyY4bVSvHmk zvUbksCLe3Z6LdodLjUkVhifQWQmNeRpJLZdtz4UTpPcZQwhTU9g2V+rZfqN1z4K81MW fxkQ== X-Gm-Message-State: AOJu0YxCF++r7xFCsWn62a9fDKAlsCZtEqWhSpKYZgRlyehfIaXP6UIS OYrH+u9LtJr9valEGKkYTOODrQ== X-Received: by 2002:a05:6a21:789a:b0:133:7276:324b with SMTP id bf26-20020a056a21789a00b001337276324bmr1346719pzc.23.1691141902614; Fri, 04 Aug 2023 02:38:22 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.142]) by smtp.gmail.com with ESMTPSA id c23-20020aa78817000000b00687933946ddsm1214837pfo.23.2023.08.04.02.38.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Aug 2023 02:38:22 -0700 (PDT) From: Chuyi Zhou To: hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, muchun.song@linux.dev Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, wuyun.abel@bytedance.com, robin.lu@bytedance.com, Chuyi Zhou Subject: [RFC PATCH 1/2] mm, oom: Introduce bpf_select_task Date: Fri, 4 Aug 2023 17:38:03 +0800 Message-Id: <20230804093804.47039-2-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230804093804.47039-1-zhouchuyi@bytedance.com> References: <20230804093804.47039-1-zhouchuyi@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds a new hook bpf_select_task in oom_evaluate_task. It takes oc and current iterating task as parameters and returns a result indicating which one is selected by bpf program. Although bpf_select_task is used to bypass the default method, there are some existing rules should be obeyed. Specifically, we skip these "unkillable" tasks(e.g., kthread, MMF_OOM_SKIP, in_vfork()).So we do not consider tasks with lowest score returned by oom_badness except it was caused by OOM_SCORE_ADJ_MIN. If we attach a prog to the hook, the interface is enabled only when we have successfully chosen at least one valid candidate in previous iteraion. This is to avoid that we find nothing if bpf program rejects all tasks. Signed-off-by: Chuyi Zhou --- mm/oom_kill.c | 57 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 612b5597d3af..aec4c55ed49a 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -18,6 +18,7 @@ * kernel subsystems and hints as to where to find out what things do. */ +#include #include #include #include @@ -210,6 +211,16 @@ long oom_badness(struct task_struct *p, unsigned long totalpages) if (!p) return LONG_MIN; + /* + * If task is allocating a lot of memory and has been marked to be + * killed first if it triggers an oom, then set points to LONG_MAX. + * It will be selected unless we keep oc->chosen through bpf interface. + */ + if (oom_task_origin(p)) { + task_unlock(p); + return LONG_MAX; + } + /* * Do not even consider tasks which are explicitly marked oom * unkillable or have been already oom reaped or the are in @@ -305,8 +316,30 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc) return CONSTRAINT_NONE; } +enum bpf_select_ret { + BPF_SELECT_DISABLE, + BPF_SELECT_TASK, + BPF_SELECT_CHOSEN, +}; + +__weak noinline int bpf_select_task(struct oom_control *oc, + struct task_struct *task, long badness_points) +{ + return BPF_SELECT_DISABLE; +} + +BTF_SET8_START(oom_bpf_fmodret_ids) +BTF_ID_FLAGS(func, bpf_select_task) +BTF_SET8_END(oom_bpf_fmodret_ids) + +static const struct btf_kfunc_id_set oom_bpf_fmodret_set = { + .owner = THIS_MODULE, + .set = &oom_bpf_fmodret_ids, +}; + static int oom_evaluate_task(struct task_struct *task, void *arg) { + enum bpf_select_ret bpf_ret = BPF_SELECT_DISABLE; struct oom_control *oc = arg; long points; @@ -329,17 +362,23 @@ static int oom_evaluate_task(struct task_struct *task, void *arg) goto abort; } + points = oom_badness(task, oc->totalpages); + /* - * If task is allocating a lot of memory and has been marked to be - * killed first if it triggers an oom, then select it. + * Do not consider tasks with lowest score value except it was caused + * by OOM_SCORE_ADJ_MIN. Give these tasks a chance to be selected by + * bpf interface. */ - if (oom_task_origin(task)) { - points = LONG_MAX; + if (points == LONG_MIN && task->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) + goto next; + + if (oc->chosen) + bpf_ret = bpf_select_task(oc, task, points); + + if (bpf_ret == BPF_SELECT_TASK) goto select; - } - points = oom_badness(task, oc->totalpages); - if (points == LONG_MIN || points < oc->chosen_points) + if (bpf_ret == BPF_SELECT_CHOSEN || points == LONG_MIN || points < oc->chosen_points) goto next; select: @@ -732,10 +771,14 @@ static struct ctl_table vm_oom_kill_table[] = { static int __init oom_init(void) { + int err; oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper"); #ifdef CONFIG_SYSCTL register_sysctl_init("vm", vm_oom_kill_table); #endif + err = register_btf_fmodret_id_set(&oom_bpf_fmodret_set); + if (err) + pr_warn("error while registering oom fmodret entrypoints: %d", err); return 0; } subsys_initcall(oom_init) -- 2.20.1