Skip to contentSkip to conversation

lib: Add library function for parsing kernel config

PATCH Superseded Cyril Hrubis 2 participants 5 messages Nov 27, 2018 – Jan 23, 2019 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lib: Add library function for parsing kernel config

This is meant as last resort action for disabling tests if certain kernel funcitonality was not present, in general case runtime checks are prefered.

For functionality that can be build as a module tst_check_driver() is most likely better fit since it will also insert requested kernel module into kernel if needed.

For newly added syscalls kernel version comparsion and/or checking errno is prefered.

However in rare cases certain core kernel functionality cannot be detected in any other way than checking the kernel config, which is where this API gets into the play.

The path to the kernel config could be specified by KCONFIG_PATH environment variable, which also takes precedence before the autodetection that attempts to read the config from known locations.

The required kernel options are passed as an array of strings via the .needs_kconfigs pointer in the tst_test structure. The purpose of this is twofold, one is that the test can disable itself at runtime if given functionality is missing from kernel .config and second is about being able to propagate this information to the testrunner (this could be done once we figure out how export the information from the structure to the test runner) then we can avoid running tests on unsuitable configurations from the start.

There are two different ways how to specify the required kernel options either by stating the config name i.e. "CONFIG_FOO" which is considered present in kernel if set to a certain value and "CONFIG_FOO=bar" which requires the config option to be strictly set to a desired value.

The code has been also split into parsing phase that simply picks up the config option values from the config and to evaluation which does the actual checks and aborts the test if needed. This makes it easier to expand the domain language for specifying the required options, if we ever need to match against subset of values this should be reasonably easy to implement.

---
 doc/test-writing-guidelines.txt  |  35 ++++
 include/tst_kconfig.h            |  57 +++++++
 include/tst_test.h               |   6 +
 lib/newlib_tests/.gitignore      |   1 +
 lib/newlib_tests/config01        |   4 +
 lib/newlib_tests/config02        |   4 +
 lib/newlib_tests/config03        |   4 +
 lib/newlib_tests/config04        |   4 +
 lib/newlib_tests/config05        |   3 +
 lib/newlib_tests/test_kconfig.c  |  23 +++
 lib/newlib_tests/test_kconfig.sh |  11 ++
 lib/tst_kconfig.c                | 284 +++++++++++++++++++++++++++++++
 lib/tst_test.c                   |   4 +
 13 files changed, 440 insertions(+)
 create mode 100644 include/tst_kconfig.h
 create mode 100644 lib/newlib_tests/config01
 create mode 100644 lib/newlib_tests/config02
 create mode 100644 lib/newlib_tests/config03
 create mode 100644 lib/newlib_tests/config04
 create mode 100644 lib/newlib_tests/config05
 create mode 100644 lib/newlib_tests/test_kconfig.c
 create mode 100755 lib/newlib_tests/test_kconfig.sh
 create mode 100644 lib/tst_kconfig.c
doc/test-writing-guidelines.txt +35 -0
index d0b91c362..681f650b9 100644
@@ -1504,6 +1504,41 @@ static struct tst_test test = {
 	.save_restore = save_restore,
 };
 
+2.2.28 Parsing kernel .config
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Generally testcases should attempt to autodetect as much kernel features as
+possible based on the currently running kernel. We do have tst_check_driver()
+to check if functionality that could be compiled as kernel module is present
+on the system, disabled syscalls can be detected by checking for 'ENOSYS'
+errno etc.
+
+However in rare cases core kernel features couldn't be detected based on the
+kernel userspace API and we have to resort on kernel .config parsing.
+
+For this cases the test should set the 'NULL' terminated needs_kconfig array
+of kernel config options required for the test. The config option can be
+specified either as plain "CONFIG_FOO" in which case it's sufficient for the
+test continue if it's set to any value (typically =y or =m). Or with a value
+as "CONFIG_FOO=bar" in which case the value has to match as well. The test is
+aborted with 'TCONF' if any of the required options wasn't set.
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+
+static const char *kconfigs[] = {
+	"CONFIG_X86_INTEL_UMIP",
+	NULL
+};
+
+static struct tst_test test = {
+	...
+	.needs_kconfigs = kconfigs,
+	...
+};
+-------------------------------------------------------------------------------
+
 
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/tst_kconfig.h new file +57 -0

57 lines not on this page · raw message

