Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1027897yba; Thu, 4 Apr 2019 02:46:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqyeo6A+M8eUdXsfM8aBfsaf6R2NyH7IBb1euaxvMy6Ih9z/kJyZy4dcr4sFZcLr6tKBmWSX X-Received: by 2002:a17:902:ab91:: with SMTP id f17mr735429plr.151.1554371162221; Thu, 04 Apr 2019 02:46:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554371162; cv=none; d=google.com; s=arc-20160816; b=bwUC9F7vmNUXRv6wiu8M85Neqn5ZfW76gQ/ahKFG4XeM+Pl5tA5r5dr9sB6mYAI5ii 0O7BBjzjY4H/ZGWSNmtKV/JxyO/KymOTyKYyu+6ZuUZGvdYfEhpB2exbHnKWw8pkPo4g ZPNl5MAjtJmgt1OJNC5IpL7uau21WQIuFzHl2QJUunrzAYTuOTF4I6CYdkhHvBS0NJr3 183jcD718o6ksppUhyxoNtrWabtrAnyTuh76yHgOIwKCiUzBudRKF+EZAZbNgXJn5HXg fgagoowGvcfgXDFMYu/behoSGcIKNwOVjLApHn6kDxnBxc0THOqEwNx9RQr+9HBoN6Sl nvWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/ZmeYlysE51xCC3MmMtA0ppB4sVY4PynSdlFSAGKQBE=; b=v7krAlupYayVOQ72WOEkvETdTlJs5v3lzoaRF3tkJbmt+m4W+0/zeIuet33y/xswpk Vm+1P9WkvEw1gjT9qj/Lz6w7ODfoH/ABmXTV1EI+H2VkppAa8QbRjY0tyfmCk3e9QHt0 zI8bHr3w6yaX6oWl/FBERns7rT+AXVAJ6T92e3LvXoz5z6LW10HxheWXHg2zcwIlY3w4 XxsWnOvtnNCl/OrYNSPBnsKhKRtdMSfV1JsdjR1NfLrrpyoM/KehiX73hGAlYpt1vbNU VPr/J1SwdvVuNzoGCK8BgvOIunNNqiNCtplFkx9jevuk49BFYA1oZcgFoT1GQHneTg3i zWJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MV2du5rP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w15si16049850pgj.427.2019.04.04.02.45.46; Thu, 04 Apr 2019 02:46:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MV2du5rP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731205AbfDDJAL (ORCPT + 99 others); Thu, 4 Apr 2019 05:00:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:36730 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726790AbfDDJAJ (ORCPT ); Thu, 4 Apr 2019 05:00:09 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4A77D2147C; Thu, 4 Apr 2019 09:00:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554368407; bh=HyiRK5TXYbUZAnhey+B+rcvNUqgHyMYi4D17qHDW10w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MV2du5rPgG5KmcMjh30cRf9X116Xa97gpqX7RPb4ut3h9F165GcvDXS2OCTZ4M+LK E6e1xWJHYju5jfnRtUNwtVkD4GdF5rPkYf8dOG8l6m4mHCVXedcnG5raU2qx/Ypr8a lqgIeTJV5YOjN9qIKXDijMi0PL0VPw1UdzHvWqNA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tetsuo Handa , Michal Hocko , Johannes Weiner , David Rientjes , Kirill Tkhai , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.19 028/187] memcg: killed threads should not invoke memcg OOM killer Date: Thu, 4 Apr 2019 10:46:05 +0200 Message-Id: <20190404084604.405337736@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190404084603.119654039@linuxfoundation.org> References: <20190404084603.119654039@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 7775face207922ea62a4e96b9cd45abfdc7b9840 ] If a memory cgroup contains a single process with many threads (including different process group sharing the mm) then it is possible to trigger a race when the oom killer complains that there are no oom elible tasks and complain into the log which is both annoying and confusing because there is no actual problem. The race looks as follows: P1 oom_reaper P2 try_charge try_charge mem_cgroup_out_of_memory mutex_lock(oom_lock) out_of_memory oom_kill_process(P1,P2) wake_oom_reaper mutex_unlock(oom_lock) oom_reap_task mutex_lock(oom_lock) select_bad_process # no victim The problem is more visible with many threads. Fix this by checking for fatal_signal_pending from mem_cgroup_out_of_memory when the oom_lock is already held. The oom bypass is safe because we do the same early in the try_charge path already. The situation migh have changed in the mean time. It should be safe to check for fatal_signal_pending and tsk_is_oom_victim but for a better code readability abstract the current charge bypass condition into should_force_charge and reuse it from that path. " Link: http://lkml.kernel.org/r/01370f70-e1f6-ebe4-b95e-0df21a0bc15e@i-love.sakura.ne.jp Signed-off-by: Tetsuo Handa Acked-by: Michal Hocko Acked-by: Johannes Weiner Cc: David Rientjes Cc: Kirill Tkhai Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/memcontrol.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 9518aefd8cbb..7c712c4565e6 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -248,6 +248,12 @@ enum res_type { iter != NULL; \ iter = mem_cgroup_iter(NULL, iter, NULL)) +static inline bool should_force_charge(void) +{ + return tsk_is_oom_victim(current) || fatal_signal_pending(current) || + (current->flags & PF_EXITING); +} + /* Some nice accessors for the vmpressure. */ struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg) { @@ -1382,8 +1388,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 = should_force_charge() || out_of_memory(&oc); mutex_unlock(&oom_lock); return ret; } @@ -2200,9 +2211,7 @@ retry: * bypass the last charges so that they can exit quickly and * free their memory. */ - if (unlikely(tsk_is_oom_victim(current) || - fatal_signal_pending(current) || - current->flags & PF_EXITING)) + if (unlikely(should_force_charge())) goto force; /* -- 2.19.1