2024-06-06 23:26:20

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 1/1] regmap: kunit: Use array_size() and sizeof(*ptr) consistently

Some of the allocations use explit sizeof(type) instead of sizeof(*ptr),
which is fragile. In particular, stress_insert() allocates double
of memory without obvious need for a test. Convert all allocations
to use array_size() and sizeof(*ptr) to eliminate similar mistakes
or wrong memory sizes.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/base/regmap/regmap-kunit.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/base/regmap/regmap-kunit.c b/drivers/base/regmap/regmap-kunit.c
index 31df46cbcae7..9ec91b0de4a6 100644
--- a/drivers/base/regmap/regmap-kunit.c
+++ b/drivers/base/regmap/regmap-kunit.c
@@ -163,7 +163,7 @@ static struct regmap *gen_regmap(struct kunit *test,
config->max_register += (BLOCK_TEST_SIZE * config->reg_stride);
}

- size = (config->max_register + 1) * sizeof(unsigned int);
+ size = array_size(config->max_register + 1, sizeof(*buf));
buf = kmalloc(size, GFP_KERNEL);
if (!buf)
return ERR_PTR(-ENOMEM);
@@ -768,10 +768,9 @@ static void stress_insert(struct kunit *test)
if (IS_ERR(map))
return;

- vals = kunit_kcalloc(test, sizeof(unsigned long), config.max_register,
- GFP_KERNEL);
+ buf_sz = array_size(sizeof(*vals), config.max_register);
+ vals = kunit_kmalloc(test, buf_sz, GFP_KERNEL);
KUNIT_ASSERT_FALSE(test, vals == NULL);
- buf_sz = sizeof(unsigned long) * config.max_register;

get_random_bytes(vals, buf_sz);

@@ -1507,15 +1506,16 @@ static struct regmap *gen_raw_regmap(struct kunit *test,
const struct regmap_test_param *param = test->param_value;
u16 *buf;
struct regmap *ret = ERR_PTR(-ENOMEM);
- size_t size = (config->max_register + 1) * config->reg_bits / 8;
int i, error;
struct reg_default *defaults;
+ size_t size;

config->cache_type = param->cache;
config->val_format_endian = param->val_endian;
config->disable_locking = config->cache_type == REGCACHE_RBTREE ||
config->cache_type == REGCACHE_MAPLE;

+ size = array_size(config->max_register + 1, BITS_TO_BYTES(config->reg_bits));
buf = kmalloc(size, GFP_KERNEL);
if (!buf)
return ERR_PTR(-ENOMEM);
@@ -1615,7 +1615,7 @@ static void raw_read_defaults(struct kunit *test)
if (IS_ERR(map))
return;

- val_len = sizeof(*rval) * (config.max_register + 1);
+ val_len = array_size(sizeof(*rval), config.max_register + 1);
rval = kunit_kmalloc(test, val_len, GFP_KERNEL);
KUNIT_ASSERT_TRUE(test, rval != NULL);
if (!rval)
--
2.43.0.rc1.1336.g36b5255a03ac



2024-06-07 19:47:20

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] regmap: kunit: Use array_size() and sizeof(*ptr) consistently

On Thu, 06 Jun 2024 23:21:02 +0300, Andy Shevchenko wrote:
> Some of the allocations use explit sizeof(type) instead of sizeof(*ptr),
> which is fragile. In particular, stress_insert() allocates double
> of memory without obvious need for a test. Convert all allocations
> to use array_size() and sizeof(*ptr) to eliminate similar mistakes
> or wrong memory sizes.
>
>
> [...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-next

Thanks!

[1/1] regmap: kunit: Use array_size() and sizeof(*ptr) consistently
commit: f82ecf76cdd477c64b09f328aaa182c1dc64dd8b

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark