[PATCH] selftests: Add a taint selftest

From: Andrew Delgadilo
Date: Tue Aug 23 2022 - 17:20:10 EST


From: Andrew Delgadillo <adelg@xxxxxxxxxx>

When testing a kernel, one of the earliest signals one can get is if a
kernel has become tainted. For example, an organization might be
interested in mass testing commits on their hardware. An obvious first
step would be to make sure every commit boots, and a next step would be
to make sure there are no warnings/crashes/lockups, hence the utility of
a taint test.

Signed-off-by: Andrew Delgadillo <adelg@xxxxxxxxxx>
---
tools/testing/selftests/core/Makefile | 1 +
tools/testing/selftests/core/taint.sh | 28 +++++++++++++++++++++++++++
2 files changed, 29 insertions(+)
create mode 100755 tools/testing/selftests/core/taint.sh

diff --git a/tools/testing/selftests/core/Makefile b/tools/testing/selftests/core/Makefile
index f6f2d6f473c6a..695bdbfb02f90 100644
--- a/tools/testing/selftests/core/Makefile
+++ b/tools/testing/selftests/core/Makefile
@@ -2,6 +2,7 @@
CFLAGS += -g -I../../../../usr/include/

TEST_GEN_PROGS := close_range_test
+TEST_PROGS := taint.sh

include ../lib.mk

diff --git a/tools/testing/selftests/core/taint.sh b/tools/testing/selftests/core/taint.sh
new file mode 100755
index 0000000000000..661c2cb8cd9bf
--- /dev/null
+++ b/tools/testing/selftests/core/taint.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+set -oue pipefail
+
+# By default, we only want to check if our system has:
+# - seen an oops or bug
+# - a warning occurred
+# - a lockup occurred
+# The bit values for these, and more, can be found at
+# Documentation/admin-guide/tainted-kernels.html
+# This value can be overridden by passing a mask as the
+# first positional argument.
+taint_bitmask=$(( 128 + 512 + 16384 ))
+
+# If we have a positional argument, then override our
+# default bitmask.
+if [[ -n "${1-}" ]]; then
+ taint_bitmask=$1
+fi
+
+taint_bits=$(cat /proc/sys/kernel/tainted)
+
+result=$(( taint_bitmask & taint_bits ))
+if [[ "$result" -ne 0 ]]; then
+ exit 1
+fi
+
+exit 0
--
2.37.1.595.g718a3a8f04-goog