[PATCH 08/11] regmap: kunit: Use a KUnit action to call regmap_exit()

From: Richard Fitzgerald
Date: Mon Apr 08 2024 - 10:47:22 EST


Registert a KUnit action handler to call regmap_exit() when a test
terminates. This ensures that regmap_exit() will be called if a test
function returns early or aborts.

Signed-off-by: Richard Fitzgerald <rf@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/base/regmap/regmap-kunit.c | 62 ++++--------------------------
1 file changed, 7 insertions(+), 55 deletions(-)

diff --git a/drivers/base/regmap/regmap-kunit.c b/drivers/base/regmap/regmap-kunit.c
index 1b34f92b1aaf..e21028cbddc3 100644
--- a/drivers/base/regmap/regmap-kunit.c
+++ b/drivers/base/regmap/regmap-kunit.c
@@ -5,11 +5,14 @@
// Copyright 2023 Arm Ltd

#include <kunit/device.h>
+#include <kunit/resource.h>
#include <kunit/test.h>
#include "internal.h"

#define BLOCK_TEST_SIZE 12

+KUNIT_DEFINE_ACTION_WRAPPER(regmap_exit_action, regmap_exit, struct regmap *);
+
struct regmap_test_priv {
struct device *dev;
};
@@ -190,6 +193,8 @@ static struct regmap *gen_regmap(struct kunit *test,
if (IS_ERR(ret)) {
kfree(buf);
kfree(*data);
+ } else {
+ kunit_add_action(test, regmap_exit_action, ret);
}

return ret;
@@ -226,8 +231,6 @@ static void basic_read_write(struct kunit *test)

/* If using a cache the cache satisfied the read */
KUNIT_EXPECT_EQ(test, config.cache_type == REGCACHE_NONE, data->read[0]);
-
- regmap_exit(map);
}

static void bulk_write(struct kunit *test)
@@ -261,8 +264,6 @@ static void bulk_write(struct kunit *test)
/* If using a cache the cache satisfied the read */
for (i = 0; i < BLOCK_TEST_SIZE; i++)
KUNIT_EXPECT_EQ(test, config.cache_type == REGCACHE_NONE, data->read[i]);
-
- regmap_exit(map);
}

static void bulk_read(struct kunit *test)
@@ -292,8 +293,6 @@ static void bulk_read(struct kunit *test)
/* If using a cache the cache satisfied the read */
for (i = 0; i < BLOCK_TEST_SIZE; i++)
KUNIT_EXPECT_EQ(test, config.cache_type == REGCACHE_NONE, data->read[i]);
-
- regmap_exit(map);
}

static void write_readonly(struct kunit *test)
@@ -325,8 +324,6 @@ static void write_readonly(struct kunit *test)
/* Did that match what we see on the device? */
for (i = 0; i < BLOCK_TEST_SIZE; i++)
KUNIT_EXPECT_EQ(test, i != 5, data->written[i]);
-
- regmap_exit(map);
}

static void read_writeonly(struct kunit *test)
@@ -363,8 +360,6 @@ static void read_writeonly(struct kunit *test)

/* Did we trigger a hardware access? */
KUNIT_EXPECT_FALSE(test, data->read[5]);
-
- regmap_exit(map);
}

static void reg_defaults(struct kunit *test)
@@ -471,8 +466,6 @@ static void register_patch(struct kunit *test)
break;
}
}
-
- regmap_exit(map);
}

static void stride(struct kunit *test)
@@ -512,8 +505,6 @@ static void stride(struct kunit *test)
KUNIT_EXPECT_TRUE(test, data->written[i]);
}
}
-
- regmap_exit(map);
}

static struct regmap_range_cfg test_range = {
@@ -612,8 +603,6 @@ static void basic_ranges(struct kunit *test)
KUNIT_EXPECT_FALSE(test, data->read[i]);
KUNIT_EXPECT_FALSE(test, data->written[i]);
}
-
- regmap_exit(map);
}

