mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sahaj Chaudhari <sahaj123.sc@gmail.com>
To: Tejun Heo <tj@kernel.org>
Cc: "Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Koutný" <mkoutny@suse.com>,
	"Shuah Khan" <shuah@kernel.org>,
	cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	"Sahaj Chaudhari" <sahaj123.sc@gmail.com>
Subject: [PATCH v4] selftests/cgroup: test pids.events notification
Date: Thu, 15 Oct 2026 16:42:43 +0530	[thread overview]
Message-ID: <20261015111243.1551387-1-sahaj123.sc@gmail.com> (raw)

Add a test to test_pids.c that checks a fork rejected by pids.max
increments pids.events and generates an IN_MODIFY inotify event on that
file. The test cgroup is the one over the limit, so the notification is
expected both on a default mount and with pids_localevents.

This covers the fix in commit 1765a153d985 ("cgroup/pids: Restore
pids.events notifications in local mode"), where a rejected fork changed
pids.events without waking its pollers when pids_localevents was enabled.

Tested on x86_64 under QEMU without KVM, on kernels built from the
cgroup for-7.4 branch (eb69e955de7b) and from the same tree with that
fix reverted. With the fix, the new test passes on a default cgroup2
mount and on a pids_localevents mount. With the fix reverted, it passes
on a default mount and fails on a pids_localevents mount. No kernel
warnings were logged.

Link: https://lore.kernel.org/r/20261013064812.875787-1-sahaj123.sc@gmail.com
Signed-off-by: Sahaj Chaudhari <sahaj123.sc@gmail.com>
---
Changes in v4:
- Move the test into test_pids.c as test_pids_events_notify(), using
  cg_run_nowait() and cg_control(), instead of a separate program. This
  drops the Makefile and .gitignore changes.
- Drop the pids_localevents skip. The test cgroup is the one over the
  limit, so the rejected fork notifies it on both mount types.
- Drop the duplicate pids.events check after reading the inotify event.
- Cite 1765a153d985 as the fix the test covers.
- Move the read() error-handling note from the commit message to this
  changelog.
- Rebase onto the cgroup for-7.4 branch.

Changes in v3:
- Check for a failed read() of the inotify event before comparing its
  signed result with sizeof(struct inotify_event).

v3: https://lore.kernel.org/r/20261013064812.875787-1-sahaj123.sc@gmail.com
 tools/testing/selftests/cgroup/test_pids.c | 73 ++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/tools/testing/selftests/cgroup/test_pids.c b/tools/testing/selftests/cgroup/test_pids.c
index 710109b53dfe..e775c5510cf2 100644
--- a/tools/testing/selftests/cgroup/test_pids.c
+++ b/tools/testing/selftests/cgroup/test_pids.c
@@ -3,8 +3,11 @@
 
 #include <errno.h>
 #include <linux/limits.h>
+#include <poll.h>
 #include <signal.h>
+#include <stdlib.h>
 #include <string.h>
+#include <sys/inotify.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -133,6 +136,75 @@ static int test_pids_events(const char *root)
 
 
 
+/*
+ * This test checks that a fork rejected by pids.max increments pids.events
+ * and notifies pollers of that file, with or without pids_localevents.
+ */
+static int test_pids_events_notify(const char *root)
+{
+	union {
+		struct inotify_event align;
+		char bytes[sizeof(struct inotify_event) + NAME_MAX + 1];
+	} event;
+	int ret = KSFT_FAIL;
+	int inotify_fd = -1;
+	char *cg_pids = NULL, *events_path = NULL;
+	struct pollfd pfd;
+	ssize_t n;
+
+	cg_pids = cg_name(root, "pids_notify_test");
+	if (!cg_pids)
+		goto cleanup;
+
+	if (cg_create(cg_pids))
+		goto cleanup;
+
+	if (cg_write(cg_pids, "pids.max", "1"))
+		goto cleanup;
+
+	events_path = cg_control(cg_pids, "pids.events");
+	if (!events_path)
+		goto cleanup;
+
+	inotify_fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
+	if (inotify_fd < 0)
+		goto cleanup;
+
+	if (inotify_add_watch(inotify_fd, events_path, IN_MODIFY) < 0)
+		goto cleanup;
+
+	if (cg_enter_current(cg_pids))
+		goto cleanup;
+
+	if (cg_run_nowait(cg_pids, run_success, NULL) != -1 || errno != EAGAIN)
+		goto cleanup;
+
+	if (cg_read_key_long(cg_pids, "pids.events", "max ") != 1)
+		goto cleanup;
+
+	pfd.fd = inotify_fd;
+	pfd.events = POLLIN;
+	if (poll(&pfd, 1, 1000) <= 0)
+		goto cleanup;
+
+	n = read(inotify_fd, event.bytes, sizeof(event.bytes));
+	if (n < (ssize_t)sizeof(event.align) || !(event.align.mask & IN_MODIFY))
+		goto cleanup;
+
+	ret = KSFT_PASS;
+
+cleanup:
+	cg_enter_current(root);
+	if (inotify_fd >= 0)
+		close(inotify_fd);
+	if (cg_pids)
+		cg_destroy(cg_pids);
+	free(events_path);
+	free(cg_pids);
+
+	return ret;
+}
+
 #define T(x) { x, #x }
 struct pids_test {
 	int (*fn)(const char *root);
@@ -140,6 +212,7 @@ struct pids_test {
 } tests[] = {
 	T(test_pids_max),
 	T(test_pids_events),
+	T(test_pids_events_notify),
 };
 #undef T
 
-- 
2.43.0


             reply	other threads:[~2026-10-15 11:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-15 11:12 Sahaj Chaudhari [this message]
2026-10-16 20:07 ` Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261015111243.1551387-1-sahaj123.sc@gmail.com \
    --to=sahaj123.sc@gmail.com \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mkoutny@suse.com \
    --cc=shuah@kernel.org \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®