summaryrefslogtreecommitdiff
path: root/drivers/gpu/buddy.c
diff options
context:
space:
mode:
authorSanjay Yadav <sanjay.kumar.yadav@intel.com>2026-02-27 18:30:38 +0530
committerArunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>2026-03-03 13:37:42 +0530
commitabdcd4c3586dee8c190ac932cb544036b0c937b9 (patch)
tree0bdd2346eef090fe8e2cd8e6a4eefb04ffa64fc7 /drivers/gpu/buddy.c
parent01086b97dcc92799bd03cd243ed6061fd21db7a6 (diff)
downloadlinux-next-abdcd4c3586dee8c190ac932cb544036b0c937b9.tar.gz
linux-next-abdcd4c3586dee8c190ac932cb544036b0c937b9.zip
gpu/buddy: Introduce gpu_buddy_assert() for kunit-aware assertions
Introduce gpu_buddy_assert(), a small helper that wraps WARN_ON() and, when CONFIG_KUNIT is enabled, also calls kunit_fail_current_test() so that any active KUnit test is marked as failed. In non-KUnit builds the macro reduces to WARN_ON(), preserving existing behaviour. Stringify the asserted condition in the failure message to make it easy to identify which assertion fired. Leave the WARN_ON() in gpu_buddy_block_trim() unchanged, as it returns -EINVAL and the caller already observes the failure via the return code. Cc: Christian König <christian.koenig@amd.com> Cc: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com> Suggested-by: Matthew Auld <matthew.auld@intel.com> Signed-off-by: Sanjay Yadav <sanjay.kumar.yadav@intel.com> Reviewed-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com> Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com> Link: https://patch.msgid.link/20260227130037.53615-2-sanjay.kumar.yadav@intel.com
Diffstat (limited to 'drivers/gpu/buddy.c')
-rw-r--r--drivers/gpu/buddy.c36
1 files changed, 28 insertions, 8 deletions
diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
index b27761246d4b..da5a1222f46b 100644
--- a/drivers/gpu/buddy.c
+++ b/drivers/gpu/buddy.c
@@ -3,8 +3,7 @@
* Copyright © 2021 Intel Corporation
*/
-#include <kunit/test-bug.h>
-
+#include <linux/bug.h>
#include <linux/export.h>
#include <linux/kmemleak.h>
#include <linux/module.h>
@@ -12,6 +11,28 @@
#include <linux/gpu_buddy.h>
+/**
+ * gpu_buddy_assert - assert a condition in the buddy allocator
+ * @condition: condition expected to be true
+ *
+ * When CONFIG_KUNIT is enabled, evaluates @condition and, if false, triggers
+ * a WARN_ON() and also calls kunit_fail_current_test() so that any running
+ * kunit test is properly marked as failed. The stringified condition is
+ * included in the failure message for easy identification.
+ *
+ * When CONFIG_KUNIT is not enabled, this reduces to WARN_ON() so production
+ * builds retain the same warning semantics as before.
+ */
+#if IS_ENABLED(CONFIG_KUNIT)
+#include <kunit/test-bug.h>
+#define gpu_buddy_assert(condition) do { \
+ if (WARN_ON(!(condition))) \
+ kunit_fail_current_test("gpu_buddy_assert(" #condition ")"); \
+} while (0)
+#else
+#define gpu_buddy_assert(condition) WARN_ON(!(condition))
+#endif
+
static struct kmem_cache *slab_blocks;
static unsigned int
@@ -268,8 +289,8 @@ static int __force_merge(struct gpu_buddy *mm,
if (!gpu_buddy_block_is_free(buddy))
continue;
- WARN_ON(gpu_buddy_block_is_clear(block) ==
- gpu_buddy_block_is_clear(buddy));
+ gpu_buddy_assert(gpu_buddy_block_is_clear(block) !=
+ gpu_buddy_block_is_clear(buddy));
/*
* Advance to the next node when the current node is the buddy,
@@ -415,8 +436,7 @@ void gpu_buddy_fini(struct gpu_buddy *mm)
start = gpu_buddy_block_offset(mm->roots[i]);
__force_merge(mm, start, start + size, order);
- if (WARN_ON(!gpu_buddy_block_is_free(mm->roots[i])))
- kunit_fail_current_test("buddy_fini() root");
+ gpu_buddy_assert(gpu_buddy_block_is_free(mm->roots[i]));
gpu_block_free(mm, mm->roots[i]);
@@ -424,7 +444,7 @@ void gpu_buddy_fini(struct gpu_buddy *mm)
size -= root_size;
}
- WARN_ON(mm->avail != mm->size);
+ gpu_buddy_assert(mm->avail == mm->size);
for_each_free_tree(i)
kfree(mm->free_trees[i]);
@@ -541,7 +561,7 @@ static void __gpu_buddy_free_list(struct gpu_buddy *mm,
{
struct gpu_buddy_block *block, *on;
- WARN_ON(mark_dirty && mark_clear);
+ gpu_buddy_assert(!(mark_dirty && mark_clear));
list_for_each_entry_safe(block, on, objects, link) {
if (mark_clear)