linux-kselftest-kunit-6.1-rc1-2

This second KUnit update for Linux 6.1-rc1 consists of features and
 fixes:
 
 - simplifying 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.
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEPZKym/RZuOCGeA/kCwJExA0NQxwFAmNG/4EACgkQCwJExA0N
 QxxznhAAqtXbCYxIxerdiAHwYifnsrLcCMm/Ol2yuFJhmTn6sZh7w4S8bRBt0RlX
 +1IfqtzOi1K1fTpmWQqnq0/fH8gNZrhZHHqXxx3c353pG0BfrC3vODx1VzxuPCMi
 nr/OHqAQ0VSTuxgWxsIr0SuhOM4LFDjhBcLDoCDoBF5aQSJricpa++ixiYsVgaUt
 nG+E1i7I/hvEYwqqUqtJLp9fOD6LK2IeiOP4oH2PwYBIpFO+BXwk0Gbs/ISL+fRP
 F8pph2Qm2jxCJ4kRDvs/N41mkIvG9PwC1h7fW4vDXix0zryJdh0TbilFQFFwiuW3
 S8kFE1tarMBWyqEZU/2cln9MFdZpxXAWtJu1/B8dqOvLA06mBOaNbB4tOXzfyriE
 QBOnEJNqgT0wqnwWONvrljz7L+YaFAkJAGxbub1cGIUa/t5HHs0WX5XncctGfsaE
 Ec6bLOXMgemb3dm35fDpBHyN6np9K5BMmz8Ggv02+V8FH8nrXAzblOW/CN8KgXiG
 R5+1vd3SxaLq7npal4S88LmNRoJCVCSWnNPItBTgWFXy6Ni2T5WEoi6rSdqJNX+/
 bpPM4G47IO5BH0YEbl9IPvKLfDGczVB4TVLpIt61QST4rf+puUhysr76ZweqoU6f
 sOyEenr3YZ7C3EpSbcAztzgyPomPAacR/lNbG5lezcEPRSo184I=
 =FgDN
 -----END PGP SIGNATURE-----

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
This commit is contained in:
Linus Torvalds 2022-10-12 15:01:58 -07:00
commit a185a09955
9 changed files with 134 additions and 209 deletions

View File

@ -251,14 +251,15 @@ command line arguments:
compiling a kernel (using ``build`` or ``run`` commands). For example:
to enable compiler warnings, we can pass ``--make_options W=1``.
- ``--alltests``: Builds a UML kernel with all config options enabled
using ``make allyesconfig``. This allows us to run as many tests as
possible.
- ``--alltests``: Enable a predefined set of options in order to build
as many tests as possible.
.. note:: It is slow and prone to breakage as new options are
added or modified. Instead, enable all tests
which have satisfied dependencies by adding
``CONFIG_KUNIT_ALL_TESTS=y`` to your ``.kunitconfig``.
.. note:: The list of enabled options can be found in
``tools/testing/kunit/configs/all_tests.config``.
If you only want to enable all tests with otherwise satisfied
dependencies, instead add ``CONFIG_KUNIT_ALL_TESTS=y`` to your
``.kunitconfig``.
- ``--kunitconfig``: Specifies the path or the directory of the ``.kunitconfig``
file. For example:

View File

