public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] i2c: atr: use kzalloc_flex
@ 2026-03-13  0:23 Rosen Penev
  2026-03-13  8:17 ` Luca Ceresoli
  2026-03-30  8:50 ` Romain Gantois
  0 siblings, 2 replies; 7+ messages in thread
From: Rosen Penev @ 2026-03-13  0:23 UTC (permalink / raw)
  To: linux-i2c
  Cc: Wolfram Sang, Tomi Valkeinen, Luca Ceresoli, Kees Cook,
	Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] i2c: atr: use kzalloc_flex
  2026-03-13  0:23 Rosen Penev
@ 2026-03-13  8:17 ` Luca Ceresoli
  2026-03-13 21:48   ` Rosen Penev
  2026-03-30  8:50 ` Romain Gantois
  1 sibling, 1 reply; 7+ messages in thread
From: Luca Ceresoli @ 2026-03-13  8:17 UTC (permalink / raw)
  To: Rosen Penev, linux-i2c
  Cc: Wolfram Sang, Tomi Valkeinen, Kees Cook, Gustavo A. R. Silva,
	open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] i2c: atr: use kzalloc_flex
  2026-03-13  8:17 ` Luca Ceresoli
@ 2026-03-13 21:48   ` Rosen Penev
  0 siblings, 0 replies; 7+ messages in thread
From: Rosen Penev @ 2026-03-13 21:48 UTC (permalink / raw)
  To: Luca Ceresoli
  Cc: linux-i2c, Wolfram Sang, Tomi Valkeinen, Kees Cook,
	Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] i2c: atr: use kzalloc_flex
@ 2026-03-27  3:03 Rosen Penev
  2026-03-27  8:12 ` Luca Ceresoli
  0 siblings, 1 reply; 7+ messages in thread
From: Rosen Penev @ 2026-03-27  3:03 UTC (permalink / raw)
  To: linux-i2c
  Cc: Tomi Valkeinen, Luca Ceresoli, Wolfram Sang, Kees Cook,
	Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.

Add __counted_by to get extra runtime analysis.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/i2c/i2c-atr.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/drivers/i2c/i2c-atr.c b/drivers/i2c/i2c-atr.c
index f9fcb4793aaf..e6d2af659d81 100644
--- a/drivers/i2c/i2c-atr.c
+++ b/drivers/i2c/i2c-atr.c
@@ -49,8 +49,8 @@ struct i2c_atr_alias_pair {
  * @shared:   Indicates if this alias pool is shared by multiple channels
  *
  * @lock:     Lock protecting @aliases and @use_mask
- * @aliases:  Array of aliases, must hold exactly @size elements
  * @use_mask: Mask of used aliases
+ * @aliases:  Array of aliases, must hold exactly @size elements
  */
 struct i2c_atr_alias_pool {
 	size_t size;
@@ -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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] i2c: atr: use kzalloc_flex
  2026-03-27  3:03 [PATCH] i2c: atr: use kzalloc_flex Rosen Penev
@ 2026-03-27  8:12 ` Luca Ceresoli
  2026-03-27 18:35   ` Rosen Penev
  0 siblings, 1 reply; 7+ messages in thread
From: Luca Ceresoli @ 2026-03-27  8:12 UTC (permalink / raw)
  To: Rosen Penev, linux-i2c
  Cc: Tomi Valkeinen, Wolfram Sang, Kees Cook, Gustavo A. R. Silva,
	open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b,
	Cosmin Tanislav, Romain Gantois

Hello Rosen,

+Cc Cosmin, Romain

On Fri Mar 27, 2026 at 4:03 AM CET, Rosen Penev wrote:
> Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.
>
> Add __counted_by to get extra runtime analysis.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

This is the second version of the previous patch, so the subject line
should start with "[PATCH v2]". You may try using b4 to automate this, it
takes over this and many other boring-but-useful details.

Additionally I suspect b4 would have a more complete Cc list, including
recent contributors to this driver like Cosmin Tanislav and Romain Gantois
who might want to review this patch. I'm Cc-ing them this time.

> ---

Also a changelog would be nice after the '---' marker, to help reviewers,
and a link to previous versions. It took a moment to me to realize I had
already reviewed almost the exact same code.

Other than than the patch looks good:

Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>

--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] i2c: atr: use kzalloc_flex
  2026-03-27  8:12 ` Luca Ceresoli
@ 2026-03-27 18:35   ` Rosen Penev
  0 siblings, 0 replies; 7+ messages in thread
From: Rosen Penev @ 2026-03-27 18:35 UTC (permalink / raw)
  To: Luca Ceresoli
  Cc: linux-i2c, Tomi Valkeinen, Wolfram Sang, Kees Cook,
	Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b,
	Cosmin Tanislav, Romain Gantois

On Fri, Mar 27, 2026 at 1:12 AM Luca Ceresoli <luca.ceresoli@bootlin.com> wrote:
>
> Hello Rosen,
>
> +Cc Cosmin, Romain
>
> On Fri Mar 27, 2026 at 4:03 AM CET, Rosen Penev wrote:
> > Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.
> >
> > Add __counted_by to get extra runtime analysis.
> >
> > Signed-off-by: Rosen Penev <rosenp@gmail.com>
>
> This is the second version of the previous patch, so the subject line
> should start with "[PATCH v2]". You may try using b4 to automate this, it
> takes over this and many other boring-but-useful details.
Didn't realize that. I searched for attr vs atr in Sent...
>
> Additionally I suspect b4 would have a more complete Cc list, including
> recent contributors to this driver like Cosmin Tanislav and Romain Gantois
> who might want to review this patch. I'm Cc-ing them this time.
I use ./scripts/get_maintainer.pl --no-git-fallback as it was
requested elsewhere.
>
> > ---
>
> Also a changelog would be nice after the '---' marker, to help reviewers,
> and a link to previous versions. It took a moment to me to realize I had
> already reviewed almost the exact same code.
Will do.
>
> Other than than the patch looks good:
>
> Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
>
> --
> Luca Ceresoli, Bootlin
> Embedded Linux and Kernel engineering
> https://bootlin.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] i2c: atr: use kzalloc_flex
  2026-03-13  0:23 Rosen Penev
  2026-03-13  8:17 ` Luca Ceresoli
@ 2026-03-30  8:50 ` Romain Gantois
  1 sibling, 0 replies; 7+ messages in thread
From: Romain Gantois @ 2026-03-30  8:50 UTC (permalink / raw)
  To: linux-i2c, Rosen Penev
  Cc: Wolfram Sang, Tomi Valkeinen, Luca Ceresoli, Kees Cook,
	Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

[-- Attachment #1: Type: text/plain, Size: 2265 bytes --]

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>


[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-03-30  8:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-27  3:03 [PATCH] i2c: atr: use kzalloc_flex Rosen Penev
2026-03-27  8:12 ` Luca Ceresoli
2026-03-27 18:35   ` Rosen Penev
  -- strict thread matches above, loose matches on Subject: below --
2026-03-13  0:23 Rosen Penev
2026-03-13  8:17 ` Luca Ceresoli
2026-03-13 21:48   ` Rosen Penev
2026-03-30  8:50 ` Romain Gantois

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox