From patchwork Mon Apr 10 02:50:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gang Li X-Patchwork-Id: 13206080 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE8F6C77B61 for ; Mon, 10 Apr 2023 02:51:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 56738900005; Sun, 9 Apr 2023 22:51:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F002900002; Sun, 9 Apr 2023 22:51:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36B25900005; Sun, 9 Apr 2023 22:51:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 21B44900002 for ; Sun, 9 Apr 2023 22:51:10 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D7BB480ABC for ; Mon, 10 Apr 2023 02:51:09 +0000 (UTC) X-FDA: 80663954658.28.DFF297C Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf06.hostedemail.com (Postfix) with ESMTP id 9DB42180008 for ; Mon, 10 Apr 2023 02:51:05 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=JOtB5P+M; spf=pass (imf06.hostedemail.com: domain of ligang.bdlg@bytedance.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=ligang.bdlg@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681095068; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=rJOefRQrozTqAzaaP0k/ueE5wQvUaa1yyg3cUW9q0RM=; b=ql9Pf8orNBkOP4CMyEnNBB2TsoWlUW8LkgXyk7X9hR229uIMIHZT3RyhsRCpUK8U9wcucv f2FiqrEvB1HQjbJJhivJZjh0CLwaa3n9Z9NIy8HyhTCYQxGyzR3B7vwIxVJVB/CNJpPUVB i/6Iydn1h6uFrklz4KWwbcpo8zGjo5E= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=JOtB5P+M; spf=pass (imf06.hostedemail.com: domain of ligang.bdlg@bytedance.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=ligang.bdlg@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681095068; a=rsa-sha256; cv=none; b=gnYthNsnjSeicML2lJUBFWtU4i7pR+hWC0ANkZuKAmNsCMOPxxmPekGKuh7nH6EuXABu4x DWQiF9yXNnaOp23dH5vmGfS2qJSp1g95kyTfadcety3AEgGri3vz6kqTF3kQHYE04rbpve S3twMit9IK+DRJdmGC+I8Npl1UtUpHI= Received: by mail-pl1-f170.google.com with SMTP id n14so28720709plc.8 for ; Sun, 09 Apr 2023 19:51:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1681095064; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=rJOefRQrozTqAzaaP0k/ueE5wQvUaa1yyg3cUW9q0RM=; b=JOtB5P+MButyoMf9OTRe1POvR1FUCeEkPzDMZv8Bf65mSDeY3ESN4yjsOLA9st3qAm gYuvCr+rS8xZMdOSRNr+GQiBa95YINrW/NhmzXZEaYVvfvgUypoGq2TxvcpODn7tXZm1 88riwOcwh8sXn9EAAiAyjSqpkMvYmbzM8vHEcIb4T9o3IuYX5BYw5DICpMXbolHP6rGi 78Ux6vpGXbjJ7OHnMmCz7XwApLtcLuHnJ1RVGVWtH7Yw1rD49Pm8v4cn74JVV49M9Zde K0QxZfdvJ5ZB6P21qLX9CaZ5CVljB8y33KUQrPL8DMN+OrDzqAXM5j/RrvwNYvB8BamF Jcxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681095064; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rJOefRQrozTqAzaaP0k/ueE5wQvUaa1yyg3cUW9q0RM=; b=zwlK8BiEN+/872IQiGD4KJzr22yXjWbzEvhYY9X7PX2d35Fl9nj8UqKo9bWwDe3Dnl 2hXSPoriGtGreCpFaq1lGSe1TyMZ97+t+CQEQJ2+jw5reNcEMDO7IpbuXNNQh0lWAmLC BBgYWgwKb0j+8aVbuNZu+h0z+idMV65b/pBoc7rMkAtmWWzwpin2Fx4UIfA/mB/RHVBH qMPbzwaeodub+Z1tUr7OOurUL3SCRoFabKYvRJIQztg5W/6O3IX/mbNIL2ixYlfkDSnX hSCarx3xmuuB5MHjHUJlGOf60CTWtHAZIw0j0aTd1sZ99eVhSlmo3RvgBQyqd+hV3Ebe EiVg== X-Gm-Message-State: AAQBX9dxFOiLosWEpDu61iljjhQniCUqR8w0FzTtf+wtUf4UqFd55juQ 2Axbpgc67KOv2tUCUt6owaIBdwPrBVOwvUsrHkk= X-Google-Smtp-Source: AKy350Yf2yL+hcn31obOfvgDB7o8eZAIg8yZihoYaCUkEzKXDRgpjBnJf850MT7CeXE2kvA0SURqjw== X-Received: by 2002:a17:902:e88c:b0:19f:35df:5d60 with SMTP id w12-20020a170902e88c00b0019f35df5d60mr12188126plg.22.1681095064232; Sun, 09 Apr 2023 19:51:04 -0700 (PDT) Received: from C02FT5A6MD6R.bytedance.net ([139.177.225.224]) by smtp.gmail.com with ESMTPSA id jh19-20020a170903329300b00199193e5ea1sm6544674plb.61.2023.04.09.19.51.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Apr 2023 19:51:03 -0700 (PDT) From: Gang Li To: Waiman Long , Michal Hocko Cc: Gang Li , cgroups@vger.kernel.org, linux-mm@kvack.org, rientjes@google.com, Zefan Li , linux-kernel@vger.kernel.org Subject: [PATCH v3] mm: oom: introduce cpuset oom Date: Mon, 10 Apr 2023 10:50:55 +0800 Message-Id: <20230410025056.22103-1-ligang.bdlg@bytedance.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9DB42180008 X-Stat-Signature: ctgyxofc48azrekf8yzncxfie6rpk9au X-HE-Tag: 1681095065-932022 X-HE-Meta: U2FsdGVkX1/7CMraHxyJsmZKx5YyuKyhSScy7ZT7G+LlLgCkN4UslDGq+FNcB5KRKRyxfuXGPCz2SVgObQTpiu75zV/lul0zbYA3rD55aUJRywTv94RXSzegqV0lqJr5K73cOl3YK8AeJHJtfByOfr6OGpdYS/0rqHCE64cravuCNZP25ZN5bF9uVaD2FwtHRnzWUWa+ES0cRJRrypkkKgfgPDysxtb0btnOjf0N5xApsAlVUKkRJzCIVsTUzc5QzRIxMsaKVfL+6FZtZl8lsFQVoBq0ZXQe+t5pqBD1VCu2Qf3cs8GHB5HbKpkdsyNkKtDZISRMrbxYd/1AJ5bLFx4GzdcUyeDCugpsob2/7zAVoE4dDrZ+WwU9c4carDx4wTW6HDqR6kQpmwHRabswZCzB1UbZErRYVT7GjvAp5X6BKuL2NUGtGGGd3FGxWehbUrGN28fYDjoOIp2A+DhiaqC1tKAxCF/Dkmi/aHV9vENip1l/yGJZ1M/2+Wqm71XlAYbD2mJI2uJHawMxgrlEMqVb479aaE7NR42MLV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XO3LyUGwMQutybIosy0w0rs4omLMoE3MdzDcNv92XEi/phYFAtFZoCkw5OVXUJm5CdbPO8kXeARilc7n2J1FH3xPfYIiiosKi2fLggXdl/QQUhpBs3NcDHn46IIWbx X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Cpusets constrain the CPU and Memory placement of tasks. `CONSTRAINT_CPUSET` type in oom has existed for a long time, but has never been utilized. When a process in cpuset which constrain memory placement triggers oom, it may kill a completely irrelevant process on other numa nodes, which will not release any memory for this cpuset. We can easily achieve node aware oom by using `CONSTRAINT_CPUSET` and selecting victim from all cpusets with the same mems_allowed as the current cpuset. Example: Create two processes named mem_on_node0 and mem_on_node1 constrained by cpusets respectively. These two processes alloc memory on their own node. Now node0 has run out of memory, OOM will be invokled by mem_on_node0. Before this patch: Since `CONSTRAINT_CPUSET` do nothing, the victim will be selected from the entire system. Therefore, the OOM is highly likely to kill mem_on_node1, which will not free any memory for mem_on_node0. This is a useless kill. ``` [ 2786.519080] mem_on_node0 invoked oom-killer [ 2786.885738] [ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name [ 2787.181724] [ 13432] 0 13432 787016 786745 6344704 0 0 mem_on_node1 [ 2787.189115] [ 13457] 0 13457 787002 785504 6340608 0 0 mem_on_node0 [ 2787.216534] oom-kill:constraint=CONSTRAINT_CPUSET,nodemask=(null),cpuset=test,mems_allowed=0 [ 2787.229991] Out of memory: Killed process 13432 (mem_on_node1) ``` After this patch: The victim will be selected only in all cpusets that have the same mems_allowed as the cpuset that invoked oom. This will prevent useless kill and protect innocent victims. ``` [ 395.922444] mem_on_node0 invoked oom-killer [ 396.239777] [ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name [ 396.246128] [ 2614] 0 2614 1311294 1144192 9224192 0 0 mem_on_node0 [ 396.252655] oom-kill:constraint=CONSTRAINT_CPUSET,nodemask=(null),cpuset=test,mems_allowed=0 [ 396.264068] Out of memory: Killed process 2614 (mem_on_node0) ``` Suggested-by: Michal Hocko Cc: Cc: Cc: Cc: Waiman Long Cc: Zefan Li Signed-off-by: Gang Li --- Changes in v3: - Provide more details about the use case, testing, implementation. - Document the userspace visible change in Documentation. - Rename cpuset_cgroup_scan_tasks() to cpuset_scan_tasks() and add a doctext comment about its purpose and how it should be used. - Take cpuset_rwsem to ensure that cpusets are stable. Changes in v2: - https://lore.kernel.org/all/20230404115509.14299-1-ligang.bdlg@bytedance.com/ - Select victim from all cpusets with the same mems_allowed as the current cpuset. (David Rientjes ) v1: - https://lore.kernel.org/all/20220921064710.89663-1-ligang.bdlg@bytedance.com/ - Introduce cpuset oom. --- .../admin-guide/cgroup-v1/cpusets.rst | 14 +++++- include/linux/cpuset.h | 6 +++ kernel/cgroup/cpuset.c | 44 +++++++++++++++++++ mm/oom_kill.c | 4 ++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v1/cpusets.rst b/Documentation/admin-guide/cgroup-v1/cpusets.rst index 5d844ed4df69..d686cd47e91d 100644 --- a/Documentation/admin-guide/cgroup-v1/cpusets.rst +++ b/Documentation/admin-guide/cgroup-v1/cpusets.rst @@ -25,7 +25,8 @@ Written by Simon.Derr@bull.net 1.6 What is memory spread ? 1.7 What is sched_load_balance ? 1.8 What is sched_relax_domain_level ? - 1.9 How do I use cpusets ? + 1.9 What is cpuset oom ? + 1.10 How do I use cpusets ? 2. Usage Examples and Syntax 2.1 Basic Usage 2.2 Adding/removing cpus @@ -607,8 +608,17 @@ If your situation is: - The latency is required even it sacrifices cache hit rate etc. then increasing 'sched_relax_domain_level' would benefit you. +1.9 What is cpuset oom ? +-------------------------- +If there is no available memory to allocate on the nodes specified by +cpuset.mems, then an OOM (Out-Of-Memory) will be invoked. + +Since the victim selection is a heuristic algorithm, we cannot select +the "perfect" victim. Therefore, currently, the victim will be selected +from all the cpusets that have the same mems_allowed as the cpuset +which invoked OOM. -1.9 How do I use cpusets ? +1.10 How do I use cpusets ? -------------------------- In order to minimize the impact of cpusets on critical kernel diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h index 980b76a1237e..75465bf58f74 100644 --- a/include/linux/cpuset.h +++ b/include/linux/cpuset.h @@ -171,6 +171,8 @@ static inline void set_mems_allowed(nodemask_t nodemask) task_unlock(current); } +int cpuset_scan_tasks(int (*fn)(struct task_struct *, void *), void *arg); + #else /* !CONFIG_CPUSETS */ static inline bool cpusets_enabled(void) { return false; } @@ -287,6 +289,10 @@ static inline bool read_mems_allowed_retry(unsigned int seq) return false; } +static inline int cpuset_scan_tasks(int (*fn)(struct task_struct *, void *), void *arg) +{ + return 0; +} #endif /* !CONFIG_CPUSETS */ #endif /* _LINUX_CPUSET_H */ diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index bc4dcfd7bee5..4c51225568aa 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -4013,6 +4013,50 @@ void cpuset_print_current_mems_allowed(void) rcu_read_unlock(); } +/** + * cpuset_scan_tasks - specify the oom scan range + * @fn: callback function to select oom victim + * @arg: argument for callback function, usually a pointer to struct oom_control + * + * Description: This function is used to specify the oom scan range. Return 0 if + * no task is selected, otherwise return 1. The selected task will be stored in + * arg->chosen. Thins function can only be called in select_bad_process() + * while oc->onstraint == CONSTRAINT_CPUSET. + * + * The selection algorithm is heuristic, therefore requires constant iteration + * based on user feedback. Currently, we just iterate through all cpusets with + * the same mems_allowed as the current cpuset. + */ +int cpuset_scan_tasks(int (*fn)(struct task_struct *, void *), void *arg) +{ + int ret = 0; + struct css_task_iter it; + struct task_struct *task; + struct cpuset *cs; + struct cgroup_subsys_state *pos_css; + + /* + * Situation gets complex with overlapping nodemasks in different cpusets. + * TODO: Maybe we should calculate the "distance" between different mems_allowed. + * + * But for now, let's make it simple. Just iterate through all cpusets + * with the same mems_allowed as the current cpuset. + */ + cpuset_read_lock(); + rcu_read_lock(); + cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { + if (nodes_equal(cs->mems_allowed, task_cs(current)->mems_allowed)) { + css_task_iter_start(&(cs->css), CSS_TASK_ITER_PROCS, &it); + while (!ret && (task = css_task_iter_next(&it))) + ret = fn(task, arg); + css_task_iter_end(&it); + } + } + rcu_read_unlock(); + cpuset_read_unlock(); + return ret; +} + /* * Collection of memory_pressure is suppressed unless * this flag is enabled by writing "1" to the special diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 044e1eed720e..228257788d9e 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -367,6 +367,8 @@ static void select_bad_process(struct oom_control *oc) if (is_memcg_oom(oc)) mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc); + else if (oc->constraint == CONSTRAINT_CPUSET) + cpuset_scan_tasks(oom_evaluate_task, oc); else { struct task_struct *p; @@ -427,6 +429,8 @@ static void dump_tasks(struct oom_control *oc) if (is_memcg_oom(oc)) mem_cgroup_scan_tasks(oc->memcg, dump_task, oc); + else if (oc->constraint == CONSTRAINT_CPUSET) + cpuset_scan_tasks(dump_task, oc); else { struct task_struct *p;