/* Try to stress dynamic creation of cache data structures */
@@ -665,8 +654,6 @@ static void stress_insert(struct kunit *test)
KUNIT_EXPECT_EQ(test, rval, vals[i]);
KUNIT_EXPECT_EQ(test, config.cache_type == REGCACHE_NONE, data->read[i]);
}
-
- regmap_exit(map);
}

static void cache_bypass(struct kunit *test)
@@ -702,8 +689,6 @@ static void cache_bypass(struct kunit *test)
regcache_cache_bypass(map, false);
KUNIT_EXPECT_EQ(test, 0, regmap_read(map, param->from_reg, &rval));
KUNIT_EXPECT_EQ(test, val, rval);
-
- regmap_exit(map);
}

static void cache_sync_marked_dirty(struct kunit *test)
@@ -739,8 +724,6 @@ static void cache_sync_marked_dirty(struct kunit *test)
KUNIT_EXPECT_MEMEQ(test, &data->vals[param->from_reg], val, sizeof(val));
for (i = 0; i < BLOCK_TEST_SIZE; i++)
KUNIT_EXPECT_EQ(test, true, data->written[param->from_reg + i]);
-
- regmap_exit(map);
}

static void cache_sync_after_cache_only(struct kunit *test)
@@ -790,8 +773,6 @@ static void cache_sync_after_cache_only(struct kunit *test)
KUNIT_EXPECT_MEMEQ(test, &data->vals[param->from_reg], val, sizeof(val));
for (i = 0; i < BLOCK_TEST_SIZE; i++)
KUNIT_EXPECT_TRUE(test, data->written[param->from_reg + i]);
-
- regmap_exit(map);
}

static void cache_sync_defaults_marked_dirty(struct kunit *test)
@@ -841,8 +822,6 @@ static void cache_sync_defaults_marked_dirty(struct kunit *test)
KUNIT_EXPECT_EQ(test, 0, regcache_sync(map));
for (i = 0; i < BLOCK_TEST_SIZE; i++)
KUNIT_EXPECT_FALSE(test, data->written[param->from_reg + i]);
-
- regmap_exit(map);
}

static void cache_sync_default_after_cache_only(struct kunit *test)
@@ -890,8 +869,6 @@ static void cache_sync_default_after_cache_only(struct kunit *test)
KUNIT_EXPECT_EQ(test, 0, regcache_sync(map));
KUNIT_EXPECT_TRUE(test, data->written[param->from_reg + 2]);
KUNIT_EXPECT_EQ(test, data->vals[param->from_reg + 2], orig_val);
-
- regmap_exit(map);
}

static void cache_sync_readonly(struct kunit *test)
@@ -930,8 +907,6 @@ static void cache_sync_readonly(struct kunit *test)
/* Did that match what we see on the device? */
for (i = 0; i < BLOCK_TEST_SIZE; i++)
KUNIT_EXPECT_EQ(test, i != 5, data->written[param->from_reg + i]);
-
- regmap_exit(map);
}

static void cache_sync_patch(struct kunit *test)
@@ -990,8 +965,6 @@ static void cache_sync_patch(struct kunit *test)
break;
}
}
-
- regmap_exit(map);
}

static void cache_drop(struct kunit *test)
@@ -1032,8 +1005,6 @@ static void cache_drop(struct kunit *test)
for (i = 0; i < BLOCK_TEST_SIZE; i++)
KUNIT_EXPECT_EQ(test, data->read[param->from_reg + i], i >= 3 && i <= 5);
KUNIT_EXPECT_MEMEQ(test, &data->vals[param->from_reg], rval, sizeof(rval));
-
- regmap_exit(map);
}

static void cache_drop_all_and_sync_marked_dirty(struct kunit *test)
@@ -1075,8 +1046,6 @@ static void cache_drop_all_and_sync_marked_dirty(struct kunit *test)
KUNIT_EXPECT_EQ(test, 0, regcache_sync(map));
for (i = 0; i <= config.max_register; i++)
KUNIT_EXPECT_FALSE(test, data->written[i]);
-
- regmap_exit(map);
}

