From nobody Tue Dec 16 16:41:26 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1D0AFC61D97 for ; Thu, 23 Nov 2023 07:20:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344859AbjKWHUC (ORCPT ); Thu, 23 Nov 2023 02:20:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230267AbjKWHT5 (ORCPT ); Thu, 23 Nov 2023 02:19:57 -0500 Received: from mx1.sberdevices.ru (mx1.sberdevices.ru [37.18.73.165]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 448CEF9; Wed, 22 Nov 2023 23:20:01 -0800 (PST) Received: from p-infra-ksmg-sc-msk01 (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id A7B06100068; Thu, 23 Nov 2023 10:19:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru A7B06100068 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1700723998; bh=eohC8GA+wehGLdFcqpgwGNVkXeQrwyMu8sDPWJjxuws=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=BctS3ayvecYTInoSh1lwgOzm2DdEKF2KMLmskK4h6IC4F2Hxd+q09bxZ6mDF+h+d3 ZWP3VPjnhKUJUrC5u7qQA/UvaFCyunHVf7axluxqnKlkUCAkuC8Xw1rO9Etw4+akQ+ ATdW6/hI5INmGcA+ijok1tsYabF5N6AF3dTu0cDBEPzaNRFF+bX2qeEcRh/+z5pBnq WkTQ8NrLhGV6TRrXUSFCH8MOclE2DsEnw3n783nNKXq9I/0VyTCmTFmUmeyaY85vvq LAuwmsJO+MhtQdn8uFdxIT41FLKR19+DQCzZUxd/EATnc0YtZcstXwI0DVbBRYqKBD NoBWQTUKpVf3w== Received: from p-i-exch-sc-m01.sberdevices.ru (p-i-exch-sc-m01.sberdevices.ru [172.16.192.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Thu, 23 Nov 2023 10:19:58 +0300 (MSK) Received: from localhost.localdomain (100.64.160.123) by p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Thu, 23 Nov 2023 10:19:58 +0300 From: Dmitry Rokosov To: , , , , , CC: , , , , , , Dmitry Rokosov Subject: [PATCH v3 1/3] samples: introduce new samples subdir for cgroup Date: Thu, 23 Nov 2023 10:19:43 +0300 Message-ID: <20231123071945.25811-2-ddrokosov@salutedevices.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20231123071945.25811-1-ddrokosov@salutedevices.com> References: <20231123071945.25811-1-ddrokosov@salutedevices.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [100.64.160.123] X-ClientProxiedBy: p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) To p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 181550 [Nov 23 2023] X-KSMG-AntiSpam-Version: 6.0.0.2 X-KSMG-AntiSpam-Envelope-From: ddrokosov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 3 0.3.3 e5c6a18a9a9bff0226d530c5b790210c0bd117c8, {Tracking_uf_ne_domains}, {Tracking_from_domain_doesnt_match_to}, d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;127.0.0.199:7.1.2;100.64.160.123:7.1.2;p-i-exch-sc-m01.sberdevices.ru:5.0.1,7.1.1;salutedevices.com:7.1.1;lore.kernel.org:7.1.1, FromAlignment: s, ApMailHostAddress: 100.64.160.123 X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean, bases: 2023/11/23 06:48:00 X-KSMG-LinksScanning: Clean, bases: 2023/11/23 06:48:00 X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2023/11/23 04:50:00 #22507336 X-KSMG-AntiVirus-Status: Clean, skipped Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the cgroup_event_listener for cgroup v1 to the samples directory. This suggestion was proposed by Andrew Morton during the discussion [1]. Links: [1] - https://lore.kernel.org/all/20231106140934.3f5d4960141562fe8da539= 06@linux-foundation.org/ Signed-off-by: Dmitry Rokosov --- MAINTAINERS | 1 + samples/Kconfig | 6 ++++++ samples/Makefile | 1 + samples/cgroup/Makefile | 5 +++++ {tools =3D> samples}/cgroup/cgroup_event_listener.c | 0 tools/cgroup/Makefile | 11 ----------- 6 files changed, 13 insertions(+), 11 deletions(-) create mode 100644 samples/cgroup/Makefile rename {tools =3D> samples}/cgroup/cgroup_event_listener.c (100%) delete mode 100644 tools/cgroup/Makefile diff --git a/MAINTAINERS b/MAINTAINERS index d516295978a4..6a0a580c34dc 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5243,6 +5243,7 @@ L: linux-mm@kvack.org S: Maintained F: mm/memcontrol.c F: mm/swap_cgroup.c +F: samples/cgroup/* F: tools/testing/selftests/cgroup/memcg_protection.m F: tools/testing/selftests/cgroup/test_kmem.c F: tools/testing/selftests/cgroup/test_memcontrol.c diff --git a/samples/Kconfig b/samples/Kconfig index bf49ed0d7362..339c8e2ee749 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -287,6 +287,12 @@ config SAMPLE_KMEMLEAK Build a sample program which have explicitly leaks memory to test kmemleak =20 +config SAMPLE_CGROUP + bool "Build cgroup sample code" + depends on CGROUPS && CC_CAN_LINK && HEADERS_INSTALL + help + Build samples that demonstrate the usage of the cgroup API. + source "samples/rust/Kconfig" =20 endif # SAMPLES diff --git a/samples/Makefile b/samples/Makefile index 0a551c2b33f4..b85fa64390c5 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -3,6 +3,7 @@ =20 subdir-$(CONFIG_SAMPLE_AUXDISPLAY) +=3D auxdisplay subdir-$(CONFIG_SAMPLE_ANDROID_BINDERFS) +=3D binderfs +subdir-$(CONFIG_SAMPLE_CGROUP) +=3D cgroup obj-$(CONFIG_SAMPLE_CONFIGFS) +=3D configfs/ obj-$(CONFIG_SAMPLE_CONNECTOR) +=3D connector/ obj-$(CONFIG_SAMPLE_FANOTIFY_ERROR) +=3D fanotify/ diff --git a/samples/cgroup/Makefile b/samples/cgroup/Makefile new file mode 100644 index 000000000000..deef4530f5e7 --- /dev/null +++ b/samples/cgroup/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 + +userprogs-always-y +=3D cgroup_event_listener + +userccflags +=3D -I usr/include diff --git a/tools/cgroup/cgroup_event_listener.c b/samples/cgroup/cgroup_e= vent_listener.c similarity index 100% rename from tools/cgroup/cgroup_event_listener.c rename to samples/cgroup/cgroup_event_listener.c diff --git a/tools/cgroup/Makefile b/tools/cgroup/Makefile deleted file mode 100644 index ffca068e4a76..000000000000 --- a/tools/cgroup/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0 -# Makefile for cgroup tools - -CFLAGS =3D -Wall -Wextra - -all: cgroup_event_listener -%: %.c - $(CC) $(CFLAGS) -o $@ $^ - -clean: - $(RM) cgroup_event_listener --=20 2.36.0 From nobody Tue Dec 16 16:41:26 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3F95C61D85 for ; Thu, 23 Nov 2023 07:20:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344876AbjKWHUJ (ORCPT ); Thu, 23 Nov 2023 02:20:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231744AbjKWHT5 (ORCPT ); Thu, 23 Nov 2023 02:19:57 -0500 Received: from mx1.sberdevices.ru (mx2.sberdevices.ru [45.89.224.132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 702DD100; Wed, 22 Nov 2023 23:20:01 -0800 (PST) Received: from p-infra-ksmg-sc-msk02 (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id 1044E120071; Thu, 23 Nov 2023 10:20:00 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru 1044E120071 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1700724000; bh=FqpWkqFlCXy5e8M9JPLBeFC7vxG1fv8P80GWkWslETo=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=mtmk0lNQIHJ+w3JQvX+Fg7IrkYNKjHPHvktagFO4wXgd6zM21fmy3drWFlgrBaPcQ 8io4+0ZIjsazgOMbX5IP8Y4cxZieQh+II8JQlYRblDDE5AnXsqJvDGh+KjXxpROD4w SOeU2ZAYuA8Km1kJEe3GTaHxnymJMRur9+zYbDGGYryJ+EIe6slEa8TANxfWrhA3iY pXG416DuqQjiGOLtnpu3XWCjsZGXZoo2BRnO4BK8IZvft/Wo9tmX+XHvWaSGsXMkjj IGBsgrrYjs0tGE32PmmBTjO90PDiAD5WftT1YyThe6h4aGdUq04lQbH97YTIsFP/Bi gQuGBh69GI/mg== Received: from p-i-exch-sc-m01.sberdevices.ru (p-i-exch-sc-m01.sberdevices.ru [172.16.192.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Thu, 23 Nov 2023 10:19:59 +0300 (MSK) Received: from localhost.localdomain (100.64.160.123) by p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Thu, 23 Nov 2023 10:19:59 +0300 From: Dmitry Rokosov To: , , , , , CC: , , , , , , Dmitry Rokosov Subject: [PATCH v3 2/3] samples/cgroup: introduce memcg memory.events listener Date: Thu, 23 Nov 2023 10:19:44 +0300 Message-ID: <20231123071945.25811-3-ddrokosov@salutedevices.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20231123071945.25811-1-ddrokosov@salutedevices.com> References: <20231123071945.25811-1-ddrokosov@salutedevices.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [100.64.160.123] X-ClientProxiedBy: p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) To p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 181550 [Nov 23 2023] X-KSMG-AntiSpam-Version: 6.0.0.2 X-KSMG-AntiSpam-Envelope-From: ddrokosov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 3 0.3.3 e5c6a18a9a9bff0226d530c5b790210c0bd117c8, {Tracking_from_domain_doesnt_match_to}, d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;salutedevices.com:7.1.1;100.64.160.123:7.1.2;127.0.0.199:7.1.2;p-i-exch-sc-m01.sberdevices.ru:5.0.1,7.1.1, FromAlignment: s, ApMailHostAddress: 100.64.160.123 X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean X-KSMG-LinksScanning: Clean X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2023/11/23 04:50:00 #22507336 X-KSMG-AntiVirus-Status: Clean, skipped Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is a simple listener for memory events that handles counter changes in runtime. It can be set up for a specific memory cgroup v2. The output example: =3D=3D=3D=3D=3D $ /tmp/memcg_event_listener test Initialized MEMCG events with counters: MEMCG events: low: 0 high: 0 max: 0 oom: 0 oom_kill: 0 oom_group_kill: 0 Started monitoring memory events from '/sys/fs/cgroup/test/memory.events'... Received event in /sys/fs/cgroup/test/memory.events: *** 1 MEMCG oom_kill event, change counter 0 =3D> 1 Received event in /sys/fs/cgroup/test/memory.events: *** 1 MEMCG oom_kill event, change counter 1 =3D> 2 Received event in /sys/fs/cgroup/test/memory.events: *** 1 MEMCG oom_kill event, change counter 2 =3D> 3 Received event in /sys/fs/cgroup/test/memory.events: *** 1 MEMCG oom_kill event, change counter 3 =3D> 4 Received event in /sys/fs/cgroup/test/memory.events: *** 2 MEMCG max events, change counter 0 =3D> 2 Received event in /sys/fs/cgroup/test/memory.events: *** 8 MEMCG max events, change counter 2 =3D> 10 *** 1 MEMCG oom event, change counter 0 =3D> 1 Received event in /sys/fs/cgroup/test/memory.events: *** 1 MEMCG oom_kill event, change counter 4 =3D> 5 ^CExiting memcg event listener... =3D=3D=3D=3D=3D Signed-off-by: Dmitry Rokosov --- samples/cgroup/Makefile | 2 +- samples/cgroup/memcg_event_listener.c | 330 ++++++++++++++++++++++++++ 2 files changed, 331 insertions(+), 1 deletion(-) create mode 100644 samples/cgroup/memcg_event_listener.c diff --git a/samples/cgroup/Makefile b/samples/cgroup/Makefile index deef4530f5e7..526c8569707c 100644 --- a/samples/cgroup/Makefile +++ b/samples/cgroup/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 =20 -userprogs-always-y +=3D cgroup_event_listener +userprogs-always-y +=3D cgroup_event_listener memcg_event_listener =20 userccflags +=3D -I usr/include diff --git a/samples/cgroup/memcg_event_listener.c b/samples/cgroup/memcg_e= vent_listener.c new file mode 100644 index 000000000000..a1667fe2489a --- /dev/null +++ b/samples/cgroup/memcg_event_listener.c @@ -0,0 +1,330 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * memcg_event_listener.c - Simple listener of memcg memory.events + * + * Copyright (c) 2023, SaluteDevices. All Rights Reserved. + * + * Author: Dmitry Rokosov + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MEMCG_EVENTS "memory.events" + +/* Size of buffer to use when reading inotify events */ +#define INOTIFY_BUFFER_SIZE 8192 + +#define INOTIFY_EVENT_NEXT(event, length) ({ \ + (length) -=3D sizeof(*(event)) + (event)->len; \ + (event)++; \ +}) + +#define INOTIFY_EVENT_OK(event, length) ((length) >=3D (ssize_t)sizeof(*(e= vent))) + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) + +struct memcg_counters { + long low; + long high; + long max; + long oom; + long oom_kill; + long oom_group_kill; +}; + +struct memcg_events { + struct memcg_counters counters; + char path[PATH_MAX]; + int inotify_fd; + int inotify_wd; +}; + +static void print_memcg_counters(const struct memcg_counters *counters) +{ + printf("MEMCG events:\n"); + printf("\tlow: %ld\n", counters->low); + printf("\thigh: %ld\n", counters->high); + printf("\tmax: %ld\n", counters->max); + printf("\toom: %ld\n", counters->oom); + printf("\toom_kill: %ld\n", counters->oom_kill); + printf("\toom_group_kill: %ld\n", counters->oom_group_kill); +} + +static int get_memcg_counter(char *line, const char *name, long *counter) +{ + size_t len =3D strlen(name); + char *endptr; + long tmp; + + if (memcmp(line, name, len)) { + warnx("Counter line %s has wrong name, %s is expected", + line, name); + return -EINVAL; + } + + /* skip the whitespace delimiter */ + len +=3D 1; + + errno =3D 0; + tmp =3D strtol(&line[len], &endptr, 10); + if (((tmp =3D=3D LONG_MAX || tmp =3D=3D LONG_MIN) && errno =3D=3D ERANGE)= || + (errno && !tmp)) { + warnx("Failed to parse: %s", &line[len]); + return -ERANGE; + } + + if (endptr =3D=3D &line[len]) { + warnx("Not digits were found in line %s", &line[len]); + return -EINVAL; + } + + if (!(*endptr =3D=3D '\0' || (*endptr =3D=3D '\n' && *++endptr =3D=3D '\0= '))) { + warnx("Further characters after number: %s", endptr); + return -EINVAL; + } + + *counter =3D tmp; + + return 0; +} + +static int read_memcg_events(struct memcg_events *events, bool show_diff) +{ + FILE *fp =3D fopen(events->path, "re"); + size_t i; + int ret =3D 0; + bool any_new_events =3D false; + char *line =3D NULL; + size_t len =3D 0; + struct memcg_counters new_counters; + struct memcg_counters *counters =3D &events->counters; + struct { + const char *name; + long *new; + long *old; + } map[] =3D { + { + .name =3D "low", + .new =3D &new_counters.low, + .old =3D &counters->low, + }, + { + .name =3D "high", + .new =3D &new_counters.high, + .old =3D &counters->high, + }, + { + .name =3D "max", + .new =3D &new_counters.max, + .old =3D &counters->max, + }, + { + .name =3D "oom", + .new =3D &new_counters.oom, + .old =3D &counters->oom, + }, + { + .name =3D "oom_kill", + .new =3D &new_counters.oom_kill, + .old =3D &counters->oom_kill, + }, + { + .name =3D "oom_group_kill", + .new =3D &new_counters.oom_group_kill, + .old =3D &counters->oom_group_kill, + }, + }; + + if (!fp) { + warn("Failed to open memcg events file %s", events->path); + return -EBADF; + } + + /* Read new values for memcg counters */ + for (i =3D 0; i < ARRAY_SIZE(map); ++i) { + ssize_t nread; + + errno =3D 0; + nread =3D getline(&line, &len, fp); + if (nread =3D=3D -1) { + if (errno) { + warn("Failed to read line for counter %s", + map[i].name); + ret =3D -EIO; + goto exit; + } + + break; + } + + ret =3D get_memcg_counter(line, map[i].name, map[i].new); + if (ret) { + warnx("Failed to get counter value from line %s", line); + goto exit; + } + } + + for (i =3D 0; i < ARRAY_SIZE(map); ++i) { + long diff; + + if (*map[i].new > *map[i].old) { + diff =3D *map[i].new - *map[i].old; + + if (show_diff) + printf("*** %ld MEMCG %s event%s, " + "change counter %ld =3D> %ld\n", + diff, map[i].name, + (diff =3D=3D 1) ? "" : "s", + *map[i].old, *map[i].new); + + *map[i].old +=3D diff; + any_new_events =3D true; + } + } + + if (show_diff && !any_new_events) + printf("*** No new untracked memcg events available\n"); + +exit: + free(line); + fclose(fp); + + return ret; +} + +static void process_memcg_events(struct memcg_events *events, + struct inotify_event *event) +{ + int ret; + + if (events->inotify_wd !=3D event->wd) { + warnx("Unknown inotify event %d, should be %d", event->wd, + events->inotify_wd); + return; + } + + printf("Received event in %s:\n", events->path); + + if (!(event->mask & IN_MODIFY)) { + warnx("No IN_MODIFY event, skip it"); + return; + } + + ret =3D read_memcg_events(events, /* show_diff =3D */true); + if (ret) + warnx("Can't read memcg events"); +} + +static void monitor_events(struct memcg_events *events) +{ + struct pollfd fds[1]; + int ret; + + printf("Started monitoring memory events from '%s'...\n", events->path); + + fds[0].fd =3D events->inotify_fd; + fds[0].events =3D POLLIN; + + for (;;) { + ret =3D poll(fds, ARRAY_SIZE(fds), -1); + if (ret < 0 && errno !=3D EAGAIN) + err(EXIT_FAILURE, "Can't poll memcg events (%d)", ret); + + if (fds[0].revents & POLLERR) + err(EXIT_FAILURE, "Got POLLERR during monitor events"); + + if (fds[0].revents & POLLIN) { + struct inotify_event *event; + char buffer[INOTIFY_BUFFER_SIZE]; + ssize_t length; + + length =3D read(fds[0].fd, buffer, INOTIFY_BUFFER_SIZE); + if (length <=3D 0) + continue; + + event =3D (struct inotify_event *)buffer; + while (INOTIFY_EVENT_OK(event, length)) { + process_memcg_events(events, event); + event =3D INOTIFY_EVENT_NEXT(event, length); + } + } + } +} + +static int initialize_memcg_events(struct memcg_events *events, + const char *cgroup) +{ + int ret; + + memset(events, 0, sizeof(struct memcg_events)); + + ret =3D snprintf(events->path, PATH_MAX, + "/sys/fs/cgroup/%s/memory.events", cgroup); + if (ret >=3D PATH_MAX) { + warnx("Path to cgroup memory.events is too long"); + return -EMSGSIZE; + } else if (ret < 0) { + warn("Can't generate cgroup event full name"); + return ret; + } + + ret =3D read_memcg_events(events, /* show_diff =3D */false); + if (ret) { + warnx("Failed to read initial memcg events state (%d)", ret); + return ret; + } + + events->inotify_fd =3D inotify_init(); + if (events->inotify_fd < 0) { + warn("Failed to setup new inotify device"); + return -EMFILE; + } + + events->inotify_wd =3D inotify_add_watch(events->inotify_fd, + events->path, IN_MODIFY); + if (events->inotify_wd < 0) { + warn("Couldn't add monitor in dir %s", events->path); + return -EIO; + } + + printf("Initialized MEMCG events with counters:\n"); + print_memcg_counters(&events->counters); + + return 0; +} + +static void cleanup_memcg_events(struct memcg_events *events) +{ + inotify_rm_watch(events->inotify_fd, events->inotify_wd); + close(events->inotify_fd); +} + +int main(int argc, const char **argv) +{ + struct memcg_events events; + ssize_t ret; + + if (argc !=3D 2) + errx(EXIT_FAILURE, "Usage: %s ", argv[0]); + + ret =3D initialize_memcg_events(&events, argv[1]); + if (ret) + errx(EXIT_FAILURE, "Can't initialize memcg events (%zd)", ret); + + monitor_events(&events); + + cleanup_memcg_events(&events); + + printf("Exiting memcg event listener...\n"); + + return EXIT_SUCCESS; +} --=20 2.36.0 From nobody Tue Dec 16 16:41:26 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9B65C61D85 for ; Thu, 23 Nov 2023 07:20:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344871AbjKWHUF (ORCPT ); Thu, 23 Nov 2023 02:20:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229995AbjKWHT5 (ORCPT ); Thu, 23 Nov 2023 02:19:57 -0500 Received: from mx1.sberdevices.ru (mx2.sberdevices.ru [45.89.224.132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42978109; Wed, 22 Nov 2023 23:20:02 -0800 (PST) Received: from p-infra-ksmg-sc-msk02 (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id E26FC120007; Thu, 23 Nov 2023 10:20:00 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru E26FC120007 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1700724000; bh=4xqDYEZxoQBKfiroxVAcCaTQ0QNc/WhY15vgNkVDJCI=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=orKhJIw62iEDOIX8Eo122DiZ9ABea2OkrpyBXvy1q4BEINmqjrEPfxqvpJkzoASye 0k8ivVu56ck2hQ0OK9iFWoM880QCHg1z7W1Mzbrd2SogdxqsCknmIyee6eYAPbj6I/ 1Q1F3vSHK8twKbcd49ZNuDdAW7PlZve+cJ1CKoJ/6OsIdFMoJggTEAN50uNNqekbGA 4o9ofIP2//am+lWImoqWaX/icU1NWCKq6US4qJOYp7j/UgAoibUojuHxAi6v8u1URz Aj8Ht6iZb7myhpul2Fm2YRflSvyOLw9dJFTCZ6aeWhmMF9AGl4yKgCweZWaoLm+W1v 0cSEpdgm0QtOw== Received: from p-i-exch-sc-m01.sberdevices.ru (p-i-exch-sc-m01.sberdevices.ru [172.16.192.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Thu, 23 Nov 2023 10:20:00 +0300 (MSK) Received: from localhost.localdomain (100.64.160.123) by p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Thu, 23 Nov 2023 10:20:00 +0300 From: Dmitry Rokosov To: , , , , , CC: , , , , , , Dmitry Rokosov Subject: [PATCH v3 3/3] mm: memcg: add reminder comment for the memcg v2 events Date: Thu, 23 Nov 2023 10:19:45 +0300 Message-ID: <20231123071945.25811-4-ddrokosov@salutedevices.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20231123071945.25811-1-ddrokosov@salutedevices.com> References: <20231123071945.25811-1-ddrokosov@salutedevices.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [100.64.160.123] X-ClientProxiedBy: p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) To p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 181550 [Nov 23 2023] X-KSMG-AntiSpam-Version: 6.0.0.2 X-KSMG-AntiSpam-Envelope-From: ddrokosov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 3 0.3.3 e5c6a18a9a9bff0226d530c5b790210c0bd117c8, {Tracking_from_domain_doesnt_match_to}, d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;salutedevices.com:7.1.1;100.64.160.123:7.1.2;127.0.0.199:7.1.2;p-i-exch-sc-m01.sberdevices.ru:5.0.1,7.1.1, FromAlignment: s, ApMailHostAddress: 100.64.160.123 X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean X-KSMG-LinksScanning: Clean X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2023/11/23 04:50:00 #22507336 X-KSMG-AntiVirus-Status: Clean, skipped Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" To maintain the correct state, it is important to ensure that events for the memory cgroup v2 are aligned with the sample cgroup codes. Signed-off-by: Dmitry Rokosov --- mm/memcontrol.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e8ca4bdcb03c..a75c4584f58f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6555,6 +6555,10 @@ static ssize_t memory_max_write(struct kernfs_open_f= ile *of, return nbytes; } =20 +/* + * Note: don't forget to update the 'samples/cgroup/memcg_event_listener' + * if any new events become available. + */ static void __memory_events_show(struct seq_file *m, atomic_long_t *events) { seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW])); --=20 2.36.0