Changes since V1:
- V1: https://lore.kernel.org/lkml/cover.1644000145.git.reinette.chatre@intel.com/
- Change solution to not use __cpuid_count() from compiler's
cpuid.h but instead use a local define of __cpuid_count()
provided in kselftest.h to ensure tests continue working
in all supported environments. (Shuah)
- Rewrite cover letter and changelogs to reflect new solution.
A few tests that require running CPUID do so with a private
implementation of a wrapper for CPUID. This duplication of
the CPUID wrapper should be avoided.
Both gcc and clang/LLVM provide wrappers for CPUID but
the wrappers are not available in the minimal required
version of gcc, v3.2, that the selftests need to be used
in. __cpuid_count() was added to gcc in v4.4, which is ok for
kernels after v4.19 when the gcc minimal required version
was changed to v4.6.
Add a local define of __cpuid_count() to kselftest.h to
ensure that selftests can still work in environments with
older stable kernels (v4.9 and v4.14 that have the minimal
required version of gcc of v3.2). Update tests with private
CPUID wrappers to use the new macro.
Cc: Dave Hansen <dave.hansen(a)linux.intel.com>
Cc: Sandipan Das <sandipan(a)linux.ibm.com>
Cc: Florian Weimer <fweimer(a)redhat.com>
Cc: "Desnes A. Nunes do Rosario" <desnesn(a)linux.vnet.ibm.com>
Cc: Ingo Molnar <mingo(a)kernel.org>
Cc: Thiago Jung Bauermann <bauerman(a)linux.ibm.com>
Cc: Michael Ellerman <mpe(a)ellerman.id.au>
Cc: Michal Suchanek <msuchanek(a)suse.de>
Cc: linux-mm(a)kvack.org
Cc: Chang S. Bae <chang.seok.bae(a)intel.com>
Cc: Borislav Petkov <bp(a)suse.de>
Cc: Thomas Gleixner <tglx(a)linutronix.de>
Cc: Ingo Molnar <mingo(a)redhat.com>
Cc: "H. Peter Anvin" <hpa(a)zytor.com>
Cc: x86(a)kernel.org
Cc: Andy Lutomirski <luto(a)kernel.org>
Reinette Chatre (4):
selftests: Provide local define of __cpuid_count()
selftests/vm/pkeys: Use provided __cpuid_count() macro
selftests/x86/amx: Use provided __cpuid_count() macro
selftests/x86/corrupt_xstate_header: Use provided __cpuid_count()
macro
tools/testing/selftests/kselftest.h | 15 ++++++++++++
tools/testing/selftests/vm/pkey-x86.h | 21 ++--------------
tools/testing/selftests/x86/amx.c | 24 ++++++-------------
.../selftests/x86/corrupt_xstate_header.c | 16 ++-----------
4 files changed, 26 insertions(+), 50 deletions(-)
base-commit: 09688c0166e76ce2fb85e86b9d99be8b0084cdf9
--
2.25.1
This patchset proposes roadtest, a device-driver testing framework. Drivers
are tested under User Mode Linux (UML) and interact with mocked/modelled
hardware. The tests and hardware models are written in Python, the former
using Python's built-in unittest framework.
Drivers are tested via their userspace interfaces. The hardware models allow
tests to inject values into registers and assert that drivers control the
hardware in the right way and react as expected to stimuli.
Roadtest is meant to be used for relatively simple drivers, such as the ones
part of the IIO, regulator and RTC subsystems.
Questions and answers:
= Why do we need this?
There are a large amount of these kind of drivers in the kernel. Most of the
hardware is not available in current CI systems so most drivers can only, at
best, be build-tested there. Even basic soundness such as a driver
successfully probing and binding to the devices it tries to be support cannot
be tested. Drivers cannot be easily regression-tested to ensure that bugs
fixed once do not get reintroduced.
Many drivers support multiple related hardware variants, and far from all patch
submitters have access to all the variants which the driver that they are
patching supports, so there is no way for them to easily verify that they
haven't broken something basic on a variant which they do not own.
Furthermore, hardware can be used in many different configurations with drivers
supporting many different devicetree properties, so even just having access to
all the variants would be insufficient.
On top of that, some of the chips measure environmental conditions such as
temperature, so testing extreme cases may not be simple even if one has access
to the hardware.
All this makes development, modification, maintenance, and reviewing of these
drivers harder than it necessarily needs to be. Roadtest hopes to make some of
these things slightly easier by providing a framework to create hardware
models/mocks and to write testcases which exercise drivers using these models.
= Do you have some specific examples of the kind of code this could be used to
test?
Here is an example of a patch which can easily be regression-tested using
roadtest (in fact, this series includes such a regression test) but is much
harder to do so automatically with real hardware since it requires specific
environmental conditions:
iio: light: opt3001: Fixed timeout error when 0 lux
https://lore.kernel.org/lkml/20210920125351.6569-1-valek@2n.cz/
Here is another example. This driver has code which correctly parses a
documented devicetree property (amstaos,proximity-diodes) but which then fails
to actually communicate this setting to the hardware in any way. Such code can
be easily tested with roadtest since the framework integrates devicetree
support and provides functions to assert that drivers writes expected registers
with expected values:
drivers/iio/light/tsl2772.c tsl2772_read_prox_diodes()
(Both the above examples happen to be from the same subsystem but that should
in no way be taken to imply that such issues are unique to that subsystem or
that that subsystem has more of them.)
= How does this relate to kselftests?
Tests in kselftests also test kernel code using the userspace interfaces, but
that's about what's common between the frameworks. kselftests has other goals
and does not provide any kind of mechanism for hardware mocking.
= How does this relate to kunit?
Kunit is for unit testing of functions in kernel code, and is not meant for
testing kernel code via userspace interfaces. It could in theory be used to
test some of the simple drivers too, but that would require (1) a large amount
of mocking code in various kernel frameworks, and, more importantly, (2)
refactoring of the drivers to be tested.
This can be contrasted with roadtest which works with mostly unmodified drivers
and which mocks the hardware at the lowest level without having to change
kernel frameworks.
= How do I use it?
See Documentation/dev-tools/roadtest.rst added by the documentation patch for
more information about running and writing tests using this framework.
= What's included in the patchset?
The current framework allows developing tests for hardware which uses the I2C
bus. Hardware models can also control GPIOs and use them to trigger
interrupts.
This series includes tests for some IIO, regulator and RTC drivers. The
regulator and RTC tests depend on a few driver patches which are either in
review or in linux-next. These are noted in the commit messages.
The entire patch set, including the required dependencies, is also available in
a git tree:
https://github.com/vwax/linux/commits/roadtest/rfc-v1
Cc: linux-kernel(a)vger.kernel.org
Cc: devicetree(a)vger.kernel.org
Cc: linux-um(a)lists.infradead.org
Cc: shuah(a)kernel.org
Cc: brendanhiggins(a)google.com
Cc: linux-kselftest(a)vger.kernel.org
Cc: jic23(a)kernel.org
Cc: linux-iio(a)vger.kernel.org
Cc: lgirdwood(a)gmail.com
Cc: broonie(a)kernel.org
Cc: a.zummo(a)towertech.it
Cc: alexandre.belloni(a)bootlin.com
Cc: linux-rtc(a)vger.kernel.org
Cc: corbet(a)lwn.net
Cc: linux-doc(a)vger.kernel.org
Vincent Whitchurch (10):
roadtest: import libvhost-user from QEMU
roadtest: add C backend
roadtest: add framework
roadtest: add base config
roadtest: add build files
roadtest: add documentation
iio: light: opt3001: add roadtest
iio: light: vcnl4000: add roadtest
regulator: tps62864: add roadtest
rtc: pcf8563: add roadtest
Documentation/dev-tools/index.rst | 1 +
Documentation/dev-tools/roadtest.rst | 669 ++++
tools/testing/roadtest/.gitignore | 2 +
tools/testing/roadtest/Dockerfile | 25 +
tools/testing/roadtest/Makefile | 84 +
tools/testing/roadtest/init.sh | 19 +
tools/testing/roadtest/pyproject.toml | 10 +
tools/testing/roadtest/requirements.txt | 4 +
tools/testing/roadtest/roadtest/__init__.py | 2 +
.../roadtest/roadtest/backend/__init__.py | 0
.../roadtest/roadtest/backend/backend.py | 32 +
.../testing/roadtest/roadtest/backend/gpio.py | 111 +
.../testing/roadtest/roadtest/backend/i2c.py | 123 +
.../testing/roadtest/roadtest/backend/main.py | 13 +
.../testing/roadtest/roadtest/backend/mock.py | 20 +
.../roadtest/roadtest/backend/test_gpio.py | 98 +
.../roadtest/roadtest/backend/test_i2c.py | 84 +
.../testing/roadtest/roadtest/cmd/__init__.py | 0
tools/testing/roadtest/roadtest/cmd/main.py | 146 +
tools/testing/roadtest/roadtest/cmd/remote.py | 48 +
.../roadtest/roadtest/core/__init__.py | 0
.../testing/roadtest/roadtest/core/control.py | 52 +
.../roadtest/roadtest/core/devicetree.py | 155 +
.../roadtest/roadtest/core/hardware.py | 94 +
tools/testing/roadtest/roadtest/core/log.py | 42 +
.../testing/roadtest/roadtest/core/modules.py | 38 +
.../testing/roadtest/roadtest/core/opslog.py | 35 +
tools/testing/roadtest/roadtest/core/proxy.py | 48 +
tools/testing/roadtest/roadtest/core/suite.py | 286 ++
tools/testing/roadtest/roadtest/core/sysfs.py | 77 +
.../roadtest/roadtest/core/test_control.py | 35 +
.../roadtest/roadtest/core/test_devicetree.py | 31 +
.../roadtest/roadtest/core/test_hardware.py | 41 +
.../roadtest/roadtest/core/test_log.py | 54 +
.../roadtest/roadtest/core/test_opslog.py | 27 +
.../roadtest/roadtest/tests/__init__.py | 0
.../roadtest/roadtest/tests/base/config | 84 +
.../roadtest/roadtest/tests/iio/__init__.py | 0
.../roadtest/roadtest/tests/iio/config | 1 +
.../roadtest/roadtest/tests/iio/iio.py | 112 +
.../roadtest/tests/iio/light/__init__.py | 0
.../roadtest/roadtest/tests/iio/light/config | 2 +
.../roadtest/tests/iio/light/test_opt3001.py | 95 +
.../roadtest/tests/iio/light/test_vcnl4000.py | 132 +
.../roadtest/tests/iio/light/test_vcnl4010.py | 282 ++
.../roadtest/tests/iio/light/test_vcnl4040.py | 104 +
.../roadtest/tests/iio/light/test_vcnl4200.py | 96 +
.../roadtest/tests/regulator/__init__.py | 0
.../roadtest/roadtest/tests/regulator/config | 4 +
.../roadtest/tests/regulator/test_tps62864.py | 187 ++
.../roadtest/roadtest/tests/rtc/__init__.py | 0
.../roadtest/roadtest/tests/rtc/config | 1 +
.../roadtest/roadtest/tests/rtc/rtc.py | 73 +
.../roadtest/tests/rtc/test_pcf8563.py | 348 ++
tools/testing/roadtest/src/.gitignore | 1 +
tools/testing/roadtest/src/backend.c | 884 +++++
.../src/libvhost-user/include/atomic.h | 310 ++
.../src/libvhost-user/libvhost-user.c | 2885 +++++++++++++++++
.../src/libvhost-user/libvhost-user.h | 691 ++++
59 files changed, 8798 insertions(+)
create mode 100644 Documentation/dev-tools/roadtest.rst
create mode 100644 tools/testing/roadtest/.gitignore
create mode 100644 tools/testing/roadtest/Dockerfile
create mode 100644 tools/testing/roadtest/Makefile
create mode 100755 tools/testing/roadtest/init.sh
create mode 100644 tools/testing/roadtest/pyproject.toml
create mode 100644 tools/testing/roadtest/requirements.txt
create mode 100644 tools/testing/roadtest/roadtest/__init__.py
create mode 100644 tools/testing/roadtest/roadtest/backend/__init__.py
create mode 100644 tools/testing/roadtest/roadtest/backend/backend.py
create mode 100644 tools/testing/roadtest/roadtest/backend/gpio.py
create mode 100644 tools/testing/roadtest/roadtest/backend/i2c.py
create mode 100644 tools/testing/roadtest/roadtest/backend/main.py
create mode 100644 tools/testing/roadtest/roadtest/backend/mock.py
create mode 100644 tools/testing/roadtest/roadtest/backend/test_gpio.py
create mode 100644 tools/testing/roadtest/roadtest/backend/test_i2c.py
create mode 100644 tools/testing/roadtest/roadtest/cmd/__init__.py
create mode 100644 tools/testing/roadtest/roadtest/cmd/main.py
create mode 100644 tools/testing/roadtest/roadtest/cmd/remote.py
create mode 100644 tools/testing/roadtest/roadtest/core/__init__.py
create mode 100644 tools/testing/roadtest/roadtest/core/control.py
create mode 100644 tools/testing/roadtest/roadtest/core/devicetree.py
create mode 100644 tools/testing/roadtest/roadtest/core/hardware.py
create mode 100644 tools/testing/roadtest/roadtest/core/log.py
create mode 100644 tools/testing/roadtest/roadtest/core/modules.py
create mode 100644 tools/testing/roadtest/roadtest/core/opslog.py
create mode 100644 tools/testing/roadtest/roadtest/core/proxy.py
create mode 100644 tools/testing/roadtest/roadtest/core/suite.py
create mode 100644 tools/testing/roadtest/roadtest/core/sysfs.py
create mode 100644 tools/testing/roadtest/roadtest/core/test_control.py
create mode 100644 tools/testing/roadtest/roadtest/core/test_devicetree.py
create mode 100644 tools/testing/roadtest/roadtest/core/test_hardware.py
create mode 100644 tools/testing/roadtest/roadtest/core/test_log.py
create mode 100644 tools/testing/roadtest/roadtest/core/test_opslog.py
create mode 100644 tools/testing/roadtest/roadtest/tests/__init__.py
create mode 100644 tools/testing/roadtest/roadtest/tests/base/config
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/__init__.py
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/config
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/iio.py
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/light/__init__.py
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/light/config
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/light/test_opt3001.py
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/light/test_vcnl4000.py
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/light/test_vcnl4010.py
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/light/test_vcnl4040.py
create mode 100644 tools/testing/roadtest/roadtest/tests/iio/light/test_vcnl4200.py
create mode 100644 tools/testing/roadtest/roadtest/tests/regulator/__init__.py
create mode 100644 tools/testing/roadtest/roadtest/tests/regulator/config
create mode 100644 tools/testing/roadtest/roadtest/tests/regulator/test_tps62864.py
create mode 100644 tools/testing/roadtest/roadtest/tests/rtc/__init__.py
create mode 100644 tools/testing/roadtest/roadtest/tests/rtc/config
create mode 100644 tools/testing/roadtest/roadtest/tests/rtc/rtc.py
create mode 100644 tools/testing/roadtest/roadtest/tests/rtc/test_pcf8563.py
create mode 100644 tools/testing/roadtest/src/.gitignore
create mode 100644 tools/testing/roadtest/src/backend.c
create mode 100644 tools/testing/roadtest/src/libvhost-user/include/atomic.h
create mode 100644 tools/testing/roadtest/src/libvhost-user/libvhost-user.c
create mode 100644 tools/testing/roadtest/src/libvhost-user/libvhost-user.h
--
2.34.1
Build of kselftests fail if kernel's top most Makefile is used for
running or building kselftests with separate output directory. The
absolute path is needed to reference other files during this kind of
build. Set KBUILD_ABS_SRCTREE to use absolute path during the build. It
fixes the following different types of errors:
make kselftest-all O=/linux_mainline/build
Makefile:1080: ../scripts/Makefile.extrawarn: No such file or directory
make kselftest-all O=build
Makefile:1080: ../scripts/Makefile.extrawarn: No such file or directory
Signed-off-by: Muhammad Usama Anjum <usama.anjum(a)collabora.com>
---
I've tested this patch on top of next-20220217. The latest next-20220222
have missing patches.
---
Makefile | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
index 86f633c2809ea..62b3eb8a102ab 100644
--- a/Makefile
+++ b/Makefile
@@ -1411,10 +1411,10 @@ tools/%: FORCE
PHONY += kselftest
kselftest:
- $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests run_tests
+ $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests KBUILD_ABS_SRCTREE=1 run_tests
kselftest-%: FORCE
- $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests $*
+ $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests KBUILD_ABS_SRCTREE=1 $*
PHONY += kselftest-merge
kselftest-merge:
--
2.30.2
The XSAVE feature set supports the saving and restoring of xstate components.
XSAVE feature has been used for process context switching. XSAVE components
include x87 state for FP execution environment, SSE state, AVX state and so on.
In order to ensure that XSAVE works correctly, add XSAVE most basic test for
XSAVE architecture functionality.
This patch tests "FP, SSE(XMM), AVX2(YMM), AVX512_OPMASK/AVX512_ZMM_Hi256/
AVX512_Hi16_ZMM and PKRU parts" xstates with following cases:
1. The content of these xstates in the process should not change after the
signal handling.
2. The content of these xstates in the child process should be the same as
the content of the parent process after the fork syscall.
Because xstate like XMM will not be preserved across function calls, fork() and
raise() are implemented and inlined.
To prevent GCC from generating any FP/SSE(XMM)/AVX/PKRU code, add
"-mno-sse -mno-mmx -mno-sse2 -mno-avx -mno-pku" compiler arguments. stdlib.h
can not be used because of the "-mno-sse" option.
Thanks Dave, Hansen for the above suggestion!
Thanks Chen Yu; Shuah Khan; Chatre Reinette and Tony Luck's comments!
Thanks to Bae, Chang Seok for a bunch of comments!
========
- Change from v7 to v8
Many thanks to Bae, Chang Seok for a bunch of comments as follow:
- Use the filling buffer way to prepare the xstate buffer, and use xrstor
instruction way to load the tested xstates.
- Remove useless dump_buffer, compare_buffer functions.
- Improve the struct of xstate_info.
- Added AVX512_ZMM_Hi256 and AVX512_Hi16_ZMM components in xstate test.
- Remove redundant xstate_info.xstate_mask, xstate_flag[], and
xfeature_test_mask, use xstate_info.mask instead.
- Check if xfeature is supported outside of fill_xstate_buf() , this change
is easier to read and understand.
- Remove useless wrpkru, only use filling all tested xstate buffer in
fill_xstates_buf().
- Improve a bunch of function names and variable names.
- Improve test steps flow for readability.
- Change from v6 to v7:
- Added the error number and error description of the reason for the
failure, thanks Shuah Khan's suggestion.
- Added a description of what these tests are doing in the head comments.
- Added changes update in the head comments.
- Added description of the purpose of the function. thanks Shuah Khan.
- Change from v5 to v6:
- In order to prevent GCC from generating any FP code by mistake,
"-mno-sse -mno-mmx -mno-sse2 -mno-avx -mno-pku" compiler parameter was
added, it's referred to the parameters for compiling the x86 kernel. Thanks
Dave Hansen's suggestion.
- Removed the use of "kselftest.h", because kselftest.h included <stdlib.h>,
and "stdlib.h" would use sse instructions in it's libc, and this *XSAVE*
test needed to be compiled without libc sse instructions(-mno-sse).
- Improved the description in commit header, thanks Chen Yu's suggestion.
- Becasue test code could not use buildin xsave64 in libc without sse, added
xsave function by instruction way.
- Every key test action would not use libc(like printf) except syscall until
it's failed or done. If it's failed, then it would print the failed reason.
- Used __cpuid_count() instead of native_cpuid(), becasue __cpuid_count()
was a macro definition function with one instruction in libc and did not
change xstate. Thanks Chatre Reinette, Shuah Khan.
https://lore.kernel.org/linux-sgx/8b7c98f4-f050-bc1c-5699-fa598ecc66a2@linu…
- Change from v4 to v5:
- Moved code files into tools/testing/selftests/x86.
- Delete xsave instruction test, becaue it's not related to kernel.
- Improved case description.
- Added AVX512 opmask change and related XSAVE content verification.
- Added PKRU part xstate test into instruction and signal handling test.
- Added XSAVE process swich test for FPU, AVX2, AVX512 opmask and PKRU part.
- Change from v3 to v4:
- Improve the comment in patch 1.
- Change from v2 to v3:
- Improve the description of patch 2 git log.
- Change from v1 to v2:
- Improve the cover-letter. Thanks Dave Hansen's suggestion.
Pengfei Xu (1):
selftests/x86/xstate: Add xstate test cases for XSAVE feature
tools/testing/selftests/x86/Makefile | 3 +-
tools/testing/selftests/x86/xstate.c | 574 +++++++++++++++++++++++++++
2 files changed, 576 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/x86/xstate.c
--
2.31.1
Add support for a new kind of kunit_suite registration macro called
kunit_test_init_suite(); this new registration macro allows the
registration of kunit_suites that reference functions marked __init and
data marked __initdata.
Signed-off-by: Brendan Higgins <brendanhiggins(a)google.com>
Tested-by: Martin Fernandez <martin.fernandez(a)eclypsium.com>
Reviewed-by: Kees Cook <keescook(a)chromium.org>
Reviewed-by: David Gow <davidgow(a)google.com>
---
This is a follow-up to the RFC here[1].
This patch is in response to a KUnit user issue[2] in which the user was
attempting to test some init functions; although this is a functional
solution as long as KUnit tests only run during the init phase, we will
need to do more work if we ever allow tests to run after the init phase
is over; it is for this reason that this patch adds a new registration
macro rather than simply modifying the existing macros.
Changes since last version:
- I added more to the kunit_test_init_suites() kernel-doc comment
detailing "how" the modpost warnings are suppressed in addition to
the existing information regarding "why" it is OK for the modpost
warnings to be suppressed.
[1] https://lore.kernel.org/linux-kselftest/20220310210210.2124637-1-brendanhig…
[2] https://groups.google.com/g/kunit-dev/c/XDjieRHEneg/m/D0rFCwVABgAJ
---
include/kunit/test.h | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/include/kunit/test.h b/include/kunit/test.h
index b26400731c02..7f303a06bc97 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -379,6 +379,32 @@ static inline int kunit_run_all_tests(void)
#define kunit_test_suite(suite) kunit_test_suites(&suite)
+/**
+ * kunit_test_init_suites() - used to register one or more &struct kunit_suite
+ * containing init functions or init data.
+ *
+ * @__suites: a statically allocated list of &struct kunit_suite.
+ *
+ * This functions identically as &kunit_test_suites() except that it suppresses
+ * modpost warnings for referencing functions marked __init or data marked
+ * __initdata; this is OK because currently KUnit only runs tests upon boot
+ * during the init phase or upon loading a module during the init phase.
+ *
+ * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
+ * tests must be excluded.
+ *
+ * The only thing this macro does that's different from kunit_test_suites is
+ * that it suffixes the array and suite declarations it makes with _probe;
+ * modpost suppresses warnings about referencing init data for symbols named in
+ * this manner.
+ */
+#define kunit_test_init_suites(__suites...) \
+ __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
+ CONCATENATE(__UNIQUE_ID(suites), _probe), \
+ ##__suites)
+
+#define kunit_test_init_suite(suite) kunit_test_init_suites(&suite)
+
#define kunit_suite_for_each_test_case(suite, test_case) \
for (test_case = suite->test_cases; test_case->run_case; test_case++)
base-commit: 330f4c53d3c2d8b11d86ec03a964b86dc81452f5
--
2.35.1.723.g4982287a31-goog
eBPF already allows programs to be preloaded and kept running without
intervention from user space. There is a dedicated kernel module called
bpf_preload, which contains the light skeleton of the iterators_bpf eBPF
program. If this module is enabled in the kernel configuration, its loading
will be triggered when the bpf filesystem is mounted (unless the module is
built-in), and the links of iterators_bpf are pinned in that filesystem
(they will appear as the progs.debug and maps.debug files).
However, the current mechanism, if used to preload an LSM, would not offer
the same security guarantees of LSMs integrated in the security subsystem.
Also, it is not generic enough to be used for preloading arbitrary eBPF
programs, unless the bpf_preload code is heavily modified.
More specifically, the security problems are:
- any program can be pinned to the bpf filesystem without limitations
(unless a MAC mechanism enforces some restrictions);
- programs being executed can be terminated at any time by deleting the
pinned objects or unmounting the bpf filesystem.
The usability problems are:
- only a fixed amount of links can be pinned;
- only links can be pinned, other object types are not supported;
- code to pin objects has to be written manually;
- preloading multiple eBPF programs is not practical, bpf_preload has to be
modified to include additional light skeletons.
Solve the security problems by mounting the bpf filesystem from the kernel,
by preloading authenticated kernel modules (e.g. with module.sig_enforce)
and by pinning objects to that filesystem. This particular filesystem
instance guarantees that desired eBPF programs run until the very end of
the kernel lifecycle, since even root cannot interfere with it.
Solve the usability problems by generalizing the pinning function, to
handle not only links but also maps and progs. Also increment the object
reference count and call the pinning function directly from the preload
method (currently in the bpf_preload kernel module) rather than from the
bpf filesystem code itself, so that a generic eBPF program can do those
operations depending on its objects (this also avoids the limitation of the
fixed-size array for storing the objects to pin).
Then, simplify the process of pinning objects defined by a generic eBPF
program by automatically generating the required methods in the light
skeleton. Also, generate a separate kernel module for each eBPF program to
preload, so that existing ones don't have to be modified. Finally, support
preloading multiple eBPF programs by allowing users to specify a list from
the kernel configuration, at build time, or with the new kernel option
bpf_preload_list=, at run-time.
To summarize, this patch set makes it possible to plug in out-of-tree LSMs
matching the security guarantees of their counterpart in the security
subsystem, without having to modify the kernel itself. The same benefits
are extended to other eBPF program types.
Only one remaining problem is how to support auto-attaching eBPF programs
with LSM type. It will be solved with a separate patch set.
Patches 1-2 export some definitions, to build out-of-tree kernel modules
with eBPF programs to preload. Patches 3-4 allow eBPF programs to pin
objects by themselves. Patches 5-10 automatically generate the methods for
preloading in the light skeleton. Patches 11-14 make it possible to preload
multiple eBPF programs. Patch 15 automatically generates the kernel module
for preloading an eBPF program, patch 16 does a kernel mount of the bpf
filesystem, and finally patches 17-18 test the functionality introduced.
Roberto Sassu (18):
bpf: Export bpf_link_inc()
bpf-preload: Move bpf_preload.h to include/linux
bpf-preload: Generalize object pinning from the kernel
bpf-preload: Export and call bpf_obj_do_pin_kernel()
bpf-preload: Generate static variables
bpf-preload: Generate free_objs_and_skel()
bpf-preload: Generate preload()
bpf-preload: Generate load_skel()
bpf-preload: Generate code to pin non-internal maps
bpf-preload: Generate bpf_preload_ops
bpf-preload: Store multiple bpf_preload_ops structures in a linked
list
bpf-preload: Implement new registration method for preloading eBPF
programs
bpf-preload: Move pinned links and maps to a dedicated directory in
bpffs
bpf-preload: Switch to new preload registration method
bpf-preload: Generate code of kernel module to preload
bpf-preload: Do kernel mount to ensure that pinned objects don't
disappear
bpf-preload/selftests: Add test for automatic generation of preload
methods
bpf-preload/selftests: Preload a test eBPF program and check pinned
objects
.../admin-guide/kernel-parameters.txt | 8 +
fs/namespace.c | 1 +
include/linux/bpf.h | 5 +
include/linux/bpf_preload.h | 37 ++
init/main.c | 2 +
kernel/bpf/inode.c | 295 +++++++++--
kernel/bpf/preload/Kconfig | 25 +-
kernel/bpf/preload/bpf_preload.h | 16 -
kernel/bpf/preload/bpf_preload_kern.c | 85 +---
kernel/bpf/preload/iterators/Makefile | 9 +-
.../bpf/preload/iterators/iterators.lskel.h | 466 +++++++++++-------
kernel/bpf/syscall.c | 1 +
.../bpf/bpftool/Documentation/bpftool-gen.rst | 13 +
tools/bpf/bpftool/bash-completion/bpftool | 6 +-
tools/bpf/bpftool/gen.c | 331 +++++++++++++
tools/bpf/bpftool/main.c | 7 +-
tools/bpf/bpftool/main.h | 1 +
tools/testing/selftests/bpf/Makefile | 32 +-
.../bpf/bpf_testmod_preload/.gitignore | 7 +
.../bpf/bpf_testmod_preload/Makefile | 20 +
.../gen_preload_methods.expected.diff | 97 ++++
.../bpf/prog_tests/test_gen_preload_methods.c | 27 +
.../bpf/prog_tests/test_preload_methods.c | 69 +++
.../selftests/bpf/progs/gen_preload_methods.c | 23 +
24 files changed, 1246 insertions(+), 337 deletions(-)
create mode 100644 include/linux/bpf_preload.h
delete mode 100644 kernel/bpf/preload/bpf_preload.h
create mode 100644 tools/testing/selftests/bpf/bpf_testmod_preload/.gitignore
create mode 100644 tools/testing/selftests/bpf/bpf_testmod_preload/Makefile
create mode 100644 tools/testing/selftests/bpf/prog_tests/gen_preload_methods.expected.diff
create mode 100644 tools/testing/selftests/bpf/prog_tests/test_gen_preload_methods.c
create mode 100644 tools/testing/selftests/bpf/prog_tests/test_preload_methods.c
create mode 100644 tools/testing/selftests/bpf/progs/gen_preload_methods.c
--
2.32.0