Skip to contentSkip to conversation

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

PATCH New Michael Moese 2 participants 4 messages Jan 23, 2018 – Jan 25, 2018 Library code

lore
Share & tools
lore
#0 lore raw

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

Someteimes, it is important to detect if the kernel has issued a warning, or died, or is tainted in another way. Linux provides this information in /proc/sys/kernel/tainted in the form of a bitfield. From 2.6.26 on, Linux supports detection of kernel warnings or even if the kernel had died.
This patch provides library functions for testcases to detect, if it has triggered a bug in the kernel.

The following functions will be introduced:

- int tst_taint_supported(void)
are TAINT_W and TAINT_D available on your kernel? Determined simply using the running kernel version

- int tst_taint_check_mask(unsigned int mask) check if one or more of the bits specified in mask are set in /sys/proc/kernel/tainted

- unsigned int tst_taint_read(void)
unfiltered access to /sys/proc/kernel/tainted, for advanced use

These can be used in the following way:

First, check support state during setup():

void setup(void)

{
	...
	if (!tst_taint_supported())
		tst_brk(TCONF, "Kernel does not support tainted flags");
}

Second, check if the test triggered a bug:

void run(void)

{
	if (tst_taint_read() != 0)
		tst_res(TFAIL, "kernel is already tainted, stopping");

	...
	if(tst_taint_check(TST_TAINT_W | TST_TAINT_D))
		tst_res(TFAIL, "kernel has issues");
	 else
		tst_res(TPASS, "kernel seems to be fine");
}
---
 include/tst_taint.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/tst_taint.c     |  52 ++++++++++++++++++++++++++
 2 files changed, 156 insertions(+)
 create mode 100644 include/tst_taint.h
 create mode 100644 lib/tst_taint.c
include/tst_taint.h new file +104 -0
new file mode 100644
index 000000000..34e69c2f5
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2018 Michael Moese <[email protected]>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Usage example
+ *
+ * ...
+ * #include "tst_test.h"
+ * #include "tst_taint.h"
+ * ..
+ * void setup(void)
+ * {
+ *	if (!tst_taint_supported())
+ *		tst_brk(TCONF, "Kernel does not support tainted flags");
+ * }
+ *
+ * void run(void)
+ * {
+ *	...
+ *	. test code here
+ *	...
+ *	if(tst_taint_check(TST_TAINT_W | TST_TAINT_D))
+ *		tst_res(TFAIL, "kernel has issues");
+ *	else
+ *		tst_res(TPASS, "kernel seems to be fine");
+ * }
+ *
+ *
+ *
+ * The above code checks, if the kernel issued a warning (TST_TAINT_W)
+ * or even died (TST_TAINT_D). If these are set durin execution of a
+ * test case, we most likely triggered a kernel bug.
+ */
+
+#ifndef TST_TAINTED_H__
+#define TST_TAINTED_H__
+
+/*
+ * This are all 17 flags that are present in kernel 4.15
+ * see kernel/panic.c in kernel sources
+ *
+ * Not all of them are valid in all kernel versions.
+ */
+#define TST_TAINT_G     (1 <<  0) /* a module with non-GPL license loaded */
+#define TST_TAINT_F     (1 <<  1) /* a module was force-loaded */
+#define TST_TAINT_S     (1 <<  2) /* SMP with Non-SMP kernel */
+#define TST_TAINT_R     (1 <<  3) /* module force unloaded */
+#define TST_TAINT_M     (1 <<  4) /* machine check error occurred */
+#define TST_TAINT_B     (1 <<  5) /* page-release function found bad page */
+#define TST_TAINT_U     (1 <<  6) /* user requested taint flag */
+#define TST_TAINT_D     (1 <<  7) /* kernel died recently - OOPS or BUG */
+#define TST_TAINT_A     (1 <<  8) /* ACPI table has been overwritten */
+#define TST_TAINT_W     (1 <<  9) /* a warning has been issued by kernel */
+#define TST_TAINT_C     (1 << 10) /* driver from drivers/staging was loaded */
+#define TST_TAINT_I     (1 << 11) /* working around BIOS/Firmware bug */
+#define TST_TAINT_O     (1 << 12) /* out of tree module loaded */
+#define TST_TAINT_E     (1 << 13) /* unsigned module was loaded */
+#define TST_TAINT_L     (1 << 14) /* A soft lock-up has previously occurred */
+#define TST_TAINT_K     (1 << 15) /* kernel has been live-patched */
+#define TST_TAINT_X	(1 << 16) /* auxiliary taint, for distro's use */
+
+/*
+ * check if kernel is recent enough to support the tainted-flags that
+ * allow us to check if the kernel issued a warning or died.
+ * This _must_ be called before calling other functions!
+ *
+ * returns 1 if supported, 0 otherwise
+ */
+int tst_taint_supported(void);
+
+
+/*
+ * read /proc/sys/kernel/tainted and check if@least one of the
+ * bits specified in mask is set.
+ * mask is required to be > 0. Use tst_taint_read() in order to check
+ * for absence of any tainted flags.
+ *
+ * returns 1 if at least one bit of mask is set in
+ * /proc/sys/kernel/tainted, 0 otherwise.
+ * (unsigned int) -1 is returned if mask is zero or tst_taint_supported
+ * was not called before
+ */
+int tst_taint_check_mask(unsigned int mask);
+
+
+/*
+ * read /proc/sys/kernel/tainted and return the value
+ */
+unsigned int tst_taint_read(void);
+
+#endif /* TST_TAINTED_H__ */
lib/tst_taint.c new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.13.6
#1 lore raw

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

