Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+38 -16
+38 -16
drivers/base/regmap/regmap-kunit.c
··· 9 9 10 10 #define BLOCK_TEST_SIZE 12 11 11 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 + 12 29 static const struct regmap_config test_regmap_config = { 13 30 .max_register = BLOCK_TEST_SIZE, 14 31 .reg_stride = 1, ··· 1269 1252 struct regmap *map; 1270 1253 struct regmap_config config; 1271 1254 struct regmap_ram_data *data; 1272 - u16 val[2]; 1255 + u16 val[3]; 1273 1256 u16 *hw_buf; 1274 1257 unsigned int rval; 1275 1258 int i; ··· 1283 1266 1284 1267 hw_buf = (u16 *)data->vals; 1285 1268 1286 - get_random_bytes(&val, sizeof(val)); 1269 + get_changed_bytes(&hw_buf[2], &val[0], sizeof(val)); 1287 1270 1288 1271 /* Do a regular write and a raw write in cache only mode */ 1289 1272 regcache_cache_only(map, true); 1290 - KUNIT_EXPECT_EQ(test, 0, regmap_raw_write(map, 2, val, sizeof(val))); 1291 - if (config.val_format_endian == REGMAP_ENDIAN_BIG) 1292 - KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 6, 1293 - be16_to_cpu(val[0]))); 1294 - else 1295 - KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 6, 1296 - le16_to_cpu(val[0]))); 1273 + KUNIT_EXPECT_EQ(test, 0, regmap_raw_write(map, 2, val, 1274 + sizeof(u16) * 2)); 1275 + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 4, val[2])); 1297 1276 1298 1277 /* We should read back the new values, and defaults for the rest */ 1299 1278 for (i = 0; i < config.max_register + 1; i++) { ··· 1298 1285 switch (i) { 1299 1286 case 2: 1300 1287 case 3: 1301 - case 6: 1302 1288 if (config.val_format_endian == REGMAP_ENDIAN_BIG) { 1303 1289 KUNIT_EXPECT_EQ(test, rval, 1304 - be16_to_cpu(val[i % 2])); 1290 + be16_to_cpu(val[i - 2])); 1305 1291 } else { 1306 1292 KUNIT_EXPECT_EQ(test, rval, 1307 - le16_to_cpu(val[i % 2])); 1293 + le16_to_cpu(val[i - 2])); 1308 1294 } 1295 + break; 1296 + case 4: 1297 + KUNIT_EXPECT_EQ(test, rval, val[i - 2]); 1309 1298 break; 1310 1299 default: 1311 1300 KUNIT_EXPECT_EQ(test, config.reg_defaults[i].def, rval); 1312 1301 break; 1313 1302 } 1314 1303 } 1304 + 1305 + /* 1306 + * The value written via _write() was translated by the core, 1307 + * translate the original copy for comparison purposes. 1308 + */ 1309 + if (config.val_format_endian == REGMAP_ENDIAN_BIG) 1310 + val[2] = cpu_to_be16(val[2]); 1311 + else 1312 + val[2] = cpu_to_le16(val[2]); 1315 1313 1316 1314 /* The values should not appear in the "hardware" */ 1317 - KUNIT_EXPECT_MEMNEQ(test, &hw_buf[2], val, sizeof(val)); 1318 - KUNIT_EXPECT_MEMNEQ(test, &hw_buf[6], val, sizeof(u16)); 1315 + KUNIT_EXPECT_MEMNEQ(test, &hw_buf[2], &val[0], sizeof(val)); 1319 1316 1320 1317 for (i = 0; i < config.max_register + 1; i++) 1321 1318 data->written[i] = false; ··· 1336 1313 KUNIT_EXPECT_EQ(test, 0, regcache_sync(map)); 1337 1314 1338 1315 /* The values should now appear in the "hardware" */ 1339 - KUNIT_EXPECT_MEMEQ(test, &hw_buf[2], val, sizeof(val)); 1340 - KUNIT_EXPECT_MEMEQ(test, &hw_buf[6], val, sizeof(u16)); 1316 + KUNIT_EXPECT_MEMEQ(test, &hw_buf[2], &val[0], sizeof(val)); 1341 1317 1342 1318 regmap_exit(map); 1343 1319 }