1
linux/drivers/of/of_kunit_helpers.c

93 lines
2.4 KiB
C
Raw Permalink Normal View History

of: Add test managed wrappers for of_overlay_apply()/of_node_put() Add test managed wrappers for of_overlay_apply() that automatically removes the overlay when the test is finished. This API is intended for use by KUnit tests that test code which relies on 'struct device_node's and of_*() APIs. KUnit tests will call of_overlay_apply_kunit() to load an overlay that's been built into the kernel image. When the test is complete, the overlay will be removed. This has a few benefits: 1) It keeps the tests hermetic because the overlay is removed when the test is complete. Tests won't even be aware that an overlay was loaded in another test. 2) The overlay code can live right next to the unit test that loads it. The overlay and the unit test can be compiled into one kernel module if desired. 3) We can test different device tree configurations by loading different overlays. The overlays can be written for a specific test, and there can be many of them loaded per-test without needing to jam all possible combinations into one DTB. 4) It also allows KUnit to test device tree dependent code on any architecture, not just UML. This allows KUnit tests to test architecture specific device tree code. There are some potential pitfalls though. Test authors need to be careful to not overwrite properties in the live tree. The easiest way to do this is to add and remove nodes with a 'kunit-' prefix, almost guaranteeing that the same node won't be present in the tree loaded at boot. Suggested-by: Rob Herring <robh@kernel.org> Cc: Rob Herring <robh@kernel.org> Cc: Saravana Kannan <saravanak@google.com> Reviewed-by: Rob Herring (Arm) <robh@kernel.org> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Stephen Boyd <sboyd@kernel.org> Link: https://lore.kernel.org/r/20240718210513.3801024-3-sboyd@kernel.org
2024-07-18 14:05:01 -07:00
// SPDX-License-Identifier: GPL-2.0
/*
* Test managed DeviceTree APIs
*/
#include <linux/of.h>
#include <linux/of_fdt.h>
#include <kunit/of.h>
#include <kunit/test.h>
#include <kunit/resource.h>
#include "of_private.h"
/**
* of_root_kunit_skip() - Skip test if the root node isn't populated
* @test: test to skip if the root node isn't populated
*/
void of_root_kunit_skip(struct kunit *test)
{
if (IS_ENABLED(CONFIG_ARM64) && IS_ENABLED(CONFIG_ACPI) && !of_root)
kunit_skip(test, "arm64+acpi doesn't populate a root node");
}
EXPORT_SYMBOL_GPL(of_root_kunit_skip);
of: Add test managed wrappers for of_overlay_apply()/of_node_put() Add test managed wrappers for of_overlay_apply() that automatically removes the overlay when the test is finished. This API is intended for use by KUnit tests that test code which relies on 'struct device_node's and of_*() APIs. KUnit tests will call of_overlay_apply_kunit() to load an overlay that's been built into the kernel image. When the test is complete, the overlay will be removed. This has a few benefits: 1) It keeps the tests hermetic because the overlay is removed when the test is complete. Tests won't even be aware that an overlay was loaded in another test. 2) The overlay code can live right next to the unit test that loads it. The overlay and the unit test can be compiled into one kernel module if desired. 3) We can test different device tree configurations by loading different overlays. The overlays can be written for a specific test, and there can be many of them loaded per-test without needing to jam all possible combinations into one DTB. 4) It also allows KUnit to test device tree dependent code on any architecture, not just UML. This allows KUnit tests to test architecture specific device tree code. There are some potential pitfalls though. Test authors need to be careful to not overwrite properties in the live tree. The easiest way to do this is to add and remove nodes with a 'kunit-' prefix, almost guaranteeing that the same node won't be present in the tree loaded at boot. Suggested-by: Rob Herring <robh@kernel.org> Cc: Rob Herring <robh@kernel.org> Cc: Saravana Kannan <saravanak@google.com> Reviewed-by: Rob Herring (Arm) <robh@kernel.org> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Stephen Boyd <sboyd@kernel.org> Link: https://lore.kernel.org/r/20240718210513.3801024-3-sboyd@kernel.org
2024-07-18 14:05:01 -07:00
#if defined(CONFIG_OF_OVERLAY) && defined(CONFIG_OF_EARLY_FLATTREE)
static void of_overlay_fdt_apply_kunit_exit(void *ovcs_id)
{
of_overlay_remove(ovcs_id);
}
/**
* of_overlay_fdt_apply_kunit() - Test managed of_overlay_fdt_apply()
* @test: test context
* @overlay_fdt: device tree overlay to apply
* @overlay_fdt_size: size in bytes of @overlay_fdt
* @ovcs_id: identifier of overlay, used to remove the overlay
*
* Just like of_overlay_fdt_apply(), except the overlay is managed by the test
* case and is automatically removed with of_overlay_remove() after the test
* case concludes.
*
* Return: 0 on success, negative errno on failure
*/
int of_overlay_fdt_apply_kunit(struct kunit *test, void *overlay_fdt,
u32 overlay_fdt_size, int *ovcs_id)
{
int ret;
int *copy_id;
of_root_kunit_skip(test);
of: Add test managed wrappers for of_overlay_apply()/of_node_put() Add test managed wrappers for of_overlay_apply() that automatically removes the overlay when the test is finished. This API is intended for use by KUnit tests that test code which relies on 'struct device_node's and of_*() APIs. KUnit tests will call of_overlay_apply_kunit() to load an overlay that's been built into the kernel image. When the test is complete, the overlay will be removed. This has a few benefits: 1) It keeps the tests hermetic because the overlay is removed when the test is complete. Tests won't even be aware that an overlay was loaded in another test. 2) The overlay code can live right next to the unit test that loads it. The overlay and the unit test can be compiled into one kernel module if desired. 3) We can test different device tree configurations by loading different overlays. The overlays can be written for a specific test, and there can be many of them loaded per-test without needing to jam all possible combinations into one DTB. 4) It also allows KUnit to test device tree dependent code on any architecture, not just UML. This allows KUnit tests to test architecture specific device tree code. There are some potential pitfalls though. Test authors need to be careful to not overwrite properties in the live tree. The easiest way to do this is to add and remove nodes with a 'kunit-' prefix, almost guaranteeing that the same node won't be present in the tree loaded at boot. Suggested-by: Rob Herring <robh@kernel.org> Cc: Rob Herring <robh@kernel.org> Cc: Saravana Kannan <saravanak@google.com> Reviewed-by: Rob Herring (Arm) <robh@kernel.org> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Stephen Boyd <sboyd@kernel.org> Link: https://lore.kernel.org/r/20240718210513.3801024-3-sboyd@kernel.org
2024-07-18 14:05:01 -07:00
copy_id = kunit_kmalloc(test, sizeof(*copy_id), GFP_KERNEL);
if (!copy_id)
return -ENOMEM;
ret = of_overlay_fdt_apply(overlay_fdt, overlay_fdt_size,
ovcs_id, NULL);
if (ret)
return ret;
*copy_id = *ovcs_id;
return kunit_add_action_or_reset(test, of_overlay_fdt_apply_kunit_exit,
copy_id);
}
EXPORT_SYMBOL_GPL(of_overlay_fdt_apply_kunit);
#endif
KUNIT_DEFINE_ACTION_WRAPPER(of_node_put_wrapper, of_node_put, struct device_node *);
/**
* of_node_put_kunit() - Test managed of_node_put()
* @test: test context
* @node: node to pass to `of_node_put()`
*
* Just like of_node_put(), except the node is managed by the test case and is
* automatically put with of_node_put() after the test case concludes.
*/
void of_node_put_kunit(struct kunit *test, struct device_node *node)
{
if (kunit_add_action(test, of_node_put_wrapper, node)) {
KUNIT_FAIL(test,
"Can't allocate a kunit resource to put of_node\n");
}
}
EXPORT_SYMBOL_GPL(of_node_put_kunit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Test managed DeviceTree APIs");