Skip to contentSkip to conversation

Add library support for /proc/sys/kernel/tainted

PATCH Superseded Michael Moese 2 participants 3 messages Jan 25, 2018 – Jan 26, 2018 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Add library support for /proc/sys/kernel/tainted

Someteimes, it is important to detect if the kernel has issued a warning, died, or is tainted in another way. Linux provides this information in /proc/sys/kernel/tainted in the form of a bitfield. This patch provides library functions for testcases to detect, if it has tainted the kernel.

The following functions will be introduced:

- int tst_taint_init(unsigned int mask)
check if the flags supplied as mask are supported by the running kernel, and if so, if they are not yet set.

- int tst_taint_check()
check if one or more of the bits specified in the mask provided to tst_taint_init() before are set.
Returns 0 if those flags are not set, or the bitmask of set flags

These can be used in the following way:

First, during testcase setup:

void setup(void)

{
	...
	tst_taint_init(TST_TAINT_W | TST_TAINT_D);
}

Second, check if the test triggered a bug:

void run(void)

{
	...
	. test code here
	...
	if (tst_taint_check() != 0)
		tst_res(TFAIL, "kernel has issues");
	 else
		tst_res(TPASS, "kernel seems to be fine");
}
---
 doc/test-writing-guidelines.txt |  44 +++++++++++++++++
 include/tst_taint.h             | 104 +++++++++++++++++++++++++++++++++++++++
 lib/tst_taint.c                 | 106 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 254 insertions(+)
 create mode 100644 include/tst_taint.h
 create mode 100644 lib/tst_taint.c
doc/test-writing-guidelines.txt +44 -0
index 739b295b8..bfd0d9892 100644
@@ -1312,6 +1312,50 @@ common.h:9: FAIL: check failed
 test.c:8: INFO: do_action(arg) failed
 -------------------------------------------------------------------------------
 
+2.2.24 Tainted kernels
+^^^^^^^^^^^^^^^^^^^^^^
+
+If you need to detect, if the testcase triggers the kernel to be tained,
+just do the following to detect TAINT_W or TAINT_D:
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+#include "tst_taint.h"
+
+void setup(void)
+{
+	...
+	tst_taint_init(TST_TAINT_W | TST_TAINT_D);
+	...
+}
+...
+void run(void)
+{
+	...
+	if (tst_taint_check() == 0)
+		tst_res(TPASS, "kernel is not tainted");
+	else
+		tst_res(TFAIL, "kernel is tainted");
+}
+-------------------------------------------------------------------------------
+
+You have to call tst_taint_init() with non-zero flags first, preferably during
+setup(). The function will generate a TCONF if the requested flags are not
+fully supported on the running kernel, and TBROK if either a zero mask was
+supplied or if the kernel is already tainted before executing the test.
+
+You then can call tst_taint_check() during run(), which generates TBROK if
+you try to call it before tst_taint_init(). Otherwise, 0 is returned if the
+kernel is not tainted with the mask supplied earlier, otherwise the taint
+flags are set in the return value.
+
+Depending on your kernel version, not all tainted-flags will be supported.
+
+For reference to tainted kernels, see kernel documentation:
+Documentation/admin-guide/tainted-kernels.rst or
+https://www.kernel.org/doc/html/latest/admin-guide/tainted-kernels.html
+
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
include/tst_taint.h new file +104 -0

104 lines not on this page · raw message

lib/tst_taint.c new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.13.6
#1 lore raw

PATCH v3 1/1 Add library support for /proc/sys/kernel/tainted

Hi!

Quoted 1 line

Someteimes, it is important to detect if the kernel has issued a

^
Typo.

Quoted 53 lines

warning, died, or is tainted in another way. Linux provides this information in /proc/sys/kernel/tainted in the form of a bitfield. This patch provides library functions for testcases to detect, if it has tainted the kernel.

The following functions will be introduced:

- int tst_taint_init(unsigned int mask)
check if the flags supplied as mask are supported by the running kernel, and if so, if they are not yet set.

