From patchwork Mon Nov 8 21:19:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 12609111 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8698AC4167B for ; Mon, 8 Nov 2021 21:20:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1F153614C8 for ; Mon, 8 Nov 2021 21:20:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 1F153614C8 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B74246B0073; Mon, 8 Nov 2021 16:20:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B22E66B0074; Mon, 8 Nov 2021 16:20:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A13036B0075; Mon, 8 Nov 2021 16:20:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0009.hostedemail.com [216.40.44.9]) by kanga.kvack.org (Postfix) with ESMTP id 910AD6B0073 for ; Mon, 8 Nov 2021 16:20:13 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 553597CA53 for ; Mon, 8 Nov 2021 21:20:13 +0000 (UTC) X-FDA: 78787031106.16.2386E94 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf13.hostedemail.com (Postfix) with ESMTP id 91387104F7B8 for ; Mon, 8 Nov 2021 21:20:02 +0000 (UTC) Received: by mail-pl1-f201.google.com with SMTP id m17-20020a170902db1100b001421cb34857so6903806plx.15 for ; Mon, 08 Nov 2021 13:20:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=HuOsUesI41VMxHbd9WL9Fs5X3h8wZbAlymoJuWa+M0o=; b=mgCbALj0RzI1y7dYKpXbyaiAMLfsKBudFfa7Ub2nVulXL0OgGprL2nvZQ5jgnYWFQk FyRwZRuXkDXDZQg/EmG5RPSKJ0THWJP0Gn8tavb7JhK0Lx6F7Dj9+4l1egra+m7YtrPy lpmGx4XlaWZ9qtVHp2GizypVKABYErQ15IN9nQhWjo3/L/uwJ8MJb1L7MoiMNoVeyH0V iwZn7AfMAJUXEPjwLeaNB9ZvVpm0reoL4Ua4Yl6E+SYJvPNIlcuk4x63koSryTbpjh2C Dfoc/LjYIFQeWJUEHu9QP2jJta84c10T9HT3Jpk5hQhWEHbsuURruitDfys71o4MWJGi BxHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=HuOsUesI41VMxHbd9WL9Fs5X3h8wZbAlymoJuWa+M0o=; b=GSunfRrTPv7wc4TTXN7Z0kxRQ9+q5qHKgDP0NEVLqJB3M4Y9lJdg0rp10yVEcutUGL yN6hj0ntfsk4krntwXK6crDlYiMzVPppmmNT8tvTv8UClLLmydPhlEu2RV1aqy0q7gKl rE72iW2asN363pT91/FCb/oKNfbnMd45w1RtfjEw+rzvPHrXjV3ok5n6Qzjaqnq7YA/A ngtJ7IOiiBAZuRmAi3Hs1cqTfpkmZPz9ywcZ2wxhCj/uuYC7CIUqEizSAz9jjQfxxgBr vm5f2S7yPNVU3od0BG6gzrqmdl8FdmqWR4ti32X20htrjWu9J7LgjSpUUjp0BIOYfnQw TvyA== X-Gm-Message-State: AOAM532b9nVN1WLb7AD5rTcwNDr+HNb6q4BtwSSibqH+yDtZ2dBqAK9P wKXHPYQQhQypiBTt+iocVqdKo7Ndr41HsoH8Dg== X-Google-Smtp-Source: ABdhPJxtKMCyalpwo6Rk4XPSadnMPMqQz4M27ofn10pg+q/fy4JS+sm19K/CP1QUZKWIjfQ6AuvOtu9HpgCeOuOOeQ== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2cd:202:8717:7707:fb59:664e]) (user=almasrymina job=sendgmr) by 2002:aa7:88cb:0:b0:49f:ad17:c08 with SMTP id k11-20020aa788cb000000b0049fad170c08mr2017245pff.19.1636406411895; Mon, 08 Nov 2021 13:20:11 -0800 (PST) Date: Mon, 8 Nov 2021 13:19:57 -0800 In-Reply-To: <20211108211959.1750915-1-almasrymina@google.com> Message-Id: <20211108211959.1750915-4-almasrymina@google.com> Mime-Version: 1.0 References: <20211108211959.1750915-1-almasrymina@google.com> X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH v1 3/5] mm/oom: handle remote ooms From: Mina Almasry Cc: Mina Almasry , Michal Hocko , "Theodore Ts'o" , Greg Thelen , Shakeel Butt , Andrew Morton , Hugh Dickins , Roman Gushchin , Johannes Weiner , Tejun Heo , Vladimir Davydov , riel@surriel.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 91387104F7B8 X-Stat-Signature: iubrmj5iym7tf489eubb6bqhsk5i9rnt Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=mgCbALj0; spf=pass (imf13.hostedemail.com: domain of 3i5SJYQsKCOwOZaOgfmaWbOUccUZS.QcaZWbil-aaYjOQY.cfU@flex--almasrymina.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3i5SJYQsKCOwOZaOgfmaWbOUccUZS.QcaZWbil-aaYjOQY.cfU@flex--almasrymina.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1636406402-897352 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: On remote ooms (OOMs due to remote charging), the oom-killer will attempt to find a task to kill in the memcg under oom, if the oom-killer is unable to find one, the oom-killer should simply return ENOMEM to the allocating process. If we're in pagefault path and we're unable to return ENOMEM to the allocating process, we instead kill the allocating process. Signed-off-by: Mina Almasry Cc: Michal Hocko Cc: Theodore Ts'o Cc: Greg Thelen Cc: Shakeel Butt Cc: Andrew Morton Cc: Hugh Dickins Cc: Roman Gushchin Cc: Johannes Weiner Cc: Hugh Dickins Cc: Tejun Heo Cc: Vladimir Davydov Cc: Muchun Song Cc: riel@surriel.com Cc: linux-mm@kvack.org Cc: linux-fsdevel@vger.kernel.org Cc: cgroups@vger.kernel.org --- mm/memcontrol.c | 21 +++++++++++++++++++++ mm/oom_kill.c | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) -- 2.34.0.rc0.344.g81b53c2807-goog diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 2e4c20d09f959..fc9c6280266b6 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2664,6 +2664,27 @@ int mem_cgroup_get_name_from_sb(struct super_block *sb, char *buf, size_t len) return ret < 0 ? ret : 0; } +/* + * Returns true if current's mm is a descendant of the memcg_under_oom (or + * equal to it). False otherwise. This is used by the oom-killer to detect + * ooms due to remote charging. + */ +bool is_remote_oom(struct mem_cgroup *memcg_under_oom) +{ + struct mem_cgroup *current_memcg; + bool is_remote_oom; + + if (!memcg_under_oom) + return false; + + current_memcg = get_mem_cgroup_from_mm(current->mm); + is_remote_oom = + !mem_cgroup_is_descendant(current_memcg, memcg_under_oom); + css_put(¤t_memcg->css); + + return is_remote_oom; +} + /* * Set or clear (if @memcg is NULL) charge association from file system to * memcg. If @memcg != NULL, then a css reference must be held by the caller to diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 0a7e16b16b8c3..556329dee273f 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -1106,6 +1106,27 @@ bool out_of_memory(struct oom_control *oc) } select_bad_process(oc); + + /* + * For remote ooms in userfaults, we have no choice but to kill the + * allocating process. + */ + if (!oc->chosen && is_remote_oom(oc->memcg) && current->in_user_fault && + !oom_unkillable_task(current)) { + get_task_struct(current); + oc->chosen = current; + oom_kill_process( + oc, "Out of memory (Killing remote allocating task)"); + return true; + } + + /* + * For remote ooms in non-userfaults, simply return ENOMEM to the + * caller. + */ + if (!oc->chosen && is_remote_oom(oc->memcg)) + return false; + /* Found nothing?!?! */ if (!oc->chosen) { dump_header(oc, NULL);