Hi!

Quoted 29 lines
 include/tst_taint.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/tst_taint.c     |  52 ++++++++++++++++++++++++++
 2 files changed, 156 insertions(+)
 create mode 100644 include/tst_taint.h
 create mode 100644 lib/tst_taint.c
include/tst_taint.h new file +17 -0
new file mode 100644
index 000000000..34e69c2f5
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2018 Michael Moese <[email protected]>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+

include/tst_taint.h line 27

Quoted 10 lines
include/tst_taint.h new file +10 -0
+/* Usage example
+ *
+ * ...
+ * #include "tst_test.h"
+ * #include "tst_taint.h"
+ * ..
+ * void setup(void)
+ * {
+ *	if (!tst_taint_supported())
+ *		tst_brk(TCONF, "Kernel does not support tainted flags");

I think that we should check here if the kernel was tainted before the test here and issue TBROK.

And also I would like to have the API tailored for the most common case as well. So what about something as adding tst_check_taint(unsigned mask) to be called from the test setup that would:

* check if kernel is new enough, otherwise do TCONF

* check if all flags in the mask are not set, otherwise do TBROK

Quoted 88 lines
include/tst_taint.h new file +77 -0
+ * }
+ *
+ * void run(void)
+ * {
+ *	...
+ *	. test code here
+ *	...
+ *	if(tst_taint_check(TST_TAINT_W | TST_TAINT_D))
+ *		tst_res(TFAIL, "kernel has issues");
+ *	else
+ *		tst_res(TPASS, "kernel seems to be fine");
+ * }
+ *
+ *
+ *
+ * The above code checks, if the kernel issued a warning (TST_TAINT_W)
+ * or even died (TST_TAINT_D). If these are set durin execution of a
+ * test case, we most likely triggered a kernel bug.
+ */
+
+#ifndef TST_TAINTED_H__
+#define TST_TAINTED_H__
+
+/*
+ * This are all 17 flags that are present in kernel 4.15
+ * see kernel/panic.c in kernel sources
+ *
+ * Not all of them are valid in all kernel versions.
+ */
+#define TST_TAINT_G     (1 <<  0) /* a module with non-GPL license loaded */
+#define TST_TAINT_F     (1 <<  1) /* a module was force-loaded */
+#define TST_TAINT_S     (1 <<  2) /* SMP with Non-SMP kernel */
+#define TST_TAINT_R     (1 <<  3) /* module force unloaded */
+#define TST_TAINT_M     (1 <<  4) /* machine check error occurred */
+#define TST_TAINT_B     (1 <<  5) /* page-release function found bad page */
+#define TST_TAINT_U     (1 <<  6) /* user requested taint flag */
+#define TST_TAINT_D     (1 <<  7) /* kernel died recently - OOPS or BUG */
+#define TST_TAINT_A     (1 <<  8) /* ACPI table has been overwritten */
+#define TST_TAINT_W     (1 <<  9) /* a warning has been issued by kernel */
+#define TST_TAINT_C     (1 << 10) /* driver from drivers/staging was loaded */
+#define TST_TAINT_I     (1 << 11) /* working around BIOS/Firmware bug */
+#define TST_TAINT_O     (1 << 12) /* out of tree module loaded */
+#define TST_TAINT_E     (1 << 13) /* unsigned module was loaded */
+#define TST_TAINT_L     (1 << 14) /* A soft lock-up has previously occurred */
+#define TST_TAINT_K     (1 << 15) /* kernel has been live-patched */
+#define TST_TAINT_X	(1 << 16) /* auxiliary taint, for distro's use */
+
+/*
+ * check if kernel is recent enough to support the tainted-flags that
+ * allow us to check if the kernel issued a warning or died.
+ * This _must_ be called before calling other functions!
+ *
+ * returns 1 if supported, 0 otherwise
+ */
+int tst_taint_supported(void);
+
+
+/*
+ * read /proc/sys/kernel/tainted and check if at least one of the
+ * bits specified in mask is set.
+ * mask is required to be > 0. Use tst_taint_read() in order to check
+ * for absence of any tainted flags.
+ *
+ * returns 1 if at least one bit of mask is set in
+ * /proc/sys/kernel/tainted, 0 otherwise.
+ * (unsigned int) -1 is returned if mask is zero or tst_taint_supported
+ * was not called before
+ */
+int tst_taint_check_mask(unsigned int mask);
+
+
+/*
+ * read /proc/sys/kernel/tainted and return the value
+ */
+unsigned int tst_taint_read(void);
+
+#endif /* TST_TAINTED_H__ */
lib/tst_taint.c new file +5 -0
new file mode 100644
index 000000000..de9093976
@@ -0,0 +1,52 @@
+#define TST_NO_DEFAULT_MAIN
+
+#include "tst_test.h"
+#include "tst_taint.h"
+#include "tst_safe_stdio.h"

