From patchwork Wed Dec 26 10:13:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tetsuo Handa X-Patchwork-Id: 10742957 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 65FC113BF for ; Wed, 26 Dec 2018 10:14:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 46C9025EF7 for ; Wed, 26 Dec 2018 10:14:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 353DC27F88; Wed, 26 Dec 2018 10:14:03 +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 DB47D25EF7 for ; Wed, 26 Dec 2018 10:14:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 57AE98E0002; Wed, 26 Dec 2018 05:14:00 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 52AFE8E0001; Wed, 26 Dec 2018 05:14:00 -0500 (EST) 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 440138E0002; Wed, 26 Dec 2018 05:14:00 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-oi1-f197.google.com (mail-oi1-f197.google.com [209.85.167.197]) by kanga.kvack.org (Postfix) with ESMTP id 1D74D8E0001 for ; Wed, 26 Dec 2018 05:14:00 -0500 (EST) Received: by mail-oi1-f197.google.com with SMTP id e141so11104814oig.11 for ; Wed, 26 Dec 2018 02:14:00 -0800 (PST) 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:from:to:cc :subject:date:message-id; bh=WjncdfyEMu5g7ofcFTEBk8862YYj7niuy07HpjhGmvA=; b=CsrJiwHmKaa2gAk9GqX/MBFSDuZ1KFq8xDqT8ZV1VZP/AmGK8oNCJ4nJEnG+lfvVuF XQieAVAAQwwTRI3TXHf/c7LogMBGWUxWDoQSDOC7ze1sObHvXkp5OdlYeDc3/wk3rOoe smR70PbGOdvtJuu1YopyG0mZTMAp7RbkzxEA2RSy6xQNLTZ6/aqQ0XTSMwkyPtX6By5f 1svOq/HvMOv2ZUC8E/+OoxqkC8G82fLE13r6k7tIy2SFVmhxR3WqfziRL7Rp/UFeq3p3 TH3f2EwiQ0eiv6ivpRmOeoZi6L19FjFk2ijrWZAW0szpYBseRBHWQ+DsPS1qZEpj7Sfq PbVQ== 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: AA+aEWa448sLjZoklQVLRqwRSfZPS/cqz2Tyxi+SRQ27Auwjh6S3z/sk OrFuQ5w5+B6V5ZYXQedSiXU0lHKe4HVGQcgzAzin4zDRJc8X0BdaWbqNrUqbNOb/Owz1/k6xfqT ZwEnkM1qxp4Pe4mgxGLXf1lNQIR/UTkyOdmIKvJV6YNziN0cQR66/OPThKk6rjQPzFA== X-Received: by 2002:aca:d905:: with SMTP id q5mr12730937oig.0.1545819239628; Wed, 26 Dec 2018 02:13:59 -0800 (PST) X-Google-Smtp-Source: AFSGD/XU2i0lnCgirqb8yc8uonjIUhHkJs1yOM/orOgZxDkxpAYtd7VuDsyWqyBeIpNHsp+WxLfV X-Received: by 2002:aca:d905:: with SMTP id q5mr12730916oig.0.1545819238763; Wed, 26 Dec 2018 02:13:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545819238; cv=none; d=google.com; s=arc-20160816; b=LWb5bYj3rSbONcwPXmGScp66diCmFQ1OIkK/s18poSbTLbfQVgoxkfyA8Sg7Qlbh85 p9yDeJtu+3owoPwvVISa3Ms9dmL5z8BYz/wiiXLiISagiP0gisCEnIINzuPsvaxP//+h Ql/jVcI2TK9mktQBwqaOtG1YjWEx5N27ol1+rTK3XWZQ4mI/84TU1L+eEZHQpM3oKreM mXwpJK+4zERXC8ylrwq78MnxRNzCAtTfD8i+IybPkVB8x2zeewmMvJMi93+zy756Qt68 nZKaBlhTgYK1G5V1iEJZhHtpHl+7B+jxQj+BqQtZ1ModFJDIinhfX3hGeshuwRQkhXc3 6KSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from; bh=WjncdfyEMu5g7ofcFTEBk8862YYj7niuy07HpjhGmvA=; b=mOvQy5koEVGO2KyMq+airbzr8hIVMD778PbfRdf4HStTlT1g6VOCOH/En2I28+YOAH beeqhui3cd0A1i4Gs6p2u5SDcJUjPQvgKGN25tIaJ2Cnav4uLFK33tlLlZW88WgPjGiI ur6cjDl2IINJbu/rsmpkiKGkmZFpLHdY/JTSD3oJgxsjr7zyXHCRTIoIBDv5hjoZcm+O LifAVePXYmr6W6oadJ6rebFa5FqkeHMOSYJ5y3ux2joNlc5i03BXBPh58WY0xoDBuQML avTf3Qj+GG07aKq0Id4oiS+bpDtEsQBkwMAPSv+RxrGPieG0u9d8cplAX+Rw0TGFx0QA E3Iw== 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 z36si14393068ota.0.2018.12.26.02.13.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Dec 2018 02:13:58 -0800 (PST) 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 wBQADcsr036595; Wed, 26 Dec 2018 19:13:38 +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); Wed, 26 Dec 2018 19:13:38 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/530/fsav105.sakura.ne.jp) Received: from ccsecurity.localdomain (softbank126126163036.bbtec.net [126.126.163.36]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id wBQADXV0036553 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 26 Dec 2018 19:13:38 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) From: Tetsuo Handa To: Michal Hocko , Andrew Morton Cc: Johannes Weiner , David Rientjes , linux-mm@kvack.org, Tetsuo Handa Subject: [PATCH] memcg: killed threads should not invoke memcg OOM killer Date: Wed, 26 Dec 2018 19:13:35 +0900 Message-Id: <1545819215-10892-1-git-send-email-penguin-kernel@I-love.SAKURA.ne.jp> X-Mailer: git-send-email 1.8.3.1 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 It is possible that a single process group memcg easily swamps the log with no-eligible OOM victim messages after current thread was OOM-killed, due to race between the memcg charge and the OOM reaper [1]. Thread-1 Thread-2 OOM reaper try_charge() mem_cgroup_out_of_memory() mutex_lock(oom_lock) try_charge() mem_cgroup_out_of_memory() mutex_lock(oom_lock) out_of_memory() select_bad_process() oom_kill_process(current) wake_oom_reaper() oom_reap_task() # sets MMF_OOM_SKIP mutex_unlock(oom_lock) out_of_memory() select_bad_process() # no task mutex_unlock(oom_lock) We don't need to invoke the memcg OOM killer if current thread was killed when waiting for oom_lock, for mem_cgroup_oom_synchronize(true) and memory_max_write() can bail out upon SIGKILL, and try_charge() allows already killed/exiting threads to make forward progress. Michal has a plan to use tsk_is_oom_victim() by calling mark_oom_victim() on all thread groups sharing victim's mm. But fatal_signal_pending() in this patch helps regardless of Michal's plan because it will avoid needlessly calling out_of_memory() when current thread is already terminating (e.g. got SIGINT after passing fatal_signal_pending() check in try_charge() and mutex_lock_killable() did not block). [1] https://lkml.kernel.org/r/ea637f9a-5dd0-f927-d26d-d0b4fd8ccb6f@i-love.sakura.ne.jp Signed-off-by: Tetsuo Handa Signed-off-by: Tetsuo Handa Signed-off-by: Tetsuo Handa --- mm/memcontrol.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b860dd4f7..b0d3bf3 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1389,8 +1389,13 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, }; bool ret; - mutex_lock(&oom_lock); - ret = out_of_memory(&oc); + if (mutex_lock_killable(&oom_lock)) + return true; + /* + * A few threads which were not waiting at mutex_lock_killable() can + * fail to bail out. Therefore, check again after holding oom_lock. + */ + ret = fatal_signal_pending(current) || out_of_memory(&oc); mutex_unlock(&oom_lock); return ret; }