[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20211008235504.2957528-1-keescook@chromium.org>
Date: Fri, 8 Oct 2021 16:55:04 -0700
From: Kees Cook <keescook@...omium.org>
To: Peter Zijlstra <peterz@...radead.org>
Cc: Kees Cook <keescook@...omium.org>,
Mark Rutland <mark.rutland@....com>,
Shuah Khan <shuah@...nel.org>,
Alexey Dobriyan <adobriyan@...il.com>,
linux-kselftest@...r.kernel.org,
Josh Poimboeuf <jpoimboe@...hat.com>,
Andrew Morton <akpm@...ux-foundation.org>,
Alexey Gladkov <gladkov.alexey@...il.com>, jannh@...gle.com,
vcaputo@...garu.com, mingo@...hat.com, juri.lelli@...hat.com,
vincent.guittot@...aro.org, dietmar.eggemann@....com,
rostedt@...dmis.org, bsegall@...gle.com, mgorman@...e.de,
bristot@...hat.com, christian.brauner@...ntu.com,
amistry@...gle.com, Kenta.Tada@...y.com, legion@...nel.org,
michael.weiss@...ec.fraunhofer.de, mhocko@...e.com, deller@....de,
zhengqi.arch@...edance.com, me@...in.cc, tycho@...ho.pizza,
tglx@...utronix.de, bp@...en8.de, hpa@...or.com, axboe@...nel.dk,
metze@...ba.org, laijs@...ux.alibaba.com, luto@...nel.org,
dave.hansen@...ux.intel.com, ebiederm@...ssion.com,
ohoono.kwon@...sung.com, kaleshsingh@...gle.com,
yifeifz2@...inois.edu, linux-arch@...r.kernel.org,
vgupta@...nel.org, linux@...linux.org.uk, will@...nel.org,
guoren@...nel.org, bcain@...eaurora.org, monstr@...str.eu,
tsbogend@...ha.franken.de, nickhu@...estech.com,
jonas@...thpole.se, mpe@...erman.id.au, paul.walmsley@...ive.com,
hca@...ux.ibm.com, ysato@...rs.sourceforge.jp, davem@...emloft.net,
chris@...kel.net, linux-kernel@...r.kernel.org,
linux-fsdevel@...r.kernel.org, linux-hardening@...r.kernel.org
Subject: [PATCH] selftests: proc: Make sure wchan works when it exists
This makes sure that wchan contains a sensible symbol when a process is
blocked. Specifically this calls the sleep() syscall, and expects the
architecture to have called schedule() from a function that has "sleep"
somewhere in its name. For example, on the architectures I tested
(x86_64, arm64, arm, mips, and powerpc) this is "hrtimer_nanosleep":
$ tools/testing/selftests/proc/proc-pid-wchan
ok: found 'sleep' in wchan 'hrtimer_nanosleep'
Cc: Peter Zijlstra <peterz@...radead.org>
Cc: Mark Rutland <mark.rutland@....com>
Cc: Shuah Khan <shuah@...nel.org>
Cc: Alexey Dobriyan <adobriyan@...il.com>
Cc: linux-kselftest@...r.kernel.org
Signed-off-by: Kees Cook <keescook@...omium.org>
---
Hi Peter,
Can you add this to the wchan series, please? This should help wchan from
regressing in the future, and allow us to notice if the depth accidentally
changes, like Mark saw.
---
tools/testing/selftests/proc/Makefile | 1 +
tools/testing/selftests/proc/proc-pid-wchan.c | 69 +++++++++++++++++++
2 files changed, 70 insertions(+)
create mode 100644 tools/testing/selftests/proc/proc-pid-wchan.c
diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile
index 1054e40a499a..45cf35703ece 100644
--- a/tools/testing/selftests/proc/Makefile
+++ b/tools/testing/selftests/proc/Makefile
@@ -8,6 +8,7 @@ TEST_GEN_PROGS += fd-002-posix-eq
TEST_GEN_PROGS += fd-003-kthread
TEST_GEN_PROGS += proc-loadavg-001
TEST_GEN_PROGS += proc-pid-vm
+TEST_GEN_PROGS += proc-pid-wchan
TEST_GEN_PROGS += proc-self-map-files-001
TEST_GEN_PROGS += proc-self-map-files-002
TEST_GEN_PROGS += proc-self-syscall
diff --git a/tools/testing/selftests/proc/proc-pid-wchan.c b/tools/testing/selftests/proc/proc-pid-wchan.c
new file mode 100644
index 000000000000..7d7870c31cef
--- /dev/null
+++ b/tools/testing/selftests/proc/proc-pid-wchan.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Make sure that wchan returns a reasonable symbol when blocked.
+ */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define perror_exit(str) do { perror(str); _exit(1); } while (0)
+
+int main(void)
+{
+ char buf[64];
+ pid_t child;
+ int sync[2], fd;
+
+ if (pipe(sync) < 0)
+ perror_exit("pipe");
+
+ child = fork();
+ if (child < 0)
+ perror_exit("fork");
+ if (child == 0) {
+ /* Child */
+ if (close(sync[0]) < 0)
+ perror_exit("child close sync[0]");
+ if (close(sync[1]) < 0)
+ perror_exit("child close sync[1]");
+ sleep(10);
+ _exit(0);
+ }
+ /* Parent */
+ if (close(sync[1]) < 0)
+ perror_exit("parent close sync[1]");
+ if (read(sync[0], buf, 1) != 0)
+ perror_exit("parent read sync[0]");
+
+ snprintf(buf, sizeof(buf), "/proc/%d/wchan", child);
+ fd = open(buf, O_RDONLY);
+ if (fd < 0) {
+ if (errno == ENOENT)
+ return 4;
+ perror_exit(buf);
+ }
+
+ memset(buf, 0, sizeof(buf));
+ if (read(fd, buf, sizeof(buf) - 1) < 1)
+ perror_exit(buf);
+ if (strstr(buf, "sleep") == NULL) {
+ fprintf(stderr, "FAIL: did not find 'sleep' in wchan '%s'\n", buf);
+ return 1;
+ }
+ printf("ok: found 'sleep' in wchan '%s'\n", buf);
+
+ if (kill(child, SIGKILL) < 0)
+ perror_exit("kill");
+ if (waitpid(child, NULL, 0) != child) {
+ fprintf(stderr, "waitpid: got the wrong child!?\n");
+ return 1;
+ }
+
+ return 0;
+}
--
2.30.2
Powered by blists - more mailing lists