Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5656103img; Wed, 27 Mar 2019 12:33:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqz+uc4u4fFZjrv4F967unHnWl9fyOSJJLTQjp9eO80zkS3ZvepMxs5XyPmf5kEzE/joaKFF X-Received: by 2002:a62:e315:: with SMTP id g21mr35911962pfh.2.1553715229195; Wed, 27 Mar 2019 12:33:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553715229; cv=none; d=google.com; s=arc-20160816; b=ZxEHIKn9msN8fyTXgPBVCpeF5naa+6UrwB93A4CCGz1DDEWwy0SRCK8B9WistyPt+f sAn6BpRL07CVzvGoQKsNzoOMyhL5tMlqQCW1wtB4y/olmGjB8Adn8IFqj87xNhcX8ame 2N1LY0kdb4vsT9oGhr6cqOWBGhp0wrYZt5h1IAH3OPSmMeUjUheKfniv5iJom3nZ7pC6 8b6Lf2fPm/YOI7aolwa2GjcWEE4qxKoAAtVuEYfv4XAgvCETZGTlPAI477yO3abIRVY5 OLLaV/R+miOjuQLckedFK5lEQthMPFq0+Z6omg7D6DrYsflYiBtbeyg3AT27WmM/W/Rp CLyQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qGRaJYdU0RPlNYy/n/qyDjRuBW/Cix7lpsYJgd4zT0Q=; b=Sn0VlzFLxegTX3jnfPtHTaK6cMyAHWHp3Uw1npjMlcVfHxwm82Cq8Kex0FjfrQm1Na 3l6XThL1NP2rIT+v2HqARFJDQo5lxMdRpk4cMpMd9CSoKcniwWl9IN3DXZ/AdT8pea24 8AFp/WpaQQBY3jF5MxnOgsArU+VyRpNnroGWgUntpbcBijGNombXh0fZ/1XxUbtP/oZd VV1Jn3vcrGpmE0cDyoA5zVHvo4lDJtl7VB39g/9/+5Cm09pgnHQo7qE+s5WmIZ8XsPWk 98g0vGrb1oQQ3P8ik+XcPT85SU9+3zzchTU5npP22nTLzwESkAahpNcPU11SdJAAwUMf TQCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CaENcYPG; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z5si18588240pgb.415.2019.03.27.12.33.34; Wed, 27 Mar 2019 12:33:49 -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=CaENcYPG; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733044AbfC0Tbp (ORCPT + 99 others); Wed, 27 Mar 2019 15:31:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:43088 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731250AbfC0SDE (ORCPT ); Wed, 27 Mar 2019 14:03:04 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9D64121738; Wed, 27 Mar 2019 18:03:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553709783; bh=pPf0xG7tuJ/KeSsZbMSwoj8AVi5Un66nVo/A/1eQSFM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CaENcYPGYio1KqBnWQi9APPoYaZrdGohufPuawQZc6bmaKTC+HEtPqHFEQfeb6r89 I88mB8kxI+OtVjI4yYiXc2533KwBuE+4qxIVzV9fwR0/I6x6XWKoHF2JNNgSYxV6nc QTMDL77c0VJaKPZprfS3hg6l7PPmpxyDUaGu3ku0= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Tetsuo Handa , David Rientjes , Kirill Tkhai , Andrew Morton , Linus Torvalds , Sasha Levin , cgroups@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH AUTOSEL 5.0 036/262] memcg: killed threads should not invoke memcg OOM killer Date: Wed, 27 Mar 2019 13:58:11 -0400 Message-Id: <20190327180158.10245-36-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327180158.10245-1-sashal@kernel.org> References: <20190327180158.10245-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tetsuo Handa [ 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 af7f18b32389..79a7d2a06bba 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) { @@ -1389,8 +1395,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; } @@ -2209,9 +2220,7 @@ static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, * 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