Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp3803258imm; Fri, 25 May 2018 11:56:48 -0700 (PDT) X-Google-Smtp-Source: AB8JxZren14cwm69fxcWVys7VUAihF2j/JJSyCBQb491GvwNXidbbF5D9UjUF6LKt/XbeGZOlpoo X-Received: by 2002:a62:ac14:: with SMTP id v20-v6mr3784239pfe.101.1527274608667; Fri, 25 May 2018 11:56:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527274608; cv=none; d=google.com; s=arc-20160816; b=GHd836LmFPGqIfMGMGePJcuaRDFELtOn9trVTjLW44sjmgWNxziQexwFFYzZeHnImh DZiLGlp34sJKSYmg5NLTeDwAgKPD049VwAv4yw8rX1u7NKsOz67fmQVtG7uHMibRkccv b2WLaQ+gAVv3IgPrEBTh6xLRfxG6Dv6Ud36CwLdC0hWqfdh5w93NRJaIXQfTpM86/rbB IrTFccvx9B2IewMSYc1hYGrTpE2zHl7/9G0WvGefCyBDd73vM6e9kMKKgq6t777YPiEZ DXcVomM5fu7uof+AnxKxaHBx785Lulhiq2Ojmb2FB4oQmXZcN/Ps6qK0MH3La29ZIGKS Mzug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=Ii8pbkgyKnKKb8mIj9iKO/rsfe91Ujvj6+NbEtYEzkc=; b=cIftWcHn7GjayKQjHCRmirDXiQ1mp48ftNPpUeEoBDSa2vdr+unlxBX+qSr/WvWVIj nK0Shme1uKd6Q19icYhuOQMfJmkPqXfP5TCNI8TtXB6piiEjWGj56wA4Iq3XvlVpW1L8 jQnV/yml1nmPrXwNQyEaQnNgimGAwtpm7Cy84NGRlGSJ9uCEzUSr5BiXlTRPpwU7I7e8 Kud15Yqx6fAYMfeyTMXrOcEDv0ZL51TbYphOKADX2b9iEe+dHJ/Eix1RHc8jm96+m9r+ xtcjrBFjFsazUIK1l4biYvfYSsaONbKxE3W7nP7w9hbKd3LXses9J2VDOgZrMLzR1jP5 WICw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=HCsJYvaQ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q1-v6si24203627plb.549.2018.05.25.11.56.33; Fri, 25 May 2018 11:56:48 -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=@google.com header.s=20161025 header.b=HCsJYvaQ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S967865AbeEYSzK (ORCPT + 99 others); Fri, 25 May 2018 14:55:10 -0400 Received: from mail-pl0-f68.google.com ([209.85.160.68]:40410 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967543AbeEYSzI (ORCPT ); Fri, 25 May 2018 14:55:08 -0400 Received: by mail-pl0-f68.google.com with SMTP id t12-v6so3643340plo.7 for ; Fri, 25 May 2018 11:55:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Ii8pbkgyKnKKb8mIj9iKO/rsfe91Ujvj6+NbEtYEzkc=; b=HCsJYvaQYD1hNu8rrAKKmrOJMbL/UuHslAmnPzVugF9PKVXAj5/PNFbL2PbVwnPvWm OKqxlKUIr5otcIJmnQtfB6MRKpdhBqibXwGxedAOa07mXFQef36OsJKK///BmoxB6Awn k61fyU9t0sycX6q9fxfyczmBkeEDN0A8+CQ3BVQGVXpziSoCw1NNFZJSlEQlR2SIQpUn Q8TDuNtpj47jxbHOxUFDYlJBfP3DR9v7hFaXV3waGrwkQTYV1A5yFYfGzDe7Pm7ef+GC 8BSNFPnz5qpbkoIBu4g9s9T5/IKjHsfaPTIyAc84o0xbOije8RZs+qD08vebYm0qqwZf CPuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Ii8pbkgyKnKKb8mIj9iKO/rsfe91Ujvj6+NbEtYEzkc=; b=YAYN+QvLGL6wD1aHqqwlcq8PsggegSPkJX+Y8VcZ1A6a9S4b4JDvgTTdoabnYSamls B/y7iLvKiCv+MxiSFk5LWssEdx6bQ49bLrZIaFcK22K5Nlukgw2im+YD6CTwyZCe8foy dtzZSaisnsqA/fm9eI9eR0nvMAq+trl0Zk1GZUFIXhujQE8Z+4Y5AqQydaJ/SKkdDigq W2kN4om7pidyg2kcGM+bKTdsxVKjPU4WrzcxoefhuI+TWiLq4ARa0EWQKhnMqlG38pma TUU3ruWQiSr21gbiUbrsTrnq9JIXLjXPWKtsYt3o26y+kafB6CiUal3O6VWehxlahGNn 4/zw== X-Gm-Message-State: ALKqPwcbOM/I7qEGHb0iMreJpUMWj6FTofVWZc3aQY5BGfhGNE+AxLnV Kstb+wREyxrgILTG+2y0wpe8XA== X-Received: by 2002:a17:902:bcc5:: with SMTP id o5-v6mr3689275pls.349.1527274507455; Fri, 25 May 2018 11:55:07 -0700 (PDT) Received: from shakeelb.mtv.corp.google.com ([2620:15c:2cb:201:3a5f:3a4f:fa44:6b63]) by smtp.gmail.com with ESMTPSA id n83-v6sm49518809pfi.183.2018.05.25.11.55.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 May 2018 11:55:06 -0700 (PDT) From: Shakeel Butt To: Michal Hocko , Andrew Morton , Greg Thelen , Johannes Weiner , Vladimir Davydov Cc: Linux MM , cgroups@vger.kernel.org, LKML , Shakeel Butt Subject: [PATCH] memcg: force charge kmem counter too Date: Fri, 25 May 2018 11:55:01 -0700 Message-Id: <20180525185501.82098-1-shakeelb@google.com> X-Mailer: git-send-email 2.17.0.921.gf22659ad46-goog Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Based on several conditions the kernel can decide to force charge an allocation for a memcg i.e. overcharge memcg->memory and memcg->memsw counters. Do the same for memcg->kmem counter too. In cgroup-v1, this bug can cause a __GFP_NOFAIL kmem allocation fail if an explicit limit on kmem counter is set and reached. Signed-off-by: Shakeel Butt --- mm/memcontrol.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index ab5673dbfc4e..0a88f824c550 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1893,6 +1893,18 @@ void mem_cgroup_handle_over_high(void) current->memcg_nr_pages_over_high = 0; } +/* + * Based on try_charge() force charge conditions. + */ +static inline bool should_force_charge(gfp_t gfp_mask) +{ + return (unlikely(tsk_is_oom_victim(current) || + fatal_signal_pending(current) || + current->flags & PF_EXITING || + current->flags & PF_MEMALLOC || + gfp_mask & __GFP_NOFAIL)); +} + static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, unsigned int nr_pages) { @@ -2008,6 +2020,8 @@ static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, * The allocation either can't fail or will lead to more memory * being freed very soon. Allow memory usage go over the limit * temporarily by force charging it. + * + * NOTE: Please keep the should_force_charge() conditions in sync. */ page_counter_charge(&memcg->memory, nr_pages); if (do_memsw_account()) @@ -2331,8 +2345,11 @@ int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order, if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !page_counter_try_charge(&memcg->kmem, nr_pages, &counter)) { - cancel_charge(memcg, nr_pages); - return -ENOMEM; + if (!should_force_charge(gfp)) { + cancel_charge(memcg, nr_pages); + return -ENOMEM; + } + page_counter_charge(&memcg->kmem, nr_pages); } page->mem_cgroup = memcg; -- 2.17.0.921.gf22659ad46-goog