diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2014-12-13 16:00:36 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-12-13 16:00:36 -0500 |
commit | 78a45c6f067824cf5d0a9fedea7339ac2e28603c (patch) | |
tree | b4f78c8b6b9059ddace0a18c11629b8d2045f793 /tools | |
parent | f96fe225677b3efb74346ebd56fafe3997b02afa (diff) | |
parent | 29d293b6007b91a4463f05bc8d0b26e0e65c5816 (diff) |
Merge branch 'akpm' (second patch-bomb from Andrew)
Merge second patchbomb from Andrew Morton:
- the rest of MM
- misc fs fixes
- add execveat() syscall
- new ratelimit feature for fault-injection
- decompressor updates
- ipc/ updates
- fallocate feature creep
- fsnotify cleanups
- a few other misc things
* emailed patches from Andrew Morton <akpm@linux-foundation.org>: (99 commits)
cgroups: Documentation: fix trivial typos and wrong paragraph numberings
parisc: percpu: update comments referring to __get_cpu_var
percpu: update local_ops.txt to reflect this_cpu operations
percpu: remove __get_cpu_var and __raw_get_cpu_var macros
fsnotify: remove destroy_list from fsnotify_mark
fsnotify: unify inode and mount marks handling
fallocate: create FAN_MODIFY and IN_MODIFY events
mm/cma: make kmemleak ignore CMA regions
slub: fix cpuset check in get_any_partial
slab: fix cpuset check in fallback_alloc
shmdt: use i_size_read() instead of ->i_size
ipc/shm.c: fix overly aggressive shmdt() when calls span multiple segments
ipc/msg: increase MSGMNI, remove scaling
ipc/sem.c: increase SEMMSL, SEMMNI, SEMOPM
ipc/sem.c: change memory barrier in sem_lock() to smp_rmb()
lib/decompress.c: consistency of compress formats for kernel image
decompress_bunzip2: off by one in get_next_block()
usr/Kconfig: make initrd compression algorithm selection not expert
fault-inject: add ratelimit option
ratelimit: add initialization macro
...
Diffstat (limited to 'tools')
-rw-r--r-- | tools/testing/selftests/Makefile | 1 | ||||
-rw-r--r-- | tools/testing/selftests/exec/.gitignore | 9 | ||||
-rw-r--r-- | tools/testing/selftests/exec/Makefile | 25 | ||||
-rw-r--r-- | tools/testing/selftests/exec/execveat.c | 397 | ||||
-rw-r--r-- | tools/vm/Makefile | 4 | ||||
-rw-r--r-- | tools/vm/page_owner_sort.c | 144 |
6 files changed, 578 insertions, 2 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 45f145c6f843..c14893b501a9 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile | |||
@@ -15,6 +15,7 @@ TARGETS += user | |||
15 | TARGETS += sysctl | 15 | TARGETS += sysctl |
16 | TARGETS += firmware | 16 | TARGETS += firmware |
17 | TARGETS += ftrace | 17 | TARGETS += ftrace |
18 | TARGETS += exec | ||
18 | 19 | ||
19 | TARGETS_HOTPLUG = cpu-hotplug | 20 | TARGETS_HOTPLUG = cpu-hotplug |
20 | TARGETS_HOTPLUG += memory-hotplug | 21 | TARGETS_HOTPLUG += memory-hotplug |
diff --git a/tools/testing/selftests/exec/.gitignore b/tools/testing/selftests/exec/.gitignore new file mode 100644 index 000000000000..64073e050c6a --- /dev/null +++ b/tools/testing/selftests/exec/.gitignore | |||
@@ -0,0 +1,9 @@ | |||
1 | subdir* | ||
2 | script* | ||
3 | execveat | ||
4 | execveat.symlink | ||
5 | execveat.moved | ||
6 | execveat.path.ephemeral | ||
7 | execveat.ephemeral | ||
8 | execveat.denatured | ||
9 | xxxxxxxx* \ No newline at end of file | ||
diff --git a/tools/testing/selftests/exec/Makefile b/tools/testing/selftests/exec/Makefile new file mode 100644 index 000000000000..66dfc2ce1788 --- /dev/null +++ b/tools/testing/selftests/exec/Makefile | |||
@@ -0,0 +1,25 @@ | |||
1 | CC = $(CROSS_COMPILE)gcc | ||
2 | CFLAGS = -Wall | ||
3 | BINARIES = execveat | ||
4 | DEPS = execveat.symlink execveat.denatured script subdir | ||
5 | all: $(BINARIES) $(DEPS) | ||
6 | |||
7 | subdir: | ||
8 | mkdir -p $@ | ||
9 | script: | ||
10 | echo '#!/bin/sh' > $@ | ||
11 | echo 'exit $$*' >> $@ | ||
12 | chmod +x $@ | ||
13 | execveat.symlink: execveat | ||
14 | ln -s -f $< $@ | ||
15 | execveat.denatured: execveat | ||
16 | cp $< $@ | ||
17 | chmod -x $@ | ||
18 | %: %.c | ||
19 | $(CC) $(CFLAGS) -o $@ $^ | ||
20 | |||
21 | run_tests: all | ||
22 | ./execveat | ||
23 | |||
24 | clean: | ||
25 | rm -rf $(BINARIES) $(DEPS) subdir.moved execveat.moved xxxxx* | ||
diff --git a/tools/testing/selftests/exec/execveat.c b/tools/testing/selftests/exec/execveat.c new file mode 100644 index 000000000000..33a5c06d95ca --- /dev/null +++ b/tools/testing/selftests/exec/execveat.c | |||
@@ -0,0 +1,397 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2014 Google, Inc. | ||
3 | * | ||
4 | * Licensed under the terms of the GNU GPL License version 2 | ||
5 | * | ||
6 | * Selftests for execveat(2). | ||
7 | */ | ||
8 | |||
9 | #define _GNU_SOURCE /* to get O_PATH, AT_EMPTY_PATH */ | ||
10 | #include <sys/sendfile.h> | ||
11 | #include <sys/stat.h> | ||
12 | #include <sys/syscall.h> | ||
13 | #include <sys/types.h> | ||
14 | #include <sys/wait.h> | ||
15 | #include <errno.h> | ||
16 | #include <fcntl.h> | ||
17 | #include <limits.h> | ||
18 | #include <stdio.h> | ||
19 | #include <stdlib.h> | ||
20 | #include <string.h> | ||
21 | #include <unistd.h> | ||
22 | |||
23 | static char longpath[2 * PATH_MAX] = ""; | ||
24 | static char *envp[] = { "IN_TEST=yes", NULL, NULL }; | ||
25 | static char *argv[] = { "execveat", "99", NULL }; | ||
26 | |||
27 | static int execveat_(int fd, const char *path, char **argv, char **envp, | ||
28 | int flags) | ||
29 | { | ||
30 | #ifdef __NR_execveat | ||
31 | return syscall(__NR_execveat, fd, path, argv, envp, flags); | ||
32 | #else | ||
33 | errno = -ENOSYS; | ||
34 | return -1; | ||
35 | #endif | ||
36 | } | ||
37 | |||
38 | #define check_execveat_fail(fd, path, flags, errno) \ | ||
39 | _check_execveat_fail(fd, path, flags, errno, #errno) | ||
40 | static int _check_execveat_fail(int fd, const char *path, int flags, | ||
41 | int expected_errno, const char *errno_str) | ||
42 | { | ||
43 | int rc; | ||
44 | |||
45 | errno = 0; | ||
46 | printf("Check failure of execveat(%d, '%s', %d) with %s... ", | ||
47 | fd, path?:"(null)", flags, errno_str); | ||
48 | rc = execveat_(fd, path, argv, envp, flags); | ||
49 | |||
50 | if (rc > 0) { | ||
51 | printf("[FAIL] (unexpected success from execveat(2))\n"); | ||
52 | return 1; | ||
53 | } | ||
54 | if (errno != expected_errno) { | ||
55 | printf("[FAIL] (expected errno %d (%s) not %d (%s)\n", | ||
56 | expected_errno, strerror(expected_errno), | ||
57 | errno, strerror(errno)); | ||
58 | return 1; | ||
59 | } | ||
60 | printf("[OK]\n"); | ||
61 | return 0; | ||
62 | } | ||
63 | |||
64 | static int check_execveat_invoked_rc(int fd, const char *path, int flags, | ||
65 | int expected_rc) | ||
66 | { | ||
67 | int status; | ||
68 | int rc; | ||
69 | pid_t child; | ||
70 | int pathlen = path ? strlen(path) : 0; | ||
71 | |||
72 | if (pathlen > 40) | ||
73 | printf("Check success of execveat(%d, '%.20s...%s', %d)... ", | ||
74 | fd, path, (path + pathlen - 20), flags); | ||
75 | else | ||
76 | printf("Check success of execveat(%d, '%s', %d)... ", | ||
77 | fd, path?:"(null)", flags); | ||
78 | child = fork(); | ||
79 | if (child < 0) { | ||
80 | printf("[FAIL] (fork() failed)\n"); | ||
81 | return 1; | ||
82 | } | ||
83 | if (child == 0) { | ||
84 | /* Child: do execveat(). */ | ||
85 | rc = execveat_(fd, path, argv, envp, flags); | ||
86 | printf("[FAIL]: execveat() failed, rc=%d errno=%d (%s)\n", | ||
87 | rc, errno, strerror(errno)); | ||
88 | exit(1); /* should not reach here */ | ||
89 | } | ||
90 | /* Parent: wait for & check child's exit status. */ | ||
91 | rc = waitpid(child, &status, 0); | ||
92 | if (rc != child) { | ||
93 | printf("[FAIL] (waitpid(%d,...) returned %d)\n", child, rc); | ||
94 | return 1; | ||
95 | } | ||
96 | if (!WIFEXITED(status)) { | ||
97 | printf("[FAIL] (child %d did not exit cleanly, status=%08x)\n", | ||
98 | child, status); | ||
99 | return 1; | ||
100 | } | ||
101 | if (WEXITSTATUS(status) != expected_rc) { | ||
102 | printf("[FAIL] (child %d exited with %d not %d)\n", | ||
103 | child, WEXITSTATUS(status), expected_rc); | ||
104 | return 1; | ||
105 | } | ||
106 | printf("[OK]\n"); | ||
107 | return 0; | ||
108 | } | ||
109 | |||
110 | static int check_execveat(int fd, const char *path, int flags) | ||
111 | { | ||
112 | return check_execveat_invoked_rc(fd, path, flags, 99); | ||
113 | } | ||
114 | |||
115 | static char *concat(const char *left, const char *right) | ||
116 | { | ||
117 | char *result = malloc(strlen(left) + strlen(right) + 1); | ||
118 | |||
119 | strcpy(result, left); | ||
120 | strcat(result, right); | ||
121 | return result; | ||
122 | } | ||
123 | |||
124 | static int open_or_die(const char *filename, int flags) | ||
125 | { | ||
126 | int fd = open(filename, flags); | ||
127 | |||
128 | if (fd < 0) { | ||
129 | printf("Failed to open '%s'; " | ||
130 | "check prerequisites are available\n", filename); | ||
131 | exit(1); | ||
132 | } | ||
133 | return fd; | ||
134 | } | ||
135 | |||
136 | static void exe_cp(const char *src, const char *dest) | ||
137 | { | ||
138 | int in_fd = open_or_die(src, O_RDONLY); | ||
139 | int out_fd = open(dest, O_RDWR|O_CREAT|O_TRUNC, 0755); | ||
140 | struct stat info; | ||
141 | |||
142 | fstat(in_fd, &info); | ||
143 | sendfile(out_fd, in_fd, NULL, info.st_size); | ||
144 | close(in_fd); | ||
145 | close(out_fd); | ||
146 | } | ||
147 | |||
148 | #define XX_DIR_LEN 200 | ||
149 | static int check_execveat_pathmax(int dot_dfd, const char *src, int is_script) | ||
150 | { | ||
151 | int fail = 0; | ||
152 | int ii, count, len; | ||
153 | char longname[XX_DIR_LEN + 1]; | ||
154 | int fd; | ||
155 | |||
156 | if (*longpath == '\0') { | ||
157 | /* Create a filename close to PATH_MAX in length */ | ||
158 | memset(longname, 'x', XX_DIR_LEN - 1); | ||
159 | longname[XX_DIR_LEN - 1] = '/'; | ||
160 | longname[XX_DIR_LEN] = '\0'; | ||
161 | count = (PATH_MAX - 3) / XX_DIR_LEN; | ||
162 | for (ii = 0; ii < count; ii++) { | ||
163 | strcat(longpath, longname); | ||
164 | mkdir(longpath, 0755); | ||
165 | } | ||
166 | len = (PATH_MAX - 3) - (count * XX_DIR_LEN); | ||
167 | if (len <= 0) | ||
168 | len = 1; | ||
169 | memset(longname, 'y', len); | ||
170 | longname[len] = '\0'; | ||
171 | strcat(longpath, longname); | ||
172 | } | ||
173 | exe_cp(src, longpath); | ||
174 | |||
175 | /* | ||
176 | * Execute as a pre-opened file descriptor, which works whether this is | ||
177 | * a script or not (because the interpreter sees a filename like | ||
178 | * "/dev/fd/20"). | ||
179 | */ | ||
180 | fd = open(longpath, O_RDONLY); | ||
181 | if (fd > 0) { | ||
182 | printf("Invoke copy of '%s' via filename of length %lu:\n", | ||
183 | src, strlen(longpath)); | ||
184 | fail += check_execveat(fd, "", AT_EMPTY_PATH); | ||
185 | } else { | ||
186 | printf("Failed to open length %lu filename, errno=%d (%s)\n", | ||
187 | strlen(longpath), errno, strerror(errno)); | ||
188 | fail++; | ||
189 | } | ||
190 | |||
191 | /* | ||
192 | * Execute as a long pathname relative to ".". If this is a script, | ||
193 | * the interpreter will launch but fail to open the script because its | ||
194 | * name ("/dev/fd/5/xxx....") is bigger than PATH_MAX. | ||
195 | */ | ||
196 | if (is_script) | ||
197 | fail += check_execveat_invoked_rc(dot_dfd, longpath, 0, 127); | ||
198 | else | ||
199 | fail += check_execveat(dot_dfd, longpath, 0); | ||
200 | |||
201 | return fail; | ||
202 | } | ||
203 | |||
204 | static int run_tests(void) | ||
205 | { | ||
206 | int fail = 0; | ||
207 | char *fullname = realpath("execveat", NULL); | ||
208 | char *fullname_script = realpath("script", NULL); | ||
209 | char *fullname_symlink = concat(fullname, ".symlink"); | ||
210 | int subdir_dfd = open_or_die("subdir", O_DIRECTORY|O_RDONLY); | ||
211 | int subdir_dfd_ephemeral = open_or_die("subdir.ephemeral", | ||
212 | O_DIRECTORY|O_RDONLY); | ||
213 | int dot_dfd = open_or_die(".", O_DIRECTORY|O_RDONLY); | ||
214 | int dot_dfd_path = open_or_die(".", O_DIRECTORY|O_RDONLY|O_PATH); | ||
215 | int dot_dfd_cloexec = open_or_die(".", O_DIRECTORY|O_RDONLY|O_CLOEXEC); | ||
216 | int fd = open_or_die("execveat", O_RDONLY); | ||
217 | int fd_path = open_or_die("execveat", O_RDONLY|O_PATH); | ||
218 | int fd_symlink = open_or_die("execveat.symlink", O_RDONLY); | ||
219 | int fd_denatured = open_or_die("execveat.denatured", O_RDONLY); | ||
220 | int fd_denatured_path = open_or_die("execveat.denatured", | ||
221 | O_RDONLY|O_PATH); | ||
222 | int fd_script = open_or_die("script", O_RDONLY); | ||
223 | int fd_ephemeral = open_or_die("execveat.ephemeral", O_RDONLY); | ||
224 | int fd_ephemeral_path = open_or_die("execveat.path.ephemeral", | ||
225 | O_RDONLY|O_PATH); | ||
226 | int fd_script_ephemeral = open_or_die("script.ephemeral", O_RDONLY); | ||
227 | int fd_cloexec = open_or_die("execveat", O_RDONLY|O_CLOEXEC); | ||
228 | int fd_script_cloexec = open_or_die("script", O_RDONLY|O_CLOEXEC); | ||
229 | |||
230 | /* Change file position to confirm it doesn't affect anything */ | ||
231 | lseek(fd, 10, SEEK_SET); | ||
232 | |||
233 | /* Normal executable file: */ | ||
234 | /* dfd + path */ | ||
235 | fail += check_execveat(subdir_dfd, "../execveat", 0); | ||
236 | fail += check_execveat(dot_dfd, "execveat", 0); | ||
237 | fail += check_execveat(dot_dfd_path, "execveat", 0); | ||
238 | /* absolute path */ | ||
239 | fail += check_execveat(AT_FDCWD, fullname, 0); | ||
240 | /* absolute path with nonsense dfd */ | ||
241 | fail += check_execveat(99, fullname, 0); | ||
242 | /* fd + no path */ | ||
243 | fail += check_execveat(fd, "", AT_EMPTY_PATH); | ||
244 | /* O_CLOEXEC fd + no path */ | ||
245 | fail += check_execveat(fd_cloexec, "", AT_EMPTY_PATH); | ||
246 | /* O_PATH fd */ | ||
247 | fail += check_execveat(fd_path, "", AT_EMPTY_PATH); | ||
248 | |||
249 | /* Mess with executable file that's already open: */ | ||
250 | /* fd + no path to a file that's been renamed */ | ||
251 | rename("execveat.ephemeral", "execveat.moved"); | ||
252 | fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH); | ||
253 | /* fd + no path to a file that's been deleted */ | ||
254 | unlink("execveat.moved"); /* remove the file now fd open */ | ||
255 | fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH); | ||
256 | |||
257 | /* Mess with executable file that's already open with O_PATH */ | ||
258 | /* fd + no path to a file that's been deleted */ | ||
259 | unlink("execveat.path.ephemeral"); | ||
260 | fail += check_execveat(fd_ephemeral_path, "", AT_EMPTY_PATH); | ||
261 | |||
262 | /* Invalid argument failures */ | ||
263 | fail += check_execveat_fail(fd, "", 0, ENOENT); | ||
264 | fail += check_execveat_fail(fd, NULL, AT_EMPTY_PATH, EFAULT); | ||
265 | |||
266 | /* Symlink to executable file: */ | ||
267 | /* dfd + path */ | ||
268 | fail += check_execveat(dot_dfd, "execveat.symlink", 0); | ||
269 | fail += check_execveat(dot_dfd_path, "execveat.symlink", 0); | ||
270 | /* absolute path */ | ||
271 | fail += check_execveat(AT_FDCWD, fullname_symlink, 0); | ||
272 | /* fd + no path, even with AT_SYMLINK_NOFOLLOW (already followed) */ | ||
273 | fail += check_execveat(fd_symlink, "", AT_EMPTY_PATH); | ||
274 | fail += check_execveat(fd_symlink, "", | ||
275 | AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW); | ||
276 | |||
277 | /* Symlink fails when AT_SYMLINK_NOFOLLOW set: */ | ||
278 | /* dfd + path */ | ||
279 | fail += check_execveat_fail(dot_dfd, "execveat.symlink", | ||
280 | AT_SYMLINK_NOFOLLOW, ELOOP); | ||
281 | fail += check_execveat_fail(dot_dfd_path, "execveat.symlink", | ||
282 | AT_SYMLINK_NOFOLLOW, ELOOP); | ||
283 | /* absolute path */ | ||
284 | fail += check_execveat_fail(AT_FDCWD, fullname_symlink, | ||
285 | AT_SYMLINK_NOFOLLOW, ELOOP); | ||
286 | |||
287 | /* Shell script wrapping executable file: */ | ||
288 | /* dfd + path */ | ||
289 | fail += check_execveat(subdir_dfd, "../script", 0); | ||
290 | fail += check_execveat(dot_dfd, "script", 0); | ||
291 | fail += check_execveat(dot_dfd_path, "script", 0); | ||
292 | /* absolute path */ | ||
293 | fail += check_execveat(AT_FDCWD, fullname_script, 0); | ||
294 | /* fd + no path */ | ||
295 | fail += check_execveat(fd_script, "", AT_EMPTY_PATH); | ||
296 | fail += check_execveat(fd_script, "", | ||
297 | AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW); | ||
298 | /* O_CLOEXEC fd fails for a script (as script file inaccessible) */ | ||
299 | fail += check_execveat_fail(fd_script_cloexec, "", AT_EMPTY_PATH, | ||
300 | ENOENT); | ||
301 | fail += check_execveat_fail(dot_dfd_cloexec, "script", 0, ENOENT); | ||
302 | |||
303 | /* Mess with script file that's already open: */ | ||
304 | /* fd + no path to a file that's been renamed */ | ||
305 | rename("script.ephemeral", "script.moved"); | ||
306 | fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH); | ||
307 | /* fd + no path to a file that's been deleted */ | ||
308 | unlink("script.moved"); /* remove the file while fd open */ | ||
309 | fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH); | ||
310 | |||
311 | /* Rename a subdirectory in the path: */ | ||
312 | rename("subdir.ephemeral", "subdir.moved"); | ||
313 | fail += check_execveat(subdir_dfd_ephemeral, "../script", 0); | ||
314 | fail += check_execveat(subdir_dfd_ephemeral, "script", 0); | ||
315 | /* Remove the subdir and its contents */ | ||
316 | unlink("subdir.moved/script"); | ||
317 | unlink("subdir.moved"); | ||
318 | /* Shell loads via deleted subdir OK because name starts with .. */ | ||
319 | fail += check_execveat(subdir_dfd_ephemeral, "../script", 0); | ||
320 | fail += check_execveat_fail(subdir_dfd_ephemeral, "script", 0, ENOENT); | ||
321 | |||
322 | /* Flag values other than AT_SYMLINK_NOFOLLOW => EINVAL */ | ||
323 | fail += check_execveat_fail(dot_dfd, "execveat", 0xFFFF, EINVAL); | ||
324 | /* Invalid path => ENOENT */ | ||
325 | fail += check_execveat_fail(dot_dfd, "no-such-file", 0, ENOENT); | ||
326 | fail += check_execveat_fail(dot_dfd_path, "no-such-file", 0, ENOENT); | ||
327 | fail += check_execveat_fail(AT_FDCWD, "no-such-file", 0, ENOENT); | ||
328 | /* Attempt to execute directory => EACCES */ | ||
329 | fail += check_execveat_fail(dot_dfd, "", AT_EMPTY_PATH, EACCES); | ||
330 | /* Attempt to execute non-executable => EACCES */ | ||
331 | fail += check_execveat_fail(dot_dfd, "Makefile", 0, EACCES); | ||
332 | fail += check_execveat_fail(fd_denatured, "", AT_EMPTY_PATH, EACCES); | ||
333 | fail += check_execveat_fail(fd_denatured_path, "", AT_EMPTY_PATH, | ||
334 | EACCES); | ||
335 | /* Attempt to execute nonsense FD => EBADF */ | ||
336 | fail += check_execveat_fail(99, "", AT_EMPTY_PATH, EBADF); | ||
337 | fail += check_execveat_fail(99, "execveat", 0, EBADF); | ||
338 | /* Attempt to execute relative to non-directory => ENOTDIR */ | ||
339 | fail += check_execveat_fail(fd, "execveat", 0, ENOTDIR); | ||
340 | |||
341 | fail += check_execveat_pathmax(dot_dfd, "execveat", 0); | ||
342 | fail += check_execveat_pathmax(dot_dfd, "script", 1); | ||
343 | return fail; | ||
344 | } | ||
345 | |||
346 | static void prerequisites(void) | ||
347 | { | ||
348 | int fd; | ||
349 | const char *script = "#!/bin/sh\nexit $*\n"; | ||
350 | |||
351 | /* Create ephemeral copies of files */ | ||
352 | exe_cp("execveat", "execveat.ephemeral"); | ||
353 | exe_cp("execveat", "execveat.path.ephemeral"); | ||
354 | exe_cp("script", "script.ephemeral"); | ||
355 | mkdir("subdir.ephemeral", 0755); | ||
356 | |||
357 | fd = open("subdir.ephemeral/script", O_RDWR|O_CREAT|O_TRUNC, 0755); | ||
358 | write(fd, script, strlen(script)); | ||
359 | close(fd); | ||
360 | } | ||
361 | |||
362 | int main(int argc, char **argv) | ||
363 | { | ||
364 | int ii; | ||
365 | int rc; | ||
366 | const char *verbose = getenv("VERBOSE"); | ||
367 | |||
368 | if (argc >= 2) { | ||
369 | /* If we are invoked with an argument, don't run tests. */ | ||
370 | const char *in_test = getenv("IN_TEST"); | ||
371 | |||
372 | if (verbose) { | ||
373 | printf(" invoked with:"); | ||
374 | for (ii = 0; ii < argc; ii++) | ||
375 | printf(" [%d]='%s'", ii, argv[ii]); | ||
376 | printf("\n"); | ||
377 | } | ||
378 | |||
379 | /* Check expected environment transferred. */ | ||
380 | if (!in_test || strcmp(in_test, "yes") != 0) { | ||
381 | printf("[FAIL] (no IN_TEST=yes in env)\n"); | ||
382 | return 1; | ||
383 | } | ||
384 | |||
385 | /* Use the final argument as an exit code. */ | ||
386 | rc = atoi(argv[argc - 1]); | ||
387 | fflush(stdout); | ||
388 | } else { | ||
389 | prerequisites(); | ||
390 | if (verbose) | ||
391 | envp[1] = "VERBOSE=1"; | ||
392 | rc = run_tests(); | ||
393 | if (rc > 0) | ||
394 | printf("%d tests failed\n", rc); | ||
395 | } | ||
396 | return rc; | ||
397 | } | ||
diff --git a/tools/vm/Makefile b/tools/vm/Makefile index 3d907dacf2ac..ac884b65a072 100644 --- a/tools/vm/Makefile +++ b/tools/vm/Makefile | |||
@@ -1,6 +1,6 @@ | |||
1 | # Makefile for vm tools | 1 | # Makefile for vm tools |
2 | # | 2 | # |
3 | TARGETS=page-types slabinfo | 3 | TARGETS=page-types slabinfo page_owner_sort |
4 | 4 | ||
5 | LIB_DIR = ../lib/api | 5 | LIB_DIR = ../lib/api |
6 | LIBS = $(LIB_DIR)/libapikfs.a | 6 | LIBS = $(LIB_DIR)/libapikfs.a |
@@ -18,5 +18,5 @@ $(LIBS): | |||
18 | $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) | 18 | $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) |
19 | 19 | ||
20 | clean: | 20 | clean: |
21 | $(RM) page-types slabinfo | 21 | $(RM) page-types slabinfo page_owner_sort |
22 | make -C $(LIB_DIR) clean | 22 | make -C $(LIB_DIR) clean |
diff --git a/tools/vm/page_owner_sort.c b/tools/vm/page_owner_sort.c new file mode 100644 index 000000000000..77147b42d598 --- /dev/null +++ b/tools/vm/page_owner_sort.c | |||
@@ -0,0 +1,144 @@ | |||
1 | /* | ||
2 | * User-space helper to sort the output of /sys/kernel/debug/page_owner | ||
3 | * | ||
4 | * Example use: | ||
5 | * cat /sys/kernel/debug/page_owner > page_owner_full.txt | ||
6 | * grep -v ^PFN page_owner_full.txt > page_owner.txt | ||
7 | * ./sort page_owner.txt sorted_page_owner.txt | ||
8 | */ | ||
9 | |||
10 | #include <stdio.h> | ||
11 | #include <stdlib.h> | ||
12 | #include <sys/types.h> | ||
13 | #include <sys/stat.h> | ||
14 | #include <fcntl.h> | ||
15 | #include <unistd.h> | ||
16 | #include <string.h> | ||
17 | |||
18 | struct block_list { | ||
19 | char *txt; | ||
20 | int len; | ||
21 | int num; | ||
22 | }; | ||
23 | |||
24 | |||
25 | static struct block_list *list; | ||
26 | static int list_size; | ||
27 | static int max_size; | ||
28 | |||
29 | struct block_list *block_head; | ||
30 | |||
31 | int read_block(char *buf, int buf_size, FILE *fin) | ||
32 | { | ||
33 | char *curr = buf, *const buf_end = buf + buf_size; | ||
34 | |||
35 | while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) { | ||
36 | if (*curr == '\n') /* empty line */ | ||
37 | return curr - buf; | ||
38 | curr += strlen(curr); | ||
39 | } | ||
40 | |||
41 | return -1; /* EOF or no space left in buf. */ | ||
42 | } | ||
43 | |||
44 | static int compare_txt(const void *p1, const void *p2) | ||
45 | { | ||
46 | const struct block_list *l1 = p1, *l2 = p2; | ||
47 | |||
48 | return strcmp(l1->txt, l2->txt); | ||
49 | } | ||
50 | |||
51 | static int compare_num(const void *p1, const void *p2) | ||
52 | { | ||
53 | const struct block_list *l1 = p1, *l2 = p2; | ||
54 | |||
55 | return l2->num - l1->num; | ||
56 | } | ||
57 | |||
58 | static void add_list(char *buf, int len) | ||
59 | { | ||
60 | if (list_size != 0 && | ||
61 | len == list[list_size-1].len && | ||
62 | memcmp(buf, list[list_size-1].txt, len) == 0) { | ||
63 | list[list_size-1].num++; | ||
64 | return; | ||
65 | } | ||
66 | if (list_size == max_size) { | ||
67 | printf("max_size too small??\n"); | ||
68 | exit(1); | ||
69 | } | ||
70 | list[list_size].txt = malloc(len+1); | ||
71 | list[list_size].len = len; | ||
72 | list[list_size].num = 1; | ||
73 | memcpy(list[list_size].txt, buf, len); | ||
74 | list[list_size].txt[len] = 0; | ||
75 | list_size++; | ||
76 | if (list_size % 1000 == 0) { | ||
77 | printf("loaded %d\r", list_size); | ||
78 | fflush(stdout); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | #define BUF_SIZE 1024 | ||
83 | |||
84 | int main(int argc, char **argv) | ||
85 | { | ||
86 | FILE *fin, *fout; | ||
87 | char buf[BUF_SIZE]; | ||
88 | int ret, i, count; | ||
89 | struct block_list *list2; | ||
90 | struct stat st; | ||
91 | |||
92 | if (argc < 3) { | ||
93 | printf("Usage: ./program <input> <output>\n"); | ||
94 | perror("open: "); | ||
95 | exit(1); | ||
96 | } | ||
97 | |||
98 | fin = fopen(argv[1], "r"); | ||
99 | fout = fopen(argv[2], "w"); | ||
100 | if (!fin || !fout) { | ||
101 | printf("Usage: ./program <input> <output>\n"); | ||
102 | perror("open: "); | ||
103 | exit(1); | ||
104 | } | ||
105 | |||
106 | fstat(fileno(fin), &st); | ||
107 | max_size = st.st_size / 100; /* hack ... */ | ||
108 | |||
109 | list = malloc(max_size * sizeof(*list)); | ||
110 | |||
111 | for ( ; ; ) { | ||
112 | ret = read_block(buf, BUF_SIZE, fin); | ||
113 | if (ret < 0) | ||
114 | break; | ||
115 | |||
116 | add_list(buf, ret); | ||
117 | } | ||
118 | |||
119 | printf("loaded %d\n", list_size); | ||
120 | |||
121 | printf("sorting ....\n"); | ||
122 | |||
123 | qsort(list, list_size, sizeof(list[0]), compare_txt); | ||
124 | |||
125 | list2 = malloc(sizeof(*list) * list_size); | ||
126 | |||
127 | printf("culling\n"); | ||
128 | |||
129 | for (i = count = 0; i < list_size; i++) { | ||
130 | if (count == 0 || | ||
131 | strcmp(list2[count-1].txt, list[i].txt) != 0) { | ||
132 | list2[count++] = list[i]; | ||
133 | } else { | ||
134 | list2[count-1].num += list[i].num; | ||
135 | } | ||
136 | } | ||
137 | |||
138 | qsort(list2, count, sizeof(list[0]), compare_num); | ||
139 | |||
140 | for (i = 0; i < count; i++) | ||
141 | fprintf(fout, "%d times:\n%s\n", list2[i].num, list2[i].txt); | ||
142 | |||
143 | return 0; | ||
144 | } | ||