static void cache_drop_all_and_sync_no_defaults(struct kunit *test)
@@ -1119,8 +1088,6 @@ static void cache_drop_all_and_sync_no_defaults(struct kunit *test)
KUNIT_EXPECT_EQ(test, 0, regcache_sync(map));
for (i = 0; i <= config.max_register; i++)
KUNIT_EXPECT_FALSE(test, data->written[i]);
-
- regmap_exit(map);
}

static void cache_drop_all_and_sync_has_defaults(struct kunit *test)
@@ -1164,8 +1131,6 @@ static void cache_drop_all_and_sync_has_defaults(struct kunit *test)
KUNIT_EXPECT_EQ(test, 0, regcache_sync(map));
for (i = 0; i <= config.max_register; i++)
KUNIT_EXPECT_FALSE(test, data->written[i]);
-
- regmap_exit(map);
}

static void cache_present(struct kunit *test)
@@ -1202,8 +1167,6 @@ static void cache_present(struct kunit *test)
/* Now everything should be cached */
for (i = 0; i < BLOCK_TEST_SIZE; i++)
KUNIT_ASSERT_TRUE(test, regcache_reg_cached(map, param->from_reg + i));
-
- regmap_exit(map);
}

/* Check that caching the window register works with sync */
@@ -1350,6 +1313,8 @@ static struct regmap *gen_raw_regmap(struct kunit *test,
if (IS_ERR(ret)) {
kfree(buf);
kfree(*data);
+ } else {
+ kunit_add_action(test, regmap_exit_action, ret);
}

return ret;
@@ -1375,8 +1340,6 @@ static void raw_read_defaults_single(struct kunit *test)
KUNIT_EXPECT_EQ(test, 0, regmap_read(map, i, &rval));
KUNIT_EXPECT_EQ(test, config.reg_defaults[i].def, rval);
}
-
- regmap_exit(map);
}

static void raw_read_defaults(struct kunit *test)
@@ -1414,7 +1377,6 @@ static void raw_read_defaults(struct kunit *test)
}

kfree(rval);
- regmap_exit(map);
}

static void raw_write_read_single(struct kunit *test)
@@ -1438,8 +1400,6 @@ static void raw_write_read_single(struct kunit *test)
KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 0, val));
KUNIT_EXPECT_EQ(test, 0, regmap_read(map, 0, &rval));
KUNIT_EXPECT_EQ(test, val, rval);
-
- regmap_exit(map);
}

static void raw_write(struct kunit *test)
@@ -1489,8 +1449,6 @@ static void raw_write(struct kunit *test)

/* The values should appear in the "hardware" */
KUNIT_EXPECT_MEMEQ(test, &hw_buf[2], val, sizeof(val));
-
- regmap_exit(map);
}

static bool reg_zero(struct device *dev, unsigned int reg)
@@ -1548,8 +1506,6 @@ static void raw_noinc_write(struct kunit *test)
/* Make sure we didn't touch the register after the noinc register */
KUNIT_EXPECT_EQ(test, 0, regmap_read(map, 1, &val));
KUNIT_ASSERT_EQ(test, val_test, val);
-
- regmap_exit(map);
}

static void raw_sync(struct kunit *test)
@@ -1625,8 +1581,6 @@ static void raw_sync(struct kunit *test)

/* The values should now appear in the "hardware" */
KUNIT_EXPECT_MEMEQ(test, &hw_buf[2], &val[0], sizeof(val));
-
- regmap_exit(map);
}

static void raw_ranges(struct kunit *test)
@@ -1689,8 +1643,6 @@ static void raw_ranges(struct kunit *test)
KUNIT_EXPECT_FALSE(test, data->read[i]);
KUNIT_EXPECT_FALSE(test, data->written[i]);
}
-
- regmap_exit(map);
}

static struct kunit_case regmap_test_cases[] = {
--
2.39.2