[PATCH v2 3/5] regcache: flat: Remove unneeded check and error message for -ENOMEM

Andy Shevchenko posted 5 patches 3 months, 1 week ago
There is a newer version of this series
[PATCH v2 3/5] regcache: flat: Remove unneeded check and error message for -ENOMEM
Posted by Andy Shevchenko 3 months, 1 week ago
There is a convention in the kernel to avoid error messages
in the cases of -ENOMEM errors. Besides that, the idea behind
using struct_size() and other macros from overflow.h is
to saturate the size that the following allocation call will
definitely fail, hence the check and the error messaging added
in regcache_flat_init() are redundant. Remove them.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/base/regmap/regcache-flat.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/drivers/base/regmap/regcache-flat.c b/drivers/base/regmap/regcache-flat.c
index 3b9235bb8313..bacb7137092f 100644
--- a/drivers/base/regmap/regcache-flat.c
+++ b/drivers/base/regmap/regcache-flat.c
@@ -30,7 +30,6 @@ struct regcache_flat_data {
 static int regcache_flat_init(struct regmap *map)
 {
 	int i;
-	size_t cache_data_size;
 	unsigned int cache_size;
 	struct regcache_flat_data *cache;
 
@@ -38,14 +37,7 @@ static int regcache_flat_init(struct regmap *map)
 		return -EINVAL;
 
 	cache_size = regcache_flat_get_index(map, map->max_register) + 1;
-	cache_data_size = struct_size(cache, data, cache_size);
-
-	if (cache_data_size == SIZE_MAX) {
-		dev_err(map->dev, "cannot allocate regmap cache");
-		return -ENOMEM;
-	}
-
-	cache = kzalloc(cache_data_size, map->alloc_flags);
+	cache = kzalloc(struct_size(cache, data, cache_size), map->alloc_flags);
 	if (!cache)
 		return -ENOMEM;
 
-- 
2.50.1
Re: [PATCH v2 3/5] regcache: flat: Remove unneeded check and error message for -ENOMEM
Posted by Sander Vanheule 3 months, 1 week ago
Hi Andy,

On Thu, 2025-10-30 at 18:37 +0100, Andy Shevchenko wrote:
> There is a convention in the kernel to avoid error messages
> in the cases of -ENOMEM errors. Besides that, the idea behind
> using struct_size() and other macros from overflow.h is
> to saturate the size that the following allocation call will
> definitely fail, hence the check and the error messaging added
> in regcache_flat_init() are redundant. Remove them.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---

Makes sense, although I couldn't find the failure path myself in the code (it's probably
too deep down in the memory management code). But I see now there are unit tests that
check allocation failure for overflowed sizes.

FWIW

Acked-by: Sander Vanheule <sander@svanheule.net>

Best,
Sander

>  drivers/base/regmap/regcache-flat.c | 10 +---------
>  1 file changed, 1 insertion(+), 9 deletions(-)
> 
> diff --git a/drivers/base/regmap/regcache-flat.c b/drivers/base/regmap/regcache-flat.c
> index 3b9235bb8313..bacb7137092f 100644
> --- a/drivers/base/regmap/regcache-flat.c
> +++ b/drivers/base/regmap/regcache-flat.c
> @@ -30,7 +30,6 @@ struct regcache_flat_data {
>  static int regcache_flat_init(struct regmap *map)
>  {
>  	int i;
> -	size_t cache_data_size;
>  	unsigned int cache_size;
>  	struct regcache_flat_data *cache;
>  
> @@ -38,14 +37,7 @@ static int regcache_flat_init(struct regmap *map)
>  		return -EINVAL;
>  
>  	cache_size = regcache_flat_get_index(map, map->max_register) + 1;
> -	cache_data_size = struct_size(cache, data, cache_size);
> -
> -	if (cache_data_size == SIZE_MAX) {
> -		dev_err(map->dev, "cannot allocate regmap cache");
> -		return -ENOMEM;
> -	}
> -
> -	cache = kzalloc(cache_data_size, map->alloc_flags);
> +	cache = kzalloc(struct_size(cache, data, cache_size), map->alloc_flags);
>  	if (!cache)
>  		return -ENOMEM;
>  
Re: [PATCH v2 3/5] regcache: flat: Remove unneeded check and error message for -ENOMEM
Posted by Andy Shevchenko 3 months, 1 week ago
On Thu, Oct 30, 2025 at 09:18:11PM +0100, Sander Vanheule wrote:
> On Thu, 2025-10-30 at 18:37 +0100, Andy Shevchenko wrote:
> > There is a convention in the kernel to avoid error messages
> > in the cases of -ENOMEM errors. Besides that, the idea behind
> > using struct_size() and other macros from overflow.h is
> > to saturate the size that the following allocation call will
> > definitely fail, hence the check and the error messaging added
> > in regcache_flat_init() are redundant. Remove them.

> Makes sense, although I couldn't find the failure path myself in the code (it's probably
> too deep down in the memory management code). But I see now there are unit tests that
> check allocation failure for overflowed sizes.
> 
> FWIW
> 
> Acked-by: Sander Vanheule <sander@svanheule.net>

Thanks for looking into this.

-- 
With Best Regards,
Andy Shevchenko