Skip to content

Commit

Permalink
Merge tag 'linux_kselftest-kunit-6.13-rc1-fixed' of git://git.kernel.…
Browse files Browse the repository at this point in the history
…org/pub/scm/linux/kernel/git/shuah/linux-kselftest

Pull kunit updates from Shuah Khan:

 - fix user-after-free (UAF) bug in kunit_init_suite()

 - add option to kunit tool to print just the summary of test results

 - add option to kunit tool to print just the failed test results

 - fix kunit_zalloc_skb() to use user passed in gfp value instead of
   hardcoding GFP_KERNEL

 - fixe kunit_zalloc_skb() kernel doc to include allocation flags
   variable

 - update KUnit email address for Brendan Higgins

 - add LoongArch config to qemu_configs

 - allow overriding the shutdown mode from qemu config

 - enable shutdown in loongarch qemu_config

 - fix potential null dereference in kunit_device_driver_test()

 - fix debugfs to use IS_ERR() for alloc_string_stream() error check

* tag 'linux_kselftest-kunit-6.13-rc1-fixed' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest:
  kunit: qemu_configs: loongarch: Enable shutdown
  kunit: tool: Allow overriding the shutdown mode from qemu config
  kunit: qemu_configs: Add LoongArch config
  kunit: debugfs: Use IS_ERR() for alloc_string_stream() error check
  kunit: Fix potential null dereference in kunit_device_driver_test()
  MAINTAINERS: Update KUnit email address for Brendan Higgins
  kunit: string-stream: Fix a UAF bug in kunit_init_suite()
  kunit: tool: print failed tests only
  kunit: tool: Only print the summary
  kunit: skb: add gfp to kernel doc for kunit_zalloc_skb()
  kunit: skb: use "gfp" variable instead of hardcoding GFP_KERNEL
  • Loading branch information
torvalds committed Nov 23, 2024
2 parents 06afb0f + 62adcae commit e288c35
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 91 deletions.
2 changes: 1 addition & 1 deletion MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -12473,7 +12473,7 @@ F: fs/smb/common/
F: fs/smb/server/

KERNEL UNIT TESTING FRAMEWORK (KUnit)
M: Brendan Higgins <brendanhiggins@google.com>
M: Brendan Higgins <brendan.higgins@linux.dev>
M: David Gow <davidgow@google.com>
R: Rae Moar <rmoar@google.com>
L: linux-kselftest@vger.kernel.org
Expand Down
5 changes: 3 additions & 2 deletions include/kunit/skbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ static void kunit_action_kfree_skb(void *p)
* kunit_zalloc_skb() - Allocate and initialize a resource managed skb.
* @test: The test case to which the skb belongs
* @len: size to allocate
* @gfp: allocation flags
*
* Allocate a new struct sk_buff with GFP_KERNEL, zero fill the give length
* Allocate a new struct sk_buff with gfp flags, zero fill the given length
* and add it as a resource to the kunit test for automatic cleanup.
*
* Returns: newly allocated SKB, or %NULL on error
*/
static inline struct sk_buff *kunit_zalloc_skb(struct kunit *test, int len,
gfp_t gfp)
{
struct sk_buff *res = alloc_skb(len, GFP_KERNEL);
struct sk_buff *res = alloc_skb(len, gfp);

if (!res || skb_pad(res, len))
return NULL;
Expand Down
9 changes: 6 additions & 3 deletions lib/kunit/debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ void kunit_debugfs_create_suite(struct kunit_suite *suite)
* successfully.
*/
stream = alloc_string_stream(GFP_KERNEL);
if (IS_ERR_OR_NULL(stream))
if (IS_ERR(stream))
return;

string_stream_set_append_newlines(stream, true);
suite->log = stream;

kunit_suite_for_each_test_case(suite, test_case) {
stream = alloc_string_stream(GFP_KERNEL);
if (IS_ERR_OR_NULL(stream))
if (IS_ERR(stream))
goto err;

string_stream_set_append_newlines(stream, true);
Expand All @@ -212,8 +212,11 @@ void kunit_debugfs_create_suite(struct kunit_suite *suite)

err:
string_stream_destroy(suite->log);
kunit_suite_for_each_test_case(suite, test_case)
suite->log = NULL;
kunit_suite_for_each_test_case(suite, test_case) {
string_stream_destroy(test_case->log);
test_case->log = NULL;
}
}

void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
Expand Down
2 changes: 2 additions & 0 deletions lib/kunit/kunit-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,8 @@ static void kunit_device_driver_test(struct kunit *test)
struct device *test_device;
struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);

KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_state);

test->priv = test_state;
test_driver = kunit_driver_create(test, "my_driver");

Expand Down
28 changes: 25 additions & 3 deletions tools/testing/kunit/kunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import kunit_json
import kunit_kernel
import kunit_parser
from kunit_printer import stdout
from kunit_printer import stdout, null_printer

class KunitStatus(Enum):
SUCCESS = auto()
Expand All @@ -49,6 +49,8 @@ class KunitBuildRequest(KunitConfigRequest):
class KunitParseRequest:
raw_output: Optional[str]
json: Optional[str]
summary: bool
failed: bool

@dataclass
class KunitExecRequest(KunitParseRequest):
Expand Down Expand Up @@ -235,11 +237,18 @@ def parse_tests(request: KunitParseRequest, metadata: kunit_json.Metadata, input
parse_time = time.time() - parse_start
return KunitResult(KunitStatus.SUCCESS, parse_time), fake_test

default_printer = stdout
if request.summary or request.failed:
default_printer = null_printer

# Actually parse the test results.
test = kunit_parser.parse_run_tests(input_data)
test = kunit_parser.parse_run_tests(input_data, default_printer)
parse_time = time.time() - parse_start

if request.failed:
kunit_parser.print_test(test, request.failed, stdout)
kunit_parser.print_summary_line(test, stdout)

if request.json:
json_str = kunit_json.get_json_result(
test=test,
Expand Down Expand Up @@ -413,6 +422,14 @@ def add_parse_opts(parser: argparse.ArgumentParser) -> None:
help='Prints parsed test results as JSON to stdout or a file if '
'a filename is specified. Does nothing if --raw_output is set.',
type=str, const='stdout', default=None, metavar='FILE')
parser.add_argument('--summary',
help='Prints only the summary line for parsed test results.'
'Does nothing if --raw_output is set.',
action='store_true')
parser.add_argument('--failed',
help='Prints only the failed parsed test results and summary line.'
'Does nothing if --raw_output is set.',
action='store_true')


def tree_from_args(cli_args: argparse.Namespace) -> kunit_kernel.LinuxSourceTree:
Expand Down Expand Up @@ -448,6 +465,8 @@ def run_handler(cli_args: argparse.Namespace) -> None:
jobs=cli_args.jobs,
raw_output=cli_args.raw_output,
json=cli_args.json,
summary=cli_args.summary,
failed=cli_args.failed,
timeout=cli_args.timeout,
filter_glob=cli_args.filter_glob,
filter=cli_args.filter,
Expand Down Expand Up @@ -495,6 +514,8 @@ def exec_handler(cli_args: argparse.Namespace) -> None:
exec_request = KunitExecRequest(raw_output=cli_args.raw_output,
build_dir=cli_args.build_dir,
json=cli_args.json,
summary=cli_args.summary,
failed=cli_args.failed,
timeout=cli_args.timeout,
filter_glob=cli_args.filter_glob,
filter=cli_args.filter,
Expand All @@ -520,7 +541,8 @@ def parse_handler(cli_args: argparse.Namespace) -> None:
# We know nothing about how the result was created!
metadata = kunit_json.Metadata()
request = KunitParseRequest(raw_output=cli_args.raw_output,
json=cli_args.json)
json=cli_args.json, summary=cli_args.summary,
failed=cli_args.failed)
result, _ = parse_tests(request, metadata, kunit_output)
if result.status != KunitStatus.SUCCESS:
sys.exit(1)
Expand Down
4 changes: 3 additions & 1 deletion tools/testing/kunit/kunit_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ def __init__(self, qemu_arch_params: qemu_config.QemuArchParams, cross_compile:
self._kconfig = qemu_arch_params.kconfig
self._qemu_arch = qemu_arch_params.qemu_arch
self._kernel_path = qemu_arch_params.kernel_path
self._kernel_command_line = qemu_arch_params.kernel_command_line + ' kunit_shutdown=reboot'
self._kernel_command_line = qemu_arch_params.kernel_command_line
if 'kunit_shutdown=' not in self._kernel_command_line:
self._kernel_command_line += ' kunit_shutdown=reboot'
self._extra_qemu_params = qemu_arch_params.extra_qemu_params
self._serial = qemu_arch_params.serial

Expand Down
Loading

0 comments on commit e288c35

Please sign in to comment.