2023-07-18 13:03:03 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* KUnit test for the FPGA Region
|
|
|
|
*
|
|
|
|
* Copyright (C) 2023 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* Author: Marco Pagani <marpagan@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2024-03-29 17:48:47 +00:00
|
|
|
#include <kunit/device.h>
|
2023-07-18 13:03:03 +00:00
|
|
|
#include <kunit/test.h>
|
|
|
|
#include <linux/fpga/fpga-bridge.h>
|
|
|
|
#include <linux/fpga/fpga-mgr.h>
|
|
|
|
#include <linux/fpga/fpga-region.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
|
|
|
|
struct mgr_stats {
|
|
|
|
u32 write_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bridge_stats {
|
|
|
|
bool enable;
|
|
|
|
u32 cycles_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct test_ctx {
|
|
|
|
struct fpga_manager *mgr;
|
2024-03-29 17:48:47 +00:00
|
|
|
struct device *mgr_dev;
|
2023-07-18 13:03:03 +00:00
|
|
|
struct fpga_bridge *bridge;
|
2024-03-29 17:48:47 +00:00
|
|
|
struct device *bridge_dev;
|
2023-07-18 13:03:03 +00:00
|
|
|
struct fpga_region *region;
|
2024-03-29 17:48:47 +00:00
|
|
|
struct device *region_dev;
|
2023-07-18 13:03:03 +00:00
|
|
|
struct bridge_stats bridge_stats;
|
|
|
|
struct mgr_stats mgr_stats;
|
|
|
|
};
|
|
|
|
|
2024-07-25 12:50:31 +00:00
|
|
|
/*
|
|
|
|
* Wrappers to avoid cast warnings when passing action functions directly
|
|
|
|
* to kunit_add_action().
|
|
|
|
*/
|
|
|
|
KUNIT_DEFINE_ACTION_WRAPPER(fpga_image_info_free_wrapper, fpga_image_info_free,
|
|
|
|
struct fpga_image_info *);
|
|
|
|
|
|
|
|
KUNIT_DEFINE_ACTION_WRAPPER(fpga_bridge_unregister_wrapper, fpga_bridge_unregister,
|
|
|
|
struct fpga_bridge *);
|
|
|
|
|
|
|
|
KUNIT_DEFINE_ACTION_WRAPPER(fpga_region_unregister_wrapper, fpga_region_unregister,
|
|
|
|
struct fpga_region *);
|
|
|
|
|
2023-07-18 13:03:03 +00:00
|
|
|
static int op_write(struct fpga_manager *mgr, const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct mgr_stats *stats = mgr->priv;
|
|
|
|
|
|
|
|
stats->write_count++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fake FPGA manager that implements only the write op to count the number
|
|
|
|
* of programming cycles. The internals of the programming sequence are
|
|
|
|
* tested in the Manager suite since they are outside the responsibility
|
|
|
|
* of the Region.
|
|
|
|
*/
|
|
|
|
static const struct fpga_manager_ops fake_mgr_ops = {
|
|
|
|
.write = op_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int op_enable_set(struct fpga_bridge *bridge, bool enable)
|
|
|
|
{
|
|
|
|
struct bridge_stats *stats = bridge->priv;
|
|
|
|
|
|
|
|
if (!stats->enable && enable)
|
|
|
|
stats->cycles_count++;
|
|
|
|
|
|
|
|
stats->enable = enable;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fake FPGA bridge that implements only enable_set op to count the number
|
|
|
|
* of activation cycles.
|
|
|
|
*/
|
|
|
|
static const struct fpga_bridge_ops fake_bridge_ops = {
|
|
|
|
.enable_set = op_enable_set,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int fake_region_get_bridges(struct fpga_region *region)
|
|
|
|
{
|
|
|
|
struct fpga_bridge *bridge = region->priv;
|
|
|
|
|
|
|
|
return fpga_bridge_get_to_list(bridge->dev.parent, region->info, ®ion->bridge_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fake_region_match(struct device *dev, const void *data)
|
|
|
|
{
|
|
|
|
return dev->parent == data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fpga_region_test_class_find(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct test_ctx *ctx = test->priv;
|
|
|
|
struct fpga_region *region;
|
|
|
|
|
2024-03-29 17:48:47 +00:00
|
|
|
region = fpga_region_class_find(NULL, ctx->region_dev, fake_region_match);
|
2023-07-18 13:03:03 +00:00
|
|
|
KUNIT_EXPECT_PTR_EQ(test, region, ctx->region);
|
fpga: Fix memory leak for fpga_region_test_class_find()
fpga_region_class_find() in fpga_region_test_class_find() will call
get_device() if the data is matched, which will increment refcount for
dev->kobj, so it should call put_device() to decrement refcount for
dev->kobj to free the region, because fpga_region_unregister() will call
fpga_region_dev_release() only when the refcount for dev->kobj is zero
but fpga_region_test_init() call device_register() in
fpga_region_register_full(), which also increment refcount.
So call put_device() after calling fpga_region_class_find() in
fpga_region_test_class_find(). After applying this patch, the following
memory leak is never detected.
unreferenced object 0xffff88810c8ef000 (size 1024):
comm "kunit_try_catch", pid 1875, jiffies 4294715298 (age 836.836s)
hex dump (first 32 bytes):
b8 d1 fb 05 81 88 ff ff 08 f0 8e 0c 81 88 ff ff ................
08 f0 8e 0c 81 88 ff ff 00 00 00 00 00 00 00 00 ................
backtrace:
[<ffffffff817ebad7>] kmalloc_trace+0x27/0xa0
[<ffffffffa02385e1>] fpga_region_register_full+0x51/0x430 [fpga_region]
[<ffffffffa0228e47>] 0xffffffffa0228e47
[<ffffffff829c479d>] kunit_try_run_case+0xdd/0x250
[<ffffffff829c9f2a>] kunit_generic_run_threadfn_adapter+0x4a/0x90
[<ffffffff81238b85>] kthread+0x2b5/0x380
[<ffffffff81097ded>] ret_from_fork+0x2d/0x70
[<ffffffff810034d1>] ret_from_fork_asm+0x11/0x20
unreferenced object 0xffff888105fbd1b8 (size 8):
comm "kunit_try_catch", pid 1875, jiffies 4294715298 (age 836.836s)
hex dump (first 8 bytes):
72 65 67 69 6f 6e 30 00 region0.
backtrace:
[<ffffffff817ec023>] __kmalloc_node_track_caller+0x53/0x150
[<ffffffff82995590>] kvasprintf+0xb0/0x130
[<ffffffff83f713b1>] kobject_set_name_vargs+0x41/0x110
[<ffffffff8304ac1b>] dev_set_name+0xab/0xe0
[<ffffffffa02388a2>] fpga_region_register_full+0x312/0x430 [fpga_region]
[<ffffffffa0228e47>] 0xffffffffa0228e47
[<ffffffff829c479d>] kunit_try_run_case+0xdd/0x250
[<ffffffff829c9f2a>] kunit_generic_run_threadfn_adapter+0x4a/0x90
[<ffffffff81238b85>] kthread+0x2b5/0x380
[<ffffffff81097ded>] ret_from_fork+0x2d/0x70
[<ffffffff810034d1>] ret_from_fork_asm+0x11/0x20
unreferenced object 0xffff88810b3b8a00 (size 256):
comm "kunit_try_catch", pid 1875, jiffies 4294715298 (age 836.836s)
hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 08 8a 3b 0b 81 88 ff ff ..........;.....
08 8a 3b 0b 81 88 ff ff e0 ac 04 83 ff ff ff ff ..;.............
backtrace:
[<ffffffff817ebad7>] kmalloc_trace+0x27/0xa0
[<ffffffff83056d7a>] device_add+0xa2a/0x15e0
[<ffffffffa02388b1>] fpga_region_register_full+0x321/0x430 [fpga_region]
[<ffffffffa0228e47>] 0xffffffffa0228e47
[<ffffffff829c479d>] kunit_try_run_case+0xdd/0x250
[<ffffffff829c9f2a>] kunit_generic_run_threadfn_adapter+0x4a/0x90
[<ffffffff81238b85>] kthread+0x2b5/0x380
[<ffffffff81097ded>] ret_from_fork+0x2d/0x70
[<ffffffff810034d1>] ret_from_fork_asm+0x11/0x20
Fixes: 64a5f972c93d ("fpga: add an initial KUnit suite for the FPGA Region")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Reviewed-by: Marco Pagani <marpagan@redhat.com>
Acked-by: Xu Yilun <yilun.xu@intel.com>
Link: https://lore.kernel.org/r/20231007094321.3447084-1-ruanjinjie@huawei.com
[yilun.xu@intel.com: slightly changes the commit message]
Signed-off-by: Xu Yilun <yilun.xu@linux.intel.com>
Link: https://lore.kernel.org/r/20231023032857.902699-3-yilun.xu@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-10-23 03:28:57 +00:00
|
|
|
|
|
|
|
put_device(®ion->dev);
|
2023-07-18 13:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FPGA Region programming test. The Region must call get_bridges() to get
|
|
|
|
* and control the bridges, and then the Manager for the actual programming.
|
|
|
|
*/
|
|
|
|
static void fpga_region_test_program_fpga(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct test_ctx *ctx = test->priv;
|
|
|
|
struct fpga_image_info *img_info;
|
|
|
|
char img_buf[4];
|
|
|
|
int ret;
|
|
|
|
|
2024-03-29 17:48:47 +00:00
|
|
|
img_info = fpga_image_info_alloc(ctx->mgr_dev);
|
2023-07-18 13:03:03 +00:00
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, img_info);
|
|
|
|
|
2024-07-25 12:50:31 +00:00
|
|
|
ret = kunit_add_action_or_reset(test, fpga_image_info_free_wrapper, img_info);
|
|
|
|
KUNIT_ASSERT_EQ(test, ret, 0);
|
|
|
|
|
2023-07-18 13:03:03 +00:00
|
|
|
img_info->buf = img_buf;
|
|
|
|
img_info->count = sizeof(img_buf);
|
|
|
|
|
|
|
|
ctx->region->info = img_info;
|
|
|
|
ret = fpga_region_program_fpga(ctx->region);
|
|
|
|
KUNIT_ASSERT_EQ(test, ret, 0);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_EQ(test, 1, ctx->mgr_stats.write_count);
|
|
|
|
KUNIT_EXPECT_EQ(test, 1, ctx->bridge_stats.cycles_count);
|
|
|
|
|
|
|
|
fpga_bridges_put(&ctx->region->bridge_list);
|
|
|
|
|
|
|
|
ret = fpga_region_program_fpga(ctx->region);
|
|
|
|
KUNIT_ASSERT_EQ(test, ret, 0);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_EQ(test, 2, ctx->mgr_stats.write_count);
|
|
|
|
KUNIT_EXPECT_EQ(test, 2, ctx->bridge_stats.cycles_count);
|
|
|
|
|
|
|
|
fpga_bridges_put(&ctx->region->bridge_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The configuration used in this test suite uses a single bridge to
|
|
|
|
* limit the code under test to a single unit. The functions used by the
|
|
|
|
* Region for getting and controlling bridges are tested (with a list of
|
|
|
|
* multiple bridges) in the Bridge suite.
|
|
|
|
*/
|
|
|
|
static int fpga_region_test_init(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct test_ctx *ctx;
|
|
|
|
struct fpga_region_info region_info = { 0 };
|
2024-07-25 12:50:31 +00:00
|
|
|
int ret;
|
2023-07-18 13:03:03 +00:00
|
|
|
|
|
|
|
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
|
|
|
|
|
2024-03-29 17:48:47 +00:00
|
|
|
ctx->mgr_dev = kunit_device_register(test, "fpga-manager-test-dev");
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->mgr_dev);
|
2023-07-18 13:03:03 +00:00
|
|
|
|
2024-03-29 17:48:47 +00:00
|
|
|
ctx->mgr = devm_fpga_mgr_register(ctx->mgr_dev, "Fake FPGA Manager",
|
|
|
|
&fake_mgr_ops, &ctx->mgr_stats);
|
2023-07-18 13:03:03 +00:00
|
|
|
KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->mgr));
|
|
|
|
|
2024-03-29 17:48:47 +00:00
|
|
|
ctx->bridge_dev = kunit_device_register(test, "fpga-bridge-test-dev");
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->bridge_dev);
|
2023-07-18 13:03:03 +00:00
|
|
|
|
2024-03-29 17:48:47 +00:00
|
|
|
ctx->bridge = fpga_bridge_register(ctx->bridge_dev, "Fake FPGA Bridge",
|
2023-07-18 13:03:03 +00:00
|
|
|
&fake_bridge_ops, &ctx->bridge_stats);
|
|
|
|
KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->bridge));
|
|
|
|
|
|
|
|
ctx->bridge_stats.enable = true;
|
|
|
|
|
2024-07-25 12:50:31 +00:00
|
|
|
ret = kunit_add_action_or_reset(test, fpga_bridge_unregister_wrapper, ctx->bridge);
|
|
|
|
KUNIT_ASSERT_EQ(test, ret, 0);
|
|
|
|
|
2024-03-29 17:48:47 +00:00
|
|
|
ctx->region_dev = kunit_device_register(test, "fpga-region-test-dev");
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->region_dev);
|
2023-07-18 13:03:03 +00:00
|
|
|
|
|
|
|
region_info.mgr = ctx->mgr;
|
|
|
|
region_info.priv = ctx->bridge;
|
|
|
|
region_info.get_bridges = fake_region_get_bridges;
|
|
|
|
|
2024-03-29 17:48:47 +00:00
|
|
|
ctx->region = fpga_region_register_full(ctx->region_dev, ®ion_info);
|
2023-07-18 13:03:03 +00:00
|
|
|
KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->region));
|
|
|
|
|
2024-07-25 12:50:31 +00:00
|
|
|
ret = kunit_add_action_or_reset(test, fpga_region_unregister_wrapper, ctx->region);
|
|
|
|
KUNIT_ASSERT_EQ(test, ret, 0);
|
|
|
|
|
2023-07-18 13:03:03 +00:00
|
|
|
test->priv = ctx;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kunit_case fpga_region_test_cases[] = {
|
|
|
|
KUNIT_CASE(fpga_region_test_class_find),
|
|
|
|
KUNIT_CASE(fpga_region_test_program_fpga),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kunit_suite fpga_region_suite = {
|
2024-07-25 12:50:31 +00:00
|
|
|
.name = "fpga_region",
|
2023-07-18 13:03:03 +00:00
|
|
|
.init = fpga_region_test_init,
|
|
|
|
.test_cases = fpga_region_test_cases,
|
|
|
|
};
|
|
|
|
|
|
|
|
kunit_test_suite(fpga_region_suite);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|