Skip to content

Commit 2c46083

Browse files
committed
Merge tag 'regmap-fix-v6.8-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap
Pull regmap test fixes from Mark Brown: "Guenter runs a lot of KUnit tests so noticed that there were a couple of the regmap tests, including the newly added noinc test, which could show spurious failures due to the use of randomly generated test values. These changes handle the randomly generated data properly" * tag 'regmap-fix-v6.8-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap: regmap: kunit: Ensure that changed bytes are actually different regmap: kunit: fix raw noinc write test wrapping
2 parents 9207fe7 + 2f0dbb2 commit 2c46083

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

drivers/base/regmap/regmap-kunit.c

Lines changed: 40 additions & 17 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,
@@ -1202,7 +1219,8 @@ static void raw_noinc_write(struct kunit *test)
12021219
struct regmap *map;
12031220
struct regmap_config config;
12041221
struct regmap_ram_data *data;
1205-
unsigned int val, val_test, val_last;
1222+
unsigned int val;
1223+
u16 val_test, val_last;
12061224
u16 val_array[BLOCK_TEST_SIZE];
12071225

12081226
config = raw_regmap_config;
@@ -1251,7 +1269,7 @@ static void raw_sync(struct kunit *test)
12511269
struct regmap *map;
12521270
struct regmap_config config;
12531271
struct regmap_ram_data *data;
1254-
u16 val[2];
1272+
u16 val[3];
12551273
u16 *hw_buf;
12561274
unsigned int rval;
12571275
int i;
@@ -1265,17 +1283,13 @@ static void raw_sync(struct kunit *test)
12651283

12661284
hw_buf = (u16 *)data->vals;
12671285

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

12701288
/* Do a regular write and a raw write in cache only mode */
12711289
regcache_cache_only(map, true);
1272-
KUNIT_EXPECT_EQ(test, 0, regmap_raw_write(map, 2, val, sizeof(val)));
1273-
if (config.val_format_endian == REGMAP_ENDIAN_BIG)
1274-
KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 6,
1275-
be16_to_cpu(val[0])));
1276-
else
1277-
KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 6,
1278-
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]));
12791293

12801294
/* We should read back the new values, and defaults for the rest */
12811295
for (i = 0; i < config.max_register + 1; i++) {
@@ -1284,24 +1298,34 @@ static void raw_sync(struct kunit *test)
12841298
switch (i) {
12851299
case 2:
12861300
case 3:
1287-
case 6:
12881301
if (config.val_format_endian == REGMAP_ENDIAN_BIG) {
12891302
KUNIT_EXPECT_EQ(test, rval,
1290-
be16_to_cpu(val[i % 2]));
1303+
be16_to_cpu(val[i - 2]));
12911304
} else {
12921305
KUNIT_EXPECT_EQ(test, rval,
1293-
le16_to_cpu(val[i % 2]));
1306+
le16_to_cpu(val[i - 2]));
12941307
}
12951308
break;
1309+
case 4:
1310+
KUNIT_EXPECT_EQ(test, rval, val[i - 2]);
1311+
break;
12961312
default:
12971313
KUNIT_EXPECT_EQ(test, config.reg_defaults[i].def, rval);
12981314
break;
12991315
}
13001316
}
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]);
13011326

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

13061330
for (i = 0; i < config.max_register + 1; i++)
13071331
data->written[i] = false;
@@ -1312,8 +1336,7 @@ static void raw_sync(struct kunit *test)
13121336
KUNIT_EXPECT_EQ(test, 0, regcache_sync(map));
13131337

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

13181341
regmap_exit(map);
13191342
}

0 commit comments

Comments
 (0)