From patchwork Tue Jun 5 13:31:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tetsuo Handa X-Patchwork-Id: 10448279 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A13CB6024A for ; Tue, 5 Jun 2018 13:32:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8248B294C7 for ; Tue, 5 Jun 2018 13:32:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 80BE529387; Tue, 5 Jun 2018 13:32:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BBBE82956B for ; Tue, 5 Jun 2018 13:32:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6EA756B0005; Tue, 5 Jun 2018 09:32:17 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 672E36B0006; Tue, 5 Jun 2018 09:32:17 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53AF76B0007; Tue, 5 Jun 2018 09:32:17 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f70.google.com (mail-pl0-f70.google.com [209.85.160.70]) by kanga.kvack.org (Postfix) with ESMTP id 07C006B0005 for ; Tue, 5 Jun 2018 09:32:17 -0400 (EDT) Received: by mail-pl0-f70.google.com with SMTP id c6-v6so1363452pll.4 for ; Tue, 05 Jun 2018 06:32:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:to:cc:from :subject:message-id:date:user-agent:mime-version:content-language :content-transfer-encoding; bh=D+5r52xyuB8x8quMO8nHw5U+FN47h4U7owsTR2//TJE=; b=PRCzjhn/Ev6NpMG3+w1yLdOB4EmWHfmHX4oTX4vHezDWutSm3Sb5ZBrDliTuLAoqJ0 VlFjN2K8w+l1FRKOpnUrPA8sa5kTDB8gn4EyqtKcUsBU0vFyGwRx0GGsnTgfdaodjexd om7xB8/0U451AMn8jNKE8N0DZ/SVUW8P1e4ZwUaUmFEVMO7jZzzpj6MAZ1LJZ8cksuI8 /MW+/mk7dyZYamOJe5zxYmk3OZPKgvSzRnlBBGkX08Db5bSYvOsMKLS+ITpliGVzVz6g B0aTZcc2MaGr2MEIqzpa857da3Kc0XpP6l1q26++ZbOaR5XgNnVPFXmCSlWf27MWPovx K+Fw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of penguin-kernel@i-love.sakura.ne.jp designates 202.181.97.72 as permitted sender) smtp.mailfrom=penguin-kernel@i-love.sakura.ne.jp X-Gm-Message-State: ALKqPweVC/E1I5QHWa5RWg6pXZr98uL+SVn4nV/mXDvmMVbYKiAvgR0v QVL89xuTB5oJv6EvCS77R8LGcTq+99IwNZeZLpjuHhIr9GxDy/POvEoZKef6UFvker0dOKZITfT mBLXHapyxAmPRufGr6/vjOkTFMU4R3uj6SoGiCQfnAr2fw7SQaHV2K3QK90CutS0uOQ== X-Received: by 2002:a63:7208:: with SMTP id n8-v6mr20709766pgc.420.1528205536675; Tue, 05 Jun 2018 06:32:16 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLcQVUKQ6gVIMqitoawuI8B2nRKVHZI4kk+6j3G4AUSdDDazVuaub+l/n1Q+wIyIZY5uxvN X-Received: by 2002:a63:7208:: with SMTP id n8-v6mr20709684pgc.420.1528205535307; Tue, 05 Jun 2018 06:32:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528205535; cv=none; d=google.com; s=arc-20160816; b=YAgLzBiSfYHzlVCI81sFybPqkzcZbYNr7TaOJBaVE0WqP2+VPwysvXMDVDxsTxxpae utSPIaMehL4qtUvwV2sfPK9xPp5hKH6AdJVPRWFKxWSyDTaZRgjKguvKpmN6sAplY+GH 0/PQqfa7fEpyvKavEDFYftZ1RZbESh3LsPAAljWRFleCAwH/nW4/N63/kRGAtELLECOV cqfpfhdXXbzpmDfZ6gENygili5wjtrkyO6iSNA4mVlE0GhP3dkMv6fr0XSseLBjQyMMq o5/nnHRe4uykGE9yhj19Eh/tGTenzxiYBWBWmkPztGwDNSbqDp31j9dX98U0f/WODINx hMxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:mime-version:user-agent :date:message-id:subject:from:cc:to:arc-authentication-results; bh=D+5r52xyuB8x8quMO8nHw5U+FN47h4U7owsTR2//TJE=; b=ZFSdY5bvNKS/lkxdh9ipiB/Gtp8L4SB7t8bp/LwY1rJ46LYEtKhD4L6Pzo4/U+LBr2 2BmJAntzbj/ohVEnMxNuyZvddUGoQgiu5sPsX5P4oc0xw0jlOGyUEd08IMWVhtaH5e1s 64wjz0pohwSwIkw6rt9cbriMFYjZzjvYuPrtSJtvbxDubPDMUm+oLO8fQEEN7v8jWBVT Yk4RK+r3vFlc82ZZR7RdetngXw726G6GebVTk4I7O0UFl5zMtlQP6IimiP7bPLVZ4IBk TVEIf5ljUS4Nb3djrdhaDTIatfMVxYre7DcURozX2luLXKECl+9nXBy1p3jCA0XMW0tf 9DNg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of penguin-kernel@i-love.sakura.ne.jp designates 202.181.97.72 as permitted sender) smtp.mailfrom=penguin-kernel@i-love.sakura.ne.jp Received: from www262.sakura.ne.jp (www262.sakura.ne.jp. [202.181.97.72]) by mx.google.com with ESMTPS id m37-v6si32639123plg.491.2018.06.05.06.32.14 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Jun 2018 06:32:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of penguin-kernel@i-love.sakura.ne.jp designates 202.181.97.72 as permitted sender) client-ip=202.181.97.72; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of penguin-kernel@i-love.sakura.ne.jp designates 202.181.97.72 as permitted sender) smtp.mailfrom=penguin-kernel@i-love.sakura.ne.jp Received: from fsav105.sakura.ne.jp (fsav105.sakura.ne.jp [27.133.134.232]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id w55DW228050998; Tue, 5 Jun 2018 22:32:02 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav105.sakura.ne.jp (F-Secure/fsigk_smtp/530/fsav105.sakura.ne.jp); Tue, 05 Jun 2018 22:32:02 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/530/fsav105.sakura.ne.jp) Received: from [192.168.1.8] (softbank126074194044.bbtec.net [126.74.194.44]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id w55DW0Fe050985 (version=TLSv1.2 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 5 Jun 2018 22:32:02 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) To: Michal Hocko , David Rientjes , Roman Gushchin Cc: Vladimir Davydov , Johannes Weiner , Andrew Morton , Tejun Heo , linux-mm From: Tetsuo Handa Subject: [RFC] Getting rid of INFLIGHT_VICTIM Message-ID: <10e40484-66e2-d879-e9b7-f50fdc5846ac@i-love.sakura.ne.jp> Date: Tue, 5 Jun 2018 22:31:55 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 Content-Language: en-US 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: X-Virus-Scanned: ClamAV using ClamSMTP The "mm, oom: cgroup-aware OOM killer" patchset is about to introduce INFLIGHT_VICTIM in order to replace open-coded ((void *)-1UL). But (regarding CONFIG_MMU=y case) we have a list of inflight OOM victim threads which are connected to oom_reaper_list. Then, I think that we can check whether there are inflight OOM victims before starting process/memcg list traversal. Since it is likely that only few threads are linked to oom_reaper_list, checking MMF_OOM_SKIP should not be heavy. Then, I think that checking whether there are inflight OOM victims before starting process/memcg list traversal can eliminate the "abort" path (like shown below) and simplify "abort" path handling at out_of_memory(). What do you think? include/linux/memcontrol.h | 4 - mm/memcontrol.c | 14 +---- mm/oom_kill.c | 108 ++++++++++++++++++++++----------------------- 3 files changed, 61 insertions(+), 65 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index d99b71b..73d3094 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -355,8 +355,8 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *, struct mem_cgroup *, struct mem_cgroup_reclaim_cookie *); void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *); -int mem_cgroup_scan_tasks(struct mem_cgroup *, - int (*)(struct task_struct *, void *), void *); +void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, + void (*fn)(struct task_struct *, void *), void *arg); static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 1695f38..188ffbc 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -890,11 +890,10 @@ static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg) * * This function must not be called for the root memory cgroup. */ -int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, - int (*fn)(struct task_struct *, void *), void *arg) +void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, + void (*fn)(struct task_struct *, void *), void *arg) { struct mem_cgroup *iter; - int ret = 0; BUG_ON(memcg == root_mem_cgroup); @@ -903,15 +902,10 @@ int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, struct task_struct *task; css_task_iter_start(&iter->css, 0, &it); - while (!ret && (task = css_task_iter_next(&it))) - ret = fn(task, arg); + while ((task = css_task_iter_next(&it))) + fn(task, arg); css_task_iter_end(&it); - if (ret) { - mem_cgroup_iter_break(memcg, iter); - break; - } } - return ret; } /** diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 8ba6cb8..3b3b8ff 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -304,25 +304,13 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc) return CONSTRAINT_NONE; } -static int oom_evaluate_task(struct task_struct *task, void *arg) +static void oom_evaluate_task(struct task_struct *task, void *arg) { struct oom_control *oc = arg; unsigned long points; if (oom_unkillable_task(task, NULL, oc->nodemask)) - goto next; - - /* - * 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 - * the task has MMF_OOM_SKIP because chances that it would release - * any memory is quite low. - */ - if (!is_sysrq_oom(oc) && tsk_is_oom_victim(task)) { - if (test_bit(MMF_OOM_SKIP, &task->signal->oom_mm->flags)) - goto next; - goto abort; - } + return; /* * If task is allocating a lot of memory and has been marked to be @@ -335,29 +323,22 @@ static int oom_evaluate_task(struct task_struct *task, void *arg) points = oom_badness(task, NULL, oc->nodemask, oc->totalpages); if (!points || points < oc->chosen_points) - goto next; + return; /* Prefer thread group leaders for display purposes */ if (points == oc->chosen_points && thread_group_leader(oc->chosen)) - goto next; + return; select: if (oc->chosen) put_task_struct(oc->chosen); get_task_struct(task); oc->chosen = task; oc->chosen_points = points; -next: - return 0; -abort: - if (oc->chosen) - put_task_struct(oc->chosen); - oc->chosen = (void *)-1UL; - return 1; } /* * Simple selection loop. We choose the process with the highest number of - * 'points'. In case scan was aborted, oc->chosen is set to -1. + * 'points'. */ static void select_bad_process(struct oom_control *oc) { @@ -368,8 +349,7 @@ static void select_bad_process(struct oom_control *oc) rcu_read_lock(); for_each_process(p) - if (oom_evaluate_task(p, oc)) - break; + oom_evaluate_task(p, oc); rcu_read_unlock(); } @@ -476,7 +456,6 @@ bool process_shares_mm(struct task_struct *p, struct mm_struct *mm) */ static struct task_struct *oom_reaper_th; static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait); -static struct task_struct *oom_reaper_list; static DEFINE_SPINLOCK(oom_reaper_lock); void __oom_reap_task_mm(struct mm_struct *mm) @@ -605,14 +584,16 @@ static void oom_reap_task(struct task_struct *tsk) debug_show_all_locks(); done: - tsk->oom_reaper_list = NULL; - /* * Hide this mm from OOM killer because it has been either reaped or * somebody can't call up_write(mmap_sem). */ set_bit(MMF_OOM_SKIP, &mm->flags); + spin_lock(&oom_reaper_lock); + oom_reaper_th->oom_reaper_list = tsk->oom_reaper_list; + spin_unlock(&oom_reaper_lock); + /* Drop a reference taken by wake_oom_reaper */ put_task_struct(tsk); } @@ -620,14 +601,12 @@ static void oom_reap_task(struct task_struct *tsk) static int oom_reaper(void *unused) { while (true) { - struct task_struct *tsk = NULL; + struct task_struct *tsk; - wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL); + wait_event_freezable(oom_reaper_wait, + oom_reaper_th->oom_reaper_list != NULL); spin_lock(&oom_reaper_lock); - if (oom_reaper_list != NULL) { - tsk = oom_reaper_list; - oom_reaper_list = tsk->oom_reaper_list; - } + tsk = oom_reaper_th->oom_reaper_list; spin_unlock(&oom_reaper_lock); if (tsk) @@ -639,15 +618,18 @@ static int oom_reaper(void *unused) static void wake_oom_reaper(struct task_struct *tsk) { - /* tsk is already queued? */ - if (tsk == oom_reaper_list || tsk->oom_reaper_list) - return; - - get_task_struct(tsk); + struct task_struct *p = oom_reaper_th; spin_lock(&oom_reaper_lock); - tsk->oom_reaper_list = oom_reaper_list; - oom_reaper_list = tsk; + while (p != tsk && p->oom_reaper_list) + p = p->oom_reaper_list; + if (p != tsk) { + spin_unlock(&oom_reaper_lock); + return; + } + p->oom_reaper_list = tsk; + tsk->oom_reaper_list = NULL; + get_task_struct(tsk); spin_unlock(&oom_reaper_lock); trace_wake_reaper(tsk->pid); wake_up(&oom_reaper_wait); @@ -1009,6 +991,23 @@ int unregister_oom_notifier(struct notifier_block *nb) } EXPORT_SYMBOL_GPL(unregister_oom_notifier); +static bool oom_has_pending_victims(struct oom_control *oc) +{ +#ifdef CONFIG_MMU + struct task_struct *p; + + spin_lock(&oom_reaper_lock); + for (p = oom_reaper_th; p; p = p->oom_reaper_list) + if (!oom_unkillable_task(p, oc->memcg, oc->nodemask) && + !test_bit(MMF_OOM_SKIP, &p->signal->oom_mm->flags)) + break; + spin_unlock(&oom_reaper_lock); + return p != NULL; +#else + return false; +#endif +} + /** * out_of_memory - kill the "best" process when we run out of memory * @oc: pointer to struct oom_control @@ -1062,6 +1061,9 @@ bool out_of_memory(struct oom_control *oc) oc->nodemask = NULL; check_panic_on_oom(oc, constraint); + if (oom_has_pending_victims(oc)) + return true; + if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task && current->mm && !oom_unkillable_task(current, NULL, oc->nodemask) && current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) { @@ -1073,20 +1075,20 @@ bool out_of_memory(struct oom_control *oc) select_bad_process(oc); /* Found nothing?!?! Either we hang forever, or we panic. */ - if (!oc->chosen && !is_sysrq_oom(oc) && !is_memcg_oom(oc)) { + if (!oc->chosen) { + if (is_sysrq_oom(oc) || is_memcg_oom(oc)) + return false; dump_header(oc, NULL); panic("Out of memory and no killable processes...\n"); } - if (oc->chosen && oc->chosen != (void *)-1UL) { - oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" : - "Memory cgroup out of memory"); - /* - * Give the killed process a good chance to exit before trying - * to allocate memory again. - */ - schedule_timeout_killable(1); - } - return !!oc->chosen; + oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" : + "Memory cgroup out of memory"); + /* + * Give the killed process a good chance to exit before trying + * to allocate memory again. + */ + schedule_timeout_killable(1); + return true; } /*