- int tst_taint_check()
check if one or more of the bits specified in the mask provided to tst_taint_init() before are set.
Returns 0 if those flags are not set, or the bitmask of set flags

These can be used in the following way:

First, during testcase setup:

void setup(void)

{
	...
	tst_taint_init(TST_TAINT_W | TST_TAINT_D);
}

Second, check if the test triggered a bug:

void run(void)

{
	...
	. test code here
	...
	if (tst_taint_check() != 0)
		tst_res(TFAIL, "kernel has issues");
	 else
		tst_res(TPASS, "kernel seems to be fine");
}
---
 doc/test-writing-guidelines.txt |  44 +++++++++++++++++
 include/tst_taint.h             | 104 +++++++++++++++++++++++++++++++++++++++
 lib/tst_taint.c                 | 106 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 254 insertions(+)
 create mode 100644 include/tst_taint.h
 create mode 100644 lib/tst_taint.c
doc/test-writing-guidelines.txt +0 -0
index 739b295b8..bfd0d9892 100644

doc/test-writing-guidelines.txt common.h:9: FAIL: check failed · line 1318

Quoted 7 lines
doc/test-writing-guidelines.txt +4 -0
@@ -1312,6 +1312,50 @@ common.h:9: FAIL: check failed
 test.c:8: INFO: do_action(arg) failed
 -------------------------------------------------------------------------------
 
+2.2.24 Tainted kernels
+^^^^^^^^^^^^^^^^^^^^^^
+
+If you need to detect, if the testcase triggers the kernel to be tained,

^ We should probably say here something as:

"triggers kernel warning, bug or oops"

Since tainted kernel can mean much more than bug.

Quoted 20 lines
doc/test-writing-guidelines.txt +20 -0
+just do the following to detect TAINT_W or TAINT_D:
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+#include "tst_taint.h"
+
+void setup(void)
+{
+	...
+	tst_taint_init(TST_TAINT_W | TST_TAINT_D);
+	...
+}
+...
+void run(void)
+{
+	...
+	if (tst_taint_check() == 0)
+		tst_res(TPASS, "kernel is not tainted");
+	else

doc/test-writing-guidelines.txt common.h:9: FAIL: check failed · line 1348

Quoted 10 lines
doc/test-writing-guidelines.txt +10 -0
+		tst_res(TFAIL, "kernel is tainted");
+}
+-------------------------------------------------------------------------------
+
+You have to call tst_taint_init() with non-zero flags first, preferably during
+setup(). The function will generate a TCONF if the requested flags are not
+fully supported on the running kernel, and TBROK if either a zero mask was
+supplied or if the kernel is already tainted before executing the test.
+
+You then can call tst_taint_check() during run(), which generates TBROK if

^
Then you can

Quoted 1 line

you try to call it before tst_taint_init().

The second part of the sentence only describes safety measure, I wouldn't have included it here at all, it only confuses the reader.

doc/test-writing-guidelines.txt common.h:9: FAIL: check failed · line 1351

Quoted 3 lines

Oherwise, 0 is returned if the

doc/test-writing-guidelines.txt +2 -0
+kernel is not tainted with the mask supplied earlier, otherwise the taint
+flags are set in the return value.

Also let's drop the Otherwise here it's making the whole sentece a bit confusing as well.

I can update the documentation and fix the typos upon pushing the patch, if you do not mind.

Also I would like to push this along with actual test that uses the interface.

Signature · 2 lines
Cyril Hrubis
[email protected]
#2 lore raw

PATCH v3 1/1 Add library support for /proc/sys/kernel/tainted

Quoted 4 lines · Cyril Hrubis

Hi!

Also I would like to push this along with actual test that uses the interface.

I wanted to avoid sending the testcase with this patch all the time. The test is almost ready, I'll do some more testin and cleanups, so I think I can submit it later.

I can as well rework your remarks and send API plus testcase together.

THanks,
Michael

Signature · 1 line
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)