xref: /linux/tools/testing/selftests/cgroup/test_pids.c (revision 1850c669516cb8568150937497a9a16a7bfa1582)
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 
4 #include <errno.h>
5 #include <linux/limits.h>
6 #include <poll.h>
7 #include <signal.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/inotify.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 
15 #include "kselftest.h"
16 #include "cgroup_util.h"
17 
18 static int run_success(const char *cgroup, void *arg)
19 {
20 	return 0;
21 }
22 
23 static int run_pause(const char *cgroup, void *arg)
24 {
25 	return pause();
26 }
27 
28 /*
29  * This test checks that pids.max prevents forking new children above the
30  * specified limit in the cgroup.
31  */
32 static int test_pids_max(const char *root)
33 {
34 	int ret = KSFT_FAIL;
35 	char *cg_pids;
36 	int pid;
37 
38 	cg_pids = cg_name(root, "pids_test");
39 	if (!cg_pids)
40 		goto cleanup;
41 
42 	if (cg_create(cg_pids))
43 		goto cleanup;
44 
45 	if (cg_read_strcmp(cg_pids, "pids.max", "max\n"))
46 		goto cleanup;
47 
48 	if (cg_write(cg_pids, "pids.max", "2"))
49 		goto cleanup;
50 
51 	if (cg_enter_current(cg_pids))
52 		goto cleanup;
53 
54 	pid = cg_run_nowait(cg_pids, run_pause, NULL);
55 	if (pid < 0)
56 		goto cleanup;
57 
58 	if (cg_run_nowait(cg_pids, run_success, NULL) != -1 || errno != EAGAIN)
59 		goto cleanup;
60 
61 	if (kill(pid, SIGINT))
62 		goto cleanup;
63 
64 	ret = KSFT_PASS;
65 
66 cleanup:
67 	cg_enter_current(root);
68 	cg_destroy(cg_pids);
69 	free(cg_pids);
70 
71 	return ret;
72 }
73 
74 /*
75  * This test checks that pids.events are counted in cgroup associated with pids.max
76  */
77 static int test_pids_events(const char *root)
78 {
79 	int ret = KSFT_FAIL;
80 	char *cg_parent = NULL, *cg_child = NULL;
81 	int pid;
82 
83 	if (cgroup_feature("pids_localevents") <= 0)
84 		return KSFT_SKIP;
85 
86 	cg_parent = cg_name(root, "pids_parent");
87 	cg_child = cg_name(cg_parent, "pids_child");
88 	if (!cg_parent || !cg_child)
89 		goto cleanup;
90 
91 	if (cg_create(cg_parent))
92 		goto cleanup;
93 	if (cg_write(cg_parent, "cgroup.subtree_control", "+pids"))
94 		goto cleanup;
95 	if (cg_create(cg_child))
96 		goto cleanup;
97 
98 	if (cg_write(cg_parent, "pids.max", "2"))
99 		goto cleanup;
100 
101 	if (cg_read_strcmp(cg_child, "pids.max", "max\n"))
102 		goto cleanup;
103 
104 	if (cg_enter_current(cg_child))
105 		goto cleanup;
106 
107 	pid = cg_run_nowait(cg_child, run_pause, NULL);
108 	if (pid < 0)
109 		goto cleanup;
110 
111 	if (cg_run_nowait(cg_child, run_success, NULL) != -1 || errno != EAGAIN)
112 		goto cleanup;
113 
114 	if (kill(pid, SIGINT))
115 		goto cleanup;
116 
117 	if (cg_read_key_long(cg_child, "pids.events", "max ") != 0)
118 		goto cleanup;
119 	if (cg_read_key_long(cg_parent, "pids.events", "max ") != 1)
120 		goto cleanup;
121 
122 
123 	ret = KSFT_PASS;
124 
125 cleanup:
126 	cg_enter_current(root);
127 	if (cg_child)
128 		cg_destroy(cg_child);
129 	if (cg_parent)
130 		cg_destroy(cg_parent);
131 	free(cg_child);
132 	free(cg_parent);
133 
134 	return ret;
135 }
136 
137 
138 
139 /*
140  * This test checks that a fork rejected by pids.max increments pids.events
141  * and notifies pollers of that file, with or without pids_localevents.
142  */
143 static int test_pids_events_notify(const char *root)
144 {
145 	union {
146 		struct inotify_event align;
147 		char bytes[sizeof(struct inotify_event) + NAME_MAX + 1];
148 	} event;
149 	int ret = KSFT_FAIL;
150 	int inotify_fd = -1;
151 	char *cg_pids = NULL, *events_path = NULL;
152 	struct pollfd pfd;
153 	ssize_t n;
154 
155 	cg_pids = cg_name(root, "pids_notify_test");
156 	if (!cg_pids)
157 		goto cleanup;
158 
159 	if (cg_create(cg_pids))
160 		goto cleanup;
161 
162 	if (cg_write(cg_pids, "pids.max", "1"))
163 		goto cleanup;
164 
165 	events_path = cg_control(cg_pids, "pids.events");
166 	if (!events_path)
167 		goto cleanup;
168 
169 	inotify_fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
170 	if (inotify_fd < 0)
171 		goto cleanup;
172 
173 	if (inotify_add_watch(inotify_fd, events_path, IN_MODIFY) < 0)
174 		goto cleanup;
175 
176 	if (cg_enter_current(cg_pids))
177 		goto cleanup;
178 
179 	if (cg_run_nowait(cg_pids, run_success, NULL) != -1 || errno != EAGAIN)
180 		goto cleanup;
181 
182 	if (cg_read_key_long(cg_pids, "pids.events", "max ") != 1)
183 		goto cleanup;
184 
185 	pfd.fd = inotify_fd;
186 	pfd.events = POLLIN;
187 	if (poll(&pfd, 1, 1000) <= 0)
188 		goto cleanup;
189 
190 	n = read(inotify_fd, event.bytes, sizeof(event.bytes));
191 	if (n < (ssize_t)sizeof(event.align) || !(event.align.mask & IN_MODIFY))
192 		goto cleanup;
193 
194 	ret = KSFT_PASS;
195 
196 cleanup:
197 	cg_enter_current(root);
198 	if (inotify_fd >= 0)
199 		close(inotify_fd);
200 	if (cg_pids)
201 		cg_destroy(cg_pids);
202 	free(events_path);
203 	free(cg_pids);
204 
205 	return ret;
206 }
207 
208 #define T(x) { x, #x }
209 struct pids_test {
210 	int (*fn)(const char *root);
211 	const char *name;
212 } tests[] = {
213 	T(test_pids_max),
214 	T(test_pids_events),
215 	T(test_pids_events_notify),
216 };
217 #undef T
218 
219 int main(int argc, char **argv)
220 {
221 	char root[PATH_MAX];
222 
223 	ksft_print_header();
224 	if (cg_find_unified_root(root, sizeof(root), NULL))
225 		ksft_exit_skip("cgroup v2 isn't mounted\n");
226 
227 	/*
228 	 * Check that pids controller is available:
229 	 * pids is listed in cgroup.controllers
230 	 */
231 	if (cg_read_strstr(root, "cgroup.controllers", "pids"))
232 		ksft_exit_skip("pids controller isn't available\n");
233 
234 	if (cg_read_strstr(root, "cgroup.subtree_control", "pids"))
235 		if (cg_write(root, "cgroup.subtree_control", "+pids"))
236 			ksft_exit_skip("Failed to set pids controller\n");
237 
238 	ksft_set_plan(ARRAY_SIZE(tests));
239 	for (int i = 0; i < ARRAY_SIZE(tests); i++) {
240 		switch (tests[i].fn(root)) {
241 		case KSFT_PASS:
242 			ksft_test_result_pass("%s\n", tests[i].name);
243 			break;
244 		case KSFT_SKIP:
245 			ksft_test_result_skip("%s\n", tests[i].name);
246 			break;
247 		default:
248 			ksft_test_result_fail("%s\n", tests[i].name);
249 			break;
250 		}
251 	}
252 
253 	ksft_finished();
254 }
255