Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp3452445rwo; Fri, 4 Aug 2023 05:18:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHRa5hX1ocG5AITlze2gduqZvMfe4venqUEGqtlhw5DOxBBKhk5X0YG0WNbeth48s4/qs8K X-Received: by 2002:ac2:4d88:0:b0:4fd:d18f:2d93 with SMTP id g8-20020ac24d88000000b004fdd18f2d93mr1171799lfe.6.1691151520266; Fri, 04 Aug 2023 05:18:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691151520; cv=none; d=google.com; s=arc-20160816; b=n09LJeqWH0s0cUw9oAW4m33xoXrjP8vBYbZvZbVRK4zlPOuOHeK/Ik+cfgnEs3JjLa w5PxwGu5wIsr3gVtAgMmYlJJSIQ+EbA6IbcXzoYveiIAkamtjRyEVLQoeoZsElfTW/ML 80h2YcA+wbmcRPKkCGtGwJFVuSnU+WOJI+iri4JtqtQ/yNT0Ap3GDihqn4F2UvlLNUbG DjUSp/3SxSIINy2BIhhrOkC0XSEgYTMepuukaB7yOHa0cCeA6KfHwxAK8IYEXVOAIAsH oy4n7OLinPFKL7zJ6D4qiWB2J4ZfNky0h4R/KJLiYpn09QbnoFo0X5RbnT8oEBKeP69V WUcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=gWX8UAE3vE+ECHESJ7u2irG1vRHlk+PFNMojuMOOzsU=; fh=dS5ezokBs3uSbMDZai6lUOon4HZG0Q9bXakGoyEosYE=; b=z0QXqJfQDuzoJYdHuEHlU3JbANn7CvqBPoabZvw04Ei9qD+6PcjVk1WDwdpUg0uLcv hd1YjjN8WbUg3B9oUk07rmPmESWOGCZdPJcld56OpbQvJzV5TcAGQqXlklSfMF7JMYqy n3LyfRI4+AORVOtI6DPoaqQSXLpUalqNqHt/LtFcrtTM38PXsc2wP9tdhjK7hBLxEJif Z6Tqd1NhmUxs9yV/zy33y8M4QSxcOySJgguWB58UIhWQZh137Jst3HE8tf5H/cQCuiB0 vAJ97ShsMN+iHGyq5oQmVhmRQ85JBDCmVN8v+Oii2aVJFZtgX1piOgMfAOOhpNWUBE27 I/Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=Yi1KQ6Y6; 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=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bo2-20020a0564020b2200b005227e5499e1si36049edb.180.2023.08.04.05.18.15; Fri, 04 Aug 2023 05:18:40 -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=@suse.com header.s=susede1 header.b=Yi1KQ6Y6; 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=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229742AbjHDL3Y (ORCPT + 99 others); Fri, 4 Aug 2023 07:29:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229554AbjHDL3W (ORCPT ); Fri, 4 Aug 2023 07:29:22 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C69711B; Fri, 4 Aug 2023 04:29:18 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 2BAFD1F8AF; Fri, 4 Aug 2023 11:29:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1691148557; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=gWX8UAE3vE+ECHESJ7u2irG1vRHlk+PFNMojuMOOzsU=; b=Yi1KQ6Y6gjCUg17Re2YSDtjdjGq8O6Uw+IWTvdXzRT9SToLnRJl81QhBp9t8Hfuvy/V0EP pWuDnUkcCIr0qRKh7wqscRObmm7NfR5fM/PeDHmDD8Jj2V+a2HDs+GFQGaAN2EvbiZ/BwE xsClVxAsEAt5HYkQjUDARP66k3B87Kk= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 024B2133B5; Fri, 4 Aug 2023 11:29:16 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Jh09OQzhzGQ3VAAAMHmgww (envelope-from ); Fri, 04 Aug 2023 11:29:16 +0000 Date: Fri, 4 Aug 2023 13:29:16 +0200 From: Michal Hocko To: Chuyi Zhou Cc: hannes@cmpxchg.org, roman.gushchin@linux.dev, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, muchun.song@linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, wuyun.abel@bytedance.com, robin.lu@bytedance.com Subject: Re: [RFC PATCH 1/2] mm, oom: Introduce bpf_select_task Message-ID: References: <20230804093804.47039-1-zhouchuyi@bytedance.com> <20230804093804.47039-2-zhouchuyi@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230804093804.47039-2-zhouchuyi@bytedance.com> 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_BLOCKED,SPF_PASS, T_SPF_HELO_TEMPERROR,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 On Fri 04-08-23 17:38:03, Chuyi Zhou wrote: > 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. Is this really necessary? I do get why we need to preserve OOM_SCORE_ADJ_* semantic for in-kernel oom selection logic but why should an arbitrary oom policy care. Look at it from an arbitrary user space based policy. It just picks a task or memcg and kills taks by sending SIG_KILL (or maybe SIG_TERM first) signal. oom_score constrains will not prevent anybody from doing that. tsk_is_oom_victim (and MMF_OOM_SKIP) is a slightly different case but not too much. The primary motivation is to prevent new oom victims while there is one already being killed. This is a reasonable heuristic especially with the async oom reclaim (oom_reaper). It also reduces amount of oom emergency memory reserves to some degree but since those are not absolute this is no longer the primary motivation. _But_ I can imagine that some policies might be much more aggresive and allow to select new victims if preexisting are not being killed in time. oom_unkillable_task is a general sanity check so it should remain in place. I am not really sure about oom_task_origin. That is just a very weird case and I guess it wouldn't hurt to keep it in generic path. All that being said I think we want something like the following (very pseudo-code). I have no idea what is the proper way how to define BPF hooks though so a help from BPF maintainers would be more then handy --- diff --git a/include/linux/nmi.h b/include/linux/nmi.h index 00982b133dc1..9f1743ee2b28 100644 --- a/include/linux/nmi.h +++ b/include/linux/nmi.h @@ -190,10 +190,6 @@ static inline bool trigger_all_cpu_backtrace(void) { return false; } -static inline bool trigger_allbutself_cpu_backtrace(void) -{ - return false; -} static inline bool trigger_cpumask_backtrace(struct cpumask *mask) { return false; diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 612b5597d3af..c9e04be52700 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -317,6 +317,22 @@ static int oom_evaluate_task(struct task_struct *task, void *arg) if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc)) goto next; + /* + * If task is allocating a lot of memory and has been marked to be + * killed first if it triggers an oom, then select it. + */ + if (oom_task_origin(task)) { + points = LONG_MAX; + goto select; + } + + switch (bpf_oom_evaluate_task(task, oc, &points)) { + case -EOPNOTSUPP: break; /* No BPF policy */ + case -EBUSY: goto abort; /* abort search process */ + case 0: goto next; /* ignore process */ + default: goto select; /* note the task */ + } + /* * This task already has access to memory reserves and is being killed. * Don't allow any other task to have access to the reserves unless @@ -329,15 +345,6 @@ static int oom_evaluate_task(struct task_struct *task, void *arg) goto abort; } - /* - * If task is allocating a lot of memory and has been marked to be - * killed first if it triggers an oom, then select it. - */ - if (oom_task_origin(task)) { - points = LONG_MAX; - goto select; - } - points = oom_badness(task, oc->totalpages); if (points == LONG_MIN || points < oc->chosen_points) goto next; -- Michal Hocko SUSE Labs