Skip to content

Commit 42afe80

Browse files
committed
regmap: Specifically test writing 0 as a value to sparse caches
Since 0 can look a lot like a NULL pointer when used in a cache some clever data structures might potentially introduce bugs specific to handling it. Add some explicit testing of storing 0 as a value in a sparse cache, at the minute there are no issues and this will stop any appearing in the future. Signed-off-by: Mark Brown <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 21e9a1d commit 42afe80

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

drivers/base/regmap/regmap-kunit.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,48 @@ static void cache_present(struct kunit *test)
14991499
KUNIT_ASSERT_TRUE(test, regcache_reg_cached(map, param->from_reg + i));
15001500
}
15011501

1502+
static void cache_write_zero(struct kunit *test)
1503+
{
1504+
const struct regmap_test_param *param = test->param_value;
1505+
struct regmap *map;
1506+
struct regmap_config config;
1507+
struct regmap_ram_data *data;
1508+
unsigned int val;
1509+
int i;
1510+
1511+
config = test_regmap_config;
1512+
1513+
map = gen_regmap(test, &config, &data);
1514+
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
1515+
if (IS_ERR(map))
1516+
return;
1517+
1518+
for (i = 0; i < BLOCK_TEST_SIZE; i++)
1519+
data->read[param->from_reg + i] = false;
1520+
1521+
/* No defaults so no registers cached. */
1522+
for (i = 0; i < BLOCK_TEST_SIZE; i++)
1523+
KUNIT_ASSERT_FALSE(test, regcache_reg_cached(map, param->from_reg + i));
1524+
1525+
/* We didn't trigger any reads */
1526+
for (i = 0; i < BLOCK_TEST_SIZE; i++)
1527+
KUNIT_ASSERT_FALSE(test, data->read[param->from_reg + i]);
1528+
1529+
/* Write a zero value */
1530+
KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 1, 0));
1531+
1532+
/* Read that zero value back */
1533+
KUNIT_EXPECT_EQ(test, 0, regmap_read(map, 1, &val));
1534+
KUNIT_EXPECT_EQ(test, 0, val);
1535+
1536+
/* From the cache? */
1537+
KUNIT_ASSERT_TRUE(test, regcache_reg_cached(map, 1));
1538+
1539+
/* Try to throw it away */
1540+
KUNIT_EXPECT_EQ(test, 0, regcache_drop_region(map, 1, 1));
1541+
KUNIT_ASSERT_FALSE(test, regcache_reg_cached(map, 1));
1542+
}
1543+
15021544
/* Check that caching the window register works with sync */
15031545
static void cache_range_window_reg(struct kunit *test)
15041546
{
@@ -2012,6 +2054,7 @@ static struct kunit_case regmap_test_cases[] = {
20122054
KUNIT_CASE_PARAM(cache_drop_all_and_sync_no_defaults, sparse_cache_types_gen_params),
20132055
KUNIT_CASE_PARAM(cache_drop_all_and_sync_has_defaults, sparse_cache_types_gen_params),
20142056
KUNIT_CASE_PARAM(cache_present, sparse_cache_types_gen_params),
2057+
KUNIT_CASE_PARAM(cache_write_zero, sparse_cache_types_gen_params),
20152058
KUNIT_CASE_PARAM(cache_range_window_reg, real_cache_types_only_gen_params),
20162059

20172060
KUNIT_CASE_PARAM(raw_read_defaults_single, raw_test_types_gen_params),

0 commit comments

Comments
 (0)