From: Ayubjon Qosimov The hint that reserve_memblock_reserved_resources() uses must leave the resource tree exactly as reserve_region_with_split() builds it; it only changes where each walk starts. Build the same tree under two identical roots, once with reserve_region_with_split() and once through __reserve_region_with_split() with a hint, for ascending ranges, ranges that overlap pre-existing children, ranges that overlap the hint (as page rounding produces) and unsorted ranges, and check after each range that the hint is a child of the root starting no higher than the range. A second case checks that a range a child already covers moves the hint onto that child. A third runs the helper's walk on a memblock table of its own, against the loop the architectures used: memory resources out of order, a range spanning two of them, a resource no range reaches, overlapping resources, a pre-existing child, and inverted and wrapping entries that must be skipped. For this, __reserve_region_with_split() and the walk, __reserve_memblock_resources(), are made visible to KUnit through kernel/resource_internal.h. The functions are __init, so the cases run in an init-section suite, and only when the test is built in. Signed-off-by: Ayubjon Qosimov --- kernel/resource.c | 12 ++- kernel/resource_internal.h | 18 ++++ kernel/resource_kunit.c | 254 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 279 insertions(+), 5 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index 2b274f30fbde..247c4b56563d 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -31,6 +31,9 @@ #include #include #include +#include + +#include "resource_internal.h" struct resource ioport_resource = { @@ -1169,7 +1172,7 @@ int adjust_resource(struct resource *res, resource_size_t start, EXPORT_SYMBOL(adjust_resource); /* Called with resource_lock held; @hint is passed to __request_resource_hint(). */ -static void __init +VISIBLE_IF_KUNIT void __init __reserve_region_with_split(struct resource *root, resource_size_t start, resource_size_t end, const char *name, struct resource **hint) @@ -1261,10 +1264,9 @@ reserve_region_with_split(struct resource *root, resource_size_t start, } /* reserve_memblock_reserved_resources() on the ranges of @type. */ -static void __init __reserve_memblock_resources(struct memblock_type *type, - struct resource *res, - unsigned int nr, - const char *name) +VISIBLE_IF_KUNIT void __init +__reserve_memblock_resources(struct memblock_type *type, struct resource *res, + unsigned int nr, const char *name) { phys_addr_t r_start, r_end; unsigned int k; diff --git a/kernel/resource_internal.h b/kernel/resource_internal.h new file mode 100644 index 000000000000..81a872b48385 --- /dev/null +++ b/kernel/resource_internal.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _KERNEL_RESOURCE_INTERNAL_H +#define _KERNEL_RESOURCE_INTERNAL_H + +#include + +#if IS_ENABLED(CONFIG_KUNIT) +/* Exposed for kernel/resource_kunit.c */ +void __reserve_region_with_split(struct resource *root, resource_size_t start, + resource_size_t end, const char *name, + struct resource **hint); +struct memblock_type; +void __reserve_memblock_resources(struct memblock_type *type, + struct resource *res, unsigned int nr, + const char *name); +#endif + +#endif /* _KERNEL_RESOURCE_INTERNAL_H */ diff --git a/kernel/resource_kunit.c b/kernel/resource_kunit.c index 9eeb2b1a85c0..75d7a92a23bb 100644 --- a/kernel/resource_kunit.c +++ b/kernel/resource_kunit.c @@ -9,6 +9,11 @@ #include #include #include +#include +#include +#include + +#include "resource_internal.h" #define R0_START 0x0000 #define R0_END 0xffff @@ -305,5 +310,254 @@ static struct kunit_suite resource_test_suite = { }; kunit_test_suite(resource_test_suite); +#if IS_BUILTIN(CONFIG_RESOURCE_KUNIT_TEST) +/* + * The functions below are __init, so these cases run in an init-section suite + * and only when the test is built in. Each case builds the same tree twice, + * once the way the architectures used to and once with the hint, and checks + * that the two match. + */ +struct hint_range { + resource_size_t start, end; +}; + +/* Frees every child below @root; they are all kmalloc'ed. */ +static void __init hint_free_tree(struct resource *root) +{ + struct resource *p = root->child, *next; + + root->child = NULL; + for (; p; p = next) { + next = p->sibling; + hint_free_tree(p); + kfree(p); + } +} + +struct hint_roots { + struct resource *res; + unsigned int nr; +}; + +/* Runs when the case ends, also after a failed assertion. */ +static void __init hint_cleanup(void *data) +{ + struct hint_roots *roots = data; + unsigned int k; + + for (k = 0; k < roots->nr; k++) + hint_free_tree(&roots->res[k]); +} + +static struct resource * __init hint_alloc_roots(struct kunit *test, + const struct hint_range *mem, + unsigned int nr) +{ + struct hint_roots *roots; + unsigned int k; + + roots = kunit_kzalloc(test, sizeof(*roots), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, roots); + roots->res = kunit_kcalloc(test, nr, sizeof(*roots->res), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, roots->res); + roots->nr = nr; + for (k = 0; k < nr; k++) { + resource_size_t size = mem[k].end - mem[k].start + 1; + + roots->res[k] = DEFINE_RES_MEM_NAMED(mem[k].start, size, "mem"); + } + resource_test_add_action_or_abort(test, hint_cleanup, roots); + return roots->res; +} + +/* Request a kmalloc'ed [start, end] named "pre" under @a and under @b. */ +static void __init hint_add_pre(struct kunit *test, struct resource *a, + struct resource *b, resource_size_t start, + resource_size_t end) +{ + struct resource *root[] = { a, b }, *r; + int i, ret; + + for (i = 0; i < 2; i++) { + r = kzalloc_obj(*r); + KUNIT_ASSERT_NOT_NULL(test, r); + *r = DEFINE_RES_MEM_NAMED(start, end - start + 1, "pre"); + ret = request_resource(root[i], r); + if (ret) + kfree(r); + KUNIT_ASSERT_EQ(test, 0, ret); + } +} + +static void __init hint_expect_same(struct kunit *test, struct resource *a, + struct resource *b) +{ + struct resource *pa, *pb; + + for (pa = a->child, pb = b->child; pa && pb; + pa = pa->sibling, pb = pb->sibling) { + KUNIT_EXPECT_EQ(test, pa->start, pb->start); + KUNIT_EXPECT_EQ(test, pa->end, pb->end); + KUNIT_EXPECT_STREQ(test, pa->name, pb->name); + KUNIT_EXPECT_PTR_EQ(test, pb->parent, b); + hint_expect_same(test, pa, pb); + } + KUNIT_EXPECT_NULL(test, pa); + KUNIT_EXPECT_NULL(test, pb); +} + +struct hint_case { + const char *desc; + struct hint_range pre[2]; + unsigned int npre; + struct hint_range r[5]; + unsigned int n; + bool tail; +}; + +static const struct hint_case hint_cases[] = { + { "ascending", {}, 0, + { { 0x0000, 0x0fff }, { 0x2000, 0x2fff }, { 0x3000, 0x3fff }, + { 0x8000, 0x80ff }, { 0x9000, 0xffff } }, 5, true }, + { "pre-existing children", { { 0x1800, 0x27ff }, { 0x6000, 0x6fff } }, 2, + { { 0x1000, 0x1fff }, { 0x2000, 0x3fff }, { 0x5000, 0x7fff }, + { 0x6100, 0x61ff }, { 0x8000, 0x8fff } }, 5, false }, + { "overlapping the hint", {}, 0, + { { 0x1000, 0x1fff }, { 0x1000, 0x1fff }, { 0x1000, 0x2fff }, + { 0x2000, 0x3fff }, { 0x4000, 0x4fff } }, 5, false }, + { "unsorted", {}, 0, + { { 0x9000, 0x9fff }, { 0x5000, 0x5fff }, { 0x7000, 0x7fff }, + { 0x1000, 0x1fff }, { 0x0000, 0x0fff } }, 5, false }, +}; + +static void hint_case_desc(const struct hint_case *c, char *desc) +{ + strscpy(desc, c->desc, KUNIT_PARAM_DESC_SIZE); +} + +KUNIT_ARRAY_PARAM(hint, hint_cases, hint_case_desc); + +static void __init resource_test_hint(struct kunit *test) +{ + static const struct hint_range root = { 0, SZ_64K - 1 }; + const struct hint_case *c = test->param_value; + struct resource *a, *b, *hint = NULL; + unsigned int i; + + a = hint_alloc_roots(test, &root, 1); + b = hint_alloc_roots(test, &root, 1); + for (i = 0; i < c->npre; i++) + hint_add_pre(test, a, b, c->pre[i].start, c->pre[i].end); + + for (i = 0; i < c->n; i++) { + reserve_region_with_split(a, c->r[i].start, c->r[i].end, "r"); + /* @b is not in the resource tree, so resource_lock is not needed. */ + __reserve_region_with_split(b, c->r[i].start, c->r[i].end, "r", + &hint); + + /* The hint is a child of @b starting no higher than r[i].end. */ + KUNIT_ASSERT_NOT_NULL(test, hint); + KUNIT_EXPECT_PTR_EQ(test, hint->parent, b); + KUNIT_EXPECT_LE(test, hint->start, c->r[i].end); + if (c->tail) + KUNIT_EXPECT_NULL(test, hint->sibling); + } + hint_expect_same(test, a, b); +} + +static void __init resource_test_hint_absorbed(struct kunit *test) +{ + /* A range that a child covers moves the hint onto that child. */ + static const struct hint_range root = { 0, SZ_64K - 1 }; + struct resource *a, *b, *pre, *hint = NULL; + + a = hint_alloc_roots(test, &root, 1); + b = hint_alloc_roots(test, &root, 1); + hint_add_pre(test, a, b, 0x6000, 0x6fff); + pre = b->child; + + __reserve_region_with_split(b, 0x5000, 0x5fff, "r", &hint); + KUNIT_ASSERT_NOT_NULL(test, hint); + KUNIT_EXPECT_PTR_EQ(test, hint->sibling, pre); + __reserve_region_with_split(b, 0x6100, 0x61ff, "r", &hint); + KUNIT_EXPECT_PTR_EQ(test, hint, pre); + __reserve_region_with_split(b, 0x6200, 0x62ff, "r", &hint); + KUNIT_EXPECT_PTR_EQ(test, hint, pre); +} + +static void __init resource_test_memblock_walk(struct kunit *test) +{ + /* + * Memory resources out of order: a range spans two of them, no range + * reaches one, one overlaps another, one has a child already, and an + * inverted and a wrapping one must be skipped. + */ + static struct memblock_region regions[] __initdata = { + { .base = 1 * PAGE_SIZE, .size = PAGE_SIZE }, + { .base = 3 * PAGE_SIZE, .size = 2 * PAGE_SIZE }, + { .base = 7 * PAGE_SIZE + PAGE_SIZE / 2, .size = PAGE_SIZE / 16 }, + { .base = 10 * PAGE_SIZE, .size = PAGE_SIZE / 16 }, + { .base = 12 * PAGE_SIZE, .size = 4 * PAGE_SIZE }, + }; + static struct memblock_type type __initdata = { + .regions = regions, + .cnt = ARRAY_SIZE(regions), + .max = ARRAY_SIZE(regions), + .name = "test", + }; +#define P(n) ((resource_size_t)(n) * PAGE_SIZE) + static const struct hint_range mem[] __initconst = { + { P(14), P(17) - 1 }, { P(9), P(10) - 1 }, { P(4), P(8) - 1 }, + { P(11), P(14) - 1 }, { P(0), P(4) - 1 }, { P(2), P(6) - 1 }, + { P(32), P(32) - 1 }, { 0, ~(resource_size_t)0 }, + }; +#undef P + const unsigned int nr = ARRAY_SIZE(mem); + phys_addr_t r_start, r_end; + struct resource *a, *b; + unsigned int k; + u64 i; + + a = hint_alloc_roots(test, mem, nr); + b = hint_alloc_roots(test, mem, nr); + hint_add_pre(test, &a[2], &b[2], 4 * PAGE_SIZE + PAGE_SIZE / 4, + 4 * PAGE_SIZE + PAGE_SIZE / 2 - 1); + + /* The loop the helper replaces, on the six valid resources. */ + for (k = 0; k < nr - 2; k++) { + __for_each_mem_range(i, &type, NULL, NUMA_NO_NODE, MEMBLOCK_NONE, + &r_start, &r_end, NULL) { + resource_size_t start, end; + + start = max(PFN_PHYS(PFN_DOWN(r_start)), a[k].start); + end = min(PFN_PHYS(PFN_UP(r_end)) - 1, a[k].end); + if (start > a[k].end || end < a[k].start) + continue; + reserve_region_with_split(&a[k], start, end, "r"); + } + } + __reserve_memblock_resources(&type, b, nr, "r"); + + for (k = 0; k < nr; k++) + hint_expect_same(test, &a[k], &b[k]); + KUNIT_EXPECT_NOT_NULL(test, b[0].child); + KUNIT_EXPECT_NULL(test, b[1].child); + KUNIT_EXPECT_NOT_NULL(test, b[3].child); +} + +static struct kunit_case __refdata resource_init_test_cases[] = { + KUNIT_CASE_PARAM(resource_test_hint, hint_gen_params), + KUNIT_CASE(resource_test_hint_absorbed), + KUNIT_CASE(resource_test_memblock_walk), + {} +}; + +static struct kunit_suite resource_init_test_suite = { + .name = "resource_init", + .test_cases = resource_init_test_cases, +}; +kunit_test_init_section_suite(resource_init_test_suite); +#endif /* IS_BUILTIN(CONFIG_RESOURCE_KUNIT_TEST) */ + MODULE_DESCRIPTION("I/O Port & Memory Resource manager unit tests"); MODULE_LICENSE("GPL"); -- 2.47.3