From patchwork Tue Apr 30 06:06:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 13648293 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 0B2A4C4345F for ; Tue, 30 Apr 2024 06:06:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 150896B00A3; Tue, 30 Apr 2024 02:06:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E5646B00A2; Tue, 30 Apr 2024 02:06:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E6F816B00A3; Tue, 30 Apr 2024 02:06:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C07136B00A1 for ; Tue, 30 Apr 2024 02:06:45 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7CA5B14058E for ; Tue, 30 Apr 2024 06:06:45 +0000 (UTC) X-FDA: 82065164370.09.113A0DC Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.173]) by imf22.hostedemail.com (Postfix) with ESMTP id D8AB8C0010 for ; Tue, 30 Apr 2024 06:06:43 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=i9Ajc1vV; spf=pass (imf22.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.173 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714457204; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/V685kde9L0ZctsmCACzkCSs3YoKScmeI639UOEVavY=; b=T3xDyNhapJmYn+pLBip02xppqqaYT2Yc0sGpe2/NdxxYrb5gdnUnpTO9kzOGZJ23R4ahhM tTFMEq/0F2GLd2tJSXcG3QKYRyBa8dMUdUbKSXcChurZ2k5Z/SmeaA88ceQZUdSEeBPooc ylYtbWuYWZacfa6jlrd+YA6DJLjK69I= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=i9Ajc1vV; spf=pass (imf22.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.173 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714457204; a=rsa-sha256; cv=none; b=DkcvfPRGd19RZGndf36lu/2hB4qVQ+0ZLSwYEQMf+DTajGIZfG7fdV/0XpwdbIlBz6S2t+ DM2FbWBP/ROcaSUhbLcckSD4cgqx7Qg9wJfj59YzRjksqEe2PEENXUZs4jou1tUuV60+Ia Ka/7ZOPHwK2flO/iZK2BWAQ2dUqd9ow= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1714457202; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/V685kde9L0ZctsmCACzkCSs3YoKScmeI639UOEVavY=; b=i9Ajc1vVEAxx3DPEALD4PHoTBLkXVrvxG8ZT0+gtHZi8WqtrmLvE1uboXg7LzmLXkBmgAe mkiSrRVvWlwUHmiKEHwyPJOySqzpYOfr9raHJAytZqjOw0fL57/fyFm58R4EVIawHdQKE1 7rcnHurz3ckYjl7kQ8vgmKrnNFzRbXU= From: Shakeel Butt To: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Yosry Ahmed , "T . J . Mercier" Cc: kernel-team@meta.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 7/8] memcg: warn for unexpected events and stats Date: Mon, 29 Apr 2024 23:06:11 -0700 Message-ID: <20240430060612.2171650-8-shakeel.butt@linux.dev> In-Reply-To: <20240430060612.2171650-1-shakeel.butt@linux.dev> References: <20240430060612.2171650-1-shakeel.butt@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: 9hswke3u455sqx4uysa1mj4gafkcfyag X-Rspam-User: X-Rspamd-Queue-Id: D8AB8C0010 X-Rspamd-Server: rspam05 X-HE-Tag: 1714457203-275623 X-HE-Meta: U2FsdGVkX197xhx3oTyfyYWi6AdUYvUPYwedoFqgtEXzyKRVp5naXKPb8XWph/Z+cOpRz2/PNNNfLHGnIiDbtpFZ7vjyy6r0NbYbQMPKwg1GdwHh8rAOLbOBVWyoM9w65ubL6K40mWwyLVehE1VxSsjKUoePJt/DcNDdv1ra9z6BC0CicYvxx2Kfop7xxdfUoEFliAvG4JFocipS2ZIibZcyO/20PB6FtCTqniU8urCj1KsFD2CDYSEVJHPsVjL2uTkizXBv/Va0YeMzyLJJIzN46ElpX4r5BvXU5CqqldsgjP7CNBv9jGS/VzJ+KiY1k5uFFhNzDCtRRDT7wQRoh4OHz10OjFE6aRcMjO/n5wKAm/bUN4LpamPg2t2Q2dXtK3EnUidVz9+ykIxB3A3P3ZhNa/ETDzq5rsE1jmEeGahRyYoqorvLOmyc7JdJhrcSli+EcorHVIl0Qp+PFnOgmxthS0a7I7/0XqLQzssiTo935DxNYB+wdqdlcYZmT5nshIq9CEZcx/0Bv/+me7h4UbNUY8MImNkJlMq1NbvBGdXRCtOOyft0mbU1ofPHvWGs1R3sz/jbDJFEPhP0NC9KjdNehKRCCAfw70m+sqfLZauDthmk3g+8pUmwgGmehHCORkorPu15a68w07nnQ8hUQllP7Rx446pVvrpCs9W5Wh6rS7iUJ2ZZoc86IX8ACuVyUHYDg1ciyftPLBXSITi35hI6QxqDaZRgoZbTJ41dVmiOP+1sZYFp3C+ZiE2TqH6OtPRmjTSRQNeNBEp8tjV4Tj4yoiBYf80xm10BpWbrFGrMrqFNdNuNuLaH5SUXBRRw007WRAjOEfwwJ1zPmqRrLQ== 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: List-Subscribe: List-Unsubscribe: To reduce memory usage by the memcg events and stats, the kernel uses indirection table and only allocate stats and events which are being used by the memcg code. To make this more robust, let's add warnings where unexpected stats and events indexes are used. Signed-off-by: Shakeel Butt --- Changes since v2: - Based on feedback from Johannes, switched to WARN_ONCE() from pr_warn_once(). mm/memcontrol.c | 55 ++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index df94abc0088f..72e36977a96e 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -664,17 +664,18 @@ struct lruvec_stats { unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx) { struct mem_cgroup_per_node *pn; - long x = 0; + long x; int i; if (mem_cgroup_disabled()) return node_page_state(lruvec_pgdat(lruvec), idx); i = memcg_stats_index(idx); - if (i >= 0) { - pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - x = READ_ONCE(pn->lruvec_stats->state[i]); - } + if (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, idx)) + return 0; + + pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); + x = READ_ONCE(pn->lruvec_stats->state[i]); #ifdef CONFIG_SMP if (x < 0) x = 0; @@ -686,17 +687,18 @@ unsigned long lruvec_page_state_local(struct lruvec *lruvec, enum node_stat_item idx) { struct mem_cgroup_per_node *pn; - long x = 0; + long x; int i; if (mem_cgroup_disabled()) return node_page_state(lruvec_pgdat(lruvec), idx); i = memcg_stats_index(idx); - if (i >= 0) { - pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - x = READ_ONCE(pn->lruvec_stats->state_local[i]); - } + if (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, idx)) + return 0; + + pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); + x = READ_ONCE(pn->lruvec_stats->state_local[i]); #ifdef CONFIG_SMP if (x < 0) x = 0; @@ -921,7 +923,7 @@ unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) long x; int i = memcg_stats_index(idx); - if (i < 0) + if (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, idx)) return 0; x = READ_ONCE(memcg->vmstats->state[i]); @@ -958,7 +960,10 @@ void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val) { int i = memcg_stats_index(idx); - if (mem_cgroup_disabled() || i < 0) + if (mem_cgroup_disabled()) + return; + + if (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, idx)) return; __this_cpu_add(memcg->vmstats_percpu->state[i], val); @@ -971,7 +976,7 @@ static unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx) long x; int i = memcg_stats_index(idx); - if (i < 0) + if (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, idx)) return 0; x = READ_ONCE(memcg->vmstats->state_local[i]); @@ -990,7 +995,7 @@ static void __mod_memcg_lruvec_state(struct lruvec *lruvec, struct mem_cgroup *memcg; int i = memcg_stats_index(idx); - if (i < 0) + if (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, idx)) return; pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); @@ -1104,34 +1109,38 @@ void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val) void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, unsigned long count) { - int index = memcg_events_index(idx); + int i = memcg_events_index(idx); - if (mem_cgroup_disabled() || index < 0) + if (mem_cgroup_disabled()) + return; + + if (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, idx)) return; memcg_stats_lock(); - __this_cpu_add(memcg->vmstats_percpu->events[index], count); + __this_cpu_add(memcg->vmstats_percpu->events[i], count); memcg_rstat_updated(memcg, count); memcg_stats_unlock(); } static unsigned long memcg_events(struct mem_cgroup *memcg, int event) { - int index = memcg_events_index(event); + int i = memcg_events_index(event); - if (index < 0) + if (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, event)) return 0; - return READ_ONCE(memcg->vmstats->events[index]); + + return READ_ONCE(memcg->vmstats->events[i]); } static unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) { - int index = memcg_events_index(event); + int i = memcg_events_index(event); - if (index < 0) + if (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, event)) return 0; - return READ_ONCE(memcg->vmstats->events_local[index]); + return READ_ONCE(memcg->vmstats->events_local[i]); } static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,