Skip to content

Commit 2f0dbb2

Browse files
committed
regmap: kunit: Ensure that changed bytes are actually different
During the cache sync test we verify that values we expect to have been written only to the cache do not appear in the hardware. This works most of the time but since we randomly generate both the original and new values there is a low probability that these values may actually be the same. Wrap get_random_bytes() to ensure that the values are different, there are other tests which should have similar verification that we actually changed something. While we're at it refactor the test to use three changed values rather than attempting to use one of them twice, that just complicates checking that our new values are actually new. We use random generation to try to avoid data dependencies in the tests. Reported-by: Guenter Roeck <linux@roeck-us.net> Reviewed-by: Guenter Roeck <linux@roeck-us.net> Tested-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Mark Brown <broonie@kernel.org> Link: https://msgid.link/r/20240211-regmap-kunit-random-change-v3-1-e387a9ea4468@kernel.org Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 7011b51 commit 2f0dbb2

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

drivers/base/regmap/regmap-kunit.c

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@
99

1010
#define BLOCK_TEST_SIZE 12
1111

12+
static void get_changed_bytes(void *orig, void *new, size_t size)
13+
{
14+
char *o = orig;
15+
char *n = new;
16+
int i;
17+
18+
get_random_bytes(new, size);
19+
20+
/*
21+
* This could be nicer and more efficient but we shouldn't
22+
* super care.
23+
*/
24+
for (i = 0; i < size; i++)
25+
while (n[i] == o[i])
26+
get_random_bytes(&n[i], 1);
27+
}
28+
1229
static const struct regmap_config test_regmap_config = {
1330
.max_register = BLOCK_TEST_SIZE,
1431
.reg_stride = 1,
@@ -1252,7 +1269,7 @@ static void raw_sync(struct kunit *test)
12521269
struct regmap *map;
12531270
struct regmap_config config;
12541271
struct regmap_ram_data *data;
1255-
u16 val[2];
1272+
u16 val[3];
12561273
u16 *hw_buf;
12571274
unsigned int rval;
12581275
int i;
@@ -1266,17 +1283,13 @@ static void raw_sync(struct kunit *test)
12661283

12671284
hw_buf = (u16 *)data->vals;
12681285

1269-
get_random_bytes(&val, sizeof(val));
1286+
get_changed_bytes(&hw_buf[2], &val[0], sizeof(val));
12701287

12711288
/* Do a regular write and a raw write in cache only mode */
12721289
regcache_cache_only(map, true);
1273-
KUNIT_EXPECT_EQ(test, 0, regmap_raw_write(map, 2, val, sizeof(val)));
1274-
if (config.val_format_endian == REGMAP_ENDIAN_BIG)
1275-
KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 6,
1276-
be16_to_cpu(val[0])));
1277-
else
1278-
KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 6,
1279-
le16_to_cpu(val[0])));
1290+
KUNIT_EXPECT_EQ(test, 0, regmap_raw_write(map, 2, val,
1291+
sizeof(u16) * 2));
1292+
KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 4, val[2]));
12801293

12811294
/* We should read back the new values, and defaults for the rest */
12821295
for (i = 0; i < config.max_register + 1; i++) {
@@ -1285,24 +1298,34 @@ static void raw_sync(struct kunit *test)
12851298
switch (i) {
12861299
case 2:
12871300
case 3:
1288-
case 6:
12891301
if (config.val_format_endian == REGMAP_ENDIAN_BIG) {
12901302
KUNIT_EXPECT_EQ(test, rval,
1291-
be16_to_cpu(val[i % 2]));
1303+
be16_to_cpu(val[i - 2]));
12921304
} else {
12931305
KUNIT_EXPECT_EQ(test, rval,
1294-
le16_to_cpu(val[i % 2]));
1306+
le16_to_cpu(val[i - 2]));
12951307
}
12961308
break;
1309+
case 4:
1310+
KUNIT_EXPECT_EQ(test, rval, val[i - 2]);
1311+
break;
12971312
default:
12981313
KUNIT_EXPECT_EQ(test, config.reg_defaults[i].def, rval);
12991314
break;
13001315
}
13011316
}
1317+
1318+
/*
1319+
* The value written via _write() was translated by the core,
1320+
* translate the original copy for comparison purposes.
1321+
*/
1322+
if (config.val_format_endian == REGMAP_ENDIAN_BIG)
1323+
val[2] = cpu_to_be16(val[2]);
1324+
else
1325+
val[2] = cpu_to_le16(val[2]);
13021326

13031327
/* The values should not appear in the "hardware" */
1304-
KUNIT_EXPECT_MEMNEQ(test, &hw_buf[2], val, sizeof(val));
1305-
KUNIT_EXPECT_MEMNEQ(test, &hw_buf[6], val, sizeof(u16));
1328+
KUNIT_EXPECT_MEMNEQ(test, &hw_buf[2], &val[0], sizeof(val));
13061329

13071330
for (i = 0; i < config.max_register + 1; i++)
13081331
data->written[i] = false;
@@ -1313,8 +1336,7 @@ static void raw_sync(struct kunit *test)
13131336
KUNIT_EXPECT_EQ(test, 0, regcache_sync(map));
13141337

13151338
/* The values should now appear in the "hardware" */
1316-
KUNIT_EXPECT_MEMEQ(test, &hw_buf[2], val, sizeof(val));
1317-
KUNIT_EXPECT_MEMEQ(test, &hw_buf[6], val, sizeof(u16));
1339+
KUNIT_EXPECT_MEMEQ(test, &hw_buf[2], &val[0], sizeof(val));
13181340

13191341
regmap_exit(map);
13201342
}

0 commit comments

Comments
 (0)