Skip to content

Commit 85a7c65

Browse files
mdouchapevik
authored andcommitted
Integrate tst_taint_check() into main LTP library
Add .taint_check attribute to struct tst_test and use it to initialize taint checking functions. Then call tst_taint_check() automatically at the end of testing if needed. Reviewed-by: Petr Vorel <[email protected]> Signed-off-by: Martin Doucha <[email protected]>
1 parent c24c540 commit 85a7c65

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

doc/test-writing-guidelines.txt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,38 +1535,41 @@ test.c:8: INFO: do_action(arg) failed
15351535
2.2.24 Tainted kernels
15361536
^^^^^^^^^^^^^^^^^^^^^^
15371537

1538-
If you need to detect, if a testcase triggers a kernel warning, bug or oops,
1539-
the following can be used to detect TAINT_W or TAINT_D:
1538+
If you need to detect whether a testcase triggers a kernel warning, bug or
1539+
oops, the following can be used to detect TAINT_W or TAINT_D:
15401540

15411541
[source,c]
15421542
-------------------------------------------------------------------------------
15431543
#include "tst_test.h"
1544-
#include "tst_taint.h"
15451544

1546-
void setup(void)
1547-
{
1545+
static struct tst_test test = {
15481546
...
1549-
tst_taint_init(TST_TAINT_W | TST_TAINT_D);
1547+
.taint_check = TST_TAINT_W | TST_TAINT_D,
15501548
...
1551-
}
1552-
...
1549+
};
1550+
15531551
void run(void)
15541552
{
15551553
...
1556-
if (tst_taint_check() == 0)
1557-
tst_res(TPASS, "kernel is not tainted");
1554+
if (tst_taint_check() != 0)
1555+
tst_res(TFAIL, "kernel has issues");
15581556
else
1559-
tst_res(TFAIL, "kernel is tainted");
1557+
tst_res(TPASS, "kernel seems to be fine");
15601558
}
15611559
-------------------------------------------------------------------------------
15621560

1563-
You have to call 'tst_taint_init()' with non-zero flags first, preferably during
1564-
setup(). The function will generate a 'TCONF' if the requested flags are not
1565-
fully supported on the running kernel, and 'TBROK' if either a zero mask was
1566-
supplied or if the kernel is already tainted before executing the test.
1561+
To initialize taint checks, you have to set the taint flags you want to test
1562+
for in the 'taint_check' attribute of the tst_test struct. LTP library will
1563+
then automatically call 'tst_taint_init()' during test setup. The function
1564+
will generate a 'TCONF' if the requested flags are not fully supported on the
1565+
running kernel, and 'TBROK' if the kernel is already tainted before executing
1566+
the test.
15671567

1568-
Then you can call 'tst_taint_check()' during 'run()', which returns 0 or the
1569-
tainted flags set in '/proc/sys/kernel/tainted' as specified earlier.
1568+
LTP library will then automatically check kernel taint at the end of testing.
1569+
If '.all_filesystems' is set in struct tst_test, taint check will be performed
1570+
after each file system and testing may be aborted early by 'TBROK'. You can
1571+
optionally also call 'tst_taint_check()' during 'run()', which returns 0 or
1572+
the tainted flags set in '/proc/sys/kernel/tainted' as specified earlier.
15701573

15711574
Depending on your kernel version, not all tainted-flags will be supported.
15721575

include/tst_taint.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
*
88
* ...
99
* #include "tst_test.h"
10-
* #include "tst_taint.h"
1110
* ..
12-
* void setup(void)
13-
* {
11+
* static struct tst_test test = {
1412
* ...
15-
* tst_taint_init(TST_TAINT_W | TST_TAINT_D));
13+
* .taint_check = TST_TAINT_W | TST_TAINT_D,
1614
* ...
17-
* }
15+
* };
1816
*
1917
* void run(void)
2018
* {
@@ -29,10 +27,14 @@
2927
*
3028
*
3129
*
32-
* The above code checks, if the kernel issued a warning (TST_TAINT_W)
30+
* The above code checks whether the kernel issued a warning (TST_TAINT_W)
3331
* or even died (TST_TAINT_D) during test execution.
3432
* If these are set after running a test case, we most likely
3533
* triggered a kernel bug.
34+
*
35+
* You do not need to use tst_taint_check() explicitly because it'll be called
36+
* automatically at the end of testing by the LTP library if
37+
* tst_test.taint_check in non-zero.
3638
*/
3739

3840
#ifndef TST_TAINTED_H__
@@ -64,15 +66,18 @@
6466
#define TST_TAINT_T (1 << 17) /* kernel was built with the struct randomization plugin */
6567

6668
/*
67-
* Initialize and prepare support for checking tainted kernel.
69+
* Initialize and prepare support for checking tainted kernel. Called
70+
* automatically by LTP library during test setup if tst_test.taint_check
71+
* is non-zero. The value of tst_test.taint_check will be passed as the mask
72+
* argument.
6873
*
6974
* supply the mask of TAINT-flags you want to check, for example
7075
* (TST_TAINT_W | TST_TAINT_D) when you want to check if the kernel issued
7176
* a warning or even reported it died.
7277
*
7378
* This function tests if the requested flags are supported on the
7479
* locally running kernel. In case the tainted-flags are already set by
75-
* the kernel, there is no reason to continue and TCONF is generated.
80+
* the kernel, there is no reason to continue and TBROK is generated.
7681
*
7782
* The mask must not be zero.
7883
*/

include/tst_test.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "tst_assert.h"
4242
#include "tst_cgroup.h"
4343
#include "tst_lockdown.h"
44+
#include "tst_taint.h"
4445

4546
/*
4647
* Reports testcase result.
@@ -168,6 +169,14 @@ struct tst_test {
168169
*/
169170
unsigned long request_hugepages;
170171

172+
/*
173+
* If set to non-zero, call tst_taint_init(taint_check) during setup
174+
* and check kernel taint at the end of the test. If all_filesystems
175+
* is non-zero, taint check will be performed after each FS test and
176+
* testing will be terminated by TBROK if taint is detected.
177+
*/
178+
unsigned int taint_check;
179+
171180
/*
172181
* If set non-zero denotes number of test variant, the test is executed
173182
* variants times each time with tst_variant set to different number.

lib/tst_test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,9 @@ static void do_setup(int argc, char *argv[])
10011001

10021002
if (tst_test->restore_wallclock)
10031003
tst_wallclock_save();
1004+
1005+
if (tst_test->taint_check)
1006+
tst_taint_init(tst_test->taint_check);
10041007
}
10051008

10061009
static void do_test_setup(void)
@@ -1279,6 +1282,9 @@ static int fork_testrun(void)
12791282
alarm(0);
12801283
SAFE_SIGNAL(SIGINT, SIG_DFL);
12811284

1285+
if (tst_test->taint_check && tst_taint_check())
1286+
tst_brk(TBROK, "Kernel is now tainted.");
1287+
12821288
if (WIFEXITED(status) && WEXITSTATUS(status))
12831289
return WEXITSTATUS(status);
12841290

0 commit comments

Comments
 (0)