include/tst_test.h +6 -0
index 2ebf746eb..f21687c06 100644
@@ -182,6 +182,12 @@ struct tst_test {
 	 * before setup and restore after cleanup
 	 */
 	const char * const *save_restore;
+
+	/*
+	 * NULL terminated array of kernel config options required for the
+	 * test.
+	 */
+	const char *const *needs_kconfigs;
 };
 
 /*
lib/newlib_tests/.gitignore +1 -0
index c702644f0..645ba349e 100644
@@ -24,3 +24,4 @@ test19
 tst_expiration_timer
 test_exec
 test_exec_child
+test_kconfig
lib/newlib_tests/config01 new file +4 -0
new file mode 100644
index 000000000..96d68d836
@@ -0,0 +1,4 @@
+# Test should PASS in this case
+CONFIG_MMU=y
+CONFIG_EXT4_FS=m
+CONFIG_PGTABLE_LEVELS=4
lib/newlib_tests/config02 new file +4 -0
new file mode 100644
index 000000000..2de45cff8
@@ -0,0 +1,4 @@
+# Unset CONFIG_MMU
+# CONFIG_MMU is not set
+CONFIG_EXT4_FS=m
+CONFIG_PGTABLE_LEVELS=4
lib/newlib_tests/config03 new file +4 -0
new file mode 100644
index 000000000..1a3b9e648
@@ -0,0 +1,4 @@
+# Wrong number of page table levels
+CONFIG_MMU=y
+CONFIG_EXT4_FS=m
+CONFIG_PGTABLE_LEVELS=44
lib/newlib_tests/config04 new file +4 -0
new file mode 100644
index 000000000..cce7051ae
@@ -0,0 +1,4 @@
+# Unexpected CONFIG_EXT4_FS compiled in
+CONFIG_MMU=y
+CONFIG_EXT4_FS=y
+CONFIG_PGTABLE_LEVELS=4
lib/newlib_tests/config05 new file +3 -0
new file mode 100644
index 000000000..a9d7bab4d
@@ -0,0 +1,3 @@
+# Everything is wrong
+CONFIG_EXT4_FS=y
+CONFIG_PGTABLE_LEVELS=44
lib/newlib_tests/test_kconfig.c new file +23 -0
new file mode 100644
index 000000000..d9c662fc5
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2018 Cyril Hrubis <[email protected]>
+ */
+
+#include "tst_test.h"
+#include "tst_kconfig.h"
+
+static void do_test(void)
+{
+	tst_res(TPASS, "Test passed!");
+}
+
+static const char *kconfigs[] = {
+	"CONFIG_MMU",
+	"CONFIG_EXT4_FS=m",
+	"CONFIG_PGTABLE_LEVELS=4",
+	NULL
+};
+
+static struct tst_test test = {
+	.test_all = do_test,
+	.needs_kconfigs = kconfigs,
+};
lib/newlib_tests/test_kconfig.sh new file +11 -0
new file mode 100755
index 000000000..4b83c3542
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+echo "Attempting to parse currently running kernel config"
+./test_kconfig
+echo
+
+for i in config0*; do
+	head -n 1 "$i"
+	KCONFIG_PATH="$i" ./test_kconfig
+	echo
+done
lib/tst_kconfig.c new file +284 -0

284 lines not on this page · raw message

lib/tst_test.c +4 -0
index 661fbbfce..da3e0c8a0 100644
@@ -36,6 +36,7 @@
 #include "tst_clocks.h"
 #include "tst_timer.h"
 #include "tst_sys_conf.h"
+#include "tst_kconfig.h"
 
 #include "old_resource.h"
 #include "old_device.h"
@@ -770,6 +771,9 @@ static void do_setup(int argc, char *argv[])
 	if (tst_test->tconf_msg)
 		tst_brk(TCONF, "%s", tst_test->tconf_msg);
 
+	if (tst_test->needs_kconfigs)
+		tst_kconfig_check(tst_test->needs_kconfigs);
+
 	assert_test_fn();
 
 	tid = get_tid(argv);
Signature · 1 line
2.18.1
#1 lore raw

PATCH v2 1/1 lib: Add library function for parsing kernel config

Hi Cyril,

Nice patch.

lib/tst_kconfig.c line 33

Quoted 2 lines
lib/tst_kconfig.c new file +2 -0
+	/* Debian and derivatives */
+	snprintf(path_buf, path_buf_len, "/boot/config-%s", un.release);

/boot/config-$(uname -r) use also non Debian (and it's derivates) distros :).

Kind regards,
Petr

#2 lore raw

PATCH v2 1/1 lib: Add library function for parsing kernel config

In reply to Cyril Hrubis

Hi Cyril,

lib/newlib_tests/test_kconfig.sh line 3

Quoted 5 lines

+++ b/lib/newlib_tests/test_kconfig.sh

lib/newlib_tests/test_kconfig.sh new file +3 -0
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+echo "Attempting to parse currently running kernel config"

Could you please add cd into test directory to save people to cd into directory? cd $(dirname $0)

This might not be needed once I'll finish [1] my "make: Add test target" patch. (wanted to finish unit testing shell library [2][3] before it).

Review line 5

Quoted 2 lines
(quoted diff) +2 -0
+./test_kconfig
+echo

Kind regards,
Petr

[1] https://patchwork.ozlabs.org/patch/960363/
[2] https://patchwork.ozlabs.org/patch/964552/
[3] https://github.com/linux-test-project/ltp/issues/312