From patchwork Mon Dec 13 16:53:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12674399 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0991C433F5 for ; Mon, 13 Dec 2021 16:59:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6E4EB6B0083; Mon, 13 Dec 2021 11:56:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 692536B0085; Mon, 13 Dec 2021 11:56:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 532B46B0087; Mon, 13 Dec 2021 11:56:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0062.hostedemail.com [216.40.44.62]) by kanga.kvack.org (Postfix) with ESMTP id 40AF96B0083 for ; Mon, 13 Dec 2021 11:56:01 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 0A9878249980 for ; Mon, 13 Dec 2021 16:55:51 +0000 (UTC) X-FDA: 78913372902.07.094548C Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) by imf11.hostedemail.com (Postfix) with ESMTP id 5E97440012 for ; Mon, 13 Dec 2021 16:55:48 +0000 (UTC) Received: by mail-pj1-f50.google.com with SMTP id p18-20020a17090ad31200b001a78bb52876so15075619pju.3 for ; Mon, 13 Dec 2021 08:55:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rMvcBVwXnNFX2TRf33ZNs6lKAej6Dsa2gPzCzMJOYCg=; b=lfXnVKzTkZU3MXNg6TihjcQ+iHgVkP74/qv/wtul53UjFFoxdX8KW/dmxXymyUg2IU /njJPUj9NDUPYwpsdRGhe7s7Gat9F7qXsT3t/yRiZISMPoqgVNW6kRqnvEUkGFOnSSdW thaZJ9iNYRL+2KLd7WrZHjJZOjoqJsgj+5Y+ryGt8uEuADHDVO+3IarLtHgAkn/fzpno 6mB495Gu3LMV2PH6pNPglrILmuMcUjfvg309i1ijjXwpLArZ10+X8fsx3Fjf68lNa+i5 tsmCT6IjrZPml0fR+1bcl4W54xVsbC19o6lZQokVOadL7oFS95/1PWT9wvDxJAol+GQ3 Nubw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rMvcBVwXnNFX2TRf33ZNs6lKAej6Dsa2gPzCzMJOYCg=; b=jpk6AmyW0WW3p9pCojx7k4OBaucNcBd99WnjdTDf8eTfTusTs3ijbW/KEzDorfuVlr pu3rOqBlnhzmtQCSaEAfvNOkL+4BYdnN6wDP88ckfSpE0xavLKEP0slk3WdR8dqTR+E4 4DP5vJMyTCtBzxqhIe4A8W1sFH++5+jhrzR5bkloSebHJ3fWC61cC5xw17p5i5CqZ2e/ SI24xMQvmcqg+iaRPSP3bYnHxXbD9ZBN7flrPmdZ0lBwGF2cX94pGN5cgpq3WCER8mRk 9MtrEDgg/wIGhUZbD0mi4nIKdQYfwLIsPUkeCQmlI+7heTcABOcC7qrJfzTSmEB/TY0b aPMA== X-Gm-Message-State: AOAM533jOceocU8R1BSdBrwFVo9VEFA1kDyu/bzOqsZVgyx7tmjpoIF/ Kd452p4zpkuvYpDUlne8t3HFGw== X-Google-Smtp-Source: ABdhPJx8/3PTUAli8LKJlWUvnStf3L6LdDPjo5NKylkzrf4G+l+7taR/KBkCf9aKwmNxdbyjhmrFPw== X-Received: by 2002:a17:90b:1e02:: with SMTP id pg2mr45914118pjb.148.1639414549098; Mon, 13 Dec 2021 08:55:49 -0800 (PST) Received: from localhost.localdomain ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id n11sm10430992pgp.15.2021.12.13.08.55.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Dec 2021 08:55:48 -0800 (PST) From: Muchun Song To: willy@infradead.org, akpm@linux-foundation.org, hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com, shakeelb@google.com, guro@fb.com, shy828301@gmail.com, alexs@kernel.org, richard.weiyang@gmail.com, david@fromorbit.com, trond.myklebust@hammerspace.com, anna.schumaker@netapp.com, jaegeuk@kernel.org, chao@kernel.org, kari.argillander@gmail.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-nfs@vger.kernel.org, zhengqi.arch@bytedance.com, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, smuchun@gmail.com, Muchun Song Subject: [PATCH v4 10/17] mm: memcontrol: move memcg_online_kmem() to mem_cgroup_css_online() Date: Tue, 14 Dec 2021 00:53:35 +0800 Message-Id: <20211213165342.74704-11-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20211213165342.74704-1-songmuchun@bytedance.com> References: <20211213165342.74704-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 5E97440012 X-Stat-Signature: yr4c3gtd41stk7y7pmds1ak974x78dz3 Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=lfXnVKzT; spf=pass (imf11.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.216.50 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-HE-Tag: 1639414548-332023 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: It will simplify the code if moving memcg_online_kmem() to mem_cgroup_css_online() and do not need to set ->kmemcg_id to -1 to indicate the memcg is offline. In the next patch, ->kmemcg_id will be used to sync list lru reparenting which requires not to change ->kmemcg_id. Signed-off-by: Muchun Song --- mm/memcontrol.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index d505b43d5f3b..ec7a62f39326 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3604,7 +3604,8 @@ static int memcg_online_kmem(struct mem_cgroup *memcg) if (cgroup_memory_nokmem) return 0; - BUG_ON(memcg->kmemcg_id >= 0); + if (unlikely(mem_cgroup_is_root(memcg))) + return 0; memcg_id = memcg_alloc_cache_id(); if (memcg_id < 0) @@ -3630,7 +3631,10 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg) struct mem_cgroup *parent; int kmemcg_id; - if (memcg->kmemcg_id == -1) + if (cgroup_memory_nokmem) + return; + + if (unlikely(mem_cgroup_is_root(memcg))) return; parent = parent_mem_cgroup(memcg); @@ -3640,7 +3644,6 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg) memcg_reparent_objcgs(memcg, parent); kmemcg_id = memcg->kmemcg_id; - BUG_ON(kmemcg_id < 0); /* * After we have finished memcg_reparent_objcgs(), all list_lrus @@ -3651,7 +3654,6 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg) memcg_drain_all_list_lrus(kmemcg_id, parent); memcg_free_cache_id(kmemcg_id); - memcg->kmemcg_id = -1; } #else static int memcg_online_kmem(struct mem_cgroup *memcg) @@ -5159,7 +5161,6 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) { struct mem_cgroup *parent = mem_cgroup_from_css(parent_css); struct mem_cgroup *memcg, *old_memcg; - long error = -ENOMEM; old_memcg = set_active_memcg(parent); memcg = mem_cgroup_alloc(); @@ -5188,34 +5189,26 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) return &memcg->css; } - /* The following stuff does not apply to the root */ - error = memcg_online_kmem(memcg); - if (error) - goto fail; - if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket) static_branch_inc(&memcg_sockets_enabled_key); return &memcg->css; -fail: - mem_cgroup_id_remove(memcg); - mem_cgroup_free(memcg); - return ERR_PTR(error); } static int mem_cgroup_css_online(struct cgroup_subsys_state *css) { struct mem_cgroup *memcg = mem_cgroup_from_css(css); + if (memcg_online_kmem(memcg)) + goto remove_id; + /* * A memcg must be visible for expand_shrinker_info() * by the time the maps are allocated. So, we allocate maps * here, when for_each_mem_cgroup() can't skip it. */ - if (alloc_shrinker_info(memcg)) { - mem_cgroup_id_remove(memcg); - return -ENOMEM; - } + if (alloc_shrinker_info(memcg)) + goto offline_kmem; /* Online state pins memcg ID, memcg ID pins CSS */ refcount_set(&memcg->id.ref, 1); @@ -5225,6 +5218,11 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css) queue_delayed_work(system_unbound_wq, &stats_flush_dwork, 2UL*HZ); return 0; +offline_kmem: + memcg_offline_kmem(memcg); +remove_id: + mem_cgroup_id_remove(memcg); + return -ENOMEM; } static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) @@ -5282,9 +5280,6 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css) cancel_work_sync(&memcg->high_work); mem_cgroup_remove_from_trees(memcg); free_shrinker_info(memcg); - - /* Need to offline kmem if online_css() fails */ - memcg_offline_kmem(memcg); mem_cgroup_free(memcg); }