Skip to content

Commit a185a09

Browse files
committed
Merge tag 'linux-kselftest-kunit-6.1-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull more KUnit updates from Shuah Khan: "Features and fixes: - simplify resource use - make kunit_malloc() and kunit_free() allocations and frees consistent. kunit_free() frees only the memory allocated by kunit_malloc() - stop downloading risc-v opensbi binaries using wget - other fixes and improvements to tool and KUnit framework" * tag 'linux-kselftest-kunit-6.1-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: Documentation: kunit: Update description of --alltests option kunit: declare kunit_assert structs as const kunit: rename base KUNIT_ASSERTION macro to _KUNIT_FAILED kunit: remove format func from struct kunit_assert, get it to 0 bytes kunit: tool: Don't download risc-v opensbi firmware with wget kunit: make kunit_kfree(NULL) a no-op to match kfree() kunit: make kunit_kfree() not segfault on invalid inputs kunit: make kunit_kfree() only work on pointers from kunit_malloc() and friends kunit: drop test pointer in string_stream_fragment kunit: string-stream: Simplify resource use
2 parents 661e009 + e98c4f6 commit a185a09

File tree

9 files changed

+131
-206
lines changed

9 files changed

+131
-206
lines changed

Documentation/dev-tools/kunit/run_wrapper.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,15 @@ command line arguments:
251251
compiling a kernel (using ``build`` or ``run`` commands). For example:
252252
to enable compiler warnings, we can pass ``--make_options W=1``.
253253

254-
- ``--alltests``: Builds a UML kernel with all config options enabled
255-
using ``make allyesconfig``. This allows us to run as many tests as
256-
possible.
257-
258-
.. note:: It is slow and prone to breakage as new options are
259-
added or modified. Instead, enable all tests
260-
which have satisfied dependencies by adding
261-
``CONFIG_KUNIT_ALL_TESTS=y`` to your ``.kunitconfig``.
254+
- ``--alltests``: Enable a predefined set of options in order to build
255+
as many tests as possible.
256+
257+
.. note:: The list of enabled options can be found in
258+
``tools/testing/kunit/configs/all_tests.config``.
259+
260+
If you only want to enable all tests with otherwise satisfied
261+
dependencies, instead add ``CONFIG_KUNIT_ALL_TESTS=y`` to your
262+
``.kunitconfig``.
262263

263264
- ``--kunitconfig``: Specifies the path or the directory of the ``.kunitconfig``
264265
file. For example:

