Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

proc: test "Kthread:" field

/proc/${pid}/status got Kthread field recently.

Test that userspace program is not reported as kernel thread.

Test that kernel thread is reported as kernel thread.
Use kthreadd with pid 2 for this.

Link: https://lkml.kernel.org/r/818c4c41-8668-4566-97a9-7254abf819ee@p183
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Chunguang Wu <fullspring2018@gmail.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Alexey Dobriyan and committed by
Andrew Morton
e7679a5c d0bff054

+94
+2
tools/testing/selftests/proc/.gitignore
··· 2 2 /fd-001-lookup 3 3 /fd-002-posix-eq 4 4 /fd-003-kthread 5 + /proc-2-is-kthread 5 6 /proc-fsconfig-hidepid 6 7 /proc-loadavg-001 7 8 /proc-multiple-procfs ··· 10 9 /proc-pid-vm 11 10 /proc-self-map-files-001 12 11 /proc-self-map-files-002 12 + /proc-self-isnt-kthread 13 13 /proc-self-syscall 14 14 /proc-self-wchan 15 15 /proc-subset-pid
+2
tools/testing/selftests/proc/Makefile
··· 7 7 TEST_GEN_PROGS += fd-001-lookup 8 8 TEST_GEN_PROGS += fd-002-posix-eq 9 9 TEST_GEN_PROGS += fd-003-kthread 10 + TEST_GEN_PROGS += proc-2-is-kthread 10 11 TEST_GEN_PROGS += proc-loadavg-001 11 12 TEST_GEN_PROGS += proc-empty-vm 12 13 TEST_GEN_PROGS += proc-pid-vm 13 14 TEST_GEN_PROGS += proc-self-map-files-001 14 15 TEST_GEN_PROGS += proc-self-map-files-002 16 + TEST_GEN_PROGS += proc-self-isnt-kthread 15 17 TEST_GEN_PROGS += proc-self-syscall 16 18 TEST_GEN_PROGS += proc-self-wchan 17 19 TEST_GEN_PROGS += proc-subset-pid
+53
tools/testing/selftests/proc/proc-2-is-kthread.c
··· 1 + /* 2 + * Copyright (c) 2024 Alexey Dobriyan <adobriyan@gmail.com> 3 + * 4 + * Permission to use, copy, modify, and distribute this software for any 5 + * purpose with or without fee is hereby granted, provided that the above 6 + * copyright notice and this permission notice appear in all copies. 7 + * 8 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 + */ 16 + /* Test that kernel thread is reported as such. */ 17 + #undef NDEBUG 18 + #include <assert.h> 19 + #include <errno.h> 20 + #include <fcntl.h> 21 + #include <string.h> 22 + #include <unistd.h> 23 + 24 + int main(void) 25 + { 26 + /* 27 + * The following solutions don't really work: 28 + * 29 + * 1) jit kernel module which creates kernel thread: 30 + * test becomes arch-specific, 31 + * problems with mandatory module signing, 32 + * problems with lockdown mode, 33 + * doesn't work with CONFIG_MODULES=n at all, 34 + * kthread creation API is formally unstable internal kernel API, 35 + * need a mechanism to report test kernel thread's PID back, 36 + * 37 + * 2) ksoftirqd/0 and kswapd0 look like stable enough kernel threads, 38 + * but their PIDs are unstable. 39 + * 40 + * Check against kthreadd which always seem to exist under pid 2. 41 + */ 42 + int fd = open("/proc/2/status", O_RDONLY); 43 + assert(fd >= 0); 44 + 45 + char buf[4096]; 46 + ssize_t rv = read(fd, buf, sizeof(buf)); 47 + assert(0 <= rv && rv < sizeof(buf)); 48 + buf[rv] = '\0'; 49 + 50 + assert(strstr(buf, "Kthread:\t1\n")); 51 + 52 + return 0; 53 + }
+37
tools/testing/selftests/proc/proc-self-isnt-kthread.c
··· 1 + /* 2 + * Copyright (c) 2024 Alexey Dobriyan <adobriyan@gmail.com> 3 + * 4 + * Permission to use, copy, modify, and distribute this software for any 5 + * purpose with or without fee is hereby granted, provided that the above 6 + * copyright notice and this permission notice appear in all copies. 7 + * 8 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 + */ 16 + /* Test that userspace program is not kernel thread. */ 17 + #undef NDEBUG 18 + #include <assert.h> 19 + #include <fcntl.h> 20 + #include <string.h> 21 + #include <unistd.h> 22 + 23 + int main(void) 24 + { 25 + int fd = open("/proc/self/status", O_RDONLY); 26 + assert(fd >= 0); 27 + 28 + char buf[4096]; 29 + ssize_t rv = read(fd, buf, sizeof(buf)); 30 + assert(0 <= rv && rv < sizeof(buf)); 31 + buf[rv] = '\0'; 32 + 33 + /* This test is very much not kernel thread. */ 34 + assert(strstr(buf, "Kthread:\t0\n")); 35 + 36 + return 0; 37 + }