@ -42,16 +42,15 @@ struct kunit_loc {
/**
* struct kunit_assert - Data for printing a failed assertion or expectation.
* @format: a function which formats the data in this kunit_assert to a string.
*
* Represents a failed expectation/assertion. Contains all the data necessary to
* format a string to a user reporting the failure.
*/
struct kunit_assert {
void (*format)(const struct kunit_assert *assert,
const struct va_format *message,
struct string_stream *stream);
};
struct kunit_assert {};
typedef void (*assert_format_t)(const struct kunit_assert *assert,
const struct va_format *message,
struct string_stream *stream);
void kunit_assert_prologue(const struct kunit_loc *loc,
enum kunit_assert_type type,
@ -71,16 +70,6 @@ void kunit_fail_assert_format(const struct kunit_assert *assert,
const struct va_format *message,
struct string_stream *stream);
/**
* KUNIT_INIT_FAIL_ASSERT_STRUCT - Initializer for &struct kunit_fail_assert.
*
* Initializes a &struct kunit_fail_assert. Intended to be used in
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
*/
#define KUNIT_INIT_FAIL_ASSERT_STRUCT { \
.assert = { .format = kunit_fail_assert_format }, \
}
/**
* struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
* @assert: The parent of this type.
@ -110,7 +99,6 @@ void kunit_unary_assert_format(const struct kunit_assert *assert,
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
*/
#define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \
.assert = { .format = kunit_unary_assert_format }, \
.condition = cond, \
.expected_true = expect_true \
}
@ -145,7 +133,6 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
*/
#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \
.assert = { .format = kunit_ptr_not_err_assert_format }, \
.text = txt, \
.value = val \
}
@ -190,7 +177,6 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
* KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like
* kunit_binary_assert, kunit_binary_ptr_assert, etc.
*
* @format_func: a function which formats the assert to a string.
* @text_: Pointer to a kunit_binary_assert_text.
* @left_val: The actual evaluated value of the expression in the left slot.
* @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,
* fields but with different types for left_val/right_val.
* This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ, etc.
*/
#define KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
text_, \
#define KUNIT_INIT_BINARY_ASSERT_STRUCT(text_, \
left_val, \
right_val) { \
.assert = { .format = format_func }, \
.text = text_, \
.left_value = left_val, \
.right_value = right_val \

View File

@ -300,22 +300,6 @@ typedef bool (*kunit_resource_match_t)(struct kunit *test,
struct kunit_resource *res,
void *match_data);
/**
* kunit_resource_instance_match() - Match a resource with the same instance.
* @test: Test case to which the resource belongs.
* @res: The resource.
* @match_data: The resource pointer to match against.
*
* An instance of kunit_resource_match_t that matches a resource whose
* allocation matches @match_data.
*/
static inline bool kunit_resource_instance_match(struct kunit *test,
struct kunit_resource *res,
void *match_data)
{
return res->data == match_data;
}
/**
* kunit_resource_name_match() - Match a resource with the same name.
* @test: Test case to which the resource belongs.

View File

@ -473,30 +473,30 @@ void kunit_do_failed_assertion(struct kunit *test,
const struct kunit_loc *loc,
enum kunit_assert_type type,
const struct kunit_assert *assert,
assert_format_t assert_format,
const char *fmt, ...);
#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
if (unlikely(!(pass))) { \
static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
struct assert_class __assertion = INITIALIZER; \
kunit_do_failed_assertion(test, \
&__loc, \
assert_type, \
&__assertion.assert, \
fmt, \
##__VA_ARGS__); \
} \
#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
const struct assert_class __assertion = INITIALIZER; \
kunit_do_failed_assertion(test, \
&__loc, \
assert_type, \
&__assertion.assert, \
assert_format, \
fmt, \
##__VA_ARGS__); \
} while (0)
#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
KUNIT_ASSERTION(test, \
assert_type, \
false, \
kunit_fail_assert, \
KUNIT_INIT_FAIL_ASSERT_STRUCT, \
fmt, \
##__VA_ARGS__)
_KUNIT_FAILED(test, \
assert_type, \
kunit_fail_assert, \
kunit_fail_assert_format, \
{}, \
fmt, \
##__VA_ARGS__)
/**
* KUNIT_FAIL() - Always causes a test to fail when evaluated.
@ -521,14 +521,19 @@ void kunit_do_failed_assertion(struct kunit *test,
expected_true, \
fmt, \
...) \
KUNIT_ASSERTION(test, \
assert_type, \
!!(condition) == !!expected_true, \
kunit_unary_assert, \
KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
expected_true), \
fmt, \
##__VA_ARGS__)
do { \
if (likely(!!(condition) == !!expected_true)) \
break; \
\
_KUNIT_FAILED(test, \
assert_type, \
kunit_unary_assert, \
kunit_unary_assert_format, \
KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
expected_true), \
fmt, \
##__VA_ARGS__); \
} while (0)
#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
KUNIT_UNARY_ASSERTION(test, \
@ -578,16 +583,18 @@ do { \
.right_text = #right, \
}; \
\
KUNIT_ASSERTION(test, \
assert_type, \
__left op __right, \
assert_class, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
&__text, \
__left, \
__right), \
fmt, \
##__VA_ARGS__); \
if (likely(__left op __right)) \
break; \
\
_KUNIT_FAILED(test, \
assert_type, \
assert_class, \
format_func, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
__left, \
__right), \
fmt, \
##__VA_ARGS__); \
} while (0)
#define KUNIT_BINARY_INT_ASSERTION(test, \
@ -636,16 +643,19 @@ do { \
.right_text = #right, \
}; \
\
KUNIT_ASSERTION(test, \
assert_type, \
strcmp(__left, __right) op 0, \
kunit_binary_str_assert, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
&__text, \
__left, \
__right), \
fmt, \
##__VA_ARGS__); \
if (likely(strcmp(__left, __right) op 0)) \
break; \
\
\
_KUNIT_FAILED(test, \
assert_type, \
kunit_binary_str_assert, \
kunit_binary_str_assert_format, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
__left, \
__right), \
fmt, \
##__VA_ARGS__); \
} while (0)
#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
@ -656,14 +666,16 @@ do { \
do { \
const typeof(ptr) __ptr = (ptr); \
\
KUNIT_ASSERTION(test, \
assert_type, \
!IS_ERR_OR_NULL(__ptr), \
kunit_ptr_not_err_assert, \
KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
__ptr), \
fmt, \
##__VA_ARGS__); \
if (!IS_ERR_OR_NULL(__ptr)) \
break; \
\
_KUNIT_FAILED(test, \
assert_type, \
kunit_ptr_not_err_assert, \
kunit_ptr_not_err_assert_format, \
KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \
fmt, \
##__VA_ARGS__); \
} while (0)
/**

View File

@ -161,6 +161,13 @@ static void kunit_resource_test_alloc_resource(struct kunit *test)
kunit_put_resource(res);
}
static inline bool kunit_resource_instance_match(struct kunit *test,
struct kunit_resource *res,
void *match_data)
{
return res->data == match_data;
}
/*
* Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
* they have a reference to the associated resource that they must release

View File

@ -12,62 +12,29 @@
#include "string-stream.h"
struct string_stream_fragment_alloc_context {
struct kunit *test;
int len;
gfp_t gfp;
};
static int string_stream_fragment_init(struct kunit_resource *res,
void *context)
{
struct string_stream_fragment_alloc_context *ctx = context;
struct string_stream_fragment *frag;
frag = kunit_kzalloc(ctx->test, sizeof(*frag), ctx->gfp);
if (!frag)
return -ENOMEM;
frag->test = ctx->test;
frag->fragment = kunit_kmalloc(ctx->test, ctx->len, ctx->gfp);
if (!frag->fragment)
return -ENOMEM;
res->data = frag;
return 0;
}
static void string_stream_fragment_free(struct kunit_resource *res)
{
struct string_stream_fragment *frag = res->data;
list_del(&frag->node);
kunit_kfree(frag->test, frag->fragment);
kunit_kfree(frag->test, frag);
}
static struct string_stream_fragment *alloc_string_stream_fragment(
struct kunit *test, int len, gfp_t gfp)
{
struct string_stream_fragment_alloc_context context = {
.test = test,
.len = len,
.gfp = gfp
};
struct string_stream_fragment *frag;
return kunit_alloc_resource(test,
string_stream_fragment_init,
string_stream_fragment_free,
gfp,
&context);
frag = kunit_kzalloc(test, sizeof(*frag), gfp);
if (!frag)
return ERR_PTR(-ENOMEM);
frag->fragment = kunit_kmalloc(test, len, gfp);
if (!frag->fragment)
return ERR_PTR(-ENOMEM);
return frag;
}
static int string_stream_fragment_destroy(struct string_stream_fragment *frag)
static void string_stream_fragment_destroy(struct kunit *test,
struct string_stream_fragment *frag)
{
return kunit_destroy_resource(frag->test,
kunit_resource_instance_match,
frag);
list_del(&frag->node);
kunit_kfree(test, frag->fragment);
kunit_kfree(test, frag);
}
int string_stream_vadd(struct string_stream *stream,
@ -122,7 +89,7 @@ static void string_stream_clear(struct string_stream *stream)
frag_container_safe,
&stream->fragments,
node) {
string_stream_fragment_destroy(frag_container);
string_stream_fragment_destroy(stream->test, frag_container);
}
stream->length = 0;
spin_unlock(&stream->lock);
@ -169,48 +136,23 @@ struct string_stream_alloc_context {
gfp_t gfp;
};
static int string_stream_init(struct kunit_resource *res, void *context)
struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp)
{
struct string_stream *stream;
struct string_stream_alloc_context *ctx = context;
stream = kunit_kzalloc(ctx->test, sizeof(*stream), ctx->gfp);
stream = kunit_kzalloc(test, sizeof(*stream), gfp);
if (!stream)
return -ENOMEM;
return ERR_PTR(-ENOMEM);
res->data = stream;
stream->gfp = ctx->gfp;
stream->test = ctx->test;
stream->gfp = gfp;
stream->test = test;
INIT_LIST_HEAD(&stream->fragments);
spin_lock_init(&stream->lock);
return 0;
return stream;
}
static void string_stream_free(struct kunit_resource *res)
void string_stream_destroy(struct string_stream *stream)
{
struct string_stream *stream = res->data;
string_stream_clear(stream);
}
struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp)
{
struct string_stream_alloc_context context = {
.test = test,
.gfp = gfp
};
return kunit_alloc_resource(test,
string_stream_init,
string_stream_free,
gfp,
&context);
}
int string_stream_destroy(struct string_stream *stream)
{
return kunit_destroy_resource(stream->test,
kunit_resource_instance_match,
stream);
}

View File

@ -14,7 +14,6 @@
#include <linux/stdarg.h>
struct string_stream_fragment {
struct kunit *test;
struct list_head node;
char *fragment;
};
@ -46,6 +45,6 @@ int string_stream_append(struct string_stream *stream,
bool string_stream_is_empty(struct string_stream *stream);
int string_stream_destroy(struct string_stream *stream);
void string_stream_destroy(struct string_stream *stream);
#endif /* _KUNIT_STRING_STREAM_H */

View File

@ -258,7 +258,7 @@ static void kunit_print_string_stream(struct kunit *test,
static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
enum kunit_assert_type type, const struct kunit_assert *assert,
const struct va_format *message)
assert_format_t assert_format, const struct va_format *message)
{
struct string_stream *stream;
@ -274,11 +274,11 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
}
kunit_assert_prologue(loc, type, stream);
assert->format(assert, message, stream);
assert_format(assert, message, stream);
kunit_print_string_stream(test, stream);
WARN_ON(string_stream_destroy(stream));
string_stream_destroy(stream);
}
static void __noreturn kunit_abort(struct kunit *test)
@ -298,6 +298,7 @@ void kunit_do_failed_assertion(struct kunit *test,
const struct kunit_loc *loc,
enum kunit_assert_type type,
const struct kunit_assert *assert,
assert_format_t assert_format,
const char *fmt, ...)
{
va_list args;
@ -307,7 +308,7 @@ void kunit_do_failed_assertion(struct kunit *test,
message.fmt = fmt;
message.va = &args;
kunit_fail(test, loc, type, assert, &message);
kunit_fail(test, loc, type, assert, assert_format, &message);
va_end(args);
@ -713,21 +714,20 @@ void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
}
EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
static inline bool kunit_kfree_match(struct kunit *test,
struct kunit_resource *res, void *match_data)
{
/* Only match resources allocated with kunit_kmalloc() and friends. */
return res->free == kunit_kmalloc_array_free && res->data == match_data;
}
void kunit_kfree(struct kunit *test, const void *ptr)
{
struct kunit_resource *res;
res = kunit_find_resource(test, kunit_resource_instance_match,
(void *)ptr);
/*
* Removing the resource from the list of resources drops the
* reference count to 1; the final put will trigger the free.
*/
kunit_remove_resource(test, res);
kunit_put_resource(res);
if (!ptr)
return;
if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr))
KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr);
}
EXPORT_SYMBOL_GPL(kunit_kfree);

