Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp622291lqm; Wed, 1 May 2024 10:28:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWHR9Fhw0Zy4aOtlMvXV4bk0NVE2u+Mg1QYByAu94MksUABzZsTbcirj2taob9CgVIRt+gdvlwitAcLQafkBEoVxV1aYOyrUagPC4VCww== X-Google-Smtp-Source: AGHT+IHNnpswIdGaJ6LejTbQUSspdOi//WxkUgnaxzzu4c2Q4Y4uM9F5oDAdnhXjLm/P6zC+xws5 X-Received: by 2002:a17:90a:c7d2:b0:2ab:e45a:a27d with SMTP id gf18-20020a17090ac7d200b002abe45aa27dmr2969705pjb.39.1714584487957; Wed, 01 May 2024 10:28:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714584487; cv=pass; d=google.com; s=arc-20160816; b=SX/6vmmfCFHE1lbDCew8w7hfaXyQRD13HtXul35QzHxObWQ0SX4kUvaaliHH7D10L3 /WWnfO6+SuJLaqOgu9+jn55pg4B8uto6WFUMjoJV3vCzcmt5uHYi75u7hPj48MVSwyVS gOMLD+QsSSZGUpmYS+qpRmEDq34ENxAiDsbdsxYH77A6yKZQtyvJVx+FPJA/aoS2OHq+ bd8f0lc0uieIhX/YShMqS1+al6S1ObotQNrFqG6fRwQo8hGyomwo57WsPE0Rr6pKZR0I dJlVmW6vMQjKAYEprBztdPHacdcwmiYl7NNI7Xhb7aulz1TBe4AOxbGo+X3kJC/Qcu4s iMAQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=GSfZvMT0meM6+Tg2n6OjVJNbS0vHa+pAGl0pke03oZo=; fh=wnfKuHOUY4Mz1NUoc5XDTD1KV85duf1XGuItoJ8zV0s=; b=olCkUI4IWQ3D6B3GkeNUMXKPvq3koicETNnGAiqc4PGqTll7EBCwTiPVpJ7Jg6STkt dWkMGrW1bnTJmorlUIKrMuvZGnxbVs3Sge2hIrKvR5itRmEsRXzKZ3kuJ+qTV9hlKN2h IJ7QPxFFdBFmLivQkcRi/mfCe4nDvT8XMXYKvn9bzS+9v6G/WD8FTawDC1HVV7wn13wE yVPBGzCPsvJD88Fy7Fiq1iBOBCdaJtFDK84RJvp4d48O6AQif823g/syBs9Uyhy3Wf/9 bUVGste2d5HQ4K0ivf6pcpE/fS4c/elDItLTJ3bEVrgK5Wk2OMNW7hADx4FN6KbbCL1X 0xpw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=trfIiJqg; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-165649-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165649-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id t12-20020a17090ae50c00b002aeae8eda5csi1854222pjy.135.2024.05.01.10.28.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 10:28:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165649-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=trfIiJqg; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-165649-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165649-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 47B452830DB for ; Wed, 1 May 2024 17:28:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6A458130A6F; Wed, 1 May 2024 17:26:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="trfIiJqg" Received: from out-187.mta0.migadu.com (out-187.mta0.migadu.com [91.218.175.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D18DA130A48 for ; Wed, 1 May 2024 17:26:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.187 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714584411; cv=none; b=n5J1OKT2YZCXHqf51TsFWlAGMS3exrg8YXRb8BRH/bYOTbnNjDCutyXFKW4d8cjnrQCr6Xiy0PkxD93AMFy4YWTYr7EbMY3M11Sx4rUwLdYp7pKojlqW22bXpo8/N6myehTf0Ryss9cX6v90fYgEhUX3Bcm3CM5iG+SiidCnxwE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714584411; c=relaxed/simple; bh=8cFU4L9onx/D15Kw9WsRrYrXqLb8O5GWIxTJCH23XfM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HvXhr0YADzF9wpXcUNqSlwbs7/AfFJ3PNcgfOQSge2flM6nMRyvh2FbCMyjbpznXe4+4KBL4O4jW+CGJi5MjVd1q/izp1RFt3xJM+5koshjQg09Tn3Mbd7/7snTT4d71aEA7nc4yoFLRcSCc8kAHTarYhFJojVq19MqtrrfNahk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=trfIiJqg; arc=none smtp.client-ip=91.218.175.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=1714584408; 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=GSfZvMT0meM6+Tg2n6OjVJNbS0vHa+pAGl0pke03oZo=; b=trfIiJqgV4cZZHqhDCNO3VkVT22d/YhoirgzSeIxHk0Kyp4BRRYVdbzVdcRQUlf99KJVIB +sqEruaqw97BM6RkWEWKQYlkT3DaUVJvFUGmcuMT4MflewgGqt6Zg8xgqKU5yk0B6aoJF4 tPo0pBwMJJ1keoewuqZXlJvbt60TGN0= 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 v4 7/8] memcg: warn for unexpected events and stats Date: Wed, 1 May 2024 10:26:16 -0700 Message-ID: <20240501172617.678560-8-shakeel.butt@linux.dev> In-Reply-To: <20240501172617.678560-1-shakeel.butt@linux.dev> References: <20240501172617.678560-1-shakeel.butt@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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 v3: - N/A mm/memcontrol.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index a78cf00dd537..b4a1b4bb599d 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -671,7 +671,7 @@ 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 (WARN_ONCE(i < 0, "%s: missing stat item %d\n", __func__, idx)) return 0; pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); @@ -687,14 +687,14 @@ 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) + 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); @@ -923,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]); @@ -960,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); @@ -973,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]); @@ -992,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); @@ -1106,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, -- 2.43.0