Skip to content

Commit 71ae5fc

Browse files
committed
Merge tag 'linux-kselftest-5.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull Kselftest updates from Shuah Khan: - fixes to seccomp test, and kselftest framework - cleanups to remove duplicate header defines - fixes to efivarfs "make clean" target - cgroup cleanup path - Moving the IMA kexec_load selftest to selftests/kexec work from Mimi Johar and Petr Vorel - A framework to kselftest for writing kernel test modules addition from Tobin C. Harding * tag 'linux-kselftest-5.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: (29 commits) selftests: build and run gpio when output directory is the src dir selftests/ipc: Fix msgque compiler warnings selftests/efivarfs: clean up test files from test_create*() selftests: fix headers_install circular dependency selftests/kexec: update get_secureboot_mode selftests/kexec: make kexec_load test independent of IMA being enabled selftests/kexec: check kexec_load and kexec_file_load are enabled selftests/kexec: Add missing '=y' to config options selftests/kexec: kexec_file_load syscall test selftests/kexec: define "require_root_privileges" selftests/kexec: define common logging functions selftests/kexec: define a set of common functions selftests/kexec: cleanup the kexec selftest selftests/kexec: move the IMA kexec_load selftest to selftests/kexec selftests/harness: Add 30 second timeout per test selftests/seccomp: Handle namespace failures gracefully selftests: cgroup: fix cleanup path in test_memcg_subtree_control() selftests: efivarfs: remove the test_create_read file if it was exist rseq/selftests: Adapt number of threads to the number of detected cpus lib: Add test module for strscpy_pad ...
2 parents 81ff5d2 + d917fb8 commit 71ae5fc

35 files changed

+1081
-223
lines changed

Documentation/dev-tools/kselftest.rst

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ in safe mode with a limited scope. In limited mode, cpu-hotplug test is
1414
run on a single cpu as opposed to all hotplug capable cpus, and memory
1515
hotplug test is run on 2% of hotplug capable memory instead of 10%.
1616

17+
kselftest runs as a userspace process. Tests that can be written/run in
18+
userspace may wish to use the `Test Harness`_. Tests that need to be
19+
run in kernel space may wish to use a `Test Module`_.
20+
1721
Running the selftests (hotplug tests are run in limited mode)
1822
=============================================================
1923

@@ -161,11 +165,97 @@ Contributing new tests (details)
161165

162166
e.g: tools/testing/selftests/android/config
163167

168+
Test Module
169+
===========
170+
171+
Kselftest tests the kernel from userspace. Sometimes things need
172+
testing from within the kernel, one method of doing this is to create a
173+
test module. We can tie the module into the kselftest framework by
174+
using a shell script test runner. ``kselftest_module.sh`` is designed
175+
to facilitate this process. There is also a header file provided to
176+
assist writing kernel modules that are for use with kselftest:
177+
178+
- ``tools/testing/kselftest/kselftest_module.h``
179+
- ``tools/testing/kselftest/kselftest_module.sh``
180+
181+
How to use
182+
----------
183+
184+
Here we show the typical steps to create a test module and tie it into
185+
kselftest. We use kselftests for lib/ as an example.
186+
187+
1. Create the test module
188+
189+
2. Create the test script that will run (load/unload) the module
190+
e.g. ``tools/testing/selftests/lib/printf.sh``
191+
192+
3. Add line to config file e.g. ``tools/testing/selftests/lib/config``
193+
194+
4. Add test script to makefile e.g. ``tools/testing/selftests/lib/Makefile``
195+
196+
5. Verify it works:
197+
198+
.. code-block:: sh
199+
200+
# Assumes you have booted a fresh build of this kernel tree
201+
cd /path/to/linux/tree
202+
make kselftest-merge
203+
make modules
204+
sudo make modules_install
205+
make TARGETS=lib kselftest
206+
207+
Example Module
208+
--------------
209+
210+
A bare bones test module might look like this:
211+
212+
.. code-block:: c
213+
214+
// SPDX-License-Identifier: GPL-2.0+
215+
216+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
217+
218+
#include "../tools/testing/selftests/kselftest_module.h"
219+
220+
KSTM_MODULE_GLOBALS();
221+
222+
/*
223+
* Kernel module for testing the foobinator
224+
*/
225+
226+
static int __init test_function()
227+
{
228+
...
229+
}
230+
231+
static void __init selftest(void)
232+
{
233+
KSTM_CHECK_ZERO(do_test_case("", 0));
234+
}
235+
236+
KSTM_MODULE_LOADERS(test_foo);
237+
MODULE_AUTHOR("John Developer <[email protected]>");
238+
MODULE_LICENSE("GPL");
239+
240+
Example test script
241+
-------------------
242+
243+
.. code-block:: sh
244+
245+
#!/bin/bash
246+
# SPDX-License-Identifier: GPL-2.0+
247+
$(dirname $0)/../kselftest_module.sh "foo" test_foo
248+
249+
164250
Test Harness
165251
============
166252

