From patchwork Fri May 25 18:55:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 10428349 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4BF35602D8 for ; Fri, 25 May 2018 18:55:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 35B39297A5 for ; Fri, 25 May 2018 18:55:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2A763297A8; Fri, 25 May 2018 18:55:14 +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=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=unavailable 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 CD265297A5 for ; Fri, 25 May 2018 18:55:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E3316B0003; Fri, 25 May 2018 14:55:09 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8923A6B0006; Fri, 25 May 2018 14:55:09 -0400 (EDT) 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 7A9756B0007; Fri, 25 May 2018 14:55:09 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f200.google.com (mail-pf0-f200.google.com [209.85.192.200]) by kanga.kvack.org (Postfix) with ESMTP id 3B85C6B0003 for ; Fri, 25 May 2018 14:55:09 -0400 (EDT) Received: by mail-pf0-f200.google.com with SMTP id x21-v6so3358200pfn.23 for ; Fri, 25 May 2018 11:55:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id; bh=Ii8pbkgyKnKKb8mIj9iKO/rsfe91Ujvj6+NbEtYEzkc=; b=cF+nYRfxumuA6stho47lcEvwDbs6TAhfzNDITp5gDbmpSWz+d02ve8LwXQb4Mbdj3t iSNJCD2EfLvuhsu5Su/FIrBySVx7PQo8awrsavmt+vQpO5veuaGn0B7r6TT3Tx4F2HDG zFmojlfAoESxUSKyVvdr9Geiob9XSDkHFKpn5O1FUR17XsyojZ/loNEoGMerhV66fxE5 DJbS1RpYfC7lZf/FaEbRn+aQmTGEemsTtotzFAlxLsbtygRb8fD7TWfF2NUOL7LA+GNZ EZiq76fYX+rSuj8DoZzWTd5RBRgGmyb3dDpIl/yisU6Mx3V4E71KkiEAsVclpViYpreY vsHA== X-Gm-Message-State: ALKqPwcVbaT7OAumNjhSoJMffM/5jbu0Riool/Rwtv7wrdCixAFMSDPz rEtUrq24hrN7ctnI0V41Dp00HuRNjTFG9zVsO8pmk5M2yT+lDR+EWNmZEMcwharHzZPglWCHJFC a22AYzgTRKLY4867Y8lMh2gvDtunRo0GWiWRE/70p60gd+W7Ymk18ebSyJFIFWbXpefTEkmvoml 5cqz+H1UOQZkEjeCpUCY+DUFAc28cFZEJQqY21t18E+I7EgjH2SRL6/RFfQ54pbo9WbM0xwwOYJ 4OyhxFUGr+LyVY/kOuAVsU/cLzixMwR6nhYNIlaTVDppun0yBHV4zLhzH9ZojxQTPwCzfPB9YiA QCkveHD/qJ4AHneOxsg94uqDoORFIdkLjVvbeq0LIcorI6tAm9k9JpOqrjqXD/ifHt4m807042/ v X-Received: by 2002:a62:3745:: with SMTP id e66-v6mr3714932pfa.43.1527274508917; Fri, 25 May 2018 11:55:08 -0700 (PDT) X-Received: by 2002:a62:3745:: with SMTP id e66-v6mr3714896pfa.43.1527274508117; Fri, 25 May 2018 11:55:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527274508; cv=none; d=google.com; s=arc-20160816; b=YTNJmuIkB7bLQDsoAoUbDDFPC5OKhLGHb19feBm8uAUWTlPHJ7kenYa485w9YM45eT MzGrLTilI6f/nrsN1FR785idjosGp3+bTJ0RgnwnW+llA3Ag6A8wmIMTfe3eV6E1qBXK 2HrYKZKJeZVoCwbklGWMoM1lyilsuFL9eBWZ4gYewuBcP8U3mkOceOuj6laR7yjk4IxV qvNiTuwbhOYanl1ehjPmM+uKrMlajinPbungP37bza9bW8D1RUBeh+jLLMqKkImjKaPy IbWi00F8zjLNTIvxgbvjXY5RFeHWaR3rAogj/ck4PuJPug6uaUacwLXuGDi4R4JwqTkm XU5A== 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:dkim-signature :arc-authentication-results; bh=Ii8pbkgyKnKKb8mIj9iKO/rsfe91Ujvj6+NbEtYEzkc=; b=o4shcGnXGUWLepPXiH/1BpSkpwgaa+RxoHaJhzfpJNDXpdm8Zy8UpLdXHIIrZXNkl4 TkeLjdt9KqTvypJ+Ny4DkpWP5cMwHtK1edRmms6mFx7n57vLNJNGAPJYrYZGF7GQmCDr nzDy1HwEyczUKx6vvI5pjVVD/MBxIaXMylpuJNCnCVaHPsWOHrJYBilMp7iiFJEDJ9cI mSYtocd7svISoKP04WhsrYd8Z3Sw1mIL00KdYfJEIDGCAeyb1vpkZpJVftN2QRLOa8g5 eV4DfuLNwJCPh/9iOAhRxOS7T0KCfP5zQ+qRSwoZmEdk9EwJCJtA8t5I34rkQhmoBaei nozA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=HCsJYvaQ; spf=pass (google.com: domain of shakeelb@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=shakeelb@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id q1-v6sor10346859plr.28.2018.05.25.11.55.08 for (Google Transport Security); Fri, 25 May 2018 11:55:08 -0700 (PDT) Received-SPF: pass (google.com: domain of shakeelb@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=HCsJYvaQ; spf=pass (google.com: domain of shakeelb@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=shakeelb@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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-Smtp-Source: AB8JxZpixhlaWx8a+2loowbMSVsLJbAfViCz4jkcAoAC2cPp+ahK7YS7CKV1dXt2LEZUsY8+txs3sA== 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 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 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;