lib/tst_taint.c line 15

Quoted 10 lines
lib/tst_taint.c new file +10 -0
+
+#define TAINT_FILE "/proc/sys/kernel/tainted"
+
+static int taint_supported = -1;
+
+int tst_taint_supported(void)
+{
+	if (tst_kvercmp(2, 6, 26) < 0) {
+		tst_res(TINFO, "Kernel version prior to 2.6.26 detected.\n");
+		tst_res(TINFO, "please check kernel output manually\n");

Actually we skip the test if kernel is too old, so the second tst_res() message does to make much sense.

lib/tst_taint.c line 28

Quoted 13 lines
lib/tst_taint.c new file +13 -0
+		taint_supported = 0;
+	} else
+		taint_supported = 1;
+
+	return taint_supported;
+}
+
+unsigned int tst_taint_read(void)
+{
+	unsigned int val;
+
+	if (taint_supported == -1) {
+		tst_brk(TCONF, "call tst_taint_supported() before tst_taint_read()");
                         ^
			 This is definitelly TBROK == test broken, since
			 we are calling functions out of order.

lib/tst_taint.c line 29

Quoted 1 line
lib/tst_taint.c new file +1 -0
+		return (unsigned int) -1;

Actually we will never got here, so we may as well remove the return here.

lib/tst_taint.c line 42

Quoted 13 lines
lib/tst_taint.c new file +13 -0
+	}
+	SAFE_FILE_SCANF(TAINT_FILE, "%u", &val);
+
+	return val;
+}
+
+int tst_taint_check_mask(unsigned int mask)
+{
+	unsigned int taint;
+
+	taint = tst_taint_read();
+	if (taint == (unsigned int) -1)
+		return -1;

Here as well no need to check the return value against -1 since tst_taint_read() will exit anyways.

lib/tst_taint.c line 47

Quoted 4 lines
lib/tst_taint.c new file +4 -0
+	if (mask == 0) {
+		tst_brk(TCONF, "mask is not allowed to be 0");
+		return  -1;
+	}

This should be done before we attempt to read the the tainted file, also no need for the return, tst_brk() will exit the test.

Quoted 11 lines
lib/tst_taint.c new file +5 -0
+	if ((taint & mask) != 0)
+		return 1;
+
+	return 0;
+}
Signature · 5 lines
2.13.6


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
#2 lore raw

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

Hi,
Thanks for all the remarks. I won't comment on those where I totally agree.

Quoted 12 lines · Cyril Hrubis
Quoted 8 lines

+ * ...
+ * #include "tst_test.h"
+ * #include "tst_taint.h"
+ * ..
+ * void setup(void)
+ * {
+ * if (!tst_taint_supported())
+ * tst_brk(TCONF, "Kernel does not support tainted flags");

I think that we should check here if the kernel was tainted before the test here and issue TBROK.

Quoted 7 lines

And also I would like to have the API tailored for the most common case as well. So what about something as adding tst_check_taint(unsigned mask) to be called from the test setup that would:

* check if kernel is new enough, otherwise do TCONF

* check if all flags in the mask are not set, otherwise do TBROK

This one kept me busy thinking about it yesterday evening. I wonder if we could reduce it to two functions:
tst_taint_init(unsigned mask)
and
tst_taint_check()

The first one saves the desired flags, checks for support in kernel etc. and the latter just checks if the tainted flags remain the same.

After thinking it through, I can hardly imagine a different use case.

Thanks,
Michael

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

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

Hi!

Quoted 10 lines

This one kept me busy thinking about it yesterday evening. I wonder if we could reduce it to two functions:
tst_taint_init(unsigned mask)
and
tst_taint_check()

The first one saves the desired flags, checks for support in kernel etc. and the latter just checks if the tainted flags remain the same.

After thinking it through, I can hardly imagine a different use case.

Agreed, we also have the luxury of having all the code that uses the library in one repository. Hence we do not have to carry on with API mistakes for years as libc and other core library have to...

Signature · 2 lines
Cyril Hrubis
[email protected]