Skip to contentSkip to conversation

i2c: atr: use kzalloc_flex

PATCH Applied Rosen Penev 3 participants 6 messages Mar 27 – Apr 9

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: atr: use kzalloc_flex

Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.

Add __counted_by to get extra runtime analysis.

---
 drivers/i2c/i2c-atr.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/i2c/i2c-atr.c +4 -13
index f9fcb4793aaf..e6d2af659d81 100644
@@ -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);
 }
 
Signature · 1 line
2.53.0
reviewer Reviewed-by on v1 1/1
#1 lore raw

Hello Rosen,

+Cc Cosmin, Romain

Quoted 5 lines · Rosen Penev

Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.

Add __counted_by to get extra runtime analysis.

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.

Quoted 1 line

---

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:

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

#2 lore raw
Quoted 8 lines · Luca Ceresoli

Hello Rosen,

+Cc Cosmin, Romain

Quoted 2 lines · Rosen Penev

Convert kzalloc_obj + kcalloc to kzalloc_flex to save an allocation.

Quoted 7 lines
Quoted 3 lines

Add __counted_by to get extra runtime analysis.

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...

Quoted 4 lines

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.

Quoted 6 lines
Quoted 2 lines

---

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.

Quoted 9 lines

Other than than the patch looks good:

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

maintainer
#3 lore raw
In reply to Rosen Penev

Hi Rosen,

along with Luca's comments, I will ask you for one more change:

...

Quoted 5 lines
drivers/i2c/i2c-atr.c +1 -1
 	/* Protects aliases and use_mask */
 	spinlock_t lock;
-	u16 *aliases;
 	unsigned long *use_mask;
+	u16 aliases[] __counted_by(size);

...

drivers/i2c/i2c-atr.c static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_aliases, b · line 144

Quoted 6 lines
drivers/i2c/i2c-atr.c +1 -1
-	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;

this line is not needed anymore because kzalloc_flex will resolve to alias_pool size and will set it tu num_aliases.

We can remove it.

Thanks,
Andi

Quoted 6 lines
drivers/i2c/i2c-atr.c +0 -6
-	alias_pool->aliases = kcalloc(num_aliases, sizeof(*alias_pool->aliases), GFP_KERNEL);
-	if (!alias_pool->aliases) {
-		ret = -ENOMEM;
-		goto err_free_alias_pool;
-	}
-
#4 lore raw
Quoted 20 lines · Andi Shyti

Hi Rosen,

along with Luca's comments, I will ask you for one more change:

...

Quoted 5 lines
      /* Protects aliases and use_mask */
      spinlock_t lock;
drivers/i2c/i2c-atr.c +1 -1
-     u16 *aliases;
      unsigned long *use_mask;
+     u16 aliases[] __counted_by(size);

...

Quoted 5 lines
drivers/i2c/i2c-atr.c +1 -1
-     alias_pool = kzalloc_obj(*alias_pool);
+     alias_pool = kzalloc_flex(*alias_pool, aliases, num_aliases);
      if (!alias_pool)
              return ERR_PTR(-ENOMEM);
Quoted 4 lines
Quoted 1 line
      alias_pool->size = num_aliases;

this line is not needed anymore because kzalloc_flex will resolve to alias_pool size and will set it tu num_aliases.

I believe that only works with GCC15 and above.

Quoted 12 lines

We can remove it.

Thanks,
Andi

Quoted 6 lines
drivers/i2c/i2c-atr.c +0 -6
-     alias_pool->aliases = kcalloc(num_aliases, sizeof(*alias_pool->aliases), GFP_KERNEL);
-     if (!alias_pool->aliases) {
-             ret = -ENOMEM;
-             goto err_free_alias_pool;
-     }
-
maintainer Applied
#5 lore raw

Hi Rosen,

Quoted 10 lines
Quoted 9 lines

drivers/i2c/i2c-atr.c static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_aliases, b · line 144

Quoted 6 lines
drivers/i2c/i2c-atr.c +1 -1
-     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;

this line is not needed anymore because kzalloc_flex will resolve to alias_pool size and will set it tu num_aliases.

I believe that only works with GCC15 and above.

Yes, you're right, it works with clang.

Merged to i2c/i2c-host.

Thanks,
Andi