include/kunit/assert.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@ struct kunit_loc {
4242

4343
/**
4444
* struct kunit_assert - Data for printing a failed assertion or expectation.
45-
* @format: a function which formats the data in this kunit_assert to a string.
4645
*
4746
* Represents a failed expectation/assertion. Contains all the data necessary to
4847
* format a string to a user reporting the failure.
4948
*/
50-
struct kunit_assert {
51-
void (*format)(const struct kunit_assert *assert,
52-
const struct va_format *message,
53-
struct string_stream *stream);
54-
};
49+
struct kunit_assert {};
50+
51+
typedef void (*assert_format_t)(const struct kunit_assert *assert,
52+
const struct va_format *message,
53+
struct string_stream *stream);
5554

5655
void kunit_assert_prologue(const struct kunit_loc *loc,
5756
enum kunit_assert_type type,
@@ -71,16 +70,6 @@ void kunit_fail_assert_format(const struct kunit_assert *assert,
7170
const struct va_format *message,
7271
struct string_stream *stream);
7372

74-
/**
75-
* KUNIT_INIT_FAIL_ASSERT_STRUCT - Initializer for &struct kunit_fail_assert.
76-
*
77-
* Initializes a &struct kunit_fail_assert. Intended to be used in
78-
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
79-
*/
80-
#define KUNIT_INIT_FAIL_ASSERT_STRUCT { \
81-
.assert = { .format = kunit_fail_assert_format }, \
82-
}
83-
8473
/**
8574
* struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
8675
* @assert: The parent of this type.
@@ -110,7 +99,6 @@ void kunit_unary_assert_format(const struct kunit_assert *assert,
11099
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
111100
*/
112101
#define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \
113-
.assert = { .format = kunit_unary_assert_format }, \
114102
.condition = cond, \
115103
.expected_true = expect_true \
116104
}
@@ -145,7 +133,6 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
145133
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
146134
*/
147135
#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \
148-
.assert = { .format = kunit_ptr_not_err_assert_format }, \
149136
.text = txt, \
150137
.value = val \
151138
}
@@ -190,7 +177,6 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
190177
* KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like
191178
* kunit_binary_assert, kunit_binary_ptr_assert, etc.
192179
*
193-
* @format_func: a function which formats the assert to a string.
194180
* @text_: Pointer to a kunit_binary_assert_text.
195181
* @left_val: The actual evaluated value of the expression in the left slot.
196182
* @right_val: The actual evaluated value of the expression in the right slot.
@@ -200,11 +186,9 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
200186
* fields but with different types for left_val/right_val.
201187
* This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ, etc.
202188
*/
203-
#define KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
204-
text_, \
189+
#define KUNIT_INIT_BINARY_ASSERT_STRUCT(text_, \
205190
left_val, \
206191
right_val) { \
207-
.assert = { .format = format_func }, \
208192
.text = text_, \
209193
.left_value = left_val, \
210194
.right_value = right_val \

include/kunit/resource.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -300,22 +300,6 @@ typedef bool (*kunit_resource_match_t)(struct kunit *test,
300300
struct kunit_resource *res,
301301
void *match_data);
302302

303-
/**
304-
* kunit_resource_instance_match() - Match a resource with the same instance.
305-
* @test: Test case to which the resource belongs.
306-
* @res: The resource.
307-
* @match_data: The resource pointer to match against.
308-
*
309-
* An instance of kunit_resource_match_t that matches a resource whose
310-
* allocation matches @match_data.
311-
*/
312-
static inline bool kunit_resource_instance_match(struct kunit *test,
313-
struct kunit_resource *res,
314-
void *match_data)
315-
{
316-
return res->data == match_data;
317-
}
318-
319303
/**
320304
* kunit_resource_name_match() - Match a resource with the same name.
321305
* @test: Test case to which the resource belongs.

include/kunit/test.h

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -473,30 +473,30 @@ void kunit_do_failed_assertion(struct kunit *test,
473473
const struct kunit_loc *loc,
474474
enum kunit_assert_type type,
475475
const struct kunit_assert *assert,
476+
assert_format_t assert_format,
476477
const char *fmt, ...);
477478

478-
#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
479-
if (unlikely(!(pass))) { \
480-
static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
481-
struct assert_class __assertion = INITIALIZER; \
482-
kunit_do_failed_assertion(test, \
483-
&__loc, \
484-
assert_type, \
485-
&__assertion.assert, \
486-
fmt, \
487-
##__VA_ARGS__); \
488-
} \
479+
#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
480+
static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
481+
const struct assert_class __assertion = INITIALIZER; \
482+
kunit_do_failed_assertion(test, \
483+
&__loc, \
484+
assert_type, \
485+
&__assertion.assert, \
486+
assert_format, \
487+
fmt, \
488+
##__VA_ARGS__); \
489489
} while (0)
490490

491491

492492
#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
493-
KUNIT_ASSERTION(test, \
494-
assert_type, \
495-
false, \
496-
kunit_fail_assert, \
497-
KUNIT_INIT_FAIL_ASSERT_STRUCT, \
498-
fmt, \
499-
##__VA_ARGS__)
493+
_KUNIT_FAILED(test, \
494+
assert_type, \
495+
kunit_fail_assert, \
496+
kunit_fail_assert_format, \
497+
{}, \
498+
fmt, \
499+
##__VA_ARGS__)
500500

501501
/**
502502
* KUNIT_FAIL() - Always causes a test to fail when evaluated.
@@ -521,14 +521,19 @@ void kunit_do_failed_assertion(struct kunit *test,
521521
expected_true, \
522522
fmt, \
523523
...) \
524-
KUNIT_ASSERTION(test, \
525-
assert_type, \
526-
!!(condition) == !!expected_true, \
527-
kunit_unary_assert, \
528-
KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
529-
expected_true), \
530-
fmt, \
531-
##__VA_ARGS__)
524+
do { \
525+
if (likely(!!(condition) == !!expected_true)) \
526+
break; \
527+
\
528+
_KUNIT_FAILED(test, \
529+
assert_type, \
530+
kunit_unary_assert, \
531+
kunit_unary_assert_format, \
532+
KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
533+
expected_true), \
534+
fmt, \
535+
##__VA_ARGS__); \
536+
} while (0)
532537

533538
#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
534539
KUNIT_UNARY_ASSERTION(test, \
@@ -578,16 +583,18 @@ do { \
578583
.right_text = #right, \
579584
}; \
580585
\
581-
KUNIT_ASSERTION(test, \
582-
assert_type, \
583-
__left op __right, \
584-
assert_class, \
585-
KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
586-
&__text, \
587-
__left, \
588-
__right), \
589-
fmt, \
590-
##__VA_ARGS__); \
586+
if (likely(__left op __right)) \
587+
break; \
588+
\
589+
_KUNIT_FAILED(test, \
590+
assert_type, \
591+
assert_class, \
592+
format_func, \
593+
KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
594+
__left, \
595+
__right), \
596+
fmt, \
597+
##__VA_ARGS__); \
591598
} while (0)
592599

593600
#define KUNIT_BINARY_INT_ASSERTION(test, \
@@ -636,16 +643,19 @@ do { \
636643
.right_text = #right, \
637644
}; \
638645
\
639-
KUNIT_ASSERTION(test, \
640-
assert_type, \
641-
strcmp(__left, __right) op 0, \
642-
kunit_binary_str_assert, \
643-
KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
644-
&__text, \
645-
__left, \
646-
__right), \
647-
fmt, \
648-
##__VA_ARGS__); \
646+
if (likely(strcmp(__left, __right) op 0)) \
647+
break; \
648+
\
649+
\
650+
_KUNIT_FAILED(test, \
651+
assert_type, \
652+
kunit_binary_str_assert, \
653+
kunit_binary_str_assert_format, \
654+
KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
655+
__left, \
656+
__right), \
657+
fmt, \
658+
##__VA_ARGS__); \
649659
} while (0)
650660

651661
#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
@@ -656,14 +666,16 @@ do { \
656666
do { \
657667
const typeof(ptr) __ptr = (ptr); \
658668
\
659-
KUNIT_ASSERTION(test, \
660-
assert_type, \
661-
!IS_ERR_OR_NULL(__ptr), \
662-
kunit_ptr_not_err_assert, \
663-
KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
664-
__ptr), \
665-
fmt, \
666-
##__VA_ARGS__); \
669+
if (!IS_ERR_OR_NULL(__ptr)) \
670+
break; \
671+
\
672+
_KUNIT_FAILED(test, \
673+
assert_type, \
674+
kunit_ptr_not_err_assert, \
675+
kunit_ptr_not_err_assert_format, \
676+
KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \
677+
fmt, \
678+
##__VA_ARGS__); \
667679
} while (0)
668680

669681
/**

lib/kunit/kunit-test.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ static void kunit_resource_test_alloc_resource(struct kunit *test)
161161
kunit_put_resource(res);
162162
}
163163

164+
static inline bool kunit_resource_instance_match(struct kunit *test,
165+
struct kunit_resource *res,
166+
void *match_data)
167+
{
168+
return res->data == match_data;
169+
}
170+
164171
/*
165172
* Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
166173
* they have a reference to the associated resource that they must release

0 commit comments

Comments
 (0)