167-
The kselftest_harness.h file contains useful helpers to build tests. The tests
168-
from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as example.
253+
The kselftest_harness.h file contains useful helpers to build tests. The
254+
test harness is for userspace testing, for kernel space testing see `Test
255+
Module`_ above.
256+
257+
The tests from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as
258+
example.
169259

170260
Example
171261
-------

include/linux/string.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ size_t strlcpy(char *, const char *, size_t);
3131
#ifndef __HAVE_ARCH_STRSCPY
3232
ssize_t strscpy(char *, const char *, size_t);
3333
#endif
34+
35+
/* Wraps calls to strscpy()/memset(), no arch specific code required */
36+
ssize_t strscpy_pad(char *dest, const char *src, size_t count);
37+
3438
#ifndef __HAVE_ARCH_STRCAT
3539
extern char * strcat(char *, const char *);
3640
#endif

lib/Kconfig.debug

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,9 @@ config TEST_HEXDUMP
17691769
config TEST_STRING_HELPERS
17701770
tristate "Test functions located in the string_helpers module at runtime"
17711771

1772+
config TEST_STRSCPY
1773+
tristate "Test strscpy*() family of functions at runtime"
1774+
17721775
config TEST_KSTRTOX
17731776
tristate "Test kstrto*() family of functions at runtime"
17741777

lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
8181
obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
8282
obj-$(CONFIG_TEST_PRINTF) += test_printf.o
8383
obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
84+
obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o
8485
obj-$(CONFIG_TEST_BITFIELD) += test_bitfield.o
8586
obj-$(CONFIG_TEST_UUID) += test_uuid.o
8687
obj-$(CONFIG_TEST_XARRAY) += test_xarray.o