View File

@ -3,17 +3,13 @@ import os
import os.path
import sys
GITHUB_OPENSBI_URL = 'https://github.com/qemu/qemu/raw/master/pc-bios/opensbi-riscv64-generic-fw_dynamic.bin'
OPENSBI_FILE = os.path.basename(GITHUB_OPENSBI_URL)
OPENSBI_FILE = 'opensbi-riscv64-generic-fw_dynamic.bin'
OPENSBI_PATH = '/usr/share/qemu/' + OPENSBI_FILE
if not os.path.isfile(OPENSBI_FILE):
print('\n\nOpenSBI file is not in the current working directory.\n'
'Would you like me to download it for you from:\n' + GITHUB_OPENSBI_URL + ' ?\n')
response = input('yes/[no]: ')
if response.strip() == 'yes':
os.system('wget ' + GITHUB_OPENSBI_URL)
else:
sys.exit()
if not os.path.isfile(OPENSBI_PATH):
print('\n\nOpenSBI bios was not found in "' + OPENSBI_PATH + '".\n'
'Please ensure that qemu-system-riscv is installed, or edit the path in "qemu_configs/riscv.py"\n')
sys.exit()
QEMU_ARCH = QemuArchParams(linux_arch='riscv',
kconfig='''
@ -29,4 +25,4 @@ CONFIG_SERIAL_EARLYCON_RISCV_SBI=y''',
extra_qemu_params=[
'-machine', 'virt',
'-cpu', 'rv64',
'-bios', 'opensbi-riscv64-generic-fw_dynamic.bin'])
'-bios', OPENSBI_PATH])