From patchwork Sat Apr 27 00:37:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 13645517 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 5D615C4345F for ; Sat, 27 Apr 2024 00:38:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CF0E6B0099; Fri, 26 Apr 2024 20:37:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 908C26B009B; Fri, 26 Apr 2024 20:37:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75BE86B009C; Fri, 26 Apr 2024 20:37:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4EEC96B0099 for ; Fri, 26 Apr 2024 20:37:59 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CA90C4015B for ; Sat, 27 Apr 2024 00:37:58 +0000 (UTC) X-FDA: 82053449436.23.FDBB1A3 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) by imf18.hostedemail.com (Postfix) with ESMTP id 1CAD91C0002 for ; Sat, 27 Apr 2024 00:37:56 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=houO09vD; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf18.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.176 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714178277; 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=zLHD4gb8t6zDRcF8qzenGgWmsGCHlQSRICk2vCIqKfk=; b=OAfeVf4mkUrics7k88HviOdGVMdk2hgpH0wUVk7p4oWJMD2Kvu2C8xQYY7eDCI36YUbH1E DMI+k1HpCPL565JvX1wdzZoMk82UvlfZUU3PBXlooBTcGoyod3MK6SX+NbiptKYWHQnL/n RLmW7d2zIw4KmRFzqEhOyT83PR4whB8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=houO09vD; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf18.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.176 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714178277; a=rsa-sha256; cv=none; b=fGXyTZBEzah2cSN97B6lASpFM6iIHbXhGijsPVVsNLmjIsuCo+ZJadYUa0Nalz+NUqWuZ3 uLXiWxjlazZznVA6kPs2LihBMCA80JI57AvledDsLAJCt7jP1KH0TkhvlRL7j0BYy6qpLf 69hsEs54M2dsNr3A9sqTGD+D2sEKQKU= 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=1714178275; 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=zLHD4gb8t6zDRcF8qzenGgWmsGCHlQSRICk2vCIqKfk=; b=houO09vDhohFagXTbOfhLSJqhpc84FITSFNLRlY6ylNtuLjFRkyPyDJyqpgi6EbQHK4FZn RhE2P7AnzkgfYA/sfqlnm1T5oG8du97LuVbDILIJXqYXe1+56MCMYjkGUrV8Oga2UzpQz5 EeBGScH0Y4HpVoFLboUXkjNMFabeuPk= From: Shakeel Butt To: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 5/7] memcg: pr_warn_once for unexpected events and stats Date: Fri, 26 Apr 2024 17:37:31 -0700 Message-ID: <20240427003733.3898961-6-shakeel.butt@linux.dev> In-Reply-To: <20240427003733.3898961-1-shakeel.butt@linux.dev> References: <20240427003733.3898961-1-shakeel.butt@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 1CAD91C0002 X-Stat-Signature: 3omnu3eoikq6iosz6ib6ixw8tkpqjtp7 X-HE-Tag: 1714178276-136177 X-HE-Meta: U2FsdGVkX1+uyHXxVIYQ+xqVgUxM4Jfi/5BYDY8LIO7TAxxoQrK4TOCPEb3LCcz8Nkd1WT7eTWfZJ4AzKhN8nSg8E6J1HvBsCDxUcqZlIc6gj+WQs+cOfanaMTUe/K9eFlie3NRAn/WVhvqcuul+89jWB3o/Z1IBNUXjOmAORjCVzqaQg//R94BxgD+11HlSV3rcp6xJRLFnSSENYhYDMbgZE4HGKpdhljVg2BkJV/gcUj7abVd6uWNa8UVgsJNzcv0VFMdxbkySKRolkjlPAu09Q/FeJZZFuW4RksSncIrr1nt2+tibZdOggtlR5ZID+FOWCt2xjm7lrxtWGuD9xGALmZIEc++ekvKKLlaEf7u9HEzS5nra8aYgxixK0YYYQ4S94vVgm9JqARQhW2BVp50w7LFjumSVXWyQmYqiqJJWv1it2IEfIuDBOJHXU7ZP/n8bkrLtLao8dGbfhFaLiq+fVWhiD9O4hzM/+5+DLUsCTdF8MuPg0EnTrJdciEUgeLh+Gf7Hq0FgDU5Z0kkLCgW5tliA6zT29H/VzTbBdDrNTsY5Bi19iX42l4H/61hUasJK3HtMh/qSNATWafDUqz2NDTJBi3TCoxjr6oXjxpyiobDQ3tvriO0N2GCQF2HraKLgXSCMN0otprzhBBBnD1mmiBmMvP/5jaTyRewxTq/XvrbcPxBoBncF/D4+ExThkj6iFTv8L2Dvrg4qy1A/eCktdjnQnXApxL/oAESAZZ41Prsq3z0ZQ0NxR3zVuMPKeItIsRf74HNbdAjlkoV+/u6LJD4cAaiNtEpsIpALLpezzuwM78/iRCj2OP+q93SIB8XtXvQIHwSMebhh/gIebtal+0t2wIpmZAXqiyN3rHgOsP2fEci8pOMxCsjEEXPi93ptWAD/4kXoXEDUdJ+xJAdHIRR24+LreyMB845L2jtJMow8KonfCE+WHT3JT0I8yT1usG2HTBrwrdMhevF KynBpIUp pSZkkcBb+pPcKEHEwzReJrFW5OMvD1t+Mjec9J9Flo6wSg0RfQSPnOBvTaAlK6TLLuPIWxgXyGjhNbmr9Cqd+ZOfhr5MwM6a2RqQxqri7UT7D0bW9AAedFvJnMg== 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 --- mm/memcontrol.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 103e0e53e20a..36145089dcf5 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -671,9 +671,11 @@ unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx) return node_page_state(lruvec_pgdat(lruvec), idx); i = memcg_stats_index(idx); - if (i >= 0) { + if (likely(i >= 0)) { pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); x = READ_ONCE(pn->lruvec_stats->state[i]); + } else { + pr_warn_once("%s: stat item index: %d\n", __func__, idx); } #ifdef CONFIG_SMP if (x < 0) @@ -693,9 +695,11 @@ unsigned long lruvec_page_state_local(struct lruvec *lruvec, return node_page_state(lruvec_pgdat(lruvec), idx); i = memcg_stats_index(idx); - if (i >= 0) { + if (likely(i >= 0)) { pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); x = READ_ONCE(pn->lruvec_stats->state_local[i]); + } else { + pr_warn_once("%s: stat item index: %d\n", __func__, idx); } #ifdef CONFIG_SMP if (x < 0) @@ -922,8 +926,10 @@ unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) long x; int i = memcg_stats_index(idx); - if (i < 0) + if (unlikely(i < 0)) { + pr_warn_once("%s: stat item index: %d\n", __func__, idx); return 0; + } x = READ_ONCE(memcg->vmstats->state[i]); #ifdef CONFIG_SMP @@ -959,8 +965,13 @@ 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 (unlikely(i < 0)) { + pr_warn_once("%s: stat item index: %d\n", __func__, idx); return; + } __this_cpu_add(memcg->vmstats_percpu->state[i], val); memcg_rstat_updated(memcg, memcg_state_val_in_pages(idx, val)); @@ -972,8 +983,10 @@ 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 (unlikely(i < 0)) { + pr_warn_once("%s: stat item index: %d\n", __func__, idx); return 0; + } x = READ_ONCE(memcg->vmstats->state_local[i]); #ifdef CONFIG_SMP @@ -991,8 +1004,10 @@ static void __mod_memcg_lruvec_state(struct lruvec *lruvec, struct mem_cgroup *memcg; int i = memcg_stats_index(idx); - if (i < 0) + if (unlikely(i < 0)) { + pr_warn_once("%s: stat item index: %d\n", __func__, idx); return; + } pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); memcg = pn->memcg; @@ -1107,8 +1122,13 @@ void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, { int index = memcg_events_index(idx); - if (mem_cgroup_disabled() || index < 0) + if (mem_cgroup_disabled()) + return; + + if (unlikely(index < 0)) { + pr_warn_once("%s: event item index: %d\n", __func__, idx); return; + } memcg_stats_lock(); __this_cpu_add(memcg->vmstats_percpu->events[index], count); @@ -1120,8 +1140,11 @@ static unsigned long memcg_events(struct mem_cgroup *memcg, int event) { int index = memcg_events_index(event); - if (index < 0) + if (unlikely(index < 0)) { + pr_warn_once("%s: event item index: %d\n", __func__, event); return 0; + } + return READ_ONCE(memcg->vmstats->events[index]); } @@ -1129,8 +1152,10 @@ static unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) { int index = memcg_events_index(event); - if (index < 0) + if (unlikely(index < 0)) { + pr_warn_once("%s: event item index: %d\n", __func__, event); return 0; + } return READ_ONCE(memcg->vmstats->events_local[index]); }