lib/string.c

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,9 @@ EXPORT_SYMBOL(strlcpy);
159159
* @src: Where to copy the string from
160160
* @count: Size of destination buffer
161161
*
162-
* Copy the string, or as much of it as fits, into the dest buffer.
163-
* The routine returns the number of characters copied (not including
164-
* the trailing NUL) or -E2BIG if the destination buffer wasn't big enough.
165-
* The behavior is undefined if the string buffers overlap.
166-
* The destination buffer is always NUL terminated, unless it's zero-sized.
162+
* Copy the string, or as much of it as fits, into the dest buffer. The
163+
* behavior is undefined if the string buffers overlap. The destination
164+
* buffer is always NUL terminated, unless it's zero-sized.
167165
*
168166
* Preferred to strlcpy() since the API doesn't require reading memory
169167
* from the src string beyond the specified "count" bytes, and since
@@ -173,8 +171,10 @@ EXPORT_SYMBOL(strlcpy);
173171
*
174172
* Preferred to strncpy() since it always returns a valid string, and
175173
* doesn't unnecessarily force the tail of the destination buffer to be
176-
* zeroed. If the zeroing is desired, it's likely cleaner to use strscpy()
177-
* with an overflow test, then just memset() the tail of the dest buffer.
174+
* zeroed. If zeroing is desired please use strscpy_pad().
175+
*
176+
* Return: The number of characters copied (not including the trailing
177+
* %NUL) or -E2BIG if the destination buffer wasn't big enough.
178178
*/
179179
ssize_t strscpy(char *dest, const char *src, size_t count)
180180
{
@@ -237,6 +237,39 @@ ssize_t strscpy(char *dest, const char *src, size_t count)
237237
EXPORT_SYMBOL(strscpy);
238238
#endif
239239

240+
/**
241+
* strscpy_pad() - Copy a C-string into a sized buffer
242+
* @dest: Where to copy the string to
243+
* @src: Where to copy the string from
244+
* @count: Size of destination buffer
245+
*
246+
* Copy the string, or as much of it as fits, into the dest buffer. The
247+
* behavior is undefined if the string buffers overlap. The destination
248+
* buffer is always %NUL terminated, unless it's zero-sized.
249+
*
250+
* If the source string is shorter than the destination buffer, zeros
251+
* the tail of the destination buffer.
252+
*
253+
* For full explanation of why you may want to consider using the
254+
* 'strscpy' functions please see the function docstring for strscpy().
255+
*
256+
* Return: The number of characters copied (not including the trailing
257+
* %NUL) or -E2BIG if the destination buffer wasn't big enough.
258+
*/
259+
ssize_t strscpy_pad(char *dest, const char *src, size_t count)
260+
{
261+
ssize_t written;
262+
263+
written = strscpy(dest, src, count);
264+
if (written < 0 || written == count - 1)
265+
return written;
266+
267+
memset(dest + written + 1, 0, count - written - 1);
268+
269+
return written;
270+
}
271+
EXPORT_SYMBOL(strscpy_pad);
272+
240273
#ifndef __HAVE_ARCH_STRCAT
241274
/**
242275
* strcat - Append one %NUL-terminated string to another

lib/test_bitmap.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <linux/slab.h>
1313
#include <linux/string.h>
1414

15+
#include "../tools/testing/selftests/kselftest_module.h"
16+
1517
static unsigned total_tests __initdata;
1618
static unsigned failed_tests __initdata;
1719

@@ -361,30 +363,16 @@ static void noinline __init test_mem_optimisations(void)
361363
}
362364
}
363365

364-
static int __init test_bitmap_init(void)
366+
static void __init selftest(void)
365367
{
366368
test_zero_clear();
367369
test_fill_set();
368370
test_copy();
369371
test_bitmap_arr32();
370372
test_bitmap_parselist();
371373
test_mem_optimisations();
372-
373-
if (failed_tests == 0)
374-
pr_info("all %u tests passed\n", total_tests);
375-
else
376-
pr_warn("failed %u out of %u tests\n",
377-
failed_tests, total_tests);
378-
379-
return failed_tests ? -EINVAL : 0;
380374
}
381375

382-
static void __exit test_bitmap_cleanup(void)
383-
{
384-
}
385-
386-
module_init(test_bitmap_init);
387-
module_exit(test_bitmap_cleanup);
388-
376+
KSTM_MODULE_LOADERS(test_bitmap);
389377
MODULE_AUTHOR("david decotigny <[email protected]>");
390378
MODULE_LICENSE("GPL");

lib/test_printf.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <linux/gfp.h>
2222
#include <linux/mm.h>
2323

24+
#include "../tools/testing/selftests/kselftest_module.h"
25+
2426
#define BUF_SIZE 256
2527
#define PAD_SIZE 16
2628
#define FILL_CHAR '$'
@@ -590,12 +592,11 @@ test_pointer(void)
590592
flags();
591593
}
592594

593-
static int __init
594-
test_printf_init(void)
595+
static void __init selftest(void)
595596
{
596597
alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
597598
if (!alloced_buffer)
598-
return -ENOMEM;
599+
return;
599600
test_buffer = alloced_buffer + PAD_SIZE;
600601

601602
test_basic();
@@ -604,16 +605,8 @@ test_printf_init(void)
604605
test_pointer();
605606

606607
kfree(alloced_buffer);
607-
608-
if (failed_tests == 0)
609-
pr_info("all %u tests passed\n", total_tests);
610-
else
611-
pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
612-
613-
return failed_tests ? -EINVAL : 0;
614608
}
615609

616-
module_init(test_printf_init);
617-
610+
KSTM_MODULE_LOADERS(test_printf);
618611
MODULE_AUTHOR("Rasmus Villemoes <[email protected]>");
619612
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)