From patchwork Fri Feb 21 19:59:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 11397325 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DBFEE1580 for ; Fri, 21 Feb 2020 19:59:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A856C24650 for ; Fri, 21 Feb 2020 19:59:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="v6Kq5LL0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A856C24650 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E36CD6B0003; Fri, 21 Feb 2020 14:59:32 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id DC0736B0006; Fri, 21 Feb 2020 14:59:32 -0500 (EST) 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 CB9BE6B0007; Fri, 21 Feb 2020 14:59:32 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0139.hostedemail.com [216.40.44.139]) by kanga.kvack.org (Postfix) with ESMTP id AFC456B0003 for ; Fri, 21 Feb 2020 14:59:32 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5C9CF181AC9CB for ; Fri, 21 Feb 2020 19:59:32 +0000 (UTC) X-FDA: 76515198984.06.page08_1907751132103 X-Spam-Summary: 20,1.5,0,369de652842e98bc,d41d8cd98f00b204,3ozzqxggkceo4tmwqqxns00sxq.o0yxuz69-yyw7mow.03s@flex--shakeelb.bounces.google.com,,RULES_HIT:41:152:355:379:541:800:960:966:968:973:988:989:1260:1277:1313:1314:1345:1437:1516:1518:1535:1542:1593:1594:1711:1730:1747:1777:1792:2194:2196:2199:2200:2328:2393:2559:2562:2693:2741:2899:3138:3139:3140:3141:3142:3152:3354:3865:3866:3867:3868:3870:3871:3872:3874:4321:4385:5007:6261:6653:9969:10004:10128:10400:11026:11658:11914:12043:12296:12297:12438:12555:12679:12895:13161:13229:14096:14097:14181:14394:14659:14721:21080:21251:21444:21451:21627:21740:21990:30012:30054,0,RBL:209.85.161.73:@flex--shakeelb.bounces.google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: page08_1907751132103 X-Filterd-Recvd-Size: 5432 Received: from mail-yw1-f73.google.com (mail-yw1-f73.google.com [209.85.161.73]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Fri, 21 Feb 2020 19:59:31 +0000 (UTC) Received: by mail-yw1-f73.google.com with SMTP id o200so2453218ywd.22 for ; Fri, 21 Feb 2020 11:59:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=RAIlg0f7zdLpk9f1Byl/cwdgiwxUwp6aSHTCC3RY/oM=; b=v6Kq5LL08ClIooNXUY3nDPFgWPI70VdsrwNaD3pbavipoxOiOoJyBhjmpWp2d+htUB m12EesD8fukKMUJgNl4xdfmT5c4OuYOuWvovtiV/dIcx06EuCYYHAaYKg+UbNAUEEzSM ONFjph/fYxjmElgTODoeSFzmqW+oSCSmZ8ZawNY1iqbXHr5bruclTHeD11A4dQjQfXGw 49pd1mMKkQG1ID5JtkSlnlZlPqYRvQTiR9HOKLRd7gC03H5pOmZPXoCOy0yDYXMIiDm1 da5HxInP1D2BW9gLZlZZGSQYD0BGCRoO0uuaIEUAEBJfFHICupfExZrs6Xfln9y7SURv PJrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=RAIlg0f7zdLpk9f1Byl/cwdgiwxUwp6aSHTCC3RY/oM=; b=V70/v+duc7pWkcc3ocTUyoq088x1AnRvK4bidNLZKuiWZO+vl8tNvlFD3Pnpq7O6v/ IWJzSeHq6BfvNk+b+rKrgavOlp/Tu+UazaCwPFypiC33DFqNzk2B2RETCgy/qAmY2uDt eR1hOn5IyeYNAkuoxaAzn20oueNmp6hURMGWhyAta+Gsp3qhcSb2p+o6fKQtebiH0JOp S6mdrVaxFuvISx2yO28oeF5yqGF4cPWu0y7wO1QbO2d2STDCQsZH1Wru/bD/LJSj1xvc M9hYeTuZWZ8tS/S1XjDtCbGEa0Y5PUAjR5JngA1xjxnzFcBmVcH9gRmhYgCIWJUy2vrO rqUQ== X-Gm-Message-State: APjAAAXGykOUNHdEzOu5wLdDZxAaP7RVrKQdJTwJ5v9fIH72U1GSuU4b CvsID15rd8k1JBsLy2CUfH/R9mVScxd3Yg== X-Google-Smtp-Source: APXvYqxjnxy+eZRYTRVN8mAGrdTES2zjvK9Gx/bLaPZ5OB6bYfhHJPW6A8a13wzgYYHT2Kphl/2XA9Pmpmnu2Q== X-Received: by 2002:a0d:c905:: with SMTP id l5mr29812030ywd.44.1582315171205; Fri, 21 Feb 2020 11:59:31 -0800 (PST) Date: Fri, 21 Feb 2020 11:59:19 -0800 Message-Id: <20200221195919.186576-1-shakeelb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog Subject: [PATCH] memcg: css_tryget_online cleanups From: Shakeel Butt To: Johannes Weiner , Roman Gushchin Cc: Michal Hocko , Andrew Morton , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Shakeel Butt 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: Currently multiple locations in memcg code, css_tryget_online() is being used. However it doesn't matter whether the cgroup is online for the callers. Online used to matter when we had reparenting on offlining and we needed a way to prevent new ones from showing up. The failure case for couple of these css_tryget_online usage is to fallback to root_mem_cgroup which kind of make bypassing the memcg limits possible for some workloads. For example creating an inotify group in a subcontainer and then deleting that container after moving the process to a different container will make all the event objects allocated for that group to the root_mem_cgroup. So, using css_tryget_online() is dangerous for such cases. Two locations still use the online version. The swapin of offlined memcg's pages and the memcg kmem cache creation. The kmem cache indeed needs the online version as the kernel does the reparenting of memcg kmem caches. For the swapin case, it has been left for later as the fallback is not really that concerning. Signed-off-by: Shakeel Butt --- mm/memcontrol.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 63bb6a2aab81..75fa8123909e 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -656,7 +656,7 @@ __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) */ __mem_cgroup_remove_exceeded(mz, mctz); if (!soft_limit_excess(mz->memcg) || - !css_tryget_online(&mz->memcg->css)) + !css_tryget(&mz->memcg->css)) goto retry; done: return mz; @@ -962,7 +962,8 @@ struct mem_cgroup *get_mem_cgroup_from_page(struct page *page) return NULL; rcu_read_lock(); - if (!memcg || !css_tryget_online(&memcg->css)) + /* Page should not get uncharged and freed memcg under us. */ + if (!memcg || WARN_ON(!css_tryget(&memcg->css))) memcg = root_mem_cgroup; rcu_read_unlock(); return memcg; @@ -975,10 +976,13 @@ EXPORT_SYMBOL(get_mem_cgroup_from_page); static __always_inline struct mem_cgroup *get_mem_cgroup_from_current(void) { if (unlikely(current->active_memcg)) { - struct mem_cgroup *memcg = root_mem_cgroup; + struct mem_cgroup *memcg; rcu_read_lock(); - if (css_tryget_online(¤t->active_memcg->css)) + /* current->active_memcg must hold a ref. */ + if (WARN_ON(!css_tryget(¤t->active_memcg->css))) + memcg = root_mem_cgroup; + else memcg = current->active_memcg; rcu_read_unlock(); return memcg; @@ -6703,7 +6707,7 @@ void mem_cgroup_sk_alloc(struct sock *sk) goto out; if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active) goto out; - if (css_tryget_online(&memcg->css)) + if (css_tryget(&memcg->css)) sk->sk_memcg = memcg; out: rcu_read_unlock();