From patchwork Mon Mar 20 03:06:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cai Xinchen X-Patchwork-Id: 13180704 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 57D85C7618A for ; Mon, 20 Mar 2023 03:12:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3CF1A900004; Sun, 19 Mar 2023 23:12:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 37F37900002; Sun, 19 Mar 2023 23:12:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24731900004; Sun, 19 Mar 2023 23:12:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 16AFD900002 for ; Sun, 19 Mar 2023 23:12:32 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CA67A1202D1 for ; Mon, 20 Mar 2023 03:12:31 +0000 (UTC) X-FDA: 80587803702.10.09FDFE4 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf14.hostedemail.com (Postfix) with ESMTP id 729F5100005 for ; Mon, 20 Mar 2023 03:12:28 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf14.hostedemail.com: domain of caixinchen1@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=caixinchen1@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679281949; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PA2/9C+AQ1uCU7yV1xbJTodGisVVQgRHv58CMHgly4E=; b=N+kueeNxBDivoU/KaSP8LEHx5tugK+nIfpuxdd8EZLhros5NPTn/F4RrZAfsExVeprYDAy ZLWQ3kMgKPmdYV9wUs2h/NlVa9nC2D06A7UXrUMjvkw+laGQGCoQFQiqSSFKCJBv0oDnhL Iu/VfmR3gdU8xRj2qKuY8/xLLWI08AE= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf14.hostedemail.com: domain of caixinchen1@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=caixinchen1@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679281949; a=rsa-sha256; cv=none; b=ieGWFTV6lH7Ey/0bVi15UDS84yTnvcBGJ2PcyJoXHrMq1jenaxP4XNIumktbz3y2LuJk1O Zu9aCh06neOM7pYbYjq1b/bo56+u5E8nXko1QUThB3uGO0erYo4RUb8Iz+fW1DjBUfnhJt iIWCD4eQbQLM35H4bRRKRji6LnmPXhU= Received: from kwepemi500024.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Pg0800992znYTs; Mon, 20 Mar 2023 11:09:19 +0800 (CST) Received: from ci.huawei.com (10.67.175.89) by kwepemi500024.china.huawei.com (7.221.188.100) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Mon, 20 Mar 2023 11:12:23 +0800 From: Cai Xinchen To: , , , , , , CC: , , , , , Subject: [PATCH 1/1] mm: memcontrol: fix vmstats_percpu state incorrect subtraction after reparent Date: Mon, 20 Mar 2023 03:06:48 +0000 Message-ID: <20230320030648.50663-2-caixinchen1@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230320030648.50663-1-caixinchen1@huawei.com> References: <20230320030648.50663-1-caixinchen1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.175.89] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemi500024.china.huawei.com (7.221.188.100) X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 729F5100005 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: kp3wfg1t8bgzy9cgiz9t7zypzgfh7y81 X-HE-Tag: 1679281948-369298 X-HE-Meta: U2FsdGVkX19f0TWqu42p+OFMZ8sFWMjybnA38CDsqvShrk1pGqcfQZTDlBphPcaZPAM8OY90tUa3AxDgh5MlQnxFJTdaPeTfnpAmkrTn0XQreTSMBaLl8r2AL/hLXmwfZHmjf9KEFtZTGppieozk2VLROaOA90kDRFK3ekV+xczIVz7ou87x8aHs+fxXR2vWmq46xuYPg66aJp2AcZheDn2XlgUJk6FIOGQ2YFLplWy8FLDkuiT4T9IPxpA1vnve14MM3OrjarSuIAHciwzqIImtwLNwg7aIP69ovAKlExwoA9H7JUzARAzcktI37ssewZ0FZ0TmDTM2ElbjLRbVhfTqrXqCXY367yXs0Hg5N9oPZAn9drtjIbF8qK1NySPIR1re+CazIAx3mxdwbGxUupLyKgXO1JZKC12dfLUiHlIiumImA0wXgJe68yNTAQJQN6p0yNPS6xU+iXbGVxNcmE/FXKUfkRKCE3h6Ilkzb1MYZSg38mXYwQzaIxeEA8Q8KlM7RFo+3MOrSPipRVO1cZlbopukmGDn41kpEUQdEvqv8Pd41NGz3Ksdtxv1m9+83N3jvlP/jHNQzqQd/6GlYgxPc1lRGBD87jpve+X6DsB7/XBwL6Y5s2dA5DRQBEjxoHk53nBzQMl+StC2Z+3dO+3MAmx5h2GciaamHT2iflDlcm2tEmeRsjgMmkuzr1lp5mP9gDRp8rYn9sJ7y8XIQ9lXsC9JttDcMof23LUg1aGS76HNQgPDd6fSePyXRfi0qtYjCRvUTbEHQtGttbEmeWAWt9kxjgBBNWYyLhAO9loZwbb9fzv4FIy+XLWSOCM52ISkDFpAgEKH99mfQeqYYQxvBF/aDznjzRPzyZjc00o5saUB0vIyQfbB7SF/JWuHfNV7TzM5lg/mrJd04aC+el6BYNdHswUZo5/3PaFMklqbIZKL9n4a6jT3sSpqH/6ojgulY3SGtgAjqA9h+3r lEavVDuG bUEwF4YOSRl1tWc5sboLiQx7QoTHTDOMXL7i0YiulEmMmC5cpbAaFnAFojDlcR97mPC1DY7nJ3mcnL/dzMCqNuIWC9/+CHnRNWGdLZL0Q5OoaTVLPpAWeB10WlkY/5kaLKGO66/6jDGd/Vq4CD36UN8/kikKSXmjyEPof2/RLgIBSFP8= 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: When memcg C is offllined, its pages are reparented to memcg P, so far P->vmstats (hierarchical) have those pages, and P->vmstats_percpu (non-hierarchical) don't. When those pages get uncharged, P->vmstats (hierachical) decreases, which is correct, but P->vmstats_percpu (non-hierarchical) also decreases, which is wrong, as those stats were never added to P->vmstats_percpu to begin with. If the reparented memory exceeds the original non-hierarchical memory in P, some arg such as cache which is shown in memory.stat will be zero (if x < 0, it shows 0) To solve this problem, after reparent memcg, we should use cgroup_rstat_updated to add cgrp to rstat updated tree, and then after css refcount == 0, cgroup_rstat_flush is called by css_release_work_fn and css->flags is set as CSS_RELEASED. We propagate state in vmstat_percpu of struct mem_cgroup to its parent if css->flags is CSS_RELEASED. We use reparent_state to record those state and reparent_tag to ensure propagate vmstats_percpu of dying memcg only once. When mem_cgroup_css_rstat_flush is called, we add reparent_state to parent_memcg->vmstats_percpu->state and memcg->vmstgats_percpu->state_prev in locked context. The delta between state and state_prev would not change. So some accumulative value of vmstats_percpu (such as cache in memory.stat) will be shown correctly. And this patch does not produce delta between state and state_prev in vmstats_percpu which will be added to vmstats. Signed-off-by: Cai Xinchen --- kernel/cgroup/cgroup.c | 5 +++++ mm/memcontrol.c | 43 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 935e8121b21e..11138ee09a61 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -5384,12 +5384,17 @@ static void css_release_work_fn(struct work_struct *work) container_of(work, struct cgroup_subsys_state, destroy_work); struct cgroup_subsys *ss = css->ss; struct cgroup *cgrp = css->cgroup; + int cpu; mutex_lock(&cgroup_mutex); css->flags |= CSS_RELEASED; list_del_rcu(&css->sibling); + /* update all cgrp rstat because reparent rstat flush */ + for_each_possible_cpu(cpu) + cgroup_rstat_updated(cgrp, cpu); + if (ss) { /* css release path */ if (!list_empty(&css->rstat_css_node)) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 77929d08c8c9..658d42dc9820 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -842,6 +842,9 @@ struct memcg_vmstats_percpu { /* Cgroup1: threshold notifications & softlimit tree updates */ unsigned long nr_page_events; unsigned long targets[MEM_CGROUP_NTARGETS]; + + /* Tag for propagate vmstat_percpu, 1 for start, 0 for stop */ + long reparent_tag; }; struct memcg_vmstats { @@ -852,6 +855,9 @@ struct memcg_vmstats { /* Pending child counts during tree propagation */ long state_pending[MEMCG_NR_STAT]; unsigned long events_pending[NR_MEMCG_EVENTS]; + + /* Propagate vmstat_percpu after reparent */ + long reparent_state[MEMCG_NR_STAT]; }; unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) @@ -5558,6 +5564,17 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css) return -ENOMEM; } +static void set_reparent_tag_for_vmstats(struct mem_cgroup *memcg) +{ + int cpu; + struct memcg_vmstats_percpu *statc; + + for_each_possible_cpu(cpu) { + statc = per_cpu_ptr(memcg->vmstats_percpu, cpu); + WRITE_ONCE(statc->reparent_tag, 1); + } +} + static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) { struct mem_cgroup *memcg = mem_cgroup_from_css(css); @@ -5579,6 +5596,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) page_counter_set_low(&memcg->memory, 0); memcg_reparent_objcgs(memcg); + set_reparent_tag_for_vmstats(memcg); memcg_offline_kmem(memcg); reparent_shrinker_deferred(memcg); wb_memcg_offline(memcg); @@ -5653,8 +5671,9 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) struct mem_cgroup *memcg = mem_cgroup_from_css(css); struct mem_cgroup *parent = parent_mem_cgroup(memcg); struct memcg_vmstats_percpu *statc; - long delta, v; + long delta, v, reparent_v; int i, nid; + bool reparent = false; statc = per_cpu_ptr(memcg->vmstats_percpu, cpu); @@ -5668,6 +5687,10 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) if (delta) memcg->vmstats->state_pending[i] = 0; + reparent_v = memcg->vmstats->reparent_state[i]; + if (reparent_v) + memcg->vmstats->reparent_state[i] = 0; + /* Add CPU changes on this level since the last flush */ v = READ_ONCE(statc->state[i]); if (v != statc->state_prev[i]) { @@ -5675,6 +5698,21 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) statc->state_prev[i] = v; } + /* if reparent tag is set, this cgroup is offline and reparent. + * if css->flags & CSS_RELEASED, this cgroup will be released + * soon. We should propagate memcg's vmstats_percpu to its parent. + */ + if (READ_ONCE(statc->reparent_tag) && + css->flags & CSS_RELEASED && parent) { + parent->vmstats->reparent_state[i] += v + reparent_v; + reparent = true; + } else if (reparent_v) { + __this_cpu_add(memcg->vmstats_percpu->state[i], + reparent_v); + __this_cpu_add(memcg->vmstats_percpu->state_prev[i], + reparent_v); + } + if (!delta) continue; @@ -5684,6 +5722,9 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) parent->vmstats->state_pending[i] += delta; } + if (reparent) + WRITE_ONCE(statc->reparent_tag, 0); + for (i = 0; i < NR_MEMCG_EVENTS; i++) { delta = memcg->vmstats->events_pending[i]; if (delta)