drivers/i2c/i2c-atr.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)
Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.
Add __counted_by to get extra runtime analysis. Move counting variable
assignment immediately after allocation as required by __counted_by.
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/i2c/i2c-atr.c | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)
diff --git a/drivers/i2c/i2c-atr.c b/drivers/i2c/i2c-atr.c
index f9fcb4793aaf..b0aedcf0bf71 100644
--- a/drivers/i2c/i2c-atr.c
+++ b/drivers/i2c/i2c-atr.c
@@ -58,8 +58,8 @@ struct i2c_atr_alias_pool {
/* Protects aliases and use_mask */
spinlock_t lock;
- u16 *aliases;
unsigned long *use_mask;
+ u16 aliases[] __counted_by(size);
};
/**
@@ -137,22 +137,16 @@ static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_aliases, b
struct i2c_atr_alias_pool *alias_pool;
int ret;
- alias_pool = kzalloc_obj(*alias_pool);
+ alias_pool = kzalloc_flex(*alias_pool, aliases, num_aliases);
if (!alias_pool)
return ERR_PTR(-ENOMEM);
alias_pool->size = num_aliases;
- alias_pool->aliases = kcalloc(num_aliases, sizeof(*alias_pool->aliases), GFP_KERNEL);
- if (!alias_pool->aliases) {
- ret = -ENOMEM;
- goto err_free_alias_pool;
- }
-
alias_pool->use_mask = bitmap_zalloc(num_aliases, GFP_KERNEL);
if (!alias_pool->use_mask) {
ret = -ENOMEM;
- goto err_free_aliases;
+ goto err_free_alias_pool;
}
alias_pool->shared = shared;
@@ -161,8 +155,6 @@ static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_aliases, b
return alias_pool;
-err_free_aliases:
- kfree(alias_pool->aliases);
err_free_alias_pool:
kfree(alias_pool);
return ERR_PTR(ret);
@@ -171,7 +163,6 @@ static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_aliases, b
static void i2c_atr_free_alias_pool(struct i2c_atr_alias_pool *alias_pool)
{
bitmap_free(alias_pool->use_mask);
- kfree(alias_pool->aliases);
kfree(alias_pool);
}
--
2.53.0
Hello Rosen,
On Friday, 13 March 2026 01:23:08 CEST Rosen Penev wrote:
> Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.
>
> Add __counted_by to get extra runtime analysis. Move counting variable
> assignment immediately after allocation as required by __counted_by.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>
> drivers/i2c/i2c-atr.c | 15 +++------------
> 1 file changed, 3 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/i2c/i2c-atr.c b/drivers/i2c/i2c-atr.c
> index f9fcb4793aaf..b0aedcf0bf71 100644
> --- a/drivers/i2c/i2c-atr.c
> +++ b/drivers/i2c/i2c-atr.c
> @@ -58,8 +58,8 @@ struct i2c_atr_alias_pool {
>
> /* Protects aliases and use_mask */
> spinlock_t lock;
>
> - u16 *aliases;
>
> unsigned long *use_mask;
>
> + u16 aliases[] __counted_by(size);
>
> };
>
> /**
>
> @@ -137,22 +137,16 @@ static struct i2c_atr_alias_pool
> *i2c_atr_alloc_alias_pool(size_t num_aliases, b struct i2c_atr_alias_pool
> *alias_pool;
>
> int ret;
>
> - alias_pool = kzalloc_obj(*alias_pool);
> + alias_pool = kzalloc_flex(*alias_pool, aliases, num_aliases);
>
> if (!alias_pool)
>
> return ERR_PTR(-ENOMEM);
>
> alias_pool->size = num_aliases;
>
> - alias_pool->aliases = kcalloc(num_aliases, sizeof(*alias_pool->aliases),
> GFP_KERNEL); - if (!alias_pool->aliases) {
> - ret = -ENOMEM;
> - goto err_free_alias_pool;
> - }
> -
>
> alias_pool->use_mask = bitmap_zalloc(num_aliases, GFP_KERNEL);
> if (!alias_pool->use_mask) {
>
> ret = -ENOMEM;
>
> - goto err_free_aliases;
> + goto err_free_alias_pool;
>
> }
>
> alias_pool->shared = shared;
>
> @@ -161,8 +155,6 @@ static struct i2c_atr_alias_pool
> *i2c_atr_alloc_alias_pool(size_t num_aliases, b
>
> return alias_pool;
>
> -err_free_aliases:
> - kfree(alias_pool->aliases);
>
> err_free_alias_pool:
> kfree(alias_pool);
> return ERR_PTR(ret);
>
> @@ -171,7 +163,6 @@ static struct i2c_atr_alias_pool
> *i2c_atr_alloc_alias_pool(size_t num_aliases, b static void
> i2c_atr_free_alias_pool(struct i2c_atr_alias_pool *alias_pool) {
>
> bitmap_free(alias_pool->use_mask);
>
> - kfree(alias_pool->aliases);
>
> kfree(alias_pool);
>
> }
Reviewed-by: Romain Gantois <romain.gantois@bootlin.com>
Hello Rosen,
On Fri Mar 13, 2026 at 1:23 AM CET, Rosen Penev wrote:
> Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.
>
> Add __counted_by to get extra runtime analysis. Move counting variable
> assignment immediately after allocation as required by __counted_by.
Not sure what you mean by the last sentence, you are not moving the
assignment.
> --- a/drivers/i2c/i2c-atr.c
> +++ b/drivers/i2c/i2c-atr.c
> @@ -58,8 +58,8 @@ struct i2c_atr_alias_pool {
>
> /* Protects aliases and use_mask */
> spinlock_t lock;
> - u16 *aliases;
> unsigned long *use_mask;
> + u16 aliases[] __counted_by(size);
Please reorder the parameters accordingly in the kdoc documentation above.
> @@ -137,22 +137,16 @@ static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_aliases, b
> struct i2c_atr_alias_pool *alias_pool;
> int ret;
>
> - alias_pool = kzalloc_obj(*alias_pool);
> + alias_pool = kzalloc_flex(*alias_pool, aliases, num_aliases);
> if (!alias_pool)
> return ERR_PTR(-ENOMEM);
>
> alias_pool->size = num_aliases;
I'm not really sure about that, but I suspect kzalloc_flex() does set the
counter variable automatically if it's annotated using __counted_by. If
that's true, you can drop this line.
Otherwise LGTM.
Luca
--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
On Fri, Mar 13, 2026 at 1:23 AM Luca Ceresoli <luca.ceresoli@bootlin.com> wrote:
>
> Hello Rosen,
>
> On Fri Mar 13, 2026 at 1:23 AM CET, Rosen Penev wrote:
> > Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.
> >
> > Add __counted_by to get extra runtime analysis. Move counting variable
> > assignment immediately after allocation as required by __counted_by.
>
> Not sure what you mean by the last sentence, you are not moving the
> assignment.
Yeah, will remove.
>
> > --- a/drivers/i2c/i2c-atr.c
> > +++ b/drivers/i2c/i2c-atr.c
> > @@ -58,8 +58,8 @@ struct i2c_atr_alias_pool {
> >
> > /* Protects aliases and use_mask */
> > spinlock_t lock;
> > - u16 *aliases;
> > unsigned long *use_mask;
> > + u16 aliases[] __counted_by(size);
>
> Please reorder the parameters accordingly in the kdoc documentation above.
Will do.
>
> > @@ -137,22 +137,16 @@ static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_aliases, b
> > struct i2c_atr_alias_pool *alias_pool;
> > int ret;
> >
> > - alias_pool = kzalloc_obj(*alias_pool);
> > + alias_pool = kzalloc_flex(*alias_pool, aliases, num_aliases);
> > if (!alias_pool)
> > return ERR_PTR(-ENOMEM);
> >
> > alias_pool->size = num_aliases;
>
> I'm not really sure about that, but I suspect kzalloc_flex() does set the
> counter variable automatically if it's annotated using __counted_by. If
> that's true, you can drop this line.
It's automatic with GCC15+.
>
> Otherwise LGTM.
>
> Luca
>
> --
> Luca Ceresoli, Bootlin
> Embedded Linux and Kernel engineering
> https://bootlin.com
© 2016 - 2026 Red Hat, Inc.